PR libfortran/47970
[official-gcc.git] / gcc / c-family / c-common.c
blob0329bc7e01cb58456095b663971e4cbc3325305f
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "intl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "output.h"
30 #include "c-pragma.h"
31 #include "ggc.h"
32 #include "c-common.h"
33 #include "c-objc.h"
34 #include "tm_p.h"
35 #include "obstack.h"
36 #include "cpplib.h"
37 #include "target.h"
38 #include "common/common-target.h"
39 #include "langhooks.h"
40 #include "tree-inline.h"
41 #include "toplev.h"
42 #include "diagnostic.h"
43 #include "tree-iterator.h"
44 #include "hashtab.h"
45 #include "tree-mudflap.h"
46 #include "opts.h"
47 #include "cgraph.h"
48 #include "target-def.h"
49 #include "libfuncs.h"
51 cpp_reader *parse_in; /* Declared in c-pragma.h. */
53 /* The following symbols are subsumed in the c_global_trees array, and
54 listed here individually for documentation purposes.
56 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
58 tree short_integer_type_node;
59 tree long_integer_type_node;
60 tree long_long_integer_type_node;
61 tree int128_integer_type_node;
63 tree short_unsigned_type_node;
64 tree long_unsigned_type_node;
65 tree long_long_unsigned_type_node;
66 tree int128_unsigned_type_node;
68 tree truthvalue_type_node;
69 tree truthvalue_false_node;
70 tree truthvalue_true_node;
72 tree ptrdiff_type_node;
74 tree unsigned_char_type_node;
75 tree signed_char_type_node;
76 tree wchar_type_node;
78 tree char16_type_node;
79 tree char32_type_node;
81 tree float_type_node;
82 tree double_type_node;
83 tree long_double_type_node;
85 tree complex_integer_type_node;
86 tree complex_float_type_node;
87 tree complex_double_type_node;
88 tree complex_long_double_type_node;
90 tree dfloat32_type_node;
91 tree dfloat64_type_node;
92 tree_dfloat128_type_node;
94 tree intQI_type_node;
95 tree intHI_type_node;
96 tree intSI_type_node;
97 tree intDI_type_node;
98 tree intTI_type_node;
100 tree unsigned_intQI_type_node;
101 tree unsigned_intHI_type_node;
102 tree unsigned_intSI_type_node;
103 tree unsigned_intDI_type_node;
104 tree unsigned_intTI_type_node;
106 tree widest_integer_literal_type_node;
107 tree widest_unsigned_literal_type_node;
109 Nodes for types `void *' and `const void *'.
111 tree ptr_type_node, const_ptr_type_node;
113 Nodes for types `char *' and `const char *'.
115 tree string_type_node, const_string_type_node;
117 Type `char[SOMENUMBER]'.
118 Used when an array of char is needed and the size is irrelevant.
120 tree char_array_type_node;
122 Type `int[SOMENUMBER]' or something like it.
123 Used when an array of int needed and the size is irrelevant.
125 tree int_array_type_node;
127 Type `wchar_t[SOMENUMBER]' or something like it.
128 Used when a wide string literal is created.
130 tree wchar_array_type_node;
132 Type `char16_t[SOMENUMBER]' or something like it.
133 Used when a UTF-16 string literal is created.
135 tree char16_array_type_node;
137 Type `char32_t[SOMENUMBER]' or something like it.
138 Used when a UTF-32 string literal is created.
140 tree char32_array_type_node;
142 Type `int ()' -- used for implicit declaration of functions.
144 tree default_function_type;
146 A VOID_TYPE node, packaged in a TREE_LIST.
148 tree void_list_node;
150 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
151 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
152 VAR_DECLS, but C++ does.)
154 tree function_name_decl_node;
155 tree pretty_function_name_decl_node;
156 tree c99_function_name_decl_node;
158 Stack of nested function name VAR_DECLs.
160 tree saved_function_name_decls;
164 tree c_global_trees[CTI_MAX];
166 /* Switches common to the C front ends. */
168 /* Nonzero means don't output line number information. */
170 char flag_no_line_commands;
172 /* Nonzero causes -E output not to be done, but directives such as
173 #define that have side effects are still obeyed. */
175 char flag_no_output;
177 /* Nonzero means dump macros in some fashion. */
179 char flag_dump_macros;
181 /* Nonzero means pass #include lines through to the output. */
183 char flag_dump_includes;
185 /* Nonzero means process PCH files while preprocessing. */
187 bool flag_pch_preprocess;
189 /* The file name to which we should write a precompiled header, or
190 NULL if no header will be written in this compile. */
192 const char *pch_file;
194 /* Nonzero if an ISO standard was selected. It rejects macros in the
195 user's namespace. */
196 int flag_iso;
198 /* Warn about #pragma directives that are not recognized. */
200 int warn_unknown_pragmas; /* Tri state variable. */
202 /* Warn about format/argument anomalies in calls to formatted I/O functions
203 (*printf, *scanf, strftime, strfmon, etc.). */
205 int warn_format;
207 /* C/ObjC language option variables. */
210 /* Nonzero means allow type mismatches in conditional expressions;
211 just make their values `void'. */
213 int flag_cond_mismatch;
215 /* Nonzero means enable C89 Amendment 1 features. */
217 int flag_isoc94;
219 /* Nonzero means use the ISO C99 (or C1X) dialect of C. */
221 int flag_isoc99;
223 /* Nonzero means use the ISO C1X dialect of C. */
225 int flag_isoc1x;
227 /* Nonzero means that we have builtin functions, and main is an int. */
229 int flag_hosted = 1;
232 /* ObjC language option variables. */
235 /* Tells the compiler that this is a special run. Do not perform any
236 compiling, instead we are to test some platform dependent features
237 and output a C header file with appropriate definitions. */
239 int print_struct_values;
241 /* Tells the compiler what is the constant string class for ObjC. */
243 const char *constant_string_class_name;
246 /* C++ language option variables. */
249 /* Nonzero means generate separate instantiation control files and
250 juggle them at link time. */
252 int flag_use_repository;
254 /* The C++ dialect being used. C++98 is the default. */
256 enum cxx_dialect cxx_dialect = cxx98;
258 /* Maximum template instantiation depth. This limit exists to limit the
259 time it takes to notice excessively recursive template instantiations.
261 The default is lower than the 1024 recommended by the C++0x standard
262 because G++ runs out of stack before 1024 with highly recursive template
263 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
265 int max_tinst_depth = 900;
267 /* The elements of `ridpointers' are identifier nodes for the reserved
268 type names and storage classes. It is indexed by a RID_... value. */
269 tree *ridpointers;
271 tree (*make_fname_decl) (location_t, tree, int);
273 /* Nonzero means don't warn about problems that occur when the code is
274 executed. */
275 int c_inhibit_evaluation_warnings;
277 /* Whether we are building a boolean conversion inside
278 convert_for_assignment, or some other late binary operation. If
279 build_binary_op is called for C (from code shared by C and C++) in
280 this case, then the operands have already been folded and the
281 result will not be folded again, so C_MAYBE_CONST_EXPR should not
282 be generated. */
283 bool in_late_binary_op;
285 /* Whether lexing has been completed, so subsequent preprocessor
286 errors should use the compiler's input_location. */
287 bool done_lexing = false;
289 /* Information about how a function name is generated. */
290 struct fname_var_t
292 tree *const decl; /* pointer to the VAR_DECL. */
293 const unsigned rid; /* RID number for the identifier. */
294 const int pretty; /* How pretty is it? */
297 /* The three ways of getting then name of the current function. */
299 const struct fname_var_t fname_vars[] =
301 /* C99 compliant __func__, must be first. */
302 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
303 /* GCC __FUNCTION__ compliant. */
304 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
305 /* GCC __PRETTY_FUNCTION__ compliant. */
306 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
307 {NULL, 0, 0},
310 /* Global visibility options. */
311 struct visibility_flags visibility_options;
313 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
314 static tree check_case_value (tree);
315 static bool check_case_bounds (tree, tree, tree *, tree *);
317 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_always_inline_attribute (tree *, tree, tree, int,
327 bool *);
328 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
335 bool *);
336 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_transparent_union_attribute (tree *, tree, tree,
338 int, bool *);
339 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
345 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
346 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
349 static tree handle_visibility_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_tls_model_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_no_instrument_function_attribute (tree *, tree,
354 tree, int, bool *);
355 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_deprecated_attribute (tree *, tree, tree, int,
362 bool *);
363 static tree handle_vector_size_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
378 static void check_function_nonnull (tree, int, tree *);
379 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
380 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
381 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
382 static int resort_field_decl_cmp (const void *, const void *);
384 /* Reserved words. The third field is a mask: keywords are disabled
385 if they match the mask.
387 Masks for languages:
388 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
389 C --std=c99: D_CXXONLY | D_OBJC
390 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
391 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
392 C++ --std=c0x: D_CONLY | D_OBJC
393 ObjC++ is like C++ except that D_OBJC is not set
395 If -fno-asm is used, D_ASM is added to the mask. If
396 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
397 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
398 In C with -Wc++-compat, we warn if D_CXXWARN is set.
400 Note the complication of the D_CXX_OBJC keywords. These are
401 reserved words such as 'class'. In C++, 'class' is a reserved
402 word. In Objective-C++ it is too. In Objective-C, it is a
403 reserved word too, but only if it follows an '@' sign.
405 const struct c_common_resword c_common_reswords[] =
407 { "_Alignas", RID_ALIGNAS, D_CONLY },
408 { "_Alignof", RID_ALIGNOF, D_CONLY },
409 { "_Bool", RID_BOOL, D_CONLY },
410 { "_Complex", RID_COMPLEX, 0 },
411 { "_Imaginary", RID_IMAGINARY, D_CONLY },
412 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
413 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
414 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
415 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
416 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
417 { "_Sat", RID_SAT, D_CONLY | D_EXT },
418 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
419 { "_Noreturn", RID_NORETURN, D_CONLY },
420 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
421 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
422 { "__alignof", RID_ALIGNOF, 0 },
423 { "__alignof__", RID_ALIGNOF, 0 },
424 { "__asm", RID_ASM, 0 },
425 { "__asm__", RID_ASM, 0 },
426 { "__attribute", RID_ATTRIBUTE, 0 },
427 { "__attribute__", RID_ATTRIBUTE, 0 },
428 { "__bases", RID_BASES, D_CXXONLY },
429 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
430 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
431 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, D_CONLY },
432 { "__builtin_offsetof", RID_OFFSETOF, 0 },
433 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
434 { "__builtin_va_arg", RID_VA_ARG, 0 },
435 { "__complex", RID_COMPLEX, 0 },
436 { "__complex__", RID_COMPLEX, 0 },
437 { "__const", RID_CONST, 0 },
438 { "__const__", RID_CONST, 0 },
439 { "__decltype", RID_DECLTYPE, D_CXXONLY },
440 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
441 { "__extension__", RID_EXTENSION, 0 },
442 { "__func__", RID_C99_FUNCTION_NAME, 0 },
443 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
444 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
445 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
446 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
447 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
448 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
449 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
450 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
451 { "__imag", RID_IMAGPART, 0 },
452 { "__imag__", RID_IMAGPART, 0 },
453 { "__inline", RID_INLINE, 0 },
454 { "__inline__", RID_INLINE, 0 },
455 { "__int128", RID_INT128, 0 },
456 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
457 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
458 { "__is_class", RID_IS_CLASS, D_CXXONLY },
459 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
460 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
461 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
462 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
463 { "__is_pod", RID_IS_POD, D_CXXONLY },
464 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
465 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
466 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
467 { "__is_union", RID_IS_UNION, D_CXXONLY },
468 { "__label__", RID_LABEL, 0 },
469 { "__null", RID_NULL, 0 },
470 { "__real", RID_REALPART, 0 },
471 { "__real__", RID_REALPART, 0 },
472 { "__restrict", RID_RESTRICT, 0 },
473 { "__restrict__", RID_RESTRICT, 0 },
474 { "__signed", RID_SIGNED, 0 },
475 { "__signed__", RID_SIGNED, 0 },
476 { "__thread", RID_THREAD, 0 },
477 { "__typeof", RID_TYPEOF, 0 },
478 { "__typeof__", RID_TYPEOF, 0 },
479 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
480 { "__volatile", RID_VOLATILE, 0 },
481 { "__volatile__", RID_VOLATILE, 0 },
482 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
483 { "asm", RID_ASM, D_ASM },
484 { "auto", RID_AUTO, 0 },
485 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
486 { "break", RID_BREAK, 0 },
487 { "case", RID_CASE, 0 },
488 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
489 { "char", RID_CHAR, 0 },
490 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
491 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
492 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
493 { "const", RID_CONST, 0 },
494 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
495 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
496 { "continue", RID_CONTINUE, 0 },
497 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
498 { "default", RID_DEFAULT, 0 },
499 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
500 { "do", RID_DO, 0 },
501 { "double", RID_DOUBLE, 0 },
502 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
503 { "else", RID_ELSE, 0 },
504 { "enum", RID_ENUM, 0 },
505 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
506 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
507 { "extern", RID_EXTERN, 0 },
508 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
509 { "float", RID_FLOAT, 0 },
510 { "for", RID_FOR, 0 },
511 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
512 { "goto", RID_GOTO, 0 },
513 { "if", RID_IF, 0 },
514 { "inline", RID_INLINE, D_EXT89 },
515 { "int", RID_INT, 0 },
516 { "long", RID_LONG, 0 },
517 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
518 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
519 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
520 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
521 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
522 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
523 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
524 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
525 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
526 { "register", RID_REGISTER, 0 },
527 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
528 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
529 { "return", RID_RETURN, 0 },
530 { "short", RID_SHORT, 0 },
531 { "signed", RID_SIGNED, 0 },
532 { "sizeof", RID_SIZEOF, 0 },
533 { "static", RID_STATIC, 0 },
534 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
535 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
536 { "struct", RID_STRUCT, 0 },
537 { "switch", RID_SWITCH, 0 },
538 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
539 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
540 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
541 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
542 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
543 { "typedef", RID_TYPEDEF, 0 },
544 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
545 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
546 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
547 { "union", RID_UNION, 0 },
548 { "unsigned", RID_UNSIGNED, 0 },
549 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
550 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
551 { "void", RID_VOID, 0 },
552 { "volatile", RID_VOLATILE, 0 },
553 { "wchar_t", RID_WCHAR, D_CXXONLY },
554 { "while", RID_WHILE, 0 },
555 /* These Objective-C keywords are recognized only immediately after
556 an '@'. */
557 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
558 { "defs", RID_AT_DEFS, D_OBJC },
559 { "encode", RID_AT_ENCODE, D_OBJC },
560 { "end", RID_AT_END, D_OBJC },
561 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
562 { "interface", RID_AT_INTERFACE, D_OBJC },
563 { "protocol", RID_AT_PROTOCOL, D_OBJC },
564 { "selector", RID_AT_SELECTOR, D_OBJC },
565 { "finally", RID_AT_FINALLY, D_OBJC },
566 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
567 { "optional", RID_AT_OPTIONAL, D_OBJC },
568 { "required", RID_AT_REQUIRED, D_OBJC },
569 { "property", RID_AT_PROPERTY, D_OBJC },
570 { "package", RID_AT_PACKAGE, D_OBJC },
571 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
572 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
573 /* These are recognized only in protocol-qualifier context
574 (see above) */
575 { "bycopy", RID_BYCOPY, D_OBJC },
576 { "byref", RID_BYREF, D_OBJC },
577 { "in", RID_IN, D_OBJC },
578 { "inout", RID_INOUT, D_OBJC },
579 { "oneway", RID_ONEWAY, D_OBJC },
580 { "out", RID_OUT, D_OBJC },
581 /* These are recognized inside a property attribute list */
582 { "assign", RID_ASSIGN, D_OBJC },
583 { "copy", RID_COPY, D_OBJC },
584 { "getter", RID_GETTER, D_OBJC },
585 { "nonatomic", RID_NONATOMIC, D_OBJC },
586 { "readonly", RID_READONLY, D_OBJC },
587 { "readwrite", RID_READWRITE, D_OBJC },
588 { "retain", RID_RETAIN, D_OBJC },
589 { "setter", RID_SETTER, D_OBJC },
592 const unsigned int num_c_common_reswords =
593 sizeof c_common_reswords / sizeof (struct c_common_resword);
595 /* Table of machine-independent attributes common to all C-like languages. */
596 const struct attribute_spec c_common_attribute_table[] =
598 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
599 affects_type_identity } */
600 { "packed", 0, 0, false, false, false,
601 handle_packed_attribute , false},
602 { "nocommon", 0, 0, true, false, false,
603 handle_nocommon_attribute, false},
604 { "common", 0, 0, true, false, false,
605 handle_common_attribute, false },
606 /* FIXME: logically, noreturn attributes should be listed as
607 "false, true, true" and apply to function types. But implementing this
608 would require all the places in the compiler that use TREE_THIS_VOLATILE
609 on a decl to identify non-returning functions to be located and fixed
610 to check the function type instead. */
611 { "noreturn", 0, 0, true, false, false,
612 handle_noreturn_attribute, false },
613 { "volatile", 0, 0, true, false, false,
614 handle_noreturn_attribute, false },
615 { "noinline", 0, 0, true, false, false,
616 handle_noinline_attribute, false },
617 { "noclone", 0, 0, true, false, false,
618 handle_noclone_attribute, false },
619 { "leaf", 0, 0, true, false, false,
620 handle_leaf_attribute, false },
621 { "always_inline", 0, 0, true, false, false,
622 handle_always_inline_attribute, false },
623 { "gnu_inline", 0, 0, true, false, false,
624 handle_gnu_inline_attribute, false },
625 { "artificial", 0, 0, true, false, false,
626 handle_artificial_attribute, false },
627 { "flatten", 0, 0, true, false, false,
628 handle_flatten_attribute, false },
629 { "used", 0, 0, true, false, false,
630 handle_used_attribute, false },
631 { "unused", 0, 0, false, false, false,
632 handle_unused_attribute, false },
633 { "externally_visible", 0, 0, true, false, false,
634 handle_externally_visible_attribute, false },
635 /* The same comments as for noreturn attributes apply to const ones. */
636 { "const", 0, 0, true, false, false,
637 handle_const_attribute, false },
638 { "transparent_union", 0, 0, false, false, false,
639 handle_transparent_union_attribute, false },
640 { "constructor", 0, 1, true, false, false,
641 handle_constructor_attribute, false },
642 { "destructor", 0, 1, true, false, false,
643 handle_destructor_attribute, false },
644 { "mode", 1, 1, false, true, false,
645 handle_mode_attribute, false },
646 { "section", 1, 1, true, false, false,
647 handle_section_attribute, false },
648 { "aligned", 0, 1, false, false, false,
649 handle_aligned_attribute, false },
650 { "weak", 0, 0, true, false, false,
651 handle_weak_attribute, false },
652 { "ifunc", 1, 1, true, false, false,
653 handle_ifunc_attribute, false },
654 { "alias", 1, 1, true, false, false,
655 handle_alias_attribute, false },
656 { "weakref", 0, 1, true, false, false,
657 handle_weakref_attribute, false },
658 { "no_instrument_function", 0, 0, true, false, false,
659 handle_no_instrument_function_attribute,
660 false },
661 { "malloc", 0, 0, true, false, false,
662 handle_malloc_attribute, false },
663 { "returns_twice", 0, 0, true, false, false,
664 handle_returns_twice_attribute, false },
665 { "no_stack_limit", 0, 0, true, false, false,
666 handle_no_limit_stack_attribute, false },
667 { "pure", 0, 0, true, false, false,
668 handle_pure_attribute, false },
669 /* For internal use (marking of builtins) only. The name contains space
670 to prevent its usage in source code. */
671 { "no vops", 0, 0, true, false, false,
672 handle_novops_attribute, false },
673 { "deprecated", 0, 1, false, false, false,
674 handle_deprecated_attribute, false },
675 { "vector_size", 1, 1, false, true, false,
676 handle_vector_size_attribute, false },
677 { "visibility", 1, 1, false, false, false,
678 handle_visibility_attribute, false },
679 { "tls_model", 1, 1, true, false, false,
680 handle_tls_model_attribute, false },
681 { "nonnull", 0, -1, false, true, true,
682 handle_nonnull_attribute, false },
683 { "nothrow", 0, 0, true, false, false,
684 handle_nothrow_attribute, false },
685 { "may_alias", 0, 0, false, true, false, NULL, false },
686 { "cleanup", 1, 1, true, false, false,
687 handle_cleanup_attribute, false },
688 { "warn_unused_result", 0, 0, false, true, true,
689 handle_warn_unused_result_attribute, false },
690 { "sentinel", 0, 1, false, true, true,
691 handle_sentinel_attribute, false },
692 /* For internal use (marking of builtins) only. The name contains space
693 to prevent its usage in source code. */
694 { "type generic", 0, 0, false, true, true,
695 handle_type_generic_attribute, false },
696 { "alloc_size", 1, 2, false, true, true,
697 handle_alloc_size_attribute, false },
698 { "cold", 0, 0, true, false, false,
699 handle_cold_attribute, false },
700 { "hot", 0, 0, true, false, false,
701 handle_hot_attribute, false },
702 { "warning", 1, 1, true, false, false,
703 handle_error_attribute, false },
704 { "error", 1, 1, true, false, false,
705 handle_error_attribute, false },
706 { "target", 1, -1, true, false, false,
707 handle_target_attribute, false },
708 { "optimize", 1, -1, true, false, false,
709 handle_optimize_attribute, false },
710 { "no_split_stack", 0, 0, true, false, false,
711 handle_no_split_stack_attribute, false },
712 /* For internal use (marking of builtins and runtime functions) only.
713 The name contains space to prevent its usage in source code. */
714 { "fn spec", 1, 1, false, true, true,
715 handle_fnspec_attribute, false },
716 { NULL, 0, 0, false, false, false, NULL, false }
719 /* Give the specifications for the format attributes, used by C and all
720 descendants. */
722 const struct attribute_spec c_common_format_attribute_table[] =
724 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
725 affects_type_identity } */
726 { "format", 3, 3, false, true, true,
727 handle_format_attribute, false },
728 { "format_arg", 1, 1, false, true, true,
729 handle_format_arg_attribute, false },
730 { NULL, 0, 0, false, false, false, NULL, false }
733 /* Return identifier for address space AS. */
735 const char *
736 c_addr_space_name (addr_space_t as)
738 int rid = RID_FIRST_ADDR_SPACE + as;
739 gcc_assert (ridpointers [rid]);
740 return IDENTIFIER_POINTER (ridpointers [rid]);
743 /* Push current bindings for the function name VAR_DECLS. */
745 void
746 start_fname_decls (void)
748 unsigned ix;
749 tree saved = NULL_TREE;
751 for (ix = 0; fname_vars[ix].decl; ix++)
753 tree decl = *fname_vars[ix].decl;
755 if (decl)
757 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
758 saved);
759 *fname_vars[ix].decl = NULL_TREE;
762 if (saved || saved_function_name_decls)
763 /* Normally they'll have been NULL, so only push if we've got a
764 stack, or they are non-NULL. */
765 saved_function_name_decls = tree_cons (saved, NULL_TREE,
766 saved_function_name_decls);
769 /* Finish up the current bindings, adding them into the current function's
770 statement tree. This must be done _before_ finish_stmt_tree is called.
771 If there is no current function, we must be at file scope and no statements
772 are involved. Pop the previous bindings. */
774 void
775 finish_fname_decls (void)
777 unsigned ix;
778 tree stmts = NULL_TREE;
779 tree stack = saved_function_name_decls;
781 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
782 append_to_statement_list (TREE_VALUE (stack), &stmts);
784 if (stmts)
786 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
788 if (TREE_CODE (*bodyp) == BIND_EXPR)
789 bodyp = &BIND_EXPR_BODY (*bodyp);
791 append_to_statement_list_force (*bodyp, &stmts);
792 *bodyp = stmts;
795 for (ix = 0; fname_vars[ix].decl; ix++)
796 *fname_vars[ix].decl = NULL_TREE;
798 if (stack)
800 /* We had saved values, restore them. */
801 tree saved;
803 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
805 tree decl = TREE_PURPOSE (saved);
806 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
808 *fname_vars[ix].decl = decl;
810 stack = TREE_CHAIN (stack);
812 saved_function_name_decls = stack;
815 /* Return the text name of the current function, suitably prettified
816 by PRETTY_P. Return string must be freed by caller. */
818 const char *
819 fname_as_string (int pretty_p)
821 const char *name = "top level";
822 char *namep;
823 int vrb = 2, len;
824 cpp_string cstr = { 0, 0 }, strname;
826 if (!pretty_p)
828 name = "";
829 vrb = 0;
832 if (current_function_decl)
833 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
835 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
837 namep = XNEWVEC (char, len);
838 snprintf (namep, len, "\"%s\"", name);
839 strname.text = (unsigned char *) namep;
840 strname.len = len - 1;
842 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
844 XDELETEVEC (namep);
845 return (const char *) cstr.text;
848 return namep;
851 /* Return the VAR_DECL for a const char array naming the current
852 function. If the VAR_DECL has not yet been created, create it
853 now. RID indicates how it should be formatted and IDENTIFIER_NODE
854 ID is its name (unfortunately C and C++ hold the RID values of
855 keywords in different places, so we can't derive RID from ID in
856 this language independent code. LOC is the location of the
857 function. */
859 tree
860 fname_decl (location_t loc, unsigned int rid, tree id)
862 unsigned ix;
863 tree decl = NULL_TREE;
865 for (ix = 0; fname_vars[ix].decl; ix++)
866 if (fname_vars[ix].rid == rid)
867 break;
869 decl = *fname_vars[ix].decl;
870 if (!decl)
872 /* If a tree is built here, it would normally have the lineno of
873 the current statement. Later this tree will be moved to the
874 beginning of the function and this line number will be wrong.
875 To avoid this problem set the lineno to 0 here; that prevents
876 it from appearing in the RTL. */
877 tree stmts;
878 location_t saved_location = input_location;
879 input_location = UNKNOWN_LOCATION;
881 stmts = push_stmt_list ();
882 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
883 stmts = pop_stmt_list (stmts);
884 if (!IS_EMPTY_STMT (stmts))
885 saved_function_name_decls
886 = tree_cons (decl, stmts, saved_function_name_decls);
887 *fname_vars[ix].decl = decl;
888 input_location = saved_location;
890 if (!ix && !current_function_decl)
891 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
893 return decl;
896 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
898 tree
899 fix_string_type (tree value)
901 int length = TREE_STRING_LENGTH (value);
902 int nchars;
903 tree e_type, i_type, a_type;
905 /* Compute the number of elements, for the array type. */
906 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
908 nchars = length;
909 e_type = char_type_node;
911 else if (TREE_TYPE (value) == char16_array_type_node)
913 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
914 e_type = char16_type_node;
916 else if (TREE_TYPE (value) == char32_array_type_node)
918 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
919 e_type = char32_type_node;
921 else
923 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
924 e_type = wchar_type_node;
927 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
928 limit in C++98 Annex B is very large (65536) and is not normative,
929 so we do not diagnose it (warn_overlength_strings is forced off
930 in c_common_post_options). */
931 if (warn_overlength_strings)
933 const int nchars_max = flag_isoc99 ? 4095 : 509;
934 const int relevant_std = flag_isoc99 ? 99 : 90;
935 if (nchars - 1 > nchars_max)
936 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
937 separate the %d from the 'C'. 'ISO' should not be
938 translated, but it may be moved after 'C%d' in languages
939 where modifiers follow nouns. */
940 pedwarn (input_location, OPT_Woverlength_strings,
941 "string length %qd is greater than the length %qd "
942 "ISO C%d compilers are required to support",
943 nchars - 1, nchars_max, relevant_std);
946 /* Create the array type for the string constant. The ISO C++
947 standard says that a string literal has type `const char[N]' or
948 `const wchar_t[N]'. We use the same logic when invoked as a C
949 front-end with -Wwrite-strings.
950 ??? We should change the type of an expression depending on the
951 state of a warning flag. We should just be warning -- see how
952 this is handled in the C++ front-end for the deprecated implicit
953 conversion from string literals to `char*' or `wchar_t*'.
955 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
956 array type being the unqualified version of that type.
957 Therefore, if we are constructing an array of const char, we must
958 construct the matching unqualified array type first. The C front
959 end does not require this, but it does no harm, so we do it
960 unconditionally. */
961 i_type = build_index_type (size_int (nchars - 1));
962 a_type = build_array_type (e_type, i_type);
963 if (c_dialect_cxx() || warn_write_strings)
964 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
966 TREE_TYPE (value) = a_type;
967 TREE_CONSTANT (value) = 1;
968 TREE_READONLY (value) = 1;
969 TREE_STATIC (value) = 1;
970 return value;
973 /* If DISABLE is true, stop issuing warnings. This is used when
974 parsing code that we know will not be executed. This function may
975 be called multiple times, and works as a stack. */
977 static void
978 c_disable_warnings (bool disable)
980 if (disable)
982 ++c_inhibit_evaluation_warnings;
983 fold_defer_overflow_warnings ();
987 /* If ENABLE is true, reenable issuing warnings. */
989 static void
990 c_enable_warnings (bool enable)
992 if (enable)
994 --c_inhibit_evaluation_warnings;
995 fold_undefer_and_ignore_overflow_warnings ();
999 /* Fully fold EXPR, an expression that was not folded (beyond integer
1000 constant expressions and null pointer constants) when being built
1001 up. If IN_INIT, this is in a static initializer and certain
1002 changes are made to the folding done. Clear *MAYBE_CONST if
1003 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1004 expression because it contains an evaluated operator (in C99) or an
1005 operator outside of sizeof returning an integer constant (in C90)
1006 not permitted in constant expressions, or because it contains an
1007 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1008 set to true by callers before calling this function.) Return the
1009 folded expression. Function arguments have already been folded
1010 before calling this function, as have the contents of SAVE_EXPR,
1011 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1012 C_MAYBE_CONST_EXPR. */
1014 tree
1015 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1017 tree ret;
1018 tree eptype = NULL_TREE;
1019 bool dummy = true;
1020 bool maybe_const_itself = true;
1021 location_t loc = EXPR_LOCATION (expr);
1023 /* This function is not relevant to C++ because C++ folds while
1024 parsing, and may need changes to be correct for C++ when C++
1025 stops folding while parsing. */
1026 if (c_dialect_cxx ())
1027 gcc_unreachable ();
1029 if (!maybe_const)
1030 maybe_const = &dummy;
1031 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1033 eptype = TREE_TYPE (expr);
1034 expr = TREE_OPERAND (expr, 0);
1036 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1037 &maybe_const_itself);
1038 if (eptype)
1039 ret = fold_convert_loc (loc, eptype, ret);
1040 *maybe_const &= maybe_const_itself;
1041 return ret;
1044 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1045 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1046 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1047 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1048 both evaluated and unevaluated subexpressions while
1049 *MAYBE_CONST_ITSELF is carried from only evaluated
1050 subexpressions). */
1052 static tree
1053 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1054 bool *maybe_const_itself)
1056 tree ret = expr;
1057 enum tree_code code = TREE_CODE (expr);
1058 enum tree_code_class kind = TREE_CODE_CLASS (code);
1059 location_t loc = EXPR_LOCATION (expr);
1060 tree op0, op1, op2, op3;
1061 tree orig_op0, orig_op1, orig_op2;
1062 bool op0_const = true, op1_const = true, op2_const = true;
1063 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1064 bool nowarning = TREE_NO_WARNING (expr);
1065 bool unused_p;
1067 /* This function is not relevant to C++ because C++ folds while
1068 parsing, and may need changes to be correct for C++ when C++
1069 stops folding while parsing. */
1070 if (c_dialect_cxx ())
1071 gcc_unreachable ();
1073 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1074 anything else not counted as an expression cannot usefully be
1075 folded further at this point. */
1076 if (!IS_EXPR_CODE_CLASS (kind)
1077 || kind == tcc_statement
1078 || code == SAVE_EXPR)
1079 return expr;
1081 /* Operands of variable-length expressions (function calls) have
1082 already been folded, as have __builtin_* function calls, and such
1083 expressions cannot occur in constant expressions. */
1084 if (kind == tcc_vl_exp)
1086 *maybe_const_operands = false;
1087 ret = fold (expr);
1088 goto out;
1091 if (code == C_MAYBE_CONST_EXPR)
1093 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1094 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1095 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1096 *maybe_const_operands = false;
1097 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1098 *maybe_const_itself = false;
1099 if (pre && !in_init)
1100 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1101 else
1102 ret = inner;
1103 goto out;
1106 /* Assignment, increment, decrement, function call and comma
1107 operators, and statement expressions, cannot occur in constant
1108 expressions if evaluated / outside of sizeof. (Function calls
1109 were handled above, though VA_ARG_EXPR is treated like a function
1110 call here, and statement expressions are handled through
1111 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1112 switch (code)
1114 case MODIFY_EXPR:
1115 case PREDECREMENT_EXPR:
1116 case PREINCREMENT_EXPR:
1117 case POSTDECREMENT_EXPR:
1118 case POSTINCREMENT_EXPR:
1119 case COMPOUND_EXPR:
1120 *maybe_const_operands = false;
1121 break;
1123 case VA_ARG_EXPR:
1124 case TARGET_EXPR:
1125 case BIND_EXPR:
1126 case OBJ_TYPE_REF:
1127 *maybe_const_operands = false;
1128 ret = fold (expr);
1129 goto out;
1131 default:
1132 break;
1135 /* Fold individual tree codes as appropriate. */
1136 switch (code)
1138 case COMPOUND_LITERAL_EXPR:
1139 /* Any non-constancy will have been marked in a containing
1140 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1141 goto out;
1143 case COMPONENT_REF:
1144 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1145 op1 = TREE_OPERAND (expr, 1);
1146 op2 = TREE_OPERAND (expr, 2);
1147 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1148 maybe_const_itself);
1149 STRIP_TYPE_NOPS (op0);
1150 if (op0 != orig_op0)
1151 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1152 if (ret != expr)
1154 TREE_READONLY (ret) = TREE_READONLY (expr);
1155 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1157 goto out;
1159 case ARRAY_REF:
1160 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1161 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1162 op2 = TREE_OPERAND (expr, 2);
1163 op3 = TREE_OPERAND (expr, 3);
1164 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1165 maybe_const_itself);
1166 STRIP_TYPE_NOPS (op0);
1167 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1168 maybe_const_itself);
1169 STRIP_TYPE_NOPS (op1);
1170 op1 = decl_constant_value_for_optimization (op1);
1171 if (op0 != orig_op0 || op1 != orig_op1)
1172 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1173 if (ret != expr)
1175 TREE_READONLY (ret) = TREE_READONLY (expr);
1176 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1177 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1179 ret = fold (ret);
1180 goto out;
1182 case COMPOUND_EXPR:
1183 case MODIFY_EXPR:
1184 case PREDECREMENT_EXPR:
1185 case PREINCREMENT_EXPR:
1186 case POSTDECREMENT_EXPR:
1187 case POSTINCREMENT_EXPR:
1188 case PLUS_EXPR:
1189 case MINUS_EXPR:
1190 case MULT_EXPR:
1191 case POINTER_PLUS_EXPR:
1192 case TRUNC_DIV_EXPR:
1193 case CEIL_DIV_EXPR:
1194 case FLOOR_DIV_EXPR:
1195 case TRUNC_MOD_EXPR:
1196 case RDIV_EXPR:
1197 case EXACT_DIV_EXPR:
1198 case LSHIFT_EXPR:
1199 case RSHIFT_EXPR:
1200 case BIT_IOR_EXPR:
1201 case BIT_XOR_EXPR:
1202 case BIT_AND_EXPR:
1203 case LT_EXPR:
1204 case LE_EXPR:
1205 case GT_EXPR:
1206 case GE_EXPR:
1207 case EQ_EXPR:
1208 case NE_EXPR:
1209 case COMPLEX_EXPR:
1210 case TRUTH_AND_EXPR:
1211 case TRUTH_OR_EXPR:
1212 case TRUTH_XOR_EXPR:
1213 case UNORDERED_EXPR:
1214 case ORDERED_EXPR:
1215 case UNLT_EXPR:
1216 case UNLE_EXPR:
1217 case UNGT_EXPR:
1218 case UNGE_EXPR:
1219 case UNEQ_EXPR:
1220 /* Binary operations evaluating both arguments (increment and
1221 decrement are binary internally in GCC). */
1222 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1223 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1224 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1225 maybe_const_itself);
1226 STRIP_TYPE_NOPS (op0);
1227 if (code != MODIFY_EXPR
1228 && code != PREDECREMENT_EXPR
1229 && code != PREINCREMENT_EXPR
1230 && code != POSTDECREMENT_EXPR
1231 && code != POSTINCREMENT_EXPR)
1232 op0 = decl_constant_value_for_optimization (op0);
1233 /* The RHS of a MODIFY_EXPR was fully folded when building that
1234 expression for the sake of conversion warnings. */
1235 if (code != MODIFY_EXPR)
1236 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1237 maybe_const_itself);
1238 STRIP_TYPE_NOPS (op1);
1239 op1 = decl_constant_value_for_optimization (op1);
1240 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1241 ret = in_init
1242 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1243 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1244 else
1245 ret = fold (expr);
1246 if (TREE_OVERFLOW_P (ret)
1247 && !TREE_OVERFLOW_P (op0)
1248 && !TREE_OVERFLOW_P (op1))
1249 overflow_warning (EXPR_LOCATION (expr), ret);
1250 goto out;
1252 case INDIRECT_REF:
1253 case FIX_TRUNC_EXPR:
1254 case FLOAT_EXPR:
1255 CASE_CONVERT:
1256 case VIEW_CONVERT_EXPR:
1257 case NON_LVALUE_EXPR:
1258 case NEGATE_EXPR:
1259 case BIT_NOT_EXPR:
1260 case TRUTH_NOT_EXPR:
1261 case ADDR_EXPR:
1262 case CONJ_EXPR:
1263 case REALPART_EXPR:
1264 case IMAGPART_EXPR:
1265 /* Unary operations. */
1266 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1267 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1268 maybe_const_itself);
1269 STRIP_TYPE_NOPS (op0);
1270 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1271 op0 = decl_constant_value_for_optimization (op0);
1272 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1273 not prepared to deal with them if they occur in initializers. */
1274 if (op0 != orig_op0
1275 && code == ADDR_EXPR
1276 && (op1 = get_base_address (op0)) != NULL_TREE
1277 && TREE_CODE (op1) == INDIRECT_REF
1278 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1279 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1280 else if (op0 != orig_op0 || in_init)
1281 ret = in_init
1282 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1283 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1284 else
1285 ret = fold (expr);
1286 if (code == INDIRECT_REF
1287 && ret != expr
1288 && TREE_CODE (ret) == INDIRECT_REF)
1290 TREE_READONLY (ret) = TREE_READONLY (expr);
1291 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1292 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1294 switch (code)
1296 case FIX_TRUNC_EXPR:
1297 case FLOAT_EXPR:
1298 CASE_CONVERT:
1299 /* Don't warn about explicit conversions. We will already
1300 have warned about suspect implicit conversions. */
1301 break;
1303 default:
1304 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1305 overflow_warning (EXPR_LOCATION (expr), ret);
1306 break;
1308 goto out;
1310 case TRUTH_ANDIF_EXPR:
1311 case TRUTH_ORIF_EXPR:
1312 /* Binary operations not necessarily evaluating both
1313 arguments. */
1314 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1315 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1316 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1317 STRIP_TYPE_NOPS (op0);
1319 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1320 ? truthvalue_false_node
1321 : truthvalue_true_node));
1322 c_disable_warnings (unused_p);
1323 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1324 STRIP_TYPE_NOPS (op1);
1325 c_enable_warnings (unused_p);
1327 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1328 ret = in_init
1329 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1330 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1331 else
1332 ret = fold (expr);
1333 *maybe_const_operands &= op0_const;
1334 *maybe_const_itself &= op0_const_self;
1335 if (!(flag_isoc99
1336 && op0_const
1337 && op0_const_self
1338 && (code == TRUTH_ANDIF_EXPR
1339 ? op0 == truthvalue_false_node
1340 : op0 == truthvalue_true_node)))
1341 *maybe_const_operands &= op1_const;
1342 if (!(op0_const
1343 && op0_const_self
1344 && (code == TRUTH_ANDIF_EXPR
1345 ? op0 == truthvalue_false_node
1346 : op0 == truthvalue_true_node)))
1347 *maybe_const_itself &= op1_const_self;
1348 goto out;
1350 case COND_EXPR:
1351 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1352 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1353 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1354 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1356 STRIP_TYPE_NOPS (op0);
1357 c_disable_warnings (op0 == truthvalue_false_node);
1358 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1359 STRIP_TYPE_NOPS (op1);
1360 c_enable_warnings (op0 == truthvalue_false_node);
1362 c_disable_warnings (op0 == truthvalue_true_node);
1363 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1364 STRIP_TYPE_NOPS (op2);
1365 c_enable_warnings (op0 == truthvalue_true_node);
1367 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1368 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1369 else
1370 ret = fold (expr);
1371 *maybe_const_operands &= op0_const;
1372 *maybe_const_itself &= op0_const_self;
1373 if (!(flag_isoc99
1374 && op0_const
1375 && op0_const_self
1376 && op0 == truthvalue_false_node))
1377 *maybe_const_operands &= op1_const;
1378 if (!(op0_const
1379 && op0_const_self
1380 && op0 == truthvalue_false_node))
1381 *maybe_const_itself &= op1_const_self;
1382 if (!(flag_isoc99
1383 && op0_const
1384 && op0_const_self
1385 && op0 == truthvalue_true_node))
1386 *maybe_const_operands &= op2_const;
1387 if (!(op0_const
1388 && op0_const_self
1389 && op0 == truthvalue_true_node))
1390 *maybe_const_itself &= op2_const_self;
1391 goto out;
1393 case EXCESS_PRECISION_EXPR:
1394 /* Each case where an operand with excess precision may be
1395 encountered must remove the EXCESS_PRECISION_EXPR around
1396 inner operands and possibly put one around the whole
1397 expression or possibly convert to the semantic type (which
1398 c_fully_fold does); we cannot tell at this stage which is
1399 appropriate in any particular case. */
1400 gcc_unreachable ();
1402 default:
1403 /* Various codes may appear through folding built-in functions
1404 and their arguments. */
1405 goto out;
1408 out:
1409 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1410 have been done by this point, so remove them again. */
1411 nowarning |= TREE_NO_WARNING (ret);
1412 STRIP_TYPE_NOPS (ret);
1413 if (nowarning && !TREE_NO_WARNING (ret))
1415 if (!CAN_HAVE_LOCATION_P (ret))
1416 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1417 TREE_NO_WARNING (ret) = 1;
1419 if (ret != expr)
1420 protected_set_expr_location (ret, loc);
1421 return ret;
1424 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1425 return EXP. Otherwise, return either EXP or its known constant
1426 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1427 Is the BLKmode test appropriate? */
1429 tree
1430 decl_constant_value_for_optimization (tree exp)
1432 tree ret;
1434 /* This function is only used by C, for c_fully_fold and other
1435 optimization, and may not be correct for C++. */
1436 if (c_dialect_cxx ())
1437 gcc_unreachable ();
1439 if (!optimize
1440 || TREE_CODE (exp) != VAR_DECL
1441 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1442 || DECL_MODE (exp) == BLKmode)
1443 return exp;
1445 ret = decl_constant_value (exp);
1446 /* Avoid unwanted tree sharing between the initializer and current
1447 function's body where the tree can be modified e.g. by the
1448 gimplifier. */
1449 if (ret != exp && TREE_STATIC (exp))
1450 ret = unshare_expr (ret);
1451 return ret;
1454 /* Print a warning if a constant expression had overflow in folding.
1455 Invoke this function on every expression that the language
1456 requires to be a constant expression.
1457 Note the ANSI C standard says it is erroneous for a
1458 constant expression to overflow. */
1460 void
1461 constant_expression_warning (tree value)
1463 if (warn_overflow && pedantic
1464 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1465 || TREE_CODE (value) == FIXED_CST
1466 || TREE_CODE (value) == VECTOR_CST
1467 || TREE_CODE (value) == COMPLEX_CST)
1468 && TREE_OVERFLOW (value))
1469 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1472 /* The same as above but print an unconditional error. */
1473 void
1474 constant_expression_error (tree value)
1476 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1477 || TREE_CODE (value) == FIXED_CST
1478 || TREE_CODE (value) == VECTOR_CST
1479 || TREE_CODE (value) == COMPLEX_CST)
1480 && TREE_OVERFLOW (value))
1481 error ("overflow in constant expression");
1484 /* Print a warning if an expression had overflow in folding and its
1485 operands hadn't.
1487 Invoke this function on every expression that
1488 (1) appears in the source code, and
1489 (2) is a constant expression that overflowed, and
1490 (3) is not already checked by convert_and_check;
1491 however, do not invoke this function on operands of explicit casts
1492 or when the expression is the result of an operator and any operand
1493 already overflowed. */
1495 void
1496 overflow_warning (location_t loc, tree value)
1498 if (c_inhibit_evaluation_warnings != 0)
1499 return;
1501 switch (TREE_CODE (value))
1503 case INTEGER_CST:
1504 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1505 break;
1507 case REAL_CST:
1508 warning_at (loc, OPT_Woverflow,
1509 "floating point overflow in expression");
1510 break;
1512 case FIXED_CST:
1513 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1514 break;
1516 case VECTOR_CST:
1517 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1518 break;
1520 case COMPLEX_CST:
1521 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1522 warning_at (loc, OPT_Woverflow,
1523 "complex integer overflow in expression");
1524 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1525 warning_at (loc, OPT_Woverflow,
1526 "complex floating point overflow in expression");
1527 break;
1529 default:
1530 break;
1534 /* Warn about uses of logical || / && operator in a context where it
1535 is likely that the bitwise equivalent was intended by the
1536 programmer. We have seen an expression in which CODE is a binary
1537 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1538 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1539 void
1540 warn_logical_operator (location_t location, enum tree_code code, tree type,
1541 enum tree_code code_left, tree op_left,
1542 enum tree_code ARG_UNUSED (code_right), tree op_right)
1544 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1545 int in0_p, in1_p, in_p;
1546 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1547 bool strict_overflow_p = false;
1549 if (code != TRUTH_ANDIF_EXPR
1550 && code != TRUTH_AND_EXPR
1551 && code != TRUTH_ORIF_EXPR
1552 && code != TRUTH_OR_EXPR)
1553 return;
1555 /* Warn if &&/|| are being used in a context where it is
1556 likely that the bitwise equivalent was intended by the
1557 programmer. That is, an expression such as op && MASK
1558 where op should not be any boolean expression, nor a
1559 constant, and mask seems to be a non-boolean integer constant. */
1560 if (!truth_value_p (code_left)
1561 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1562 && !CONSTANT_CLASS_P (op_left)
1563 && !TREE_NO_WARNING (op_left)
1564 && TREE_CODE (op_right) == INTEGER_CST
1565 && !integer_zerop (op_right)
1566 && !integer_onep (op_right))
1568 if (or_op)
1569 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1570 " applied to non-boolean constant");
1571 else
1572 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1573 " applied to non-boolean constant");
1574 TREE_NO_WARNING (op_left) = true;
1575 return;
1578 /* We do not warn for constants because they are typical of macro
1579 expansions that test for features. */
1580 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1581 return;
1583 /* This warning only makes sense with logical operands. */
1584 if (!(truth_value_p (TREE_CODE (op_left))
1585 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1586 || !(truth_value_p (TREE_CODE (op_right))
1587 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1588 return;
1590 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1591 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1593 if (lhs && TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1594 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1596 if (rhs && TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1597 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1599 /* If this is an OR operation, invert both sides; we will invert
1600 again at the end. */
1601 if (or_op)
1602 in0_p = !in0_p, in1_p = !in1_p;
1604 /* If both expressions are the same, if we can merge the ranges, and we
1605 can build the range test, return it or it inverted. */
1606 if (lhs && rhs && operand_equal_p (lhs, rhs, 0)
1607 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1608 in1_p, low1, high1)
1609 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1610 type, lhs, in_p, low, high)))
1612 if (TREE_CODE (tem) != INTEGER_CST)
1613 return;
1615 if (or_op)
1616 warning_at (location, OPT_Wlogical_op,
1617 "logical %<or%> "
1618 "of collectively exhaustive tests is always true");
1619 else
1620 warning_at (location, OPT_Wlogical_op,
1621 "logical %<and%> "
1622 "of mutually exclusive tests is always false");
1627 /* Print a warning about casts that might indicate violation
1628 of strict aliasing rules if -Wstrict-aliasing is used and
1629 strict aliasing mode is in effect. OTYPE is the original
1630 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1632 bool
1633 strict_aliasing_warning (tree otype, tree type, tree expr)
1635 /* Strip pointer conversion chains and get to the correct original type. */
1636 STRIP_NOPS (expr);
1637 otype = TREE_TYPE (expr);
1639 if (!(flag_strict_aliasing
1640 && POINTER_TYPE_P (type)
1641 && POINTER_TYPE_P (otype)
1642 && !VOID_TYPE_P (TREE_TYPE (type)))
1643 /* If the type we are casting to is a ref-all pointer
1644 dereferencing it is always valid. */
1645 || TYPE_REF_CAN_ALIAS_ALL (type))
1646 return false;
1648 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1649 && (DECL_P (TREE_OPERAND (expr, 0))
1650 || handled_component_p (TREE_OPERAND (expr, 0))))
1652 /* Casting the address of an object to non void pointer. Warn
1653 if the cast breaks type based aliasing. */
1654 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1656 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1657 "might break strict-aliasing rules");
1658 return true;
1660 else
1662 /* warn_strict_aliasing >= 3. This includes the default (3).
1663 Only warn if the cast is dereferenced immediately. */
1664 alias_set_type set1 =
1665 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1666 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1668 if (set1 != set2 && set2 != 0
1669 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1671 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1672 "pointer will break strict-aliasing rules");
1673 return true;
1675 else if (warn_strict_aliasing == 2
1676 && !alias_sets_must_conflict_p (set1, set2))
1678 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1679 "pointer might break strict-aliasing rules");
1680 return true;
1684 else
1685 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1687 /* At this level, warn for any conversions, even if an address is
1688 not taken in the same statement. This will likely produce many
1689 false positives, but could be useful to pinpoint problems that
1690 are not revealed at higher levels. */
1691 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1692 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1693 if (!COMPLETE_TYPE_P (type)
1694 || !alias_sets_must_conflict_p (set1, set2))
1696 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1697 "pointer might break strict-aliasing rules");
1698 return true;
1702 return false;
1705 /* Warn for unlikely, improbable, or stupid DECL declarations
1706 of `main'. */
1708 void
1709 check_main_parameter_types (tree decl)
1711 function_args_iterator iter;
1712 tree type;
1713 int argct = 0;
1715 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1717 /* XXX void_type_node belies the abstraction. */
1718 if (type == void_type_node || type == error_mark_node )
1719 break;
1721 ++argct;
1722 switch (argct)
1724 case 1:
1725 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1726 pedwarn (input_location, OPT_Wmain,
1727 "first argument of %q+D should be %<int%>", decl);
1728 break;
1730 case 2:
1731 if (TREE_CODE (type) != POINTER_TYPE
1732 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1733 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1734 != char_type_node))
1735 pedwarn (input_location, OPT_Wmain,
1736 "second argument of %q+D should be %<char **%>", decl);
1737 break;
1739 case 3:
1740 if (TREE_CODE (type) != POINTER_TYPE
1741 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1742 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1743 != char_type_node))
1744 pedwarn (input_location, OPT_Wmain,
1745 "third argument of %q+D should probably be "
1746 "%<char **%>", decl);
1747 break;
1751 /* It is intentional that this message does not mention the third
1752 argument because it's only mentioned in an appendix of the
1753 standard. */
1754 if (argct > 0 && (argct < 2 || argct > 3))
1755 pedwarn (input_location, OPT_Wmain,
1756 "%q+D takes only zero or two arguments", decl);
1759 /* True if pointers to distinct types T1 and T2 can be converted to
1760 each other without an explicit cast. Only returns true for opaque
1761 vector types. */
1762 bool
1763 vector_targets_convertible_p (const_tree t1, const_tree t2)
1765 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
1766 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1767 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1768 return true;
1770 return false;
1773 /* True if vector types T1 and T2 can be converted to each other
1774 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1775 can only be converted with -flax-vector-conversions yet that is not
1776 in effect, emit a note telling the user about that option if such
1777 a note has not previously been emitted. */
1778 bool
1779 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1781 static bool emitted_lax_note = false;
1782 bool convertible_lax;
1784 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1785 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1786 return true;
1788 convertible_lax =
1789 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1790 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
1791 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1792 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1793 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1795 if (!convertible_lax || flag_lax_vector_conversions)
1796 return convertible_lax;
1798 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1799 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1800 return true;
1802 if (emit_lax_note && !emitted_lax_note)
1804 emitted_lax_note = true;
1805 inform (input_location, "use -flax-vector-conversions to permit "
1806 "conversions between vectors with differing "
1807 "element types or numbers of subparts");
1810 return false;
1813 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1814 to integral type. */
1816 static tree
1817 c_common_get_narrower (tree op, int *unsignedp_ptr)
1819 op = get_narrower (op, unsignedp_ptr);
1821 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1822 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1824 /* C++0x scoped enumerations don't implicitly convert to integral
1825 type; if we stripped an explicit conversion to a larger type we
1826 need to replace it so common_type will still work. */
1827 tree type = (lang_hooks.types.type_for_size
1828 (TYPE_PRECISION (TREE_TYPE (op)),
1829 TYPE_UNSIGNED (TREE_TYPE (op))));
1830 op = fold_convert (type, op);
1832 return op;
1835 /* This is a helper function of build_binary_op.
1837 For certain operations if both args were extended from the same
1838 smaller type, do the arithmetic in that type and then extend.
1840 BITWISE indicates a bitwise operation.
1841 For them, this optimization is safe only if
1842 both args are zero-extended or both are sign-extended.
1843 Otherwise, we might change the result.
1844 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1845 but calculated in (unsigned short) it would be (unsigned short)-1.
1847 tree
1848 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1850 int unsigned0, unsigned1;
1851 tree arg0, arg1;
1852 int uns;
1853 tree type;
1855 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1856 excessive narrowing when we call get_narrower below. For
1857 example, suppose that OP0 is of unsigned int extended
1858 from signed char and that RESULT_TYPE is long long int.
1859 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1860 like
1862 (long long int) (unsigned int) signed_char
1864 which get_narrower would narrow down to
1866 (unsigned int) signed char
1868 If we do not cast OP0 first, get_narrower would return
1869 signed_char, which is inconsistent with the case of the
1870 explicit cast. */
1871 op0 = convert (result_type, op0);
1872 op1 = convert (result_type, op1);
1874 arg0 = c_common_get_narrower (op0, &unsigned0);
1875 arg1 = c_common_get_narrower (op1, &unsigned1);
1877 /* UNS is 1 if the operation to be done is an unsigned one. */
1878 uns = TYPE_UNSIGNED (result_type);
1880 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1881 but it *requires* conversion to FINAL_TYPE. */
1883 if ((TYPE_PRECISION (TREE_TYPE (op0))
1884 == TYPE_PRECISION (TREE_TYPE (arg0)))
1885 && TREE_TYPE (op0) != result_type)
1886 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1887 if ((TYPE_PRECISION (TREE_TYPE (op1))
1888 == TYPE_PRECISION (TREE_TYPE (arg1)))
1889 && TREE_TYPE (op1) != result_type)
1890 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1892 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1894 /* For bitwise operations, signedness of nominal type
1895 does not matter. Consider only how operands were extended. */
1896 if (bitwise)
1897 uns = unsigned0;
1899 /* Note that in all three cases below we refrain from optimizing
1900 an unsigned operation on sign-extended args.
1901 That would not be valid. */
1903 /* Both args variable: if both extended in same way
1904 from same width, do it in that width.
1905 Do it unsigned if args were zero-extended. */
1906 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1907 < TYPE_PRECISION (result_type))
1908 && (TYPE_PRECISION (TREE_TYPE (arg1))
1909 == TYPE_PRECISION (TREE_TYPE (arg0)))
1910 && unsigned0 == unsigned1
1911 && (unsigned0 || !uns))
1912 return c_common_signed_or_unsigned_type
1913 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1915 else if (TREE_CODE (arg0) == INTEGER_CST
1916 && (unsigned1 || !uns)
1917 && (TYPE_PRECISION (TREE_TYPE (arg1))
1918 < TYPE_PRECISION (result_type))
1919 && (type
1920 = c_common_signed_or_unsigned_type (unsigned1,
1921 TREE_TYPE (arg1)))
1922 && !POINTER_TYPE_P (type)
1923 && int_fits_type_p (arg0, type))
1924 return type;
1926 else if (TREE_CODE (arg1) == INTEGER_CST
1927 && (unsigned0 || !uns)
1928 && (TYPE_PRECISION (TREE_TYPE (arg0))
1929 < TYPE_PRECISION (result_type))
1930 && (type
1931 = c_common_signed_or_unsigned_type (unsigned0,
1932 TREE_TYPE (arg0)))
1933 && !POINTER_TYPE_P (type)
1934 && int_fits_type_p (arg1, type))
1935 return type;
1937 return result_type;
1940 /* Checks if expression EXPR of real/integer type cannot be converted
1941 to the real/integer type TYPE. Function returns true when:
1942 * EXPR is a constant which cannot be exactly converted to TYPE
1943 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1944 for EXPR type and TYPE being both integers or both real.
1945 * EXPR is not a constant of real type and TYPE is an integer.
1946 * EXPR is not a constant of integer type which cannot be
1947 exactly converted to real type.
1948 Function allows conversions between types of different signedness and
1949 does not return true in that case. Function can produce signedness
1950 warnings if PRODUCE_WARNS is true. */
1951 bool
1952 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
1954 bool give_warning = false;
1955 tree expr_type = TREE_TYPE (expr);
1956 location_t loc = EXPR_LOC_OR_HERE (expr);
1958 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1960 /* Warn for real constant that is not an exact integer converted
1961 to integer type. */
1962 if (TREE_CODE (expr_type) == REAL_TYPE
1963 && TREE_CODE (type) == INTEGER_TYPE)
1965 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1966 give_warning = true;
1968 /* Warn for an integer constant that does not fit into integer type. */
1969 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1970 && TREE_CODE (type) == INTEGER_TYPE
1971 && !int_fits_type_p (expr, type))
1973 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1974 && tree_int_cst_sgn (expr) < 0)
1976 if (produce_warns)
1977 warning_at (loc, OPT_Wsign_conversion, "negative integer"
1978 " implicitly converted to unsigned type");
1980 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1982 if (produce_warns)
1983 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
1984 " constant value to negative integer");
1986 else
1987 give_warning = true;
1989 else if (TREE_CODE (type) == REAL_TYPE)
1991 /* Warn for an integer constant that does not fit into real type. */
1992 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1994 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1995 if (!exact_real_truncate (TYPE_MODE (type), &a))
1996 give_warning = true;
1998 /* Warn for a real constant that does not fit into a smaller
1999 real type. */
2000 else if (TREE_CODE (expr_type) == REAL_TYPE
2001 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2003 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2004 if (!exact_real_truncate (TYPE_MODE (type), &a))
2005 give_warning = true;
2009 else
2011 /* Warn for real types converted to integer types. */
2012 if (TREE_CODE (expr_type) == REAL_TYPE
2013 && TREE_CODE (type) == INTEGER_TYPE)
2014 give_warning = true;
2016 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2017 && TREE_CODE (type) == INTEGER_TYPE)
2019 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2020 expr = get_unwidened (expr, 0);
2021 expr_type = TREE_TYPE (expr);
2023 /* Don't warn for short y; short x = ((int)y & 0xff); */
2024 if (TREE_CODE (expr) == BIT_AND_EXPR
2025 || TREE_CODE (expr) == BIT_IOR_EXPR
2026 || TREE_CODE (expr) == BIT_XOR_EXPR)
2028 /* If both args were extended from a shortest type,
2029 use that type if that is safe. */
2030 expr_type = shorten_binary_op (expr_type,
2031 TREE_OPERAND (expr, 0),
2032 TREE_OPERAND (expr, 1),
2033 /* bitwise */1);
2035 if (TREE_CODE (expr) == BIT_AND_EXPR)
2037 tree op0 = TREE_OPERAND (expr, 0);
2038 tree op1 = TREE_OPERAND (expr, 1);
2039 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2040 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2042 /* If one of the operands is a non-negative constant
2043 that fits in the target type, then the type of the
2044 other operand does not matter. */
2045 if ((TREE_CODE (op0) == INTEGER_CST
2046 && int_fits_type_p (op0, c_common_signed_type (type))
2047 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2048 || (TREE_CODE (op1) == INTEGER_CST
2049 && int_fits_type_p (op1, c_common_signed_type (type))
2050 && int_fits_type_p (op1,
2051 c_common_unsigned_type (type))))
2052 return false;
2053 /* If constant is unsigned and fits in the target
2054 type, then the result will also fit. */
2055 else if ((TREE_CODE (op0) == INTEGER_CST
2056 && unsigned0
2057 && int_fits_type_p (op0, type))
2058 || (TREE_CODE (op1) == INTEGER_CST
2059 && unsigned1
2060 && int_fits_type_p (op1, type)))
2061 return false;
2064 /* Warn for integer types converted to smaller integer types. */
2065 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2066 give_warning = true;
2068 /* When they are the same width but different signedness,
2069 then the value may change. */
2070 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2071 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2072 /* Even when converted to a bigger type, if the type is
2073 unsigned but expr is signed, then negative values
2074 will be changed. */
2075 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2076 && produce_warns)
2077 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2078 "may change the sign of the result",
2079 type, expr_type);
2082 /* Warn for integer types converted to real types if and only if
2083 all the range of values of the integer type cannot be
2084 represented by the real type. */
2085 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2086 && TREE_CODE (type) == REAL_TYPE)
2088 tree type_low_bound, type_high_bound;
2089 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2091 /* Don't warn about char y = 0xff; float x = (int) y; */
2092 expr = get_unwidened (expr, 0);
2093 expr_type = TREE_TYPE (expr);
2095 type_low_bound = TYPE_MIN_VALUE (expr_type);
2096 type_high_bound = TYPE_MAX_VALUE (expr_type);
2097 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2098 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2100 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2101 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2102 give_warning = true;
2105 /* Warn for real types converted to smaller real types. */
2106 else if (TREE_CODE (expr_type) == REAL_TYPE
2107 && TREE_CODE (type) == REAL_TYPE
2108 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2109 give_warning = true;
2112 return give_warning;
2115 /* Warns if the conversion of EXPR to TYPE may alter a value.
2116 This is a helper function for warnings_for_convert_and_check. */
2118 static void
2119 conversion_warning (tree type, tree expr)
2121 tree expr_type = TREE_TYPE (expr);
2122 location_t loc = EXPR_LOC_OR_HERE (expr);
2124 if (!warn_conversion && !warn_sign_conversion)
2125 return;
2127 switch (TREE_CODE (expr))
2129 case EQ_EXPR:
2130 case NE_EXPR:
2131 case LE_EXPR:
2132 case GE_EXPR:
2133 case LT_EXPR:
2134 case GT_EXPR:
2135 case TRUTH_ANDIF_EXPR:
2136 case TRUTH_ORIF_EXPR:
2137 case TRUTH_AND_EXPR:
2138 case TRUTH_OR_EXPR:
2139 case TRUTH_XOR_EXPR:
2140 case TRUTH_NOT_EXPR:
2141 /* Conversion from boolean to a signed:1 bit-field (which only
2142 can hold the values 0 and -1) doesn't lose information - but
2143 it does change the value. */
2144 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2145 warning_at (loc, OPT_Wconversion,
2146 "conversion to %qT from boolean expression", type);
2147 return;
2149 case REAL_CST:
2150 case INTEGER_CST:
2151 if (unsafe_conversion_p (type, expr, true))
2152 warning_at (loc, OPT_Wconversion,
2153 "conversion to %qT alters %qT constant value",
2154 type, expr_type);
2155 return;
2157 case COND_EXPR:
2159 /* In case of COND_EXPR, if both operands are constants or
2160 COND_EXPR, then we do not care about the type of COND_EXPR,
2161 only about the conversion of each operand. */
2162 tree op1 = TREE_OPERAND (expr, 1);
2163 tree op2 = TREE_OPERAND (expr, 2);
2165 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2166 || TREE_CODE (op1) == COND_EXPR)
2167 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2168 || TREE_CODE (op2) == COND_EXPR))
2170 conversion_warning (type, op1);
2171 conversion_warning (type, op2);
2172 return;
2174 /* Fall through. */
2177 default: /* 'expr' is not a constant. */
2178 if (unsafe_conversion_p (type, expr, true))
2179 warning_at (loc, OPT_Wconversion,
2180 "conversion to %qT from %qT may alter its value",
2181 type, expr_type);
2185 /* Produce warnings after a conversion. RESULT is the result of
2186 converting EXPR to TYPE. This is a helper function for
2187 convert_and_check and cp_convert_and_check. */
2189 void
2190 warnings_for_convert_and_check (tree type, tree expr, tree result)
2192 if (TREE_CODE (expr) == INTEGER_CST
2193 && (TREE_CODE (type) == INTEGER_TYPE
2194 || TREE_CODE (type) == ENUMERAL_TYPE)
2195 && !int_fits_type_p (expr, type))
2197 /* Do not diagnose overflow in a constant expression merely
2198 because a conversion overflowed. */
2199 if (TREE_OVERFLOW (result))
2200 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2202 if (TYPE_UNSIGNED (type))
2204 /* This detects cases like converting -129 or 256 to
2205 unsigned char. */
2206 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2207 warning (OPT_Woverflow,
2208 "large integer implicitly truncated to unsigned type");
2209 else
2210 conversion_warning (type, expr);
2212 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2213 warning (OPT_Woverflow,
2214 "overflow in implicit constant conversion");
2215 /* No warning for converting 0x80000000 to int. */
2216 else if (pedantic
2217 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2218 || TYPE_PRECISION (TREE_TYPE (expr))
2219 != TYPE_PRECISION (type)))
2220 warning (OPT_Woverflow,
2221 "overflow in implicit constant conversion");
2223 else
2224 conversion_warning (type, expr);
2226 else if ((TREE_CODE (result) == INTEGER_CST
2227 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2228 warning (OPT_Woverflow,
2229 "overflow in implicit constant conversion");
2230 else
2231 conversion_warning (type, expr);
2235 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2236 Invoke this function on every expression that is converted implicitly,
2237 i.e. because of language rules and not because of an explicit cast. */
2239 tree
2240 convert_and_check (tree type, tree expr)
2242 tree result;
2243 tree expr_for_warning;
2245 /* Convert from a value with possible excess precision rather than
2246 via the semantic type, but do not warn about values not fitting
2247 exactly in the semantic type. */
2248 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2250 tree orig_type = TREE_TYPE (expr);
2251 expr = TREE_OPERAND (expr, 0);
2252 expr_for_warning = convert (orig_type, expr);
2253 if (orig_type == type)
2254 return expr_for_warning;
2256 else
2257 expr_for_warning = expr;
2259 if (TREE_TYPE (expr) == type)
2260 return expr;
2262 result = convert (type, expr);
2264 if (c_inhibit_evaluation_warnings == 0
2265 && !TREE_OVERFLOW_P (expr)
2266 && result != error_mark_node)
2267 warnings_for_convert_and_check (type, expr_for_warning, result);
2269 return result;
2272 /* A node in a list that describes references to variables (EXPR), which are
2273 either read accesses if WRITER is zero, or write accesses, in which case
2274 WRITER is the parent of EXPR. */
2275 struct tlist
2277 struct tlist *next;
2278 tree expr, writer;
2281 /* Used to implement a cache the results of a call to verify_tree. We only
2282 use this for SAVE_EXPRs. */
2283 struct tlist_cache
2285 struct tlist_cache *next;
2286 struct tlist *cache_before_sp;
2287 struct tlist *cache_after_sp;
2288 tree expr;
2291 /* Obstack to use when allocating tlist structures, and corresponding
2292 firstobj. */
2293 static struct obstack tlist_obstack;
2294 static char *tlist_firstobj = 0;
2296 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2297 warnings. */
2298 static struct tlist *warned_ids;
2299 /* SAVE_EXPRs need special treatment. We process them only once and then
2300 cache the results. */
2301 static struct tlist_cache *save_expr_cache;
2303 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2304 static void merge_tlist (struct tlist **, struct tlist *, int);
2305 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2306 static int warning_candidate_p (tree);
2307 static bool candidate_equal_p (const_tree, const_tree);
2308 static void warn_for_collisions (struct tlist *);
2309 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2310 static struct tlist *new_tlist (struct tlist *, tree, tree);
2312 /* Create a new struct tlist and fill in its fields. */
2313 static struct tlist *
2314 new_tlist (struct tlist *next, tree t, tree writer)
2316 struct tlist *l;
2317 l = XOBNEW (&tlist_obstack, struct tlist);
2318 l->next = next;
2319 l->expr = t;
2320 l->writer = writer;
2321 return l;
2324 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2325 is nonnull, we ignore any node we find which has a writer equal to it. */
2327 static void
2328 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2330 while (add)
2332 struct tlist *next = add->next;
2333 if (!copy)
2334 add->next = *to;
2335 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2336 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2337 add = next;
2341 /* Merge the nodes of ADD into TO. This merging process is done so that for
2342 each variable that already exists in TO, no new node is added; however if
2343 there is a write access recorded in ADD, and an occurrence on TO is only
2344 a read access, then the occurrence in TO will be modified to record the
2345 write. */
2347 static void
2348 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2350 struct tlist **end = to;
2352 while (*end)
2353 end = &(*end)->next;
2355 while (add)
2357 int found = 0;
2358 struct tlist *tmp2;
2359 struct tlist *next = add->next;
2361 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2362 if (candidate_equal_p (tmp2->expr, add->expr))
2364 found = 1;
2365 if (!tmp2->writer)
2366 tmp2->writer = add->writer;
2368 if (!found)
2370 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2371 end = &(*end)->next;
2372 *end = 0;
2374 add = next;
2378 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2379 references in list LIST conflict with it, excluding reads if ONLY writers
2380 is nonzero. */
2382 static void
2383 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2384 int only_writes)
2386 struct tlist *tmp;
2388 /* Avoid duplicate warnings. */
2389 for (tmp = warned_ids; tmp; tmp = tmp->next)
2390 if (candidate_equal_p (tmp->expr, written))
2391 return;
2393 while (list)
2395 if (candidate_equal_p (list->expr, written)
2396 && !candidate_equal_p (list->writer, writer)
2397 && (!only_writes || list->writer))
2399 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2400 warning_at (EXPR_LOC_OR_HERE (writer),
2401 OPT_Wsequence_point, "operation on %qE may be undefined",
2402 list->expr);
2404 list = list->next;
2408 /* Given a list LIST of references to variables, find whether any of these
2409 can cause conflicts due to missing sequence points. */
2411 static void
2412 warn_for_collisions (struct tlist *list)
2414 struct tlist *tmp;
2416 for (tmp = list; tmp; tmp = tmp->next)
2418 if (tmp->writer)
2419 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2423 /* Return nonzero if X is a tree that can be verified by the sequence point
2424 warnings. */
2425 static int
2426 warning_candidate_p (tree x)
2428 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2429 return 0;
2431 if (TREE_CODE (x) == BLOCK)
2432 return 0;
2434 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2435 (lvalue_p) crash on TRY/CATCH. */
2436 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2437 return 0;
2439 if (!lvalue_p (x))
2440 return 0;
2442 /* No point to track non-const calls, they will never satisfy
2443 operand_equal_p. */
2444 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2445 return 0;
2447 if (TREE_CODE (x) == STRING_CST)
2448 return 0;
2450 return 1;
2453 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2454 static bool
2455 candidate_equal_p (const_tree x, const_tree y)
2457 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2460 /* Walk the tree X, and record accesses to variables. If X is written by the
2461 parent tree, WRITER is the parent.
2462 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2463 expression or its only operand forces a sequence point, then everything up
2464 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2465 in PNO_SP.
2466 Once we return, we will have emitted warnings if any subexpression before
2467 such a sequence point could be undefined. On a higher level, however, the
2468 sequence point may not be relevant, and we'll merge the two lists.
2470 Example: (b++, a) + b;
2471 The call that processes the COMPOUND_EXPR will store the increment of B
2472 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2473 processes the PLUS_EXPR will need to merge the two lists so that
2474 eventually, all accesses end up on the same list (and we'll warn about the
2475 unordered subexpressions b++ and b.
2477 A note on merging. If we modify the former example so that our expression
2478 becomes
2479 (b++, b) + a
2480 care must be taken not simply to add all three expressions into the final
2481 PNO_SP list. The function merge_tlist takes care of that by merging the
2482 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2483 way, so that no more than one access to B is recorded. */
2485 static void
2486 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2487 tree writer)
2489 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2490 enum tree_code code;
2491 enum tree_code_class cl;
2493 /* X may be NULL if it is the operand of an empty statement expression
2494 ({ }). */
2495 if (x == NULL)
2496 return;
2498 restart:
2499 code = TREE_CODE (x);
2500 cl = TREE_CODE_CLASS (code);
2502 if (warning_candidate_p (x))
2503 *pno_sp = new_tlist (*pno_sp, x, writer);
2505 switch (code)
2507 case CONSTRUCTOR:
2508 return;
2510 case COMPOUND_EXPR:
2511 case TRUTH_ANDIF_EXPR:
2512 case TRUTH_ORIF_EXPR:
2513 tmp_before = tmp_nosp = tmp_list3 = 0;
2514 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2515 warn_for_collisions (tmp_nosp);
2516 merge_tlist (pbefore_sp, tmp_before, 0);
2517 merge_tlist (pbefore_sp, tmp_nosp, 0);
2518 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2519 merge_tlist (pbefore_sp, tmp_list3, 0);
2520 return;
2522 case COND_EXPR:
2523 tmp_before = tmp_list2 = 0;
2524 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2525 warn_for_collisions (tmp_list2);
2526 merge_tlist (pbefore_sp, tmp_before, 0);
2527 merge_tlist (pbefore_sp, tmp_list2, 1);
2529 tmp_list3 = tmp_nosp = 0;
2530 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2531 warn_for_collisions (tmp_nosp);
2532 merge_tlist (pbefore_sp, tmp_list3, 0);
2534 tmp_list3 = tmp_list2 = 0;
2535 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2536 warn_for_collisions (tmp_list2);
2537 merge_tlist (pbefore_sp, tmp_list3, 0);
2538 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2539 two first, to avoid warning for (a ? b++ : b++). */
2540 merge_tlist (&tmp_nosp, tmp_list2, 0);
2541 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2542 return;
2544 case PREDECREMENT_EXPR:
2545 case PREINCREMENT_EXPR:
2546 case POSTDECREMENT_EXPR:
2547 case POSTINCREMENT_EXPR:
2548 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2549 return;
2551 case MODIFY_EXPR:
2552 tmp_before = tmp_nosp = tmp_list3 = 0;
2553 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2554 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2555 /* Expressions inside the LHS are not ordered wrt. the sequence points
2556 in the RHS. Example:
2557 *a = (a++, 2)
2558 Despite the fact that the modification of "a" is in the before_sp
2559 list (tmp_before), it conflicts with the use of "a" in the LHS.
2560 We can handle this by adding the contents of tmp_list3
2561 to those of tmp_before, and redoing the collision warnings for that
2562 list. */
2563 add_tlist (&tmp_before, tmp_list3, x, 1);
2564 warn_for_collisions (tmp_before);
2565 /* Exclude the LHS itself here; we first have to merge it into the
2566 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2567 didn't exclude the LHS, we'd get it twice, once as a read and once
2568 as a write. */
2569 add_tlist (pno_sp, tmp_list3, x, 0);
2570 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2572 merge_tlist (pbefore_sp, tmp_before, 0);
2573 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2574 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2575 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2576 return;
2578 case CALL_EXPR:
2579 /* We need to warn about conflicts among arguments and conflicts between
2580 args and the function address. Side effects of the function address,
2581 however, are not ordered by the sequence point of the call. */
2583 call_expr_arg_iterator iter;
2584 tree arg;
2585 tmp_before = tmp_nosp = 0;
2586 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2587 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2589 tmp_list2 = tmp_list3 = 0;
2590 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2591 merge_tlist (&tmp_list3, tmp_list2, 0);
2592 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2594 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2595 warn_for_collisions (tmp_before);
2596 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2597 return;
2600 case TREE_LIST:
2601 /* Scan all the list, e.g. indices of multi dimensional array. */
2602 while (x)
2604 tmp_before = tmp_nosp = 0;
2605 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2606 merge_tlist (&tmp_nosp, tmp_before, 0);
2607 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2608 x = TREE_CHAIN (x);
2610 return;
2612 case SAVE_EXPR:
2614 struct tlist_cache *t;
2615 for (t = save_expr_cache; t; t = t->next)
2616 if (candidate_equal_p (t->expr, x))
2617 break;
2619 if (!t)
2621 t = XOBNEW (&tlist_obstack, struct tlist_cache);
2622 t->next = save_expr_cache;
2623 t->expr = x;
2624 save_expr_cache = t;
2626 tmp_before = tmp_nosp = 0;
2627 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2628 warn_for_collisions (tmp_nosp);
2630 tmp_list3 = 0;
2631 while (tmp_nosp)
2633 struct tlist *t = tmp_nosp;
2634 tmp_nosp = t->next;
2635 merge_tlist (&tmp_list3, t, 0);
2637 t->cache_before_sp = tmp_before;
2638 t->cache_after_sp = tmp_list3;
2640 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2641 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2642 return;
2645 case ADDR_EXPR:
2646 x = TREE_OPERAND (x, 0);
2647 if (DECL_P (x))
2648 return;
2649 writer = 0;
2650 goto restart;
2652 default:
2653 /* For other expressions, simply recurse on their operands.
2654 Manual tail recursion for unary expressions.
2655 Other non-expressions need not be processed. */
2656 if (cl == tcc_unary)
2658 x = TREE_OPERAND (x, 0);
2659 writer = 0;
2660 goto restart;
2662 else if (IS_EXPR_CODE_CLASS (cl))
2664 int lp;
2665 int max = TREE_OPERAND_LENGTH (x);
2666 for (lp = 0; lp < max; lp++)
2668 tmp_before = tmp_nosp = 0;
2669 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2670 merge_tlist (&tmp_nosp, tmp_before, 0);
2671 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2674 return;
2678 /* Try to warn for undefined behavior in EXPR due to missing sequence
2679 points. */
2681 DEBUG_FUNCTION void
2682 verify_sequence_points (tree expr)
2684 struct tlist *before_sp = 0, *after_sp = 0;
2686 warned_ids = 0;
2687 save_expr_cache = 0;
2688 if (tlist_firstobj == 0)
2690 gcc_obstack_init (&tlist_obstack);
2691 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2694 verify_tree (expr, &before_sp, &after_sp, 0);
2695 warn_for_collisions (after_sp);
2696 obstack_free (&tlist_obstack, tlist_firstobj);
2699 /* Validate the expression after `case' and apply default promotions. */
2701 static tree
2702 check_case_value (tree value)
2704 if (value == NULL_TREE)
2705 return value;
2707 if (TREE_CODE (value) == INTEGER_CST)
2708 /* Promote char or short to int. */
2709 value = perform_integral_promotions (value);
2710 else if (value != error_mark_node)
2712 error ("case label does not reduce to an integer constant");
2713 value = error_mark_node;
2716 constant_expression_warning (value);
2718 return value;
2721 /* See if the case values LOW and HIGH are in the range of the original
2722 type (i.e. before the default conversion to int) of the switch testing
2723 expression.
2724 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2725 the type before promoting it. CASE_LOW_P is a pointer to the lower
2726 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2727 if the case is not a case range.
2728 The caller has to make sure that we are not called with NULL for
2729 CASE_LOW_P (i.e. the default case).
2730 Returns true if the case label is in range of ORIG_TYPE (saturated or
2731 untouched) or false if the label is out of range. */
2733 static bool
2734 check_case_bounds (tree type, tree orig_type,
2735 tree *case_low_p, tree *case_high_p)
2737 tree min_value, max_value;
2738 tree case_low = *case_low_p;
2739 tree case_high = case_high_p ? *case_high_p : case_low;
2741 /* If there was a problem with the original type, do nothing. */
2742 if (orig_type == error_mark_node)
2743 return true;
2745 min_value = TYPE_MIN_VALUE (orig_type);
2746 max_value = TYPE_MAX_VALUE (orig_type);
2748 /* Case label is less than minimum for type. */
2749 if (tree_int_cst_compare (case_low, min_value) < 0
2750 && tree_int_cst_compare (case_high, min_value) < 0)
2752 warning (0, "case label value is less than minimum value for type");
2753 return false;
2756 /* Case value is greater than maximum for type. */
2757 if (tree_int_cst_compare (case_low, max_value) > 0
2758 && tree_int_cst_compare (case_high, max_value) > 0)
2760 warning (0, "case label value exceeds maximum value for type");
2761 return false;
2764 /* Saturate lower case label value to minimum. */
2765 if (tree_int_cst_compare (case_high, min_value) >= 0
2766 && tree_int_cst_compare (case_low, min_value) < 0)
2768 warning (0, "lower value in case label range"
2769 " less than minimum value for type");
2770 case_low = min_value;
2773 /* Saturate upper case label value to maximum. */
2774 if (tree_int_cst_compare (case_low, max_value) <= 0
2775 && tree_int_cst_compare (case_high, max_value) > 0)
2777 warning (0, "upper value in case label range"
2778 " exceeds maximum value for type");
2779 case_high = max_value;
2782 if (*case_low_p != case_low)
2783 *case_low_p = convert (type, case_low);
2784 if (case_high_p && *case_high_p != case_high)
2785 *case_high_p = convert (type, case_high);
2787 return true;
2790 /* Return an integer type with BITS bits of precision,
2791 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2793 tree
2794 c_common_type_for_size (unsigned int bits, int unsignedp)
2796 if (bits == TYPE_PRECISION (integer_type_node))
2797 return unsignedp ? unsigned_type_node : integer_type_node;
2799 if (bits == TYPE_PRECISION (signed_char_type_node))
2800 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2802 if (bits == TYPE_PRECISION (short_integer_type_node))
2803 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2805 if (bits == TYPE_PRECISION (long_integer_type_node))
2806 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2808 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2809 return (unsignedp ? long_long_unsigned_type_node
2810 : long_long_integer_type_node);
2812 if (int128_integer_type_node
2813 && bits == TYPE_PRECISION (int128_integer_type_node))
2814 return (unsignedp ? int128_unsigned_type_node
2815 : int128_integer_type_node);
2817 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2818 return (unsignedp ? widest_unsigned_literal_type_node
2819 : widest_integer_literal_type_node);
2821 if (bits <= TYPE_PRECISION (intQI_type_node))
2822 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2824 if (bits <= TYPE_PRECISION (intHI_type_node))
2825 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2827 if (bits <= TYPE_PRECISION (intSI_type_node))
2828 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2830 if (bits <= TYPE_PRECISION (intDI_type_node))
2831 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2833 return 0;
2836 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2837 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2838 and saturating if SATP is nonzero, otherwise not saturating. */
2840 tree
2841 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2842 int unsignedp, int satp)
2844 enum machine_mode mode;
2845 if (ibit == 0)
2846 mode = unsignedp ? UQQmode : QQmode;
2847 else
2848 mode = unsignedp ? UHAmode : HAmode;
2850 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2851 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2852 break;
2854 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2856 sorry ("GCC cannot support operators with integer types and "
2857 "fixed-point types that have too many integral and "
2858 "fractional bits together");
2859 return 0;
2862 return c_common_type_for_mode (mode, satp);
2865 /* Used for communication between c_common_type_for_mode and
2866 c_register_builtin_type. */
2867 static GTY(()) tree registered_builtin_types;
2869 /* Return a data type that has machine mode MODE.
2870 If the mode is an integer,
2871 then UNSIGNEDP selects between signed and unsigned types.
2872 If the mode is a fixed-point mode,
2873 then UNSIGNEDP selects between saturating and nonsaturating types. */
2875 tree
2876 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
2878 tree t;
2880 if (mode == TYPE_MODE (integer_type_node))
2881 return unsignedp ? unsigned_type_node : integer_type_node;
2883 if (mode == TYPE_MODE (signed_char_type_node))
2884 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2886 if (mode == TYPE_MODE (short_integer_type_node))
2887 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2889 if (mode == TYPE_MODE (long_integer_type_node))
2890 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2892 if (mode == TYPE_MODE (long_long_integer_type_node))
2893 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2895 if (int128_integer_type_node
2896 && mode == TYPE_MODE (int128_integer_type_node))
2897 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
2899 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2900 return unsignedp ? widest_unsigned_literal_type_node
2901 : widest_integer_literal_type_node;
2903 if (mode == QImode)
2904 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2906 if (mode == HImode)
2907 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2909 if (mode == SImode)
2910 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2912 if (mode == DImode)
2913 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2915 #if HOST_BITS_PER_WIDE_INT >= 64
2916 if (mode == TYPE_MODE (intTI_type_node))
2917 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2918 #endif
2920 if (mode == TYPE_MODE (float_type_node))
2921 return float_type_node;
2923 if (mode == TYPE_MODE (double_type_node))
2924 return double_type_node;
2926 if (mode == TYPE_MODE (long_double_type_node))
2927 return long_double_type_node;
2929 if (mode == TYPE_MODE (void_type_node))
2930 return void_type_node;
2932 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2933 return (unsignedp
2934 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2935 : make_signed_type (GET_MODE_PRECISION (mode)));
2937 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2938 return (unsignedp
2939 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2940 : make_signed_type (GET_MODE_PRECISION (mode)));
2942 if (COMPLEX_MODE_P (mode))
2944 enum machine_mode inner_mode;
2945 tree inner_type;
2947 if (mode == TYPE_MODE (complex_float_type_node))
2948 return complex_float_type_node;
2949 if (mode == TYPE_MODE (complex_double_type_node))
2950 return complex_double_type_node;
2951 if (mode == TYPE_MODE (complex_long_double_type_node))
2952 return complex_long_double_type_node;
2954 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2955 return complex_integer_type_node;
2957 inner_mode = GET_MODE_INNER (mode);
2958 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2959 if (inner_type != NULL_TREE)
2960 return build_complex_type (inner_type);
2962 else if (VECTOR_MODE_P (mode))
2964 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2965 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2966 if (inner_type != NULL_TREE)
2967 return build_vector_type_for_mode (inner_type, mode);
2970 if (mode == TYPE_MODE (dfloat32_type_node))
2971 return dfloat32_type_node;
2972 if (mode == TYPE_MODE (dfloat64_type_node))
2973 return dfloat64_type_node;
2974 if (mode == TYPE_MODE (dfloat128_type_node))
2975 return dfloat128_type_node;
2977 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2979 if (mode == TYPE_MODE (short_fract_type_node))
2980 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2981 if (mode == TYPE_MODE (fract_type_node))
2982 return unsignedp ? sat_fract_type_node : fract_type_node;
2983 if (mode == TYPE_MODE (long_fract_type_node))
2984 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2985 if (mode == TYPE_MODE (long_long_fract_type_node))
2986 return unsignedp ? sat_long_long_fract_type_node
2987 : long_long_fract_type_node;
2989 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2990 return unsignedp ? sat_unsigned_short_fract_type_node
2991 : unsigned_short_fract_type_node;
2992 if (mode == TYPE_MODE (unsigned_fract_type_node))
2993 return unsignedp ? sat_unsigned_fract_type_node
2994 : unsigned_fract_type_node;
2995 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2996 return unsignedp ? sat_unsigned_long_fract_type_node
2997 : unsigned_long_fract_type_node;
2998 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2999 return unsignedp ? sat_unsigned_long_long_fract_type_node
3000 : unsigned_long_long_fract_type_node;
3002 if (mode == TYPE_MODE (short_accum_type_node))
3003 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3004 if (mode == TYPE_MODE (accum_type_node))
3005 return unsignedp ? sat_accum_type_node : accum_type_node;
3006 if (mode == TYPE_MODE (long_accum_type_node))
3007 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3008 if (mode == TYPE_MODE (long_long_accum_type_node))
3009 return unsignedp ? sat_long_long_accum_type_node
3010 : long_long_accum_type_node;
3012 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3013 return unsignedp ? sat_unsigned_short_accum_type_node
3014 : unsigned_short_accum_type_node;
3015 if (mode == TYPE_MODE (unsigned_accum_type_node))
3016 return unsignedp ? sat_unsigned_accum_type_node
3017 : unsigned_accum_type_node;
3018 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3019 return unsignedp ? sat_unsigned_long_accum_type_node
3020 : unsigned_long_accum_type_node;
3021 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3022 return unsignedp ? sat_unsigned_long_long_accum_type_node
3023 : unsigned_long_long_accum_type_node;
3025 if (mode == QQmode)
3026 return unsignedp ? sat_qq_type_node : qq_type_node;
3027 if (mode == HQmode)
3028 return unsignedp ? sat_hq_type_node : hq_type_node;
3029 if (mode == SQmode)
3030 return unsignedp ? sat_sq_type_node : sq_type_node;
3031 if (mode == DQmode)
3032 return unsignedp ? sat_dq_type_node : dq_type_node;
3033 if (mode == TQmode)
3034 return unsignedp ? sat_tq_type_node : tq_type_node;
3036 if (mode == UQQmode)
3037 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3038 if (mode == UHQmode)
3039 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3040 if (mode == USQmode)
3041 return unsignedp ? sat_usq_type_node : usq_type_node;
3042 if (mode == UDQmode)
3043 return unsignedp ? sat_udq_type_node : udq_type_node;
3044 if (mode == UTQmode)
3045 return unsignedp ? sat_utq_type_node : utq_type_node;
3047 if (mode == HAmode)
3048 return unsignedp ? sat_ha_type_node : ha_type_node;
3049 if (mode == SAmode)
3050 return unsignedp ? sat_sa_type_node : sa_type_node;
3051 if (mode == DAmode)
3052 return unsignedp ? sat_da_type_node : da_type_node;
3053 if (mode == TAmode)
3054 return unsignedp ? sat_ta_type_node : ta_type_node;
3056 if (mode == UHAmode)
3057 return unsignedp ? sat_uha_type_node : uha_type_node;
3058 if (mode == USAmode)
3059 return unsignedp ? sat_usa_type_node : usa_type_node;
3060 if (mode == UDAmode)
3061 return unsignedp ? sat_uda_type_node : uda_type_node;
3062 if (mode == UTAmode)
3063 return unsignedp ? sat_uta_type_node : uta_type_node;
3066 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3067 if (TYPE_MODE (TREE_VALUE (t)) == mode)
3068 return TREE_VALUE (t);
3070 return 0;
3073 tree
3074 c_common_unsigned_type (tree type)
3076 return c_common_signed_or_unsigned_type (1, type);
3079 /* Return a signed type the same as TYPE in other respects. */
3081 tree
3082 c_common_signed_type (tree type)
3084 return c_common_signed_or_unsigned_type (0, type);
3087 /* Return a type the same as TYPE except unsigned or
3088 signed according to UNSIGNEDP. */
3090 tree
3091 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3093 tree type1;
3095 /* This block of code emulates the behavior of the old
3096 c_common_unsigned_type. In particular, it returns
3097 long_unsigned_type_node if passed a long, even when a int would
3098 have the same size. This is necessary for warnings to work
3099 correctly in archs where sizeof(int) == sizeof(long) */
3101 type1 = TYPE_MAIN_VARIANT (type);
3102 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3103 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3104 if (type1 == integer_type_node || type1 == unsigned_type_node)
3105 return unsignedp ? unsigned_type_node : integer_type_node;
3106 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3107 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3108 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3109 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3110 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3111 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3112 if (int128_integer_type_node
3113 && (type1 == int128_integer_type_node
3114 || type1 == int128_unsigned_type_node))
3115 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3116 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3117 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3118 #if HOST_BITS_PER_WIDE_INT >= 64
3119 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3120 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3121 #endif
3122 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3123 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3124 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3125 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3126 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3127 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3128 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3129 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3131 #define C_COMMON_FIXED_TYPES(NAME) \
3132 if (type1 == short_ ## NAME ## _type_node \
3133 || type1 == unsigned_short_ ## NAME ## _type_node) \
3134 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3135 : short_ ## NAME ## _type_node; \
3136 if (type1 == NAME ## _type_node \
3137 || type1 == unsigned_ ## NAME ## _type_node) \
3138 return unsignedp ? unsigned_ ## NAME ## _type_node \
3139 : NAME ## _type_node; \
3140 if (type1 == long_ ## NAME ## _type_node \
3141 || type1 == unsigned_long_ ## NAME ## _type_node) \
3142 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3143 : long_ ## NAME ## _type_node; \
3144 if (type1 == long_long_ ## NAME ## _type_node \
3145 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3146 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3147 : long_long_ ## NAME ## _type_node;
3149 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3150 if (type1 == NAME ## _type_node \
3151 || type1 == u ## NAME ## _type_node) \
3152 return unsignedp ? u ## NAME ## _type_node \
3153 : NAME ## _type_node;
3155 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3156 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3157 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3158 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3159 : sat_ ## short_ ## NAME ## _type_node; \
3160 if (type1 == sat_ ## NAME ## _type_node \
3161 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3162 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3163 : sat_ ## NAME ## _type_node; \
3164 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3165 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3166 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3167 : sat_ ## long_ ## NAME ## _type_node; \
3168 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3169 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3170 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3171 : sat_ ## long_long_ ## NAME ## _type_node;
3173 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3174 if (type1 == sat_ ## NAME ## _type_node \
3175 || type1 == sat_ ## u ## NAME ## _type_node) \
3176 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3177 : sat_ ## NAME ## _type_node;
3179 C_COMMON_FIXED_TYPES (fract);
3180 C_COMMON_FIXED_TYPES_SAT (fract);
3181 C_COMMON_FIXED_TYPES (accum);
3182 C_COMMON_FIXED_TYPES_SAT (accum);
3184 C_COMMON_FIXED_MODE_TYPES (qq);
3185 C_COMMON_FIXED_MODE_TYPES (hq);
3186 C_COMMON_FIXED_MODE_TYPES (sq);
3187 C_COMMON_FIXED_MODE_TYPES (dq);
3188 C_COMMON_FIXED_MODE_TYPES (tq);
3189 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3190 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3191 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3192 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3193 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3194 C_COMMON_FIXED_MODE_TYPES (ha);
3195 C_COMMON_FIXED_MODE_TYPES (sa);
3196 C_COMMON_FIXED_MODE_TYPES (da);
3197 C_COMMON_FIXED_MODE_TYPES (ta);
3198 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3199 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3200 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3201 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3203 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3204 the precision; they have precision set to match their range, but
3205 may use a wider mode to match an ABI. If we change modes, we may
3206 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3207 the precision as well, so as to yield correct results for
3208 bit-field types. C++ does not have these separate bit-field
3209 types, and producing a signed or unsigned variant of an
3210 ENUMERAL_TYPE may cause other problems as well. */
3212 if (!INTEGRAL_TYPE_P (type)
3213 || TYPE_UNSIGNED (type) == unsignedp)
3214 return type;
3216 #define TYPE_OK(node) \
3217 (TYPE_MODE (type) == TYPE_MODE (node) \
3218 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3219 if (TYPE_OK (signed_char_type_node))
3220 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3221 if (TYPE_OK (integer_type_node))
3222 return unsignedp ? unsigned_type_node : integer_type_node;
3223 if (TYPE_OK (short_integer_type_node))
3224 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3225 if (TYPE_OK (long_integer_type_node))
3226 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3227 if (TYPE_OK (long_long_integer_type_node))
3228 return (unsignedp ? long_long_unsigned_type_node
3229 : long_long_integer_type_node);
3230 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3231 return (unsignedp ? int128_unsigned_type_node
3232 : int128_integer_type_node);
3233 if (TYPE_OK (widest_integer_literal_type_node))
3234 return (unsignedp ? widest_unsigned_literal_type_node
3235 : widest_integer_literal_type_node);
3237 #if HOST_BITS_PER_WIDE_INT >= 64
3238 if (TYPE_OK (intTI_type_node))
3239 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3240 #endif
3241 if (TYPE_OK (intDI_type_node))
3242 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3243 if (TYPE_OK (intSI_type_node))
3244 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3245 if (TYPE_OK (intHI_type_node))
3246 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3247 if (TYPE_OK (intQI_type_node))
3248 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3249 #undef TYPE_OK
3251 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3254 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3256 tree
3257 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3259 /* Extended integer types of the same width as a standard type have
3260 lesser rank, so those of the same width as int promote to int or
3261 unsigned int and are valid for printf formats expecting int or
3262 unsigned int. To avoid such special cases, avoid creating
3263 extended integer types for bit-fields if a standard integer type
3264 is available. */
3265 if (width == TYPE_PRECISION (integer_type_node))
3266 return unsignedp ? unsigned_type_node : integer_type_node;
3267 if (width == TYPE_PRECISION (signed_char_type_node))
3268 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3269 if (width == TYPE_PRECISION (short_integer_type_node))
3270 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3271 if (width == TYPE_PRECISION (long_integer_type_node))
3272 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3273 if (width == TYPE_PRECISION (long_long_integer_type_node))
3274 return (unsignedp ? long_long_unsigned_type_node
3275 : long_long_integer_type_node);
3276 if (int128_integer_type_node
3277 && width == TYPE_PRECISION (int128_integer_type_node))
3278 return (unsignedp ? int128_unsigned_type_node
3279 : int128_integer_type_node);
3280 return build_nonstandard_integer_type (width, unsignedp);
3283 /* The C version of the register_builtin_type langhook. */
3285 void
3286 c_register_builtin_type (tree type, const char* name)
3288 tree decl;
3290 decl = build_decl (UNKNOWN_LOCATION,
3291 TYPE_DECL, get_identifier (name), type);
3292 DECL_ARTIFICIAL (decl) = 1;
3293 if (!TYPE_NAME (type))
3294 TYPE_NAME (type) = decl;
3295 pushdecl (decl);
3297 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3300 /* Print an error message for invalid operands to arith operation
3301 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3302 LOCATION is the location of the message. */
3304 void
3305 binary_op_error (location_t location, enum tree_code code,
3306 tree type0, tree type1)
3308 const char *opname;
3310 switch (code)
3312 case PLUS_EXPR:
3313 opname = "+"; break;
3314 case MINUS_EXPR:
3315 opname = "-"; break;
3316 case MULT_EXPR:
3317 opname = "*"; break;
3318 case MAX_EXPR:
3319 opname = "max"; break;
3320 case MIN_EXPR:
3321 opname = "min"; break;
3322 case EQ_EXPR:
3323 opname = "=="; break;
3324 case NE_EXPR:
3325 opname = "!="; break;
3326 case LE_EXPR:
3327 opname = "<="; break;
3328 case GE_EXPR:
3329 opname = ">="; break;
3330 case LT_EXPR:
3331 opname = "<"; break;
3332 case GT_EXPR:
3333 opname = ">"; break;
3334 case LSHIFT_EXPR:
3335 opname = "<<"; break;
3336 case RSHIFT_EXPR:
3337 opname = ">>"; break;
3338 case TRUNC_MOD_EXPR:
3339 case FLOOR_MOD_EXPR:
3340 opname = "%"; break;
3341 case TRUNC_DIV_EXPR:
3342 case FLOOR_DIV_EXPR:
3343 opname = "/"; break;
3344 case BIT_AND_EXPR:
3345 opname = "&"; break;
3346 case BIT_IOR_EXPR:
3347 opname = "|"; break;
3348 case TRUTH_ANDIF_EXPR:
3349 opname = "&&"; break;
3350 case TRUTH_ORIF_EXPR:
3351 opname = "||"; break;
3352 case BIT_XOR_EXPR:
3353 opname = "^"; break;
3354 default:
3355 gcc_unreachable ();
3357 error_at (location,
3358 "invalid operands to binary %s (have %qT and %qT)", opname,
3359 type0, type1);
3362 /* Subroutine of build_binary_op, used for comparison operations.
3363 See if the operands have both been converted from subword integer types
3364 and, if so, perhaps change them both back to their original type.
3365 This function is also responsible for converting the two operands
3366 to the proper common type for comparison.
3368 The arguments of this function are all pointers to local variables
3369 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3370 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3372 If this function returns nonzero, it means that the comparison has
3373 a constant value. What this function returns is an expression for
3374 that value. */
3376 tree
3377 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3378 enum tree_code *rescode_ptr)
3380 tree type;
3381 tree op0 = *op0_ptr;
3382 tree op1 = *op1_ptr;
3383 int unsignedp0, unsignedp1;
3384 int real1, real2;
3385 tree primop0, primop1;
3386 enum tree_code code = *rescode_ptr;
3388 /* Throw away any conversions to wider types
3389 already present in the operands. */
3391 primop0 = c_common_get_narrower (op0, &unsignedp0);
3392 primop1 = c_common_get_narrower (op1, &unsignedp1);
3394 /* If primopN is first sign-extended from primopN's precision to opN's
3395 precision, then zero-extended from opN's precision to
3396 *restype_ptr precision, shortenings might be invalid. */
3397 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3398 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3399 && !unsignedp0
3400 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3401 primop0 = op0;
3402 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3403 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3404 && !unsignedp1
3405 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3406 primop1 = op1;
3408 /* Handle the case that OP0 does not *contain* a conversion
3409 but it *requires* conversion to FINAL_TYPE. */
3411 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3412 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3413 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3414 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3416 /* If one of the operands must be floated, we cannot optimize. */
3417 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3418 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3420 /* If first arg is constant, swap the args (changing operation
3421 so value is preserved), for canonicalization. Don't do this if
3422 the second arg is 0. */
3424 if (TREE_CONSTANT (primop0)
3425 && !integer_zerop (primop1) && !real_zerop (primop1)
3426 && !fixed_zerop (primop1))
3428 tree tem = primop0;
3429 int temi = unsignedp0;
3430 primop0 = primop1;
3431 primop1 = tem;
3432 tem = op0;
3433 op0 = op1;
3434 op1 = tem;
3435 *op0_ptr = op0;
3436 *op1_ptr = op1;
3437 unsignedp0 = unsignedp1;
3438 unsignedp1 = temi;
3439 temi = real1;
3440 real1 = real2;
3441 real2 = temi;
3443 switch (code)
3445 case LT_EXPR:
3446 code = GT_EXPR;
3447 break;
3448 case GT_EXPR:
3449 code = LT_EXPR;
3450 break;
3451 case LE_EXPR:
3452 code = GE_EXPR;
3453 break;
3454 case GE_EXPR:
3455 code = LE_EXPR;
3456 break;
3457 default:
3458 break;
3460 *rescode_ptr = code;
3463 /* If comparing an integer against a constant more bits wide,
3464 maybe we can deduce a value of 1 or 0 independent of the data.
3465 Or else truncate the constant now
3466 rather than extend the variable at run time.
3468 This is only interesting if the constant is the wider arg.
3469 Also, it is not safe if the constant is unsigned and the
3470 variable arg is signed, since in this case the variable
3471 would be sign-extended and then regarded as unsigned.
3472 Our technique fails in this case because the lowest/highest
3473 possible unsigned results don't follow naturally from the
3474 lowest/highest possible values of the variable operand.
3475 For just EQ_EXPR and NE_EXPR there is another technique that
3476 could be used: see if the constant can be faithfully represented
3477 in the other operand's type, by truncating it and reextending it
3478 and see if that preserves the constant's value. */
3480 if (!real1 && !real2
3481 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3482 && TREE_CODE (primop1) == INTEGER_CST
3483 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3485 int min_gt, max_gt, min_lt, max_lt;
3486 tree maxval, minval;
3487 /* 1 if comparison is nominally unsigned. */
3488 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3489 tree val;
3491 type = c_common_signed_or_unsigned_type (unsignedp0,
3492 TREE_TYPE (primop0));
3494 maxval = TYPE_MAX_VALUE (type);
3495 minval = TYPE_MIN_VALUE (type);
3497 if (unsignedp && !unsignedp0)
3498 *restype_ptr = c_common_signed_type (*restype_ptr);
3500 if (TREE_TYPE (primop1) != *restype_ptr)
3502 /* Convert primop1 to target type, but do not introduce
3503 additional overflow. We know primop1 is an int_cst. */
3504 primop1 = force_fit_type_double (*restype_ptr,
3505 tree_to_double_int (primop1),
3506 0, TREE_OVERFLOW (primop1));
3508 if (type != *restype_ptr)
3510 minval = convert (*restype_ptr, minval);
3511 maxval = convert (*restype_ptr, maxval);
3514 if (unsignedp && unsignedp0)
3516 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3517 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3518 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3519 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3521 else
3523 min_gt = INT_CST_LT (primop1, minval);
3524 max_gt = INT_CST_LT (primop1, maxval);
3525 min_lt = INT_CST_LT (minval, primop1);
3526 max_lt = INT_CST_LT (maxval, primop1);
3529 val = 0;
3530 /* This used to be a switch, but Genix compiler can't handle that. */
3531 if (code == NE_EXPR)
3533 if (max_lt || min_gt)
3534 val = truthvalue_true_node;
3536 else if (code == EQ_EXPR)
3538 if (max_lt || min_gt)
3539 val = truthvalue_false_node;
3541 else if (code == LT_EXPR)
3543 if (max_lt)
3544 val = truthvalue_true_node;
3545 if (!min_lt)
3546 val = truthvalue_false_node;
3548 else if (code == GT_EXPR)
3550 if (min_gt)
3551 val = truthvalue_true_node;
3552 if (!max_gt)
3553 val = truthvalue_false_node;
3555 else if (code == LE_EXPR)
3557 if (!max_gt)
3558 val = truthvalue_true_node;
3559 if (min_gt)
3560 val = truthvalue_false_node;
3562 else if (code == GE_EXPR)
3564 if (!min_lt)
3565 val = truthvalue_true_node;
3566 if (max_lt)
3567 val = truthvalue_false_node;
3570 /* If primop0 was sign-extended and unsigned comparison specd,
3571 we did a signed comparison above using the signed type bounds.
3572 But the comparison we output must be unsigned.
3574 Also, for inequalities, VAL is no good; but if the signed
3575 comparison had *any* fixed result, it follows that the
3576 unsigned comparison just tests the sign in reverse
3577 (positive values are LE, negative ones GE).
3578 So we can generate an unsigned comparison
3579 against an extreme value of the signed type. */
3581 if (unsignedp && !unsignedp0)
3583 if (val != 0)
3584 switch (code)
3586 case LT_EXPR:
3587 case GE_EXPR:
3588 primop1 = TYPE_MIN_VALUE (type);
3589 val = 0;
3590 break;
3592 case LE_EXPR:
3593 case GT_EXPR:
3594 primop1 = TYPE_MAX_VALUE (type);
3595 val = 0;
3596 break;
3598 default:
3599 break;
3601 type = c_common_unsigned_type (type);
3604 if (TREE_CODE (primop0) != INTEGER_CST)
3606 if (val == truthvalue_false_node)
3607 warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type");
3608 if (val == truthvalue_true_node)
3609 warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type");
3612 if (val != 0)
3614 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3615 if (TREE_SIDE_EFFECTS (primop0))
3616 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3617 return val;
3620 /* Value is not predetermined, but do the comparison
3621 in the type of the operand that is not constant.
3622 TYPE is already properly set. */
3625 /* If either arg is decimal float and the other is float, find the
3626 proper common type to use for comparison. */
3627 else if (real1 && real2
3628 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3629 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3630 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3632 else if (real1 && real2
3633 && (TYPE_PRECISION (TREE_TYPE (primop0))
3634 == TYPE_PRECISION (TREE_TYPE (primop1))))
3635 type = TREE_TYPE (primop0);
3637 /* If args' natural types are both narrower than nominal type
3638 and both extend in the same manner, compare them
3639 in the type of the wider arg.
3640 Otherwise must actually extend both to the nominal
3641 common type lest different ways of extending
3642 alter the result.
3643 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3645 else if (unsignedp0 == unsignedp1 && real1 == real2
3646 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3647 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3649 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3650 type = c_common_signed_or_unsigned_type (unsignedp0
3651 || TYPE_UNSIGNED (*restype_ptr),
3652 type);
3653 /* Make sure shorter operand is extended the right way
3654 to match the longer operand. */
3655 primop0
3656 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3657 TREE_TYPE (primop0)),
3658 primop0);
3659 primop1
3660 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3661 TREE_TYPE (primop1)),
3662 primop1);
3664 else
3666 /* Here we must do the comparison on the nominal type
3667 using the args exactly as we received them. */
3668 type = *restype_ptr;
3669 primop0 = op0;
3670 primop1 = op1;
3672 if (!real1 && !real2 && integer_zerop (primop1)
3673 && TYPE_UNSIGNED (*restype_ptr))
3675 tree value = 0;
3676 switch (code)
3678 case GE_EXPR:
3679 /* All unsigned values are >= 0, so we warn. However,
3680 if OP0 is a constant that is >= 0, the signedness of
3681 the comparison isn't an issue, so suppress the
3682 warning. */
3683 if (warn_type_limits && !in_system_header
3684 && !(TREE_CODE (primop0) == INTEGER_CST
3685 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3686 primop0))))
3687 warning (OPT_Wtype_limits,
3688 "comparison of unsigned expression >= 0 is always true");
3689 value = truthvalue_true_node;
3690 break;
3692 case LT_EXPR:
3693 if (warn_type_limits && !in_system_header
3694 && !(TREE_CODE (primop0) == INTEGER_CST
3695 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3696 primop0))))
3697 warning (OPT_Wtype_limits,
3698 "comparison of unsigned expression < 0 is always false");
3699 value = truthvalue_false_node;
3700 break;
3702 default:
3703 break;
3706 if (value != 0)
3708 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3709 if (TREE_SIDE_EFFECTS (primop0))
3710 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3711 primop0, value);
3712 return value;
3717 *op0_ptr = convert (type, primop0);
3718 *op1_ptr = convert (type, primop1);
3720 *restype_ptr = truthvalue_type_node;
3722 return 0;
3725 /* Return a tree for the sum or difference (RESULTCODE says which)
3726 of pointer PTROP and integer INTOP. */
3728 tree
3729 pointer_int_sum (location_t loc, enum tree_code resultcode,
3730 tree ptrop, tree intop)
3732 tree size_exp, ret;
3734 /* The result is a pointer of the same type that is being added. */
3735 tree result_type = TREE_TYPE (ptrop);
3737 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3739 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3740 "pointer of type %<void *%> used in arithmetic");
3741 size_exp = integer_one_node;
3743 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3745 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3746 "pointer to a function used in arithmetic");
3747 size_exp = integer_one_node;
3749 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3751 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3752 "pointer to member function used in arithmetic");
3753 size_exp = integer_one_node;
3755 else
3756 size_exp = size_in_bytes (TREE_TYPE (result_type));
3758 /* We are manipulating pointer values, so we don't need to warn
3759 about relying on undefined signed overflow. We disable the
3760 warning here because we use integer types so fold won't know that
3761 they are really pointers. */
3762 fold_defer_overflow_warnings ();
3764 /* If what we are about to multiply by the size of the elements
3765 contains a constant term, apply distributive law
3766 and multiply that constant term separately.
3767 This helps produce common subexpressions. */
3768 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3769 && !TREE_CONSTANT (intop)
3770 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3771 && TREE_CONSTANT (size_exp)
3772 /* If the constant comes from pointer subtraction,
3773 skip this optimization--it would cause an error. */
3774 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3775 /* If the constant is unsigned, and smaller than the pointer size,
3776 then we must skip this optimization. This is because it could cause
3777 an overflow error if the constant is negative but INTOP is not. */
3778 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3779 || (TYPE_PRECISION (TREE_TYPE (intop))
3780 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3782 enum tree_code subcode = resultcode;
3783 tree int_type = TREE_TYPE (intop);
3784 if (TREE_CODE (intop) == MINUS_EXPR)
3785 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3786 /* Convert both subexpression types to the type of intop,
3787 because weird cases involving pointer arithmetic
3788 can result in a sum or difference with different type args. */
3789 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3790 subcode, ptrop,
3791 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3792 intop = convert (int_type, TREE_OPERAND (intop, 0));
3795 /* Convert the integer argument to a type the same size as sizetype
3796 so the multiply won't overflow spuriously. */
3797 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3798 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3799 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3800 TYPE_UNSIGNED (sizetype)), intop);
3802 /* Replace the integer argument with a suitable product by the object size.
3803 Do this multiplication as signed, then convert to the appropriate type
3804 for the pointer operation and disregard an overflow that occured only
3805 because of the sign-extension change in the latter conversion. */
3807 tree t = build_binary_op (loc,
3808 MULT_EXPR, intop,
3809 convert (TREE_TYPE (intop), size_exp), 1);
3810 intop = convert (sizetype, t);
3811 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3812 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
3813 TREE_INT_CST_HIGH (intop));
3816 /* Create the sum or difference. */
3817 if (resultcode == MINUS_EXPR)
3818 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3820 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3822 fold_undefer_and_ignore_overflow_warnings ();
3824 return ret;
3827 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3828 and if NON_CONST is known not to be permitted in an evaluated part
3829 of a constant expression. */
3831 tree
3832 c_wrap_maybe_const (tree expr, bool non_const)
3834 bool nowarning = TREE_NO_WARNING (expr);
3835 location_t loc = EXPR_LOCATION (expr);
3837 /* This should never be called for C++. */
3838 if (c_dialect_cxx ())
3839 gcc_unreachable ();
3841 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3842 STRIP_TYPE_NOPS (expr);
3843 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3844 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3845 if (nowarning)
3846 TREE_NO_WARNING (expr) = 1;
3847 protected_set_expr_location (expr, loc);
3849 return expr;
3852 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
3853 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3854 around the SAVE_EXPR if needed so that c_fully_fold does not need
3855 to look inside SAVE_EXPRs. */
3857 tree
3858 c_save_expr (tree expr)
3860 bool maybe_const = true;
3861 if (c_dialect_cxx ())
3862 return save_expr (expr);
3863 expr = c_fully_fold (expr, false, &maybe_const);
3864 expr = save_expr (expr);
3865 if (!maybe_const)
3866 expr = c_wrap_maybe_const (expr, true);
3867 return expr;
3870 /* Return whether EXPR is a declaration whose address can never be
3871 NULL. */
3873 bool
3874 decl_with_nonnull_addr_p (const_tree expr)
3876 return (DECL_P (expr)
3877 && (TREE_CODE (expr) == PARM_DECL
3878 || TREE_CODE (expr) == LABEL_DECL
3879 || !DECL_WEAK (expr)));
3882 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3883 or for an `if' or `while' statement or ?..: exp. It should already
3884 have been validated to be of suitable type; otherwise, a bad
3885 diagnostic may result.
3887 The EXPR is located at LOCATION.
3889 This preparation consists of taking the ordinary
3890 representation of an expression expr and producing a valid tree
3891 boolean expression describing whether expr is nonzero. We could
3892 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3893 but we optimize comparisons, &&, ||, and !.
3895 The resulting type should always be `truthvalue_type_node'. */
3897 tree
3898 c_common_truthvalue_conversion (location_t location, tree expr)
3900 switch (TREE_CODE (expr))
3902 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3903 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3904 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3905 case ORDERED_EXPR: case UNORDERED_EXPR:
3906 if (TREE_TYPE (expr) == truthvalue_type_node)
3907 return expr;
3908 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3909 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3910 goto ret;
3912 case TRUTH_ANDIF_EXPR:
3913 case TRUTH_ORIF_EXPR:
3914 case TRUTH_AND_EXPR:
3915 case TRUTH_OR_EXPR:
3916 case TRUTH_XOR_EXPR:
3917 if (TREE_TYPE (expr) == truthvalue_type_node)
3918 return expr;
3919 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3920 c_common_truthvalue_conversion (location,
3921 TREE_OPERAND (expr, 0)),
3922 c_common_truthvalue_conversion (location,
3923 TREE_OPERAND (expr, 1)));
3924 goto ret;
3926 case TRUTH_NOT_EXPR:
3927 if (TREE_TYPE (expr) == truthvalue_type_node)
3928 return expr;
3929 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3930 c_common_truthvalue_conversion (location,
3931 TREE_OPERAND (expr, 0)));
3932 goto ret;
3934 case ERROR_MARK:
3935 return expr;
3937 case INTEGER_CST:
3938 return integer_zerop (expr) ? truthvalue_false_node
3939 : truthvalue_true_node;
3941 case REAL_CST:
3942 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3943 ? truthvalue_true_node
3944 : truthvalue_false_node;
3946 case FIXED_CST:
3947 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3948 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3949 ? truthvalue_true_node
3950 : truthvalue_false_node;
3952 case FUNCTION_DECL:
3953 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
3954 /* Fall through. */
3956 case ADDR_EXPR:
3958 tree inner = TREE_OPERAND (expr, 0);
3959 if (decl_with_nonnull_addr_p (inner))
3961 /* Common Ada/Pascal programmer's mistake. */
3962 warning_at (location,
3963 OPT_Waddress,
3964 "the address of %qD will always evaluate as %<true%>",
3965 inner);
3966 return truthvalue_true_node;
3968 break;
3971 case COMPLEX_EXPR:
3972 expr = build_binary_op (EXPR_LOCATION (expr),
3973 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3974 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3975 c_common_truthvalue_conversion (location,
3976 TREE_OPERAND (expr, 0)),
3977 c_common_truthvalue_conversion (location,
3978 TREE_OPERAND (expr, 1)),
3980 goto ret;
3982 case NEGATE_EXPR:
3983 case ABS_EXPR:
3984 case FLOAT_EXPR:
3985 case EXCESS_PRECISION_EXPR:
3986 /* These don't change whether an object is nonzero or zero. */
3987 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3989 case LROTATE_EXPR:
3990 case RROTATE_EXPR:
3991 /* These don't change whether an object is zero or nonzero, but
3992 we can't ignore them if their second arg has side-effects. */
3993 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3995 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3996 TREE_OPERAND (expr, 1),
3997 c_common_truthvalue_conversion
3998 (location, TREE_OPERAND (expr, 0)));
3999 goto ret;
4001 else
4002 return c_common_truthvalue_conversion (location,
4003 TREE_OPERAND (expr, 0));
4005 case COND_EXPR:
4006 /* Distribute the conversion into the arms of a COND_EXPR. */
4007 if (c_dialect_cxx ())
4009 tree op1 = TREE_OPERAND (expr, 1);
4010 tree op2 = TREE_OPERAND (expr, 2);
4011 /* In C++ one of the arms might have void type if it is throw. */
4012 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4013 op1 = c_common_truthvalue_conversion (location, op1);
4014 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4015 op2 = c_common_truthvalue_conversion (location, op2);
4016 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4017 TREE_OPERAND (expr, 0), op1, op2);
4018 goto ret;
4020 else
4022 /* Folding will happen later for C. */
4023 expr = build3 (COND_EXPR, truthvalue_type_node,
4024 TREE_OPERAND (expr, 0),
4025 c_common_truthvalue_conversion (location,
4026 TREE_OPERAND (expr, 1)),
4027 c_common_truthvalue_conversion (location,
4028 TREE_OPERAND (expr, 2)));
4029 goto ret;
4032 CASE_CONVERT:
4034 tree totype = TREE_TYPE (expr);
4035 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4037 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4038 since that affects how `default_conversion' will behave. */
4039 if (TREE_CODE (totype) == REFERENCE_TYPE
4040 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4041 break;
4042 /* Don't strip a conversion from C++0x scoped enum, since they
4043 don't implicitly convert to other types. */
4044 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4045 && ENUM_IS_SCOPED (fromtype))
4046 break;
4047 /* If this isn't narrowing the argument, we can ignore it. */
4048 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4049 return c_common_truthvalue_conversion (location,
4050 TREE_OPERAND (expr, 0));
4052 break;
4054 case MODIFY_EXPR:
4055 if (!TREE_NO_WARNING (expr)
4056 && warn_parentheses)
4058 warning (OPT_Wparentheses,
4059 "suggest parentheses around assignment used as truth value");
4060 TREE_NO_WARNING (expr) = 1;
4062 break;
4064 default:
4065 break;
4068 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4070 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4071 expr = (build_binary_op
4072 (EXPR_LOCATION (expr),
4073 (TREE_SIDE_EFFECTS (expr)
4074 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4075 c_common_truthvalue_conversion
4076 (location,
4077 build_unary_op (location, REALPART_EXPR, t, 0)),
4078 c_common_truthvalue_conversion
4079 (location,
4080 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4081 0));
4082 goto ret;
4085 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4087 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4088 FCONST0 (TYPE_MODE
4089 (TREE_TYPE (expr))));
4090 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4092 else
4093 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4095 ret:
4096 protected_set_expr_location (expr, location);
4097 return expr;
4100 static void def_builtin_1 (enum built_in_function fncode,
4101 const char *name,
4102 enum built_in_class fnclass,
4103 tree fntype, tree libtype,
4104 bool both_p, bool fallback_p, bool nonansi_p,
4105 tree fnattrs, bool implicit_p);
4108 /* Apply the TYPE_QUALS to the new DECL. */
4110 void
4111 c_apply_type_quals_to_decl (int type_quals, tree decl)
4113 tree type = TREE_TYPE (decl);
4115 if (type == error_mark_node)
4116 return;
4118 if ((type_quals & TYPE_QUAL_CONST)
4119 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4120 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4121 constructor can produce constant init, so rely on cp_finish_decl to
4122 clear TREE_READONLY if the variable has non-constant init. */
4123 TREE_READONLY (decl) = 1;
4124 if (type_quals & TYPE_QUAL_VOLATILE)
4126 TREE_SIDE_EFFECTS (decl) = 1;
4127 TREE_THIS_VOLATILE (decl) = 1;
4129 if (type_quals & TYPE_QUAL_RESTRICT)
4131 while (type && TREE_CODE (type) == ARRAY_TYPE)
4132 /* Allow 'restrict' on arrays of pointers.
4133 FIXME currently we just ignore it. */
4134 type = TREE_TYPE (type);
4135 if (!type
4136 || !POINTER_TYPE_P (type)
4137 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4138 error ("invalid use of %<restrict%>");
4142 /* Hash function for the problem of multiple type definitions in
4143 different files. This must hash all types that will compare
4144 equal via comptypes to the same value. In practice it hashes
4145 on some of the simple stuff and leaves the details to comptypes. */
4147 static hashval_t
4148 c_type_hash (const void *p)
4150 int n_elements;
4151 int shift, size;
4152 const_tree const t = (const_tree) p;
4153 tree t2;
4154 switch (TREE_CODE (t))
4156 /* For pointers, hash on pointee type plus some swizzling. */
4157 case POINTER_TYPE:
4158 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4159 /* Hash on number of elements and total size. */
4160 case ENUMERAL_TYPE:
4161 shift = 3;
4162 t2 = TYPE_VALUES (t);
4163 break;
4164 case RECORD_TYPE:
4165 shift = 0;
4166 t2 = TYPE_FIELDS (t);
4167 break;
4168 case QUAL_UNION_TYPE:
4169 shift = 1;
4170 t2 = TYPE_FIELDS (t);
4171 break;
4172 case UNION_TYPE:
4173 shift = 2;
4174 t2 = TYPE_FIELDS (t);
4175 break;
4176 default:
4177 gcc_unreachable ();
4179 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4180 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4181 n_elements = list_length (t2);
4182 /* We might have a VLA here. */
4183 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4184 size = 0;
4185 else
4186 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4187 return ((size << 24) | (n_elements << shift));
4190 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4192 /* Return the typed-based alias set for T, which may be an expression
4193 or a type. Return -1 if we don't do anything special. */
4195 alias_set_type
4196 c_common_get_alias_set (tree t)
4198 tree u;
4199 PTR *slot;
4201 /* For VLAs, use the alias set of the element type rather than the
4202 default of alias set 0 for types compared structurally. */
4203 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4205 if (TREE_CODE (t) == ARRAY_TYPE)
4206 return get_alias_set (TREE_TYPE (t));
4207 return -1;
4210 /* Permit type-punning when accessing a union, provided the access
4211 is directly through the union. For example, this code does not
4212 permit taking the address of a union member and then storing
4213 through it. Even the type-punning allowed here is a GCC
4214 extension, albeit a common and useful one; the C standard says
4215 that such accesses have implementation-defined behavior. */
4216 for (u = t;
4217 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4218 u = TREE_OPERAND (u, 0))
4219 if (TREE_CODE (u) == COMPONENT_REF
4220 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4221 return 0;
4223 /* That's all the expressions we handle specially. */
4224 if (!TYPE_P (t))
4225 return -1;
4227 /* The C standard guarantees that any object may be accessed via an
4228 lvalue that has character type. */
4229 if (t == char_type_node
4230 || t == signed_char_type_node
4231 || t == unsigned_char_type_node)
4232 return 0;
4234 /* The C standard specifically allows aliasing between signed and
4235 unsigned variants of the same type. We treat the signed
4236 variant as canonical. */
4237 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4239 tree t1 = c_common_signed_type (t);
4241 /* t1 == t can happen for boolean nodes which are always unsigned. */
4242 if (t1 != t)
4243 return get_alias_set (t1);
4246 /* Handle the case of multiple type nodes referring to "the same" type,
4247 which occurs with IMA. These share an alias set. FIXME: Currently only
4248 C90 is handled. (In C99 type compatibility is not transitive, which
4249 complicates things mightily. The alias set splay trees can theoretically
4250 represent this, but insertion is tricky when you consider all the
4251 different orders things might arrive in.) */
4253 if (c_language != clk_c || flag_isoc99)
4254 return -1;
4256 /* Save time if there's only one input file. */
4257 if (num_in_fnames == 1)
4258 return -1;
4260 /* Pointers need special handling if they point to any type that
4261 needs special handling (below). */
4262 if (TREE_CODE (t) == POINTER_TYPE)
4264 tree t2;
4265 /* Find bottom type under any nested POINTERs. */
4266 for (t2 = TREE_TYPE (t);
4267 TREE_CODE (t2) == POINTER_TYPE;
4268 t2 = TREE_TYPE (t2))
4270 if (TREE_CODE (t2) != RECORD_TYPE
4271 && TREE_CODE (t2) != ENUMERAL_TYPE
4272 && TREE_CODE (t2) != QUAL_UNION_TYPE
4273 && TREE_CODE (t2) != UNION_TYPE)
4274 return -1;
4275 if (TYPE_SIZE (t2) == 0)
4276 return -1;
4278 /* These are the only cases that need special handling. */
4279 if (TREE_CODE (t) != RECORD_TYPE
4280 && TREE_CODE (t) != ENUMERAL_TYPE
4281 && TREE_CODE (t) != QUAL_UNION_TYPE
4282 && TREE_CODE (t) != UNION_TYPE
4283 && TREE_CODE (t) != POINTER_TYPE)
4284 return -1;
4285 /* Undefined? */
4286 if (TYPE_SIZE (t) == 0)
4287 return -1;
4289 /* Look up t in hash table. Only one of the compatible types within each
4290 alias set is recorded in the table. */
4291 if (!type_hash_table)
4292 type_hash_table = htab_create_ggc (1021, c_type_hash,
4293 (htab_eq) lang_hooks.types_compatible_p,
4294 NULL);
4295 slot = htab_find_slot (type_hash_table, t, INSERT);
4296 if (*slot != NULL)
4298 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4299 return TYPE_ALIAS_SET ((tree)*slot);
4301 else
4302 /* Our caller will assign and record (in t) a new alias set; all we need
4303 to do is remember t in the hash table. */
4304 *slot = t;
4306 return -1;
4309 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4310 the second parameter indicates which OPERATOR is being applied.
4311 The COMPLAIN flag controls whether we should diagnose possibly
4312 ill-formed constructs or not. LOC is the location of the SIZEOF or
4313 TYPEOF operator. */
4315 tree
4316 c_sizeof_or_alignof_type (location_t loc,
4317 tree type, bool is_sizeof, int complain)
4319 const char *op_name;
4320 tree value = NULL;
4321 enum tree_code type_code = TREE_CODE (type);
4323 op_name = is_sizeof ? "sizeof" : "__alignof__";
4325 if (type_code == FUNCTION_TYPE)
4327 if (is_sizeof)
4329 if (complain && (pedantic || warn_pointer_arith))
4330 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4331 "invalid application of %<sizeof%> to a function type");
4332 else if (!complain)
4333 return error_mark_node;
4334 value = size_one_node;
4336 else
4338 if (complain)
4340 if (c_dialect_cxx ())
4341 pedwarn (loc, OPT_pedantic, "ISO C++ does not permit "
4342 "%<alignof%> applied to a function type");
4343 else
4344 pedwarn (loc, OPT_pedantic, "ISO C does not permit "
4345 "%<_Alignof%> applied to a function type");
4347 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4350 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4352 if (type_code == VOID_TYPE
4353 && complain && (pedantic || warn_pointer_arith))
4354 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4355 "invalid application of %qs to a void type", op_name);
4356 else if (!complain)
4357 return error_mark_node;
4358 value = size_one_node;
4360 else if (!COMPLETE_TYPE_P (type))
4362 if (complain)
4363 error_at (loc, "invalid application of %qs to incomplete type %qT ",
4364 op_name, type);
4365 return error_mark_node;
4367 else
4369 if (is_sizeof)
4370 /* Convert in case a char is more than one unit. */
4371 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4372 size_int (TYPE_PRECISION (char_type_node)
4373 / BITS_PER_UNIT));
4374 else
4375 value = size_int (TYPE_ALIGN_UNIT (type));
4378 /* VALUE will have an integer type with TYPE_IS_SIZETYPE set.
4379 TYPE_IS_SIZETYPE means that certain things (like overflow) will
4380 never happen. However, this node should really have type
4381 `size_t', which is just a typedef for an ordinary integer type. */
4382 value = fold_convert_loc (loc, size_type_node, value);
4383 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
4385 return value;
4388 /* Implement the __alignof keyword: Return the minimum required
4389 alignment of EXPR, measured in bytes. For VAR_DECLs,
4390 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4391 from an "aligned" __attribute__ specification). LOC is the
4392 location of the ALIGNOF operator. */
4394 tree
4395 c_alignof_expr (location_t loc, tree expr)
4397 tree t;
4399 if (VAR_OR_FUNCTION_DECL_P (expr))
4400 t = size_int (DECL_ALIGN_UNIT (expr));
4402 else if (TREE_CODE (expr) == COMPONENT_REF
4403 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4405 error_at (loc, "%<__alignof%> applied to a bit-field");
4406 t = size_one_node;
4408 else if (TREE_CODE (expr) == COMPONENT_REF
4409 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4410 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4412 else if (TREE_CODE (expr) == INDIRECT_REF)
4414 tree t = TREE_OPERAND (expr, 0);
4415 tree best = t;
4416 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4418 while (CONVERT_EXPR_P (t)
4419 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4421 int thisalign;
4423 t = TREE_OPERAND (t, 0);
4424 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4425 if (thisalign > bestalign)
4426 best = t, bestalign = thisalign;
4428 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4430 else
4431 return c_alignof (loc, TREE_TYPE (expr));
4433 return fold_convert_loc (loc, size_type_node, t);
4436 /* Handle C and C++ default attributes. */
4438 enum built_in_attribute
4440 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4441 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4442 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4443 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4444 #include "builtin-attrs.def"
4445 #undef DEF_ATTR_NULL_TREE
4446 #undef DEF_ATTR_INT
4447 #undef DEF_ATTR_IDENT
4448 #undef DEF_ATTR_TREE_LIST
4449 ATTR_LAST
4452 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4454 static void c_init_attributes (void);
4456 enum c_builtin_type
4458 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4459 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4460 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4461 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4462 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4463 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4464 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4465 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4466 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4467 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4468 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4469 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4470 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4471 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4472 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4473 NAME,
4474 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4475 #include "builtin-types.def"
4476 #undef DEF_PRIMITIVE_TYPE
4477 #undef DEF_FUNCTION_TYPE_0
4478 #undef DEF_FUNCTION_TYPE_1
4479 #undef DEF_FUNCTION_TYPE_2
4480 #undef DEF_FUNCTION_TYPE_3
4481 #undef DEF_FUNCTION_TYPE_4
4482 #undef DEF_FUNCTION_TYPE_5
4483 #undef DEF_FUNCTION_TYPE_6
4484 #undef DEF_FUNCTION_TYPE_7
4485 #undef DEF_FUNCTION_TYPE_VAR_0
4486 #undef DEF_FUNCTION_TYPE_VAR_1
4487 #undef DEF_FUNCTION_TYPE_VAR_2
4488 #undef DEF_FUNCTION_TYPE_VAR_3
4489 #undef DEF_FUNCTION_TYPE_VAR_4
4490 #undef DEF_FUNCTION_TYPE_VAR_5
4491 #undef DEF_POINTER_TYPE
4492 BT_LAST
4495 typedef enum c_builtin_type builtin_type;
4497 /* A temporary array for c_common_nodes_and_builtins. Used in
4498 communication with def_fn_type. */
4499 static tree builtin_types[(int) BT_LAST + 1];
4501 /* A helper function for c_common_nodes_and_builtins. Build function type
4502 for DEF with return type RET and N arguments. If VAR is true, then the
4503 function should be variadic after those N arguments.
4505 Takes special care not to ICE if any of the types involved are
4506 error_mark_node, which indicates that said type is not in fact available
4507 (see builtin_type_for_size). In which case the function type as a whole
4508 should be error_mark_node. */
4510 static void
4511 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4513 tree t;
4514 tree *args = XALLOCAVEC (tree, n);
4515 va_list list;
4516 int i;
4518 va_start (list, n);
4519 for (i = 0; i < n; ++i)
4521 builtin_type a = (builtin_type) va_arg (list, int);
4522 t = builtin_types[a];
4523 if (t == error_mark_node)
4524 goto egress;
4525 args[i] = t;
4528 t = builtin_types[ret];
4529 if (t == error_mark_node)
4530 goto egress;
4531 if (var)
4532 t = build_varargs_function_type_array (t, n, args);
4533 else
4534 t = build_function_type_array (t, n, args);
4536 egress:
4537 builtin_types[def] = t;
4538 va_end (list);
4541 /* Build builtin functions common to both C and C++ language
4542 frontends. */
4544 static void
4545 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4547 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4548 builtin_types[ENUM] = VALUE;
4549 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4550 def_fn_type (ENUM, RETURN, 0, 0);
4551 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4552 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4553 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4554 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4555 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4556 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4557 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4558 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4559 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4560 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4561 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4562 ARG6) \
4563 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4564 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4565 ARG6, ARG7) \
4566 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4567 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4568 def_fn_type (ENUM, RETURN, 1, 0);
4569 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4570 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4571 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4572 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4573 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4574 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4575 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4576 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4577 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4578 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4579 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4580 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4582 #include "builtin-types.def"
4584 #undef DEF_PRIMITIVE_TYPE
4585 #undef DEF_FUNCTION_TYPE_1
4586 #undef DEF_FUNCTION_TYPE_2
4587 #undef DEF_FUNCTION_TYPE_3
4588 #undef DEF_FUNCTION_TYPE_4
4589 #undef DEF_FUNCTION_TYPE_5
4590 #undef DEF_FUNCTION_TYPE_6
4591 #undef DEF_FUNCTION_TYPE_VAR_0
4592 #undef DEF_FUNCTION_TYPE_VAR_1
4593 #undef DEF_FUNCTION_TYPE_VAR_2
4594 #undef DEF_FUNCTION_TYPE_VAR_3
4595 #undef DEF_FUNCTION_TYPE_VAR_4
4596 #undef DEF_FUNCTION_TYPE_VAR_5
4597 #undef DEF_POINTER_TYPE
4598 builtin_types[(int) BT_LAST] = NULL_TREE;
4600 c_init_attributes ();
4602 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4603 NONANSI_P, ATTRS, IMPLICIT, COND) \
4604 if (NAME && COND) \
4605 def_builtin_1 (ENUM, NAME, CLASS, \
4606 builtin_types[(int) TYPE], \
4607 builtin_types[(int) LIBTYPE], \
4608 BOTH_P, FALLBACK_P, NONANSI_P, \
4609 built_in_attributes[(int) ATTRS], IMPLICIT);
4610 #include "builtins.def"
4611 #undef DEF_BUILTIN
4613 targetm.init_builtins ();
4615 build_common_builtin_nodes ();
4617 if (flag_mudflap)
4618 mudflap_init ();
4621 /* Like get_identifier, but avoid warnings about null arguments when
4622 the argument may be NULL for targets where GCC lacks stdint.h type
4623 information. */
4625 static inline tree
4626 c_get_ident (const char *id)
4628 return get_identifier (id);
4631 /* Build tree nodes and builtin functions common to both C and C++ language
4632 frontends. */
4634 void
4635 c_common_nodes_and_builtins (void)
4637 int char16_type_size;
4638 int char32_type_size;
4639 int wchar_type_size;
4640 tree array_domain_type;
4641 tree va_list_ref_type_node;
4642 tree va_list_arg_type_node;
4644 build_common_tree_nodes (flag_signed_char, flag_short_double);
4646 /* Define `int' and `char' first so that dbx will output them first. */
4647 record_builtin_type (RID_INT, NULL, integer_type_node);
4648 record_builtin_type (RID_CHAR, "char", char_type_node);
4650 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4651 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4652 but not C. Are the conditionals here needed? */
4653 if (c_dialect_cxx ())
4654 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4655 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4656 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4657 record_builtin_type (RID_MAX, "long unsigned int",
4658 long_unsigned_type_node);
4659 if (int128_integer_type_node != NULL_TREE)
4661 record_builtin_type (RID_INT128, "__int128",
4662 int128_integer_type_node);
4663 record_builtin_type (RID_MAX, "__int128 unsigned",
4664 int128_unsigned_type_node);
4666 if (c_dialect_cxx ())
4667 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4668 record_builtin_type (RID_MAX, "long long int",
4669 long_long_integer_type_node);
4670 record_builtin_type (RID_MAX, "long long unsigned int",
4671 long_long_unsigned_type_node);
4672 if (c_dialect_cxx ())
4673 record_builtin_type (RID_MAX, "long long unsigned",
4674 long_long_unsigned_type_node);
4675 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4676 record_builtin_type (RID_MAX, "short unsigned int",
4677 short_unsigned_type_node);
4678 if (c_dialect_cxx ())
4679 record_builtin_type (RID_MAX, "unsigned short",
4680 short_unsigned_type_node);
4682 /* Define both `signed char' and `unsigned char'. */
4683 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4684 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4686 /* These are types that c_common_type_for_size and
4687 c_common_type_for_mode use. */
4688 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4689 TYPE_DECL, NULL_TREE,
4690 intQI_type_node));
4691 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4692 TYPE_DECL, NULL_TREE,
4693 intHI_type_node));
4694 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4695 TYPE_DECL, NULL_TREE,
4696 intSI_type_node));
4697 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4698 TYPE_DECL, NULL_TREE,
4699 intDI_type_node));
4700 #if HOST_BITS_PER_WIDE_INT >= 64
4701 if (targetm.scalar_mode_supported_p (TImode))
4702 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4703 TYPE_DECL,
4704 get_identifier ("__int128_t"),
4705 intTI_type_node));
4706 #endif
4707 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4708 TYPE_DECL, NULL_TREE,
4709 unsigned_intQI_type_node));
4710 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4711 TYPE_DECL, NULL_TREE,
4712 unsigned_intHI_type_node));
4713 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4714 TYPE_DECL, NULL_TREE,
4715 unsigned_intSI_type_node));
4716 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4717 TYPE_DECL, NULL_TREE,
4718 unsigned_intDI_type_node));
4719 #if HOST_BITS_PER_WIDE_INT >= 64
4720 if (targetm.scalar_mode_supported_p (TImode))
4721 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4722 TYPE_DECL,
4723 get_identifier ("__uint128_t"),
4724 unsigned_intTI_type_node));
4725 #endif
4727 /* Create the widest literal types. */
4728 widest_integer_literal_type_node
4729 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
4730 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4731 TYPE_DECL, NULL_TREE,
4732 widest_integer_literal_type_node));
4734 widest_unsigned_literal_type_node
4735 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
4736 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4737 TYPE_DECL, NULL_TREE,
4738 widest_unsigned_literal_type_node));
4740 signed_size_type_node = c_common_signed_type (size_type_node);
4742 pid_type_node =
4743 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4745 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4746 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4747 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4749 /* Only supported decimal floating point extension if the target
4750 actually supports underlying modes. */
4751 if (targetm.scalar_mode_supported_p (SDmode)
4752 && targetm.scalar_mode_supported_p (DDmode)
4753 && targetm.scalar_mode_supported_p (TDmode))
4755 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4756 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4757 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4760 if (targetm.fixed_point_supported_p ())
4762 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4763 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4764 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4765 record_builtin_type (RID_MAX, "long long _Fract",
4766 long_long_fract_type_node);
4767 record_builtin_type (RID_MAX, "unsigned short _Fract",
4768 unsigned_short_fract_type_node);
4769 record_builtin_type (RID_MAX, "unsigned _Fract",
4770 unsigned_fract_type_node);
4771 record_builtin_type (RID_MAX, "unsigned long _Fract",
4772 unsigned_long_fract_type_node);
4773 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4774 unsigned_long_long_fract_type_node);
4775 record_builtin_type (RID_MAX, "_Sat short _Fract",
4776 sat_short_fract_type_node);
4777 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4778 record_builtin_type (RID_MAX, "_Sat long _Fract",
4779 sat_long_fract_type_node);
4780 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4781 sat_long_long_fract_type_node);
4782 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4783 sat_unsigned_short_fract_type_node);
4784 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4785 sat_unsigned_fract_type_node);
4786 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4787 sat_unsigned_long_fract_type_node);
4788 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4789 sat_unsigned_long_long_fract_type_node);
4790 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4791 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4792 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4793 record_builtin_type (RID_MAX, "long long _Accum",
4794 long_long_accum_type_node);
4795 record_builtin_type (RID_MAX, "unsigned short _Accum",
4796 unsigned_short_accum_type_node);
4797 record_builtin_type (RID_MAX, "unsigned _Accum",
4798 unsigned_accum_type_node);
4799 record_builtin_type (RID_MAX, "unsigned long _Accum",
4800 unsigned_long_accum_type_node);
4801 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4802 unsigned_long_long_accum_type_node);
4803 record_builtin_type (RID_MAX, "_Sat short _Accum",
4804 sat_short_accum_type_node);
4805 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4806 record_builtin_type (RID_MAX, "_Sat long _Accum",
4807 sat_long_accum_type_node);
4808 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4809 sat_long_long_accum_type_node);
4810 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4811 sat_unsigned_short_accum_type_node);
4812 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4813 sat_unsigned_accum_type_node);
4814 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4815 sat_unsigned_long_accum_type_node);
4816 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4817 sat_unsigned_long_long_accum_type_node);
4821 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4822 TYPE_DECL,
4823 get_identifier ("complex int"),
4824 complex_integer_type_node));
4825 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4826 TYPE_DECL,
4827 get_identifier ("complex float"),
4828 complex_float_type_node));
4829 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4830 TYPE_DECL,
4831 get_identifier ("complex double"),
4832 complex_double_type_node));
4833 lang_hooks.decls.pushdecl
4834 (build_decl (UNKNOWN_LOCATION,
4835 TYPE_DECL, get_identifier ("complex long double"),
4836 complex_long_double_type_node));
4838 if (c_dialect_cxx ())
4839 /* For C++, make fileptr_type_node a distinct void * type until
4840 FILE type is defined. */
4841 fileptr_type_node = build_variant_type_copy (ptr_type_node);
4843 record_builtin_type (RID_VOID, NULL, void_type_node);
4845 /* Set the TYPE_NAME for any variants that were built before
4846 record_builtin_type gave names to the built-in types. */
4848 tree void_name = TYPE_NAME (void_type_node);
4849 TYPE_NAME (void_type_node) = NULL_TREE;
4850 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4851 = void_name;
4852 TYPE_NAME (void_type_node) = void_name;
4855 /* This node must not be shared. */
4856 void_zero_node = make_node (INTEGER_CST);
4857 TREE_TYPE (void_zero_node) = void_type_node;
4859 void_list_node = build_void_list_node ();
4861 /* Make a type to be the domain of a few array types
4862 whose domains don't really matter.
4863 200 is small enough that it always fits in size_t
4864 and large enough that it can hold most function names for the
4865 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4866 array_domain_type = build_index_type (size_int (200));
4868 /* Make a type for arrays of characters.
4869 With luck nothing will ever really depend on the length of this
4870 array type. */
4871 char_array_type_node
4872 = build_array_type (char_type_node, array_domain_type);
4874 /* Likewise for arrays of ints. */
4875 int_array_type_node
4876 = build_array_type (integer_type_node, array_domain_type);
4878 string_type_node = build_pointer_type (char_type_node);
4879 const_string_type_node
4880 = build_pointer_type (build_qualified_type
4881 (char_type_node, TYPE_QUAL_CONST));
4883 /* This is special for C++ so functions can be overloaded. */
4884 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4885 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4886 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4887 underlying_wchar_type_node = wchar_type_node;
4888 if (c_dialect_cxx ())
4890 if (TYPE_UNSIGNED (wchar_type_node))
4891 wchar_type_node = make_unsigned_type (wchar_type_size);
4892 else
4893 wchar_type_node = make_signed_type (wchar_type_size);
4894 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4897 /* This is for wide string constants. */
4898 wchar_array_type_node
4899 = build_array_type (wchar_type_node, array_domain_type);
4901 /* Define 'char16_t'. */
4902 char16_type_node = get_identifier (CHAR16_TYPE);
4903 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4904 char16_type_size = TYPE_PRECISION (char16_type_node);
4905 if (c_dialect_cxx ())
4907 char16_type_node = make_unsigned_type (char16_type_size);
4909 if (cxx_dialect == cxx0x)
4910 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4913 /* This is for UTF-16 string constants. */
4914 char16_array_type_node
4915 = build_array_type (char16_type_node, array_domain_type);
4917 /* Define 'char32_t'. */
4918 char32_type_node = get_identifier (CHAR32_TYPE);
4919 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4920 char32_type_size = TYPE_PRECISION (char32_type_node);
4921 if (c_dialect_cxx ())
4923 char32_type_node = make_unsigned_type (char32_type_size);
4925 if (cxx_dialect == cxx0x)
4926 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4929 /* This is for UTF-32 string constants. */
4930 char32_array_type_node
4931 = build_array_type (char32_type_node, array_domain_type);
4933 wint_type_node =
4934 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4936 intmax_type_node =
4937 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4938 uintmax_type_node =
4939 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4941 if (SIG_ATOMIC_TYPE)
4942 sig_atomic_type_node =
4943 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4944 if (INT8_TYPE)
4945 int8_type_node =
4946 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4947 if (INT16_TYPE)
4948 int16_type_node =
4949 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4950 if (INT32_TYPE)
4951 int32_type_node =
4952 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4953 if (INT64_TYPE)
4954 int64_type_node =
4955 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4956 if (UINT8_TYPE)
4957 uint8_type_node =
4958 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4959 if (UINT16_TYPE)
4960 uint16_type_node =
4961 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4962 if (UINT32_TYPE)
4963 c_uint32_type_node =
4964 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4965 if (UINT64_TYPE)
4966 c_uint64_type_node =
4967 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4968 if (INT_LEAST8_TYPE)
4969 int_least8_type_node =
4970 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4971 if (INT_LEAST16_TYPE)
4972 int_least16_type_node =
4973 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4974 if (INT_LEAST32_TYPE)
4975 int_least32_type_node =
4976 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4977 if (INT_LEAST64_TYPE)
4978 int_least64_type_node =
4979 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4980 if (UINT_LEAST8_TYPE)
4981 uint_least8_type_node =
4982 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4983 if (UINT_LEAST16_TYPE)
4984 uint_least16_type_node =
4985 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4986 if (UINT_LEAST32_TYPE)
4987 uint_least32_type_node =
4988 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4989 if (UINT_LEAST64_TYPE)
4990 uint_least64_type_node =
4991 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4992 if (INT_FAST8_TYPE)
4993 int_fast8_type_node =
4994 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4995 if (INT_FAST16_TYPE)
4996 int_fast16_type_node =
4997 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4998 if (INT_FAST32_TYPE)
4999 int_fast32_type_node =
5000 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5001 if (INT_FAST64_TYPE)
5002 int_fast64_type_node =
5003 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5004 if (UINT_FAST8_TYPE)
5005 uint_fast8_type_node =
5006 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5007 if (UINT_FAST16_TYPE)
5008 uint_fast16_type_node =
5009 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5010 if (UINT_FAST32_TYPE)
5011 uint_fast32_type_node =
5012 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5013 if (UINT_FAST64_TYPE)
5014 uint_fast64_type_node =
5015 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5016 if (INTPTR_TYPE)
5017 intptr_type_node =
5018 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5019 if (UINTPTR_TYPE)
5020 uintptr_type_node =
5021 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5023 default_function_type
5024 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5025 ptrdiff_type_node
5026 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5027 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5029 lang_hooks.decls.pushdecl
5030 (build_decl (UNKNOWN_LOCATION,
5031 TYPE_DECL, get_identifier ("__builtin_va_list"),
5032 va_list_type_node));
5033 if (targetm.enum_va_list_p)
5035 int l;
5036 const char *pname;
5037 tree ptype;
5039 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5041 lang_hooks.decls.pushdecl
5042 (build_decl (UNKNOWN_LOCATION,
5043 TYPE_DECL, get_identifier (pname),
5044 ptype));
5049 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5051 va_list_arg_type_node = va_list_ref_type_node =
5052 build_pointer_type (TREE_TYPE (va_list_type_node));
5054 else
5056 va_list_arg_type_node = va_list_type_node;
5057 va_list_ref_type_node = build_reference_type (va_list_type_node);
5060 if (!flag_preprocess_only)
5061 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5063 main_identifier_node = get_identifier ("main");
5065 /* Create the built-in __null node. It is important that this is
5066 not shared. */
5067 null_node = make_node (INTEGER_CST);
5068 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5070 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5071 memset (builtin_types, 0, sizeof (builtin_types));
5074 /* The number of named compound-literals generated thus far. */
5075 static GTY(()) int compound_literal_number;
5077 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5079 void
5080 set_compound_literal_name (tree decl)
5082 char *name;
5083 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5084 compound_literal_number);
5085 compound_literal_number++;
5086 DECL_NAME (decl) = get_identifier (name);
5089 tree
5090 build_va_arg (location_t loc, tree expr, tree type)
5092 expr = build1 (VA_ARG_EXPR, type, expr);
5093 SET_EXPR_LOCATION (expr, loc);
5094 return expr;
5098 /* Linked list of disabled built-in functions. */
5100 typedef struct disabled_builtin
5102 const char *name;
5103 struct disabled_builtin *next;
5104 } disabled_builtin;
5105 static disabled_builtin *disabled_builtins = NULL;
5107 static bool builtin_function_disabled_p (const char *);
5109 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5110 begins with "__builtin_", give an error. */
5112 void
5113 disable_builtin_function (const char *name)
5115 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5116 error ("cannot disable built-in function %qs", name);
5117 else
5119 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5120 new_disabled_builtin->name = name;
5121 new_disabled_builtin->next = disabled_builtins;
5122 disabled_builtins = new_disabled_builtin;
5127 /* Return true if the built-in function NAME has been disabled, false
5128 otherwise. */
5130 static bool
5131 builtin_function_disabled_p (const char *name)
5133 disabled_builtin *p;
5134 for (p = disabled_builtins; p != NULL; p = p->next)
5136 if (strcmp (name, p->name) == 0)
5137 return true;
5139 return false;
5143 /* Worker for DEF_BUILTIN.
5144 Possibly define a builtin function with one or two names.
5145 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5146 nonansi_p and flag_no_nonansi_builtin. */
5148 static void
5149 def_builtin_1 (enum built_in_function fncode,
5150 const char *name,
5151 enum built_in_class fnclass,
5152 tree fntype, tree libtype,
5153 bool both_p, bool fallback_p, bool nonansi_p,
5154 tree fnattrs, bool implicit_p)
5156 tree decl;
5157 const char *libname;
5159 if (fntype == error_mark_node)
5160 return;
5162 gcc_assert ((!both_p && !fallback_p)
5163 || !strncmp (name, "__builtin_",
5164 strlen ("__builtin_")));
5166 libname = name + strlen ("__builtin_");
5167 decl = add_builtin_function (name, fntype, fncode, fnclass,
5168 (fallback_p ? libname : NULL),
5169 fnattrs);
5171 set_builtin_decl (fncode, decl, implicit_p);
5173 if (both_p
5174 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5175 && !(nonansi_p && flag_no_nonansi_builtin))
5176 add_builtin_function (libname, libtype, fncode, fnclass,
5177 NULL, fnattrs);
5180 /* Nonzero if the type T promotes to int. This is (nearly) the
5181 integral promotions defined in ISO C99 6.3.1.1/2. */
5183 bool
5184 c_promoting_integer_type_p (const_tree t)
5186 switch (TREE_CODE (t))
5188 case INTEGER_TYPE:
5189 return (TYPE_MAIN_VARIANT (t) == char_type_node
5190 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5191 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5192 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5193 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5194 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5196 case ENUMERAL_TYPE:
5197 /* ??? Technically all enumerations not larger than an int
5198 promote to an int. But this is used along code paths
5199 that only want to notice a size change. */
5200 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5202 case BOOLEAN_TYPE:
5203 return 1;
5205 default:
5206 return 0;
5210 /* Return 1 if PARMS specifies a fixed number of parameters
5211 and none of their types is affected by default promotions. */
5214 self_promoting_args_p (const_tree parms)
5216 const_tree t;
5217 for (t = parms; t; t = TREE_CHAIN (t))
5219 tree type = TREE_VALUE (t);
5221 if (type == error_mark_node)
5222 continue;
5224 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5225 return 0;
5227 if (type == 0)
5228 return 0;
5230 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5231 return 0;
5233 if (c_promoting_integer_type_p (type))
5234 return 0;
5236 return 1;
5239 /* Recursively remove any '*' or '&' operator from TYPE. */
5240 tree
5241 strip_pointer_operator (tree t)
5243 while (POINTER_TYPE_P (t))
5244 t = TREE_TYPE (t);
5245 return t;
5248 /* Recursively remove pointer or array type from TYPE. */
5249 tree
5250 strip_pointer_or_array_types (tree t)
5252 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5253 t = TREE_TYPE (t);
5254 return t;
5257 /* Used to compare case labels. K1 and K2 are actually tree nodes
5258 representing case labels, or NULL_TREE for a `default' label.
5259 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5260 K2, and 0 if K1 and K2 are equal. */
5263 case_compare (splay_tree_key k1, splay_tree_key k2)
5265 /* Consider a NULL key (such as arises with a `default' label) to be
5266 smaller than anything else. */
5267 if (!k1)
5268 return k2 ? -1 : 0;
5269 else if (!k2)
5270 return k1 ? 1 : 0;
5272 return tree_int_cst_compare ((tree) k1, (tree) k2);
5275 /* Process a case label, located at LOC, for the range LOW_VALUE
5276 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5277 then this case label is actually a `default' label. If only
5278 HIGH_VALUE is NULL_TREE, then case label was declared using the
5279 usual C/C++ syntax, rather than the GNU case range extension.
5280 CASES is a tree containing all the case ranges processed so far;
5281 COND is the condition for the switch-statement itself. Returns the
5282 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5283 is created. */
5285 tree
5286 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5287 tree low_value, tree high_value)
5289 tree type;
5290 tree label;
5291 tree case_label;
5292 splay_tree_node node;
5294 /* Create the LABEL_DECL itself. */
5295 label = create_artificial_label (loc);
5297 /* If there was an error processing the switch condition, bail now
5298 before we get more confused. */
5299 if (!cond || cond == error_mark_node)
5300 goto error_out;
5302 if ((low_value && TREE_TYPE (low_value)
5303 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5304 || (high_value && TREE_TYPE (high_value)
5305 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5307 error_at (loc, "pointers are not permitted as case values");
5308 goto error_out;
5311 /* Case ranges are a GNU extension. */
5312 if (high_value)
5313 pedwarn (loc, OPT_pedantic,
5314 "range expressions in switch statements are non-standard");
5316 type = TREE_TYPE (cond);
5317 if (low_value)
5319 low_value = check_case_value (low_value);
5320 low_value = convert_and_check (type, low_value);
5321 if (low_value == error_mark_node)
5322 goto error_out;
5324 if (high_value)
5326 high_value = check_case_value (high_value);
5327 high_value = convert_and_check (type, high_value);
5328 if (high_value == error_mark_node)
5329 goto error_out;
5332 if (low_value && high_value)
5334 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5335 really a case range, even though it was written that way.
5336 Remove the HIGH_VALUE to simplify later processing. */
5337 if (tree_int_cst_equal (low_value, high_value))
5338 high_value = NULL_TREE;
5339 else if (!tree_int_cst_lt (low_value, high_value))
5340 warning_at (loc, 0, "empty range specified");
5343 /* See if the case is in range of the type of the original testing
5344 expression. If both low_value and high_value are out of range,
5345 don't insert the case label and return NULL_TREE. */
5346 if (low_value
5347 && !check_case_bounds (type, orig_type,
5348 &low_value, high_value ? &high_value : NULL))
5349 return NULL_TREE;
5351 /* Look up the LOW_VALUE in the table of case labels we already
5352 have. */
5353 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5354 /* If there was not an exact match, check for overlapping ranges.
5355 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5356 that's a `default' label and the only overlap is an exact match. */
5357 if (!node && (low_value || high_value))
5359 splay_tree_node low_bound;
5360 splay_tree_node high_bound;
5362 /* Even though there wasn't an exact match, there might be an
5363 overlap between this case range and another case range.
5364 Since we've (inductively) not allowed any overlapping case
5365 ranges, we simply need to find the greatest low case label
5366 that is smaller that LOW_VALUE, and the smallest low case
5367 label that is greater than LOW_VALUE. If there is an overlap
5368 it will occur in one of these two ranges. */
5369 low_bound = splay_tree_predecessor (cases,
5370 (splay_tree_key) low_value);
5371 high_bound = splay_tree_successor (cases,
5372 (splay_tree_key) low_value);
5374 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5375 the LOW_VALUE, so there is no need to check unless the
5376 LOW_BOUND is in fact itself a case range. */
5377 if (low_bound
5378 && CASE_HIGH ((tree) low_bound->value)
5379 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5380 low_value) >= 0)
5381 node = low_bound;
5382 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5383 range is bigger than the low end of the current range, so we
5384 are only interested if the current range is a real range, and
5385 not an ordinary case label. */
5386 else if (high_bound
5387 && high_value
5388 && (tree_int_cst_compare ((tree) high_bound->key,
5389 high_value)
5390 <= 0))
5391 node = high_bound;
5393 /* If there was an overlap, issue an error. */
5394 if (node)
5396 tree duplicate = CASE_LABEL ((tree) node->value);
5398 if (high_value)
5400 error_at (loc, "duplicate (or overlapping) case value");
5401 error_at (DECL_SOURCE_LOCATION (duplicate),
5402 "this is the first entry overlapping that value");
5404 else if (low_value)
5406 error_at (loc, "duplicate case value") ;
5407 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5409 else
5411 error_at (loc, "multiple default labels in one switch");
5412 error_at (DECL_SOURCE_LOCATION (duplicate),
5413 "this is the first default label");
5415 goto error_out;
5418 /* Add a CASE_LABEL to the statement-tree. */
5419 case_label = add_stmt (build_case_label (low_value, high_value, label));
5420 /* Register this case label in the splay tree. */
5421 splay_tree_insert (cases,
5422 (splay_tree_key) low_value,
5423 (splay_tree_value) case_label);
5425 return case_label;
5427 error_out:
5428 /* Add a label so that the back-end doesn't think that the beginning of
5429 the switch is unreachable. Note that we do not add a case label, as
5430 that just leads to duplicates and thence to failure later on. */
5431 if (!cases->root)
5433 tree t = create_artificial_label (loc);
5434 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5436 return error_mark_node;
5439 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5440 Used to verify that case values match up with enumerator values. */
5442 static void
5443 match_case_to_enum_1 (tree key, tree type, tree label)
5445 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5447 /* ??? Not working too hard to print the double-word value.
5448 Should perhaps be done with %lwd in the diagnostic routines? */
5449 if (TREE_INT_CST_HIGH (key) == 0)
5450 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5451 TREE_INT_CST_LOW (key));
5452 else if (!TYPE_UNSIGNED (type)
5453 && TREE_INT_CST_HIGH (key) == -1
5454 && TREE_INT_CST_LOW (key) != 0)
5455 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5456 -TREE_INT_CST_LOW (key));
5457 else
5458 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5459 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5460 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5462 if (TYPE_NAME (type) == 0)
5463 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5464 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5465 "case value %qs not in enumerated type",
5466 buf);
5467 else
5468 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5469 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5470 "case value %qs not in enumerated type %qT",
5471 buf, type);
5474 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5475 Used to verify that case values match up with enumerator values. */
5477 static int
5478 match_case_to_enum (splay_tree_node node, void *data)
5480 tree label = (tree) node->value;
5481 tree type = (tree) data;
5483 /* Skip default case. */
5484 if (!CASE_LOW (label))
5485 return 0;
5487 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5488 when we did our enum->case scan. Reset our scratch bit after. */
5489 if (!CASE_LOW_SEEN (label))
5490 match_case_to_enum_1 (CASE_LOW (label), type, label);
5491 else
5492 CASE_LOW_SEEN (label) = 0;
5494 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
5495 not set, that means that CASE_HIGH did not appear when we did our
5496 enum->case scan. Reset our scratch bit after. */
5497 if (CASE_HIGH (label))
5499 if (!CASE_HIGH_SEEN (label))
5500 match_case_to_enum_1 (CASE_HIGH (label), type, label);
5501 else
5502 CASE_HIGH_SEEN (label) = 0;
5505 return 0;
5508 /* Handle -Wswitch*. Called from the front end after parsing the
5509 switch construct. */
5510 /* ??? Should probably be somewhere generic, since other languages
5511 besides C and C++ would want this. At the moment, however, C/C++
5512 are the only tree-ssa languages that support enumerations at all,
5513 so the point is moot. */
5515 void
5516 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5517 tree type, tree cond)
5519 splay_tree_node default_node;
5520 splay_tree_node node;
5521 tree chain;
5523 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5524 return;
5526 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5527 if (!default_node)
5528 warning_at (switch_location, OPT_Wswitch_default,
5529 "switch missing default case");
5531 /* From here on, we only care about about enumerated types. */
5532 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5533 return;
5535 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
5536 if (!warn_switch_enum && !warn_switch)
5537 return;
5539 /* Check the cases. Warn about case values which are not members of
5540 the enumerated type. For -Wswitch-enum, or for -Wswitch when
5541 there is no default case, check that exactly all enumeration
5542 literals are covered by the cases. */
5544 /* Clearing COND if it is not an integer constant simplifies
5545 the tests inside the loop below. */
5546 if (TREE_CODE (cond) != INTEGER_CST)
5547 cond = NULL_TREE;
5549 /* The time complexity here is O(N*lg(N)) worst case, but for the
5550 common case of monotonically increasing enumerators, it is
5551 O(N), since the nature of the splay tree will keep the next
5552 element adjacent to the root at all times. */
5554 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5556 tree value = TREE_VALUE (chain);
5557 if (TREE_CODE (value) == CONST_DECL)
5558 value = DECL_INITIAL (value);
5559 node = splay_tree_lookup (cases, (splay_tree_key) value);
5560 if (node)
5562 /* Mark the CASE_LOW part of the case entry as seen. */
5563 tree label = (tree) node->value;
5564 CASE_LOW_SEEN (label) = 1;
5565 continue;
5568 /* Even though there wasn't an exact match, there might be a
5569 case range which includes the enumerator's value. */
5570 node = splay_tree_predecessor (cases, (splay_tree_key) value);
5571 if (node && CASE_HIGH ((tree) node->value))
5573 tree label = (tree) node->value;
5574 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
5575 if (cmp >= 0)
5577 /* If we match the upper bound exactly, mark the CASE_HIGH
5578 part of the case entry as seen. */
5579 if (cmp == 0)
5580 CASE_HIGH_SEEN (label) = 1;
5581 continue;
5585 /* We've now determined that this enumerated literal isn't
5586 handled by the case labels of the switch statement. */
5588 /* If the switch expression is a constant, we only really care
5589 about whether that constant is handled by the switch. */
5590 if (cond && tree_int_cst_compare (cond, value))
5591 continue;
5593 /* If there is a default_node, the only relevant option is
5594 Wswitch-enum. Otherwise, if both are enabled then we prefer
5595 to warn using -Wswitch because -Wswitch is enabled by -Wall
5596 while -Wswitch-enum is explicit. */
5597 warning_at (switch_location,
5598 (default_node || !warn_switch
5599 ? OPT_Wswitch_enum
5600 : OPT_Wswitch),
5601 "enumeration value %qE not handled in switch",
5602 TREE_PURPOSE (chain));
5605 /* Warn if there are case expressions that don't correspond to
5606 enumerators. This can occur since C and C++ don't enforce
5607 type-checking of assignments to enumeration variables.
5609 The time complexity here is now always O(N) worst case, since
5610 we should have marked both the lower bound and upper bound of
5611 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
5612 above. This scan also resets those fields. */
5614 splay_tree_foreach (cases, match_case_to_enum, type);
5617 /* Finish an expression taking the address of LABEL (an
5618 IDENTIFIER_NODE). Returns an expression for the address.
5620 LOC is the location for the expression returned. */
5622 tree
5623 finish_label_address_expr (tree label, location_t loc)
5625 tree result;
5627 pedwarn (input_location, OPT_pedantic, "taking the address of a label is non-standard");
5629 if (label == error_mark_node)
5630 return error_mark_node;
5632 label = lookup_label (label);
5633 if (label == NULL_TREE)
5634 result = null_pointer_node;
5635 else
5637 TREE_USED (label) = 1;
5638 result = build1 (ADDR_EXPR, ptr_type_node, label);
5639 /* The current function is not necessarily uninlinable.
5640 Computed gotos are incompatible with inlining, but the value
5641 here could be used only in a diagnostic, for example. */
5642 protected_set_expr_location (result, loc);
5645 return result;
5649 /* Given a boolean expression ARG, return a tree representing an increment
5650 or decrement (as indicated by CODE) of ARG. The front end must check for
5651 invalid cases (e.g., decrement in C++). */
5652 tree
5653 boolean_increment (enum tree_code code, tree arg)
5655 tree val;
5656 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5658 arg = stabilize_reference (arg);
5659 switch (code)
5661 case PREINCREMENT_EXPR:
5662 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5663 break;
5664 case POSTINCREMENT_EXPR:
5665 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5666 arg = save_expr (arg);
5667 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5668 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5669 break;
5670 case PREDECREMENT_EXPR:
5671 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5672 invert_truthvalue_loc (input_location, arg));
5673 break;
5674 case POSTDECREMENT_EXPR:
5675 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5676 invert_truthvalue_loc (input_location, arg));
5677 arg = save_expr (arg);
5678 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5679 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5680 break;
5681 default:
5682 gcc_unreachable ();
5684 TREE_SIDE_EFFECTS (val) = 1;
5685 return val;
5688 /* Built-in macros for stddef.h and stdint.h, that require macros
5689 defined in this file. */
5690 void
5691 c_stddef_cpp_builtins(void)
5693 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5694 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5695 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5696 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5697 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5698 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5699 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5700 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5701 if (SIG_ATOMIC_TYPE)
5702 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5703 if (INT8_TYPE)
5704 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5705 if (INT16_TYPE)
5706 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5707 if (INT32_TYPE)
5708 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5709 if (INT64_TYPE)
5710 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5711 if (UINT8_TYPE)
5712 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5713 if (UINT16_TYPE)
5714 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5715 if (UINT32_TYPE)
5716 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5717 if (UINT64_TYPE)
5718 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5719 if (INT_LEAST8_TYPE)
5720 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5721 if (INT_LEAST16_TYPE)
5722 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5723 if (INT_LEAST32_TYPE)
5724 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5725 if (INT_LEAST64_TYPE)
5726 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5727 if (UINT_LEAST8_TYPE)
5728 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5729 if (UINT_LEAST16_TYPE)
5730 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5731 if (UINT_LEAST32_TYPE)
5732 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5733 if (UINT_LEAST64_TYPE)
5734 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5735 if (INT_FAST8_TYPE)
5736 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5737 if (INT_FAST16_TYPE)
5738 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5739 if (INT_FAST32_TYPE)
5740 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5741 if (INT_FAST64_TYPE)
5742 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5743 if (UINT_FAST8_TYPE)
5744 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5745 if (UINT_FAST16_TYPE)
5746 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5747 if (UINT_FAST32_TYPE)
5748 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5749 if (UINT_FAST64_TYPE)
5750 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5751 if (INTPTR_TYPE)
5752 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5753 if (UINTPTR_TYPE)
5754 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5757 static void
5758 c_init_attributes (void)
5760 /* Fill in the built_in_attributes array. */
5761 #define DEF_ATTR_NULL_TREE(ENUM) \
5762 built_in_attributes[(int) ENUM] = NULL_TREE;
5763 #define DEF_ATTR_INT(ENUM, VALUE) \
5764 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5765 #define DEF_ATTR_IDENT(ENUM, STRING) \
5766 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5767 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5768 built_in_attributes[(int) ENUM] \
5769 = tree_cons (built_in_attributes[(int) PURPOSE], \
5770 built_in_attributes[(int) VALUE], \
5771 built_in_attributes[(int) CHAIN]);
5772 #include "builtin-attrs.def"
5773 #undef DEF_ATTR_NULL_TREE
5774 #undef DEF_ATTR_INT
5775 #undef DEF_ATTR_IDENT
5776 #undef DEF_ATTR_TREE_LIST
5779 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
5780 identifier as an argument, so the front end shouldn't look it up. */
5782 bool
5783 attribute_takes_identifier_p (const_tree attr_id)
5785 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
5786 if (spec == NULL)
5787 /* Unknown attribute that we'll end up ignoring, return true so we
5788 don't complain about an identifier argument. */
5789 return true;
5790 else if (!strcmp ("mode", spec->name)
5791 || !strcmp ("format", spec->name)
5792 || !strcmp ("cleanup", spec->name))
5793 return true;
5794 else
5795 return targetm.attribute_takes_identifier_p (attr_id);
5798 /* Attribute handlers common to C front ends. */
5800 /* Handle a "packed" attribute; arguments as in
5801 struct attribute_spec.handler. */
5803 static tree
5804 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5805 int flags, bool *no_add_attrs)
5807 if (TYPE_P (*node))
5809 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5810 *node = build_variant_type_copy (*node);
5811 TYPE_PACKED (*node) = 1;
5813 else if (TREE_CODE (*node) == FIELD_DECL)
5815 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
5816 /* Still pack bitfields. */
5817 && ! DECL_INITIAL (*node))
5818 warning (OPT_Wattributes,
5819 "%qE attribute ignored for field of type %qT",
5820 name, TREE_TYPE (*node));
5821 else
5822 DECL_PACKED (*node) = 1;
5824 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
5825 used for DECL_REGISTER. It wouldn't mean anything anyway.
5826 We can't set DECL_PACKED on the type of a TYPE_DECL, because
5827 that changes what the typedef is typing. */
5828 else
5830 warning (OPT_Wattributes, "%qE attribute ignored", name);
5831 *no_add_attrs = true;
5834 return NULL_TREE;
5837 /* Handle a "nocommon" attribute; arguments as in
5838 struct attribute_spec.handler. */
5840 static tree
5841 handle_nocommon_attribute (tree *node, tree name,
5842 tree ARG_UNUSED (args),
5843 int ARG_UNUSED (flags), bool *no_add_attrs)
5845 if (TREE_CODE (*node) == VAR_DECL)
5846 DECL_COMMON (*node) = 0;
5847 else
5849 warning (OPT_Wattributes, "%qE attribute ignored", name);
5850 *no_add_attrs = true;
5853 return NULL_TREE;
5856 /* Handle a "common" attribute; arguments as in
5857 struct attribute_spec.handler. */
5859 static tree
5860 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5861 int ARG_UNUSED (flags), bool *no_add_attrs)
5863 if (TREE_CODE (*node) == VAR_DECL)
5864 DECL_COMMON (*node) = 1;
5865 else
5867 warning (OPT_Wattributes, "%qE attribute ignored", name);
5868 *no_add_attrs = true;
5871 return NULL_TREE;
5874 /* Handle a "noreturn" attribute; arguments as in
5875 struct attribute_spec.handler. */
5877 static tree
5878 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5879 int ARG_UNUSED (flags), bool *no_add_attrs)
5881 tree type = TREE_TYPE (*node);
5883 /* See FIXME comment in c_common_attribute_table. */
5884 if (TREE_CODE (*node) == FUNCTION_DECL
5885 || objc_method_decl (TREE_CODE (*node)))
5886 TREE_THIS_VOLATILE (*node) = 1;
5887 else if (TREE_CODE (type) == POINTER_TYPE
5888 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5889 TREE_TYPE (*node)
5890 = build_pointer_type
5891 (build_type_variant (TREE_TYPE (type),
5892 TYPE_READONLY (TREE_TYPE (type)), 1));
5893 else
5895 warning (OPT_Wattributes, "%qE attribute ignored", name);
5896 *no_add_attrs = true;
5899 return NULL_TREE;
5902 /* Handle a "hot" and attribute; arguments as in
5903 struct attribute_spec.handler. */
5905 static tree
5906 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5907 int ARG_UNUSED (flags), bool *no_add_attrs)
5909 if (TREE_CODE (*node) == FUNCTION_DECL)
5911 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
5913 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5914 name, "cold");
5915 *no_add_attrs = true;
5917 /* Most of the rest of the hot processing is done later with
5918 lookup_attribute. */
5920 else
5922 warning (OPT_Wattributes, "%qE attribute ignored", name);
5923 *no_add_attrs = true;
5926 return NULL_TREE;
5928 /* Handle a "cold" and attribute; arguments as in
5929 struct attribute_spec.handler. */
5931 static tree
5932 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5933 int ARG_UNUSED (flags), bool *no_add_attrs)
5935 if (TREE_CODE (*node) == FUNCTION_DECL)
5937 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
5939 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5940 name, "hot");
5941 *no_add_attrs = true;
5943 /* Most of the rest of the cold processing is done later with
5944 lookup_attribute. */
5946 else
5948 warning (OPT_Wattributes, "%qE attribute ignored", name);
5949 *no_add_attrs = true;
5952 return NULL_TREE;
5955 /* Handle a "noinline" attribute; arguments as in
5956 struct attribute_spec.handler. */
5958 static tree
5959 handle_noinline_attribute (tree *node, tree name,
5960 tree ARG_UNUSED (args),
5961 int ARG_UNUSED (flags), bool *no_add_attrs)
5963 if (TREE_CODE (*node) == FUNCTION_DECL)
5964 DECL_UNINLINABLE (*node) = 1;
5965 else
5967 warning (OPT_Wattributes, "%qE attribute ignored", name);
5968 *no_add_attrs = true;
5971 return NULL_TREE;
5974 /* Handle a "noclone" attribute; arguments as in
5975 struct attribute_spec.handler. */
5977 static tree
5978 handle_noclone_attribute (tree *node, tree name,
5979 tree ARG_UNUSED (args),
5980 int ARG_UNUSED (flags), bool *no_add_attrs)
5982 if (TREE_CODE (*node) != FUNCTION_DECL)
5984 warning (OPT_Wattributes, "%qE attribute ignored", name);
5985 *no_add_attrs = true;
5988 return NULL_TREE;
5991 /* Handle a "always_inline" attribute; arguments as in
5992 struct attribute_spec.handler. */
5994 static tree
5995 handle_always_inline_attribute (tree *node, tree name,
5996 tree ARG_UNUSED (args),
5997 int ARG_UNUSED (flags),
5998 bool *no_add_attrs)
6000 if (TREE_CODE (*node) == FUNCTION_DECL)
6002 /* Set the attribute and mark it for disregarding inline
6003 limits. */
6004 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6006 else
6008 warning (OPT_Wattributes, "%qE attribute ignored", name);
6009 *no_add_attrs = true;
6012 return NULL_TREE;
6015 /* Handle a "gnu_inline" attribute; arguments as in
6016 struct attribute_spec.handler. */
6018 static tree
6019 handle_gnu_inline_attribute (tree *node, tree name,
6020 tree ARG_UNUSED (args),
6021 int ARG_UNUSED (flags),
6022 bool *no_add_attrs)
6024 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6026 /* Do nothing else, just set the attribute. We'll get at
6027 it later with lookup_attribute. */
6029 else
6031 warning (OPT_Wattributes, "%qE attribute ignored", name);
6032 *no_add_attrs = true;
6035 return NULL_TREE;
6038 /* Handle a "leaf" attribute; arguments as in
6039 struct attribute_spec.handler. */
6041 static tree
6042 handle_leaf_attribute (tree *node, tree name,
6043 tree ARG_UNUSED (args),
6044 int ARG_UNUSED (flags), bool *no_add_attrs)
6046 if (TREE_CODE (*node) != FUNCTION_DECL)
6048 warning (OPT_Wattributes, "%qE attribute ignored", name);
6049 *no_add_attrs = true;
6051 if (!TREE_PUBLIC (*node))
6053 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6054 *no_add_attrs = true;
6057 return NULL_TREE;
6060 /* Handle an "artificial" attribute; arguments as in
6061 struct attribute_spec.handler. */
6063 static tree
6064 handle_artificial_attribute (tree *node, tree name,
6065 tree ARG_UNUSED (args),
6066 int ARG_UNUSED (flags),
6067 bool *no_add_attrs)
6069 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6071 /* Do nothing else, just set the attribute. We'll get at
6072 it later with lookup_attribute. */
6074 else
6076 warning (OPT_Wattributes, "%qE attribute ignored", name);
6077 *no_add_attrs = true;
6080 return NULL_TREE;
6083 /* Handle a "flatten" attribute; arguments as in
6084 struct attribute_spec.handler. */
6086 static tree
6087 handle_flatten_attribute (tree *node, tree name,
6088 tree args ATTRIBUTE_UNUSED,
6089 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6091 if (TREE_CODE (*node) == FUNCTION_DECL)
6092 /* Do nothing else, just set the attribute. We'll get at
6093 it later with lookup_attribute. */
6095 else
6097 warning (OPT_Wattributes, "%qE attribute ignored", name);
6098 *no_add_attrs = true;
6101 return NULL_TREE;
6104 /* Handle a "warning" or "error" attribute; arguments as in
6105 struct attribute_spec.handler. */
6107 static tree
6108 handle_error_attribute (tree *node, tree name, tree args,
6109 int ARG_UNUSED (flags), bool *no_add_attrs)
6111 if (TREE_CODE (*node) == FUNCTION_DECL
6112 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6113 /* Do nothing else, just set the attribute. We'll get at
6114 it later with lookup_attribute. */
6116 else
6118 warning (OPT_Wattributes, "%qE attribute ignored", name);
6119 *no_add_attrs = true;
6122 return NULL_TREE;
6125 /* Handle a "used" attribute; arguments as in
6126 struct attribute_spec.handler. */
6128 static tree
6129 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6130 int ARG_UNUSED (flags), bool *no_add_attrs)
6132 tree node = *pnode;
6134 if (TREE_CODE (node) == FUNCTION_DECL
6135 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6136 || (TREE_CODE (node) == TYPE_DECL))
6138 TREE_USED (node) = 1;
6139 DECL_PRESERVE_P (node) = 1;
6140 if (TREE_CODE (node) == VAR_DECL)
6141 DECL_READ_P (node) = 1;
6143 else
6145 warning (OPT_Wattributes, "%qE attribute ignored", name);
6146 *no_add_attrs = true;
6149 return NULL_TREE;
6152 /* Handle a "unused" attribute; arguments as in
6153 struct attribute_spec.handler. */
6155 static tree
6156 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6157 int flags, bool *no_add_attrs)
6159 if (DECL_P (*node))
6161 tree decl = *node;
6163 if (TREE_CODE (decl) == PARM_DECL
6164 || TREE_CODE (decl) == VAR_DECL
6165 || TREE_CODE (decl) == FUNCTION_DECL
6166 || TREE_CODE (decl) == LABEL_DECL
6167 || TREE_CODE (decl) == TYPE_DECL)
6169 TREE_USED (decl) = 1;
6170 if (TREE_CODE (decl) == VAR_DECL
6171 || TREE_CODE (decl) == PARM_DECL)
6172 DECL_READ_P (decl) = 1;
6174 else
6176 warning (OPT_Wattributes, "%qE attribute ignored", name);
6177 *no_add_attrs = true;
6180 else
6182 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6183 *node = build_variant_type_copy (*node);
6184 TREE_USED (*node) = 1;
6187 return NULL_TREE;
6190 /* Handle a "externally_visible" attribute; arguments as in
6191 struct attribute_spec.handler. */
6193 static tree
6194 handle_externally_visible_attribute (tree *pnode, tree name,
6195 tree ARG_UNUSED (args),
6196 int ARG_UNUSED (flags),
6197 bool *no_add_attrs)
6199 tree node = *pnode;
6201 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6203 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6204 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6206 warning (OPT_Wattributes,
6207 "%qE attribute have effect only on public objects", name);
6208 *no_add_attrs = true;
6211 else
6213 warning (OPT_Wattributes, "%qE attribute ignored", name);
6214 *no_add_attrs = true;
6217 return NULL_TREE;
6220 /* Handle a "const" attribute; arguments as in
6221 struct attribute_spec.handler. */
6223 static tree
6224 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6225 int ARG_UNUSED (flags), bool *no_add_attrs)
6227 tree type = TREE_TYPE (*node);
6229 /* See FIXME comment on noreturn in c_common_attribute_table. */
6230 if (TREE_CODE (*node) == FUNCTION_DECL)
6231 TREE_READONLY (*node) = 1;
6232 else if (TREE_CODE (type) == POINTER_TYPE
6233 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6234 TREE_TYPE (*node)
6235 = build_pointer_type
6236 (build_type_variant (TREE_TYPE (type), 1,
6237 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6238 else
6240 warning (OPT_Wattributes, "%qE attribute ignored", name);
6241 *no_add_attrs = true;
6244 return NULL_TREE;
6247 /* Handle a "transparent_union" attribute; arguments as in
6248 struct attribute_spec.handler. */
6250 static tree
6251 handle_transparent_union_attribute (tree *node, tree name,
6252 tree ARG_UNUSED (args), int flags,
6253 bool *no_add_attrs)
6255 tree type;
6257 *no_add_attrs = true;
6259 if (TREE_CODE (*node) == TYPE_DECL)
6260 node = &TREE_TYPE (*node);
6261 type = *node;
6263 if (TREE_CODE (type) == UNION_TYPE)
6265 /* When IN_PLACE is set, leave the check for FIELDS and MODE to
6266 the code in finish_struct. */
6267 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6269 if (TYPE_FIELDS (type) == NULL_TREE
6270 || c_dialect_cxx ()
6271 || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type)))
6272 goto ignored;
6274 /* A type variant isn't good enough, since we don't a cast
6275 to such a type removed as a no-op. */
6276 *node = type = build_duplicate_type (type);
6279 TYPE_TRANSPARENT_AGGR (type) = 1;
6280 return NULL_TREE;
6283 ignored:
6284 warning (OPT_Wattributes, "%qE attribute ignored", name);
6285 return NULL_TREE;
6288 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6289 get the requested priority for a constructor or destructor,
6290 possibly issuing diagnostics for invalid or reserved
6291 priorities. */
6293 static priority_type
6294 get_priority (tree args, bool is_destructor)
6296 HOST_WIDE_INT pri;
6297 tree arg;
6299 if (!args)
6300 return DEFAULT_INIT_PRIORITY;
6302 if (!SUPPORTS_INIT_PRIORITY)
6304 if (is_destructor)
6305 error ("destructor priorities are not supported");
6306 else
6307 error ("constructor priorities are not supported");
6308 return DEFAULT_INIT_PRIORITY;
6311 arg = TREE_VALUE (args);
6312 if (!host_integerp (arg, /*pos=*/0)
6313 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6314 goto invalid;
6316 pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0);
6317 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6318 goto invalid;
6320 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6322 if (is_destructor)
6323 warning (0,
6324 "destructor priorities from 0 to %d are reserved "
6325 "for the implementation",
6326 MAX_RESERVED_INIT_PRIORITY);
6327 else
6328 warning (0,
6329 "constructor priorities from 0 to %d are reserved "
6330 "for the implementation",
6331 MAX_RESERVED_INIT_PRIORITY);
6333 return pri;
6335 invalid:
6336 if (is_destructor)
6337 error ("destructor priorities must be integers from 0 to %d inclusive",
6338 MAX_INIT_PRIORITY);
6339 else
6340 error ("constructor priorities must be integers from 0 to %d inclusive",
6341 MAX_INIT_PRIORITY);
6342 return DEFAULT_INIT_PRIORITY;
6345 /* Handle a "constructor" attribute; arguments as in
6346 struct attribute_spec.handler. */
6348 static tree
6349 handle_constructor_attribute (tree *node, tree name, tree args,
6350 int ARG_UNUSED (flags),
6351 bool *no_add_attrs)
6353 tree decl = *node;
6354 tree type = TREE_TYPE (decl);
6356 if (TREE_CODE (decl) == FUNCTION_DECL
6357 && TREE_CODE (type) == FUNCTION_TYPE
6358 && decl_function_context (decl) == 0)
6360 priority_type priority;
6361 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6362 priority = get_priority (args, /*is_destructor=*/false);
6363 SET_DECL_INIT_PRIORITY (decl, priority);
6364 TREE_USED (decl) = 1;
6366 else
6368 warning (OPT_Wattributes, "%qE attribute ignored", name);
6369 *no_add_attrs = true;
6372 return NULL_TREE;
6375 /* Handle a "destructor" attribute; arguments as in
6376 struct attribute_spec.handler. */
6378 static tree
6379 handle_destructor_attribute (tree *node, tree name, tree args,
6380 int ARG_UNUSED (flags),
6381 bool *no_add_attrs)
6383 tree decl = *node;
6384 tree type = TREE_TYPE (decl);
6386 if (TREE_CODE (decl) == FUNCTION_DECL
6387 && TREE_CODE (type) == FUNCTION_TYPE
6388 && decl_function_context (decl) == 0)
6390 priority_type priority;
6391 DECL_STATIC_DESTRUCTOR (decl) = 1;
6392 priority = get_priority (args, /*is_destructor=*/true);
6393 SET_DECL_FINI_PRIORITY (decl, priority);
6394 TREE_USED (decl) = 1;
6396 else
6398 warning (OPT_Wattributes, "%qE attribute ignored", name);
6399 *no_add_attrs = true;
6402 return NULL_TREE;
6405 /* Nonzero if the mode is a valid vector mode for this architecture.
6406 This returns nonzero even if there is no hardware support for the
6407 vector mode, but we can emulate with narrower modes. */
6409 static int
6410 vector_mode_valid_p (enum machine_mode mode)
6412 enum mode_class mclass = GET_MODE_CLASS (mode);
6413 enum machine_mode innermode;
6415 /* Doh! What's going on? */
6416 if (mclass != MODE_VECTOR_INT
6417 && mclass != MODE_VECTOR_FLOAT
6418 && mclass != MODE_VECTOR_FRACT
6419 && mclass != MODE_VECTOR_UFRACT
6420 && mclass != MODE_VECTOR_ACCUM
6421 && mclass != MODE_VECTOR_UACCUM)
6422 return 0;
6424 /* Hardware support. Woo hoo! */
6425 if (targetm.vector_mode_supported_p (mode))
6426 return 1;
6428 innermode = GET_MODE_INNER (mode);
6430 /* We should probably return 1 if requesting V4DI and we have no DI,
6431 but we have V2DI, but this is probably very unlikely. */
6433 /* If we have support for the inner mode, we can safely emulate it.
6434 We may not have V2DI, but me can emulate with a pair of DIs. */
6435 return targetm.scalar_mode_supported_p (innermode);
6439 /* Handle a "mode" attribute; arguments as in
6440 struct attribute_spec.handler. */
6442 static tree
6443 handle_mode_attribute (tree *node, tree name, tree args,
6444 int ARG_UNUSED (flags), bool *no_add_attrs)
6446 tree type = *node;
6447 tree ident = TREE_VALUE (args);
6449 *no_add_attrs = true;
6451 if (TREE_CODE (ident) != IDENTIFIER_NODE)
6452 warning (OPT_Wattributes, "%qE attribute ignored", name);
6453 else
6455 int j;
6456 const char *p = IDENTIFIER_POINTER (ident);
6457 int len = strlen (p);
6458 enum machine_mode mode = VOIDmode;
6459 tree typefm;
6460 bool valid_mode;
6462 if (len > 4 && p[0] == '_' && p[1] == '_'
6463 && p[len - 1] == '_' && p[len - 2] == '_')
6465 char *newp = (char *) alloca (len - 1);
6467 strcpy (newp, &p[2]);
6468 newp[len - 4] = '\0';
6469 p = newp;
6472 /* Change this type to have a type with the specified mode.
6473 First check for the special modes. */
6474 if (!strcmp (p, "byte"))
6475 mode = byte_mode;
6476 else if (!strcmp (p, "word"))
6477 mode = word_mode;
6478 else if (!strcmp (p, "pointer"))
6479 mode = ptr_mode;
6480 else if (!strcmp (p, "libgcc_cmp_return"))
6481 mode = targetm.libgcc_cmp_return_mode ();
6482 else if (!strcmp (p, "libgcc_shift_count"))
6483 mode = targetm.libgcc_shift_count_mode ();
6484 else if (!strcmp (p, "unwind_word"))
6485 mode = targetm.unwind_word_mode ();
6486 else
6487 for (j = 0; j < NUM_MACHINE_MODES; j++)
6488 if (!strcmp (p, GET_MODE_NAME (j)))
6490 mode = (enum machine_mode) j;
6491 break;
6494 if (mode == VOIDmode)
6496 error ("unknown machine mode %qE", ident);
6497 return NULL_TREE;
6500 valid_mode = false;
6501 switch (GET_MODE_CLASS (mode))
6503 case MODE_INT:
6504 case MODE_PARTIAL_INT:
6505 case MODE_FLOAT:
6506 case MODE_DECIMAL_FLOAT:
6507 case MODE_FRACT:
6508 case MODE_UFRACT:
6509 case MODE_ACCUM:
6510 case MODE_UACCUM:
6511 valid_mode = targetm.scalar_mode_supported_p (mode);
6512 break;
6514 case MODE_COMPLEX_INT:
6515 case MODE_COMPLEX_FLOAT:
6516 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
6517 break;
6519 case MODE_VECTOR_INT:
6520 case MODE_VECTOR_FLOAT:
6521 case MODE_VECTOR_FRACT:
6522 case MODE_VECTOR_UFRACT:
6523 case MODE_VECTOR_ACCUM:
6524 case MODE_VECTOR_UACCUM:
6525 warning (OPT_Wattributes, "specifying vector types with "
6526 "__attribute__ ((mode)) is deprecated");
6527 warning (OPT_Wattributes,
6528 "use __attribute__ ((vector_size)) instead");
6529 valid_mode = vector_mode_valid_p (mode);
6530 break;
6532 default:
6533 break;
6535 if (!valid_mode)
6537 error ("unable to emulate %qs", p);
6538 return NULL_TREE;
6541 if (POINTER_TYPE_P (type))
6543 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
6544 tree (*fn)(tree, enum machine_mode, bool);
6546 if (!targetm.addr_space.valid_pointer_mode (mode, as))
6548 error ("invalid pointer mode %qs", p);
6549 return NULL_TREE;
6552 if (TREE_CODE (type) == POINTER_TYPE)
6553 fn = build_pointer_type_for_mode;
6554 else
6555 fn = build_reference_type_for_mode;
6556 typefm = fn (TREE_TYPE (type), mode, false);
6558 else
6560 /* For fixed-point modes, we need to test if the signness of type
6561 and the machine mode are consistent. */
6562 if (ALL_FIXED_POINT_MODE_P (mode)
6563 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
6565 error ("signedness of type and machine mode %qs don%'t match", p);
6566 return NULL_TREE;
6568 /* For fixed-point modes, we need to pass saturating info. */
6569 typefm = lang_hooks.types.type_for_mode (mode,
6570 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
6571 : TYPE_UNSIGNED (type));
6574 if (typefm == NULL_TREE)
6576 error ("no data type for mode %qs", p);
6577 return NULL_TREE;
6579 else if (TREE_CODE (type) == ENUMERAL_TYPE)
6581 /* For enumeral types, copy the precision from the integer
6582 type returned above. If not an INTEGER_TYPE, we can't use
6583 this mode for this type. */
6584 if (TREE_CODE (typefm) != INTEGER_TYPE)
6586 error ("cannot use mode %qs for enumeral types", p);
6587 return NULL_TREE;
6590 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
6592 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
6593 typefm = type;
6595 else
6597 /* We cannot build a type variant, as there's code that assumes
6598 that TYPE_MAIN_VARIANT has the same mode. This includes the
6599 debug generators. Instead, create a subrange type. This
6600 results in all of the enumeral values being emitted only once
6601 in the original, and the subtype gets them by reference. */
6602 if (TYPE_UNSIGNED (type))
6603 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
6604 else
6605 typefm = make_signed_type (TYPE_PRECISION (typefm));
6606 TREE_TYPE (typefm) = type;
6609 else if (VECTOR_MODE_P (mode)
6610 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
6611 : TREE_CODE (type) != TREE_CODE (typefm))
6613 error ("mode %qs applied to inappropriate type", p);
6614 return NULL_TREE;
6617 *node = typefm;
6620 return NULL_TREE;
6623 /* Handle a "section" attribute; arguments as in
6624 struct attribute_spec.handler. */
6626 static tree
6627 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6628 int ARG_UNUSED (flags), bool *no_add_attrs)
6630 tree decl = *node;
6632 if (targetm_common.have_named_sections)
6634 user_defined_section_attribute = true;
6636 if ((TREE_CODE (decl) == FUNCTION_DECL
6637 || TREE_CODE (decl) == VAR_DECL)
6638 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6640 if (TREE_CODE (decl) == VAR_DECL
6641 && current_function_decl != NULL_TREE
6642 && !TREE_STATIC (decl))
6644 error_at (DECL_SOURCE_LOCATION (decl),
6645 "section attribute cannot be specified for "
6646 "local variables");
6647 *no_add_attrs = true;
6650 /* The decl may have already been given a section attribute
6651 from a previous declaration. Ensure they match. */
6652 else if (DECL_SECTION_NAME (decl) != NULL_TREE
6653 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
6654 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
6656 error ("section of %q+D conflicts with previous declaration",
6657 *node);
6658 *no_add_attrs = true;
6660 else if (TREE_CODE (decl) == VAR_DECL
6661 && !targetm.have_tls && targetm.emutls.tmpl_section
6662 && DECL_THREAD_LOCAL_P (decl))
6664 error ("section of %q+D cannot be overridden", *node);
6665 *no_add_attrs = true;
6667 else
6668 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
6670 else
6672 error ("section attribute not allowed for %q+D", *node);
6673 *no_add_attrs = true;
6676 else
6678 error_at (DECL_SOURCE_LOCATION (*node),
6679 "section attributes are not supported for this target");
6680 *no_add_attrs = true;
6683 return NULL_TREE;
6686 /* Check whether ALIGN is a valid user-specified alignment. If so,
6687 return its base-2 log; if not, output an error and return -1. If
6688 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
6689 no error. */
6691 check_user_alignment (const_tree align, bool allow_zero)
6693 int i;
6695 if (!INTEGRAL_TYPE_P (TREE_TYPE (align))
6696 || TREE_CODE (align) != INTEGER_CST)
6698 error ("requested alignment is not an integer constant");
6699 return -1;
6701 else if (allow_zero && integer_zerop (align))
6702 return -1;
6703 else if ((i = tree_log2 (align)) == -1)
6705 error ("requested alignment is not a power of 2");
6706 return -1;
6708 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
6710 error ("requested alignment is too large");
6711 return -1;
6713 return i;
6716 /* Handle a "aligned" attribute; arguments as in
6717 struct attribute_spec.handler. */
6719 static tree
6720 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6721 int flags, bool *no_add_attrs)
6723 tree decl = NULL_TREE;
6724 tree *type = NULL;
6725 int is_type = 0;
6726 tree align_expr = (args ? TREE_VALUE (args)
6727 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
6728 int i;
6730 if (DECL_P (*node))
6732 decl = *node;
6733 type = &TREE_TYPE (decl);
6734 is_type = TREE_CODE (*node) == TYPE_DECL;
6736 else if (TYPE_P (*node))
6737 type = node, is_type = 1;
6739 if ((i = check_user_alignment (align_expr, false)) == -1)
6740 *no_add_attrs = true;
6741 else if (is_type)
6743 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6744 /* OK, modify the type in place. */;
6745 /* If we have a TYPE_DECL, then copy the type, so that we
6746 don't accidentally modify a builtin type. See pushdecl. */
6747 else if (decl && TREE_TYPE (decl) != error_mark_node
6748 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
6750 tree tt = TREE_TYPE (decl);
6751 *type = build_variant_type_copy (*type);
6752 DECL_ORIGINAL_TYPE (decl) = tt;
6753 TYPE_NAME (*type) = decl;
6754 TREE_USED (*type) = TREE_USED (decl);
6755 TREE_TYPE (decl) = *type;
6757 else
6758 *type = build_variant_type_copy (*type);
6760 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
6761 TYPE_USER_ALIGN (*type) = 1;
6763 else if (! VAR_OR_FUNCTION_DECL_P (decl)
6764 && TREE_CODE (decl) != FIELD_DECL)
6766 error ("alignment may not be specified for %q+D", decl);
6767 *no_add_attrs = true;
6769 else if (TREE_CODE (decl) == FUNCTION_DECL
6770 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
6772 if (DECL_USER_ALIGN (decl))
6773 error ("alignment for %q+D was previously specified as %d "
6774 "and may not be decreased", decl,
6775 DECL_ALIGN (decl) / BITS_PER_UNIT);
6776 else
6777 error ("alignment for %q+D must be at least %d", decl,
6778 DECL_ALIGN (decl) / BITS_PER_UNIT);
6779 *no_add_attrs = true;
6781 else
6783 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
6784 DECL_USER_ALIGN (decl) = 1;
6787 return NULL_TREE;
6790 /* Handle a "weak" attribute; arguments as in
6791 struct attribute_spec.handler. */
6793 static tree
6794 handle_weak_attribute (tree *node, tree name,
6795 tree ARG_UNUSED (args),
6796 int ARG_UNUSED (flags),
6797 bool * ARG_UNUSED (no_add_attrs))
6799 if (TREE_CODE (*node) == FUNCTION_DECL
6800 && DECL_DECLARED_INLINE_P (*node))
6802 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6803 *no_add_attrs = true;
6805 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6807 error ("indirect function %q+D cannot be declared weak", *node);
6808 *no_add_attrs = true;
6809 return NULL_TREE;
6811 else if (TREE_CODE (*node) == FUNCTION_DECL
6812 || TREE_CODE (*node) == VAR_DECL)
6813 declare_weak (*node);
6814 else
6815 warning (OPT_Wattributes, "%qE attribute ignored", name);
6817 return NULL_TREE;
6820 /* Handle an "alias" or "ifunc" attribute; arguments as in
6821 struct attribute_spec.handler, except that IS_ALIAS tells us
6822 whether this is an alias as opposed to ifunc attribute. */
6824 static tree
6825 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
6826 bool *no_add_attrs)
6828 tree decl = *node;
6830 if (TREE_CODE (decl) != FUNCTION_DECL
6831 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
6833 warning (OPT_Wattributes, "%qE attribute ignored", name);
6834 *no_add_attrs = true;
6836 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
6837 || (TREE_CODE (decl) != FUNCTION_DECL
6838 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
6839 /* A static variable declaration is always a tentative definition,
6840 but the alias is a non-tentative definition which overrides. */
6841 || (TREE_CODE (decl) != FUNCTION_DECL
6842 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
6844 error ("%q+D defined both normally and as %qE attribute", decl, name);
6845 *no_add_attrs = true;
6846 return NULL_TREE;
6848 else if (!is_alias
6849 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
6850 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
6852 error ("weak %q+D cannot be defined %qE", decl, name);
6853 *no_add_attrs = true;
6854 return NULL_TREE;
6857 /* Note that the very first time we process a nested declaration,
6858 decl_function_context will not be set. Indeed, *would* never
6859 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
6860 we do below. After such frobbery, pushdecl would set the context.
6861 In any case, this is never what we want. */
6862 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
6864 tree id;
6866 id = TREE_VALUE (args);
6867 if (TREE_CODE (id) != STRING_CST)
6869 error ("attribute %qE argument not a string", name);
6870 *no_add_attrs = true;
6871 return NULL_TREE;
6873 id = get_identifier (TREE_STRING_POINTER (id));
6874 /* This counts as a use of the object pointed to. */
6875 TREE_USED (id) = 1;
6877 if (TREE_CODE (decl) == FUNCTION_DECL)
6878 DECL_INITIAL (decl) = error_mark_node;
6879 else
6881 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6882 DECL_EXTERNAL (decl) = 1;
6883 else
6884 DECL_EXTERNAL (decl) = 0;
6885 TREE_STATIC (decl) = 1;
6888 if (!is_alias)
6889 /* ifuncs are also aliases, so set that attribute too. */
6890 DECL_ATTRIBUTES (decl)
6891 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
6893 else
6895 warning (OPT_Wattributes, "%qE attribute ignored", name);
6896 *no_add_attrs = true;
6899 return NULL_TREE;
6902 /* Handle an "alias" or "ifunc" attribute; arguments as in
6903 struct attribute_spec.handler. */
6905 static tree
6906 handle_ifunc_attribute (tree *node, tree name, tree args,
6907 int ARG_UNUSED (flags), bool *no_add_attrs)
6909 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
6912 /* Handle an "alias" or "ifunc" attribute; arguments as in
6913 struct attribute_spec.handler. */
6915 static tree
6916 handle_alias_attribute (tree *node, tree name, tree args,
6917 int ARG_UNUSED (flags), bool *no_add_attrs)
6919 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
6922 /* Handle a "weakref" attribute; arguments as in struct
6923 attribute_spec.handler. */
6925 static tree
6926 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6927 int flags, bool *no_add_attrs)
6929 tree attr = NULL_TREE;
6931 /* We must ignore the attribute when it is associated with
6932 local-scoped decls, since attribute alias is ignored and many
6933 such symbols do not even have a DECL_WEAK field. */
6934 if (decl_function_context (*node)
6935 || current_function_decl
6936 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
6938 warning (OPT_Wattributes, "%qE attribute ignored", name);
6939 *no_add_attrs = true;
6940 return NULL_TREE;
6943 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6945 error ("indirect function %q+D cannot be declared weakref", *node);
6946 *no_add_attrs = true;
6947 return NULL_TREE;
6950 /* The idea here is that `weakref("name")' mutates into `weakref,
6951 alias("name")', and weakref without arguments, in turn,
6952 implicitly adds weak. */
6954 if (args)
6956 attr = tree_cons (get_identifier ("alias"), args, attr);
6957 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
6959 *no_add_attrs = true;
6961 decl_attributes (node, attr, flags);
6963 else
6965 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
6966 error_at (DECL_SOURCE_LOCATION (*node),
6967 "weakref attribute must appear before alias attribute");
6969 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
6970 and that isn't supported; and because it wants to add it to
6971 the list of weak decls, which isn't helpful. */
6972 DECL_WEAK (*node) = 1;
6975 return NULL_TREE;
6978 /* Handle an "visibility" attribute; arguments as in
6979 struct attribute_spec.handler. */
6981 static tree
6982 handle_visibility_attribute (tree *node, tree name, tree args,
6983 int ARG_UNUSED (flags),
6984 bool *ARG_UNUSED (no_add_attrs))
6986 tree decl = *node;
6987 tree id = TREE_VALUE (args);
6988 enum symbol_visibility vis;
6990 if (TYPE_P (*node))
6992 if (TREE_CODE (*node) == ENUMERAL_TYPE)
6993 /* OK */;
6994 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
6996 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
6997 name);
6998 return NULL_TREE;
7000 else if (TYPE_FIELDS (*node))
7002 error ("%qE attribute ignored because %qT is already defined",
7003 name, *node);
7004 return NULL_TREE;
7007 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7009 warning (OPT_Wattributes, "%qE attribute ignored", name);
7010 return NULL_TREE;
7013 if (TREE_CODE (id) != STRING_CST)
7015 error ("visibility argument not a string");
7016 return NULL_TREE;
7019 /* If this is a type, set the visibility on the type decl. */
7020 if (TYPE_P (decl))
7022 decl = TYPE_NAME (decl);
7023 if (!decl)
7024 return NULL_TREE;
7025 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7027 warning (OPT_Wattributes, "%qE attribute ignored on types",
7028 name);
7029 return NULL_TREE;
7033 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7034 vis = VISIBILITY_DEFAULT;
7035 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7036 vis = VISIBILITY_INTERNAL;
7037 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7038 vis = VISIBILITY_HIDDEN;
7039 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7040 vis = VISIBILITY_PROTECTED;
7041 else
7043 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7044 vis = VISIBILITY_DEFAULT;
7047 if (DECL_VISIBILITY_SPECIFIED (decl)
7048 && vis != DECL_VISIBILITY (decl))
7050 tree attributes = (TYPE_P (*node)
7051 ? TYPE_ATTRIBUTES (*node)
7052 : DECL_ATTRIBUTES (decl));
7053 if (lookup_attribute ("visibility", attributes))
7054 error ("%qD redeclared with different visibility", decl);
7055 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7056 && lookup_attribute ("dllimport", attributes))
7057 error ("%qD was declared %qs which implies default visibility",
7058 decl, "dllimport");
7059 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7060 && lookup_attribute ("dllexport", attributes))
7061 error ("%qD was declared %qs which implies default visibility",
7062 decl, "dllexport");
7065 DECL_VISIBILITY (decl) = vis;
7066 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7068 /* Go ahead and attach the attribute to the node as well. This is needed
7069 so we can determine whether we have VISIBILITY_DEFAULT because the
7070 visibility was not specified, or because it was explicitly overridden
7071 from the containing scope. */
7073 return NULL_TREE;
7076 /* Determine the ELF symbol visibility for DECL, which is either a
7077 variable or a function. It is an error to use this function if a
7078 definition of DECL is not available in this translation unit.
7079 Returns true if the final visibility has been determined by this
7080 function; false if the caller is free to make additional
7081 modifications. */
7083 bool
7084 c_determine_visibility (tree decl)
7086 gcc_assert (TREE_CODE (decl) == VAR_DECL
7087 || TREE_CODE (decl) == FUNCTION_DECL);
7089 /* If the user explicitly specified the visibility with an
7090 attribute, honor that. DECL_VISIBILITY will have been set during
7091 the processing of the attribute. We check for an explicit
7092 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7093 to distinguish the use of an attribute from the use of a "#pragma
7094 GCC visibility push(...)"; in the latter case we still want other
7095 considerations to be able to overrule the #pragma. */
7096 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7097 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7098 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7099 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7100 return true;
7102 /* Set default visibility to whatever the user supplied with
7103 visibility_specified depending on #pragma GCC visibility. */
7104 if (!DECL_VISIBILITY_SPECIFIED (decl))
7106 if (visibility_options.inpragma
7107 || DECL_VISIBILITY (decl) != default_visibility)
7109 DECL_VISIBILITY (decl) = default_visibility;
7110 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7111 /* If visibility changed and DECL already has DECL_RTL, ensure
7112 symbol flags are updated. */
7113 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7114 || TREE_CODE (decl) == FUNCTION_DECL)
7115 && DECL_RTL_SET_P (decl))
7116 make_decl_rtl (decl);
7119 return false;
7122 /* Handle an "tls_model" attribute; arguments as in
7123 struct attribute_spec.handler. */
7125 static tree
7126 handle_tls_model_attribute (tree *node, tree name, tree args,
7127 int ARG_UNUSED (flags), bool *no_add_attrs)
7129 tree id;
7130 tree decl = *node;
7131 enum tls_model kind;
7133 *no_add_attrs = true;
7135 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7137 warning (OPT_Wattributes, "%qE attribute ignored", name);
7138 return NULL_TREE;
7141 kind = DECL_TLS_MODEL (decl);
7142 id = TREE_VALUE (args);
7143 if (TREE_CODE (id) != STRING_CST)
7145 error ("tls_model argument not a string");
7146 return NULL_TREE;
7149 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7150 kind = TLS_MODEL_LOCAL_EXEC;
7151 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7152 kind = TLS_MODEL_INITIAL_EXEC;
7153 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7154 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7155 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7156 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7157 else
7158 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7160 DECL_TLS_MODEL (decl) = kind;
7161 return NULL_TREE;
7164 /* Handle a "no_instrument_function" attribute; arguments as in
7165 struct attribute_spec.handler. */
7167 static tree
7168 handle_no_instrument_function_attribute (tree *node, tree name,
7169 tree ARG_UNUSED (args),
7170 int ARG_UNUSED (flags),
7171 bool *no_add_attrs)
7173 tree decl = *node;
7175 if (TREE_CODE (decl) != FUNCTION_DECL)
7177 error_at (DECL_SOURCE_LOCATION (decl),
7178 "%qE attribute applies only to functions", name);
7179 *no_add_attrs = true;
7181 else if (DECL_INITIAL (decl))
7183 error_at (DECL_SOURCE_LOCATION (decl),
7184 "can%'t set %qE attribute after definition", name);
7185 *no_add_attrs = true;
7187 else
7188 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7190 return NULL_TREE;
7193 /* Handle a "malloc" attribute; arguments as in
7194 struct attribute_spec.handler. */
7196 static tree
7197 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7198 int ARG_UNUSED (flags), bool *no_add_attrs)
7200 if (TREE_CODE (*node) == FUNCTION_DECL
7201 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7202 DECL_IS_MALLOC (*node) = 1;
7203 else
7205 warning (OPT_Wattributes, "%qE attribute ignored", name);
7206 *no_add_attrs = true;
7209 return NULL_TREE;
7212 /* Handle a "alloc_size" attribute; arguments as in
7213 struct attribute_spec.handler. */
7215 static tree
7216 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7217 int ARG_UNUSED (flags), bool *no_add_attrs)
7219 unsigned arg_count = type_num_arguments (*node);
7220 for (; args; args = TREE_CHAIN (args))
7222 tree position = TREE_VALUE (args);
7224 if (TREE_CODE (position) != INTEGER_CST
7225 || TREE_INT_CST_HIGH (position)
7226 || TREE_INT_CST_LOW (position) < 1
7227 || TREE_INT_CST_LOW (position) > arg_count )
7229 warning (OPT_Wattributes,
7230 "alloc_size parameter outside range");
7231 *no_add_attrs = true;
7232 return NULL_TREE;
7235 return NULL_TREE;
7238 /* Handle a "fn spec" attribute; arguments as in
7239 struct attribute_spec.handler. */
7241 static tree
7242 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7243 tree args, int ARG_UNUSED (flags),
7244 bool *no_add_attrs ATTRIBUTE_UNUSED)
7246 gcc_assert (args
7247 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7248 && !TREE_CHAIN (args));
7249 return NULL_TREE;
7252 /* Handle a "returns_twice" attribute; arguments as in
7253 struct attribute_spec.handler. */
7255 static tree
7256 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7257 int ARG_UNUSED (flags), bool *no_add_attrs)
7259 if (TREE_CODE (*node) == FUNCTION_DECL)
7260 DECL_IS_RETURNS_TWICE (*node) = 1;
7261 else
7263 warning (OPT_Wattributes, "%qE attribute ignored", name);
7264 *no_add_attrs = true;
7267 return NULL_TREE;
7270 /* Handle a "no_limit_stack" attribute; arguments as in
7271 struct attribute_spec.handler. */
7273 static tree
7274 handle_no_limit_stack_attribute (tree *node, tree name,
7275 tree ARG_UNUSED (args),
7276 int ARG_UNUSED (flags),
7277 bool *no_add_attrs)
7279 tree decl = *node;
7281 if (TREE_CODE (decl) != FUNCTION_DECL)
7283 error_at (DECL_SOURCE_LOCATION (decl),
7284 "%qE attribute applies only to functions", name);
7285 *no_add_attrs = true;
7287 else if (DECL_INITIAL (decl))
7289 error_at (DECL_SOURCE_LOCATION (decl),
7290 "can%'t set %qE attribute after definition", name);
7291 *no_add_attrs = true;
7293 else
7294 DECL_NO_LIMIT_STACK (decl) = 1;
7296 return NULL_TREE;
7299 /* Handle a "pure" attribute; arguments as in
7300 struct attribute_spec.handler. */
7302 static tree
7303 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7304 int ARG_UNUSED (flags), bool *no_add_attrs)
7306 if (TREE_CODE (*node) == FUNCTION_DECL)
7307 DECL_PURE_P (*node) = 1;
7308 /* ??? TODO: Support types. */
7309 else
7311 warning (OPT_Wattributes, "%qE attribute ignored", name);
7312 *no_add_attrs = true;
7315 return NULL_TREE;
7318 /* Handle a "no vops" attribute; arguments as in
7319 struct attribute_spec.handler. */
7321 static tree
7322 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
7323 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7324 bool *ARG_UNUSED (no_add_attrs))
7326 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
7327 DECL_IS_NOVOPS (*node) = 1;
7328 return NULL_TREE;
7331 /* Handle a "deprecated" attribute; arguments as in
7332 struct attribute_spec.handler. */
7334 static tree
7335 handle_deprecated_attribute (tree *node, tree name,
7336 tree args, int flags,
7337 bool *no_add_attrs)
7339 tree type = NULL_TREE;
7340 int warn = 0;
7341 tree what = NULL_TREE;
7343 if (!args)
7344 *no_add_attrs = true;
7345 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7347 error ("deprecated message is not a string");
7348 *no_add_attrs = true;
7351 if (DECL_P (*node))
7353 tree decl = *node;
7354 type = TREE_TYPE (decl);
7356 if (TREE_CODE (decl) == TYPE_DECL
7357 || TREE_CODE (decl) == PARM_DECL
7358 || TREE_CODE (decl) == VAR_DECL
7359 || TREE_CODE (decl) == FUNCTION_DECL
7360 || TREE_CODE (decl) == FIELD_DECL
7361 || objc_method_decl (TREE_CODE (decl)))
7362 TREE_DEPRECATED (decl) = 1;
7363 else
7364 warn = 1;
7366 else if (TYPE_P (*node))
7368 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7369 *node = build_variant_type_copy (*node);
7370 TREE_DEPRECATED (*node) = 1;
7371 type = *node;
7373 else
7374 warn = 1;
7376 if (warn)
7378 *no_add_attrs = true;
7379 if (type && TYPE_NAME (type))
7381 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
7382 what = TYPE_NAME (*node);
7383 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7384 && DECL_NAME (TYPE_NAME (type)))
7385 what = DECL_NAME (TYPE_NAME (type));
7387 if (what)
7388 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
7389 else
7390 warning (OPT_Wattributes, "%qE attribute ignored", name);
7393 return NULL_TREE;
7396 /* Handle a "vector_size" attribute; arguments as in
7397 struct attribute_spec.handler. */
7399 static tree
7400 handle_vector_size_attribute (tree *node, tree name, tree args,
7401 int ARG_UNUSED (flags),
7402 bool *no_add_attrs)
7404 unsigned HOST_WIDE_INT vecsize, nunits;
7405 enum machine_mode orig_mode;
7406 tree type = *node, new_type, size;
7408 *no_add_attrs = true;
7410 size = TREE_VALUE (args);
7412 if (!host_integerp (size, 1))
7414 warning (OPT_Wattributes, "%qE attribute ignored", name);
7415 return NULL_TREE;
7418 /* Get the vector size (in bytes). */
7419 vecsize = tree_low_cst (size, 1);
7421 /* We need to provide for vector pointers, vector arrays, and
7422 functions returning vectors. For example:
7424 __attribute__((vector_size(16))) short *foo;
7426 In this case, the mode is SI, but the type being modified is
7427 HI, so we need to look further. */
7429 while (POINTER_TYPE_P (type)
7430 || TREE_CODE (type) == FUNCTION_TYPE
7431 || TREE_CODE (type) == METHOD_TYPE
7432 || TREE_CODE (type) == ARRAY_TYPE
7433 || TREE_CODE (type) == OFFSET_TYPE)
7434 type = TREE_TYPE (type);
7436 /* Get the mode of the type being modified. */
7437 orig_mode = TYPE_MODE (type);
7439 if ((!INTEGRAL_TYPE_P (type)
7440 && !SCALAR_FLOAT_TYPE_P (type)
7441 && !FIXED_POINT_TYPE_P (type))
7442 || (!SCALAR_FLOAT_MODE_P (orig_mode)
7443 && GET_MODE_CLASS (orig_mode) != MODE_INT
7444 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
7445 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
7446 || TREE_CODE (type) == BOOLEAN_TYPE)
7448 error ("invalid vector type for attribute %qE", name);
7449 return NULL_TREE;
7452 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
7454 error ("vector size not an integral multiple of component size");
7455 return NULL;
7458 if (vecsize == 0)
7460 error ("zero vector size");
7461 return NULL;
7464 /* Calculate how many units fit in the vector. */
7465 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
7466 if (nunits & (nunits - 1))
7468 error ("number of components of the vector not a power of two");
7469 return NULL_TREE;
7472 new_type = build_vector_type (type, nunits);
7474 /* Build back pointers if needed. */
7475 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
7477 return NULL_TREE;
7480 /* Handle the "nonnull" attribute. */
7481 static tree
7482 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
7483 tree args, int ARG_UNUSED (flags),
7484 bool *no_add_attrs)
7486 tree type = *node;
7487 unsigned HOST_WIDE_INT attr_arg_num;
7489 /* If no arguments are specified, all pointer arguments should be
7490 non-null. Verify a full prototype is given so that the arguments
7491 will have the correct types when we actually check them later. */
7492 if (!args)
7494 if (!prototype_p (type))
7496 error ("nonnull attribute without arguments on a non-prototype");
7497 *no_add_attrs = true;
7499 return NULL_TREE;
7502 /* Argument list specified. Verify that each argument number references
7503 a pointer argument. */
7504 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
7506 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
7508 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
7510 error ("nonnull argument has invalid operand number (argument %lu)",
7511 (unsigned long) attr_arg_num);
7512 *no_add_attrs = true;
7513 return NULL_TREE;
7516 if (prototype_p (type))
7518 function_args_iterator iter;
7519 tree argument;
7521 function_args_iter_init (&iter, type);
7522 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
7524 argument = function_args_iter_cond (&iter);
7525 if (argument == NULL_TREE || ck_num == arg_num)
7526 break;
7529 if (!argument
7530 || TREE_CODE (argument) == VOID_TYPE)
7532 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
7533 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7534 *no_add_attrs = true;
7535 return NULL_TREE;
7538 if (TREE_CODE (argument) != POINTER_TYPE)
7540 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
7541 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7542 *no_add_attrs = true;
7543 return NULL_TREE;
7548 return NULL_TREE;
7551 /* Check the argument list of a function call for null in argument slots
7552 that are marked as requiring a non-null pointer argument. The NARGS
7553 arguments are passed in the array ARGARRAY.
7556 static void
7557 check_function_nonnull (tree attrs, int nargs, tree *argarray)
7559 tree a, args;
7560 int i;
7562 for (a = attrs; a; a = TREE_CHAIN (a))
7564 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
7566 args = TREE_VALUE (a);
7568 /* Walk the argument list. If we encounter an argument number we
7569 should check for non-null, do it. If the attribute has no args,
7570 then every pointer argument is checked (in which case the check
7571 for pointer type is done in check_nonnull_arg). */
7572 for (i = 0; i < nargs; i++)
7574 if (!args || nonnull_check_p (args, i + 1))
7575 check_function_arguments_recurse (check_nonnull_arg, NULL,
7576 argarray[i],
7577 i + 1);
7583 /* Check that the Nth argument of a function call (counting backwards
7584 from the end) is a (pointer)0. The NARGS arguments are passed in the
7585 array ARGARRAY. */
7587 static void
7588 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
7590 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
7592 if (attr)
7594 int len = 0;
7595 int pos = 0;
7596 tree sentinel;
7597 function_args_iterator iter;
7598 tree t;
7600 /* Skip over the named arguments. */
7601 FOREACH_FUNCTION_ARGS (fntype, t, iter)
7603 if (len == nargs)
7604 break;
7605 len++;
7608 if (TREE_VALUE (attr))
7610 tree p = TREE_VALUE (TREE_VALUE (attr));
7611 pos = TREE_INT_CST_LOW (p);
7614 /* The sentinel must be one of the varargs, i.e.
7615 in position >= the number of fixed arguments. */
7616 if ((nargs - 1 - pos) < len)
7618 warning (OPT_Wformat,
7619 "not enough variable arguments to fit a sentinel");
7620 return;
7623 /* Validate the sentinel. */
7624 sentinel = argarray[nargs - 1 - pos];
7625 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
7626 || !integer_zerop (sentinel))
7627 /* Although __null (in C++) is only an integer we allow it
7628 nevertheless, as we are guaranteed that it's exactly
7629 as wide as a pointer, and we don't want to force
7630 users to cast the NULL they have written there.
7631 We warn with -Wstrict-null-sentinel, though. */
7632 && (warn_strict_null_sentinel || null_node != sentinel))
7633 warning (OPT_Wformat, "missing sentinel in function call");
7637 /* Helper for check_function_nonnull; given a list of operands which
7638 must be non-null in ARGS, determine if operand PARAM_NUM should be
7639 checked. */
7641 static bool
7642 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
7644 unsigned HOST_WIDE_INT arg_num = 0;
7646 for (; args; args = TREE_CHAIN (args))
7648 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
7650 gcc_assert (found);
7652 if (arg_num == param_num)
7653 return true;
7655 return false;
7658 /* Check that the function argument PARAM (which is operand number
7659 PARAM_NUM) is non-null. This is called by check_function_nonnull
7660 via check_function_arguments_recurse. */
7662 static void
7663 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
7664 unsigned HOST_WIDE_INT param_num)
7666 /* Just skip checking the argument if it's not a pointer. This can
7667 happen if the "nonnull" attribute was given without an operand
7668 list (which means to check every pointer argument). */
7670 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
7671 return;
7673 if (integer_zerop (param))
7674 warning (OPT_Wnonnull, "null argument where non-null required "
7675 "(argument %lu)", (unsigned long) param_num);
7678 /* Helper for nonnull attribute handling; fetch the operand number
7679 from the attribute argument list. */
7681 static bool
7682 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
7684 /* Verify the arg number is a constant. */
7685 if (TREE_CODE (arg_num_expr) != INTEGER_CST
7686 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
7687 return false;
7689 *valp = TREE_INT_CST_LOW (arg_num_expr);
7690 return true;
7693 /* Handle a "nothrow" attribute; arguments as in
7694 struct attribute_spec.handler. */
7696 static tree
7697 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7698 int ARG_UNUSED (flags), bool *no_add_attrs)
7700 if (TREE_CODE (*node) == FUNCTION_DECL)
7701 TREE_NOTHROW (*node) = 1;
7702 /* ??? TODO: Support types. */
7703 else
7705 warning (OPT_Wattributes, "%qE attribute ignored", name);
7706 *no_add_attrs = true;
7709 return NULL_TREE;
7712 /* Handle a "cleanup" attribute; arguments as in
7713 struct attribute_spec.handler. */
7715 static tree
7716 handle_cleanup_attribute (tree *node, tree name, tree args,
7717 int ARG_UNUSED (flags), bool *no_add_attrs)
7719 tree decl = *node;
7720 tree cleanup_id, cleanup_decl;
7722 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
7723 for global destructors in C++. This requires infrastructure that
7724 we don't have generically at the moment. It's also not a feature
7725 we'd be missing too much, since we do have attribute constructor. */
7726 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
7728 warning (OPT_Wattributes, "%qE attribute ignored", name);
7729 *no_add_attrs = true;
7730 return NULL_TREE;
7733 /* Verify that the argument is a function in scope. */
7734 /* ??? We could support pointers to functions here as well, if
7735 that was considered desirable. */
7736 cleanup_id = TREE_VALUE (args);
7737 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
7739 error ("cleanup argument not an identifier");
7740 *no_add_attrs = true;
7741 return NULL_TREE;
7743 cleanup_decl = lookup_name (cleanup_id);
7744 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
7746 error ("cleanup argument not a function");
7747 *no_add_attrs = true;
7748 return NULL_TREE;
7751 /* That the function has proper type is checked with the
7752 eventual call to build_function_call. */
7754 return NULL_TREE;
7757 /* Handle a "warn_unused_result" attribute. No special handling. */
7759 static tree
7760 handle_warn_unused_result_attribute (tree *node, tree name,
7761 tree ARG_UNUSED (args),
7762 int ARG_UNUSED (flags), bool *no_add_attrs)
7764 /* Ignore the attribute for functions not returning any value. */
7765 if (VOID_TYPE_P (TREE_TYPE (*node)))
7767 warning (OPT_Wattributes, "%qE attribute ignored", name);
7768 *no_add_attrs = true;
7771 return NULL_TREE;
7774 /* Handle a "sentinel" attribute. */
7776 static tree
7777 handle_sentinel_attribute (tree *node, tree name, tree args,
7778 int ARG_UNUSED (flags), bool *no_add_attrs)
7780 if (!prototype_p (*node))
7782 warning (OPT_Wattributes,
7783 "%qE attribute requires prototypes with named arguments", name);
7784 *no_add_attrs = true;
7786 else
7788 if (!stdarg_p (*node))
7790 warning (OPT_Wattributes,
7791 "%qE attribute only applies to variadic functions", name);
7792 *no_add_attrs = true;
7796 if (args)
7798 tree position = TREE_VALUE (args);
7800 if (TREE_CODE (position) != INTEGER_CST)
7802 warning (OPT_Wattributes,
7803 "requested position is not an integer constant");
7804 *no_add_attrs = true;
7806 else
7808 if (tree_int_cst_lt (position, integer_zero_node))
7810 warning (OPT_Wattributes,
7811 "requested position is less than zero");
7812 *no_add_attrs = true;
7817 return NULL_TREE;
7820 /* Handle a "type_generic" attribute. */
7822 static tree
7823 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
7824 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7825 bool * ARG_UNUSED (no_add_attrs))
7827 /* Ensure we have a function type. */
7828 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
7830 /* Ensure we have a variadic function. */
7831 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
7833 return NULL_TREE;
7836 /* Handle a "target" attribute. */
7838 static tree
7839 handle_target_attribute (tree *node, tree name, tree args, int flags,
7840 bool *no_add_attrs)
7842 /* Ensure we have a function type. */
7843 if (TREE_CODE (*node) != FUNCTION_DECL)
7845 warning (OPT_Wattributes, "%qE attribute ignored", name);
7846 *no_add_attrs = true;
7848 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
7849 flags))
7850 *no_add_attrs = true;
7852 return NULL_TREE;
7855 /* Arguments being collected for optimization. */
7856 typedef const char *const_char_p; /* For DEF_VEC_P. */
7857 DEF_VEC_P(const_char_p);
7858 DEF_VEC_ALLOC_P(const_char_p, gc);
7859 static GTY(()) VEC(const_char_p, gc) *optimize_args;
7862 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
7863 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
7864 false for #pragma GCC optimize. */
7866 bool
7867 parse_optimize_options (tree args, bool attr_p)
7869 bool ret = true;
7870 unsigned opt_argc;
7871 unsigned i;
7872 int saved_flag_strict_aliasing;
7873 const char **opt_argv;
7874 struct cl_decoded_option *decoded_options;
7875 unsigned int decoded_options_count;
7876 tree ap;
7878 /* Build up argv vector. Just in case the string is stored away, use garbage
7879 collected strings. */
7880 VEC_truncate (const_char_p, optimize_args, 0);
7881 VEC_safe_push (const_char_p, gc, optimize_args, NULL);
7883 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
7885 tree value = TREE_VALUE (ap);
7887 if (TREE_CODE (value) == INTEGER_CST)
7889 char buffer[20];
7890 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
7891 VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
7894 else if (TREE_CODE (value) == STRING_CST)
7896 /* Split string into multiple substrings. */
7897 size_t len = TREE_STRING_LENGTH (value);
7898 char *p = ASTRDUP (TREE_STRING_POINTER (value));
7899 char *end = p + len;
7900 char *comma;
7901 char *next_p = p;
7903 while (next_p != NULL)
7905 size_t len2;
7906 char *q, *r;
7908 p = next_p;
7909 comma = strchr (p, ',');
7910 if (comma)
7912 len2 = comma - p;
7913 *comma = '\0';
7914 next_p = comma+1;
7916 else
7918 len2 = end - p;
7919 next_p = NULL;
7922 r = q = (char *) ggc_alloc_atomic (len2 + 3);
7924 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
7925 options. */
7926 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
7928 ret = false;
7929 if (attr_p)
7930 warning (OPT_Wattributes,
7931 "bad option %s to optimize attribute", p);
7932 else
7933 warning (OPT_Wpragmas,
7934 "bad option %s to pragma attribute", p);
7935 continue;
7938 if (*p != '-')
7940 *r++ = '-';
7942 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
7943 itself is -Os, and any other switch begins with a -f. */
7944 if ((*p >= '0' && *p <= '9')
7945 || (p[0] == 's' && p[1] == '\0'))
7946 *r++ = 'O';
7947 else if (*p != 'O')
7948 *r++ = 'f';
7951 memcpy (r, p, len2);
7952 r[len2] = '\0';
7953 VEC_safe_push (const_char_p, gc, optimize_args, q);
7959 opt_argc = VEC_length (const_char_p, optimize_args);
7960 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
7962 for (i = 1; i < opt_argc; i++)
7963 opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
7965 saved_flag_strict_aliasing = flag_strict_aliasing;
7967 /* Now parse the options. */
7968 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
7969 &decoded_options,
7970 &decoded_options_count);
7971 decode_options (&global_options, &global_options_set,
7972 decoded_options, decoded_options_count,
7973 input_location, global_dc);
7975 targetm.override_options_after_change();
7977 /* Don't allow changing -fstrict-aliasing. */
7978 flag_strict_aliasing = saved_flag_strict_aliasing;
7980 VEC_truncate (const_char_p, optimize_args, 0);
7981 return ret;
7984 /* For handling "optimize" attribute. arguments as in
7985 struct attribute_spec.handler. */
7987 static tree
7988 handle_optimize_attribute (tree *node, tree name, tree args,
7989 int ARG_UNUSED (flags), bool *no_add_attrs)
7991 /* Ensure we have a function type. */
7992 if (TREE_CODE (*node) != FUNCTION_DECL)
7994 warning (OPT_Wattributes, "%qE attribute ignored", name);
7995 *no_add_attrs = true;
7997 else
7999 struct cl_optimization cur_opts;
8000 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8002 /* Save current options. */
8003 cl_optimization_save (&cur_opts, &global_options);
8005 /* If we previously had some optimization options, use them as the
8006 default. */
8007 if (old_opts)
8008 cl_optimization_restore (&global_options,
8009 TREE_OPTIMIZATION (old_opts));
8011 /* Parse options, and update the vector. */
8012 parse_optimize_options (args, true);
8013 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8014 = build_optimization_node ();
8016 /* Restore current options. */
8017 cl_optimization_restore (&global_options, &cur_opts);
8020 return NULL_TREE;
8023 /* Handle a "no_split_stack" attribute. */
8025 static tree
8026 handle_no_split_stack_attribute (tree *node, tree name,
8027 tree ARG_UNUSED (args),
8028 int ARG_UNUSED (flags),
8029 bool *no_add_attrs)
8031 tree decl = *node;
8033 if (TREE_CODE (decl) != FUNCTION_DECL)
8035 error_at (DECL_SOURCE_LOCATION (decl),
8036 "%qE attribute applies only to functions", name);
8037 *no_add_attrs = true;
8039 else if (DECL_INITIAL (decl))
8041 error_at (DECL_SOURCE_LOCATION (decl),
8042 "can%'t set %qE attribute after definition", name);
8043 *no_add_attrs = true;
8046 return NULL_TREE;
8049 /* Check for valid arguments being passed to a function with FNTYPE.
8050 There are NARGS arguments in the array ARGARRAY. */
8051 void
8052 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
8054 /* Check for null being passed in a pointer argument that must be
8055 non-null. We also need to do this if format checking is enabled. */
8057 if (warn_nonnull)
8058 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8060 /* Check for errors in format strings. */
8062 if (warn_format || warn_missing_format_attribute)
8063 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8065 if (warn_format)
8066 check_function_sentinel (fntype, nargs, argarray);
8069 /* Generic argument checking recursion routine. PARAM is the argument to
8070 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
8071 once the argument is resolved. CTX is context for the callback. */
8072 void
8073 check_function_arguments_recurse (void (*callback)
8074 (void *, tree, unsigned HOST_WIDE_INT),
8075 void *ctx, tree param,
8076 unsigned HOST_WIDE_INT param_num)
8078 if (CONVERT_EXPR_P (param)
8079 && (TYPE_PRECISION (TREE_TYPE (param))
8080 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
8082 /* Strip coercion. */
8083 check_function_arguments_recurse (callback, ctx,
8084 TREE_OPERAND (param, 0), param_num);
8085 return;
8088 if (TREE_CODE (param) == CALL_EXPR)
8090 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
8091 tree attrs;
8092 bool found_format_arg = false;
8094 /* See if this is a call to a known internationalization function
8095 that modifies a format arg. Such a function may have multiple
8096 format_arg attributes (for example, ngettext). */
8098 for (attrs = TYPE_ATTRIBUTES (type);
8099 attrs;
8100 attrs = TREE_CHAIN (attrs))
8101 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
8103 tree inner_arg;
8104 tree format_num_expr;
8105 int format_num;
8106 int i;
8107 call_expr_arg_iterator iter;
8109 /* Extract the argument number, which was previously checked
8110 to be valid. */
8111 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
8113 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
8114 && !TREE_INT_CST_HIGH (format_num_expr));
8116 format_num = TREE_INT_CST_LOW (format_num_expr);
8118 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
8119 inner_arg != 0;
8120 inner_arg = next_call_expr_arg (&iter), i++)
8121 if (i == format_num)
8123 check_function_arguments_recurse (callback, ctx,
8124 inner_arg, param_num);
8125 found_format_arg = true;
8126 break;
8130 /* If we found a format_arg attribute and did a recursive check,
8131 we are done with checking this argument. Otherwise, we continue
8132 and this will be considered a non-literal. */
8133 if (found_format_arg)
8134 return;
8137 if (TREE_CODE (param) == COND_EXPR)
8139 /* Check both halves of the conditional expression. */
8140 check_function_arguments_recurse (callback, ctx,
8141 TREE_OPERAND (param, 1), param_num);
8142 check_function_arguments_recurse (callback, ctx,
8143 TREE_OPERAND (param, 2), param_num);
8144 return;
8147 (*callback) (ctx, param, param_num);
8150 /* Checks for a builtin function FNDECL that the number of arguments
8151 NARGS against the required number REQUIRED and issues an error if
8152 there is a mismatch. Returns true if the number of arguments is
8153 correct, otherwise false. */
8155 static bool
8156 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
8158 if (nargs < required)
8160 error_at (input_location,
8161 "not enough arguments to function %qE", fndecl);
8162 return false;
8164 else if (nargs > required)
8166 error_at (input_location,
8167 "too many arguments to function %qE", fndecl);
8168 return false;
8170 return true;
8173 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
8174 Returns false if there was an error, otherwise true. */
8176 bool
8177 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
8179 if (!DECL_BUILT_IN (fndecl)
8180 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
8181 return true;
8183 switch (DECL_FUNCTION_CODE (fndecl))
8185 case BUILT_IN_CONSTANT_P:
8186 return builtin_function_validate_nargs (fndecl, nargs, 1);
8188 case BUILT_IN_ISFINITE:
8189 case BUILT_IN_ISINF:
8190 case BUILT_IN_ISINF_SIGN:
8191 case BUILT_IN_ISNAN:
8192 case BUILT_IN_ISNORMAL:
8193 if (builtin_function_validate_nargs (fndecl, nargs, 1))
8195 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
8197 error ("non-floating-point argument in call to "
8198 "function %qE", fndecl);
8199 return false;
8201 return true;
8203 return false;
8205 case BUILT_IN_ISGREATER:
8206 case BUILT_IN_ISGREATEREQUAL:
8207 case BUILT_IN_ISLESS:
8208 case BUILT_IN_ISLESSEQUAL:
8209 case BUILT_IN_ISLESSGREATER:
8210 case BUILT_IN_ISUNORDERED:
8211 if (builtin_function_validate_nargs (fndecl, nargs, 2))
8213 enum tree_code code0, code1;
8214 code0 = TREE_CODE (TREE_TYPE (args[0]));
8215 code1 = TREE_CODE (TREE_TYPE (args[1]));
8216 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
8217 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8218 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
8220 error ("non-floating-point arguments in call to "
8221 "function %qE", fndecl);
8222 return false;
8224 return true;
8226 return false;
8228 case BUILT_IN_FPCLASSIFY:
8229 if (builtin_function_validate_nargs (fndecl, nargs, 6))
8231 unsigned i;
8233 for (i=0; i<5; i++)
8234 if (TREE_CODE (args[i]) != INTEGER_CST)
8236 error ("non-const integer argument %u in call to function %qE",
8237 i+1, fndecl);
8238 return false;
8241 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
8243 error ("non-floating-point argument in call to function %qE",
8244 fndecl);
8245 return false;
8247 return true;
8249 return false;
8251 case BUILT_IN_ASSUME_ALIGNED:
8252 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
8254 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
8256 error ("non-integer argument 3 in call to function %qE", fndecl);
8257 return false;
8259 return true;
8261 return false;
8263 default:
8264 return true;
8268 /* Function to help qsort sort FIELD_DECLs by name order. */
8271 field_decl_cmp (const void *x_p, const void *y_p)
8273 const tree *const x = (const tree *const) x_p;
8274 const tree *const y = (const tree *const) y_p;
8276 if (DECL_NAME (*x) == DECL_NAME (*y))
8277 /* A nontype is "greater" than a type. */
8278 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8279 if (DECL_NAME (*x) == NULL_TREE)
8280 return -1;
8281 if (DECL_NAME (*y) == NULL_TREE)
8282 return 1;
8283 if (DECL_NAME (*x) < DECL_NAME (*y))
8284 return -1;
8285 return 1;
8288 static struct {
8289 gt_pointer_operator new_value;
8290 void *cookie;
8291 } resort_data;
8293 /* This routine compares two fields like field_decl_cmp but using the
8294 pointer operator in resort_data. */
8296 static int
8297 resort_field_decl_cmp (const void *x_p, const void *y_p)
8299 const tree *const x = (const tree *const) x_p;
8300 const tree *const y = (const tree *const) y_p;
8302 if (DECL_NAME (*x) == DECL_NAME (*y))
8303 /* A nontype is "greater" than a type. */
8304 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8305 if (DECL_NAME (*x) == NULL_TREE)
8306 return -1;
8307 if (DECL_NAME (*y) == NULL_TREE)
8308 return 1;
8310 tree d1 = DECL_NAME (*x);
8311 tree d2 = DECL_NAME (*y);
8312 resort_data.new_value (&d1, resort_data.cookie);
8313 resort_data.new_value (&d2, resort_data.cookie);
8314 if (d1 < d2)
8315 return -1;
8317 return 1;
8320 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8322 void
8323 resort_sorted_fields (void *obj,
8324 void * ARG_UNUSED (orig_obj),
8325 gt_pointer_operator new_value,
8326 void *cookie)
8328 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8329 resort_data.new_value = new_value;
8330 resort_data.cookie = cookie;
8331 qsort (&sf->elts[0], sf->len, sizeof (tree),
8332 resort_field_decl_cmp);
8335 /* Subroutine of c_parse_error.
8336 Return the result of concatenating LHS and RHS. RHS is really
8337 a string literal, its first character is indicated by RHS_START and
8338 RHS_SIZE is its length (including the terminating NUL character).
8340 The caller is responsible for deleting the returned pointer. */
8342 static char *
8343 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
8345 const int lhs_size = strlen (lhs);
8346 char *result = XNEWVEC (char, lhs_size + rhs_size);
8347 strncpy (result, lhs, lhs_size);
8348 strncpy (result + lhs_size, rhs_start, rhs_size);
8349 return result;
8352 /* Issue the error given by GMSGID, indicating that it occurred before
8353 TOKEN, which had the associated VALUE. */
8355 void
8356 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
8357 tree value, unsigned char token_flags)
8359 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
8361 char *message = NULL;
8363 if (token_type == CPP_EOF)
8364 message = catenate_messages (gmsgid, " at end of input");
8365 else if (token_type == CPP_CHAR
8366 || token_type == CPP_WCHAR
8367 || token_type == CPP_CHAR16
8368 || token_type == CPP_CHAR32)
8370 unsigned int val = TREE_INT_CST_LOW (value);
8371 const char *prefix;
8373 switch (token_type)
8375 default:
8376 prefix = "";
8377 break;
8378 case CPP_WCHAR:
8379 prefix = "L";
8380 break;
8381 case CPP_CHAR16:
8382 prefix = "u";
8383 break;
8384 case CPP_CHAR32:
8385 prefix = "U";
8386 break;
8389 if (val <= UCHAR_MAX && ISGRAPH (val))
8390 message = catenate_messages (gmsgid, " before %s'%c'");
8391 else
8392 message = catenate_messages (gmsgid, " before %s'\\x%x'");
8394 error (message, prefix, val);
8395 free (message);
8396 message = NULL;
8398 else if (token_type == CPP_STRING
8399 || token_type == CPP_WSTRING
8400 || token_type == CPP_STRING16
8401 || token_type == CPP_STRING32
8402 || token_type == CPP_UTF8STRING)
8403 message = catenate_messages (gmsgid, " before string constant");
8404 else if (token_type == CPP_NUMBER)
8405 message = catenate_messages (gmsgid, " before numeric constant");
8406 else if (token_type == CPP_NAME)
8408 message = catenate_messages (gmsgid, " before %qE");
8409 error (message, value);
8410 free (message);
8411 message = NULL;
8413 else if (token_type == CPP_PRAGMA)
8414 message = catenate_messages (gmsgid, " before %<#pragma%>");
8415 else if (token_type == CPP_PRAGMA_EOL)
8416 message = catenate_messages (gmsgid, " before end of line");
8417 else if (token_type == CPP_DECLTYPE)
8418 message = catenate_messages (gmsgid, " before %<decltype%>");
8419 else if (token_type < N_TTYPES)
8421 message = catenate_messages (gmsgid, " before %qs token");
8422 error (message, cpp_type2name (token_type, token_flags));
8423 free (message);
8424 message = NULL;
8426 else
8427 error (gmsgid);
8429 if (message)
8431 error (message);
8432 free (message);
8434 #undef catenate_messages
8437 /* Mapping for cpp message reasons to the options that enable them. */
8439 struct reason_option_codes_t
8441 const int reason; /* cpplib message reason. */
8442 const int option_code; /* gcc option that controls this message. */
8445 static const struct reason_option_codes_t option_codes[] = {
8446 {CPP_W_DEPRECATED, OPT_Wdeprecated},
8447 {CPP_W_COMMENTS, OPT_Wcomment},
8448 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
8449 {CPP_W_MULTICHAR, OPT_Wmultichar},
8450 {CPP_W_TRADITIONAL, OPT_Wtraditional},
8451 {CPP_W_LONG_LONG, OPT_Wlong_long},
8452 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
8453 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
8454 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
8455 {CPP_W_UNDEF, OPT_Wundef},
8456 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
8457 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
8458 {CPP_W_NORMALIZE, OPT_Wnormalized_},
8459 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
8460 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
8461 {CPP_W_NONE, 0}
8464 /* Return the gcc option code associated with the reason for a cpp
8465 message, or 0 if none. */
8467 static int
8468 c_option_controlling_cpp_error (int reason)
8470 const struct reason_option_codes_t *entry;
8472 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
8474 if (entry->reason == reason)
8475 return entry->option_code;
8477 return 0;
8480 /* Callback from cpp_error for PFILE to print diagnostics from the
8481 preprocessor. The diagnostic is of type LEVEL, with REASON set
8482 to the reason code if LEVEL is represents a warning, at location
8483 LOCATION unless this is after lexing and the compiler's location
8484 should be used instead, with column number possibly overridden by
8485 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
8486 the arguments. Returns true if a diagnostic was emitted, false
8487 otherwise. */
8489 bool
8490 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8491 location_t location, unsigned int column_override,
8492 const char *msg, va_list *ap)
8494 diagnostic_info diagnostic;
8495 diagnostic_t dlevel;
8496 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
8497 bool ret;
8499 switch (level)
8501 case CPP_DL_WARNING_SYSHDR:
8502 if (flag_no_output)
8503 return false;
8504 global_dc->dc_warn_system_headers = 1;
8505 /* Fall through. */
8506 case CPP_DL_WARNING:
8507 if (flag_no_output)
8508 return false;
8509 dlevel = DK_WARNING;
8510 break;
8511 case CPP_DL_PEDWARN:
8512 if (flag_no_output && !flag_pedantic_errors)
8513 return false;
8514 dlevel = DK_PEDWARN;
8515 break;
8516 case CPP_DL_ERROR:
8517 dlevel = DK_ERROR;
8518 break;
8519 case CPP_DL_ICE:
8520 dlevel = DK_ICE;
8521 break;
8522 case CPP_DL_NOTE:
8523 dlevel = DK_NOTE;
8524 break;
8525 case CPP_DL_FATAL:
8526 dlevel = DK_FATAL;
8527 break;
8528 default:
8529 gcc_unreachable ();
8531 if (done_lexing)
8532 location = input_location;
8533 diagnostic_set_info_translated (&diagnostic, msg, ap,
8534 location, dlevel);
8535 if (column_override)
8536 diagnostic_override_column (&diagnostic, column_override);
8537 diagnostic_override_option_index (&diagnostic,
8538 c_option_controlling_cpp_error (reason));
8539 ret = report_diagnostic (&diagnostic);
8540 if (level == CPP_DL_WARNING_SYSHDR)
8541 global_dc->dc_warn_system_headers = save_warn_system_headers;
8542 return ret;
8545 /* Convert a character from the host to the target execution character
8546 set. cpplib handles this, mostly. */
8548 HOST_WIDE_INT
8549 c_common_to_target_charset (HOST_WIDE_INT c)
8551 /* Character constants in GCC proper are sign-extended under -fsigned-char,
8552 zero-extended under -fno-signed-char. cpplib insists that characters
8553 and character constants are always unsigned. Hence we must convert
8554 back and forth. */
8555 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
8557 uc = cpp_host_to_exec_charset (parse_in, uc);
8559 if (flag_signed_char)
8560 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
8561 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
8562 else
8563 return uc;
8566 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
8567 references with an INDIRECT_REF of a constant at the bottom; much like the
8568 traditional rendering of offsetof as a macro. Return the folded result. */
8570 tree
8571 fold_offsetof_1 (tree expr)
8573 tree base, off, t;
8575 switch (TREE_CODE (expr))
8577 case ERROR_MARK:
8578 return expr;
8580 case VAR_DECL:
8581 error ("cannot apply %<offsetof%> to static data member %qD", expr);
8582 return error_mark_node;
8584 case CALL_EXPR:
8585 case TARGET_EXPR:
8586 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
8587 return error_mark_node;
8589 case NOP_EXPR:
8590 case INDIRECT_REF:
8591 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
8593 error ("cannot apply %<offsetof%> to a non constant address");
8594 return error_mark_node;
8596 return TREE_OPERAND (expr, 0);
8598 case COMPONENT_REF:
8599 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
8600 if (base == error_mark_node)
8601 return base;
8603 t = TREE_OPERAND (expr, 1);
8604 if (DECL_C_BIT_FIELD (t))
8606 error ("attempt to take address of bit-field structure "
8607 "member %qD", t);
8608 return error_mark_node;
8610 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
8611 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
8613 / BITS_PER_UNIT));
8614 break;
8616 case ARRAY_REF:
8617 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
8618 if (base == error_mark_node)
8619 return base;
8621 t = TREE_OPERAND (expr, 1);
8623 /* Check if the offset goes beyond the upper bound of the array. */
8624 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
8626 tree upbound = array_ref_up_bound (expr);
8627 if (upbound != NULL_TREE
8628 && TREE_CODE (upbound) == INTEGER_CST
8629 && !tree_int_cst_equal (upbound,
8630 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
8632 upbound = size_binop (PLUS_EXPR, upbound,
8633 build_int_cst (TREE_TYPE (upbound), 1));
8634 if (tree_int_cst_lt (upbound, t))
8636 tree v;
8638 for (v = TREE_OPERAND (expr, 0);
8639 TREE_CODE (v) == COMPONENT_REF;
8640 v = TREE_OPERAND (v, 0))
8641 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
8642 == RECORD_TYPE)
8644 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
8645 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
8646 if (TREE_CODE (fld_chain) == FIELD_DECL)
8647 break;
8649 if (fld_chain)
8650 break;
8652 /* Don't warn if the array might be considered a poor
8653 man's flexible array member with a very permissive
8654 definition thereof. */
8655 if (TREE_CODE (v) == ARRAY_REF
8656 || TREE_CODE (v) == COMPONENT_REF)
8657 warning (OPT_Warray_bounds,
8658 "index %E denotes an offset "
8659 "greater than size of %qT",
8660 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
8665 t = convert (sizetype, t);
8666 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
8667 break;
8669 case COMPOUND_EXPR:
8670 /* Handle static members of volatile structs. */
8671 t = TREE_OPERAND (expr, 1);
8672 gcc_assert (TREE_CODE (t) == VAR_DECL);
8673 return fold_offsetof_1 (t);
8675 default:
8676 gcc_unreachable ();
8679 return fold_build_pointer_plus (base, off);
8682 /* Likewise, but convert it to the return type of offsetof. */
8684 tree
8685 fold_offsetof (tree expr)
8687 return convert (size_type_node, fold_offsetof_1 (expr));
8690 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
8691 expression, because B will always be true. */
8693 void
8694 warn_for_omitted_condop (location_t location, tree cond)
8696 if (truth_value_p (TREE_CODE (cond)))
8697 warning_at (location, OPT_Wparentheses,
8698 "the omitted middle operand in ?: will always be %<true%>, "
8699 "suggest explicit middle operand");
8702 /* Give an error for storing into ARG, which is 'const'. USE indicates
8703 how ARG was being used. */
8705 void
8706 readonly_error (tree arg, enum lvalue_use use)
8708 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
8709 || use == lv_asm);
8710 /* Using this macro rather than (for example) arrays of messages
8711 ensures that all the format strings are checked at compile
8712 time. */
8713 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
8714 : (use == lv_increment ? (I) \
8715 : (use == lv_decrement ? (D) : (AS))))
8716 if (TREE_CODE (arg) == COMPONENT_REF)
8718 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
8719 error (READONLY_MSG (G_("assignment of member "
8720 "%qD in read-only object"),
8721 G_("increment of member "
8722 "%qD in read-only object"),
8723 G_("decrement of member "
8724 "%qD in read-only object"),
8725 G_("member %qD in read-only object "
8726 "used as %<asm%> output")),
8727 TREE_OPERAND (arg, 1));
8728 else
8729 error (READONLY_MSG (G_("assignment of read-only member %qD"),
8730 G_("increment of read-only member %qD"),
8731 G_("decrement of read-only member %qD"),
8732 G_("read-only member %qD used as %<asm%> output")),
8733 TREE_OPERAND (arg, 1));
8735 else if (TREE_CODE (arg) == VAR_DECL)
8736 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
8737 G_("increment of read-only variable %qD"),
8738 G_("decrement of read-only variable %qD"),
8739 G_("read-only variable %qD used as %<asm%> output")),
8740 arg);
8741 else if (TREE_CODE (arg) == PARM_DECL)
8742 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
8743 G_("increment of read-only parameter %qD"),
8744 G_("decrement of read-only parameter %qD"),
8745 G_("read-only parameter %qD use as %<asm%> output")),
8746 arg);
8747 else if (TREE_CODE (arg) == RESULT_DECL)
8749 gcc_assert (c_dialect_cxx ());
8750 error (READONLY_MSG (G_("assignment of "
8751 "read-only named return value %qD"),
8752 G_("increment of "
8753 "read-only named return value %qD"),
8754 G_("decrement of "
8755 "read-only named return value %qD"),
8756 G_("read-only named return value %qD "
8757 "used as %<asm%>output")),
8758 arg);
8760 else if (TREE_CODE (arg) == FUNCTION_DECL)
8761 error (READONLY_MSG (G_("assignment of function %qD"),
8762 G_("increment of function %qD"),
8763 G_("decrement of function %qD"),
8764 G_("function %qD used as %<asm%> output")),
8765 arg);
8766 else
8767 error (READONLY_MSG (G_("assignment of read-only location %qE"),
8768 G_("increment of read-only location %qE"),
8769 G_("decrement of read-only location %qE"),
8770 G_("read-only location %qE used as %<asm%> output")),
8771 arg);
8774 /* Print an error message for an invalid lvalue. USE says
8775 how the lvalue is being used and so selects the error message. LOC
8776 is the location for the error. */
8778 void
8779 lvalue_error (location_t loc, enum lvalue_use use)
8781 switch (use)
8783 case lv_assign:
8784 error_at (loc, "lvalue required as left operand of assignment");
8785 break;
8786 case lv_increment:
8787 error_at (loc, "lvalue required as increment operand");
8788 break;
8789 case lv_decrement:
8790 error_at (loc, "lvalue required as decrement operand");
8791 break;
8792 case lv_addressof:
8793 error_at (loc, "lvalue required as unary %<&%> operand");
8794 break;
8795 case lv_asm:
8796 error_at (loc, "lvalue required in asm statement");
8797 break;
8798 default:
8799 gcc_unreachable ();
8803 /* Print an error message for an invalid indirection of type TYPE.
8804 ERRSTRING is the name of the operator for the indirection. */
8806 void
8807 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
8809 switch (errstring)
8811 case RO_NULL:
8812 gcc_assert (c_dialect_cxx ());
8813 error_at (loc, "invalid type argument (have %qT)", type);
8814 break;
8815 case RO_ARRAY_INDEXING:
8816 error_at (loc,
8817 "invalid type argument of array indexing (have %qT)",
8818 type);
8819 break;
8820 case RO_UNARY_STAR:
8821 error_at (loc,
8822 "invalid type argument of unary %<*%> (have %qT)",
8823 type);
8824 break;
8825 case RO_ARROW:
8826 error_at (loc,
8827 "invalid type argument of %<->%> (have %qT)",
8828 type);
8829 break;
8830 case RO_IMPLICIT_CONVERSION:
8831 error_at (loc,
8832 "invalid type argument of implicit conversion (have %qT)",
8833 type);
8834 break;
8835 default:
8836 gcc_unreachable ();
8840 /* *PTYPE is an incomplete array. Complete it with a domain based on
8841 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
8842 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8843 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
8846 complete_array_type (tree *ptype, tree initial_value, bool do_default)
8848 tree maxindex, type, main_type, elt, unqual_elt;
8849 int failure = 0, quals;
8850 hashval_t hashcode = 0;
8852 maxindex = size_zero_node;
8853 if (initial_value)
8855 if (TREE_CODE (initial_value) == STRING_CST)
8857 int eltsize
8858 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8859 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
8861 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8863 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
8865 if (VEC_empty (constructor_elt, v))
8867 if (pedantic)
8868 failure = 3;
8869 maxindex = ssize_int (-1);
8871 else
8873 tree curindex;
8874 unsigned HOST_WIDE_INT cnt;
8875 constructor_elt *ce;
8876 bool fold_p = false;
8878 if (VEC_index (constructor_elt, v, 0)->index)
8879 maxindex = fold_convert_loc (input_location, sizetype,
8880 VEC_index (constructor_elt,
8881 v, 0)->index);
8882 curindex = maxindex;
8884 for (cnt = 1;
8885 VEC_iterate (constructor_elt, v, cnt, ce);
8886 cnt++)
8888 bool curfold_p = false;
8889 if (ce->index)
8890 curindex = ce->index, curfold_p = true;
8891 else
8893 if (fold_p)
8894 curindex = fold_convert (sizetype, curindex);
8895 curindex = size_binop (PLUS_EXPR, curindex,
8896 size_one_node);
8898 if (tree_int_cst_lt (maxindex, curindex))
8899 maxindex = curindex, fold_p = curfold_p;
8901 if (fold_p)
8902 maxindex = fold_convert (sizetype, maxindex);
8905 else
8907 /* Make an error message unless that happened already. */
8908 if (initial_value != error_mark_node)
8909 failure = 1;
8912 else
8914 failure = 2;
8915 if (!do_default)
8916 return failure;
8919 type = *ptype;
8920 elt = TREE_TYPE (type);
8921 quals = TYPE_QUALS (strip_array_types (elt));
8922 if (quals == 0)
8923 unqual_elt = elt;
8924 else
8925 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
8927 /* Using build_distinct_type_copy and modifying things afterward instead
8928 of using build_array_type to create a new type preserves all of the
8929 TYPE_LANG_FLAG_? bits that the front end may have set. */
8930 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8931 TREE_TYPE (main_type) = unqual_elt;
8932 TYPE_DOMAIN (main_type)
8933 = build_range_type (TREE_TYPE (maxindex),
8934 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
8935 layout_type (main_type);
8937 /* Make sure we have the canonical MAIN_TYPE. */
8938 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
8939 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
8940 hashcode);
8941 main_type = type_hash_canon (hashcode, main_type);
8943 /* Fix the canonical type. */
8944 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
8945 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
8946 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
8947 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
8948 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
8949 != TYPE_DOMAIN (main_type)))
8950 TYPE_CANONICAL (main_type)
8951 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
8952 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
8953 else
8954 TYPE_CANONICAL (main_type) = main_type;
8956 if (quals == 0)
8957 type = main_type;
8958 else
8959 type = c_build_qualified_type (main_type, quals);
8961 if (COMPLETE_TYPE_P (type)
8962 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8963 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
8965 error ("size of array is too large");
8966 /* If we proceed with the array type as it is, we'll eventually
8967 crash in tree_low_cst(). */
8968 type = error_mark_node;
8971 *ptype = type;
8972 return failure;
8975 /* Like c_mark_addressable but don't check register qualifier. */
8976 void
8977 c_common_mark_addressable_vec (tree t)
8979 while (handled_component_p (t))
8980 t = TREE_OPERAND (t, 0);
8981 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8982 return;
8983 TREE_ADDRESSABLE (t) = 1;
8988 /* Used to help initialize the builtin-types.def table. When a type of
8989 the correct size doesn't exist, use error_mark_node instead of NULL.
8990 The later results in segfaults even when a decl using the type doesn't
8991 get invoked. */
8993 tree
8994 builtin_type_for_size (int size, bool unsignedp)
8996 tree type = lang_hooks.types.type_for_size (size, unsignedp);
8997 return type ? type : error_mark_node;
9000 /* A helper function for resolve_overloaded_builtin in resolving the
9001 overloaded __sync_ builtins. Returns a positive power of 2 if the
9002 first operand of PARAMS is a pointer to a supported data type.
9003 Returns 0 if an error is encountered. */
9005 static int
9006 sync_resolve_size (tree function, VEC(tree,gc) *params)
9008 tree type;
9009 int size;
9011 if (VEC_empty (tree, params))
9013 error ("too few arguments to function %qE", function);
9014 return 0;
9017 type = TREE_TYPE (VEC_index (tree, params, 0));
9018 if (TREE_CODE (type) != POINTER_TYPE)
9019 goto incompatible;
9021 type = TREE_TYPE (type);
9022 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9023 goto incompatible;
9025 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
9026 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
9027 return size;
9029 incompatible:
9030 error ("incompatible type for argument %d of %qE", 1, function);
9031 return 0;
9034 /* A helper function for resolve_overloaded_builtin. Adds casts to
9035 PARAMS to make arguments match up with those of FUNCTION. Drops
9036 the variadic arguments at the end. Returns false if some error
9037 was encountered; true on success. */
9039 static bool
9040 sync_resolve_params (location_t loc, tree orig_function, tree function,
9041 VEC(tree, gc) *params, bool orig_format)
9043 function_args_iterator iter;
9044 tree ptype;
9045 unsigned int parmnum;
9047 function_args_iter_init (&iter, TREE_TYPE (function));
9048 /* We've declared the implementation functions to use "volatile void *"
9049 as the pointer parameter, so we shouldn't get any complaints from the
9050 call to check_function_arguments what ever type the user used. */
9051 function_args_iter_next (&iter);
9052 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
9054 /* For the rest of the values, we need to cast these to FTYPE, so that we
9055 don't get warnings for passing pointer types, etc. */
9056 parmnum = 0;
9057 while (1)
9059 tree val, arg_type;
9061 arg_type = function_args_iter_cond (&iter);
9062 /* XXX void_type_node belies the abstraction. */
9063 if (arg_type == void_type_node)
9064 break;
9066 ++parmnum;
9067 if (VEC_length (tree, params) <= parmnum)
9069 error_at (loc, "too few arguments to function %qE", orig_function);
9070 return false;
9073 /* Only convert parameters if the size is appropriate with new format
9074 sync routines. */
9075 if (orig_format
9076 || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (arg_type)))
9078 /* Ideally for the first conversion we'd use convert_for_assignment
9079 so that we get warnings for anything that doesn't match the pointer
9080 type. This isn't portable across the C and C++ front ends atm. */
9081 val = VEC_index (tree, params, parmnum);
9082 val = convert (ptype, val);
9083 val = convert (arg_type, val);
9084 VEC_replace (tree, params, parmnum, val);
9087 function_args_iter_next (&iter);
9090 /* __atomic routines are not variadic. */
9091 if (!orig_format && VEC_length (tree, params) != parmnum + 1)
9093 error_at (loc, "too many arguments to function %qE", orig_function);
9094 return false;
9097 /* The definition of these primitives is variadic, with the remaining
9098 being "an optional list of variables protected by the memory barrier".
9099 No clue what that's supposed to mean, precisely, but we consider all
9100 call-clobbered variables to be protected so we're safe. */
9101 VEC_truncate (tree, params, parmnum + 1);
9103 return true;
9106 /* A helper function for resolve_overloaded_builtin. Adds a cast to
9107 RESULT to make it match the type of the first pointer argument in
9108 PARAMS. */
9110 static tree
9111 sync_resolve_return (tree first_param, tree result, bool orig_format)
9113 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
9114 tree rtype = TREE_TYPE (result);
9115 ptype = TYPE_MAIN_VARIANT (ptype);
9117 /* New format doesn't require casting unless the types are the same size. */
9118 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
9119 return convert (ptype, result);
9120 else
9121 return result;
9124 /* This function verifies the PARAMS to generic atomic FUNCTION.
9125 It returns the size if all the parameters are the same size, otherwise
9126 0 is returned if the parameters are invalid. */
9128 static int
9129 get_atomic_generic_size (location_t loc, tree function, VEC(tree,gc) *params)
9131 unsigned int n_param;
9132 unsigned int n_model;
9133 unsigned int x;
9134 int size_0;
9135 tree type_0;
9137 /* Determine the parameter makeup. */
9138 switch (DECL_FUNCTION_CODE (function))
9140 case BUILT_IN_ATOMIC_EXCHANGE:
9141 n_param = 4;
9142 n_model = 1;
9143 break;
9144 case BUILT_IN_ATOMIC_LOAD:
9145 case BUILT_IN_ATOMIC_STORE:
9146 n_param = 3;
9147 n_model = 1;
9148 break;
9149 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9150 n_param = 6;
9151 n_model = 2;
9152 break;
9153 default:
9154 return 0;
9157 if (VEC_length (tree, params) != n_param)
9159 error_at (loc, "incorrect number of arguments to function %qE", function);
9160 return 0;
9163 /* Get type of first parameter, and determine its size. */
9164 type_0 = TREE_TYPE (VEC_index (tree, params, 0));
9165 if (TREE_CODE (type_0) != POINTER_TYPE)
9167 error_at (loc, "argument 1 of %qE must be a pointer type", function);
9168 return 0;
9170 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
9172 /* Check each other parameter is a pointer and the same size. */
9173 for (x = 0; x < n_param - n_model; x++)
9175 int size;
9176 tree type = TREE_TYPE (VEC_index (tree, params, x));
9177 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
9178 if (n_param == 6 && x == 3)
9179 continue;
9180 if (!POINTER_TYPE_P (type))
9182 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
9183 function);
9184 return 0;
9186 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
9187 if (size != size_0)
9189 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
9190 function);
9191 return 0;
9195 /* Check memory model parameters for validity. */
9196 for (x = n_param - n_model ; x < n_param; x++)
9198 tree p = VEC_index (tree, params, x);
9199 if (TREE_CODE (p) == INTEGER_CST)
9201 int i = tree_low_cst (p, 1);
9202 if (i < 0 || i >= MEMMODEL_LAST)
9204 warning_at (loc, OPT_Winvalid_memory_model,
9205 "invalid memory model argument %d of %qE", x + 1,
9206 function);
9207 return MEMMODEL_SEQ_CST;
9210 else
9211 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
9213 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
9214 function);
9215 return 0;
9219 return size_0;
9223 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
9224 at the beginning of the parameter list PARAMS representing the size of the
9225 objects. This is to match the library ABI requirement. LOC is the location
9226 of the function call.
9227 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
9228 returned to allow the external call to be constructed. */
9230 static tree
9231 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9232 VEC(tree,gc) *params)
9234 tree size_node;
9236 /* Insert a SIZE_T parameter as the first param. If there isn't
9237 enough space, allocate a new vector and recursively re-build with that. */
9238 if (!VEC_space (tree, params, 1))
9240 unsigned int z, len;
9241 VEC(tree,gc) *vec;
9242 tree f;
9244 len = VEC_length (tree, params);
9245 vec = VEC_alloc (tree, gc, len + 1);
9246 for (z = 0; z < len; z++)
9247 VEC_quick_push (tree, vec, VEC_index (tree, params, z));
9248 f = build_function_call_vec (loc, function, vec, NULL);
9249 VEC_free (tree, gc, vec);
9250 return f;
9253 /* Add the size parameter and leave as a function call for processing. */
9254 size_node = build_int_cst (size_type_node, n);
9255 VEC_quick_insert (tree, params, 0, size_node);
9256 return NULL_TREE;
9260 /* This will process an __atomic_exchange function call, determine whether it
9261 needs to be mapped to the _N variation, or turned into a library call.
9262 LOC is the location of the builtin call.
9263 FUNCTION is the DECL that has been invoked;
9264 PARAMS is the argument list for the call. The return value is non-null
9265 TRUE is returned if it is translated into the proper format for a call to the
9266 external library, and NEW_RETURN is set the tree for that function.
9267 FALSE is returned if processing for the _N variation is required, and
9268 NEW_RETURN is set to the the return value the result is copied into. */
9269 static bool
9270 resolve_overloaded_atomic_exchange (location_t loc, tree function,
9271 VEC(tree,gc) *params, tree *new_return)
9273 tree p0, p1, p2, p3;
9274 tree I_type, I_type_ptr;
9275 int n = get_atomic_generic_size (loc, function, params);
9277 /* If not a lock-free size, change to the library generic format. */
9278 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9280 *new_return = add_atomic_size_parameter (n, loc, function, params);
9281 return true;
9284 /* Otherwise there is a lockfree match, transform the call from:
9285 void fn(T* mem, T* desired, T* return, model)
9286 into
9287 *return = (T) (fn (In* mem, (In) *desired, model)) */
9289 p0 = VEC_index (tree, params, 0);
9290 p1 = VEC_index (tree, params, 1);
9291 p2 = VEC_index (tree, params, 2);
9292 p3 = VEC_index (tree, params, 3);
9294 /* Create pointer to appropriate size. */
9295 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9296 I_type_ptr = build_pointer_type (I_type);
9298 /* Convert object pointer to required type. */
9299 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9300 VEC_replace (tree, params, 0, p0);
9302 /* Convert new value to required type, and dereference it. */
9303 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9304 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9305 VEC_replace (tree, params, 1, p1);
9307 /* Move memory model to the 3rd position, and end param list. */
9308 VEC_replace (tree, params, 2, p3);
9309 VEC_truncate (tree, params, 3);
9311 /* Convert return pointer and dereference it for later assignment. */
9312 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
9314 return false;
9318 /* This will process an __atomic_compare_exchange function call, determine
9319 whether it needs to be mapped to the _N variation, or turned into a lib call.
9320 LOC is the location of the builtin call.
9321 FUNCTION is the DECL that has been invoked;
9322 PARAMS is the argument list for the call. The return value is non-null
9323 TRUE is returned if it is translated into the proper format for a call to the
9324 external library, and NEW_RETURN is set the tree for that function.
9325 FALSE is returned if processing for the _N variation is required. */
9327 static bool
9328 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9329 VEC(tree,gc) *params,
9330 tree *new_return)
9332 tree p0, p1, p2;
9333 tree I_type, I_type_ptr;
9334 int n = get_atomic_generic_size (loc, function, params);
9336 /* If not a lock-free size, change to the library generic format. */
9337 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9339 /* The library generic format does not have the weak parameter, so
9340 remove it from the param list. Since a parameter has been removed,
9341 we can be sure that there is room for the SIZE_T parameter, meaning
9342 there will not be a recursive rebuilding of the parameter list, so
9343 there is no danger this will be done twice. */
9344 if (n > 0)
9346 VEC_replace (tree, params, 3, VEC_index (tree, params, 4));
9347 VEC_replace (tree, params, 4, VEC_index (tree, params, 5));
9348 VEC_truncate (tree, params, 5);
9350 *new_return = add_atomic_size_parameter (n, loc, function, params);
9351 return true;
9354 /* Otherwise, there is a match, so the call needs to be transformed from:
9355 bool fn(T* mem, T* desired, T* return, weak, success, failure)
9356 into
9357 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
9359 p0 = VEC_index (tree, params, 0);
9360 p1 = VEC_index (tree, params, 1);
9361 p2 = VEC_index (tree, params, 2);
9363 /* Create pointer to appropriate size. */
9364 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9365 I_type_ptr = build_pointer_type (I_type);
9367 /* Convert object pointer to required type. */
9368 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9369 VEC_replace (tree, params, 0, p0);
9371 /* Convert expected pointer to required type. */
9372 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9373 VEC_replace (tree, params, 1, p1);
9375 /* Convert desired value to required type, and dereference it. */
9376 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
9377 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9378 VEC_replace (tree, params, 2, p2);
9380 /* The rest of the parameters are fine. NULL means no special return value
9381 processing.*/
9382 *new_return = NULL;
9383 return false;
9387 /* This will process an __atomic_load function call, determine whether it
9388 needs to be mapped to the _N variation, or turned into a library call.
9389 LOC is the location of the builtin call.
9390 FUNCTION is the DECL that has been invoked;
9391 PARAMS is the argument list for the call. The return value is non-null
9392 TRUE is returned if it is translated into the proper format for a call to the
9393 external library, and NEW_RETURN is set the tree for that function.
9394 FALSE is returned if processing for the _N variation is required, and
9395 NEW_RETURN is set to the the return value the result is copied into. */
9397 static bool
9398 resolve_overloaded_atomic_load (location_t loc, tree function,
9399 VEC(tree,gc) *params, tree *new_return)
9401 tree p0, p1, p2;
9402 tree I_type, I_type_ptr;
9403 int n = get_atomic_generic_size (loc, function, params);
9405 /* If not a lock-free size, change to the library generic format. */
9406 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9408 *new_return = add_atomic_size_parameter (n, loc, function, params);
9409 return true;
9412 /* Otherwise, there is a match, so the call needs to be transformed from:
9413 void fn(T* mem, T* return, model)
9414 into
9415 *return = (T) (fn ((In *) mem, model)) */
9417 p0 = VEC_index (tree, params, 0);
9418 p1 = VEC_index (tree, params, 1);
9419 p2 = VEC_index (tree, params, 2);
9421 /* Create pointer to appropriate size. */
9422 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9423 I_type_ptr = build_pointer_type (I_type);
9425 /* Convert object pointer to required type. */
9426 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9427 VEC_replace (tree, params, 0, p0);
9429 /* Move memory model to the 2nd position, and end param list. */
9430 VEC_replace (tree, params, 1, p2);
9431 VEC_truncate (tree, params, 2);
9433 /* Convert return pointer and dereference it for later assignment. */
9434 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9436 return false;
9440 /* This will process an __atomic_store function call, determine whether it
9441 needs to be mapped to the _N variation, or turned into a library call.
9442 LOC is the location of the builtin call.
9443 FUNCTION is the DECL that has been invoked;
9444 PARAMS is the argument list for the call. The return value is non-null
9445 TRUE is returned if it is translated into the proper format for a call to the
9446 external library, and NEW_RETURN is set the tree for that function.
9447 FALSE is returned if processing for the _N variation is required, and
9448 NEW_RETURN is set to the the return value the result is copied into. */
9450 static bool
9451 resolve_overloaded_atomic_store (location_t loc, tree function,
9452 VEC(tree,gc) *params, tree *new_return)
9454 tree p0, p1;
9455 tree I_type, I_type_ptr;
9456 int n = get_atomic_generic_size (loc, function, params);
9458 /* If not a lock-free size, change to the library generic format. */
9459 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9461 *new_return = add_atomic_size_parameter (n, loc, function, params);
9462 return true;
9465 /* Otherwise, there is a match, so the call needs to be transformed from:
9466 void fn(T* mem, T* value, model)
9467 into
9468 fn ((In *) mem, (In) *value, model) */
9470 p0 = VEC_index (tree, params, 0);
9471 p1 = VEC_index (tree, params, 1);
9473 /* Create pointer to appropriate size. */
9474 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9475 I_type_ptr = build_pointer_type (I_type);
9477 /* Convert object pointer to required type. */
9478 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9479 VEC_replace (tree, params, 0, p0);
9481 /* Convert new value to required type, and dereference it. */
9482 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9483 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9484 VEC_replace (tree, params, 1, p1);
9486 /* The memory model is in the right spot already. Return is void. */
9487 *new_return = NULL_TREE;
9489 return false;
9493 /* Some builtin functions are placeholders for other expressions. This
9494 function should be called immediately after parsing the call expression
9495 before surrounding code has committed to the type of the expression.
9497 LOC is the location of the builtin call.
9499 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
9500 PARAMS is the argument list for the call. The return value is non-null
9501 when expansion is complete, and null if normal processing should
9502 continue. */
9504 tree
9505 resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params)
9507 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
9508 bool orig_format = true;
9509 tree new_return = NULL_TREE;
9511 switch (DECL_BUILT_IN_CLASS (function))
9513 case BUILT_IN_NORMAL:
9514 break;
9515 case BUILT_IN_MD:
9516 if (targetm.resolve_overloaded_builtin)
9517 return targetm.resolve_overloaded_builtin (loc, function, params);
9518 else
9519 return NULL_TREE;
9520 default:
9521 return NULL_TREE;
9524 /* Handle BUILT_IN_NORMAL here. */
9525 switch (orig_code)
9527 case BUILT_IN_ATOMIC_EXCHANGE:
9528 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9529 case BUILT_IN_ATOMIC_LOAD:
9530 case BUILT_IN_ATOMIC_STORE:
9532 /* Handle these 4 together so that they can fall through to the next
9533 case if the call is transformed to an _N variant. */
9534 switch (orig_code)
9536 case BUILT_IN_ATOMIC_EXCHANGE:
9538 if (resolve_overloaded_atomic_exchange (loc, function, params,
9539 &new_return))
9540 return new_return;
9541 /* Change to the _N variant. */
9542 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
9543 break;
9546 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9548 if (resolve_overloaded_atomic_compare_exchange (loc, function,
9549 params,
9550 &new_return))
9551 return new_return;
9552 /* Change to the _N variant. */
9553 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
9554 break;
9556 case BUILT_IN_ATOMIC_LOAD:
9558 if (resolve_overloaded_atomic_load (loc, function, params,
9559 &new_return))
9560 return new_return;
9561 /* Change to the _N variant. */
9562 orig_code = BUILT_IN_ATOMIC_LOAD_N;
9563 break;
9565 case BUILT_IN_ATOMIC_STORE:
9567 if (resolve_overloaded_atomic_store (loc, function, params,
9568 &new_return))
9569 return new_return;
9570 /* Change to the _N variant. */
9571 orig_code = BUILT_IN_ATOMIC_STORE_N;
9572 break;
9574 default:
9575 gcc_unreachable ();
9577 /* Fallthrough to the normal processing. */
9579 case BUILT_IN_ATOMIC_EXCHANGE_N:
9580 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
9581 case BUILT_IN_ATOMIC_LOAD_N:
9582 case BUILT_IN_ATOMIC_STORE_N:
9583 case BUILT_IN_ATOMIC_ADD_FETCH_N:
9584 case BUILT_IN_ATOMIC_SUB_FETCH_N:
9585 case BUILT_IN_ATOMIC_AND_FETCH_N:
9586 case BUILT_IN_ATOMIC_NAND_FETCH_N:
9587 case BUILT_IN_ATOMIC_XOR_FETCH_N:
9588 case BUILT_IN_ATOMIC_OR_FETCH_N:
9589 case BUILT_IN_ATOMIC_FETCH_ADD_N:
9590 case BUILT_IN_ATOMIC_FETCH_SUB_N:
9591 case BUILT_IN_ATOMIC_FETCH_AND_N:
9592 case BUILT_IN_ATOMIC_FETCH_NAND_N:
9593 case BUILT_IN_ATOMIC_FETCH_XOR_N:
9594 case BUILT_IN_ATOMIC_FETCH_OR_N:
9596 orig_format = false;
9597 /* Fallthru for parameter processing. */
9599 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
9600 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
9601 case BUILT_IN_SYNC_FETCH_AND_OR_N:
9602 case BUILT_IN_SYNC_FETCH_AND_AND_N:
9603 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
9604 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
9605 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
9606 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
9607 case BUILT_IN_SYNC_OR_AND_FETCH_N:
9608 case BUILT_IN_SYNC_AND_AND_FETCH_N:
9609 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
9610 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
9611 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
9612 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
9613 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
9614 case BUILT_IN_SYNC_LOCK_RELEASE_N:
9616 int n = sync_resolve_size (function, params);
9617 tree new_function, first_param, result;
9618 enum built_in_function fncode;
9620 if (n == 0)
9621 return error_mark_node;
9623 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
9624 new_function = builtin_decl_explicit (fncode);
9625 if (!sync_resolve_params (loc, function, new_function, params,
9626 orig_format))
9627 return error_mark_node;
9629 first_param = VEC_index (tree, params, 0);
9630 result = build_function_call_vec (loc, new_function, params, NULL);
9631 if (result == error_mark_node)
9632 return result;
9633 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
9634 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
9635 && orig_code != BUILT_IN_ATOMIC_STORE_N)
9636 result = sync_resolve_return (first_param, result, orig_format);
9638 /* If new_return is set, assign function to that expr and cast the
9639 result to void since the generic interface returned void. */
9640 if (new_return)
9642 /* Cast function result from I{1,2,4,8,16} to the required type. */
9643 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
9644 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
9645 result);
9646 TREE_SIDE_EFFECTS (result) = 1;
9647 protected_set_expr_location (result, loc);
9648 result = convert (void_type_node, result);
9650 return result;
9653 default:
9654 return NULL_TREE;
9658 /* Ignoring their sign, return true if two scalar types are the same. */
9659 bool
9660 same_scalar_type_ignoring_signedness (tree t1, tree t2)
9662 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
9664 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
9665 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
9666 || c2 == FIXED_POINT_TYPE));
9668 /* Equality works here because c_common_signed_type uses
9669 TYPE_MAIN_VARIANT. */
9670 return c_common_signed_type (t1)
9671 == c_common_signed_type (t2);
9674 /* Check for missing format attributes on function pointers. LTYPE is
9675 the new type or left-hand side type. RTYPE is the old type or
9676 right-hand side type. Returns TRUE if LTYPE is missing the desired
9677 attribute. */
9679 bool
9680 check_missing_format_attribute (tree ltype, tree rtype)
9682 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
9683 tree ra;
9685 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
9686 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
9687 break;
9688 if (ra)
9690 tree la;
9691 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
9692 if (is_attribute_p ("format", TREE_PURPOSE (la)))
9693 break;
9694 return !la;
9696 else
9697 return false;
9700 /* Subscripting with type char is likely to lose on a machine where
9701 chars are signed. So warn on any machine, but optionally. Don't
9702 warn for unsigned char since that type is safe. Don't warn for
9703 signed char because anyone who uses that must have done so
9704 deliberately. Furthermore, we reduce the false positive load by
9705 warning only for non-constant value of type char. */
9707 void
9708 warn_array_subscript_with_type_char (tree index)
9710 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
9711 && TREE_CODE (index) != INTEGER_CST)
9712 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
9715 /* Implement -Wparentheses for the unexpected C precedence rules, to
9716 cover cases like x + y << z which readers are likely to
9717 misinterpret. We have seen an expression in which CODE is a binary
9718 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
9719 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
9720 CODE_RIGHT may be ERROR_MARK, which means that that side of the
9721 expression was not formed using a binary or unary operator, or it
9722 was enclosed in parentheses. */
9724 void
9725 warn_about_parentheses (enum tree_code code,
9726 enum tree_code code_left, tree arg_left,
9727 enum tree_code code_right, tree arg_right)
9729 if (!warn_parentheses)
9730 return;
9732 /* This macro tests that the expression ARG with original tree code
9733 CODE appears to be a boolean expression. or the result of folding a
9734 boolean expression. */
9735 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
9736 (truth_value_p (TREE_CODE (ARG)) \
9737 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
9738 /* Folding may create 0 or 1 integers from other expressions. */ \
9739 || ((CODE) != INTEGER_CST \
9740 && (integer_onep (ARG) || integer_zerop (ARG))))
9742 switch (code)
9744 case LSHIFT_EXPR:
9745 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9746 warning (OPT_Wparentheses,
9747 "suggest parentheses around %<+%> inside %<<<%>");
9748 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9749 warning (OPT_Wparentheses,
9750 "suggest parentheses around %<-%> inside %<<<%>");
9751 return;
9753 case RSHIFT_EXPR:
9754 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9755 warning (OPT_Wparentheses,
9756 "suggest parentheses around %<+%> inside %<>>%>");
9757 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9758 warning (OPT_Wparentheses,
9759 "suggest parentheses around %<-%> inside %<>>%>");
9760 return;
9762 case TRUTH_ORIF_EXPR:
9763 if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
9764 warning (OPT_Wparentheses,
9765 "suggest parentheses around %<&&%> within %<||%>");
9766 return;
9768 case BIT_IOR_EXPR:
9769 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
9770 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
9771 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
9772 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
9773 warning (OPT_Wparentheses,
9774 "suggest parentheses around arithmetic in operand of %<|%>");
9775 /* Check cases like x|y==z */
9776 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9777 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9778 warning (OPT_Wparentheses,
9779 "suggest parentheses around comparison in operand of %<|%>");
9780 /* Check cases like !x | y */
9781 else if (code_left == TRUTH_NOT_EXPR
9782 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
9783 warning (OPT_Wparentheses, "suggest parentheses around operand of "
9784 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
9785 return;
9787 case BIT_XOR_EXPR:
9788 if (code_left == BIT_AND_EXPR
9789 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
9790 || code_right == BIT_AND_EXPR
9791 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
9792 warning (OPT_Wparentheses,
9793 "suggest parentheses around arithmetic in operand of %<^%>");
9794 /* Check cases like x^y==z */
9795 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9796 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9797 warning (OPT_Wparentheses,
9798 "suggest parentheses around comparison in operand of %<^%>");
9799 return;
9801 case BIT_AND_EXPR:
9802 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9803 warning (OPT_Wparentheses,
9804 "suggest parentheses around %<+%> in operand of %<&%>");
9805 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9806 warning (OPT_Wparentheses,
9807 "suggest parentheses around %<-%> in operand of %<&%>");
9808 /* Check cases like x&y==z */
9809 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9810 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9811 warning (OPT_Wparentheses,
9812 "suggest parentheses around comparison in operand of %<&%>");
9813 /* Check cases like !x & y */
9814 else if (code_left == TRUTH_NOT_EXPR
9815 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
9816 warning (OPT_Wparentheses, "suggest parentheses around operand of "
9817 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
9818 return;
9820 case EQ_EXPR:
9821 if (TREE_CODE_CLASS (code_left) == tcc_comparison
9822 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9823 warning (OPT_Wparentheses,
9824 "suggest parentheses around comparison in operand of %<==%>");
9825 return;
9826 case NE_EXPR:
9827 if (TREE_CODE_CLASS (code_left) == tcc_comparison
9828 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9829 warning (OPT_Wparentheses,
9830 "suggest parentheses around comparison in operand of %<!=%>");
9831 return;
9833 default:
9834 if (TREE_CODE_CLASS (code) == tcc_comparison
9835 && ((TREE_CODE_CLASS (code_left) == tcc_comparison
9836 && code_left != NE_EXPR && code_left != EQ_EXPR
9837 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
9838 || (TREE_CODE_CLASS (code_right) == tcc_comparison
9839 && code_right != NE_EXPR && code_right != EQ_EXPR
9840 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
9841 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
9842 "have their mathematical meaning");
9843 return;
9845 #undef NOT_A_BOOLEAN_EXPR_P
9848 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
9850 void
9851 warn_for_unused_label (tree label)
9853 if (!TREE_USED (label))
9855 if (DECL_INITIAL (label))
9856 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
9857 else
9858 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
9862 /* Warn for division by zero according to the value of DIVISOR. LOC
9863 is the location of the division operator. */
9865 void
9866 warn_for_div_by_zero (location_t loc, tree divisor)
9868 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
9869 about division by zero. Do not issue a warning if DIVISOR has a
9870 floating-point type, since we consider 0.0/0.0 a valid way of
9871 generating a NaN. */
9872 if (c_inhibit_evaluation_warnings == 0
9873 && (integer_zerop (divisor) || fixed_zerop (divisor)))
9874 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
9877 /* Subroutine of build_binary_op. Give warnings for comparisons
9878 between signed and unsigned quantities that may fail. Do the
9879 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
9880 so that casts will be considered, but default promotions won't
9883 LOCATION is the location of the comparison operator.
9885 The arguments of this function map directly to local variables
9886 of build_binary_op. */
9888 void
9889 warn_for_sign_compare (location_t location,
9890 tree orig_op0, tree orig_op1,
9891 tree op0, tree op1,
9892 tree result_type, enum tree_code resultcode)
9894 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
9895 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
9896 int unsignedp0, unsignedp1;
9898 /* In C++, check for comparison of different enum types. */
9899 if (c_dialect_cxx()
9900 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
9901 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
9902 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
9903 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
9905 warning_at (location,
9906 OPT_Wsign_compare, "comparison between types %qT and %qT",
9907 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
9910 /* Do not warn if the comparison is being done in a signed type,
9911 since the signed type will only be chosen if it can represent
9912 all the values of the unsigned type. */
9913 if (!TYPE_UNSIGNED (result_type))
9914 /* OK */;
9915 /* Do not warn if both operands are unsigned. */
9916 else if (op0_signed == op1_signed)
9917 /* OK */;
9918 else
9920 tree sop, uop, base_type;
9921 bool ovf;
9923 if (op0_signed)
9924 sop = orig_op0, uop = orig_op1;
9925 else
9926 sop = orig_op1, uop = orig_op0;
9928 STRIP_TYPE_NOPS (sop);
9929 STRIP_TYPE_NOPS (uop);
9930 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
9931 ? TREE_TYPE (result_type) : result_type);
9933 /* Do not warn if the signed quantity is an unsuffixed integer
9934 literal (or some static constant expression involving such
9935 literals or a conditional expression involving such literals)
9936 and it is non-negative. */
9937 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
9938 /* OK */;
9939 /* Do not warn if the comparison is an equality operation, the
9940 unsigned quantity is an integral constant, and it would fit
9941 in the result if the result were signed. */
9942 else if (TREE_CODE (uop) == INTEGER_CST
9943 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
9944 && int_fits_type_p (uop, c_common_signed_type (base_type)))
9945 /* OK */;
9946 /* In C, do not warn if the unsigned quantity is an enumeration
9947 constant and its maximum value would fit in the result if the
9948 result were signed. */
9949 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
9950 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
9951 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
9952 c_common_signed_type (base_type)))
9953 /* OK */;
9954 else
9955 warning_at (location,
9956 OPT_Wsign_compare,
9957 "comparison between signed and unsigned integer expressions");
9960 /* Warn if two unsigned values are being compared in a size larger
9961 than their original size, and one (and only one) is the result of
9962 a `~' operator. This comparison will always fail.
9964 Also warn if one operand is a constant, and the constant does not
9965 have all bits set that are set in the ~ operand when it is
9966 extended. */
9968 op0 = c_common_get_narrower (op0, &unsignedp0);
9969 op1 = c_common_get_narrower (op1, &unsignedp1);
9971 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
9972 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
9974 if (TREE_CODE (op0) == BIT_NOT_EXPR)
9975 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
9976 if (TREE_CODE (op1) == BIT_NOT_EXPR)
9977 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
9979 if (host_integerp (op0, 0) || host_integerp (op1, 0))
9981 tree primop;
9982 HOST_WIDE_INT constant, mask;
9983 int unsignedp;
9984 unsigned int bits;
9986 if (host_integerp (op0, 0))
9988 primop = op1;
9989 unsignedp = unsignedp1;
9990 constant = tree_low_cst (op0, 0);
9992 else
9994 primop = op0;
9995 unsignedp = unsignedp0;
9996 constant = tree_low_cst (op1, 0);
9999 bits = TYPE_PRECISION (TREE_TYPE (primop));
10000 if (bits < TYPE_PRECISION (result_type)
10001 && bits < HOST_BITS_PER_LONG && unsignedp)
10003 mask = (~ (HOST_WIDE_INT) 0) << bits;
10004 if ((mask & constant) != mask)
10006 if (constant == 0)
10007 warning (OPT_Wsign_compare,
10008 "promoted ~unsigned is always non-zero");
10009 else
10010 warning_at (location, OPT_Wsign_compare,
10011 "comparison of promoted ~unsigned with constant");
10015 else if (unsignedp0 && unsignedp1
10016 && (TYPE_PRECISION (TREE_TYPE (op0))
10017 < TYPE_PRECISION (result_type))
10018 && (TYPE_PRECISION (TREE_TYPE (op1))
10019 < TYPE_PRECISION (result_type)))
10020 warning_at (location, OPT_Wsign_compare,
10021 "comparison of promoted ~unsigned with unsigned");
10025 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
10026 type via c_common_type. If -Wdouble-promotion is in use, and the
10027 conditions for warning have been met, issue a warning. GMSGID is
10028 the warning message. It must have two %T specifiers for the type
10029 that was converted (generally "float") and the type to which it was
10030 converted (generally "double), respectively. LOC is the location
10031 to which the awrning should refer. */
10033 void
10034 do_warn_double_promotion (tree result_type, tree type1, tree type2,
10035 const char *gmsgid, location_t loc)
10037 tree source_type;
10039 if (!warn_double_promotion)
10040 return;
10041 /* If the conversion will not occur at run-time, there is no need to
10042 warn about it. */
10043 if (c_inhibit_evaluation_warnings)
10044 return;
10045 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
10046 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
10047 return;
10048 if (TYPE_MAIN_VARIANT (type1) == float_type_node
10049 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
10050 source_type = type1;
10051 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
10052 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
10053 source_type = type2;
10054 else
10055 return;
10056 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
10059 /* Setup a TYPE_DECL node as a typedef representation.
10061 X is a TYPE_DECL for a typedef statement. Create a brand new
10062 ..._TYPE node (which will be just a variant of the existing
10063 ..._TYPE node with identical properties) and then install X
10064 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
10066 The whole point here is to end up with a situation where each
10067 and every ..._TYPE node the compiler creates will be uniquely
10068 associated with AT MOST one node representing a typedef name.
10069 This way, even though the compiler substitutes corresponding
10070 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
10071 early on, later parts of the compiler can always do the reverse
10072 translation and get back the corresponding typedef name. For
10073 example, given:
10075 typedef struct S MY_TYPE;
10076 MY_TYPE object;
10078 Later parts of the compiler might only know that `object' was of
10079 type `struct S' if it were not for code just below. With this
10080 code however, later parts of the compiler see something like:
10082 struct S' == struct S
10083 typedef struct S' MY_TYPE;
10084 struct S' object;
10086 And they can then deduce (from the node for type struct S') that
10087 the original object declaration was:
10089 MY_TYPE object;
10091 Being able to do this is important for proper support of protoize,
10092 and also for generating precise symbolic debugging information
10093 which takes full account of the programmer's (typedef) vocabulary.
10095 Obviously, we don't want to generate a duplicate ..._TYPE node if
10096 the TYPE_DECL node that we are now processing really represents a
10097 standard built-in type. */
10099 void
10100 set_underlying_type (tree x)
10102 if (x == error_mark_node)
10103 return;
10104 if (DECL_IS_BUILTIN (x))
10106 if (TYPE_NAME (TREE_TYPE (x)) == 0)
10107 TYPE_NAME (TREE_TYPE (x)) = x;
10109 else if (TREE_TYPE (x) != error_mark_node
10110 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
10112 tree tt = TREE_TYPE (x);
10113 DECL_ORIGINAL_TYPE (x) = tt;
10114 tt = build_variant_type_copy (tt);
10115 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
10116 TYPE_NAME (tt) = x;
10117 TREE_USED (tt) = TREE_USED (x);
10118 TREE_TYPE (x) = tt;
10122 /* Record the types used by the current global variable declaration
10123 being parsed, so that we can decide later to emit their debug info.
10124 Those types are in types_used_by_cur_var_decl, and we are going to
10125 store them in the types_used_by_vars_hash hash table.
10126 DECL is the declaration of the global variable that has been parsed. */
10128 void
10129 record_types_used_by_current_var_decl (tree decl)
10131 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
10133 while (!VEC_empty (tree, types_used_by_cur_var_decl))
10135 tree type = VEC_pop (tree, types_used_by_cur_var_decl);
10136 types_used_by_var_decl_insert (type, decl);
10140 /* If DECL is a typedef that is declared in the current function,
10141 record it for the purpose of -Wunused-local-typedefs. */
10143 void
10144 record_locally_defined_typedef (tree decl)
10146 struct c_language_function *l;
10148 if (!warn_unused_local_typedefs
10149 || cfun == NULL
10150 /* if this is not a locally defined typedef then we are not
10151 interested. */
10152 || !is_typedef_decl (decl)
10153 || !decl_function_context (decl))
10154 return;
10156 l = (struct c_language_function *) cfun->language;
10157 VEC_safe_push (tree, gc, l->local_typedefs, decl);
10160 /* If T is a TYPE_DECL declared locally, mark it as used. */
10162 void
10163 maybe_record_typedef_use (tree t)
10165 if (!is_typedef_decl (t))
10166 return;
10168 TREE_USED (t) = true;
10171 /* Warn if there are some unused locally defined typedefs in the
10172 current function. */
10174 void
10175 maybe_warn_unused_local_typedefs (void)
10177 int i;
10178 tree decl;
10179 /* The number of times we have emitted -Wunused-local-typedefs
10180 warnings. If this is different from errorcount, that means some
10181 unrelated errors have been issued. In which case, we'll avoid
10182 emitting "unused-local-typedefs" warnings. */
10183 static int unused_local_typedefs_warn_count;
10184 struct c_language_function *l;
10186 if (cfun == NULL)
10187 return;
10189 if ((l = (struct c_language_function *) cfun->language) == NULL)
10190 return;
10192 if (warn_unused_local_typedefs
10193 && errorcount == unused_local_typedefs_warn_count)
10195 FOR_EACH_VEC_ELT (tree, l->local_typedefs, i, decl)
10196 if (!TREE_USED (decl))
10197 warning_at (DECL_SOURCE_LOCATION (decl),
10198 OPT_Wunused_local_typedefs,
10199 "typedef %qD locally defined but not used", decl);
10200 unused_local_typedefs_warn_count = errorcount;
10203 if (l->local_typedefs)
10205 VEC_free (tree, gc, l->local_typedefs);
10206 l->local_typedefs = NULL;
10210 /* The C and C++ parsers both use vectors to hold function arguments.
10211 For efficiency, we keep a cache of unused vectors. This is the
10212 cache. */
10214 typedef VEC(tree,gc)* tree_gc_vec;
10215 DEF_VEC_P(tree_gc_vec);
10216 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
10217 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
10219 /* Return a new vector from the cache. If the cache is empty,
10220 allocate a new vector. These vectors are GC'ed, so it is OK if the
10221 pointer is not released.. */
10223 VEC(tree,gc) *
10224 make_tree_vector (void)
10226 if (!VEC_empty (tree_gc_vec, tree_vector_cache))
10227 return VEC_pop (tree_gc_vec, tree_vector_cache);
10228 else
10230 /* Passing 0 to VEC_alloc returns NULL, and our callers require
10231 that we always return a non-NULL value. The vector code uses
10232 4 when growing a NULL vector, so we do too. */
10233 return VEC_alloc (tree, gc, 4);
10237 /* Release a vector of trees back to the cache. */
10239 void
10240 release_tree_vector (VEC(tree,gc) *vec)
10242 if (vec != NULL)
10244 VEC_truncate (tree, vec, 0);
10245 VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
10249 /* Get a new tree vector holding a single tree. */
10251 VEC(tree,gc) *
10252 make_tree_vector_single (tree t)
10254 VEC(tree,gc) *ret = make_tree_vector ();
10255 VEC_quick_push (tree, ret, t);
10256 return ret;
10259 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
10261 VEC(tree,gc) *
10262 make_tree_vector_from_list (tree list)
10264 VEC(tree,gc) *ret = make_tree_vector ();
10265 for (; list; list = TREE_CHAIN (list))
10266 VEC_safe_push (tree, gc, ret, TREE_VALUE (list));
10267 return ret;
10270 /* Get a new tree vector which is a copy of an existing one. */
10272 VEC(tree,gc) *
10273 make_tree_vector_copy (const VEC(tree,gc) *orig)
10275 VEC(tree,gc) *ret;
10276 unsigned int ix;
10277 tree t;
10279 ret = make_tree_vector ();
10280 VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
10281 FOR_EACH_VEC_ELT (tree, orig, ix, t)
10282 VEC_quick_push (tree, ret, t);
10283 return ret;
10286 /* Return true if KEYWORD starts a type specifier. */
10288 bool
10289 keyword_begins_type_specifier (enum rid keyword)
10291 switch (keyword)
10293 case RID_INT:
10294 case RID_CHAR:
10295 case RID_FLOAT:
10296 case RID_DOUBLE:
10297 case RID_VOID:
10298 case RID_INT128:
10299 case RID_UNSIGNED:
10300 case RID_LONG:
10301 case RID_SHORT:
10302 case RID_SIGNED:
10303 case RID_DFLOAT32:
10304 case RID_DFLOAT64:
10305 case RID_DFLOAT128:
10306 case RID_FRACT:
10307 case RID_ACCUM:
10308 case RID_BOOL:
10309 case RID_WCHAR:
10310 case RID_CHAR16:
10311 case RID_CHAR32:
10312 case RID_SAT:
10313 case RID_COMPLEX:
10314 case RID_TYPEOF:
10315 case RID_STRUCT:
10316 case RID_CLASS:
10317 case RID_UNION:
10318 case RID_ENUM:
10319 return true;
10320 default:
10321 return false;
10325 /* Return true if KEYWORD names a type qualifier. */
10327 bool
10328 keyword_is_type_qualifier (enum rid keyword)
10330 switch (keyword)
10332 case RID_CONST:
10333 case RID_VOLATILE:
10334 case RID_RESTRICT:
10335 return true;
10336 default:
10337 return false;
10341 /* Return true if KEYWORD names a storage class specifier.
10343 RID_TYPEDEF is not included in this list despite `typedef' being
10344 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
10345 such for syntactic convenience only. */
10347 bool
10348 keyword_is_storage_class_specifier (enum rid keyword)
10350 switch (keyword)
10352 case RID_STATIC:
10353 case RID_EXTERN:
10354 case RID_REGISTER:
10355 case RID_AUTO:
10356 case RID_MUTABLE:
10357 case RID_THREAD:
10358 return true;
10359 default:
10360 return false;
10364 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
10366 static bool
10367 keyword_is_function_specifier (enum rid keyword)
10369 switch (keyword)
10371 case RID_INLINE:
10372 case RID_NORETURN:
10373 case RID_VIRTUAL:
10374 case RID_EXPLICIT:
10375 return true;
10376 default:
10377 return false;
10381 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
10382 declaration-specifier (C99 6.7). */
10384 bool
10385 keyword_is_decl_specifier (enum rid keyword)
10387 if (keyword_is_storage_class_specifier (keyword)
10388 || keyword_is_type_qualifier (keyword)
10389 || keyword_is_function_specifier (keyword))
10390 return true;
10392 switch (keyword)
10394 case RID_TYPEDEF:
10395 case RID_FRIEND:
10396 case RID_CONSTEXPR:
10397 return true;
10398 default:
10399 return false;
10403 /* Initialize language-specific-bits of tree_contains_struct. */
10405 void
10406 c_common_init_ts (void)
10408 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
10409 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
10412 /* Build a user-defined numeric literal out of an integer constant type VALUE
10413 with identifier SUFFIX. */
10415 tree
10416 build_userdef_literal (tree suffix_id, tree value, tree num_string)
10418 tree literal = make_node (USERDEF_LITERAL);
10419 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
10420 USERDEF_LITERAL_VALUE (literal) = value;
10421 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
10422 return literal;
10425 #include "gt-c-family-c-common.h"