fix pr/45972
[official-gcc.git] / gcc / c-family / c-common.c
blobff3526b03c307508d7f413393b0e77992e8d77ae
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
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 "tm_p.h"
34 #include "obstack.h"
35 #include "cpplib.h"
36 #include "target.h"
37 #include "langhooks.h"
38 #include "tree-inline.h"
39 #include "toplev.h"
40 #include "diagnostic.h"
41 #include "tree-iterator.h"
42 #include "hashtab.h"
43 #include "tree-mudflap.h"
44 #include "opts.h"
45 #include "cgraph.h"
46 #include "target-def.h"
47 #include "libfuncs.h"
49 cpp_reader *parse_in; /* Declared in c-pragma.h. */
51 /* The following symbols are subsumed in the c_global_trees array, and
52 listed here individually for documentation purposes.
54 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
56 tree short_integer_type_node;
57 tree long_integer_type_node;
58 tree long_long_integer_type_node;
59 tree int128_integer_type_node;
61 tree short_unsigned_type_node;
62 tree long_unsigned_type_node;
63 tree long_long_unsigned_type_node;
64 tree int128_unsigned_type_node;
66 tree truthvalue_type_node;
67 tree truthvalue_false_node;
68 tree truthvalue_true_node;
70 tree ptrdiff_type_node;
72 tree unsigned_char_type_node;
73 tree signed_char_type_node;
74 tree wchar_type_node;
76 tree char16_type_node;
77 tree char32_type_node;
79 tree float_type_node;
80 tree double_type_node;
81 tree long_double_type_node;
83 tree complex_integer_type_node;
84 tree complex_float_type_node;
85 tree complex_double_type_node;
86 tree complex_long_double_type_node;
88 tree dfloat32_type_node;
89 tree dfloat64_type_node;
90 tree_dfloat128_type_node;
92 tree intQI_type_node;
93 tree intHI_type_node;
94 tree intSI_type_node;
95 tree intDI_type_node;
96 tree intTI_type_node;
98 tree unsigned_intQI_type_node;
99 tree unsigned_intHI_type_node;
100 tree unsigned_intSI_type_node;
101 tree unsigned_intDI_type_node;
102 tree unsigned_intTI_type_node;
104 tree widest_integer_literal_type_node;
105 tree widest_unsigned_literal_type_node;
107 Nodes for types `void *' and `const void *'.
109 tree ptr_type_node, const_ptr_type_node;
111 Nodes for types `char *' and `const char *'.
113 tree string_type_node, const_string_type_node;
115 Type `char[SOMENUMBER]'.
116 Used when an array of char is needed and the size is irrelevant.
118 tree char_array_type_node;
120 Type `int[SOMENUMBER]' or something like it.
121 Used when an array of int needed and the size is irrelevant.
123 tree int_array_type_node;
125 Type `wchar_t[SOMENUMBER]' or something like it.
126 Used when a wide string literal is created.
128 tree wchar_array_type_node;
130 Type `char16_t[SOMENUMBER]' or something like it.
131 Used when a UTF-16 string literal is created.
133 tree char16_array_type_node;
135 Type `char32_t[SOMENUMBER]' or something like it.
136 Used when a UTF-32 string literal is created.
138 tree char32_array_type_node;
140 Type `int ()' -- used for implicit declaration of functions.
142 tree default_function_type;
144 A VOID_TYPE node, packaged in a TREE_LIST.
146 tree void_list_node;
148 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
149 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
150 VAR_DECLS, but C++ does.)
152 tree function_name_decl_node;
153 tree pretty_function_name_decl_node;
154 tree c99_function_name_decl_node;
156 Stack of nested function name VAR_DECLs.
158 tree saved_function_name_decls;
162 tree c_global_trees[CTI_MAX];
164 /* Switches common to the C front ends. */
166 /* Nonzero means don't output line number information. */
168 char flag_no_line_commands;
170 /* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
173 char flag_no_output;
175 /* Nonzero means dump macros in some fashion. */
177 char flag_dump_macros;
179 /* Nonzero means pass #include lines through to the output. */
181 char flag_dump_includes;
183 /* Nonzero means process PCH files while preprocessing. */
185 bool flag_pch_preprocess;
187 /* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
190 const char *pch_file;
192 /* Nonzero if an ISO standard was selected. It rejects macros in the
193 user's namespace. */
194 int flag_iso;
196 /* Warn about #pragma directives that are not recognized. */
198 int warn_unknown_pragmas; /* Tri state variable. */
200 /* Warn about format/argument anomalies in calls to formatted I/O functions
201 (*printf, *scanf, strftime, strfmon, etc.). */
203 int warn_format;
205 /* C/ObjC language option variables. */
208 /* Nonzero means allow type mismatches in conditional expressions;
209 just make their values `void'. */
211 int flag_cond_mismatch;
213 /* Nonzero means enable C89 Amendment 1 features. */
215 int flag_isoc94;
217 /* Nonzero means use the ISO C99 (or C1X) dialect of C. */
219 int flag_isoc99;
221 /* Nonzero means use the ISO C1X dialect of C. */
223 int flag_isoc1x;
225 /* Nonzero means that we have builtin functions, and main is an int. */
227 int flag_hosted = 1;
230 /* ObjC language option variables. */
233 /* Tells the compiler that this is a special run. Do not perform any
234 compiling, instead we are to test some platform dependent features
235 and output a C header file with appropriate definitions. */
237 int print_struct_values;
239 /* Tells the compiler what is the constant string class for ObjC. */
241 const char *constant_string_class_name;
244 /* C++ language option variables. */
247 /* Nonzero means generate separate instantiation control files and
248 juggle them at link time. */
250 int flag_use_repository;
252 /* The C++ dialect being used. C++98 is the default. */
254 enum cxx_dialect cxx_dialect = cxx98;
256 /* Maximum template instantiation depth. This limit exists to limit the
257 time it takes to notice infinite template instantiations; the default
258 value of 1024 is likely to be in the next C++ standard. */
260 int max_tinst_depth = 1024;
264 /* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
266 tree *ridpointers;
268 tree (*make_fname_decl) (location_t, tree, int);
270 /* Nonzero means don't warn about problems that occur when the code is
271 executed. */
272 int c_inhibit_evaluation_warnings;
274 /* Whether lexing has been completed, so subsequent preprocessor
275 errors should use the compiler's input_location. */
276 bool done_lexing = false;
278 /* Information about how a function name is generated. */
279 struct fname_var_t
281 tree *const decl; /* pointer to the VAR_DECL. */
282 const unsigned rid; /* RID number for the identifier. */
283 const int pretty; /* How pretty is it? */
286 /* The three ways of getting then name of the current function. */
288 const struct fname_var_t fname_vars[] =
290 /* C99 compliant __func__, must be first. */
291 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
292 /* GCC __FUNCTION__ compliant. */
293 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
294 /* GCC __PRETTY_FUNCTION__ compliant. */
295 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
296 {NULL, 0, 0},
299 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
300 static tree check_case_value (tree);
301 static bool check_case_bounds (tree, tree, tree *, tree *);
303 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
304 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
305 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
306 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
312 static tree handle_always_inline_attribute (tree *, tree, tree, int,
313 bool *);
314 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
315 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
321 bool *);
322 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_transparent_union_attribute (tree *, tree, tree,
324 int, bool *);
325 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
331 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
332 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
335 static tree handle_visibility_attribute (tree *, tree, tree, int,
336 bool *);
337 static tree handle_tls_model_attribute (tree *, tree, tree, int,
338 bool *);
339 static tree handle_no_instrument_function_attribute (tree *, tree,
340 tree, int, bool *);
341 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
344 bool *);
345 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_deprecated_attribute (tree *, tree, tree, int,
348 bool *);
349 static tree handle_vector_size_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
355 bool *);
356 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
364 static void check_function_nonnull (tree, int, tree *);
365 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
366 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
367 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
368 static int resort_field_decl_cmp (const void *, const void *);
370 /* Reserved words. The third field is a mask: keywords are disabled
371 if they match the mask.
373 Masks for languages:
374 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
375 C --std=c99: D_CXXONLY | D_OBJC
376 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
377 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
378 C++ --std=c0x: D_CONLY | D_OBJC
379 ObjC++ is like C++ except that D_OBJC is not set
381 If -fno-asm is used, D_ASM is added to the mask. If
382 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
383 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
384 In C with -Wc++-compat, we warn if D_CXXWARN is set.
386 Note the complication of the D_CXX_OBJC keywords. These are
387 reserved words such as 'class'. In C++, 'class' is a reserved
388 word. In Objective-C++ it is too. In Objective-C, it is a
389 reserved word too, but only if it follows an '@' sign.
391 const struct c_common_resword c_common_reswords[] =
393 { "_Bool", RID_BOOL, D_CONLY },
394 { "_Complex", RID_COMPLEX, 0 },
395 { "_Imaginary", RID_IMAGINARY, D_CONLY },
396 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
397 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
398 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
399 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
400 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
401 { "_Sat", RID_SAT, D_CONLY | D_EXT },
402 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
403 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
404 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
405 { "__alignof", RID_ALIGNOF, 0 },
406 { "__alignof__", RID_ALIGNOF, 0 },
407 { "__asm", RID_ASM, 0 },
408 { "__asm__", RID_ASM, 0 },
409 { "__attribute", RID_ATTRIBUTE, 0 },
410 { "__attribute__", RID_ATTRIBUTE, 0 },
411 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
412 { "__builtin_offsetof", RID_OFFSETOF, 0 },
413 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
414 { "__builtin_va_arg", RID_VA_ARG, 0 },
415 { "__complex", RID_COMPLEX, 0 },
416 { "__complex__", RID_COMPLEX, 0 },
417 { "__const", RID_CONST, 0 },
418 { "__const__", RID_CONST, 0 },
419 { "__decltype", RID_DECLTYPE, D_CXXONLY },
420 { "__extension__", RID_EXTENSION, 0 },
421 { "__func__", RID_C99_FUNCTION_NAME, 0 },
422 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
423 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
424 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
425 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
426 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
427 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
428 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
429 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
430 { "__int128", RID_INT128, 0 },
431 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
432 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
433 { "__is_class", RID_IS_CLASS, D_CXXONLY },
434 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
435 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
436 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
437 { "__is_pod", RID_IS_POD, D_CXXONLY },
438 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
439 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
440 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
441 { "__is_union", RID_IS_UNION, D_CXXONLY },
442 { "__imag", RID_IMAGPART, 0 },
443 { "__imag__", RID_IMAGPART, 0 },
444 { "__inline", RID_INLINE, 0 },
445 { "__inline__", RID_INLINE, 0 },
446 { "__label__", RID_LABEL, 0 },
447 { "__null", RID_NULL, 0 },
448 { "__real", RID_REALPART, 0 },
449 { "__real__", RID_REALPART, 0 },
450 { "__restrict", RID_RESTRICT, 0 },
451 { "__restrict__", RID_RESTRICT, 0 },
452 { "__signed", RID_SIGNED, 0 },
453 { "__signed__", RID_SIGNED, 0 },
454 { "__thread", RID_THREAD, 0 },
455 { "__typeof", RID_TYPEOF, 0 },
456 { "__typeof__", RID_TYPEOF, 0 },
457 { "__volatile", RID_VOLATILE, 0 },
458 { "__volatile__", RID_VOLATILE, 0 },
459 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
460 { "asm", RID_ASM, D_ASM },
461 { "auto", RID_AUTO, 0 },
462 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
463 { "break", RID_BREAK, 0 },
464 { "case", RID_CASE, 0 },
465 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
466 { "char", RID_CHAR, 0 },
467 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
468 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
469 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
470 { "const", RID_CONST, 0 },
471 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
472 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
473 { "continue", RID_CONTINUE, 0 },
474 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
475 { "default", RID_DEFAULT, 0 },
476 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
477 { "do", RID_DO, 0 },
478 { "double", RID_DOUBLE, 0 },
479 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
480 { "else", RID_ELSE, 0 },
481 { "enum", RID_ENUM, 0 },
482 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
483 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
484 { "extern", RID_EXTERN, 0 },
485 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
486 { "float", RID_FLOAT, 0 },
487 { "for", RID_FOR, 0 },
488 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
489 { "goto", RID_GOTO, 0 },
490 { "if", RID_IF, 0 },
491 { "inline", RID_INLINE, D_EXT89 },
492 { "int", RID_INT, 0 },
493 { "long", RID_LONG, 0 },
494 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
495 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
496 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
497 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
498 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
499 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
500 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
501 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
502 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
503 { "register", RID_REGISTER, 0 },
504 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
505 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
506 { "return", RID_RETURN, 0 },
507 { "short", RID_SHORT, 0 },
508 { "signed", RID_SIGNED, 0 },
509 { "sizeof", RID_SIZEOF, 0 },
510 { "static", RID_STATIC, 0 },
511 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
512 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
513 { "struct", RID_STRUCT, 0 },
514 { "switch", RID_SWITCH, 0 },
515 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
516 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
517 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
518 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
519 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
520 { "typedef", RID_TYPEDEF, 0 },
521 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
522 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
523 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
524 { "union", RID_UNION, 0 },
525 { "unsigned", RID_UNSIGNED, 0 },
526 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
527 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
528 { "void", RID_VOID, 0 },
529 { "volatile", RID_VOLATILE, 0 },
530 { "wchar_t", RID_WCHAR, D_CXXONLY },
531 { "while", RID_WHILE, 0 },
532 /* These Objective-C keywords are recognized only immediately after
533 an '@'. */
534 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
535 { "defs", RID_AT_DEFS, D_OBJC },
536 { "encode", RID_AT_ENCODE, D_OBJC },
537 { "end", RID_AT_END, D_OBJC },
538 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
539 { "interface", RID_AT_INTERFACE, D_OBJC },
540 { "protocol", RID_AT_PROTOCOL, D_OBJC },
541 { "selector", RID_AT_SELECTOR, D_OBJC },
542 { "finally", RID_AT_FINALLY, D_OBJC },
543 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
544 { "optional", RID_AT_OPTIONAL, D_OBJC },
545 { "required", RID_AT_REQUIRED, D_OBJC },
546 /* These are recognized only in protocol-qualifier context
547 (see above) */
548 { "bycopy", RID_BYCOPY, D_OBJC },
549 { "byref", RID_BYREF, D_OBJC },
550 { "in", RID_IN, D_OBJC },
551 { "inout", RID_INOUT, D_OBJC },
552 { "oneway", RID_ONEWAY, D_OBJC },
553 { "out", RID_OUT, D_OBJC },
556 const unsigned int num_c_common_reswords =
557 sizeof c_common_reswords / sizeof (struct c_common_resword);
559 /* Table of machine-independent attributes common to all C-like languages. */
560 const struct attribute_spec c_common_attribute_table[] =
562 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
563 { "packed", 0, 0, false, false, false,
564 handle_packed_attribute },
565 { "nocommon", 0, 0, true, false, false,
566 handle_nocommon_attribute },
567 { "common", 0, 0, true, false, false,
568 handle_common_attribute },
569 /* FIXME: logically, noreturn attributes should be listed as
570 "false, true, true" and apply to function types. But implementing this
571 would require all the places in the compiler that use TREE_THIS_VOLATILE
572 on a decl to identify non-returning functions to be located and fixed
573 to check the function type instead. */
574 { "noreturn", 0, 0, true, false, false,
575 handle_noreturn_attribute },
576 { "volatile", 0, 0, true, false, false,
577 handle_noreturn_attribute },
578 { "noinline", 0, 0, true, false, false,
579 handle_noinline_attribute },
580 { "noclone", 0, 0, true, false, false,
581 handle_noclone_attribute },
582 { "leaf", 0, 0, true, false, false,
583 handle_leaf_attribute },
584 { "always_inline", 0, 0, true, false, false,
585 handle_always_inline_attribute },
586 { "gnu_inline", 0, 0, true, false, false,
587 handle_gnu_inline_attribute },
588 { "artificial", 0, 0, true, false, false,
589 handle_artificial_attribute },
590 { "flatten", 0, 0, true, false, false,
591 handle_flatten_attribute },
592 { "used", 0, 0, true, false, false,
593 handle_used_attribute },
594 { "unused", 0, 0, false, false, false,
595 handle_unused_attribute },
596 { "externally_visible", 0, 0, true, false, false,
597 handle_externally_visible_attribute },
598 /* The same comments as for noreturn attributes apply to const ones. */
599 { "const", 0, 0, true, false, false,
600 handle_const_attribute },
601 { "transparent_union", 0, 0, false, false, false,
602 handle_transparent_union_attribute },
603 { "constructor", 0, 1, true, false, false,
604 handle_constructor_attribute },
605 { "destructor", 0, 1, true, false, false,
606 handle_destructor_attribute },
607 { "mode", 1, 1, false, true, false,
608 handle_mode_attribute },
609 { "section", 1, 1, true, false, false,
610 handle_section_attribute },
611 { "aligned", 0, 1, false, false, false,
612 handle_aligned_attribute },
613 { "weak", 0, 0, true, false, false,
614 handle_weak_attribute },
615 { "ifunc", 1, 1, true, false, false,
616 handle_ifunc_attribute },
617 { "alias", 1, 1, true, false, false,
618 handle_alias_attribute },
619 { "weakref", 0, 1, true, false, false,
620 handle_weakref_attribute },
621 { "no_instrument_function", 0, 0, true, false, false,
622 handle_no_instrument_function_attribute },
623 { "malloc", 0, 0, true, false, false,
624 handle_malloc_attribute },
625 { "returns_twice", 0, 0, true, false, false,
626 handle_returns_twice_attribute },
627 { "no_stack_limit", 0, 0, true, false, false,
628 handle_no_limit_stack_attribute },
629 { "pure", 0, 0, true, false, false,
630 handle_pure_attribute },
631 /* For internal use (marking of builtins) only. The name contains space
632 to prevent its usage in source code. */
633 { "no vops", 0, 0, true, false, false,
634 handle_novops_attribute },
635 { "deprecated", 0, 1, false, false, false,
636 handle_deprecated_attribute },
637 { "vector_size", 1, 1, false, true, false,
638 handle_vector_size_attribute },
639 { "visibility", 1, 1, false, false, false,
640 handle_visibility_attribute },
641 { "tls_model", 1, 1, true, false, false,
642 handle_tls_model_attribute },
643 { "nonnull", 0, -1, false, true, true,
644 handle_nonnull_attribute },
645 { "nothrow", 0, 0, true, false, false,
646 handle_nothrow_attribute },
647 { "may_alias", 0, 0, false, true, false, NULL },
648 { "cleanup", 1, 1, true, false, false,
649 handle_cleanup_attribute },
650 { "warn_unused_result", 0, 0, false, true, true,
651 handle_warn_unused_result_attribute },
652 { "sentinel", 0, 1, false, true, true,
653 handle_sentinel_attribute },
654 /* For internal use (marking of builtins) only. The name contains space
655 to prevent its usage in source code. */
656 { "type generic", 0, 0, false, true, true,
657 handle_type_generic_attribute },
658 { "alloc_size", 1, 2, false, true, true,
659 handle_alloc_size_attribute },
660 { "cold", 0, 0, true, false, false,
661 handle_cold_attribute },
662 { "hot", 0, 0, true, false, false,
663 handle_hot_attribute },
664 { "warning", 1, 1, true, false, false,
665 handle_error_attribute },
666 { "error", 1, 1, true, false, false,
667 handle_error_attribute },
668 { "target", 1, -1, true, false, false,
669 handle_target_attribute },
670 { "optimize", 1, -1, true, false, false,
671 handle_optimize_attribute },
672 { "no_split_stack", 0, 0, true, false, false,
673 handle_no_split_stack_attribute },
674 /* For internal use (marking of builtins and runtime functions) only.
675 The name contains space to prevent its usage in source code. */
676 { "fn spec", 1, 1, false, true, true,
677 handle_fnspec_attribute },
678 { NULL, 0, 0, false, false, false, NULL }
681 /* Give the specifications for the format attributes, used by C and all
682 descendants. */
684 const struct attribute_spec c_common_format_attribute_table[] =
686 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
687 { "format", 3, 3, false, true, true,
688 handle_format_attribute },
689 { "format_arg", 1, 1, false, true, true,
690 handle_format_arg_attribute },
691 { NULL, 0, 0, false, false, false, NULL }
694 /* Return identifier for address space AS. */
696 const char *
697 c_addr_space_name (addr_space_t as)
699 int rid = RID_FIRST_ADDR_SPACE + as;
700 gcc_assert (ridpointers [rid]);
701 return IDENTIFIER_POINTER (ridpointers [rid]);
704 /* Push current bindings for the function name VAR_DECLS. */
706 void
707 start_fname_decls (void)
709 unsigned ix;
710 tree saved = NULL_TREE;
712 for (ix = 0; fname_vars[ix].decl; ix++)
714 tree decl = *fname_vars[ix].decl;
716 if (decl)
718 saved = tree_cons (decl, build_int_cst (NULL_TREE, ix), saved);
719 *fname_vars[ix].decl = NULL_TREE;
722 if (saved || saved_function_name_decls)
723 /* Normally they'll have been NULL, so only push if we've got a
724 stack, or they are non-NULL. */
725 saved_function_name_decls = tree_cons (saved, NULL_TREE,
726 saved_function_name_decls);
729 /* Finish up the current bindings, adding them into the current function's
730 statement tree. This must be done _before_ finish_stmt_tree is called.
731 If there is no current function, we must be at file scope and no statements
732 are involved. Pop the previous bindings. */
734 void
735 finish_fname_decls (void)
737 unsigned ix;
738 tree stmts = NULL_TREE;
739 tree stack = saved_function_name_decls;
741 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
742 append_to_statement_list (TREE_VALUE (stack), &stmts);
744 if (stmts)
746 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
748 if (TREE_CODE (*bodyp) == BIND_EXPR)
749 bodyp = &BIND_EXPR_BODY (*bodyp);
751 append_to_statement_list_force (*bodyp, &stmts);
752 *bodyp = stmts;
755 for (ix = 0; fname_vars[ix].decl; ix++)
756 *fname_vars[ix].decl = NULL_TREE;
758 if (stack)
760 /* We had saved values, restore them. */
761 tree saved;
763 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
765 tree decl = TREE_PURPOSE (saved);
766 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
768 *fname_vars[ix].decl = decl;
770 stack = TREE_CHAIN (stack);
772 saved_function_name_decls = stack;
775 /* Return the text name of the current function, suitably prettified
776 by PRETTY_P. Return string must be freed by caller. */
778 const char *
779 fname_as_string (int pretty_p)
781 const char *name = "top level";
782 char *namep;
783 int vrb = 2, len;
784 cpp_string cstr = { 0, 0 }, strname;
786 if (!pretty_p)
788 name = "";
789 vrb = 0;
792 if (current_function_decl)
793 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
795 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
797 namep = XNEWVEC (char, len);
798 snprintf (namep, len, "\"%s\"", name);
799 strname.text = (unsigned char *) namep;
800 strname.len = len - 1;
802 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
804 XDELETEVEC (namep);
805 return (const char *) cstr.text;
808 return namep;
811 /* Return the VAR_DECL for a const char array naming the current
812 function. If the VAR_DECL has not yet been created, create it
813 now. RID indicates how it should be formatted and IDENTIFIER_NODE
814 ID is its name (unfortunately C and C++ hold the RID values of
815 keywords in different places, so we can't derive RID from ID in
816 this language independent code. LOC is the location of the
817 function. */
819 tree
820 fname_decl (location_t loc, unsigned int rid, tree id)
822 unsigned ix;
823 tree decl = NULL_TREE;
825 for (ix = 0; fname_vars[ix].decl; ix++)
826 if (fname_vars[ix].rid == rid)
827 break;
829 decl = *fname_vars[ix].decl;
830 if (!decl)
832 /* If a tree is built here, it would normally have the lineno of
833 the current statement. Later this tree will be moved to the
834 beginning of the function and this line number will be wrong.
835 To avoid this problem set the lineno to 0 here; that prevents
836 it from appearing in the RTL. */
837 tree stmts;
838 location_t saved_location = input_location;
839 input_location = UNKNOWN_LOCATION;
841 stmts = push_stmt_list ();
842 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
843 stmts = pop_stmt_list (stmts);
844 if (!IS_EMPTY_STMT (stmts))
845 saved_function_name_decls
846 = tree_cons (decl, stmts, saved_function_name_decls);
847 *fname_vars[ix].decl = decl;
848 input_location = saved_location;
850 if (!ix && !current_function_decl)
851 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
853 return decl;
856 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
858 tree
859 fix_string_type (tree value)
861 int length = TREE_STRING_LENGTH (value);
862 int nchars;
863 tree e_type, i_type, a_type;
865 /* Compute the number of elements, for the array type. */
866 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
868 nchars = length;
869 e_type = char_type_node;
871 else if (TREE_TYPE (value) == char16_array_type_node)
873 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
874 e_type = char16_type_node;
876 else if (TREE_TYPE (value) == char32_array_type_node)
878 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
879 e_type = char32_type_node;
881 else
883 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
884 e_type = wchar_type_node;
887 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
888 limit in C++98 Annex B is very large (65536) and is not normative,
889 so we do not diagnose it (warn_overlength_strings is forced off
890 in c_common_post_options). */
891 if (warn_overlength_strings)
893 const int nchars_max = flag_isoc99 ? 4095 : 509;
894 const int relevant_std = flag_isoc99 ? 99 : 90;
895 if (nchars - 1 > nchars_max)
896 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
897 separate the %d from the 'C'. 'ISO' should not be
898 translated, but it may be moved after 'C%d' in languages
899 where modifiers follow nouns. */
900 pedwarn (input_location, OPT_Woverlength_strings,
901 "string length %qd is greater than the length %qd "
902 "ISO C%d compilers are required to support",
903 nchars - 1, nchars_max, relevant_std);
906 /* Create the array type for the string constant. The ISO C++
907 standard says that a string literal has type `const char[N]' or
908 `const wchar_t[N]'. We use the same logic when invoked as a C
909 front-end with -Wwrite-strings.
910 ??? We should change the type of an expression depending on the
911 state of a warning flag. We should just be warning -- see how
912 this is handled in the C++ front-end for the deprecated implicit
913 conversion from string literals to `char*' or `wchar_t*'.
915 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
916 array type being the unqualified version of that type.
917 Therefore, if we are constructing an array of const char, we must
918 construct the matching unqualified array type first. The C front
919 end does not require this, but it does no harm, so we do it
920 unconditionally. */
921 i_type = build_index_type (build_int_cst (NULL_TREE, nchars - 1));
922 a_type = build_array_type (e_type, i_type);
923 if (c_dialect_cxx() || warn_write_strings)
924 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
926 TREE_TYPE (value) = a_type;
927 TREE_CONSTANT (value) = 1;
928 TREE_READONLY (value) = 1;
929 TREE_STATIC (value) = 1;
930 return value;
933 /* Fully fold EXPR, an expression that was not folded (beyond integer
934 constant expressions and null pointer constants) when being built
935 up. If IN_INIT, this is in a static initializer and certain
936 changes are made to the folding done. Clear *MAYBE_CONST if
937 MAYBE_CONST is not NULL and EXPR is definitely not a constant
938 expression because it contains an evaluated operator (in C99) or an
939 operator outside of sizeof returning an integer constant (in C90)
940 not permitted in constant expressions, or because it contains an
941 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
942 set to true by callers before calling this function.) Return the
943 folded expression. Function arguments have already been folded
944 before calling this function, as have the contents of SAVE_EXPR,
945 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
946 C_MAYBE_CONST_EXPR. */
948 tree
949 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
951 tree ret;
952 tree eptype = NULL_TREE;
953 bool dummy = true;
954 bool maybe_const_itself = true;
955 location_t loc = EXPR_LOCATION (expr);
957 /* This function is not relevant to C++ because C++ folds while
958 parsing, and may need changes to be correct for C++ when C++
959 stops folding while parsing. */
960 if (c_dialect_cxx ())
961 gcc_unreachable ();
963 if (!maybe_const)
964 maybe_const = &dummy;
965 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
967 eptype = TREE_TYPE (expr);
968 expr = TREE_OPERAND (expr, 0);
970 ret = c_fully_fold_internal (expr, in_init, maybe_const,
971 &maybe_const_itself);
972 if (eptype)
973 ret = fold_convert_loc (loc, eptype, ret);
974 *maybe_const &= maybe_const_itself;
975 return ret;
978 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
979 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
980 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
981 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
982 both evaluated and unevaluated subexpressions while
983 *MAYBE_CONST_ITSELF is carried from only evaluated
984 subexpressions). */
986 static tree
987 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
988 bool *maybe_const_itself)
990 tree ret = expr;
991 enum tree_code code = TREE_CODE (expr);
992 enum tree_code_class kind = TREE_CODE_CLASS (code);
993 location_t loc = EXPR_LOCATION (expr);
994 tree op0, op1, op2, op3;
995 tree orig_op0, orig_op1, orig_op2;
996 bool op0_const = true, op1_const = true, op2_const = true;
997 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
998 bool nowarning = TREE_NO_WARNING (expr);
999 int unused_p;
1001 /* This function is not relevant to C++ because C++ folds while
1002 parsing, and may need changes to be correct for C++ when C++
1003 stops folding while parsing. */
1004 if (c_dialect_cxx ())
1005 gcc_unreachable ();
1007 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1008 anything else not counted as an expression cannot usefully be
1009 folded further at this point. */
1010 if (!IS_EXPR_CODE_CLASS (kind)
1011 || kind == tcc_statement
1012 || code == SAVE_EXPR)
1013 return expr;
1015 /* Operands of variable-length expressions (function calls) have
1016 already been folded, as have __builtin_* function calls, and such
1017 expressions cannot occur in constant expressions. */
1018 if (kind == tcc_vl_exp)
1020 *maybe_const_operands = false;
1021 ret = fold (expr);
1022 goto out;
1025 if (code == C_MAYBE_CONST_EXPR)
1027 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1028 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1029 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1030 *maybe_const_operands = false;
1031 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1032 *maybe_const_itself = false;
1033 if (pre && !in_init)
1034 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1035 else
1036 ret = inner;
1037 goto out;
1040 /* Assignment, increment, decrement, function call and comma
1041 operators, and statement expressions, cannot occur in constant
1042 expressions if evaluated / outside of sizeof. (Function calls
1043 were handled above, though VA_ARG_EXPR is treated like a function
1044 call here, and statement expressions are handled through
1045 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1046 switch (code)
1048 case MODIFY_EXPR:
1049 case PREDECREMENT_EXPR:
1050 case PREINCREMENT_EXPR:
1051 case POSTDECREMENT_EXPR:
1052 case POSTINCREMENT_EXPR:
1053 case COMPOUND_EXPR:
1054 *maybe_const_operands = false;
1055 break;
1057 case VA_ARG_EXPR:
1058 case TARGET_EXPR:
1059 case BIND_EXPR:
1060 case OBJ_TYPE_REF:
1061 *maybe_const_operands = false;
1062 ret = fold (expr);
1063 goto out;
1065 default:
1066 break;
1069 /* Fold individual tree codes as appropriate. */
1070 switch (code)
1072 case COMPOUND_LITERAL_EXPR:
1073 /* Any non-constancy will have been marked in a containing
1074 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1075 goto out;
1077 case COMPONENT_REF:
1078 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1079 op1 = TREE_OPERAND (expr, 1);
1080 op2 = TREE_OPERAND (expr, 2);
1081 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1082 maybe_const_itself);
1083 STRIP_TYPE_NOPS (op0);
1084 if (op0 != orig_op0)
1085 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1086 if (ret != expr)
1088 TREE_READONLY (ret) = TREE_READONLY (expr);
1089 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1091 goto out;
1093 case ARRAY_REF:
1094 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1095 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1096 op2 = TREE_OPERAND (expr, 2);
1097 op3 = TREE_OPERAND (expr, 3);
1098 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1099 maybe_const_itself);
1100 STRIP_TYPE_NOPS (op0);
1101 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1102 maybe_const_itself);
1103 STRIP_TYPE_NOPS (op1);
1104 op1 = decl_constant_value_for_optimization (op1);
1105 if (op0 != orig_op0 || op1 != orig_op1)
1106 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1107 if (ret != expr)
1109 TREE_READONLY (ret) = TREE_READONLY (expr);
1110 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1111 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1113 ret = fold (ret);
1114 goto out;
1116 case COMPOUND_EXPR:
1117 case MODIFY_EXPR:
1118 case PREDECREMENT_EXPR:
1119 case PREINCREMENT_EXPR:
1120 case POSTDECREMENT_EXPR:
1121 case POSTINCREMENT_EXPR:
1122 case PLUS_EXPR:
1123 case MINUS_EXPR:
1124 case MULT_EXPR:
1125 case POINTER_PLUS_EXPR:
1126 case TRUNC_DIV_EXPR:
1127 case CEIL_DIV_EXPR:
1128 case FLOOR_DIV_EXPR:
1129 case TRUNC_MOD_EXPR:
1130 case RDIV_EXPR:
1131 case EXACT_DIV_EXPR:
1132 case LSHIFT_EXPR:
1133 case RSHIFT_EXPR:
1134 case BIT_IOR_EXPR:
1135 case BIT_XOR_EXPR:
1136 case BIT_AND_EXPR:
1137 case LT_EXPR:
1138 case LE_EXPR:
1139 case GT_EXPR:
1140 case GE_EXPR:
1141 case EQ_EXPR:
1142 case NE_EXPR:
1143 case COMPLEX_EXPR:
1144 case TRUTH_AND_EXPR:
1145 case TRUTH_OR_EXPR:
1146 case TRUTH_XOR_EXPR:
1147 case UNORDERED_EXPR:
1148 case ORDERED_EXPR:
1149 case UNLT_EXPR:
1150 case UNLE_EXPR:
1151 case UNGT_EXPR:
1152 case UNGE_EXPR:
1153 case UNEQ_EXPR:
1154 /* Binary operations evaluating both arguments (increment and
1155 decrement are binary internally in GCC). */
1156 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1157 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1158 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1159 maybe_const_itself);
1160 STRIP_TYPE_NOPS (op0);
1161 if (code != MODIFY_EXPR
1162 && code != PREDECREMENT_EXPR
1163 && code != PREINCREMENT_EXPR
1164 && code != POSTDECREMENT_EXPR
1165 && code != POSTINCREMENT_EXPR)
1166 op0 = decl_constant_value_for_optimization (op0);
1167 /* The RHS of a MODIFY_EXPR was fully folded when building that
1168 expression for the sake of conversion warnings. */
1169 if (code != MODIFY_EXPR)
1170 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1171 maybe_const_itself);
1172 STRIP_TYPE_NOPS (op1);
1173 op1 = decl_constant_value_for_optimization (op1);
1174 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1175 ret = in_init
1176 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1177 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1178 else
1179 ret = fold (expr);
1180 if (TREE_OVERFLOW_P (ret)
1181 && !TREE_OVERFLOW_P (op0)
1182 && !TREE_OVERFLOW_P (op1))
1183 overflow_warning (EXPR_LOCATION (expr), ret);
1184 goto out;
1186 case INDIRECT_REF:
1187 case FIX_TRUNC_EXPR:
1188 case FLOAT_EXPR:
1189 CASE_CONVERT:
1190 case NON_LVALUE_EXPR:
1191 case NEGATE_EXPR:
1192 case BIT_NOT_EXPR:
1193 case TRUTH_NOT_EXPR:
1194 case ADDR_EXPR:
1195 case CONJ_EXPR:
1196 case REALPART_EXPR:
1197 case IMAGPART_EXPR:
1198 /* Unary operations. */
1199 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1200 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1201 maybe_const_itself);
1202 STRIP_TYPE_NOPS (op0);
1203 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1204 op0 = decl_constant_value_for_optimization (op0);
1205 if (op0 != orig_op0 || in_init)
1206 ret = in_init
1207 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1208 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1209 else
1210 ret = fold (expr);
1211 if (code == INDIRECT_REF
1212 && ret != expr
1213 && TREE_CODE (ret) == INDIRECT_REF)
1215 TREE_READONLY (ret) = TREE_READONLY (expr);
1216 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1217 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1219 switch (code)
1221 case FIX_TRUNC_EXPR:
1222 case FLOAT_EXPR:
1223 CASE_CONVERT:
1224 /* Don't warn about explicit conversions. We will already
1225 have warned about suspect implicit conversions. */
1226 break;
1228 default:
1229 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1230 overflow_warning (EXPR_LOCATION (expr), ret);
1231 break;
1233 goto out;
1235 case TRUTH_ANDIF_EXPR:
1236 case TRUTH_ORIF_EXPR:
1237 /* Binary operations not necessarily evaluating both
1238 arguments. */
1239 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1240 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1241 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1242 STRIP_TYPE_NOPS (op0);
1244 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1245 ? truthvalue_false_node
1246 : truthvalue_true_node));
1247 c_inhibit_evaluation_warnings += unused_p;
1248 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1249 STRIP_TYPE_NOPS (op1);
1250 c_inhibit_evaluation_warnings -= unused_p;
1252 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1253 ret = in_init
1254 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1255 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1256 else
1257 ret = fold (expr);
1258 *maybe_const_operands &= op0_const;
1259 *maybe_const_itself &= op0_const_self;
1260 if (!(flag_isoc99
1261 && op0_const
1262 && op0_const_self
1263 && (code == TRUTH_ANDIF_EXPR
1264 ? op0 == truthvalue_false_node
1265 : op0 == truthvalue_true_node)))
1266 *maybe_const_operands &= op1_const;
1267 if (!(op0_const
1268 && op0_const_self
1269 && (code == TRUTH_ANDIF_EXPR
1270 ? op0 == truthvalue_false_node
1271 : op0 == truthvalue_true_node)))
1272 *maybe_const_itself &= op1_const_self;
1273 goto out;
1275 case COND_EXPR:
1276 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1277 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1278 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1279 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1281 STRIP_TYPE_NOPS (op0);
1282 c_inhibit_evaluation_warnings += (op0 == truthvalue_false_node);
1283 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1284 STRIP_TYPE_NOPS (op1);
1285 c_inhibit_evaluation_warnings -= (op0 == truthvalue_false_node);
1287 c_inhibit_evaluation_warnings += (op0 == truthvalue_true_node);
1288 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1289 STRIP_TYPE_NOPS (op2);
1290 c_inhibit_evaluation_warnings -= (op0 == truthvalue_true_node);
1292 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1293 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1294 else
1295 ret = fold (expr);
1296 *maybe_const_operands &= op0_const;
1297 *maybe_const_itself &= op0_const_self;
1298 if (!(flag_isoc99
1299 && op0_const
1300 && op0_const_self
1301 && op0 == truthvalue_false_node))
1302 *maybe_const_operands &= op1_const;
1303 if (!(op0_const
1304 && op0_const_self
1305 && op0 == truthvalue_false_node))
1306 *maybe_const_itself &= op1_const_self;
1307 if (!(flag_isoc99
1308 && op0_const
1309 && op0_const_self
1310 && op0 == truthvalue_true_node))
1311 *maybe_const_operands &= op2_const;
1312 if (!(op0_const
1313 && op0_const_self
1314 && op0 == truthvalue_true_node))
1315 *maybe_const_itself &= op2_const_self;
1316 goto out;
1318 case EXCESS_PRECISION_EXPR:
1319 /* Each case where an operand with excess precision may be
1320 encountered must remove the EXCESS_PRECISION_EXPR around
1321 inner operands and possibly put one around the whole
1322 expression or possibly convert to the semantic type (which
1323 c_fully_fold does); we cannot tell at this stage which is
1324 appropriate in any particular case. */
1325 gcc_unreachable ();
1327 default:
1328 /* Various codes may appear through folding built-in functions
1329 and their arguments. */
1330 goto out;
1333 out:
1334 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1335 have been done by this point, so remove them again. */
1336 nowarning |= TREE_NO_WARNING (ret);
1337 STRIP_TYPE_NOPS (ret);
1338 if (nowarning && !TREE_NO_WARNING (ret))
1340 if (!CAN_HAVE_LOCATION_P (ret))
1341 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1342 TREE_NO_WARNING (ret) = 1;
1344 if (ret != expr)
1345 protected_set_expr_location (ret, loc);
1346 return ret;
1349 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1350 return EXP. Otherwise, return either EXP or its known constant
1351 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1352 Is the BLKmode test appropriate? */
1354 tree
1355 decl_constant_value_for_optimization (tree exp)
1357 tree ret;
1359 /* This function is only used by C, for c_fully_fold and other
1360 optimization, and may not be correct for C++. */
1361 if (c_dialect_cxx ())
1362 gcc_unreachable ();
1364 if (!optimize
1365 || TREE_CODE (exp) != VAR_DECL
1366 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1367 || DECL_MODE (exp) == BLKmode)
1368 return exp;
1370 ret = decl_constant_value (exp);
1371 /* Avoid unwanted tree sharing between the initializer and current
1372 function's body where the tree can be modified e.g. by the
1373 gimplifier. */
1374 if (ret != exp && TREE_STATIC (exp))
1375 ret = unshare_expr (ret);
1376 return ret;
1379 /* Print a warning if a constant expression had overflow in folding.
1380 Invoke this function on every expression that the language
1381 requires to be a constant expression.
1382 Note the ANSI C standard says it is erroneous for a
1383 constant expression to overflow. */
1385 void
1386 constant_expression_warning (tree value)
1388 if (warn_overflow && pedantic
1389 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1390 || TREE_CODE (value) == FIXED_CST
1391 || TREE_CODE (value) == VECTOR_CST
1392 || TREE_CODE (value) == COMPLEX_CST)
1393 && TREE_OVERFLOW (value))
1394 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1397 /* The same as above but print an unconditional error. */
1398 void
1399 constant_expression_error (tree value)
1401 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1402 || TREE_CODE (value) == FIXED_CST
1403 || TREE_CODE (value) == VECTOR_CST
1404 || TREE_CODE (value) == COMPLEX_CST)
1405 && TREE_OVERFLOW (value))
1406 error ("overflow in constant expression");
1409 /* Print a warning if an expression had overflow in folding and its
1410 operands hadn't.
1412 Invoke this function on every expression that
1413 (1) appears in the source code, and
1414 (2) is a constant expression that overflowed, and
1415 (3) is not already checked by convert_and_check;
1416 however, do not invoke this function on operands of explicit casts
1417 or when the expression is the result of an operator and any operand
1418 already overflowed. */
1420 void
1421 overflow_warning (location_t loc, tree value)
1423 if (c_inhibit_evaluation_warnings != 0)
1424 return;
1426 switch (TREE_CODE (value))
1428 case INTEGER_CST:
1429 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1430 break;
1432 case REAL_CST:
1433 warning_at (loc, OPT_Woverflow,
1434 "floating point overflow in expression");
1435 break;
1437 case FIXED_CST:
1438 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1439 break;
1441 case VECTOR_CST:
1442 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1443 break;
1445 case COMPLEX_CST:
1446 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1447 warning_at (loc, OPT_Woverflow,
1448 "complex integer overflow in expression");
1449 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1450 warning_at (loc, OPT_Woverflow,
1451 "complex floating point overflow in expression");
1452 break;
1454 default:
1455 break;
1459 /* Warn about uses of logical || / && operator in a context where it
1460 is likely that the bitwise equivalent was intended by the
1461 programmer. We have seen an expression in which CODE is a binary
1462 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1463 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1464 void
1465 warn_logical_operator (location_t location, enum tree_code code, tree type,
1466 enum tree_code code_left, tree op_left,
1467 enum tree_code ARG_UNUSED (code_right), tree op_right)
1469 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1470 int in0_p, in1_p, in_p;
1471 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1472 bool strict_overflow_p = false;
1474 if (code != TRUTH_ANDIF_EXPR
1475 && code != TRUTH_AND_EXPR
1476 && code != TRUTH_ORIF_EXPR
1477 && code != TRUTH_OR_EXPR)
1478 return;
1480 /* Warn if &&/|| are being used in a context where it is
1481 likely that the bitwise equivalent was intended by the
1482 programmer. That is, an expression such as op && MASK
1483 where op should not be any boolean expression, nor a
1484 constant, and mask seems to be a non-boolean integer constant. */
1485 if (!truth_value_p (code_left)
1486 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1487 && !CONSTANT_CLASS_P (op_left)
1488 && !TREE_NO_WARNING (op_left)
1489 && TREE_CODE (op_right) == INTEGER_CST
1490 && !integer_zerop (op_right)
1491 && !integer_onep (op_right))
1493 if (or_op)
1494 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1495 " applied to non-boolean constant");
1496 else
1497 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1498 " applied to non-boolean constant");
1499 TREE_NO_WARNING (op_left) = true;
1500 return;
1503 /* We do not warn for constants because they are typical of macro
1504 expansions that test for features. */
1505 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1506 return;
1508 /* This warning only makes sense with logical operands. */
1509 if (!(truth_value_p (TREE_CODE (op_left))
1510 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1511 || !(truth_value_p (TREE_CODE (op_right))
1512 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1513 return;
1515 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1516 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1518 if (lhs && TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1519 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1521 if (rhs && TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1522 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1524 /* If this is an OR operation, invert both sides; we will invert
1525 again at the end. */
1526 if (or_op)
1527 in0_p = !in0_p, in1_p = !in1_p;
1529 /* If both expressions are the same, if we can merge the ranges, and we
1530 can build the range test, return it or it inverted. */
1531 if (lhs && rhs && operand_equal_p (lhs, rhs, 0)
1532 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1533 in1_p, low1, high1)
1534 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1535 type, lhs, in_p, low, high)))
1537 if (TREE_CODE (tem) != INTEGER_CST)
1538 return;
1540 if (or_op)
1541 warning_at (location, OPT_Wlogical_op,
1542 "logical %<or%> "
1543 "of collectively exhaustive tests is always true");
1544 else
1545 warning_at (location, OPT_Wlogical_op,
1546 "logical %<and%> "
1547 "of mutually exclusive tests is always false");
1552 /* Print a warning about casts that might indicate violation
1553 of strict aliasing rules if -Wstrict-aliasing is used and
1554 strict aliasing mode is in effect. OTYPE is the original
1555 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1557 bool
1558 strict_aliasing_warning (tree otype, tree type, tree expr)
1560 /* Strip pointer conversion chains and get to the correct original type. */
1561 STRIP_NOPS (expr);
1562 otype = TREE_TYPE (expr);
1564 if (!(flag_strict_aliasing
1565 && POINTER_TYPE_P (type)
1566 && POINTER_TYPE_P (otype)
1567 && !VOID_TYPE_P (TREE_TYPE (type)))
1568 /* If the type we are casting to is a ref-all pointer
1569 dereferencing it is always valid. */
1570 || TYPE_REF_CAN_ALIAS_ALL (type))
1571 return false;
1573 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1574 && (DECL_P (TREE_OPERAND (expr, 0))
1575 || handled_component_p (TREE_OPERAND (expr, 0))))
1577 /* Casting the address of an object to non void pointer. Warn
1578 if the cast breaks type based aliasing. */
1579 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1581 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1582 "might break strict-aliasing rules");
1583 return true;
1585 else
1587 /* warn_strict_aliasing >= 3. This includes the default (3).
1588 Only warn if the cast is dereferenced immediately. */
1589 alias_set_type set1 =
1590 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1591 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1593 if (set1 != set2 && set2 != 0
1594 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1596 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1597 "pointer will break strict-aliasing rules");
1598 return true;
1600 else if (warn_strict_aliasing == 2
1601 && !alias_sets_must_conflict_p (set1, set2))
1603 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1604 "pointer might break strict-aliasing rules");
1605 return true;
1609 else
1610 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1612 /* At this level, warn for any conversions, even if an address is
1613 not taken in the same statement. This will likely produce many
1614 false positives, but could be useful to pinpoint problems that
1615 are not revealed at higher levels. */
1616 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1617 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1618 if (!COMPLETE_TYPE_P (type)
1619 || !alias_sets_must_conflict_p (set1, set2))
1621 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1622 "pointer might break strict-aliasing rules");
1623 return true;
1627 return false;
1630 /* Warn for unlikely, improbable, or stupid DECL declarations
1631 of `main'. */
1633 void
1634 check_main_parameter_types (tree decl)
1636 tree args;
1637 int argct = 0;
1639 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl)); args;
1640 args = TREE_CHAIN (args))
1642 tree type = args ? TREE_VALUE (args) : 0;
1644 if (type == void_type_node || type == error_mark_node )
1645 break;
1647 ++argct;
1648 switch (argct)
1650 case 1:
1651 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1652 pedwarn (input_location, OPT_Wmain, "first argument of %q+D should be %<int%>",
1653 decl);
1654 break;
1656 case 2:
1657 if (TREE_CODE (type) != POINTER_TYPE
1658 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1659 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1660 != char_type_node))
1661 pedwarn (input_location, OPT_Wmain, "second argument of %q+D should be %<char **%>",
1662 decl);
1663 break;
1665 case 3:
1666 if (TREE_CODE (type) != POINTER_TYPE
1667 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1668 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1669 != char_type_node))
1670 pedwarn (input_location, OPT_Wmain, "third argument of %q+D should probably be "
1671 "%<char **%>", decl);
1672 break;
1676 /* It is intentional that this message does not mention the third
1677 argument because it's only mentioned in an appendix of the
1678 standard. */
1679 if (argct > 0 && (argct < 2 || argct > 3))
1680 pedwarn (input_location, OPT_Wmain, "%q+D takes only zero or two arguments", decl);
1683 /* True if pointers to distinct types T1 and T2 can be converted to
1684 each other without an explicit cast. Only returns true for opaque
1685 vector types. */
1686 bool
1687 vector_targets_convertible_p (const_tree t1, const_tree t2)
1689 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
1690 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1691 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1692 return true;
1694 return false;
1697 /* True if vector types T1 and T2 can be converted to each other
1698 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1699 can only be converted with -flax-vector-conversions yet that is not
1700 in effect, emit a note telling the user about that option if such
1701 a note has not previously been emitted. */
1702 bool
1703 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1705 static bool emitted_lax_note = false;
1706 bool convertible_lax;
1708 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1709 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1710 return true;
1712 convertible_lax =
1713 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1714 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
1715 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1716 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1717 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1719 if (!convertible_lax || flag_lax_vector_conversions)
1720 return convertible_lax;
1722 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1723 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1724 return true;
1726 if (emit_lax_note && !emitted_lax_note)
1728 emitted_lax_note = true;
1729 inform (input_location, "use -flax-vector-conversions to permit "
1730 "conversions between vectors with differing "
1731 "element types or numbers of subparts");
1734 return false;
1737 /* This is a helper function of build_binary_op.
1739 For certain operations if both args were extended from the same
1740 smaller type, do the arithmetic in that type and then extend.
1742 BITWISE indicates a bitwise operation.
1743 For them, this optimization is safe only if
1744 both args are zero-extended or both are sign-extended.
1745 Otherwise, we might change the result.
1746 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1747 but calculated in (unsigned short) it would be (unsigned short)-1.
1749 tree shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1751 int unsigned0, unsigned1;
1752 tree arg0, arg1;
1753 int uns;
1754 tree type;
1756 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1757 excessive narrowing when we call get_narrower below. For
1758 example, suppose that OP0 is of unsigned int extended
1759 from signed char and that RESULT_TYPE is long long int.
1760 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1761 like
1763 (long long int) (unsigned int) signed_char
1765 which get_narrower would narrow down to
1767 (unsigned int) signed char
1769 If we do not cast OP0 first, get_narrower would return
1770 signed_char, which is inconsistent with the case of the
1771 explicit cast. */
1772 op0 = convert (result_type, op0);
1773 op1 = convert (result_type, op1);
1775 arg0 = get_narrower (op0, &unsigned0);
1776 arg1 = get_narrower (op1, &unsigned1);
1778 /* UNS is 1 if the operation to be done is an unsigned one. */
1779 uns = TYPE_UNSIGNED (result_type);
1781 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1782 but it *requires* conversion to FINAL_TYPE. */
1784 if ((TYPE_PRECISION (TREE_TYPE (op0))
1785 == TYPE_PRECISION (TREE_TYPE (arg0)))
1786 && TREE_TYPE (op0) != result_type)
1787 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1788 if ((TYPE_PRECISION (TREE_TYPE (op1))
1789 == TYPE_PRECISION (TREE_TYPE (arg1)))
1790 && TREE_TYPE (op1) != result_type)
1791 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1793 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1795 /* For bitwise operations, signedness of nominal type
1796 does not matter. Consider only how operands were extended. */
1797 if (bitwise)
1798 uns = unsigned0;
1800 /* Note that in all three cases below we refrain from optimizing
1801 an unsigned operation on sign-extended args.
1802 That would not be valid. */
1804 /* Both args variable: if both extended in same way
1805 from same width, do it in that width.
1806 Do it unsigned if args were zero-extended. */
1807 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1808 < TYPE_PRECISION (result_type))
1809 && (TYPE_PRECISION (TREE_TYPE (arg1))
1810 == TYPE_PRECISION (TREE_TYPE (arg0)))
1811 && unsigned0 == unsigned1
1812 && (unsigned0 || !uns))
1813 return c_common_signed_or_unsigned_type
1814 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1816 else if (TREE_CODE (arg0) == INTEGER_CST
1817 && (unsigned1 || !uns)
1818 && (TYPE_PRECISION (TREE_TYPE (arg1))
1819 < TYPE_PRECISION (result_type))
1820 && (type
1821 = c_common_signed_or_unsigned_type (unsigned1,
1822 TREE_TYPE (arg1)))
1823 && !POINTER_TYPE_P (type)
1824 && int_fits_type_p (arg0, type))
1825 return type;
1827 else if (TREE_CODE (arg1) == INTEGER_CST
1828 && (unsigned0 || !uns)
1829 && (TYPE_PRECISION (TREE_TYPE (arg0))
1830 < TYPE_PRECISION (result_type))
1831 && (type
1832 = c_common_signed_or_unsigned_type (unsigned0,
1833 TREE_TYPE (arg0)))
1834 && !POINTER_TYPE_P (type)
1835 && int_fits_type_p (arg1, type))
1836 return type;
1838 return result_type;
1841 /* Warns if the conversion of EXPR to TYPE may alter a value.
1842 This is a helper function for warnings_for_convert_and_check. */
1844 static void
1845 conversion_warning (tree type, tree expr)
1847 bool give_warning = false;
1849 int i;
1850 const int expr_num_operands = TREE_OPERAND_LENGTH (expr);
1851 tree expr_type = TREE_TYPE (expr);
1852 location_t loc = EXPR_HAS_LOCATION (expr)
1853 ? EXPR_LOCATION (expr) : input_location;
1855 if (!warn_conversion && !warn_sign_conversion)
1856 return;
1858 /* If any operand is artificial, then this expression was generated
1859 by the compiler and we do not warn. */
1860 for (i = 0; i < expr_num_operands; i++)
1862 tree op = TREE_OPERAND (expr, i);
1863 if (op && DECL_P (op) && DECL_ARTIFICIAL (op))
1864 return;
1867 switch (TREE_CODE (expr))
1869 case EQ_EXPR:
1870 case NE_EXPR:
1871 case LE_EXPR:
1872 case GE_EXPR:
1873 case LT_EXPR:
1874 case GT_EXPR:
1875 case TRUTH_ANDIF_EXPR:
1876 case TRUTH_ORIF_EXPR:
1877 case TRUTH_AND_EXPR:
1878 case TRUTH_OR_EXPR:
1879 case TRUTH_XOR_EXPR:
1880 case TRUTH_NOT_EXPR:
1881 /* Conversion from boolean to a signed:1 bit-field (which only
1882 can hold the values 0 and -1) doesn't lose information - but
1883 it does change the value. */
1884 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
1885 warning_at (loc, OPT_Wconversion,
1886 "conversion to %qT from boolean expression", type);
1887 return;
1889 case REAL_CST:
1890 case INTEGER_CST:
1892 /* Warn for real constant that is not an exact integer converted
1893 to integer type. */
1894 if (TREE_CODE (expr_type) == REAL_TYPE
1895 && TREE_CODE (type) == INTEGER_TYPE)
1897 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1898 give_warning = true;
1900 /* Warn for an integer constant that does not fit into integer type. */
1901 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1902 && TREE_CODE (type) == INTEGER_TYPE
1903 && !int_fits_type_p (expr, type))
1905 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1906 && tree_int_cst_sgn (expr) < 0)
1907 warning_at (loc, OPT_Wsign_conversion, "negative integer"
1908 " implicitly converted to unsigned type");
1909 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1910 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
1911 " constant value to negative integer");
1912 else
1913 give_warning = true;
1915 else if (TREE_CODE (type) == REAL_TYPE)
1917 /* Warn for an integer constant that does not fit into real type. */
1918 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1920 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1921 if (!exact_real_truncate (TYPE_MODE (type), &a))
1922 give_warning = true;
1924 /* Warn for a real constant that does not fit into a smaller
1925 real type. */
1926 else if (TREE_CODE (expr_type) == REAL_TYPE
1927 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1929 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1930 if (!exact_real_truncate (TYPE_MODE (type), &a))
1931 give_warning = true;
1935 if (give_warning)
1936 warning_at (loc, OPT_Wconversion,
1937 "conversion to %qT alters %qT constant value",
1938 type, expr_type);
1940 return;
1942 case COND_EXPR:
1944 /* In case of COND_EXPR, if both operands are constants or
1945 COND_EXPR, then we do not care about the type of COND_EXPR,
1946 only about the conversion of each operand. */
1947 tree op1 = TREE_OPERAND (expr, 1);
1948 tree op2 = TREE_OPERAND (expr, 2);
1950 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
1951 || TREE_CODE (op1) == COND_EXPR)
1952 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
1953 || TREE_CODE (op2) == COND_EXPR))
1955 conversion_warning (type, op1);
1956 conversion_warning (type, op2);
1957 return;
1959 /* Fall through. */
1962 default: /* 'expr' is not a constant. */
1964 /* Warn for real types converted to integer types. */
1965 if (TREE_CODE (expr_type) == REAL_TYPE
1966 && TREE_CODE (type) == INTEGER_TYPE)
1967 give_warning = true;
1969 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1970 && TREE_CODE (type) == INTEGER_TYPE)
1972 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1973 expr = get_unwidened (expr, 0);
1974 expr_type = TREE_TYPE (expr);
1976 /* Don't warn for short y; short x = ((int)y & 0xff); */
1977 if (TREE_CODE (expr) == BIT_AND_EXPR
1978 || TREE_CODE (expr) == BIT_IOR_EXPR
1979 || TREE_CODE (expr) == BIT_XOR_EXPR)
1981 /* If both args were extended from a shortest type,
1982 use that type if that is safe. */
1983 expr_type = shorten_binary_op (expr_type,
1984 TREE_OPERAND (expr, 0),
1985 TREE_OPERAND (expr, 1),
1986 /* bitwise */1);
1988 if (TREE_CODE (expr) == BIT_AND_EXPR)
1990 tree op0 = TREE_OPERAND (expr, 0);
1991 tree op1 = TREE_OPERAND (expr, 1);
1992 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1993 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1995 /* If one of the operands is a non-negative constant
1996 that fits in the target type, then the type of the
1997 other operand does not matter. */
1998 if ((TREE_CODE (op0) == INTEGER_CST
1999 && int_fits_type_p (op0, c_common_signed_type (type))
2000 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2001 || (TREE_CODE (op1) == INTEGER_CST
2002 && int_fits_type_p (op1, c_common_signed_type (type))
2003 && int_fits_type_p (op1,
2004 c_common_unsigned_type (type))))
2005 return;
2006 /* If constant is unsigned and fits in the target
2007 type, then the result will also fit. */
2008 else if ((TREE_CODE (op0) == INTEGER_CST
2009 && unsigned0
2010 && int_fits_type_p (op0, type))
2011 || (TREE_CODE (op1) == INTEGER_CST
2012 && unsigned1
2013 && int_fits_type_p (op1, type)))
2014 return;
2017 /* Warn for integer types converted to smaller integer types. */
2018 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2019 give_warning = true;
2021 /* When they are the same width but different signedness,
2022 then the value may change. */
2023 else if ((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2024 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2025 /* Even when converted to a bigger type, if the type is
2026 unsigned but expr is signed, then negative values
2027 will be changed. */
2028 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2029 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2030 "may change the sign of the result",
2031 type, expr_type);
2034 /* Warn for integer types converted to real types if and only if
2035 all the range of values of the integer type cannot be
2036 represented by the real type. */
2037 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2038 && TREE_CODE (type) == REAL_TYPE)
2040 tree type_low_bound, type_high_bound;
2041 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2043 /* Don't warn about char y = 0xff; float x = (int) y; */
2044 expr = get_unwidened (expr, 0);
2045 expr_type = TREE_TYPE (expr);
2047 type_low_bound = TYPE_MIN_VALUE (expr_type);
2048 type_high_bound = TYPE_MAX_VALUE (expr_type);
2049 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2050 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2052 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2053 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2054 give_warning = true;
2057 /* Warn for real types converted to smaller real types. */
2058 else if (TREE_CODE (expr_type) == REAL_TYPE
2059 && TREE_CODE (type) == REAL_TYPE
2060 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2061 give_warning = true;
2064 if (give_warning)
2065 warning_at (loc, OPT_Wconversion,
2066 "conversion to %qT from %qT may alter its value",
2067 type, expr_type);
2071 /* Produce warnings after a conversion. RESULT is the result of
2072 converting EXPR to TYPE. This is a helper function for
2073 convert_and_check and cp_convert_and_check. */
2075 void
2076 warnings_for_convert_and_check (tree type, tree expr, tree result)
2078 if (TREE_CODE (expr) == INTEGER_CST
2079 && (TREE_CODE (type) == INTEGER_TYPE
2080 || TREE_CODE (type) == ENUMERAL_TYPE)
2081 && !int_fits_type_p (expr, type))
2083 /* Do not diagnose overflow in a constant expression merely
2084 because a conversion overflowed. */
2085 if (TREE_OVERFLOW (result))
2086 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2088 if (TYPE_UNSIGNED (type))
2090 /* This detects cases like converting -129 or 256 to
2091 unsigned char. */
2092 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2093 warning (OPT_Woverflow,
2094 "large integer implicitly truncated to unsigned type");
2095 else
2096 conversion_warning (type, expr);
2098 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2099 warning (OPT_Woverflow,
2100 "overflow in implicit constant conversion");
2101 /* No warning for converting 0x80000000 to int. */
2102 else if (pedantic
2103 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2104 || TYPE_PRECISION (TREE_TYPE (expr))
2105 != TYPE_PRECISION (type)))
2106 warning (OPT_Woverflow,
2107 "overflow in implicit constant conversion");
2109 else
2110 conversion_warning (type, expr);
2112 else if ((TREE_CODE (result) == INTEGER_CST
2113 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2114 warning (OPT_Woverflow,
2115 "overflow in implicit constant conversion");
2116 else
2117 conversion_warning (type, expr);
2121 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2122 Invoke this function on every expression that is converted implicitly,
2123 i.e. because of language rules and not because of an explicit cast. */
2125 tree
2126 convert_and_check (tree type, tree expr)
2128 tree result;
2129 tree expr_for_warning;
2131 /* Convert from a value with possible excess precision rather than
2132 via the semantic type, but do not warn about values not fitting
2133 exactly in the semantic type. */
2134 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2136 tree orig_type = TREE_TYPE (expr);
2137 expr = TREE_OPERAND (expr, 0);
2138 expr_for_warning = convert (orig_type, expr);
2139 if (orig_type == type)
2140 return expr_for_warning;
2142 else
2143 expr_for_warning = expr;
2145 if (TREE_TYPE (expr) == type)
2146 return expr;
2148 result = convert (type, expr);
2150 if (c_inhibit_evaluation_warnings == 0
2151 && !TREE_OVERFLOW_P (expr)
2152 && result != error_mark_node)
2153 warnings_for_convert_and_check (type, expr_for_warning, result);
2155 return result;
2158 /* A node in a list that describes references to variables (EXPR), which are
2159 either read accesses if WRITER is zero, or write accesses, in which case
2160 WRITER is the parent of EXPR. */
2161 struct tlist
2163 struct tlist *next;
2164 tree expr, writer;
2167 /* Used to implement a cache the results of a call to verify_tree. We only
2168 use this for SAVE_EXPRs. */
2169 struct tlist_cache
2171 struct tlist_cache *next;
2172 struct tlist *cache_before_sp;
2173 struct tlist *cache_after_sp;
2174 tree expr;
2177 /* Obstack to use when allocating tlist structures, and corresponding
2178 firstobj. */
2179 static struct obstack tlist_obstack;
2180 static char *tlist_firstobj = 0;
2182 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2183 warnings. */
2184 static struct tlist *warned_ids;
2185 /* SAVE_EXPRs need special treatment. We process them only once and then
2186 cache the results. */
2187 static struct tlist_cache *save_expr_cache;
2189 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2190 static void merge_tlist (struct tlist **, struct tlist *, int);
2191 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2192 static int warning_candidate_p (tree);
2193 static bool candidate_equal_p (const_tree, const_tree);
2194 static void warn_for_collisions (struct tlist *);
2195 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2196 static struct tlist *new_tlist (struct tlist *, tree, tree);
2198 /* Create a new struct tlist and fill in its fields. */
2199 static struct tlist *
2200 new_tlist (struct tlist *next, tree t, tree writer)
2202 struct tlist *l;
2203 l = XOBNEW (&tlist_obstack, struct tlist);
2204 l->next = next;
2205 l->expr = t;
2206 l->writer = writer;
2207 return l;
2210 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2211 is nonnull, we ignore any node we find which has a writer equal to it. */
2213 static void
2214 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2216 while (add)
2218 struct tlist *next = add->next;
2219 if (!copy)
2220 add->next = *to;
2221 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2222 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2223 add = next;
2227 /* Merge the nodes of ADD into TO. This merging process is done so that for
2228 each variable that already exists in TO, no new node is added; however if
2229 there is a write access recorded in ADD, and an occurrence on TO is only
2230 a read access, then the occurrence in TO will be modified to record the
2231 write. */
2233 static void
2234 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2236 struct tlist **end = to;
2238 while (*end)
2239 end = &(*end)->next;
2241 while (add)
2243 int found = 0;
2244 struct tlist *tmp2;
2245 struct tlist *next = add->next;
2247 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2248 if (candidate_equal_p (tmp2->expr, add->expr))
2250 found = 1;
2251 if (!tmp2->writer)
2252 tmp2->writer = add->writer;
2254 if (!found)
2256 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2257 end = &(*end)->next;
2258 *end = 0;
2260 add = next;
2264 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2265 references in list LIST conflict with it, excluding reads if ONLY writers
2266 is nonzero. */
2268 static void
2269 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2270 int only_writes)
2272 struct tlist *tmp;
2274 /* Avoid duplicate warnings. */
2275 for (tmp = warned_ids; tmp; tmp = tmp->next)
2276 if (candidate_equal_p (tmp->expr, written))
2277 return;
2279 while (list)
2281 if (candidate_equal_p (list->expr, written)
2282 && !candidate_equal_p (list->writer, writer)
2283 && (!only_writes || list->writer))
2285 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2286 warning_at (EXPR_HAS_LOCATION (writer)
2287 ? EXPR_LOCATION (writer) : input_location,
2288 OPT_Wsequence_point, "operation on %qE may be undefined",
2289 list->expr);
2291 list = list->next;
2295 /* Given a list LIST of references to variables, find whether any of these
2296 can cause conflicts due to missing sequence points. */
2298 static void
2299 warn_for_collisions (struct tlist *list)
2301 struct tlist *tmp;
2303 for (tmp = list; tmp; tmp = tmp->next)
2305 if (tmp->writer)
2306 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2310 /* Return nonzero if X is a tree that can be verified by the sequence point
2311 warnings. */
2312 static int
2313 warning_candidate_p (tree x)
2315 /* !VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2316 (lvalue_p) crash on TRY/CATCH. */
2317 return !(DECL_P (x) && DECL_ARTIFICIAL (x))
2318 && TREE_TYPE (x) && !VOID_TYPE_P (TREE_TYPE (x)) && lvalue_p (x);
2321 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2322 static bool
2323 candidate_equal_p (const_tree x, const_tree y)
2325 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2328 /* Walk the tree X, and record accesses to variables. If X is written by the
2329 parent tree, WRITER is the parent.
2330 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2331 expression or its only operand forces a sequence point, then everything up
2332 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2333 in PNO_SP.
2334 Once we return, we will have emitted warnings if any subexpression before
2335 such a sequence point could be undefined. On a higher level, however, the
2336 sequence point may not be relevant, and we'll merge the two lists.
2338 Example: (b++, a) + b;
2339 The call that processes the COMPOUND_EXPR will store the increment of B
2340 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2341 processes the PLUS_EXPR will need to merge the two lists so that
2342 eventually, all accesses end up on the same list (and we'll warn about the
2343 unordered subexpressions b++ and b.
2345 A note on merging. If we modify the former example so that our expression
2346 becomes
2347 (b++, b) + a
2348 care must be taken not simply to add all three expressions into the final
2349 PNO_SP list. The function merge_tlist takes care of that by merging the
2350 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2351 way, so that no more than one access to B is recorded. */
2353 static void
2354 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2355 tree writer)
2357 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2358 enum tree_code code;
2359 enum tree_code_class cl;
2361 /* X may be NULL if it is the operand of an empty statement expression
2362 ({ }). */
2363 if (x == NULL)
2364 return;
2366 restart:
2367 code = TREE_CODE (x);
2368 cl = TREE_CODE_CLASS (code);
2370 if (warning_candidate_p (x))
2371 *pno_sp = new_tlist (*pno_sp, x, writer);
2373 switch (code)
2375 case CONSTRUCTOR:
2376 return;
2378 case COMPOUND_EXPR:
2379 case TRUTH_ANDIF_EXPR:
2380 case TRUTH_ORIF_EXPR:
2381 tmp_before = tmp_nosp = tmp_list3 = 0;
2382 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2383 warn_for_collisions (tmp_nosp);
2384 merge_tlist (pbefore_sp, tmp_before, 0);
2385 merge_tlist (pbefore_sp, tmp_nosp, 0);
2386 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2387 merge_tlist (pbefore_sp, tmp_list3, 0);
2388 return;
2390 case COND_EXPR:
2391 tmp_before = tmp_list2 = 0;
2392 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2393 warn_for_collisions (tmp_list2);
2394 merge_tlist (pbefore_sp, tmp_before, 0);
2395 merge_tlist (pbefore_sp, tmp_list2, 1);
2397 tmp_list3 = tmp_nosp = 0;
2398 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2399 warn_for_collisions (tmp_nosp);
2400 merge_tlist (pbefore_sp, tmp_list3, 0);
2402 tmp_list3 = tmp_list2 = 0;
2403 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2404 warn_for_collisions (tmp_list2);
2405 merge_tlist (pbefore_sp, tmp_list3, 0);
2406 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2407 two first, to avoid warning for (a ? b++ : b++). */
2408 merge_tlist (&tmp_nosp, tmp_list2, 0);
2409 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2410 return;
2412 case PREDECREMENT_EXPR:
2413 case PREINCREMENT_EXPR:
2414 case POSTDECREMENT_EXPR:
2415 case POSTINCREMENT_EXPR:
2416 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2417 return;
2419 case MODIFY_EXPR:
2420 tmp_before = tmp_nosp = tmp_list3 = 0;
2421 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2422 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2423 /* Expressions inside the LHS are not ordered wrt. the sequence points
2424 in the RHS. Example:
2425 *a = (a++, 2)
2426 Despite the fact that the modification of "a" is in the before_sp
2427 list (tmp_before), it conflicts with the use of "a" in the LHS.
2428 We can handle this by adding the contents of tmp_list3
2429 to those of tmp_before, and redoing the collision warnings for that
2430 list. */
2431 add_tlist (&tmp_before, tmp_list3, x, 1);
2432 warn_for_collisions (tmp_before);
2433 /* Exclude the LHS itself here; we first have to merge it into the
2434 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2435 didn't exclude the LHS, we'd get it twice, once as a read and once
2436 as a write. */
2437 add_tlist (pno_sp, tmp_list3, x, 0);
2438 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2440 merge_tlist (pbefore_sp, tmp_before, 0);
2441 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2442 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2443 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2444 return;
2446 case CALL_EXPR:
2447 /* We need to warn about conflicts among arguments and conflicts between
2448 args and the function address. Side effects of the function address,
2449 however, are not ordered by the sequence point of the call. */
2451 call_expr_arg_iterator iter;
2452 tree arg;
2453 tmp_before = tmp_nosp = 0;
2454 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2455 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2457 tmp_list2 = tmp_list3 = 0;
2458 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2459 merge_tlist (&tmp_list3, tmp_list2, 0);
2460 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2462 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2463 warn_for_collisions (tmp_before);
2464 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2465 return;
2468 case TREE_LIST:
2469 /* Scan all the list, e.g. indices of multi dimensional array. */
2470 while (x)
2472 tmp_before = tmp_nosp = 0;
2473 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2474 merge_tlist (&tmp_nosp, tmp_before, 0);
2475 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2476 x = TREE_CHAIN (x);
2478 return;
2480 case SAVE_EXPR:
2482 struct tlist_cache *t;
2483 for (t = save_expr_cache; t; t = t->next)
2484 if (candidate_equal_p (t->expr, x))
2485 break;
2487 if (!t)
2489 t = XOBNEW (&tlist_obstack, struct tlist_cache);
2490 t->next = save_expr_cache;
2491 t->expr = x;
2492 save_expr_cache = t;
2494 tmp_before = tmp_nosp = 0;
2495 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2496 warn_for_collisions (tmp_nosp);
2498 tmp_list3 = 0;
2499 while (tmp_nosp)
2501 struct tlist *t = tmp_nosp;
2502 tmp_nosp = t->next;
2503 merge_tlist (&tmp_list3, t, 0);
2505 t->cache_before_sp = tmp_before;
2506 t->cache_after_sp = tmp_list3;
2508 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2509 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2510 return;
2513 case ADDR_EXPR:
2514 x = TREE_OPERAND (x, 0);
2515 if (DECL_P (x))
2516 return;
2517 writer = 0;
2518 goto restart;
2520 default:
2521 /* For other expressions, simply recurse on their operands.
2522 Manual tail recursion for unary expressions.
2523 Other non-expressions need not be processed. */
2524 if (cl == tcc_unary)
2526 x = TREE_OPERAND (x, 0);
2527 writer = 0;
2528 goto restart;
2530 else if (IS_EXPR_CODE_CLASS (cl))
2532 int lp;
2533 int max = TREE_OPERAND_LENGTH (x);
2534 for (lp = 0; lp < max; lp++)
2536 tmp_before = tmp_nosp = 0;
2537 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2538 merge_tlist (&tmp_nosp, tmp_before, 0);
2539 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2542 return;
2546 /* Try to warn for undefined behavior in EXPR due to missing sequence
2547 points. */
2549 DEBUG_FUNCTION void
2550 verify_sequence_points (tree expr)
2552 struct tlist *before_sp = 0, *after_sp = 0;
2554 warned_ids = 0;
2555 save_expr_cache = 0;
2556 if (tlist_firstobj == 0)
2558 gcc_obstack_init (&tlist_obstack);
2559 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2562 verify_tree (expr, &before_sp, &after_sp, 0);
2563 warn_for_collisions (after_sp);
2564 obstack_free (&tlist_obstack, tlist_firstobj);
2567 /* Validate the expression after `case' and apply default promotions. */
2569 static tree
2570 check_case_value (tree value)
2572 if (value == NULL_TREE)
2573 return value;
2575 /* ??? Can we ever get nops here for a valid case value? We
2576 shouldn't for C. */
2577 STRIP_TYPE_NOPS (value);
2578 /* In C++, the following is allowed:
2580 const int i = 3;
2581 switch (...) { case i: ... }
2583 So, we try to reduce the VALUE to a constant that way. */
2584 if (c_dialect_cxx ())
2586 value = decl_constant_value (value);
2587 STRIP_TYPE_NOPS (value);
2588 value = fold (value);
2591 if (TREE_CODE (value) == INTEGER_CST)
2592 /* Promote char or short to int. */
2593 value = perform_integral_promotions (value);
2594 else if (value != error_mark_node)
2596 error ("case label does not reduce to an integer constant");
2597 value = error_mark_node;
2600 constant_expression_warning (value);
2602 return value;
2605 /* See if the case values LOW and HIGH are in the range of the original
2606 type (i.e. before the default conversion to int) of the switch testing
2607 expression.
2608 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2609 the type before promoting it. CASE_LOW_P is a pointer to the lower
2610 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2611 if the case is not a case range.
2612 The caller has to make sure that we are not called with NULL for
2613 CASE_LOW_P (i.e. the default case).
2614 Returns true if the case label is in range of ORIG_TYPE (saturated or
2615 untouched) or false if the label is out of range. */
2617 static bool
2618 check_case_bounds (tree type, tree orig_type,
2619 tree *case_low_p, tree *case_high_p)
2621 tree min_value, max_value;
2622 tree case_low = *case_low_p;
2623 tree case_high = case_high_p ? *case_high_p : case_low;
2625 /* If there was a problem with the original type, do nothing. */
2626 if (orig_type == error_mark_node)
2627 return true;
2629 min_value = TYPE_MIN_VALUE (orig_type);
2630 max_value = TYPE_MAX_VALUE (orig_type);
2632 /* Case label is less than minimum for type. */
2633 if (tree_int_cst_compare (case_low, min_value) < 0
2634 && tree_int_cst_compare (case_high, min_value) < 0)
2636 warning (0, "case label value is less than minimum value for type");
2637 return false;
2640 /* Case value is greater than maximum for type. */
2641 if (tree_int_cst_compare (case_low, max_value) > 0
2642 && tree_int_cst_compare (case_high, max_value) > 0)
2644 warning (0, "case label value exceeds maximum value for type");
2645 return false;
2648 /* Saturate lower case label value to minimum. */
2649 if (tree_int_cst_compare (case_high, min_value) >= 0
2650 && tree_int_cst_compare (case_low, min_value) < 0)
2652 warning (0, "lower value in case label range"
2653 " less than minimum value for type");
2654 case_low = min_value;
2657 /* Saturate upper case label value to maximum. */
2658 if (tree_int_cst_compare (case_low, max_value) <= 0
2659 && tree_int_cst_compare (case_high, max_value) > 0)
2661 warning (0, "upper value in case label range"
2662 " exceeds maximum value for type");
2663 case_high = max_value;
2666 if (*case_low_p != case_low)
2667 *case_low_p = convert (type, case_low);
2668 if (case_high_p && *case_high_p != case_high)
2669 *case_high_p = convert (type, case_high);
2671 return true;
2674 /* Return an integer type with BITS bits of precision,
2675 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2677 tree
2678 c_common_type_for_size (unsigned int bits, int unsignedp)
2680 if (bits == TYPE_PRECISION (integer_type_node))
2681 return unsignedp ? unsigned_type_node : integer_type_node;
2683 if (bits == TYPE_PRECISION (signed_char_type_node))
2684 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2686 if (bits == TYPE_PRECISION (short_integer_type_node))
2687 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2689 if (bits == TYPE_PRECISION (long_integer_type_node))
2690 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2692 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2693 return (unsignedp ? long_long_unsigned_type_node
2694 : long_long_integer_type_node);
2696 if (int128_integer_type_node
2697 && bits == TYPE_PRECISION (int128_integer_type_node))
2698 return (unsignedp ? int128_unsigned_type_node
2699 : int128_integer_type_node);
2701 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2702 return (unsignedp ? widest_unsigned_literal_type_node
2703 : widest_integer_literal_type_node);
2705 if (bits <= TYPE_PRECISION (intQI_type_node))
2706 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2708 if (bits <= TYPE_PRECISION (intHI_type_node))
2709 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2711 if (bits <= TYPE_PRECISION (intSI_type_node))
2712 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2714 if (bits <= TYPE_PRECISION (intDI_type_node))
2715 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2717 return 0;
2720 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2721 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2722 and saturating if SATP is nonzero, otherwise not saturating. */
2724 tree
2725 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2726 int unsignedp, int satp)
2728 enum machine_mode mode;
2729 if (ibit == 0)
2730 mode = unsignedp ? UQQmode : QQmode;
2731 else
2732 mode = unsignedp ? UHAmode : HAmode;
2734 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2735 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2736 break;
2738 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2740 sorry ("GCC cannot support operators with integer types and "
2741 "fixed-point types that have too many integral and "
2742 "fractional bits together");
2743 return 0;
2746 return c_common_type_for_mode (mode, satp);
2749 /* Used for communication between c_common_type_for_mode and
2750 c_register_builtin_type. */
2751 static GTY(()) tree registered_builtin_types;
2753 /* Return a data type that has machine mode MODE.
2754 If the mode is an integer,
2755 then UNSIGNEDP selects between signed and unsigned types.
2756 If the mode is a fixed-point mode,
2757 then UNSIGNEDP selects between saturating and nonsaturating types. */
2759 tree
2760 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
2762 tree t;
2764 if (mode == TYPE_MODE (integer_type_node))
2765 return unsignedp ? unsigned_type_node : integer_type_node;
2767 if (mode == TYPE_MODE (signed_char_type_node))
2768 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2770 if (mode == TYPE_MODE (short_integer_type_node))
2771 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2773 if (mode == TYPE_MODE (long_integer_type_node))
2774 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2776 if (mode == TYPE_MODE (long_long_integer_type_node))
2777 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2779 if (int128_integer_type_node
2780 && mode == TYPE_MODE (int128_integer_type_node))
2781 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
2783 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2784 return unsignedp ? widest_unsigned_literal_type_node
2785 : widest_integer_literal_type_node;
2787 if (mode == QImode)
2788 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2790 if (mode == HImode)
2791 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2793 if (mode == SImode)
2794 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2796 if (mode == DImode)
2797 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2799 #if HOST_BITS_PER_WIDE_INT >= 64
2800 if (mode == TYPE_MODE (intTI_type_node))
2801 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2802 #endif
2804 if (mode == TYPE_MODE (float_type_node))
2805 return float_type_node;
2807 if (mode == TYPE_MODE (double_type_node))
2808 return double_type_node;
2810 if (mode == TYPE_MODE (long_double_type_node))
2811 return long_double_type_node;
2813 if (mode == TYPE_MODE (void_type_node))
2814 return void_type_node;
2816 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2817 return (unsignedp
2818 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2819 : make_signed_type (GET_MODE_PRECISION (mode)));
2821 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2822 return (unsignedp
2823 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2824 : make_signed_type (GET_MODE_PRECISION (mode)));
2826 if (COMPLEX_MODE_P (mode))
2828 enum machine_mode inner_mode;
2829 tree inner_type;
2831 if (mode == TYPE_MODE (complex_float_type_node))
2832 return complex_float_type_node;
2833 if (mode == TYPE_MODE (complex_double_type_node))
2834 return complex_double_type_node;
2835 if (mode == TYPE_MODE (complex_long_double_type_node))
2836 return complex_long_double_type_node;
2838 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2839 return complex_integer_type_node;
2841 inner_mode = GET_MODE_INNER (mode);
2842 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2843 if (inner_type != NULL_TREE)
2844 return build_complex_type (inner_type);
2846 else if (VECTOR_MODE_P (mode))
2848 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2849 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2850 if (inner_type != NULL_TREE)
2851 return build_vector_type_for_mode (inner_type, mode);
2854 if (mode == TYPE_MODE (dfloat32_type_node))
2855 return dfloat32_type_node;
2856 if (mode == TYPE_MODE (dfloat64_type_node))
2857 return dfloat64_type_node;
2858 if (mode == TYPE_MODE (dfloat128_type_node))
2859 return dfloat128_type_node;
2861 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2863 if (mode == TYPE_MODE (short_fract_type_node))
2864 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2865 if (mode == TYPE_MODE (fract_type_node))
2866 return unsignedp ? sat_fract_type_node : fract_type_node;
2867 if (mode == TYPE_MODE (long_fract_type_node))
2868 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2869 if (mode == TYPE_MODE (long_long_fract_type_node))
2870 return unsignedp ? sat_long_long_fract_type_node
2871 : long_long_fract_type_node;
2873 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2874 return unsignedp ? sat_unsigned_short_fract_type_node
2875 : unsigned_short_fract_type_node;
2876 if (mode == TYPE_MODE (unsigned_fract_type_node))
2877 return unsignedp ? sat_unsigned_fract_type_node
2878 : unsigned_fract_type_node;
2879 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2880 return unsignedp ? sat_unsigned_long_fract_type_node
2881 : unsigned_long_fract_type_node;
2882 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2883 return unsignedp ? sat_unsigned_long_long_fract_type_node
2884 : unsigned_long_long_fract_type_node;
2886 if (mode == TYPE_MODE (short_accum_type_node))
2887 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2888 if (mode == TYPE_MODE (accum_type_node))
2889 return unsignedp ? sat_accum_type_node : accum_type_node;
2890 if (mode == TYPE_MODE (long_accum_type_node))
2891 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2892 if (mode == TYPE_MODE (long_long_accum_type_node))
2893 return unsignedp ? sat_long_long_accum_type_node
2894 : long_long_accum_type_node;
2896 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2897 return unsignedp ? sat_unsigned_short_accum_type_node
2898 : unsigned_short_accum_type_node;
2899 if (mode == TYPE_MODE (unsigned_accum_type_node))
2900 return unsignedp ? sat_unsigned_accum_type_node
2901 : unsigned_accum_type_node;
2902 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2903 return unsignedp ? sat_unsigned_long_accum_type_node
2904 : unsigned_long_accum_type_node;
2905 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2906 return unsignedp ? sat_unsigned_long_long_accum_type_node
2907 : unsigned_long_long_accum_type_node;
2909 if (mode == QQmode)
2910 return unsignedp ? sat_qq_type_node : qq_type_node;
2911 if (mode == HQmode)
2912 return unsignedp ? sat_hq_type_node : hq_type_node;
2913 if (mode == SQmode)
2914 return unsignedp ? sat_sq_type_node : sq_type_node;
2915 if (mode == DQmode)
2916 return unsignedp ? sat_dq_type_node : dq_type_node;
2917 if (mode == TQmode)
2918 return unsignedp ? sat_tq_type_node : tq_type_node;
2920 if (mode == UQQmode)
2921 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2922 if (mode == UHQmode)
2923 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2924 if (mode == USQmode)
2925 return unsignedp ? sat_usq_type_node : usq_type_node;
2926 if (mode == UDQmode)
2927 return unsignedp ? sat_udq_type_node : udq_type_node;
2928 if (mode == UTQmode)
2929 return unsignedp ? sat_utq_type_node : utq_type_node;
2931 if (mode == HAmode)
2932 return unsignedp ? sat_ha_type_node : ha_type_node;
2933 if (mode == SAmode)
2934 return unsignedp ? sat_sa_type_node : sa_type_node;
2935 if (mode == DAmode)
2936 return unsignedp ? sat_da_type_node : da_type_node;
2937 if (mode == TAmode)
2938 return unsignedp ? sat_ta_type_node : ta_type_node;
2940 if (mode == UHAmode)
2941 return unsignedp ? sat_uha_type_node : uha_type_node;
2942 if (mode == USAmode)
2943 return unsignedp ? sat_usa_type_node : usa_type_node;
2944 if (mode == UDAmode)
2945 return unsignedp ? sat_uda_type_node : uda_type_node;
2946 if (mode == UTAmode)
2947 return unsignedp ? sat_uta_type_node : uta_type_node;
2950 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2951 if (TYPE_MODE (TREE_VALUE (t)) == mode)
2952 return TREE_VALUE (t);
2954 return 0;
2957 tree
2958 c_common_unsigned_type (tree type)
2960 return c_common_signed_or_unsigned_type (1, type);
2963 /* Return a signed type the same as TYPE in other respects. */
2965 tree
2966 c_common_signed_type (tree type)
2968 return c_common_signed_or_unsigned_type (0, type);
2971 /* Return a type the same as TYPE except unsigned or
2972 signed according to UNSIGNEDP. */
2974 tree
2975 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2977 tree type1;
2979 /* This block of code emulates the behavior of the old
2980 c_common_unsigned_type. In particular, it returns
2981 long_unsigned_type_node if passed a long, even when a int would
2982 have the same size. This is necessary for warnings to work
2983 correctly in archs where sizeof(int) == sizeof(long) */
2985 type1 = TYPE_MAIN_VARIANT (type);
2986 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2987 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2988 if (type1 == integer_type_node || type1 == unsigned_type_node)
2989 return unsignedp ? unsigned_type_node : integer_type_node;
2990 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2991 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2992 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2993 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2994 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2995 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2996 if (int128_integer_type_node
2997 && (type1 == int128_integer_type_node
2998 || type1 == int128_unsigned_type_node))
2999 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3000 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3001 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3002 #if HOST_BITS_PER_WIDE_INT >= 64
3003 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3004 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3005 #endif
3006 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3007 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3008 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3009 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3010 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3011 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3012 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3013 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3015 #define C_COMMON_FIXED_TYPES(NAME) \
3016 if (type1 == short_ ## NAME ## _type_node \
3017 || type1 == unsigned_short_ ## NAME ## _type_node) \
3018 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3019 : short_ ## NAME ## _type_node; \
3020 if (type1 == NAME ## _type_node \
3021 || type1 == unsigned_ ## NAME ## _type_node) \
3022 return unsignedp ? unsigned_ ## NAME ## _type_node \
3023 : NAME ## _type_node; \
3024 if (type1 == long_ ## NAME ## _type_node \
3025 || type1 == unsigned_long_ ## NAME ## _type_node) \
3026 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3027 : long_ ## NAME ## _type_node; \
3028 if (type1 == long_long_ ## NAME ## _type_node \
3029 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3030 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3031 : long_long_ ## NAME ## _type_node;
3033 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3034 if (type1 == NAME ## _type_node \
3035 || type1 == u ## NAME ## _type_node) \
3036 return unsignedp ? u ## NAME ## _type_node \
3037 : NAME ## _type_node;
3039 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3040 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3041 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3042 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3043 : sat_ ## short_ ## NAME ## _type_node; \
3044 if (type1 == sat_ ## NAME ## _type_node \
3045 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3046 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3047 : sat_ ## NAME ## _type_node; \
3048 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3049 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3050 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3051 : sat_ ## long_ ## NAME ## _type_node; \
3052 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3053 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3054 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3055 : sat_ ## long_long_ ## NAME ## _type_node;
3057 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3058 if (type1 == sat_ ## NAME ## _type_node \
3059 || type1 == sat_ ## u ## NAME ## _type_node) \
3060 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3061 : sat_ ## NAME ## _type_node;
3063 C_COMMON_FIXED_TYPES (fract);
3064 C_COMMON_FIXED_TYPES_SAT (fract);
3065 C_COMMON_FIXED_TYPES (accum);
3066 C_COMMON_FIXED_TYPES_SAT (accum);
3068 C_COMMON_FIXED_MODE_TYPES (qq);
3069 C_COMMON_FIXED_MODE_TYPES (hq);
3070 C_COMMON_FIXED_MODE_TYPES (sq);
3071 C_COMMON_FIXED_MODE_TYPES (dq);
3072 C_COMMON_FIXED_MODE_TYPES (tq);
3073 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3074 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3075 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3076 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3077 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3078 C_COMMON_FIXED_MODE_TYPES (ha);
3079 C_COMMON_FIXED_MODE_TYPES (sa);
3080 C_COMMON_FIXED_MODE_TYPES (da);
3081 C_COMMON_FIXED_MODE_TYPES (ta);
3082 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3083 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3084 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3085 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3087 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3088 the precision; they have precision set to match their range, but
3089 may use a wider mode to match an ABI. If we change modes, we may
3090 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3091 the precision as well, so as to yield correct results for
3092 bit-field types. C++ does not have these separate bit-field
3093 types, and producing a signed or unsigned variant of an
3094 ENUMERAL_TYPE may cause other problems as well. */
3096 if (!INTEGRAL_TYPE_P (type)
3097 || TYPE_UNSIGNED (type) == unsignedp)
3098 return type;
3100 #define TYPE_OK(node) \
3101 (TYPE_MODE (type) == TYPE_MODE (node) \
3102 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3103 if (TYPE_OK (signed_char_type_node))
3104 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3105 if (TYPE_OK (integer_type_node))
3106 return unsignedp ? unsigned_type_node : integer_type_node;
3107 if (TYPE_OK (short_integer_type_node))
3108 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3109 if (TYPE_OK (long_integer_type_node))
3110 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3111 if (TYPE_OK (long_long_integer_type_node))
3112 return (unsignedp ? long_long_unsigned_type_node
3113 : long_long_integer_type_node);
3114 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3115 return (unsignedp ? int128_unsigned_type_node
3116 : int128_integer_type_node);
3117 if (TYPE_OK (widest_integer_literal_type_node))
3118 return (unsignedp ? widest_unsigned_literal_type_node
3119 : widest_integer_literal_type_node);
3121 #if HOST_BITS_PER_WIDE_INT >= 64
3122 if (TYPE_OK (intTI_type_node))
3123 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3124 #endif
3125 if (TYPE_OK (intDI_type_node))
3126 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3127 if (TYPE_OK (intSI_type_node))
3128 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3129 if (TYPE_OK (intHI_type_node))
3130 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3131 if (TYPE_OK (intQI_type_node))
3132 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3133 #undef TYPE_OK
3135 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3138 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3140 tree
3141 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3143 /* Extended integer types of the same width as a standard type have
3144 lesser rank, so those of the same width as int promote to int or
3145 unsigned int and are valid for printf formats expecting int or
3146 unsigned int. To avoid such special cases, avoid creating
3147 extended integer types for bit-fields if a standard integer type
3148 is available. */
3149 if (width == TYPE_PRECISION (integer_type_node))
3150 return unsignedp ? unsigned_type_node : integer_type_node;
3151 if (width == TYPE_PRECISION (signed_char_type_node))
3152 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3153 if (width == TYPE_PRECISION (short_integer_type_node))
3154 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3155 if (width == TYPE_PRECISION (long_integer_type_node))
3156 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3157 if (width == TYPE_PRECISION (long_long_integer_type_node))
3158 return (unsignedp ? long_long_unsigned_type_node
3159 : long_long_integer_type_node);
3160 if (int128_integer_type_node
3161 && width == TYPE_PRECISION (int128_integer_type_node))
3162 return (unsignedp ? int128_unsigned_type_node
3163 : int128_integer_type_node);
3164 return build_nonstandard_integer_type (width, unsignedp);
3167 /* The C version of the register_builtin_type langhook. */
3169 void
3170 c_register_builtin_type (tree type, const char* name)
3172 tree decl;
3174 decl = build_decl (UNKNOWN_LOCATION,
3175 TYPE_DECL, get_identifier (name), type);
3176 DECL_ARTIFICIAL (decl) = 1;
3177 if (!TYPE_NAME (type))
3178 TYPE_NAME (type) = decl;
3179 pushdecl (decl);
3181 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3184 /* Print an error message for invalid operands to arith operation
3185 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3186 LOCATION is the location of the message. */
3188 void
3189 binary_op_error (location_t location, enum tree_code code,
3190 tree type0, tree type1)
3192 const char *opname;
3194 switch (code)
3196 case PLUS_EXPR:
3197 opname = "+"; break;
3198 case MINUS_EXPR:
3199 opname = "-"; break;
3200 case MULT_EXPR:
3201 opname = "*"; break;
3202 case MAX_EXPR:
3203 opname = "max"; break;
3204 case MIN_EXPR:
3205 opname = "min"; break;
3206 case EQ_EXPR:
3207 opname = "=="; break;
3208 case NE_EXPR:
3209 opname = "!="; break;
3210 case LE_EXPR:
3211 opname = "<="; break;
3212 case GE_EXPR:
3213 opname = ">="; break;
3214 case LT_EXPR:
3215 opname = "<"; break;
3216 case GT_EXPR:
3217 opname = ">"; break;
3218 case LSHIFT_EXPR:
3219 opname = "<<"; break;
3220 case RSHIFT_EXPR:
3221 opname = ">>"; break;
3222 case TRUNC_MOD_EXPR:
3223 case FLOOR_MOD_EXPR:
3224 opname = "%"; break;
3225 case TRUNC_DIV_EXPR:
3226 case FLOOR_DIV_EXPR:
3227 opname = "/"; break;
3228 case BIT_AND_EXPR:
3229 opname = "&"; break;
3230 case BIT_IOR_EXPR:
3231 opname = "|"; break;
3232 case TRUTH_ANDIF_EXPR:
3233 opname = "&&"; break;
3234 case TRUTH_ORIF_EXPR:
3235 opname = "||"; break;
3236 case BIT_XOR_EXPR:
3237 opname = "^"; break;
3238 default:
3239 gcc_unreachable ();
3241 error_at (location,
3242 "invalid operands to binary %s (have %qT and %qT)", opname,
3243 type0, type1);
3246 /* Subroutine of build_binary_op, used for comparison operations.
3247 See if the operands have both been converted from subword integer types
3248 and, if so, perhaps change them both back to their original type.
3249 This function is also responsible for converting the two operands
3250 to the proper common type for comparison.
3252 The arguments of this function are all pointers to local variables
3253 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3254 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3256 If this function returns nonzero, it means that the comparison has
3257 a constant value. What this function returns is an expression for
3258 that value. */
3260 tree
3261 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3262 enum tree_code *rescode_ptr)
3264 tree type;
3265 tree op0 = *op0_ptr;
3266 tree op1 = *op1_ptr;
3267 int unsignedp0, unsignedp1;
3268 int real1, real2;
3269 tree primop0, primop1;
3270 enum tree_code code = *rescode_ptr;
3272 /* Throw away any conversions to wider types
3273 already present in the operands. */
3275 primop0 = get_narrower (op0, &unsignedp0);
3276 primop1 = get_narrower (op1, &unsignedp1);
3278 /* Handle the case that OP0 does not *contain* a conversion
3279 but it *requires* conversion to FINAL_TYPE. */
3281 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3282 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3283 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3284 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3286 /* If one of the operands must be floated, we cannot optimize. */
3287 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3288 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3290 /* If first arg is constant, swap the args (changing operation
3291 so value is preserved), for canonicalization. Don't do this if
3292 the second arg is 0. */
3294 if (TREE_CONSTANT (primop0)
3295 && !integer_zerop (primop1) && !real_zerop (primop1)
3296 && !fixed_zerop (primop1))
3298 tree tem = primop0;
3299 int temi = unsignedp0;
3300 primop0 = primop1;
3301 primop1 = tem;
3302 tem = op0;
3303 op0 = op1;
3304 op1 = tem;
3305 *op0_ptr = op0;
3306 *op1_ptr = op1;
3307 unsignedp0 = unsignedp1;
3308 unsignedp1 = temi;
3309 temi = real1;
3310 real1 = real2;
3311 real2 = temi;
3313 switch (code)
3315 case LT_EXPR:
3316 code = GT_EXPR;
3317 break;
3318 case GT_EXPR:
3319 code = LT_EXPR;
3320 break;
3321 case LE_EXPR:
3322 code = GE_EXPR;
3323 break;
3324 case GE_EXPR:
3325 code = LE_EXPR;
3326 break;
3327 default:
3328 break;
3330 *rescode_ptr = code;
3333 /* If comparing an integer against a constant more bits wide,
3334 maybe we can deduce a value of 1 or 0 independent of the data.
3335 Or else truncate the constant now
3336 rather than extend the variable at run time.
3338 This is only interesting if the constant is the wider arg.
3339 Also, it is not safe if the constant is unsigned and the
3340 variable arg is signed, since in this case the variable
3341 would be sign-extended and then regarded as unsigned.
3342 Our technique fails in this case because the lowest/highest
3343 possible unsigned results don't follow naturally from the
3344 lowest/highest possible values of the variable operand.
3345 For just EQ_EXPR and NE_EXPR there is another technique that
3346 could be used: see if the constant can be faithfully represented
3347 in the other operand's type, by truncating it and reextending it
3348 and see if that preserves the constant's value. */
3350 if (!real1 && !real2
3351 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3352 && TREE_CODE (primop1) == INTEGER_CST
3353 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3355 int min_gt, max_gt, min_lt, max_lt;
3356 tree maxval, minval;
3357 /* 1 if comparison is nominally unsigned. */
3358 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3359 tree val;
3361 type = c_common_signed_or_unsigned_type (unsignedp0,
3362 TREE_TYPE (primop0));
3364 maxval = TYPE_MAX_VALUE (type);
3365 minval = TYPE_MIN_VALUE (type);
3367 if (unsignedp && !unsignedp0)
3368 *restype_ptr = c_common_signed_type (*restype_ptr);
3370 if (TREE_TYPE (primop1) != *restype_ptr)
3372 /* Convert primop1 to target type, but do not introduce
3373 additional overflow. We know primop1 is an int_cst. */
3374 primop1 = force_fit_type_double (*restype_ptr,
3375 tree_to_double_int (primop1),
3376 0, TREE_OVERFLOW (primop1));
3378 if (type != *restype_ptr)
3380 minval = convert (*restype_ptr, minval);
3381 maxval = convert (*restype_ptr, maxval);
3384 if (unsignedp && unsignedp0)
3386 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3387 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3388 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3389 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3391 else
3393 min_gt = INT_CST_LT (primop1, minval);
3394 max_gt = INT_CST_LT (primop1, maxval);
3395 min_lt = INT_CST_LT (minval, primop1);
3396 max_lt = INT_CST_LT (maxval, primop1);
3399 val = 0;
3400 /* This used to be a switch, but Genix compiler can't handle that. */
3401 if (code == NE_EXPR)
3403 if (max_lt || min_gt)
3404 val = truthvalue_true_node;
3406 else if (code == EQ_EXPR)
3408 if (max_lt || min_gt)
3409 val = truthvalue_false_node;
3411 else if (code == LT_EXPR)
3413 if (max_lt)
3414 val = truthvalue_true_node;
3415 if (!min_lt)
3416 val = truthvalue_false_node;
3418 else if (code == GT_EXPR)
3420 if (min_gt)
3421 val = truthvalue_true_node;
3422 if (!max_gt)
3423 val = truthvalue_false_node;
3425 else if (code == LE_EXPR)
3427 if (!max_gt)
3428 val = truthvalue_true_node;
3429 if (min_gt)
3430 val = truthvalue_false_node;
3432 else if (code == GE_EXPR)
3434 if (!min_lt)
3435 val = truthvalue_true_node;
3436 if (max_lt)
3437 val = truthvalue_false_node;
3440 /* If primop0 was sign-extended and unsigned comparison specd,
3441 we did a signed comparison above using the signed type bounds.
3442 But the comparison we output must be unsigned.
3444 Also, for inequalities, VAL is no good; but if the signed
3445 comparison had *any* fixed result, it follows that the
3446 unsigned comparison just tests the sign in reverse
3447 (positive values are LE, negative ones GE).
3448 So we can generate an unsigned comparison
3449 against an extreme value of the signed type. */
3451 if (unsignedp && !unsignedp0)
3453 if (val != 0)
3454 switch (code)
3456 case LT_EXPR:
3457 case GE_EXPR:
3458 primop1 = TYPE_MIN_VALUE (type);
3459 val = 0;
3460 break;
3462 case LE_EXPR:
3463 case GT_EXPR:
3464 primop1 = TYPE_MAX_VALUE (type);
3465 val = 0;
3466 break;
3468 default:
3469 break;
3471 type = c_common_unsigned_type (type);
3474 if (TREE_CODE (primop0) != INTEGER_CST)
3476 if (val == truthvalue_false_node)
3477 warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type");
3478 if (val == truthvalue_true_node)
3479 warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type");
3482 if (val != 0)
3484 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3485 if (TREE_SIDE_EFFECTS (primop0))
3486 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3487 return val;
3490 /* Value is not predetermined, but do the comparison
3491 in the type of the operand that is not constant.
3492 TYPE is already properly set. */
3495 /* If either arg is decimal float and the other is float, find the
3496 proper common type to use for comparison. */
3497 else if (real1 && real2
3498 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3499 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3500 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3502 else if (real1 && real2
3503 && (TYPE_PRECISION (TREE_TYPE (primop0))
3504 == TYPE_PRECISION (TREE_TYPE (primop1))))
3505 type = TREE_TYPE (primop0);
3507 /* If args' natural types are both narrower than nominal type
3508 and both extend in the same manner, compare them
3509 in the type of the wider arg.
3510 Otherwise must actually extend both to the nominal
3511 common type lest different ways of extending
3512 alter the result.
3513 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3515 else if (unsignedp0 == unsignedp1 && real1 == real2
3516 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3517 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3519 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3520 type = c_common_signed_or_unsigned_type (unsignedp0
3521 || TYPE_UNSIGNED (*restype_ptr),
3522 type);
3523 /* Make sure shorter operand is extended the right way
3524 to match the longer operand. */
3525 primop0
3526 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3527 TREE_TYPE (primop0)),
3528 primop0);
3529 primop1
3530 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3531 TREE_TYPE (primop1)),
3532 primop1);
3534 else
3536 /* Here we must do the comparison on the nominal type
3537 using the args exactly as we received them. */
3538 type = *restype_ptr;
3539 primop0 = op0;
3540 primop1 = op1;
3542 if (!real1 && !real2 && integer_zerop (primop1)
3543 && TYPE_UNSIGNED (*restype_ptr))
3545 tree value = 0;
3546 switch (code)
3548 case GE_EXPR:
3549 /* All unsigned values are >= 0, so we warn. However,
3550 if OP0 is a constant that is >= 0, the signedness of
3551 the comparison isn't an issue, so suppress the
3552 warning. */
3553 if (warn_type_limits && !in_system_header
3554 && !(TREE_CODE (primop0) == INTEGER_CST
3555 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3556 primop0))))
3557 warning (OPT_Wtype_limits,
3558 "comparison of unsigned expression >= 0 is always true");
3559 value = truthvalue_true_node;
3560 break;
3562 case LT_EXPR:
3563 if (warn_type_limits && !in_system_header
3564 && !(TREE_CODE (primop0) == INTEGER_CST
3565 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3566 primop0))))
3567 warning (OPT_Wtype_limits,
3568 "comparison of unsigned expression < 0 is always false");
3569 value = truthvalue_false_node;
3570 break;
3572 default:
3573 break;
3576 if (value != 0)
3578 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3579 if (TREE_SIDE_EFFECTS (primop0))
3580 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3581 primop0, value);
3582 return value;
3587 *op0_ptr = convert (type, primop0);
3588 *op1_ptr = convert (type, primop1);
3590 *restype_ptr = truthvalue_type_node;
3592 return 0;
3595 /* Return a tree for the sum or difference (RESULTCODE says which)
3596 of pointer PTROP and integer INTOP. */
3598 tree
3599 pointer_int_sum (location_t loc, enum tree_code resultcode,
3600 tree ptrop, tree intop)
3602 tree size_exp, ret;
3604 /* The result is a pointer of the same type that is being added. */
3605 tree result_type = TREE_TYPE (ptrop);
3607 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3609 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3610 "pointer of type %<void *%> used in arithmetic");
3611 size_exp = integer_one_node;
3613 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3615 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3616 "pointer to a function used in arithmetic");
3617 size_exp = integer_one_node;
3619 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3621 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3622 "pointer to member function used in arithmetic");
3623 size_exp = integer_one_node;
3625 else
3626 size_exp = size_in_bytes (TREE_TYPE (result_type));
3628 /* We are manipulating pointer values, so we don't need to warn
3629 about relying on undefined signed overflow. We disable the
3630 warning here because we use integer types so fold won't know that
3631 they are really pointers. */
3632 fold_defer_overflow_warnings ();
3634 /* If what we are about to multiply by the size of the elements
3635 contains a constant term, apply distributive law
3636 and multiply that constant term separately.
3637 This helps produce common subexpressions. */
3638 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3639 && !TREE_CONSTANT (intop)
3640 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3641 && TREE_CONSTANT (size_exp)
3642 /* If the constant comes from pointer subtraction,
3643 skip this optimization--it would cause an error. */
3644 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3645 /* If the constant is unsigned, and smaller than the pointer size,
3646 then we must skip this optimization. This is because it could cause
3647 an overflow error if the constant is negative but INTOP is not. */
3648 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3649 || (TYPE_PRECISION (TREE_TYPE (intop))
3650 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3652 enum tree_code subcode = resultcode;
3653 tree int_type = TREE_TYPE (intop);
3654 if (TREE_CODE (intop) == MINUS_EXPR)
3655 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3656 /* Convert both subexpression types to the type of intop,
3657 because weird cases involving pointer arithmetic
3658 can result in a sum or difference with different type args. */
3659 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3660 subcode, ptrop,
3661 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3662 intop = convert (int_type, TREE_OPERAND (intop, 0));
3665 /* Convert the integer argument to a type the same size as sizetype
3666 so the multiply won't overflow spuriously. */
3667 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3668 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3669 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3670 TYPE_UNSIGNED (sizetype)), intop);
3672 /* Replace the integer argument with a suitable product by the object size.
3673 Do this multiplication as signed, then convert to the appropriate type
3674 for the pointer operation and disregard an overflow that occured only
3675 because of the sign-extension change in the latter conversion. */
3677 tree t = build_binary_op (loc,
3678 MULT_EXPR, intop,
3679 convert (TREE_TYPE (intop), size_exp), 1);
3680 intop = convert (sizetype, t);
3681 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3682 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
3683 TREE_INT_CST_HIGH (intop));
3686 /* Create the sum or difference. */
3687 if (resultcode == MINUS_EXPR)
3688 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3690 ret = fold_build2_loc (loc, POINTER_PLUS_EXPR, result_type, ptrop, intop);
3692 fold_undefer_and_ignore_overflow_warnings ();
3694 return ret;
3697 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3698 and if NON_CONST is known not to be permitted in an evaluated part
3699 of a constant expression. */
3701 tree
3702 c_wrap_maybe_const (tree expr, bool non_const)
3704 bool nowarning = TREE_NO_WARNING (expr);
3705 location_t loc = EXPR_LOCATION (expr);
3707 /* This should never be called for C++. */
3708 if (c_dialect_cxx ())
3709 gcc_unreachable ();
3711 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3712 STRIP_TYPE_NOPS (expr);
3713 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3714 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3715 if (nowarning)
3716 TREE_NO_WARNING (expr) = 1;
3717 protected_set_expr_location (expr, loc);
3719 return expr;
3722 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
3723 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3724 around the SAVE_EXPR if needed so that c_fully_fold does not need
3725 to look inside SAVE_EXPRs. */
3727 tree
3728 c_save_expr (tree expr)
3730 bool maybe_const = true;
3731 if (c_dialect_cxx ())
3732 return save_expr (expr);
3733 expr = c_fully_fold (expr, false, &maybe_const);
3734 expr = save_expr (expr);
3735 if (!maybe_const)
3736 expr = c_wrap_maybe_const (expr, true);
3737 return expr;
3740 /* Return whether EXPR is a declaration whose address can never be
3741 NULL. */
3743 bool
3744 decl_with_nonnull_addr_p (const_tree expr)
3746 return (DECL_P (expr)
3747 && (TREE_CODE (expr) == PARM_DECL
3748 || TREE_CODE (expr) == LABEL_DECL
3749 || !DECL_WEAK (expr)));
3752 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3753 or for an `if' or `while' statement or ?..: exp. It should already
3754 have been validated to be of suitable type; otherwise, a bad
3755 diagnostic may result.
3757 The EXPR is located at LOCATION.
3759 This preparation consists of taking the ordinary
3760 representation of an expression expr and producing a valid tree
3761 boolean expression describing whether expr is nonzero. We could
3762 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3763 but we optimize comparisons, &&, ||, and !.
3765 The resulting type should always be `truthvalue_type_node'. */
3767 tree
3768 c_common_truthvalue_conversion (location_t location, tree expr)
3770 switch (TREE_CODE (expr))
3772 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3773 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3774 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3775 case ORDERED_EXPR: case UNORDERED_EXPR:
3776 if (TREE_TYPE (expr) == truthvalue_type_node)
3777 return expr;
3778 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3779 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3780 goto ret;
3782 case TRUTH_ANDIF_EXPR:
3783 case TRUTH_ORIF_EXPR:
3784 case TRUTH_AND_EXPR:
3785 case TRUTH_OR_EXPR:
3786 case TRUTH_XOR_EXPR:
3787 if (TREE_TYPE (expr) == truthvalue_type_node)
3788 return expr;
3789 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3790 c_common_truthvalue_conversion (location,
3791 TREE_OPERAND (expr, 0)),
3792 c_common_truthvalue_conversion (location,
3793 TREE_OPERAND (expr, 1)));
3794 goto ret;
3796 case TRUTH_NOT_EXPR:
3797 if (TREE_TYPE (expr) == truthvalue_type_node)
3798 return expr;
3799 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3800 c_common_truthvalue_conversion (location,
3801 TREE_OPERAND (expr, 0)));
3802 goto ret;
3804 case ERROR_MARK:
3805 return expr;
3807 case INTEGER_CST:
3808 return integer_zerop (expr) ? truthvalue_false_node
3809 : truthvalue_true_node;
3811 case REAL_CST:
3812 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3813 ? truthvalue_true_node
3814 : truthvalue_false_node;
3816 case FIXED_CST:
3817 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3818 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3819 ? truthvalue_true_node
3820 : truthvalue_false_node;
3822 case FUNCTION_DECL:
3823 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
3824 /* Fall through. */
3826 case ADDR_EXPR:
3828 tree inner = TREE_OPERAND (expr, 0);
3829 if (decl_with_nonnull_addr_p (inner))
3831 /* Common Ada/Pascal programmer's mistake. */
3832 warning_at (location,
3833 OPT_Waddress,
3834 "the address of %qD will always evaluate as %<true%>",
3835 inner);
3836 return truthvalue_true_node;
3838 break;
3841 case COMPLEX_EXPR:
3842 expr = build_binary_op (EXPR_LOCATION (expr),
3843 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3844 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3845 c_common_truthvalue_conversion (location,
3846 TREE_OPERAND (expr, 0)),
3847 c_common_truthvalue_conversion (location,
3848 TREE_OPERAND (expr, 1)),
3850 goto ret;
3852 case NEGATE_EXPR:
3853 case ABS_EXPR:
3854 case FLOAT_EXPR:
3855 case EXCESS_PRECISION_EXPR:
3856 /* These don't change whether an object is nonzero or zero. */
3857 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3859 case LROTATE_EXPR:
3860 case RROTATE_EXPR:
3861 /* These don't change whether an object is zero or nonzero, but
3862 we can't ignore them if their second arg has side-effects. */
3863 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3865 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3866 TREE_OPERAND (expr, 1),
3867 c_common_truthvalue_conversion
3868 (location, TREE_OPERAND (expr, 0)));
3869 goto ret;
3871 else
3872 return c_common_truthvalue_conversion (location,
3873 TREE_OPERAND (expr, 0));
3875 case COND_EXPR:
3876 /* Distribute the conversion into the arms of a COND_EXPR. */
3877 if (c_dialect_cxx ())
3879 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
3880 TREE_OPERAND (expr, 0),
3881 c_common_truthvalue_conversion (location,
3882 TREE_OPERAND (expr,
3883 1)),
3884 c_common_truthvalue_conversion (location,
3885 TREE_OPERAND (expr,
3886 2)));
3887 goto ret;
3889 else
3891 /* Folding will happen later for C. */
3892 expr = build3 (COND_EXPR, truthvalue_type_node,
3893 TREE_OPERAND (expr, 0),
3894 c_common_truthvalue_conversion (location,
3895 TREE_OPERAND (expr, 1)),
3896 c_common_truthvalue_conversion (location,
3897 TREE_OPERAND (expr, 2)));
3898 goto ret;
3901 CASE_CONVERT:
3902 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3903 since that affects how `default_conversion' will behave. */
3904 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
3905 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
3906 break;
3907 /* If this is widening the argument, we can ignore it. */
3908 if (TYPE_PRECISION (TREE_TYPE (expr))
3909 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3910 return c_common_truthvalue_conversion (location,
3911 TREE_OPERAND (expr, 0));
3912 break;
3914 case MODIFY_EXPR:
3915 if (!TREE_NO_WARNING (expr)
3916 && warn_parentheses)
3918 warning (OPT_Wparentheses,
3919 "suggest parentheses around assignment used as truth value");
3920 TREE_NO_WARNING (expr) = 1;
3922 break;
3924 default:
3925 break;
3928 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3930 tree t = c_save_expr (expr);
3931 expr = (build_binary_op
3932 (EXPR_LOCATION (expr),
3933 (TREE_SIDE_EFFECTS (expr)
3934 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3935 c_common_truthvalue_conversion
3936 (location,
3937 build_unary_op (location, REALPART_EXPR, t, 0)),
3938 c_common_truthvalue_conversion
3939 (location,
3940 build_unary_op (location, IMAGPART_EXPR, t, 0)),
3941 0));
3942 goto ret;
3945 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3947 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3948 FCONST0 (TYPE_MODE
3949 (TREE_TYPE (expr))));
3950 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
3952 else
3953 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
3955 ret:
3956 protected_set_expr_location (expr, location);
3957 return expr;
3960 static void def_builtin_1 (enum built_in_function fncode,
3961 const char *name,
3962 enum built_in_class fnclass,
3963 tree fntype, tree libtype,
3964 bool both_p, bool fallback_p, bool nonansi_p,
3965 tree fnattrs, bool implicit_p);
3968 /* Apply the TYPE_QUALS to the new DECL. */
3970 void
3971 c_apply_type_quals_to_decl (int type_quals, tree decl)
3973 tree type = TREE_TYPE (decl);
3975 if (type == error_mark_node)
3976 return;
3978 if (((type_quals & TYPE_QUAL_CONST)
3979 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3980 /* An object declared 'const' is only readonly after it is
3981 initialized. We don't have any way of expressing this currently,
3982 so we need to be conservative and unset TREE_READONLY for types
3983 with constructors. Otherwise aliasing code will ignore stores in
3984 an inline constructor. */
3985 && !(type && TYPE_NEEDS_CONSTRUCTING (type)))
3986 TREE_READONLY (decl) = 1;
3987 if (type_quals & TYPE_QUAL_VOLATILE)
3989 TREE_SIDE_EFFECTS (decl) = 1;
3990 TREE_THIS_VOLATILE (decl) = 1;
3992 if (type_quals & TYPE_QUAL_RESTRICT)
3994 while (type && TREE_CODE (type) == ARRAY_TYPE)
3995 /* Allow 'restrict' on arrays of pointers.
3996 FIXME currently we just ignore it. */
3997 type = TREE_TYPE (type);
3998 if (!type
3999 || !POINTER_TYPE_P (type)
4000 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4001 error ("invalid use of %<restrict%>");
4005 /* Hash function for the problem of multiple type definitions in
4006 different files. This must hash all types that will compare
4007 equal via comptypes to the same value. In practice it hashes
4008 on some of the simple stuff and leaves the details to comptypes. */
4010 static hashval_t
4011 c_type_hash (const void *p)
4013 int i = 0;
4014 int shift, size;
4015 const_tree const t = (const_tree) p;
4016 tree t2;
4017 switch (TREE_CODE (t))
4019 /* For pointers, hash on pointee type plus some swizzling. */
4020 case POINTER_TYPE:
4021 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4022 /* Hash on number of elements and total size. */
4023 case ENUMERAL_TYPE:
4024 shift = 3;
4025 t2 = TYPE_VALUES (t);
4026 break;
4027 case RECORD_TYPE:
4028 shift = 0;
4029 t2 = TYPE_FIELDS (t);
4030 break;
4031 case QUAL_UNION_TYPE:
4032 shift = 1;
4033 t2 = TYPE_FIELDS (t);
4034 break;
4035 case UNION_TYPE:
4036 shift = 2;
4037 t2 = TYPE_FIELDS (t);
4038 break;
4039 default:
4040 gcc_unreachable ();
4042 for (; t2; t2 = DECL_CHAIN (t2))
4043 i++;
4044 /* We might have a VLA here. */
4045 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4046 size = 0;
4047 else
4048 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4049 return ((size << 24) | (i << shift));
4052 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4054 /* Return the typed-based alias set for T, which may be an expression
4055 or a type. Return -1 if we don't do anything special. */
4057 alias_set_type
4058 c_common_get_alias_set (tree t)
4060 tree u;
4061 PTR *slot;
4063 /* For VLAs, use the alias set of the element type rather than the
4064 default of alias set 0 for types compared structurally. */
4065 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4067 if (TREE_CODE (t) == ARRAY_TYPE)
4068 return get_alias_set (TREE_TYPE (t));
4069 return -1;
4072 /* Permit type-punning when accessing a union, provided the access
4073 is directly through the union. For example, this code does not
4074 permit taking the address of a union member and then storing
4075 through it. Even the type-punning allowed here is a GCC
4076 extension, albeit a common and useful one; the C standard says
4077 that such accesses have implementation-defined behavior. */
4078 for (u = t;
4079 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4080 u = TREE_OPERAND (u, 0))
4081 if (TREE_CODE (u) == COMPONENT_REF
4082 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4083 return 0;
4085 /* That's all the expressions we handle specially. */
4086 if (!TYPE_P (t))
4087 return -1;
4089 /* The C standard guarantees that any object may be accessed via an
4090 lvalue that has character type. */
4091 if (t == char_type_node
4092 || t == signed_char_type_node
4093 || t == unsigned_char_type_node)
4094 return 0;
4096 /* The C standard specifically allows aliasing between signed and
4097 unsigned variants of the same type. We treat the signed
4098 variant as canonical. */
4099 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4101 tree t1 = c_common_signed_type (t);
4103 /* t1 == t can happen for boolean nodes which are always unsigned. */
4104 if (t1 != t)
4105 return get_alias_set (t1);
4108 /* Handle the case of multiple type nodes referring to "the same" type,
4109 which occurs with IMA. These share an alias set. FIXME: Currently only
4110 C90 is handled. (In C99 type compatibility is not transitive, which
4111 complicates things mightily. The alias set splay trees can theoretically
4112 represent this, but insertion is tricky when you consider all the
4113 different orders things might arrive in.) */
4115 if (c_language != clk_c || flag_isoc99)
4116 return -1;
4118 /* Save time if there's only one input file. */
4119 if (num_in_fnames == 1)
4120 return -1;
4122 /* Pointers need special handling if they point to any type that
4123 needs special handling (below). */
4124 if (TREE_CODE (t) == POINTER_TYPE)
4126 tree t2;
4127 /* Find bottom type under any nested POINTERs. */
4128 for (t2 = TREE_TYPE (t);
4129 TREE_CODE (t2) == POINTER_TYPE;
4130 t2 = TREE_TYPE (t2))
4132 if (TREE_CODE (t2) != RECORD_TYPE
4133 && TREE_CODE (t2) != ENUMERAL_TYPE
4134 && TREE_CODE (t2) != QUAL_UNION_TYPE
4135 && TREE_CODE (t2) != UNION_TYPE)
4136 return -1;
4137 if (TYPE_SIZE (t2) == 0)
4138 return -1;
4140 /* These are the only cases that need special handling. */
4141 if (TREE_CODE (t) != RECORD_TYPE
4142 && TREE_CODE (t) != ENUMERAL_TYPE
4143 && TREE_CODE (t) != QUAL_UNION_TYPE
4144 && TREE_CODE (t) != UNION_TYPE
4145 && TREE_CODE (t) != POINTER_TYPE)
4146 return -1;
4147 /* Undefined? */
4148 if (TYPE_SIZE (t) == 0)
4149 return -1;
4151 /* Look up t in hash table. Only one of the compatible types within each
4152 alias set is recorded in the table. */
4153 if (!type_hash_table)
4154 type_hash_table = htab_create_ggc (1021, c_type_hash,
4155 (htab_eq) lang_hooks.types_compatible_p,
4156 NULL);
4157 slot = htab_find_slot (type_hash_table, t, INSERT);
4158 if (*slot != NULL)
4160 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4161 return TYPE_ALIAS_SET ((tree)*slot);
4163 else
4164 /* Our caller will assign and record (in t) a new alias set; all we need
4165 to do is remember t in the hash table. */
4166 *slot = t;
4168 return -1;
4171 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4172 the second parameter indicates which OPERATOR is being applied.
4173 The COMPLAIN flag controls whether we should diagnose possibly
4174 ill-formed constructs or not. LOC is the location of the SIZEOF or
4175 TYPEOF operator. */
4177 tree
4178 c_sizeof_or_alignof_type (location_t loc,
4179 tree type, bool is_sizeof, int complain)
4181 const char *op_name;
4182 tree value = NULL;
4183 enum tree_code type_code = TREE_CODE (type);
4185 op_name = is_sizeof ? "sizeof" : "__alignof__";
4187 if (type_code == FUNCTION_TYPE)
4189 if (is_sizeof)
4191 if (complain && (pedantic || warn_pointer_arith))
4192 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4193 "invalid application of %<sizeof%> to a function type");
4194 else if (!complain)
4195 return error_mark_node;
4196 value = size_one_node;
4198 else
4199 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4201 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4203 if (type_code == VOID_TYPE
4204 && complain && (pedantic || warn_pointer_arith))
4205 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4206 "invalid application of %qs to a void type", op_name);
4207 else if (!complain)
4208 return error_mark_node;
4209 value = size_one_node;
4211 else if (!COMPLETE_TYPE_P (type))
4213 if (complain)
4214 error_at (loc, "invalid application of %qs to incomplete type %qT ",
4215 op_name, type);
4216 return error_mark_node;
4218 else
4220 if (is_sizeof)
4221 /* Convert in case a char is more than one unit. */
4222 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4223 size_int (TYPE_PRECISION (char_type_node)
4224 / BITS_PER_UNIT));
4225 else
4226 value = size_int (TYPE_ALIGN_UNIT (type));
4229 /* VALUE will have an integer type with TYPE_IS_SIZETYPE set.
4230 TYPE_IS_SIZETYPE means that certain things (like overflow) will
4231 never happen. However, this node should really have type
4232 `size_t', which is just a typedef for an ordinary integer type. */
4233 value = fold_convert_loc (loc, size_type_node, value);
4234 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
4236 return value;
4239 /* Implement the __alignof keyword: Return the minimum required
4240 alignment of EXPR, measured in bytes. For VAR_DECLs,
4241 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4242 from an "aligned" __attribute__ specification). LOC is the
4243 location of the ALIGNOF operator. */
4245 tree
4246 c_alignof_expr (location_t loc, tree expr)
4248 tree t;
4250 if (VAR_OR_FUNCTION_DECL_P (expr))
4251 t = size_int (DECL_ALIGN_UNIT (expr));
4253 else if (TREE_CODE (expr) == COMPONENT_REF
4254 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4256 error_at (loc, "%<__alignof%> applied to a bit-field");
4257 t = size_one_node;
4259 else if (TREE_CODE (expr) == COMPONENT_REF
4260 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4261 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4263 else if (TREE_CODE (expr) == INDIRECT_REF)
4265 tree t = TREE_OPERAND (expr, 0);
4266 tree best = t;
4267 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4269 while (CONVERT_EXPR_P (t)
4270 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4272 int thisalign;
4274 t = TREE_OPERAND (t, 0);
4275 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4276 if (thisalign > bestalign)
4277 best = t, bestalign = thisalign;
4279 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4281 else
4282 return c_alignof (loc, TREE_TYPE (expr));
4284 return fold_convert_loc (loc, size_type_node, t);
4287 /* Handle C and C++ default attributes. */
4289 enum built_in_attribute
4291 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4292 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4293 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4294 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4295 #include "builtin-attrs.def"
4296 #undef DEF_ATTR_NULL_TREE
4297 #undef DEF_ATTR_INT
4298 #undef DEF_ATTR_IDENT
4299 #undef DEF_ATTR_TREE_LIST
4300 ATTR_LAST
4303 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4305 static void c_init_attributes (void);
4307 enum c_builtin_type
4309 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4310 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4311 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4312 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4313 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4314 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4315 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4316 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4317 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4318 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4319 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4320 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4321 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4322 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4323 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4324 NAME,
4325 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4326 #include "builtin-types.def"
4327 #undef DEF_PRIMITIVE_TYPE
4328 #undef DEF_FUNCTION_TYPE_0
4329 #undef DEF_FUNCTION_TYPE_1
4330 #undef DEF_FUNCTION_TYPE_2
4331 #undef DEF_FUNCTION_TYPE_3
4332 #undef DEF_FUNCTION_TYPE_4
4333 #undef DEF_FUNCTION_TYPE_5
4334 #undef DEF_FUNCTION_TYPE_6
4335 #undef DEF_FUNCTION_TYPE_7
4336 #undef DEF_FUNCTION_TYPE_VAR_0
4337 #undef DEF_FUNCTION_TYPE_VAR_1
4338 #undef DEF_FUNCTION_TYPE_VAR_2
4339 #undef DEF_FUNCTION_TYPE_VAR_3
4340 #undef DEF_FUNCTION_TYPE_VAR_4
4341 #undef DEF_FUNCTION_TYPE_VAR_5
4342 #undef DEF_POINTER_TYPE
4343 BT_LAST
4346 typedef enum c_builtin_type builtin_type;
4348 /* A temporary array for c_common_nodes_and_builtins. Used in
4349 communication with def_fn_type. */
4350 static tree builtin_types[(int) BT_LAST + 1];
4352 /* A helper function for c_common_nodes_and_builtins. Build function type
4353 for DEF with return type RET and N arguments. If VAR is true, then the
4354 function should be variadic after those N arguments.
4356 Takes special care not to ICE if any of the types involved are
4357 error_mark_node, which indicates that said type is not in fact available
4358 (see builtin_type_for_size). In which case the function type as a whole
4359 should be error_mark_node. */
4361 static void
4362 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4364 tree args = NULL, t;
4365 va_list list;
4366 int i;
4368 va_start (list, n);
4369 for (i = 0; i < n; ++i)
4371 builtin_type a = (builtin_type) va_arg (list, int);
4372 t = builtin_types[a];
4373 if (t == error_mark_node)
4374 goto egress;
4375 args = tree_cons (NULL_TREE, t, args);
4377 va_end (list);
4379 args = nreverse (args);
4380 if (!var)
4381 args = chainon (args, void_list_node);
4383 t = builtin_types[ret];
4384 if (t == error_mark_node)
4385 goto egress;
4386 t = build_function_type (t, args);
4388 egress:
4389 builtin_types[def] = t;
4392 /* Build builtin functions common to both C and C++ language
4393 frontends. */
4395 static void
4396 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4398 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4399 builtin_types[ENUM] = VALUE;
4400 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4401 def_fn_type (ENUM, RETURN, 0, 0);
4402 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4403 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4404 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4405 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4406 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4407 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4408 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4409 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4410 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4411 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4412 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4413 ARG6) \
4414 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4415 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4416 ARG6, ARG7) \
4417 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4418 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4419 def_fn_type (ENUM, RETURN, 1, 0);
4420 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4421 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4422 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4423 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4424 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4425 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4426 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4427 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4428 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4429 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4430 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4431 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4433 #include "builtin-types.def"
4435 #undef DEF_PRIMITIVE_TYPE
4436 #undef DEF_FUNCTION_TYPE_1
4437 #undef DEF_FUNCTION_TYPE_2
4438 #undef DEF_FUNCTION_TYPE_3
4439 #undef DEF_FUNCTION_TYPE_4
4440 #undef DEF_FUNCTION_TYPE_5
4441 #undef DEF_FUNCTION_TYPE_6
4442 #undef DEF_FUNCTION_TYPE_VAR_0
4443 #undef DEF_FUNCTION_TYPE_VAR_1
4444 #undef DEF_FUNCTION_TYPE_VAR_2
4445 #undef DEF_FUNCTION_TYPE_VAR_3
4446 #undef DEF_FUNCTION_TYPE_VAR_4
4447 #undef DEF_FUNCTION_TYPE_VAR_5
4448 #undef DEF_POINTER_TYPE
4449 builtin_types[(int) BT_LAST] = NULL_TREE;
4451 c_init_attributes ();
4453 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4454 NONANSI_P, ATTRS, IMPLICIT, COND) \
4455 if (NAME && COND) \
4456 def_builtin_1 (ENUM, NAME, CLASS, \
4457 builtin_types[(int) TYPE], \
4458 builtin_types[(int) LIBTYPE], \
4459 BOTH_P, FALLBACK_P, NONANSI_P, \
4460 built_in_attributes[(int) ATTRS], IMPLICIT);
4461 #include "builtins.def"
4462 #undef DEF_BUILTIN
4464 targetm.init_builtins ();
4466 build_common_builtin_nodes ();
4468 if (flag_mudflap)
4469 mudflap_init ();
4472 /* Like get_identifier, but avoid warnings about null arguments when
4473 the argument may be NULL for targets where GCC lacks stdint.h type
4474 information. */
4476 static inline tree
4477 c_get_ident (const char *id)
4479 return get_identifier (id);
4482 /* Build tree nodes and builtin functions common to both C and C++ language
4483 frontends. */
4485 void
4486 c_common_nodes_and_builtins (void)
4488 int char16_type_size;
4489 int char32_type_size;
4490 int wchar_type_size;
4491 tree array_domain_type;
4492 tree va_list_ref_type_node;
4493 tree va_list_arg_type_node;
4495 /* Define `int' and `char' first so that dbx will output them first. */
4496 record_builtin_type (RID_INT, NULL, integer_type_node);
4497 record_builtin_type (RID_CHAR, "char", char_type_node);
4499 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4500 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4501 but not C. Are the conditionals here needed? */
4502 if (c_dialect_cxx ())
4503 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4504 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4505 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4506 record_builtin_type (RID_MAX, "long unsigned int",
4507 long_unsigned_type_node);
4508 if (int128_integer_type_node != NULL_TREE)
4510 record_builtin_type (RID_INT128, "__int128",
4511 int128_integer_type_node);
4512 record_builtin_type (RID_MAX, "__int128 unsigned",
4513 int128_unsigned_type_node);
4515 if (c_dialect_cxx ())
4516 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4517 record_builtin_type (RID_MAX, "long long int",
4518 long_long_integer_type_node);
4519 record_builtin_type (RID_MAX, "long long unsigned int",
4520 long_long_unsigned_type_node);
4521 if (c_dialect_cxx ())
4522 record_builtin_type (RID_MAX, "long long unsigned",
4523 long_long_unsigned_type_node);
4524 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4525 record_builtin_type (RID_MAX, "short unsigned int",
4526 short_unsigned_type_node);
4527 if (c_dialect_cxx ())
4528 record_builtin_type (RID_MAX, "unsigned short",
4529 short_unsigned_type_node);
4531 /* Define both `signed char' and `unsigned char'. */
4532 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4533 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4535 /* These are types that c_common_type_for_size and
4536 c_common_type_for_mode use. */
4537 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4538 TYPE_DECL, NULL_TREE,
4539 intQI_type_node));
4540 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4541 TYPE_DECL, NULL_TREE,
4542 intHI_type_node));
4543 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4544 TYPE_DECL, NULL_TREE,
4545 intSI_type_node));
4546 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4547 TYPE_DECL, NULL_TREE,
4548 intDI_type_node));
4549 #if HOST_BITS_PER_WIDE_INT >= 64
4550 if (targetm.scalar_mode_supported_p (TImode))
4551 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4552 TYPE_DECL,
4553 get_identifier ("__int128_t"),
4554 intTI_type_node));
4555 #endif
4556 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4557 TYPE_DECL, NULL_TREE,
4558 unsigned_intQI_type_node));
4559 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4560 TYPE_DECL, NULL_TREE,
4561 unsigned_intHI_type_node));
4562 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4563 TYPE_DECL, NULL_TREE,
4564 unsigned_intSI_type_node));
4565 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4566 TYPE_DECL, NULL_TREE,
4567 unsigned_intDI_type_node));
4568 #if HOST_BITS_PER_WIDE_INT >= 64
4569 if (targetm.scalar_mode_supported_p (TImode))
4570 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4571 TYPE_DECL,
4572 get_identifier ("__uint128_t"),
4573 unsigned_intTI_type_node));
4574 #endif
4576 /* Create the widest literal types. */
4577 widest_integer_literal_type_node
4578 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
4579 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4580 TYPE_DECL, NULL_TREE,
4581 widest_integer_literal_type_node));
4583 widest_unsigned_literal_type_node
4584 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
4585 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4586 TYPE_DECL, NULL_TREE,
4587 widest_unsigned_literal_type_node));
4589 /* `unsigned long' is the standard type for sizeof.
4590 Note that stddef.h uses `unsigned long',
4591 and this must agree, even if long and int are the same size. */
4592 size_type_node =
4593 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
4594 signed_size_type_node = c_common_signed_type (size_type_node);
4595 set_sizetype (size_type_node);
4597 pid_type_node =
4598 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4600 build_common_tree_nodes_2 (flag_short_double);
4602 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4603 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4604 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4606 /* Only supported decimal floating point extension if the target
4607 actually supports underlying modes. */
4608 if (targetm.scalar_mode_supported_p (SDmode)
4609 && targetm.scalar_mode_supported_p (DDmode)
4610 && targetm.scalar_mode_supported_p (TDmode))
4612 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4613 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4614 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4617 if (targetm.fixed_point_supported_p ())
4619 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4620 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4621 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4622 record_builtin_type (RID_MAX, "long long _Fract",
4623 long_long_fract_type_node);
4624 record_builtin_type (RID_MAX, "unsigned short _Fract",
4625 unsigned_short_fract_type_node);
4626 record_builtin_type (RID_MAX, "unsigned _Fract",
4627 unsigned_fract_type_node);
4628 record_builtin_type (RID_MAX, "unsigned long _Fract",
4629 unsigned_long_fract_type_node);
4630 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4631 unsigned_long_long_fract_type_node);
4632 record_builtin_type (RID_MAX, "_Sat short _Fract",
4633 sat_short_fract_type_node);
4634 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4635 record_builtin_type (RID_MAX, "_Sat long _Fract",
4636 sat_long_fract_type_node);
4637 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4638 sat_long_long_fract_type_node);
4639 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4640 sat_unsigned_short_fract_type_node);
4641 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4642 sat_unsigned_fract_type_node);
4643 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4644 sat_unsigned_long_fract_type_node);
4645 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4646 sat_unsigned_long_long_fract_type_node);
4647 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4648 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4649 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4650 record_builtin_type (RID_MAX, "long long _Accum",
4651 long_long_accum_type_node);
4652 record_builtin_type (RID_MAX, "unsigned short _Accum",
4653 unsigned_short_accum_type_node);
4654 record_builtin_type (RID_MAX, "unsigned _Accum",
4655 unsigned_accum_type_node);
4656 record_builtin_type (RID_MAX, "unsigned long _Accum",
4657 unsigned_long_accum_type_node);
4658 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4659 unsigned_long_long_accum_type_node);
4660 record_builtin_type (RID_MAX, "_Sat short _Accum",
4661 sat_short_accum_type_node);
4662 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4663 record_builtin_type (RID_MAX, "_Sat long _Accum",
4664 sat_long_accum_type_node);
4665 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4666 sat_long_long_accum_type_node);
4667 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4668 sat_unsigned_short_accum_type_node);
4669 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4670 sat_unsigned_accum_type_node);
4671 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4672 sat_unsigned_long_accum_type_node);
4673 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4674 sat_unsigned_long_long_accum_type_node);
4678 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4679 TYPE_DECL,
4680 get_identifier ("complex int"),
4681 complex_integer_type_node));
4682 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4683 TYPE_DECL,
4684 get_identifier ("complex float"),
4685 complex_float_type_node));
4686 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4687 TYPE_DECL,
4688 get_identifier ("complex double"),
4689 complex_double_type_node));
4690 lang_hooks.decls.pushdecl
4691 (build_decl (UNKNOWN_LOCATION,
4692 TYPE_DECL, get_identifier ("complex long double"),
4693 complex_long_double_type_node));
4695 if (c_dialect_cxx ())
4696 /* For C++, make fileptr_type_node a distinct void * type until
4697 FILE type is defined. */
4698 fileptr_type_node = build_variant_type_copy (ptr_type_node);
4700 record_builtin_type (RID_VOID, NULL, void_type_node);
4702 /* Set the TYPE_NAME for any variants that were built before
4703 record_builtin_type gave names to the built-in types. */
4705 tree void_name = TYPE_NAME (void_type_node);
4706 TYPE_NAME (void_type_node) = NULL_TREE;
4707 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4708 = void_name;
4709 TYPE_NAME (void_type_node) = void_name;
4712 /* This node must not be shared. */
4713 void_zero_node = make_node (INTEGER_CST);
4714 TREE_TYPE (void_zero_node) = void_type_node;
4716 void_list_node = build_void_list_node ();
4718 /* Make a type to be the domain of a few array types
4719 whose domains don't really matter.
4720 200 is small enough that it always fits in size_t
4721 and large enough that it can hold most function names for the
4722 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4723 array_domain_type = build_index_type (size_int (200));
4725 /* Make a type for arrays of characters.
4726 With luck nothing will ever really depend on the length of this
4727 array type. */
4728 char_array_type_node
4729 = build_array_type (char_type_node, array_domain_type);
4731 /* Likewise for arrays of ints. */
4732 int_array_type_node
4733 = build_array_type (integer_type_node, array_domain_type);
4735 string_type_node = build_pointer_type (char_type_node);
4736 const_string_type_node
4737 = build_pointer_type (build_qualified_type
4738 (char_type_node, TYPE_QUAL_CONST));
4740 /* This is special for C++ so functions can be overloaded. */
4741 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4742 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4743 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4744 underlying_wchar_type_node = wchar_type_node;
4745 if (c_dialect_cxx ())
4747 if (TYPE_UNSIGNED (wchar_type_node))
4748 wchar_type_node = make_unsigned_type (wchar_type_size);
4749 else
4750 wchar_type_node = make_signed_type (wchar_type_size);
4751 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4754 /* This is for wide string constants. */
4755 wchar_array_type_node
4756 = build_array_type (wchar_type_node, array_domain_type);
4758 /* Define 'char16_t'. */
4759 char16_type_node = get_identifier (CHAR16_TYPE);
4760 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4761 char16_type_size = TYPE_PRECISION (char16_type_node);
4762 if (c_dialect_cxx ())
4764 char16_type_node = make_unsigned_type (char16_type_size);
4766 if (cxx_dialect == cxx0x)
4767 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4770 /* This is for UTF-16 string constants. */
4771 char16_array_type_node
4772 = build_array_type (char16_type_node, array_domain_type);
4774 /* Define 'char32_t'. */
4775 char32_type_node = get_identifier (CHAR32_TYPE);
4776 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4777 char32_type_size = TYPE_PRECISION (char32_type_node);
4778 if (c_dialect_cxx ())
4780 char32_type_node = make_unsigned_type (char32_type_size);
4782 if (cxx_dialect == cxx0x)
4783 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4786 /* This is for UTF-32 string constants. */
4787 char32_array_type_node
4788 = build_array_type (char32_type_node, array_domain_type);
4790 wint_type_node =
4791 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4793 intmax_type_node =
4794 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4795 uintmax_type_node =
4796 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4798 if (SIG_ATOMIC_TYPE)
4799 sig_atomic_type_node =
4800 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4801 if (INT8_TYPE)
4802 int8_type_node =
4803 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4804 if (INT16_TYPE)
4805 int16_type_node =
4806 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4807 if (INT32_TYPE)
4808 int32_type_node =
4809 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4810 if (INT64_TYPE)
4811 int64_type_node =
4812 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4813 if (UINT8_TYPE)
4814 uint8_type_node =
4815 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4816 if (UINT16_TYPE)
4817 uint16_type_node =
4818 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4819 if (UINT32_TYPE)
4820 c_uint32_type_node =
4821 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4822 if (UINT64_TYPE)
4823 c_uint64_type_node =
4824 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4825 if (INT_LEAST8_TYPE)
4826 int_least8_type_node =
4827 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4828 if (INT_LEAST16_TYPE)
4829 int_least16_type_node =
4830 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4831 if (INT_LEAST32_TYPE)
4832 int_least32_type_node =
4833 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4834 if (INT_LEAST64_TYPE)
4835 int_least64_type_node =
4836 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4837 if (UINT_LEAST8_TYPE)
4838 uint_least8_type_node =
4839 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4840 if (UINT_LEAST16_TYPE)
4841 uint_least16_type_node =
4842 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4843 if (UINT_LEAST32_TYPE)
4844 uint_least32_type_node =
4845 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4846 if (UINT_LEAST64_TYPE)
4847 uint_least64_type_node =
4848 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4849 if (INT_FAST8_TYPE)
4850 int_fast8_type_node =
4851 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4852 if (INT_FAST16_TYPE)
4853 int_fast16_type_node =
4854 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4855 if (INT_FAST32_TYPE)
4856 int_fast32_type_node =
4857 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4858 if (INT_FAST64_TYPE)
4859 int_fast64_type_node =
4860 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4861 if (UINT_FAST8_TYPE)
4862 uint_fast8_type_node =
4863 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4864 if (UINT_FAST16_TYPE)
4865 uint_fast16_type_node =
4866 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4867 if (UINT_FAST32_TYPE)
4868 uint_fast32_type_node =
4869 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4870 if (UINT_FAST64_TYPE)
4871 uint_fast64_type_node =
4872 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4873 if (INTPTR_TYPE)
4874 intptr_type_node =
4875 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4876 if (UINTPTR_TYPE)
4877 uintptr_type_node =
4878 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4880 default_function_type = build_function_type (integer_type_node, NULL_TREE);
4881 ptrdiff_type_node
4882 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
4883 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4885 lang_hooks.decls.pushdecl
4886 (build_decl (UNKNOWN_LOCATION,
4887 TYPE_DECL, get_identifier ("__builtin_va_list"),
4888 va_list_type_node));
4889 if (targetm.enum_va_list_p)
4891 int l;
4892 const char *pname;
4893 tree ptype;
4895 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4897 lang_hooks.decls.pushdecl
4898 (build_decl (UNKNOWN_LOCATION,
4899 TYPE_DECL, get_identifier (pname),
4900 ptype));
4905 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4907 va_list_arg_type_node = va_list_ref_type_node =
4908 build_pointer_type (TREE_TYPE (va_list_type_node));
4910 else
4912 va_list_arg_type_node = va_list_type_node;
4913 va_list_ref_type_node = build_reference_type (va_list_type_node);
4916 if (!flag_preprocess_only)
4917 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4919 main_identifier_node = get_identifier ("main");
4921 /* Create the built-in __null node. It is important that this is
4922 not shared. */
4923 null_node = make_node (INTEGER_CST);
4924 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4926 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4927 memset (builtin_types, 0, sizeof (builtin_types));
4930 /* The number of named compound-literals generated thus far. */
4931 static GTY(()) int compound_literal_number;
4933 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4935 void
4936 set_compound_literal_name (tree decl)
4938 char *name;
4939 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4940 compound_literal_number);
4941 compound_literal_number++;
4942 DECL_NAME (decl) = get_identifier (name);
4945 tree
4946 build_va_arg (location_t loc, tree expr, tree type)
4948 expr = build1 (VA_ARG_EXPR, type, expr);
4949 SET_EXPR_LOCATION (expr, loc);
4950 return expr;
4954 /* Linked list of disabled built-in functions. */
4956 typedef struct disabled_builtin
4958 const char *name;
4959 struct disabled_builtin *next;
4960 } disabled_builtin;
4961 static disabled_builtin *disabled_builtins = NULL;
4963 static bool builtin_function_disabled_p (const char *);
4965 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4966 begins with "__builtin_", give an error. */
4968 void
4969 disable_builtin_function (const char *name)
4971 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
4972 error ("cannot disable built-in function %qs", name);
4973 else
4975 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4976 new_disabled_builtin->name = name;
4977 new_disabled_builtin->next = disabled_builtins;
4978 disabled_builtins = new_disabled_builtin;
4983 /* Return true if the built-in function NAME has been disabled, false
4984 otherwise. */
4986 static bool
4987 builtin_function_disabled_p (const char *name)
4989 disabled_builtin *p;
4990 for (p = disabled_builtins; p != NULL; p = p->next)
4992 if (strcmp (name, p->name) == 0)
4993 return true;
4995 return false;
4999 /* Worker for DEF_BUILTIN.
5000 Possibly define a builtin function with one or two names.
5001 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5002 nonansi_p and flag_no_nonansi_builtin. */
5004 static void
5005 def_builtin_1 (enum built_in_function fncode,
5006 const char *name,
5007 enum built_in_class fnclass,
5008 tree fntype, tree libtype,
5009 bool both_p, bool fallback_p, bool nonansi_p,
5010 tree fnattrs, bool implicit_p)
5012 tree decl;
5013 const char *libname;
5015 if (fntype == error_mark_node)
5016 return;
5018 gcc_assert ((!both_p && !fallback_p)
5019 || !strncmp (name, "__builtin_",
5020 strlen ("__builtin_")));
5022 libname = name + strlen ("__builtin_");
5023 decl = add_builtin_function (name, fntype, fncode, fnclass,
5024 (fallback_p ? libname : NULL),
5025 fnattrs);
5026 if (both_p
5027 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5028 && !(nonansi_p && flag_no_nonansi_builtin))
5029 add_builtin_function (libname, libtype, fncode, fnclass,
5030 NULL, fnattrs);
5032 built_in_decls[(int) fncode] = decl;
5033 if (implicit_p)
5034 implicit_built_in_decls[(int) fncode] = decl;
5037 /* Nonzero if the type T promotes to int. This is (nearly) the
5038 integral promotions defined in ISO C99 6.3.1.1/2. */
5040 bool
5041 c_promoting_integer_type_p (const_tree t)
5043 switch (TREE_CODE (t))
5045 case INTEGER_TYPE:
5046 return (TYPE_MAIN_VARIANT (t) == char_type_node
5047 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5048 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5049 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5050 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5051 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5053 case ENUMERAL_TYPE:
5054 /* ??? Technically all enumerations not larger than an int
5055 promote to an int. But this is used along code paths
5056 that only want to notice a size change. */
5057 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5059 case BOOLEAN_TYPE:
5060 return 1;
5062 default:
5063 return 0;
5067 /* Return 1 if PARMS specifies a fixed number of parameters
5068 and none of their types is affected by default promotions. */
5071 self_promoting_args_p (const_tree parms)
5073 const_tree t;
5074 for (t = parms; t; t = TREE_CHAIN (t))
5076 tree type = TREE_VALUE (t);
5078 if (type == error_mark_node)
5079 continue;
5081 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5082 return 0;
5084 if (type == 0)
5085 return 0;
5087 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5088 return 0;
5090 if (c_promoting_integer_type_p (type))
5091 return 0;
5093 return 1;
5096 /* Recursively remove any '*' or '&' operator from TYPE. */
5097 tree
5098 strip_pointer_operator (tree t)
5100 while (POINTER_TYPE_P (t))
5101 t = TREE_TYPE (t);
5102 return t;
5105 /* Recursively remove pointer or array type from TYPE. */
5106 tree
5107 strip_pointer_or_array_types (tree t)
5109 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5110 t = TREE_TYPE (t);
5111 return t;
5114 /* Used to compare case labels. K1 and K2 are actually tree nodes
5115 representing case labels, or NULL_TREE for a `default' label.
5116 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5117 K2, and 0 if K1 and K2 are equal. */
5120 case_compare (splay_tree_key k1, splay_tree_key k2)
5122 /* Consider a NULL key (such as arises with a `default' label) to be
5123 smaller than anything else. */
5124 if (!k1)
5125 return k2 ? -1 : 0;
5126 else if (!k2)
5127 return k1 ? 1 : 0;
5129 return tree_int_cst_compare ((tree) k1, (tree) k2);
5132 /* Process a case label, located at LOC, for the range LOW_VALUE
5133 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5134 then this case label is actually a `default' label. If only
5135 HIGH_VALUE is NULL_TREE, then case label was declared using the
5136 usual C/C++ syntax, rather than the GNU case range extension.
5137 CASES is a tree containing all the case ranges processed so far;
5138 COND is the condition for the switch-statement itself. Returns the
5139 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5140 is created. */
5142 tree
5143 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5144 tree low_value, tree high_value)
5146 tree type;
5147 tree label;
5148 tree case_label;
5149 splay_tree_node node;
5151 /* Create the LABEL_DECL itself. */
5152 label = create_artificial_label (loc);
5154 /* If there was an error processing the switch condition, bail now
5155 before we get more confused. */
5156 if (!cond || cond == error_mark_node)
5157 goto error_out;
5159 if ((low_value && TREE_TYPE (low_value)
5160 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5161 || (high_value && TREE_TYPE (high_value)
5162 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5164 error_at (loc, "pointers are not permitted as case values");
5165 goto error_out;
5168 /* Case ranges are a GNU extension. */
5169 if (high_value)
5170 pedwarn (loc, OPT_pedantic,
5171 "range expressions in switch statements are non-standard");
5173 type = TREE_TYPE (cond);
5174 if (low_value)
5176 low_value = check_case_value (low_value);
5177 low_value = convert_and_check (type, low_value);
5178 if (low_value == error_mark_node)
5179 goto error_out;
5181 if (high_value)
5183 high_value = check_case_value (high_value);
5184 high_value = convert_and_check (type, high_value);
5185 if (high_value == error_mark_node)
5186 goto error_out;
5189 if (low_value && high_value)
5191 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5192 really a case range, even though it was written that way.
5193 Remove the HIGH_VALUE to simplify later processing. */
5194 if (tree_int_cst_equal (low_value, high_value))
5195 high_value = NULL_TREE;
5196 else if (!tree_int_cst_lt (low_value, high_value))
5197 warning_at (loc, 0, "empty range specified");
5200 /* See if the case is in range of the type of the original testing
5201 expression. If both low_value and high_value are out of range,
5202 don't insert the case label and return NULL_TREE. */
5203 if (low_value
5204 && !check_case_bounds (type, orig_type,
5205 &low_value, high_value ? &high_value : NULL))
5206 return NULL_TREE;
5208 /* Look up the LOW_VALUE in the table of case labels we already
5209 have. */
5210 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5211 /* If there was not an exact match, check for overlapping ranges.
5212 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5213 that's a `default' label and the only overlap is an exact match. */
5214 if (!node && (low_value || high_value))
5216 splay_tree_node low_bound;
5217 splay_tree_node high_bound;
5219 /* Even though there wasn't an exact match, there might be an
5220 overlap between this case range and another case range.
5221 Since we've (inductively) not allowed any overlapping case
5222 ranges, we simply need to find the greatest low case label
5223 that is smaller that LOW_VALUE, and the smallest low case
5224 label that is greater than LOW_VALUE. If there is an overlap
5225 it will occur in one of these two ranges. */
5226 low_bound = splay_tree_predecessor (cases,
5227 (splay_tree_key) low_value);
5228 high_bound = splay_tree_successor (cases,
5229 (splay_tree_key) low_value);
5231 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5232 the LOW_VALUE, so there is no need to check unless the
5233 LOW_BOUND is in fact itself a case range. */
5234 if (low_bound
5235 && CASE_HIGH ((tree) low_bound->value)
5236 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5237 low_value) >= 0)
5238 node = low_bound;
5239 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5240 range is bigger than the low end of the current range, so we
5241 are only interested if the current range is a real range, and
5242 not an ordinary case label. */
5243 else if (high_bound
5244 && high_value
5245 && (tree_int_cst_compare ((tree) high_bound->key,
5246 high_value)
5247 <= 0))
5248 node = high_bound;
5250 /* If there was an overlap, issue an error. */
5251 if (node)
5253 tree duplicate = CASE_LABEL ((tree) node->value);
5255 if (high_value)
5257 error_at (loc, "duplicate (or overlapping) case value");
5258 error_at (DECL_SOURCE_LOCATION (duplicate),
5259 "this is the first entry overlapping that value");
5261 else if (low_value)
5263 error_at (loc, "duplicate case value") ;
5264 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5266 else
5268 error_at (loc, "multiple default labels in one switch");
5269 error_at (DECL_SOURCE_LOCATION (duplicate),
5270 "this is the first default label");
5272 goto error_out;
5275 /* Add a CASE_LABEL to the statement-tree. */
5276 case_label = add_stmt (build_case_label (loc, low_value, high_value, label));
5277 /* Register this case label in the splay tree. */
5278 splay_tree_insert (cases,
5279 (splay_tree_key) low_value,
5280 (splay_tree_value) case_label);
5282 return case_label;
5284 error_out:
5285 /* Add a label so that the back-end doesn't think that the beginning of
5286 the switch is unreachable. Note that we do not add a case label, as
5287 that just leads to duplicates and thence to failure later on. */
5288 if (!cases->root)
5290 tree t = create_artificial_label (loc);
5291 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5293 return error_mark_node;
5296 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5297 Used to verify that case values match up with enumerator values. */
5299 static void
5300 match_case_to_enum_1 (tree key, tree type, tree label)
5302 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5304 /* ??? Not working too hard to print the double-word value.
5305 Should perhaps be done with %lwd in the diagnostic routines? */
5306 if (TREE_INT_CST_HIGH (key) == 0)
5307 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5308 TREE_INT_CST_LOW (key));
5309 else if (!TYPE_UNSIGNED (type)
5310 && TREE_INT_CST_HIGH (key) == -1
5311 && TREE_INT_CST_LOW (key) != 0)
5312 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5313 -TREE_INT_CST_LOW (key));
5314 else
5315 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5316 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5317 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5319 if (TYPE_NAME (type) == 0)
5320 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5321 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5322 "case value %qs not in enumerated type",
5323 buf);
5324 else
5325 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5326 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5327 "case value %qs not in enumerated type %qT",
5328 buf, type);
5331 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5332 Used to verify that case values match up with enumerator values. */
5334 static int
5335 match_case_to_enum (splay_tree_node node, void *data)
5337 tree label = (tree) node->value;
5338 tree type = (tree) data;
5340 /* Skip default case. */
5341 if (!CASE_LOW (label))
5342 return 0;
5344 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5345 when we did our enum->case scan. Reset our scratch bit after. */
5346 if (!CASE_LOW_SEEN (label))
5347 match_case_to_enum_1 (CASE_LOW (label), type, label);
5348 else
5349 CASE_LOW_SEEN (label) = 0;
5351 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
5352 not set, that means that CASE_HIGH did not appear when we did our
5353 enum->case scan. Reset our scratch bit after. */
5354 if (CASE_HIGH (label))
5356 if (!CASE_HIGH_SEEN (label))
5357 match_case_to_enum_1 (CASE_HIGH (label), type, label);
5358 else
5359 CASE_HIGH_SEEN (label) = 0;
5362 return 0;
5365 /* Handle -Wswitch*. Called from the front end after parsing the
5366 switch construct. */
5367 /* ??? Should probably be somewhere generic, since other languages
5368 besides C and C++ would want this. At the moment, however, C/C++
5369 are the only tree-ssa languages that support enumerations at all,
5370 so the point is moot. */
5372 void
5373 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5374 tree type, tree cond)
5376 splay_tree_node default_node;
5377 splay_tree_node node;
5378 tree chain;
5380 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5381 return;
5383 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5384 if (!default_node)
5385 warning_at (switch_location, OPT_Wswitch_default,
5386 "switch missing default case");
5388 /* From here on, we only care about about enumerated types. */
5389 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5390 return;
5392 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
5393 if (!warn_switch_enum && !warn_switch)
5394 return;
5396 /* Check the cases. Warn about case values which are not members of
5397 the enumerated type. For -Wswitch-enum, or for -Wswitch when
5398 there is no default case, check that exactly all enumeration
5399 literals are covered by the cases. */
5401 /* Clearing COND if it is not an integer constant simplifies
5402 the tests inside the loop below. */
5403 if (TREE_CODE (cond) != INTEGER_CST)
5404 cond = NULL_TREE;
5406 /* The time complexity here is O(N*lg(N)) worst case, but for the
5407 common case of monotonically increasing enumerators, it is
5408 O(N), since the nature of the splay tree will keep the next
5409 element adjacent to the root at all times. */
5411 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5413 tree value = TREE_VALUE (chain);
5414 if (TREE_CODE (value) == CONST_DECL)
5415 value = DECL_INITIAL (value);
5416 node = splay_tree_lookup (cases, (splay_tree_key) value);
5417 if (node)
5419 /* Mark the CASE_LOW part of the case entry as seen. */
5420 tree label = (tree) node->value;
5421 CASE_LOW_SEEN (label) = 1;
5422 continue;
5425 /* Even though there wasn't an exact match, there might be a
5426 case range which includes the enumerator's value. */
5427 node = splay_tree_predecessor (cases, (splay_tree_key) value);
5428 if (node && CASE_HIGH ((tree) node->value))
5430 tree label = (tree) node->value;
5431 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
5432 if (cmp >= 0)
5434 /* If we match the upper bound exactly, mark the CASE_HIGH
5435 part of the case entry as seen. */
5436 if (cmp == 0)
5437 CASE_HIGH_SEEN (label) = 1;
5438 continue;
5442 /* We've now determined that this enumerated literal isn't
5443 handled by the case labels of the switch statement. */
5445 /* If the switch expression is a constant, we only really care
5446 about whether that constant is handled by the switch. */
5447 if (cond && tree_int_cst_compare (cond, value))
5448 continue;
5450 /* If there is a default_node, the only relevant option is
5451 Wswitch-enum. Otherwise, if both are enabled then we prefer
5452 to warn using -Wswitch because -Wswitch is enabled by -Wall
5453 while -Wswitch-enum is explicit. */
5454 warning_at (switch_location,
5455 (default_node || !warn_switch
5456 ? OPT_Wswitch_enum
5457 : OPT_Wswitch),
5458 "enumeration value %qE not handled in switch",
5459 TREE_PURPOSE (chain));
5462 /* Warn if there are case expressions that don't correspond to
5463 enumerators. This can occur since C and C++ don't enforce
5464 type-checking of assignments to enumeration variables.
5466 The time complexity here is now always O(N) worst case, since
5467 we should have marked both the lower bound and upper bound of
5468 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
5469 above. This scan also resets those fields. */
5471 splay_tree_foreach (cases, match_case_to_enum, type);
5474 /* Finish an expression taking the address of LABEL (an
5475 IDENTIFIER_NODE). Returns an expression for the address.
5477 LOC is the location for the expression returned. */
5479 tree
5480 finish_label_address_expr (tree label, location_t loc)
5482 tree result;
5484 pedwarn (input_location, OPT_pedantic, "taking the address of a label is non-standard");
5486 if (label == error_mark_node)
5487 return error_mark_node;
5489 label = lookup_label (label);
5490 if (label == NULL_TREE)
5491 result = null_pointer_node;
5492 else
5494 TREE_USED (label) = 1;
5495 result = build1 (ADDR_EXPR, ptr_type_node, label);
5496 /* The current function is not necessarily uninlinable.
5497 Computed gotos are incompatible with inlining, but the value
5498 here could be used only in a diagnostic, for example. */
5499 protected_set_expr_location (result, loc);
5502 return result;
5506 /* Given a boolean expression ARG, return a tree representing an increment
5507 or decrement (as indicated by CODE) of ARG. The front end must check for
5508 invalid cases (e.g., decrement in C++). */
5509 tree
5510 boolean_increment (enum tree_code code, tree arg)
5512 tree val;
5513 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5515 arg = stabilize_reference (arg);
5516 switch (code)
5518 case PREINCREMENT_EXPR:
5519 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5520 break;
5521 case POSTINCREMENT_EXPR:
5522 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5523 arg = save_expr (arg);
5524 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5525 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5526 break;
5527 case PREDECREMENT_EXPR:
5528 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5529 invert_truthvalue_loc (input_location, arg));
5530 break;
5531 case POSTDECREMENT_EXPR:
5532 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5533 invert_truthvalue_loc (input_location, arg));
5534 arg = save_expr (arg);
5535 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5536 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5537 break;
5538 default:
5539 gcc_unreachable ();
5541 TREE_SIDE_EFFECTS (val) = 1;
5542 return val;
5545 /* Built-in macros for stddef.h and stdint.h, that require macros
5546 defined in this file. */
5547 void
5548 c_stddef_cpp_builtins(void)
5550 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5551 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5552 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5553 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5554 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5555 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5556 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5557 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5558 if (SIG_ATOMIC_TYPE)
5559 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5560 if (INT8_TYPE)
5561 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5562 if (INT16_TYPE)
5563 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5564 if (INT32_TYPE)
5565 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5566 if (INT64_TYPE)
5567 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5568 if (UINT8_TYPE)
5569 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5570 if (UINT16_TYPE)
5571 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5572 if (UINT32_TYPE)
5573 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5574 if (UINT64_TYPE)
5575 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5576 if (INT_LEAST8_TYPE)
5577 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5578 if (INT_LEAST16_TYPE)
5579 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5580 if (INT_LEAST32_TYPE)
5581 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5582 if (INT_LEAST64_TYPE)
5583 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5584 if (UINT_LEAST8_TYPE)
5585 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5586 if (UINT_LEAST16_TYPE)
5587 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5588 if (UINT_LEAST32_TYPE)
5589 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5590 if (UINT_LEAST64_TYPE)
5591 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5592 if (INT_FAST8_TYPE)
5593 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5594 if (INT_FAST16_TYPE)
5595 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5596 if (INT_FAST32_TYPE)
5597 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5598 if (INT_FAST64_TYPE)
5599 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5600 if (UINT_FAST8_TYPE)
5601 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5602 if (UINT_FAST16_TYPE)
5603 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5604 if (UINT_FAST32_TYPE)
5605 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5606 if (UINT_FAST64_TYPE)
5607 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5608 if (INTPTR_TYPE)
5609 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5610 if (UINTPTR_TYPE)
5611 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5614 static void
5615 c_init_attributes (void)
5617 /* Fill in the built_in_attributes array. */
5618 #define DEF_ATTR_NULL_TREE(ENUM) \
5619 built_in_attributes[(int) ENUM] = NULL_TREE;
5620 #define DEF_ATTR_INT(ENUM, VALUE) \
5621 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
5622 #define DEF_ATTR_IDENT(ENUM, STRING) \
5623 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5624 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5625 built_in_attributes[(int) ENUM] \
5626 = tree_cons (built_in_attributes[(int) PURPOSE], \
5627 built_in_attributes[(int) VALUE], \
5628 built_in_attributes[(int) CHAIN]);
5629 #include "builtin-attrs.def"
5630 #undef DEF_ATTR_NULL_TREE
5631 #undef DEF_ATTR_INT
5632 #undef DEF_ATTR_IDENT
5633 #undef DEF_ATTR_TREE_LIST
5636 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
5637 identifier as an argument, so the front end shouldn't look it up. */
5639 bool
5640 attribute_takes_identifier_p (const_tree attr_id)
5642 if (is_attribute_p ("mode", attr_id)
5643 || is_attribute_p ("format", attr_id)
5644 || is_attribute_p ("cleanup", attr_id))
5645 return true;
5646 else
5647 return targetm.attribute_takes_identifier_p (attr_id);
5650 /* Attribute handlers common to C front ends. */
5652 /* Handle a "packed" attribute; arguments as in
5653 struct attribute_spec.handler. */
5655 static tree
5656 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5657 int flags, bool *no_add_attrs)
5659 if (TYPE_P (*node))
5661 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5662 *node = build_variant_type_copy (*node);
5663 TYPE_PACKED (*node) = 1;
5665 else if (TREE_CODE (*node) == FIELD_DECL)
5667 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
5668 /* Still pack bitfields. */
5669 && ! DECL_INITIAL (*node))
5670 warning (OPT_Wattributes,
5671 "%qE attribute ignored for field of type %qT",
5672 name, TREE_TYPE (*node));
5673 else
5674 DECL_PACKED (*node) = 1;
5676 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
5677 used for DECL_REGISTER. It wouldn't mean anything anyway.
5678 We can't set DECL_PACKED on the type of a TYPE_DECL, because
5679 that changes what the typedef is typing. */
5680 else
5682 warning (OPT_Wattributes, "%qE attribute ignored", name);
5683 *no_add_attrs = true;
5686 return NULL_TREE;
5689 /* Handle a "nocommon" attribute; arguments as in
5690 struct attribute_spec.handler. */
5692 static tree
5693 handle_nocommon_attribute (tree *node, tree name,
5694 tree ARG_UNUSED (args),
5695 int ARG_UNUSED (flags), bool *no_add_attrs)
5697 if (TREE_CODE (*node) == VAR_DECL)
5698 DECL_COMMON (*node) = 0;
5699 else
5701 warning (OPT_Wattributes, "%qE attribute ignored", name);
5702 *no_add_attrs = true;
5705 return NULL_TREE;
5708 /* Handle a "common" attribute; arguments as in
5709 struct attribute_spec.handler. */
5711 static tree
5712 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5713 int ARG_UNUSED (flags), bool *no_add_attrs)
5715 if (TREE_CODE (*node) == VAR_DECL)
5716 DECL_COMMON (*node) = 1;
5717 else
5719 warning (OPT_Wattributes, "%qE attribute ignored", name);
5720 *no_add_attrs = true;
5723 return NULL_TREE;
5726 /* Handle a "noreturn" attribute; arguments as in
5727 struct attribute_spec.handler. */
5729 static tree
5730 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5731 int ARG_UNUSED (flags), bool *no_add_attrs)
5733 tree type = TREE_TYPE (*node);
5735 /* See FIXME comment in c_common_attribute_table. */
5736 if (TREE_CODE (*node) == FUNCTION_DECL)
5737 TREE_THIS_VOLATILE (*node) = 1;
5738 else if (TREE_CODE (type) == POINTER_TYPE
5739 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5740 TREE_TYPE (*node)
5741 = build_pointer_type
5742 (build_type_variant (TREE_TYPE (type),
5743 TYPE_READONLY (TREE_TYPE (type)), 1));
5744 else
5746 warning (OPT_Wattributes, "%qE attribute ignored", name);
5747 *no_add_attrs = true;
5750 return NULL_TREE;
5753 /* Handle a "hot" and attribute; arguments as in
5754 struct attribute_spec.handler. */
5756 static tree
5757 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5758 int ARG_UNUSED (flags), bool *no_add_attrs)
5760 if (TREE_CODE (*node) == FUNCTION_DECL)
5762 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
5764 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5765 name, "cold");
5766 *no_add_attrs = true;
5768 /* Most of the rest of the hot processing is done later with
5769 lookup_attribute. */
5771 else
5773 warning (OPT_Wattributes, "%qE attribute ignored", name);
5774 *no_add_attrs = true;
5777 return NULL_TREE;
5779 /* Handle a "cold" and attribute; arguments as in
5780 struct attribute_spec.handler. */
5782 static tree
5783 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5784 int ARG_UNUSED (flags), bool *no_add_attrs)
5786 if (TREE_CODE (*node) == FUNCTION_DECL)
5788 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
5790 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5791 name, "hot");
5792 *no_add_attrs = true;
5794 /* Most of the rest of the cold processing is done later with
5795 lookup_attribute. */
5797 else
5799 warning (OPT_Wattributes, "%qE attribute ignored", name);
5800 *no_add_attrs = true;
5803 return NULL_TREE;
5806 /* Handle a "noinline" attribute; arguments as in
5807 struct attribute_spec.handler. */
5809 static tree
5810 handle_noinline_attribute (tree *node, tree name,
5811 tree ARG_UNUSED (args),
5812 int ARG_UNUSED (flags), bool *no_add_attrs)
5814 if (TREE_CODE (*node) == FUNCTION_DECL)
5815 DECL_UNINLINABLE (*node) = 1;
5816 else
5818 warning (OPT_Wattributes, "%qE attribute ignored", name);
5819 *no_add_attrs = true;
5822 return NULL_TREE;
5825 /* Handle a "noclone" attribute; arguments as in
5826 struct attribute_spec.handler. */
5828 static tree
5829 handle_noclone_attribute (tree *node, tree name,
5830 tree ARG_UNUSED (args),
5831 int ARG_UNUSED (flags), bool *no_add_attrs)
5833 if (TREE_CODE (*node) != FUNCTION_DECL)
5835 warning (OPT_Wattributes, "%qE attribute ignored", name);
5836 *no_add_attrs = true;
5839 return NULL_TREE;
5842 /* Handle a "always_inline" attribute; arguments as in
5843 struct attribute_spec.handler. */
5845 static tree
5846 handle_always_inline_attribute (tree *node, tree name,
5847 tree ARG_UNUSED (args),
5848 int ARG_UNUSED (flags),
5849 bool *no_add_attrs)
5851 if (TREE_CODE (*node) == FUNCTION_DECL)
5853 /* Set the attribute and mark it for disregarding inline
5854 limits. */
5855 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
5857 else
5859 warning (OPT_Wattributes, "%qE attribute ignored", name);
5860 *no_add_attrs = true;
5863 return NULL_TREE;
5866 /* Handle a "gnu_inline" attribute; arguments as in
5867 struct attribute_spec.handler. */
5869 static tree
5870 handle_gnu_inline_attribute (tree *node, tree name,
5871 tree ARG_UNUSED (args),
5872 int ARG_UNUSED (flags),
5873 bool *no_add_attrs)
5875 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
5877 /* Do nothing else, just set the attribute. We'll get at
5878 it later with lookup_attribute. */
5880 else
5882 warning (OPT_Wattributes, "%qE attribute ignored", name);
5883 *no_add_attrs = true;
5886 return NULL_TREE;
5889 /* Handle a "leaf" attribute; arguments as in
5890 struct attribute_spec.handler. */
5892 static tree
5893 handle_leaf_attribute (tree *node, tree name,
5894 tree ARG_UNUSED (args),
5895 int ARG_UNUSED (flags), bool *no_add_attrs)
5897 if (TREE_CODE (*node) != FUNCTION_DECL)
5899 warning (OPT_Wattributes, "%qE attribute ignored", name);
5900 *no_add_attrs = true;
5902 if (!TREE_PUBLIC (*node))
5904 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
5905 *no_add_attrs = true;
5908 return NULL_TREE;
5911 /* Handle an "artificial" attribute; arguments as in
5912 struct attribute_spec.handler. */
5914 static tree
5915 handle_artificial_attribute (tree *node, tree name,
5916 tree ARG_UNUSED (args),
5917 int ARG_UNUSED (flags),
5918 bool *no_add_attrs)
5920 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
5922 /* Do nothing else, just set the attribute. We'll get at
5923 it later with lookup_attribute. */
5925 else
5927 warning (OPT_Wattributes, "%qE attribute ignored", name);
5928 *no_add_attrs = true;
5931 return NULL_TREE;
5934 /* Handle a "flatten" attribute; arguments as in
5935 struct attribute_spec.handler. */
5937 static tree
5938 handle_flatten_attribute (tree *node, tree name,
5939 tree args ATTRIBUTE_UNUSED,
5940 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
5942 if (TREE_CODE (*node) == FUNCTION_DECL)
5943 /* Do nothing else, just set the attribute. We'll get at
5944 it later with lookup_attribute. */
5946 else
5948 warning (OPT_Wattributes, "%qE attribute ignored", name);
5949 *no_add_attrs = true;
5952 return NULL_TREE;
5955 /* Handle a "warning" or "error" attribute; arguments as in
5956 struct attribute_spec.handler. */
5958 static tree
5959 handle_error_attribute (tree *node, tree name, tree args,
5960 int ARG_UNUSED (flags), bool *no_add_attrs)
5962 if (TREE_CODE (*node) == FUNCTION_DECL
5963 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
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 "used" attribute; arguments as in
5977 struct attribute_spec.handler. */
5979 static tree
5980 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
5981 int ARG_UNUSED (flags), bool *no_add_attrs)
5983 tree node = *pnode;
5985 if (TREE_CODE (node) == FUNCTION_DECL
5986 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)))
5988 TREE_USED (node) = 1;
5989 DECL_PRESERVE_P (node) = 1;
5990 if (TREE_CODE (node) == VAR_DECL)
5991 DECL_READ_P (node) = 1;
5993 else
5995 warning (OPT_Wattributes, "%qE attribute ignored", name);
5996 *no_add_attrs = true;
5999 return NULL_TREE;
6002 /* Handle a "unused" attribute; arguments as in
6003 struct attribute_spec.handler. */
6005 static tree
6006 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6007 int flags, bool *no_add_attrs)
6009 if (DECL_P (*node))
6011 tree decl = *node;
6013 if (TREE_CODE (decl) == PARM_DECL
6014 || TREE_CODE (decl) == VAR_DECL
6015 || TREE_CODE (decl) == FUNCTION_DECL
6016 || TREE_CODE (decl) == LABEL_DECL
6017 || TREE_CODE (decl) == TYPE_DECL)
6019 TREE_USED (decl) = 1;
6020 if (TREE_CODE (decl) == VAR_DECL
6021 || TREE_CODE (decl) == PARM_DECL)
6022 DECL_READ_P (decl) = 1;
6024 else
6026 warning (OPT_Wattributes, "%qE attribute ignored", name);
6027 *no_add_attrs = true;
6030 else
6032 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6033 *node = build_variant_type_copy (*node);
6034 TREE_USED (*node) = 1;
6037 return NULL_TREE;
6040 /* Handle a "externally_visible" attribute; arguments as in
6041 struct attribute_spec.handler. */
6043 static tree
6044 handle_externally_visible_attribute (tree *pnode, tree name,
6045 tree ARG_UNUSED (args),
6046 int ARG_UNUSED (flags),
6047 bool *no_add_attrs)
6049 tree node = *pnode;
6051 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6053 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6054 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6056 warning (OPT_Wattributes,
6057 "%qE attribute have effect only on public objects", name);
6058 *no_add_attrs = true;
6061 else
6063 warning (OPT_Wattributes, "%qE attribute ignored", name);
6064 *no_add_attrs = true;
6067 return NULL_TREE;
6070 /* Handle a "const" attribute; arguments as in
6071 struct attribute_spec.handler. */
6073 static tree
6074 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6075 int ARG_UNUSED (flags), bool *no_add_attrs)
6077 tree type = TREE_TYPE (*node);
6079 /* See FIXME comment on noreturn in c_common_attribute_table. */
6080 if (TREE_CODE (*node) == FUNCTION_DECL)
6081 TREE_READONLY (*node) = 1;
6082 else if (TREE_CODE (type) == POINTER_TYPE
6083 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6084 TREE_TYPE (*node)
6085 = build_pointer_type
6086 (build_type_variant (TREE_TYPE (type), 1,
6087 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6088 else
6090 warning (OPT_Wattributes, "%qE attribute ignored", name);
6091 *no_add_attrs = true;
6094 return NULL_TREE;
6097 /* Handle a "transparent_union" attribute; arguments as in
6098 struct attribute_spec.handler. */
6100 static tree
6101 handle_transparent_union_attribute (tree *node, tree name,
6102 tree ARG_UNUSED (args), int flags,
6103 bool *no_add_attrs)
6105 tree type;
6107 *no_add_attrs = true;
6109 if (TREE_CODE (*node) == TYPE_DECL)
6110 node = &TREE_TYPE (*node);
6111 type = *node;
6113 if (TREE_CODE (type) == UNION_TYPE)
6115 /* When IN_PLACE is set, leave the check for FIELDS and MODE to
6116 the code in finish_struct. */
6117 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6119 if (TYPE_FIELDS (type) == NULL_TREE
6120 || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type)))
6121 goto ignored;
6123 /* A type variant isn't good enough, since we don't a cast
6124 to such a type removed as a no-op. */
6125 *node = type = build_duplicate_type (type);
6128 TYPE_TRANSPARENT_AGGR (type) = 1;
6129 return NULL_TREE;
6132 ignored:
6133 warning (OPT_Wattributes, "%qE attribute ignored", name);
6134 return NULL_TREE;
6137 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6138 get the requested priority for a constructor or destructor,
6139 possibly issuing diagnostics for invalid or reserved
6140 priorities. */
6142 static priority_type
6143 get_priority (tree args, bool is_destructor)
6145 HOST_WIDE_INT pri;
6146 tree arg;
6148 if (!args)
6149 return DEFAULT_INIT_PRIORITY;
6151 if (!SUPPORTS_INIT_PRIORITY)
6153 if (is_destructor)
6154 error ("destructor priorities are not supported");
6155 else
6156 error ("constructor priorities are not supported");
6157 return DEFAULT_INIT_PRIORITY;
6160 arg = TREE_VALUE (args);
6161 if (!host_integerp (arg, /*pos=*/0)
6162 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6163 goto invalid;
6165 pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0);
6166 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6167 goto invalid;
6169 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6171 if (is_destructor)
6172 warning (0,
6173 "destructor priorities from 0 to %d are reserved "
6174 "for the implementation",
6175 MAX_RESERVED_INIT_PRIORITY);
6176 else
6177 warning (0,
6178 "constructor priorities from 0 to %d are reserved "
6179 "for the implementation",
6180 MAX_RESERVED_INIT_PRIORITY);
6182 return pri;
6184 invalid:
6185 if (is_destructor)
6186 error ("destructor priorities must be integers from 0 to %d inclusive",
6187 MAX_INIT_PRIORITY);
6188 else
6189 error ("constructor priorities must be integers from 0 to %d inclusive",
6190 MAX_INIT_PRIORITY);
6191 return DEFAULT_INIT_PRIORITY;
6194 /* Handle a "constructor" attribute; arguments as in
6195 struct attribute_spec.handler. */
6197 static tree
6198 handle_constructor_attribute (tree *node, tree name, tree args,
6199 int ARG_UNUSED (flags),
6200 bool *no_add_attrs)
6202 tree decl = *node;
6203 tree type = TREE_TYPE (decl);
6205 if (TREE_CODE (decl) == FUNCTION_DECL
6206 && TREE_CODE (type) == FUNCTION_TYPE
6207 && decl_function_context (decl) == 0)
6209 priority_type priority;
6210 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6211 priority = get_priority (args, /*is_destructor=*/false);
6212 SET_DECL_INIT_PRIORITY (decl, priority);
6213 TREE_USED (decl) = 1;
6215 else
6217 warning (OPT_Wattributes, "%qE attribute ignored", name);
6218 *no_add_attrs = true;
6221 return NULL_TREE;
6224 /* Handle a "destructor" attribute; arguments as in
6225 struct attribute_spec.handler. */
6227 static tree
6228 handle_destructor_attribute (tree *node, tree name, tree args,
6229 int ARG_UNUSED (flags),
6230 bool *no_add_attrs)
6232 tree decl = *node;
6233 tree type = TREE_TYPE (decl);
6235 if (TREE_CODE (decl) == FUNCTION_DECL
6236 && TREE_CODE (type) == FUNCTION_TYPE
6237 && decl_function_context (decl) == 0)
6239 priority_type priority;
6240 DECL_STATIC_DESTRUCTOR (decl) = 1;
6241 priority = get_priority (args, /*is_destructor=*/true);
6242 SET_DECL_FINI_PRIORITY (decl, priority);
6243 TREE_USED (decl) = 1;
6245 else
6247 warning (OPT_Wattributes, "%qE attribute ignored", name);
6248 *no_add_attrs = true;
6251 return NULL_TREE;
6254 /* Nonzero if the mode is a valid vector mode for this architecture.
6255 This returns nonzero even if there is no hardware support for the
6256 vector mode, but we can emulate with narrower modes. */
6258 static int
6259 vector_mode_valid_p (enum machine_mode mode)
6261 enum mode_class mclass = GET_MODE_CLASS (mode);
6262 enum machine_mode innermode;
6264 /* Doh! What's going on? */
6265 if (mclass != MODE_VECTOR_INT
6266 && mclass != MODE_VECTOR_FLOAT
6267 && mclass != MODE_VECTOR_FRACT
6268 && mclass != MODE_VECTOR_UFRACT
6269 && mclass != MODE_VECTOR_ACCUM
6270 && mclass != MODE_VECTOR_UACCUM)
6271 return 0;
6273 /* Hardware support. Woo hoo! */
6274 if (targetm.vector_mode_supported_p (mode))
6275 return 1;
6277 innermode = GET_MODE_INNER (mode);
6279 /* We should probably return 1 if requesting V4DI and we have no DI,
6280 but we have V2DI, but this is probably very unlikely. */
6282 /* If we have support for the inner mode, we can safely emulate it.
6283 We may not have V2DI, but me can emulate with a pair of DIs. */
6284 return targetm.scalar_mode_supported_p (innermode);
6288 /* Handle a "mode" attribute; arguments as in
6289 struct attribute_spec.handler. */
6291 static tree
6292 handle_mode_attribute (tree *node, tree name, tree args,
6293 int ARG_UNUSED (flags), bool *no_add_attrs)
6295 tree type = *node;
6296 tree ident = TREE_VALUE (args);
6298 *no_add_attrs = true;
6300 if (TREE_CODE (ident) != IDENTIFIER_NODE)
6301 warning (OPT_Wattributes, "%qE attribute ignored", name);
6302 else
6304 int j;
6305 const char *p = IDENTIFIER_POINTER (ident);
6306 int len = strlen (p);
6307 enum machine_mode mode = VOIDmode;
6308 tree typefm;
6309 bool valid_mode;
6311 if (len > 4 && p[0] == '_' && p[1] == '_'
6312 && p[len - 1] == '_' && p[len - 2] == '_')
6314 char *newp = (char *) alloca (len - 1);
6316 strcpy (newp, &p[2]);
6317 newp[len - 4] = '\0';
6318 p = newp;
6321 /* Change this type to have a type with the specified mode.
6322 First check for the special modes. */
6323 if (!strcmp (p, "byte"))
6324 mode = byte_mode;
6325 else if (!strcmp (p, "word"))
6326 mode = word_mode;
6327 else if (!strcmp (p, "pointer"))
6328 mode = ptr_mode;
6329 else if (!strcmp (p, "libgcc_cmp_return"))
6330 mode = targetm.libgcc_cmp_return_mode ();
6331 else if (!strcmp (p, "libgcc_shift_count"))
6332 mode = targetm.libgcc_shift_count_mode ();
6333 else if (!strcmp (p, "unwind_word"))
6334 mode = targetm.unwind_word_mode ();
6335 else
6336 for (j = 0; j < NUM_MACHINE_MODES; j++)
6337 if (!strcmp (p, GET_MODE_NAME (j)))
6339 mode = (enum machine_mode) j;
6340 break;
6343 if (mode == VOIDmode)
6345 error ("unknown machine mode %qE", ident);
6346 return NULL_TREE;
6349 valid_mode = false;
6350 switch (GET_MODE_CLASS (mode))
6352 case MODE_INT:
6353 case MODE_PARTIAL_INT:
6354 case MODE_FLOAT:
6355 case MODE_DECIMAL_FLOAT:
6356 case MODE_FRACT:
6357 case MODE_UFRACT:
6358 case MODE_ACCUM:
6359 case MODE_UACCUM:
6360 valid_mode = targetm.scalar_mode_supported_p (mode);
6361 break;
6363 case MODE_COMPLEX_INT:
6364 case MODE_COMPLEX_FLOAT:
6365 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
6366 break;
6368 case MODE_VECTOR_INT:
6369 case MODE_VECTOR_FLOAT:
6370 case MODE_VECTOR_FRACT:
6371 case MODE_VECTOR_UFRACT:
6372 case MODE_VECTOR_ACCUM:
6373 case MODE_VECTOR_UACCUM:
6374 warning (OPT_Wattributes, "specifying vector types with "
6375 "__attribute__ ((mode)) is deprecated");
6376 warning (OPT_Wattributes,
6377 "use __attribute__ ((vector_size)) instead");
6378 valid_mode = vector_mode_valid_p (mode);
6379 break;
6381 default:
6382 break;
6384 if (!valid_mode)
6386 error ("unable to emulate %qs", p);
6387 return NULL_TREE;
6390 if (POINTER_TYPE_P (type))
6392 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
6393 tree (*fn)(tree, enum machine_mode, bool);
6395 if (!targetm.addr_space.valid_pointer_mode (mode, as))
6397 error ("invalid pointer mode %qs", p);
6398 return NULL_TREE;
6401 if (TREE_CODE (type) == POINTER_TYPE)
6402 fn = build_pointer_type_for_mode;
6403 else
6404 fn = build_reference_type_for_mode;
6405 typefm = fn (TREE_TYPE (type), mode, false);
6407 else
6409 /* For fixed-point modes, we need to test if the signness of type
6410 and the machine mode are consistent. */
6411 if (ALL_FIXED_POINT_MODE_P (mode)
6412 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
6414 error ("signness of type and machine mode %qs don't match", p);
6415 return NULL_TREE;
6417 /* For fixed-point modes, we need to pass saturating info. */
6418 typefm = lang_hooks.types.type_for_mode (mode,
6419 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
6420 : TYPE_UNSIGNED (type));
6423 if (typefm == NULL_TREE)
6425 error ("no data type for mode %qs", p);
6426 return NULL_TREE;
6428 else if (TREE_CODE (type) == ENUMERAL_TYPE)
6430 /* For enumeral types, copy the precision from the integer
6431 type returned above. If not an INTEGER_TYPE, we can't use
6432 this mode for this type. */
6433 if (TREE_CODE (typefm) != INTEGER_TYPE)
6435 error ("cannot use mode %qs for enumeral types", p);
6436 return NULL_TREE;
6439 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
6441 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
6442 typefm = type;
6444 else
6446 /* We cannot build a type variant, as there's code that assumes
6447 that TYPE_MAIN_VARIANT has the same mode. This includes the
6448 debug generators. Instead, create a subrange type. This
6449 results in all of the enumeral values being emitted only once
6450 in the original, and the subtype gets them by reference. */
6451 if (TYPE_UNSIGNED (type))
6452 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
6453 else
6454 typefm = make_signed_type (TYPE_PRECISION (typefm));
6455 TREE_TYPE (typefm) = type;
6458 else if (VECTOR_MODE_P (mode)
6459 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
6460 : TREE_CODE (type) != TREE_CODE (typefm))
6462 error ("mode %qs applied to inappropriate type", p);
6463 return NULL_TREE;
6466 *node = typefm;
6469 return NULL_TREE;
6472 /* Handle a "section" attribute; arguments as in
6473 struct attribute_spec.handler. */
6475 static tree
6476 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6477 int ARG_UNUSED (flags), bool *no_add_attrs)
6479 tree decl = *node;
6481 if (targetm.have_named_sections)
6483 user_defined_section_attribute = true;
6485 if ((TREE_CODE (decl) == FUNCTION_DECL
6486 || TREE_CODE (decl) == VAR_DECL)
6487 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6489 if (TREE_CODE (decl) == VAR_DECL
6490 && current_function_decl != NULL_TREE
6491 && !TREE_STATIC (decl))
6493 error_at (DECL_SOURCE_LOCATION (decl),
6494 "section attribute cannot be specified for "
6495 "local variables");
6496 *no_add_attrs = true;
6499 /* The decl may have already been given a section attribute
6500 from a previous declaration. Ensure they match. */
6501 else if (DECL_SECTION_NAME (decl) != NULL_TREE
6502 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
6503 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
6505 error ("section of %q+D conflicts with previous declaration",
6506 *node);
6507 *no_add_attrs = true;
6509 else if (TREE_CODE (decl) == VAR_DECL
6510 && !targetm.have_tls && targetm.emutls.tmpl_section
6511 && DECL_THREAD_LOCAL_P (decl))
6513 error ("section of %q+D cannot be overridden", *node);
6514 *no_add_attrs = true;
6516 else
6517 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
6519 else
6521 error ("section attribute not allowed for %q+D", *node);
6522 *no_add_attrs = true;
6525 else
6527 error_at (DECL_SOURCE_LOCATION (*node),
6528 "section attributes are not supported for this target");
6529 *no_add_attrs = true;
6532 return NULL_TREE;
6535 /* Handle a "aligned" attribute; arguments as in
6536 struct attribute_spec.handler. */
6538 static tree
6539 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6540 int flags, bool *no_add_attrs)
6542 tree decl = NULL_TREE;
6543 tree *type = NULL;
6544 int is_type = 0;
6545 tree align_expr = (args ? TREE_VALUE (args)
6546 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
6547 int i;
6549 if (DECL_P (*node))
6551 decl = *node;
6552 type = &TREE_TYPE (decl);
6553 is_type = TREE_CODE (*node) == TYPE_DECL;
6555 else if (TYPE_P (*node))
6556 type = node, is_type = 1;
6558 if (TREE_CODE (align_expr) != INTEGER_CST)
6560 error ("requested alignment is not a constant");
6561 *no_add_attrs = true;
6563 else if ((i = tree_log2 (align_expr)) == -1)
6565 error ("requested alignment is not a power of 2");
6566 *no_add_attrs = true;
6568 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
6570 error ("requested alignment is too large");
6571 *no_add_attrs = true;
6573 else if (is_type)
6575 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6576 /* OK, modify the type in place. */;
6577 /* If we have a TYPE_DECL, then copy the type, so that we
6578 don't accidentally modify a builtin type. See pushdecl. */
6579 else if (decl && TREE_TYPE (decl) != error_mark_node
6580 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
6582 tree tt = TREE_TYPE (decl);
6583 *type = build_variant_type_copy (*type);
6584 DECL_ORIGINAL_TYPE (decl) = tt;
6585 TYPE_NAME (*type) = decl;
6586 TREE_USED (*type) = TREE_USED (decl);
6587 TREE_TYPE (decl) = *type;
6589 else
6590 *type = build_variant_type_copy (*type);
6592 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
6593 TYPE_USER_ALIGN (*type) = 1;
6595 else if (! VAR_OR_FUNCTION_DECL_P (decl)
6596 && TREE_CODE (decl) != FIELD_DECL)
6598 error ("alignment may not be specified for %q+D", decl);
6599 *no_add_attrs = true;
6601 else if (TREE_CODE (decl) == FUNCTION_DECL
6602 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
6604 if (DECL_USER_ALIGN (decl))
6605 error ("alignment for %q+D was previously specified as %d "
6606 "and may not be decreased", decl,
6607 DECL_ALIGN (decl) / BITS_PER_UNIT);
6608 else
6609 error ("alignment for %q+D must be at least %d", decl,
6610 DECL_ALIGN (decl) / BITS_PER_UNIT);
6611 *no_add_attrs = true;
6613 else
6615 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
6616 DECL_USER_ALIGN (decl) = 1;
6619 return NULL_TREE;
6622 /* Handle a "weak" attribute; arguments as in
6623 struct attribute_spec.handler. */
6625 static tree
6626 handle_weak_attribute (tree *node, tree name,
6627 tree ARG_UNUSED (args),
6628 int ARG_UNUSED (flags),
6629 bool * ARG_UNUSED (no_add_attrs))
6631 if (TREE_CODE (*node) == FUNCTION_DECL
6632 && DECL_DECLARED_INLINE_P (*node))
6634 error ("inline function %q+D cannot be declared weak", *node);
6635 *no_add_attrs = true;
6637 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6639 error ("indirect function %q+D cannot be declared weak", *node);
6640 *no_add_attrs = true;
6641 return NULL_TREE;
6643 else if (TREE_CODE (*node) == FUNCTION_DECL
6644 || TREE_CODE (*node) == VAR_DECL)
6645 declare_weak (*node);
6646 else
6647 warning (OPT_Wattributes, "%qE attribute ignored", name);
6649 return NULL_TREE;
6652 /* Handle an "alias" or "ifunc" attribute; arguments as in
6653 struct attribute_spec.handler, except that IS_ALIAS tells us
6654 whether this is an alias as opposed to ifunc attribute. */
6656 static tree
6657 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
6658 bool *no_add_attrs)
6660 tree decl = *node;
6662 if (TREE_CODE (decl) != FUNCTION_DECL
6663 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
6665 warning (OPT_Wattributes, "%qE attribute ignored", name);
6666 *no_add_attrs = true;
6668 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
6669 || (TREE_CODE (decl) != FUNCTION_DECL
6670 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
6671 /* A static variable declaration is always a tentative definition,
6672 but the alias is a non-tentative definition which overrides. */
6673 || (TREE_CODE (decl) != FUNCTION_DECL
6674 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
6676 error ("%q+D defined both normally and as %qE attribute", decl, name);
6677 *no_add_attrs = true;
6678 return NULL_TREE;
6680 else if (!is_alias
6681 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
6682 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
6684 error ("weak %q+D cannot be defined %qE", decl, name);
6685 *no_add_attrs = true;
6686 return NULL_TREE;
6689 /* Note that the very first time we process a nested declaration,
6690 decl_function_context will not be set. Indeed, *would* never
6691 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
6692 we do below. After such frobbery, pushdecl would set the context.
6693 In any case, this is never what we want. */
6694 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
6696 tree id;
6698 id = TREE_VALUE (args);
6699 if (TREE_CODE (id) != STRING_CST)
6701 error ("attribute %qE argument not a string", name);
6702 *no_add_attrs = true;
6703 return NULL_TREE;
6705 id = get_identifier (TREE_STRING_POINTER (id));
6706 /* This counts as a use of the object pointed to. */
6707 TREE_USED (id) = 1;
6709 if (TREE_CODE (decl) == FUNCTION_DECL)
6710 DECL_INITIAL (decl) = error_mark_node;
6711 else
6713 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6714 DECL_EXTERNAL (decl) = 1;
6715 else
6716 DECL_EXTERNAL (decl) = 0;
6717 TREE_STATIC (decl) = 1;
6720 if (!is_alias)
6721 /* ifuncs are also aliases, so set that attribute too. */
6722 DECL_ATTRIBUTES (decl)
6723 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
6725 else
6727 warning (OPT_Wattributes, "%qE attribute ignored", name);
6728 *no_add_attrs = true;
6731 return NULL_TREE;
6734 /* Handle an "alias" or "ifunc" attribute; arguments as in
6735 struct attribute_spec.handler. */
6737 static tree
6738 handle_ifunc_attribute (tree *node, tree name, tree args,
6739 int ARG_UNUSED (flags), bool *no_add_attrs)
6741 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
6744 /* Handle an "alias" or "ifunc" attribute; arguments as in
6745 struct attribute_spec.handler. */
6747 static tree
6748 handle_alias_attribute (tree *node, tree name, tree args,
6749 int ARG_UNUSED (flags), bool *no_add_attrs)
6751 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
6754 /* Handle a "weakref" attribute; arguments as in struct
6755 attribute_spec.handler. */
6757 static tree
6758 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6759 int flags, bool *no_add_attrs)
6761 tree attr = NULL_TREE;
6763 /* We must ignore the attribute when it is associated with
6764 local-scoped decls, since attribute alias is ignored and many
6765 such symbols do not even have a DECL_WEAK field. */
6766 if (decl_function_context (*node)
6767 || current_function_decl
6768 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
6770 warning (OPT_Wattributes, "%qE attribute ignored", name);
6771 *no_add_attrs = true;
6772 return NULL_TREE;
6775 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6777 error ("indirect function %q+D cannot be declared weakref", *node);
6778 *no_add_attrs = true;
6779 return NULL_TREE;
6782 /* The idea here is that `weakref("name")' mutates into `weakref,
6783 alias("name")', and weakref without arguments, in turn,
6784 implicitly adds weak. */
6786 if (args)
6788 attr = tree_cons (get_identifier ("alias"), args, attr);
6789 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
6791 *no_add_attrs = true;
6793 decl_attributes (node, attr, flags);
6795 else
6797 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
6798 error_at (DECL_SOURCE_LOCATION (*node),
6799 "weakref attribute must appear before alias attribute");
6801 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
6802 and that isn't supported; and because it wants to add it to
6803 the list of weak decls, which isn't helpful. */
6804 DECL_WEAK (*node) = 1;
6807 return NULL_TREE;
6810 /* Handle an "visibility" attribute; arguments as in
6811 struct attribute_spec.handler. */
6813 static tree
6814 handle_visibility_attribute (tree *node, tree name, tree args,
6815 int ARG_UNUSED (flags),
6816 bool *ARG_UNUSED (no_add_attrs))
6818 tree decl = *node;
6819 tree id = TREE_VALUE (args);
6820 enum symbol_visibility vis;
6822 if (TYPE_P (*node))
6824 if (TREE_CODE (*node) == ENUMERAL_TYPE)
6825 /* OK */;
6826 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
6828 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
6829 name);
6830 return NULL_TREE;
6832 else if (TYPE_FIELDS (*node))
6834 error ("%qE attribute ignored because %qT is already defined",
6835 name, *node);
6836 return NULL_TREE;
6839 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
6841 warning (OPT_Wattributes, "%qE attribute ignored", name);
6842 return NULL_TREE;
6845 if (TREE_CODE (id) != STRING_CST)
6847 error ("visibility argument not a string");
6848 return NULL_TREE;
6851 /* If this is a type, set the visibility on the type decl. */
6852 if (TYPE_P (decl))
6854 decl = TYPE_NAME (decl);
6855 if (!decl)
6856 return NULL_TREE;
6857 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6859 warning (OPT_Wattributes, "%qE attribute ignored on types",
6860 name);
6861 return NULL_TREE;
6865 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
6866 vis = VISIBILITY_DEFAULT;
6867 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
6868 vis = VISIBILITY_INTERNAL;
6869 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
6870 vis = VISIBILITY_HIDDEN;
6871 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
6872 vis = VISIBILITY_PROTECTED;
6873 else
6875 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
6876 vis = VISIBILITY_DEFAULT;
6879 if (DECL_VISIBILITY_SPECIFIED (decl)
6880 && vis != DECL_VISIBILITY (decl))
6882 tree attributes = (TYPE_P (*node)
6883 ? TYPE_ATTRIBUTES (*node)
6884 : DECL_ATTRIBUTES (decl));
6885 if (lookup_attribute ("visibility", attributes))
6886 error ("%qD redeclared with different visibility", decl);
6887 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6888 && lookup_attribute ("dllimport", attributes))
6889 error ("%qD was declared %qs which implies default visibility",
6890 decl, "dllimport");
6891 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6892 && lookup_attribute ("dllexport", attributes))
6893 error ("%qD was declared %qs which implies default visibility",
6894 decl, "dllexport");
6897 DECL_VISIBILITY (decl) = vis;
6898 DECL_VISIBILITY_SPECIFIED (decl) = 1;
6900 /* Go ahead and attach the attribute to the node as well. This is needed
6901 so we can determine whether we have VISIBILITY_DEFAULT because the
6902 visibility was not specified, or because it was explicitly overridden
6903 from the containing scope. */
6905 return NULL_TREE;
6908 /* Determine the ELF symbol visibility for DECL, which is either a
6909 variable or a function. It is an error to use this function if a
6910 definition of DECL is not available in this translation unit.
6911 Returns true if the final visibility has been determined by this
6912 function; false if the caller is free to make additional
6913 modifications. */
6915 bool
6916 c_determine_visibility (tree decl)
6918 gcc_assert (TREE_CODE (decl) == VAR_DECL
6919 || TREE_CODE (decl) == FUNCTION_DECL);
6921 /* If the user explicitly specified the visibility with an
6922 attribute, honor that. DECL_VISIBILITY will have been set during
6923 the processing of the attribute. We check for an explicit
6924 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
6925 to distinguish the use of an attribute from the use of a "#pragma
6926 GCC visibility push(...)"; in the latter case we still want other
6927 considerations to be able to overrule the #pragma. */
6928 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
6929 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6930 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
6931 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
6932 return true;
6934 /* Set default visibility to whatever the user supplied with
6935 visibility_specified depending on #pragma GCC visibility. */
6936 if (!DECL_VISIBILITY_SPECIFIED (decl))
6938 if (visibility_options.inpragma
6939 || DECL_VISIBILITY (decl) != default_visibility)
6941 DECL_VISIBILITY (decl) = default_visibility;
6942 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
6943 /* If visibility changed and DECL already has DECL_RTL, ensure
6944 symbol flags are updated. */
6945 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
6946 || TREE_CODE (decl) == FUNCTION_DECL)
6947 && DECL_RTL_SET_P (decl))
6948 make_decl_rtl (decl);
6951 return false;
6954 /* Handle an "tls_model" attribute; arguments as in
6955 struct attribute_spec.handler. */
6957 static tree
6958 handle_tls_model_attribute (tree *node, tree name, tree args,
6959 int ARG_UNUSED (flags), bool *no_add_attrs)
6961 tree id;
6962 tree decl = *node;
6963 enum tls_model kind;
6965 *no_add_attrs = true;
6967 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
6969 warning (OPT_Wattributes, "%qE attribute ignored", name);
6970 return NULL_TREE;
6973 kind = DECL_TLS_MODEL (decl);
6974 id = TREE_VALUE (args);
6975 if (TREE_CODE (id) != STRING_CST)
6977 error ("tls_model argument not a string");
6978 return NULL_TREE;
6981 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
6982 kind = TLS_MODEL_LOCAL_EXEC;
6983 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
6984 kind = TLS_MODEL_INITIAL_EXEC;
6985 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
6986 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
6987 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
6988 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6989 else
6990 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
6992 DECL_TLS_MODEL (decl) = kind;
6993 return NULL_TREE;
6996 /* Handle a "no_instrument_function" attribute; arguments as in
6997 struct attribute_spec.handler. */
6999 static tree
7000 handle_no_instrument_function_attribute (tree *node, tree name,
7001 tree ARG_UNUSED (args),
7002 int ARG_UNUSED (flags),
7003 bool *no_add_attrs)
7005 tree decl = *node;
7007 if (TREE_CODE (decl) != FUNCTION_DECL)
7009 error_at (DECL_SOURCE_LOCATION (decl),
7010 "%qE attribute applies only to functions", name);
7011 *no_add_attrs = true;
7013 else if (DECL_INITIAL (decl))
7015 error_at (DECL_SOURCE_LOCATION (decl),
7016 "can%'t set %qE attribute after definition", name);
7017 *no_add_attrs = true;
7019 else
7020 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7022 return NULL_TREE;
7025 /* Handle a "malloc" attribute; arguments as in
7026 struct attribute_spec.handler. */
7028 static tree
7029 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7030 int ARG_UNUSED (flags), bool *no_add_attrs)
7032 if (TREE_CODE (*node) == FUNCTION_DECL
7033 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7034 DECL_IS_MALLOC (*node) = 1;
7035 else
7037 warning (OPT_Wattributes, "%qE attribute ignored", name);
7038 *no_add_attrs = true;
7041 return NULL_TREE;
7044 /* Handle a "alloc_size" attribute; arguments as in
7045 struct attribute_spec.handler. */
7047 static tree
7048 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7049 int ARG_UNUSED (flags), bool *no_add_attrs)
7051 unsigned arg_count = type_num_arguments (*node);
7052 for (; args; args = TREE_CHAIN (args))
7054 tree position = TREE_VALUE (args);
7056 if (TREE_CODE (position) != INTEGER_CST
7057 || TREE_INT_CST_HIGH (position)
7058 || TREE_INT_CST_LOW (position) < 1
7059 || TREE_INT_CST_LOW (position) > arg_count )
7061 warning (OPT_Wattributes,
7062 "alloc_size parameter outside range");
7063 *no_add_attrs = true;
7064 return NULL_TREE;
7067 return NULL_TREE;
7070 /* Handle a "fn spec" attribute; arguments as in
7071 struct attribute_spec.handler. */
7073 static tree
7074 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7075 tree args, int ARG_UNUSED (flags),
7076 bool *no_add_attrs ATTRIBUTE_UNUSED)
7078 gcc_assert (args
7079 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7080 && !TREE_CHAIN (args));
7081 return NULL_TREE;
7084 /* Handle a "returns_twice" attribute; arguments as in
7085 struct attribute_spec.handler. */
7087 static tree
7088 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7089 int ARG_UNUSED (flags), bool *no_add_attrs)
7091 if (TREE_CODE (*node) == FUNCTION_DECL)
7092 DECL_IS_RETURNS_TWICE (*node) = 1;
7093 else
7095 warning (OPT_Wattributes, "%qE attribute ignored", name);
7096 *no_add_attrs = true;
7099 return NULL_TREE;
7102 /* Handle a "no_limit_stack" attribute; arguments as in
7103 struct attribute_spec.handler. */
7105 static tree
7106 handle_no_limit_stack_attribute (tree *node, tree name,
7107 tree ARG_UNUSED (args),
7108 int ARG_UNUSED (flags),
7109 bool *no_add_attrs)
7111 tree decl = *node;
7113 if (TREE_CODE (decl) != FUNCTION_DECL)
7115 error_at (DECL_SOURCE_LOCATION (decl),
7116 "%qE attribute applies only to functions", name);
7117 *no_add_attrs = true;
7119 else if (DECL_INITIAL (decl))
7121 error_at (DECL_SOURCE_LOCATION (decl),
7122 "can%'t set %qE attribute after definition", name);
7123 *no_add_attrs = true;
7125 else
7126 DECL_NO_LIMIT_STACK (decl) = 1;
7128 return NULL_TREE;
7131 /* Handle a "pure" attribute; arguments as in
7132 struct attribute_spec.handler. */
7134 static tree
7135 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7136 int ARG_UNUSED (flags), bool *no_add_attrs)
7138 if (TREE_CODE (*node) == FUNCTION_DECL)
7139 DECL_PURE_P (*node) = 1;
7140 /* ??? TODO: Support types. */
7141 else
7143 warning (OPT_Wattributes, "%qE attribute ignored", name);
7144 *no_add_attrs = true;
7147 return NULL_TREE;
7150 /* Handle a "no vops" attribute; arguments as in
7151 struct attribute_spec.handler. */
7153 static tree
7154 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
7155 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7156 bool *ARG_UNUSED (no_add_attrs))
7158 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
7159 DECL_IS_NOVOPS (*node) = 1;
7160 return NULL_TREE;
7163 /* Handle a "deprecated" attribute; arguments as in
7164 struct attribute_spec.handler. */
7166 static tree
7167 handle_deprecated_attribute (tree *node, tree name,
7168 tree args, int flags,
7169 bool *no_add_attrs)
7171 tree type = NULL_TREE;
7172 int warn = 0;
7173 tree what = NULL_TREE;
7175 if (!args)
7176 *no_add_attrs = true;
7177 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7179 error ("deprecated message is not a string");
7180 *no_add_attrs = true;
7183 if (DECL_P (*node))
7185 tree decl = *node;
7186 type = TREE_TYPE (decl);
7188 if (TREE_CODE (decl) == TYPE_DECL
7189 || TREE_CODE (decl) == PARM_DECL
7190 || TREE_CODE (decl) == VAR_DECL
7191 || TREE_CODE (decl) == FUNCTION_DECL
7192 || TREE_CODE (decl) == FIELD_DECL)
7193 TREE_DEPRECATED (decl) = 1;
7194 else
7195 warn = 1;
7197 else if (TYPE_P (*node))
7199 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7200 *node = build_variant_type_copy (*node);
7201 TREE_DEPRECATED (*node) = 1;
7202 type = *node;
7204 else
7205 warn = 1;
7207 if (warn)
7209 *no_add_attrs = true;
7210 if (type && TYPE_NAME (type))
7212 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
7213 what = TYPE_NAME (*node);
7214 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7215 && DECL_NAME (TYPE_NAME (type)))
7216 what = DECL_NAME (TYPE_NAME (type));
7218 if (what)
7219 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
7220 else
7221 warning (OPT_Wattributes, "%qE attribute ignored", name);
7224 return NULL_TREE;
7227 /* Handle a "vector_size" attribute; arguments as in
7228 struct attribute_spec.handler. */
7230 static tree
7231 handle_vector_size_attribute (tree *node, tree name, tree args,
7232 int ARG_UNUSED (flags),
7233 bool *no_add_attrs)
7235 unsigned HOST_WIDE_INT vecsize, nunits;
7236 enum machine_mode orig_mode;
7237 tree type = *node, new_type, size;
7239 *no_add_attrs = true;
7241 size = TREE_VALUE (args);
7243 if (!host_integerp (size, 1))
7245 warning (OPT_Wattributes, "%qE attribute ignored", name);
7246 return NULL_TREE;
7249 /* Get the vector size (in bytes). */
7250 vecsize = tree_low_cst (size, 1);
7252 /* We need to provide for vector pointers, vector arrays, and
7253 functions returning vectors. For example:
7255 __attribute__((vector_size(16))) short *foo;
7257 In this case, the mode is SI, but the type being modified is
7258 HI, so we need to look further. */
7260 while (POINTER_TYPE_P (type)
7261 || TREE_CODE (type) == FUNCTION_TYPE
7262 || TREE_CODE (type) == METHOD_TYPE
7263 || TREE_CODE (type) == ARRAY_TYPE
7264 || TREE_CODE (type) == OFFSET_TYPE)
7265 type = TREE_TYPE (type);
7267 /* Get the mode of the type being modified. */
7268 orig_mode = TYPE_MODE (type);
7270 if ((!INTEGRAL_TYPE_P (type)
7271 && !SCALAR_FLOAT_TYPE_P (type)
7272 && !FIXED_POINT_TYPE_P (type))
7273 || (!SCALAR_FLOAT_MODE_P (orig_mode)
7274 && GET_MODE_CLASS (orig_mode) != MODE_INT
7275 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
7276 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
7277 || TREE_CODE (type) == BOOLEAN_TYPE)
7279 error ("invalid vector type for attribute %qE", name);
7280 return NULL_TREE;
7283 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
7285 error ("vector size not an integral multiple of component size");
7286 return NULL;
7289 if (vecsize == 0)
7291 error ("zero vector size");
7292 return NULL;
7295 /* Calculate how many units fit in the vector. */
7296 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
7297 if (nunits & (nunits - 1))
7299 error ("number of components of the vector not a power of two");
7300 return NULL_TREE;
7303 new_type = build_vector_type (type, nunits);
7305 /* Build back pointers if needed. */
7306 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
7308 return NULL_TREE;
7311 /* Handle the "nonnull" attribute. */
7312 static tree
7313 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
7314 tree args, int ARG_UNUSED (flags),
7315 bool *no_add_attrs)
7317 tree type = *node;
7318 unsigned HOST_WIDE_INT attr_arg_num;
7320 /* If no arguments are specified, all pointer arguments should be
7321 non-null. Verify a full prototype is given so that the arguments
7322 will have the correct types when we actually check them later. */
7323 if (!args)
7325 if (!TYPE_ARG_TYPES (type))
7327 error ("nonnull attribute without arguments on a non-prototype");
7328 *no_add_attrs = true;
7330 return NULL_TREE;
7333 /* Argument list specified. Verify that each argument number references
7334 a pointer argument. */
7335 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
7337 tree argument;
7338 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
7340 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
7342 error ("nonnull argument has invalid operand number (argument %lu)",
7343 (unsigned long) attr_arg_num);
7344 *no_add_attrs = true;
7345 return NULL_TREE;
7348 argument = TYPE_ARG_TYPES (type);
7349 if (argument)
7351 for (ck_num = 1; ; ck_num++)
7353 if (!argument || ck_num == arg_num)
7354 break;
7355 argument = TREE_CHAIN (argument);
7358 if (!argument
7359 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
7361 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
7362 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7363 *no_add_attrs = true;
7364 return NULL_TREE;
7367 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
7369 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
7370 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7371 *no_add_attrs = true;
7372 return NULL_TREE;
7377 return NULL_TREE;
7380 /* Check the argument list of a function call for null in argument slots
7381 that are marked as requiring a non-null pointer argument. The NARGS
7382 arguments are passed in the array ARGARRAY.
7385 static void
7386 check_function_nonnull (tree attrs, int nargs, tree *argarray)
7388 tree a, args;
7389 int i;
7391 for (a = attrs; a; a = TREE_CHAIN (a))
7393 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
7395 args = TREE_VALUE (a);
7397 /* Walk the argument list. If we encounter an argument number we
7398 should check for non-null, do it. If the attribute has no args,
7399 then every pointer argument is checked (in which case the check
7400 for pointer type is done in check_nonnull_arg). */
7401 for (i = 0; i < nargs; i++)
7403 if (!args || nonnull_check_p (args, i + 1))
7404 check_function_arguments_recurse (check_nonnull_arg, NULL,
7405 argarray[i],
7406 i + 1);
7412 /* Check that the Nth argument of a function call (counting backwards
7413 from the end) is a (pointer)0. The NARGS arguments are passed in the
7414 array ARGARRAY. */
7416 static void
7417 check_function_sentinel (tree attrs, int nargs, tree *argarray, tree typelist)
7419 tree attr = lookup_attribute ("sentinel", attrs);
7421 if (attr)
7423 int len = 0;
7424 int pos = 0;
7425 tree sentinel;
7427 /* Skip over the named arguments. */
7428 while (typelist && len < nargs)
7430 typelist = TREE_CHAIN (typelist);
7431 len++;
7434 if (TREE_VALUE (attr))
7436 tree p = TREE_VALUE (TREE_VALUE (attr));
7437 pos = TREE_INT_CST_LOW (p);
7440 /* The sentinel must be one of the varargs, i.e.
7441 in position >= the number of fixed arguments. */
7442 if ((nargs - 1 - pos) < len)
7444 warning (OPT_Wformat,
7445 "not enough variable arguments to fit a sentinel");
7446 return;
7449 /* Validate the sentinel. */
7450 sentinel = argarray[nargs - 1 - pos];
7451 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
7452 || !integer_zerop (sentinel))
7453 /* Although __null (in C++) is only an integer we allow it
7454 nevertheless, as we are guaranteed that it's exactly
7455 as wide as a pointer, and we don't want to force
7456 users to cast the NULL they have written there.
7457 We warn with -Wstrict-null-sentinel, though. */
7458 && (warn_strict_null_sentinel || null_node != sentinel))
7459 warning (OPT_Wformat, "missing sentinel in function call");
7463 /* Helper for check_function_nonnull; given a list of operands which
7464 must be non-null in ARGS, determine if operand PARAM_NUM should be
7465 checked. */
7467 static bool
7468 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
7470 unsigned HOST_WIDE_INT arg_num = 0;
7472 for (; args; args = TREE_CHAIN (args))
7474 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
7476 gcc_assert (found);
7478 if (arg_num == param_num)
7479 return true;
7481 return false;
7484 /* Check that the function argument PARAM (which is operand number
7485 PARAM_NUM) is non-null. This is called by check_function_nonnull
7486 via check_function_arguments_recurse. */
7488 static void
7489 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
7490 unsigned HOST_WIDE_INT param_num)
7492 /* Just skip checking the argument if it's not a pointer. This can
7493 happen if the "nonnull" attribute was given without an operand
7494 list (which means to check every pointer argument). */
7496 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
7497 return;
7499 if (integer_zerop (param))
7500 warning (OPT_Wnonnull, "null argument where non-null required "
7501 "(argument %lu)", (unsigned long) param_num);
7504 /* Helper for nonnull attribute handling; fetch the operand number
7505 from the attribute argument list. */
7507 static bool
7508 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
7510 /* Verify the arg number is a constant. */
7511 if (TREE_CODE (arg_num_expr) != INTEGER_CST
7512 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
7513 return false;
7515 *valp = TREE_INT_CST_LOW (arg_num_expr);
7516 return true;
7519 /* Handle a "nothrow" attribute; arguments as in
7520 struct attribute_spec.handler. */
7522 static tree
7523 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7524 int ARG_UNUSED (flags), bool *no_add_attrs)
7526 if (TREE_CODE (*node) == FUNCTION_DECL)
7527 TREE_NOTHROW (*node) = 1;
7528 /* ??? TODO: Support types. */
7529 else
7531 warning (OPT_Wattributes, "%qE attribute ignored", name);
7532 *no_add_attrs = true;
7535 return NULL_TREE;
7538 /* Handle a "cleanup" attribute; arguments as in
7539 struct attribute_spec.handler. */
7541 static tree
7542 handle_cleanup_attribute (tree *node, tree name, tree args,
7543 int ARG_UNUSED (flags), bool *no_add_attrs)
7545 tree decl = *node;
7546 tree cleanup_id, cleanup_decl;
7548 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
7549 for global destructors in C++. This requires infrastructure that
7550 we don't have generically at the moment. It's also not a feature
7551 we'd be missing too much, since we do have attribute constructor. */
7552 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
7554 warning (OPT_Wattributes, "%qE attribute ignored", name);
7555 *no_add_attrs = true;
7556 return NULL_TREE;
7559 /* Verify that the argument is a function in scope. */
7560 /* ??? We could support pointers to functions here as well, if
7561 that was considered desirable. */
7562 cleanup_id = TREE_VALUE (args);
7563 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
7565 error ("cleanup argument not an identifier");
7566 *no_add_attrs = true;
7567 return NULL_TREE;
7569 cleanup_decl = lookup_name (cleanup_id);
7570 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
7572 error ("cleanup argument not a function");
7573 *no_add_attrs = true;
7574 return NULL_TREE;
7577 /* That the function has proper type is checked with the
7578 eventual call to build_function_call. */
7580 return NULL_TREE;
7583 /* Handle a "warn_unused_result" attribute. No special handling. */
7585 static tree
7586 handle_warn_unused_result_attribute (tree *node, tree name,
7587 tree ARG_UNUSED (args),
7588 int ARG_UNUSED (flags), bool *no_add_attrs)
7590 /* Ignore the attribute for functions not returning any value. */
7591 if (VOID_TYPE_P (TREE_TYPE (*node)))
7593 warning (OPT_Wattributes, "%qE attribute ignored", name);
7594 *no_add_attrs = true;
7597 return NULL_TREE;
7600 /* Handle a "sentinel" attribute. */
7602 static tree
7603 handle_sentinel_attribute (tree *node, tree name, tree args,
7604 int ARG_UNUSED (flags), bool *no_add_attrs)
7606 tree params = TYPE_ARG_TYPES (*node);
7608 if (!params)
7610 warning (OPT_Wattributes,
7611 "%qE attribute requires prototypes with named arguments", name);
7612 *no_add_attrs = true;
7614 else
7616 while (TREE_CHAIN (params))
7617 params = TREE_CHAIN (params);
7619 if (VOID_TYPE_P (TREE_VALUE (params)))
7621 warning (OPT_Wattributes,
7622 "%qE attribute only applies to variadic functions", name);
7623 *no_add_attrs = true;
7627 if (args)
7629 tree position = TREE_VALUE (args);
7631 if (TREE_CODE (position) != INTEGER_CST)
7633 warning (OPT_Wattributes,
7634 "requested position is not an integer constant");
7635 *no_add_attrs = true;
7637 else
7639 if (tree_int_cst_lt (position, integer_zero_node))
7641 warning (OPT_Wattributes,
7642 "requested position is less than zero");
7643 *no_add_attrs = true;
7648 return NULL_TREE;
7651 /* Handle a "type_generic" attribute. */
7653 static tree
7654 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
7655 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7656 bool * ARG_UNUSED (no_add_attrs))
7658 tree params;
7660 /* Ensure we have a function type. */
7661 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
7663 params = TYPE_ARG_TYPES (*node);
7664 while (params && ! VOID_TYPE_P (TREE_VALUE (params)))
7665 params = TREE_CHAIN (params);
7667 /* Ensure we have a variadic function. */
7668 gcc_assert (!params);
7670 return NULL_TREE;
7673 /* Handle a "target" attribute. */
7675 static tree
7676 handle_target_attribute (tree *node, tree name, tree args, int flags,
7677 bool *no_add_attrs)
7679 /* Ensure we have a function type. */
7680 if (TREE_CODE (*node) != FUNCTION_DECL)
7682 warning (OPT_Wattributes, "%qE attribute ignored", name);
7683 *no_add_attrs = true;
7685 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
7686 flags))
7687 *no_add_attrs = true;
7689 return NULL_TREE;
7692 /* Arguments being collected for optimization. */
7693 typedef const char *const_char_p; /* For DEF_VEC_P. */
7694 DEF_VEC_P(const_char_p);
7695 DEF_VEC_ALLOC_P(const_char_p, gc);
7696 static GTY(()) VEC(const_char_p, gc) *optimize_args;
7699 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
7700 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
7701 false for #pragma GCC optimize. */
7703 bool
7704 parse_optimize_options (tree args, bool attr_p)
7706 bool ret = true;
7707 unsigned opt_argc;
7708 unsigned i;
7709 int saved_flag_strict_aliasing;
7710 const char **opt_argv;
7711 struct cl_decoded_option *decoded_options;
7712 unsigned int decoded_options_count;
7713 tree ap;
7715 /* Build up argv vector. Just in case the string is stored away, use garbage
7716 collected strings. */
7717 VEC_truncate (const_char_p, optimize_args, 0);
7718 VEC_safe_push (const_char_p, gc, optimize_args, NULL);
7720 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
7722 tree value = TREE_VALUE (ap);
7724 if (TREE_CODE (value) == INTEGER_CST)
7726 char buffer[20];
7727 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
7728 VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
7731 else if (TREE_CODE (value) == STRING_CST)
7733 /* Split string into multiple substrings. */
7734 size_t len = TREE_STRING_LENGTH (value);
7735 char *p = ASTRDUP (TREE_STRING_POINTER (value));
7736 char *end = p + len;
7737 char *comma;
7738 char *next_p = p;
7740 while (next_p != NULL)
7742 size_t len2;
7743 char *q, *r;
7745 p = next_p;
7746 comma = strchr (p, ',');
7747 if (comma)
7749 len2 = comma - p;
7750 *comma = '\0';
7751 next_p = comma+1;
7753 else
7755 len2 = end - p;
7756 next_p = NULL;
7759 r = q = (char *) ggc_alloc_atomic (len2 + 3);
7761 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
7762 options. */
7763 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
7765 ret = false;
7766 if (attr_p)
7767 warning (OPT_Wattributes,
7768 "bad option %s to optimize attribute", p);
7769 else
7770 warning (OPT_Wpragmas,
7771 "bad option %s to pragma attribute", p);
7772 continue;
7775 if (*p != '-')
7777 *r++ = '-';
7779 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
7780 itself is -Os, and any other switch begins with a -f. */
7781 if ((*p >= '0' && *p <= '9')
7782 || (p[0] == 's' && p[1] == '\0'))
7783 *r++ = 'O';
7784 else if (*p != 'O')
7785 *r++ = 'f';
7788 memcpy (r, p, len2);
7789 r[len2] = '\0';
7790 VEC_safe_push (const_char_p, gc, optimize_args, q);
7796 opt_argc = VEC_length (const_char_p, optimize_args);
7797 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
7799 for (i = 1; i < opt_argc; i++)
7800 opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
7802 saved_flag_strict_aliasing = flag_strict_aliasing;
7804 /* Now parse the options. */
7805 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
7806 &decoded_options,
7807 &decoded_options_count);
7808 decode_options (&global_options, &global_options_set,
7809 decoded_options, decoded_options_count);
7811 targetm.override_options_after_change();
7813 /* Don't allow changing -fstrict-aliasing. */
7814 flag_strict_aliasing = saved_flag_strict_aliasing;
7816 VEC_truncate (const_char_p, optimize_args, 0);
7817 return ret;
7820 /* For handling "optimize" attribute. arguments as in
7821 struct attribute_spec.handler. */
7823 static tree
7824 handle_optimize_attribute (tree *node, tree name, tree args,
7825 int ARG_UNUSED (flags), bool *no_add_attrs)
7827 /* Ensure we have a function type. */
7828 if (TREE_CODE (*node) != FUNCTION_DECL)
7830 warning (OPT_Wattributes, "%qE attribute ignored", name);
7831 *no_add_attrs = true;
7833 else
7835 struct cl_optimization cur_opts;
7836 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
7838 /* Save current options. */
7839 cl_optimization_save (&cur_opts, &global_options);
7841 /* If we previously had some optimization options, use them as the
7842 default. */
7843 if (old_opts)
7844 cl_optimization_restore (&global_options,
7845 TREE_OPTIMIZATION (old_opts));
7847 /* Parse options, and update the vector. */
7848 parse_optimize_options (args, true);
7849 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
7850 = build_optimization_node ();
7852 /* Restore current options. */
7853 cl_optimization_restore (&global_options, &cur_opts);
7856 return NULL_TREE;
7859 /* Handle a "no_split_stack" attribute. */
7861 static tree
7862 handle_no_split_stack_attribute (tree *node, tree name,
7863 tree ARG_UNUSED (args),
7864 int ARG_UNUSED (flags),
7865 bool *no_add_attrs)
7867 tree decl = *node;
7869 if (TREE_CODE (decl) != FUNCTION_DECL)
7871 error_at (DECL_SOURCE_LOCATION (decl),
7872 "%qE attribute applies only to functions", name);
7873 *no_add_attrs = true;
7875 else if (DECL_INITIAL (decl))
7877 error_at (DECL_SOURCE_LOCATION (decl),
7878 "can%'t set %qE attribute after definition", name);
7879 *no_add_attrs = true;
7882 return NULL_TREE;
7885 /* Check for valid arguments being passed to a function.
7886 ATTRS is a list of attributes. There are NARGS arguments in the array
7887 ARGARRAY. TYPELIST is the list of argument types for the function.
7889 void
7890 check_function_arguments (tree attrs, int nargs, tree *argarray, tree typelist)
7892 /* Check for null being passed in a pointer argument that must be
7893 non-null. We also need to do this if format checking is enabled. */
7895 if (warn_nonnull)
7896 check_function_nonnull (attrs, nargs, argarray);
7898 /* Check for errors in format strings. */
7900 if (warn_format || warn_missing_format_attribute)
7901 check_function_format (attrs, nargs, argarray);
7903 if (warn_format)
7904 check_function_sentinel (attrs, nargs, argarray, typelist);
7907 /* Generic argument checking recursion routine. PARAM is the argument to
7908 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
7909 once the argument is resolved. CTX is context for the callback. */
7910 void
7911 check_function_arguments_recurse (void (*callback)
7912 (void *, tree, unsigned HOST_WIDE_INT),
7913 void *ctx, tree param,
7914 unsigned HOST_WIDE_INT param_num)
7916 if (CONVERT_EXPR_P (param)
7917 && (TYPE_PRECISION (TREE_TYPE (param))
7918 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
7920 /* Strip coercion. */
7921 check_function_arguments_recurse (callback, ctx,
7922 TREE_OPERAND (param, 0), param_num);
7923 return;
7926 if (TREE_CODE (param) == CALL_EXPR)
7928 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
7929 tree attrs;
7930 bool found_format_arg = false;
7932 /* See if this is a call to a known internationalization function
7933 that modifies a format arg. Such a function may have multiple
7934 format_arg attributes (for example, ngettext). */
7936 for (attrs = TYPE_ATTRIBUTES (type);
7937 attrs;
7938 attrs = TREE_CHAIN (attrs))
7939 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
7941 tree inner_arg;
7942 tree format_num_expr;
7943 int format_num;
7944 int i;
7945 call_expr_arg_iterator iter;
7947 /* Extract the argument number, which was previously checked
7948 to be valid. */
7949 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
7951 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
7952 && !TREE_INT_CST_HIGH (format_num_expr));
7954 format_num = TREE_INT_CST_LOW (format_num_expr);
7956 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
7957 inner_arg != 0;
7958 inner_arg = next_call_expr_arg (&iter), i++)
7959 if (i == format_num)
7961 check_function_arguments_recurse (callback, ctx,
7962 inner_arg, param_num);
7963 found_format_arg = true;
7964 break;
7968 /* If we found a format_arg attribute and did a recursive check,
7969 we are done with checking this argument. Otherwise, we continue
7970 and this will be considered a non-literal. */
7971 if (found_format_arg)
7972 return;
7975 if (TREE_CODE (param) == COND_EXPR)
7977 /* Check both halves of the conditional expression. */
7978 check_function_arguments_recurse (callback, ctx,
7979 TREE_OPERAND (param, 1), param_num);
7980 check_function_arguments_recurse (callback, ctx,
7981 TREE_OPERAND (param, 2), param_num);
7982 return;
7985 (*callback) (ctx, param, param_num);
7988 /* Checks for a builtin function FNDECL that the number of arguments
7989 NARGS against the required number REQUIRED and issues an error if
7990 there is a mismatch. Returns true if the number of arguments is
7991 correct, otherwise false. */
7993 static bool
7994 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
7996 if (nargs < required)
7998 error_at (input_location,
7999 "not enough arguments to function %qE", fndecl);
8000 return false;
8002 else if (nargs > required)
8004 error_at (input_location,
8005 "too many arguments to function %qE", fndecl);
8006 return false;
8008 return true;
8011 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
8012 Returns false if there was an error, otherwise true. */
8014 bool
8015 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
8017 if (!DECL_BUILT_IN (fndecl)
8018 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
8019 return true;
8021 switch (DECL_FUNCTION_CODE (fndecl))
8023 case BUILT_IN_CONSTANT_P:
8024 return builtin_function_validate_nargs (fndecl, nargs, 1);
8026 case BUILT_IN_ISFINITE:
8027 case BUILT_IN_ISINF:
8028 case BUILT_IN_ISINF_SIGN:
8029 case BUILT_IN_ISNAN:
8030 case BUILT_IN_ISNORMAL:
8031 if (builtin_function_validate_nargs (fndecl, nargs, 1))
8033 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
8035 error ("non-floating-point argument in call to "
8036 "function %qE", fndecl);
8037 return false;
8039 return true;
8041 return false;
8043 case BUILT_IN_ISGREATER:
8044 case BUILT_IN_ISGREATEREQUAL:
8045 case BUILT_IN_ISLESS:
8046 case BUILT_IN_ISLESSEQUAL:
8047 case BUILT_IN_ISLESSGREATER:
8048 case BUILT_IN_ISUNORDERED:
8049 if (builtin_function_validate_nargs (fndecl, nargs, 2))
8051 enum tree_code code0, code1;
8052 code0 = TREE_CODE (TREE_TYPE (args[0]));
8053 code1 = TREE_CODE (TREE_TYPE (args[1]));
8054 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
8055 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8056 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
8058 error ("non-floating-point arguments in call to "
8059 "function %qE", fndecl);
8060 return false;
8062 return true;
8064 return false;
8066 case BUILT_IN_FPCLASSIFY:
8067 if (builtin_function_validate_nargs (fndecl, nargs, 6))
8069 unsigned i;
8071 for (i=0; i<5; i++)
8072 if (TREE_CODE (args[i]) != INTEGER_CST)
8074 error ("non-const integer argument %u in call to function %qE",
8075 i+1, fndecl);
8076 return false;
8079 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
8081 error ("non-floating-point argument in call to function %qE",
8082 fndecl);
8083 return false;
8085 return true;
8087 return false;
8089 default:
8090 return true;
8094 /* Function to help qsort sort FIELD_DECLs by name order. */
8097 field_decl_cmp (const void *x_p, const void *y_p)
8099 const tree *const x = (const tree *const) x_p;
8100 const tree *const y = (const tree *const) y_p;
8102 if (DECL_NAME (*x) == DECL_NAME (*y))
8103 /* A nontype is "greater" than a type. */
8104 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8105 if (DECL_NAME (*x) == NULL_TREE)
8106 return -1;
8107 if (DECL_NAME (*y) == NULL_TREE)
8108 return 1;
8109 if (DECL_NAME (*x) < DECL_NAME (*y))
8110 return -1;
8111 return 1;
8114 static struct {
8115 gt_pointer_operator new_value;
8116 void *cookie;
8117 } resort_data;
8119 /* This routine compares two fields like field_decl_cmp but using the
8120 pointer operator in resort_data. */
8122 static int
8123 resort_field_decl_cmp (const void *x_p, const void *y_p)
8125 const tree *const x = (const tree *const) x_p;
8126 const tree *const y = (const tree *const) y_p;
8128 if (DECL_NAME (*x) == DECL_NAME (*y))
8129 /* A nontype is "greater" than a type. */
8130 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8131 if (DECL_NAME (*x) == NULL_TREE)
8132 return -1;
8133 if (DECL_NAME (*y) == NULL_TREE)
8134 return 1;
8136 tree d1 = DECL_NAME (*x);
8137 tree d2 = DECL_NAME (*y);
8138 resort_data.new_value (&d1, resort_data.cookie);
8139 resort_data.new_value (&d2, resort_data.cookie);
8140 if (d1 < d2)
8141 return -1;
8143 return 1;
8146 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8148 void
8149 resort_sorted_fields (void *obj,
8150 void * ARG_UNUSED (orig_obj),
8151 gt_pointer_operator new_value,
8152 void *cookie)
8154 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8155 resort_data.new_value = new_value;
8156 resort_data.cookie = cookie;
8157 qsort (&sf->elts[0], sf->len, sizeof (tree),
8158 resort_field_decl_cmp);
8161 /* Subroutine of c_parse_error.
8162 Return the result of concatenating LHS and RHS. RHS is really
8163 a string literal, its first character is indicated by RHS_START and
8164 RHS_SIZE is its length (including the terminating NUL character).
8166 The caller is responsible for deleting the returned pointer. */
8168 static char *
8169 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
8171 const int lhs_size = strlen (lhs);
8172 char *result = XNEWVEC (char, lhs_size + rhs_size);
8173 strncpy (result, lhs, lhs_size);
8174 strncpy (result + lhs_size, rhs_start, rhs_size);
8175 return result;
8178 /* Issue the error given by GMSGID, indicating that it occurred before
8179 TOKEN, which had the associated VALUE. */
8181 void
8182 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
8183 tree value, unsigned char token_flags)
8185 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
8187 char *message = NULL;
8189 if (token_type == CPP_EOF)
8190 message = catenate_messages (gmsgid, " at end of input");
8191 else if (token_type == CPP_CHAR
8192 || token_type == CPP_WCHAR
8193 || token_type == CPP_CHAR16
8194 || token_type == CPP_CHAR32)
8196 unsigned int val = TREE_INT_CST_LOW (value);
8197 const char *prefix;
8199 switch (token_type)
8201 default:
8202 prefix = "";
8203 break;
8204 case CPP_WCHAR:
8205 prefix = "L";
8206 break;
8207 case CPP_CHAR16:
8208 prefix = "u";
8209 break;
8210 case CPP_CHAR32:
8211 prefix = "U";
8212 break;
8215 if (val <= UCHAR_MAX && ISGRAPH (val))
8216 message = catenate_messages (gmsgid, " before %s'%c'");
8217 else
8218 message = catenate_messages (gmsgid, " before %s'\\x%x'");
8220 error (message, prefix, val);
8221 free (message);
8222 message = NULL;
8224 else if (token_type == CPP_STRING
8225 || token_type == CPP_WSTRING
8226 || token_type == CPP_STRING16
8227 || token_type == CPP_STRING32
8228 || token_type == CPP_UTF8STRING)
8229 message = catenate_messages (gmsgid, " before string constant");
8230 else if (token_type == CPP_NUMBER)
8231 message = catenate_messages (gmsgid, " before numeric constant");
8232 else if (token_type == CPP_NAME)
8234 message = catenate_messages (gmsgid, " before %qE");
8235 error (message, value);
8236 free (message);
8237 message = NULL;
8239 else if (token_type == CPP_PRAGMA)
8240 message = catenate_messages (gmsgid, " before %<#pragma%>");
8241 else if (token_type == CPP_PRAGMA_EOL)
8242 message = catenate_messages (gmsgid, " before end of line");
8243 else if (token_type < N_TTYPES)
8245 message = catenate_messages (gmsgid, " before %qs token");
8246 error (message, cpp_type2name (token_type, token_flags));
8247 free (message);
8248 message = NULL;
8250 else
8251 error (gmsgid);
8253 if (message)
8255 error (message);
8256 free (message);
8258 #undef catenate_messages
8261 /* Mapping for cpp message reasons to the options that enable them. */
8263 struct reason_option_codes_t
8265 const int reason; /* cpplib message reason. */
8266 const int option_code; /* gcc option that controls this message. */
8269 static const struct reason_option_codes_t option_codes[] = {
8270 {CPP_W_DEPRECATED, OPT_Wdeprecated},
8271 {CPP_W_COMMENTS, OPT_Wcomment},
8272 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
8273 {CPP_W_MULTICHAR, OPT_Wmultichar},
8274 {CPP_W_TRADITIONAL, OPT_Wtraditional},
8275 {CPP_W_LONG_LONG, OPT_Wlong_long},
8276 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
8277 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
8278 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
8279 {CPP_W_UNDEF, OPT_Wundef},
8280 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
8281 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
8282 {CPP_W_NORMALIZE, OPT_Wnormalized_},
8283 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
8284 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
8285 {CPP_W_NONE, 0}
8288 /* Return the gcc option code associated with the reason for a cpp
8289 message, or 0 if none. */
8291 static int
8292 c_option_controlling_cpp_error (int reason)
8294 const struct reason_option_codes_t *entry;
8296 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
8298 if (entry->reason == reason)
8299 return entry->option_code;
8301 return 0;
8304 /* Callback from cpp_error for PFILE to print diagnostics from the
8305 preprocessor. The diagnostic is of type LEVEL, with REASON set
8306 to the reason code if LEVEL is represents a warning, at location
8307 LOCATION unless this is after lexing and the compiler's location
8308 should be used instead, with column number possibly overridden by
8309 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
8310 the arguments. Returns true if a diagnostic was emitted, false
8311 otherwise. */
8313 bool
8314 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8315 location_t location, unsigned int column_override,
8316 const char *msg, va_list *ap)
8318 diagnostic_info diagnostic;
8319 diagnostic_t dlevel;
8320 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
8321 bool ret;
8323 switch (level)
8325 case CPP_DL_WARNING_SYSHDR:
8326 if (flag_no_output)
8327 return false;
8328 global_dc->dc_warn_system_headers = 1;
8329 /* Fall through. */
8330 case CPP_DL_WARNING:
8331 if (flag_no_output)
8332 return false;
8333 dlevel = DK_WARNING;
8334 break;
8335 case CPP_DL_PEDWARN:
8336 if (flag_no_output && !flag_pedantic_errors)
8337 return false;
8338 dlevel = DK_PEDWARN;
8339 break;
8340 case CPP_DL_ERROR:
8341 dlevel = DK_ERROR;
8342 break;
8343 case CPP_DL_ICE:
8344 dlevel = DK_ICE;
8345 break;
8346 case CPP_DL_NOTE:
8347 dlevel = DK_NOTE;
8348 break;
8349 case CPP_DL_FATAL:
8350 dlevel = DK_FATAL;
8351 break;
8352 default:
8353 gcc_unreachable ();
8355 if (done_lexing)
8356 location = input_location;
8357 diagnostic_set_info_translated (&diagnostic, msg, ap,
8358 location, dlevel);
8359 if (column_override)
8360 diagnostic_override_column (&diagnostic, column_override);
8361 diagnostic_override_option_index (&diagnostic,
8362 c_option_controlling_cpp_error (reason));
8363 ret = report_diagnostic (&diagnostic);
8364 if (level == CPP_DL_WARNING_SYSHDR)
8365 global_dc->dc_warn_system_headers = save_warn_system_headers;
8366 return ret;
8369 /* Convert a character from the host to the target execution character
8370 set. cpplib handles this, mostly. */
8372 HOST_WIDE_INT
8373 c_common_to_target_charset (HOST_WIDE_INT c)
8375 /* Character constants in GCC proper are sign-extended under -fsigned-char,
8376 zero-extended under -fno-signed-char. cpplib insists that characters
8377 and character constants are always unsigned. Hence we must convert
8378 back and forth. */
8379 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
8381 uc = cpp_host_to_exec_charset (parse_in, uc);
8383 if (flag_signed_char)
8384 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
8385 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
8386 else
8387 return uc;
8390 /* Build the result of __builtin_offsetof. EXPR is a nested sequence of
8391 component references, with STOP_REF, or alternatively an INDIRECT_REF of
8392 NULL, at the bottom; much like the traditional rendering of offsetof as a
8393 macro. Returns the folded and properly cast result. */
8395 static tree
8396 fold_offsetof_1 (tree expr, tree stop_ref)
8398 enum tree_code code = PLUS_EXPR;
8399 tree base, off, t;
8401 if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK)
8402 return size_zero_node;
8404 switch (TREE_CODE (expr))
8406 case ERROR_MARK:
8407 return expr;
8409 case VAR_DECL:
8410 error ("cannot apply %<offsetof%> to static data member %qD", expr);
8411 return error_mark_node;
8413 case CALL_EXPR:
8414 case TARGET_EXPR:
8415 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
8416 return error_mark_node;
8418 case NOP_EXPR:
8419 case INDIRECT_REF:
8420 if (!integer_zerop (TREE_OPERAND (expr, 0)))
8422 error ("cannot apply %<offsetof%> to a non constant address");
8423 return error_mark_node;
8425 return size_zero_node;
8427 case COMPONENT_REF:
8428 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
8429 if (base == error_mark_node)
8430 return base;
8432 t = TREE_OPERAND (expr, 1);
8433 if (DECL_C_BIT_FIELD (t))
8435 error ("attempt to take address of bit-field structure "
8436 "member %qD", t);
8437 return error_mark_node;
8439 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
8440 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
8442 / BITS_PER_UNIT));
8443 break;
8445 case ARRAY_REF:
8446 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
8447 if (base == error_mark_node)
8448 return base;
8450 t = TREE_OPERAND (expr, 1);
8451 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0)
8453 code = MINUS_EXPR;
8454 t = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (t), t);
8456 t = convert (sizetype, t);
8457 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
8459 /* Check if the offset goes beyond the upper bound of the array. */
8460 if (code == PLUS_EXPR && TREE_CODE (t) == INTEGER_CST)
8462 tree upbound = array_ref_up_bound (expr);
8463 if (upbound != NULL_TREE
8464 && TREE_CODE (upbound) == INTEGER_CST
8465 && !tree_int_cst_equal (upbound,
8466 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
8468 upbound = size_binop (PLUS_EXPR, upbound,
8469 build_int_cst (TREE_TYPE (upbound), 1));
8470 if (tree_int_cst_lt (upbound, t))
8472 tree v;
8474 for (v = TREE_OPERAND (expr, 0);
8475 TREE_CODE (v) == COMPONENT_REF;
8476 v = TREE_OPERAND (v, 0))
8477 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
8478 == RECORD_TYPE)
8480 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
8481 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
8482 if (TREE_CODE (fld_chain) == FIELD_DECL)
8483 break;
8485 if (fld_chain)
8486 break;
8488 /* Don't warn if the array might be considered a poor
8489 man's flexible array member with a very permissive
8490 definition thereof. */
8491 if (TREE_CODE (v) == ARRAY_REF
8492 || TREE_CODE (v) == COMPONENT_REF)
8493 warning (OPT_Warray_bounds,
8494 "index %E denotes an offset "
8495 "greater than size of %qT",
8496 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
8500 break;
8502 case COMPOUND_EXPR:
8503 /* Handle static members of volatile structs. */
8504 t = TREE_OPERAND (expr, 1);
8505 gcc_assert (TREE_CODE (t) == VAR_DECL);
8506 return fold_offsetof_1 (t, stop_ref);
8508 default:
8509 gcc_unreachable ();
8512 return size_binop (code, base, off);
8515 tree
8516 fold_offsetof (tree expr, tree stop_ref)
8518 /* Convert back from the internal sizetype to size_t. */
8519 return convert (size_type_node, fold_offsetof_1 (expr, stop_ref));
8522 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
8523 expression, because B will always be true. */
8525 void
8526 warn_for_omitted_condop (location_t location, tree cond)
8528 if (truth_value_p (TREE_CODE (cond)))
8529 warning_at (location, OPT_Wparentheses,
8530 "the omitted middle operand in ?: will always be %<true%>, "
8531 "suggest explicit middle operand");
8534 /* Print an error message for an invalid lvalue. USE says
8535 how the lvalue is being used and so selects the error message. */
8537 void
8538 lvalue_error (enum lvalue_use use)
8540 switch (use)
8542 case lv_assign:
8543 error ("lvalue required as left operand of assignment");
8544 break;
8545 case lv_increment:
8546 error ("lvalue required as increment operand");
8547 break;
8548 case lv_decrement:
8549 error ("lvalue required as decrement operand");
8550 break;
8551 case lv_addressof:
8552 error ("lvalue required as unary %<&%> operand");
8553 break;
8554 case lv_asm:
8555 error ("lvalue required in asm statement");
8556 break;
8557 default:
8558 gcc_unreachable ();
8562 /* *PTYPE is an incomplete array. Complete it with a domain based on
8563 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
8564 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8565 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
8568 complete_array_type (tree *ptype, tree initial_value, bool do_default)
8570 tree maxindex, type, main_type, elt, unqual_elt;
8571 int failure = 0, quals;
8572 hashval_t hashcode = 0;
8574 maxindex = size_zero_node;
8575 if (initial_value)
8577 if (TREE_CODE (initial_value) == STRING_CST)
8579 int eltsize
8580 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8581 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
8583 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8585 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
8587 if (VEC_empty (constructor_elt, v))
8589 if (pedantic)
8590 failure = 3;
8591 maxindex = integer_minus_one_node;
8593 else
8595 tree curindex;
8596 unsigned HOST_WIDE_INT cnt;
8597 constructor_elt *ce;
8598 bool fold_p = false;
8600 if (VEC_index (constructor_elt, v, 0)->index)
8601 maxindex = fold_convert_loc (input_location, sizetype,
8602 VEC_index (constructor_elt,
8603 v, 0)->index);
8604 curindex = maxindex;
8606 for (cnt = 1;
8607 VEC_iterate (constructor_elt, v, cnt, ce);
8608 cnt++)
8610 bool curfold_p = false;
8611 if (ce->index)
8612 curindex = ce->index, curfold_p = true;
8613 else
8615 if (fold_p)
8616 curindex = fold_convert (sizetype, curindex);
8617 curindex = size_binop (PLUS_EXPR, curindex,
8618 size_one_node);
8620 if (tree_int_cst_lt (maxindex, curindex))
8621 maxindex = curindex, fold_p = curfold_p;
8623 if (fold_p)
8624 maxindex = fold_convert (sizetype, maxindex);
8627 else
8629 /* Make an error message unless that happened already. */
8630 if (initial_value != error_mark_node)
8631 failure = 1;
8634 else
8636 failure = 2;
8637 if (!do_default)
8638 return failure;
8641 type = *ptype;
8642 elt = TREE_TYPE (type);
8643 quals = TYPE_QUALS (strip_array_types (elt));
8644 if (quals == 0)
8645 unqual_elt = elt;
8646 else
8647 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
8649 /* Using build_distinct_type_copy and modifying things afterward instead
8650 of using build_array_type to create a new type preserves all of the
8651 TYPE_LANG_FLAG_? bits that the front end may have set. */
8652 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8653 TREE_TYPE (main_type) = unqual_elt;
8654 TYPE_DOMAIN (main_type) = build_index_type (maxindex);
8655 layout_type (main_type);
8657 /* Make sure we have the canonical MAIN_TYPE. */
8658 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
8659 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
8660 hashcode);
8661 main_type = type_hash_canon (hashcode, main_type);
8663 /* Fix the canonical type. */
8664 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
8665 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
8666 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
8667 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
8668 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
8669 != TYPE_DOMAIN (main_type)))
8670 TYPE_CANONICAL (main_type)
8671 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
8672 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
8673 else
8674 TYPE_CANONICAL (main_type) = main_type;
8676 if (quals == 0)
8677 type = main_type;
8678 else
8679 type = c_build_qualified_type (main_type, quals);
8681 if (COMPLETE_TYPE_P (type)
8682 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8683 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
8685 error ("size of array is too large");
8686 /* If we proceed with the array type as it is, we'll eventually
8687 crash in tree_low_cst(). */
8688 type = error_mark_node;
8691 *ptype = type;
8692 return failure;
8696 /* Used to help initialize the builtin-types.def table. When a type of
8697 the correct size doesn't exist, use error_mark_node instead of NULL.
8698 The later results in segfaults even when a decl using the type doesn't
8699 get invoked. */
8701 tree
8702 builtin_type_for_size (int size, bool unsignedp)
8704 tree type = lang_hooks.types.type_for_size (size, unsignedp);
8705 return type ? type : error_mark_node;
8708 /* A helper function for resolve_overloaded_builtin in resolving the
8709 overloaded __sync_ builtins. Returns a positive power of 2 if the
8710 first operand of PARAMS is a pointer to a supported data type.
8711 Returns 0 if an error is encountered. */
8713 static int
8714 sync_resolve_size (tree function, VEC(tree,gc) *params)
8716 tree type;
8717 int size;
8719 if (VEC_empty (tree, params))
8721 error ("too few arguments to function %qE", function);
8722 return 0;
8725 type = TREE_TYPE (VEC_index (tree, params, 0));
8726 if (TREE_CODE (type) != POINTER_TYPE)
8727 goto incompatible;
8729 type = TREE_TYPE (type);
8730 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8731 goto incompatible;
8733 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8734 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
8735 return size;
8737 incompatible:
8738 error ("incompatible type for argument %d of %qE", 1, function);
8739 return 0;
8742 /* A helper function for resolve_overloaded_builtin. Adds casts to
8743 PARAMS to make arguments match up with those of FUNCTION. Drops
8744 the variadic arguments at the end. Returns false if some error
8745 was encountered; true on success. */
8747 static bool
8748 sync_resolve_params (tree orig_function, tree function, VEC(tree, gc) *params)
8750 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
8751 tree ptype;
8752 unsigned int parmnum;
8754 /* We've declared the implementation functions to use "volatile void *"
8755 as the pointer parameter, so we shouldn't get any complaints from the
8756 call to check_function_arguments what ever type the user used. */
8757 arg_types = TREE_CHAIN (arg_types);
8758 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
8760 /* For the rest of the values, we need to cast these to FTYPE, so that we
8761 don't get warnings for passing pointer types, etc. */
8762 parmnum = 0;
8763 while (arg_types != void_list_node)
8765 tree val;
8767 ++parmnum;
8768 if (VEC_length (tree, params) <= parmnum)
8770 error ("too few arguments to function %qE", orig_function);
8771 return false;
8774 /* ??? Ideally for the first conversion we'd use convert_for_assignment
8775 so that we get warnings for anything that doesn't match the pointer
8776 type. This isn't portable across the C and C++ front ends atm. */
8777 val = VEC_index (tree, params, parmnum);
8778 val = convert (ptype, val);
8779 val = convert (TREE_VALUE (arg_types), val);
8780 VEC_replace (tree, params, parmnum, val);
8782 arg_types = TREE_CHAIN (arg_types);
8785 /* The definition of these primitives is variadic, with the remaining
8786 being "an optional list of variables protected by the memory barrier".
8787 No clue what that's supposed to mean, precisely, but we consider all
8788 call-clobbered variables to be protected so we're safe. */
8789 VEC_truncate (tree, params, parmnum + 1);
8791 return true;
8794 /* A helper function for resolve_overloaded_builtin. Adds a cast to
8795 RESULT to make it match the type of the first pointer argument in
8796 PARAMS. */
8798 static tree
8799 sync_resolve_return (tree first_param, tree result)
8801 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
8802 ptype = TYPE_MAIN_VARIANT (ptype);
8803 return convert (ptype, result);
8806 /* Some builtin functions are placeholders for other expressions. This
8807 function should be called immediately after parsing the call expression
8808 before surrounding code has committed to the type of the expression.
8810 LOC is the location of the builtin call.
8812 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
8813 PARAMS is the argument list for the call. The return value is non-null
8814 when expansion is complete, and null if normal processing should
8815 continue. */
8817 tree
8818 resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params)
8820 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
8821 switch (DECL_BUILT_IN_CLASS (function))
8823 case BUILT_IN_NORMAL:
8824 break;
8825 case BUILT_IN_MD:
8826 if (targetm.resolve_overloaded_builtin)
8827 return targetm.resolve_overloaded_builtin (loc, function, params);
8828 else
8829 return NULL_TREE;
8830 default:
8831 return NULL_TREE;
8834 /* Handle BUILT_IN_NORMAL here. */
8835 switch (orig_code)
8837 case BUILT_IN_FETCH_AND_ADD_N:
8838 case BUILT_IN_FETCH_AND_SUB_N:
8839 case BUILT_IN_FETCH_AND_OR_N:
8840 case BUILT_IN_FETCH_AND_AND_N:
8841 case BUILT_IN_FETCH_AND_XOR_N:
8842 case BUILT_IN_FETCH_AND_NAND_N:
8843 case BUILT_IN_ADD_AND_FETCH_N:
8844 case BUILT_IN_SUB_AND_FETCH_N:
8845 case BUILT_IN_OR_AND_FETCH_N:
8846 case BUILT_IN_AND_AND_FETCH_N:
8847 case BUILT_IN_XOR_AND_FETCH_N:
8848 case BUILT_IN_NAND_AND_FETCH_N:
8849 case BUILT_IN_BOOL_COMPARE_AND_SWAP_N:
8850 case BUILT_IN_VAL_COMPARE_AND_SWAP_N:
8851 case BUILT_IN_LOCK_TEST_AND_SET_N:
8852 case BUILT_IN_LOCK_RELEASE_N:
8854 int n = sync_resolve_size (function, params);
8855 tree new_function, first_param, result;
8857 if (n == 0)
8858 return error_mark_node;
8860 new_function = built_in_decls[orig_code + exact_log2 (n) + 1];
8861 if (!sync_resolve_params (function, new_function, params))
8862 return error_mark_node;
8864 first_param = VEC_index (tree, params, 0);
8865 result = build_function_call_vec (loc, new_function, params, NULL);
8866 if (orig_code != BUILT_IN_BOOL_COMPARE_AND_SWAP_N
8867 && orig_code != BUILT_IN_LOCK_RELEASE_N)
8868 result = sync_resolve_return (first_param, result);
8870 return result;
8873 default:
8874 return NULL_TREE;
8878 /* Ignoring their sign, return true if two scalar types are the same. */
8879 bool
8880 same_scalar_type_ignoring_signedness (tree t1, tree t2)
8882 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
8884 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
8885 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
8886 || c2 == FIXED_POINT_TYPE));
8888 /* Equality works here because c_common_signed_type uses
8889 TYPE_MAIN_VARIANT. */
8890 return c_common_signed_type (t1)
8891 == c_common_signed_type (t2);
8894 /* Check for missing format attributes on function pointers. LTYPE is
8895 the new type or left-hand side type. RTYPE is the old type or
8896 right-hand side type. Returns TRUE if LTYPE is missing the desired
8897 attribute. */
8899 bool
8900 check_missing_format_attribute (tree ltype, tree rtype)
8902 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
8903 tree ra;
8905 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
8906 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
8907 break;
8908 if (ra)
8910 tree la;
8911 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
8912 if (is_attribute_p ("format", TREE_PURPOSE (la)))
8913 break;
8914 return !la;
8916 else
8917 return false;
8920 /* Subscripting with type char is likely to lose on a machine where
8921 chars are signed. So warn on any machine, but optionally. Don't
8922 warn for unsigned char since that type is safe. Don't warn for
8923 signed char because anyone who uses that must have done so
8924 deliberately. Furthermore, we reduce the false positive load by
8925 warning only for non-constant value of type char. */
8927 void
8928 warn_array_subscript_with_type_char (tree index)
8930 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
8931 && TREE_CODE (index) != INTEGER_CST)
8932 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
8935 /* Implement -Wparentheses for the unexpected C precedence rules, to
8936 cover cases like x + y << z which readers are likely to
8937 misinterpret. We have seen an expression in which CODE is a binary
8938 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
8939 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
8940 CODE_RIGHT may be ERROR_MARK, which means that that side of the
8941 expression was not formed using a binary or unary operator, or it
8942 was enclosed in parentheses. */
8944 void
8945 warn_about_parentheses (enum tree_code code,
8946 enum tree_code code_left, tree arg_left,
8947 enum tree_code code_right, tree arg_right)
8949 if (!warn_parentheses)
8950 return;
8952 /* This macro tests that the expression ARG with original tree code
8953 CODE appears to be a boolean expression. or the result of folding a
8954 boolean expression. */
8955 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
8956 (truth_value_p (TREE_CODE (ARG)) \
8957 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
8958 /* Folding may create 0 or 1 integers from other expressions. */ \
8959 || ((CODE) != INTEGER_CST \
8960 && (integer_onep (ARG) || integer_zerop (ARG))))
8962 switch (code)
8964 case LSHIFT_EXPR:
8965 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
8966 warning (OPT_Wparentheses,
8967 "suggest parentheses around %<+%> inside %<<<%>");
8968 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
8969 warning (OPT_Wparentheses,
8970 "suggest parentheses around %<-%> inside %<<<%>");
8971 return;
8973 case RSHIFT_EXPR:
8974 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
8975 warning (OPT_Wparentheses,
8976 "suggest parentheses around %<+%> inside %<>>%>");
8977 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
8978 warning (OPT_Wparentheses,
8979 "suggest parentheses around %<-%> inside %<>>%>");
8980 return;
8982 case TRUTH_ORIF_EXPR:
8983 if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
8984 warning (OPT_Wparentheses,
8985 "suggest parentheses around %<&&%> within %<||%>");
8986 return;
8988 case BIT_IOR_EXPR:
8989 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
8990 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
8991 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
8992 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
8993 warning (OPT_Wparentheses,
8994 "suggest parentheses around arithmetic in operand of %<|%>");
8995 /* Check cases like x|y==z */
8996 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
8997 || TREE_CODE_CLASS (code_right) == tcc_comparison)
8998 warning (OPT_Wparentheses,
8999 "suggest parentheses around comparison in operand of %<|%>");
9000 /* Check cases like !x | y */
9001 else if (code_left == TRUTH_NOT_EXPR
9002 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
9003 warning (OPT_Wparentheses, "suggest parentheses around operand of "
9004 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
9005 return;
9007 case BIT_XOR_EXPR:
9008 if (code_left == BIT_AND_EXPR
9009 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
9010 || code_right == BIT_AND_EXPR
9011 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
9012 warning (OPT_Wparentheses,
9013 "suggest parentheses around arithmetic in operand of %<^%>");
9014 /* Check cases like x^y==z */
9015 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9016 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9017 warning (OPT_Wparentheses,
9018 "suggest parentheses around comparison in operand of %<^%>");
9019 return;
9021 case BIT_AND_EXPR:
9022 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9023 warning (OPT_Wparentheses,
9024 "suggest parentheses around %<+%> in operand of %<&%>");
9025 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9026 warning (OPT_Wparentheses,
9027 "suggest parentheses around %<-%> in operand of %<&%>");
9028 /* Check cases like x&y==z */
9029 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9030 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9031 warning (OPT_Wparentheses,
9032 "suggest parentheses around comparison in operand of %<&%>");
9033 /* Check cases like !x & y */
9034 else if (code_left == TRUTH_NOT_EXPR
9035 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
9036 warning (OPT_Wparentheses, "suggest parentheses around operand of "
9037 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
9038 return;
9040 case EQ_EXPR:
9041 if (TREE_CODE_CLASS (code_left) == tcc_comparison
9042 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9043 warning (OPT_Wparentheses,
9044 "suggest parentheses around comparison in operand of %<==%>");
9045 return;
9046 case NE_EXPR:
9047 if (TREE_CODE_CLASS (code_left) == tcc_comparison
9048 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9049 warning (OPT_Wparentheses,
9050 "suggest parentheses around comparison in operand of %<!=%>");
9051 return;
9053 default:
9054 if (TREE_CODE_CLASS (code) == tcc_comparison
9055 && ((TREE_CODE_CLASS (code_left) == tcc_comparison
9056 && code_left != NE_EXPR && code_left != EQ_EXPR
9057 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
9058 || (TREE_CODE_CLASS (code_right) == tcc_comparison
9059 && code_right != NE_EXPR && code_right != EQ_EXPR
9060 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
9061 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
9062 "have their mathematical meaning");
9063 return;
9065 #undef NOT_A_BOOLEAN_EXPR_P
9068 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
9070 void
9071 warn_for_unused_label (tree label)
9073 if (!TREE_USED (label))
9075 if (DECL_INITIAL (label))
9076 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
9077 else
9078 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
9082 #ifndef TARGET_HAS_TARGETCM
9083 struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;
9084 #endif
9086 /* Warn for division by zero according to the value of DIVISOR. LOC
9087 is the location of the division operator. */
9089 void
9090 warn_for_div_by_zero (location_t loc, tree divisor)
9092 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
9093 about division by zero. Do not issue a warning if DIVISOR has a
9094 floating-point type, since we consider 0.0/0.0 a valid way of
9095 generating a NaN. */
9096 if (c_inhibit_evaluation_warnings == 0
9097 && (integer_zerop (divisor) || fixed_zerop (divisor)))
9098 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
9101 /* Subroutine of build_binary_op. Give warnings for comparisons
9102 between signed and unsigned quantities that may fail. Do the
9103 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
9104 so that casts will be considered, but default promotions won't
9107 LOCATION is the location of the comparison operator.
9109 The arguments of this function map directly to local variables
9110 of build_binary_op. */
9112 void
9113 warn_for_sign_compare (location_t location,
9114 tree orig_op0, tree orig_op1,
9115 tree op0, tree op1,
9116 tree result_type, enum tree_code resultcode)
9118 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
9119 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
9120 int unsignedp0, unsignedp1;
9122 /* In C++, check for comparison of different enum types. */
9123 if (c_dialect_cxx()
9124 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
9125 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
9126 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
9127 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
9129 warning_at (location,
9130 OPT_Wsign_compare, "comparison between types %qT and %qT",
9131 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
9134 /* Do not warn if the comparison is being done in a signed type,
9135 since the signed type will only be chosen if it can represent
9136 all the values of the unsigned type. */
9137 if (!TYPE_UNSIGNED (result_type))
9138 /* OK */;
9139 /* Do not warn if both operands are unsigned. */
9140 else if (op0_signed == op1_signed)
9141 /* OK */;
9142 else
9144 tree sop, uop, base_type;
9145 bool ovf;
9147 if (op0_signed)
9148 sop = orig_op0, uop = orig_op1;
9149 else
9150 sop = orig_op1, uop = orig_op0;
9152 STRIP_TYPE_NOPS (sop);
9153 STRIP_TYPE_NOPS (uop);
9154 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
9155 ? TREE_TYPE (result_type) : result_type);
9157 /* Do not warn if the signed quantity is an unsuffixed integer
9158 literal (or some static constant expression involving such
9159 literals or a conditional expression involving such literals)
9160 and it is non-negative. */
9161 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
9162 /* OK */;
9163 /* Do not warn if the comparison is an equality operation, the
9164 unsigned quantity is an integral constant, and it would fit
9165 in the result if the result were signed. */
9166 else if (TREE_CODE (uop) == INTEGER_CST
9167 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
9168 && int_fits_type_p (uop, c_common_signed_type (base_type)))
9169 /* OK */;
9170 /* In C, do not warn if the unsigned quantity is an enumeration
9171 constant and its maximum value would fit in the result if the
9172 result were signed. */
9173 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
9174 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
9175 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
9176 c_common_signed_type (base_type)))
9177 /* OK */;
9178 else
9179 warning_at (location,
9180 OPT_Wsign_compare,
9181 "comparison between signed and unsigned integer expressions");
9184 /* Warn if two unsigned values are being compared in a size larger
9185 than their original size, and one (and only one) is the result of
9186 a `~' operator. This comparison will always fail.
9188 Also warn if one operand is a constant, and the constant does not
9189 have all bits set that are set in the ~ operand when it is
9190 extended. */
9192 op0 = get_narrower (op0, &unsignedp0);
9193 op1 = get_narrower (op1, &unsignedp1);
9195 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
9196 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
9198 if (TREE_CODE (op0) == BIT_NOT_EXPR)
9199 op0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
9200 if (TREE_CODE (op1) == BIT_NOT_EXPR)
9201 op1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
9203 if (host_integerp (op0, 0) || host_integerp (op1, 0))
9205 tree primop;
9206 HOST_WIDE_INT constant, mask;
9207 int unsignedp;
9208 unsigned int bits;
9210 if (host_integerp (op0, 0))
9212 primop = op1;
9213 unsignedp = unsignedp1;
9214 constant = tree_low_cst (op0, 0);
9216 else
9218 primop = op0;
9219 unsignedp = unsignedp0;
9220 constant = tree_low_cst (op1, 0);
9223 bits = TYPE_PRECISION (TREE_TYPE (primop));
9224 if (bits < TYPE_PRECISION (result_type)
9225 && bits < HOST_BITS_PER_LONG && unsignedp)
9227 mask = (~ (HOST_WIDE_INT) 0) << bits;
9228 if ((mask & constant) != mask)
9230 if (constant == 0)
9231 warning (OPT_Wsign_compare,
9232 "promoted ~unsigned is always non-zero");
9233 else
9234 warning_at (location, OPT_Wsign_compare,
9235 "comparison of promoted ~unsigned with constant");
9239 else if (unsignedp0 && unsignedp1
9240 && (TYPE_PRECISION (TREE_TYPE (op0))
9241 < TYPE_PRECISION (result_type))
9242 && (TYPE_PRECISION (TREE_TYPE (op1))
9243 < TYPE_PRECISION (result_type)))
9244 warning_at (location, OPT_Wsign_compare,
9245 "comparison of promoted ~unsigned with unsigned");
9249 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
9250 type via c_common_type. If -Wdouble-promotion is in use, and the
9251 conditions for warning have been met, issue a warning. GMSGID is
9252 the warning message. It must have two %T specifiers for the type
9253 that was converted (generally "float") and the type to which it was
9254 converted (generally "double), respectively. LOC is the location
9255 to which the awrning should refer. */
9257 void
9258 do_warn_double_promotion (tree result_type, tree type1, tree type2,
9259 const char *gmsgid, location_t loc)
9261 tree source_type;
9263 if (!warn_double_promotion)
9264 return;
9265 /* If the conversion will not occur at run-time, there is no need to
9266 warn about it. */
9267 if (c_inhibit_evaluation_warnings)
9268 return;
9269 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
9270 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
9271 return;
9272 if (TYPE_MAIN_VARIANT (type1) == float_type_node
9273 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
9274 source_type = type1;
9275 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
9276 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
9277 source_type = type2;
9278 else
9279 return;
9280 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
9283 /* Setup a TYPE_DECL node as a typedef representation.
9285 X is a TYPE_DECL for a typedef statement. Create a brand new
9286 ..._TYPE node (which will be just a variant of the existing
9287 ..._TYPE node with identical properties) and then install X
9288 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
9290 The whole point here is to end up with a situation where each
9291 and every ..._TYPE node the compiler creates will be uniquely
9292 associated with AT MOST one node representing a typedef name.
9293 This way, even though the compiler substitutes corresponding
9294 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
9295 early on, later parts of the compiler can always do the reverse
9296 translation and get back the corresponding typedef name. For
9297 example, given:
9299 typedef struct S MY_TYPE;
9300 MY_TYPE object;
9302 Later parts of the compiler might only know that `object' was of
9303 type `struct S' if it were not for code just below. With this
9304 code however, later parts of the compiler see something like:
9306 struct S' == struct S
9307 typedef struct S' MY_TYPE;
9308 struct S' object;
9310 And they can then deduce (from the node for type struct S') that
9311 the original object declaration was:
9313 MY_TYPE object;
9315 Being able to do this is important for proper support of protoize,
9316 and also for generating precise symbolic debugging information
9317 which takes full account of the programmer's (typedef) vocabulary.
9319 Obviously, we don't want to generate a duplicate ..._TYPE node if
9320 the TYPE_DECL node that we are now processing really represents a
9321 standard built-in type. */
9323 void
9324 set_underlying_type (tree x)
9326 if (x == error_mark_node)
9327 return;
9328 if (DECL_IS_BUILTIN (x))
9330 if (TYPE_NAME (TREE_TYPE (x)) == 0)
9331 TYPE_NAME (TREE_TYPE (x)) = x;
9333 else if (TREE_TYPE (x) != error_mark_node
9334 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
9336 tree tt = TREE_TYPE (x);
9337 DECL_ORIGINAL_TYPE (x) = tt;
9338 tt = build_variant_type_copy (tt);
9339 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
9340 TYPE_NAME (tt) = x;
9341 TREE_USED (tt) = TREE_USED (x);
9342 TREE_TYPE (x) = tt;
9346 /* Record the types used by the current global variable declaration
9347 being parsed, so that we can decide later to emit their debug info.
9348 Those types are in types_used_by_cur_var_decl, and we are going to
9349 store them in the types_used_by_vars_hash hash table.
9350 DECL is the declaration of the global variable that has been parsed. */
9352 void
9353 record_types_used_by_current_var_decl (tree decl)
9355 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
9357 while (!VEC_empty (tree, types_used_by_cur_var_decl))
9359 tree type = VEC_pop (tree, types_used_by_cur_var_decl);
9360 types_used_by_var_decl_insert (type, decl);
9364 /* The C and C++ parsers both use vectors to hold function arguments.
9365 For efficiency, we keep a cache of unused vectors. This is the
9366 cache. */
9368 typedef VEC(tree,gc)* tree_gc_vec;
9369 DEF_VEC_P(tree_gc_vec);
9370 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
9371 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
9373 /* Return a new vector from the cache. If the cache is empty,
9374 allocate a new vector. These vectors are GC'ed, so it is OK if the
9375 pointer is not released.. */
9377 VEC(tree,gc) *
9378 make_tree_vector (void)
9380 if (!VEC_empty (tree_gc_vec, tree_vector_cache))
9381 return VEC_pop (tree_gc_vec, tree_vector_cache);
9382 else
9384 /* Passing 0 to VEC_alloc returns NULL, and our callers require
9385 that we always return a non-NULL value. The vector code uses
9386 4 when growing a NULL vector, so we do too. */
9387 return VEC_alloc (tree, gc, 4);
9391 /* Release a vector of trees back to the cache. */
9393 void
9394 release_tree_vector (VEC(tree,gc) *vec)
9396 if (vec != NULL)
9398 VEC_truncate (tree, vec, 0);
9399 VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
9403 /* Get a new tree vector holding a single tree. */
9405 VEC(tree,gc) *
9406 make_tree_vector_single (tree t)
9408 VEC(tree,gc) *ret = make_tree_vector ();
9409 VEC_quick_push (tree, ret, t);
9410 return ret;
9413 /* Get a new tree vector which is a copy of an existing one. */
9415 VEC(tree,gc) *
9416 make_tree_vector_copy (const VEC(tree,gc) *orig)
9418 VEC(tree,gc) *ret;
9419 unsigned int ix;
9420 tree t;
9422 ret = make_tree_vector ();
9423 VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
9424 FOR_EACH_VEC_ELT (tree, orig, ix, t)
9425 VEC_quick_push (tree, ret, t);
9426 return ret;
9429 #include "gt-c-family-c-common.h"