2014-06-18 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c-family / c-common.c
blob077263e1de5881d82f9a0d25ff581954952fd62e
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "intl.h"
25 #include "tree.h"
26 #include "fold-const.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "varasm.h"
32 #include "trans-mem.h"
33 #include "flags.h"
34 #include "c-pragma.h"
35 #include "c-common.h"
36 #include "c-objc.h"
37 #include "tm_p.h"
38 #include "obstack.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "diagnostic.h"
46 #include "tree-iterator.h"
47 #include "hashtab.h"
48 #include "opts.h"
49 #include "cgraph.h"
50 #include "target-def.h"
51 #include "gimplify.h"
52 #include "wide-int-print.h"
54 cpp_reader *parse_in; /* Declared in c-pragma.h. */
56 /* The following symbols are subsumed in the c_global_trees array, and
57 listed here individually for documentation purposes.
59 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
61 tree short_integer_type_node;
62 tree long_integer_type_node;
63 tree long_long_integer_type_node;
64 tree int128_integer_type_node;
66 tree short_unsigned_type_node;
67 tree long_unsigned_type_node;
68 tree long_long_unsigned_type_node;
69 tree int128_unsigned_type_node;
71 tree truthvalue_type_node;
72 tree truthvalue_false_node;
73 tree truthvalue_true_node;
75 tree ptrdiff_type_node;
77 tree unsigned_char_type_node;
78 tree signed_char_type_node;
79 tree wchar_type_node;
81 tree char16_type_node;
82 tree char32_type_node;
84 tree float_type_node;
85 tree double_type_node;
86 tree long_double_type_node;
88 tree complex_integer_type_node;
89 tree complex_float_type_node;
90 tree complex_double_type_node;
91 tree complex_long_double_type_node;
93 tree dfloat32_type_node;
94 tree dfloat64_type_node;
95 tree_dfloat128_type_node;
97 tree intQI_type_node;
98 tree intHI_type_node;
99 tree intSI_type_node;
100 tree intDI_type_node;
101 tree intTI_type_node;
103 tree unsigned_intQI_type_node;
104 tree unsigned_intHI_type_node;
105 tree unsigned_intSI_type_node;
106 tree unsigned_intDI_type_node;
107 tree unsigned_intTI_type_node;
109 tree widest_integer_literal_type_node;
110 tree widest_unsigned_literal_type_node;
112 Nodes for types `void *' and `const void *'.
114 tree ptr_type_node, const_ptr_type_node;
116 Nodes for types `char *' and `const char *'.
118 tree string_type_node, const_string_type_node;
120 Type `char[SOMENUMBER]'.
121 Used when an array of char is needed and the size is irrelevant.
123 tree char_array_type_node;
125 Type `wchar_t[SOMENUMBER]' or something like it.
126 Used when a wide string literal is created.
128 tree wchar_array_type_node;
130 Type `char16_t[SOMENUMBER]' or something like it.
131 Used when a UTF-16 string literal is created.
133 tree char16_array_type_node;
135 Type `char32_t[SOMENUMBER]' or something like it.
136 Used when a UTF-32 string literal is created.
138 tree char32_array_type_node;
140 Type `int ()' -- used for implicit declaration of functions.
142 tree default_function_type;
144 A VOID_TYPE node, packaged in a TREE_LIST.
146 tree void_list_node;
148 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
149 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
150 VAR_DECLS, but C++ does.)
152 tree function_name_decl_node;
153 tree pretty_function_name_decl_node;
154 tree c99_function_name_decl_node;
156 Stack of nested function name VAR_DECLs.
158 tree saved_function_name_decls;
162 tree c_global_trees[CTI_MAX];
164 /* Switches common to the C front ends. */
166 /* Nonzero means don't output line number information. */
168 char flag_no_line_commands;
170 /* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
173 char flag_no_output;
175 /* Nonzero means dump macros in some fashion. */
177 char flag_dump_macros;
179 /* Nonzero means pass #include lines through to the output. */
181 char flag_dump_includes;
183 /* Nonzero means process PCH files while preprocessing. */
185 bool flag_pch_preprocess;
187 /* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
190 const char *pch_file;
192 /* Nonzero if an ISO standard was selected. It rejects macros in the
193 user's namespace. */
194 int flag_iso;
196 /* C/ObjC language option variables. */
199 /* Nonzero means allow type mismatches in conditional expressions;
200 just make their values `void'. */
202 int flag_cond_mismatch;
204 /* Nonzero means enable C89 Amendment 1 features. */
206 int flag_isoc94;
208 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
210 int flag_isoc99;
212 /* Nonzero means use the ISO C11 dialect of C. */
214 int flag_isoc11;
216 /* Nonzero means that we have builtin functions, and main is an int. */
218 int flag_hosted = 1;
221 /* ObjC language option variables. */
224 /* Tells the compiler that this is a special run. Do not perform any
225 compiling, instead we are to test some platform dependent features
226 and output a C header file with appropriate definitions. */
228 int print_struct_values;
230 /* Tells the compiler what is the constant string class for ObjC. */
232 const char *constant_string_class_name;
235 /* C++ language option variables. */
238 /* Nonzero means generate separate instantiation control files and
239 juggle them at link time. */
241 int flag_use_repository;
243 /* The C++ dialect being used. C++98 is the default. */
245 enum cxx_dialect cxx_dialect = cxx98;
247 /* Maximum template instantiation depth. This limit exists to limit the
248 time it takes to notice excessively recursive template instantiations.
250 The default is lower than the 1024 recommended by the C++0x standard
251 because G++ runs out of stack before 1024 with highly recursive template
252 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
254 int max_tinst_depth = 900;
256 /* The elements of `ridpointers' are identifier nodes for the reserved
257 type names and storage classes. It is indexed by a RID_... value. */
258 tree *ridpointers;
260 tree (*make_fname_decl) (location_t, tree, int);
262 /* Nonzero means don't warn about problems that occur when the code is
263 executed. */
264 int c_inhibit_evaluation_warnings;
266 /* Whether we are building a boolean conversion inside
267 convert_for_assignment, or some other late binary operation. If
268 build_binary_op is called for C (from code shared by C and C++) in
269 this case, then the operands have already been folded and the
270 result will not be folded again, so C_MAYBE_CONST_EXPR should not
271 be generated. */
272 bool in_late_binary_op;
274 /* Whether lexing has been completed, so subsequent preprocessor
275 errors should use the compiler's input_location. */
276 bool done_lexing = false;
278 /* Information about how a function name is generated. */
279 struct fname_var_t
281 tree *const decl; /* pointer to the VAR_DECL. */
282 const unsigned rid; /* RID number for the identifier. */
283 const int pretty; /* How pretty is it? */
286 /* The three ways of getting then name of the current function. */
288 const struct fname_var_t fname_vars[] =
290 /* C99 compliant __func__, must be first. */
291 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
292 /* GCC __FUNCTION__ compliant. */
293 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
294 /* GCC __PRETTY_FUNCTION__ compliant. */
295 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
296 {NULL, 0, 0},
299 /* Global visibility options. */
300 struct visibility_flags visibility_options;
302 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
303 static tree check_case_value (tree);
304 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
306 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
312 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
313 int, bool *);
314 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
315 int, bool *);
316 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
317 bool *);
318 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_always_inline_attribute (tree *, tree, tree, int,
322 bool *);
323 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
330 bool *);
331 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_transparent_union_attribute (tree *, tree, tree,
333 int, bool *);
334 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
340 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
341 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
344 static tree handle_visibility_attribute (tree *, tree, tree, int,
345 bool *);
346 static tree handle_tls_model_attribute (tree *, tree, tree, int,
347 bool *);
348 static tree handle_no_instrument_function_attribute (tree *, tree,
349 tree, int, bool *);
350 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
353 bool *);
354 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_deprecated_attribute (tree *, tree, tree, int,
359 bool *);
360 static tree handle_vector_size_attribute (tree *, tree, tree, int,
361 bool *);
362 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
366 bool *);
367 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
374 static tree ignore_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 *);
377 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
380 bool *);
381 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
382 bool *);
384 static void check_function_nonnull (tree, int, tree *);
385 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
386 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
387 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
388 static int resort_field_decl_cmp (const void *, const void *);
390 /* Reserved words. The third field is a mask: keywords are disabled
391 if they match the mask.
393 Masks for languages:
394 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
395 C --std=c99: D_CXXONLY | D_OBJC
396 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
397 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
398 C++ --std=c0x: D_CONLY | D_OBJC
399 ObjC++ is like C++ except that D_OBJC is not set
401 If -fno-asm is used, D_ASM is added to the mask. If
402 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
403 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
404 In C with -Wc++-compat, we warn if D_CXXWARN is set.
406 Note the complication of the D_CXX_OBJC keywords. These are
407 reserved words such as 'class'. In C++, 'class' is a reserved
408 word. In Objective-C++ it is too. In Objective-C, it is a
409 reserved word too, but only if it follows an '@' sign.
411 const struct c_common_resword c_common_reswords[] =
413 { "_Alignas", RID_ALIGNAS, D_CONLY },
414 { "_Alignof", RID_ALIGNOF, D_CONLY },
415 { "_Atomic", RID_ATOMIC, D_CONLY },
416 { "_Bool", RID_BOOL, D_CONLY },
417 { "_Complex", RID_COMPLEX, 0 },
418 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
419 { "_Cilk_sync", RID_CILK_SYNC, 0 },
420 { "_Imaginary", RID_IMAGINARY, D_CONLY },
421 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
422 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
423 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
424 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
425 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
426 { "_Sat", RID_SAT, D_CONLY | D_EXT },
427 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
428 { "_Noreturn", RID_NORETURN, D_CONLY },
429 { "_Generic", RID_GENERIC, D_CONLY },
430 { "_Thread_local", RID_THREAD, D_CONLY },
431 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
432 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
433 { "__alignof", RID_ALIGNOF, 0 },
434 { "__alignof__", RID_ALIGNOF, 0 },
435 { "__asm", RID_ASM, 0 },
436 { "__asm__", RID_ASM, 0 },
437 { "__attribute", RID_ATTRIBUTE, 0 },
438 { "__attribute__", RID_ATTRIBUTE, 0 },
439 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
440 { "__bases", RID_BASES, D_CXXONLY },
441 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
442 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
443 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
444 { "__builtin_offsetof", RID_OFFSETOF, 0 },
445 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
446 { "__builtin_va_arg", RID_VA_ARG, 0 },
447 { "__complex", RID_COMPLEX, 0 },
448 { "__complex__", RID_COMPLEX, 0 },
449 { "__const", RID_CONST, 0 },
450 { "__const__", RID_CONST, 0 },
451 { "__decltype", RID_DECLTYPE, D_CXXONLY },
452 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
453 { "__extension__", RID_EXTENSION, 0 },
454 { "__func__", RID_C99_FUNCTION_NAME, 0 },
455 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
456 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
457 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
458 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
459 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
460 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
461 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
462 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
463 { "__imag", RID_IMAGPART, 0 },
464 { "__imag__", RID_IMAGPART, 0 },
465 { "__inline", RID_INLINE, 0 },
466 { "__inline__", RID_INLINE, 0 },
467 { "__int128", RID_INT128, 0 },
468 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
469 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
470 { "__is_class", RID_IS_CLASS, D_CXXONLY },
471 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
472 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
473 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
474 { "__is_final", RID_IS_FINAL, D_CXXONLY },
475 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
476 { "__is_pod", RID_IS_POD, D_CXXONLY },
477 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
478 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
479 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
480 { "__is_union", RID_IS_UNION, D_CXXONLY },
481 { "__label__", RID_LABEL, 0 },
482 { "__null", RID_NULL, 0 },
483 { "__real", RID_REALPART, 0 },
484 { "__real__", RID_REALPART, 0 },
485 { "__restrict", RID_RESTRICT, 0 },
486 { "__restrict__", RID_RESTRICT, 0 },
487 { "__signed", RID_SIGNED, 0 },
488 { "__signed__", RID_SIGNED, 0 },
489 { "__thread", RID_THREAD, 0 },
490 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
491 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
492 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
493 { "__typeof", RID_TYPEOF, 0 },
494 { "__typeof__", RID_TYPEOF, 0 },
495 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
496 { "__volatile", RID_VOLATILE, 0 },
497 { "__volatile__", RID_VOLATILE, 0 },
498 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
499 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
500 { "asm", RID_ASM, D_ASM },
501 { "auto", RID_AUTO, 0 },
502 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
503 { "break", RID_BREAK, 0 },
504 { "case", RID_CASE, 0 },
505 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
506 { "char", RID_CHAR, 0 },
507 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
508 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
510 { "const", RID_CONST, 0 },
511 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
512 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
513 { "continue", RID_CONTINUE, 0 },
514 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
515 { "default", RID_DEFAULT, 0 },
516 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
517 { "do", RID_DO, 0 },
518 { "double", RID_DOUBLE, 0 },
519 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
520 { "else", RID_ELSE, 0 },
521 { "enum", RID_ENUM, 0 },
522 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
523 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
524 { "extern", RID_EXTERN, 0 },
525 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
526 { "float", RID_FLOAT, 0 },
527 { "for", RID_FOR, 0 },
528 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
529 { "goto", RID_GOTO, 0 },
530 { "if", RID_IF, 0 },
531 { "inline", RID_INLINE, D_EXT89 },
532 { "int", RID_INT, 0 },
533 { "long", RID_LONG, 0 },
534 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
535 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
536 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
537 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
538 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
539 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
540 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
541 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
542 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
543 { "register", RID_REGISTER, 0 },
544 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
545 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
546 { "return", RID_RETURN, 0 },
547 { "short", RID_SHORT, 0 },
548 { "signed", RID_SIGNED, 0 },
549 { "sizeof", RID_SIZEOF, 0 },
550 { "static", RID_STATIC, 0 },
551 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
552 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
553 { "struct", RID_STRUCT, 0 },
554 { "switch", RID_SWITCH, 0 },
555 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
556 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
557 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
558 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
559 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
560 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
561 { "typedef", RID_TYPEDEF, 0 },
562 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
563 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
564 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
565 { "union", RID_UNION, 0 },
566 { "unsigned", RID_UNSIGNED, 0 },
567 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
568 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
569 { "void", RID_VOID, 0 },
570 { "volatile", RID_VOLATILE, 0 },
571 { "wchar_t", RID_WCHAR, D_CXXONLY },
572 { "while", RID_WHILE, 0 },
573 /* These Objective-C keywords are recognized only immediately after
574 an '@'. */
575 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
576 { "defs", RID_AT_DEFS, D_OBJC },
577 { "encode", RID_AT_ENCODE, D_OBJC },
578 { "end", RID_AT_END, D_OBJC },
579 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
580 { "interface", RID_AT_INTERFACE, D_OBJC },
581 { "protocol", RID_AT_PROTOCOL, D_OBJC },
582 { "selector", RID_AT_SELECTOR, D_OBJC },
583 { "finally", RID_AT_FINALLY, D_OBJC },
584 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
585 { "optional", RID_AT_OPTIONAL, D_OBJC },
586 { "required", RID_AT_REQUIRED, D_OBJC },
587 { "property", RID_AT_PROPERTY, D_OBJC },
588 { "package", RID_AT_PACKAGE, D_OBJC },
589 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
590 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
591 /* These are recognized only in protocol-qualifier context
592 (see above) */
593 { "bycopy", RID_BYCOPY, D_OBJC },
594 { "byref", RID_BYREF, D_OBJC },
595 { "in", RID_IN, D_OBJC },
596 { "inout", RID_INOUT, D_OBJC },
597 { "oneway", RID_ONEWAY, D_OBJC },
598 { "out", RID_OUT, D_OBJC },
599 /* These are recognized inside a property attribute list */
600 { "assign", RID_ASSIGN, D_OBJC },
601 { "copy", RID_COPY, D_OBJC },
602 { "getter", RID_GETTER, D_OBJC },
603 { "nonatomic", RID_NONATOMIC, D_OBJC },
604 { "readonly", RID_READONLY, D_OBJC },
605 { "readwrite", RID_READWRITE, D_OBJC },
606 { "retain", RID_RETAIN, D_OBJC },
607 { "setter", RID_SETTER, D_OBJC },
610 const unsigned int num_c_common_reswords =
611 sizeof c_common_reswords / sizeof (struct c_common_resword);
613 /* Table of machine-independent attributes common to all C-like languages. */
614 const struct attribute_spec c_common_attribute_table[] =
616 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
617 affects_type_identity } */
618 { "packed", 0, 0, false, false, false,
619 handle_packed_attribute , false},
620 { "nocommon", 0, 0, true, false, false,
621 handle_nocommon_attribute, false},
622 { "common", 0, 0, true, false, false,
623 handle_common_attribute, false },
624 /* FIXME: logically, noreturn attributes should be listed as
625 "false, true, true" and apply to function types. But implementing this
626 would require all the places in the compiler that use TREE_THIS_VOLATILE
627 on a decl to identify non-returning functions to be located and fixed
628 to check the function type instead. */
629 { "noreturn", 0, 0, true, false, false,
630 handle_noreturn_attribute, false },
631 { "volatile", 0, 0, true, false, false,
632 handle_noreturn_attribute, false },
633 { "noinline", 0, 0, true, false, false,
634 handle_noinline_attribute, false },
635 { "noclone", 0, 0, true, false, false,
636 handle_noclone_attribute, false },
637 { "leaf", 0, 0, true, false, false,
638 handle_leaf_attribute, false },
639 { "always_inline", 0, 0, true, false, false,
640 handle_always_inline_attribute, false },
641 { "gnu_inline", 0, 0, true, false, false,
642 handle_gnu_inline_attribute, false },
643 { "artificial", 0, 0, true, false, false,
644 handle_artificial_attribute, false },
645 { "flatten", 0, 0, true, false, false,
646 handle_flatten_attribute, false },
647 { "used", 0, 0, true, false, false,
648 handle_used_attribute, false },
649 { "unused", 0, 0, false, false, false,
650 handle_unused_attribute, false },
651 { "externally_visible", 0, 0, true, false, false,
652 handle_externally_visible_attribute, false },
653 /* The same comments as for noreturn attributes apply to const ones. */
654 { "const", 0, 0, true, false, false,
655 handle_const_attribute, false },
656 { "transparent_union", 0, 0, false, false, false,
657 handle_transparent_union_attribute, false },
658 { "constructor", 0, 1, true, false, false,
659 handle_constructor_attribute, false },
660 { "destructor", 0, 1, true, false, false,
661 handle_destructor_attribute, false },
662 { "mode", 1, 1, false, true, false,
663 handle_mode_attribute, false },
664 { "section", 1, 1, true, false, false,
665 handle_section_attribute, false },
666 { "aligned", 0, 1, false, false, false,
667 handle_aligned_attribute, false },
668 { "weak", 0, 0, true, false, false,
669 handle_weak_attribute, false },
670 { "ifunc", 1, 1, true, false, false,
671 handle_ifunc_attribute, false },
672 { "alias", 1, 1, true, false, false,
673 handle_alias_attribute, false },
674 { "weakref", 0, 1, true, false, false,
675 handle_weakref_attribute, false },
676 { "no_instrument_function", 0, 0, true, false, false,
677 handle_no_instrument_function_attribute,
678 false },
679 { "malloc", 0, 0, true, false, false,
680 handle_malloc_attribute, false },
681 { "returns_twice", 0, 0, true, false, false,
682 handle_returns_twice_attribute, false },
683 { "no_stack_limit", 0, 0, true, false, false,
684 handle_no_limit_stack_attribute, false },
685 { "pure", 0, 0, true, false, false,
686 handle_pure_attribute, false },
687 { "transaction_callable", 0, 0, false, true, false,
688 handle_tm_attribute, false },
689 { "transaction_unsafe", 0, 0, false, true, false,
690 handle_tm_attribute, false },
691 { "transaction_safe", 0, 0, false, true, false,
692 handle_tm_attribute, false },
693 { "transaction_may_cancel_outer", 0, 0, false, true, false,
694 handle_tm_attribute, false },
695 /* ??? These two attributes didn't make the transition from the
696 Intel language document to the multi-vendor language document. */
697 { "transaction_pure", 0, 0, false, true, false,
698 handle_tm_attribute, false },
699 { "transaction_wrap", 1, 1, true, false, false,
700 handle_tm_wrap_attribute, false },
701 /* For internal use (marking of builtins) only. The name contains space
702 to prevent its usage in source code. */
703 { "no vops", 0, 0, true, false, false,
704 handle_novops_attribute, false },
705 { "deprecated", 0, 1, false, false, false,
706 handle_deprecated_attribute, false },
707 { "vector_size", 1, 1, false, true, false,
708 handle_vector_size_attribute, false },
709 { "visibility", 1, 1, false, false, false,
710 handle_visibility_attribute, false },
711 { "tls_model", 1, 1, true, false, false,
712 handle_tls_model_attribute, false },
713 { "nonnull", 0, -1, false, true, true,
714 handle_nonnull_attribute, false },
715 { "nothrow", 0, 0, true, false, false,
716 handle_nothrow_attribute, false },
717 { "may_alias", 0, 0, false, true, false, NULL, false },
718 { "cleanup", 1, 1, true, false, false,
719 handle_cleanup_attribute, false },
720 { "warn_unused_result", 0, 0, false, true, true,
721 handle_warn_unused_result_attribute, false },
722 { "sentinel", 0, 1, false, true, true,
723 handle_sentinel_attribute, false },
724 /* For internal use (marking of builtins) only. The name contains space
725 to prevent its usage in source code. */
726 { "type generic", 0, 0, false, true, true,
727 handle_type_generic_attribute, false },
728 { "alloc_size", 1, 2, false, true, true,
729 handle_alloc_size_attribute, false },
730 { "cold", 0, 0, true, false, false,
731 handle_cold_attribute, false },
732 { "hot", 0, 0, true, false, false,
733 handle_hot_attribute, false },
734 { "no_address_safety_analysis",
735 0, 0, true, false, false,
736 handle_no_address_safety_analysis_attribute,
737 false },
738 { "no_sanitize_address", 0, 0, true, false, false,
739 handle_no_sanitize_address_attribute,
740 false },
741 { "no_sanitize_undefined", 0, 0, true, false, false,
742 handle_no_sanitize_undefined_attribute,
743 false },
744 { "warning", 1, 1, true, false, false,
745 handle_error_attribute, false },
746 { "error", 1, 1, true, false, false,
747 handle_error_attribute, false },
748 { "target", 1, -1, true, false, false,
749 handle_target_attribute, false },
750 { "optimize", 1, -1, true, false, false,
751 handle_optimize_attribute, false },
752 /* For internal use only. The leading '*' both prevents its usage in
753 source code and signals that it may be overridden by machine tables. */
754 { "*tm regparm", 0, 0, false, true, true,
755 ignore_attribute, false },
756 { "no_split_stack", 0, 0, true, false, false,
757 handle_no_split_stack_attribute, false },
758 /* For internal use (marking of builtins and runtime functions) only.
759 The name contains space to prevent its usage in source code. */
760 { "fn spec", 1, 1, false, true, true,
761 handle_fnspec_attribute, false },
762 { "warn_unused", 0, 0, false, false, false,
763 handle_warn_unused_attribute, false },
764 { "returns_nonnull", 0, 0, false, true, true,
765 handle_returns_nonnull_attribute, false },
766 { "omp declare simd", 0, -1, true, false, false,
767 handle_omp_declare_simd_attribute, false },
768 { "cilk simd function", 0, -1, true, false, false,
769 handle_omp_declare_simd_attribute, false },
770 { "omp declare target", 0, 0, true, false, false,
771 handle_omp_declare_target_attribute, false },
772 { "alloc_align", 1, 1, false, true, true,
773 handle_alloc_align_attribute, false },
774 { "assume_aligned", 1, 2, false, true, true,
775 handle_assume_aligned_attribute, false },
776 { NULL, 0, 0, false, false, false, NULL, false }
779 /* Give the specifications for the format attributes, used by C and all
780 descendants. */
782 const struct attribute_spec c_common_format_attribute_table[] =
784 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
785 affects_type_identity } */
786 { "format", 3, 3, false, true, true,
787 handle_format_attribute, false },
788 { "format_arg", 1, 1, false, true, true,
789 handle_format_arg_attribute, false },
790 { NULL, 0, 0, false, false, false, NULL, false }
793 /* Return identifier for address space AS. */
795 const char *
796 c_addr_space_name (addr_space_t as)
798 int rid = RID_FIRST_ADDR_SPACE + as;
799 gcc_assert (ridpointers [rid]);
800 return IDENTIFIER_POINTER (ridpointers [rid]);
803 /* Push current bindings for the function name VAR_DECLS. */
805 void
806 start_fname_decls (void)
808 unsigned ix;
809 tree saved = NULL_TREE;
811 for (ix = 0; fname_vars[ix].decl; ix++)
813 tree decl = *fname_vars[ix].decl;
815 if (decl)
817 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
818 saved);
819 *fname_vars[ix].decl = NULL_TREE;
822 if (saved || saved_function_name_decls)
823 /* Normally they'll have been NULL, so only push if we've got a
824 stack, or they are non-NULL. */
825 saved_function_name_decls = tree_cons (saved, NULL_TREE,
826 saved_function_name_decls);
829 /* Finish up the current bindings, adding them into the current function's
830 statement tree. This must be done _before_ finish_stmt_tree is called.
831 If there is no current function, we must be at file scope and no statements
832 are involved. Pop the previous bindings. */
834 void
835 finish_fname_decls (void)
837 unsigned ix;
838 tree stmts = NULL_TREE;
839 tree stack = saved_function_name_decls;
841 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
842 append_to_statement_list (TREE_VALUE (stack), &stmts);
844 if (stmts)
846 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
848 if (TREE_CODE (*bodyp) == BIND_EXPR)
849 bodyp = &BIND_EXPR_BODY (*bodyp);
851 append_to_statement_list_force (*bodyp, &stmts);
852 *bodyp = stmts;
855 for (ix = 0; fname_vars[ix].decl; ix++)
856 *fname_vars[ix].decl = NULL_TREE;
858 if (stack)
860 /* We had saved values, restore them. */
861 tree saved;
863 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
865 tree decl = TREE_PURPOSE (saved);
866 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
868 *fname_vars[ix].decl = decl;
870 stack = TREE_CHAIN (stack);
872 saved_function_name_decls = stack;
875 /* Return the text name of the current function, suitably prettified
876 by PRETTY_P. Return string must be freed by caller. */
878 const char *
879 fname_as_string (int pretty_p)
881 const char *name = "top level";
882 char *namep;
883 int vrb = 2, len;
884 cpp_string cstr = { 0, 0 }, strname;
886 if (!pretty_p)
888 name = "";
889 vrb = 0;
892 if (current_function_decl)
893 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
895 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
897 namep = XNEWVEC (char, len);
898 snprintf (namep, len, "\"%s\"", name);
899 strname.text = (unsigned char *) namep;
900 strname.len = len - 1;
902 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
904 XDELETEVEC (namep);
905 return (const char *) cstr.text;
908 return namep;
911 /* Return the VAR_DECL for a const char array naming the current
912 function. If the VAR_DECL has not yet been created, create it
913 now. RID indicates how it should be formatted and IDENTIFIER_NODE
914 ID is its name (unfortunately C and C++ hold the RID values of
915 keywords in different places, so we can't derive RID from ID in
916 this language independent code. LOC is the location of the
917 function. */
919 tree
920 fname_decl (location_t loc, unsigned int rid, tree id)
922 unsigned ix;
923 tree decl = NULL_TREE;
925 for (ix = 0; fname_vars[ix].decl; ix++)
926 if (fname_vars[ix].rid == rid)
927 break;
929 decl = *fname_vars[ix].decl;
930 if (!decl)
932 /* If a tree is built here, it would normally have the lineno of
933 the current statement. Later this tree will be moved to the
934 beginning of the function and this line number will be wrong.
935 To avoid this problem set the lineno to 0 here; that prevents
936 it from appearing in the RTL. */
937 tree stmts;
938 location_t saved_location = input_location;
939 input_location = UNKNOWN_LOCATION;
941 stmts = push_stmt_list ();
942 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
943 stmts = pop_stmt_list (stmts);
944 if (!IS_EMPTY_STMT (stmts))
945 saved_function_name_decls
946 = tree_cons (decl, stmts, saved_function_name_decls);
947 *fname_vars[ix].decl = decl;
948 input_location = saved_location;
950 if (!ix && !current_function_decl)
951 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
953 return decl;
956 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
958 tree
959 fix_string_type (tree value)
961 int length = TREE_STRING_LENGTH (value);
962 int nchars;
963 tree e_type, i_type, a_type;
965 /* Compute the number of elements, for the array type. */
966 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
968 nchars = length;
969 e_type = char_type_node;
971 else if (TREE_TYPE (value) == char16_array_type_node)
973 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
974 e_type = char16_type_node;
976 else if (TREE_TYPE (value) == char32_array_type_node)
978 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
979 e_type = char32_type_node;
981 else
983 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
984 e_type = wchar_type_node;
987 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
988 limit in C++98 Annex B is very large (65536) and is not normative,
989 so we do not diagnose it (warn_overlength_strings is forced off
990 in c_common_post_options). */
991 if (warn_overlength_strings)
993 const int nchars_max = flag_isoc99 ? 4095 : 509;
994 const int relevant_std = flag_isoc99 ? 99 : 90;
995 if (nchars - 1 > nchars_max)
996 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
997 separate the %d from the 'C'. 'ISO' should not be
998 translated, but it may be moved after 'C%d' in languages
999 where modifiers follow nouns. */
1000 pedwarn (input_location, OPT_Woverlength_strings,
1001 "string length %qd is greater than the length %qd "
1002 "ISO C%d compilers are required to support",
1003 nchars - 1, nchars_max, relevant_std);
1006 /* Create the array type for the string constant. The ISO C++
1007 standard says that a string literal has type `const char[N]' or
1008 `const wchar_t[N]'. We use the same logic when invoked as a C
1009 front-end with -Wwrite-strings.
1010 ??? We should change the type of an expression depending on the
1011 state of a warning flag. We should just be warning -- see how
1012 this is handled in the C++ front-end for the deprecated implicit
1013 conversion from string literals to `char*' or `wchar_t*'.
1015 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1016 array type being the unqualified version of that type.
1017 Therefore, if we are constructing an array of const char, we must
1018 construct the matching unqualified array type first. The C front
1019 end does not require this, but it does no harm, so we do it
1020 unconditionally. */
1021 i_type = build_index_type (size_int (nchars - 1));
1022 a_type = build_array_type (e_type, i_type);
1023 if (c_dialect_cxx() || warn_write_strings)
1024 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1026 TREE_TYPE (value) = a_type;
1027 TREE_CONSTANT (value) = 1;
1028 TREE_READONLY (value) = 1;
1029 TREE_STATIC (value) = 1;
1030 return value;
1033 /* If DISABLE is true, stop issuing warnings. This is used when
1034 parsing code that we know will not be executed. This function may
1035 be called multiple times, and works as a stack. */
1037 static void
1038 c_disable_warnings (bool disable)
1040 if (disable)
1042 ++c_inhibit_evaluation_warnings;
1043 fold_defer_overflow_warnings ();
1047 /* If ENABLE is true, reenable issuing warnings. */
1049 static void
1050 c_enable_warnings (bool enable)
1052 if (enable)
1054 --c_inhibit_evaluation_warnings;
1055 fold_undefer_and_ignore_overflow_warnings ();
1059 /* Fully fold EXPR, an expression that was not folded (beyond integer
1060 constant expressions and null pointer constants) when being built
1061 up. If IN_INIT, this is in a static initializer and certain
1062 changes are made to the folding done. Clear *MAYBE_CONST if
1063 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1064 expression because it contains an evaluated operator (in C99) or an
1065 operator outside of sizeof returning an integer constant (in C90)
1066 not permitted in constant expressions, or because it contains an
1067 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1068 set to true by callers before calling this function.) Return the
1069 folded expression. Function arguments have already been folded
1070 before calling this function, as have the contents of SAVE_EXPR,
1071 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1072 C_MAYBE_CONST_EXPR. */
1074 tree
1075 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1077 tree ret;
1078 tree eptype = NULL_TREE;
1079 bool dummy = true;
1080 bool maybe_const_itself = true;
1081 location_t loc = EXPR_LOCATION (expr);
1083 /* This function is not relevant to C++ because C++ folds while
1084 parsing, and may need changes to be correct for C++ when C++
1085 stops folding while parsing. */
1086 if (c_dialect_cxx ())
1087 gcc_unreachable ();
1089 if (!maybe_const)
1090 maybe_const = &dummy;
1091 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1093 eptype = TREE_TYPE (expr);
1094 expr = TREE_OPERAND (expr, 0);
1096 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1097 &maybe_const_itself);
1098 if (eptype)
1099 ret = fold_convert_loc (loc, eptype, ret);
1100 *maybe_const &= maybe_const_itself;
1101 return ret;
1104 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1105 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1106 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1107 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1108 both evaluated and unevaluated subexpressions while
1109 *MAYBE_CONST_ITSELF is carried from only evaluated
1110 subexpressions). */
1112 static tree
1113 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1114 bool *maybe_const_itself)
1116 tree ret = expr;
1117 enum tree_code code = TREE_CODE (expr);
1118 enum tree_code_class kind = TREE_CODE_CLASS (code);
1119 location_t loc = EXPR_LOCATION (expr);
1120 tree op0, op1, op2, op3;
1121 tree orig_op0, orig_op1, orig_op2;
1122 bool op0_const = true, op1_const = true, op2_const = true;
1123 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1124 bool nowarning = TREE_NO_WARNING (expr);
1125 bool unused_p;
1127 /* This function is not relevant to C++ because C++ folds while
1128 parsing, and may need changes to be correct for C++ when C++
1129 stops folding while parsing. */
1130 if (c_dialect_cxx ())
1131 gcc_unreachable ();
1133 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1134 anything else not counted as an expression cannot usefully be
1135 folded further at this point. */
1136 if (!IS_EXPR_CODE_CLASS (kind)
1137 || kind == tcc_statement
1138 || code == SAVE_EXPR)
1139 return expr;
1141 /* Operands of variable-length expressions (function calls) have
1142 already been folded, as have __builtin_* function calls, and such
1143 expressions cannot occur in constant expressions. */
1144 if (kind == tcc_vl_exp)
1146 *maybe_const_operands = false;
1147 ret = fold (expr);
1148 goto out;
1151 if (code == C_MAYBE_CONST_EXPR)
1153 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1154 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1155 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1156 *maybe_const_operands = false;
1157 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1158 *maybe_const_itself = false;
1159 if (pre && !in_init)
1160 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1161 else
1162 ret = inner;
1163 goto out;
1166 /* Assignment, increment, decrement, function call and comma
1167 operators, and statement expressions, cannot occur in constant
1168 expressions if evaluated / outside of sizeof. (Function calls
1169 were handled above, though VA_ARG_EXPR is treated like a function
1170 call here, and statement expressions are handled through
1171 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1172 switch (code)
1174 case MODIFY_EXPR:
1175 case PREDECREMENT_EXPR:
1176 case PREINCREMENT_EXPR:
1177 case POSTDECREMENT_EXPR:
1178 case POSTINCREMENT_EXPR:
1179 case COMPOUND_EXPR:
1180 *maybe_const_operands = false;
1181 break;
1183 case VA_ARG_EXPR:
1184 case TARGET_EXPR:
1185 case BIND_EXPR:
1186 case OBJ_TYPE_REF:
1187 *maybe_const_operands = false;
1188 ret = fold (expr);
1189 goto out;
1191 default:
1192 break;
1195 /* Fold individual tree codes as appropriate. */
1196 switch (code)
1198 case COMPOUND_LITERAL_EXPR:
1199 /* Any non-constancy will have been marked in a containing
1200 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1201 goto out;
1203 case COMPONENT_REF:
1204 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1205 op1 = TREE_OPERAND (expr, 1);
1206 op2 = TREE_OPERAND (expr, 2);
1207 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1208 maybe_const_itself);
1209 STRIP_TYPE_NOPS (op0);
1210 if (op0 != orig_op0)
1211 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1212 if (ret != expr)
1214 TREE_READONLY (ret) = TREE_READONLY (expr);
1215 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1217 goto out;
1219 case ARRAY_REF:
1220 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1221 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1222 op2 = TREE_OPERAND (expr, 2);
1223 op3 = TREE_OPERAND (expr, 3);
1224 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1225 maybe_const_itself);
1226 STRIP_TYPE_NOPS (op0);
1227 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1228 maybe_const_itself);
1229 STRIP_TYPE_NOPS (op1);
1230 op1 = decl_constant_value_for_optimization (op1);
1231 if (op0 != orig_op0 || op1 != orig_op1)
1232 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1233 if (ret != expr)
1235 TREE_READONLY (ret) = TREE_READONLY (expr);
1236 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1237 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1239 ret = fold (ret);
1240 goto out;
1242 case COMPOUND_EXPR:
1243 case MODIFY_EXPR:
1244 case PREDECREMENT_EXPR:
1245 case PREINCREMENT_EXPR:
1246 case POSTDECREMENT_EXPR:
1247 case POSTINCREMENT_EXPR:
1248 case PLUS_EXPR:
1249 case MINUS_EXPR:
1250 case MULT_EXPR:
1251 case POINTER_PLUS_EXPR:
1252 case TRUNC_DIV_EXPR:
1253 case CEIL_DIV_EXPR:
1254 case FLOOR_DIV_EXPR:
1255 case TRUNC_MOD_EXPR:
1256 case RDIV_EXPR:
1257 case EXACT_DIV_EXPR:
1258 case LSHIFT_EXPR:
1259 case RSHIFT_EXPR:
1260 case BIT_IOR_EXPR:
1261 case BIT_XOR_EXPR:
1262 case BIT_AND_EXPR:
1263 case LT_EXPR:
1264 case LE_EXPR:
1265 case GT_EXPR:
1266 case GE_EXPR:
1267 case EQ_EXPR:
1268 case NE_EXPR:
1269 case COMPLEX_EXPR:
1270 case TRUTH_AND_EXPR:
1271 case TRUTH_OR_EXPR:
1272 case TRUTH_XOR_EXPR:
1273 case UNORDERED_EXPR:
1274 case ORDERED_EXPR:
1275 case UNLT_EXPR:
1276 case UNLE_EXPR:
1277 case UNGT_EXPR:
1278 case UNGE_EXPR:
1279 case UNEQ_EXPR:
1280 /* Binary operations evaluating both arguments (increment and
1281 decrement are binary internally in GCC). */
1282 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1283 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1284 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1285 maybe_const_itself);
1286 STRIP_TYPE_NOPS (op0);
1287 if (code != MODIFY_EXPR
1288 && code != PREDECREMENT_EXPR
1289 && code != PREINCREMENT_EXPR
1290 && code != POSTDECREMENT_EXPR
1291 && code != POSTINCREMENT_EXPR)
1292 op0 = decl_constant_value_for_optimization (op0);
1293 /* The RHS of a MODIFY_EXPR was fully folded when building that
1294 expression for the sake of conversion warnings. */
1295 if (code != MODIFY_EXPR)
1296 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1297 maybe_const_itself);
1298 STRIP_TYPE_NOPS (op1);
1299 op1 = decl_constant_value_for_optimization (op1);
1300 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1301 ret = in_init
1302 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1303 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1304 else
1305 ret = fold (expr);
1306 if (TREE_OVERFLOW_P (ret)
1307 && !TREE_OVERFLOW_P (op0)
1308 && !TREE_OVERFLOW_P (op1))
1309 overflow_warning (EXPR_LOCATION (expr), ret);
1310 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1311 && TREE_CODE (orig_op1) != INTEGER_CST
1312 && TREE_CODE (op1) == INTEGER_CST
1313 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1314 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1315 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1316 && c_inhibit_evaluation_warnings == 0)
1318 if (tree_int_cst_sgn (op1) < 0)
1319 warning_at (loc, 0, (code == LSHIFT_EXPR
1320 ? G_("left shift count is negative")
1321 : G_("right shift count is negative")));
1322 else if (compare_tree_int (op1,
1323 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1324 >= 0)
1325 warning_at (loc, 0, (code == LSHIFT_EXPR
1326 ? G_("left shift count >= width of type")
1327 : G_("right shift count >= width of type")));
1329 goto out;
1331 case INDIRECT_REF:
1332 case FIX_TRUNC_EXPR:
1333 case FLOAT_EXPR:
1334 CASE_CONVERT:
1335 case ADDR_SPACE_CONVERT_EXPR:
1336 case VIEW_CONVERT_EXPR:
1337 case NON_LVALUE_EXPR:
1338 case NEGATE_EXPR:
1339 case BIT_NOT_EXPR:
1340 case TRUTH_NOT_EXPR:
1341 case ADDR_EXPR:
1342 case CONJ_EXPR:
1343 case REALPART_EXPR:
1344 case IMAGPART_EXPR:
1345 /* Unary operations. */
1346 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1347 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1348 maybe_const_itself);
1349 STRIP_TYPE_NOPS (op0);
1350 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1351 op0 = decl_constant_value_for_optimization (op0);
1352 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1353 not prepared to deal with them if they occur in initializers. */
1354 if (op0 != orig_op0
1355 && code == ADDR_EXPR
1356 && (op1 = get_base_address (op0)) != NULL_TREE
1357 && TREE_CODE (op1) == INDIRECT_REF
1358 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1359 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1360 else if (op0 != orig_op0 || in_init)
1361 ret = in_init
1362 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1363 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1364 else
1365 ret = fold (expr);
1366 if (code == INDIRECT_REF
1367 && ret != expr
1368 && TREE_CODE (ret) == INDIRECT_REF)
1370 TREE_READONLY (ret) = TREE_READONLY (expr);
1371 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1372 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1374 switch (code)
1376 case FIX_TRUNC_EXPR:
1377 case FLOAT_EXPR:
1378 CASE_CONVERT:
1379 /* Don't warn about explicit conversions. We will already
1380 have warned about suspect implicit conversions. */
1381 break;
1383 default:
1384 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1385 overflow_warning (EXPR_LOCATION (expr), ret);
1386 break;
1388 goto out;
1390 case TRUTH_ANDIF_EXPR:
1391 case TRUTH_ORIF_EXPR:
1392 /* Binary operations not necessarily evaluating both
1393 arguments. */
1394 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1395 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1396 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1397 STRIP_TYPE_NOPS (op0);
1399 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1400 ? truthvalue_false_node
1401 : truthvalue_true_node));
1402 c_disable_warnings (unused_p);
1403 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1404 STRIP_TYPE_NOPS (op1);
1405 c_enable_warnings (unused_p);
1407 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1408 ret = in_init
1409 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1410 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1411 else
1412 ret = fold (expr);
1413 *maybe_const_operands &= op0_const;
1414 *maybe_const_itself &= op0_const_self;
1415 if (!(flag_isoc99
1416 && op0_const
1417 && op0_const_self
1418 && (code == TRUTH_ANDIF_EXPR
1419 ? op0 == truthvalue_false_node
1420 : op0 == truthvalue_true_node)))
1421 *maybe_const_operands &= op1_const;
1422 if (!(op0_const
1423 && op0_const_self
1424 && (code == TRUTH_ANDIF_EXPR
1425 ? op0 == truthvalue_false_node
1426 : op0 == truthvalue_true_node)))
1427 *maybe_const_itself &= op1_const_self;
1428 goto out;
1430 case COND_EXPR:
1431 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1432 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1433 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1434 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1436 STRIP_TYPE_NOPS (op0);
1437 c_disable_warnings (op0 == truthvalue_false_node);
1438 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1439 STRIP_TYPE_NOPS (op1);
1440 c_enable_warnings (op0 == truthvalue_false_node);
1442 c_disable_warnings (op0 == truthvalue_true_node);
1443 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1444 STRIP_TYPE_NOPS (op2);
1445 c_enable_warnings (op0 == truthvalue_true_node);
1447 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1448 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1449 else
1450 ret = fold (expr);
1451 *maybe_const_operands &= op0_const;
1452 *maybe_const_itself &= op0_const_self;
1453 if (!(flag_isoc99
1454 && op0_const
1455 && op0_const_self
1456 && op0 == truthvalue_false_node))
1457 *maybe_const_operands &= op1_const;
1458 if (!(op0_const
1459 && op0_const_self
1460 && op0 == truthvalue_false_node))
1461 *maybe_const_itself &= op1_const_self;
1462 if (!(flag_isoc99
1463 && op0_const
1464 && op0_const_self
1465 && op0 == truthvalue_true_node))
1466 *maybe_const_operands &= op2_const;
1467 if (!(op0_const
1468 && op0_const_self
1469 && op0 == truthvalue_true_node))
1470 *maybe_const_itself &= op2_const_self;
1471 goto out;
1473 case EXCESS_PRECISION_EXPR:
1474 /* Each case where an operand with excess precision may be
1475 encountered must remove the EXCESS_PRECISION_EXPR around
1476 inner operands and possibly put one around the whole
1477 expression or possibly convert to the semantic type (which
1478 c_fully_fold does); we cannot tell at this stage which is
1479 appropriate in any particular case. */
1480 gcc_unreachable ();
1482 default:
1483 /* Various codes may appear through folding built-in functions
1484 and their arguments. */
1485 goto out;
1488 out:
1489 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1490 have been done by this point, so remove them again. */
1491 nowarning |= TREE_NO_WARNING (ret);
1492 STRIP_TYPE_NOPS (ret);
1493 if (nowarning && !TREE_NO_WARNING (ret))
1495 if (!CAN_HAVE_LOCATION_P (ret))
1496 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1497 TREE_NO_WARNING (ret) = 1;
1499 if (ret != expr)
1500 protected_set_expr_location (ret, loc);
1501 return ret;
1504 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1505 return EXP. Otherwise, return either EXP or its known constant
1506 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1507 Is the BLKmode test appropriate? */
1509 tree
1510 decl_constant_value_for_optimization (tree exp)
1512 tree ret;
1514 /* This function is only used by C, for c_fully_fold and other
1515 optimization, and may not be correct for C++. */
1516 if (c_dialect_cxx ())
1517 gcc_unreachable ();
1519 if (!optimize
1520 || TREE_CODE (exp) != VAR_DECL
1521 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1522 || DECL_MODE (exp) == BLKmode)
1523 return exp;
1525 ret = decl_constant_value (exp);
1526 /* Avoid unwanted tree sharing between the initializer and current
1527 function's body where the tree can be modified e.g. by the
1528 gimplifier. */
1529 if (ret != exp && TREE_STATIC (exp))
1530 ret = unshare_expr (ret);
1531 return ret;
1534 /* Print a warning if a constant expression had overflow in folding.
1535 Invoke this function on every expression that the language
1536 requires to be a constant expression.
1537 Note the ANSI C standard says it is erroneous for a
1538 constant expression to overflow. */
1540 void
1541 constant_expression_warning (tree value)
1543 if (warn_overflow && pedantic
1544 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1545 || TREE_CODE (value) == FIXED_CST
1546 || TREE_CODE (value) == VECTOR_CST
1547 || TREE_CODE (value) == COMPLEX_CST)
1548 && TREE_OVERFLOW (value))
1549 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1552 /* The same as above but print an unconditional error. */
1553 void
1554 constant_expression_error (tree value)
1556 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1557 || TREE_CODE (value) == FIXED_CST
1558 || TREE_CODE (value) == VECTOR_CST
1559 || TREE_CODE (value) == COMPLEX_CST)
1560 && TREE_OVERFLOW (value))
1561 error ("overflow in constant expression");
1564 /* Print a warning if an expression had overflow in folding and its
1565 operands hadn't.
1567 Invoke this function on every expression that
1568 (1) appears in the source code, and
1569 (2) is a constant expression that overflowed, and
1570 (3) is not already checked by convert_and_check;
1571 however, do not invoke this function on operands of explicit casts
1572 or when the expression is the result of an operator and any operand
1573 already overflowed. */
1575 void
1576 overflow_warning (location_t loc, tree value)
1578 if (c_inhibit_evaluation_warnings != 0)
1579 return;
1581 switch (TREE_CODE (value))
1583 case INTEGER_CST:
1584 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1585 break;
1587 case REAL_CST:
1588 warning_at (loc, OPT_Woverflow,
1589 "floating point overflow in expression");
1590 break;
1592 case FIXED_CST:
1593 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1594 break;
1596 case VECTOR_CST:
1597 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1598 break;
1600 case COMPLEX_CST:
1601 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1602 warning_at (loc, OPT_Woverflow,
1603 "complex integer overflow in expression");
1604 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1605 warning_at (loc, OPT_Woverflow,
1606 "complex floating point overflow in expression");
1607 break;
1609 default:
1610 break;
1614 /* Warn about uses of logical || / && operator in a context where it
1615 is likely that the bitwise equivalent was intended by the
1616 programmer. We have seen an expression in which CODE is a binary
1617 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1618 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1619 void
1620 warn_logical_operator (location_t location, enum tree_code code, tree type,
1621 enum tree_code code_left, tree op_left,
1622 enum tree_code ARG_UNUSED (code_right), tree op_right)
1624 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1625 int in0_p, in1_p, in_p;
1626 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1627 bool strict_overflow_p = false;
1629 if (code != TRUTH_ANDIF_EXPR
1630 && code != TRUTH_AND_EXPR
1631 && code != TRUTH_ORIF_EXPR
1632 && code != TRUTH_OR_EXPR)
1633 return;
1635 /* Warn if &&/|| are being used in a context where it is
1636 likely that the bitwise equivalent was intended by the
1637 programmer. That is, an expression such as op && MASK
1638 where op should not be any boolean expression, nor a
1639 constant, and mask seems to be a non-boolean integer constant. */
1640 if (!truth_value_p (code_left)
1641 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1642 && !CONSTANT_CLASS_P (op_left)
1643 && !TREE_NO_WARNING (op_left)
1644 && TREE_CODE (op_right) == INTEGER_CST
1645 && !integer_zerop (op_right)
1646 && !integer_onep (op_right))
1648 if (or_op)
1649 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1650 " applied to non-boolean constant");
1651 else
1652 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1653 " applied to non-boolean constant");
1654 TREE_NO_WARNING (op_left) = true;
1655 return;
1658 /* We do not warn for constants because they are typical of macro
1659 expansions that test for features. */
1660 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1661 return;
1663 /* This warning only makes sense with logical operands. */
1664 if (!(truth_value_p (TREE_CODE (op_left))
1665 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1666 || !(truth_value_p (TREE_CODE (op_right))
1667 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1668 return;
1671 /* We first test whether either side separately is trivially true
1672 (with OR) or trivially false (with AND). If so, do not warn.
1673 This is a common idiom for testing ranges of data types in
1674 portable code. */
1675 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1676 if (!lhs)
1677 return;
1678 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1679 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1681 /* If this is an OR operation, invert both sides; now, the result
1682 should be always false to get a warning. */
1683 if (or_op)
1684 in0_p = !in0_p;
1686 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1687 if (tem && integer_zerop (tem))
1688 return;
1690 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1691 if (!rhs)
1692 return;
1693 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1694 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1696 /* If this is an OR operation, invert both sides; now, the result
1697 should be always false to get a warning. */
1698 if (or_op)
1699 in1_p = !in1_p;
1701 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1702 if (tem && integer_zerop (tem))
1703 return;
1705 /* If both expressions have the same operand, if we can merge the
1706 ranges, and if the range test is always false, then warn. */
1707 if (operand_equal_p (lhs, rhs, 0)
1708 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1709 in1_p, low1, high1)
1710 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1711 type, lhs, in_p, low, high))
1712 && integer_zerop (tem))
1714 if (or_op)
1715 warning_at (location, OPT_Wlogical_op,
1716 "logical %<or%> "
1717 "of collectively exhaustive tests is always true");
1718 else
1719 warning_at (location, OPT_Wlogical_op,
1720 "logical %<and%> "
1721 "of mutually exclusive tests is always false");
1725 /* Warn about logical not used on the left hand side operand of a comparison.
1726 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1727 Do not warn if the LHS or RHS is of a boolean or a vector type. */
1729 void
1730 warn_logical_not_parentheses (location_t location, enum tree_code code,
1731 tree lhs, tree rhs)
1733 if (TREE_CODE_CLASS (code) != tcc_comparison)
1734 return;
1735 if (TREE_TYPE (lhs) == NULL_TREE
1736 || TREE_TYPE (rhs) == NULL_TREE)
1738 else if (TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE
1739 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE
1740 || VECTOR_TYPE_P (TREE_TYPE (lhs))
1741 || VECTOR_TYPE_P (TREE_TYPE (rhs)))
1742 return;
1744 warning_at (location, OPT_Wlogical_not_parentheses,
1745 "logical not is only applied to the left hand side of "
1746 "comparison");
1749 /* Warn if EXP contains any computations whose results are not used.
1750 Return true if a warning is printed; false otherwise. LOCUS is the
1751 (potential) location of the expression. */
1753 bool
1754 warn_if_unused_value (const_tree exp, location_t locus)
1756 restart:
1757 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1758 return false;
1760 /* Don't warn about void constructs. This includes casting to void,
1761 void function calls, and statement expressions with a final cast
1762 to void. */
1763 if (VOID_TYPE_P (TREE_TYPE (exp)))
1764 return false;
1766 if (EXPR_HAS_LOCATION (exp))
1767 locus = EXPR_LOCATION (exp);
1769 switch (TREE_CODE (exp))
1771 case PREINCREMENT_EXPR:
1772 case POSTINCREMENT_EXPR:
1773 case PREDECREMENT_EXPR:
1774 case POSTDECREMENT_EXPR:
1775 case MODIFY_EXPR:
1776 case INIT_EXPR:
1777 case TARGET_EXPR:
1778 case CALL_EXPR:
1779 case TRY_CATCH_EXPR:
1780 case WITH_CLEANUP_EXPR:
1781 case EXIT_EXPR:
1782 case VA_ARG_EXPR:
1783 return false;
1785 case BIND_EXPR:
1786 /* For a binding, warn if no side effect within it. */
1787 exp = BIND_EXPR_BODY (exp);
1788 goto restart;
1790 case SAVE_EXPR:
1791 case NON_LVALUE_EXPR:
1792 case NOP_EXPR:
1793 exp = TREE_OPERAND (exp, 0);
1794 goto restart;
1796 case TRUTH_ORIF_EXPR:
1797 case TRUTH_ANDIF_EXPR:
1798 /* In && or ||, warn if 2nd operand has no side effect. */
1799 exp = TREE_OPERAND (exp, 1);
1800 goto restart;
1802 case COMPOUND_EXPR:
1803 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1804 return true;
1805 /* Let people do `(foo (), 0)' without a warning. */
1806 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1807 return false;
1808 exp = TREE_OPERAND (exp, 1);
1809 goto restart;
1811 case COND_EXPR:
1812 /* If this is an expression with side effects, don't warn; this
1813 case commonly appears in macro expansions. */
1814 if (TREE_SIDE_EFFECTS (exp))
1815 return false;
1816 goto warn;
1818 case INDIRECT_REF:
1819 /* Don't warn about automatic dereferencing of references, since
1820 the user cannot control it. */
1821 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1823 exp = TREE_OPERAND (exp, 0);
1824 goto restart;
1826 /* Fall through. */
1828 default:
1829 /* Referencing a volatile value is a side effect, so don't warn. */
1830 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1831 && TREE_THIS_VOLATILE (exp))
1832 return false;
1834 /* If this is an expression which has no operands, there is no value
1835 to be unused. There are no such language-independent codes,
1836 but front ends may define such. */
1837 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1838 return false;
1840 warn:
1841 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1846 /* Print a warning about casts that might indicate violation
1847 of strict aliasing rules if -Wstrict-aliasing is used and
1848 strict aliasing mode is in effect. OTYPE is the original
1849 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1851 bool
1852 strict_aliasing_warning (tree otype, tree type, tree expr)
1854 /* Strip pointer conversion chains and get to the correct original type. */
1855 STRIP_NOPS (expr);
1856 otype = TREE_TYPE (expr);
1858 if (!(flag_strict_aliasing
1859 && POINTER_TYPE_P (type)
1860 && POINTER_TYPE_P (otype)
1861 && !VOID_TYPE_P (TREE_TYPE (type)))
1862 /* If the type we are casting to is a ref-all pointer
1863 dereferencing it is always valid. */
1864 || TYPE_REF_CAN_ALIAS_ALL (type))
1865 return false;
1867 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1868 && (DECL_P (TREE_OPERAND (expr, 0))
1869 || handled_component_p (TREE_OPERAND (expr, 0))))
1871 /* Casting the address of an object to non void pointer. Warn
1872 if the cast breaks type based aliasing. */
1873 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1875 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1876 "might break strict-aliasing rules");
1877 return true;
1879 else
1881 /* warn_strict_aliasing >= 3. This includes the default (3).
1882 Only warn if the cast is dereferenced immediately. */
1883 alias_set_type set1 =
1884 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1885 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1887 if (set1 != set2 && set2 != 0
1888 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1890 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1891 "pointer will break strict-aliasing rules");
1892 return true;
1894 else if (warn_strict_aliasing == 2
1895 && !alias_sets_must_conflict_p (set1, set2))
1897 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1898 "pointer might break strict-aliasing rules");
1899 return true;
1903 else
1904 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1906 /* At this level, warn for any conversions, even if an address is
1907 not taken in the same statement. This will likely produce many
1908 false positives, but could be useful to pinpoint problems that
1909 are not revealed at higher levels. */
1910 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1911 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1912 if (!COMPLETE_TYPE_P (type)
1913 || !alias_sets_must_conflict_p (set1, set2))
1915 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1916 "pointer might break strict-aliasing rules");
1917 return true;
1921 return false;
1924 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1925 sizeof as last operand of certain builtins. */
1927 void
1928 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1929 vec<tree, va_gc> *params, tree *sizeof_arg,
1930 bool (*comp_types) (tree, tree))
1932 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1933 bool strop = false, cmp = false;
1934 unsigned int idx = ~0;
1935 location_t loc;
1937 if (TREE_CODE (callee) != FUNCTION_DECL
1938 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1939 || vec_safe_length (params) <= 1)
1940 return;
1942 switch (DECL_FUNCTION_CODE (callee))
1944 case BUILT_IN_STRNCMP:
1945 case BUILT_IN_STRNCASECMP:
1946 cmp = true;
1947 /* FALLTHRU */
1948 case BUILT_IN_STRNCPY:
1949 case BUILT_IN_STRNCPY_CHK:
1950 case BUILT_IN_STRNCAT:
1951 case BUILT_IN_STRNCAT_CHK:
1952 case BUILT_IN_STPNCPY:
1953 case BUILT_IN_STPNCPY_CHK:
1954 strop = true;
1955 /* FALLTHRU */
1956 case BUILT_IN_MEMCPY:
1957 case BUILT_IN_MEMCPY_CHK:
1958 case BUILT_IN_MEMMOVE:
1959 case BUILT_IN_MEMMOVE_CHK:
1960 if (params->length () < 3)
1961 return;
1962 src = (*params)[1];
1963 dest = (*params)[0];
1964 idx = 2;
1965 break;
1966 case BUILT_IN_BCOPY:
1967 if (params->length () < 3)
1968 return;
1969 src = (*params)[0];
1970 dest = (*params)[1];
1971 idx = 2;
1972 break;
1973 case BUILT_IN_MEMCMP:
1974 case BUILT_IN_BCMP:
1975 if (params->length () < 3)
1976 return;
1977 src = (*params)[1];
1978 dest = (*params)[0];
1979 idx = 2;
1980 cmp = true;
1981 break;
1982 case BUILT_IN_MEMSET:
1983 case BUILT_IN_MEMSET_CHK:
1984 if (params->length () < 3)
1985 return;
1986 dest = (*params)[0];
1987 idx = 2;
1988 break;
1989 case BUILT_IN_BZERO:
1990 dest = (*params)[0];
1991 idx = 1;
1992 break;
1993 case BUILT_IN_STRNDUP:
1994 src = (*params)[0];
1995 strop = true;
1996 idx = 1;
1997 break;
1998 case BUILT_IN_MEMCHR:
1999 if (params->length () < 3)
2000 return;
2001 src = (*params)[0];
2002 idx = 2;
2003 break;
2004 case BUILT_IN_SNPRINTF:
2005 case BUILT_IN_SNPRINTF_CHK:
2006 case BUILT_IN_VSNPRINTF:
2007 case BUILT_IN_VSNPRINTF_CHK:
2008 dest = (*params)[0];
2009 idx = 1;
2010 strop = true;
2011 break;
2012 default:
2013 break;
2016 if (idx >= 3)
2017 return;
2019 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2020 return;
2022 type = TYPE_P (sizeof_arg[idx])
2023 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2024 if (!POINTER_TYPE_P (type))
2025 return;
2027 if (dest
2028 && (tem = tree_strip_nop_conversions (dest))
2029 && POINTER_TYPE_P (TREE_TYPE (tem))
2030 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2031 return;
2033 if (src
2034 && (tem = tree_strip_nop_conversions (src))
2035 && POINTER_TYPE_P (TREE_TYPE (tem))
2036 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2037 return;
2039 loc = sizeof_arg_loc[idx];
2041 if (dest && !cmp)
2043 if (!TYPE_P (sizeof_arg[idx])
2044 && operand_equal_p (dest, sizeof_arg[idx], 0)
2045 && comp_types (TREE_TYPE (dest), type))
2047 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2048 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2049 "argument to %<sizeof%> in %qD call is the same "
2050 "expression as the destination; did you mean to "
2051 "remove the addressof?", callee);
2052 else if ((TYPE_PRECISION (TREE_TYPE (type))
2053 == TYPE_PRECISION (char_type_node))
2054 || strop)
2055 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2056 "argument to %<sizeof%> in %qD call is the same "
2057 "expression as the destination; did you mean to "
2058 "provide an explicit length?", callee);
2059 else
2060 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2061 "argument to %<sizeof%> in %qD call is the same "
2062 "expression as the destination; did you mean to "
2063 "dereference it?", callee);
2064 return;
2067 if (POINTER_TYPE_P (TREE_TYPE (dest))
2068 && !strop
2069 && comp_types (TREE_TYPE (dest), type)
2070 && !VOID_TYPE_P (TREE_TYPE (type)))
2072 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2073 "argument to %<sizeof%> in %qD call is the same "
2074 "pointer type %qT as the destination; expected %qT "
2075 "or an explicit length", callee, TREE_TYPE (dest),
2076 TREE_TYPE (TREE_TYPE (dest)));
2077 return;
2081 if (src && !cmp)
2083 if (!TYPE_P (sizeof_arg[idx])
2084 && operand_equal_p (src, sizeof_arg[idx], 0)
2085 && comp_types (TREE_TYPE (src), type))
2087 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2088 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2089 "argument to %<sizeof%> in %qD call is the same "
2090 "expression as the source; did you mean to "
2091 "remove the addressof?", callee);
2092 else if ((TYPE_PRECISION (TREE_TYPE (type))
2093 == TYPE_PRECISION (char_type_node))
2094 || strop)
2095 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2096 "argument to %<sizeof%> in %qD call is the same "
2097 "expression as the source; did you mean to "
2098 "provide an explicit length?", callee);
2099 else
2100 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2101 "argument to %<sizeof%> in %qD call is the same "
2102 "expression as the source; did you mean to "
2103 "dereference it?", callee);
2104 return;
2107 if (POINTER_TYPE_P (TREE_TYPE (src))
2108 && !strop
2109 && comp_types (TREE_TYPE (src), type)
2110 && !VOID_TYPE_P (TREE_TYPE (type)))
2112 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2113 "argument to %<sizeof%> in %qD call is the same "
2114 "pointer type %qT as the source; expected %qT "
2115 "or an explicit length", callee, TREE_TYPE (src),
2116 TREE_TYPE (TREE_TYPE (src)));
2117 return;
2121 if (dest)
2123 if (!TYPE_P (sizeof_arg[idx])
2124 && operand_equal_p (dest, sizeof_arg[idx], 0)
2125 && comp_types (TREE_TYPE (dest), type))
2127 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2128 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2129 "argument to %<sizeof%> in %qD call is the same "
2130 "expression as the first source; did you mean to "
2131 "remove the addressof?", callee);
2132 else if ((TYPE_PRECISION (TREE_TYPE (type))
2133 == TYPE_PRECISION (char_type_node))
2134 || strop)
2135 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2136 "argument to %<sizeof%> in %qD call is the same "
2137 "expression as the first source; did you mean to "
2138 "provide an explicit length?", callee);
2139 else
2140 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2141 "argument to %<sizeof%> in %qD call is the same "
2142 "expression as the first source; did you mean to "
2143 "dereference it?", callee);
2144 return;
2147 if (POINTER_TYPE_P (TREE_TYPE (dest))
2148 && !strop
2149 && comp_types (TREE_TYPE (dest), type)
2150 && !VOID_TYPE_P (TREE_TYPE (type)))
2152 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2153 "argument to %<sizeof%> in %qD call is the same "
2154 "pointer type %qT as the first source; expected %qT "
2155 "or an explicit length", callee, TREE_TYPE (dest),
2156 TREE_TYPE (TREE_TYPE (dest)));
2157 return;
2161 if (src)
2163 if (!TYPE_P (sizeof_arg[idx])
2164 && operand_equal_p (src, sizeof_arg[idx], 0)
2165 && comp_types (TREE_TYPE (src), type))
2167 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2168 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2169 "argument to %<sizeof%> in %qD call is the same "
2170 "expression as the second source; did you mean to "
2171 "remove the addressof?", callee);
2172 else if ((TYPE_PRECISION (TREE_TYPE (type))
2173 == TYPE_PRECISION (char_type_node))
2174 || strop)
2175 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2176 "argument to %<sizeof%> in %qD call is the same "
2177 "expression as the second source; did you mean to "
2178 "provide an explicit length?", callee);
2179 else
2180 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2181 "argument to %<sizeof%> in %qD call is the same "
2182 "expression as the second source; did you mean to "
2183 "dereference it?", callee);
2184 return;
2187 if (POINTER_TYPE_P (TREE_TYPE (src))
2188 && !strop
2189 && comp_types (TREE_TYPE (src), type)
2190 && !VOID_TYPE_P (TREE_TYPE (type)))
2192 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2193 "argument to %<sizeof%> in %qD call is the same "
2194 "pointer type %qT as the second source; expected %qT "
2195 "or an explicit length", callee, TREE_TYPE (src),
2196 TREE_TYPE (TREE_TYPE (src)));
2197 return;
2203 /* Warn for unlikely, improbable, or stupid DECL declarations
2204 of `main'. */
2206 void
2207 check_main_parameter_types (tree decl)
2209 function_args_iterator iter;
2210 tree type;
2211 int argct = 0;
2213 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2215 /* XXX void_type_node belies the abstraction. */
2216 if (type == void_type_node || type == error_mark_node )
2217 break;
2219 tree t = type;
2220 if (TYPE_ATOMIC (t))
2221 pedwarn (input_location, OPT_Wmain,
2222 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2223 type, decl);
2224 while (POINTER_TYPE_P (t))
2226 t = TREE_TYPE (t);
2227 if (TYPE_ATOMIC (t))
2228 pedwarn (input_location, OPT_Wmain,
2229 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2230 type, decl);
2233 ++argct;
2234 switch (argct)
2236 case 1:
2237 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2238 pedwarn (input_location, OPT_Wmain,
2239 "first argument of %q+D should be %<int%>", decl);
2240 break;
2242 case 2:
2243 if (TREE_CODE (type) != POINTER_TYPE
2244 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2245 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2246 != char_type_node))
2247 pedwarn (input_location, OPT_Wmain,
2248 "second argument of %q+D should be %<char **%>", decl);
2249 break;
2251 case 3:
2252 if (TREE_CODE (type) != POINTER_TYPE
2253 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2254 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2255 != char_type_node))
2256 pedwarn (input_location, OPT_Wmain,
2257 "third argument of %q+D should probably be "
2258 "%<char **%>", decl);
2259 break;
2263 /* It is intentional that this message does not mention the third
2264 argument because it's only mentioned in an appendix of the
2265 standard. */
2266 if (argct > 0 && (argct < 2 || argct > 3))
2267 pedwarn (input_location, OPT_Wmain,
2268 "%q+D takes only zero or two arguments", decl);
2270 if (stdarg_p (TREE_TYPE (decl)))
2271 pedwarn (input_location, OPT_Wmain,
2272 "%q+D declared as variadic function", decl);
2275 /* vector_targets_convertible_p is used for vector pointer types. The
2276 callers perform various checks that the qualifiers are satisfactory,
2277 while OTOH vector_targets_convertible_p ignores the number of elements
2278 in the vectors. That's fine with vector pointers as we can consider,
2279 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2280 and that does not require and conversion of the pointer values.
2281 In contrast, vector_types_convertible_p and
2282 vector_types_compatible_elements_p are used for vector value types. */
2283 /* True if pointers to distinct types T1 and T2 can be converted to
2284 each other without an explicit cast. Only returns true for opaque
2285 vector types. */
2286 bool
2287 vector_targets_convertible_p (const_tree t1, const_tree t2)
2289 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2290 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2291 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2292 return true;
2294 return false;
2297 /* vector_types_convertible_p is used for vector value types.
2298 It could in principle call vector_targets_convertible_p as a subroutine,
2299 but then the check for vector type would be duplicated with its callers,
2300 and also the purpose of vector_targets_convertible_p would become
2301 muddled.
2302 Where vector_types_convertible_p returns true, a conversion might still be
2303 needed to make the types match.
2304 In contrast, vector_targets_convertible_p is used for vector pointer
2305 values, and vector_types_compatible_elements_p is used specifically
2306 in the context for binary operators, as a check if use is possible without
2307 conversion. */
2308 /* True if vector types T1 and T2 can be converted to each other
2309 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2310 can only be converted with -flax-vector-conversions yet that is not
2311 in effect, emit a note telling the user about that option if such
2312 a note has not previously been emitted. */
2313 bool
2314 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2316 static bool emitted_lax_note = false;
2317 bool convertible_lax;
2319 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2320 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2321 return true;
2323 convertible_lax =
2324 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2325 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2326 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2327 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2328 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2330 if (!convertible_lax || flag_lax_vector_conversions)
2331 return convertible_lax;
2333 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2334 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2335 return true;
2337 if (emit_lax_note && !emitted_lax_note)
2339 emitted_lax_note = true;
2340 inform (input_location, "use -flax-vector-conversions to permit "
2341 "conversions between vectors with differing "
2342 "element types or numbers of subparts");
2345 return false;
2348 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2349 and have vector types, V0 has the same type as V1, and the number of
2350 elements of V0, V1, MASK is the same.
2352 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2353 called with two arguments. In this case implementation passes the
2354 first argument twice in order to share the same tree code. This fact
2355 could enable the mask-values being twice the vector length. This is
2356 an implementation accident and this semantics is not guaranteed to
2357 the user. */
2358 tree
2359 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2360 bool complain)
2362 tree ret;
2363 bool wrap = true;
2364 bool maybe_const = false;
2365 bool two_arguments = false;
2367 if (v1 == NULL_TREE)
2369 two_arguments = true;
2370 v1 = v0;
2373 if (v0 == error_mark_node || v1 == error_mark_node
2374 || mask == error_mark_node)
2375 return error_mark_node;
2377 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2378 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2380 if (complain)
2381 error_at (loc, "__builtin_shuffle last argument must "
2382 "be an integer vector");
2383 return error_mark_node;
2386 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2387 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2389 if (complain)
2390 error_at (loc, "__builtin_shuffle arguments must be vectors");
2391 return error_mark_node;
2394 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2396 if (complain)
2397 error_at (loc, "__builtin_shuffle argument vectors must be of "
2398 "the same type");
2399 return error_mark_node;
2402 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2403 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2404 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2405 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2407 if (complain)
2408 error_at (loc, "__builtin_shuffle number of elements of the "
2409 "argument vector(s) and the mask vector should "
2410 "be the same");
2411 return error_mark_node;
2414 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2415 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2417 if (complain)
2418 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2419 "must have the same size as inner type of the mask");
2420 return error_mark_node;
2423 if (!c_dialect_cxx ())
2425 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2426 v0 = c_fully_fold (v0, false, &maybe_const);
2427 wrap &= maybe_const;
2429 if (two_arguments)
2430 v1 = v0 = save_expr (v0);
2431 else
2433 v1 = c_fully_fold (v1, false, &maybe_const);
2434 wrap &= maybe_const;
2437 mask = c_fully_fold (mask, false, &maybe_const);
2438 wrap &= maybe_const;
2440 else if (two_arguments)
2441 v1 = v0 = save_expr (v0);
2443 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2445 if (!c_dialect_cxx () && !wrap)
2446 ret = c_wrap_maybe_const (ret, true);
2448 return ret;
2451 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2452 to integral type. */
2454 static tree
2455 c_common_get_narrower (tree op, int *unsignedp_ptr)
2457 op = get_narrower (op, unsignedp_ptr);
2459 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2460 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2462 /* C++0x scoped enumerations don't implicitly convert to integral
2463 type; if we stripped an explicit conversion to a larger type we
2464 need to replace it so common_type will still work. */
2465 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2466 TYPE_UNSIGNED (TREE_TYPE (op)));
2467 op = fold_convert (type, op);
2469 return op;
2472 /* This is a helper function of build_binary_op.
2474 For certain operations if both args were extended from the same
2475 smaller type, do the arithmetic in that type and then extend.
2477 BITWISE indicates a bitwise operation.
2478 For them, this optimization is safe only if
2479 both args are zero-extended or both are sign-extended.
2480 Otherwise, we might change the result.
2481 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2482 but calculated in (unsigned short) it would be (unsigned short)-1.
2484 tree
2485 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2487 int unsigned0, unsigned1;
2488 tree arg0, arg1;
2489 int uns;
2490 tree type;
2492 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2493 excessive narrowing when we call get_narrower below. For
2494 example, suppose that OP0 is of unsigned int extended
2495 from signed char and that RESULT_TYPE is long long int.
2496 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2497 like
2499 (long long int) (unsigned int) signed_char
2501 which get_narrower would narrow down to
2503 (unsigned int) signed char
2505 If we do not cast OP0 first, get_narrower would return
2506 signed_char, which is inconsistent with the case of the
2507 explicit cast. */
2508 op0 = convert (result_type, op0);
2509 op1 = convert (result_type, op1);
2511 arg0 = c_common_get_narrower (op0, &unsigned0);
2512 arg1 = c_common_get_narrower (op1, &unsigned1);
2514 /* UNS is 1 if the operation to be done is an unsigned one. */
2515 uns = TYPE_UNSIGNED (result_type);
2517 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2518 but it *requires* conversion to FINAL_TYPE. */
2520 if ((TYPE_PRECISION (TREE_TYPE (op0))
2521 == TYPE_PRECISION (TREE_TYPE (arg0)))
2522 && TREE_TYPE (op0) != result_type)
2523 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2524 if ((TYPE_PRECISION (TREE_TYPE (op1))
2525 == TYPE_PRECISION (TREE_TYPE (arg1)))
2526 && TREE_TYPE (op1) != result_type)
2527 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2529 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2531 /* For bitwise operations, signedness of nominal type
2532 does not matter. Consider only how operands were extended. */
2533 if (bitwise)
2534 uns = unsigned0;
2536 /* Note that in all three cases below we refrain from optimizing
2537 an unsigned operation on sign-extended args.
2538 That would not be valid. */
2540 /* Both args variable: if both extended in same way
2541 from same width, do it in that width.
2542 Do it unsigned if args were zero-extended. */
2543 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2544 < TYPE_PRECISION (result_type))
2545 && (TYPE_PRECISION (TREE_TYPE (arg1))
2546 == TYPE_PRECISION (TREE_TYPE (arg0)))
2547 && unsigned0 == unsigned1
2548 && (unsigned0 || !uns))
2549 return c_common_signed_or_unsigned_type
2550 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2552 else if (TREE_CODE (arg0) == INTEGER_CST
2553 && (unsigned1 || !uns)
2554 && (TYPE_PRECISION (TREE_TYPE (arg1))
2555 < TYPE_PRECISION (result_type))
2556 && (type
2557 = c_common_signed_or_unsigned_type (unsigned1,
2558 TREE_TYPE (arg1)))
2559 && !POINTER_TYPE_P (type)
2560 && int_fits_type_p (arg0, type))
2561 return type;
2563 else if (TREE_CODE (arg1) == INTEGER_CST
2564 && (unsigned0 || !uns)
2565 && (TYPE_PRECISION (TREE_TYPE (arg0))
2566 < TYPE_PRECISION (result_type))
2567 && (type
2568 = c_common_signed_or_unsigned_type (unsigned0,
2569 TREE_TYPE (arg0)))
2570 && !POINTER_TYPE_P (type)
2571 && int_fits_type_p (arg1, type))
2572 return type;
2574 return result_type;
2577 /* Checks if expression EXPR of real/integer type cannot be converted
2578 to the real/integer type TYPE. Function returns non-zero when:
2579 * EXPR is a constant which cannot be exactly converted to TYPE.
2580 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2581 for EXPR type and TYPE being both integers or both real.
2582 * EXPR is not a constant of real type and TYPE is an integer.
2583 * EXPR is not a constant of integer type which cannot be
2584 exactly converted to real type.
2585 Function allows conversions between types of different signedness and
2586 can return SAFE_CONVERSION (zero) in that case. Function can produce
2587 signedness warnings if PRODUCE_WARNS is true. */
2589 enum conversion_safety
2590 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2592 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2593 tree expr_type = TREE_TYPE (expr);
2594 loc = expansion_point_location_if_in_system_header (loc);
2596 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2598 /* Warn for real constant that is not an exact integer converted
2599 to integer type. */
2600 if (TREE_CODE (expr_type) == REAL_TYPE
2601 && TREE_CODE (type) == INTEGER_TYPE)
2603 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2604 give_warning = UNSAFE_REAL;
2606 /* Warn for an integer constant that does not fit into integer type. */
2607 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2608 && TREE_CODE (type) == INTEGER_TYPE
2609 && !int_fits_type_p (expr, type))
2611 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2612 && tree_int_cst_sgn (expr) < 0)
2614 if (produce_warns)
2615 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2616 " implicitly converted to unsigned type");
2618 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2620 if (produce_warns)
2621 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2622 " constant value to negative integer");
2624 else
2625 give_warning = UNSAFE_OTHER;
2627 else if (TREE_CODE (type) == REAL_TYPE)
2629 /* Warn for an integer constant that does not fit into real type. */
2630 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2632 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2633 if (!exact_real_truncate (TYPE_MODE (type), &a))
2634 give_warning = UNSAFE_REAL;
2636 /* Warn for a real constant that does not fit into a smaller
2637 real type. */
2638 else if (TREE_CODE (expr_type) == REAL_TYPE
2639 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2641 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2642 if (!exact_real_truncate (TYPE_MODE (type), &a))
2643 give_warning = UNSAFE_REAL;
2647 else
2649 /* Warn for real types converted to integer types. */
2650 if (TREE_CODE (expr_type) == REAL_TYPE
2651 && TREE_CODE (type) == INTEGER_TYPE)
2652 give_warning = UNSAFE_REAL;
2654 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2655 && TREE_CODE (type) == INTEGER_TYPE)
2657 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2658 expr = get_unwidened (expr, 0);
2659 expr_type = TREE_TYPE (expr);
2661 /* Don't warn for short y; short x = ((int)y & 0xff); */
2662 if (TREE_CODE (expr) == BIT_AND_EXPR
2663 || TREE_CODE (expr) == BIT_IOR_EXPR
2664 || TREE_CODE (expr) == BIT_XOR_EXPR)
2666 /* If both args were extended from a shortest type,
2667 use that type if that is safe. */
2668 expr_type = shorten_binary_op (expr_type,
2669 TREE_OPERAND (expr, 0),
2670 TREE_OPERAND (expr, 1),
2671 /* bitwise */1);
2673 if (TREE_CODE (expr) == BIT_AND_EXPR)
2675 tree op0 = TREE_OPERAND (expr, 0);
2676 tree op1 = TREE_OPERAND (expr, 1);
2677 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2678 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2680 /* If one of the operands is a non-negative constant
2681 that fits in the target type, then the type of the
2682 other operand does not matter. */
2683 if ((TREE_CODE (op0) == INTEGER_CST
2684 && int_fits_type_p (op0, c_common_signed_type (type))
2685 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2686 || (TREE_CODE (op1) == INTEGER_CST
2687 && int_fits_type_p (op1, c_common_signed_type (type))
2688 && int_fits_type_p (op1,
2689 c_common_unsigned_type (type))))
2690 return SAFE_CONVERSION;
2691 /* If constant is unsigned and fits in the target
2692 type, then the result will also fit. */
2693 else if ((TREE_CODE (op0) == INTEGER_CST
2694 && unsigned0
2695 && int_fits_type_p (op0, type))
2696 || (TREE_CODE (op1) == INTEGER_CST
2697 && unsigned1
2698 && int_fits_type_p (op1, type)))
2699 return SAFE_CONVERSION;
2702 /* Warn for integer types converted to smaller integer types. */
2703 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2704 give_warning = UNSAFE_OTHER;
2706 /* When they are the same width but different signedness,
2707 then the value may change. */
2708 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2709 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2710 /* Even when converted to a bigger type, if the type is
2711 unsigned but expr is signed, then negative values
2712 will be changed. */
2713 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2714 && produce_warns)
2715 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2716 "may change the sign of the result",
2717 type, expr_type);
2720 /* Warn for integer types converted to real types if and only if
2721 all the range of values of the integer type cannot be
2722 represented by the real type. */
2723 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2724 && TREE_CODE (type) == REAL_TYPE)
2726 tree type_low_bound, type_high_bound;
2727 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2729 /* Don't warn about char y = 0xff; float x = (int) y; */
2730 expr = get_unwidened (expr, 0);
2731 expr_type = TREE_TYPE (expr);
2733 type_low_bound = TYPE_MIN_VALUE (expr_type);
2734 type_high_bound = TYPE_MAX_VALUE (expr_type);
2735 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2736 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2738 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2739 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2740 give_warning = UNSAFE_OTHER;
2743 /* Warn for real types converted to smaller real types. */
2744 else if (TREE_CODE (expr_type) == REAL_TYPE
2745 && TREE_CODE (type) == REAL_TYPE
2746 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2747 give_warning = UNSAFE_REAL;
2750 return give_warning;
2753 /* Warns if the conversion of EXPR to TYPE may alter a value.
2754 This is a helper function for warnings_for_convert_and_check. */
2756 static void
2757 conversion_warning (location_t loc, tree type, tree expr)
2759 tree expr_type = TREE_TYPE (expr);
2760 enum conversion_safety conversion_kind;
2762 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2763 return;
2765 /* This may happen, because for LHS op= RHS we preevaluate
2766 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2767 means we could no longer see the code of the EXPR. */
2768 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2769 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2770 if (TREE_CODE (expr) == SAVE_EXPR)
2771 expr = TREE_OPERAND (expr, 0);
2773 switch (TREE_CODE (expr))
2775 case EQ_EXPR:
2776 case NE_EXPR:
2777 case LE_EXPR:
2778 case GE_EXPR:
2779 case LT_EXPR:
2780 case GT_EXPR:
2781 case TRUTH_ANDIF_EXPR:
2782 case TRUTH_ORIF_EXPR:
2783 case TRUTH_AND_EXPR:
2784 case TRUTH_OR_EXPR:
2785 case TRUTH_XOR_EXPR:
2786 case TRUTH_NOT_EXPR:
2787 /* Conversion from boolean to a signed:1 bit-field (which only
2788 can hold the values 0 and -1) doesn't lose information - but
2789 it does change the value. */
2790 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2791 warning_at (loc, OPT_Wconversion,
2792 "conversion to %qT from boolean expression", type);
2793 return;
2795 case REAL_CST:
2796 case INTEGER_CST:
2797 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2798 if (conversion_kind == UNSAFE_REAL)
2799 warning_at (loc, OPT_Wfloat_conversion,
2800 "conversion to %qT alters %qT constant value",
2801 type, expr_type);
2802 else if (conversion_kind)
2803 warning_at (loc, OPT_Wconversion,
2804 "conversion to %qT alters %qT constant value",
2805 type, expr_type);
2806 return;
2808 case COND_EXPR:
2810 /* In case of COND_EXPR, we do not care about the type of
2811 COND_EXPR, only about the conversion of each operand. */
2812 tree op1 = TREE_OPERAND (expr, 1);
2813 tree op2 = TREE_OPERAND (expr, 2);
2815 conversion_warning (loc, type, op1);
2816 conversion_warning (loc, type, op2);
2817 return;
2820 default: /* 'expr' is not a constant. */
2821 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2822 if (conversion_kind == UNSAFE_REAL)
2823 warning_at (loc, OPT_Wfloat_conversion,
2824 "conversion to %qT from %qT may alter its value",
2825 type, expr_type);
2826 else if (conversion_kind)
2827 warning_at (loc, OPT_Wconversion,
2828 "conversion to %qT from %qT may alter its value",
2829 type, expr_type);
2833 /* Produce warnings after a conversion. RESULT is the result of
2834 converting EXPR to TYPE. This is a helper function for
2835 convert_and_check and cp_convert_and_check. */
2837 void
2838 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2839 tree result)
2841 loc = expansion_point_location_if_in_system_header (loc);
2843 if (TREE_CODE (expr) == INTEGER_CST
2844 && (TREE_CODE (type) == INTEGER_TYPE
2845 || TREE_CODE (type) == ENUMERAL_TYPE)
2846 && !int_fits_type_p (expr, type))
2848 /* Do not diagnose overflow in a constant expression merely
2849 because a conversion overflowed. */
2850 if (TREE_OVERFLOW (result))
2851 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2853 if (TYPE_UNSIGNED (type))
2855 /* This detects cases like converting -129 or 256 to
2856 unsigned char. */
2857 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2858 warning_at (loc, OPT_Woverflow,
2859 "large integer implicitly truncated to unsigned type");
2860 else
2861 conversion_warning (loc, type, expr);
2863 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2864 warning_at (loc, OPT_Woverflow,
2865 "overflow in implicit constant conversion");
2866 /* No warning for converting 0x80000000 to int. */
2867 else if (pedantic
2868 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2869 || TYPE_PRECISION (TREE_TYPE (expr))
2870 != TYPE_PRECISION (type)))
2871 warning_at (loc, OPT_Woverflow,
2872 "overflow in implicit constant conversion");
2874 else
2875 conversion_warning (loc, type, expr);
2877 else if ((TREE_CODE (result) == INTEGER_CST
2878 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2879 warning_at (loc, OPT_Woverflow,
2880 "overflow in implicit constant conversion");
2881 else
2882 conversion_warning (loc, type, expr);
2886 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2887 Invoke this function on every expression that is converted implicitly,
2888 i.e. because of language rules and not because of an explicit cast. */
2890 tree
2891 convert_and_check (location_t loc, tree type, tree expr)
2893 tree result;
2894 tree expr_for_warning;
2896 /* Convert from a value with possible excess precision rather than
2897 via the semantic type, but do not warn about values not fitting
2898 exactly in the semantic type. */
2899 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2901 tree orig_type = TREE_TYPE (expr);
2902 expr = TREE_OPERAND (expr, 0);
2903 expr_for_warning = convert (orig_type, expr);
2904 if (orig_type == type)
2905 return expr_for_warning;
2907 else
2908 expr_for_warning = expr;
2910 if (TREE_TYPE (expr) == type)
2911 return expr;
2913 result = convert (type, expr);
2915 if (c_inhibit_evaluation_warnings == 0
2916 && !TREE_OVERFLOW_P (expr)
2917 && result != error_mark_node)
2918 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2920 return result;
2923 /* A node in a list that describes references to variables (EXPR), which are
2924 either read accesses if WRITER is zero, or write accesses, in which case
2925 WRITER is the parent of EXPR. */
2926 struct tlist
2928 struct tlist *next;
2929 tree expr, writer;
2932 /* Used to implement a cache the results of a call to verify_tree. We only
2933 use this for SAVE_EXPRs. */
2934 struct tlist_cache
2936 struct tlist_cache *next;
2937 struct tlist *cache_before_sp;
2938 struct tlist *cache_after_sp;
2939 tree expr;
2942 /* Obstack to use when allocating tlist structures, and corresponding
2943 firstobj. */
2944 static struct obstack tlist_obstack;
2945 static char *tlist_firstobj = 0;
2947 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2948 warnings. */
2949 static struct tlist *warned_ids;
2950 /* SAVE_EXPRs need special treatment. We process them only once and then
2951 cache the results. */
2952 static struct tlist_cache *save_expr_cache;
2954 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2955 static void merge_tlist (struct tlist **, struct tlist *, int);
2956 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2957 static int warning_candidate_p (tree);
2958 static bool candidate_equal_p (const_tree, const_tree);
2959 static void warn_for_collisions (struct tlist *);
2960 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2961 static struct tlist *new_tlist (struct tlist *, tree, tree);
2963 /* Create a new struct tlist and fill in its fields. */
2964 static struct tlist *
2965 new_tlist (struct tlist *next, tree t, tree writer)
2967 struct tlist *l;
2968 l = XOBNEW (&tlist_obstack, struct tlist);
2969 l->next = next;
2970 l->expr = t;
2971 l->writer = writer;
2972 return l;
2975 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2976 is nonnull, we ignore any node we find which has a writer equal to it. */
2978 static void
2979 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2981 while (add)
2983 struct tlist *next = add->next;
2984 if (!copy)
2985 add->next = *to;
2986 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2987 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2988 add = next;
2992 /* Merge the nodes of ADD into TO. This merging process is done so that for
2993 each variable that already exists in TO, no new node is added; however if
2994 there is a write access recorded in ADD, and an occurrence on TO is only
2995 a read access, then the occurrence in TO will be modified to record the
2996 write. */
2998 static void
2999 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3001 struct tlist **end = to;
3003 while (*end)
3004 end = &(*end)->next;
3006 while (add)
3008 int found = 0;
3009 struct tlist *tmp2;
3010 struct tlist *next = add->next;
3012 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3013 if (candidate_equal_p (tmp2->expr, add->expr))
3015 found = 1;
3016 if (!tmp2->writer)
3017 tmp2->writer = add->writer;
3019 if (!found)
3021 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3022 end = &(*end)->next;
3023 *end = 0;
3025 add = next;
3029 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3030 references in list LIST conflict with it, excluding reads if ONLY writers
3031 is nonzero. */
3033 static void
3034 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3035 int only_writes)
3037 struct tlist *tmp;
3039 /* Avoid duplicate warnings. */
3040 for (tmp = warned_ids; tmp; tmp = tmp->next)
3041 if (candidate_equal_p (tmp->expr, written))
3042 return;
3044 while (list)
3046 if (candidate_equal_p (list->expr, written)
3047 && !candidate_equal_p (list->writer, writer)
3048 && (!only_writes || list->writer))
3050 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3051 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3052 OPT_Wsequence_point, "operation on %qE may be undefined",
3053 list->expr);
3055 list = list->next;
3059 /* Given a list LIST of references to variables, find whether any of these
3060 can cause conflicts due to missing sequence points. */
3062 static void
3063 warn_for_collisions (struct tlist *list)
3065 struct tlist *tmp;
3067 for (tmp = list; tmp; tmp = tmp->next)
3069 if (tmp->writer)
3070 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3074 /* Return nonzero if X is a tree that can be verified by the sequence point
3075 warnings. */
3076 static int
3077 warning_candidate_p (tree x)
3079 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3080 return 0;
3082 if (TREE_CODE (x) == BLOCK)
3083 return 0;
3085 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3086 (lvalue_p) crash on TRY/CATCH. */
3087 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3088 return 0;
3090 if (!lvalue_p (x))
3091 return 0;
3093 /* No point to track non-const calls, they will never satisfy
3094 operand_equal_p. */
3095 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3096 return 0;
3098 if (TREE_CODE (x) == STRING_CST)
3099 return 0;
3101 return 1;
3104 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3105 static bool
3106 candidate_equal_p (const_tree x, const_tree y)
3108 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3111 /* Walk the tree X, and record accesses to variables. If X is written by the
3112 parent tree, WRITER is the parent.
3113 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3114 expression or its only operand forces a sequence point, then everything up
3115 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3116 in PNO_SP.
3117 Once we return, we will have emitted warnings if any subexpression before
3118 such a sequence point could be undefined. On a higher level, however, the
3119 sequence point may not be relevant, and we'll merge the two lists.
3121 Example: (b++, a) + b;
3122 The call that processes the COMPOUND_EXPR will store the increment of B
3123 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3124 processes the PLUS_EXPR will need to merge the two lists so that
3125 eventually, all accesses end up on the same list (and we'll warn about the
3126 unordered subexpressions b++ and b.
3128 A note on merging. If we modify the former example so that our expression
3129 becomes
3130 (b++, b) + a
3131 care must be taken not simply to add all three expressions into the final
3132 PNO_SP list. The function merge_tlist takes care of that by merging the
3133 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3134 way, so that no more than one access to B is recorded. */
3136 static void
3137 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3138 tree writer)
3140 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3141 enum tree_code code;
3142 enum tree_code_class cl;
3144 /* X may be NULL if it is the operand of an empty statement expression
3145 ({ }). */
3146 if (x == NULL)
3147 return;
3149 restart:
3150 code = TREE_CODE (x);
3151 cl = TREE_CODE_CLASS (code);
3153 if (warning_candidate_p (x))
3154 *pno_sp = new_tlist (*pno_sp, x, writer);
3156 switch (code)
3158 case CONSTRUCTOR:
3159 case SIZEOF_EXPR:
3160 return;
3162 case COMPOUND_EXPR:
3163 case TRUTH_ANDIF_EXPR:
3164 case TRUTH_ORIF_EXPR:
3165 tmp_before = tmp_nosp = tmp_list3 = 0;
3166 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3167 warn_for_collisions (tmp_nosp);
3168 merge_tlist (pbefore_sp, tmp_before, 0);
3169 merge_tlist (pbefore_sp, tmp_nosp, 0);
3170 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3171 merge_tlist (pbefore_sp, tmp_list3, 0);
3172 return;
3174 case COND_EXPR:
3175 tmp_before = tmp_list2 = 0;
3176 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3177 warn_for_collisions (tmp_list2);
3178 merge_tlist (pbefore_sp, tmp_before, 0);
3179 merge_tlist (pbefore_sp, tmp_list2, 0);
3181 tmp_list3 = tmp_nosp = 0;
3182 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3183 warn_for_collisions (tmp_nosp);
3184 merge_tlist (pbefore_sp, tmp_list3, 0);
3186 tmp_list3 = tmp_list2 = 0;
3187 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3188 warn_for_collisions (tmp_list2);
3189 merge_tlist (pbefore_sp, tmp_list3, 0);
3190 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3191 two first, to avoid warning for (a ? b++ : b++). */
3192 merge_tlist (&tmp_nosp, tmp_list2, 0);
3193 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3194 return;
3196 case PREDECREMENT_EXPR:
3197 case PREINCREMENT_EXPR:
3198 case POSTDECREMENT_EXPR:
3199 case POSTINCREMENT_EXPR:
3200 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3201 return;
3203 case MODIFY_EXPR:
3204 tmp_before = tmp_nosp = tmp_list3 = 0;
3205 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3206 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3207 /* Expressions inside the LHS are not ordered wrt. the sequence points
3208 in the RHS. Example:
3209 *a = (a++, 2)
3210 Despite the fact that the modification of "a" is in the before_sp
3211 list (tmp_before), it conflicts with the use of "a" in the LHS.
3212 We can handle this by adding the contents of tmp_list3
3213 to those of tmp_before, and redoing the collision warnings for that
3214 list. */
3215 add_tlist (&tmp_before, tmp_list3, x, 1);
3216 warn_for_collisions (tmp_before);
3217 /* Exclude the LHS itself here; we first have to merge it into the
3218 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3219 didn't exclude the LHS, we'd get it twice, once as a read and once
3220 as a write. */
3221 add_tlist (pno_sp, tmp_list3, x, 0);
3222 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3224 merge_tlist (pbefore_sp, tmp_before, 0);
3225 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3226 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3227 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3228 return;
3230 case CALL_EXPR:
3231 /* We need to warn about conflicts among arguments and conflicts between
3232 args and the function address. Side effects of the function address,
3233 however, are not ordered by the sequence point of the call. */
3235 call_expr_arg_iterator iter;
3236 tree arg;
3237 tmp_before = tmp_nosp = 0;
3238 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3239 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3241 tmp_list2 = tmp_list3 = 0;
3242 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3243 merge_tlist (&tmp_list3, tmp_list2, 0);
3244 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3246 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3247 warn_for_collisions (tmp_before);
3248 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3249 return;
3252 case TREE_LIST:
3253 /* Scan all the list, e.g. indices of multi dimensional array. */
3254 while (x)
3256 tmp_before = tmp_nosp = 0;
3257 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3258 merge_tlist (&tmp_nosp, tmp_before, 0);
3259 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3260 x = TREE_CHAIN (x);
3262 return;
3264 case SAVE_EXPR:
3266 struct tlist_cache *t;
3267 for (t = save_expr_cache; t; t = t->next)
3268 if (candidate_equal_p (t->expr, x))
3269 break;
3271 if (!t)
3273 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3274 t->next = save_expr_cache;
3275 t->expr = x;
3276 save_expr_cache = t;
3278 tmp_before = tmp_nosp = 0;
3279 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3280 warn_for_collisions (tmp_nosp);
3282 tmp_list3 = 0;
3283 merge_tlist (&tmp_list3, tmp_nosp, 0);
3284 t->cache_before_sp = tmp_before;
3285 t->cache_after_sp = tmp_list3;
3287 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3288 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3289 return;
3292 case ADDR_EXPR:
3293 x = TREE_OPERAND (x, 0);
3294 if (DECL_P (x))
3295 return;
3296 writer = 0;
3297 goto restart;
3299 default:
3300 /* For other expressions, simply recurse on their operands.
3301 Manual tail recursion for unary expressions.
3302 Other non-expressions need not be processed. */
3303 if (cl == tcc_unary)
3305 x = TREE_OPERAND (x, 0);
3306 writer = 0;
3307 goto restart;
3309 else if (IS_EXPR_CODE_CLASS (cl))
3311 int lp;
3312 int max = TREE_OPERAND_LENGTH (x);
3313 for (lp = 0; lp < max; lp++)
3315 tmp_before = tmp_nosp = 0;
3316 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3317 merge_tlist (&tmp_nosp, tmp_before, 0);
3318 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3321 return;
3325 /* Try to warn for undefined behavior in EXPR due to missing sequence
3326 points. */
3328 DEBUG_FUNCTION void
3329 verify_sequence_points (tree expr)
3331 struct tlist *before_sp = 0, *after_sp = 0;
3333 warned_ids = 0;
3334 save_expr_cache = 0;
3335 if (tlist_firstobj == 0)
3337 gcc_obstack_init (&tlist_obstack);
3338 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3341 verify_tree (expr, &before_sp, &after_sp, 0);
3342 warn_for_collisions (after_sp);
3343 obstack_free (&tlist_obstack, tlist_firstobj);
3346 /* Validate the expression after `case' and apply default promotions. */
3348 static tree
3349 check_case_value (tree value)
3351 if (value == NULL_TREE)
3352 return value;
3354 if (TREE_CODE (value) == INTEGER_CST)
3355 /* Promote char or short to int. */
3356 value = perform_integral_promotions (value);
3357 else if (value != error_mark_node)
3359 error ("case label does not reduce to an integer constant");
3360 value = error_mark_node;
3363 constant_expression_warning (value);
3365 return value;
3368 /* See if the case values LOW and HIGH are in the range of the original
3369 type (i.e. before the default conversion to int) of the switch testing
3370 expression.
3371 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3372 the type before promoting it. CASE_LOW_P is a pointer to the lower
3373 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3374 if the case is not a case range.
3375 The caller has to make sure that we are not called with NULL for
3376 CASE_LOW_P (i.e. the default case).
3377 Returns true if the case label is in range of ORIG_TYPE (saturated or
3378 untouched) or false if the label is out of range. */
3380 static bool
3381 check_case_bounds (location_t loc, tree type, tree orig_type,
3382 tree *case_low_p, tree *case_high_p)
3384 tree min_value, max_value;
3385 tree case_low = *case_low_p;
3386 tree case_high = case_high_p ? *case_high_p : case_low;
3388 /* If there was a problem with the original type, do nothing. */
3389 if (orig_type == error_mark_node)
3390 return true;
3392 min_value = TYPE_MIN_VALUE (orig_type);
3393 max_value = TYPE_MAX_VALUE (orig_type);
3395 /* Case label is less than minimum for type. */
3396 if (tree_int_cst_compare (case_low, min_value) < 0
3397 && tree_int_cst_compare (case_high, min_value) < 0)
3399 warning_at (loc, 0, "case label value is less than minimum value "
3400 "for type");
3401 return false;
3404 /* Case value is greater than maximum for type. */
3405 if (tree_int_cst_compare (case_low, max_value) > 0
3406 && tree_int_cst_compare (case_high, max_value) > 0)
3408 warning_at (loc, 0, "case label value exceeds maximum value for type");
3409 return false;
3412 /* Saturate lower case label value to minimum. */
3413 if (tree_int_cst_compare (case_high, min_value) >= 0
3414 && tree_int_cst_compare (case_low, min_value) < 0)
3416 warning_at (loc, 0, "lower value in case label range"
3417 " less than minimum value for type");
3418 case_low = min_value;
3421 /* Saturate upper case label value to maximum. */
3422 if (tree_int_cst_compare (case_low, max_value) <= 0
3423 && tree_int_cst_compare (case_high, max_value) > 0)
3425 warning_at (loc, 0, "upper value in case label range"
3426 " exceeds maximum value for type");
3427 case_high = max_value;
3430 if (*case_low_p != case_low)
3431 *case_low_p = convert (type, case_low);
3432 if (case_high_p && *case_high_p != case_high)
3433 *case_high_p = convert (type, case_high);
3435 return true;
3438 /* Return an integer type with BITS bits of precision,
3439 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3441 tree
3442 c_common_type_for_size (unsigned int bits, int unsignedp)
3444 if (bits == TYPE_PRECISION (integer_type_node))
3445 return unsignedp ? unsigned_type_node : integer_type_node;
3447 if (bits == TYPE_PRECISION (signed_char_type_node))
3448 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3450 if (bits == TYPE_PRECISION (short_integer_type_node))
3451 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3453 if (bits == TYPE_PRECISION (long_integer_type_node))
3454 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3456 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3457 return (unsignedp ? long_long_unsigned_type_node
3458 : long_long_integer_type_node);
3460 if (int128_integer_type_node
3461 && bits == TYPE_PRECISION (int128_integer_type_node))
3462 return (unsignedp ? int128_unsigned_type_node
3463 : int128_integer_type_node);
3465 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3466 return (unsignedp ? widest_unsigned_literal_type_node
3467 : widest_integer_literal_type_node);
3469 if (bits <= TYPE_PRECISION (intQI_type_node))
3470 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3472 if (bits <= TYPE_PRECISION (intHI_type_node))
3473 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3475 if (bits <= TYPE_PRECISION (intSI_type_node))
3476 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3478 if (bits <= TYPE_PRECISION (intDI_type_node))
3479 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3481 return 0;
3484 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3485 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3486 and saturating if SATP is nonzero, otherwise not saturating. */
3488 tree
3489 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3490 int unsignedp, int satp)
3492 enum machine_mode mode;
3493 if (ibit == 0)
3494 mode = unsignedp ? UQQmode : QQmode;
3495 else
3496 mode = unsignedp ? UHAmode : HAmode;
3498 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3499 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3500 break;
3502 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3504 sorry ("GCC cannot support operators with integer types and "
3505 "fixed-point types that have too many integral and "
3506 "fractional bits together");
3507 return 0;
3510 return c_common_type_for_mode (mode, satp);
3513 /* Used for communication between c_common_type_for_mode and
3514 c_register_builtin_type. */
3515 tree registered_builtin_types;
3517 /* Return a data type that has machine mode MODE.
3518 If the mode is an integer,
3519 then UNSIGNEDP selects between signed and unsigned types.
3520 If the mode is a fixed-point mode,
3521 then UNSIGNEDP selects between saturating and nonsaturating types. */
3523 tree
3524 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3526 tree t;
3528 if (mode == TYPE_MODE (integer_type_node))
3529 return unsignedp ? unsigned_type_node : integer_type_node;
3531 if (mode == TYPE_MODE (signed_char_type_node))
3532 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3534 if (mode == TYPE_MODE (short_integer_type_node))
3535 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3537 if (mode == TYPE_MODE (long_integer_type_node))
3538 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3540 if (mode == TYPE_MODE (long_long_integer_type_node))
3541 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3543 if (int128_integer_type_node
3544 && mode == TYPE_MODE (int128_integer_type_node))
3545 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3547 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3548 return unsignedp ? widest_unsigned_literal_type_node
3549 : widest_integer_literal_type_node;
3551 if (mode == QImode)
3552 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3554 if (mode == HImode)
3555 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3557 if (mode == SImode)
3558 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3560 if (mode == DImode)
3561 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3563 #if HOST_BITS_PER_WIDE_INT >= 64
3564 if (mode == TYPE_MODE (intTI_type_node))
3565 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3566 #endif
3568 if (mode == TYPE_MODE (float_type_node))
3569 return float_type_node;
3571 if (mode == TYPE_MODE (double_type_node))
3572 return double_type_node;
3574 if (mode == TYPE_MODE (long_double_type_node))
3575 return long_double_type_node;
3577 if (mode == TYPE_MODE (void_type_node))
3578 return void_type_node;
3580 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3581 return (unsignedp
3582 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3583 : make_signed_type (GET_MODE_PRECISION (mode)));
3585 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3586 return (unsignedp
3587 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3588 : make_signed_type (GET_MODE_PRECISION (mode)));
3590 if (COMPLEX_MODE_P (mode))
3592 enum machine_mode inner_mode;
3593 tree inner_type;
3595 if (mode == TYPE_MODE (complex_float_type_node))
3596 return complex_float_type_node;
3597 if (mode == TYPE_MODE (complex_double_type_node))
3598 return complex_double_type_node;
3599 if (mode == TYPE_MODE (complex_long_double_type_node))
3600 return complex_long_double_type_node;
3602 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3603 return complex_integer_type_node;
3605 inner_mode = GET_MODE_INNER (mode);
3606 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3607 if (inner_type != NULL_TREE)
3608 return build_complex_type (inner_type);
3610 else if (VECTOR_MODE_P (mode))
3612 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3613 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3614 if (inner_type != NULL_TREE)
3615 return build_vector_type_for_mode (inner_type, mode);
3618 if (mode == TYPE_MODE (dfloat32_type_node))
3619 return dfloat32_type_node;
3620 if (mode == TYPE_MODE (dfloat64_type_node))
3621 return dfloat64_type_node;
3622 if (mode == TYPE_MODE (dfloat128_type_node))
3623 return dfloat128_type_node;
3625 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3627 if (mode == TYPE_MODE (short_fract_type_node))
3628 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3629 if (mode == TYPE_MODE (fract_type_node))
3630 return unsignedp ? sat_fract_type_node : fract_type_node;
3631 if (mode == TYPE_MODE (long_fract_type_node))
3632 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3633 if (mode == TYPE_MODE (long_long_fract_type_node))
3634 return unsignedp ? sat_long_long_fract_type_node
3635 : long_long_fract_type_node;
3637 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3638 return unsignedp ? sat_unsigned_short_fract_type_node
3639 : unsigned_short_fract_type_node;
3640 if (mode == TYPE_MODE (unsigned_fract_type_node))
3641 return unsignedp ? sat_unsigned_fract_type_node
3642 : unsigned_fract_type_node;
3643 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3644 return unsignedp ? sat_unsigned_long_fract_type_node
3645 : unsigned_long_fract_type_node;
3646 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3647 return unsignedp ? sat_unsigned_long_long_fract_type_node
3648 : unsigned_long_long_fract_type_node;
3650 if (mode == TYPE_MODE (short_accum_type_node))
3651 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3652 if (mode == TYPE_MODE (accum_type_node))
3653 return unsignedp ? sat_accum_type_node : accum_type_node;
3654 if (mode == TYPE_MODE (long_accum_type_node))
3655 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3656 if (mode == TYPE_MODE (long_long_accum_type_node))
3657 return unsignedp ? sat_long_long_accum_type_node
3658 : long_long_accum_type_node;
3660 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3661 return unsignedp ? sat_unsigned_short_accum_type_node
3662 : unsigned_short_accum_type_node;
3663 if (mode == TYPE_MODE (unsigned_accum_type_node))
3664 return unsignedp ? sat_unsigned_accum_type_node
3665 : unsigned_accum_type_node;
3666 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3667 return unsignedp ? sat_unsigned_long_accum_type_node
3668 : unsigned_long_accum_type_node;
3669 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3670 return unsignedp ? sat_unsigned_long_long_accum_type_node
3671 : unsigned_long_long_accum_type_node;
3673 if (mode == QQmode)
3674 return unsignedp ? sat_qq_type_node : qq_type_node;
3675 if (mode == HQmode)
3676 return unsignedp ? sat_hq_type_node : hq_type_node;
3677 if (mode == SQmode)
3678 return unsignedp ? sat_sq_type_node : sq_type_node;
3679 if (mode == DQmode)
3680 return unsignedp ? sat_dq_type_node : dq_type_node;
3681 if (mode == TQmode)
3682 return unsignedp ? sat_tq_type_node : tq_type_node;
3684 if (mode == UQQmode)
3685 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3686 if (mode == UHQmode)
3687 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3688 if (mode == USQmode)
3689 return unsignedp ? sat_usq_type_node : usq_type_node;
3690 if (mode == UDQmode)
3691 return unsignedp ? sat_udq_type_node : udq_type_node;
3692 if (mode == UTQmode)
3693 return unsignedp ? sat_utq_type_node : utq_type_node;
3695 if (mode == HAmode)
3696 return unsignedp ? sat_ha_type_node : ha_type_node;
3697 if (mode == SAmode)
3698 return unsignedp ? sat_sa_type_node : sa_type_node;
3699 if (mode == DAmode)
3700 return unsignedp ? sat_da_type_node : da_type_node;
3701 if (mode == TAmode)
3702 return unsignedp ? sat_ta_type_node : ta_type_node;
3704 if (mode == UHAmode)
3705 return unsignedp ? sat_uha_type_node : uha_type_node;
3706 if (mode == USAmode)
3707 return unsignedp ? sat_usa_type_node : usa_type_node;
3708 if (mode == UDAmode)
3709 return unsignedp ? sat_uda_type_node : uda_type_node;
3710 if (mode == UTAmode)
3711 return unsignedp ? sat_uta_type_node : uta_type_node;
3714 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3715 if (TYPE_MODE (TREE_VALUE (t)) == mode
3716 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3717 return TREE_VALUE (t);
3719 return 0;
3722 tree
3723 c_common_unsigned_type (tree type)
3725 return c_common_signed_or_unsigned_type (1, type);
3728 /* Return a signed type the same as TYPE in other respects. */
3730 tree
3731 c_common_signed_type (tree type)
3733 return c_common_signed_or_unsigned_type (0, type);
3736 /* Return a type the same as TYPE except unsigned or
3737 signed according to UNSIGNEDP. */
3739 tree
3740 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3742 tree type1;
3744 /* This block of code emulates the behavior of the old
3745 c_common_unsigned_type. In particular, it returns
3746 long_unsigned_type_node if passed a long, even when a int would
3747 have the same size. This is necessary for warnings to work
3748 correctly in archs where sizeof(int) == sizeof(long) */
3750 type1 = TYPE_MAIN_VARIANT (type);
3751 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3752 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3753 if (type1 == integer_type_node || type1 == unsigned_type_node)
3754 return unsignedp ? unsigned_type_node : integer_type_node;
3755 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3756 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3757 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3758 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3759 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3760 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3761 if (int128_integer_type_node
3762 && (type1 == int128_integer_type_node
3763 || type1 == int128_unsigned_type_node))
3764 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3765 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3766 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3767 #if HOST_BITS_PER_WIDE_INT >= 64
3768 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3769 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3770 #endif
3771 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3772 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3773 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3774 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3775 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3776 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3777 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3778 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3780 #define C_COMMON_FIXED_TYPES(NAME) \
3781 if (type1 == short_ ## NAME ## _type_node \
3782 || type1 == unsigned_short_ ## NAME ## _type_node) \
3783 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3784 : short_ ## NAME ## _type_node; \
3785 if (type1 == NAME ## _type_node \
3786 || type1 == unsigned_ ## NAME ## _type_node) \
3787 return unsignedp ? unsigned_ ## NAME ## _type_node \
3788 : NAME ## _type_node; \
3789 if (type1 == long_ ## NAME ## _type_node \
3790 || type1 == unsigned_long_ ## NAME ## _type_node) \
3791 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3792 : long_ ## NAME ## _type_node; \
3793 if (type1 == long_long_ ## NAME ## _type_node \
3794 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3795 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3796 : long_long_ ## NAME ## _type_node;
3798 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3799 if (type1 == NAME ## _type_node \
3800 || type1 == u ## NAME ## _type_node) \
3801 return unsignedp ? u ## NAME ## _type_node \
3802 : NAME ## _type_node;
3804 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3805 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3806 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3807 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3808 : sat_ ## short_ ## NAME ## _type_node; \
3809 if (type1 == sat_ ## NAME ## _type_node \
3810 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3811 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3812 : sat_ ## NAME ## _type_node; \
3813 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3814 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3815 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3816 : sat_ ## long_ ## NAME ## _type_node; \
3817 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3818 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3819 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3820 : sat_ ## long_long_ ## NAME ## _type_node;
3822 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3823 if (type1 == sat_ ## NAME ## _type_node \
3824 || type1 == sat_ ## u ## NAME ## _type_node) \
3825 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3826 : sat_ ## NAME ## _type_node;
3828 C_COMMON_FIXED_TYPES (fract);
3829 C_COMMON_FIXED_TYPES_SAT (fract);
3830 C_COMMON_FIXED_TYPES (accum);
3831 C_COMMON_FIXED_TYPES_SAT (accum);
3833 C_COMMON_FIXED_MODE_TYPES (qq);
3834 C_COMMON_FIXED_MODE_TYPES (hq);
3835 C_COMMON_FIXED_MODE_TYPES (sq);
3836 C_COMMON_FIXED_MODE_TYPES (dq);
3837 C_COMMON_FIXED_MODE_TYPES (tq);
3838 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3839 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3840 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3841 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3842 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3843 C_COMMON_FIXED_MODE_TYPES (ha);
3844 C_COMMON_FIXED_MODE_TYPES (sa);
3845 C_COMMON_FIXED_MODE_TYPES (da);
3846 C_COMMON_FIXED_MODE_TYPES (ta);
3847 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3848 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3849 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3850 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3852 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3853 the precision; they have precision set to match their range, but
3854 may use a wider mode to match an ABI. If we change modes, we may
3855 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3856 the precision as well, so as to yield correct results for
3857 bit-field types. C++ does not have these separate bit-field
3858 types, and producing a signed or unsigned variant of an
3859 ENUMERAL_TYPE may cause other problems as well. */
3861 if (!INTEGRAL_TYPE_P (type)
3862 || TYPE_UNSIGNED (type) == unsignedp)
3863 return type;
3865 #define TYPE_OK(node) \
3866 (TYPE_MODE (type) == TYPE_MODE (node) \
3867 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3868 if (TYPE_OK (signed_char_type_node))
3869 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3870 if (TYPE_OK (integer_type_node))
3871 return unsignedp ? unsigned_type_node : integer_type_node;
3872 if (TYPE_OK (short_integer_type_node))
3873 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3874 if (TYPE_OK (long_integer_type_node))
3875 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3876 if (TYPE_OK (long_long_integer_type_node))
3877 return (unsignedp ? long_long_unsigned_type_node
3878 : long_long_integer_type_node);
3879 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3880 return (unsignedp ? int128_unsigned_type_node
3881 : int128_integer_type_node);
3882 if (TYPE_OK (widest_integer_literal_type_node))
3883 return (unsignedp ? widest_unsigned_literal_type_node
3884 : widest_integer_literal_type_node);
3886 #if HOST_BITS_PER_WIDE_INT >= 64
3887 if (TYPE_OK (intTI_type_node))
3888 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3889 #endif
3890 if (TYPE_OK (intDI_type_node))
3891 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3892 if (TYPE_OK (intSI_type_node))
3893 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3894 if (TYPE_OK (intHI_type_node))
3895 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3896 if (TYPE_OK (intQI_type_node))
3897 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3898 #undef TYPE_OK
3900 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3903 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3905 tree
3906 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3908 /* Extended integer types of the same width as a standard type have
3909 lesser rank, so those of the same width as int promote to int or
3910 unsigned int and are valid for printf formats expecting int or
3911 unsigned int. To avoid such special cases, avoid creating
3912 extended integer types for bit-fields if a standard integer type
3913 is available. */
3914 if (width == TYPE_PRECISION (integer_type_node))
3915 return unsignedp ? unsigned_type_node : integer_type_node;
3916 if (width == TYPE_PRECISION (signed_char_type_node))
3917 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3918 if (width == TYPE_PRECISION (short_integer_type_node))
3919 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3920 if (width == TYPE_PRECISION (long_integer_type_node))
3921 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3922 if (width == TYPE_PRECISION (long_long_integer_type_node))
3923 return (unsignedp ? long_long_unsigned_type_node
3924 : long_long_integer_type_node);
3925 if (int128_integer_type_node
3926 && width == TYPE_PRECISION (int128_integer_type_node))
3927 return (unsignedp ? int128_unsigned_type_node
3928 : int128_integer_type_node);
3929 return build_nonstandard_integer_type (width, unsignedp);
3932 /* The C version of the register_builtin_type langhook. */
3934 void
3935 c_register_builtin_type (tree type, const char* name)
3937 tree decl;
3939 decl = build_decl (UNKNOWN_LOCATION,
3940 TYPE_DECL, get_identifier (name), type);
3941 DECL_ARTIFICIAL (decl) = 1;
3942 if (!TYPE_NAME (type))
3943 TYPE_NAME (type) = decl;
3944 pushdecl (decl);
3946 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3949 /* Print an error message for invalid operands to arith operation
3950 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3951 LOCATION is the location of the message. */
3953 void
3954 binary_op_error (location_t location, enum tree_code code,
3955 tree type0, tree type1)
3957 const char *opname;
3959 switch (code)
3961 case PLUS_EXPR:
3962 opname = "+"; break;
3963 case MINUS_EXPR:
3964 opname = "-"; break;
3965 case MULT_EXPR:
3966 opname = "*"; break;
3967 case MAX_EXPR:
3968 opname = "max"; break;
3969 case MIN_EXPR:
3970 opname = "min"; break;
3971 case EQ_EXPR:
3972 opname = "=="; break;
3973 case NE_EXPR:
3974 opname = "!="; break;
3975 case LE_EXPR:
3976 opname = "<="; break;
3977 case GE_EXPR:
3978 opname = ">="; break;
3979 case LT_EXPR:
3980 opname = "<"; break;
3981 case GT_EXPR:
3982 opname = ">"; break;
3983 case LSHIFT_EXPR:
3984 opname = "<<"; break;
3985 case RSHIFT_EXPR:
3986 opname = ">>"; break;
3987 case TRUNC_MOD_EXPR:
3988 case FLOOR_MOD_EXPR:
3989 opname = "%"; break;
3990 case TRUNC_DIV_EXPR:
3991 case FLOOR_DIV_EXPR:
3992 opname = "/"; break;
3993 case BIT_AND_EXPR:
3994 opname = "&"; break;
3995 case BIT_IOR_EXPR:
3996 opname = "|"; break;
3997 case TRUTH_ANDIF_EXPR:
3998 opname = "&&"; break;
3999 case TRUTH_ORIF_EXPR:
4000 opname = "||"; break;
4001 case BIT_XOR_EXPR:
4002 opname = "^"; break;
4003 default:
4004 gcc_unreachable ();
4006 error_at (location,
4007 "invalid operands to binary %s (have %qT and %qT)", opname,
4008 type0, type1);
4011 /* Given an expression as a tree, return its original type. Do this
4012 by stripping any conversion that preserves the sign and precision. */
4013 static tree
4014 expr_original_type (tree expr)
4016 STRIP_SIGN_NOPS (expr);
4017 return TREE_TYPE (expr);
4020 /* Subroutine of build_binary_op, used for comparison operations.
4021 See if the operands have both been converted from subword integer types
4022 and, if so, perhaps change them both back to their original type.
4023 This function is also responsible for converting the two operands
4024 to the proper common type for comparison.
4026 The arguments of this function are all pointers to local variables
4027 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4028 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4030 LOC is the location of the comparison.
4032 If this function returns nonzero, it means that the comparison has
4033 a constant value. What this function returns is an expression for
4034 that value. */
4036 tree
4037 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4038 tree *restype_ptr, enum tree_code *rescode_ptr)
4040 tree type;
4041 tree op0 = *op0_ptr;
4042 tree op1 = *op1_ptr;
4043 int unsignedp0, unsignedp1;
4044 int real1, real2;
4045 tree primop0, primop1;
4046 enum tree_code code = *rescode_ptr;
4048 /* Throw away any conversions to wider types
4049 already present in the operands. */
4051 primop0 = c_common_get_narrower (op0, &unsignedp0);
4052 primop1 = c_common_get_narrower (op1, &unsignedp1);
4054 /* If primopN is first sign-extended from primopN's precision to opN's
4055 precision, then zero-extended from opN's precision to
4056 *restype_ptr precision, shortenings might be invalid. */
4057 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4058 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4059 && !unsignedp0
4060 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4061 primop0 = op0;
4062 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4063 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4064 && !unsignedp1
4065 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4066 primop1 = op1;
4068 /* Handle the case that OP0 does not *contain* a conversion
4069 but it *requires* conversion to FINAL_TYPE. */
4071 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4072 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4073 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4074 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4076 /* If one of the operands must be floated, we cannot optimize. */
4077 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4078 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4080 /* If first arg is constant, swap the args (changing operation
4081 so value is preserved), for canonicalization. Don't do this if
4082 the second arg is 0. */
4084 if (TREE_CONSTANT (primop0)
4085 && !integer_zerop (primop1) && !real_zerop (primop1)
4086 && !fixed_zerop (primop1))
4088 tree tem = primop0;
4089 int temi = unsignedp0;
4090 primop0 = primop1;
4091 primop1 = tem;
4092 tem = op0;
4093 op0 = op1;
4094 op1 = tem;
4095 *op0_ptr = op0;
4096 *op1_ptr = op1;
4097 unsignedp0 = unsignedp1;
4098 unsignedp1 = temi;
4099 temi = real1;
4100 real1 = real2;
4101 real2 = temi;
4103 switch (code)
4105 case LT_EXPR:
4106 code = GT_EXPR;
4107 break;
4108 case GT_EXPR:
4109 code = LT_EXPR;
4110 break;
4111 case LE_EXPR:
4112 code = GE_EXPR;
4113 break;
4114 case GE_EXPR:
4115 code = LE_EXPR;
4116 break;
4117 default:
4118 break;
4120 *rescode_ptr = code;
4123 /* If comparing an integer against a constant more bits wide,
4124 maybe we can deduce a value of 1 or 0 independent of the data.
4125 Or else truncate the constant now
4126 rather than extend the variable at run time.
4128 This is only interesting if the constant is the wider arg.
4129 Also, it is not safe if the constant is unsigned and the
4130 variable arg is signed, since in this case the variable
4131 would be sign-extended and then regarded as unsigned.
4132 Our technique fails in this case because the lowest/highest
4133 possible unsigned results don't follow naturally from the
4134 lowest/highest possible values of the variable operand.
4135 For just EQ_EXPR and NE_EXPR there is another technique that
4136 could be used: see if the constant can be faithfully represented
4137 in the other operand's type, by truncating it and reextending it
4138 and see if that preserves the constant's value. */
4140 if (!real1 && !real2
4141 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4142 && TREE_CODE (primop1) == INTEGER_CST
4143 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4145 int min_gt, max_gt, min_lt, max_lt;
4146 tree maxval, minval;
4147 /* 1 if comparison is nominally unsigned. */
4148 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4149 tree val;
4151 type = c_common_signed_or_unsigned_type (unsignedp0,
4152 TREE_TYPE (primop0));
4154 maxval = TYPE_MAX_VALUE (type);
4155 minval = TYPE_MIN_VALUE (type);
4157 if (unsignedp && !unsignedp0)
4158 *restype_ptr = c_common_signed_type (*restype_ptr);
4160 if (TREE_TYPE (primop1) != *restype_ptr)
4162 /* Convert primop1 to target type, but do not introduce
4163 additional overflow. We know primop1 is an int_cst. */
4164 primop1 = force_fit_type (*restype_ptr,
4165 wide_int::from
4166 (primop1,
4167 TYPE_PRECISION (*restype_ptr),
4168 TYPE_SIGN (TREE_TYPE (primop1))),
4169 0, TREE_OVERFLOW (primop1));
4171 if (type != *restype_ptr)
4173 minval = convert (*restype_ptr, minval);
4174 maxval = convert (*restype_ptr, maxval);
4177 min_gt = tree_int_cst_lt (primop1, minval);
4178 max_gt = tree_int_cst_lt (primop1, maxval);
4179 min_lt = tree_int_cst_lt (minval, primop1);
4180 max_lt = tree_int_cst_lt (maxval, primop1);
4182 val = 0;
4183 /* This used to be a switch, but Genix compiler can't handle that. */
4184 if (code == NE_EXPR)
4186 if (max_lt || min_gt)
4187 val = truthvalue_true_node;
4189 else if (code == EQ_EXPR)
4191 if (max_lt || min_gt)
4192 val = truthvalue_false_node;
4194 else if (code == LT_EXPR)
4196 if (max_lt)
4197 val = truthvalue_true_node;
4198 if (!min_lt)
4199 val = truthvalue_false_node;
4201 else if (code == GT_EXPR)
4203 if (min_gt)
4204 val = truthvalue_true_node;
4205 if (!max_gt)
4206 val = truthvalue_false_node;
4208 else if (code == LE_EXPR)
4210 if (!max_gt)
4211 val = truthvalue_true_node;
4212 if (min_gt)
4213 val = truthvalue_false_node;
4215 else if (code == GE_EXPR)
4217 if (!min_lt)
4218 val = truthvalue_true_node;
4219 if (max_lt)
4220 val = truthvalue_false_node;
4223 /* If primop0 was sign-extended and unsigned comparison specd,
4224 we did a signed comparison above using the signed type bounds.
4225 But the comparison we output must be unsigned.
4227 Also, for inequalities, VAL is no good; but if the signed
4228 comparison had *any* fixed result, it follows that the
4229 unsigned comparison just tests the sign in reverse
4230 (positive values are LE, negative ones GE).
4231 So we can generate an unsigned comparison
4232 against an extreme value of the signed type. */
4234 if (unsignedp && !unsignedp0)
4236 if (val != 0)
4237 switch (code)
4239 case LT_EXPR:
4240 case GE_EXPR:
4241 primop1 = TYPE_MIN_VALUE (type);
4242 val = 0;
4243 break;
4245 case LE_EXPR:
4246 case GT_EXPR:
4247 primop1 = TYPE_MAX_VALUE (type);
4248 val = 0;
4249 break;
4251 default:
4252 break;
4254 type = c_common_unsigned_type (type);
4257 if (TREE_CODE (primop0) != INTEGER_CST)
4259 if (val == truthvalue_false_node)
4260 warning_at (loc, OPT_Wtype_limits,
4261 "comparison is always false due to limited range of data type");
4262 if (val == truthvalue_true_node)
4263 warning_at (loc, OPT_Wtype_limits,
4264 "comparison is always true due to limited range of data type");
4267 if (val != 0)
4269 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4270 if (TREE_SIDE_EFFECTS (primop0))
4271 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4272 return val;
4275 /* Value is not predetermined, but do the comparison
4276 in the type of the operand that is not constant.
4277 TYPE is already properly set. */
4280 /* If either arg is decimal float and the other is float, find the
4281 proper common type to use for comparison. */
4282 else if (real1 && real2
4283 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4284 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4285 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4287 else if (real1 && real2
4288 && (TYPE_PRECISION (TREE_TYPE (primop0))
4289 == TYPE_PRECISION (TREE_TYPE (primop1))))
4290 type = TREE_TYPE (primop0);
4292 /* If args' natural types are both narrower than nominal type
4293 and both extend in the same manner, compare them
4294 in the type of the wider arg.
4295 Otherwise must actually extend both to the nominal
4296 common type lest different ways of extending
4297 alter the result.
4298 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4300 else if (unsignedp0 == unsignedp1 && real1 == real2
4301 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4302 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4304 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4305 type = c_common_signed_or_unsigned_type (unsignedp0
4306 || TYPE_UNSIGNED (*restype_ptr),
4307 type);
4308 /* Make sure shorter operand is extended the right way
4309 to match the longer operand. */
4310 primop0
4311 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4312 TREE_TYPE (primop0)),
4313 primop0);
4314 primop1
4315 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4316 TREE_TYPE (primop1)),
4317 primop1);
4319 else
4321 /* Here we must do the comparison on the nominal type
4322 using the args exactly as we received them. */
4323 type = *restype_ptr;
4324 primop0 = op0;
4325 primop1 = op1;
4327 if (!real1 && !real2 && integer_zerop (primop1)
4328 && TYPE_UNSIGNED (*restype_ptr))
4330 tree value = 0;
4331 /* All unsigned values are >= 0, so we warn. However,
4332 if OP0 is a constant that is >= 0, the signedness of
4333 the comparison isn't an issue, so suppress the
4334 warning. */
4335 bool warn =
4336 warn_type_limits && !in_system_header_at (loc)
4337 && !(TREE_CODE (primop0) == INTEGER_CST
4338 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4339 primop0)))
4340 /* Do not warn for enumeration types. */
4341 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4343 switch (code)
4345 case GE_EXPR:
4346 if (warn)
4347 warning_at (loc, OPT_Wtype_limits,
4348 "comparison of unsigned expression >= 0 is always true");
4349 value = truthvalue_true_node;
4350 break;
4352 case LT_EXPR:
4353 if (warn)
4354 warning_at (loc, OPT_Wtype_limits,
4355 "comparison of unsigned expression < 0 is always false");
4356 value = truthvalue_false_node;
4357 break;
4359 default:
4360 break;
4363 if (value != 0)
4365 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4366 if (TREE_SIDE_EFFECTS (primop0))
4367 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4368 primop0, value);
4369 return value;
4374 *op0_ptr = convert (type, primop0);
4375 *op1_ptr = convert (type, primop1);
4377 *restype_ptr = truthvalue_type_node;
4379 return 0;
4382 /* Return a tree for the sum or difference (RESULTCODE says which)
4383 of pointer PTROP and integer INTOP. */
4385 tree
4386 pointer_int_sum (location_t loc, enum tree_code resultcode,
4387 tree ptrop, tree intop, bool complain)
4389 tree size_exp, ret;
4391 /* The result is a pointer of the same type that is being added. */
4392 tree result_type = TREE_TYPE (ptrop);
4394 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4396 if (complain && warn_pointer_arith)
4397 pedwarn (loc, OPT_Wpointer_arith,
4398 "pointer of type %<void *%> used in arithmetic");
4399 else if (!complain)
4400 return error_mark_node;
4401 size_exp = integer_one_node;
4403 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4405 if (complain && warn_pointer_arith)
4406 pedwarn (loc, OPT_Wpointer_arith,
4407 "pointer to a function used in arithmetic");
4408 else if (!complain)
4409 return error_mark_node;
4410 size_exp = integer_one_node;
4412 else
4413 size_exp = size_in_bytes (TREE_TYPE (result_type));
4415 /* We are manipulating pointer values, so we don't need to warn
4416 about relying on undefined signed overflow. We disable the
4417 warning here because we use integer types so fold won't know that
4418 they are really pointers. */
4419 fold_defer_overflow_warnings ();
4421 /* If what we are about to multiply by the size of the elements
4422 contains a constant term, apply distributive law
4423 and multiply that constant term separately.
4424 This helps produce common subexpressions. */
4425 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4426 && !TREE_CONSTANT (intop)
4427 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4428 && TREE_CONSTANT (size_exp)
4429 /* If the constant comes from pointer subtraction,
4430 skip this optimization--it would cause an error. */
4431 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4432 /* If the constant is unsigned, and smaller than the pointer size,
4433 then we must skip this optimization. This is because it could cause
4434 an overflow error if the constant is negative but INTOP is not. */
4435 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4436 || (TYPE_PRECISION (TREE_TYPE (intop))
4437 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4439 enum tree_code subcode = resultcode;
4440 tree int_type = TREE_TYPE (intop);
4441 if (TREE_CODE (intop) == MINUS_EXPR)
4442 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4443 /* Convert both subexpression types to the type of intop,
4444 because weird cases involving pointer arithmetic
4445 can result in a sum or difference with different type args. */
4446 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4447 subcode, ptrop,
4448 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4449 intop = convert (int_type, TREE_OPERAND (intop, 0));
4452 /* Convert the integer argument to a type the same size as sizetype
4453 so the multiply won't overflow spuriously. */
4454 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4455 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4456 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4457 TYPE_UNSIGNED (sizetype)), intop);
4459 /* Replace the integer argument with a suitable product by the object size.
4460 Do this multiplication as signed, then convert to the appropriate type
4461 for the pointer operation and disregard an overflow that occurred only
4462 because of the sign-extension change in the latter conversion. */
4464 tree t = build_binary_op (loc,
4465 MULT_EXPR, intop,
4466 convert (TREE_TYPE (intop), size_exp), 1);
4467 intop = convert (sizetype, t);
4468 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4469 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4472 /* Create the sum or difference. */
4473 if (resultcode == MINUS_EXPR)
4474 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4476 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4478 fold_undefer_and_ignore_overflow_warnings ();
4480 return ret;
4483 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4484 and if NON_CONST is known not to be permitted in an evaluated part
4485 of a constant expression. */
4487 tree
4488 c_wrap_maybe_const (tree expr, bool non_const)
4490 bool nowarning = TREE_NO_WARNING (expr);
4491 location_t loc = EXPR_LOCATION (expr);
4493 /* This should never be called for C++. */
4494 if (c_dialect_cxx ())
4495 gcc_unreachable ();
4497 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4498 STRIP_TYPE_NOPS (expr);
4499 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4500 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4501 if (nowarning)
4502 TREE_NO_WARNING (expr) = 1;
4503 protected_set_expr_location (expr, loc);
4505 return expr;
4508 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4509 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4510 around the SAVE_EXPR if needed so that c_fully_fold does not need
4511 to look inside SAVE_EXPRs. */
4513 tree
4514 c_save_expr (tree expr)
4516 bool maybe_const = true;
4517 if (c_dialect_cxx ())
4518 return save_expr (expr);
4519 expr = c_fully_fold (expr, false, &maybe_const);
4520 expr = save_expr (expr);
4521 if (!maybe_const)
4522 expr = c_wrap_maybe_const (expr, true);
4523 return expr;
4526 /* Return whether EXPR is a declaration whose address can never be
4527 NULL. */
4529 bool
4530 decl_with_nonnull_addr_p (const_tree expr)
4532 return (DECL_P (expr)
4533 && (TREE_CODE (expr) == PARM_DECL
4534 || TREE_CODE (expr) == LABEL_DECL
4535 || !DECL_WEAK (expr)));
4538 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4539 or for an `if' or `while' statement or ?..: exp. It should already
4540 have been validated to be of suitable type; otherwise, a bad
4541 diagnostic may result.
4543 The EXPR is located at LOCATION.
4545 This preparation consists of taking the ordinary
4546 representation of an expression expr and producing a valid tree
4547 boolean expression describing whether expr is nonzero. We could
4548 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4549 but we optimize comparisons, &&, ||, and !.
4551 The resulting type should always be `truthvalue_type_node'. */
4553 tree
4554 c_common_truthvalue_conversion (location_t location, tree expr)
4556 switch (TREE_CODE (expr))
4558 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4559 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4560 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4561 case ORDERED_EXPR: case UNORDERED_EXPR:
4562 if (TREE_TYPE (expr) == truthvalue_type_node)
4563 return expr;
4564 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4565 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4566 goto ret;
4568 case TRUTH_ANDIF_EXPR:
4569 case TRUTH_ORIF_EXPR:
4570 case TRUTH_AND_EXPR:
4571 case TRUTH_OR_EXPR:
4572 case TRUTH_XOR_EXPR:
4573 if (TREE_TYPE (expr) == truthvalue_type_node)
4574 return expr;
4575 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4576 c_common_truthvalue_conversion (location,
4577 TREE_OPERAND (expr, 0)),
4578 c_common_truthvalue_conversion (location,
4579 TREE_OPERAND (expr, 1)));
4580 goto ret;
4582 case TRUTH_NOT_EXPR:
4583 if (TREE_TYPE (expr) == truthvalue_type_node)
4584 return expr;
4585 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4586 c_common_truthvalue_conversion (location,
4587 TREE_OPERAND (expr, 0)));
4588 goto ret;
4590 case ERROR_MARK:
4591 return expr;
4593 case INTEGER_CST:
4594 return integer_zerop (expr) ? truthvalue_false_node
4595 : truthvalue_true_node;
4597 case REAL_CST:
4598 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4599 ? truthvalue_true_node
4600 : truthvalue_false_node;
4602 case FIXED_CST:
4603 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4604 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4605 ? truthvalue_true_node
4606 : truthvalue_false_node;
4608 case FUNCTION_DECL:
4609 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4610 /* Fall through. */
4612 case ADDR_EXPR:
4614 tree inner = TREE_OPERAND (expr, 0);
4615 if (decl_with_nonnull_addr_p (inner))
4617 /* Common Ada/Pascal programmer's mistake. */
4618 warning_at (location,
4619 OPT_Waddress,
4620 "the address of %qD will always evaluate as %<true%>",
4621 inner);
4622 return truthvalue_true_node;
4624 break;
4627 case COMPLEX_EXPR:
4628 expr = build_binary_op (EXPR_LOCATION (expr),
4629 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4630 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4631 c_common_truthvalue_conversion (location,
4632 TREE_OPERAND (expr, 0)),
4633 c_common_truthvalue_conversion (location,
4634 TREE_OPERAND (expr, 1)),
4636 goto ret;
4638 case NEGATE_EXPR:
4639 case ABS_EXPR:
4640 case FLOAT_EXPR:
4641 case EXCESS_PRECISION_EXPR:
4642 /* These don't change whether an object is nonzero or zero. */
4643 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4645 case LROTATE_EXPR:
4646 case RROTATE_EXPR:
4647 /* These don't change whether an object is zero or nonzero, but
4648 we can't ignore them if their second arg has side-effects. */
4649 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4651 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4652 TREE_OPERAND (expr, 1),
4653 c_common_truthvalue_conversion
4654 (location, TREE_OPERAND (expr, 0)));
4655 goto ret;
4657 else
4658 return c_common_truthvalue_conversion (location,
4659 TREE_OPERAND (expr, 0));
4661 case COND_EXPR:
4662 /* Distribute the conversion into the arms of a COND_EXPR. */
4663 if (c_dialect_cxx ())
4665 tree op1 = TREE_OPERAND (expr, 1);
4666 tree op2 = TREE_OPERAND (expr, 2);
4667 /* In C++ one of the arms might have void type if it is throw. */
4668 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4669 op1 = c_common_truthvalue_conversion (location, op1);
4670 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4671 op2 = c_common_truthvalue_conversion (location, op2);
4672 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4673 TREE_OPERAND (expr, 0), op1, op2);
4674 goto ret;
4676 else
4678 /* Folding will happen later for C. */
4679 expr = build3 (COND_EXPR, truthvalue_type_node,
4680 TREE_OPERAND (expr, 0),
4681 c_common_truthvalue_conversion (location,
4682 TREE_OPERAND (expr, 1)),
4683 c_common_truthvalue_conversion (location,
4684 TREE_OPERAND (expr, 2)));
4685 goto ret;
4688 CASE_CONVERT:
4690 tree totype = TREE_TYPE (expr);
4691 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4693 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4694 since that affects how `default_conversion' will behave. */
4695 if (TREE_CODE (totype) == REFERENCE_TYPE
4696 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4697 break;
4698 /* Don't strip a conversion from C++0x scoped enum, since they
4699 don't implicitly convert to other types. */
4700 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4701 && ENUM_IS_SCOPED (fromtype))
4702 break;
4703 /* If this isn't narrowing the argument, we can ignore it. */
4704 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4705 return c_common_truthvalue_conversion (location,
4706 TREE_OPERAND (expr, 0));
4708 break;
4710 case MODIFY_EXPR:
4711 if (!TREE_NO_WARNING (expr)
4712 && warn_parentheses)
4714 warning (OPT_Wparentheses,
4715 "suggest parentheses around assignment used as truth value");
4716 TREE_NO_WARNING (expr) = 1;
4718 break;
4720 default:
4721 break;
4724 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4726 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4727 expr = (build_binary_op
4728 (EXPR_LOCATION (expr),
4729 (TREE_SIDE_EFFECTS (expr)
4730 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4731 c_common_truthvalue_conversion
4732 (location,
4733 build_unary_op (location, REALPART_EXPR, t, 0)),
4734 c_common_truthvalue_conversion
4735 (location,
4736 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4737 0));
4738 goto ret;
4741 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4743 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4744 FCONST0 (TYPE_MODE
4745 (TREE_TYPE (expr))));
4746 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4748 else
4749 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4751 ret:
4752 protected_set_expr_location (expr, location);
4753 return expr;
4756 static void def_builtin_1 (enum built_in_function fncode,
4757 const char *name,
4758 enum built_in_class fnclass,
4759 tree fntype, tree libtype,
4760 bool both_p, bool fallback_p, bool nonansi_p,
4761 tree fnattrs, bool implicit_p);
4764 /* Apply the TYPE_QUALS to the new DECL. */
4766 void
4767 c_apply_type_quals_to_decl (int type_quals, tree decl)
4769 tree type = TREE_TYPE (decl);
4771 if (type == error_mark_node)
4772 return;
4774 if ((type_quals & TYPE_QUAL_CONST)
4775 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4776 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4777 constructor can produce constant init, so rely on cp_finish_decl to
4778 clear TREE_READONLY if the variable has non-constant init. */
4779 TREE_READONLY (decl) = 1;
4780 if (type_quals & TYPE_QUAL_VOLATILE)
4782 TREE_SIDE_EFFECTS (decl) = 1;
4783 TREE_THIS_VOLATILE (decl) = 1;
4785 if (type_quals & TYPE_QUAL_RESTRICT)
4787 while (type && TREE_CODE (type) == ARRAY_TYPE)
4788 /* Allow 'restrict' on arrays of pointers.
4789 FIXME currently we just ignore it. */
4790 type = TREE_TYPE (type);
4791 if (!type
4792 || !POINTER_TYPE_P (type)
4793 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4794 error ("invalid use of %<restrict%>");
4798 /* Hash function for the problem of multiple type definitions in
4799 different files. This must hash all types that will compare
4800 equal via comptypes to the same value. In practice it hashes
4801 on some of the simple stuff and leaves the details to comptypes. */
4803 static hashval_t
4804 c_type_hash (const void *p)
4806 int n_elements;
4807 int shift, size;
4808 const_tree const t = (const_tree) p;
4809 tree t2;
4810 switch (TREE_CODE (t))
4812 /* For pointers, hash on pointee type plus some swizzling. */
4813 case POINTER_TYPE:
4814 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4815 /* Hash on number of elements and total size. */
4816 case ENUMERAL_TYPE:
4817 shift = 3;
4818 t2 = TYPE_VALUES (t);
4819 break;
4820 case RECORD_TYPE:
4821 shift = 0;
4822 t2 = TYPE_FIELDS (t);
4823 break;
4824 case QUAL_UNION_TYPE:
4825 shift = 1;
4826 t2 = TYPE_FIELDS (t);
4827 break;
4828 case UNION_TYPE:
4829 shift = 2;
4830 t2 = TYPE_FIELDS (t);
4831 break;
4832 default:
4833 gcc_unreachable ();
4835 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4836 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4837 n_elements = list_length (t2);
4838 /* We might have a VLA here. */
4839 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4840 size = 0;
4841 else
4842 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4843 return ((size << 24) | (n_elements << shift));
4846 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4848 /* Return the typed-based alias set for T, which may be an expression
4849 or a type. Return -1 if we don't do anything special. */
4851 alias_set_type
4852 c_common_get_alias_set (tree t)
4854 tree u;
4855 PTR *slot;
4857 /* For VLAs, use the alias set of the element type rather than the
4858 default of alias set 0 for types compared structurally. */
4859 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4861 if (TREE_CODE (t) == ARRAY_TYPE)
4862 return get_alias_set (TREE_TYPE (t));
4863 return -1;
4866 /* Permit type-punning when accessing a union, provided the access
4867 is directly through the union. For example, this code does not
4868 permit taking the address of a union member and then storing
4869 through it. Even the type-punning allowed here is a GCC
4870 extension, albeit a common and useful one; the C standard says
4871 that such accesses have implementation-defined behavior. */
4872 for (u = t;
4873 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4874 u = TREE_OPERAND (u, 0))
4875 if (TREE_CODE (u) == COMPONENT_REF
4876 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4877 return 0;
4879 /* That's all the expressions we handle specially. */
4880 if (!TYPE_P (t))
4881 return -1;
4883 /* The C standard guarantees that any object may be accessed via an
4884 lvalue that has character type. */
4885 if (t == char_type_node
4886 || t == signed_char_type_node
4887 || t == unsigned_char_type_node)
4888 return 0;
4890 /* The C standard specifically allows aliasing between signed and
4891 unsigned variants of the same type. We treat the signed
4892 variant as canonical. */
4893 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4895 tree t1 = c_common_signed_type (t);
4897 /* t1 == t can happen for boolean nodes which are always unsigned. */
4898 if (t1 != t)
4899 return get_alias_set (t1);
4902 /* Handle the case of multiple type nodes referring to "the same" type,
4903 which occurs with IMA. These share an alias set. FIXME: Currently only
4904 C90 is handled. (In C99 type compatibility is not transitive, which
4905 complicates things mightily. The alias set splay trees can theoretically
4906 represent this, but insertion is tricky when you consider all the
4907 different orders things might arrive in.) */
4909 if (c_language != clk_c || flag_isoc99)
4910 return -1;
4912 /* Save time if there's only one input file. */
4913 if (num_in_fnames == 1)
4914 return -1;
4916 /* Pointers need special handling if they point to any type that
4917 needs special handling (below). */
4918 if (TREE_CODE (t) == POINTER_TYPE)
4920 tree t2;
4921 /* Find bottom type under any nested POINTERs. */
4922 for (t2 = TREE_TYPE (t);
4923 TREE_CODE (t2) == POINTER_TYPE;
4924 t2 = TREE_TYPE (t2))
4926 if (TREE_CODE (t2) != RECORD_TYPE
4927 && TREE_CODE (t2) != ENUMERAL_TYPE
4928 && TREE_CODE (t2) != QUAL_UNION_TYPE
4929 && TREE_CODE (t2) != UNION_TYPE)
4930 return -1;
4931 if (TYPE_SIZE (t2) == 0)
4932 return -1;
4934 /* These are the only cases that need special handling. */
4935 if (TREE_CODE (t) != RECORD_TYPE
4936 && TREE_CODE (t) != ENUMERAL_TYPE
4937 && TREE_CODE (t) != QUAL_UNION_TYPE
4938 && TREE_CODE (t) != UNION_TYPE
4939 && TREE_CODE (t) != POINTER_TYPE)
4940 return -1;
4941 /* Undefined? */
4942 if (TYPE_SIZE (t) == 0)
4943 return -1;
4945 /* Look up t in hash table. Only one of the compatible types within each
4946 alias set is recorded in the table. */
4947 if (!type_hash_table)
4948 type_hash_table = htab_create_ggc (1021, c_type_hash,
4949 (htab_eq) lang_hooks.types_compatible_p,
4950 NULL);
4951 slot = htab_find_slot (type_hash_table, t, INSERT);
4952 if (*slot != NULL)
4954 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4955 return TYPE_ALIAS_SET ((tree)*slot);
4957 else
4958 /* Our caller will assign and record (in t) a new alias set; all we need
4959 to do is remember t in the hash table. */
4960 *slot = t;
4962 return -1;
4965 /* Return the least alignment required for type TYPE. */
4967 unsigned int
4968 min_align_of_type (tree type)
4970 unsigned int align = TYPE_ALIGN (type);
4971 align = MIN (align, BIGGEST_ALIGNMENT);
4972 #ifdef BIGGEST_FIELD_ALIGNMENT
4973 align = MIN (align, BIGGEST_FIELD_ALIGNMENT);
4974 #endif
4975 unsigned int field_align = align;
4976 #ifdef ADJUST_FIELD_ALIGN
4977 tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
4978 type);
4979 field_align = ADJUST_FIELD_ALIGN (field, field_align);
4980 #endif
4981 align = MIN (align, field_align);
4982 return align / BITS_PER_UNIT;
4985 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4986 the IS_SIZEOF parameter indicates which operator is being applied.
4987 The COMPLAIN flag controls whether we should diagnose possibly
4988 ill-formed constructs or not. LOC is the location of the SIZEOF or
4989 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4990 a type in any context should be returned, rather than the normal
4991 alignment for that type. */
4993 tree
4994 c_sizeof_or_alignof_type (location_t loc,
4995 tree type, bool is_sizeof, bool min_alignof,
4996 int complain)
4998 const char *op_name;
4999 tree value = NULL;
5000 enum tree_code type_code = TREE_CODE (type);
5002 op_name = is_sizeof ? "sizeof" : "__alignof__";
5004 if (type_code == FUNCTION_TYPE)
5006 if (is_sizeof)
5008 if (complain && warn_pointer_arith)
5009 pedwarn (loc, OPT_Wpointer_arith,
5010 "invalid application of %<sizeof%> to a function type");
5011 else if (!complain)
5012 return error_mark_node;
5013 value = size_one_node;
5015 else
5017 if (complain)
5019 if (c_dialect_cxx ())
5020 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5021 "%<alignof%> applied to a function type");
5022 else
5023 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5024 "%<_Alignof%> applied to a function type");
5026 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5029 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5031 if (type_code == VOID_TYPE
5032 && complain && warn_pointer_arith)
5033 pedwarn (loc, OPT_Wpointer_arith,
5034 "invalid application of %qs to a void type", op_name);
5035 else if (!complain)
5036 return error_mark_node;
5037 value = size_one_node;
5039 else if (!COMPLETE_TYPE_P (type)
5040 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5042 if (complain)
5043 error_at (loc, "invalid application of %qs to incomplete type %qT",
5044 op_name, type);
5045 return error_mark_node;
5047 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5048 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5050 if (complain)
5051 error_at (loc, "invalid application of %qs to array type %qT of "
5052 "incomplete element type", op_name, type);
5053 return error_mark_node;
5055 else
5057 if (is_sizeof)
5058 /* Convert in case a char is more than one unit. */
5059 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5060 size_int (TYPE_PRECISION (char_type_node)
5061 / BITS_PER_UNIT));
5062 else if (min_alignof)
5063 value = size_int (min_align_of_type (type));
5064 else
5065 value = size_int (TYPE_ALIGN_UNIT (type));
5068 /* VALUE will have the middle-end integer type sizetype.
5069 However, we should really return a value of type `size_t',
5070 which is just a typedef for an ordinary integer type. */
5071 value = fold_convert_loc (loc, size_type_node, value);
5073 return value;
5076 /* Implement the __alignof keyword: Return the minimum required
5077 alignment of EXPR, measured in bytes. For VAR_DECLs,
5078 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5079 from an "aligned" __attribute__ specification). LOC is the
5080 location of the ALIGNOF operator. */
5082 tree
5083 c_alignof_expr (location_t loc, tree expr)
5085 tree t;
5087 if (VAR_OR_FUNCTION_DECL_P (expr))
5088 t = size_int (DECL_ALIGN_UNIT (expr));
5090 else if (TREE_CODE (expr) == COMPONENT_REF
5091 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5093 error_at (loc, "%<__alignof%> applied to a bit-field");
5094 t = size_one_node;
5096 else if (TREE_CODE (expr) == COMPONENT_REF
5097 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5098 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5100 else if (TREE_CODE (expr) == INDIRECT_REF)
5102 tree t = TREE_OPERAND (expr, 0);
5103 tree best = t;
5104 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5106 while (CONVERT_EXPR_P (t)
5107 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5109 int thisalign;
5111 t = TREE_OPERAND (t, 0);
5112 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5113 if (thisalign > bestalign)
5114 best = t, bestalign = thisalign;
5116 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5118 else
5119 return c_alignof (loc, TREE_TYPE (expr));
5121 return fold_convert_loc (loc, size_type_node, t);
5124 /* Handle C and C++ default attributes. */
5126 enum built_in_attribute
5128 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5129 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5130 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5131 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5132 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5133 #include "builtin-attrs.def"
5134 #undef DEF_ATTR_NULL_TREE
5135 #undef DEF_ATTR_INT
5136 #undef DEF_ATTR_STRING
5137 #undef DEF_ATTR_IDENT
5138 #undef DEF_ATTR_TREE_LIST
5139 ATTR_LAST
5142 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5144 static void c_init_attributes (void);
5146 enum c_builtin_type
5148 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5149 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5150 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5151 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5152 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5153 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5154 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5155 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5156 ARG6) NAME,
5157 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5158 ARG6, ARG7) NAME,
5159 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5160 ARG6, ARG7, ARG8) NAME,
5161 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5162 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5163 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5164 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5165 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5166 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5167 NAME,
5168 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5169 #include "builtin-types.def"
5170 #undef DEF_PRIMITIVE_TYPE
5171 #undef DEF_FUNCTION_TYPE_0
5172 #undef DEF_FUNCTION_TYPE_1
5173 #undef DEF_FUNCTION_TYPE_2
5174 #undef DEF_FUNCTION_TYPE_3
5175 #undef DEF_FUNCTION_TYPE_4
5176 #undef DEF_FUNCTION_TYPE_5
5177 #undef DEF_FUNCTION_TYPE_6
5178 #undef DEF_FUNCTION_TYPE_7
5179 #undef DEF_FUNCTION_TYPE_8
5180 #undef DEF_FUNCTION_TYPE_VAR_0
5181 #undef DEF_FUNCTION_TYPE_VAR_1
5182 #undef DEF_FUNCTION_TYPE_VAR_2
5183 #undef DEF_FUNCTION_TYPE_VAR_3
5184 #undef DEF_FUNCTION_TYPE_VAR_4
5185 #undef DEF_FUNCTION_TYPE_VAR_5
5186 #undef DEF_POINTER_TYPE
5187 BT_LAST
5190 typedef enum c_builtin_type builtin_type;
5192 /* A temporary array for c_common_nodes_and_builtins. Used in
5193 communication with def_fn_type. */
5194 static tree builtin_types[(int) BT_LAST + 1];
5196 /* A helper function for c_common_nodes_and_builtins. Build function type
5197 for DEF with return type RET and N arguments. If VAR is true, then the
5198 function should be variadic after those N arguments.
5200 Takes special care not to ICE if any of the types involved are
5201 error_mark_node, which indicates that said type is not in fact available
5202 (see builtin_type_for_size). In which case the function type as a whole
5203 should be error_mark_node. */
5205 static void
5206 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5208 tree t;
5209 tree *args = XALLOCAVEC (tree, n);
5210 va_list list;
5211 int i;
5213 va_start (list, n);
5214 for (i = 0; i < n; ++i)
5216 builtin_type a = (builtin_type) va_arg (list, int);
5217 t = builtin_types[a];
5218 if (t == error_mark_node)
5219 goto egress;
5220 args[i] = t;
5223 t = builtin_types[ret];
5224 if (t == error_mark_node)
5225 goto egress;
5226 if (var)
5227 t = build_varargs_function_type_array (t, n, args);
5228 else
5229 t = build_function_type_array (t, n, args);
5231 egress:
5232 builtin_types[def] = t;
5233 va_end (list);
5236 /* Build builtin functions common to both C and C++ language
5237 frontends. */
5239 static void
5240 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5242 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5243 builtin_types[ENUM] = VALUE;
5244 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5245 def_fn_type (ENUM, RETURN, 0, 0);
5246 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5247 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5248 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5249 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5250 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5251 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5252 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5253 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5254 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5255 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5256 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5257 ARG6) \
5258 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5259 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5260 ARG6, ARG7) \
5261 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5262 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5263 ARG6, ARG7, ARG8) \
5264 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5265 ARG7, ARG8);
5266 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5267 def_fn_type (ENUM, RETURN, 1, 0);
5268 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5269 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5270 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5271 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5272 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5273 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5274 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5275 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5276 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5277 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5278 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5279 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5281 #include "builtin-types.def"
5283 #undef DEF_PRIMITIVE_TYPE
5284 #undef DEF_FUNCTION_TYPE_0
5285 #undef DEF_FUNCTION_TYPE_1
5286 #undef DEF_FUNCTION_TYPE_2
5287 #undef DEF_FUNCTION_TYPE_3
5288 #undef DEF_FUNCTION_TYPE_4
5289 #undef DEF_FUNCTION_TYPE_5
5290 #undef DEF_FUNCTION_TYPE_6
5291 #undef DEF_FUNCTION_TYPE_7
5292 #undef DEF_FUNCTION_TYPE_8
5293 #undef DEF_FUNCTION_TYPE_VAR_0
5294 #undef DEF_FUNCTION_TYPE_VAR_1
5295 #undef DEF_FUNCTION_TYPE_VAR_2
5296 #undef DEF_FUNCTION_TYPE_VAR_3
5297 #undef DEF_FUNCTION_TYPE_VAR_4
5298 #undef DEF_FUNCTION_TYPE_VAR_5
5299 #undef DEF_POINTER_TYPE
5300 builtin_types[(int) BT_LAST] = NULL_TREE;
5302 c_init_attributes ();
5304 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5305 NONANSI_P, ATTRS, IMPLICIT, COND) \
5306 if (NAME && COND) \
5307 def_builtin_1 (ENUM, NAME, CLASS, \
5308 builtin_types[(int) TYPE], \
5309 builtin_types[(int) LIBTYPE], \
5310 BOTH_P, FALLBACK_P, NONANSI_P, \
5311 built_in_attributes[(int) ATTRS], IMPLICIT);
5312 #include "builtins.def"
5313 #undef DEF_BUILTIN
5315 targetm.init_builtins ();
5317 build_common_builtin_nodes ();
5319 if (flag_cilkplus)
5320 cilk_init_builtins ();
5323 /* Like get_identifier, but avoid warnings about null arguments when
5324 the argument may be NULL for targets where GCC lacks stdint.h type
5325 information. */
5327 static inline tree
5328 c_get_ident (const char *id)
5330 return get_identifier (id);
5333 /* Build tree nodes and builtin functions common to both C and C++ language
5334 frontends. */
5336 void
5337 c_common_nodes_and_builtins (void)
5339 int char16_type_size;
5340 int char32_type_size;
5341 int wchar_type_size;
5342 tree array_domain_type;
5343 tree va_list_ref_type_node;
5344 tree va_list_arg_type_node;
5346 build_common_tree_nodes (flag_signed_char, flag_short_double);
5348 /* Define `int' and `char' first so that dbx will output them first. */
5349 record_builtin_type (RID_INT, NULL, integer_type_node);
5350 record_builtin_type (RID_CHAR, "char", char_type_node);
5352 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5353 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5354 but not C. Are the conditionals here needed? */
5355 if (c_dialect_cxx ())
5356 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5357 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5358 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5359 record_builtin_type (RID_MAX, "long unsigned int",
5360 long_unsigned_type_node);
5361 if (int128_integer_type_node != NULL_TREE)
5363 record_builtin_type (RID_INT128, "__int128",
5364 int128_integer_type_node);
5365 record_builtin_type (RID_MAX, "__int128 unsigned",
5366 int128_unsigned_type_node);
5368 if (c_dialect_cxx ())
5369 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5370 record_builtin_type (RID_MAX, "long long int",
5371 long_long_integer_type_node);
5372 record_builtin_type (RID_MAX, "long long unsigned int",
5373 long_long_unsigned_type_node);
5374 if (c_dialect_cxx ())
5375 record_builtin_type (RID_MAX, "long long unsigned",
5376 long_long_unsigned_type_node);
5377 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5378 record_builtin_type (RID_MAX, "short unsigned int",
5379 short_unsigned_type_node);
5380 if (c_dialect_cxx ())
5381 record_builtin_type (RID_MAX, "unsigned short",
5382 short_unsigned_type_node);
5384 /* Define both `signed char' and `unsigned char'. */
5385 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5386 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5388 /* These are types that c_common_type_for_size and
5389 c_common_type_for_mode use. */
5390 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5391 TYPE_DECL, NULL_TREE,
5392 intQI_type_node));
5393 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5394 TYPE_DECL, NULL_TREE,
5395 intHI_type_node));
5396 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5397 TYPE_DECL, NULL_TREE,
5398 intSI_type_node));
5399 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5400 TYPE_DECL, NULL_TREE,
5401 intDI_type_node));
5402 #if HOST_BITS_PER_WIDE_INT >= 64
5403 if (targetm.scalar_mode_supported_p (TImode))
5404 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5405 TYPE_DECL,
5406 get_identifier ("__int128_t"),
5407 intTI_type_node));
5408 #endif
5409 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5410 TYPE_DECL, NULL_TREE,
5411 unsigned_intQI_type_node));
5412 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5413 TYPE_DECL, NULL_TREE,
5414 unsigned_intHI_type_node));
5415 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5416 TYPE_DECL, NULL_TREE,
5417 unsigned_intSI_type_node));
5418 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5419 TYPE_DECL, NULL_TREE,
5420 unsigned_intDI_type_node));
5421 #if HOST_BITS_PER_WIDE_INT >= 64
5422 if (targetm.scalar_mode_supported_p (TImode))
5423 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5424 TYPE_DECL,
5425 get_identifier ("__uint128_t"),
5426 unsigned_intTI_type_node));
5427 #endif
5429 /* Create the widest literal types. */
5430 widest_integer_literal_type_node
5431 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5432 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5433 TYPE_DECL, NULL_TREE,
5434 widest_integer_literal_type_node));
5436 widest_unsigned_literal_type_node
5437 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5438 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5439 TYPE_DECL, NULL_TREE,
5440 widest_unsigned_literal_type_node));
5442 signed_size_type_node = c_common_signed_type (size_type_node);
5444 pid_type_node =
5445 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5447 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5448 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5449 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5451 /* Only supported decimal floating point extension if the target
5452 actually supports underlying modes. */
5453 if (targetm.scalar_mode_supported_p (SDmode)
5454 && targetm.scalar_mode_supported_p (DDmode)
5455 && targetm.scalar_mode_supported_p (TDmode))
5457 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5458 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5459 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5462 if (targetm.fixed_point_supported_p ())
5464 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5465 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5466 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5467 record_builtin_type (RID_MAX, "long long _Fract",
5468 long_long_fract_type_node);
5469 record_builtin_type (RID_MAX, "unsigned short _Fract",
5470 unsigned_short_fract_type_node);
5471 record_builtin_type (RID_MAX, "unsigned _Fract",
5472 unsigned_fract_type_node);
5473 record_builtin_type (RID_MAX, "unsigned long _Fract",
5474 unsigned_long_fract_type_node);
5475 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5476 unsigned_long_long_fract_type_node);
5477 record_builtin_type (RID_MAX, "_Sat short _Fract",
5478 sat_short_fract_type_node);
5479 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5480 record_builtin_type (RID_MAX, "_Sat long _Fract",
5481 sat_long_fract_type_node);
5482 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5483 sat_long_long_fract_type_node);
5484 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5485 sat_unsigned_short_fract_type_node);
5486 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5487 sat_unsigned_fract_type_node);
5488 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5489 sat_unsigned_long_fract_type_node);
5490 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5491 sat_unsigned_long_long_fract_type_node);
5492 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5493 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5494 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5495 record_builtin_type (RID_MAX, "long long _Accum",
5496 long_long_accum_type_node);
5497 record_builtin_type (RID_MAX, "unsigned short _Accum",
5498 unsigned_short_accum_type_node);
5499 record_builtin_type (RID_MAX, "unsigned _Accum",
5500 unsigned_accum_type_node);
5501 record_builtin_type (RID_MAX, "unsigned long _Accum",
5502 unsigned_long_accum_type_node);
5503 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5504 unsigned_long_long_accum_type_node);
5505 record_builtin_type (RID_MAX, "_Sat short _Accum",
5506 sat_short_accum_type_node);
5507 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5508 record_builtin_type (RID_MAX, "_Sat long _Accum",
5509 sat_long_accum_type_node);
5510 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5511 sat_long_long_accum_type_node);
5512 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5513 sat_unsigned_short_accum_type_node);
5514 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5515 sat_unsigned_accum_type_node);
5516 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5517 sat_unsigned_long_accum_type_node);
5518 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5519 sat_unsigned_long_long_accum_type_node);
5523 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5524 TYPE_DECL,
5525 get_identifier ("complex int"),
5526 complex_integer_type_node));
5527 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5528 TYPE_DECL,
5529 get_identifier ("complex float"),
5530 complex_float_type_node));
5531 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5532 TYPE_DECL,
5533 get_identifier ("complex double"),
5534 complex_double_type_node));
5535 lang_hooks.decls.pushdecl
5536 (build_decl (UNKNOWN_LOCATION,
5537 TYPE_DECL, get_identifier ("complex long double"),
5538 complex_long_double_type_node));
5540 if (c_dialect_cxx ())
5541 /* For C++, make fileptr_type_node a distinct void * type until
5542 FILE type is defined. */
5543 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5545 record_builtin_type (RID_VOID, NULL, void_type_node);
5547 /* Set the TYPE_NAME for any variants that were built before
5548 record_builtin_type gave names to the built-in types. */
5550 tree void_name = TYPE_NAME (void_type_node);
5551 TYPE_NAME (void_type_node) = NULL_TREE;
5552 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5553 = void_name;
5554 TYPE_NAME (void_type_node) = void_name;
5557 void_list_node = build_void_list_node ();
5559 /* Make a type to be the domain of a few array types
5560 whose domains don't really matter.
5561 200 is small enough that it always fits in size_t
5562 and large enough that it can hold most function names for the
5563 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5564 array_domain_type = build_index_type (size_int (200));
5566 /* Make a type for arrays of characters.
5567 With luck nothing will ever really depend on the length of this
5568 array type. */
5569 char_array_type_node
5570 = build_array_type (char_type_node, array_domain_type);
5572 string_type_node = build_pointer_type (char_type_node);
5573 const_string_type_node
5574 = build_pointer_type (build_qualified_type
5575 (char_type_node, TYPE_QUAL_CONST));
5577 /* This is special for C++ so functions can be overloaded. */
5578 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5579 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5580 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5581 underlying_wchar_type_node = wchar_type_node;
5582 if (c_dialect_cxx ())
5584 if (TYPE_UNSIGNED (wchar_type_node))
5585 wchar_type_node = make_unsigned_type (wchar_type_size);
5586 else
5587 wchar_type_node = make_signed_type (wchar_type_size);
5588 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5591 /* This is for wide string constants. */
5592 wchar_array_type_node
5593 = build_array_type (wchar_type_node, array_domain_type);
5595 /* Define 'char16_t'. */
5596 char16_type_node = get_identifier (CHAR16_TYPE);
5597 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5598 char16_type_size = TYPE_PRECISION (char16_type_node);
5599 if (c_dialect_cxx ())
5601 char16_type_node = make_unsigned_type (char16_type_size);
5603 if (cxx_dialect >= cxx11)
5604 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5607 /* This is for UTF-16 string constants. */
5608 char16_array_type_node
5609 = build_array_type (char16_type_node, array_domain_type);
5611 /* Define 'char32_t'. */
5612 char32_type_node = get_identifier (CHAR32_TYPE);
5613 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5614 char32_type_size = TYPE_PRECISION (char32_type_node);
5615 if (c_dialect_cxx ())
5617 char32_type_node = make_unsigned_type (char32_type_size);
5619 if (cxx_dialect >= cxx11)
5620 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5623 /* This is for UTF-32 string constants. */
5624 char32_array_type_node
5625 = build_array_type (char32_type_node, array_domain_type);
5627 wint_type_node =
5628 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5630 intmax_type_node =
5631 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5632 uintmax_type_node =
5633 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5635 if (SIG_ATOMIC_TYPE)
5636 sig_atomic_type_node =
5637 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5638 if (INT8_TYPE)
5639 int8_type_node =
5640 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5641 if (INT16_TYPE)
5642 int16_type_node =
5643 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5644 if (INT32_TYPE)
5645 int32_type_node =
5646 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5647 if (INT64_TYPE)
5648 int64_type_node =
5649 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5650 if (UINT8_TYPE)
5651 uint8_type_node =
5652 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5653 if (UINT16_TYPE)
5654 c_uint16_type_node = uint16_type_node =
5655 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5656 if (UINT32_TYPE)
5657 c_uint32_type_node = uint32_type_node =
5658 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5659 if (UINT64_TYPE)
5660 c_uint64_type_node = uint64_type_node =
5661 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5662 if (INT_LEAST8_TYPE)
5663 int_least8_type_node =
5664 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5665 if (INT_LEAST16_TYPE)
5666 int_least16_type_node =
5667 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5668 if (INT_LEAST32_TYPE)
5669 int_least32_type_node =
5670 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5671 if (INT_LEAST64_TYPE)
5672 int_least64_type_node =
5673 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5674 if (UINT_LEAST8_TYPE)
5675 uint_least8_type_node =
5676 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5677 if (UINT_LEAST16_TYPE)
5678 uint_least16_type_node =
5679 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5680 if (UINT_LEAST32_TYPE)
5681 uint_least32_type_node =
5682 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5683 if (UINT_LEAST64_TYPE)
5684 uint_least64_type_node =
5685 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5686 if (INT_FAST8_TYPE)
5687 int_fast8_type_node =
5688 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5689 if (INT_FAST16_TYPE)
5690 int_fast16_type_node =
5691 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5692 if (INT_FAST32_TYPE)
5693 int_fast32_type_node =
5694 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5695 if (INT_FAST64_TYPE)
5696 int_fast64_type_node =
5697 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5698 if (UINT_FAST8_TYPE)
5699 uint_fast8_type_node =
5700 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5701 if (UINT_FAST16_TYPE)
5702 uint_fast16_type_node =
5703 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5704 if (UINT_FAST32_TYPE)
5705 uint_fast32_type_node =
5706 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5707 if (UINT_FAST64_TYPE)
5708 uint_fast64_type_node =
5709 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5710 if (INTPTR_TYPE)
5711 intptr_type_node =
5712 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5713 if (UINTPTR_TYPE)
5714 uintptr_type_node =
5715 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5717 default_function_type
5718 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5719 ptrdiff_type_node
5720 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5721 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5723 lang_hooks.decls.pushdecl
5724 (build_decl (UNKNOWN_LOCATION,
5725 TYPE_DECL, get_identifier ("__builtin_va_list"),
5726 va_list_type_node));
5727 if (targetm.enum_va_list_p)
5729 int l;
5730 const char *pname;
5731 tree ptype;
5733 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5735 lang_hooks.decls.pushdecl
5736 (build_decl (UNKNOWN_LOCATION,
5737 TYPE_DECL, get_identifier (pname),
5738 ptype));
5743 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5745 va_list_arg_type_node = va_list_ref_type_node =
5746 build_pointer_type (TREE_TYPE (va_list_type_node));
5748 else
5750 va_list_arg_type_node = va_list_type_node;
5751 va_list_ref_type_node = build_reference_type (va_list_type_node);
5754 if (!flag_preprocess_only)
5755 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5757 main_identifier_node = get_identifier ("main");
5759 /* Create the built-in __null node. It is important that this is
5760 not shared. */
5761 null_node = make_int_cst (1, 1);
5762 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5764 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5765 memset (builtin_types, 0, sizeof (builtin_types));
5768 /* The number of named compound-literals generated thus far. */
5769 static GTY(()) int compound_literal_number;
5771 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5773 void
5774 set_compound_literal_name (tree decl)
5776 char *name;
5777 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5778 compound_literal_number);
5779 compound_literal_number++;
5780 DECL_NAME (decl) = get_identifier (name);
5783 tree
5784 build_va_arg (location_t loc, tree expr, tree type)
5786 expr = build1 (VA_ARG_EXPR, type, expr);
5787 SET_EXPR_LOCATION (expr, loc);
5788 return expr;
5792 /* Linked list of disabled built-in functions. */
5794 typedef struct disabled_builtin
5796 const char *name;
5797 struct disabled_builtin *next;
5798 } disabled_builtin;
5799 static disabled_builtin *disabled_builtins = NULL;
5801 static bool builtin_function_disabled_p (const char *);
5803 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5804 begins with "__builtin_", give an error. */
5806 void
5807 disable_builtin_function (const char *name)
5809 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5810 error ("cannot disable built-in function %qs", name);
5811 else
5813 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5814 new_disabled_builtin->name = name;
5815 new_disabled_builtin->next = disabled_builtins;
5816 disabled_builtins = new_disabled_builtin;
5821 /* Return true if the built-in function NAME has been disabled, false
5822 otherwise. */
5824 static bool
5825 builtin_function_disabled_p (const char *name)
5827 disabled_builtin *p;
5828 for (p = disabled_builtins; p != NULL; p = p->next)
5830 if (strcmp (name, p->name) == 0)
5831 return true;
5833 return false;
5837 /* Worker for DEF_BUILTIN.
5838 Possibly define a builtin function with one or two names.
5839 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5840 nonansi_p and flag_no_nonansi_builtin. */
5842 static void
5843 def_builtin_1 (enum built_in_function fncode,
5844 const char *name,
5845 enum built_in_class fnclass,
5846 tree fntype, tree libtype,
5847 bool both_p, bool fallback_p, bool nonansi_p,
5848 tree fnattrs, bool implicit_p)
5850 tree decl;
5851 const char *libname;
5853 if (fntype == error_mark_node)
5854 return;
5856 gcc_assert ((!both_p && !fallback_p)
5857 || !strncmp (name, "__builtin_",
5858 strlen ("__builtin_")));
5860 libname = name + strlen ("__builtin_");
5861 decl = add_builtin_function (name, fntype, fncode, fnclass,
5862 (fallback_p ? libname : NULL),
5863 fnattrs);
5865 set_builtin_decl (fncode, decl, implicit_p);
5867 if (both_p
5868 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5869 && !(nonansi_p && flag_no_nonansi_builtin))
5870 add_builtin_function (libname, libtype, fncode, fnclass,
5871 NULL, fnattrs);
5874 /* Nonzero if the type T promotes to int. This is (nearly) the
5875 integral promotions defined in ISO C99 6.3.1.1/2. */
5877 bool
5878 c_promoting_integer_type_p (const_tree t)
5880 switch (TREE_CODE (t))
5882 case INTEGER_TYPE:
5883 return (TYPE_MAIN_VARIANT (t) == char_type_node
5884 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5885 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5886 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5887 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5888 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5890 case ENUMERAL_TYPE:
5891 /* ??? Technically all enumerations not larger than an int
5892 promote to an int. But this is used along code paths
5893 that only want to notice a size change. */
5894 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5896 case BOOLEAN_TYPE:
5897 return 1;
5899 default:
5900 return 0;
5904 /* Return 1 if PARMS specifies a fixed number of parameters
5905 and none of their types is affected by default promotions. */
5908 self_promoting_args_p (const_tree parms)
5910 const_tree t;
5911 for (t = parms; t; t = TREE_CHAIN (t))
5913 tree type = TREE_VALUE (t);
5915 if (type == error_mark_node)
5916 continue;
5918 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5919 return 0;
5921 if (type == 0)
5922 return 0;
5924 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5925 return 0;
5927 if (c_promoting_integer_type_p (type))
5928 return 0;
5930 return 1;
5933 /* Recursively remove any '*' or '&' operator from TYPE. */
5934 tree
5935 strip_pointer_operator (tree t)
5937 while (POINTER_TYPE_P (t))
5938 t = TREE_TYPE (t);
5939 return t;
5942 /* Recursively remove pointer or array type from TYPE. */
5943 tree
5944 strip_pointer_or_array_types (tree t)
5946 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5947 t = TREE_TYPE (t);
5948 return t;
5951 /* Used to compare case labels. K1 and K2 are actually tree nodes
5952 representing case labels, or NULL_TREE for a `default' label.
5953 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5954 K2, and 0 if K1 and K2 are equal. */
5957 case_compare (splay_tree_key k1, splay_tree_key k2)
5959 /* Consider a NULL key (such as arises with a `default' label) to be
5960 smaller than anything else. */
5961 if (!k1)
5962 return k2 ? -1 : 0;
5963 else if (!k2)
5964 return k1 ? 1 : 0;
5966 return tree_int_cst_compare ((tree) k1, (tree) k2);
5969 /* Process a case label, located at LOC, for the range LOW_VALUE
5970 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5971 then this case label is actually a `default' label. If only
5972 HIGH_VALUE is NULL_TREE, then case label was declared using the
5973 usual C/C++ syntax, rather than the GNU case range extension.
5974 CASES is a tree containing all the case ranges processed so far;
5975 COND is the condition for the switch-statement itself. Returns the
5976 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5977 is created. */
5979 tree
5980 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5981 tree low_value, tree high_value)
5983 tree type;
5984 tree label;
5985 tree case_label;
5986 splay_tree_node node;
5988 /* Create the LABEL_DECL itself. */
5989 label = create_artificial_label (loc);
5991 /* If there was an error processing the switch condition, bail now
5992 before we get more confused. */
5993 if (!cond || cond == error_mark_node)
5994 goto error_out;
5996 if ((low_value && TREE_TYPE (low_value)
5997 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5998 || (high_value && TREE_TYPE (high_value)
5999 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6001 error_at (loc, "pointers are not permitted as case values");
6002 goto error_out;
6005 /* Case ranges are a GNU extension. */
6006 if (high_value)
6007 pedwarn (loc, OPT_Wpedantic,
6008 "range expressions in switch statements are non-standard");
6010 type = TREE_TYPE (cond);
6011 if (low_value)
6013 low_value = check_case_value (low_value);
6014 low_value = convert_and_check (loc, type, low_value);
6015 if (low_value == error_mark_node)
6016 goto error_out;
6018 if (high_value)
6020 high_value = check_case_value (high_value);
6021 high_value = convert_and_check (loc, type, high_value);
6022 if (high_value == error_mark_node)
6023 goto error_out;
6026 if (low_value && high_value)
6028 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6029 really a case range, even though it was written that way.
6030 Remove the HIGH_VALUE to simplify later processing. */
6031 if (tree_int_cst_equal (low_value, high_value))
6032 high_value = NULL_TREE;
6033 else if (!tree_int_cst_lt (low_value, high_value))
6034 warning_at (loc, 0, "empty range specified");
6037 /* See if the case is in range of the type of the original testing
6038 expression. If both low_value and high_value are out of range,
6039 don't insert the case label and return NULL_TREE. */
6040 if (low_value
6041 && !check_case_bounds (loc, type, orig_type,
6042 &low_value, high_value ? &high_value : NULL))
6043 return NULL_TREE;
6045 /* Look up the LOW_VALUE in the table of case labels we already
6046 have. */
6047 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6048 /* If there was not an exact match, check for overlapping ranges.
6049 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6050 that's a `default' label and the only overlap is an exact match. */
6051 if (!node && (low_value || high_value))
6053 splay_tree_node low_bound;
6054 splay_tree_node high_bound;
6056 /* Even though there wasn't an exact match, there might be an
6057 overlap between this case range and another case range.
6058 Since we've (inductively) not allowed any overlapping case
6059 ranges, we simply need to find the greatest low case label
6060 that is smaller that LOW_VALUE, and the smallest low case
6061 label that is greater than LOW_VALUE. If there is an overlap
6062 it will occur in one of these two ranges. */
6063 low_bound = splay_tree_predecessor (cases,
6064 (splay_tree_key) low_value);
6065 high_bound = splay_tree_successor (cases,
6066 (splay_tree_key) low_value);
6068 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6069 the LOW_VALUE, so there is no need to check unless the
6070 LOW_BOUND is in fact itself a case range. */
6071 if (low_bound
6072 && CASE_HIGH ((tree) low_bound->value)
6073 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6074 low_value) >= 0)
6075 node = low_bound;
6076 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6077 range is bigger than the low end of the current range, so we
6078 are only interested if the current range is a real range, and
6079 not an ordinary case label. */
6080 else if (high_bound
6081 && high_value
6082 && (tree_int_cst_compare ((tree) high_bound->key,
6083 high_value)
6084 <= 0))
6085 node = high_bound;
6087 /* If there was an overlap, issue an error. */
6088 if (node)
6090 tree duplicate = CASE_LABEL ((tree) node->value);
6092 if (high_value)
6094 error_at (loc, "duplicate (or overlapping) case value");
6095 error_at (DECL_SOURCE_LOCATION (duplicate),
6096 "this is the first entry overlapping that value");
6098 else if (low_value)
6100 error_at (loc, "duplicate case value") ;
6101 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6103 else
6105 error_at (loc, "multiple default labels in one switch");
6106 error_at (DECL_SOURCE_LOCATION (duplicate),
6107 "this is the first default label");
6109 goto error_out;
6112 /* Add a CASE_LABEL to the statement-tree. */
6113 case_label = add_stmt (build_case_label (low_value, high_value, label));
6114 /* Register this case label in the splay tree. */
6115 splay_tree_insert (cases,
6116 (splay_tree_key) low_value,
6117 (splay_tree_value) case_label);
6119 return case_label;
6121 error_out:
6122 /* Add a label so that the back-end doesn't think that the beginning of
6123 the switch is unreachable. Note that we do not add a case label, as
6124 that just leads to duplicates and thence to failure later on. */
6125 if (!cases->root)
6127 tree t = create_artificial_label (loc);
6128 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6130 return error_mark_node;
6133 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6134 Used to verify that case values match up with enumerator values. */
6136 static void
6137 match_case_to_enum_1 (tree key, tree type, tree label)
6139 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6141 if (tree_fits_uhwi_p (key))
6142 print_dec (key, buf, UNSIGNED);
6143 else if (tree_fits_shwi_p (key))
6144 print_dec (key, buf, SIGNED);
6145 else
6146 print_hex (key, buf);
6148 if (TYPE_NAME (type) == 0)
6149 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6150 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6151 "case value %qs not in enumerated type",
6152 buf);
6153 else
6154 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6155 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6156 "case value %qs not in enumerated type %qT",
6157 buf, type);
6160 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6161 Used to verify that case values match up with enumerator values. */
6163 static int
6164 match_case_to_enum (splay_tree_node node, void *data)
6166 tree label = (tree) node->value;
6167 tree type = (tree) data;
6169 /* Skip default case. */
6170 if (!CASE_LOW (label))
6171 return 0;
6173 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6174 when we did our enum->case scan. Reset our scratch bit after. */
6175 if (!CASE_LOW_SEEN (label))
6176 match_case_to_enum_1 (CASE_LOW (label), type, label);
6177 else
6178 CASE_LOW_SEEN (label) = 0;
6180 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6181 not set, that means that CASE_HIGH did not appear when we did our
6182 enum->case scan. Reset our scratch bit after. */
6183 if (CASE_HIGH (label))
6185 if (!CASE_HIGH_SEEN (label))
6186 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6187 else
6188 CASE_HIGH_SEEN (label) = 0;
6191 return 0;
6194 /* Handle -Wswitch*. Called from the front end after parsing the
6195 switch construct. */
6196 /* ??? Should probably be somewhere generic, since other languages
6197 besides C and C++ would want this. At the moment, however, C/C++
6198 are the only tree-ssa languages that support enumerations at all,
6199 so the point is moot. */
6201 void
6202 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6203 tree type, tree cond)
6205 splay_tree_node default_node;
6206 splay_tree_node node;
6207 tree chain;
6209 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6210 return;
6212 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6213 if (!default_node)
6214 warning_at (switch_location, OPT_Wswitch_default,
6215 "switch missing default case");
6217 /* From here on, we only care about about enumerated types. */
6218 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6219 return;
6221 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6222 if (!warn_switch_enum && !warn_switch)
6223 return;
6225 /* Check the cases. Warn about case values which are not members of
6226 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6227 there is no default case, check that exactly all enumeration
6228 literals are covered by the cases. */
6230 /* Clearing COND if it is not an integer constant simplifies
6231 the tests inside the loop below. */
6232 if (TREE_CODE (cond) != INTEGER_CST)
6233 cond = NULL_TREE;
6235 /* The time complexity here is O(N*lg(N)) worst case, but for the
6236 common case of monotonically increasing enumerators, it is
6237 O(N), since the nature of the splay tree will keep the next
6238 element adjacent to the root at all times. */
6240 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6242 tree value = TREE_VALUE (chain);
6243 if (TREE_CODE (value) == CONST_DECL)
6244 value = DECL_INITIAL (value);
6245 node = splay_tree_lookup (cases, (splay_tree_key) value);
6246 if (node)
6248 /* Mark the CASE_LOW part of the case entry as seen. */
6249 tree label = (tree) node->value;
6250 CASE_LOW_SEEN (label) = 1;
6251 continue;
6254 /* Even though there wasn't an exact match, there might be a
6255 case range which includes the enumerator's value. */
6256 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6257 if (node && CASE_HIGH ((tree) node->value))
6259 tree label = (tree) node->value;
6260 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6261 if (cmp >= 0)
6263 /* If we match the upper bound exactly, mark the CASE_HIGH
6264 part of the case entry as seen. */
6265 if (cmp == 0)
6266 CASE_HIGH_SEEN (label) = 1;
6267 continue;
6271 /* We've now determined that this enumerated literal isn't
6272 handled by the case labels of the switch statement. */
6274 /* If the switch expression is a constant, we only really care
6275 about whether that constant is handled by the switch. */
6276 if (cond && tree_int_cst_compare (cond, value))
6277 continue;
6279 /* If there is a default_node, the only relevant option is
6280 Wswitch-enum. Otherwise, if both are enabled then we prefer
6281 to warn using -Wswitch because -Wswitch is enabled by -Wall
6282 while -Wswitch-enum is explicit. */
6283 warning_at (switch_location,
6284 (default_node || !warn_switch
6285 ? OPT_Wswitch_enum
6286 : OPT_Wswitch),
6287 "enumeration value %qE not handled in switch",
6288 TREE_PURPOSE (chain));
6291 /* Warn if there are case expressions that don't correspond to
6292 enumerators. This can occur since C and C++ don't enforce
6293 type-checking of assignments to enumeration variables.
6295 The time complexity here is now always O(N) worst case, since
6296 we should have marked both the lower bound and upper bound of
6297 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6298 above. This scan also resets those fields. */
6300 splay_tree_foreach (cases, match_case_to_enum, type);
6303 /* Finish an expression taking the address of LABEL (an
6304 IDENTIFIER_NODE). Returns an expression for the address.
6306 LOC is the location for the expression returned. */
6308 tree
6309 finish_label_address_expr (tree label, location_t loc)
6311 tree result;
6313 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6315 if (label == error_mark_node)
6316 return error_mark_node;
6318 label = lookup_label (label);
6319 if (label == NULL_TREE)
6320 result = null_pointer_node;
6321 else
6323 TREE_USED (label) = 1;
6324 result = build1 (ADDR_EXPR, ptr_type_node, label);
6325 /* The current function is not necessarily uninlinable.
6326 Computed gotos are incompatible with inlining, but the value
6327 here could be used only in a diagnostic, for example. */
6328 protected_set_expr_location (result, loc);
6331 return result;
6335 /* Given a boolean expression ARG, return a tree representing an increment
6336 or decrement (as indicated by CODE) of ARG. The front end must check for
6337 invalid cases (e.g., decrement in C++). */
6338 tree
6339 boolean_increment (enum tree_code code, tree arg)
6341 tree val;
6342 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6344 arg = stabilize_reference (arg);
6345 switch (code)
6347 case PREINCREMENT_EXPR:
6348 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6349 break;
6350 case POSTINCREMENT_EXPR:
6351 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6352 arg = save_expr (arg);
6353 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6354 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6355 break;
6356 case PREDECREMENT_EXPR:
6357 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6358 invert_truthvalue_loc (input_location, arg));
6359 break;
6360 case POSTDECREMENT_EXPR:
6361 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6362 invert_truthvalue_loc (input_location, arg));
6363 arg = save_expr (arg);
6364 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6365 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6366 break;
6367 default:
6368 gcc_unreachable ();
6370 TREE_SIDE_EFFECTS (val) = 1;
6371 return val;
6374 /* Built-in macros for stddef.h and stdint.h, that require macros
6375 defined in this file. */
6376 void
6377 c_stddef_cpp_builtins(void)
6379 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6380 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6381 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6382 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6383 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6384 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6385 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6386 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6387 if (SIG_ATOMIC_TYPE)
6388 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6389 if (INT8_TYPE)
6390 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6391 if (INT16_TYPE)
6392 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6393 if (INT32_TYPE)
6394 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6395 if (INT64_TYPE)
6396 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6397 if (UINT8_TYPE)
6398 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6399 if (UINT16_TYPE)
6400 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6401 if (UINT32_TYPE)
6402 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6403 if (UINT64_TYPE)
6404 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6405 if (INT_LEAST8_TYPE)
6406 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6407 if (INT_LEAST16_TYPE)
6408 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6409 if (INT_LEAST32_TYPE)
6410 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6411 if (INT_LEAST64_TYPE)
6412 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6413 if (UINT_LEAST8_TYPE)
6414 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6415 if (UINT_LEAST16_TYPE)
6416 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6417 if (UINT_LEAST32_TYPE)
6418 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6419 if (UINT_LEAST64_TYPE)
6420 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6421 if (INT_FAST8_TYPE)
6422 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6423 if (INT_FAST16_TYPE)
6424 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6425 if (INT_FAST32_TYPE)
6426 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6427 if (INT_FAST64_TYPE)
6428 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6429 if (UINT_FAST8_TYPE)
6430 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6431 if (UINT_FAST16_TYPE)
6432 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6433 if (UINT_FAST32_TYPE)
6434 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6435 if (UINT_FAST64_TYPE)
6436 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6437 if (INTPTR_TYPE)
6438 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6439 if (UINTPTR_TYPE)
6440 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6443 static void
6444 c_init_attributes (void)
6446 /* Fill in the built_in_attributes array. */
6447 #define DEF_ATTR_NULL_TREE(ENUM) \
6448 built_in_attributes[(int) ENUM] = NULL_TREE;
6449 #define DEF_ATTR_INT(ENUM, VALUE) \
6450 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6451 #define DEF_ATTR_STRING(ENUM, VALUE) \
6452 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6453 #define DEF_ATTR_IDENT(ENUM, STRING) \
6454 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6455 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6456 built_in_attributes[(int) ENUM] \
6457 = tree_cons (built_in_attributes[(int) PURPOSE], \
6458 built_in_attributes[(int) VALUE], \
6459 built_in_attributes[(int) CHAIN]);
6460 #include "builtin-attrs.def"
6461 #undef DEF_ATTR_NULL_TREE
6462 #undef DEF_ATTR_INT
6463 #undef DEF_ATTR_IDENT
6464 #undef DEF_ATTR_TREE_LIST
6467 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6468 identifier as an argument, so the front end shouldn't look it up. */
6470 bool
6471 attribute_takes_identifier_p (const_tree attr_id)
6473 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6474 if (spec == NULL)
6475 /* Unknown attribute that we'll end up ignoring, return true so we
6476 don't complain about an identifier argument. */
6477 return true;
6478 else if (!strcmp ("mode", spec->name)
6479 || !strcmp ("format", spec->name)
6480 || !strcmp ("cleanup", spec->name))
6481 return true;
6482 else
6483 return targetm.attribute_takes_identifier_p (attr_id);
6486 /* Attribute handlers common to C front ends. */
6488 /* Handle a "packed" attribute; arguments as in
6489 struct attribute_spec.handler. */
6491 static tree
6492 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6493 int flags, bool *no_add_attrs)
6495 if (TYPE_P (*node))
6497 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6498 *node = build_variant_type_copy (*node);
6499 TYPE_PACKED (*node) = 1;
6501 else if (TREE_CODE (*node) == FIELD_DECL)
6503 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6504 /* Still pack bitfields. */
6505 && ! DECL_INITIAL (*node))
6506 warning (OPT_Wattributes,
6507 "%qE attribute ignored for field of type %qT",
6508 name, TREE_TYPE (*node));
6509 else
6510 DECL_PACKED (*node) = 1;
6512 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6513 used for DECL_REGISTER. It wouldn't mean anything anyway.
6514 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6515 that changes what the typedef is typing. */
6516 else
6518 warning (OPT_Wattributes, "%qE attribute ignored", name);
6519 *no_add_attrs = true;
6522 return NULL_TREE;
6525 /* Handle a "nocommon" attribute; arguments as in
6526 struct attribute_spec.handler. */
6528 static tree
6529 handle_nocommon_attribute (tree *node, tree name,
6530 tree ARG_UNUSED (args),
6531 int ARG_UNUSED (flags), bool *no_add_attrs)
6533 if (TREE_CODE (*node) == VAR_DECL)
6534 DECL_COMMON (*node) = 0;
6535 else
6537 warning (OPT_Wattributes, "%qE attribute ignored", name);
6538 *no_add_attrs = true;
6541 return NULL_TREE;
6544 /* Handle a "common" attribute; arguments as in
6545 struct attribute_spec.handler. */
6547 static tree
6548 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6549 int ARG_UNUSED (flags), bool *no_add_attrs)
6551 if (TREE_CODE (*node) == VAR_DECL)
6552 DECL_COMMON (*node) = 1;
6553 else
6555 warning (OPT_Wattributes, "%qE attribute ignored", name);
6556 *no_add_attrs = true;
6559 return NULL_TREE;
6562 /* Handle a "noreturn" attribute; arguments as in
6563 struct attribute_spec.handler. */
6565 static tree
6566 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6567 int ARG_UNUSED (flags), bool *no_add_attrs)
6569 tree type = TREE_TYPE (*node);
6571 /* See FIXME comment in c_common_attribute_table. */
6572 if (TREE_CODE (*node) == FUNCTION_DECL
6573 || objc_method_decl (TREE_CODE (*node)))
6574 TREE_THIS_VOLATILE (*node) = 1;
6575 else if (TREE_CODE (type) == POINTER_TYPE
6576 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6577 TREE_TYPE (*node)
6578 = build_pointer_type
6579 (build_type_variant (TREE_TYPE (type),
6580 TYPE_READONLY (TREE_TYPE (type)), 1));
6581 else
6583 warning (OPT_Wattributes, "%qE attribute ignored", name);
6584 *no_add_attrs = true;
6587 return NULL_TREE;
6590 /* Handle a "hot" and attribute; arguments as in
6591 struct attribute_spec.handler. */
6593 static tree
6594 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6595 int ARG_UNUSED (flags), bool *no_add_attrs)
6597 if (TREE_CODE (*node) == FUNCTION_DECL
6598 || TREE_CODE (*node) == LABEL_DECL)
6600 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6602 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6603 "with attribute %qs", name, "cold");
6604 *no_add_attrs = true;
6606 /* Most of the rest of the hot processing is done later with
6607 lookup_attribute. */
6609 else
6611 warning (OPT_Wattributes, "%qE attribute ignored", name);
6612 *no_add_attrs = true;
6615 return NULL_TREE;
6618 /* Handle a "cold" and attribute; arguments as in
6619 struct attribute_spec.handler. */
6621 static tree
6622 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6623 int ARG_UNUSED (flags), bool *no_add_attrs)
6625 if (TREE_CODE (*node) == FUNCTION_DECL
6626 || TREE_CODE (*node) == LABEL_DECL)
6628 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6630 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6631 "with attribute %qs", name, "hot");
6632 *no_add_attrs = true;
6634 /* Most of the rest of the cold processing is done later with
6635 lookup_attribute. */
6637 else
6639 warning (OPT_Wattributes, "%qE attribute ignored", name);
6640 *no_add_attrs = true;
6643 return NULL_TREE;
6646 /* Handle a "no_sanitize_address" attribute; arguments as in
6647 struct attribute_spec.handler. */
6649 static tree
6650 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6651 bool *no_add_attrs)
6653 if (TREE_CODE (*node) != FUNCTION_DECL)
6655 warning (OPT_Wattributes, "%qE attribute ignored", name);
6656 *no_add_attrs = true;
6659 return NULL_TREE;
6662 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6663 struct attribute_spec.handler. */
6665 static tree
6666 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6667 bool *no_add_attrs)
6669 if (TREE_CODE (*node) != FUNCTION_DECL)
6670 warning (OPT_Wattributes, "%qE attribute ignored", name);
6671 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6672 DECL_ATTRIBUTES (*node)
6673 = tree_cons (get_identifier ("no_sanitize_address"),
6674 NULL_TREE, DECL_ATTRIBUTES (*node));
6675 *no_add_attrs = true;
6676 return NULL_TREE;
6679 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6680 struct attribute_spec.handler. */
6682 static tree
6683 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6684 bool *no_add_attrs)
6686 if (TREE_CODE (*node) != FUNCTION_DECL)
6688 warning (OPT_Wattributes, "%qE attribute ignored", name);
6689 *no_add_attrs = true;
6692 return NULL_TREE;
6695 /* Handle a "noinline" attribute; arguments as in
6696 struct attribute_spec.handler. */
6698 static tree
6699 handle_noinline_attribute (tree *node, tree name,
6700 tree ARG_UNUSED (args),
6701 int ARG_UNUSED (flags), bool *no_add_attrs)
6703 if (TREE_CODE (*node) == FUNCTION_DECL)
6705 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6707 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6708 "with attribute %qs", name, "always_inline");
6709 *no_add_attrs = true;
6711 else
6712 DECL_UNINLINABLE (*node) = 1;
6714 else
6716 warning (OPT_Wattributes, "%qE attribute ignored", name);
6717 *no_add_attrs = true;
6720 return NULL_TREE;
6723 /* Handle a "noclone" attribute; arguments as in
6724 struct attribute_spec.handler. */
6726 static tree
6727 handle_noclone_attribute (tree *node, tree name,
6728 tree ARG_UNUSED (args),
6729 int ARG_UNUSED (flags), bool *no_add_attrs)
6731 if (TREE_CODE (*node) != FUNCTION_DECL)
6733 warning (OPT_Wattributes, "%qE attribute ignored", name);
6734 *no_add_attrs = true;
6737 return NULL_TREE;
6740 /* Handle a "always_inline" attribute; arguments as in
6741 struct attribute_spec.handler. */
6743 static tree
6744 handle_always_inline_attribute (tree *node, tree name,
6745 tree ARG_UNUSED (args),
6746 int ARG_UNUSED (flags),
6747 bool *no_add_attrs)
6749 if (TREE_CODE (*node) == FUNCTION_DECL)
6751 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6753 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6754 "with %qs attribute", name, "noinline");
6755 *no_add_attrs = true;
6757 else
6758 /* Set the attribute and mark it for disregarding inline
6759 limits. */
6760 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6762 else
6764 warning (OPT_Wattributes, "%qE attribute ignored", name);
6765 *no_add_attrs = true;
6768 return NULL_TREE;
6771 /* Handle a "gnu_inline" attribute; arguments as in
6772 struct attribute_spec.handler. */
6774 static tree
6775 handle_gnu_inline_attribute (tree *node, tree name,
6776 tree ARG_UNUSED (args),
6777 int ARG_UNUSED (flags),
6778 bool *no_add_attrs)
6780 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6782 /* Do nothing else, just set the attribute. We'll get at
6783 it later with lookup_attribute. */
6785 else
6787 warning (OPT_Wattributes, "%qE attribute ignored", name);
6788 *no_add_attrs = true;
6791 return NULL_TREE;
6794 /* Handle a "leaf" attribute; arguments as in
6795 struct attribute_spec.handler. */
6797 static tree
6798 handle_leaf_attribute (tree *node, tree name,
6799 tree ARG_UNUSED (args),
6800 int ARG_UNUSED (flags), bool *no_add_attrs)
6802 if (TREE_CODE (*node) != FUNCTION_DECL)
6804 warning (OPT_Wattributes, "%qE attribute ignored", name);
6805 *no_add_attrs = true;
6807 if (!TREE_PUBLIC (*node))
6809 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6810 *no_add_attrs = true;
6813 return NULL_TREE;
6816 /* Handle an "artificial" attribute; arguments as in
6817 struct attribute_spec.handler. */
6819 static tree
6820 handle_artificial_attribute (tree *node, tree name,
6821 tree ARG_UNUSED (args),
6822 int ARG_UNUSED (flags),
6823 bool *no_add_attrs)
6825 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6827 /* Do nothing else, just set the attribute. We'll get at
6828 it later with lookup_attribute. */
6830 else
6832 warning (OPT_Wattributes, "%qE attribute ignored", name);
6833 *no_add_attrs = true;
6836 return NULL_TREE;
6839 /* Handle a "flatten" attribute; arguments as in
6840 struct attribute_spec.handler. */
6842 static tree
6843 handle_flatten_attribute (tree *node, tree name,
6844 tree args ATTRIBUTE_UNUSED,
6845 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6847 if (TREE_CODE (*node) == FUNCTION_DECL)
6848 /* Do nothing else, just set the attribute. We'll get at
6849 it later with lookup_attribute. */
6851 else
6853 warning (OPT_Wattributes, "%qE attribute ignored", name);
6854 *no_add_attrs = true;
6857 return NULL_TREE;
6860 /* Handle a "warning" or "error" attribute; arguments as in
6861 struct attribute_spec.handler. */
6863 static tree
6864 handle_error_attribute (tree *node, tree name, tree args,
6865 int ARG_UNUSED (flags), bool *no_add_attrs)
6867 if (TREE_CODE (*node) == FUNCTION_DECL
6868 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6869 /* Do nothing else, just set the attribute. We'll get at
6870 it later with lookup_attribute. */
6872 else
6874 warning (OPT_Wattributes, "%qE attribute ignored", name);
6875 *no_add_attrs = true;
6878 return NULL_TREE;
6881 /* Handle a "used" attribute; arguments as in
6882 struct attribute_spec.handler. */
6884 static tree
6885 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6886 int ARG_UNUSED (flags), bool *no_add_attrs)
6888 tree node = *pnode;
6890 if (TREE_CODE (node) == FUNCTION_DECL
6891 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6892 || (TREE_CODE (node) == TYPE_DECL))
6894 TREE_USED (node) = 1;
6895 DECL_PRESERVE_P (node) = 1;
6896 if (TREE_CODE (node) == VAR_DECL)
6897 DECL_READ_P (node) = 1;
6899 else
6901 warning (OPT_Wattributes, "%qE attribute ignored", name);
6902 *no_add_attrs = true;
6905 return NULL_TREE;
6908 /* Handle a "unused" attribute; arguments as in
6909 struct attribute_spec.handler. */
6911 static tree
6912 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6913 int flags, bool *no_add_attrs)
6915 if (DECL_P (*node))
6917 tree decl = *node;
6919 if (TREE_CODE (decl) == PARM_DECL
6920 || TREE_CODE (decl) == VAR_DECL
6921 || TREE_CODE (decl) == FUNCTION_DECL
6922 || TREE_CODE (decl) == LABEL_DECL
6923 || TREE_CODE (decl) == TYPE_DECL)
6925 TREE_USED (decl) = 1;
6926 if (TREE_CODE (decl) == VAR_DECL
6927 || TREE_CODE (decl) == PARM_DECL)
6928 DECL_READ_P (decl) = 1;
6930 else
6932 warning (OPT_Wattributes, "%qE attribute ignored", name);
6933 *no_add_attrs = true;
6936 else
6938 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6939 *node = build_variant_type_copy (*node);
6940 TREE_USED (*node) = 1;
6943 return NULL_TREE;
6946 /* Handle a "externally_visible" attribute; arguments as in
6947 struct attribute_spec.handler. */
6949 static tree
6950 handle_externally_visible_attribute (tree *pnode, tree name,
6951 tree ARG_UNUSED (args),
6952 int ARG_UNUSED (flags),
6953 bool *no_add_attrs)
6955 tree node = *pnode;
6957 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6959 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6960 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6962 warning (OPT_Wattributes,
6963 "%qE attribute have effect only on public objects", name);
6964 *no_add_attrs = true;
6967 else
6969 warning (OPT_Wattributes, "%qE attribute ignored", name);
6970 *no_add_attrs = true;
6973 return NULL_TREE;
6976 /* Handle a "const" attribute; arguments as in
6977 struct attribute_spec.handler. */
6979 static tree
6980 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6981 int ARG_UNUSED (flags), bool *no_add_attrs)
6983 tree type = TREE_TYPE (*node);
6985 /* See FIXME comment on noreturn in c_common_attribute_table. */
6986 if (TREE_CODE (*node) == FUNCTION_DECL)
6987 TREE_READONLY (*node) = 1;
6988 else if (TREE_CODE (type) == POINTER_TYPE
6989 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6990 TREE_TYPE (*node)
6991 = build_pointer_type
6992 (build_type_variant (TREE_TYPE (type), 1,
6993 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6994 else
6996 warning (OPT_Wattributes, "%qE attribute ignored", name);
6997 *no_add_attrs = true;
7000 return NULL_TREE;
7003 /* Handle a "transparent_union" attribute; arguments as in
7004 struct attribute_spec.handler. */
7006 static tree
7007 handle_transparent_union_attribute (tree *node, tree name,
7008 tree ARG_UNUSED (args), int flags,
7009 bool *no_add_attrs)
7011 tree type;
7013 *no_add_attrs = true;
7016 if (TREE_CODE (*node) == TYPE_DECL
7017 && ! (flags & ATTR_FLAG_CXX11))
7018 node = &TREE_TYPE (*node);
7019 type = *node;
7021 if (TREE_CODE (type) == UNION_TYPE)
7023 /* Make sure that the first field will work for a transparent union.
7024 If the type isn't complete yet, leave the check to the code in
7025 finish_struct. */
7026 if (TYPE_SIZE (type))
7028 tree first = first_field (type);
7029 if (first == NULL_TREE
7030 || DECL_ARTIFICIAL (first)
7031 || TYPE_MODE (type) != DECL_MODE (first))
7032 goto ignored;
7035 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7037 /* If the type isn't complete yet, setting the flag
7038 on a variant wouldn't ever be checked. */
7039 if (!TYPE_SIZE (type))
7040 goto ignored;
7042 /* build_duplicate_type doesn't work for C++. */
7043 if (c_dialect_cxx ())
7044 goto ignored;
7046 /* A type variant isn't good enough, since we don't a cast
7047 to such a type removed as a no-op. */
7048 *node = type = build_duplicate_type (type);
7051 TYPE_TRANSPARENT_AGGR (type) = 1;
7052 return NULL_TREE;
7055 ignored:
7056 warning (OPT_Wattributes, "%qE attribute ignored", name);
7057 return NULL_TREE;
7060 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7061 get the requested priority for a constructor or destructor,
7062 possibly issuing diagnostics for invalid or reserved
7063 priorities. */
7065 static priority_type
7066 get_priority (tree args, bool is_destructor)
7068 HOST_WIDE_INT pri;
7069 tree arg;
7071 if (!args)
7072 return DEFAULT_INIT_PRIORITY;
7074 if (!SUPPORTS_INIT_PRIORITY)
7076 if (is_destructor)
7077 error ("destructor priorities are not supported");
7078 else
7079 error ("constructor priorities are not supported");
7080 return DEFAULT_INIT_PRIORITY;
7083 arg = TREE_VALUE (args);
7084 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7085 goto invalid;
7086 if (arg == error_mark_node)
7087 return DEFAULT_INIT_PRIORITY;
7088 arg = default_conversion (arg);
7089 if (!tree_fits_shwi_p (arg)
7090 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7091 goto invalid;
7093 pri = tree_to_shwi (arg);
7094 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7095 goto invalid;
7097 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7099 if (is_destructor)
7100 warning (0,
7101 "destructor priorities from 0 to %d are reserved "
7102 "for the implementation",
7103 MAX_RESERVED_INIT_PRIORITY);
7104 else
7105 warning (0,
7106 "constructor priorities from 0 to %d are reserved "
7107 "for the implementation",
7108 MAX_RESERVED_INIT_PRIORITY);
7110 return pri;
7112 invalid:
7113 if (is_destructor)
7114 error ("destructor priorities must be integers from 0 to %d inclusive",
7115 MAX_INIT_PRIORITY);
7116 else
7117 error ("constructor priorities must be integers from 0 to %d inclusive",
7118 MAX_INIT_PRIORITY);
7119 return DEFAULT_INIT_PRIORITY;
7122 /* Handle a "constructor" attribute; arguments as in
7123 struct attribute_spec.handler. */
7125 static tree
7126 handle_constructor_attribute (tree *node, tree name, tree args,
7127 int ARG_UNUSED (flags),
7128 bool *no_add_attrs)
7130 tree decl = *node;
7131 tree type = TREE_TYPE (decl);
7133 if (TREE_CODE (decl) == FUNCTION_DECL
7134 && TREE_CODE (type) == FUNCTION_TYPE
7135 && decl_function_context (decl) == 0)
7137 priority_type priority;
7138 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7139 priority = get_priority (args, /*is_destructor=*/false);
7140 SET_DECL_INIT_PRIORITY (decl, priority);
7141 TREE_USED (decl) = 1;
7143 else
7145 warning (OPT_Wattributes, "%qE attribute ignored", name);
7146 *no_add_attrs = true;
7149 return NULL_TREE;
7152 /* Handle a "destructor" attribute; arguments as in
7153 struct attribute_spec.handler. */
7155 static tree
7156 handle_destructor_attribute (tree *node, tree name, tree args,
7157 int ARG_UNUSED (flags),
7158 bool *no_add_attrs)
7160 tree decl = *node;
7161 tree type = TREE_TYPE (decl);
7163 if (TREE_CODE (decl) == FUNCTION_DECL
7164 && TREE_CODE (type) == FUNCTION_TYPE
7165 && decl_function_context (decl) == 0)
7167 priority_type priority;
7168 DECL_STATIC_DESTRUCTOR (decl) = 1;
7169 priority = get_priority (args, /*is_destructor=*/true);
7170 SET_DECL_FINI_PRIORITY (decl, priority);
7171 TREE_USED (decl) = 1;
7173 else
7175 warning (OPT_Wattributes, "%qE attribute ignored", name);
7176 *no_add_attrs = true;
7179 return NULL_TREE;
7182 /* Nonzero if the mode is a valid vector mode for this architecture.
7183 This returns nonzero even if there is no hardware support for the
7184 vector mode, but we can emulate with narrower modes. */
7186 static int
7187 vector_mode_valid_p (enum machine_mode mode)
7189 enum mode_class mclass = GET_MODE_CLASS (mode);
7190 enum machine_mode innermode;
7192 /* Doh! What's going on? */
7193 if (mclass != MODE_VECTOR_INT
7194 && mclass != MODE_VECTOR_FLOAT
7195 && mclass != MODE_VECTOR_FRACT
7196 && mclass != MODE_VECTOR_UFRACT
7197 && mclass != MODE_VECTOR_ACCUM
7198 && mclass != MODE_VECTOR_UACCUM)
7199 return 0;
7201 /* Hardware support. Woo hoo! */
7202 if (targetm.vector_mode_supported_p (mode))
7203 return 1;
7205 innermode = GET_MODE_INNER (mode);
7207 /* We should probably return 1 if requesting V4DI and we have no DI,
7208 but we have V2DI, but this is probably very unlikely. */
7210 /* If we have support for the inner mode, we can safely emulate it.
7211 We may not have V2DI, but me can emulate with a pair of DIs. */
7212 return targetm.scalar_mode_supported_p (innermode);
7216 /* Handle a "mode" attribute; arguments as in
7217 struct attribute_spec.handler. */
7219 static tree
7220 handle_mode_attribute (tree *node, tree name, tree args,
7221 int ARG_UNUSED (flags), bool *no_add_attrs)
7223 tree type = *node;
7224 tree ident = TREE_VALUE (args);
7226 *no_add_attrs = true;
7228 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7229 warning (OPT_Wattributes, "%qE attribute ignored", name);
7230 else
7232 int j;
7233 const char *p = IDENTIFIER_POINTER (ident);
7234 int len = strlen (p);
7235 enum machine_mode mode = VOIDmode;
7236 tree typefm;
7237 bool valid_mode;
7239 if (len > 4 && p[0] == '_' && p[1] == '_'
7240 && p[len - 1] == '_' && p[len - 2] == '_')
7242 char *newp = (char *) alloca (len - 1);
7244 strcpy (newp, &p[2]);
7245 newp[len - 4] = '\0';
7246 p = newp;
7249 /* Change this type to have a type with the specified mode.
7250 First check for the special modes. */
7251 if (!strcmp (p, "byte"))
7252 mode = byte_mode;
7253 else if (!strcmp (p, "word"))
7254 mode = word_mode;
7255 else if (!strcmp (p, "pointer"))
7256 mode = ptr_mode;
7257 else if (!strcmp (p, "libgcc_cmp_return"))
7258 mode = targetm.libgcc_cmp_return_mode ();
7259 else if (!strcmp (p, "libgcc_shift_count"))
7260 mode = targetm.libgcc_shift_count_mode ();
7261 else if (!strcmp (p, "unwind_word"))
7262 mode = targetm.unwind_word_mode ();
7263 else
7264 for (j = 0; j < NUM_MACHINE_MODES; j++)
7265 if (!strcmp (p, GET_MODE_NAME (j)))
7267 mode = (enum machine_mode) j;
7268 break;
7271 if (mode == VOIDmode)
7273 error ("unknown machine mode %qE", ident);
7274 return NULL_TREE;
7277 valid_mode = false;
7278 switch (GET_MODE_CLASS (mode))
7280 case MODE_INT:
7281 case MODE_PARTIAL_INT:
7282 case MODE_FLOAT:
7283 case MODE_DECIMAL_FLOAT:
7284 case MODE_FRACT:
7285 case MODE_UFRACT:
7286 case MODE_ACCUM:
7287 case MODE_UACCUM:
7288 valid_mode = targetm.scalar_mode_supported_p (mode);
7289 break;
7291 case MODE_COMPLEX_INT:
7292 case MODE_COMPLEX_FLOAT:
7293 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7294 break;
7296 case MODE_VECTOR_INT:
7297 case MODE_VECTOR_FLOAT:
7298 case MODE_VECTOR_FRACT:
7299 case MODE_VECTOR_UFRACT:
7300 case MODE_VECTOR_ACCUM:
7301 case MODE_VECTOR_UACCUM:
7302 warning (OPT_Wattributes, "specifying vector types with "
7303 "__attribute__ ((mode)) is deprecated");
7304 warning (OPT_Wattributes,
7305 "use __attribute__ ((vector_size)) instead");
7306 valid_mode = vector_mode_valid_p (mode);
7307 break;
7309 default:
7310 break;
7312 if (!valid_mode)
7314 error ("unable to emulate %qs", p);
7315 return NULL_TREE;
7318 if (POINTER_TYPE_P (type))
7320 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7321 tree (*fn)(tree, enum machine_mode, bool);
7323 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7325 error ("invalid pointer mode %qs", p);
7326 return NULL_TREE;
7329 if (TREE_CODE (type) == POINTER_TYPE)
7330 fn = build_pointer_type_for_mode;
7331 else
7332 fn = build_reference_type_for_mode;
7333 typefm = fn (TREE_TYPE (type), mode, false);
7335 else
7337 /* For fixed-point modes, we need to test if the signness of type
7338 and the machine mode are consistent. */
7339 if (ALL_FIXED_POINT_MODE_P (mode)
7340 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7342 error ("signedness of type and machine mode %qs don%'t match", p);
7343 return NULL_TREE;
7345 /* For fixed-point modes, we need to pass saturating info. */
7346 typefm = lang_hooks.types.type_for_mode (mode,
7347 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7348 : TYPE_UNSIGNED (type));
7351 if (typefm == NULL_TREE)
7353 error ("no data type for mode %qs", p);
7354 return NULL_TREE;
7356 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7358 /* For enumeral types, copy the precision from the integer
7359 type returned above. If not an INTEGER_TYPE, we can't use
7360 this mode for this type. */
7361 if (TREE_CODE (typefm) != INTEGER_TYPE)
7363 error ("cannot use mode %qs for enumeral types", p);
7364 return NULL_TREE;
7367 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7369 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7370 typefm = type;
7372 else
7374 /* We cannot build a type variant, as there's code that assumes
7375 that TYPE_MAIN_VARIANT has the same mode. This includes the
7376 debug generators. Instead, create a subrange type. This
7377 results in all of the enumeral values being emitted only once
7378 in the original, and the subtype gets them by reference. */
7379 if (TYPE_UNSIGNED (type))
7380 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7381 else
7382 typefm = make_signed_type (TYPE_PRECISION (typefm));
7383 TREE_TYPE (typefm) = type;
7386 else if (VECTOR_MODE_P (mode)
7387 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7388 : TREE_CODE (type) != TREE_CODE (typefm))
7390 error ("mode %qs applied to inappropriate type", p);
7391 return NULL_TREE;
7394 *node = typefm;
7397 return NULL_TREE;
7400 /* Handle a "section" attribute; arguments as in
7401 struct attribute_spec.handler. */
7403 static tree
7404 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7405 int ARG_UNUSED (flags), bool *no_add_attrs)
7407 tree decl = *node;
7409 if (targetm_common.have_named_sections)
7411 user_defined_section_attribute = true;
7413 if ((TREE_CODE (decl) == FUNCTION_DECL
7414 || TREE_CODE (decl) == VAR_DECL)
7415 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7417 if (TREE_CODE (decl) == VAR_DECL
7418 && current_function_decl != NULL_TREE
7419 && !TREE_STATIC (decl))
7421 error_at (DECL_SOURCE_LOCATION (decl),
7422 "section attribute cannot be specified for "
7423 "local variables");
7424 *no_add_attrs = true;
7427 /* The decl may have already been given a section attribute
7428 from a previous declaration. Ensure they match. */
7429 else if (DECL_SECTION_NAME (decl) != NULL
7430 && strcmp (DECL_SECTION_NAME (decl),
7431 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7433 error ("section of %q+D conflicts with previous declaration",
7434 *node);
7435 *no_add_attrs = true;
7437 else if (TREE_CODE (decl) == VAR_DECL
7438 && !targetm.have_tls && targetm.emutls.tmpl_section
7439 && DECL_THREAD_LOCAL_P (decl))
7441 error ("section of %q+D cannot be overridden", *node);
7442 *no_add_attrs = true;
7444 else
7445 set_decl_section_name (decl,
7446 TREE_STRING_POINTER (TREE_VALUE (args)));
7448 else
7450 error ("section attribute not allowed for %q+D", *node);
7451 *no_add_attrs = true;
7454 else
7456 error_at (DECL_SOURCE_LOCATION (*node),
7457 "section attributes are not supported for this target");
7458 *no_add_attrs = true;
7461 return NULL_TREE;
7464 /* Check whether ALIGN is a valid user-specified alignment. If so,
7465 return its base-2 log; if not, output an error and return -1. If
7466 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7467 no error. */
7469 check_user_alignment (const_tree align, bool allow_zero)
7471 int i;
7473 if (error_operand_p (align))
7474 return -1;
7475 if (TREE_CODE (align) != INTEGER_CST
7476 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7478 error ("requested alignment is not an integer constant");
7479 return -1;
7481 else if (allow_zero && integer_zerop (align))
7482 return -1;
7483 else if (tree_int_cst_sgn (align) == -1
7484 || (i = tree_log2 (align)) == -1)
7486 error ("requested alignment is not a positive power of 2");
7487 return -1;
7489 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7491 error ("requested alignment is too large");
7492 return -1;
7494 return i;
7498 If in c++-11, check if the c++-11 alignment constraint with respect
7499 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7500 c++-11 mode, does nothing.
7502 [dcl.align]2/ says:
7504 [* if the constant expression evaluates to a fundamental alignment,
7505 the alignment requirement of the declared entity shall be the
7506 specified fundamental alignment.
7508 * if the constant expression evaluates to an extended alignment
7509 and the implementation supports that alignment in the context
7510 of the declaration, the alignment of the declared entity shall
7511 be that alignment
7513 * if the constant expression evaluates to an extended alignment
7514 and the implementation does not support that alignment in the
7515 context of the declaration, the program is ill-formed]. */
7517 static bool
7518 check_cxx_fundamental_alignment_constraints (tree node,
7519 unsigned align_log,
7520 int flags)
7522 bool alignment_too_large_p = false;
7523 unsigned requested_alignment = 1U << align_log;
7524 unsigned max_align = 0;
7526 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7527 || (node == NULL_TREE || node == error_mark_node))
7528 return true;
7530 if (cxx_fundamental_alignment_p (requested_alignment))
7531 return true;
7533 if (DECL_P (node))
7535 if (TREE_STATIC (node))
7537 /* For file scope variables and static members, the target
7538 supports alignments that are at most
7539 MAX_OFILE_ALIGNMENT. */
7540 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7541 alignment_too_large_p = true;
7543 else
7545 #ifdef BIGGEST_FIELD_ALIGNMENT
7546 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7547 #else
7548 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7549 #endif
7550 /* For non-static members, the target supports either
7551 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7552 if it is defined or BIGGEST_ALIGNMENT. */
7553 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7554 if (TREE_CODE (node) == FIELD_DECL
7555 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7556 alignment_too_large_p = true;
7557 #undef MAX_TARGET_FIELD_ALIGNMENT
7558 /* For stack variables, the target supports at most
7559 MAX_STACK_ALIGNMENT. */
7560 else if (decl_function_context (node) != NULL
7561 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7562 alignment_too_large_p = true;
7565 else if (TYPE_P (node))
7567 /* Let's be liberal for types. */
7568 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7569 alignment_too_large_p = true;
7572 if (alignment_too_large_p)
7573 pedwarn (input_location, OPT_Wattributes,
7574 "requested alignment %d is larger than %d",
7575 requested_alignment, max_align);
7577 return !alignment_too_large_p;
7580 /* Handle a "aligned" attribute; arguments as in
7581 struct attribute_spec.handler. */
7583 static tree
7584 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7585 int flags, bool *no_add_attrs)
7587 tree decl = NULL_TREE;
7588 tree *type = NULL;
7589 int is_type = 0;
7590 tree align_expr;
7591 int i;
7593 if (args)
7595 align_expr = TREE_VALUE (args);
7596 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7597 && TREE_CODE (align_expr) != FUNCTION_DECL)
7598 align_expr = default_conversion (align_expr);
7600 else
7601 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7603 if (DECL_P (*node))
7605 decl = *node;
7606 type = &TREE_TYPE (decl);
7607 is_type = TREE_CODE (*node) == TYPE_DECL;
7609 else if (TYPE_P (*node))
7610 type = node, is_type = 1;
7612 if ((i = check_user_alignment (align_expr, false)) == -1
7613 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7614 *no_add_attrs = true;
7615 else if (is_type)
7617 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7618 /* OK, modify the type in place. */;
7619 /* If we have a TYPE_DECL, then copy the type, so that we
7620 don't accidentally modify a builtin type. See pushdecl. */
7621 else if (decl && TREE_TYPE (decl) != error_mark_node
7622 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7624 tree tt = TREE_TYPE (decl);
7625 *type = build_variant_type_copy (*type);
7626 DECL_ORIGINAL_TYPE (decl) = tt;
7627 TYPE_NAME (*type) = decl;
7628 TREE_USED (*type) = TREE_USED (decl);
7629 TREE_TYPE (decl) = *type;
7631 else
7632 *type = build_variant_type_copy (*type);
7634 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7635 TYPE_USER_ALIGN (*type) = 1;
7637 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7638 && TREE_CODE (decl) != FIELD_DECL)
7640 error ("alignment may not be specified for %q+D", decl);
7641 *no_add_attrs = true;
7643 else if (DECL_USER_ALIGN (decl)
7644 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7645 /* C++-11 [dcl.align/4]:
7647 When multiple alignment-specifiers are specified for an
7648 entity, the alignment requirement shall be set to the
7649 strictest specified alignment.
7651 This formally comes from the c++11 specification but we are
7652 doing it for the GNU attribute syntax as well. */
7653 *no_add_attrs = true;
7654 else if (TREE_CODE (decl) == FUNCTION_DECL
7655 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7657 if (DECL_USER_ALIGN (decl))
7658 error ("alignment for %q+D was previously specified as %d "
7659 "and may not be decreased", decl,
7660 DECL_ALIGN (decl) / BITS_PER_UNIT);
7661 else
7662 error ("alignment for %q+D must be at least %d", decl,
7663 DECL_ALIGN (decl) / BITS_PER_UNIT);
7664 *no_add_attrs = true;
7666 else
7668 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7669 DECL_USER_ALIGN (decl) = 1;
7672 return NULL_TREE;
7675 /* Handle a "weak" attribute; arguments as in
7676 struct attribute_spec.handler. */
7678 static tree
7679 handle_weak_attribute (tree *node, tree name,
7680 tree ARG_UNUSED (args),
7681 int ARG_UNUSED (flags),
7682 bool * ARG_UNUSED (no_add_attrs))
7684 if (TREE_CODE (*node) == FUNCTION_DECL
7685 && DECL_DECLARED_INLINE_P (*node))
7687 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7688 *no_add_attrs = true;
7690 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7692 error ("indirect function %q+D cannot be declared weak", *node);
7693 *no_add_attrs = true;
7694 return NULL_TREE;
7696 else if (TREE_CODE (*node) == FUNCTION_DECL
7697 || TREE_CODE (*node) == VAR_DECL)
7698 declare_weak (*node);
7699 else
7700 warning (OPT_Wattributes, "%qE attribute ignored", name);
7702 return NULL_TREE;
7705 /* Handle an "alias" or "ifunc" attribute; arguments as in
7706 struct attribute_spec.handler, except that IS_ALIAS tells us
7707 whether this is an alias as opposed to ifunc attribute. */
7709 static tree
7710 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7711 bool *no_add_attrs)
7713 tree decl = *node;
7715 if (TREE_CODE (decl) != FUNCTION_DECL
7716 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7718 warning (OPT_Wattributes, "%qE attribute ignored", name);
7719 *no_add_attrs = true;
7721 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7722 || (TREE_CODE (decl) != FUNCTION_DECL
7723 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7724 /* A static variable declaration is always a tentative definition,
7725 but the alias is a non-tentative definition which overrides. */
7726 || (TREE_CODE (decl) != FUNCTION_DECL
7727 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7729 error ("%q+D defined both normally and as %qE attribute", decl, name);
7730 *no_add_attrs = true;
7731 return NULL_TREE;
7733 else if (!is_alias
7734 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7735 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7737 error ("weak %q+D cannot be defined %qE", decl, name);
7738 *no_add_attrs = true;
7739 return NULL_TREE;
7742 /* Note that the very first time we process a nested declaration,
7743 decl_function_context will not be set. Indeed, *would* never
7744 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7745 we do below. After such frobbery, pushdecl would set the context.
7746 In any case, this is never what we want. */
7747 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7749 tree id;
7751 id = TREE_VALUE (args);
7752 if (TREE_CODE (id) != STRING_CST)
7754 error ("attribute %qE argument not a string", name);
7755 *no_add_attrs = true;
7756 return NULL_TREE;
7758 id = get_identifier (TREE_STRING_POINTER (id));
7759 /* This counts as a use of the object pointed to. */
7760 TREE_USED (id) = 1;
7762 if (TREE_CODE (decl) == FUNCTION_DECL)
7763 DECL_INITIAL (decl) = error_mark_node;
7764 else
7765 TREE_STATIC (decl) = 1;
7767 if (!is_alias)
7768 /* ifuncs are also aliases, so set that attribute too. */
7769 DECL_ATTRIBUTES (decl)
7770 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7772 else
7774 warning (OPT_Wattributes, "%qE attribute ignored", name);
7775 *no_add_attrs = true;
7778 return NULL_TREE;
7781 /* Handle an "alias" or "ifunc" attribute; arguments as in
7782 struct attribute_spec.handler. */
7784 static tree
7785 handle_ifunc_attribute (tree *node, tree name, tree args,
7786 int ARG_UNUSED (flags), bool *no_add_attrs)
7788 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7791 /* Handle an "alias" or "ifunc" attribute; arguments as in
7792 struct attribute_spec.handler. */
7794 static tree
7795 handle_alias_attribute (tree *node, tree name, tree args,
7796 int ARG_UNUSED (flags), bool *no_add_attrs)
7798 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7801 /* Handle a "weakref" attribute; arguments as in struct
7802 attribute_spec.handler. */
7804 static tree
7805 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7806 int flags, bool *no_add_attrs)
7808 tree attr = NULL_TREE;
7810 /* We must ignore the attribute when it is associated with
7811 local-scoped decls, since attribute alias is ignored and many
7812 such symbols do not even have a DECL_WEAK field. */
7813 if (decl_function_context (*node)
7814 || current_function_decl
7815 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7817 warning (OPT_Wattributes, "%qE attribute ignored", name);
7818 *no_add_attrs = true;
7819 return NULL_TREE;
7822 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7824 error ("indirect function %q+D cannot be declared weakref", *node);
7825 *no_add_attrs = true;
7826 return NULL_TREE;
7829 /* The idea here is that `weakref("name")' mutates into `weakref,
7830 alias("name")', and weakref without arguments, in turn,
7831 implicitly adds weak. */
7833 if (args)
7835 attr = tree_cons (get_identifier ("alias"), args, attr);
7836 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7838 *no_add_attrs = true;
7840 decl_attributes (node, attr, flags);
7842 else
7844 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7845 error_at (DECL_SOURCE_LOCATION (*node),
7846 "weakref attribute must appear before alias attribute");
7848 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7849 and that isn't supported; and because it wants to add it to
7850 the list of weak decls, which isn't helpful. */
7851 DECL_WEAK (*node) = 1;
7854 return NULL_TREE;
7857 /* Handle an "visibility" attribute; arguments as in
7858 struct attribute_spec.handler. */
7860 static tree
7861 handle_visibility_attribute (tree *node, tree name, tree args,
7862 int ARG_UNUSED (flags),
7863 bool *ARG_UNUSED (no_add_attrs))
7865 tree decl = *node;
7866 tree id = TREE_VALUE (args);
7867 enum symbol_visibility vis;
7869 if (TYPE_P (*node))
7871 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7872 /* OK */;
7873 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7875 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7876 name);
7877 return NULL_TREE;
7879 else if (TYPE_FIELDS (*node))
7881 error ("%qE attribute ignored because %qT is already defined",
7882 name, *node);
7883 return NULL_TREE;
7886 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7888 warning (OPT_Wattributes, "%qE attribute ignored", name);
7889 return NULL_TREE;
7892 if (TREE_CODE (id) != STRING_CST)
7894 error ("visibility argument not a string");
7895 return NULL_TREE;
7898 /* If this is a type, set the visibility on the type decl. */
7899 if (TYPE_P (decl))
7901 decl = TYPE_NAME (decl);
7902 if (!decl)
7903 return NULL_TREE;
7904 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7906 warning (OPT_Wattributes, "%qE attribute ignored on types",
7907 name);
7908 return NULL_TREE;
7912 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7913 vis = VISIBILITY_DEFAULT;
7914 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7915 vis = VISIBILITY_INTERNAL;
7916 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7917 vis = VISIBILITY_HIDDEN;
7918 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7919 vis = VISIBILITY_PROTECTED;
7920 else
7922 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7923 vis = VISIBILITY_DEFAULT;
7926 if (DECL_VISIBILITY_SPECIFIED (decl)
7927 && vis != DECL_VISIBILITY (decl))
7929 tree attributes = (TYPE_P (*node)
7930 ? TYPE_ATTRIBUTES (*node)
7931 : DECL_ATTRIBUTES (decl));
7932 if (lookup_attribute ("visibility", attributes))
7933 error ("%qD redeclared with different visibility", decl);
7934 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7935 && lookup_attribute ("dllimport", attributes))
7936 error ("%qD was declared %qs which implies default visibility",
7937 decl, "dllimport");
7938 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7939 && lookup_attribute ("dllexport", attributes))
7940 error ("%qD was declared %qs which implies default visibility",
7941 decl, "dllexport");
7944 DECL_VISIBILITY (decl) = vis;
7945 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7947 /* Go ahead and attach the attribute to the node as well. This is needed
7948 so we can determine whether we have VISIBILITY_DEFAULT because the
7949 visibility was not specified, or because it was explicitly overridden
7950 from the containing scope. */
7952 return NULL_TREE;
7955 /* Determine the ELF symbol visibility for DECL, which is either a
7956 variable or a function. It is an error to use this function if a
7957 definition of DECL is not available in this translation unit.
7958 Returns true if the final visibility has been determined by this
7959 function; false if the caller is free to make additional
7960 modifications. */
7962 bool
7963 c_determine_visibility (tree decl)
7965 gcc_assert (TREE_CODE (decl) == VAR_DECL
7966 || TREE_CODE (decl) == FUNCTION_DECL);
7968 /* If the user explicitly specified the visibility with an
7969 attribute, honor that. DECL_VISIBILITY will have been set during
7970 the processing of the attribute. We check for an explicit
7971 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7972 to distinguish the use of an attribute from the use of a "#pragma
7973 GCC visibility push(...)"; in the latter case we still want other
7974 considerations to be able to overrule the #pragma. */
7975 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7976 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7977 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7978 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7979 return true;
7981 /* Set default visibility to whatever the user supplied with
7982 visibility_specified depending on #pragma GCC visibility. */
7983 if (!DECL_VISIBILITY_SPECIFIED (decl))
7985 if (visibility_options.inpragma
7986 || DECL_VISIBILITY (decl) != default_visibility)
7988 DECL_VISIBILITY (decl) = default_visibility;
7989 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7990 /* If visibility changed and DECL already has DECL_RTL, ensure
7991 symbol flags are updated. */
7992 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7993 || TREE_CODE (decl) == FUNCTION_DECL)
7994 && DECL_RTL_SET_P (decl))
7995 make_decl_rtl (decl);
7998 return false;
8001 /* Handle an "tls_model" attribute; arguments as in
8002 struct attribute_spec.handler. */
8004 static tree
8005 handle_tls_model_attribute (tree *node, tree name, tree args,
8006 int ARG_UNUSED (flags), bool *no_add_attrs)
8008 tree id;
8009 tree decl = *node;
8010 enum tls_model kind;
8012 *no_add_attrs = true;
8014 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8016 warning (OPT_Wattributes, "%qE attribute ignored", name);
8017 return NULL_TREE;
8020 kind = DECL_TLS_MODEL (decl);
8021 id = TREE_VALUE (args);
8022 if (TREE_CODE (id) != STRING_CST)
8024 error ("tls_model argument not a string");
8025 return NULL_TREE;
8028 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8029 kind = TLS_MODEL_LOCAL_EXEC;
8030 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8031 kind = TLS_MODEL_INITIAL_EXEC;
8032 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8033 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8034 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8035 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8036 else
8037 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8039 set_decl_tls_model (decl, kind);
8040 return NULL_TREE;
8043 /* Handle a "no_instrument_function" attribute; arguments as in
8044 struct attribute_spec.handler. */
8046 static tree
8047 handle_no_instrument_function_attribute (tree *node, tree name,
8048 tree ARG_UNUSED (args),
8049 int ARG_UNUSED (flags),
8050 bool *no_add_attrs)
8052 tree decl = *node;
8054 if (TREE_CODE (decl) != FUNCTION_DECL)
8056 error_at (DECL_SOURCE_LOCATION (decl),
8057 "%qE attribute applies only to functions", name);
8058 *no_add_attrs = true;
8060 else
8061 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8063 return NULL_TREE;
8066 /* Handle a "malloc" attribute; arguments as in
8067 struct attribute_spec.handler. */
8069 static tree
8070 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8071 int ARG_UNUSED (flags), bool *no_add_attrs)
8073 if (TREE_CODE (*node) == FUNCTION_DECL
8074 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8075 DECL_IS_MALLOC (*node) = 1;
8076 else
8078 warning (OPT_Wattributes, "%qE attribute ignored", name);
8079 *no_add_attrs = true;
8082 return NULL_TREE;
8085 /* Handle a "alloc_size" attribute; arguments as in
8086 struct attribute_spec.handler. */
8088 static tree
8089 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8090 int ARG_UNUSED (flags), bool *no_add_attrs)
8092 unsigned arg_count = type_num_arguments (*node);
8093 for (; args; args = TREE_CHAIN (args))
8095 tree position = TREE_VALUE (args);
8096 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8097 && TREE_CODE (position) != FUNCTION_DECL)
8098 position = default_conversion (position);
8100 if (!tree_fits_uhwi_p (position)
8101 || !arg_count
8102 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8104 warning (OPT_Wattributes,
8105 "alloc_size parameter outside range");
8106 *no_add_attrs = true;
8107 return NULL_TREE;
8110 return NULL_TREE;
8113 /* Handle a "alloc_align" attribute; arguments as in
8114 struct attribute_spec.handler. */
8116 static tree
8117 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8118 bool *no_add_attrs)
8120 unsigned arg_count = type_num_arguments (*node);
8121 tree position = TREE_VALUE (args);
8122 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8123 position = default_conversion (position);
8125 if (!tree_fits_uhwi_p (position)
8126 || !arg_count
8127 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8129 warning (OPT_Wattributes,
8130 "alloc_align parameter outside range");
8131 *no_add_attrs = true;
8132 return NULL_TREE;
8134 return NULL_TREE;
8137 /* Handle a "assume_aligned" attribute; arguments as in
8138 struct attribute_spec.handler. */
8140 static tree
8141 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8142 bool *no_add_attrs)
8144 for (; args; args = TREE_CHAIN (args))
8146 tree position = TREE_VALUE (args);
8147 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8148 && TREE_CODE (position) != FUNCTION_DECL)
8149 position = default_conversion (position);
8151 if (TREE_CODE (position) != INTEGER_CST)
8153 warning (OPT_Wattributes,
8154 "assume_aligned parameter not integer constant");
8155 *no_add_attrs = true;
8156 return NULL_TREE;
8159 return NULL_TREE;
8162 /* Handle a "fn spec" attribute; arguments as in
8163 struct attribute_spec.handler. */
8165 static tree
8166 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8167 tree args, int ARG_UNUSED (flags),
8168 bool *no_add_attrs ATTRIBUTE_UNUSED)
8170 gcc_assert (args
8171 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8172 && !TREE_CHAIN (args));
8173 return NULL_TREE;
8176 /* Handle a "warn_unused" attribute; arguments as in
8177 struct attribute_spec.handler. */
8179 static tree
8180 handle_warn_unused_attribute (tree *node, tree name,
8181 tree args ATTRIBUTE_UNUSED,
8182 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8184 if (TYPE_P (*node))
8185 /* Do nothing else, just set the attribute. We'll get at
8186 it later with lookup_attribute. */
8188 else
8190 warning (OPT_Wattributes, "%qE attribute ignored", name);
8191 *no_add_attrs = true;
8194 return NULL_TREE;
8197 /* Handle an "omp declare simd" attribute; arguments as in
8198 struct attribute_spec.handler. */
8200 static tree
8201 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8203 return NULL_TREE;
8206 /* Handle an "omp declare target" attribute; arguments as in
8207 struct attribute_spec.handler. */
8209 static tree
8210 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8212 return NULL_TREE;
8215 /* Handle a "returns_twice" attribute; arguments as in
8216 struct attribute_spec.handler. */
8218 static tree
8219 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8220 int ARG_UNUSED (flags), bool *no_add_attrs)
8222 if (TREE_CODE (*node) == FUNCTION_DECL)
8223 DECL_IS_RETURNS_TWICE (*node) = 1;
8224 else
8226 warning (OPT_Wattributes, "%qE attribute ignored", name);
8227 *no_add_attrs = true;
8230 return NULL_TREE;
8233 /* Handle a "no_limit_stack" attribute; arguments as in
8234 struct attribute_spec.handler. */
8236 static tree
8237 handle_no_limit_stack_attribute (tree *node, tree name,
8238 tree ARG_UNUSED (args),
8239 int ARG_UNUSED (flags),
8240 bool *no_add_attrs)
8242 tree decl = *node;
8244 if (TREE_CODE (decl) != FUNCTION_DECL)
8246 error_at (DECL_SOURCE_LOCATION (decl),
8247 "%qE attribute applies only to functions", name);
8248 *no_add_attrs = true;
8250 else if (DECL_INITIAL (decl))
8252 error_at (DECL_SOURCE_LOCATION (decl),
8253 "can%'t set %qE attribute after definition", name);
8254 *no_add_attrs = true;
8256 else
8257 DECL_NO_LIMIT_STACK (decl) = 1;
8259 return NULL_TREE;
8262 /* Handle a "pure" attribute; arguments as in
8263 struct attribute_spec.handler. */
8265 static tree
8266 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8267 int ARG_UNUSED (flags), bool *no_add_attrs)
8269 if (TREE_CODE (*node) == FUNCTION_DECL)
8270 DECL_PURE_P (*node) = 1;
8271 /* ??? TODO: Support types. */
8272 else
8274 warning (OPT_Wattributes, "%qE attribute ignored", name);
8275 *no_add_attrs = true;
8278 return NULL_TREE;
8281 /* Digest an attribute list destined for a transactional memory statement.
8282 ALLOWED is the set of attributes that are allowed for this statement;
8283 return the attribute we parsed. Multiple attributes are never allowed. */
8286 parse_tm_stmt_attr (tree attrs, int allowed)
8288 tree a_seen = NULL;
8289 int m_seen = 0;
8291 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8293 tree a = TREE_PURPOSE (attrs);
8294 int m = 0;
8296 if (is_attribute_p ("outer", a))
8297 m = TM_STMT_ATTR_OUTER;
8299 if ((m & allowed) == 0)
8301 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8302 continue;
8305 if (m_seen == 0)
8307 a_seen = a;
8308 m_seen = m;
8310 else if (m_seen == m)
8311 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8312 else
8313 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8316 return m_seen;
8319 /* Transform a TM attribute name into a maskable integer and back.
8320 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8321 to how the lack of an attribute is treated. */
8324 tm_attr_to_mask (tree attr)
8326 if (attr == NULL)
8327 return 0;
8328 if (is_attribute_p ("transaction_safe", attr))
8329 return TM_ATTR_SAFE;
8330 if (is_attribute_p ("transaction_callable", attr))
8331 return TM_ATTR_CALLABLE;
8332 if (is_attribute_p ("transaction_pure", attr))
8333 return TM_ATTR_PURE;
8334 if (is_attribute_p ("transaction_unsafe", attr))
8335 return TM_ATTR_IRREVOCABLE;
8336 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8337 return TM_ATTR_MAY_CANCEL_OUTER;
8338 return 0;
8341 tree
8342 tm_mask_to_attr (int mask)
8344 const char *str;
8345 switch (mask)
8347 case TM_ATTR_SAFE:
8348 str = "transaction_safe";
8349 break;
8350 case TM_ATTR_CALLABLE:
8351 str = "transaction_callable";
8352 break;
8353 case TM_ATTR_PURE:
8354 str = "transaction_pure";
8355 break;
8356 case TM_ATTR_IRREVOCABLE:
8357 str = "transaction_unsafe";
8358 break;
8359 case TM_ATTR_MAY_CANCEL_OUTER:
8360 str = "transaction_may_cancel_outer";
8361 break;
8362 default:
8363 gcc_unreachable ();
8365 return get_identifier (str);
8368 /* Return the first TM attribute seen in LIST. */
8370 tree
8371 find_tm_attribute (tree list)
8373 for (; list ; list = TREE_CHAIN (list))
8375 tree name = TREE_PURPOSE (list);
8376 if (tm_attr_to_mask (name) != 0)
8377 return name;
8379 return NULL_TREE;
8382 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8383 Here we accept only function types, and verify that none of the other
8384 function TM attributes are also applied. */
8385 /* ??? We need to accept class types for C++, but not C. This greatly
8386 complicates this function, since we can no longer rely on the extra
8387 processing given by function_type_required. */
8389 static tree
8390 handle_tm_attribute (tree *node, tree name, tree args,
8391 int flags, bool *no_add_attrs)
8393 /* Only one path adds the attribute; others don't. */
8394 *no_add_attrs = true;
8396 switch (TREE_CODE (*node))
8398 case RECORD_TYPE:
8399 case UNION_TYPE:
8400 /* Only tm_callable and tm_safe apply to classes. */
8401 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8402 goto ignored;
8403 /* FALLTHRU */
8405 case FUNCTION_TYPE:
8406 case METHOD_TYPE:
8408 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8409 if (old_name == name)
8411 else if (old_name != NULL_TREE)
8412 error ("type was previously declared %qE", old_name);
8413 else
8414 *no_add_attrs = false;
8416 break;
8418 case POINTER_TYPE:
8420 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8421 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8423 tree fn_tmp = TREE_TYPE (*node);
8424 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8425 *node = build_pointer_type (fn_tmp);
8426 break;
8429 /* FALLTHRU */
8431 default:
8432 /* If a function is next, pass it on to be tried next. */
8433 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8434 return tree_cons (name, args, NULL);
8436 ignored:
8437 warning (OPT_Wattributes, "%qE attribute ignored", name);
8438 break;
8441 return NULL_TREE;
8444 /* Handle the TM_WRAP attribute; arguments as in
8445 struct attribute_spec.handler. */
8447 static tree
8448 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8449 int ARG_UNUSED (flags), bool *no_add_attrs)
8451 tree decl = *node;
8453 /* We don't need the attribute even on success, since we
8454 record the entry in an external table. */
8455 *no_add_attrs = true;
8457 if (TREE_CODE (decl) != FUNCTION_DECL)
8458 warning (OPT_Wattributes, "%qE attribute ignored", name);
8459 else
8461 tree wrap_decl = TREE_VALUE (args);
8462 if (error_operand_p (wrap_decl))
8464 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8465 && TREE_CODE (wrap_decl) != VAR_DECL
8466 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8467 error ("%qE argument not an identifier", name);
8468 else
8470 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8471 wrap_decl = lookup_name (wrap_decl);
8472 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8474 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8475 TREE_TYPE (wrap_decl)))
8476 record_tm_replacement (wrap_decl, decl);
8477 else
8478 error ("%qD is not compatible with %qD", wrap_decl, decl);
8480 else
8481 error ("%qE argument is not a function", name);
8485 return NULL_TREE;
8488 /* Ignore the given attribute. Used when this attribute may be usefully
8489 overridden by the target, but is not used generically. */
8491 static tree
8492 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8493 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8494 bool *no_add_attrs)
8496 *no_add_attrs = true;
8497 return NULL_TREE;
8500 /* Handle a "no vops" attribute; arguments as in
8501 struct attribute_spec.handler. */
8503 static tree
8504 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8505 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8506 bool *ARG_UNUSED (no_add_attrs))
8508 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8509 DECL_IS_NOVOPS (*node) = 1;
8510 return NULL_TREE;
8513 /* Handle a "deprecated" attribute; arguments as in
8514 struct attribute_spec.handler. */
8516 static tree
8517 handle_deprecated_attribute (tree *node, tree name,
8518 tree args, int flags,
8519 bool *no_add_attrs)
8521 tree type = NULL_TREE;
8522 int warn = 0;
8523 tree what = NULL_TREE;
8525 if (!args)
8526 *no_add_attrs = true;
8527 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8529 error ("deprecated message is not a string");
8530 *no_add_attrs = true;
8533 if (DECL_P (*node))
8535 tree decl = *node;
8536 type = TREE_TYPE (decl);
8538 if (TREE_CODE (decl) == TYPE_DECL
8539 || TREE_CODE (decl) == PARM_DECL
8540 || TREE_CODE (decl) == VAR_DECL
8541 || TREE_CODE (decl) == FUNCTION_DECL
8542 || TREE_CODE (decl) == FIELD_DECL
8543 || objc_method_decl (TREE_CODE (decl)))
8544 TREE_DEPRECATED (decl) = 1;
8545 else
8546 warn = 1;
8548 else if (TYPE_P (*node))
8550 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8551 *node = build_variant_type_copy (*node);
8552 TREE_DEPRECATED (*node) = 1;
8553 type = *node;
8555 else
8556 warn = 1;
8558 if (warn)
8560 *no_add_attrs = true;
8561 if (type && TYPE_NAME (type))
8563 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8564 what = TYPE_NAME (*node);
8565 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8566 && DECL_NAME (TYPE_NAME (type)))
8567 what = DECL_NAME (TYPE_NAME (type));
8569 if (what)
8570 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8571 else
8572 warning (OPT_Wattributes, "%qE attribute ignored", name);
8575 return NULL_TREE;
8578 /* Handle a "vector_size" attribute; arguments as in
8579 struct attribute_spec.handler. */
8581 static tree
8582 handle_vector_size_attribute (tree *node, tree name, tree args,
8583 int ARG_UNUSED (flags),
8584 bool *no_add_attrs)
8586 unsigned HOST_WIDE_INT vecsize, nunits;
8587 enum machine_mode orig_mode;
8588 tree type = *node, new_type, size;
8590 *no_add_attrs = true;
8592 size = TREE_VALUE (args);
8593 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8594 && TREE_CODE (size) != FUNCTION_DECL)
8595 size = default_conversion (size);
8597 if (!tree_fits_uhwi_p (size))
8599 warning (OPT_Wattributes, "%qE attribute ignored", name);
8600 return NULL_TREE;
8603 /* Get the vector size (in bytes). */
8604 vecsize = tree_to_uhwi (size);
8606 /* We need to provide for vector pointers, vector arrays, and
8607 functions returning vectors. For example:
8609 __attribute__((vector_size(16))) short *foo;
8611 In this case, the mode is SI, but the type being modified is
8612 HI, so we need to look further. */
8614 while (POINTER_TYPE_P (type)
8615 || TREE_CODE (type) == FUNCTION_TYPE
8616 || TREE_CODE (type) == METHOD_TYPE
8617 || TREE_CODE (type) == ARRAY_TYPE
8618 || TREE_CODE (type) == OFFSET_TYPE)
8619 type = TREE_TYPE (type);
8621 /* Get the mode of the type being modified. */
8622 orig_mode = TYPE_MODE (type);
8624 if ((!INTEGRAL_TYPE_P (type)
8625 && !SCALAR_FLOAT_TYPE_P (type)
8626 && !FIXED_POINT_TYPE_P (type))
8627 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8628 && GET_MODE_CLASS (orig_mode) != MODE_INT
8629 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8630 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8631 || TREE_CODE (type) == BOOLEAN_TYPE)
8633 error ("invalid vector type for attribute %qE", name);
8634 return NULL_TREE;
8637 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8639 error ("vector size not an integral multiple of component size");
8640 return NULL;
8643 if (vecsize == 0)
8645 error ("zero vector size");
8646 return NULL;
8649 /* Calculate how many units fit in the vector. */
8650 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8651 if (nunits & (nunits - 1))
8653 error ("number of components of the vector not a power of two");
8654 return NULL_TREE;
8657 new_type = build_vector_type (type, nunits);
8659 /* Build back pointers if needed. */
8660 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8662 return NULL_TREE;
8665 /* Handle the "nonnull" attribute. */
8666 static tree
8667 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8668 tree args, int ARG_UNUSED (flags),
8669 bool *no_add_attrs)
8671 tree type = *node;
8672 unsigned HOST_WIDE_INT attr_arg_num;
8674 /* If no arguments are specified, all pointer arguments should be
8675 non-null. Verify a full prototype is given so that the arguments
8676 will have the correct types when we actually check them later. */
8677 if (!args)
8679 if (!prototype_p (type))
8681 error ("nonnull attribute without arguments on a non-prototype");
8682 *no_add_attrs = true;
8684 return NULL_TREE;
8687 /* Argument list specified. Verify that each argument number references
8688 a pointer argument. */
8689 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8691 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8693 tree arg = TREE_VALUE (args);
8694 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8695 && TREE_CODE (arg) != FUNCTION_DECL)
8696 arg = default_conversion (arg);
8698 if (!get_nonnull_operand (arg, &arg_num))
8700 error ("nonnull argument has invalid operand number (argument %lu)",
8701 (unsigned long) attr_arg_num);
8702 *no_add_attrs = true;
8703 return NULL_TREE;
8706 if (prototype_p (type))
8708 function_args_iterator iter;
8709 tree argument;
8711 function_args_iter_init (&iter, type);
8712 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8714 argument = function_args_iter_cond (&iter);
8715 if (argument == NULL_TREE || ck_num == arg_num)
8716 break;
8719 if (!argument
8720 || TREE_CODE (argument) == VOID_TYPE)
8722 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8723 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8724 *no_add_attrs = true;
8725 return NULL_TREE;
8728 if (TREE_CODE (argument) != POINTER_TYPE)
8730 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8731 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8732 *no_add_attrs = true;
8733 return NULL_TREE;
8738 return NULL_TREE;
8741 /* Check the argument list of a function call for null in argument slots
8742 that are marked as requiring a non-null pointer argument. The NARGS
8743 arguments are passed in the array ARGARRAY.
8746 static void
8747 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8749 tree a;
8750 int i;
8752 attrs = lookup_attribute ("nonnull", attrs);
8753 if (attrs == NULL_TREE)
8754 return;
8756 a = attrs;
8757 /* See if any of the nonnull attributes has no arguments. If so,
8758 then every pointer argument is checked (in which case the check
8759 for pointer type is done in check_nonnull_arg). */
8760 if (TREE_VALUE (a) != NULL_TREE)
8762 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8763 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8765 if (a != NULL_TREE)
8766 for (i = 0; i < nargs; i++)
8767 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8768 i + 1);
8769 else
8771 /* Walk the argument list. If we encounter an argument number we
8772 should check for non-null, do it. */
8773 for (i = 0; i < nargs; i++)
8775 for (a = attrs; ; a = TREE_CHAIN (a))
8777 a = lookup_attribute ("nonnull", a);
8778 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8779 break;
8782 if (a != NULL_TREE)
8783 check_function_arguments_recurse (check_nonnull_arg, NULL,
8784 argarray[i], i + 1);
8789 /* Check that the Nth argument of a function call (counting backwards
8790 from the end) is a (pointer)0. The NARGS arguments are passed in the
8791 array ARGARRAY. */
8793 static void
8794 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8796 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8798 if (attr)
8800 int len = 0;
8801 int pos = 0;
8802 tree sentinel;
8803 function_args_iterator iter;
8804 tree t;
8806 /* Skip over the named arguments. */
8807 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8809 if (len == nargs)
8810 break;
8811 len++;
8814 if (TREE_VALUE (attr))
8816 tree p = TREE_VALUE (TREE_VALUE (attr));
8817 pos = TREE_INT_CST_LOW (p);
8820 /* The sentinel must be one of the varargs, i.e.
8821 in position >= the number of fixed arguments. */
8822 if ((nargs - 1 - pos) < len)
8824 warning (OPT_Wformat_,
8825 "not enough variable arguments to fit a sentinel");
8826 return;
8829 /* Validate the sentinel. */
8830 sentinel = argarray[nargs - 1 - pos];
8831 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8832 || !integer_zerop (sentinel))
8833 /* Although __null (in C++) is only an integer we allow it
8834 nevertheless, as we are guaranteed that it's exactly
8835 as wide as a pointer, and we don't want to force
8836 users to cast the NULL they have written there.
8837 We warn with -Wstrict-null-sentinel, though. */
8838 && (warn_strict_null_sentinel || null_node != sentinel))
8839 warning (OPT_Wformat_, "missing sentinel in function call");
8843 /* Helper for check_function_nonnull; given a list of operands which
8844 must be non-null in ARGS, determine if operand PARAM_NUM should be
8845 checked. */
8847 static bool
8848 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8850 unsigned HOST_WIDE_INT arg_num = 0;
8852 for (; args; args = TREE_CHAIN (args))
8854 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8856 gcc_assert (found);
8858 if (arg_num == param_num)
8859 return true;
8861 return false;
8864 /* Check that the function argument PARAM (which is operand number
8865 PARAM_NUM) is non-null. This is called by check_function_nonnull
8866 via check_function_arguments_recurse. */
8868 static void
8869 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8870 unsigned HOST_WIDE_INT param_num)
8872 /* Just skip checking the argument if it's not a pointer. This can
8873 happen if the "nonnull" attribute was given without an operand
8874 list (which means to check every pointer argument). */
8876 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8877 return;
8879 if (integer_zerop (param))
8880 warning (OPT_Wnonnull, "null argument where non-null required "
8881 "(argument %lu)", (unsigned long) param_num);
8884 /* Helper for nonnull attribute handling; fetch the operand number
8885 from the attribute argument list. */
8887 static bool
8888 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8890 /* Verify the arg number is a small constant. */
8891 if (tree_fits_uhwi_p (arg_num_expr))
8893 *valp = TREE_INT_CST_LOW (arg_num_expr);
8894 return true;
8896 else
8897 return false;
8900 /* Handle a "nothrow" attribute; arguments as in
8901 struct attribute_spec.handler. */
8903 static tree
8904 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8905 int ARG_UNUSED (flags), bool *no_add_attrs)
8907 if (TREE_CODE (*node) == FUNCTION_DECL)
8908 TREE_NOTHROW (*node) = 1;
8909 /* ??? TODO: Support types. */
8910 else
8912 warning (OPT_Wattributes, "%qE attribute ignored", name);
8913 *no_add_attrs = true;
8916 return NULL_TREE;
8919 /* Handle a "cleanup" attribute; arguments as in
8920 struct attribute_spec.handler. */
8922 static tree
8923 handle_cleanup_attribute (tree *node, tree name, tree args,
8924 int ARG_UNUSED (flags), bool *no_add_attrs)
8926 tree decl = *node;
8927 tree cleanup_id, cleanup_decl;
8929 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8930 for global destructors in C++. This requires infrastructure that
8931 we don't have generically at the moment. It's also not a feature
8932 we'd be missing too much, since we do have attribute constructor. */
8933 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8935 warning (OPT_Wattributes, "%qE attribute ignored", name);
8936 *no_add_attrs = true;
8937 return NULL_TREE;
8940 /* Verify that the argument is a function in scope. */
8941 /* ??? We could support pointers to functions here as well, if
8942 that was considered desirable. */
8943 cleanup_id = TREE_VALUE (args);
8944 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8946 error ("cleanup argument not an identifier");
8947 *no_add_attrs = true;
8948 return NULL_TREE;
8950 cleanup_decl = lookup_name (cleanup_id);
8951 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8953 error ("cleanup argument not a function");
8954 *no_add_attrs = true;
8955 return NULL_TREE;
8958 /* That the function has proper type is checked with the
8959 eventual call to build_function_call. */
8961 return NULL_TREE;
8964 /* Handle a "warn_unused_result" attribute. No special handling. */
8966 static tree
8967 handle_warn_unused_result_attribute (tree *node, tree name,
8968 tree ARG_UNUSED (args),
8969 int ARG_UNUSED (flags), bool *no_add_attrs)
8971 /* Ignore the attribute for functions not returning any value. */
8972 if (VOID_TYPE_P (TREE_TYPE (*node)))
8974 warning (OPT_Wattributes, "%qE attribute ignored", name);
8975 *no_add_attrs = true;
8978 return NULL_TREE;
8981 /* Handle a "sentinel" attribute. */
8983 static tree
8984 handle_sentinel_attribute (tree *node, tree name, tree args,
8985 int ARG_UNUSED (flags), bool *no_add_attrs)
8987 if (!prototype_p (*node))
8989 warning (OPT_Wattributes,
8990 "%qE attribute requires prototypes with named arguments", name);
8991 *no_add_attrs = true;
8993 else
8995 if (!stdarg_p (*node))
8997 warning (OPT_Wattributes,
8998 "%qE attribute only applies to variadic functions", name);
8999 *no_add_attrs = true;
9003 if (args)
9005 tree position = TREE_VALUE (args);
9006 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9007 && TREE_CODE (position) != FUNCTION_DECL)
9008 position = default_conversion (position);
9010 if (TREE_CODE (position) != INTEGER_CST
9011 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9013 warning (OPT_Wattributes,
9014 "requested position is not an integer constant");
9015 *no_add_attrs = true;
9017 else
9019 if (tree_int_cst_lt (position, integer_zero_node))
9021 warning (OPT_Wattributes,
9022 "requested position is less than zero");
9023 *no_add_attrs = true;
9028 return NULL_TREE;
9031 /* Handle a "type_generic" attribute. */
9033 static tree
9034 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9035 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9036 bool * ARG_UNUSED (no_add_attrs))
9038 /* Ensure we have a function type. */
9039 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9041 /* Ensure we have a variadic function. */
9042 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9044 return NULL_TREE;
9047 /* Handle a "target" attribute. */
9049 static tree
9050 handle_target_attribute (tree *node, tree name, tree args, int flags,
9051 bool *no_add_attrs)
9053 /* Ensure we have a function type. */
9054 if (TREE_CODE (*node) != FUNCTION_DECL)
9056 warning (OPT_Wattributes, "%qE attribute ignored", name);
9057 *no_add_attrs = true;
9059 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9060 flags))
9061 *no_add_attrs = true;
9063 return NULL_TREE;
9066 /* Arguments being collected for optimization. */
9067 typedef const char *const_char_p; /* For DEF_VEC_P. */
9068 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9071 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9072 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9073 false for #pragma GCC optimize. */
9075 bool
9076 parse_optimize_options (tree args, bool attr_p)
9078 bool ret = true;
9079 unsigned opt_argc;
9080 unsigned i;
9081 int saved_flag_strict_aliasing;
9082 const char **opt_argv;
9083 struct cl_decoded_option *decoded_options;
9084 unsigned int decoded_options_count;
9085 tree ap;
9087 /* Build up argv vector. Just in case the string is stored away, use garbage
9088 collected strings. */
9089 vec_safe_truncate (optimize_args, 0);
9090 vec_safe_push (optimize_args, (const char *) NULL);
9092 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9094 tree value = TREE_VALUE (ap);
9096 if (TREE_CODE (value) == INTEGER_CST)
9098 char buffer[20];
9099 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9100 vec_safe_push (optimize_args, ggc_strdup (buffer));
9103 else if (TREE_CODE (value) == STRING_CST)
9105 /* Split string into multiple substrings. */
9106 size_t len = TREE_STRING_LENGTH (value);
9107 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9108 char *end = p + len;
9109 char *comma;
9110 char *next_p = p;
9112 while (next_p != NULL)
9114 size_t len2;
9115 char *q, *r;
9117 p = next_p;
9118 comma = strchr (p, ',');
9119 if (comma)
9121 len2 = comma - p;
9122 *comma = '\0';
9123 next_p = comma+1;
9125 else
9127 len2 = end - p;
9128 next_p = NULL;
9131 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9133 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9134 options. */
9135 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9137 ret = false;
9138 if (attr_p)
9139 warning (OPT_Wattributes,
9140 "bad option %s to optimize attribute", p);
9141 else
9142 warning (OPT_Wpragmas,
9143 "bad option %s to pragma attribute", p);
9144 continue;
9147 if (*p != '-')
9149 *r++ = '-';
9151 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9152 itself is -Os, and any other switch begins with a -f. */
9153 if ((*p >= '0' && *p <= '9')
9154 || (p[0] == 's' && p[1] == '\0'))
9155 *r++ = 'O';
9156 else if (*p != 'O')
9157 *r++ = 'f';
9160 memcpy (r, p, len2);
9161 r[len2] = '\0';
9162 vec_safe_push (optimize_args, (const char *) q);
9168 opt_argc = optimize_args->length ();
9169 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9171 for (i = 1; i < opt_argc; i++)
9172 opt_argv[i] = (*optimize_args)[i];
9174 saved_flag_strict_aliasing = flag_strict_aliasing;
9176 /* Now parse the options. */
9177 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9178 &decoded_options,
9179 &decoded_options_count);
9180 decode_options (&global_options, &global_options_set,
9181 decoded_options, decoded_options_count,
9182 input_location, global_dc);
9184 targetm.override_options_after_change();
9186 /* Don't allow changing -fstrict-aliasing. */
9187 flag_strict_aliasing = saved_flag_strict_aliasing;
9189 optimize_args->truncate (0);
9190 return ret;
9193 /* For handling "optimize" attribute. arguments as in
9194 struct attribute_spec.handler. */
9196 static tree
9197 handle_optimize_attribute (tree *node, tree name, tree args,
9198 int ARG_UNUSED (flags), bool *no_add_attrs)
9200 /* Ensure we have a function type. */
9201 if (TREE_CODE (*node) != FUNCTION_DECL)
9203 warning (OPT_Wattributes, "%qE attribute ignored", name);
9204 *no_add_attrs = true;
9206 else
9208 struct cl_optimization cur_opts;
9209 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9211 /* Save current options. */
9212 cl_optimization_save (&cur_opts, &global_options);
9214 /* If we previously had some optimization options, use them as the
9215 default. */
9216 if (old_opts)
9217 cl_optimization_restore (&global_options,
9218 TREE_OPTIMIZATION (old_opts));
9220 /* Parse options, and update the vector. */
9221 parse_optimize_options (args, true);
9222 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9223 = build_optimization_node (&global_options);
9225 /* Restore current options. */
9226 cl_optimization_restore (&global_options, &cur_opts);
9229 return NULL_TREE;
9232 /* Handle a "no_split_stack" attribute. */
9234 static tree
9235 handle_no_split_stack_attribute (tree *node, tree name,
9236 tree ARG_UNUSED (args),
9237 int ARG_UNUSED (flags),
9238 bool *no_add_attrs)
9240 tree decl = *node;
9242 if (TREE_CODE (decl) != FUNCTION_DECL)
9244 error_at (DECL_SOURCE_LOCATION (decl),
9245 "%qE attribute applies only to functions", name);
9246 *no_add_attrs = true;
9248 else if (DECL_INITIAL (decl))
9250 error_at (DECL_SOURCE_LOCATION (decl),
9251 "can%'t set %qE attribute after definition", name);
9252 *no_add_attrs = true;
9255 return NULL_TREE;
9258 /* Handle a "returns_nonnull" attribute; arguments as in
9259 struct attribute_spec.handler. */
9261 static tree
9262 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9263 bool *no_add_attrs)
9265 // Even without a prototype we still have a return type we can check.
9266 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9268 error ("returns_nonnull attribute on a function not returning a pointer");
9269 *no_add_attrs = true;
9271 return NULL_TREE;
9275 /* Check for valid arguments being passed to a function with FNTYPE.
9276 There are NARGS arguments in the array ARGARRAY. */
9277 void
9278 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9280 /* Check for null being passed in a pointer argument that must be
9281 non-null. We also need to do this if format checking is enabled. */
9283 if (warn_nonnull)
9284 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9286 /* Check for errors in format strings. */
9288 if (warn_format || warn_suggest_attribute_format)
9289 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9291 if (warn_format)
9292 check_function_sentinel (fntype, nargs, argarray);
9295 /* Generic argument checking recursion routine. PARAM is the argument to
9296 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9297 once the argument is resolved. CTX is context for the callback. */
9298 void
9299 check_function_arguments_recurse (void (*callback)
9300 (void *, tree, unsigned HOST_WIDE_INT),
9301 void *ctx, tree param,
9302 unsigned HOST_WIDE_INT param_num)
9304 if (CONVERT_EXPR_P (param)
9305 && (TYPE_PRECISION (TREE_TYPE (param))
9306 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9308 /* Strip coercion. */
9309 check_function_arguments_recurse (callback, ctx,
9310 TREE_OPERAND (param, 0), param_num);
9311 return;
9314 if (TREE_CODE (param) == CALL_EXPR)
9316 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9317 tree attrs;
9318 bool found_format_arg = false;
9320 /* See if this is a call to a known internationalization function
9321 that modifies a format arg. Such a function may have multiple
9322 format_arg attributes (for example, ngettext). */
9324 for (attrs = TYPE_ATTRIBUTES (type);
9325 attrs;
9326 attrs = TREE_CHAIN (attrs))
9327 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9329 tree inner_arg;
9330 tree format_num_expr;
9331 int format_num;
9332 int i;
9333 call_expr_arg_iterator iter;
9335 /* Extract the argument number, which was previously checked
9336 to be valid. */
9337 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9339 format_num = tree_to_uhwi (format_num_expr);
9341 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9342 inner_arg != 0;
9343 inner_arg = next_call_expr_arg (&iter), i++)
9344 if (i == format_num)
9346 check_function_arguments_recurse (callback, ctx,
9347 inner_arg, param_num);
9348 found_format_arg = true;
9349 break;
9353 /* If we found a format_arg attribute and did a recursive check,
9354 we are done with checking this argument. Otherwise, we continue
9355 and this will be considered a non-literal. */
9356 if (found_format_arg)
9357 return;
9360 if (TREE_CODE (param) == COND_EXPR)
9362 /* Check both halves of the conditional expression. */
9363 check_function_arguments_recurse (callback, ctx,
9364 TREE_OPERAND (param, 1), param_num);
9365 check_function_arguments_recurse (callback, ctx,
9366 TREE_OPERAND (param, 2), param_num);
9367 return;
9370 (*callback) (ctx, param, param_num);
9373 /* Checks for a builtin function FNDECL that the number of arguments
9374 NARGS against the required number REQUIRED and issues an error if
9375 there is a mismatch. Returns true if the number of arguments is
9376 correct, otherwise false. */
9378 static bool
9379 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9381 if (nargs < required)
9383 error_at (input_location,
9384 "not enough arguments to function %qE", fndecl);
9385 return false;
9387 else if (nargs > required)
9389 error_at (input_location,
9390 "too many arguments to function %qE", fndecl);
9391 return false;
9393 return true;
9396 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9397 Returns false if there was an error, otherwise true. */
9399 bool
9400 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9402 if (!DECL_BUILT_IN (fndecl)
9403 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9404 return true;
9406 switch (DECL_FUNCTION_CODE (fndecl))
9408 case BUILT_IN_CONSTANT_P:
9409 return builtin_function_validate_nargs (fndecl, nargs, 1);
9411 case BUILT_IN_ISFINITE:
9412 case BUILT_IN_ISINF:
9413 case BUILT_IN_ISINF_SIGN:
9414 case BUILT_IN_ISNAN:
9415 case BUILT_IN_ISNORMAL:
9416 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9418 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9420 error ("non-floating-point argument in call to "
9421 "function %qE", fndecl);
9422 return false;
9424 return true;
9426 return false;
9428 case BUILT_IN_ISGREATER:
9429 case BUILT_IN_ISGREATEREQUAL:
9430 case BUILT_IN_ISLESS:
9431 case BUILT_IN_ISLESSEQUAL:
9432 case BUILT_IN_ISLESSGREATER:
9433 case BUILT_IN_ISUNORDERED:
9434 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9436 enum tree_code code0, code1;
9437 code0 = TREE_CODE (TREE_TYPE (args[0]));
9438 code1 = TREE_CODE (TREE_TYPE (args[1]));
9439 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9440 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9441 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9443 error ("non-floating-point arguments in call to "
9444 "function %qE", fndecl);
9445 return false;
9447 return true;
9449 return false;
9451 case BUILT_IN_FPCLASSIFY:
9452 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9454 unsigned i;
9456 for (i=0; i<5; i++)
9457 if (TREE_CODE (args[i]) != INTEGER_CST)
9459 error ("non-const integer argument %u in call to function %qE",
9460 i+1, fndecl);
9461 return false;
9464 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9466 error ("non-floating-point argument in call to function %qE",
9467 fndecl);
9468 return false;
9470 return true;
9472 return false;
9474 case BUILT_IN_ASSUME_ALIGNED:
9475 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9477 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9479 error ("non-integer argument 3 in call to function %qE", fndecl);
9480 return false;
9482 return true;
9484 return false;
9486 default:
9487 return true;
9491 /* Function to help qsort sort FIELD_DECLs by name order. */
9494 field_decl_cmp (const void *x_p, const void *y_p)
9496 const tree *const x = (const tree *const) x_p;
9497 const tree *const y = (const tree *const) y_p;
9499 if (DECL_NAME (*x) == DECL_NAME (*y))
9500 /* A nontype is "greater" than a type. */
9501 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9502 if (DECL_NAME (*x) == NULL_TREE)
9503 return -1;
9504 if (DECL_NAME (*y) == NULL_TREE)
9505 return 1;
9506 if (DECL_NAME (*x) < DECL_NAME (*y))
9507 return -1;
9508 return 1;
9511 static struct {
9512 gt_pointer_operator new_value;
9513 void *cookie;
9514 } resort_data;
9516 /* This routine compares two fields like field_decl_cmp but using the
9517 pointer operator in resort_data. */
9519 static int
9520 resort_field_decl_cmp (const void *x_p, const void *y_p)
9522 const tree *const x = (const tree *const) x_p;
9523 const tree *const y = (const tree *const) y_p;
9525 if (DECL_NAME (*x) == DECL_NAME (*y))
9526 /* A nontype is "greater" than a type. */
9527 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9528 if (DECL_NAME (*x) == NULL_TREE)
9529 return -1;
9530 if (DECL_NAME (*y) == NULL_TREE)
9531 return 1;
9533 tree d1 = DECL_NAME (*x);
9534 tree d2 = DECL_NAME (*y);
9535 resort_data.new_value (&d1, resort_data.cookie);
9536 resort_data.new_value (&d2, resort_data.cookie);
9537 if (d1 < d2)
9538 return -1;
9540 return 1;
9543 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9545 void
9546 resort_sorted_fields (void *obj,
9547 void * ARG_UNUSED (orig_obj),
9548 gt_pointer_operator new_value,
9549 void *cookie)
9551 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9552 resort_data.new_value = new_value;
9553 resort_data.cookie = cookie;
9554 qsort (&sf->elts[0], sf->len, sizeof (tree),
9555 resort_field_decl_cmp);
9558 /* Subroutine of c_parse_error.
9559 Return the result of concatenating LHS and RHS. RHS is really
9560 a string literal, its first character is indicated by RHS_START and
9561 RHS_SIZE is its length (including the terminating NUL character).
9563 The caller is responsible for deleting the returned pointer. */
9565 static char *
9566 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9568 const int lhs_size = strlen (lhs);
9569 char *result = XNEWVEC (char, lhs_size + rhs_size);
9570 strncpy (result, lhs, lhs_size);
9571 strncpy (result + lhs_size, rhs_start, rhs_size);
9572 return result;
9575 /* Issue the error given by GMSGID, indicating that it occurred before
9576 TOKEN, which had the associated VALUE. */
9578 void
9579 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9580 tree value, unsigned char token_flags)
9582 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9584 char *message = NULL;
9586 if (token_type == CPP_EOF)
9587 message = catenate_messages (gmsgid, " at end of input");
9588 else if (token_type == CPP_CHAR
9589 || token_type == CPP_WCHAR
9590 || token_type == CPP_CHAR16
9591 || token_type == CPP_CHAR32)
9593 unsigned int val = TREE_INT_CST_LOW (value);
9594 const char *prefix;
9596 switch (token_type)
9598 default:
9599 prefix = "";
9600 break;
9601 case CPP_WCHAR:
9602 prefix = "L";
9603 break;
9604 case CPP_CHAR16:
9605 prefix = "u";
9606 break;
9607 case CPP_CHAR32:
9608 prefix = "U";
9609 break;
9612 if (val <= UCHAR_MAX && ISGRAPH (val))
9613 message = catenate_messages (gmsgid, " before %s'%c'");
9614 else
9615 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9617 error (message, prefix, val);
9618 free (message);
9619 message = NULL;
9621 else if (token_type == CPP_CHAR_USERDEF
9622 || token_type == CPP_WCHAR_USERDEF
9623 || token_type == CPP_CHAR16_USERDEF
9624 || token_type == CPP_CHAR32_USERDEF)
9625 message = catenate_messages (gmsgid,
9626 " before user-defined character literal");
9627 else if (token_type == CPP_STRING_USERDEF
9628 || token_type == CPP_WSTRING_USERDEF
9629 || token_type == CPP_STRING16_USERDEF
9630 || token_type == CPP_STRING32_USERDEF
9631 || token_type == CPP_UTF8STRING_USERDEF)
9632 message = catenate_messages (gmsgid, " before user-defined string literal");
9633 else if (token_type == CPP_STRING
9634 || token_type == CPP_WSTRING
9635 || token_type == CPP_STRING16
9636 || token_type == CPP_STRING32
9637 || token_type == CPP_UTF8STRING)
9638 message = catenate_messages (gmsgid, " before string constant");
9639 else if (token_type == CPP_NUMBER)
9640 message = catenate_messages (gmsgid, " before numeric constant");
9641 else if (token_type == CPP_NAME)
9643 message = catenate_messages (gmsgid, " before %qE");
9644 error (message, value);
9645 free (message);
9646 message = NULL;
9648 else if (token_type == CPP_PRAGMA)
9649 message = catenate_messages (gmsgid, " before %<#pragma%>");
9650 else if (token_type == CPP_PRAGMA_EOL)
9651 message = catenate_messages (gmsgid, " before end of line");
9652 else if (token_type == CPP_DECLTYPE)
9653 message = catenate_messages (gmsgid, " before %<decltype%>");
9654 else if (token_type < N_TTYPES)
9656 message = catenate_messages (gmsgid, " before %qs token");
9657 error (message, cpp_type2name (token_type, token_flags));
9658 free (message);
9659 message = NULL;
9661 else
9662 error (gmsgid);
9664 if (message)
9666 error (message);
9667 free (message);
9669 #undef catenate_messages
9672 /* Mapping for cpp message reasons to the options that enable them. */
9674 struct reason_option_codes_t
9676 const int reason; /* cpplib message reason. */
9677 const int option_code; /* gcc option that controls this message. */
9680 static const struct reason_option_codes_t option_codes[] = {
9681 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9682 {CPP_W_COMMENTS, OPT_Wcomment},
9683 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9684 {CPP_W_MULTICHAR, OPT_Wmultichar},
9685 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9686 {CPP_W_LONG_LONG, OPT_Wlong_long},
9687 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9688 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9689 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9690 {CPP_W_UNDEF, OPT_Wundef},
9691 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9692 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9693 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9694 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9695 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9696 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9697 {CPP_W_DATE_TIME, OPT_Wdate_time},
9698 {CPP_W_NONE, 0}
9701 /* Return the gcc option code associated with the reason for a cpp
9702 message, or 0 if none. */
9704 static int
9705 c_option_controlling_cpp_error (int reason)
9707 const struct reason_option_codes_t *entry;
9709 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9711 if (entry->reason == reason)
9712 return entry->option_code;
9714 return 0;
9717 /* Callback from cpp_error for PFILE to print diagnostics from the
9718 preprocessor. The diagnostic is of type LEVEL, with REASON set
9719 to the reason code if LEVEL is represents a warning, at location
9720 LOCATION unless this is after lexing and the compiler's location
9721 should be used instead, with column number possibly overridden by
9722 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9723 the arguments. Returns true if a diagnostic was emitted, false
9724 otherwise. */
9726 bool
9727 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9728 location_t location, unsigned int column_override,
9729 const char *msg, va_list *ap)
9731 diagnostic_info diagnostic;
9732 diagnostic_t dlevel;
9733 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9734 bool ret;
9736 switch (level)
9738 case CPP_DL_WARNING_SYSHDR:
9739 if (flag_no_output)
9740 return false;
9741 global_dc->dc_warn_system_headers = 1;
9742 /* Fall through. */
9743 case CPP_DL_WARNING:
9744 if (flag_no_output)
9745 return false;
9746 dlevel = DK_WARNING;
9747 break;
9748 case CPP_DL_PEDWARN:
9749 if (flag_no_output && !flag_pedantic_errors)
9750 return false;
9751 dlevel = DK_PEDWARN;
9752 break;
9753 case CPP_DL_ERROR:
9754 dlevel = DK_ERROR;
9755 break;
9756 case CPP_DL_ICE:
9757 dlevel = DK_ICE;
9758 break;
9759 case CPP_DL_NOTE:
9760 dlevel = DK_NOTE;
9761 break;
9762 case CPP_DL_FATAL:
9763 dlevel = DK_FATAL;
9764 break;
9765 default:
9766 gcc_unreachable ();
9768 if (done_lexing)
9769 location = input_location;
9770 diagnostic_set_info_translated (&diagnostic, msg, ap,
9771 location, dlevel);
9772 if (column_override)
9773 diagnostic_override_column (&diagnostic, column_override);
9774 diagnostic_override_option_index (&diagnostic,
9775 c_option_controlling_cpp_error (reason));
9776 ret = report_diagnostic (&diagnostic);
9777 if (level == CPP_DL_WARNING_SYSHDR)
9778 global_dc->dc_warn_system_headers = save_warn_system_headers;
9779 return ret;
9782 /* Convert a character from the host to the target execution character
9783 set. cpplib handles this, mostly. */
9785 HOST_WIDE_INT
9786 c_common_to_target_charset (HOST_WIDE_INT c)
9788 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9789 zero-extended under -fno-signed-char. cpplib insists that characters
9790 and character constants are always unsigned. Hence we must convert
9791 back and forth. */
9792 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9794 uc = cpp_host_to_exec_charset (parse_in, uc);
9796 if (flag_signed_char)
9797 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9798 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9799 else
9800 return uc;
9803 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9804 references with an INDIRECT_REF of a constant at the bottom; much like the
9805 traditional rendering of offsetof as a macro. Return the folded result. */
9807 tree
9808 fold_offsetof_1 (tree expr)
9810 tree base, off, t;
9812 switch (TREE_CODE (expr))
9814 case ERROR_MARK:
9815 return expr;
9817 case VAR_DECL:
9818 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9819 return error_mark_node;
9821 case CALL_EXPR:
9822 case TARGET_EXPR:
9823 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9824 return error_mark_node;
9826 case NOP_EXPR:
9827 case INDIRECT_REF:
9828 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9830 error ("cannot apply %<offsetof%> to a non constant address");
9831 return error_mark_node;
9833 return TREE_OPERAND (expr, 0);
9835 case COMPONENT_REF:
9836 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9837 if (base == error_mark_node)
9838 return base;
9840 t = TREE_OPERAND (expr, 1);
9841 if (DECL_C_BIT_FIELD (t))
9843 error ("attempt to take address of bit-field structure "
9844 "member %qD", t);
9845 return error_mark_node;
9847 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9848 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
9849 / BITS_PER_UNIT));
9850 break;
9852 case ARRAY_REF:
9853 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9854 if (base == error_mark_node)
9855 return base;
9857 t = TREE_OPERAND (expr, 1);
9859 /* Check if the offset goes beyond the upper bound of the array. */
9860 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9862 tree upbound = array_ref_up_bound (expr);
9863 if (upbound != NULL_TREE
9864 && TREE_CODE (upbound) == INTEGER_CST
9865 && !tree_int_cst_equal (upbound,
9866 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9868 upbound = size_binop (PLUS_EXPR, upbound,
9869 build_int_cst (TREE_TYPE (upbound), 1));
9870 if (tree_int_cst_lt (upbound, t))
9872 tree v;
9874 for (v = TREE_OPERAND (expr, 0);
9875 TREE_CODE (v) == COMPONENT_REF;
9876 v = TREE_OPERAND (v, 0))
9877 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9878 == RECORD_TYPE)
9880 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9881 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9882 if (TREE_CODE (fld_chain) == FIELD_DECL)
9883 break;
9885 if (fld_chain)
9886 break;
9888 /* Don't warn if the array might be considered a poor
9889 man's flexible array member with a very permissive
9890 definition thereof. */
9891 if (TREE_CODE (v) == ARRAY_REF
9892 || TREE_CODE (v) == COMPONENT_REF)
9893 warning (OPT_Warray_bounds,
9894 "index %E denotes an offset "
9895 "greater than size of %qT",
9896 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9901 t = convert (sizetype, t);
9902 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9903 break;
9905 case COMPOUND_EXPR:
9906 /* Handle static members of volatile structs. */
9907 t = TREE_OPERAND (expr, 1);
9908 gcc_assert (TREE_CODE (t) == VAR_DECL);
9909 return fold_offsetof_1 (t);
9911 default:
9912 gcc_unreachable ();
9915 return fold_build_pointer_plus (base, off);
9918 /* Likewise, but convert it to the return type of offsetof. */
9920 tree
9921 fold_offsetof (tree expr)
9923 return convert (size_type_node, fold_offsetof_1 (expr));
9926 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9927 expression, because B will always be true. */
9929 void
9930 warn_for_omitted_condop (location_t location, tree cond)
9932 if (truth_value_p (TREE_CODE (cond)))
9933 warning_at (location, OPT_Wparentheses,
9934 "the omitted middle operand in ?: will always be %<true%>, "
9935 "suggest explicit middle operand");
9938 /* Give an error for storing into ARG, which is 'const'. USE indicates
9939 how ARG was being used. */
9941 void
9942 readonly_error (location_t loc, tree arg, enum lvalue_use use)
9944 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9945 || use == lv_asm);
9946 /* Using this macro rather than (for example) arrays of messages
9947 ensures that all the format strings are checked at compile
9948 time. */
9949 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9950 : (use == lv_increment ? (I) \
9951 : (use == lv_decrement ? (D) : (AS))))
9952 if (TREE_CODE (arg) == COMPONENT_REF)
9954 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9955 error_at (loc, READONLY_MSG (G_("assignment of member "
9956 "%qD in read-only object"),
9957 G_("increment of member "
9958 "%qD in read-only object"),
9959 G_("decrement of member "
9960 "%qD in read-only object"),
9961 G_("member %qD in read-only object "
9962 "used as %<asm%> output")),
9963 TREE_OPERAND (arg, 1));
9964 else
9965 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
9966 G_("increment of read-only member %qD"),
9967 G_("decrement of read-only member %qD"),
9968 G_("read-only member %qD used as %<asm%> output")),
9969 TREE_OPERAND (arg, 1));
9971 else if (TREE_CODE (arg) == VAR_DECL)
9972 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
9973 G_("increment of read-only variable %qD"),
9974 G_("decrement of read-only variable %qD"),
9975 G_("read-only variable %qD used as %<asm%> output")),
9976 arg);
9977 else if (TREE_CODE (arg) == PARM_DECL)
9978 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
9979 G_("increment of read-only parameter %qD"),
9980 G_("decrement of read-only parameter %qD"),
9981 G_("read-only parameter %qD use as %<asm%> output")),
9982 arg);
9983 else if (TREE_CODE (arg) == RESULT_DECL)
9985 gcc_assert (c_dialect_cxx ());
9986 error_at (loc, READONLY_MSG (G_("assignment of "
9987 "read-only named return value %qD"),
9988 G_("increment of "
9989 "read-only named return value %qD"),
9990 G_("decrement of "
9991 "read-only named return value %qD"),
9992 G_("read-only named return value %qD "
9993 "used as %<asm%>output")),
9994 arg);
9996 else if (TREE_CODE (arg) == FUNCTION_DECL)
9997 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
9998 G_("increment of function %qD"),
9999 G_("decrement of function %qD"),
10000 G_("function %qD used as %<asm%> output")),
10001 arg);
10002 else
10003 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10004 G_("increment of read-only location %qE"),
10005 G_("decrement of read-only location %qE"),
10006 G_("read-only location %qE used as %<asm%> output")),
10007 arg);
10010 /* Print an error message for an invalid lvalue. USE says
10011 how the lvalue is being used and so selects the error message. LOC
10012 is the location for the error. */
10014 void
10015 lvalue_error (location_t loc, enum lvalue_use use)
10017 switch (use)
10019 case lv_assign:
10020 error_at (loc, "lvalue required as left operand of assignment");
10021 break;
10022 case lv_increment:
10023 error_at (loc, "lvalue required as increment operand");
10024 break;
10025 case lv_decrement:
10026 error_at (loc, "lvalue required as decrement operand");
10027 break;
10028 case lv_addressof:
10029 error_at (loc, "lvalue required as unary %<&%> operand");
10030 break;
10031 case lv_asm:
10032 error_at (loc, "lvalue required in asm statement");
10033 break;
10034 default:
10035 gcc_unreachable ();
10039 /* Print an error message for an invalid indirection of type TYPE.
10040 ERRSTRING is the name of the operator for the indirection. */
10042 void
10043 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10045 switch (errstring)
10047 case RO_NULL:
10048 gcc_assert (c_dialect_cxx ());
10049 error_at (loc, "invalid type argument (have %qT)", type);
10050 break;
10051 case RO_ARRAY_INDEXING:
10052 error_at (loc,
10053 "invalid type argument of array indexing (have %qT)",
10054 type);
10055 break;
10056 case RO_UNARY_STAR:
10057 error_at (loc,
10058 "invalid type argument of unary %<*%> (have %qT)",
10059 type);
10060 break;
10061 case RO_ARROW:
10062 error_at (loc,
10063 "invalid type argument of %<->%> (have %qT)",
10064 type);
10065 break;
10066 case RO_ARROW_STAR:
10067 error_at (loc,
10068 "invalid type argument of %<->*%> (have %qT)",
10069 type);
10070 break;
10071 case RO_IMPLICIT_CONVERSION:
10072 error_at (loc,
10073 "invalid type argument of implicit conversion (have %qT)",
10074 type);
10075 break;
10076 default:
10077 gcc_unreachable ();
10081 /* *PTYPE is an incomplete array. Complete it with a domain based on
10082 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10083 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10084 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10087 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10089 tree maxindex, type, main_type, elt, unqual_elt;
10090 int failure = 0, quals;
10091 hashval_t hashcode = 0;
10092 bool overflow_p = false;
10094 maxindex = size_zero_node;
10095 if (initial_value)
10097 if (TREE_CODE (initial_value) == STRING_CST)
10099 int eltsize
10100 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10101 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10103 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10105 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10107 if (vec_safe_is_empty (v))
10109 if (pedantic)
10110 failure = 3;
10111 maxindex = ssize_int (-1);
10113 else
10115 tree curindex;
10116 unsigned HOST_WIDE_INT cnt;
10117 constructor_elt *ce;
10118 bool fold_p = false;
10120 if ((*v)[0].index)
10121 maxindex = (*v)[0].index, fold_p = true;
10123 curindex = maxindex;
10125 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10127 bool curfold_p = false;
10128 if (ce->index)
10129 curindex = ce->index, curfold_p = true;
10130 else
10132 if (fold_p)
10134 /* Since we treat size types now as ordinary
10135 unsigned types, we need an explicit overflow
10136 check. */
10137 tree orig = curindex;
10138 curindex = fold_convert (sizetype, curindex);
10139 overflow_p |= tree_int_cst_lt (curindex, orig);
10141 curindex = size_binop (PLUS_EXPR, curindex,
10142 size_one_node);
10144 if (tree_int_cst_lt (maxindex, curindex))
10145 maxindex = curindex, fold_p = curfold_p;
10147 if (fold_p)
10149 tree orig = maxindex;
10150 maxindex = fold_convert (sizetype, maxindex);
10151 overflow_p |= tree_int_cst_lt (maxindex, orig);
10155 else
10157 /* Make an error message unless that happened already. */
10158 if (initial_value != error_mark_node)
10159 failure = 1;
10162 else
10164 failure = 2;
10165 if (!do_default)
10166 return failure;
10169 type = *ptype;
10170 elt = TREE_TYPE (type);
10171 quals = TYPE_QUALS (strip_array_types (elt));
10172 if (quals == 0)
10173 unqual_elt = elt;
10174 else
10175 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10177 /* Using build_distinct_type_copy and modifying things afterward instead
10178 of using build_array_type to create a new type preserves all of the
10179 TYPE_LANG_FLAG_? bits that the front end may have set. */
10180 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10181 TREE_TYPE (main_type) = unqual_elt;
10182 TYPE_DOMAIN (main_type)
10183 = build_range_type (TREE_TYPE (maxindex),
10184 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10185 layout_type (main_type);
10187 /* Make sure we have the canonical MAIN_TYPE. */
10188 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10189 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10190 hashcode);
10191 main_type = type_hash_canon (hashcode, main_type);
10193 /* Fix the canonical type. */
10194 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10195 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10196 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10197 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10198 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10199 != TYPE_DOMAIN (main_type)))
10200 TYPE_CANONICAL (main_type)
10201 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10202 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10203 else
10204 TYPE_CANONICAL (main_type) = main_type;
10206 if (quals == 0)
10207 type = main_type;
10208 else
10209 type = c_build_qualified_type (main_type, quals);
10211 if (COMPLETE_TYPE_P (type)
10212 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10213 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10215 error ("size of array is too large");
10216 /* If we proceed with the array type as it is, we'll eventually
10217 crash in tree_to_[su]hwi(). */
10218 type = error_mark_node;
10221 *ptype = type;
10222 return failure;
10225 /* Like c_mark_addressable but don't check register qualifier. */
10226 void
10227 c_common_mark_addressable_vec (tree t)
10229 while (handled_component_p (t))
10230 t = TREE_OPERAND (t, 0);
10231 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10232 return;
10233 TREE_ADDRESSABLE (t) = 1;
10238 /* Used to help initialize the builtin-types.def table. When a type of
10239 the correct size doesn't exist, use error_mark_node instead of NULL.
10240 The later results in segfaults even when a decl using the type doesn't
10241 get invoked. */
10243 tree
10244 builtin_type_for_size (int size, bool unsignedp)
10246 tree type = c_common_type_for_size (size, unsignedp);
10247 return type ? type : error_mark_node;
10250 /* A helper function for resolve_overloaded_builtin in resolving the
10251 overloaded __sync_ builtins. Returns a positive power of 2 if the
10252 first operand of PARAMS is a pointer to a supported data type.
10253 Returns 0 if an error is encountered. */
10255 static int
10256 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10258 tree type;
10259 int size;
10261 if (!params)
10263 error ("too few arguments to function %qE", function);
10264 return 0;
10267 type = TREE_TYPE ((*params)[0]);
10268 if (TREE_CODE (type) == ARRAY_TYPE)
10270 /* Force array-to-pointer decay for C++. */
10271 gcc_assert (c_dialect_cxx());
10272 (*params)[0] = default_conversion ((*params)[0]);
10273 type = TREE_TYPE ((*params)[0]);
10275 if (TREE_CODE (type) != POINTER_TYPE)
10276 goto incompatible;
10278 type = TREE_TYPE (type);
10279 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10280 goto incompatible;
10282 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10283 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10284 return size;
10286 incompatible:
10287 error ("incompatible type for argument %d of %qE", 1, function);
10288 return 0;
10291 /* A helper function for resolve_overloaded_builtin. Adds casts to
10292 PARAMS to make arguments match up with those of FUNCTION. Drops
10293 the variadic arguments at the end. Returns false if some error
10294 was encountered; true on success. */
10296 static bool
10297 sync_resolve_params (location_t loc, tree orig_function, tree function,
10298 vec<tree, va_gc> *params, bool orig_format)
10300 function_args_iterator iter;
10301 tree ptype;
10302 unsigned int parmnum;
10304 function_args_iter_init (&iter, TREE_TYPE (function));
10305 /* We've declared the implementation functions to use "volatile void *"
10306 as the pointer parameter, so we shouldn't get any complaints from the
10307 call to check_function_arguments what ever type the user used. */
10308 function_args_iter_next (&iter);
10309 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10310 ptype = TYPE_MAIN_VARIANT (ptype);
10312 /* For the rest of the values, we need to cast these to FTYPE, so that we
10313 don't get warnings for passing pointer types, etc. */
10314 parmnum = 0;
10315 while (1)
10317 tree val, arg_type;
10319 arg_type = function_args_iter_cond (&iter);
10320 /* XXX void_type_node belies the abstraction. */
10321 if (arg_type == void_type_node)
10322 break;
10324 ++parmnum;
10325 if (params->length () <= parmnum)
10327 error_at (loc, "too few arguments to function %qE", orig_function);
10328 return false;
10331 /* Only convert parameters if arg_type is unsigned integer type with
10332 new format sync routines, i.e. don't attempt to convert pointer
10333 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10334 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10335 kinds). */
10336 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10338 /* Ideally for the first conversion we'd use convert_for_assignment
10339 so that we get warnings for anything that doesn't match the pointer
10340 type. This isn't portable across the C and C++ front ends atm. */
10341 val = (*params)[parmnum];
10342 val = convert (ptype, val);
10343 val = convert (arg_type, val);
10344 (*params)[parmnum] = val;
10347 function_args_iter_next (&iter);
10350 /* __atomic routines are not variadic. */
10351 if (!orig_format && params->length () != parmnum + 1)
10353 error_at (loc, "too many arguments to function %qE", orig_function);
10354 return false;
10357 /* The definition of these primitives is variadic, with the remaining
10358 being "an optional list of variables protected by the memory barrier".
10359 No clue what that's supposed to mean, precisely, but we consider all
10360 call-clobbered variables to be protected so we're safe. */
10361 params->truncate (parmnum + 1);
10363 return true;
10366 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10367 RESULT to make it match the type of the first pointer argument in
10368 PARAMS. */
10370 static tree
10371 sync_resolve_return (tree first_param, tree result, bool orig_format)
10373 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10374 tree rtype = TREE_TYPE (result);
10375 ptype = TYPE_MAIN_VARIANT (ptype);
10377 /* New format doesn't require casting unless the types are the same size. */
10378 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10379 return convert (ptype, result);
10380 else
10381 return result;
10384 /* This function verifies the PARAMS to generic atomic FUNCTION.
10385 It returns the size if all the parameters are the same size, otherwise
10386 0 is returned if the parameters are invalid. */
10388 static int
10389 get_atomic_generic_size (location_t loc, tree function,
10390 vec<tree, va_gc> *params)
10392 unsigned int n_param;
10393 unsigned int n_model;
10394 unsigned int x;
10395 int size_0;
10396 tree type_0;
10398 /* Determine the parameter makeup. */
10399 switch (DECL_FUNCTION_CODE (function))
10401 case BUILT_IN_ATOMIC_EXCHANGE:
10402 n_param = 4;
10403 n_model = 1;
10404 break;
10405 case BUILT_IN_ATOMIC_LOAD:
10406 case BUILT_IN_ATOMIC_STORE:
10407 n_param = 3;
10408 n_model = 1;
10409 break;
10410 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10411 n_param = 6;
10412 n_model = 2;
10413 break;
10414 default:
10415 gcc_unreachable ();
10418 if (vec_safe_length (params) != n_param)
10420 error_at (loc, "incorrect number of arguments to function %qE", function);
10421 return 0;
10424 /* Get type of first parameter, and determine its size. */
10425 type_0 = TREE_TYPE ((*params)[0]);
10426 if (TREE_CODE (type_0) == ARRAY_TYPE)
10428 /* Force array-to-pointer decay for C++. */
10429 gcc_assert (c_dialect_cxx());
10430 (*params)[0] = default_conversion ((*params)[0]);
10431 type_0 = TREE_TYPE ((*params)[0]);
10433 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10435 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10436 function);
10437 return 0;
10440 /* Types must be compile time constant sizes. */
10441 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10443 error_at (loc,
10444 "argument 1 of %qE must be a pointer to a constant size type",
10445 function);
10446 return 0;
10449 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10451 /* Zero size objects are not allowed. */
10452 if (size_0 == 0)
10454 error_at (loc,
10455 "argument 1 of %qE must be a pointer to a nonzero size object",
10456 function);
10457 return 0;
10460 /* Check each other parameter is a pointer and the same size. */
10461 for (x = 0; x < n_param - n_model; x++)
10463 int size;
10464 tree type = TREE_TYPE ((*params)[x]);
10465 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10466 if (n_param == 6 && x == 3)
10467 continue;
10468 if (!POINTER_TYPE_P (type))
10470 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10471 function);
10472 return 0;
10474 size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10475 if (size != size_0)
10477 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10478 function);
10479 return 0;
10483 /* Check memory model parameters for validity. */
10484 for (x = n_param - n_model ; x < n_param; x++)
10486 tree p = (*params)[x];
10487 if (TREE_CODE (p) == INTEGER_CST)
10489 int i = tree_to_uhwi (p);
10490 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10492 warning_at (loc, OPT_Winvalid_memory_model,
10493 "invalid memory model argument %d of %qE", x + 1,
10494 function);
10497 else
10498 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10500 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10501 function);
10502 return 0;
10506 return size_0;
10510 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10511 at the beginning of the parameter list PARAMS representing the size of the
10512 objects. This is to match the library ABI requirement. LOC is the location
10513 of the function call.
10514 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10515 returned to allow the external call to be constructed. */
10517 static tree
10518 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10519 vec<tree, va_gc> *params)
10521 tree size_node;
10523 /* Insert a SIZE_T parameter as the first param. If there isn't
10524 enough space, allocate a new vector and recursively re-build with that. */
10525 if (!params->space (1))
10527 unsigned int z, len;
10528 vec<tree, va_gc> *v;
10529 tree f;
10531 len = params->length ();
10532 vec_alloc (v, len + 1);
10533 v->quick_push (build_int_cst (size_type_node, n));
10534 for (z = 0; z < len; z++)
10535 v->quick_push ((*params)[z]);
10536 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10537 vec_free (v);
10538 return f;
10541 /* Add the size parameter and leave as a function call for processing. */
10542 size_node = build_int_cst (size_type_node, n);
10543 params->quick_insert (0, size_node);
10544 return NULL_TREE;
10548 /* Return whether atomic operations for naturally aligned N-byte
10549 arguments are supported, whether inline or through libatomic. */
10550 static bool
10551 atomic_size_supported_p (int n)
10553 switch (n)
10555 case 1:
10556 case 2:
10557 case 4:
10558 case 8:
10559 return true;
10561 case 16:
10562 return targetm.scalar_mode_supported_p (TImode);
10564 default:
10565 return false;
10569 /* This will process an __atomic_exchange function call, determine whether it
10570 needs to be mapped to the _N variation, or turned into a library call.
10571 LOC is the location of the builtin call.
10572 FUNCTION is the DECL that has been invoked;
10573 PARAMS is the argument list for the call. The return value is non-null
10574 TRUE is returned if it is translated into the proper format for a call to the
10575 external library, and NEW_RETURN is set the tree for that function.
10576 FALSE is returned if processing for the _N variation is required, and
10577 NEW_RETURN is set to the the return value the result is copied into. */
10578 static bool
10579 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10580 vec<tree, va_gc> *params, tree *new_return)
10582 tree p0, p1, p2, p3;
10583 tree I_type, I_type_ptr;
10584 int n = get_atomic_generic_size (loc, function, params);
10586 /* Size of 0 is an error condition. */
10587 if (n == 0)
10589 *new_return = error_mark_node;
10590 return true;
10593 /* If not a lock-free size, change to the library generic format. */
10594 if (!atomic_size_supported_p (n))
10596 *new_return = add_atomic_size_parameter (n, loc, function, params);
10597 return true;
10600 /* Otherwise there is a lockfree match, transform the call from:
10601 void fn(T* mem, T* desired, T* return, model)
10602 into
10603 *return = (T) (fn (In* mem, (In) *desired, model)) */
10605 p0 = (*params)[0];
10606 p1 = (*params)[1];
10607 p2 = (*params)[2];
10608 p3 = (*params)[3];
10610 /* Create pointer to appropriate size. */
10611 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10612 I_type_ptr = build_pointer_type (I_type);
10614 /* Convert object pointer to required type. */
10615 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10616 (*params)[0] = p0;
10617 /* Convert new value to required type, and dereference it. */
10618 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10619 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10620 (*params)[1] = p1;
10622 /* Move memory model to the 3rd position, and end param list. */
10623 (*params)[2] = p3;
10624 params->truncate (3);
10626 /* Convert return pointer and dereference it for later assignment. */
10627 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10629 return false;
10633 /* This will process an __atomic_compare_exchange function call, determine
10634 whether it needs to be mapped to the _N variation, or turned into a lib call.
10635 LOC is the location of the builtin call.
10636 FUNCTION is the DECL that has been invoked;
10637 PARAMS is the argument list for the call. The return value is non-null
10638 TRUE is returned if it is translated into the proper format for a call to the
10639 external library, and NEW_RETURN is set the tree for that function.
10640 FALSE is returned if processing for the _N variation is required. */
10642 static bool
10643 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10644 vec<tree, va_gc> *params,
10645 tree *new_return)
10647 tree p0, p1, p2;
10648 tree I_type, I_type_ptr;
10649 int n = get_atomic_generic_size (loc, function, params);
10651 /* Size of 0 is an error condition. */
10652 if (n == 0)
10654 *new_return = error_mark_node;
10655 return true;
10658 /* If not a lock-free size, change to the library generic format. */
10659 if (!atomic_size_supported_p (n))
10661 /* The library generic format does not have the weak parameter, so
10662 remove it from the param list. Since a parameter has been removed,
10663 we can be sure that there is room for the SIZE_T parameter, meaning
10664 there will not be a recursive rebuilding of the parameter list, so
10665 there is no danger this will be done twice. */
10666 if (n > 0)
10668 (*params)[3] = (*params)[4];
10669 (*params)[4] = (*params)[5];
10670 params->truncate (5);
10672 *new_return = add_atomic_size_parameter (n, loc, function, params);
10673 return true;
10676 /* Otherwise, there is a match, so the call needs to be transformed from:
10677 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10678 into
10679 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10681 p0 = (*params)[0];
10682 p1 = (*params)[1];
10683 p2 = (*params)[2];
10685 /* Create pointer to appropriate size. */
10686 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10687 I_type_ptr = build_pointer_type (I_type);
10689 /* Convert object pointer to required type. */
10690 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10691 (*params)[0] = p0;
10693 /* Convert expected pointer to required type. */
10694 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10695 (*params)[1] = p1;
10697 /* Convert desired value to required type, and dereference it. */
10698 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10699 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10700 (*params)[2] = p2;
10702 /* The rest of the parameters are fine. NULL means no special return value
10703 processing.*/
10704 *new_return = NULL;
10705 return false;
10709 /* This will process an __atomic_load function call, determine whether it
10710 needs to be mapped to the _N variation, or turned into a library call.
10711 LOC is the location of the builtin call.
10712 FUNCTION is the DECL that has been invoked;
10713 PARAMS is the argument list for the call. The return value is non-null
10714 TRUE is returned if it is translated into the proper format for a call to the
10715 external library, and NEW_RETURN is set the tree for that function.
10716 FALSE is returned if processing for the _N variation is required, and
10717 NEW_RETURN is set to the the return value the result is copied into. */
10719 static bool
10720 resolve_overloaded_atomic_load (location_t loc, tree function,
10721 vec<tree, va_gc> *params, tree *new_return)
10723 tree p0, p1, p2;
10724 tree I_type, I_type_ptr;
10725 int n = get_atomic_generic_size (loc, function, params);
10727 /* Size of 0 is an error condition. */
10728 if (n == 0)
10730 *new_return = error_mark_node;
10731 return true;
10734 /* If not a lock-free size, change to the library generic format. */
10735 if (!atomic_size_supported_p (n))
10737 *new_return = add_atomic_size_parameter (n, loc, function, params);
10738 return true;
10741 /* Otherwise, there is a match, so the call needs to be transformed from:
10742 void fn(T* mem, T* return, model)
10743 into
10744 *return = (T) (fn ((In *) mem, model)) */
10746 p0 = (*params)[0];
10747 p1 = (*params)[1];
10748 p2 = (*params)[2];
10750 /* Create pointer to appropriate size. */
10751 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10752 I_type_ptr = build_pointer_type (I_type);
10754 /* Convert object pointer to required type. */
10755 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10756 (*params)[0] = p0;
10758 /* Move memory model to the 2nd position, and end param list. */
10759 (*params)[1] = p2;
10760 params->truncate (2);
10762 /* Convert return pointer and dereference it for later assignment. */
10763 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10765 return false;
10769 /* This will process an __atomic_store function call, determine whether it
10770 needs to be mapped to the _N variation, or turned into a library call.
10771 LOC is the location of the builtin call.
10772 FUNCTION is the DECL that has been invoked;
10773 PARAMS is the argument list for the call. The return value is non-null
10774 TRUE is returned if it is translated into the proper format for a call to the
10775 external library, and NEW_RETURN is set the tree for that function.
10776 FALSE is returned if processing for the _N variation is required, and
10777 NEW_RETURN is set to the the return value the result is copied into. */
10779 static bool
10780 resolve_overloaded_atomic_store (location_t loc, tree function,
10781 vec<tree, va_gc> *params, tree *new_return)
10783 tree p0, p1;
10784 tree I_type, I_type_ptr;
10785 int n = get_atomic_generic_size (loc, function, params);
10787 /* Size of 0 is an error condition. */
10788 if (n == 0)
10790 *new_return = error_mark_node;
10791 return true;
10794 /* If not a lock-free size, change to the library generic format. */
10795 if (!atomic_size_supported_p (n))
10797 *new_return = add_atomic_size_parameter (n, loc, function, params);
10798 return true;
10801 /* Otherwise, there is a match, so the call needs to be transformed from:
10802 void fn(T* mem, T* value, model)
10803 into
10804 fn ((In *) mem, (In) *value, model) */
10806 p0 = (*params)[0];
10807 p1 = (*params)[1];
10809 /* Create pointer to appropriate size. */
10810 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10811 I_type_ptr = build_pointer_type (I_type);
10813 /* Convert object pointer to required type. */
10814 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10815 (*params)[0] = p0;
10817 /* Convert new value to required type, and dereference it. */
10818 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10819 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10820 (*params)[1] = p1;
10822 /* The memory model is in the right spot already. Return is void. */
10823 *new_return = NULL_TREE;
10825 return false;
10829 /* Some builtin functions are placeholders for other expressions. This
10830 function should be called immediately after parsing the call expression
10831 before surrounding code has committed to the type of the expression.
10833 LOC is the location of the builtin call.
10835 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10836 PARAMS is the argument list for the call. The return value is non-null
10837 when expansion is complete, and null if normal processing should
10838 continue. */
10840 tree
10841 resolve_overloaded_builtin (location_t loc, tree function,
10842 vec<tree, va_gc> *params)
10844 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10845 bool orig_format = true;
10846 tree new_return = NULL_TREE;
10848 switch (DECL_BUILT_IN_CLASS (function))
10850 case BUILT_IN_NORMAL:
10851 break;
10852 case BUILT_IN_MD:
10853 if (targetm.resolve_overloaded_builtin)
10854 return targetm.resolve_overloaded_builtin (loc, function, params);
10855 else
10856 return NULL_TREE;
10857 default:
10858 return NULL_TREE;
10861 /* Handle BUILT_IN_NORMAL here. */
10862 switch (orig_code)
10864 case BUILT_IN_ATOMIC_EXCHANGE:
10865 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10866 case BUILT_IN_ATOMIC_LOAD:
10867 case BUILT_IN_ATOMIC_STORE:
10869 /* Handle these 4 together so that they can fall through to the next
10870 case if the call is transformed to an _N variant. */
10871 switch (orig_code)
10873 case BUILT_IN_ATOMIC_EXCHANGE:
10875 if (resolve_overloaded_atomic_exchange (loc, function, params,
10876 &new_return))
10877 return new_return;
10878 /* Change to the _N variant. */
10879 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10880 break;
10883 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10885 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10886 params,
10887 &new_return))
10888 return new_return;
10889 /* Change to the _N variant. */
10890 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10891 break;
10893 case BUILT_IN_ATOMIC_LOAD:
10895 if (resolve_overloaded_atomic_load (loc, function, params,
10896 &new_return))
10897 return new_return;
10898 /* Change to the _N variant. */
10899 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10900 break;
10902 case BUILT_IN_ATOMIC_STORE:
10904 if (resolve_overloaded_atomic_store (loc, function, params,
10905 &new_return))
10906 return new_return;
10907 /* Change to the _N variant. */
10908 orig_code = BUILT_IN_ATOMIC_STORE_N;
10909 break;
10911 default:
10912 gcc_unreachable ();
10914 /* Fallthrough to the normal processing. */
10916 case BUILT_IN_ATOMIC_EXCHANGE_N:
10917 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10918 case BUILT_IN_ATOMIC_LOAD_N:
10919 case BUILT_IN_ATOMIC_STORE_N:
10920 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10921 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10922 case BUILT_IN_ATOMIC_AND_FETCH_N:
10923 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10924 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10925 case BUILT_IN_ATOMIC_OR_FETCH_N:
10926 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10927 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10928 case BUILT_IN_ATOMIC_FETCH_AND_N:
10929 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10930 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10931 case BUILT_IN_ATOMIC_FETCH_OR_N:
10933 orig_format = false;
10934 /* Fallthru for parameter processing. */
10936 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10937 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10938 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10939 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10940 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10941 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10942 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10943 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10944 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10945 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10946 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10947 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10948 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10949 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10950 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10951 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10953 int n = sync_resolve_size (function, params);
10954 tree new_function, first_param, result;
10955 enum built_in_function fncode;
10957 if (n == 0)
10958 return error_mark_node;
10960 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10961 new_function = builtin_decl_explicit (fncode);
10962 if (!sync_resolve_params (loc, function, new_function, params,
10963 orig_format))
10964 return error_mark_node;
10966 first_param = (*params)[0];
10967 result = build_function_call_vec (loc, vNULL, new_function, params,
10968 NULL);
10969 if (result == error_mark_node)
10970 return result;
10971 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10972 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10973 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10974 result = sync_resolve_return (first_param, result, orig_format);
10976 /* If new_return is set, assign function to that expr and cast the
10977 result to void since the generic interface returned void. */
10978 if (new_return)
10980 /* Cast function result from I{1,2,4,8,16} to the required type. */
10981 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10982 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10983 result);
10984 TREE_SIDE_EFFECTS (result) = 1;
10985 protected_set_expr_location (result, loc);
10986 result = convert (void_type_node, result);
10988 return result;
10991 default:
10992 return NULL_TREE;
10996 /* vector_types_compatible_elements_p is used in type checks of vectors
10997 values used as operands of binary operators. Where it returns true, and
10998 the other checks of the caller succeed (being vector types in he first
10999 place, and matching number of elements), we can just treat the types
11000 as essentially the same.
11001 Contrast with vector_targets_convertible_p, which is used for vector
11002 pointer types, and vector_types_convertible_p, which will allow
11003 language-specific matches under the control of flag_lax_vector_conversions,
11004 and might still require a conversion. */
11005 /* True if vector types T1 and T2 can be inputs to the same binary
11006 operator without conversion.
11007 We don't check the overall vector size here because some of our callers
11008 want to give different error messages when the vectors are compatible
11009 except for the element count. */
11011 bool
11012 vector_types_compatible_elements_p (tree t1, tree t2)
11014 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11015 t1 = TREE_TYPE (t1);
11016 t2 = TREE_TYPE (t2);
11018 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11020 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11021 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11022 || c2 == FIXED_POINT_TYPE));
11024 t1 = c_common_signed_type (t1);
11025 t2 = c_common_signed_type (t2);
11026 /* Equality works here because c_common_signed_type uses
11027 TYPE_MAIN_VARIANT. */
11028 if (t1 == t2)
11029 return true;
11030 if (opaque && c1 == c2
11031 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11032 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11033 return true;
11034 return false;
11037 /* Check for missing format attributes on function pointers. LTYPE is
11038 the new type or left-hand side type. RTYPE is the old type or
11039 right-hand side type. Returns TRUE if LTYPE is missing the desired
11040 attribute. */
11042 bool
11043 check_missing_format_attribute (tree ltype, tree rtype)
11045 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11046 tree ra;
11048 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11049 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11050 break;
11051 if (ra)
11053 tree la;
11054 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11055 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11056 break;
11057 return !la;
11059 else
11060 return false;
11063 /* Subscripting with type char is likely to lose on a machine where
11064 chars are signed. So warn on any machine, but optionally. Don't
11065 warn for unsigned char since that type is safe. Don't warn for
11066 signed char because anyone who uses that must have done so
11067 deliberately. Furthermore, we reduce the false positive load by
11068 warning only for non-constant value of type char. */
11070 void
11071 warn_array_subscript_with_type_char (tree index)
11073 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11074 && TREE_CODE (index) != INTEGER_CST)
11075 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11078 /* Implement -Wparentheses for the unexpected C precedence rules, to
11079 cover cases like x + y << z which readers are likely to
11080 misinterpret. We have seen an expression in which CODE is a binary
11081 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11082 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11083 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11084 expression was not formed using a binary or unary operator, or it
11085 was enclosed in parentheses. */
11087 void
11088 warn_about_parentheses (location_t loc, enum tree_code code,
11089 enum tree_code code_left, tree arg_left,
11090 enum tree_code code_right, tree arg_right)
11092 if (!warn_parentheses)
11093 return;
11095 /* This macro tests that the expression ARG with original tree code
11096 CODE appears to be a boolean expression. or the result of folding a
11097 boolean expression. */
11098 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11099 (truth_value_p (TREE_CODE (ARG)) \
11100 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11101 /* Folding may create 0 or 1 integers from other expressions. */ \
11102 || ((CODE) != INTEGER_CST \
11103 && (integer_onep (ARG) || integer_zerop (ARG))))
11105 switch (code)
11107 case LSHIFT_EXPR:
11108 if (code_left == PLUS_EXPR)
11109 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11110 "suggest parentheses around %<+%> inside %<<<%>");
11111 else if (code_right == PLUS_EXPR)
11112 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11113 "suggest parentheses around %<+%> inside %<<<%>");
11114 else if (code_left == MINUS_EXPR)
11115 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11116 "suggest parentheses around %<-%> inside %<<<%>");
11117 else if (code_right == MINUS_EXPR)
11118 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11119 "suggest parentheses around %<-%> inside %<<<%>");
11120 return;
11122 case RSHIFT_EXPR:
11123 if (code_left == PLUS_EXPR)
11124 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11125 "suggest parentheses around %<+%> inside %<>>%>");
11126 else if (code_right == PLUS_EXPR)
11127 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11128 "suggest parentheses around %<+%> inside %<>>%>");
11129 else if (code_left == MINUS_EXPR)
11130 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11131 "suggest parentheses around %<-%> inside %<>>%>");
11132 else if (code_right == MINUS_EXPR)
11133 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11134 "suggest parentheses around %<-%> inside %<>>%>");
11135 return;
11137 case TRUTH_ORIF_EXPR:
11138 if (code_left == TRUTH_ANDIF_EXPR)
11139 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11140 "suggest parentheses around %<&&%> within %<||%>");
11141 else if (code_right == TRUTH_ANDIF_EXPR)
11142 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11143 "suggest parentheses around %<&&%> within %<||%>");
11144 return;
11146 case BIT_IOR_EXPR:
11147 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11148 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11149 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11150 "suggest parentheses around arithmetic in operand of %<|%>");
11151 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11152 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11153 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11154 "suggest parentheses around arithmetic in operand of %<|%>");
11155 /* Check cases like x|y==z */
11156 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11157 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11158 "suggest parentheses around comparison in operand of %<|%>");
11159 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11160 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11161 "suggest parentheses around comparison in operand of %<|%>");
11162 /* Check cases like !x | y */
11163 else if (code_left == TRUTH_NOT_EXPR
11164 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11165 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11166 "suggest parentheses around operand of "
11167 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11168 return;
11170 case BIT_XOR_EXPR:
11171 if (code_left == BIT_AND_EXPR
11172 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11173 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11174 "suggest parentheses around arithmetic in operand of %<^%>");
11175 else if (code_right == BIT_AND_EXPR
11176 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11177 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11178 "suggest parentheses around arithmetic in operand of %<^%>");
11179 /* Check cases like x^y==z */
11180 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11181 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11182 "suggest parentheses around comparison in operand of %<^%>");
11183 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11184 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11185 "suggest parentheses around comparison in operand of %<^%>");
11186 return;
11188 case BIT_AND_EXPR:
11189 if (code_left == PLUS_EXPR)
11190 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11191 "suggest parentheses around %<+%> in operand of %<&%>");
11192 else if (code_right == PLUS_EXPR)
11193 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11194 "suggest parentheses around %<+%> in operand of %<&%>");
11195 else if (code_left == MINUS_EXPR)
11196 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11197 "suggest parentheses around %<-%> in operand of %<&%>");
11198 else if (code_right == MINUS_EXPR)
11199 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11200 "suggest parentheses around %<-%> in operand of %<&%>");
11201 /* Check cases like x&y==z */
11202 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11203 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11204 "suggest parentheses around comparison in operand of %<&%>");
11205 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11206 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11207 "suggest parentheses around comparison in operand of %<&%>");
11208 /* Check cases like !x & y */
11209 else if (code_left == TRUTH_NOT_EXPR
11210 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11211 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11212 "suggest parentheses around operand of "
11213 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11214 return;
11216 case EQ_EXPR:
11217 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11218 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11219 "suggest parentheses around comparison in operand of %<==%>");
11220 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11221 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11222 "suggest parentheses around comparison in operand of %<==%>");
11223 return;
11224 case NE_EXPR:
11225 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11226 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11227 "suggest parentheses around comparison in operand of %<!=%>");
11228 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11229 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11230 "suggest parentheses around comparison in operand of %<!=%>");
11231 return;
11233 default:
11234 if (TREE_CODE_CLASS (code) == tcc_comparison)
11236 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11237 && code_left != NE_EXPR && code_left != EQ_EXPR
11238 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11239 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11240 "comparisons like %<X<=Y<=Z%> do not "
11241 "have their mathematical meaning");
11242 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11243 && code_right != NE_EXPR && code_right != EQ_EXPR
11244 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11245 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11246 "comparisons like %<X<=Y<=Z%> do not "
11247 "have their mathematical meaning");
11249 return;
11251 #undef NOT_A_BOOLEAN_EXPR_P
11254 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11256 void
11257 warn_for_unused_label (tree label)
11259 if (!TREE_USED (label))
11261 if (DECL_INITIAL (label))
11262 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11263 else
11264 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11268 /* Warn for division by zero according to the value of DIVISOR. LOC
11269 is the location of the division operator. */
11271 void
11272 warn_for_div_by_zero (location_t loc, tree divisor)
11274 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11275 about division by zero. Do not issue a warning if DIVISOR has a
11276 floating-point type, since we consider 0.0/0.0 a valid way of
11277 generating a NaN. */
11278 if (c_inhibit_evaluation_warnings == 0
11279 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11280 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11283 /* Subroutine of build_binary_op. Give warnings for comparisons
11284 between signed and unsigned quantities that may fail. Do the
11285 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11286 so that casts will be considered, but default promotions won't
11289 LOCATION is the location of the comparison operator.
11291 The arguments of this function map directly to local variables
11292 of build_binary_op. */
11294 void
11295 warn_for_sign_compare (location_t location,
11296 tree orig_op0, tree orig_op1,
11297 tree op0, tree op1,
11298 tree result_type, enum tree_code resultcode)
11300 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11301 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11302 int unsignedp0, unsignedp1;
11304 /* In C++, check for comparison of different enum types. */
11305 if (c_dialect_cxx()
11306 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11307 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11308 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11309 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11311 warning_at (location,
11312 OPT_Wsign_compare, "comparison between types %qT and %qT",
11313 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11316 /* Do not warn if the comparison is being done in a signed type,
11317 since the signed type will only be chosen if it can represent
11318 all the values of the unsigned type. */
11319 if (!TYPE_UNSIGNED (result_type))
11320 /* OK */;
11321 /* Do not warn if both operands are unsigned. */
11322 else if (op0_signed == op1_signed)
11323 /* OK */;
11324 else
11326 tree sop, uop, base_type;
11327 bool ovf;
11329 if (op0_signed)
11330 sop = orig_op0, uop = orig_op1;
11331 else
11332 sop = orig_op1, uop = orig_op0;
11334 STRIP_TYPE_NOPS (sop);
11335 STRIP_TYPE_NOPS (uop);
11336 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11337 ? TREE_TYPE (result_type) : result_type);
11339 /* Do not warn if the signed quantity is an unsuffixed integer
11340 literal (or some static constant expression involving such
11341 literals or a conditional expression involving such literals)
11342 and it is non-negative. */
11343 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11344 /* OK */;
11345 /* Do not warn if the comparison is an equality operation, the
11346 unsigned quantity is an integral constant, and it would fit
11347 in the result if the result were signed. */
11348 else if (TREE_CODE (uop) == INTEGER_CST
11349 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11350 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11351 /* OK */;
11352 /* In C, do not warn if the unsigned quantity is an enumeration
11353 constant and its maximum value would fit in the result if the
11354 result were signed. */
11355 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11356 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11357 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11358 c_common_signed_type (base_type)))
11359 /* OK */;
11360 else
11361 warning_at (location,
11362 OPT_Wsign_compare,
11363 "comparison between signed and unsigned integer expressions");
11366 /* Warn if two unsigned values are being compared in a size larger
11367 than their original size, and one (and only one) is the result of
11368 a `~' operator. This comparison will always fail.
11370 Also warn if one operand is a constant, and the constant does not
11371 have all bits set that are set in the ~ operand when it is
11372 extended. */
11374 op0 = c_common_get_narrower (op0, &unsignedp0);
11375 op1 = c_common_get_narrower (op1, &unsignedp1);
11377 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11378 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11380 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11381 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11382 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11383 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11385 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11387 tree primop;
11388 HOST_WIDE_INT constant, mask;
11389 int unsignedp;
11390 unsigned int bits;
11392 if (tree_fits_shwi_p (op0))
11394 primop = op1;
11395 unsignedp = unsignedp1;
11396 constant = tree_to_shwi (op0);
11398 else
11400 primop = op0;
11401 unsignedp = unsignedp0;
11402 constant = tree_to_shwi (op1);
11405 bits = TYPE_PRECISION (TREE_TYPE (primop));
11406 if (bits < TYPE_PRECISION (result_type)
11407 && bits < HOST_BITS_PER_LONG && unsignedp)
11409 mask = (~ (HOST_WIDE_INT) 0) << bits;
11410 if ((mask & constant) != mask)
11412 if (constant == 0)
11413 warning_at (location, OPT_Wsign_compare,
11414 "promoted ~unsigned is always non-zero");
11415 else
11416 warning_at (location, OPT_Wsign_compare,
11417 "comparison of promoted ~unsigned with constant");
11421 else if (unsignedp0 && unsignedp1
11422 && (TYPE_PRECISION (TREE_TYPE (op0))
11423 < TYPE_PRECISION (result_type))
11424 && (TYPE_PRECISION (TREE_TYPE (op1))
11425 < TYPE_PRECISION (result_type)))
11426 warning_at (location, OPT_Wsign_compare,
11427 "comparison of promoted ~unsigned with unsigned");
11431 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11432 type via c_common_type. If -Wdouble-promotion is in use, and the
11433 conditions for warning have been met, issue a warning. GMSGID is
11434 the warning message. It must have two %T specifiers for the type
11435 that was converted (generally "float") and the type to which it was
11436 converted (generally "double), respectively. LOC is the location
11437 to which the awrning should refer. */
11439 void
11440 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11441 const char *gmsgid, location_t loc)
11443 tree source_type;
11445 if (!warn_double_promotion)
11446 return;
11447 /* If the conversion will not occur at run-time, there is no need to
11448 warn about it. */
11449 if (c_inhibit_evaluation_warnings)
11450 return;
11451 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11452 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11453 return;
11454 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11455 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11456 source_type = type1;
11457 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11458 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11459 source_type = type2;
11460 else
11461 return;
11462 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11465 /* Setup a TYPE_DECL node as a typedef representation.
11467 X is a TYPE_DECL for a typedef statement. Create a brand new
11468 ..._TYPE node (which will be just a variant of the existing
11469 ..._TYPE node with identical properties) and then install X
11470 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11472 The whole point here is to end up with a situation where each
11473 and every ..._TYPE node the compiler creates will be uniquely
11474 associated with AT MOST one node representing a typedef name.
11475 This way, even though the compiler substitutes corresponding
11476 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11477 early on, later parts of the compiler can always do the reverse
11478 translation and get back the corresponding typedef name. For
11479 example, given:
11481 typedef struct S MY_TYPE;
11482 MY_TYPE object;
11484 Later parts of the compiler might only know that `object' was of
11485 type `struct S' if it were not for code just below. With this
11486 code however, later parts of the compiler see something like:
11488 struct S' == struct S
11489 typedef struct S' MY_TYPE;
11490 struct S' object;
11492 And they can then deduce (from the node for type struct S') that
11493 the original object declaration was:
11495 MY_TYPE object;
11497 Being able to do this is important for proper support of protoize,
11498 and also for generating precise symbolic debugging information
11499 which takes full account of the programmer's (typedef) vocabulary.
11501 Obviously, we don't want to generate a duplicate ..._TYPE node if
11502 the TYPE_DECL node that we are now processing really represents a
11503 standard built-in type. */
11505 void
11506 set_underlying_type (tree x)
11508 if (x == error_mark_node)
11509 return;
11510 if (DECL_IS_BUILTIN (x))
11512 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11513 TYPE_NAME (TREE_TYPE (x)) = x;
11515 else if (TREE_TYPE (x) != error_mark_node
11516 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11518 tree tt = TREE_TYPE (x);
11519 DECL_ORIGINAL_TYPE (x) = tt;
11520 tt = build_variant_type_copy (tt);
11521 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11522 TYPE_NAME (tt) = x;
11523 TREE_USED (tt) = TREE_USED (x);
11524 TREE_TYPE (x) = tt;
11528 /* Record the types used by the current global variable declaration
11529 being parsed, so that we can decide later to emit their debug info.
11530 Those types are in types_used_by_cur_var_decl, and we are going to
11531 store them in the types_used_by_vars_hash hash table.
11532 DECL is the declaration of the global variable that has been parsed. */
11534 void
11535 record_types_used_by_current_var_decl (tree decl)
11537 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11539 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11541 tree type = types_used_by_cur_var_decl->pop ();
11542 types_used_by_var_decl_insert (type, decl);
11546 /* If DECL is a typedef that is declared in the current function,
11547 record it for the purpose of -Wunused-local-typedefs. */
11549 void
11550 record_locally_defined_typedef (tree decl)
11552 struct c_language_function *l;
11554 if (!warn_unused_local_typedefs
11555 || cfun == NULL
11556 /* if this is not a locally defined typedef then we are not
11557 interested. */
11558 || !is_typedef_decl (decl)
11559 || !decl_function_context (decl))
11560 return;
11562 l = (struct c_language_function *) cfun->language;
11563 vec_safe_push (l->local_typedefs, decl);
11566 /* If T is a TYPE_DECL declared locally, mark it as used. */
11568 void
11569 maybe_record_typedef_use (tree t)
11571 if (!is_typedef_decl (t))
11572 return;
11574 TREE_USED (t) = true;
11577 /* Warn if there are some unused locally defined typedefs in the
11578 current function. */
11580 void
11581 maybe_warn_unused_local_typedefs (void)
11583 int i;
11584 tree decl;
11585 /* The number of times we have emitted -Wunused-local-typedefs
11586 warnings. If this is different from errorcount, that means some
11587 unrelated errors have been issued. In which case, we'll avoid
11588 emitting "unused-local-typedefs" warnings. */
11589 static int unused_local_typedefs_warn_count;
11590 struct c_language_function *l;
11592 if (cfun == NULL)
11593 return;
11595 if ((l = (struct c_language_function *) cfun->language) == NULL)
11596 return;
11598 if (warn_unused_local_typedefs
11599 && errorcount == unused_local_typedefs_warn_count)
11601 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11602 if (!TREE_USED (decl))
11603 warning_at (DECL_SOURCE_LOCATION (decl),
11604 OPT_Wunused_local_typedefs,
11605 "typedef %qD locally defined but not used", decl);
11606 unused_local_typedefs_warn_count = errorcount;
11609 vec_free (l->local_typedefs);
11612 /* The C and C++ parsers both use vectors to hold function arguments.
11613 For efficiency, we keep a cache of unused vectors. This is the
11614 cache. */
11616 typedef vec<tree, va_gc> *tree_gc_vec;
11617 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11619 /* Return a new vector from the cache. If the cache is empty,
11620 allocate a new vector. These vectors are GC'ed, so it is OK if the
11621 pointer is not released.. */
11623 vec<tree, va_gc> *
11624 make_tree_vector (void)
11626 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11627 return tree_vector_cache->pop ();
11628 else
11630 /* Passing 0 to vec::alloc returns NULL, and our callers require
11631 that we always return a non-NULL value. The vector code uses
11632 4 when growing a NULL vector, so we do too. */
11633 vec<tree, va_gc> *v;
11634 vec_alloc (v, 4);
11635 return v;
11639 /* Release a vector of trees back to the cache. */
11641 void
11642 release_tree_vector (vec<tree, va_gc> *vec)
11644 if (vec != NULL)
11646 vec->truncate (0);
11647 vec_safe_push (tree_vector_cache, vec);
11651 /* Get a new tree vector holding a single tree. */
11653 vec<tree, va_gc> *
11654 make_tree_vector_single (tree t)
11656 vec<tree, va_gc> *ret = make_tree_vector ();
11657 ret->quick_push (t);
11658 return ret;
11661 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11663 vec<tree, va_gc> *
11664 make_tree_vector_from_list (tree list)
11666 vec<tree, va_gc> *ret = make_tree_vector ();
11667 for (; list; list = TREE_CHAIN (list))
11668 vec_safe_push (ret, TREE_VALUE (list));
11669 return ret;
11672 /* Get a new tree vector which is a copy of an existing one. */
11674 vec<tree, va_gc> *
11675 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11677 vec<tree, va_gc> *ret;
11678 unsigned int ix;
11679 tree t;
11681 ret = make_tree_vector ();
11682 vec_safe_reserve (ret, vec_safe_length (orig));
11683 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11684 ret->quick_push (t);
11685 return ret;
11688 /* Return true if KEYWORD starts a type specifier. */
11690 bool
11691 keyword_begins_type_specifier (enum rid keyword)
11693 switch (keyword)
11695 case RID_AUTO_TYPE:
11696 case RID_INT:
11697 case RID_CHAR:
11698 case RID_FLOAT:
11699 case RID_DOUBLE:
11700 case RID_VOID:
11701 case RID_INT128:
11702 case RID_UNSIGNED:
11703 case RID_LONG:
11704 case RID_SHORT:
11705 case RID_SIGNED:
11706 case RID_DFLOAT32:
11707 case RID_DFLOAT64:
11708 case RID_DFLOAT128:
11709 case RID_FRACT:
11710 case RID_ACCUM:
11711 case RID_BOOL:
11712 case RID_WCHAR:
11713 case RID_CHAR16:
11714 case RID_CHAR32:
11715 case RID_SAT:
11716 case RID_COMPLEX:
11717 case RID_TYPEOF:
11718 case RID_STRUCT:
11719 case RID_CLASS:
11720 case RID_UNION:
11721 case RID_ENUM:
11722 return true;
11723 default:
11724 return false;
11728 /* Return true if KEYWORD names a type qualifier. */
11730 bool
11731 keyword_is_type_qualifier (enum rid keyword)
11733 switch (keyword)
11735 case RID_CONST:
11736 case RID_VOLATILE:
11737 case RID_RESTRICT:
11738 case RID_ATOMIC:
11739 return true;
11740 default:
11741 return false;
11745 /* Return true if KEYWORD names a storage class specifier.
11747 RID_TYPEDEF is not included in this list despite `typedef' being
11748 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11749 such for syntactic convenience only. */
11751 bool
11752 keyword_is_storage_class_specifier (enum rid keyword)
11754 switch (keyword)
11756 case RID_STATIC:
11757 case RID_EXTERN:
11758 case RID_REGISTER:
11759 case RID_AUTO:
11760 case RID_MUTABLE:
11761 case RID_THREAD:
11762 return true;
11763 default:
11764 return false;
11768 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11770 static bool
11771 keyword_is_function_specifier (enum rid keyword)
11773 switch (keyword)
11775 case RID_INLINE:
11776 case RID_NORETURN:
11777 case RID_VIRTUAL:
11778 case RID_EXPLICIT:
11779 return true;
11780 default:
11781 return false;
11785 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11786 declaration-specifier (C99 6.7). */
11788 bool
11789 keyword_is_decl_specifier (enum rid keyword)
11791 if (keyword_is_storage_class_specifier (keyword)
11792 || keyword_is_type_qualifier (keyword)
11793 || keyword_is_function_specifier (keyword))
11794 return true;
11796 switch (keyword)
11798 case RID_TYPEDEF:
11799 case RID_FRIEND:
11800 case RID_CONSTEXPR:
11801 return true;
11802 default:
11803 return false;
11807 /* Initialize language-specific-bits of tree_contains_struct. */
11809 void
11810 c_common_init_ts (void)
11812 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11813 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11814 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11817 /* Build a user-defined numeric literal out of an integer constant type VALUE
11818 with identifier SUFFIX. */
11820 tree
11821 build_userdef_literal (tree suffix_id, tree value,
11822 enum overflow_type overflow, tree num_string)
11824 tree literal = make_node (USERDEF_LITERAL);
11825 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11826 USERDEF_LITERAL_VALUE (literal) = value;
11827 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11828 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11829 return literal;
11832 /* For vector[index], convert the vector to a
11833 pointer of the underlying type. */
11834 void
11835 convert_vector_to_pointer_for_subscript (location_t loc,
11836 tree* vecp, tree index)
11838 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11840 tree type = TREE_TYPE (*vecp);
11841 tree type1;
11843 if (TREE_CODE (index) == INTEGER_CST)
11844 if (!tree_fits_uhwi_p (index)
11845 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
11846 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11848 c_common_mark_addressable_vec (*vecp);
11849 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11850 type1 = build_pointer_type (TREE_TYPE (*vecp));
11851 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
11852 if (!ref_all
11853 && !DECL_P (*vecp))
11855 /* If the original vector isn't declared may_alias and it
11856 isn't a bare vector look if the subscripting would
11857 alias the vector we subscript, and if not, force ref-all. */
11858 alias_set_type vecset = get_alias_set (*vecp);
11859 alias_set_type sset = get_alias_set (type);
11860 if (!alias_sets_must_conflict_p (sset, vecset)
11861 && !alias_set_subset_of (sset, vecset))
11862 ref_all = true;
11864 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
11865 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11866 *vecp = convert (type, *vecp);
11870 /* Determine which of the operands, if any, is a scalar that needs to be
11871 converted to a vector, for the range of operations. */
11872 enum stv_conv
11873 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11874 bool complain)
11876 tree type0 = TREE_TYPE (op0);
11877 tree type1 = TREE_TYPE (op1);
11878 bool integer_only_op = false;
11879 enum stv_conv ret = stv_firstarg;
11881 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11882 || TREE_CODE (type1) == VECTOR_TYPE);
11883 switch (code)
11885 /* Most GENERIC binary expressions require homogeneous arguments.
11886 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11887 argument that is a vector and a second one that is a scalar, so
11888 we never return stv_secondarg for them. */
11889 case RSHIFT_EXPR:
11890 case LSHIFT_EXPR:
11891 if (TREE_CODE (type0) == INTEGER_TYPE
11892 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11894 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11896 if (complain)
11897 error_at (loc, "conversion of scalar %qT to vector %qT "
11898 "involves truncation", type0, type1);
11899 return stv_error;
11901 else
11902 return stv_firstarg;
11904 break;
11906 case BIT_IOR_EXPR:
11907 case BIT_XOR_EXPR:
11908 case BIT_AND_EXPR:
11909 integer_only_op = true;
11910 /* ... fall through ... */
11912 case VEC_COND_EXPR:
11914 case PLUS_EXPR:
11915 case MINUS_EXPR:
11916 case MULT_EXPR:
11917 case TRUNC_DIV_EXPR:
11918 case CEIL_DIV_EXPR:
11919 case FLOOR_DIV_EXPR:
11920 case ROUND_DIV_EXPR:
11921 case EXACT_DIV_EXPR:
11922 case TRUNC_MOD_EXPR:
11923 case FLOOR_MOD_EXPR:
11924 case RDIV_EXPR:
11925 case EQ_EXPR:
11926 case NE_EXPR:
11927 case LE_EXPR:
11928 case GE_EXPR:
11929 case LT_EXPR:
11930 case GT_EXPR:
11931 /* What about UNLT_EXPR? */
11932 if (TREE_CODE (type0) == VECTOR_TYPE)
11934 tree tmp;
11935 ret = stv_secondarg;
11936 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11937 tmp = type0; type0 = type1; type1 = tmp;
11938 tmp = op0; op0 = op1; op1 = tmp;
11941 if (TREE_CODE (type0) == INTEGER_TYPE
11942 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11944 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11946 if (complain)
11947 error_at (loc, "conversion of scalar %qT to vector %qT "
11948 "involves truncation", type0, type1);
11949 return stv_error;
11951 return ret;
11953 else if (!integer_only_op
11954 /* Allow integer --> real conversion if safe. */
11955 && (TREE_CODE (type0) == REAL_TYPE
11956 || TREE_CODE (type0) == INTEGER_TYPE)
11957 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11959 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11961 if (complain)
11962 error_at (loc, "conversion of scalar %qT to vector %qT "
11963 "involves truncation", type0, type1);
11964 return stv_error;
11966 return ret;
11968 default:
11969 break;
11972 return stv_nothing;
11975 /* Return true iff ALIGN is an integral constant that is a fundamental
11976 alignment, as defined by [basic.align] in the c++-11
11977 specifications.
11979 That is:
11981 [A fundamental alignment is represented by an alignment less than or
11982 equal to the greatest alignment supported by the implementation
11983 in all contexts, which is equal to
11984 alignof(max_align_t)]. */
11986 bool
11987 cxx_fundamental_alignment_p (unsigned align)
11989 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11990 TYPE_ALIGN (long_double_type_node)));
11993 /* Return true if T is a pointer to a zero-sized aggregate. */
11995 bool
11996 pointer_to_zero_sized_aggr_p (tree t)
11998 if (!POINTER_TYPE_P (t))
11999 return false;
12000 t = TREE_TYPE (t);
12001 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12004 #include "gt-c-family-c-common.h"