1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2015 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
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
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 #define GCC_C_COMMON_C
24 #include "coretypes.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
33 #include "stringpool.h"
36 #include "trans-mem.h"
44 #include "common/common-target.h"
45 #include "langhooks.h"
46 #include "tree-inline.h"
48 #include "diagnostic.h"
49 #include "tree-iterator.h"
54 #include "plugin-api.h"
58 #include "hard-reg-set.h"
63 #include "target-def.h"
65 #include "wide-int-print.h"
66 #include "gimple-expr.h"
68 cpp_reader
*parse_in
; /* Declared in c-pragma.h. */
70 /* Mode used to build pointers (VOIDmode means ptr_mode). */
72 machine_mode c_default_pointer_mode
= VOIDmode
;
74 /* The following symbols are subsumed in the c_global_trees array, and
75 listed here individually for documentation purposes.
77 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
79 tree short_integer_type_node;
80 tree long_integer_type_node;
81 tree long_long_integer_type_node;
83 tree short_unsigned_type_node;
84 tree long_unsigned_type_node;
85 tree long_long_unsigned_type_node;
87 tree truthvalue_type_node;
88 tree truthvalue_false_node;
89 tree truthvalue_true_node;
91 tree ptrdiff_type_node;
93 tree unsigned_char_type_node;
94 tree signed_char_type_node;
97 tree char16_type_node;
98 tree char32_type_node;
100 tree float_type_node;
101 tree double_type_node;
102 tree long_double_type_node;
104 tree complex_integer_type_node;
105 tree complex_float_type_node;
106 tree complex_double_type_node;
107 tree complex_long_double_type_node;
109 tree dfloat32_type_node;
110 tree dfloat64_type_node;
111 tree_dfloat128_type_node;
113 tree intQI_type_node;
114 tree intHI_type_node;
115 tree intSI_type_node;
116 tree intDI_type_node;
117 tree intTI_type_node;
119 tree unsigned_intQI_type_node;
120 tree unsigned_intHI_type_node;
121 tree unsigned_intSI_type_node;
122 tree unsigned_intDI_type_node;
123 tree unsigned_intTI_type_node;
125 tree widest_integer_literal_type_node;
126 tree widest_unsigned_literal_type_node;
128 Nodes for types `void *' and `const void *'.
130 tree ptr_type_node, const_ptr_type_node;
132 Nodes for types `char *' and `const char *'.
134 tree string_type_node, const_string_type_node;
136 Type `char[SOMENUMBER]'.
137 Used when an array of char is needed and the size is irrelevant.
139 tree char_array_type_node;
141 Type `wchar_t[SOMENUMBER]' or something like it.
142 Used when a wide string literal is created.
144 tree wchar_array_type_node;
146 Type `char16_t[SOMENUMBER]' or something like it.
147 Used when a UTF-16 string literal is created.
149 tree char16_array_type_node;
151 Type `char32_t[SOMENUMBER]' or something like it.
152 Used when a UTF-32 string literal is created.
154 tree char32_array_type_node;
156 Type `int ()' -- used for implicit declaration of functions.
158 tree default_function_type;
160 A VOID_TYPE node, packaged in a TREE_LIST.
164 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
165 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
166 VAR_DECLS, but C++ does.)
168 tree function_name_decl_node;
169 tree pretty_function_name_decl_node;
170 tree c99_function_name_decl_node;
172 Stack of nested function name VAR_DECLs.
174 tree saved_function_name_decls;
178 tree c_global_trees
[CTI_MAX
];
180 /* Switches common to the C front ends. */
182 /* Nonzero means don't output line number information. */
184 char flag_no_line_commands
;
186 /* Nonzero causes -E output not to be done, but directives such as
187 #define that have side effects are still obeyed. */
191 /* Nonzero means dump macros in some fashion. */
193 char flag_dump_macros
;
195 /* Nonzero means pass #include lines through to the output. */
197 char flag_dump_includes
;
199 /* Nonzero means process PCH files while preprocessing. */
201 bool flag_pch_preprocess
;
203 /* The file name to which we should write a precompiled header, or
204 NULL if no header will be written in this compile. */
206 const char *pch_file
;
208 /* Nonzero if an ISO standard was selected. It rejects macros in the
212 /* C/ObjC language option variables. */
215 /* Nonzero means allow type mismatches in conditional expressions;
216 just make their values `void'. */
218 int flag_cond_mismatch
;
220 /* Nonzero means enable C89 Amendment 1 features. */
224 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
228 /* Nonzero means use the ISO C11 dialect of C. */
232 /* Nonzero means that we have builtin functions, and main is an int. */
237 /* ObjC language option variables. */
240 /* Tells the compiler that this is a special run. Do not perform any
241 compiling, instead we are to test some platform dependent features
242 and output a C header file with appropriate definitions. */
244 int print_struct_values
;
246 /* Tells the compiler what is the constant string class for ObjC. */
248 const char *constant_string_class_name
;
251 /* C++ language option variables. */
254 /* Nonzero means generate separate instantiation control files and
255 juggle them at link time. */
257 int flag_use_repository
;
259 /* The C++ dialect being used. Default set in c_common_post_options. */
261 enum cxx_dialect cxx_dialect
= cxx_unset
;
263 /* Maximum template instantiation depth. This limit exists to limit the
264 time it takes to notice excessively recursive template instantiations.
266 The default is lower than the 1024 recommended by the C++0x standard
267 because G++ runs out of stack before 1024 with highly recursive template
268 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
270 int max_tinst_depth
= 900;
272 /* The elements of `ridpointers' are identifier nodes for the reserved
273 type names and storage classes. It is indexed by a RID_... value. */
276 tree (*make_fname_decl
) (location_t
, tree
, int);
278 /* Nonzero means don't warn about problems that occur when the code is
280 int c_inhibit_evaluation_warnings
;
282 /* Whether we are building a boolean conversion inside
283 convert_for_assignment, or some other late binary operation. If
284 build_binary_op is called for C (from code shared by C and C++) in
285 this case, then the operands have already been folded and the
286 result will not be folded again, so C_MAYBE_CONST_EXPR should not
288 bool in_late_binary_op
;
290 /* Whether lexing has been completed, so subsequent preprocessor
291 errors should use the compiler's input_location. */
292 bool done_lexing
= false;
294 /* Information about how a function name is generated. */
297 tree
*const decl
; /* pointer to the VAR_DECL. */
298 const unsigned rid
; /* RID number for the identifier. */
299 const int pretty
; /* How pretty is it? */
302 /* The three ways of getting then name of the current function. */
304 const struct fname_var_t fname_vars
[] =
306 /* C99 compliant __func__, must be first. */
307 {&c99_function_name_decl_node
, RID_C99_FUNCTION_NAME
, 0},
308 /* GCC __FUNCTION__ compliant. */
309 {&function_name_decl_node
, RID_FUNCTION_NAME
, 0},
310 /* GCC __PRETTY_FUNCTION__ compliant. */
311 {&pretty_function_name_decl_node
, RID_PRETTY_FUNCTION_NAME
, 1},
315 /* Global visibility options. */
316 struct visibility_flags visibility_options
;
318 static tree
c_fully_fold_internal (tree expr
, bool, bool *, bool *, bool);
319 static tree
check_case_value (location_t
, tree
);
320 static bool check_case_bounds (location_t
, tree
, tree
, tree
*, tree
*);
322 static tree
handle_packed_attribute (tree
*, tree
, tree
, int, bool *);
323 static tree
handle_nocommon_attribute (tree
*, tree
, tree
, int, bool *);
324 static tree
handle_common_attribute (tree
*, tree
, tree
, int, bool *);
325 static tree
handle_noreturn_attribute (tree
*, tree
, tree
, int, bool *);
326 static tree
handle_hot_attribute (tree
*, tree
, tree
, int, bool *);
327 static tree
handle_cold_attribute (tree
*, tree
, tree
, int, bool *);
328 static tree
handle_no_sanitize_address_attribute (tree
*, tree
, tree
,
330 static tree
handle_no_address_safety_analysis_attribute (tree
*, tree
, tree
,
332 static tree
handle_no_sanitize_undefined_attribute (tree
*, tree
, tree
, int,
334 static tree
handle_stack_protect_attribute (tree
*, tree
, tree
, int, bool *);
335 static tree
handle_noinline_attribute (tree
*, tree
, tree
, int, bool *);
336 static tree
handle_noclone_attribute (tree
*, tree
, tree
, int, bool *);
337 static tree
handle_noicf_attribute (tree
*, tree
, tree
, int, bool *);
338 static tree
handle_leaf_attribute (tree
*, tree
, tree
, int, bool *);
339 static tree
handle_always_inline_attribute (tree
*, tree
, tree
, int,
341 static tree
handle_gnu_inline_attribute (tree
*, tree
, tree
, int, bool *);
342 static tree
handle_artificial_attribute (tree
*, tree
, tree
, int, bool *);
343 static tree
handle_flatten_attribute (tree
*, tree
, tree
, int, bool *);
344 static tree
handle_error_attribute (tree
*, tree
, tree
, int, bool *);
345 static tree
handle_used_attribute (tree
*, tree
, tree
, int, bool *);
346 static tree
handle_unused_attribute (tree
*, tree
, tree
, int, bool *);
347 static tree
handle_externally_visible_attribute (tree
*, tree
, tree
, int,
349 static tree
handle_no_reorder_attribute (tree
*, tree
, tree
, int,
351 static tree
handle_const_attribute (tree
*, tree
, tree
, int, bool *);
352 static tree
handle_transparent_union_attribute (tree
*, tree
, tree
,
354 static tree
handle_constructor_attribute (tree
*, tree
, tree
, int, bool *);
355 static tree
handle_destructor_attribute (tree
*, tree
, tree
, int, bool *);
356 static tree
handle_mode_attribute (tree
*, tree
, tree
, int, bool *);
357 static tree
handle_section_attribute (tree
*, tree
, tree
, int, bool *);
358 static tree
handle_aligned_attribute (tree
*, tree
, tree
, int, bool *);
359 static tree
handle_weak_attribute (tree
*, tree
, tree
, int, bool *) ;
360 static tree
handle_alias_ifunc_attribute (bool, tree
*, tree
, tree
, bool *);
361 static tree
handle_ifunc_attribute (tree
*, tree
, tree
, int, bool *);
362 static tree
handle_alias_attribute (tree
*, tree
, tree
, int, bool *);
363 static tree
handle_weakref_attribute (tree
*, tree
, tree
, int, bool *) ;
364 static tree
handle_visibility_attribute (tree
*, tree
, tree
, int,
366 static tree
handle_tls_model_attribute (tree
*, tree
, tree
, int,
368 static tree
handle_no_instrument_function_attribute (tree
*, tree
,
370 static tree
handle_malloc_attribute (tree
*, tree
, tree
, int, bool *);
371 static tree
handle_returns_twice_attribute (tree
*, tree
, tree
, int, bool *);
372 static tree
handle_no_limit_stack_attribute (tree
*, tree
, tree
, int,
374 static tree
handle_pure_attribute (tree
*, tree
, tree
, int, bool *);
375 static tree
handle_tm_attribute (tree
*, tree
, tree
, int, bool *);
376 static tree
handle_tm_wrap_attribute (tree
*, tree
, tree
, int, bool *);
377 static tree
handle_novops_attribute (tree
*, tree
, tree
, int, bool *);
378 static tree
handle_deprecated_attribute (tree
*, tree
, tree
, int,
380 static tree
handle_vector_size_attribute (tree
*, tree
, tree
, int,
382 static tree
handle_nonnull_attribute (tree
*, tree
, tree
, int, bool *);
383 static tree
handle_nothrow_attribute (tree
*, tree
, tree
, int, bool *);
384 static tree
handle_cleanup_attribute (tree
*, tree
, tree
, int, bool *);
385 static tree
handle_warn_unused_result_attribute (tree
*, tree
, tree
, int,
387 static tree
handle_sentinel_attribute (tree
*, tree
, tree
, int, bool *);
388 static tree
handle_type_generic_attribute (tree
*, tree
, tree
, int, bool *);
389 static tree
handle_alloc_size_attribute (tree
*, tree
, tree
, int, bool *);
390 static tree
handle_alloc_align_attribute (tree
*, tree
, tree
, int, bool *);
391 static tree
handle_assume_aligned_attribute (tree
*, tree
, tree
, int, bool *);
392 static tree
handle_target_attribute (tree
*, tree
, tree
, int, bool *);
393 static tree
handle_optimize_attribute (tree
*, tree
, tree
, int, bool *);
394 static tree
ignore_attribute (tree
*, tree
, tree
, int, bool *);
395 static tree
handle_no_split_stack_attribute (tree
*, tree
, tree
, int, bool *);
396 static tree
handle_fnspec_attribute (tree
*, tree
, tree
, int, bool *);
397 static tree
handle_warn_unused_attribute (tree
*, tree
, tree
, int, bool *);
398 static tree
handle_returns_nonnull_attribute (tree
*, tree
, tree
, int, bool *);
399 static tree
handle_omp_declare_simd_attribute (tree
*, tree
, tree
, int,
401 static tree
handle_omp_declare_target_attribute (tree
*, tree
, tree
, int,
403 static tree
handle_designated_init_attribute (tree
*, tree
, tree
, int, bool *);
404 static tree
handle_bnd_variable_size_attribute (tree
*, tree
, tree
, int, bool *);
405 static tree
handle_bnd_legacy (tree
*, tree
, tree
, int, bool *);
406 static tree
handle_bnd_instrument (tree
*, tree
, tree
, int, bool *);
408 static void check_function_nonnull (tree
, int, tree
*);
409 static void check_nonnull_arg (void *, tree
, unsigned HOST_WIDE_INT
);
410 static bool nonnull_check_p (tree
, unsigned HOST_WIDE_INT
);
411 static bool get_nonnull_operand (tree
, unsigned HOST_WIDE_INT
*);
412 static int resort_field_decl_cmp (const void *, const void *);
414 /* Reserved words. The third field is a mask: keywords are disabled
415 if they match the mask.
418 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
419 C --std=c99: D_CXXONLY | D_OBJC
420 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
421 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
422 C++ --std=c0x: D_CONLY | D_OBJC
423 ObjC++ is like C++ except that D_OBJC is not set
425 If -fno-asm is used, D_ASM is added to the mask. If
426 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
427 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
428 In C with -Wc++-compat, we warn if D_CXXWARN is set.
430 Note the complication of the D_CXX_OBJC keywords. These are
431 reserved words such as 'class'. In C++, 'class' is a reserved
432 word. In Objective-C++ it is too. In Objective-C, it is a
433 reserved word too, but only if it follows an '@' sign.
435 const struct c_common_resword c_common_reswords
[] =
437 { "_Alignas", RID_ALIGNAS
, D_CONLY
},
438 { "_Alignof", RID_ALIGNOF
, D_CONLY
},
439 { "_Atomic", RID_ATOMIC
, D_CONLY
},
440 { "_Bool", RID_BOOL
, D_CONLY
},
441 { "_Complex", RID_COMPLEX
, 0 },
442 { "_Cilk_spawn", RID_CILK_SPAWN
, 0 },
443 { "_Cilk_sync", RID_CILK_SYNC
, 0 },
444 { "_Cilk_for", RID_CILK_FOR
, 0 },
445 { "_Imaginary", RID_IMAGINARY
, D_CONLY
},
446 { "_Decimal32", RID_DFLOAT32
, D_CONLY
| D_EXT
},
447 { "_Decimal64", RID_DFLOAT64
, D_CONLY
| D_EXT
},
448 { "_Decimal128", RID_DFLOAT128
, D_CONLY
| D_EXT
},
449 { "_Fract", RID_FRACT
, D_CONLY
| D_EXT
},
450 { "_Accum", RID_ACCUM
, D_CONLY
| D_EXT
},
451 { "_Sat", RID_SAT
, D_CONLY
| D_EXT
},
452 { "_Static_assert", RID_STATIC_ASSERT
, D_CONLY
},
453 { "_Noreturn", RID_NORETURN
, D_CONLY
},
454 { "_Generic", RID_GENERIC
, D_CONLY
},
455 { "_Thread_local", RID_THREAD
, D_CONLY
},
456 { "__FUNCTION__", RID_FUNCTION_NAME
, 0 },
457 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME
, 0 },
458 { "__alignof", RID_ALIGNOF
, 0 },
459 { "__alignof__", RID_ALIGNOF
, 0 },
460 { "__asm", RID_ASM
, 0 },
461 { "__asm__", RID_ASM
, 0 },
462 { "__attribute", RID_ATTRIBUTE
, 0 },
463 { "__attribute__", RID_ATTRIBUTE
, 0 },
464 { "__auto_type", RID_AUTO_TYPE
, D_CONLY
},
465 { "__bases", RID_BASES
, D_CXXONLY
},
466 { "__builtin_call_with_static_chain",
467 RID_BUILTIN_CALL_WITH_STATIC_CHAIN
, D_CONLY
},
468 { "__builtin_choose_expr", RID_CHOOSE_EXPR
, D_CONLY
},
469 { "__builtin_complex", RID_BUILTIN_COMPLEX
, D_CONLY
},
470 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE
, 0 },
471 { "__builtin_offsetof", RID_OFFSETOF
, 0 },
472 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P
, D_CONLY
},
473 { "__builtin_va_arg", RID_VA_ARG
, 0 },
474 { "__complex", RID_COMPLEX
, 0 },
475 { "__complex__", RID_COMPLEX
, 0 },
476 { "__const", RID_CONST
, 0 },
477 { "__const__", RID_CONST
, 0 },
478 { "__decltype", RID_DECLTYPE
, D_CXXONLY
},
479 { "__direct_bases", RID_DIRECT_BASES
, D_CXXONLY
},
480 { "__extension__", RID_EXTENSION
, 0 },
481 { "__func__", RID_C99_FUNCTION_NAME
, 0 },
482 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN
, D_CXXONLY
},
483 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR
, D_CXXONLY
},
484 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY
, D_CXXONLY
},
485 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN
, D_CXXONLY
},
486 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR
, D_CXXONLY
},
487 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY
, D_CXXONLY
},
488 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR
, D_CXXONLY
},
489 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR
, D_CXXONLY
},
490 { "__imag", RID_IMAGPART
, 0 },
491 { "__imag__", RID_IMAGPART
, 0 },
492 { "__inline", RID_INLINE
, 0 },
493 { "__inline__", RID_INLINE
, 0 },
494 { "__is_abstract", RID_IS_ABSTRACT
, D_CXXONLY
},
495 { "__is_base_of", RID_IS_BASE_OF
, D_CXXONLY
},
496 { "__is_class", RID_IS_CLASS
, D_CXXONLY
},
497 { "__is_empty", RID_IS_EMPTY
, D_CXXONLY
},
498 { "__is_enum", RID_IS_ENUM
, D_CXXONLY
},
499 { "__is_final", RID_IS_FINAL
, D_CXXONLY
},
500 { "__is_literal_type", RID_IS_LITERAL_TYPE
, D_CXXONLY
},
501 { "__is_pod", RID_IS_POD
, D_CXXONLY
},
502 { "__is_polymorphic", RID_IS_POLYMORPHIC
, D_CXXONLY
},
503 { "__is_standard_layout", RID_IS_STD_LAYOUT
, D_CXXONLY
},
504 { "__is_trivial", RID_IS_TRIVIAL
, D_CXXONLY
},
505 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE
, D_CXXONLY
},
506 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE
, D_CXXONLY
},
507 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE
, D_CXXONLY
},
508 { "__is_union", RID_IS_UNION
, D_CXXONLY
},
509 { "__label__", RID_LABEL
, 0 },
510 { "__null", RID_NULL
, 0 },
511 { "__real", RID_REALPART
, 0 },
512 { "__real__", RID_REALPART
, 0 },
513 { "__restrict", RID_RESTRICT
, 0 },
514 { "__restrict__", RID_RESTRICT
, 0 },
515 { "__signed", RID_SIGNED
, 0 },
516 { "__signed__", RID_SIGNED
, 0 },
517 { "__thread", RID_THREAD
, 0 },
518 { "__transaction_atomic", RID_TRANSACTION_ATOMIC
, 0 },
519 { "__transaction_relaxed", RID_TRANSACTION_RELAXED
, 0 },
520 { "__transaction_cancel", RID_TRANSACTION_CANCEL
, 0 },
521 { "__typeof", RID_TYPEOF
, 0 },
522 { "__typeof__", RID_TYPEOF
, 0 },
523 { "__underlying_type", RID_UNDERLYING_TYPE
, D_CXXONLY
},
524 { "__volatile", RID_VOLATILE
, 0 },
525 { "__volatile__", RID_VOLATILE
, 0 },
526 { "alignas", RID_ALIGNAS
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
527 { "alignof", RID_ALIGNOF
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
528 { "asm", RID_ASM
, D_ASM
},
529 { "auto", RID_AUTO
, 0 },
530 { "bool", RID_BOOL
, D_CXXONLY
| D_CXXWARN
},
531 { "break", RID_BREAK
, 0 },
532 { "case", RID_CASE
, 0 },
533 { "catch", RID_CATCH
, D_CXX_OBJC
| D_CXXWARN
},
534 { "char", RID_CHAR
, 0 },
535 { "char16_t", RID_CHAR16
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
536 { "char32_t", RID_CHAR32
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
537 { "class", RID_CLASS
, D_CXX_OBJC
| D_CXXWARN
},
538 { "const", RID_CONST
, 0 },
539 { "constexpr", RID_CONSTEXPR
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
540 { "const_cast", RID_CONSTCAST
, D_CXXONLY
| D_CXXWARN
},
541 { "continue", RID_CONTINUE
, 0 },
542 { "decltype", RID_DECLTYPE
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
543 { "default", RID_DEFAULT
, 0 },
544 { "delete", RID_DELETE
, D_CXXONLY
| D_CXXWARN
},
546 { "double", RID_DOUBLE
, 0 },
547 { "dynamic_cast", RID_DYNCAST
, D_CXXONLY
| D_CXXWARN
},
548 { "else", RID_ELSE
, 0 },
549 { "enum", RID_ENUM
, 0 },
550 { "explicit", RID_EXPLICIT
, D_CXXONLY
| D_CXXWARN
},
551 { "export", RID_EXPORT
, D_CXXONLY
| D_CXXWARN
},
552 { "extern", RID_EXTERN
, 0 },
553 { "false", RID_FALSE
, D_CXXONLY
| D_CXXWARN
},
554 { "float", RID_FLOAT
, 0 },
555 { "for", RID_FOR
, 0 },
556 { "friend", RID_FRIEND
, D_CXXONLY
| D_CXXWARN
},
557 { "goto", RID_GOTO
, 0 },
559 { "inline", RID_INLINE
, D_EXT89
},
560 { "int", RID_INT
, 0 },
561 { "long", RID_LONG
, 0 },
562 { "mutable", RID_MUTABLE
, D_CXXONLY
| D_CXXWARN
},
563 { "namespace", RID_NAMESPACE
, D_CXXONLY
| D_CXXWARN
},
564 { "new", RID_NEW
, D_CXXONLY
| D_CXXWARN
},
565 { "noexcept", RID_NOEXCEPT
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
566 { "nullptr", RID_NULLPTR
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
567 { "operator", RID_OPERATOR
, D_CXXONLY
| D_CXXWARN
},
568 { "private", RID_PRIVATE
, D_CXX_OBJC
| D_CXXWARN
},
569 { "protected", RID_PROTECTED
, D_CXX_OBJC
| D_CXXWARN
},
570 { "public", RID_PUBLIC
, D_CXX_OBJC
| D_CXXWARN
},
571 { "register", RID_REGISTER
, 0 },
572 { "reinterpret_cast", RID_REINTCAST
, D_CXXONLY
| D_CXXWARN
},
573 { "restrict", RID_RESTRICT
, D_CONLY
| D_C99
},
574 { "return", RID_RETURN
, 0 },
575 { "short", RID_SHORT
, 0 },
576 { "signed", RID_SIGNED
, 0 },
577 { "sizeof", RID_SIZEOF
, 0 },
578 { "static", RID_STATIC
, 0 },
579 { "static_assert", RID_STATIC_ASSERT
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
580 { "static_cast", RID_STATCAST
, D_CXXONLY
| D_CXXWARN
},
581 { "struct", RID_STRUCT
, 0 },
582 { "switch", RID_SWITCH
, 0 },
583 { "template", RID_TEMPLATE
, D_CXXONLY
| D_CXXWARN
},
584 { "this", RID_THIS
, D_CXXONLY
| D_CXXWARN
},
585 { "thread_local", RID_THREAD
, D_CXXONLY
| D_CXX0X
| D_CXXWARN
},
586 { "throw", RID_THROW
, D_CXX_OBJC
| D_CXXWARN
},
587 { "true", RID_TRUE
, D_CXXONLY
| D_CXXWARN
},
588 { "try", RID_TRY
, D_CXX_OBJC
| D_CXXWARN
},
589 { "typedef", RID_TYPEDEF
, 0 },
590 { "typename", RID_TYPENAME
, D_CXXONLY
| D_CXXWARN
},
591 { "typeid", RID_TYPEID
, D_CXXONLY
| D_CXXWARN
},
592 { "typeof", RID_TYPEOF
, D_ASM
| D_EXT
},
593 { "union", RID_UNION
, 0 },
594 { "unsigned", RID_UNSIGNED
, 0 },
595 { "using", RID_USING
, D_CXXONLY
| D_CXXWARN
},
596 { "virtual", RID_VIRTUAL
, D_CXXONLY
| D_CXXWARN
},
597 { "void", RID_VOID
, 0 },
598 { "volatile", RID_VOLATILE
, 0 },
599 { "wchar_t", RID_WCHAR
, D_CXXONLY
},
600 { "while", RID_WHILE
, 0 },
601 /* These Objective-C keywords are recognized only immediately after
603 { "compatibility_alias", RID_AT_ALIAS
, D_OBJC
},
604 { "defs", RID_AT_DEFS
, D_OBJC
},
605 { "encode", RID_AT_ENCODE
, D_OBJC
},
606 { "end", RID_AT_END
, D_OBJC
},
607 { "implementation", RID_AT_IMPLEMENTATION
, D_OBJC
},
608 { "interface", RID_AT_INTERFACE
, D_OBJC
},
609 { "protocol", RID_AT_PROTOCOL
, D_OBJC
},
610 { "selector", RID_AT_SELECTOR
, D_OBJC
},
611 { "finally", RID_AT_FINALLY
, D_OBJC
},
612 { "synchronized", RID_AT_SYNCHRONIZED
, D_OBJC
},
613 { "optional", RID_AT_OPTIONAL
, D_OBJC
},
614 { "required", RID_AT_REQUIRED
, D_OBJC
},
615 { "property", RID_AT_PROPERTY
, D_OBJC
},
616 { "package", RID_AT_PACKAGE
, D_OBJC
},
617 { "synthesize", RID_AT_SYNTHESIZE
, D_OBJC
},
618 { "dynamic", RID_AT_DYNAMIC
, D_OBJC
},
619 /* These are recognized only in protocol-qualifier context
621 { "bycopy", RID_BYCOPY
, D_OBJC
},
622 { "byref", RID_BYREF
, D_OBJC
},
623 { "in", RID_IN
, D_OBJC
},
624 { "inout", RID_INOUT
, D_OBJC
},
625 { "oneway", RID_ONEWAY
, D_OBJC
},
626 { "out", RID_OUT
, D_OBJC
},
627 /* These are recognized inside a property attribute list */
628 { "assign", RID_ASSIGN
, D_OBJC
},
629 { "copy", RID_COPY
, D_OBJC
},
630 { "getter", RID_GETTER
, D_OBJC
},
631 { "nonatomic", RID_NONATOMIC
, D_OBJC
},
632 { "readonly", RID_READONLY
, D_OBJC
},
633 { "readwrite", RID_READWRITE
, D_OBJC
},
634 { "retain", RID_RETAIN
, D_OBJC
},
635 { "setter", RID_SETTER
, D_OBJC
},
638 const unsigned int num_c_common_reswords
=
639 sizeof c_common_reswords
/ sizeof (struct c_common_resword
);
641 /* Table of machine-independent attributes common to all C-like languages.
643 All attributes referencing arguments should be additionally processed
644 in chkp_copy_function_type_adding_bounds for correct instrumentation
645 by Pointer Bounds Checker.
646 Current list of processed common attributes: nonnull. */
647 const struct attribute_spec c_common_attribute_table
[] =
649 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
650 affects_type_identity } */
651 { "packed", 0, 0, false, false, false,
652 handle_packed_attribute
, false},
653 { "nocommon", 0, 0, true, false, false,
654 handle_nocommon_attribute
, false},
655 { "common", 0, 0, true, false, false,
656 handle_common_attribute
, false },
657 /* FIXME: logically, noreturn attributes should be listed as
658 "false, true, true" and apply to function types. But implementing this
659 would require all the places in the compiler that use TREE_THIS_VOLATILE
660 on a decl to identify non-returning functions to be located and fixed
661 to check the function type instead. */
662 { "noreturn", 0, 0, true, false, false,
663 handle_noreturn_attribute
, false },
664 { "volatile", 0, 0, true, false, false,
665 handle_noreturn_attribute
, false },
666 { "stack_protect", 0, 0, true, false, false,
667 handle_stack_protect_attribute
, false },
668 { "noinline", 0, 0, true, false, false,
669 handle_noinline_attribute
, false },
670 { "noclone", 0, 0, true, false, false,
671 handle_noclone_attribute
, false },
672 { "no_icf", 0, 0, true, false, false,
673 handle_noicf_attribute
, false },
674 { "leaf", 0, 0, true, false, false,
675 handle_leaf_attribute
, false },
676 { "always_inline", 0, 0, true, false, false,
677 handle_always_inline_attribute
, false },
678 { "gnu_inline", 0, 0, true, false, false,
679 handle_gnu_inline_attribute
, false },
680 { "artificial", 0, 0, true, false, false,
681 handle_artificial_attribute
, false },
682 { "flatten", 0, 0, true, false, false,
683 handle_flatten_attribute
, false },
684 { "used", 0, 0, true, false, false,
685 handle_used_attribute
, false },
686 { "unused", 0, 0, false, false, false,
687 handle_unused_attribute
, false },
688 { "externally_visible", 0, 0, true, false, false,
689 handle_externally_visible_attribute
, false },
690 { "no_reorder", 0, 0, true, false, false,
691 handle_no_reorder_attribute
, false },
692 /* The same comments as for noreturn attributes apply to const ones. */
693 { "const", 0, 0, true, false, false,
694 handle_const_attribute
, false },
695 { "transparent_union", 0, 0, false, false, false,
696 handle_transparent_union_attribute
, false },
697 { "constructor", 0, 1, true, false, false,
698 handle_constructor_attribute
, false },
699 { "destructor", 0, 1, true, false, false,
700 handle_destructor_attribute
, false },
701 { "mode", 1, 1, false, true, false,
702 handle_mode_attribute
, false },
703 { "section", 1, 1, true, false, false,
704 handle_section_attribute
, false },
705 { "aligned", 0, 1, false, false, false,
706 handle_aligned_attribute
, false },
707 { "weak", 0, 0, true, false, false,
708 handle_weak_attribute
, false },
709 { "ifunc", 1, 1, true, false, false,
710 handle_ifunc_attribute
, false },
711 { "alias", 1, 1, true, false, false,
712 handle_alias_attribute
, false },
713 { "weakref", 0, 1, true, false, false,
714 handle_weakref_attribute
, false },
715 { "no_instrument_function", 0, 0, true, false, false,
716 handle_no_instrument_function_attribute
,
718 { "malloc", 0, 0, true, false, false,
719 handle_malloc_attribute
, false },
720 { "returns_twice", 0, 0, true, false, false,
721 handle_returns_twice_attribute
, false },
722 { "no_stack_limit", 0, 0, true, false, false,
723 handle_no_limit_stack_attribute
, false },
724 { "pure", 0, 0, true, false, false,
725 handle_pure_attribute
, false },
726 { "transaction_callable", 0, 0, false, true, false,
727 handle_tm_attribute
, false },
728 { "transaction_unsafe", 0, 0, false, true, false,
729 handle_tm_attribute
, false },
730 { "transaction_safe", 0, 0, false, true, false,
731 handle_tm_attribute
, false },
732 { "transaction_may_cancel_outer", 0, 0, false, true, false,
733 handle_tm_attribute
, false },
734 /* ??? These two attributes didn't make the transition from the
735 Intel language document to the multi-vendor language document. */
736 { "transaction_pure", 0, 0, false, true, false,
737 handle_tm_attribute
, false },
738 { "transaction_wrap", 1, 1, true, false, false,
739 handle_tm_wrap_attribute
, false },
740 /* For internal use (marking of builtins) only. The name contains space
741 to prevent its usage in source code. */
742 { "no vops", 0, 0, true, false, false,
743 handle_novops_attribute
, false },
744 { "deprecated", 0, 1, false, false, false,
745 handle_deprecated_attribute
, false },
746 { "vector_size", 1, 1, false, true, false,
747 handle_vector_size_attribute
, false },
748 { "visibility", 1, 1, false, false, false,
749 handle_visibility_attribute
, false },
750 { "tls_model", 1, 1, true, false, false,
751 handle_tls_model_attribute
, false },
752 { "nonnull", 0, -1, false, true, true,
753 handle_nonnull_attribute
, false },
754 { "nothrow", 0, 0, true, false, false,
755 handle_nothrow_attribute
, false },
756 { "may_alias", 0, 0, false, true, false, NULL
, false },
757 { "cleanup", 1, 1, true, false, false,
758 handle_cleanup_attribute
, false },
759 { "warn_unused_result", 0, 0, false, true, true,
760 handle_warn_unused_result_attribute
, false },
761 { "sentinel", 0, 1, false, true, true,
762 handle_sentinel_attribute
, false },
763 /* For internal use (marking of builtins) only. The name contains space
764 to prevent its usage in source code. */
765 { "type generic", 0, 0, false, true, true,
766 handle_type_generic_attribute
, false },
767 { "alloc_size", 1, 2, false, true, true,
768 handle_alloc_size_attribute
, false },
769 { "cold", 0, 0, true, false, false,
770 handle_cold_attribute
, false },
771 { "hot", 0, 0, true, false, false,
772 handle_hot_attribute
, false },
773 { "no_address_safety_analysis",
774 0, 0, true, false, false,
775 handle_no_address_safety_analysis_attribute
,
777 { "no_sanitize_address", 0, 0, true, false, false,
778 handle_no_sanitize_address_attribute
,
780 { "no_sanitize_thread", 0, 0, true, false, false,
781 handle_no_sanitize_address_attribute
,
783 { "no_sanitize_undefined", 0, 0, true, false, false,
784 handle_no_sanitize_undefined_attribute
,
786 { "warning", 1, 1, true, false, false,
787 handle_error_attribute
, false },
788 { "error", 1, 1, true, false, false,
789 handle_error_attribute
, false },
790 { "target", 1, -1, true, false, false,
791 handle_target_attribute
, false },
792 { "optimize", 1, -1, true, false, false,
793 handle_optimize_attribute
, false },
794 /* For internal use only. The leading '*' both prevents its usage in
795 source code and signals that it may be overridden by machine tables. */
796 { "*tm regparm", 0, 0, false, true, true,
797 ignore_attribute
, false },
798 { "no_split_stack", 0, 0, true, false, false,
799 handle_no_split_stack_attribute
, false },
800 /* For internal use (marking of builtins and runtime functions) only.
801 The name contains space to prevent its usage in source code. */
802 { "fn spec", 1, 1, false, true, true,
803 handle_fnspec_attribute
, false },
804 { "warn_unused", 0, 0, false, false, false,
805 handle_warn_unused_attribute
, false },
806 { "returns_nonnull", 0, 0, false, true, true,
807 handle_returns_nonnull_attribute
, false },
808 { "omp declare simd", 0, -1, true, false, false,
809 handle_omp_declare_simd_attribute
, false },
810 { "cilk simd function", 0, -1, true, false, false,
811 handle_omp_declare_simd_attribute
, false },
812 { "omp declare target", 0, 0, true, false, false,
813 handle_omp_declare_target_attribute
, false },
814 { "alloc_align", 1, 1, false, true, true,
815 handle_alloc_align_attribute
, false },
816 { "assume_aligned", 1, 2, false, true, true,
817 handle_assume_aligned_attribute
, false },
818 { "designated_init", 0, 0, false, true, false,
819 handle_designated_init_attribute
, false },
820 { "bnd_variable_size", 0, 0, true, false, false,
821 handle_bnd_variable_size_attribute
, false },
822 { "bnd_legacy", 0, 0, true, false, false,
823 handle_bnd_legacy
, false },
824 { "bnd_instrument", 0, 0, true, false, false,
825 handle_bnd_instrument
, false },
826 { NULL
, 0, 0, false, false, false, NULL
, false }
829 /* Give the specifications for the format attributes, used by C and all
832 All attributes referencing arguments should be additionally processed
833 in chkp_copy_function_type_adding_bounds for correct instrumentation
834 by Pointer Bounds Checker.
835 Current list of processed format attributes: format, format_arg. */
836 const struct attribute_spec c_common_format_attribute_table
[] =
838 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
839 affects_type_identity } */
840 { "format", 3, 3, false, true, true,
841 handle_format_attribute
, false },
842 { "format_arg", 1, 1, false, true, true,
843 handle_format_arg_attribute
, false },
844 { NULL
, 0, 0, false, false, false, NULL
, false }
847 /* Return identifier for address space AS. */
850 c_addr_space_name (addr_space_t as
)
852 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
853 gcc_assert (ridpointers
[rid
]);
854 return IDENTIFIER_POINTER (ridpointers
[rid
]);
857 /* Push current bindings for the function name VAR_DECLS. */
860 start_fname_decls (void)
863 tree saved
= NULL_TREE
;
865 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
867 tree decl
= *fname_vars
[ix
].decl
;
871 saved
= tree_cons (decl
, build_int_cst (integer_type_node
, ix
),
873 *fname_vars
[ix
].decl
= NULL_TREE
;
876 if (saved
|| saved_function_name_decls
)
877 /* Normally they'll have been NULL, so only push if we've got a
878 stack, or they are non-NULL. */
879 saved_function_name_decls
= tree_cons (saved
, NULL_TREE
,
880 saved_function_name_decls
);
883 /* Finish up the current bindings, adding them into the current function's
884 statement tree. This must be done _before_ finish_stmt_tree is called.
885 If there is no current function, we must be at file scope and no statements
886 are involved. Pop the previous bindings. */
889 finish_fname_decls (void)
892 tree stmts
= NULL_TREE
;
893 tree stack
= saved_function_name_decls
;
895 for (; stack
&& TREE_VALUE (stack
); stack
= TREE_CHAIN (stack
))
896 append_to_statement_list (TREE_VALUE (stack
), &stmts
);
900 tree
*bodyp
= &DECL_SAVED_TREE (current_function_decl
);
902 if (TREE_CODE (*bodyp
) == BIND_EXPR
)
903 bodyp
= &BIND_EXPR_BODY (*bodyp
);
905 append_to_statement_list_force (*bodyp
, &stmts
);
909 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
910 *fname_vars
[ix
].decl
= NULL_TREE
;
914 /* We had saved values, restore them. */
917 for (saved
= TREE_PURPOSE (stack
); saved
; saved
= TREE_CHAIN (saved
))
919 tree decl
= TREE_PURPOSE (saved
);
920 unsigned ix
= TREE_INT_CST_LOW (TREE_VALUE (saved
));
922 *fname_vars
[ix
].decl
= decl
;
924 stack
= TREE_CHAIN (stack
);
926 saved_function_name_decls
= stack
;
929 /* Return the text name of the current function, suitably prettified
930 by PRETTY_P. Return string must be freed by caller. */
933 fname_as_string (int pretty_p
)
935 const char *name
= "top level";
938 cpp_string cstr
= { 0, 0 }, strname
;
946 if (current_function_decl
)
947 name
= lang_hooks
.decl_printable_name (current_function_decl
, vrb
);
949 len
= strlen (name
) + 3; /* Two for '"'s. One for NULL. */
951 namep
= XNEWVEC (char, len
);
952 snprintf (namep
, len
, "\"%s\"", name
);
953 strname
.text
= (unsigned char *) namep
;
954 strname
.len
= len
- 1;
956 if (cpp_interpret_string (parse_in
, &strname
, 1, &cstr
, CPP_STRING
))
959 return (const char *) cstr
.text
;
965 /* Return the VAR_DECL for a const char array naming the current
966 function. If the VAR_DECL has not yet been created, create it
967 now. RID indicates how it should be formatted and IDENTIFIER_NODE
968 ID is its name (unfortunately C and C++ hold the RID values of
969 keywords in different places, so we can't derive RID from ID in
970 this language independent code. LOC is the location of the
974 fname_decl (location_t loc
, unsigned int rid
, tree id
)
977 tree decl
= NULL_TREE
;
979 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
980 if (fname_vars
[ix
].rid
== rid
)
983 decl
= *fname_vars
[ix
].decl
;
986 /* If a tree is built here, it would normally have the lineno of
987 the current statement. Later this tree will be moved to the
988 beginning of the function and this line number will be wrong.
989 To avoid this problem set the lineno to 0 here; that prevents
990 it from appearing in the RTL. */
992 location_t saved_location
= input_location
;
993 input_location
= UNKNOWN_LOCATION
;
995 stmts
= push_stmt_list ();
996 decl
= (*make_fname_decl
) (loc
, id
, fname_vars
[ix
].pretty
);
997 stmts
= pop_stmt_list (stmts
);
998 if (!IS_EMPTY_STMT (stmts
))
999 saved_function_name_decls
1000 = tree_cons (decl
, stmts
, saved_function_name_decls
);
1001 *fname_vars
[ix
].decl
= decl
;
1002 input_location
= saved_location
;
1004 if (!ix
&& !current_function_decl
)
1005 pedwarn (loc
, 0, "%qD is not defined outside of function scope", decl
);
1010 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1013 fix_string_type (tree value
)
1015 int length
= TREE_STRING_LENGTH (value
);
1017 tree e_type
, i_type
, a_type
;
1019 /* Compute the number of elements, for the array type. */
1020 if (TREE_TYPE (value
) == char_array_type_node
|| !TREE_TYPE (value
))
1023 e_type
= char_type_node
;
1025 else if (TREE_TYPE (value
) == char16_array_type_node
)
1027 nchars
= length
/ (TYPE_PRECISION (char16_type_node
) / BITS_PER_UNIT
);
1028 e_type
= char16_type_node
;
1030 else if (TREE_TYPE (value
) == char32_array_type_node
)
1032 nchars
= length
/ (TYPE_PRECISION (char32_type_node
) / BITS_PER_UNIT
);
1033 e_type
= char32_type_node
;
1037 nchars
= length
/ (TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
);
1038 e_type
= wchar_type_node
;
1041 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1042 limit in C++98 Annex B is very large (65536) and is not normative,
1043 so we do not diagnose it (warn_overlength_strings is forced off
1044 in c_common_post_options). */
1045 if (warn_overlength_strings
)
1047 const int nchars_max
= flag_isoc99
? 4095 : 509;
1048 const int relevant_std
= flag_isoc99
? 99 : 90;
1049 if (nchars
- 1 > nchars_max
)
1050 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1051 separate the %d from the 'C'. 'ISO' should not be
1052 translated, but it may be moved after 'C%d' in languages
1053 where modifiers follow nouns. */
1054 pedwarn (input_location
, OPT_Woverlength_strings
,
1055 "string length %qd is greater than the length %qd "
1056 "ISO C%d compilers are required to support",
1057 nchars
- 1, nchars_max
, relevant_std
);
1060 /* Create the array type for the string constant. The ISO C++
1061 standard says that a string literal has type `const char[N]' or
1062 `const wchar_t[N]'. We use the same logic when invoked as a C
1063 front-end with -Wwrite-strings.
1064 ??? We should change the type of an expression depending on the
1065 state of a warning flag. We should just be warning -- see how
1066 this is handled in the C++ front-end for the deprecated implicit
1067 conversion from string literals to `char*' or `wchar_t*'.
1069 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1070 array type being the unqualified version of that type.
1071 Therefore, if we are constructing an array of const char, we must
1072 construct the matching unqualified array type first. The C front
1073 end does not require this, but it does no harm, so we do it
1075 i_type
= build_index_type (size_int (nchars
- 1));
1076 a_type
= build_array_type (e_type
, i_type
);
1077 if (c_dialect_cxx() || warn_write_strings
)
1078 a_type
= c_build_qualified_type (a_type
, TYPE_QUAL_CONST
);
1080 TREE_TYPE (value
) = a_type
;
1081 TREE_CONSTANT (value
) = 1;
1082 TREE_READONLY (value
) = 1;
1083 TREE_STATIC (value
) = 1;
1087 /* If DISABLE is true, stop issuing warnings. This is used when
1088 parsing code that we know will not be executed. This function may
1089 be called multiple times, and works as a stack. */
1092 c_disable_warnings (bool disable
)
1096 ++c_inhibit_evaluation_warnings
;
1097 fold_defer_overflow_warnings ();
1101 /* If ENABLE is true, reenable issuing warnings. */
1104 c_enable_warnings (bool enable
)
1108 --c_inhibit_evaluation_warnings
;
1109 fold_undefer_and_ignore_overflow_warnings ();
1113 /* Fully fold EXPR, an expression that was not folded (beyond integer
1114 constant expressions and null pointer constants) when being built
1115 up. If IN_INIT, this is in a static initializer and certain
1116 changes are made to the folding done. Clear *MAYBE_CONST if
1117 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1118 expression because it contains an evaluated operator (in C99) or an
1119 operator outside of sizeof returning an integer constant (in C90)
1120 not permitted in constant expressions, or because it contains an
1121 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1122 set to true by callers before calling this function.) Return the
1123 folded expression. Function arguments have already been folded
1124 before calling this function, as have the contents of SAVE_EXPR,
1125 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1126 C_MAYBE_CONST_EXPR. */
1129 c_fully_fold (tree expr
, bool in_init
, bool *maybe_const
)
1132 tree eptype
= NULL_TREE
;
1134 bool maybe_const_itself
= true;
1135 location_t loc
= EXPR_LOCATION (expr
);
1137 /* This function is not relevant to C++ because C++ folds while
1138 parsing, and may need changes to be correct for C++ when C++
1139 stops folding while parsing. */
1140 if (c_dialect_cxx ())
1144 maybe_const
= &dummy
;
1145 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
1147 eptype
= TREE_TYPE (expr
);
1148 expr
= TREE_OPERAND (expr
, 0);
1150 ret
= c_fully_fold_internal (expr
, in_init
, maybe_const
,
1151 &maybe_const_itself
, false);
1153 ret
= fold_convert_loc (loc
, eptype
, ret
);
1154 *maybe_const
&= maybe_const_itself
;
1158 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1159 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1160 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1161 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1162 both evaluated and unevaluated subexpressions while
1163 *MAYBE_CONST_ITSELF is carried from only evaluated
1164 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1165 with integer constant operands, and if any of the operands doesn't
1166 get folded to an integer constant, don't fold the expression itself. */
1169 c_fully_fold_internal (tree expr
, bool in_init
, bool *maybe_const_operands
,
1170 bool *maybe_const_itself
, bool for_int_const
)
1173 enum tree_code code
= TREE_CODE (expr
);
1174 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
1175 location_t loc
= EXPR_LOCATION (expr
);
1176 tree op0
, op1
, op2
, op3
;
1177 tree orig_op0
, orig_op1
, orig_op2
;
1178 bool op0_const
= true, op1_const
= true, op2_const
= true;
1179 bool op0_const_self
= true, op1_const_self
= true, op2_const_self
= true;
1180 bool nowarning
= TREE_NO_WARNING (expr
);
1183 /* This function is not relevant to C++ because C++ folds while
1184 parsing, and may need changes to be correct for C++ when C++
1185 stops folding while parsing. */
1186 if (c_dialect_cxx ())
1189 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1190 anything else not counted as an expression cannot usefully be
1191 folded further at this point. */
1192 if (!IS_EXPR_CODE_CLASS (kind
)
1193 || kind
== tcc_statement
1194 || code
== SAVE_EXPR
)
1197 /* Operands of variable-length expressions (function calls) have
1198 already been folded, as have __builtin_* function calls, and such
1199 expressions cannot occur in constant expressions. */
1200 if (kind
== tcc_vl_exp
)
1202 *maybe_const_operands
= false;
1207 if (code
== C_MAYBE_CONST_EXPR
)
1209 tree pre
= C_MAYBE_CONST_EXPR_PRE (expr
);
1210 tree inner
= C_MAYBE_CONST_EXPR_EXPR (expr
);
1211 if (C_MAYBE_CONST_EXPR_NON_CONST (expr
))
1212 *maybe_const_operands
= false;
1213 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr
))
1215 *maybe_const_itself
= false;
1216 inner
= c_fully_fold_internal (inner
, in_init
, maybe_const_operands
,
1217 maybe_const_itself
, true);
1219 if (pre
&& !in_init
)
1220 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr
), pre
, inner
);
1226 /* Assignment, increment, decrement, function call and comma
1227 operators, and statement expressions, cannot occur in constant
1228 expressions if evaluated / outside of sizeof. (Function calls
1229 were handled above, though VA_ARG_EXPR is treated like a function
1230 call here, and statement expressions are handled through
1231 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1235 case PREDECREMENT_EXPR
:
1236 case PREINCREMENT_EXPR
:
1237 case POSTDECREMENT_EXPR
:
1238 case POSTINCREMENT_EXPR
:
1240 *maybe_const_operands
= false;
1247 *maybe_const_operands
= false;
1255 /* Fold individual tree codes as appropriate. */
1258 case COMPOUND_LITERAL_EXPR
:
1259 /* Any non-constancy will have been marked in a containing
1260 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1264 orig_op0
= op0
= TREE_OPERAND (expr
, 0);
1265 op1
= TREE_OPERAND (expr
, 1);
1266 op2
= TREE_OPERAND (expr
, 2);
1267 op0
= c_fully_fold_internal (op0
, in_init
, maybe_const_operands
,
1268 maybe_const_itself
, for_int_const
);
1269 STRIP_TYPE_NOPS (op0
);
1270 if (op0
!= orig_op0
)
1271 ret
= build3 (COMPONENT_REF
, TREE_TYPE (expr
), op0
, op1
, op2
);
1274 TREE_READONLY (ret
) = TREE_READONLY (expr
);
1275 TREE_THIS_VOLATILE (ret
) = TREE_THIS_VOLATILE (expr
);
1280 orig_op0
= op0
= TREE_OPERAND (expr
, 0);
1281 orig_op1
= op1
= TREE_OPERAND (expr
, 1);
1282 op2
= TREE_OPERAND (expr
, 2);
1283 op3
= TREE_OPERAND (expr
, 3);
1284 op0
= c_fully_fold_internal (op0
, in_init
, maybe_const_operands
,
1285 maybe_const_itself
, for_int_const
);
1286 STRIP_TYPE_NOPS (op0
);
1287 op1
= c_fully_fold_internal (op1
, in_init
, maybe_const_operands
,
1288 maybe_const_itself
, for_int_const
);
1289 STRIP_TYPE_NOPS (op1
);
1290 op1
= decl_constant_value_for_optimization (op1
);
1291 if (op0
!= orig_op0
|| op1
!= orig_op1
)
1292 ret
= build4 (ARRAY_REF
, TREE_TYPE (expr
), op0
, op1
, op2
, op3
);
1295 TREE_READONLY (ret
) = TREE_READONLY (expr
);
1296 TREE_SIDE_EFFECTS (ret
) = TREE_SIDE_EFFECTS (expr
);
1297 TREE_THIS_VOLATILE (ret
) = TREE_THIS_VOLATILE (expr
);
1304 case PREDECREMENT_EXPR
:
1305 case PREINCREMENT_EXPR
:
1306 case POSTDECREMENT_EXPR
:
1307 case POSTINCREMENT_EXPR
:
1311 case POINTER_PLUS_EXPR
:
1312 case TRUNC_DIV_EXPR
:
1314 case FLOOR_DIV_EXPR
:
1315 case TRUNC_MOD_EXPR
:
1317 case EXACT_DIV_EXPR
:
1330 case TRUTH_AND_EXPR
:
1332 case TRUTH_XOR_EXPR
:
1333 case UNORDERED_EXPR
:
1340 /* Binary operations evaluating both arguments (increment and
1341 decrement are binary internally in GCC). */
1342 orig_op0
= op0
= TREE_OPERAND (expr
, 0);
1343 orig_op1
= op1
= TREE_OPERAND (expr
, 1);
1344 op0
= c_fully_fold_internal (op0
, in_init
, maybe_const_operands
,
1345 maybe_const_itself
, for_int_const
);
1346 STRIP_TYPE_NOPS (op0
);
1347 if (code
!= MODIFY_EXPR
1348 && code
!= PREDECREMENT_EXPR
1349 && code
!= PREINCREMENT_EXPR
1350 && code
!= POSTDECREMENT_EXPR
1351 && code
!= POSTINCREMENT_EXPR
)
1352 op0
= decl_constant_value_for_optimization (op0
);
1353 /* The RHS of a MODIFY_EXPR was fully folded when building that
1354 expression for the sake of conversion warnings. */
1355 if (code
!= MODIFY_EXPR
)
1356 op1
= c_fully_fold_internal (op1
, in_init
, maybe_const_operands
,
1357 maybe_const_itself
, for_int_const
);
1358 STRIP_TYPE_NOPS (op1
);
1359 op1
= decl_constant_value_for_optimization (op1
);
1361 if (for_int_const
&& (TREE_CODE (op0
) != INTEGER_CST
1362 || TREE_CODE (op1
) != INTEGER_CST
))
1365 if (op0
!= orig_op0
|| op1
!= orig_op1
|| in_init
)
1367 ? fold_build2_initializer_loc (loc
, code
, TREE_TYPE (expr
), op0
, op1
)
1368 : fold_build2_loc (loc
, code
, TREE_TYPE (expr
), op0
, op1
);
1371 if (TREE_OVERFLOW_P (ret
)
1372 && !TREE_OVERFLOW_P (op0
)
1373 && !TREE_OVERFLOW_P (op1
))
1374 overflow_warning (EXPR_LOCATION (expr
), ret
);
1375 if (code
== LSHIFT_EXPR
1376 && TREE_CODE (orig_op0
) != INTEGER_CST
1377 && TREE_CODE (TREE_TYPE (orig_op0
)) == INTEGER_TYPE
1378 && TREE_CODE (op0
) == INTEGER_CST
1379 && c_inhibit_evaluation_warnings
== 0
1380 && tree_int_cst_sgn (op0
) < 0)
1381 warning_at (loc
, OPT_Wshift_negative_value
,
1382 "left shift of negative value");
1383 if ((code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
1384 && TREE_CODE (orig_op1
) != INTEGER_CST
1385 && TREE_CODE (op1
) == INTEGER_CST
1386 && (TREE_CODE (TREE_TYPE (orig_op0
)) == INTEGER_TYPE
1387 || TREE_CODE (TREE_TYPE (orig_op0
)) == FIXED_POINT_TYPE
)
1388 && TREE_CODE (TREE_TYPE (orig_op1
)) == INTEGER_TYPE
1389 && c_inhibit_evaluation_warnings
== 0)
1391 if (tree_int_cst_sgn (op1
) < 0)
1392 warning_at (loc
, OPT_Wshift_count_negative
,
1393 (code
== LSHIFT_EXPR
1394 ? G_("left shift count is negative")
1395 : G_("right shift count is negative")));
1396 else if (compare_tree_int (op1
,
1397 TYPE_PRECISION (TREE_TYPE (orig_op0
)))
1399 warning_at (loc
, OPT_Wshift_count_overflow
,
1400 (code
== LSHIFT_EXPR
1401 ? G_("left shift count >= width of type")
1402 : G_("right shift count >= width of type")));
1404 if ((code
== TRUNC_DIV_EXPR
1405 || code
== CEIL_DIV_EXPR
1406 || code
== FLOOR_DIV_EXPR
1407 || code
== EXACT_DIV_EXPR
1408 || code
== TRUNC_MOD_EXPR
)
1409 && TREE_CODE (orig_op1
) != INTEGER_CST
1410 && TREE_CODE (op1
) == INTEGER_CST
1411 && (TREE_CODE (TREE_TYPE (orig_op0
)) == INTEGER_TYPE
1412 || TREE_CODE (TREE_TYPE (orig_op0
)) == FIXED_POINT_TYPE
)
1413 && TREE_CODE (TREE_TYPE (orig_op1
)) == INTEGER_TYPE
)
1414 warn_for_div_by_zero (loc
, op1
);
1418 case FIX_TRUNC_EXPR
:
1421 case ADDR_SPACE_CONVERT_EXPR
:
1422 case VIEW_CONVERT_EXPR
:
1423 case NON_LVALUE_EXPR
:
1426 case TRUTH_NOT_EXPR
:
1431 /* Unary operations. */
1432 orig_op0
= op0
= TREE_OPERAND (expr
, 0);
1433 op0
= c_fully_fold_internal (op0
, in_init
, maybe_const_operands
,
1434 maybe_const_itself
, for_int_const
);
1435 STRIP_TYPE_NOPS (op0
);
1436 if (code
!= ADDR_EXPR
&& code
!= REALPART_EXPR
&& code
!= IMAGPART_EXPR
)
1437 op0
= decl_constant_value_for_optimization (op0
);
1439 if (for_int_const
&& TREE_CODE (op0
) != INTEGER_CST
)
1442 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1443 not prepared to deal with them if they occur in initializers. */
1445 && code
== ADDR_EXPR
1446 && (op1
= get_base_address (op0
)) != NULL_TREE
1447 && TREE_CODE (op1
) == INDIRECT_REF
1448 && TREE_CONSTANT (TREE_OPERAND (op1
, 0)))
1449 ret
= fold_convert_loc (loc
, TREE_TYPE (expr
), fold_offsetof_1 (op0
));
1450 else if (op0
!= orig_op0
|| in_init
)
1452 ? fold_build1_initializer_loc (loc
, code
, TREE_TYPE (expr
), op0
)
1453 : fold_build1_loc (loc
, code
, TREE_TYPE (expr
), op0
);
1456 if (code
== INDIRECT_REF
1458 && TREE_CODE (ret
) == INDIRECT_REF
)
1460 TREE_READONLY (ret
) = TREE_READONLY (expr
);
1461 TREE_SIDE_EFFECTS (ret
) = TREE_SIDE_EFFECTS (expr
);
1462 TREE_THIS_VOLATILE (ret
) = TREE_THIS_VOLATILE (expr
);
1466 case FIX_TRUNC_EXPR
:
1469 /* Don't warn about explicit conversions. We will already
1470 have warned about suspect implicit conversions. */
1474 if (TREE_OVERFLOW_P (ret
) && !TREE_OVERFLOW_P (op0
))
1475 overflow_warning (EXPR_LOCATION (expr
), ret
);
1480 case TRUTH_ANDIF_EXPR
:
1481 case TRUTH_ORIF_EXPR
:
1482 /* Binary operations not necessarily evaluating both
1484 orig_op0
= op0
= TREE_OPERAND (expr
, 0);
1485 orig_op1
= op1
= TREE_OPERAND (expr
, 1);
1486 op0
= c_fully_fold_internal (op0
, in_init
, &op0_const
, &op0_const_self
,
1488 STRIP_TYPE_NOPS (op0
);
1490 unused_p
= (op0
== (code
== TRUTH_ANDIF_EXPR
1491 ? truthvalue_false_node
1492 : truthvalue_true_node
));
1493 c_disable_warnings (unused_p
);
1494 op1
= c_fully_fold_internal (op1
, in_init
, &op1_const
, &op1_const_self
,
1496 STRIP_TYPE_NOPS (op1
);
1497 c_enable_warnings (unused_p
);
1500 && (TREE_CODE (op0
) != INTEGER_CST
1501 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1502 || (!unused_p
&& TREE_CODE (op1
) != INTEGER_CST
)))
1505 if (op0
!= orig_op0
|| op1
!= orig_op1
|| in_init
)
1507 ? fold_build2_initializer_loc (loc
, code
, TREE_TYPE (expr
), op0
, op1
)
1508 : fold_build2_loc (loc
, code
, TREE_TYPE (expr
), op0
, op1
);
1511 *maybe_const_operands
&= op0_const
;
1512 *maybe_const_itself
&= op0_const_self
;
1516 && (code
== TRUTH_ANDIF_EXPR
1517 ? op0
== truthvalue_false_node
1518 : op0
== truthvalue_true_node
)))
1519 *maybe_const_operands
&= op1_const
;
1522 && (code
== TRUTH_ANDIF_EXPR
1523 ? op0
== truthvalue_false_node
1524 : op0
== truthvalue_true_node
)))
1525 *maybe_const_itself
&= op1_const_self
;
1529 orig_op0
= op0
= TREE_OPERAND (expr
, 0);
1530 orig_op1
= op1
= TREE_OPERAND (expr
, 1);
1531 orig_op2
= op2
= TREE_OPERAND (expr
, 2);
1532 op0
= c_fully_fold_internal (op0
, in_init
, &op0_const
, &op0_const_self
,
1535 STRIP_TYPE_NOPS (op0
);
1536 c_disable_warnings (op0
== truthvalue_false_node
);
1537 op1
= c_fully_fold_internal (op1
, in_init
, &op1_const
, &op1_const_self
,
1539 STRIP_TYPE_NOPS (op1
);
1540 c_enable_warnings (op0
== truthvalue_false_node
);
1542 c_disable_warnings (op0
== truthvalue_true_node
);
1543 op2
= c_fully_fold_internal (op2
, in_init
, &op2_const
, &op2_const_self
,
1545 STRIP_TYPE_NOPS (op2
);
1546 c_enable_warnings (op0
== truthvalue_true_node
);
1549 && (TREE_CODE (op0
) != INTEGER_CST
1550 /* Only the evaluated operand must be an INTEGER_CST. */
1551 || (op0
== truthvalue_true_node
1552 ? TREE_CODE (op1
) != INTEGER_CST
1553 : TREE_CODE (op2
) != INTEGER_CST
)))
1556 if (op0
!= orig_op0
|| op1
!= orig_op1
|| op2
!= orig_op2
)
1557 ret
= fold_build3_loc (loc
, code
, TREE_TYPE (expr
), op0
, op1
, op2
);
1560 *maybe_const_operands
&= op0_const
;
1561 *maybe_const_itself
&= op0_const_self
;
1565 && op0
== truthvalue_false_node
))
1566 *maybe_const_operands
&= op1_const
;
1569 && op0
== truthvalue_false_node
))
1570 *maybe_const_itself
&= op1_const_self
;
1574 && op0
== truthvalue_true_node
))
1575 *maybe_const_operands
&= op2_const
;
1578 && op0
== truthvalue_true_node
))
1579 *maybe_const_itself
&= op2_const_self
;
1582 case EXCESS_PRECISION_EXPR
:
1583 /* Each case where an operand with excess precision may be
1584 encountered must remove the EXCESS_PRECISION_EXPR around
1585 inner operands and possibly put one around the whole
1586 expression or possibly convert to the semantic type (which
1587 c_fully_fold does); we cannot tell at this stage which is
1588 appropriate in any particular case. */
1592 /* Various codes may appear through folding built-in functions
1593 and their arguments. */
1598 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1599 have been done by this point, so remove them again. */
1600 nowarning
|= TREE_NO_WARNING (ret
);
1601 STRIP_TYPE_NOPS (ret
);
1602 if (nowarning
&& !TREE_NO_WARNING (ret
))
1604 if (!CAN_HAVE_LOCATION_P (ret
))
1605 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
1606 TREE_NO_WARNING (ret
) = 1;
1609 protected_set_expr_location (ret
, loc
);
1613 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1614 return EXP. Otherwise, return either EXP or its known constant
1615 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1616 Is the BLKmode test appropriate? */
1619 decl_constant_value_for_optimization (tree exp
)
1623 /* This function is only used by C, for c_fully_fold and other
1624 optimization, and may not be correct for C++. */
1625 if (c_dialect_cxx ())
1629 || TREE_CODE (exp
) != VAR_DECL
1630 || TREE_CODE (TREE_TYPE (exp
)) == ARRAY_TYPE
1631 || DECL_MODE (exp
) == BLKmode
)
1634 ret
= decl_constant_value (exp
);
1635 /* Avoid unwanted tree sharing between the initializer and current
1636 function's body where the tree can be modified e.g. by the
1638 if (ret
!= exp
&& TREE_STATIC (exp
))
1639 ret
= unshare_expr (ret
);
1643 /* Print a warning if a constant expression had overflow in folding.
1644 Invoke this function on every expression that the language
1645 requires to be a constant expression.
1646 Note the ANSI C standard says it is erroneous for a
1647 constant expression to overflow. */
1650 constant_expression_warning (tree value
)
1652 if (warn_overflow
&& pedantic
1653 && (TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
1654 || TREE_CODE (value
) == FIXED_CST
1655 || TREE_CODE (value
) == VECTOR_CST
1656 || TREE_CODE (value
) == COMPLEX_CST
)
1657 && TREE_OVERFLOW (value
))
1658 pedwarn (input_location
, OPT_Woverflow
, "overflow in constant expression");
1661 /* The same as above but print an unconditional error. */
1663 constant_expression_error (tree value
)
1665 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
1666 || TREE_CODE (value
) == FIXED_CST
1667 || TREE_CODE (value
) == VECTOR_CST
1668 || TREE_CODE (value
) == COMPLEX_CST
)
1669 && TREE_OVERFLOW (value
))
1670 error ("overflow in constant expression");
1673 /* Print a warning if an expression had overflow in folding and its
1676 Invoke this function on every expression that
1677 (1) appears in the source code, and
1678 (2) is a constant expression that overflowed, and
1679 (3) is not already checked by convert_and_check;
1680 however, do not invoke this function on operands of explicit casts
1681 or when the expression is the result of an operator and any operand
1682 already overflowed. */
1685 overflow_warning (location_t loc
, tree value
)
1687 if (c_inhibit_evaluation_warnings
!= 0)
1690 switch (TREE_CODE (value
))
1693 warning_at (loc
, OPT_Woverflow
, "integer overflow in expression");
1697 warning_at (loc
, OPT_Woverflow
,
1698 "floating point overflow in expression");
1702 warning_at (loc
, OPT_Woverflow
, "fixed-point overflow in expression");
1706 warning_at (loc
, OPT_Woverflow
, "vector overflow in expression");
1710 if (TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
)
1711 warning_at (loc
, OPT_Woverflow
,
1712 "complex integer overflow in expression");
1713 else if (TREE_CODE (TREE_REALPART (value
)) == REAL_CST
)
1714 warning_at (loc
, OPT_Woverflow
,
1715 "complex floating point overflow in expression");
1723 /* Warn about uses of logical || / && operator in a context where it
1724 is likely that the bitwise equivalent was intended by the
1725 programmer. We have seen an expression in which CODE is a binary
1726 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1727 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1729 warn_logical_operator (location_t location
, enum tree_code code
, tree type
,
1730 enum tree_code code_left
, tree op_left
,
1731 enum tree_code
ARG_UNUSED (code_right
), tree op_right
)
1733 int or_op
= (code
== TRUTH_ORIF_EXPR
|| code
== TRUTH_OR_EXPR
);
1734 int in0_p
, in1_p
, in_p
;
1735 tree low0
, low1
, low
, high0
, high1
, high
, lhs
, rhs
, tem
;
1736 bool strict_overflow_p
= false;
1738 if (code
!= TRUTH_ANDIF_EXPR
1739 && code
!= TRUTH_AND_EXPR
1740 && code
!= TRUTH_ORIF_EXPR
1741 && code
!= TRUTH_OR_EXPR
)
1744 /* We don't want to warn if either operand comes from a macro
1745 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1747 if (from_macro_expansion_at (EXPR_LOCATION (op_left
))
1748 || from_macro_expansion_at (EXPR_LOCATION (op_right
)))
1751 /* Warn if &&/|| are being used in a context where it is
1752 likely that the bitwise equivalent was intended by the
1753 programmer. That is, an expression such as op && MASK
1754 where op should not be any boolean expression, nor a
1755 constant, and mask seems to be a non-boolean integer constant. */
1756 if (!truth_value_p (code_left
)
1757 && INTEGRAL_TYPE_P (TREE_TYPE (op_left
))
1758 && !CONSTANT_CLASS_P (op_left
)
1759 && !TREE_NO_WARNING (op_left
)
1760 && TREE_CODE (op_right
) == INTEGER_CST
1761 && !integer_zerop (op_right
)
1762 && !integer_onep (op_right
))
1765 warning_at (location
, OPT_Wlogical_op
, "logical %<or%>"
1766 " applied to non-boolean constant");
1768 warning_at (location
, OPT_Wlogical_op
, "logical %<and%>"
1769 " applied to non-boolean constant");
1770 TREE_NO_WARNING (op_left
) = true;
1774 /* We do not warn for constants because they are typical of macro
1775 expansions that test for features. */
1776 if (CONSTANT_CLASS_P (op_left
) || CONSTANT_CLASS_P (op_right
))
1779 /* This warning only makes sense with logical operands. */
1780 if (!(truth_value_p (TREE_CODE (op_left
))
1781 || INTEGRAL_TYPE_P (TREE_TYPE (op_left
)))
1782 || !(truth_value_p (TREE_CODE (op_right
))
1783 || INTEGRAL_TYPE_P (TREE_TYPE (op_right
))))
1786 /* The range computations only work with scalars. */
1787 if (VECTOR_TYPE_P (TREE_TYPE (op_left
))
1788 || VECTOR_TYPE_P (TREE_TYPE (op_right
)))
1791 /* We first test whether either side separately is trivially true
1792 (with OR) or trivially false (with AND). If so, do not warn.
1793 This is a common idiom for testing ranges of data types in
1795 lhs
= make_range (op_left
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
1798 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
1799 lhs
= C_MAYBE_CONST_EXPR_EXPR (lhs
);
1801 /* If this is an OR operation, invert both sides; now, the result
1802 should be always false to get a warning. */
1806 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in0_p
, low0
, high0
);
1807 if (tem
&& integer_zerop (tem
))
1810 rhs
= make_range (op_right
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
1813 if (TREE_CODE (rhs
) == C_MAYBE_CONST_EXPR
)
1814 rhs
= C_MAYBE_CONST_EXPR_EXPR (rhs
);
1816 /* If this is an OR operation, invert both sides; now, the result
1817 should be always false to get a warning. */
1821 tem
= build_range_check (UNKNOWN_LOCATION
, type
, rhs
, in1_p
, low1
, high1
);
1822 if (tem
&& integer_zerop (tem
))
1825 /* If both expressions have the same operand, if we can merge the
1827 if (operand_equal_p (lhs
, rhs
, 0)
1828 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
1829 in1_p
, low1
, high1
))
1831 tem
= build_range_check (UNKNOWN_LOCATION
, type
, lhs
, in_p
, low
, high
);
1832 /* ... and if the range test is always false, then warn. */
1833 if (tem
&& integer_zerop (tem
))
1836 warning_at (location
, OPT_Wlogical_op
,
1837 "logical %<or%> of collectively exhaustive tests is "
1840 warning_at (location
, OPT_Wlogical_op
,
1841 "logical %<and%> of mutually exclusive tests is "
1844 /* Or warn if the operands have exactly the same range, e.g.
1846 else if (low0
== low1
&& high0
== high1
)
1849 warning_at (location
, OPT_Wlogical_op
,
1850 "logical %<or%> of equal expressions");
1852 warning_at (location
, OPT_Wlogical_op
,
1853 "logical %<and%> of equal expressions");
1858 /* Warn about logical not used on the left hand side operand of a comparison.
1859 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1860 Do not warn if RHS is of a boolean type. */
1863 warn_logical_not_parentheses (location_t location
, enum tree_code code
,
1866 if (TREE_CODE_CLASS (code
) != tcc_comparison
1867 || TREE_TYPE (rhs
) == NULL_TREE
1868 || TREE_CODE (TREE_TYPE (rhs
)) == BOOLEAN_TYPE
)
1871 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1872 !(x == 0) or !(y != 0). */
1873 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
1874 && integer_zerop (rhs
))
1877 warning_at (location
, OPT_Wlogical_not_parentheses
,
1878 "logical not is only applied to the left hand side of "
1882 /* Warn if EXP contains any computations whose results are not used.
1883 Return true if a warning is printed; false otherwise. LOCUS is the
1884 (potential) location of the expression. */
1887 warn_if_unused_value (const_tree exp
, location_t locus
)
1890 if (TREE_USED (exp
) || TREE_NO_WARNING (exp
))
1893 /* Don't warn about void constructs. This includes casting to void,
1894 void function calls, and statement expressions with a final cast
1896 if (VOID_TYPE_P (TREE_TYPE (exp
)))
1899 if (EXPR_HAS_LOCATION (exp
))
1900 locus
= EXPR_LOCATION (exp
);
1902 switch (TREE_CODE (exp
))
1904 case PREINCREMENT_EXPR
:
1905 case POSTINCREMENT_EXPR
:
1906 case PREDECREMENT_EXPR
:
1907 case POSTDECREMENT_EXPR
:
1912 case TRY_CATCH_EXPR
:
1913 case WITH_CLEANUP_EXPR
:
1919 /* For a binding, warn if no side effect within it. */
1920 exp
= BIND_EXPR_BODY (exp
);
1924 case NON_LVALUE_EXPR
:
1926 exp
= TREE_OPERAND (exp
, 0);
1929 case TRUTH_ORIF_EXPR
:
1930 case TRUTH_ANDIF_EXPR
:
1931 /* In && or ||, warn if 2nd operand has no side effect. */
1932 exp
= TREE_OPERAND (exp
, 1);
1936 if (warn_if_unused_value (TREE_OPERAND (exp
, 0), locus
))
1938 /* Let people do `(foo (), 0)' without a warning. */
1939 if (TREE_CONSTANT (TREE_OPERAND (exp
, 1)))
1941 exp
= TREE_OPERAND (exp
, 1);
1945 /* If this is an expression with side effects, don't warn; this
1946 case commonly appears in macro expansions. */
1947 if (TREE_SIDE_EFFECTS (exp
))
1952 /* Don't warn about automatic dereferencing of references, since
1953 the user cannot control it. */
1954 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == REFERENCE_TYPE
)
1956 exp
= TREE_OPERAND (exp
, 0);
1962 /* Referencing a volatile value is a side effect, so don't warn. */
1963 if ((DECL_P (exp
) || REFERENCE_CLASS_P (exp
))
1964 && TREE_THIS_VOLATILE (exp
))
1967 /* If this is an expression which has no operands, there is no value
1968 to be unused. There are no such language-independent codes,
1969 but front ends may define such. */
1970 if (EXPRESSION_CLASS_P (exp
) && TREE_OPERAND_LENGTH (exp
) == 0)
1974 return warning_at (locus
, OPT_Wunused_value
, "value computed is not used");
1979 /* Print a warning about casts that might indicate violation
1980 of strict aliasing rules if -Wstrict-aliasing is used and
1981 strict aliasing mode is in effect. OTYPE is the original
1982 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1985 strict_aliasing_warning (tree otype
, tree type
, tree expr
)
1987 /* Strip pointer conversion chains and get to the correct original type. */
1989 otype
= TREE_TYPE (expr
);
1991 if (!(flag_strict_aliasing
1992 && POINTER_TYPE_P (type
)
1993 && POINTER_TYPE_P (otype
)
1994 && !VOID_TYPE_P (TREE_TYPE (type
)))
1995 /* If the type we are casting to is a ref-all pointer
1996 dereferencing it is always valid. */
1997 || TYPE_REF_CAN_ALIAS_ALL (type
))
2000 if ((warn_strict_aliasing
> 1) && TREE_CODE (expr
) == ADDR_EXPR
2001 && (DECL_P (TREE_OPERAND (expr
, 0))
2002 || handled_component_p (TREE_OPERAND (expr
, 0))))
2004 /* Casting the address of an object to non void pointer. Warn
2005 if the cast breaks type based aliasing. */
2006 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)) && warn_strict_aliasing
== 2)
2008 warning (OPT_Wstrict_aliasing
, "type-punning to incomplete type "
2009 "might break strict-aliasing rules");
2014 /* warn_strict_aliasing >= 3. This includes the default (3).
2015 Only warn if the cast is dereferenced immediately. */
2016 alias_set_type set1
=
2017 get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
2018 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
2020 if (set1
!= set2
&& set2
!= 0
2021 && (set1
== 0 || !alias_sets_conflict_p (set1
, set2
)))
2023 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
2024 "pointer will break strict-aliasing rules");
2027 else if (warn_strict_aliasing
== 2
2028 && !alias_sets_must_conflict_p (set1
, set2
))
2030 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
2031 "pointer might break strict-aliasing rules");
2037 if ((warn_strict_aliasing
== 1) && !VOID_TYPE_P (TREE_TYPE (otype
)))
2039 /* At this level, warn for any conversions, even if an address is
2040 not taken in the same statement. This will likely produce many
2041 false positives, but could be useful to pinpoint problems that
2042 are not revealed at higher levels. */
2043 alias_set_type set1
= get_alias_set (TREE_TYPE (otype
));
2044 alias_set_type set2
= get_alias_set (TREE_TYPE (type
));
2045 if (!COMPLETE_TYPE_P (type
)
2046 || !alias_sets_must_conflict_p (set1
, set2
))
2048 warning (OPT_Wstrict_aliasing
, "dereferencing type-punned "
2049 "pointer might break strict-aliasing rules");
2057 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2058 sizeof as last operand of certain builtins. */
2061 sizeof_pointer_memaccess_warning (location_t
*sizeof_arg_loc
, tree callee
,
2062 vec
<tree
, va_gc
> *params
, tree
*sizeof_arg
,
2063 bool (*comp_types
) (tree
, tree
))
2065 tree type
, dest
= NULL_TREE
, src
= NULL_TREE
, tem
;
2066 bool strop
= false, cmp
= false;
2067 unsigned int idx
= ~0;
2070 if (TREE_CODE (callee
) != FUNCTION_DECL
2071 || DECL_BUILT_IN_CLASS (callee
) != BUILT_IN_NORMAL
2072 || vec_safe_length (params
) <= 1)
2075 switch (DECL_FUNCTION_CODE (callee
))
2077 case BUILT_IN_STRNCMP
:
2078 case BUILT_IN_STRNCASECMP
:
2081 case BUILT_IN_STRNCPY
:
2082 case BUILT_IN_STRNCPY_CHK
:
2083 case BUILT_IN_STRNCAT
:
2084 case BUILT_IN_STRNCAT_CHK
:
2085 case BUILT_IN_STPNCPY
:
2086 case BUILT_IN_STPNCPY_CHK
:
2089 case BUILT_IN_MEMCPY
:
2090 case BUILT_IN_MEMCPY_CHK
:
2091 case BUILT_IN_MEMMOVE
:
2092 case BUILT_IN_MEMMOVE_CHK
:
2093 if (params
->length () < 3)
2096 dest
= (*params
)[0];
2099 case BUILT_IN_BCOPY
:
2100 if (params
->length () < 3)
2103 dest
= (*params
)[1];
2106 case BUILT_IN_MEMCMP
:
2108 if (params
->length () < 3)
2111 dest
= (*params
)[0];
2115 case BUILT_IN_MEMSET
:
2116 case BUILT_IN_MEMSET_CHK
:
2117 if (params
->length () < 3)
2119 dest
= (*params
)[0];
2122 case BUILT_IN_BZERO
:
2123 dest
= (*params
)[0];
2126 case BUILT_IN_STRNDUP
:
2131 case BUILT_IN_MEMCHR
:
2132 if (params
->length () < 3)
2137 case BUILT_IN_SNPRINTF
:
2138 case BUILT_IN_SNPRINTF_CHK
:
2139 case BUILT_IN_VSNPRINTF
:
2140 case BUILT_IN_VSNPRINTF_CHK
:
2141 dest
= (*params
)[0];
2152 if (sizeof_arg
[idx
] == NULL
|| sizeof_arg
[idx
] == error_mark_node
)
2155 type
= TYPE_P (sizeof_arg
[idx
])
2156 ? sizeof_arg
[idx
] : TREE_TYPE (sizeof_arg
[idx
]);
2157 if (!POINTER_TYPE_P (type
))
2161 && (tem
= tree_strip_nop_conversions (dest
))
2162 && POINTER_TYPE_P (TREE_TYPE (tem
))
2163 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
2167 && (tem
= tree_strip_nop_conversions (src
))
2168 && POINTER_TYPE_P (TREE_TYPE (tem
))
2169 && comp_types (TREE_TYPE (TREE_TYPE (tem
)), type
))
2172 loc
= sizeof_arg_loc
[idx
];
2176 if (!TYPE_P (sizeof_arg
[idx
])
2177 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
2178 && comp_types (TREE_TYPE (dest
), type
))
2180 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
2181 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2182 "argument to %<sizeof%> in %qD call is the same "
2183 "expression as the destination; did you mean to "
2184 "remove the addressof?", callee
);
2185 else if ((TYPE_PRECISION (TREE_TYPE (type
))
2186 == TYPE_PRECISION (char_type_node
))
2188 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2189 "argument to %<sizeof%> in %qD call is the same "
2190 "expression as the destination; did you mean to "
2191 "provide an explicit length?", callee
);
2193 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2194 "argument to %<sizeof%> in %qD call is the same "
2195 "expression as the destination; did you mean to "
2196 "dereference it?", callee
);
2200 if (POINTER_TYPE_P (TREE_TYPE (dest
))
2202 && comp_types (TREE_TYPE (dest
), type
)
2203 && !VOID_TYPE_P (TREE_TYPE (type
)))
2205 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2206 "argument to %<sizeof%> in %qD call is the same "
2207 "pointer type %qT as the destination; expected %qT "
2208 "or an explicit length", callee
, TREE_TYPE (dest
),
2209 TREE_TYPE (TREE_TYPE (dest
)));
2216 if (!TYPE_P (sizeof_arg
[idx
])
2217 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
2218 && comp_types (TREE_TYPE (src
), type
))
2220 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
2221 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2222 "argument to %<sizeof%> in %qD call is the same "
2223 "expression as the source; did you mean to "
2224 "remove the addressof?", callee
);
2225 else if ((TYPE_PRECISION (TREE_TYPE (type
))
2226 == TYPE_PRECISION (char_type_node
))
2228 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2229 "argument to %<sizeof%> in %qD call is the same "
2230 "expression as the source; did you mean to "
2231 "provide an explicit length?", callee
);
2233 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2234 "argument to %<sizeof%> in %qD call is the same "
2235 "expression as the source; did you mean to "
2236 "dereference it?", callee
);
2240 if (POINTER_TYPE_P (TREE_TYPE (src
))
2242 && comp_types (TREE_TYPE (src
), type
)
2243 && !VOID_TYPE_P (TREE_TYPE (type
)))
2245 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2246 "argument to %<sizeof%> in %qD call is the same "
2247 "pointer type %qT as the source; expected %qT "
2248 "or an explicit length", callee
, TREE_TYPE (src
),
2249 TREE_TYPE (TREE_TYPE (src
)));
2256 if (!TYPE_P (sizeof_arg
[idx
])
2257 && operand_equal_p (dest
, sizeof_arg
[idx
], 0)
2258 && comp_types (TREE_TYPE (dest
), type
))
2260 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
2261 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2262 "argument to %<sizeof%> in %qD call is the same "
2263 "expression as the first source; did you mean to "
2264 "remove the addressof?", callee
);
2265 else if ((TYPE_PRECISION (TREE_TYPE (type
))
2266 == TYPE_PRECISION (char_type_node
))
2268 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2269 "argument to %<sizeof%> in %qD call is the same "
2270 "expression as the first source; did you mean to "
2271 "provide an explicit length?", callee
);
2273 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2274 "argument to %<sizeof%> in %qD call is the same "
2275 "expression as the first source; did you mean to "
2276 "dereference it?", callee
);
2280 if (POINTER_TYPE_P (TREE_TYPE (dest
))
2282 && comp_types (TREE_TYPE (dest
), type
)
2283 && !VOID_TYPE_P (TREE_TYPE (type
)))
2285 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2286 "argument to %<sizeof%> in %qD call is the same "
2287 "pointer type %qT as the first source; expected %qT "
2288 "or an explicit length", callee
, TREE_TYPE (dest
),
2289 TREE_TYPE (TREE_TYPE (dest
)));
2296 if (!TYPE_P (sizeof_arg
[idx
])
2297 && operand_equal_p (src
, sizeof_arg
[idx
], 0)
2298 && comp_types (TREE_TYPE (src
), type
))
2300 if (TREE_CODE (sizeof_arg
[idx
]) == ADDR_EXPR
&& !strop
)
2301 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2302 "argument to %<sizeof%> in %qD call is the same "
2303 "expression as the second source; did you mean to "
2304 "remove the addressof?", callee
);
2305 else if ((TYPE_PRECISION (TREE_TYPE (type
))
2306 == TYPE_PRECISION (char_type_node
))
2308 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2309 "argument to %<sizeof%> in %qD call is the same "
2310 "expression as the second source; did you mean to "
2311 "provide an explicit length?", callee
);
2313 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2314 "argument to %<sizeof%> in %qD call is the same "
2315 "expression as the second source; did you mean to "
2316 "dereference it?", callee
);
2320 if (POINTER_TYPE_P (TREE_TYPE (src
))
2322 && comp_types (TREE_TYPE (src
), type
)
2323 && !VOID_TYPE_P (TREE_TYPE (type
)))
2325 warning_at (loc
, OPT_Wsizeof_pointer_memaccess
,
2326 "argument to %<sizeof%> in %qD call is the same "
2327 "pointer type %qT as the second source; expected %qT "
2328 "or an explicit length", callee
, TREE_TYPE (src
),
2329 TREE_TYPE (TREE_TYPE (src
)));
2336 /* Warn for unlikely, improbable, or stupid DECL declarations
2340 check_main_parameter_types (tree decl
)
2342 function_args_iterator iter
;
2346 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl
), type
, iter
)
2348 /* XXX void_type_node belies the abstraction. */
2349 if (type
== void_type_node
|| type
== error_mark_node
)
2353 if (TYPE_ATOMIC (t
))
2354 pedwarn (input_location
, OPT_Wmain
,
2355 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2357 while (POINTER_TYPE_P (t
))
2360 if (TYPE_ATOMIC (t
))
2361 pedwarn (input_location
, OPT_Wmain
,
2362 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2370 if (TYPE_MAIN_VARIANT (type
) != integer_type_node
)
2371 pedwarn (input_location
, OPT_Wmain
,
2372 "first argument of %q+D should be %<int%>", decl
);
2376 if (TREE_CODE (type
) != POINTER_TYPE
2377 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
2378 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
2380 pedwarn (input_location
, OPT_Wmain
,
2381 "second argument of %q+D should be %<char **%>", decl
);
2385 if (TREE_CODE (type
) != POINTER_TYPE
2386 || TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
2387 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
2389 pedwarn (input_location
, OPT_Wmain
,
2390 "third argument of %q+D should probably be "
2391 "%<char **%>", decl
);
2396 /* It is intentional that this message does not mention the third
2397 argument because it's only mentioned in an appendix of the
2399 if (argct
> 0 && (argct
< 2 || argct
> 3))
2400 pedwarn (input_location
, OPT_Wmain
,
2401 "%q+D takes only zero or two arguments", decl
);
2403 if (stdarg_p (TREE_TYPE (decl
)))
2404 pedwarn (input_location
, OPT_Wmain
,
2405 "%q+D declared as variadic function", decl
);
2408 /* vector_targets_convertible_p is used for vector pointer types. The
2409 callers perform various checks that the qualifiers are satisfactory,
2410 while OTOH vector_targets_convertible_p ignores the number of elements
2411 in the vectors. That's fine with vector pointers as we can consider,
2412 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2413 and that does not require and conversion of the pointer values.
2414 In contrast, vector_types_convertible_p and
2415 vector_types_compatible_elements_p are used for vector value types. */
2416 /* True if pointers to distinct types T1 and T2 can be converted to
2417 each other without an explicit cast. Only returns true for opaque
2420 vector_targets_convertible_p (const_tree t1
, const_tree t2
)
2422 if (TREE_CODE (t1
) == VECTOR_TYPE
&& TREE_CODE (t2
) == VECTOR_TYPE
2423 && (TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
))
2424 && tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
)))
2430 /* vector_types_convertible_p is used for vector value types.
2431 It could in principle call vector_targets_convertible_p as a subroutine,
2432 but then the check for vector type would be duplicated with its callers,
2433 and also the purpose of vector_targets_convertible_p would become
2435 Where vector_types_convertible_p returns true, a conversion might still be
2436 needed to make the types match.
2437 In contrast, vector_targets_convertible_p is used for vector pointer
2438 values, and vector_types_compatible_elements_p is used specifically
2439 in the context for binary operators, as a check if use is possible without
2441 /* True if vector types T1 and T2 can be converted to each other
2442 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2443 can only be converted with -flax-vector-conversions yet that is not
2444 in effect, emit a note telling the user about that option if such
2445 a note has not previously been emitted. */
2447 vector_types_convertible_p (const_tree t1
, const_tree t2
, bool emit_lax_note
)
2449 static bool emitted_lax_note
= false;
2450 bool convertible_lax
;
2452 if ((TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
))
2453 && tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
)))
2457 (tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
))
2458 && (TREE_CODE (TREE_TYPE (t1
)) != REAL_TYPE
||
2459 TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
))
2460 && (INTEGRAL_TYPE_P (TREE_TYPE (t1
))
2461 == INTEGRAL_TYPE_P (TREE_TYPE (t2
))));
2463 if (!convertible_lax
|| flag_lax_vector_conversions
)
2464 return convertible_lax
;
2466 if (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
2467 && lang_hooks
.types_compatible_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
2470 if (emit_lax_note
&& !emitted_lax_note
)
2472 emitted_lax_note
= true;
2473 inform (input_location
, "use -flax-vector-conversions to permit "
2474 "conversions between vectors with differing "
2475 "element types or numbers of subparts");
2481 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2482 and have vector types, V0 has the same type as V1, and the number of
2483 elements of V0, V1, MASK is the same.
2485 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2486 called with two arguments. In this case implementation passes the
2487 first argument twice in order to share the same tree code. This fact
2488 could enable the mask-values being twice the vector length. This is
2489 an implementation accident and this semantics is not guaranteed to
2492 c_build_vec_perm_expr (location_t loc
, tree v0
, tree v1
, tree mask
,
2497 bool maybe_const
= false;
2498 bool two_arguments
= false;
2500 if (v1
== NULL_TREE
)
2502 two_arguments
= true;
2506 if (v0
== error_mark_node
|| v1
== error_mark_node
2507 || mask
== error_mark_node
)
2508 return error_mark_node
;
2510 if (TREE_CODE (TREE_TYPE (mask
)) != VECTOR_TYPE
2511 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask
))) != INTEGER_TYPE
)
2514 error_at (loc
, "__builtin_shuffle last argument must "
2515 "be an integer vector");
2516 return error_mark_node
;
2519 if (TREE_CODE (TREE_TYPE (v0
)) != VECTOR_TYPE
2520 || TREE_CODE (TREE_TYPE (v1
)) != VECTOR_TYPE
)
2523 error_at (loc
, "__builtin_shuffle arguments must be vectors");
2524 return error_mark_node
;
2527 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0
)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1
)))
2530 error_at (loc
, "__builtin_shuffle argument vectors must be of "
2532 return error_mark_node
;
2535 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0
))
2536 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask
))
2537 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1
))
2538 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask
)))
2541 error_at (loc
, "__builtin_shuffle number of elements of the "
2542 "argument vector(s) and the mask vector should "
2544 return error_mark_node
;
2547 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0
))))
2548 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask
)))))
2551 error_at (loc
, "__builtin_shuffle argument vector(s) inner type "
2552 "must have the same size as inner type of the mask");
2553 return error_mark_node
;
2556 if (!c_dialect_cxx ())
2558 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2559 v0
= c_fully_fold (v0
, false, &maybe_const
);
2560 wrap
&= maybe_const
;
2563 v1
= v0
= save_expr (v0
);
2566 v1
= c_fully_fold (v1
, false, &maybe_const
);
2567 wrap
&= maybe_const
;
2570 mask
= c_fully_fold (mask
, false, &maybe_const
);
2571 wrap
&= maybe_const
;
2573 else if (two_arguments
)
2574 v1
= v0
= save_expr (v0
);
2576 ret
= build3_loc (loc
, VEC_PERM_EXPR
, TREE_TYPE (v0
), v0
, v1
, mask
);
2578 if (!c_dialect_cxx () && !wrap
)
2579 ret
= c_wrap_maybe_const (ret
, true);
2584 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2585 to integral type. */
2588 c_common_get_narrower (tree op
, int *unsignedp_ptr
)
2590 op
= get_narrower (op
, unsignedp_ptr
);
2592 if (TREE_CODE (TREE_TYPE (op
)) == ENUMERAL_TYPE
2593 && ENUM_IS_SCOPED (TREE_TYPE (op
)))
2595 /* C++0x scoped enumerations don't implicitly convert to integral
2596 type; if we stripped an explicit conversion to a larger type we
2597 need to replace it so common_type will still work. */
2598 tree type
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op
)),
2599 TYPE_UNSIGNED (TREE_TYPE (op
)));
2600 op
= fold_convert (type
, op
);
2605 /* This is a helper function of build_binary_op.
2607 For certain operations if both args were extended from the same
2608 smaller type, do the arithmetic in that type and then extend.
2610 BITWISE indicates a bitwise operation.
2611 For them, this optimization is safe only if
2612 both args are zero-extended or both are sign-extended.
2613 Otherwise, we might change the result.
2614 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2615 but calculated in (unsigned short) it would be (unsigned short)-1.
2618 shorten_binary_op (tree result_type
, tree op0
, tree op1
, bool bitwise
)
2620 int unsigned0
, unsigned1
;
2625 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2626 excessive narrowing when we call get_narrower below. For
2627 example, suppose that OP0 is of unsigned int extended
2628 from signed char and that RESULT_TYPE is long long int.
2629 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2632 (long long int) (unsigned int) signed_char
2634 which get_narrower would narrow down to
2636 (unsigned int) signed char
2638 If we do not cast OP0 first, get_narrower would return
2639 signed_char, which is inconsistent with the case of the
2641 op0
= convert (result_type
, op0
);
2642 op1
= convert (result_type
, op1
);
2644 arg0
= c_common_get_narrower (op0
, &unsigned0
);
2645 arg1
= c_common_get_narrower (op1
, &unsigned1
);
2647 /* UNS is 1 if the operation to be done is an unsigned one. */
2648 uns
= TYPE_UNSIGNED (result_type
);
2650 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2651 but it *requires* conversion to FINAL_TYPE. */
2653 if ((TYPE_PRECISION (TREE_TYPE (op0
))
2654 == TYPE_PRECISION (TREE_TYPE (arg0
)))
2655 && TREE_TYPE (op0
) != result_type
)
2656 unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
2657 if ((TYPE_PRECISION (TREE_TYPE (op1
))
2658 == TYPE_PRECISION (TREE_TYPE (arg1
)))
2659 && TREE_TYPE (op1
) != result_type
)
2660 unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
2662 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2664 /* For bitwise operations, signedness of nominal type
2665 does not matter. Consider only how operands were extended. */
2669 /* Note that in all three cases below we refrain from optimizing
2670 an unsigned operation on sign-extended args.
2671 That would not be valid. */
2673 /* Both args variable: if both extended in same way
2674 from same width, do it in that width.
2675 Do it unsigned if args were zero-extended. */
2676 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
2677 < TYPE_PRECISION (result_type
))
2678 && (TYPE_PRECISION (TREE_TYPE (arg1
))
2679 == TYPE_PRECISION (TREE_TYPE (arg0
)))
2680 && unsigned0
== unsigned1
2681 && (unsigned0
|| !uns
))
2682 return c_common_signed_or_unsigned_type
2683 (unsigned0
, common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
2685 else if (TREE_CODE (arg0
) == INTEGER_CST
2686 && (unsigned1
|| !uns
)
2687 && (TYPE_PRECISION (TREE_TYPE (arg1
))
2688 < TYPE_PRECISION (result_type
))
2690 = c_common_signed_or_unsigned_type (unsigned1
,
2692 && !POINTER_TYPE_P (type
)
2693 && int_fits_type_p (arg0
, type
))
2696 else if (TREE_CODE (arg1
) == INTEGER_CST
2697 && (unsigned0
|| !uns
)
2698 && (TYPE_PRECISION (TREE_TYPE (arg0
))
2699 < TYPE_PRECISION (result_type
))
2701 = c_common_signed_or_unsigned_type (unsigned0
,
2703 && !POINTER_TYPE_P (type
)
2704 && int_fits_type_p (arg1
, type
))
2710 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2711 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2714 int_safely_convertible_to_real_p (const_tree from_type
, const_tree to_type
)
2716 tree type_low_bound
= TYPE_MIN_VALUE (from_type
);
2717 tree type_high_bound
= TYPE_MAX_VALUE (from_type
);
2718 REAL_VALUE_TYPE real_low_bound
=
2719 real_value_from_int_cst (0, type_low_bound
);
2720 REAL_VALUE_TYPE real_high_bound
=
2721 real_value_from_int_cst (0, type_high_bound
);
2723 return exact_real_truncate (TYPE_MODE (to_type
), &real_low_bound
)
2724 && exact_real_truncate (TYPE_MODE (to_type
), &real_high_bound
);
2727 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2728 to the complex/real/integer type TYPE. Function returns non-zero when:
2729 * EXPR is a constant which cannot be exactly converted to TYPE.
2730 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2731 for EXPR type and TYPE being both integers or both real, or both
2733 * EXPR is not a constant of complex type and TYPE is a real or
2735 * EXPR is not a constant of real type and TYPE is an integer.
2736 * EXPR is not a constant of integer type which cannot be
2737 exactly converted to real type.
2739 Function allows conversions between types of different signedness and
2740 can return SAFE_CONVERSION (zero) in that case. Function can produce
2741 signedness warnings if PRODUCE_WARNS is true.
2743 Function allows conversions from complex constants to non-complex types,
2744 provided that imaginary part is zero and real part can be safely converted
2747 enum conversion_safety
2748 unsafe_conversion_p (location_t loc
, tree type
, tree expr
, bool produce_warns
)
2750 enum conversion_safety give_warning
= SAFE_CONVERSION
; /* is 0 or false */
2751 tree expr_type
= TREE_TYPE (expr
);
2752 loc
= expansion_point_location_if_in_system_header (loc
);
2754 if (TREE_CODE (expr
) == REAL_CST
|| TREE_CODE (expr
) == INTEGER_CST
)
2756 /* If type is complex, we are interested in compatibility with
2758 if (TREE_CODE (type
) == COMPLEX_TYPE
)
2759 type
= TREE_TYPE (type
);
2761 /* Warn for real constant that is not an exact integer converted
2763 if (TREE_CODE (expr_type
) == REAL_TYPE
2764 && TREE_CODE (type
) == INTEGER_TYPE
)
2766 if (!real_isinteger (TREE_REAL_CST_PTR (expr
), TYPE_MODE (expr_type
)))
2767 give_warning
= UNSAFE_REAL
;
2769 /* Warn for an integer constant that does not fit into integer type. */
2770 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
2771 && TREE_CODE (type
) == INTEGER_TYPE
2772 && !int_fits_type_p (expr
, type
))
2774 if (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (expr_type
)
2775 && tree_int_cst_sgn (expr
) < 0)
2778 warning_at (loc
, OPT_Wsign_conversion
, "negative integer"
2779 " implicitly converted to unsigned type");
2781 else if (!TYPE_UNSIGNED (type
) && TYPE_UNSIGNED (expr_type
))
2784 warning_at (loc
, OPT_Wsign_conversion
, "conversion of unsigned"
2785 " constant value to negative integer");
2788 give_warning
= UNSAFE_OTHER
;
2790 else if (TREE_CODE (type
) == REAL_TYPE
)
2792 /* Warn for an integer constant that does not fit into real type. */
2793 if (TREE_CODE (expr_type
) == INTEGER_TYPE
)
2795 REAL_VALUE_TYPE a
= real_value_from_int_cst (0, expr
);
2796 if (!exact_real_truncate (TYPE_MODE (type
), &a
))
2797 give_warning
= UNSAFE_REAL
;
2799 /* Warn for a real constant that does not fit into a smaller
2801 else if (TREE_CODE (expr_type
) == REAL_TYPE
2802 && TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
2804 REAL_VALUE_TYPE a
= TREE_REAL_CST (expr
);
2805 if (!exact_real_truncate (TYPE_MODE (type
), &a
))
2806 give_warning
= UNSAFE_REAL
;
2811 else if (TREE_CODE (expr
) == COMPLEX_CST
)
2813 tree imag_part
= TREE_IMAGPART (expr
);
2814 /* Conversion from complex constant with zero imaginary part,
2815 perform check for conversion of real part. */
2816 if ((TREE_CODE (imag_part
) == REAL_CST
2817 && real_zerop (imag_part
))
2818 || (TREE_CODE (imag_part
) == INTEGER_CST
2819 && integer_zerop (imag_part
)))
2820 /* Note: in this branch we use recursive call to unsafe_conversion_p
2821 with different type of EXPR, but it is still safe, because when EXPR
2822 is a constant, it's type is not used in text of generated warnings
2823 (otherwise they could sound misleading). */
2824 return unsafe_conversion_p (loc
, type
, TREE_REALPART (expr
),
2826 /* Conversion from complex constant with non-zero imaginary part. */
2829 /* Conversion to complex type.
2830 Perform checks for both real and imaginary parts. */
2831 if (TREE_CODE (type
) == COMPLEX_TYPE
)
2833 /* Unfortunately, produce_warns must be false in two subsequent
2834 calls of unsafe_conversion_p, because otherwise we could
2835 produce strange "double" warnings, if both real and imaginary
2836 parts have conversion problems related to signedness.
2839 int32_t _Complex a = 0x80000000 + 0x80000000i;
2841 Possible solution: add a separate function for checking
2842 constants and combine result of two calls appropriately. */
2843 enum conversion_safety re_safety
=
2844 unsafe_conversion_p (loc
, type
, TREE_REALPART (expr
), false);
2845 enum conversion_safety im_safety
=
2846 unsafe_conversion_p (loc
, type
, imag_part
, false);
2848 /* Merge the results into appropriate single warning. */
2850 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2851 if (re_safety
== im_safety
)
2852 give_warning
= re_safety
;
2853 else if (!re_safety
&& im_safety
)
2854 give_warning
= im_safety
;
2855 else if (re_safety
&& !im_safety
)
2856 give_warning
= re_safety
;
2858 give_warning
= UNSAFE_OTHER
;
2860 /* Warn about conversion from complex to real or integer type. */
2862 give_warning
= UNSAFE_IMAGINARY
;
2866 /* Checks for remaining case: EXPR is not constant. */
2869 /* Warn for real types converted to integer types. */
2870 if (TREE_CODE (expr_type
) == REAL_TYPE
2871 && TREE_CODE (type
) == INTEGER_TYPE
)
2872 give_warning
= UNSAFE_REAL
;
2874 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
2875 && TREE_CODE (type
) == INTEGER_TYPE
)
2877 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2878 expr
= get_unwidened (expr
, 0);
2879 expr_type
= TREE_TYPE (expr
);
2881 /* Don't warn for short y; short x = ((int)y & 0xff); */
2882 if (TREE_CODE (expr
) == BIT_AND_EXPR
2883 || TREE_CODE (expr
) == BIT_IOR_EXPR
2884 || TREE_CODE (expr
) == BIT_XOR_EXPR
)
2886 /* If both args were extended from a shortest type,
2887 use that type if that is safe. */
2888 expr_type
= shorten_binary_op (expr_type
,
2889 TREE_OPERAND (expr
, 0),
2890 TREE_OPERAND (expr
, 1),
2893 if (TREE_CODE (expr
) == BIT_AND_EXPR
)
2895 tree op0
= TREE_OPERAND (expr
, 0);
2896 tree op1
= TREE_OPERAND (expr
, 1);
2897 bool unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
2898 bool unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
2900 /* If one of the operands is a non-negative constant
2901 that fits in the target type, then the type of the
2902 other operand does not matter. */
2903 if ((TREE_CODE (op0
) == INTEGER_CST
2904 && int_fits_type_p (op0
, c_common_signed_type (type
))
2905 && int_fits_type_p (op0
, c_common_unsigned_type (type
)))
2906 || (TREE_CODE (op1
) == INTEGER_CST
2907 && int_fits_type_p (op1
, c_common_signed_type (type
))
2908 && int_fits_type_p (op1
,
2909 c_common_unsigned_type (type
))))
2910 return SAFE_CONVERSION
;
2911 /* If constant is unsigned and fits in the target
2912 type, then the result will also fit. */
2913 else if ((TREE_CODE (op0
) == INTEGER_CST
2915 && int_fits_type_p (op0
, type
))
2916 || (TREE_CODE (op1
) == INTEGER_CST
2918 && int_fits_type_p (op1
, type
)))
2919 return SAFE_CONVERSION
;
2922 /* Warn for integer types converted to smaller integer types. */
2923 if (TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
2924 give_warning
= UNSAFE_OTHER
;
2926 /* When they are the same width but different signedness,
2927 then the value may change. */
2928 else if (((TYPE_PRECISION (type
) == TYPE_PRECISION (expr_type
)
2929 && TYPE_UNSIGNED (expr_type
) != TYPE_UNSIGNED (type
))
2930 /* Even when converted to a bigger type, if the type is
2931 unsigned but expr is signed, then negative values
2933 || (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (expr_type
)))
2935 warning_at (loc
, OPT_Wsign_conversion
, "conversion to %qT from %qT "
2936 "may change the sign of the result",
2940 /* Warn for integer types converted to real types if and only if
2941 all the range of values of the integer type cannot be
2942 represented by the real type. */
2943 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
2944 && TREE_CODE (type
) == REAL_TYPE
)
2946 /* Don't warn about char y = 0xff; float x = (int) y; */
2947 expr
= get_unwidened (expr
, 0);
2948 expr_type
= TREE_TYPE (expr
);
2950 if (!int_safely_convertible_to_real_p (expr_type
, type
))
2951 give_warning
= UNSAFE_OTHER
;
2954 /* Warn for real types converted to smaller real types. */
2955 else if (TREE_CODE (expr_type
) == REAL_TYPE
2956 && TREE_CODE (type
) == REAL_TYPE
2957 && TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
2958 give_warning
= UNSAFE_REAL
;
2960 /* Check conversion between two complex types. */
2961 else if (TREE_CODE (expr_type
) == COMPLEX_TYPE
2962 && TREE_CODE (type
) == COMPLEX_TYPE
)
2964 /* Extract underlying types (i.e., type of real and imaginary
2965 parts) of expr_type and type. */
2966 tree from_type
= TREE_TYPE (expr_type
);
2967 tree to_type
= TREE_TYPE (type
);
2969 /* Warn for real types converted to integer types. */
2970 if (TREE_CODE (from_type
) == REAL_TYPE
2971 && TREE_CODE (to_type
) == INTEGER_TYPE
)
2972 give_warning
= UNSAFE_REAL
;
2974 /* Warn for real types converted to smaller real types. */
2975 else if (TREE_CODE (from_type
) == REAL_TYPE
2976 && TREE_CODE (to_type
) == REAL_TYPE
2977 && TYPE_PRECISION (to_type
) < TYPE_PRECISION (from_type
))
2978 give_warning
= UNSAFE_REAL
;
2980 /* Check conversion for complex integer types. Here implementation
2981 is simpler than for real-domain integers because it does not
2982 involve sophisticated cases, such as bitmasks, casts, etc. */
2983 else if (TREE_CODE (from_type
) == INTEGER_TYPE
2984 && TREE_CODE (to_type
) == INTEGER_TYPE
)
2986 /* Warn for integer types converted to smaller integer types. */
2987 if (TYPE_PRECISION (to_type
) < TYPE_PRECISION (from_type
))
2988 give_warning
= UNSAFE_OTHER
;
2990 /* Check for different signedness, see case for real-domain
2991 integers (above) for a more detailed comment. */
2992 else if (((TYPE_PRECISION (to_type
) == TYPE_PRECISION (from_type
)
2993 && TYPE_UNSIGNED (to_type
) != TYPE_UNSIGNED (from_type
))
2994 || (TYPE_UNSIGNED (to_type
) && !TYPE_UNSIGNED (from_type
)))
2996 warning_at (loc
, OPT_Wsign_conversion
,
2997 "conversion to %qT from %qT "
2998 "may change the sign of the result",
3001 else if (TREE_CODE (from_type
) == INTEGER_TYPE
3002 && TREE_CODE (to_type
) == REAL_TYPE
3003 && !int_safely_convertible_to_real_p (from_type
, to_type
))
3004 give_warning
= UNSAFE_OTHER
;
3007 /* Warn for complex types converted to real or integer types. */
3008 else if (TREE_CODE (expr_type
) == COMPLEX_TYPE
3009 && TREE_CODE (type
) != COMPLEX_TYPE
)
3010 give_warning
= UNSAFE_IMAGINARY
;
3013 return give_warning
;
3016 /* Warns if the conversion of EXPR to TYPE may alter a value.
3017 This is a helper function for warnings_for_convert_and_check. */
3020 conversion_warning (location_t loc
, tree type
, tree expr
)
3022 tree expr_type
= TREE_TYPE (expr
);
3023 enum conversion_safety conversion_kind
;
3025 if (!warn_conversion
&& !warn_sign_conversion
&& !warn_float_conversion
)
3028 /* This may happen, because for LHS op= RHS we preevaluate
3029 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3030 means we could no longer see the code of the EXPR. */
3031 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
3032 expr
= C_MAYBE_CONST_EXPR_EXPR (expr
);
3033 if (TREE_CODE (expr
) == SAVE_EXPR
)
3034 expr
= TREE_OPERAND (expr
, 0);
3036 switch (TREE_CODE (expr
))
3044 case TRUTH_ANDIF_EXPR
:
3045 case TRUTH_ORIF_EXPR
:
3046 case TRUTH_AND_EXPR
:
3048 case TRUTH_XOR_EXPR
:
3049 case TRUTH_NOT_EXPR
:
3050 /* Conversion from boolean to a signed:1 bit-field (which only
3051 can hold the values 0 and -1) doesn't lose information - but
3052 it does change the value. */
3053 if (TYPE_PRECISION (type
) == 1 && !TYPE_UNSIGNED (type
))
3054 warning_at (loc
, OPT_Wconversion
,
3055 "conversion to %qT from boolean expression", type
);
3061 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, true);
3062 if (conversion_kind
== UNSAFE_REAL
)
3063 warning_at (loc
, OPT_Wfloat_conversion
,
3064 "conversion to %qT alters %qT constant value",
3066 else if (conversion_kind
)
3067 warning_at (loc
, OPT_Wconversion
,
3068 "conversion to %qT alters %qT constant value",
3074 /* In case of COND_EXPR, we do not care about the type of
3075 COND_EXPR, only about the conversion of each operand. */
3076 tree op1
= TREE_OPERAND (expr
, 1);
3077 tree op2
= TREE_OPERAND (expr
, 2);
3079 conversion_warning (loc
, type
, op1
);
3080 conversion_warning (loc
, type
, op2
);
3084 default: /* 'expr' is not a constant. */
3085 conversion_kind
= unsafe_conversion_p (loc
, type
, expr
, true);
3086 if (conversion_kind
== UNSAFE_REAL
)
3087 warning_at (loc
, OPT_Wfloat_conversion
,
3088 "conversion to %qT from %qT may alter its value",
3090 else if (conversion_kind
== UNSAFE_IMAGINARY
)
3091 warning_at (loc
, OPT_Wconversion
,
3092 "conversion to %qT from %qT discards imaginary component",
3094 else if (conversion_kind
)
3095 warning_at (loc
, OPT_Wconversion
,
3096 "conversion to %qT from %qT may alter its value",
3101 /* Produce warnings after a conversion. RESULT is the result of
3102 converting EXPR to TYPE. This is a helper function for
3103 convert_and_check and cp_convert_and_check. */
3106 warnings_for_convert_and_check (location_t loc
, tree type
, tree expr
,
3109 loc
= expansion_point_location_if_in_system_header (loc
);
3111 if (TREE_CODE (expr
) == INTEGER_CST
3112 && (TREE_CODE (type
) == INTEGER_TYPE
3113 || TREE_CODE (type
) == ENUMERAL_TYPE
)
3114 && !int_fits_type_p (expr
, type
))
3116 /* Do not diagnose overflow in a constant expression merely
3117 because a conversion overflowed. */
3118 if (TREE_OVERFLOW (result
))
3119 TREE_OVERFLOW (result
) = TREE_OVERFLOW (expr
);
3121 if (TYPE_UNSIGNED (type
))
3123 /* This detects cases like converting -129 or 256 to
3125 if (!int_fits_type_p (expr
, c_common_signed_type (type
)))
3126 warning_at (loc
, OPT_Woverflow
,
3127 "large integer implicitly truncated to unsigned type");
3129 conversion_warning (loc
, type
, expr
);
3131 else if (!int_fits_type_p (expr
, c_common_unsigned_type (type
)))
3132 warning_at (loc
, OPT_Woverflow
,
3133 "overflow in implicit constant conversion");
3134 /* No warning for converting 0x80000000 to int. */
3136 && (TREE_CODE (TREE_TYPE (expr
)) != INTEGER_TYPE
3137 || TYPE_PRECISION (TREE_TYPE (expr
))
3138 != TYPE_PRECISION (type
)))
3139 warning_at (loc
, OPT_Woverflow
,
3140 "overflow in implicit constant conversion");
3143 conversion_warning (loc
, type
, expr
);
3145 else if ((TREE_CODE (result
) == INTEGER_CST
3146 || TREE_CODE (result
) == FIXED_CST
) && TREE_OVERFLOW (result
))
3147 warning_at (loc
, OPT_Woverflow
,
3148 "overflow in implicit constant conversion");
3150 conversion_warning (loc
, type
, expr
);
3154 /* Convert EXPR to TYPE, warning about conversion problems with constants.
3155 Invoke this function on every expression that is converted implicitly,
3156 i.e. because of language rules and not because of an explicit cast. */
3159 convert_and_check (location_t loc
, tree type
, tree expr
)
3162 tree expr_for_warning
;
3164 /* Convert from a value with possible excess precision rather than
3165 via the semantic type, but do not warn about values not fitting
3166 exactly in the semantic type. */
3167 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
3169 tree orig_type
= TREE_TYPE (expr
);
3170 expr
= TREE_OPERAND (expr
, 0);
3171 expr_for_warning
= convert (orig_type
, expr
);
3172 if (orig_type
== type
)
3173 return expr_for_warning
;
3176 expr_for_warning
= expr
;
3178 if (TREE_TYPE (expr
) == type
)
3181 result
= convert (type
, expr
);
3183 if (c_inhibit_evaluation_warnings
== 0
3184 && !TREE_OVERFLOW_P (expr
)
3185 && result
!= error_mark_node
)
3186 warnings_for_convert_and_check (loc
, type
, expr_for_warning
, result
);
3191 /* A node in a list that describes references to variables (EXPR), which are
3192 either read accesses if WRITER is zero, or write accesses, in which case
3193 WRITER is the parent of EXPR. */
3200 /* Used to implement a cache the results of a call to verify_tree. We only
3201 use this for SAVE_EXPRs. */
3204 struct tlist_cache
*next
;
3205 struct tlist
*cache_before_sp
;
3206 struct tlist
*cache_after_sp
;
3210 /* Obstack to use when allocating tlist structures, and corresponding
3212 static struct obstack tlist_obstack
;
3213 static char *tlist_firstobj
= 0;
3215 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3217 static struct tlist
*warned_ids
;
3218 /* SAVE_EXPRs need special treatment. We process them only once and then
3219 cache the results. */
3220 static struct tlist_cache
*save_expr_cache
;
3222 static void add_tlist (struct tlist
**, struct tlist
*, tree
, int);
3223 static void merge_tlist (struct tlist
**, struct tlist
*, int);
3224 static void verify_tree (tree
, struct tlist
**, struct tlist
**, tree
);
3225 static int warning_candidate_p (tree
);
3226 static bool candidate_equal_p (const_tree
, const_tree
);
3227 static void warn_for_collisions (struct tlist
*);
3228 static void warn_for_collisions_1 (tree
, tree
, struct tlist
*, int);
3229 static struct tlist
*new_tlist (struct tlist
*, tree
, tree
);
3231 /* Create a new struct tlist and fill in its fields. */
3232 static struct tlist
*
3233 new_tlist (struct tlist
*next
, tree t
, tree writer
)
3236 l
= XOBNEW (&tlist_obstack
, struct tlist
);
3243 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3244 is nonnull, we ignore any node we find which has a writer equal to it. */
3247 add_tlist (struct tlist
**to
, struct tlist
*add
, tree exclude_writer
, int copy
)
3251 struct tlist
*next
= add
->next
;
3254 if (!exclude_writer
|| !candidate_equal_p (add
->writer
, exclude_writer
))
3255 *to
= copy
? new_tlist (*to
, add
->expr
, add
->writer
) : add
;
3260 /* Merge the nodes of ADD into TO. This merging process is done so that for
3261 each variable that already exists in TO, no new node is added; however if
3262 there is a write access recorded in ADD, and an occurrence on TO is only
3263 a read access, then the occurrence in TO will be modified to record the
3267 merge_tlist (struct tlist
**to
, struct tlist
*add
, int copy
)
3269 struct tlist
**end
= to
;
3272 end
= &(*end
)->next
;
3278 struct tlist
*next
= add
->next
;
3280 for (tmp2
= *to
; tmp2
; tmp2
= tmp2
->next
)
3281 if (candidate_equal_p (tmp2
->expr
, add
->expr
))
3285 tmp2
->writer
= add
->writer
;
3289 *end
= copy
? new_tlist (NULL
, add
->expr
, add
->writer
) : add
;
3290 end
= &(*end
)->next
;
3297 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3298 references in list LIST conflict with it, excluding reads if ONLY writers
3302 warn_for_collisions_1 (tree written
, tree writer
, struct tlist
*list
,
3307 /* Avoid duplicate warnings. */
3308 for (tmp
= warned_ids
; tmp
; tmp
= tmp
->next
)
3309 if (candidate_equal_p (tmp
->expr
, written
))
3314 if (candidate_equal_p (list
->expr
, written
)
3315 && !candidate_equal_p (list
->writer
, writer
)
3316 && (!only_writes
|| list
->writer
))
3318 warned_ids
= new_tlist (warned_ids
, written
, NULL_TREE
);
3319 warning_at (EXPR_LOC_OR_LOC (writer
, input_location
),
3320 OPT_Wsequence_point
, "operation on %qE may be undefined",
3327 /* Given a list LIST of references to variables, find whether any of these
3328 can cause conflicts due to missing sequence points. */
3331 warn_for_collisions (struct tlist
*list
)
3335 for (tmp
= list
; tmp
; tmp
= tmp
->next
)
3338 warn_for_collisions_1 (tmp
->expr
, tmp
->writer
, list
, 0);
3342 /* Return nonzero if X is a tree that can be verified by the sequence point
3345 warning_candidate_p (tree x
)
3347 if (DECL_P (x
) && DECL_ARTIFICIAL (x
))
3350 if (TREE_CODE (x
) == BLOCK
)
3353 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3354 (lvalue_p) crash on TRY/CATCH. */
3355 if (TREE_TYPE (x
) == NULL_TREE
|| VOID_TYPE_P (TREE_TYPE (x
)))
3361 /* No point to track non-const calls, they will never satisfy
3363 if (TREE_CODE (x
) == CALL_EXPR
&& (call_expr_flags (x
) & ECF_CONST
) == 0)
3366 if (TREE_CODE (x
) == STRING_CST
)
3372 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3374 candidate_equal_p (const_tree x
, const_tree y
)
3376 return (x
== y
) || (x
&& y
&& operand_equal_p (x
, y
, 0));
3379 /* Walk the tree X, and record accesses to variables. If X is written by the
3380 parent tree, WRITER is the parent.
3381 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3382 expression or its only operand forces a sequence point, then everything up
3383 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3385 Once we return, we will have emitted warnings if any subexpression before
3386 such a sequence point could be undefined. On a higher level, however, the
3387 sequence point may not be relevant, and we'll merge the two lists.
3389 Example: (b++, a) + b;
3390 The call that processes the COMPOUND_EXPR will store the increment of B
3391 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3392 processes the PLUS_EXPR will need to merge the two lists so that
3393 eventually, all accesses end up on the same list (and we'll warn about the
3394 unordered subexpressions b++ and b.
3396 A note on merging. If we modify the former example so that our expression
3399 care must be taken not simply to add all three expressions into the final
3400 PNO_SP list. The function merge_tlist takes care of that by merging the
3401 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3402 way, so that no more than one access to B is recorded. */
3405 verify_tree (tree x
, struct tlist
**pbefore_sp
, struct tlist
**pno_sp
,
3408 struct tlist
*tmp_before
, *tmp_nosp
, *tmp_list2
, *tmp_list3
;
3409 enum tree_code code
;
3410 enum tree_code_class cl
;
3412 /* X may be NULL if it is the operand of an empty statement expression
3418 code
= TREE_CODE (x
);
3419 cl
= TREE_CODE_CLASS (code
);
3421 if (warning_candidate_p (x
))
3422 *pno_sp
= new_tlist (*pno_sp
, x
, writer
);
3431 case TRUTH_ANDIF_EXPR
:
3432 case TRUTH_ORIF_EXPR
:
3433 tmp_before
= tmp_nosp
= tmp_list3
= 0;
3434 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_nosp
, NULL_TREE
);
3435 warn_for_collisions (tmp_nosp
);
3436 merge_tlist (pbefore_sp
, tmp_before
, 0);
3437 merge_tlist (pbefore_sp
, tmp_nosp
, 0);
3438 verify_tree (TREE_OPERAND (x
, 1), &tmp_list3
, pno_sp
, NULL_TREE
);
3439 merge_tlist (pbefore_sp
, tmp_list3
, 0);
3443 tmp_before
= tmp_list2
= 0;
3444 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_list2
, NULL_TREE
);
3445 warn_for_collisions (tmp_list2
);
3446 merge_tlist (pbefore_sp
, tmp_before
, 0);
3447 merge_tlist (pbefore_sp
, tmp_list2
, 0);
3449 tmp_list3
= tmp_nosp
= 0;
3450 verify_tree (TREE_OPERAND (x
, 1), &tmp_list3
, &tmp_nosp
, NULL_TREE
);
3451 warn_for_collisions (tmp_nosp
);
3452 merge_tlist (pbefore_sp
, tmp_list3
, 0);
3454 tmp_list3
= tmp_list2
= 0;
3455 verify_tree (TREE_OPERAND (x
, 2), &tmp_list3
, &tmp_list2
, NULL_TREE
);
3456 warn_for_collisions (tmp_list2
);
3457 merge_tlist (pbefore_sp
, tmp_list3
, 0);
3458 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3459 two first, to avoid warning for (a ? b++ : b++). */
3460 merge_tlist (&tmp_nosp
, tmp_list2
, 0);
3461 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
3464 case PREDECREMENT_EXPR
:
3465 case PREINCREMENT_EXPR
:
3466 case POSTDECREMENT_EXPR
:
3467 case POSTINCREMENT_EXPR
:
3468 verify_tree (TREE_OPERAND (x
, 0), pno_sp
, pno_sp
, x
);
3472 tmp_before
= tmp_nosp
= tmp_list3
= 0;
3473 verify_tree (TREE_OPERAND (x
, 1), &tmp_before
, &tmp_nosp
, NULL_TREE
);
3474 verify_tree (TREE_OPERAND (x
, 0), &tmp_list3
, &tmp_list3
, x
);
3475 /* Expressions inside the LHS are not ordered wrt. the sequence points
3476 in the RHS. Example:
3478 Despite the fact that the modification of "a" is in the before_sp
3479 list (tmp_before), it conflicts with the use of "a" in the LHS.
3480 We can handle this by adding the contents of tmp_list3
3481 to those of tmp_before, and redoing the collision warnings for that
3483 add_tlist (&tmp_before
, tmp_list3
, x
, 1);
3484 warn_for_collisions (tmp_before
);
3485 /* Exclude the LHS itself here; we first have to merge it into the
3486 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3487 didn't exclude the LHS, we'd get it twice, once as a read and once
3489 add_tlist (pno_sp
, tmp_list3
, x
, 0);
3490 warn_for_collisions_1 (TREE_OPERAND (x
, 0), x
, tmp_nosp
, 1);
3492 merge_tlist (pbefore_sp
, tmp_before
, 0);
3493 if (warning_candidate_p (TREE_OPERAND (x
, 0)))
3494 merge_tlist (&tmp_nosp
, new_tlist (NULL
, TREE_OPERAND (x
, 0), x
), 0);
3495 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 1);
3499 /* We need to warn about conflicts among arguments and conflicts between
3500 args and the function address. Side effects of the function address,
3501 however, are not ordered by the sequence point of the call. */
3503 call_expr_arg_iterator iter
;
3505 tmp_before
= tmp_nosp
= 0;
3506 verify_tree (CALL_EXPR_FN (x
), &tmp_before
, &tmp_nosp
, NULL_TREE
);
3507 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, x
)
3509 tmp_list2
= tmp_list3
= 0;
3510 verify_tree (arg
, &tmp_list2
, &tmp_list3
, NULL_TREE
);
3511 merge_tlist (&tmp_list3
, tmp_list2
, 0);
3512 add_tlist (&tmp_before
, tmp_list3
, NULL_TREE
, 0);
3514 add_tlist (&tmp_before
, tmp_nosp
, NULL_TREE
, 0);
3515 warn_for_collisions (tmp_before
);
3516 add_tlist (pbefore_sp
, tmp_before
, NULL_TREE
, 0);
3521 /* Scan all the list, e.g. indices of multi dimensional array. */
3524 tmp_before
= tmp_nosp
= 0;
3525 verify_tree (TREE_VALUE (x
), &tmp_before
, &tmp_nosp
, NULL_TREE
);
3526 merge_tlist (&tmp_nosp
, tmp_before
, 0);
3527 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
3534 struct tlist_cache
*t
;
3535 for (t
= save_expr_cache
; t
; t
= t
->next
)
3536 if (candidate_equal_p (t
->expr
, x
))
3541 t
= XOBNEW (&tlist_obstack
, struct tlist_cache
);
3542 t
->next
= save_expr_cache
;
3544 save_expr_cache
= t
;
3546 tmp_before
= tmp_nosp
= 0;
3547 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_nosp
, NULL_TREE
);
3548 warn_for_collisions (tmp_nosp
);
3551 merge_tlist (&tmp_list3
, tmp_nosp
, 0);
3552 t
->cache_before_sp
= tmp_before
;
3553 t
->cache_after_sp
= tmp_list3
;
3555 merge_tlist (pbefore_sp
, t
->cache_before_sp
, 1);
3556 add_tlist (pno_sp
, t
->cache_after_sp
, NULL_TREE
, 1);
3561 x
= TREE_OPERAND (x
, 0);
3568 /* For other expressions, simply recurse on their operands.
3569 Manual tail recursion for unary expressions.
3570 Other non-expressions need not be processed. */
3571 if (cl
== tcc_unary
)
3573 x
= TREE_OPERAND (x
, 0);
3577 else if (IS_EXPR_CODE_CLASS (cl
))
3580 int max
= TREE_OPERAND_LENGTH (x
);
3581 for (lp
= 0; lp
< max
; lp
++)
3583 tmp_before
= tmp_nosp
= 0;
3584 verify_tree (TREE_OPERAND (x
, lp
), &tmp_before
, &tmp_nosp
, 0);
3585 merge_tlist (&tmp_nosp
, tmp_before
, 0);
3586 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
3593 /* Try to warn for undefined behavior in EXPR due to missing sequence
3597 verify_sequence_points (tree expr
)
3599 struct tlist
*before_sp
= 0, *after_sp
= 0;
3602 save_expr_cache
= 0;
3603 if (tlist_firstobj
== 0)
3605 gcc_obstack_init (&tlist_obstack
);
3606 tlist_firstobj
= (char *) obstack_alloc (&tlist_obstack
, 0);
3609 verify_tree (expr
, &before_sp
, &after_sp
, 0);
3610 warn_for_collisions (after_sp
);
3611 obstack_free (&tlist_obstack
, tlist_firstobj
);
3614 /* Validate the expression after `case' and apply default promotions. */
3617 check_case_value (location_t loc
, tree value
)
3619 if (value
== NULL_TREE
)
3622 if (TREE_CODE (value
) == INTEGER_CST
)
3623 /* Promote char or short to int. */
3624 value
= perform_integral_promotions (value
);
3625 else if (value
!= error_mark_node
)
3627 error_at (loc
, "case label does not reduce to an integer constant");
3628 value
= error_mark_node
;
3631 constant_expression_warning (value
);
3636 /* See if the case values LOW and HIGH are in the range of the original
3637 type (i.e. before the default conversion to int) of the switch testing
3639 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3640 the type before promoting it. CASE_LOW_P is a pointer to the lower
3641 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3642 if the case is not a case range.
3643 The caller has to make sure that we are not called with NULL for
3644 CASE_LOW_P (i.e. the default case).
3645 Returns true if the case label is in range of ORIG_TYPE (saturated or
3646 untouched) or false if the label is out of range. */
3649 check_case_bounds (location_t loc
, tree type
, tree orig_type
,
3650 tree
*case_low_p
, tree
*case_high_p
)
3652 tree min_value
, max_value
;
3653 tree case_low
= *case_low_p
;
3654 tree case_high
= case_high_p
? *case_high_p
: case_low
;
3656 /* If there was a problem with the original type, do nothing. */
3657 if (orig_type
== error_mark_node
)
3660 min_value
= TYPE_MIN_VALUE (orig_type
);
3661 max_value
= TYPE_MAX_VALUE (orig_type
);
3663 /* Case label is less than minimum for type. */
3664 if (tree_int_cst_compare (case_low
, min_value
) < 0
3665 && tree_int_cst_compare (case_high
, min_value
) < 0)
3667 warning_at (loc
, 0, "case label value is less than minimum value "
3672 /* Case value is greater than maximum for type. */
3673 if (tree_int_cst_compare (case_low
, max_value
) > 0
3674 && tree_int_cst_compare (case_high
, max_value
) > 0)
3676 warning_at (loc
, 0, "case label value exceeds maximum value for type");
3680 /* Saturate lower case label value to minimum. */
3681 if (tree_int_cst_compare (case_high
, min_value
) >= 0
3682 && tree_int_cst_compare (case_low
, min_value
) < 0)
3684 warning_at (loc
, 0, "lower value in case label range"
3685 " less than minimum value for type");
3686 case_low
= min_value
;
3689 /* Saturate upper case label value to maximum. */
3690 if (tree_int_cst_compare (case_low
, max_value
) <= 0
3691 && tree_int_cst_compare (case_high
, max_value
) > 0)
3693 warning_at (loc
, 0, "upper value in case label range"
3694 " exceeds maximum value for type");
3695 case_high
= max_value
;
3698 if (*case_low_p
!= case_low
)
3699 *case_low_p
= convert (type
, case_low
);
3700 if (case_high_p
&& *case_high_p
!= case_high
)
3701 *case_high_p
= convert (type
, case_high
);
3706 /* Return an integer type with BITS bits of precision,
3707 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3710 c_common_type_for_size (unsigned int bits
, int unsignedp
)
3714 if (bits
== TYPE_PRECISION (integer_type_node
))
3715 return unsignedp
? unsigned_type_node
: integer_type_node
;
3717 if (bits
== TYPE_PRECISION (signed_char_type_node
))
3718 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
3720 if (bits
== TYPE_PRECISION (short_integer_type_node
))
3721 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
3723 if (bits
== TYPE_PRECISION (long_integer_type_node
))
3724 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
3726 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
3727 return (unsignedp
? long_long_unsigned_type_node
3728 : long_long_integer_type_node
);
3730 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
3731 if (int_n_enabled_p
[i
]
3732 && bits
== int_n_data
[i
].bitsize
)
3733 return (unsignedp
? int_n_trees
[i
].unsigned_type
3734 : int_n_trees
[i
].signed_type
);
3736 if (bits
== TYPE_PRECISION (widest_integer_literal_type_node
))
3737 return (unsignedp
? widest_unsigned_literal_type_node
3738 : widest_integer_literal_type_node
);
3740 if (bits
<= TYPE_PRECISION (intQI_type_node
))
3741 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
3743 if (bits
<= TYPE_PRECISION (intHI_type_node
))
3744 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
3746 if (bits
<= TYPE_PRECISION (intSI_type_node
))
3747 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
3749 if (bits
<= TYPE_PRECISION (intDI_type_node
))
3750 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
3755 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3756 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3757 and saturating if SATP is nonzero, otherwise not saturating. */
3760 c_common_fixed_point_type_for_size (unsigned int ibit
, unsigned int fbit
,
3761 int unsignedp
, int satp
)
3765 mode
= unsignedp
? UQQmode
: QQmode
;
3767 mode
= unsignedp
? UHAmode
: HAmode
;
3769 for (; mode
!= VOIDmode
; mode
= GET_MODE_WIDER_MODE (mode
))
3770 if (GET_MODE_IBIT (mode
) >= ibit
&& GET_MODE_FBIT (mode
) >= fbit
)
3773 if (mode
== VOIDmode
|| !targetm
.scalar_mode_supported_p (mode
))
3775 sorry ("GCC cannot support operators with integer types and "
3776 "fixed-point types that have too many integral and "
3777 "fractional bits together");
3781 return c_common_type_for_mode (mode
, satp
);
3784 /* Used for communication between c_common_type_for_mode and
3785 c_register_builtin_type. */
3786 tree registered_builtin_types
;
3788 /* Return a data type that has machine mode MODE.
3789 If the mode is an integer,
3790 then UNSIGNEDP selects between signed and unsigned types.
3791 If the mode is a fixed-point mode,
3792 then UNSIGNEDP selects between saturating and nonsaturating types. */
3795 c_common_type_for_mode (machine_mode mode
, int unsignedp
)
3800 if (mode
== TYPE_MODE (integer_type_node
))
3801 return unsignedp
? unsigned_type_node
: integer_type_node
;
3803 if (mode
== TYPE_MODE (signed_char_type_node
))
3804 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
3806 if (mode
== TYPE_MODE (short_integer_type_node
))
3807 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
3809 if (mode
== TYPE_MODE (long_integer_type_node
))
3810 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
3812 if (mode
== TYPE_MODE (long_long_integer_type_node
))
3813 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
3815 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
3816 if (int_n_enabled_p
[i
]
3817 && mode
== int_n_data
[i
].m
)
3818 return (unsignedp
? int_n_trees
[i
].unsigned_type
3819 : int_n_trees
[i
].signed_type
);
3821 if (mode
== TYPE_MODE (widest_integer_literal_type_node
))
3822 return unsignedp
? widest_unsigned_literal_type_node
3823 : widest_integer_literal_type_node
;
3826 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
3829 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
3832 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
3835 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
3837 #if HOST_BITS_PER_WIDE_INT >= 64
3838 if (mode
== TYPE_MODE (intTI_type_node
))
3839 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
3842 if (mode
== TYPE_MODE (float_type_node
))
3843 return float_type_node
;
3845 if (mode
== TYPE_MODE (double_type_node
))
3846 return double_type_node
;
3848 if (mode
== TYPE_MODE (long_double_type_node
))
3849 return long_double_type_node
;
3851 if (mode
== TYPE_MODE (void_type_node
))
3852 return void_type_node
;
3854 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
)))
3856 ? make_unsigned_type (GET_MODE_PRECISION (mode
))
3857 : make_signed_type (GET_MODE_PRECISION (mode
)));
3859 if (mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
3861 ? make_unsigned_type (GET_MODE_PRECISION (mode
))
3862 : make_signed_type (GET_MODE_PRECISION (mode
)));
3864 if (COMPLEX_MODE_P (mode
))
3866 machine_mode inner_mode
;
3869 if (mode
== TYPE_MODE (complex_float_type_node
))
3870 return complex_float_type_node
;
3871 if (mode
== TYPE_MODE (complex_double_type_node
))
3872 return complex_double_type_node
;
3873 if (mode
== TYPE_MODE (complex_long_double_type_node
))
3874 return complex_long_double_type_node
;
3876 if (mode
== TYPE_MODE (complex_integer_type_node
) && !unsignedp
)
3877 return complex_integer_type_node
;
3879 inner_mode
= GET_MODE_INNER (mode
);
3880 inner_type
= c_common_type_for_mode (inner_mode
, unsignedp
);
3881 if (inner_type
!= NULL_TREE
)
3882 return build_complex_type (inner_type
);
3884 else if (VECTOR_MODE_P (mode
))
3886 machine_mode inner_mode
= GET_MODE_INNER (mode
);
3887 tree inner_type
= c_common_type_for_mode (inner_mode
, unsignedp
);
3888 if (inner_type
!= NULL_TREE
)
3889 return build_vector_type_for_mode (inner_type
, mode
);
3892 if (mode
== TYPE_MODE (dfloat32_type_node
))
3893 return dfloat32_type_node
;
3894 if (mode
== TYPE_MODE (dfloat64_type_node
))
3895 return dfloat64_type_node
;
3896 if (mode
== TYPE_MODE (dfloat128_type_node
))
3897 return dfloat128_type_node
;
3899 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode
))
3901 if (mode
== TYPE_MODE (short_fract_type_node
))
3902 return unsignedp
? sat_short_fract_type_node
: short_fract_type_node
;
3903 if (mode
== TYPE_MODE (fract_type_node
))
3904 return unsignedp
? sat_fract_type_node
: fract_type_node
;
3905 if (mode
== TYPE_MODE (long_fract_type_node
))
3906 return unsignedp
? sat_long_fract_type_node
: long_fract_type_node
;
3907 if (mode
== TYPE_MODE (long_long_fract_type_node
))
3908 return unsignedp
? sat_long_long_fract_type_node
3909 : long_long_fract_type_node
;
3911 if (mode
== TYPE_MODE (unsigned_short_fract_type_node
))
3912 return unsignedp
? sat_unsigned_short_fract_type_node
3913 : unsigned_short_fract_type_node
;
3914 if (mode
== TYPE_MODE (unsigned_fract_type_node
))
3915 return unsignedp
? sat_unsigned_fract_type_node
3916 : unsigned_fract_type_node
;
3917 if (mode
== TYPE_MODE (unsigned_long_fract_type_node
))
3918 return unsignedp
? sat_unsigned_long_fract_type_node
3919 : unsigned_long_fract_type_node
;
3920 if (mode
== TYPE_MODE (unsigned_long_long_fract_type_node
))
3921 return unsignedp
? sat_unsigned_long_long_fract_type_node
3922 : unsigned_long_long_fract_type_node
;
3924 if (mode
== TYPE_MODE (short_accum_type_node
))
3925 return unsignedp
? sat_short_accum_type_node
: short_accum_type_node
;
3926 if (mode
== TYPE_MODE (accum_type_node
))
3927 return unsignedp
? sat_accum_type_node
: accum_type_node
;
3928 if (mode
== TYPE_MODE (long_accum_type_node
))
3929 return unsignedp
? sat_long_accum_type_node
: long_accum_type_node
;
3930 if (mode
== TYPE_MODE (long_long_accum_type_node
))
3931 return unsignedp
? sat_long_long_accum_type_node
3932 : long_long_accum_type_node
;
3934 if (mode
== TYPE_MODE (unsigned_short_accum_type_node
))
3935 return unsignedp
? sat_unsigned_short_accum_type_node
3936 : unsigned_short_accum_type_node
;
3937 if (mode
== TYPE_MODE (unsigned_accum_type_node
))
3938 return unsignedp
? sat_unsigned_accum_type_node
3939 : unsigned_accum_type_node
;
3940 if (mode
== TYPE_MODE (unsigned_long_accum_type_node
))
3941 return unsignedp
? sat_unsigned_long_accum_type_node
3942 : unsigned_long_accum_type_node
;
3943 if (mode
== TYPE_MODE (unsigned_long_long_accum_type_node
))
3944 return unsignedp
? sat_unsigned_long_long_accum_type_node
3945 : unsigned_long_long_accum_type_node
;
3948 return unsignedp
? sat_qq_type_node
: qq_type_node
;
3950 return unsignedp
? sat_hq_type_node
: hq_type_node
;
3952 return unsignedp
? sat_sq_type_node
: sq_type_node
;
3954 return unsignedp
? sat_dq_type_node
: dq_type_node
;
3956 return unsignedp
? sat_tq_type_node
: tq_type_node
;
3958 if (mode
== UQQmode
)
3959 return unsignedp
? sat_uqq_type_node
: uqq_type_node
;
3960 if (mode
== UHQmode
)
3961 return unsignedp
? sat_uhq_type_node
: uhq_type_node
;
3962 if (mode
== USQmode
)
3963 return unsignedp
? sat_usq_type_node
: usq_type_node
;
3964 if (mode
== UDQmode
)
3965 return unsignedp
? sat_udq_type_node
: udq_type_node
;
3966 if (mode
== UTQmode
)
3967 return unsignedp
? sat_utq_type_node
: utq_type_node
;
3970 return unsignedp
? sat_ha_type_node
: ha_type_node
;
3972 return unsignedp
? sat_sa_type_node
: sa_type_node
;
3974 return unsignedp
? sat_da_type_node
: da_type_node
;
3976 return unsignedp
? sat_ta_type_node
: ta_type_node
;
3978 if (mode
== UHAmode
)
3979 return unsignedp
? sat_uha_type_node
: uha_type_node
;
3980 if (mode
== USAmode
)
3981 return unsignedp
? sat_usa_type_node
: usa_type_node
;
3982 if (mode
== UDAmode
)
3983 return unsignedp
? sat_uda_type_node
: uda_type_node
;
3984 if (mode
== UTAmode
)
3985 return unsignedp
? sat_uta_type_node
: uta_type_node
;
3988 for (t
= registered_builtin_types
; t
; t
= TREE_CHAIN (t
))
3989 if (TYPE_MODE (TREE_VALUE (t
)) == mode
3990 && !!unsignedp
== !!TYPE_UNSIGNED (TREE_VALUE (t
)))
3991 return TREE_VALUE (t
);
3997 c_common_unsigned_type (tree type
)
3999 return c_common_signed_or_unsigned_type (1, type
);
4002 /* Return a signed type the same as TYPE in other respects. */
4005 c_common_signed_type (tree type
)
4007 return c_common_signed_or_unsigned_type (0, type
);
4010 /* Return a type the same as TYPE except unsigned or
4011 signed according to UNSIGNEDP. */
4014 c_common_signed_or_unsigned_type (int unsignedp
, tree type
)
4019 /* This block of code emulates the behavior of the old
4020 c_common_unsigned_type. In particular, it returns
4021 long_unsigned_type_node if passed a long, even when a int would
4022 have the same size. This is necessary for warnings to work
4023 correctly in archs where sizeof(int) == sizeof(long) */
4025 type1
= TYPE_MAIN_VARIANT (type
);
4026 if (type1
== signed_char_type_node
|| type1
== char_type_node
|| type1
== unsigned_char_type_node
)
4027 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
4028 if (type1
== integer_type_node
|| type1
== unsigned_type_node
)
4029 return unsignedp
? unsigned_type_node
: integer_type_node
;
4030 if (type1
== short_integer_type_node
|| type1
== short_unsigned_type_node
)
4031 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
4032 if (type1
== long_integer_type_node
|| type1
== long_unsigned_type_node
)
4033 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
4034 if (type1
== long_long_integer_type_node
|| type1
== long_long_unsigned_type_node
)
4035 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
4037 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
4038 if (int_n_enabled_p
[i
]
4039 && (type1
== int_n_trees
[i
].unsigned_type
4040 || type1
== int_n_trees
[i
].signed_type
))
4041 return (unsignedp
? int_n_trees
[i
].unsigned_type
4042 : int_n_trees
[i
].signed_type
);
4044 if (type1
== widest_integer_literal_type_node
|| type1
== widest_unsigned_literal_type_node
)
4045 return unsignedp
? widest_unsigned_literal_type_node
: widest_integer_literal_type_node
;
4046 #if HOST_BITS_PER_WIDE_INT >= 64
4047 if (type1
== intTI_type_node
|| type1
== unsigned_intTI_type_node
)
4048 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
4050 if (type1
== intDI_type_node
|| type1
== unsigned_intDI_type_node
)
4051 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
4052 if (type1
== intSI_type_node
|| type1
== unsigned_intSI_type_node
)
4053 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
4054 if (type1
== intHI_type_node
|| type1
== unsigned_intHI_type_node
)
4055 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
4056 if (type1
== intQI_type_node
|| type1
== unsigned_intQI_type_node
)
4057 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
4059 #define C_COMMON_FIXED_TYPES(NAME) \
4060 if (type1 == short_ ## NAME ## _type_node \
4061 || type1 == unsigned_short_ ## NAME ## _type_node) \
4062 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4063 : short_ ## NAME ## _type_node; \
4064 if (type1 == NAME ## _type_node \
4065 || type1 == unsigned_ ## NAME ## _type_node) \
4066 return unsignedp ? unsigned_ ## NAME ## _type_node \
4067 : NAME ## _type_node; \
4068 if (type1 == long_ ## NAME ## _type_node \
4069 || type1 == unsigned_long_ ## NAME ## _type_node) \
4070 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4071 : long_ ## NAME ## _type_node; \
4072 if (type1 == long_long_ ## NAME ## _type_node \
4073 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4074 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4075 : long_long_ ## NAME ## _type_node;
4077 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
4078 if (type1 == NAME ## _type_node \
4079 || type1 == u ## NAME ## _type_node) \
4080 return unsignedp ? u ## NAME ## _type_node \
4081 : NAME ## _type_node;
4083 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
4084 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4085 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4086 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4087 : sat_ ## short_ ## NAME ## _type_node; \
4088 if (type1 == sat_ ## NAME ## _type_node \
4089 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4090 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4091 : sat_ ## NAME ## _type_node; \
4092 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4093 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4094 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4095 : sat_ ## long_ ## NAME ## _type_node; \
4096 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4097 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4098 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4099 : sat_ ## long_long_ ## NAME ## _type_node;
4101 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4102 if (type1 == sat_ ## NAME ## _type_node \
4103 || type1 == sat_ ## u ## NAME ## _type_node) \
4104 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4105 : sat_ ## NAME ## _type_node;
4107 C_COMMON_FIXED_TYPES (fract
);
4108 C_COMMON_FIXED_TYPES_SAT (fract
);
4109 C_COMMON_FIXED_TYPES (accum
);
4110 C_COMMON_FIXED_TYPES_SAT (accum
);
4112 C_COMMON_FIXED_MODE_TYPES (qq
);
4113 C_COMMON_FIXED_MODE_TYPES (hq
);
4114 C_COMMON_FIXED_MODE_TYPES (sq
);
4115 C_COMMON_FIXED_MODE_TYPES (dq
);
4116 C_COMMON_FIXED_MODE_TYPES (tq
);
4117 C_COMMON_FIXED_MODE_TYPES_SAT (qq
);
4118 C_COMMON_FIXED_MODE_TYPES_SAT (hq
);
4119 C_COMMON_FIXED_MODE_TYPES_SAT (sq
);
4120 C_COMMON_FIXED_MODE_TYPES_SAT (dq
);
4121 C_COMMON_FIXED_MODE_TYPES_SAT (tq
);
4122 C_COMMON_FIXED_MODE_TYPES (ha
);
4123 C_COMMON_FIXED_MODE_TYPES (sa
);
4124 C_COMMON_FIXED_MODE_TYPES (da
);
4125 C_COMMON_FIXED_MODE_TYPES (ta
);
4126 C_COMMON_FIXED_MODE_TYPES_SAT (ha
);
4127 C_COMMON_FIXED_MODE_TYPES_SAT (sa
);
4128 C_COMMON_FIXED_MODE_TYPES_SAT (da
);
4129 C_COMMON_FIXED_MODE_TYPES_SAT (ta
);
4131 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4132 the precision; they have precision set to match their range, but
4133 may use a wider mode to match an ABI. If we change modes, we may
4134 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4135 the precision as well, so as to yield correct results for
4136 bit-field types. C++ does not have these separate bit-field
4137 types, and producing a signed or unsigned variant of an
4138 ENUMERAL_TYPE may cause other problems as well. */
4140 if (!INTEGRAL_TYPE_P (type
)
4141 || TYPE_UNSIGNED (type
) == unsignedp
)
4144 #define TYPE_OK(node) \
4145 (TYPE_MODE (type) == TYPE_MODE (node) \
4146 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4147 if (TYPE_OK (signed_char_type_node
))
4148 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
4149 if (TYPE_OK (integer_type_node
))
4150 return unsignedp
? unsigned_type_node
: integer_type_node
;
4151 if (TYPE_OK (short_integer_type_node
))
4152 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
4153 if (TYPE_OK (long_integer_type_node
))
4154 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
4155 if (TYPE_OK (long_long_integer_type_node
))
4156 return (unsignedp
? long_long_unsigned_type_node
4157 : long_long_integer_type_node
);
4159 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
4160 if (int_n_enabled_p
[i
]
4161 && TYPE_MODE (type
) == int_n_data
[i
].m
4162 && TYPE_PRECISION (type
) == int_n_data
[i
].bitsize
)
4163 return (unsignedp
? int_n_trees
[i
].unsigned_type
4164 : int_n_trees
[i
].signed_type
);
4166 if (TYPE_OK (widest_integer_literal_type_node
))
4167 return (unsignedp
? widest_unsigned_literal_type_node
4168 : widest_integer_literal_type_node
);
4170 #if HOST_BITS_PER_WIDE_INT >= 64
4171 if (TYPE_OK (intTI_type_node
))
4172 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
4174 if (TYPE_OK (intDI_type_node
))
4175 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
4176 if (TYPE_OK (intSI_type_node
))
4177 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
4178 if (TYPE_OK (intHI_type_node
))
4179 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
4180 if (TYPE_OK (intQI_type_node
))
4181 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
4184 return build_nonstandard_integer_type (TYPE_PRECISION (type
), unsignedp
);
4187 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4190 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width
, int unsignedp
)
4194 /* Extended integer types of the same width as a standard type have
4195 lesser rank, so those of the same width as int promote to int or
4196 unsigned int and are valid for printf formats expecting int or
4197 unsigned int. To avoid such special cases, avoid creating
4198 extended integer types for bit-fields if a standard integer type
4200 if (width
== TYPE_PRECISION (integer_type_node
))
4201 return unsignedp
? unsigned_type_node
: integer_type_node
;
4202 if (width
== TYPE_PRECISION (signed_char_type_node
))
4203 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
4204 if (width
== TYPE_PRECISION (short_integer_type_node
))
4205 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
4206 if (width
== TYPE_PRECISION (long_integer_type_node
))
4207 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
4208 if (width
== TYPE_PRECISION (long_long_integer_type_node
))
4209 return (unsignedp
? long_long_unsigned_type_node
4210 : long_long_integer_type_node
);
4211 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
4212 if (int_n_enabled_p
[i
]
4213 && width
== int_n_data
[i
].bitsize
)
4214 return (unsignedp
? int_n_trees
[i
].unsigned_type
4215 : int_n_trees
[i
].signed_type
);
4216 return build_nonstandard_integer_type (width
, unsignedp
);
4219 /* The C version of the register_builtin_type langhook. */
4222 c_register_builtin_type (tree type
, const char* name
)
4226 decl
= build_decl (UNKNOWN_LOCATION
,
4227 TYPE_DECL
, get_identifier (name
), type
);
4228 DECL_ARTIFICIAL (decl
) = 1;
4229 if (!TYPE_NAME (type
))
4230 TYPE_NAME (type
) = decl
;
4233 registered_builtin_types
= tree_cons (0, type
, registered_builtin_types
);
4236 /* Print an error message for invalid operands to arith operation
4237 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4238 LOCATION is the location of the message. */
4241 binary_op_error (location_t location
, enum tree_code code
,
4242 tree type0
, tree type1
)
4249 opname
= "+"; break;
4251 opname
= "-"; break;
4253 opname
= "*"; break;
4255 opname
= "max"; break;
4257 opname
= "min"; break;
4259 opname
= "=="; break;
4261 opname
= "!="; break;
4263 opname
= "<="; break;
4265 opname
= ">="; break;
4267 opname
= "<"; break;
4269 opname
= ">"; break;
4271 opname
= "<<"; break;
4273 opname
= ">>"; break;
4274 case TRUNC_MOD_EXPR
:
4275 case FLOOR_MOD_EXPR
:
4276 opname
= "%"; break;
4277 case TRUNC_DIV_EXPR
:
4278 case FLOOR_DIV_EXPR
:
4279 opname
= "/"; break;
4281 opname
= "&"; break;
4283 opname
= "|"; break;
4284 case TRUTH_ANDIF_EXPR
:
4285 opname
= "&&"; break;
4286 case TRUTH_ORIF_EXPR
:
4287 opname
= "||"; break;
4289 opname
= "^"; break;
4294 "invalid operands to binary %s (have %qT and %qT)", opname
,
4298 /* Given an expression as a tree, return its original type. Do this
4299 by stripping any conversion that preserves the sign and precision. */
4301 expr_original_type (tree expr
)
4303 STRIP_SIGN_NOPS (expr
);
4304 return TREE_TYPE (expr
);
4307 /* Subroutine of build_binary_op, used for comparison operations.
4308 See if the operands have both been converted from subword integer types
4309 and, if so, perhaps change them both back to their original type.
4310 This function is also responsible for converting the two operands
4311 to the proper common type for comparison.
4313 The arguments of this function are all pointers to local variables
4314 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4315 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4317 LOC is the location of the comparison.
4319 If this function returns nonzero, it means that the comparison has
4320 a constant value. What this function returns is an expression for
4324 shorten_compare (location_t loc
, tree
*op0_ptr
, tree
*op1_ptr
,
4325 tree
*restype_ptr
, enum tree_code
*rescode_ptr
)
4328 tree op0
= *op0_ptr
;
4329 tree op1
= *op1_ptr
;
4330 int unsignedp0
, unsignedp1
;
4332 tree primop0
, primop1
;
4333 enum tree_code code
= *rescode_ptr
;
4335 /* Throw away any conversions to wider types
4336 already present in the operands. */
4338 primop0
= c_common_get_narrower (op0
, &unsignedp0
);
4339 primop1
= c_common_get_narrower (op1
, &unsignedp1
);
4341 /* If primopN is first sign-extended from primopN's precision to opN's
4342 precision, then zero-extended from opN's precision to
4343 *restype_ptr precision, shortenings might be invalid. */
4344 if (TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (TREE_TYPE (op0
))
4345 && TYPE_PRECISION (TREE_TYPE (op0
)) < TYPE_PRECISION (*restype_ptr
)
4347 && TYPE_UNSIGNED (TREE_TYPE (op0
)))
4349 if (TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (TREE_TYPE (op1
))
4350 && TYPE_PRECISION (TREE_TYPE (op1
)) < TYPE_PRECISION (*restype_ptr
)
4352 && TYPE_UNSIGNED (TREE_TYPE (op1
)))
4355 /* Handle the case that OP0 does not *contain* a conversion
4356 but it *requires* conversion to FINAL_TYPE. */
4358 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
4359 unsignedp0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
4360 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
4361 unsignedp1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
4363 /* If one of the operands must be floated, we cannot optimize. */
4364 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
4365 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
4367 /* If first arg is constant, swap the args (changing operation
4368 so value is preserved), for canonicalization. Don't do this if
4369 the second arg is 0. */
4371 if (TREE_CONSTANT (primop0
)
4372 && !integer_zerop (primop1
) && !real_zerop (primop1
)
4373 && !fixed_zerop (primop1
))
4376 int temi
= unsignedp0
;
4384 unsignedp0
= unsignedp1
;
4407 *rescode_ptr
= code
;
4410 /* If comparing an integer against a constant more bits wide,
4411 maybe we can deduce a value of 1 or 0 independent of the data.
4412 Or else truncate the constant now
4413 rather than extend the variable at run time.
4415 This is only interesting if the constant is the wider arg.
4416 Also, it is not safe if the constant is unsigned and the
4417 variable arg is signed, since in this case the variable
4418 would be sign-extended and then regarded as unsigned.
4419 Our technique fails in this case because the lowest/highest
4420 possible unsigned results don't follow naturally from the
4421 lowest/highest possible values of the variable operand.
4422 For just EQ_EXPR and NE_EXPR there is another technique that
4423 could be used: see if the constant can be faithfully represented
4424 in the other operand's type, by truncating it and reextending it
4425 and see if that preserves the constant's value. */
4427 if (!real1
&& !real2
4428 && TREE_CODE (TREE_TYPE (primop0
)) != FIXED_POINT_TYPE
4429 && TREE_CODE (primop1
) == INTEGER_CST
4430 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
4432 int min_gt
, max_gt
, min_lt
, max_lt
;
4433 tree maxval
, minval
;
4434 /* 1 if comparison is nominally unsigned. */
4435 int unsignedp
= TYPE_UNSIGNED (*restype_ptr
);
4438 type
= c_common_signed_or_unsigned_type (unsignedp0
,
4439 TREE_TYPE (primop0
));
4441 maxval
= TYPE_MAX_VALUE (type
);
4442 minval
= TYPE_MIN_VALUE (type
);
4444 if (unsignedp
&& !unsignedp0
)
4445 *restype_ptr
= c_common_signed_type (*restype_ptr
);
4447 if (TREE_TYPE (primop1
) != *restype_ptr
)
4449 /* Convert primop1 to target type, but do not introduce
4450 additional overflow. We know primop1 is an int_cst. */
4451 primop1
= force_fit_type (*restype_ptr
,
4454 TYPE_PRECISION (*restype_ptr
),
4455 TYPE_SIGN (TREE_TYPE (primop1
))),
4456 0, TREE_OVERFLOW (primop1
));
4458 if (type
!= *restype_ptr
)
4460 minval
= convert (*restype_ptr
, minval
);
4461 maxval
= convert (*restype_ptr
, maxval
);
4464 min_gt
= tree_int_cst_lt (primop1
, minval
);
4465 max_gt
= tree_int_cst_lt (primop1
, maxval
);
4466 min_lt
= tree_int_cst_lt (minval
, primop1
);
4467 max_lt
= tree_int_cst_lt (maxval
, primop1
);
4470 /* This used to be a switch, but Genix compiler can't handle that. */
4471 if (code
== NE_EXPR
)
4473 if (max_lt
|| min_gt
)
4474 val
= truthvalue_true_node
;
4476 else if (code
== EQ_EXPR
)
4478 if (max_lt
|| min_gt
)
4479 val
= truthvalue_false_node
;
4481 else if (code
== LT_EXPR
)
4484 val
= truthvalue_true_node
;
4486 val
= truthvalue_false_node
;
4488 else if (code
== GT_EXPR
)
4491 val
= truthvalue_true_node
;
4493 val
= truthvalue_false_node
;
4495 else if (code
== LE_EXPR
)
4498 val
= truthvalue_true_node
;
4500 val
= truthvalue_false_node
;
4502 else if (code
== GE_EXPR
)
4505 val
= truthvalue_true_node
;
4507 val
= truthvalue_false_node
;
4510 /* If primop0 was sign-extended and unsigned comparison specd,
4511 we did a signed comparison above using the signed type bounds.
4512 But the comparison we output must be unsigned.
4514 Also, for inequalities, VAL is no good; but if the signed
4515 comparison had *any* fixed result, it follows that the
4516 unsigned comparison just tests the sign in reverse
4517 (positive values are LE, negative ones GE).
4518 So we can generate an unsigned comparison
4519 against an extreme value of the signed type. */
4521 if (unsignedp
&& !unsignedp0
)
4528 primop1
= TYPE_MIN_VALUE (type
);
4534 primop1
= TYPE_MAX_VALUE (type
);
4541 type
= c_common_unsigned_type (type
);
4544 if (TREE_CODE (primop0
) != INTEGER_CST
)
4546 if (val
== truthvalue_false_node
)
4547 warning_at (loc
, OPT_Wtype_limits
,
4548 "comparison is always false due to limited range of data type");
4549 if (val
== truthvalue_true_node
)
4550 warning_at (loc
, OPT_Wtype_limits
,
4551 "comparison is always true due to limited range of data type");
4556 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4557 if (TREE_SIDE_EFFECTS (primop0
))
4558 return build2 (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
4562 /* Value is not predetermined, but do the comparison
4563 in the type of the operand that is not constant.
4564 TYPE is already properly set. */
4567 /* If either arg is decimal float and the other is float, find the
4568 proper common type to use for comparison. */
4569 else if (real1
&& real2
4570 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0
)))
4571 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1
))))
4572 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
4574 /* If either arg is decimal float and the other is float, fail. */
4575 else if (real1
&& real2
4576 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0
)))
4577 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1
)))))
4580 else if (real1
&& real2
4581 && (TYPE_PRECISION (TREE_TYPE (primop0
))
4582 == TYPE_PRECISION (TREE_TYPE (primop1
))))
4583 type
= TREE_TYPE (primop0
);
4585 /* If args' natural types are both narrower than nominal type
4586 and both extend in the same manner, compare them
4587 in the type of the wider arg.
4588 Otherwise must actually extend both to the nominal
4589 common type lest different ways of extending
4591 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4593 else if (unsignedp0
== unsignedp1
&& real1
== real2
4594 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
4595 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
4597 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
4598 type
= c_common_signed_or_unsigned_type (unsignedp0
4599 || TYPE_UNSIGNED (*restype_ptr
),
4601 /* Make sure shorter operand is extended the right way
4602 to match the longer operand. */
4604 = convert (c_common_signed_or_unsigned_type (unsignedp0
,
4605 TREE_TYPE (primop0
)),
4608 = convert (c_common_signed_or_unsigned_type (unsignedp1
,
4609 TREE_TYPE (primop1
)),
4614 /* Here we must do the comparison on the nominal type
4615 using the args exactly as we received them. */
4616 type
= *restype_ptr
;
4620 if (!real1
&& !real2
&& integer_zerop (primop1
)
4621 && TYPE_UNSIGNED (*restype_ptr
))
4624 /* All unsigned values are >= 0, so we warn. However,
4625 if OP0 is a constant that is >= 0, the signedness of
4626 the comparison isn't an issue, so suppress the
4629 warn_type_limits
&& !in_system_header_at (loc
)
4630 && !(TREE_CODE (primop0
) == INTEGER_CST
4631 && !TREE_OVERFLOW (convert (c_common_signed_type (type
),
4633 /* Do not warn for enumeration types. */
4634 && (TREE_CODE (expr_original_type (primop0
)) != ENUMERAL_TYPE
);
4640 warning_at (loc
, OPT_Wtype_limits
,
4641 "comparison of unsigned expression >= 0 is always true");
4642 value
= truthvalue_true_node
;
4647 warning_at (loc
, OPT_Wtype_limits
,
4648 "comparison of unsigned expression < 0 is always false");
4649 value
= truthvalue_false_node
;
4658 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4659 if (TREE_SIDE_EFFECTS (primop0
))
4660 return build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
4667 *op0_ptr
= convert (type
, primop0
);
4668 *op1_ptr
= convert (type
, primop1
);
4670 *restype_ptr
= truthvalue_type_node
;
4675 /* Return a tree for the sum or difference (RESULTCODE says which)
4676 of pointer PTROP and integer INTOP. */
4679 pointer_int_sum (location_t loc
, enum tree_code resultcode
,
4680 tree ptrop
, tree intop
, bool complain
)
4684 /* The result is a pointer of the same type that is being added. */
4685 tree result_type
= TREE_TYPE (ptrop
);
4687 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
4689 if (complain
&& warn_pointer_arith
)
4690 pedwarn (loc
, OPT_Wpointer_arith
,
4691 "pointer of type %<void *%> used in arithmetic");
4693 return error_mark_node
;
4694 size_exp
= integer_one_node
;
4696 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
4698 if (complain
&& warn_pointer_arith
)
4699 pedwarn (loc
, OPT_Wpointer_arith
,
4700 "pointer to a function used in arithmetic");
4702 return error_mark_node
;
4703 size_exp
= integer_one_node
;
4706 size_exp
= size_in_bytes (TREE_TYPE (result_type
));
4708 /* We are manipulating pointer values, so we don't need to warn
4709 about relying on undefined signed overflow. We disable the
4710 warning here because we use integer types so fold won't know that
4711 they are really pointers. */
4712 fold_defer_overflow_warnings ();
4714 /* If what we are about to multiply by the size of the elements
4715 contains a constant term, apply distributive law
4716 and multiply that constant term separately.
4717 This helps produce common subexpressions. */
4718 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
4719 && !TREE_CONSTANT (intop
)
4720 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
4721 && TREE_CONSTANT (size_exp
)
4722 /* If the constant comes from pointer subtraction,
4723 skip this optimization--it would cause an error. */
4724 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop
, 0))) == INTEGER_TYPE
4725 /* If the constant is unsigned, and smaller than the pointer size,
4726 then we must skip this optimization. This is because it could cause
4727 an overflow error if the constant is negative but INTOP is not. */
4728 && (!TYPE_UNSIGNED (TREE_TYPE (intop
))
4729 || (TYPE_PRECISION (TREE_TYPE (intop
))
4730 == TYPE_PRECISION (TREE_TYPE (ptrop
)))))
4732 enum tree_code subcode
= resultcode
;
4733 tree int_type
= TREE_TYPE (intop
);
4734 if (TREE_CODE (intop
) == MINUS_EXPR
)
4735 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
4736 /* Convert both subexpression types to the type of intop,
4737 because weird cases involving pointer arithmetic
4738 can result in a sum or difference with different type args. */
4739 ptrop
= build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop
, 1)),
4741 convert (int_type
, TREE_OPERAND (intop
, 1)), 1);
4742 intop
= convert (int_type
, TREE_OPERAND (intop
, 0));
4745 /* Convert the integer argument to a type the same size as sizetype
4746 so the multiply won't overflow spuriously. */
4747 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
)
4748 || TYPE_UNSIGNED (TREE_TYPE (intop
)) != TYPE_UNSIGNED (sizetype
))
4749 intop
= convert (c_common_type_for_size (TYPE_PRECISION (sizetype
),
4750 TYPE_UNSIGNED (sizetype
)), intop
);
4752 /* Replace the integer argument with a suitable product by the object size.
4753 Do this multiplication as signed, then convert to the appropriate type
4754 for the pointer operation and disregard an overflow that occurred only
4755 because of the sign-extension change in the latter conversion. */
4757 tree t
= build_binary_op (loc
,
4759 convert (TREE_TYPE (intop
), size_exp
), 1);
4760 intop
= convert (sizetype
, t
);
4761 if (TREE_OVERFLOW_P (intop
) && !TREE_OVERFLOW (t
))
4762 intop
= wide_int_to_tree (TREE_TYPE (intop
), intop
);
4765 /* Create the sum or difference. */
4766 if (resultcode
== MINUS_EXPR
)
4767 intop
= fold_build1_loc (loc
, NEGATE_EXPR
, sizetype
, intop
);
4769 ret
= fold_build_pointer_plus_loc (loc
, ptrop
, intop
);
4771 fold_undefer_and_ignore_overflow_warnings ();
4776 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4777 and if NON_CONST is known not to be permitted in an evaluated part
4778 of a constant expression. */
4781 c_wrap_maybe_const (tree expr
, bool non_const
)
4783 bool nowarning
= TREE_NO_WARNING (expr
);
4784 location_t loc
= EXPR_LOCATION (expr
);
4786 /* This should never be called for C++. */
4787 if (c_dialect_cxx ())
4790 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4791 STRIP_TYPE_NOPS (expr
);
4792 expr
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL
, expr
);
4793 C_MAYBE_CONST_EXPR_NON_CONST (expr
) = non_const
;
4795 TREE_NO_WARNING (expr
) = 1;
4796 protected_set_expr_location (expr
, loc
);
4801 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4802 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4803 around the SAVE_EXPR if needed so that c_fully_fold does not need
4804 to look inside SAVE_EXPRs. */
4807 c_save_expr (tree expr
)
4809 bool maybe_const
= true;
4810 if (c_dialect_cxx ())
4811 return save_expr (expr
);
4812 expr
= c_fully_fold (expr
, false, &maybe_const
);
4813 expr
= save_expr (expr
);
4815 expr
= c_wrap_maybe_const (expr
, true);
4819 /* Return whether EXPR is a declaration whose address can never be
4823 decl_with_nonnull_addr_p (const_tree expr
)
4825 return (DECL_P (expr
)
4826 && (TREE_CODE (expr
) == PARM_DECL
4827 || TREE_CODE (expr
) == LABEL_DECL
4828 || !DECL_WEAK (expr
)));
4831 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4832 or for an `if' or `while' statement or ?..: exp. It should already
4833 have been validated to be of suitable type; otherwise, a bad
4834 diagnostic may result.
4836 The EXPR is located at LOCATION.
4838 This preparation consists of taking the ordinary
4839 representation of an expression expr and producing a valid tree
4840 boolean expression describing whether expr is nonzero. We could
4841 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4842 but we optimize comparisons, &&, ||, and !.
4844 The resulting type should always be `truthvalue_type_node'. */
4847 c_common_truthvalue_conversion (location_t location
, tree expr
)
4849 switch (TREE_CODE (expr
))
4851 case EQ_EXPR
: case NE_EXPR
: case UNEQ_EXPR
: case LTGT_EXPR
:
4852 case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
4853 case UNLE_EXPR
: case UNGE_EXPR
: case UNLT_EXPR
: case UNGT_EXPR
:
4854 case ORDERED_EXPR
: case UNORDERED_EXPR
:
4855 if (TREE_TYPE (expr
) == truthvalue_type_node
)
4857 expr
= build2 (TREE_CODE (expr
), truthvalue_type_node
,
4858 TREE_OPERAND (expr
, 0), TREE_OPERAND (expr
, 1));
4861 case TRUTH_ANDIF_EXPR
:
4862 case TRUTH_ORIF_EXPR
:
4863 case TRUTH_AND_EXPR
:
4865 case TRUTH_XOR_EXPR
:
4866 if (TREE_TYPE (expr
) == truthvalue_type_node
)
4868 expr
= build2 (TREE_CODE (expr
), truthvalue_type_node
,
4869 c_common_truthvalue_conversion (location
,
4870 TREE_OPERAND (expr
, 0)),
4871 c_common_truthvalue_conversion (location
,
4872 TREE_OPERAND (expr
, 1)));
4875 case TRUTH_NOT_EXPR
:
4876 if (TREE_TYPE (expr
) == truthvalue_type_node
)
4878 expr
= build1 (TREE_CODE (expr
), truthvalue_type_node
,
4879 c_common_truthvalue_conversion (location
,
4880 TREE_OPERAND (expr
, 0)));
4887 return integer_zerop (expr
) ? truthvalue_false_node
4888 : truthvalue_true_node
;
4891 return real_compare (NE_EXPR
, &TREE_REAL_CST (expr
), &dconst0
)
4892 ? truthvalue_true_node
4893 : truthvalue_false_node
;
4896 return fixed_compare (NE_EXPR
, &TREE_FIXED_CST (expr
),
4897 &FCONST0 (TYPE_MODE (TREE_TYPE (expr
))))
4898 ? truthvalue_true_node
4899 : truthvalue_false_node
;
4902 expr
= build_unary_op (location
, ADDR_EXPR
, expr
, 0);
4907 tree inner
= TREE_OPERAND (expr
, 0);
4908 if (decl_with_nonnull_addr_p (inner
))
4910 /* Common Ada/Pascal programmer's mistake. */
4911 warning_at (location
,
4913 "the address of %qD will always evaluate as %<true%>",
4915 return truthvalue_true_node
;
4921 expr
= build_binary_op (EXPR_LOCATION (expr
),
4922 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
4923 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
4924 c_common_truthvalue_conversion (location
,
4925 TREE_OPERAND (expr
, 0)),
4926 c_common_truthvalue_conversion (location
,
4927 TREE_OPERAND (expr
, 1)),
4934 case EXCESS_PRECISION_EXPR
:
4935 /* These don't change whether an object is nonzero or zero. */
4936 return c_common_truthvalue_conversion (location
, TREE_OPERAND (expr
, 0));
4940 /* These don't change whether an object is zero or nonzero, but
4941 we can't ignore them if their second arg has side-effects. */
4942 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
4944 expr
= build2 (COMPOUND_EXPR
, truthvalue_type_node
,
4945 TREE_OPERAND (expr
, 1),
4946 c_common_truthvalue_conversion
4947 (location
, TREE_OPERAND (expr
, 0)));
4951 return c_common_truthvalue_conversion (location
,
4952 TREE_OPERAND (expr
, 0));
4955 /* Distribute the conversion into the arms of a COND_EXPR. */
4956 if (c_dialect_cxx ())
4958 tree op1
= TREE_OPERAND (expr
, 1);
4959 tree op2
= TREE_OPERAND (expr
, 2);
4960 /* In C++ one of the arms might have void type if it is throw. */
4961 if (!VOID_TYPE_P (TREE_TYPE (op1
)))
4962 op1
= c_common_truthvalue_conversion (location
, op1
);
4963 if (!VOID_TYPE_P (TREE_TYPE (op2
)))
4964 op2
= c_common_truthvalue_conversion (location
, op2
);
4965 expr
= fold_build3_loc (location
, COND_EXPR
, truthvalue_type_node
,
4966 TREE_OPERAND (expr
, 0), op1
, op2
);
4971 /* Folding will happen later for C. */
4972 expr
= build3 (COND_EXPR
, truthvalue_type_node
,
4973 TREE_OPERAND (expr
, 0),
4974 c_common_truthvalue_conversion (location
,
4975 TREE_OPERAND (expr
, 1)),
4976 c_common_truthvalue_conversion (location
,
4977 TREE_OPERAND (expr
, 2)));
4983 tree totype
= TREE_TYPE (expr
);
4984 tree fromtype
= TREE_TYPE (TREE_OPERAND (expr
, 0));
4986 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4987 since that affects how `default_conversion' will behave. */
4988 if (TREE_CODE (totype
) == REFERENCE_TYPE
4989 || TREE_CODE (fromtype
) == REFERENCE_TYPE
)
4991 /* Don't strip a conversion from C++0x scoped enum, since they
4992 don't implicitly convert to other types. */
4993 if (TREE_CODE (fromtype
) == ENUMERAL_TYPE
4994 && ENUM_IS_SCOPED (fromtype
))
4996 /* If this isn't narrowing the argument, we can ignore it. */
4997 if (TYPE_PRECISION (totype
) >= TYPE_PRECISION (fromtype
))
4998 return c_common_truthvalue_conversion (location
,
4999 TREE_OPERAND (expr
, 0));
5004 if (!TREE_NO_WARNING (expr
)
5005 && warn_parentheses
)
5007 warning (OPT_Wparentheses
,
5008 "suggest parentheses around assignment used as truth value");
5009 TREE_NO_WARNING (expr
) = 1;
5017 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
5019 tree t
= (in_late_binary_op
? save_expr (expr
) : c_save_expr (expr
));
5020 expr
= (build_binary_op
5021 (EXPR_LOCATION (expr
),
5022 (TREE_SIDE_EFFECTS (expr
)
5023 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
5024 c_common_truthvalue_conversion
5026 build_unary_op (location
, REALPART_EXPR
, t
, 0)),
5027 c_common_truthvalue_conversion
5029 build_unary_op (location
, IMAGPART_EXPR
, t
, 0)),
5034 if (TREE_CODE (TREE_TYPE (expr
)) == FIXED_POINT_TYPE
)
5036 tree fixed_zero_node
= build_fixed (TREE_TYPE (expr
),
5038 (TREE_TYPE (expr
))));
5039 return build_binary_op (location
, NE_EXPR
, expr
, fixed_zero_node
, 1);
5042 return build_binary_op (location
, NE_EXPR
, expr
, integer_zero_node
, 1);
5045 protected_set_expr_location (expr
, location
);
5049 static void def_builtin_1 (enum built_in_function fncode
,
5051 enum built_in_class fnclass
,
5052 tree fntype
, tree libtype
,
5053 bool both_p
, bool fallback_p
, bool nonansi_p
,
5054 tree fnattrs
, bool implicit_p
);
5057 /* Apply the TYPE_QUALS to the new DECL. */
5060 c_apply_type_quals_to_decl (int type_quals
, tree decl
)
5062 tree type
= TREE_TYPE (decl
);
5064 if (type
== error_mark_node
)
5067 if ((type_quals
& TYPE_QUAL_CONST
)
5068 || (type
&& TREE_CODE (type
) == REFERENCE_TYPE
))
5069 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5070 constructor can produce constant init, so rely on cp_finish_decl to
5071 clear TREE_READONLY if the variable has non-constant init. */
5072 TREE_READONLY (decl
) = 1;
5073 if (type_quals
& TYPE_QUAL_VOLATILE
)
5075 TREE_SIDE_EFFECTS (decl
) = 1;
5076 TREE_THIS_VOLATILE (decl
) = 1;
5078 if (type_quals
& TYPE_QUAL_RESTRICT
)
5080 while (type
&& TREE_CODE (type
) == ARRAY_TYPE
)
5081 /* Allow 'restrict' on arrays of pointers.
5082 FIXME currently we just ignore it. */
5083 type
= TREE_TYPE (type
);
5085 || !POINTER_TYPE_P (type
)
5086 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
)))
5087 error ("invalid use of %<restrict%>");
5091 struct c_type_hasher
: ggc_hasher
<tree
>
5093 static hashval_t
hash (tree
);
5094 static bool equal (tree
, tree
);
5097 /* Hash function for the problem of multiple type definitions in
5098 different files. This must hash all types that will compare
5099 equal via comptypes to the same value. In practice it hashes
5100 on some of the simple stuff and leaves the details to comptypes. */
5103 c_type_hasher::hash (tree t
)
5108 switch (TREE_CODE (t
))
5110 /* For pointers, hash on pointee type plus some swizzling. */
5112 return hash (TREE_TYPE (t
)) ^ 0x3003003;
5113 /* Hash on number of elements and total size. */
5116 t2
= TYPE_VALUES (t
);
5120 t2
= TYPE_FIELDS (t
);
5122 case QUAL_UNION_TYPE
:
5124 t2
= TYPE_FIELDS (t
);
5128 t2
= TYPE_FIELDS (t
);
5133 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5134 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5135 n_elements
= list_length (t2
);
5136 /* We might have a VLA here. */
5137 if (TREE_CODE (TYPE_SIZE (t
)) != INTEGER_CST
)
5140 size
= TREE_INT_CST_LOW (TYPE_SIZE (t
));
5141 return ((size
<< 24) | (n_elements
<< shift
));
5145 c_type_hasher::equal (tree t1
, tree t2
)
5147 return lang_hooks
.types_compatible_p (t1
, t2
);
5150 static GTY(()) hash_table
<c_type_hasher
> *type_hash_table
;
5152 /* Return the typed-based alias set for T, which may be an expression
5153 or a type. Return -1 if we don't do anything special. */
5156 c_common_get_alias_set (tree t
)
5160 /* For VLAs, use the alias set of the element type rather than the
5161 default of alias set 0 for types compared structurally. */
5162 if (TYPE_P (t
) && TYPE_STRUCTURAL_EQUALITY_P (t
))
5164 if (TREE_CODE (t
) == ARRAY_TYPE
)
5165 return get_alias_set (TREE_TYPE (t
));
5169 /* Permit type-punning when accessing a union, provided the access
5170 is directly through the union. For example, this code does not
5171 permit taking the address of a union member and then storing
5172 through it. Even the type-punning allowed here is a GCC
5173 extension, albeit a common and useful one; the C standard says
5174 that such accesses have implementation-defined behavior. */
5176 TREE_CODE (u
) == COMPONENT_REF
|| TREE_CODE (u
) == ARRAY_REF
;
5177 u
= TREE_OPERAND (u
, 0))
5178 if (TREE_CODE (u
) == COMPONENT_REF
5179 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u
, 0))) == UNION_TYPE
)
5182 /* That's all the expressions we handle specially. */
5186 /* The C standard guarantees that any object may be accessed via an
5187 lvalue that has character type. */
5188 if (t
== char_type_node
5189 || t
== signed_char_type_node
5190 || t
== unsigned_char_type_node
)
5193 /* The C standard specifically allows aliasing between signed and
5194 unsigned variants of the same type. We treat the signed
5195 variant as canonical. */
5196 if (TREE_CODE (t
) == INTEGER_TYPE
&& TYPE_UNSIGNED (t
))
5198 tree t1
= c_common_signed_type (t
);
5200 /* t1 == t can happen for boolean nodes which are always unsigned. */
5202 return get_alias_set (t1
);
5205 /* Handle the case of multiple type nodes referring to "the same" type,
5206 which occurs with IMA. These share an alias set. FIXME: Currently only
5207 C90 is handled. (In C99 type compatibility is not transitive, which
5208 complicates things mightily. The alias set splay trees can theoretically
5209 represent this, but insertion is tricky when you consider all the
5210 different orders things might arrive in.) */
5212 if (c_language
!= clk_c
|| flag_isoc99
)
5215 /* Save time if there's only one input file. */
5216 if (num_in_fnames
== 1)
5219 /* Pointers need special handling if they point to any type that
5220 needs special handling (below). */
5221 if (TREE_CODE (t
) == POINTER_TYPE
)
5224 /* Find bottom type under any nested POINTERs. */
5225 for (t2
= TREE_TYPE (t
);
5226 TREE_CODE (t2
) == POINTER_TYPE
;
5227 t2
= TREE_TYPE (t2
))
5229 if (TREE_CODE (t2
) != RECORD_TYPE
5230 && TREE_CODE (t2
) != ENUMERAL_TYPE
5231 && TREE_CODE (t2
) != QUAL_UNION_TYPE
5232 && TREE_CODE (t2
) != UNION_TYPE
)
5234 if (TYPE_SIZE (t2
) == 0)
5237 /* These are the only cases that need special handling. */
5238 if (TREE_CODE (t
) != RECORD_TYPE
5239 && TREE_CODE (t
) != ENUMERAL_TYPE
5240 && TREE_CODE (t
) != QUAL_UNION_TYPE
5241 && TREE_CODE (t
) != UNION_TYPE
5242 && TREE_CODE (t
) != POINTER_TYPE
)
5245 if (TYPE_SIZE (t
) == 0)
5248 /* Look up t in hash table. Only one of the compatible types within each
5249 alias set is recorded in the table. */
5250 if (!type_hash_table
)
5251 type_hash_table
= hash_table
<c_type_hasher
>::create_ggc (1021);
5252 tree
*slot
= type_hash_table
->find_slot (t
, INSERT
);
5255 TYPE_ALIAS_SET (t
) = TYPE_ALIAS_SET ((tree
)*slot
);
5256 return TYPE_ALIAS_SET ((tree
)*slot
);
5259 /* Our caller will assign and record (in t) a new alias set; all we need
5260 to do is remember t in the hash table. */
5266 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5267 the IS_SIZEOF parameter indicates which operator is being applied.
5268 The COMPLAIN flag controls whether we should diagnose possibly
5269 ill-formed constructs or not. LOC is the location of the SIZEOF or
5270 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5271 a type in any context should be returned, rather than the normal
5272 alignment for that type. */
5275 c_sizeof_or_alignof_type (location_t loc
,
5276 tree type
, bool is_sizeof
, bool min_alignof
,
5279 const char *op_name
;
5281 enum tree_code type_code
= TREE_CODE (type
);
5283 op_name
= is_sizeof
? "sizeof" : "__alignof__";
5285 if (type_code
== FUNCTION_TYPE
)
5289 if (complain
&& warn_pointer_arith
)
5290 pedwarn (loc
, OPT_Wpointer_arith
,
5291 "invalid application of %<sizeof%> to a function type");
5293 return error_mark_node
;
5294 value
= size_one_node
;
5300 if (c_dialect_cxx ())
5301 pedwarn (loc
, OPT_Wpedantic
, "ISO C++ does not permit "
5302 "%<alignof%> applied to a function type");
5304 pedwarn (loc
, OPT_Wpedantic
, "ISO C does not permit "
5305 "%<_Alignof%> applied to a function type");
5307 value
= size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
5310 else if (type_code
== VOID_TYPE
|| type_code
== ERROR_MARK
)
5312 if (type_code
== VOID_TYPE
5313 && complain
&& warn_pointer_arith
)
5314 pedwarn (loc
, OPT_Wpointer_arith
,
5315 "invalid application of %qs to a void type", op_name
);
5317 return error_mark_node
;
5318 value
= size_one_node
;
5320 else if (!COMPLETE_TYPE_P (type
)
5321 && (!c_dialect_cxx () || is_sizeof
|| type_code
!= ARRAY_TYPE
))
5324 error_at (loc
, "invalid application of %qs to incomplete type %qT",
5326 return error_mark_node
;
5328 else if (c_dialect_cxx () && type_code
== ARRAY_TYPE
5329 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
5332 error_at (loc
, "invalid application of %qs to array type %qT of "
5333 "incomplete element type", op_name
, type
);
5334 return error_mark_node
;
5339 /* Convert in case a char is more than one unit. */
5340 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
5341 size_int (TYPE_PRECISION (char_type_node
)
5343 else if (min_alignof
)
5344 value
= size_int (min_align_of_type (type
));
5346 value
= size_int (TYPE_ALIGN_UNIT (type
));
5349 /* VALUE will have the middle-end integer type sizetype.
5350 However, we should really return a value of type `size_t',
5351 which is just a typedef for an ordinary integer type. */
5352 value
= fold_convert_loc (loc
, size_type_node
, value
);
5357 /* Implement the __alignof keyword: Return the minimum required
5358 alignment of EXPR, measured in bytes. For VAR_DECLs,
5359 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5360 from an "aligned" __attribute__ specification). LOC is the
5361 location of the ALIGNOF operator. */
5364 c_alignof_expr (location_t loc
, tree expr
)
5368 if (VAR_OR_FUNCTION_DECL_P (expr
))
5369 t
= size_int (DECL_ALIGN_UNIT (expr
));
5371 else if (TREE_CODE (expr
) == COMPONENT_REF
5372 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
5374 error_at (loc
, "%<__alignof%> applied to a bit-field");
5377 else if (TREE_CODE (expr
) == COMPONENT_REF
5378 && TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
)
5379 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr
, 1)));
5381 else if (TREE_CODE (expr
) == INDIRECT_REF
)
5383 tree t
= TREE_OPERAND (expr
, 0);
5385 int bestalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
5387 while (CONVERT_EXPR_P (t
)
5388 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == POINTER_TYPE
)
5392 t
= TREE_OPERAND (t
, 0);
5393 thisalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
5394 if (thisalign
> bestalign
)
5395 best
= t
, bestalign
= thisalign
;
5397 return c_alignof (loc
, TREE_TYPE (TREE_TYPE (best
)));
5400 return c_alignof (loc
, TREE_TYPE (expr
));
5402 return fold_convert_loc (loc
, size_type_node
, t
);
5405 /* Handle C and C++ default attributes. */
5407 enum built_in_attribute
5409 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5410 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5411 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5412 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5413 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5414 #include "builtin-attrs.def"
5415 #undef DEF_ATTR_NULL_TREE
5417 #undef DEF_ATTR_STRING
5418 #undef DEF_ATTR_IDENT
5419 #undef DEF_ATTR_TREE_LIST
5423 static GTY(()) tree built_in_attributes
[(int) ATTR_LAST
];
5425 static void c_init_attributes (void);
5429 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5430 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5431 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5432 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5433 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5434 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5435 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5436 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5438 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5440 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5441 ARG6, ARG7, ARG8) NAME,
5442 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5443 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5444 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5445 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5446 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5447 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5449 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5451 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5452 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5453 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5454 #include "builtin-types.def"
5455 #undef DEF_PRIMITIVE_TYPE
5456 #undef DEF_FUNCTION_TYPE_0
5457 #undef DEF_FUNCTION_TYPE_1
5458 #undef DEF_FUNCTION_TYPE_2
5459 #undef DEF_FUNCTION_TYPE_3
5460 #undef DEF_FUNCTION_TYPE_4
5461 #undef DEF_FUNCTION_TYPE_5
5462 #undef DEF_FUNCTION_TYPE_6
5463 #undef DEF_FUNCTION_TYPE_7
5464 #undef DEF_FUNCTION_TYPE_8
5465 #undef DEF_FUNCTION_TYPE_VAR_0
5466 #undef DEF_FUNCTION_TYPE_VAR_1
5467 #undef DEF_FUNCTION_TYPE_VAR_2
5468 #undef DEF_FUNCTION_TYPE_VAR_3
5469 #undef DEF_FUNCTION_TYPE_VAR_4
5470 #undef DEF_FUNCTION_TYPE_VAR_5
5471 #undef DEF_FUNCTION_TYPE_VAR_7
5472 #undef DEF_FUNCTION_TYPE_VAR_11
5473 #undef DEF_POINTER_TYPE
5477 typedef enum c_builtin_type builtin_type
;
5479 /* A temporary array for c_common_nodes_and_builtins. Used in
5480 communication with def_fn_type. */
5481 static tree builtin_types
[(int) BT_LAST
+ 1];
5483 /* A helper function for c_common_nodes_and_builtins. Build function type
5484 for DEF with return type RET and N arguments. If VAR is true, then the
5485 function should be variadic after those N arguments.
5487 Takes special care not to ICE if any of the types involved are
5488 error_mark_node, which indicates that said type is not in fact available
5489 (see builtin_type_for_size). In which case the function type as a whole
5490 should be error_mark_node. */
5493 def_fn_type (builtin_type def
, builtin_type ret
, bool var
, int n
, ...)
5496 tree
*args
= XALLOCAVEC (tree
, n
);
5501 for (i
= 0; i
< n
; ++i
)
5503 builtin_type a
= (builtin_type
) va_arg (list
, int);
5504 t
= builtin_types
[a
];
5505 if (t
== error_mark_node
)
5510 t
= builtin_types
[ret
];
5511 if (t
== error_mark_node
)
5514 t
= build_varargs_function_type_array (t
, n
, args
);
5516 t
= build_function_type_array (t
, n
, args
);
5519 builtin_types
[def
] = t
;
5523 /* Build builtin functions common to both C and C++ language
5527 c_define_builtins (tree va_list_ref_type_node
, tree va_list_arg_type_node
)
5529 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5530 builtin_types[ENUM] = VALUE;
5531 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5532 def_fn_type (ENUM, RETURN, 0, 0);
5533 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5534 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5535 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5536 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5537 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5538 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5539 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5540 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5541 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5542 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5543 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5545 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5546 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5548 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5549 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5551 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5553 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5554 def_fn_type (ENUM, RETURN, 1, 0);
5555 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5556 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5557 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5558 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5559 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5560 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5561 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5562 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5563 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5564 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5565 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5567 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5568 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5569 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5570 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5571 ARG7, ARG8, ARG9, ARG10, ARG11);
5572 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5573 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5575 #include "builtin-types.def"
5577 #undef DEF_PRIMITIVE_TYPE
5578 #undef DEF_FUNCTION_TYPE_0
5579 #undef DEF_FUNCTION_TYPE_1
5580 #undef DEF_FUNCTION_TYPE_2
5581 #undef DEF_FUNCTION_TYPE_3
5582 #undef DEF_FUNCTION_TYPE_4
5583 #undef DEF_FUNCTION_TYPE_5
5584 #undef DEF_FUNCTION_TYPE_6
5585 #undef DEF_FUNCTION_TYPE_7
5586 #undef DEF_FUNCTION_TYPE_8
5587 #undef DEF_FUNCTION_TYPE_VAR_0
5588 #undef DEF_FUNCTION_TYPE_VAR_1
5589 #undef DEF_FUNCTION_TYPE_VAR_2
5590 #undef DEF_FUNCTION_TYPE_VAR_3
5591 #undef DEF_FUNCTION_TYPE_VAR_4
5592 #undef DEF_FUNCTION_TYPE_VAR_5
5593 #undef DEF_FUNCTION_TYPE_VAR_7
5594 #undef DEF_FUNCTION_TYPE_VAR_11
5595 #undef DEF_POINTER_TYPE
5596 builtin_types
[(int) BT_LAST
] = NULL_TREE
;
5598 c_init_attributes ();
5600 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5601 NONANSI_P, ATTRS, IMPLICIT, COND) \
5603 def_builtin_1 (ENUM, NAME, CLASS, \
5604 builtin_types[(int) TYPE], \
5605 builtin_types[(int) LIBTYPE], \
5606 BOTH_P, FALLBACK_P, NONANSI_P, \
5607 built_in_attributes[(int) ATTRS], IMPLICIT);
5608 #include "builtins.def"
5611 targetm
.init_builtins ();
5613 build_common_builtin_nodes ();
5616 cilk_init_builtins ();
5619 /* Like get_identifier, but avoid warnings about null arguments when
5620 the argument may be NULL for targets where GCC lacks stdint.h type
5624 c_get_ident (const char *id
)
5626 return get_identifier (id
);
5629 /* Build tree nodes and builtin functions common to both C and C++ language
5633 c_common_nodes_and_builtins (void)
5635 int char16_type_size
;
5636 int char32_type_size
;
5637 int wchar_type_size
;
5638 tree array_domain_type
;
5639 tree va_list_ref_type_node
;
5640 tree va_list_arg_type_node
;
5643 build_common_tree_nodes (flag_signed_char
, flag_short_double
);
5645 /* Define `int' and `char' first so that dbx will output them first. */
5646 record_builtin_type (RID_INT
, NULL
, integer_type_node
);
5647 record_builtin_type (RID_CHAR
, "char", char_type_node
);
5649 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5650 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5651 but not C. Are the conditionals here needed? */
5652 if (c_dialect_cxx ())
5653 record_builtin_type (RID_SIGNED
, NULL
, integer_type_node
);
5654 record_builtin_type (RID_LONG
, "long int", long_integer_type_node
);
5655 record_builtin_type (RID_UNSIGNED
, "unsigned int", unsigned_type_node
);
5656 record_builtin_type (RID_MAX
, "long unsigned int",
5657 long_unsigned_type_node
);
5659 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
5663 sprintf (name
, "__int%d", int_n_data
[i
].bitsize
);
5664 record_builtin_type ((enum rid
)(RID_FIRST_INT_N
+ i
), name
,
5665 int_n_trees
[i
].signed_type
);
5666 sprintf (name
, "__int%d unsigned", int_n_data
[i
].bitsize
);
5667 record_builtin_type (RID_MAX
, name
, int_n_trees
[i
].unsigned_type
);
5670 if (c_dialect_cxx ())
5671 record_builtin_type (RID_MAX
, "unsigned long", long_unsigned_type_node
);
5672 record_builtin_type (RID_MAX
, "long long int",
5673 long_long_integer_type_node
);
5674 record_builtin_type (RID_MAX
, "long long unsigned int",
5675 long_long_unsigned_type_node
);
5676 if (c_dialect_cxx ())
5677 record_builtin_type (RID_MAX
, "long long unsigned",
5678 long_long_unsigned_type_node
);
5679 record_builtin_type (RID_SHORT
, "short int", short_integer_type_node
);
5680 record_builtin_type (RID_MAX
, "short unsigned int",
5681 short_unsigned_type_node
);
5682 if (c_dialect_cxx ())
5683 record_builtin_type (RID_MAX
, "unsigned short",
5684 short_unsigned_type_node
);
5686 /* Define both `signed char' and `unsigned char'. */
5687 record_builtin_type (RID_MAX
, "signed char", signed_char_type_node
);
5688 record_builtin_type (RID_MAX
, "unsigned char", unsigned_char_type_node
);
5690 /* These are types that c_common_type_for_size and
5691 c_common_type_for_mode use. */
5692 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5693 TYPE_DECL
, NULL_TREE
,
5695 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5696 TYPE_DECL
, NULL_TREE
,
5698 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5699 TYPE_DECL
, NULL_TREE
,
5701 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5702 TYPE_DECL
, NULL_TREE
,
5704 #if HOST_BITS_PER_WIDE_INT >= 64
5705 /* Note that this is different than the __int128 type that's part of
5706 the generic __intN support. */
5707 if (targetm
.scalar_mode_supported_p (TImode
))
5708 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5710 get_identifier ("__int128_t"),
5713 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5714 TYPE_DECL
, NULL_TREE
,
5715 unsigned_intQI_type_node
));
5716 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5717 TYPE_DECL
, NULL_TREE
,
5718 unsigned_intHI_type_node
));
5719 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5720 TYPE_DECL
, NULL_TREE
,
5721 unsigned_intSI_type_node
));
5722 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5723 TYPE_DECL
, NULL_TREE
,
5724 unsigned_intDI_type_node
));
5725 #if HOST_BITS_PER_WIDE_INT >= 64
5726 if (targetm
.scalar_mode_supported_p (TImode
))
5727 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5729 get_identifier ("__uint128_t"),
5730 unsigned_intTI_type_node
));
5733 /* Create the widest literal types. */
5734 widest_integer_literal_type_node
5735 = make_signed_type (HOST_BITS_PER_WIDE_INT
* 2);
5736 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5737 TYPE_DECL
, NULL_TREE
,
5738 widest_integer_literal_type_node
));
5740 widest_unsigned_literal_type_node
5741 = make_unsigned_type (HOST_BITS_PER_WIDE_INT
* 2);
5742 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5743 TYPE_DECL
, NULL_TREE
,
5744 widest_unsigned_literal_type_node
));
5746 signed_size_type_node
= c_common_signed_type (size_type_node
);
5749 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE
)));
5751 record_builtin_type (RID_FLOAT
, NULL
, float_type_node
);
5752 record_builtin_type (RID_DOUBLE
, NULL
, double_type_node
);
5753 record_builtin_type (RID_MAX
, "long double", long_double_type_node
);
5755 /* Only supported decimal floating point extension if the target
5756 actually supports underlying modes. */
5757 if (targetm
.scalar_mode_supported_p (SDmode
)
5758 && targetm
.scalar_mode_supported_p (DDmode
)
5759 && targetm
.scalar_mode_supported_p (TDmode
))
5761 record_builtin_type (RID_DFLOAT32
, NULL
, dfloat32_type_node
);
5762 record_builtin_type (RID_DFLOAT64
, NULL
, dfloat64_type_node
);
5763 record_builtin_type (RID_DFLOAT128
, NULL
, dfloat128_type_node
);
5766 if (targetm
.fixed_point_supported_p ())
5768 record_builtin_type (RID_MAX
, "short _Fract", short_fract_type_node
);
5769 record_builtin_type (RID_FRACT
, NULL
, fract_type_node
);
5770 record_builtin_type (RID_MAX
, "long _Fract", long_fract_type_node
);
5771 record_builtin_type (RID_MAX
, "long long _Fract",
5772 long_long_fract_type_node
);
5773 record_builtin_type (RID_MAX
, "unsigned short _Fract",
5774 unsigned_short_fract_type_node
);
5775 record_builtin_type (RID_MAX
, "unsigned _Fract",
5776 unsigned_fract_type_node
);
5777 record_builtin_type (RID_MAX
, "unsigned long _Fract",
5778 unsigned_long_fract_type_node
);
5779 record_builtin_type (RID_MAX
, "unsigned long long _Fract",
5780 unsigned_long_long_fract_type_node
);
5781 record_builtin_type (RID_MAX
, "_Sat short _Fract",
5782 sat_short_fract_type_node
);
5783 record_builtin_type (RID_MAX
, "_Sat _Fract", sat_fract_type_node
);
5784 record_builtin_type (RID_MAX
, "_Sat long _Fract",
5785 sat_long_fract_type_node
);
5786 record_builtin_type (RID_MAX
, "_Sat long long _Fract",
5787 sat_long_long_fract_type_node
);
5788 record_builtin_type (RID_MAX
, "_Sat unsigned short _Fract",
5789 sat_unsigned_short_fract_type_node
);
5790 record_builtin_type (RID_MAX
, "_Sat unsigned _Fract",
5791 sat_unsigned_fract_type_node
);
5792 record_builtin_type (RID_MAX
, "_Sat unsigned long _Fract",
5793 sat_unsigned_long_fract_type_node
);
5794 record_builtin_type (RID_MAX
, "_Sat unsigned long long _Fract",
5795 sat_unsigned_long_long_fract_type_node
);
5796 record_builtin_type (RID_MAX
, "short _Accum", short_accum_type_node
);
5797 record_builtin_type (RID_ACCUM
, NULL
, accum_type_node
);
5798 record_builtin_type (RID_MAX
, "long _Accum", long_accum_type_node
);
5799 record_builtin_type (RID_MAX
, "long long _Accum",
5800 long_long_accum_type_node
);
5801 record_builtin_type (RID_MAX
, "unsigned short _Accum",
5802 unsigned_short_accum_type_node
);
5803 record_builtin_type (RID_MAX
, "unsigned _Accum",
5804 unsigned_accum_type_node
);
5805 record_builtin_type (RID_MAX
, "unsigned long _Accum",
5806 unsigned_long_accum_type_node
);
5807 record_builtin_type (RID_MAX
, "unsigned long long _Accum",
5808 unsigned_long_long_accum_type_node
);
5809 record_builtin_type (RID_MAX
, "_Sat short _Accum",
5810 sat_short_accum_type_node
);
5811 record_builtin_type (RID_MAX
, "_Sat _Accum", sat_accum_type_node
);
5812 record_builtin_type (RID_MAX
, "_Sat long _Accum",
5813 sat_long_accum_type_node
);
5814 record_builtin_type (RID_MAX
, "_Sat long long _Accum",
5815 sat_long_long_accum_type_node
);
5816 record_builtin_type (RID_MAX
, "_Sat unsigned short _Accum",
5817 sat_unsigned_short_accum_type_node
);
5818 record_builtin_type (RID_MAX
, "_Sat unsigned _Accum",
5819 sat_unsigned_accum_type_node
);
5820 record_builtin_type (RID_MAX
, "_Sat unsigned long _Accum",
5821 sat_unsigned_long_accum_type_node
);
5822 record_builtin_type (RID_MAX
, "_Sat unsigned long long _Accum",
5823 sat_unsigned_long_long_accum_type_node
);
5827 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5829 get_identifier ("complex int"),
5830 complex_integer_type_node
));
5831 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5833 get_identifier ("complex float"),
5834 complex_float_type_node
));
5835 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
5837 get_identifier ("complex double"),
5838 complex_double_type_node
));
5839 lang_hooks
.decls
.pushdecl
5840 (build_decl (UNKNOWN_LOCATION
,
5841 TYPE_DECL
, get_identifier ("complex long double"),
5842 complex_long_double_type_node
));
5844 if (c_dialect_cxx ())
5845 /* For C++, make fileptr_type_node a distinct void * type until
5846 FILE type is defined. */
5847 fileptr_type_node
= build_variant_type_copy (ptr_type_node
);
5849 record_builtin_type (RID_VOID
, NULL
, void_type_node
);
5851 /* Set the TYPE_NAME for any variants that were built before
5852 record_builtin_type gave names to the built-in types. */
5854 tree void_name
= TYPE_NAME (void_type_node
);
5855 TYPE_NAME (void_type_node
) = NULL_TREE
;
5856 TYPE_NAME (build_qualified_type (void_type_node
, TYPE_QUAL_CONST
))
5858 TYPE_NAME (void_type_node
) = void_name
;
5861 void_list_node
= build_void_list_node ();
5863 /* Make a type to be the domain of a few array types
5864 whose domains don't really matter.
5865 200 is small enough that it always fits in size_t
5866 and large enough that it can hold most function names for the
5867 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5868 array_domain_type
= build_index_type (size_int (200));
5870 /* Make a type for arrays of characters.
5871 With luck nothing will ever really depend on the length of this
5873 char_array_type_node
5874 = build_array_type (char_type_node
, array_domain_type
);
5876 string_type_node
= build_pointer_type (char_type_node
);
5877 const_string_type_node
5878 = build_pointer_type (build_qualified_type
5879 (char_type_node
, TYPE_QUAL_CONST
));
5881 /* This is special for C++ so functions can be overloaded. */
5882 wchar_type_node
= get_identifier (MODIFIED_WCHAR_TYPE
);
5883 wchar_type_node
= TREE_TYPE (identifier_global_value (wchar_type_node
));
5884 wchar_type_size
= TYPE_PRECISION (wchar_type_node
);
5885 underlying_wchar_type_node
= wchar_type_node
;
5886 if (c_dialect_cxx ())
5888 if (TYPE_UNSIGNED (wchar_type_node
))
5889 wchar_type_node
= make_unsigned_type (wchar_type_size
);
5891 wchar_type_node
= make_signed_type (wchar_type_size
);
5892 record_builtin_type (RID_WCHAR
, "wchar_t", wchar_type_node
);
5895 /* This is for wide string constants. */
5896 wchar_array_type_node
5897 = build_array_type (wchar_type_node
, array_domain_type
);
5899 /* Define 'char16_t'. */
5900 char16_type_node
= get_identifier (CHAR16_TYPE
);
5901 char16_type_node
= TREE_TYPE (identifier_global_value (char16_type_node
));
5902 char16_type_size
= TYPE_PRECISION (char16_type_node
);
5903 if (c_dialect_cxx ())
5905 char16_type_node
= make_unsigned_type (char16_type_size
);
5907 if (cxx_dialect
>= cxx11
)
5908 record_builtin_type (RID_CHAR16
, "char16_t", char16_type_node
);
5911 /* This is for UTF-16 string constants. */
5912 char16_array_type_node
5913 = build_array_type (char16_type_node
, array_domain_type
);
5915 /* Define 'char32_t'. */
5916 char32_type_node
= get_identifier (CHAR32_TYPE
);
5917 char32_type_node
= TREE_TYPE (identifier_global_value (char32_type_node
));
5918 char32_type_size
= TYPE_PRECISION (char32_type_node
);
5919 if (c_dialect_cxx ())
5921 char32_type_node
= make_unsigned_type (char32_type_size
);
5923 if (cxx_dialect
>= cxx11
)
5924 record_builtin_type (RID_CHAR32
, "char32_t", char32_type_node
);
5927 /* This is for UTF-32 string constants. */
5928 char32_array_type_node
5929 = build_array_type (char32_type_node
, array_domain_type
);
5932 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE
)));
5935 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE
)));
5937 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE
)));
5939 if (SIG_ATOMIC_TYPE
)
5940 sig_atomic_type_node
=
5941 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE
)));
5944 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE
)));
5947 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE
)));
5950 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE
)));
5953 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE
)));
5956 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE
)));
5958 c_uint16_type_node
= uint16_type_node
=
5959 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE
)));
5961 c_uint32_type_node
= uint32_type_node
=
5962 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE
)));
5964 c_uint64_type_node
= uint64_type_node
=
5965 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE
)));
5966 if (INT_LEAST8_TYPE
)
5967 int_least8_type_node
=
5968 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE
)));
5969 if (INT_LEAST16_TYPE
)
5970 int_least16_type_node
=
5971 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE
)));
5972 if (INT_LEAST32_TYPE
)
5973 int_least32_type_node
=
5974 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE
)));
5975 if (INT_LEAST64_TYPE
)
5976 int_least64_type_node
=
5977 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE
)));
5978 if (UINT_LEAST8_TYPE
)
5979 uint_least8_type_node
=
5980 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE
)));
5981 if (UINT_LEAST16_TYPE
)
5982 uint_least16_type_node
=
5983 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE
)));
5984 if (UINT_LEAST32_TYPE
)
5985 uint_least32_type_node
=
5986 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE
)));
5987 if (UINT_LEAST64_TYPE
)
5988 uint_least64_type_node
=
5989 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE
)));
5991 int_fast8_type_node
=
5992 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE
)));
5993 if (INT_FAST16_TYPE
)
5994 int_fast16_type_node
=
5995 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE
)));
5996 if (INT_FAST32_TYPE
)
5997 int_fast32_type_node
=
5998 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE
)));
5999 if (INT_FAST64_TYPE
)
6000 int_fast64_type_node
=
6001 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE
)));
6002 if (UINT_FAST8_TYPE
)
6003 uint_fast8_type_node
=
6004 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE
)));
6005 if (UINT_FAST16_TYPE
)
6006 uint_fast16_type_node
=
6007 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE
)));
6008 if (UINT_FAST32_TYPE
)
6009 uint_fast32_type_node
=
6010 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE
)));
6011 if (UINT_FAST64_TYPE
)
6012 uint_fast64_type_node
=
6013 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE
)));
6016 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE
)));
6019 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE
)));
6021 default_function_type
6022 = build_varargs_function_type_list (integer_type_node
, NULL_TREE
);
6024 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE
)));
6025 unsigned_ptrdiff_type_node
= c_common_unsigned_type (ptrdiff_type_node
);
6027 lang_hooks
.decls
.pushdecl
6028 (build_decl (UNKNOWN_LOCATION
,
6029 TYPE_DECL
, get_identifier ("__builtin_va_list"),
6030 va_list_type_node
));
6031 if (targetm
.enum_va_list_p
)
6037 for (l
= 0; targetm
.enum_va_list_p (l
, &pname
, &ptype
); ++l
)
6039 lang_hooks
.decls
.pushdecl
6040 (build_decl (UNKNOWN_LOCATION
,
6041 TYPE_DECL
, get_identifier (pname
),
6047 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
6049 va_list_arg_type_node
= va_list_ref_type_node
=
6050 build_pointer_type (TREE_TYPE (va_list_type_node
));
6054 va_list_arg_type_node
= va_list_type_node
;
6055 va_list_ref_type_node
= build_reference_type (va_list_type_node
);
6058 if (!flag_preprocess_only
)
6059 c_define_builtins (va_list_ref_type_node
, va_list_arg_type_node
);
6061 main_identifier_node
= get_identifier ("main");
6063 /* Create the built-in __null node. It is important that this is
6065 null_node
= make_int_cst (1, 1);
6066 TREE_TYPE (null_node
) = c_common_type_for_size (POINTER_SIZE
, 0);
6068 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6069 memset (builtin_types
, 0, sizeof (builtin_types
));
6072 /* The number of named compound-literals generated thus far. */
6073 static GTY(()) int compound_literal_number
;
6075 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6078 set_compound_literal_name (tree decl
)
6081 ASM_FORMAT_PRIVATE_NAME (name
, "__compound_literal",
6082 compound_literal_number
);
6083 compound_literal_number
++;
6084 DECL_NAME (decl
) = get_identifier (name
);
6088 build_va_arg (location_t loc
, tree expr
, tree type
)
6090 tree va_type
= TREE_TYPE (expr
);
6091 tree canon_va_type
= (va_type
== error_mark_node
6093 : targetm
.canonical_va_list_type (va_type
));
6095 if (canon_va_type
!= NULL
)
6097 /* When the va_arg ap argument is a parm decl with declared type va_list,
6098 and the va_list type is an array, then grokdeclarator changes the type
6099 of the parm decl to the corresponding pointer type. We know that that
6100 pointer is constant, so there's no need to modify it, so there's no
6101 need to pass it around using an address operator, so there's no need to
6102 mark it addressable. */
6103 if (!(TREE_CODE (canon_va_type
) == ARRAY_TYPE
6104 && TREE_CODE (va_type
) != ARRAY_TYPE
))
6105 /* In gimplify_va_arg_expr we take the address of the ap argument, mark
6106 it addressable now. */
6107 mark_addressable (expr
);
6110 expr
= build1 (VA_ARG_EXPR
, type
, expr
);
6111 SET_EXPR_LOCATION (expr
, loc
);
6116 /* Linked list of disabled built-in functions. */
6118 typedef struct disabled_builtin
6121 struct disabled_builtin
*next
;
6123 static disabled_builtin
*disabled_builtins
= NULL
;
6125 static bool builtin_function_disabled_p (const char *);
6127 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6128 begins with "__builtin_", give an error. */
6131 disable_builtin_function (const char *name
)
6133 if (strncmp (name
, "__builtin_", strlen ("__builtin_")) == 0)
6134 error ("cannot disable built-in function %qs", name
);
6137 disabled_builtin
*new_disabled_builtin
= XNEW (disabled_builtin
);
6138 new_disabled_builtin
->name
= name
;
6139 new_disabled_builtin
->next
= disabled_builtins
;
6140 disabled_builtins
= new_disabled_builtin
;
6145 /* Return true if the built-in function NAME has been disabled, false
6149 builtin_function_disabled_p (const char *name
)
6151 disabled_builtin
*p
;
6152 for (p
= disabled_builtins
; p
!= NULL
; p
= p
->next
)
6154 if (strcmp (name
, p
->name
) == 0)
6161 /* Worker for DEF_BUILTIN.
6162 Possibly define a builtin function with one or two names.
6163 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6164 nonansi_p and flag_no_nonansi_builtin. */
6167 def_builtin_1 (enum built_in_function fncode
,
6169 enum built_in_class fnclass
,
6170 tree fntype
, tree libtype
,
6171 bool both_p
, bool fallback_p
, bool nonansi_p
,
6172 tree fnattrs
, bool implicit_p
)
6175 const char *libname
;
6177 if (fntype
== error_mark_node
)
6180 gcc_assert ((!both_p
&& !fallback_p
)
6181 || !strncmp (name
, "__builtin_",
6182 strlen ("__builtin_")));
6184 libname
= name
+ strlen ("__builtin_");
6185 decl
= add_builtin_function (name
, fntype
, fncode
, fnclass
,
6186 (fallback_p
? libname
: NULL
),
6189 set_builtin_decl (fncode
, decl
, implicit_p
);
6192 && !flag_no_builtin
&& !builtin_function_disabled_p (libname
)
6193 && !(nonansi_p
&& flag_no_nonansi_builtin
))
6194 add_builtin_function (libname
, libtype
, fncode
, fnclass
,
6198 /* Nonzero if the type T promotes to int. This is (nearly) the
6199 integral promotions defined in ISO C99 6.3.1.1/2. */
6202 c_promoting_integer_type_p (const_tree t
)
6204 switch (TREE_CODE (t
))
6207 return (TYPE_MAIN_VARIANT (t
) == char_type_node
6208 || TYPE_MAIN_VARIANT (t
) == signed_char_type_node
6209 || TYPE_MAIN_VARIANT (t
) == unsigned_char_type_node
6210 || TYPE_MAIN_VARIANT (t
) == short_integer_type_node
6211 || TYPE_MAIN_VARIANT (t
) == short_unsigned_type_node
6212 || TYPE_PRECISION (t
) < TYPE_PRECISION (integer_type_node
));
6215 /* ??? Technically all enumerations not larger than an int
6216 promote to an int. But this is used along code paths
6217 that only want to notice a size change. */
6218 return TYPE_PRECISION (t
) < TYPE_PRECISION (integer_type_node
);
6228 /* Return 1 if PARMS specifies a fixed number of parameters
6229 and none of their types is affected by default promotions. */
6232 self_promoting_args_p (const_tree parms
)
6235 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
6237 tree type
= TREE_VALUE (t
);
6239 if (type
== error_mark_node
)
6242 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
6248 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
6251 if (c_promoting_integer_type_p (type
))
6257 /* Recursively remove any '*' or '&' operator from TYPE. */
6259 strip_pointer_operator (tree t
)
6261 while (POINTER_TYPE_P (t
))
6266 /* Recursively remove pointer or array type from TYPE. */
6268 strip_pointer_or_array_types (tree t
)
6270 while (TREE_CODE (t
) == ARRAY_TYPE
|| POINTER_TYPE_P (t
))
6275 /* Used to compare case labels. K1 and K2 are actually tree nodes
6276 representing case labels, or NULL_TREE for a `default' label.
6277 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6278 K2, and 0 if K1 and K2 are equal. */
6281 case_compare (splay_tree_key k1
, splay_tree_key k2
)
6283 /* Consider a NULL key (such as arises with a `default' label) to be
6284 smaller than anything else. */
6290 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
6293 /* Process a case label, located at LOC, for the range LOW_VALUE
6294 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6295 then this case label is actually a `default' label. If only
6296 HIGH_VALUE is NULL_TREE, then case label was declared using the
6297 usual C/C++ syntax, rather than the GNU case range extension.
6298 CASES is a tree containing all the case ranges processed so far;
6299 COND is the condition for the switch-statement itself. Returns the
6300 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6304 c_add_case_label (location_t loc
, splay_tree cases
, tree cond
, tree orig_type
,
6305 tree low_value
, tree high_value
)
6310 splay_tree_node node
;
6312 /* Create the LABEL_DECL itself. */
6313 label
= create_artificial_label (loc
);
6315 /* If there was an error processing the switch condition, bail now
6316 before we get more confused. */
6317 if (!cond
|| cond
== error_mark_node
)
6320 if ((low_value
&& TREE_TYPE (low_value
)
6321 && POINTER_TYPE_P (TREE_TYPE (low_value
)))
6322 || (high_value
&& TREE_TYPE (high_value
)
6323 && POINTER_TYPE_P (TREE_TYPE (high_value
))))
6325 error_at (loc
, "pointers are not permitted as case values");
6329 /* Case ranges are a GNU extension. */
6331 pedwarn (loc
, OPT_Wpedantic
,
6332 "range expressions in switch statements are non-standard");
6334 type
= TREE_TYPE (cond
);
6337 low_value
= check_case_value (loc
, low_value
);
6338 low_value
= convert_and_check (loc
, type
, low_value
);
6339 if (low_value
== error_mark_node
)
6344 high_value
= check_case_value (loc
, high_value
);
6345 high_value
= convert_and_check (loc
, type
, high_value
);
6346 if (high_value
== error_mark_node
)
6350 if (low_value
&& high_value
)
6352 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6353 really a case range, even though it was written that way.
6354 Remove the HIGH_VALUE to simplify later processing. */
6355 if (tree_int_cst_equal (low_value
, high_value
))
6356 high_value
= NULL_TREE
;
6357 else if (!tree_int_cst_lt (low_value
, high_value
))
6358 warning_at (loc
, 0, "empty range specified");
6361 /* See if the case is in range of the type of the original testing
6362 expression. If both low_value and high_value are out of range,
6363 don't insert the case label and return NULL_TREE. */
6365 && !check_case_bounds (loc
, type
, orig_type
,
6366 &low_value
, high_value
? &high_value
: NULL
))
6369 /* Look up the LOW_VALUE in the table of case labels we already
6371 node
= splay_tree_lookup (cases
, (splay_tree_key
) low_value
);
6372 /* If there was not an exact match, check for overlapping ranges.
6373 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6374 that's a `default' label and the only overlap is an exact match. */
6375 if (!node
&& (low_value
|| high_value
))
6377 splay_tree_node low_bound
;
6378 splay_tree_node high_bound
;
6380 /* Even though there wasn't an exact match, there might be an
6381 overlap between this case range and another case range.
6382 Since we've (inductively) not allowed any overlapping case
6383 ranges, we simply need to find the greatest low case label
6384 that is smaller that LOW_VALUE, and the smallest low case
6385 label that is greater than LOW_VALUE. If there is an overlap
6386 it will occur in one of these two ranges. */
6387 low_bound
= splay_tree_predecessor (cases
,
6388 (splay_tree_key
) low_value
);
6389 high_bound
= splay_tree_successor (cases
,
6390 (splay_tree_key
) low_value
);
6392 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6393 the LOW_VALUE, so there is no need to check unless the
6394 LOW_BOUND is in fact itself a case range. */
6396 && CASE_HIGH ((tree
) low_bound
->value
)
6397 && tree_int_cst_compare (CASE_HIGH ((tree
) low_bound
->value
),
6400 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6401 range is bigger than the low end of the current range, so we
6402 are only interested if the current range is a real range, and
6403 not an ordinary case label. */
6406 && (tree_int_cst_compare ((tree
) high_bound
->key
,
6411 /* If there was an overlap, issue an error. */
6414 tree duplicate
= CASE_LABEL ((tree
) node
->value
);
6418 error_at (loc
, "duplicate (or overlapping) case value");
6419 error_at (DECL_SOURCE_LOCATION (duplicate
),
6420 "this is the first entry overlapping that value");
6424 error_at (loc
, "duplicate case value") ;
6425 error_at (DECL_SOURCE_LOCATION (duplicate
), "previously used here");
6429 error_at (loc
, "multiple default labels in one switch");
6430 error_at (DECL_SOURCE_LOCATION (duplicate
),
6431 "this is the first default label");
6436 /* Add a CASE_LABEL to the statement-tree. */
6437 case_label
= add_stmt (build_case_label (low_value
, high_value
, label
));
6438 /* Register this case label in the splay tree. */
6439 splay_tree_insert (cases
,
6440 (splay_tree_key
) low_value
,
6441 (splay_tree_value
) case_label
);
6446 /* Add a label so that the back-end doesn't think that the beginning of
6447 the switch is unreachable. Note that we do not add a case label, as
6448 that just leads to duplicates and thence to failure later on. */
6451 tree t
= create_artificial_label (loc
);
6452 add_stmt (build_stmt (loc
, LABEL_EXPR
, t
));
6454 return error_mark_node
;
6457 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6458 Used to verify that case values match up with enumerator values. */
6461 match_case_to_enum_1 (tree key
, tree type
, tree label
)
6463 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
];
6465 if (tree_fits_uhwi_p (key
))
6466 print_dec (key
, buf
, UNSIGNED
);
6467 else if (tree_fits_shwi_p (key
))
6468 print_dec (key
, buf
, SIGNED
);
6470 print_hex (key
, buf
);
6472 if (TYPE_NAME (type
) == 0)
6473 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
6474 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
6475 "case value %qs not in enumerated type",
6478 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label
)),
6479 warn_switch
? OPT_Wswitch
: OPT_Wswitch_enum
,
6480 "case value %qs not in enumerated type %qT",
6484 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6485 Used to verify that case values match up with enumerator values. */
6488 match_case_to_enum (splay_tree_node node
, void *data
)
6490 tree label
= (tree
) node
->value
;
6491 tree type
= (tree
) data
;
6493 /* Skip default case. */
6494 if (!CASE_LOW (label
))
6497 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6498 when we did our enum->case scan. Reset our scratch bit after. */
6499 if (!CASE_LOW_SEEN (label
))
6500 match_case_to_enum_1 (CASE_LOW (label
), type
, label
);
6502 CASE_LOW_SEEN (label
) = 0;
6504 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6505 not set, that means that CASE_HIGH did not appear when we did our
6506 enum->case scan. Reset our scratch bit after. */
6507 if (CASE_HIGH (label
))
6509 if (!CASE_HIGH_SEEN (label
))
6510 match_case_to_enum_1 (CASE_HIGH (label
), type
, label
);
6512 CASE_HIGH_SEEN (label
) = 0;
6518 /* Handle -Wswitch*. Called from the front end after parsing the
6519 switch construct. */
6520 /* ??? Should probably be somewhere generic, since other languages
6521 besides C and C++ would want this. At the moment, however, C/C++
6522 are the only tree-ssa languages that support enumerations at all,
6523 so the point is moot. */
6526 c_do_switch_warnings (splay_tree cases
, location_t switch_location
,
6527 tree type
, tree cond
)
6529 splay_tree_node default_node
;
6530 splay_tree_node node
;
6533 if (!warn_switch
&& !warn_switch_enum
&& !warn_switch_default
)
6536 default_node
= splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
6538 warning_at (switch_location
, OPT_Wswitch_default
,
6539 "switch missing default case");
6541 /* From here on, we only care about about enumerated types. */
6542 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
6545 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6546 if (!warn_switch_enum
&& !warn_switch
)
6549 /* Check the cases. Warn about case values which are not members of
6550 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6551 there is no default case, check that exactly all enumeration
6552 literals are covered by the cases. */
6554 /* Clearing COND if it is not an integer constant simplifies
6555 the tests inside the loop below. */
6556 if (TREE_CODE (cond
) != INTEGER_CST
)
6559 /* The time complexity here is O(N*lg(N)) worst case, but for the
6560 common case of monotonically increasing enumerators, it is
6561 O(N), since the nature of the splay tree will keep the next
6562 element adjacent to the root at all times. */
6564 for (chain
= TYPE_VALUES (type
); chain
; chain
= TREE_CHAIN (chain
))
6566 tree value
= TREE_VALUE (chain
);
6567 if (TREE_CODE (value
) == CONST_DECL
)
6568 value
= DECL_INITIAL (value
);
6569 node
= splay_tree_lookup (cases
, (splay_tree_key
) value
);
6572 /* Mark the CASE_LOW part of the case entry as seen. */
6573 tree label
= (tree
) node
->value
;
6574 CASE_LOW_SEEN (label
) = 1;
6578 /* Even though there wasn't an exact match, there might be a
6579 case range which includes the enumerator's value. */
6580 node
= splay_tree_predecessor (cases
, (splay_tree_key
) value
);
6581 if (node
&& CASE_HIGH ((tree
) node
->value
))
6583 tree label
= (tree
) node
->value
;
6584 int cmp
= tree_int_cst_compare (CASE_HIGH (label
), value
);
6587 /* If we match the upper bound exactly, mark the CASE_HIGH
6588 part of the case entry as seen. */
6590 CASE_HIGH_SEEN (label
) = 1;
6595 /* We've now determined that this enumerated literal isn't
6596 handled by the case labels of the switch statement. */
6598 /* If the switch expression is a constant, we only really care
6599 about whether that constant is handled by the switch. */
6600 if (cond
&& tree_int_cst_compare (cond
, value
))
6603 /* If there is a default_node, the only relevant option is
6604 Wswitch-enum. Otherwise, if both are enabled then we prefer
6605 to warn using -Wswitch because -Wswitch is enabled by -Wall
6606 while -Wswitch-enum is explicit. */
6607 warning_at (switch_location
,
6608 (default_node
|| !warn_switch
6611 "enumeration value %qE not handled in switch",
6612 TREE_PURPOSE (chain
));
6615 /* Warn if there are case expressions that don't correspond to
6616 enumerators. This can occur since C and C++ don't enforce
6617 type-checking of assignments to enumeration variables.
6619 The time complexity here is now always O(N) worst case, since
6620 we should have marked both the lower bound and upper bound of
6621 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6622 above. This scan also resets those fields. */
6624 splay_tree_foreach (cases
, match_case_to_enum
, type
);
6627 /* Finish an expression taking the address of LABEL (an
6628 IDENTIFIER_NODE). Returns an expression for the address.
6630 LOC is the location for the expression returned. */
6633 finish_label_address_expr (tree label
, location_t loc
)
6637 pedwarn (input_location
, OPT_Wpedantic
, "taking the address of a label is non-standard");
6639 if (label
== error_mark_node
)
6640 return error_mark_node
;
6642 label
= lookup_label (label
);
6643 if (label
== NULL_TREE
)
6644 result
= null_pointer_node
;
6647 TREE_USED (label
) = 1;
6648 result
= build1 (ADDR_EXPR
, ptr_type_node
, label
);
6649 /* The current function is not necessarily uninlinable.
6650 Computed gotos are incompatible with inlining, but the value
6651 here could be used only in a diagnostic, for example. */
6652 protected_set_expr_location (result
, loc
);
6659 /* Given a boolean expression ARG, return a tree representing an increment
6660 or decrement (as indicated by CODE) of ARG. The front end must check for
6661 invalid cases (e.g., decrement in C++). */
6663 boolean_increment (enum tree_code code
, tree arg
)
6666 tree true_res
= build_int_cst (TREE_TYPE (arg
), 1);
6668 arg
= stabilize_reference (arg
);
6671 case PREINCREMENT_EXPR
:
6672 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
, true_res
);
6674 case POSTINCREMENT_EXPR
:
6675 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
, true_res
);
6676 arg
= save_expr (arg
);
6677 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
6678 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
6680 case PREDECREMENT_EXPR
:
6681 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
6682 invert_truthvalue_loc (input_location
, arg
));
6684 case POSTDECREMENT_EXPR
:
6685 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
6686 invert_truthvalue_loc (input_location
, arg
));
6687 arg
= save_expr (arg
);
6688 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
6689 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
6694 TREE_SIDE_EFFECTS (val
) = 1;
6698 /* Built-in macros for stddef.h and stdint.h, that require macros
6699 defined in this file. */
6701 c_stddef_cpp_builtins(void)
6703 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE
, 0);
6704 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
, 0);
6705 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE
, 0);
6706 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE
, 0);
6707 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE
, 0);
6708 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE
, 0);
6709 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE
, 0);
6710 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE
, 0);
6711 if (SIG_ATOMIC_TYPE
)
6712 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE
, 0);
6714 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE
, 0);
6716 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE
, 0);
6718 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE
, 0);
6720 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE
, 0);
6722 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE
, 0);
6724 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE
, 0);
6726 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE
, 0);
6728 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE
, 0);
6729 if (INT_LEAST8_TYPE
)
6730 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE
, 0);
6731 if (INT_LEAST16_TYPE
)
6732 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE
, 0);
6733 if (INT_LEAST32_TYPE
)
6734 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE
, 0);
6735 if (INT_LEAST64_TYPE
)
6736 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE
, 0);
6737 if (UINT_LEAST8_TYPE
)
6738 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE
, 0);
6739 if (UINT_LEAST16_TYPE
)
6740 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE
, 0);
6741 if (UINT_LEAST32_TYPE
)
6742 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE
, 0);
6743 if (UINT_LEAST64_TYPE
)
6744 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE
, 0);
6746 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE
, 0);
6747 if (INT_FAST16_TYPE
)
6748 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE
, 0);
6749 if (INT_FAST32_TYPE
)
6750 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE
, 0);
6751 if (INT_FAST64_TYPE
)
6752 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE
, 0);
6753 if (UINT_FAST8_TYPE
)
6754 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE
, 0);
6755 if (UINT_FAST16_TYPE
)
6756 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE
, 0);
6757 if (UINT_FAST32_TYPE
)
6758 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE
, 0);
6759 if (UINT_FAST64_TYPE
)
6760 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE
, 0);
6762 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE
, 0);
6764 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE
, 0);
6768 c_init_attributes (void)
6770 /* Fill in the built_in_attributes array. */
6771 #define DEF_ATTR_NULL_TREE(ENUM) \
6772 built_in_attributes[(int) ENUM] = NULL_TREE;
6773 #define DEF_ATTR_INT(ENUM, VALUE) \
6774 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6775 #define DEF_ATTR_STRING(ENUM, VALUE) \
6776 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6777 #define DEF_ATTR_IDENT(ENUM, STRING) \
6778 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6779 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6780 built_in_attributes[(int) ENUM] \
6781 = tree_cons (built_in_attributes[(int) PURPOSE], \
6782 built_in_attributes[(int) VALUE], \
6783 built_in_attributes[(int) CHAIN]);
6784 #include "builtin-attrs.def"
6785 #undef DEF_ATTR_NULL_TREE
6787 #undef DEF_ATTR_IDENT
6788 #undef DEF_ATTR_TREE_LIST
6791 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6792 identifier as an argument, so the front end shouldn't look it up. */
6795 attribute_takes_identifier_p (const_tree attr_id
)
6797 const struct attribute_spec
*spec
= lookup_attribute_spec (attr_id
);
6799 /* Unknown attribute that we'll end up ignoring, return true so we
6800 don't complain about an identifier argument. */
6802 else if (!strcmp ("mode", spec
->name
)
6803 || !strcmp ("format", spec
->name
)
6804 || !strcmp ("cleanup", spec
->name
))
6807 return targetm
.attribute_takes_identifier_p (attr_id
);
6810 /* Attribute handlers common to C front ends. */
6812 /* Handle a "packed" attribute; arguments as in
6813 struct attribute_spec.handler. */
6816 handle_packed_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
6817 int flags
, bool *no_add_attrs
)
6821 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
6822 *node
= build_variant_type_copy (*node
);
6823 TYPE_PACKED (*node
) = 1;
6825 else if (TREE_CODE (*node
) == FIELD_DECL
)
6827 if (TYPE_ALIGN (TREE_TYPE (*node
)) <= BITS_PER_UNIT
6828 /* Still pack bitfields. */
6829 && ! DECL_INITIAL (*node
))
6830 warning (OPT_Wattributes
,
6831 "%qE attribute ignored for field of type %qT",
6832 name
, TREE_TYPE (*node
));
6834 DECL_PACKED (*node
) = 1;
6836 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6837 used for DECL_REGISTER. It wouldn't mean anything anyway.
6838 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6839 that changes what the typedef is typing. */
6842 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
6843 *no_add_attrs
= true;
6849 /* Handle a "nocommon" attribute; arguments as in
6850 struct attribute_spec.handler. */
6853 handle_nocommon_attribute (tree
*node
, tree name
,
6854 tree
ARG_UNUSED (args
),
6855 int ARG_UNUSED (flags
), bool *no_add_attrs
)
6857 if (TREE_CODE (*node
) == VAR_DECL
)
6858 DECL_COMMON (*node
) = 0;
6861 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
6862 *no_add_attrs
= true;
6868 /* Handle a "common" attribute; arguments as in
6869 struct attribute_spec.handler. */
6872 handle_common_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
6873 int ARG_UNUSED (flags
), bool *no_add_attrs
)
6875 if (TREE_CODE (*node
) == VAR_DECL
)
6876 DECL_COMMON (*node
) = 1;
6879 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
6880 *no_add_attrs
= true;
6886 /* Handle a "noreturn" attribute; arguments as in
6887 struct attribute_spec.handler. */
6890 handle_noreturn_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
6891 int ARG_UNUSED (flags
), bool *no_add_attrs
)
6893 tree type
= TREE_TYPE (*node
);
6895 /* See FIXME comment in c_common_attribute_table. */
6896 if (TREE_CODE (*node
) == FUNCTION_DECL
6897 || objc_method_decl (TREE_CODE (*node
)))
6898 TREE_THIS_VOLATILE (*node
) = 1;
6899 else if (TREE_CODE (type
) == POINTER_TYPE
6900 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
6902 = (build_qualified_type
6904 (build_type_variant (TREE_TYPE (type
),
6905 TYPE_READONLY (TREE_TYPE (type
)), 1)),
6906 TYPE_QUALS (type
)));
6909 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
6910 *no_add_attrs
= true;
6916 /* Handle a "hot" and attribute; arguments as in
6917 struct attribute_spec.handler. */
6920 handle_hot_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
6921 int ARG_UNUSED (flags
), bool *no_add_attrs
)
6923 if (TREE_CODE (*node
) == FUNCTION_DECL
6924 || TREE_CODE (*node
) == LABEL_DECL
)
6926 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node
)) != NULL
)
6928 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
6929 "with attribute %qs", name
, "cold");
6930 *no_add_attrs
= true;
6932 /* Most of the rest of the hot processing is done later with
6933 lookup_attribute. */
6937 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
6938 *no_add_attrs
= true;
6944 /* Handle a "cold" and attribute; arguments as in
6945 struct attribute_spec.handler. */
6948 handle_cold_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
6949 int ARG_UNUSED (flags
), bool *no_add_attrs
)
6951 if (TREE_CODE (*node
) == FUNCTION_DECL
6952 || TREE_CODE (*node
) == LABEL_DECL
)
6954 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node
)) != NULL
)
6956 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
6957 "with attribute %qs", name
, "hot");
6958 *no_add_attrs
= true;
6960 /* Most of the rest of the cold processing is done later with
6961 lookup_attribute. */
6965 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
6966 *no_add_attrs
= true;
6972 /* Handle a "no_sanitize_address" attribute; arguments as in
6973 struct attribute_spec.handler. */
6976 handle_no_sanitize_address_attribute (tree
*node
, tree name
, tree
, int,
6979 if (TREE_CODE (*node
) != FUNCTION_DECL
)
6981 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
6982 *no_add_attrs
= true;
6988 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6989 struct attribute_spec.handler. */
6992 handle_no_address_safety_analysis_attribute (tree
*node
, tree name
, tree
, int,
6995 if (TREE_CODE (*node
) != FUNCTION_DECL
)
6996 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
6997 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node
)))
6998 DECL_ATTRIBUTES (*node
)
6999 = tree_cons (get_identifier ("no_sanitize_address"),
7000 NULL_TREE
, DECL_ATTRIBUTES (*node
));
7001 *no_add_attrs
= true;
7005 /* Handle a "no_sanitize_undefined" attribute; arguments as in
7006 struct attribute_spec.handler. */
7009 handle_no_sanitize_undefined_attribute (tree
*node
, tree name
, tree
, int,
7012 if (TREE_CODE (*node
) != FUNCTION_DECL
)
7014 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7015 *no_add_attrs
= true;
7021 /* Handle a "stack_protect" attribute; arguments as in
7022 struct attribute_spec.handler. */
7024 handle_stack_protect_attribute (tree
*node
, tree name
, tree
, int,
7027 if (TREE_CODE (*node
) != FUNCTION_DECL
)
7029 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7030 *no_add_attrs
= true;
7033 DECL_ATTRIBUTES (*node
)
7034 = tree_cons (get_identifier ("stack_protect"),
7035 NULL_TREE
, DECL_ATTRIBUTES (*node
));
7040 /* Handle a "noinline" attribute; arguments as in
7041 struct attribute_spec.handler. */
7044 handle_noinline_attribute (tree
*node
, tree name
,
7045 tree
ARG_UNUSED (args
),
7046 int ARG_UNUSED (flags
), bool *no_add_attrs
)
7048 if (TREE_CODE (*node
) == FUNCTION_DECL
)
7050 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node
)))
7052 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
7053 "with attribute %qs", name
, "always_inline");
7054 *no_add_attrs
= true;
7057 DECL_UNINLINABLE (*node
) = 1;
7061 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7062 *no_add_attrs
= true;
7068 /* Handle a "noclone" attribute; arguments as in
7069 struct attribute_spec.handler. */
7072 handle_noclone_attribute (tree
*node
, tree name
,
7073 tree
ARG_UNUSED (args
),
7074 int ARG_UNUSED (flags
), bool *no_add_attrs
)
7076 if (TREE_CODE (*node
) != FUNCTION_DECL
)
7078 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7079 *no_add_attrs
= true;
7085 /* Handle a "no_icf" attribute; arguments as in
7086 struct attribute_spec.handler. */
7089 handle_noicf_attribute (tree
*node
, tree name
,
7090 tree
ARG_UNUSED (args
),
7091 int ARG_UNUSED (flags
), bool *no_add_attrs
)
7093 if (TREE_CODE (*node
) != FUNCTION_DECL
)
7095 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7096 *no_add_attrs
= true;
7103 /* Handle a "always_inline" attribute; arguments as in
7104 struct attribute_spec.handler. */
7107 handle_always_inline_attribute (tree
*node
, tree name
,
7108 tree
ARG_UNUSED (args
),
7109 int ARG_UNUSED (flags
),
7112 if (TREE_CODE (*node
) == FUNCTION_DECL
)
7114 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node
)))
7116 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
7117 "with %qs attribute", name
, "noinline");
7118 *no_add_attrs
= true;
7121 /* Set the attribute and mark it for disregarding inline
7123 DECL_DISREGARD_INLINE_LIMITS (*node
) = 1;
7127 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7128 *no_add_attrs
= true;
7134 /* Handle a "gnu_inline" attribute; arguments as in
7135 struct attribute_spec.handler. */
7138 handle_gnu_inline_attribute (tree
*node
, tree name
,
7139 tree
ARG_UNUSED (args
),
7140 int ARG_UNUSED (flags
),
7143 if (TREE_CODE (*node
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (*node
))
7145 /* Do nothing else, just set the attribute. We'll get at
7146 it later with lookup_attribute. */
7150 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7151 *no_add_attrs
= true;
7157 /* Handle a "leaf" attribute; arguments as in
7158 struct attribute_spec.handler. */
7161 handle_leaf_attribute (tree
*node
, tree name
,
7162 tree
ARG_UNUSED (args
),
7163 int ARG_UNUSED (flags
), bool *no_add_attrs
)
7165 if (TREE_CODE (*node
) != FUNCTION_DECL
)
7167 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7168 *no_add_attrs
= true;
7170 if (!TREE_PUBLIC (*node
))
7172 warning (OPT_Wattributes
, "%qE attribute has no effect on unit local functions", name
);
7173 *no_add_attrs
= true;
7179 /* Handle an "artificial" attribute; arguments as in
7180 struct attribute_spec.handler. */
7183 handle_artificial_attribute (tree
*node
, tree name
,
7184 tree
ARG_UNUSED (args
),
7185 int ARG_UNUSED (flags
),
7188 if (TREE_CODE (*node
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (*node
))
7190 /* Do nothing else, just set the attribute. We'll get at
7191 it later with lookup_attribute. */
7195 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7196 *no_add_attrs
= true;
7202 /* Handle a "flatten" attribute; arguments as in
7203 struct attribute_spec.handler. */
7206 handle_flatten_attribute (tree
*node
, tree name
,
7207 tree args ATTRIBUTE_UNUSED
,
7208 int flags ATTRIBUTE_UNUSED
, bool *no_add_attrs
)
7210 if (TREE_CODE (*node
) == FUNCTION_DECL
)
7211 /* Do nothing else, just set the attribute. We'll get at
7212 it later with lookup_attribute. */
7216 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7217 *no_add_attrs
= true;
7223 /* Handle a "warning" or "error" attribute; arguments as in
7224 struct attribute_spec.handler. */
7227 handle_error_attribute (tree
*node
, tree name
, tree args
,
7228 int ARG_UNUSED (flags
), bool *no_add_attrs
)
7230 if (TREE_CODE (*node
) == FUNCTION_DECL
7231 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
7232 /* Do nothing else, just set the attribute. We'll get at
7233 it later with lookup_attribute. */
7237 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7238 *no_add_attrs
= true;
7244 /* Handle a "used" attribute; arguments as in
7245 struct attribute_spec.handler. */
7248 handle_used_attribute (tree
*pnode
, tree name
, tree
ARG_UNUSED (args
),
7249 int ARG_UNUSED (flags
), bool *no_add_attrs
)
7253 if (TREE_CODE (node
) == FUNCTION_DECL
7254 || (TREE_CODE (node
) == VAR_DECL
&& TREE_STATIC (node
))
7255 || (TREE_CODE (node
) == TYPE_DECL
))
7257 TREE_USED (node
) = 1;
7258 DECL_PRESERVE_P (node
) = 1;
7259 if (TREE_CODE (node
) == VAR_DECL
)
7260 DECL_READ_P (node
) = 1;
7264 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7265 *no_add_attrs
= true;
7271 /* Handle a "unused" attribute; arguments as in
7272 struct attribute_spec.handler. */
7275 handle_unused_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
7276 int flags
, bool *no_add_attrs
)
7282 if (TREE_CODE (decl
) == PARM_DECL
7283 || TREE_CODE (decl
) == VAR_DECL
7284 || TREE_CODE (decl
) == FUNCTION_DECL
7285 || TREE_CODE (decl
) == LABEL_DECL
7286 || TREE_CODE (decl
) == TYPE_DECL
)
7288 TREE_USED (decl
) = 1;
7289 if (TREE_CODE (decl
) == VAR_DECL
7290 || TREE_CODE (decl
) == PARM_DECL
)
7291 DECL_READ_P (decl
) = 1;
7295 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7296 *no_add_attrs
= true;
7301 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
7302 *node
= build_variant_type_copy (*node
);
7303 TREE_USED (*node
) = 1;
7309 /* Handle a "externally_visible" attribute; arguments as in
7310 struct attribute_spec.handler. */
7313 handle_externally_visible_attribute (tree
*pnode
, tree name
,
7314 tree
ARG_UNUSED (args
),
7315 int ARG_UNUSED (flags
),
7320 if (TREE_CODE (node
) == FUNCTION_DECL
|| TREE_CODE (node
) == VAR_DECL
)
7322 if ((!TREE_STATIC (node
) && TREE_CODE (node
) != FUNCTION_DECL
7323 && !DECL_EXTERNAL (node
)) || !TREE_PUBLIC (node
))
7325 warning (OPT_Wattributes
,
7326 "%qE attribute have effect only on public objects", name
);
7327 *no_add_attrs
= true;
7332 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7333 *no_add_attrs
= true;
7339 /* Handle the "no_reorder" attribute. Arguments as in
7340 struct attribute_spec.handler. */
7343 handle_no_reorder_attribute (tree
*pnode
,
7351 if ((TREE_CODE (node
) != FUNCTION_DECL
&& TREE_CODE (node
) != VAR_DECL
)
7352 && !(TREE_STATIC (node
) || DECL_EXTERNAL (node
)))
7354 warning (OPT_Wattributes
,
7355 "%qE attribute only affects top level objects",
7357 *no_add_attrs
= true;
7363 /* Handle a "const" attribute; arguments as in
7364 struct attribute_spec.handler. */
7367 handle_const_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
7368 int ARG_UNUSED (flags
), bool *no_add_attrs
)
7370 tree type
= TREE_TYPE (*node
);
7372 /* See FIXME comment on noreturn in c_common_attribute_table. */
7373 if (TREE_CODE (*node
) == FUNCTION_DECL
)
7374 TREE_READONLY (*node
) = 1;
7375 else if (TREE_CODE (type
) == POINTER_TYPE
7376 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
7378 = (build_qualified_type
7380 (build_type_variant (TREE_TYPE (type
), 1,
7381 TREE_THIS_VOLATILE (TREE_TYPE (type
)))),
7382 TYPE_QUALS (type
)));
7385 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7386 *no_add_attrs
= true;
7392 /* Handle a "transparent_union" attribute; arguments as in
7393 struct attribute_spec.handler. */
7396 handle_transparent_union_attribute (tree
*node
, tree name
,
7397 tree
ARG_UNUSED (args
), int flags
,
7402 *no_add_attrs
= true;
7405 if (TREE_CODE (*node
) == TYPE_DECL
7406 && ! (flags
& ATTR_FLAG_CXX11
))
7407 node
= &TREE_TYPE (*node
);
7410 if (TREE_CODE (type
) == UNION_TYPE
)
7412 /* Make sure that the first field will work for a transparent union.
7413 If the type isn't complete yet, leave the check to the code in
7415 if (TYPE_SIZE (type
))
7417 tree first
= first_field (type
);
7418 if (first
== NULL_TREE
7419 || DECL_ARTIFICIAL (first
)
7420 || TYPE_MODE (type
) != DECL_MODE (first
))
7424 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
7426 /* If the type isn't complete yet, setting the flag
7427 on a variant wouldn't ever be checked. */
7428 if (!TYPE_SIZE (type
))
7431 /* build_duplicate_type doesn't work for C++. */
7432 if (c_dialect_cxx ())
7435 /* A type variant isn't good enough, since we don't a cast
7436 to such a type removed as a no-op. */
7437 *node
= type
= build_duplicate_type (type
);
7440 TYPE_TRANSPARENT_AGGR (type
) = 1;
7445 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7449 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7450 get the requested priority for a constructor or destructor,
7451 possibly issuing diagnostics for invalid or reserved
7454 static priority_type
7455 get_priority (tree args
, bool is_destructor
)
7461 return DEFAULT_INIT_PRIORITY
;
7463 if (!SUPPORTS_INIT_PRIORITY
)
7466 error ("destructor priorities are not supported");
7468 error ("constructor priorities are not supported");
7469 return DEFAULT_INIT_PRIORITY
;
7472 arg
= TREE_VALUE (args
);
7473 if (TREE_CODE (arg
) == IDENTIFIER_NODE
)
7475 if (arg
== error_mark_node
)
7476 return DEFAULT_INIT_PRIORITY
;
7477 arg
= default_conversion (arg
);
7478 if (!tree_fits_shwi_p (arg
)
7479 || !INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
7482 pri
= tree_to_shwi (arg
);
7483 if (pri
< 0 || pri
> MAX_INIT_PRIORITY
)
7486 if (pri
<= MAX_RESERVED_INIT_PRIORITY
)
7490 "destructor priorities from 0 to %d are reserved "
7491 "for the implementation",
7492 MAX_RESERVED_INIT_PRIORITY
);
7495 "constructor priorities from 0 to %d are reserved "
7496 "for the implementation",
7497 MAX_RESERVED_INIT_PRIORITY
);
7503 error ("destructor priorities must be integers from 0 to %d inclusive",
7506 error ("constructor priorities must be integers from 0 to %d inclusive",
7508 return DEFAULT_INIT_PRIORITY
;
7511 /* Handle a "constructor" attribute; arguments as in
7512 struct attribute_spec.handler. */
7515 handle_constructor_attribute (tree
*node
, tree name
, tree args
,
7516 int ARG_UNUSED (flags
),
7520 tree type
= TREE_TYPE (decl
);
7522 if (TREE_CODE (decl
) == FUNCTION_DECL
7523 && TREE_CODE (type
) == FUNCTION_TYPE
7524 && decl_function_context (decl
) == 0)
7526 priority_type priority
;
7527 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
7528 priority
= get_priority (args
, /*is_destructor=*/false);
7529 SET_DECL_INIT_PRIORITY (decl
, priority
);
7530 TREE_USED (decl
) = 1;
7534 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7535 *no_add_attrs
= true;
7541 /* Handle a "destructor" attribute; arguments as in
7542 struct attribute_spec.handler. */
7545 handle_destructor_attribute (tree
*node
, tree name
, tree args
,
7546 int ARG_UNUSED (flags
),
7550 tree type
= TREE_TYPE (decl
);
7552 if (TREE_CODE (decl
) == FUNCTION_DECL
7553 && TREE_CODE (type
) == FUNCTION_TYPE
7554 && decl_function_context (decl
) == 0)
7556 priority_type priority
;
7557 DECL_STATIC_DESTRUCTOR (decl
) = 1;
7558 priority
= get_priority (args
, /*is_destructor=*/true);
7559 SET_DECL_FINI_PRIORITY (decl
, priority
);
7560 TREE_USED (decl
) = 1;
7564 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7565 *no_add_attrs
= true;
7571 /* Nonzero if the mode is a valid vector mode for this architecture.
7572 This returns nonzero even if there is no hardware support for the
7573 vector mode, but we can emulate with narrower modes. */
7576 vector_mode_valid_p (machine_mode mode
)
7578 enum mode_class mclass
= GET_MODE_CLASS (mode
);
7579 machine_mode innermode
;
7581 /* Doh! What's going on? */
7582 if (mclass
!= MODE_VECTOR_INT
7583 && mclass
!= MODE_VECTOR_FLOAT
7584 && mclass
!= MODE_VECTOR_FRACT
7585 && mclass
!= MODE_VECTOR_UFRACT
7586 && mclass
!= MODE_VECTOR_ACCUM
7587 && mclass
!= MODE_VECTOR_UACCUM
)
7590 /* Hardware support. Woo hoo! */
7591 if (targetm
.vector_mode_supported_p (mode
))
7594 innermode
= GET_MODE_INNER (mode
);
7596 /* We should probably return 1 if requesting V4DI and we have no DI,
7597 but we have V2DI, but this is probably very unlikely. */
7599 /* If we have support for the inner mode, we can safely emulate it.
7600 We may not have V2DI, but me can emulate with a pair of DIs. */
7601 return targetm
.scalar_mode_supported_p (innermode
);
7605 /* Handle a "mode" attribute; arguments as in
7606 struct attribute_spec.handler. */
7609 handle_mode_attribute (tree
*node
, tree name
, tree args
,
7610 int ARG_UNUSED (flags
), bool *no_add_attrs
)
7613 tree ident
= TREE_VALUE (args
);
7615 *no_add_attrs
= true;
7617 if (TREE_CODE (ident
) != IDENTIFIER_NODE
)
7618 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
7622 const char *p
= IDENTIFIER_POINTER (ident
);
7623 int len
= strlen (p
);
7624 machine_mode mode
= VOIDmode
;
7628 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
7629 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
7631 char *newp
= (char *) alloca (len
- 1);
7633 strcpy (newp
, &p
[2]);
7634 newp
[len
- 4] = '\0';
7638 /* Change this type to have a type with the specified mode.
7639 First check for the special modes. */
7640 if (!strcmp (p
, "byte"))
7642 else if (!strcmp (p
, "word"))
7644 else if (!strcmp (p
, "pointer"))
7646 else if (!strcmp (p
, "libgcc_cmp_return"))
7647 mode
= targetm
.libgcc_cmp_return_mode ();
7648 else if (!strcmp (p
, "libgcc_shift_count"))
7649 mode
= targetm
.libgcc_shift_count_mode ();
7650 else if (!strcmp (p
, "unwind_word"))
7651 mode
= targetm
.unwind_word_mode ();
7653 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
7654 if (!strcmp (p
, GET_MODE_NAME (j
)))
7656 mode
= (machine_mode
) j
;
7660 if (mode
== VOIDmode
)
7662 error ("unknown machine mode %qE", ident
);
7667 switch (GET_MODE_CLASS (mode
))
7670 case MODE_PARTIAL_INT
:
7672 case MODE_DECIMAL_FLOAT
:
7677 valid_mode
= targetm
.scalar_mode_supported_p (mode
);
7680 case MODE_COMPLEX_INT
:
7681 case MODE_COMPLEX_FLOAT
:
7682 valid_mode
= targetm
.scalar_mode_supported_p (GET_MODE_INNER (mode
));
7685 case MODE_VECTOR_INT
:
7686 case MODE_VECTOR_FLOAT
:
7687 case MODE_VECTOR_FRACT
:
7688 case MODE_VECTOR_UFRACT
:
7689 case MODE_VECTOR_ACCUM
:
7690 case MODE_VECTOR_UACCUM
:
7691 warning (OPT_Wattributes
, "specifying vector types with "
7692 "__attribute__ ((mode)) is deprecated");
7693 warning (OPT_Wattributes
,
7694 "use __attribute__ ((vector_size)) instead");
7695 valid_mode
= vector_mode_valid_p (mode
);
7703 error ("unable to emulate %qs", p
);
7707 if (POINTER_TYPE_P (type
))
7709 addr_space_t as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
7710 tree (*fn
)(tree
, machine_mode
, bool);
7712 if (!targetm
.addr_space
.valid_pointer_mode (mode
, as
))
7714 error ("invalid pointer mode %qs", p
);
7718 if (TREE_CODE (type
) == POINTER_TYPE
)
7719 fn
= build_pointer_type_for_mode
;
7721 fn
= build_reference_type_for_mode
;
7722 typefm
= fn (TREE_TYPE (type
), mode
, false);
7726 /* For fixed-point modes, we need to test if the signness of type
7727 and the machine mode are consistent. */
7728 if (ALL_FIXED_POINT_MODE_P (mode
)
7729 && TYPE_UNSIGNED (type
) != UNSIGNED_FIXED_POINT_MODE_P (mode
))
7731 error ("signedness of type and machine mode %qs don%'t match", p
);
7734 /* For fixed-point modes, we need to pass saturating info. */
7735 typefm
= lang_hooks
.types
.type_for_mode (mode
,
7736 ALL_FIXED_POINT_MODE_P (mode
) ? TYPE_SATURATING (type
)
7737 : TYPE_UNSIGNED (type
));
7740 if (typefm
== NULL_TREE
)
7742 error ("no data type for mode %qs", p
);
7745 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
7747 /* For enumeral types, copy the precision from the integer
7748 type returned above. If not an INTEGER_TYPE, we can't use
7749 this mode for this type. */
7750 if (TREE_CODE (typefm
) != INTEGER_TYPE
)
7752 error ("cannot use mode %qs for enumeral types", p
);
7756 if (flags
& ATTR_FLAG_TYPE_IN_PLACE
)
7758 TYPE_PRECISION (type
) = TYPE_PRECISION (typefm
);
7763 /* We cannot build a type variant, as there's code that assumes
7764 that TYPE_MAIN_VARIANT has the same mode. This includes the
7765 debug generators. Instead, create a subrange type. This
7766 results in all of the enumeral values being emitted only once
7767 in the original, and the subtype gets them by reference. */
7768 if (TYPE_UNSIGNED (type
))
7769 typefm
= make_unsigned_type (TYPE_PRECISION (typefm
));
7771 typefm
= make_signed_type (TYPE_PRECISION (typefm
));
7772 TREE_TYPE (typefm
) = type
;
7775 else if (VECTOR_MODE_P (mode
)
7776 ? TREE_CODE (type
) != TREE_CODE (TREE_TYPE (typefm
))
7777 : TREE_CODE (type
) != TREE_CODE (typefm
))
7779 error ("mode %qs applied to inappropriate type", p
);
7789 /* Handle a "section" attribute; arguments as in
7790 struct attribute_spec.handler. */
7793 handle_section_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
7794 int ARG_UNUSED (flags
), bool *no_add_attrs
)
7798 if (!targetm_common
.have_named_sections
)
7800 error_at (DECL_SOURCE_LOCATION (*node
),
7801 "section attributes are not supported for this target");
7805 user_defined_section_attribute
= true;
7807 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
7809 error ("section attribute not allowed for %q+D", *node
);
7813 if (TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
7815 error ("section attribute argument not a string constant");
7819 if (TREE_CODE (decl
) == VAR_DECL
7820 && current_function_decl
!= NULL_TREE
7821 && !TREE_STATIC (decl
))
7823 error_at (DECL_SOURCE_LOCATION (decl
),
7824 "section attribute cannot be specified for local variables");
7828 /* The decl may have already been given a section attribute
7829 from a previous declaration. Ensure they match. */
7830 if (DECL_SECTION_NAME (decl
) != NULL
7831 && strcmp (DECL_SECTION_NAME (decl
),
7832 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
7834 error ("section of %q+D conflicts with previous declaration", *node
);
7838 if (TREE_CODE (decl
) == VAR_DECL
7839 && !targetm
.have_tls
&& targetm
.emutls
.tmpl_section
7840 && DECL_THREAD_LOCAL_P (decl
))
7842 error ("section of %q+D cannot be overridden", *node
);
7846 set_decl_section_name (decl
, TREE_STRING_POINTER (TREE_VALUE (args
)));
7850 *no_add_attrs
= true;
7854 /* Check whether ALIGN is a valid user-specified alignment. If so,
7855 return its base-2 log; if not, output an error and return -1. If
7856 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7859 check_user_alignment (const_tree align
, bool allow_zero
)
7863 if (error_operand_p (align
))
7865 if (TREE_CODE (align
) != INTEGER_CST
7866 || !INTEGRAL_TYPE_P (TREE_TYPE (align
)))
7868 error ("requested alignment is not an integer constant");
7871 else if (allow_zero
&& integer_zerop (align
))
7873 else if (tree_int_cst_sgn (align
) == -1
7874 || (i
= tree_log2 (align
)) == -1)
7876 error ("requested alignment is not a positive power of 2");
7879 else if (i
>= HOST_BITS_PER_INT
- BITS_PER_UNIT_LOG
)
7881 error ("requested alignment is too large");
7888 If in c++-11, check if the c++-11 alignment constraint with respect
7889 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7890 c++-11 mode, does nothing.
7894 [* if the constant expression evaluates to a fundamental alignment,
7895 the alignment requirement of the declared entity shall be the
7896 specified fundamental alignment.
7898 * if the constant expression evaluates to an extended alignment
7899 and the implementation supports that alignment in the context
7900 of the declaration, the alignment of the declared entity shall
7903 * if the constant expression evaluates to an extended alignment
7904 and the implementation does not support that alignment in the
7905 context of the declaration, the program is ill-formed]. */
7908 check_cxx_fundamental_alignment_constraints (tree node
,
7912 bool alignment_too_large_p
= false;
7913 unsigned requested_alignment
= 1U << align_log
;
7914 unsigned max_align
= 0;
7916 if ((!(flags
& ATTR_FLAG_CXX11
) && !warn_cxx_compat
)
7917 || (node
== NULL_TREE
|| node
== error_mark_node
))
7920 if (cxx_fundamental_alignment_p (requested_alignment
))
7925 if (TREE_STATIC (node
))
7927 /* For file scope variables and static members, the target
7928 supports alignments that are at most
7929 MAX_OFILE_ALIGNMENT. */
7930 if (requested_alignment
> (max_align
= MAX_OFILE_ALIGNMENT
))
7931 alignment_too_large_p
= true;
7935 #ifdef BIGGEST_FIELD_ALIGNMENT
7936 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7938 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7940 /* For non-static members, the target supports either
7941 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7942 if it is defined or BIGGEST_ALIGNMENT. */
7943 max_align
= MAX_TARGET_FIELD_ALIGNMENT
;
7944 if (TREE_CODE (node
) == FIELD_DECL
7945 && requested_alignment
> (max_align
= MAX_TARGET_FIELD_ALIGNMENT
))
7946 alignment_too_large_p
= true;
7947 #undef MAX_TARGET_FIELD_ALIGNMENT
7948 /* For stack variables, the target supports at most
7949 MAX_STACK_ALIGNMENT. */
7950 else if (decl_function_context (node
) != NULL
7951 && requested_alignment
> (max_align
= MAX_STACK_ALIGNMENT
))
7952 alignment_too_large_p
= true;
7955 else if (TYPE_P (node
))
7957 /* Let's be liberal for types. */
7958 if (requested_alignment
> (max_align
= BIGGEST_ALIGNMENT
))
7959 alignment_too_large_p
= true;
7962 if (alignment_too_large_p
)
7963 pedwarn (input_location
, OPT_Wattributes
,
7964 "requested alignment %d is larger than %d",
7965 requested_alignment
, max_align
);
7967 return !alignment_too_large_p
;
7970 /* Handle a "aligned" attribute; arguments as in
7971 struct attribute_spec.handler. */
7974 handle_aligned_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
7975 int flags
, bool *no_add_attrs
)
7977 tree decl
= NULL_TREE
;
7985 align_expr
= TREE_VALUE (args
);
7986 if (align_expr
&& TREE_CODE (align_expr
) != IDENTIFIER_NODE
7987 && TREE_CODE (align_expr
) != FUNCTION_DECL
)
7988 align_expr
= default_conversion (align_expr
);
7991 align_expr
= size_int (ATTRIBUTE_ALIGNED_VALUE
/ BITS_PER_UNIT
);
7996 type
= &TREE_TYPE (decl
);
7997 is_type
= TREE_CODE (*node
) == TYPE_DECL
;
7999 else if (TYPE_P (*node
))
8000 type
= node
, is_type
= 1;
8002 if ((i
= check_user_alignment (align_expr
, false)) == -1
8003 || !check_cxx_fundamental_alignment_constraints (*node
, i
, flags
))
8004 *no_add_attrs
= true;
8007 if ((flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
8008 /* OK, modify the type in place. */;
8009 /* If we have a TYPE_DECL, then copy the type, so that we
8010 don't accidentally modify a builtin type. See pushdecl. */
8011 else if (decl
&& TREE_TYPE (decl
) != error_mark_node
8012 && DECL_ORIGINAL_TYPE (decl
) == NULL_TREE
)
8014 tree tt
= TREE_TYPE (decl
);
8015 *type
= build_variant_type_copy (*type
);
8016 DECL_ORIGINAL_TYPE (decl
) = tt
;
8017 TYPE_NAME (*type
) = decl
;
8018 TREE_USED (*type
) = TREE_USED (decl
);
8019 TREE_TYPE (decl
) = *type
;
8022 *type
= build_variant_type_copy (*type
);
8024 TYPE_ALIGN (*type
) = (1U << i
) * BITS_PER_UNIT
;
8025 TYPE_USER_ALIGN (*type
) = 1;
8027 else if (! VAR_OR_FUNCTION_DECL_P (decl
)
8028 && TREE_CODE (decl
) != FIELD_DECL
)
8030 error ("alignment may not be specified for %q+D", decl
);
8031 *no_add_attrs
= true;
8033 else if (DECL_USER_ALIGN (decl
)
8034 && DECL_ALIGN (decl
) > (1U << i
) * BITS_PER_UNIT
)
8035 /* C++-11 [dcl.align/4]:
8037 When multiple alignment-specifiers are specified for an
8038 entity, the alignment requirement shall be set to the
8039 strictest specified alignment.
8041 This formally comes from the c++11 specification but we are
8042 doing it for the GNU attribute syntax as well. */
8043 *no_add_attrs
= true;
8044 else if (TREE_CODE (decl
) == FUNCTION_DECL
8045 && DECL_ALIGN (decl
) > (1U << i
) * BITS_PER_UNIT
)
8047 if (DECL_USER_ALIGN (decl
))
8048 error ("alignment for %q+D was previously specified as %d "
8049 "and may not be decreased", decl
,
8050 DECL_ALIGN (decl
) / BITS_PER_UNIT
);
8052 error ("alignment for %q+D must be at least %d", decl
,
8053 DECL_ALIGN (decl
) / BITS_PER_UNIT
);
8054 *no_add_attrs
= true;
8058 DECL_ALIGN (decl
) = (1U << i
) * BITS_PER_UNIT
;
8059 DECL_USER_ALIGN (decl
) = 1;
8065 /* Handle a "weak" attribute; arguments as in
8066 struct attribute_spec.handler. */
8069 handle_weak_attribute (tree
*node
, tree name
,
8070 tree
ARG_UNUSED (args
),
8071 int ARG_UNUSED (flags
),
8072 bool * ARG_UNUSED (no_add_attrs
))
8074 if (TREE_CODE (*node
) == FUNCTION_DECL
8075 && DECL_DECLARED_INLINE_P (*node
))
8077 warning (OPT_Wattributes
, "inline function %q+D declared weak", *node
);
8078 *no_add_attrs
= true;
8080 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node
)))
8082 error ("indirect function %q+D cannot be declared weak", *node
);
8083 *no_add_attrs
= true;
8086 else if (TREE_CODE (*node
) == FUNCTION_DECL
8087 || TREE_CODE (*node
) == VAR_DECL
)
8089 struct symtab_node
*n
= symtab_node::get (*node
);
8090 if (n
&& n
->refuse_visibility_changes
)
8091 error ("%+D declared weak after being used", *node
);
8092 declare_weak (*node
);
8095 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8100 /* Handle an "alias" or "ifunc" attribute; arguments as in
8101 struct attribute_spec.handler, except that IS_ALIAS tells us
8102 whether this is an alias as opposed to ifunc attribute. */
8105 handle_alias_ifunc_attribute (bool is_alias
, tree
*node
, tree name
, tree args
,
8110 if (TREE_CODE (decl
) != FUNCTION_DECL
8111 && (!is_alias
|| TREE_CODE (decl
) != VAR_DECL
))
8113 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8114 *no_add_attrs
= true;
8116 else if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
8117 || (TREE_CODE (decl
) != FUNCTION_DECL
8118 && TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
8119 /* A static variable declaration is always a tentative definition,
8120 but the alias is a non-tentative definition which overrides. */
8121 || (TREE_CODE (decl
) != FUNCTION_DECL
8122 && ! TREE_PUBLIC (decl
) && DECL_INITIAL (decl
)))
8124 error ("%q+D defined both normally and as %qE attribute", decl
, name
);
8125 *no_add_attrs
= true;
8129 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl
))
8130 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))))
8132 error ("weak %q+D cannot be defined %qE", decl
, name
);
8133 *no_add_attrs
= true;
8137 /* Note that the very first time we process a nested declaration,
8138 decl_function_context will not be set. Indeed, *would* never
8139 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8140 we do below. After such frobbery, pushdecl would set the context.
8141 In any case, this is never what we want. */
8142 else if (decl_function_context (decl
) == 0 && current_function_decl
== NULL
)
8146 id
= TREE_VALUE (args
);
8147 if (TREE_CODE (id
) != STRING_CST
)
8149 error ("attribute %qE argument not a string", name
);
8150 *no_add_attrs
= true;
8153 id
= get_identifier (TREE_STRING_POINTER (id
));
8154 /* This counts as a use of the object pointed to. */
8157 if (TREE_CODE (decl
) == FUNCTION_DECL
)
8158 DECL_INITIAL (decl
) = error_mark_node
;
8160 TREE_STATIC (decl
) = 1;
8163 /* ifuncs are also aliases, so set that attribute too. */
8164 DECL_ATTRIBUTES (decl
)
8165 = tree_cons (get_identifier ("alias"), args
, DECL_ATTRIBUTES (decl
));
8169 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8170 *no_add_attrs
= true;
8173 if (decl_in_symtab_p (*node
))
8175 struct symtab_node
*n
= symtab_node::get (decl
);
8176 if (n
&& n
->refuse_visibility_changes
)
8179 error ("%+D declared alias after being used", decl
);
8181 error ("%+D declared ifunc after being used", decl
);
8189 /* Handle an "alias" or "ifunc" attribute; arguments as in
8190 struct attribute_spec.handler. */
8193 handle_ifunc_attribute (tree
*node
, tree name
, tree args
,
8194 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8196 return handle_alias_ifunc_attribute (false, node
, name
, args
, no_add_attrs
);
8199 /* Handle an "alias" or "ifunc" attribute; arguments as in
8200 struct attribute_spec.handler. */
8203 handle_alias_attribute (tree
*node
, tree name
, tree args
,
8204 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8206 return handle_alias_ifunc_attribute (true, node
, name
, args
, no_add_attrs
);
8209 /* Handle a "weakref" attribute; arguments as in struct
8210 attribute_spec.handler. */
8213 handle_weakref_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
8214 int flags
, bool *no_add_attrs
)
8216 tree attr
= NULL_TREE
;
8218 /* We must ignore the attribute when it is associated with
8219 local-scoped decls, since attribute alias is ignored and many
8220 such symbols do not even have a DECL_WEAK field. */
8221 if (decl_function_context (*node
)
8222 || current_function_decl
8223 || (TREE_CODE (*node
) != VAR_DECL
&& TREE_CODE (*node
) != FUNCTION_DECL
))
8225 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8226 *no_add_attrs
= true;
8230 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node
)))
8232 error ("indirect function %q+D cannot be declared weakref", *node
);
8233 *no_add_attrs
= true;
8237 /* The idea here is that `weakref("name")' mutates into `weakref,
8238 alias("name")', and weakref without arguments, in turn,
8239 implicitly adds weak. */
8243 attr
= tree_cons (get_identifier ("alias"), args
, attr
);
8244 attr
= tree_cons (get_identifier ("weakref"), NULL_TREE
, attr
);
8246 *no_add_attrs
= true;
8248 decl_attributes (node
, attr
, flags
);
8252 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node
)))
8253 error_at (DECL_SOURCE_LOCATION (*node
),
8254 "weakref attribute must appear before alias attribute");
8256 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8257 and that isn't supported; and because it wants to add it to
8258 the list of weak decls, which isn't helpful. */
8259 DECL_WEAK (*node
) = 1;
8262 if (decl_in_symtab_p (*node
))
8264 struct symtab_node
*n
= symtab_node::get (*node
);
8265 if (n
&& n
->refuse_visibility_changes
)
8266 error ("%+D declared weakref after being used", *node
);
8272 /* Handle an "visibility" attribute; arguments as in
8273 struct attribute_spec.handler. */
8276 handle_visibility_attribute (tree
*node
, tree name
, tree args
,
8277 int ARG_UNUSED (flags
),
8278 bool *ARG_UNUSED (no_add_attrs
))
8281 tree id
= TREE_VALUE (args
);
8282 enum symbol_visibility vis
;
8286 if (TREE_CODE (*node
) == ENUMERAL_TYPE
)
8288 else if (TREE_CODE (*node
) != RECORD_TYPE
&& TREE_CODE (*node
) != UNION_TYPE
)
8290 warning (OPT_Wattributes
, "%qE attribute ignored on non-class types",
8294 else if (TYPE_FIELDS (*node
))
8296 error ("%qE attribute ignored because %qT is already defined",
8301 else if (decl_function_context (decl
) != 0 || !TREE_PUBLIC (decl
))
8303 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8307 if (TREE_CODE (id
) != STRING_CST
)
8309 error ("visibility argument not a string");
8313 /* If this is a type, set the visibility on the type decl. */
8316 decl
= TYPE_NAME (decl
);
8319 if (TREE_CODE (decl
) == IDENTIFIER_NODE
)
8321 warning (OPT_Wattributes
, "%qE attribute ignored on types",
8327 if (strcmp (TREE_STRING_POINTER (id
), "default") == 0)
8328 vis
= VISIBILITY_DEFAULT
;
8329 else if (strcmp (TREE_STRING_POINTER (id
), "internal") == 0)
8330 vis
= VISIBILITY_INTERNAL
;
8331 else if (strcmp (TREE_STRING_POINTER (id
), "hidden") == 0)
8332 vis
= VISIBILITY_HIDDEN
;
8333 else if (strcmp (TREE_STRING_POINTER (id
), "protected") == 0)
8334 vis
= VISIBILITY_PROTECTED
;
8337 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8338 vis
= VISIBILITY_DEFAULT
;
8341 if (DECL_VISIBILITY_SPECIFIED (decl
)
8342 && vis
!= DECL_VISIBILITY (decl
))
8344 tree attributes
= (TYPE_P (*node
)
8345 ? TYPE_ATTRIBUTES (*node
)
8346 : DECL_ATTRIBUTES (decl
));
8347 if (lookup_attribute ("visibility", attributes
))
8348 error ("%qD redeclared with different visibility", decl
);
8349 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8350 && lookup_attribute ("dllimport", attributes
))
8351 error ("%qD was declared %qs which implies default visibility",
8353 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8354 && lookup_attribute ("dllexport", attributes
))
8355 error ("%qD was declared %qs which implies default visibility",
8359 DECL_VISIBILITY (decl
) = vis
;
8360 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
8362 /* Go ahead and attach the attribute to the node as well. This is needed
8363 so we can determine whether we have VISIBILITY_DEFAULT because the
8364 visibility was not specified, or because it was explicitly overridden
8365 from the containing scope. */
8370 /* Determine the ELF symbol visibility for DECL, which is either a
8371 variable or a function. It is an error to use this function if a
8372 definition of DECL is not available in this translation unit.
8373 Returns true if the final visibility has been determined by this
8374 function; false if the caller is free to make additional
8378 c_determine_visibility (tree decl
)
8380 gcc_assert (TREE_CODE (decl
) == VAR_DECL
8381 || TREE_CODE (decl
) == FUNCTION_DECL
);
8383 /* If the user explicitly specified the visibility with an
8384 attribute, honor that. DECL_VISIBILITY will have been set during
8385 the processing of the attribute. We check for an explicit
8386 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8387 to distinguish the use of an attribute from the use of a "#pragma
8388 GCC visibility push(...)"; in the latter case we still want other
8389 considerations to be able to overrule the #pragma. */
8390 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl
))
8391 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8392 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl
))
8393 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))))
8396 /* Set default visibility to whatever the user supplied with
8397 visibility_specified depending on #pragma GCC visibility. */
8398 if (!DECL_VISIBILITY_SPECIFIED (decl
))
8400 if (visibility_options
.inpragma
8401 || DECL_VISIBILITY (decl
) != default_visibility
)
8403 DECL_VISIBILITY (decl
) = default_visibility
;
8404 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
8405 /* If visibility changed and DECL already has DECL_RTL, ensure
8406 symbol flags are updated. */
8407 if (((TREE_CODE (decl
) == VAR_DECL
&& TREE_STATIC (decl
))
8408 || TREE_CODE (decl
) == FUNCTION_DECL
)
8409 && DECL_RTL_SET_P (decl
))
8410 make_decl_rtl (decl
);
8416 /* Handle an "tls_model" attribute; arguments as in
8417 struct attribute_spec.handler. */
8420 handle_tls_model_attribute (tree
*node
, tree name
, tree args
,
8421 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8425 enum tls_model kind
;
8427 *no_add_attrs
= true;
8429 if (TREE_CODE (decl
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (decl
))
8431 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8435 kind
= DECL_TLS_MODEL (decl
);
8436 id
= TREE_VALUE (args
);
8437 if (TREE_CODE (id
) != STRING_CST
)
8439 error ("tls_model argument not a string");
8443 if (!strcmp (TREE_STRING_POINTER (id
), "local-exec"))
8444 kind
= TLS_MODEL_LOCAL_EXEC
;
8445 else if (!strcmp (TREE_STRING_POINTER (id
), "initial-exec"))
8446 kind
= TLS_MODEL_INITIAL_EXEC
;
8447 else if (!strcmp (TREE_STRING_POINTER (id
), "local-dynamic"))
8448 kind
= optimize
? TLS_MODEL_LOCAL_DYNAMIC
: TLS_MODEL_GLOBAL_DYNAMIC
;
8449 else if (!strcmp (TREE_STRING_POINTER (id
), "global-dynamic"))
8450 kind
= TLS_MODEL_GLOBAL_DYNAMIC
;
8452 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8454 set_decl_tls_model (decl
, kind
);
8458 /* Handle a "no_instrument_function" attribute; arguments as in
8459 struct attribute_spec.handler. */
8462 handle_no_instrument_function_attribute (tree
*node
, tree name
,
8463 tree
ARG_UNUSED (args
),
8464 int ARG_UNUSED (flags
),
8469 if (TREE_CODE (decl
) != FUNCTION_DECL
)
8471 error_at (DECL_SOURCE_LOCATION (decl
),
8472 "%qE attribute applies only to functions", name
);
8473 *no_add_attrs
= true;
8476 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
8481 /* Handle a "malloc" attribute; arguments as in
8482 struct attribute_spec.handler. */
8485 handle_malloc_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
8486 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8488 if (TREE_CODE (*node
) == FUNCTION_DECL
8489 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node
))))
8490 DECL_IS_MALLOC (*node
) = 1;
8493 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8494 *no_add_attrs
= true;
8500 /* Handle a "alloc_size" attribute; arguments as in
8501 struct attribute_spec.handler. */
8504 handle_alloc_size_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
8505 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8507 unsigned arg_count
= type_num_arguments (*node
);
8508 for (; args
; args
= TREE_CHAIN (args
))
8510 tree position
= TREE_VALUE (args
);
8511 if (position
&& TREE_CODE (position
) != IDENTIFIER_NODE
8512 && TREE_CODE (position
) != FUNCTION_DECL
)
8513 position
= default_conversion (position
);
8515 if (!tree_fits_uhwi_p (position
)
8517 || !IN_RANGE (tree_to_uhwi (position
), 1, arg_count
))
8519 warning (OPT_Wattributes
,
8520 "alloc_size parameter outside range");
8521 *no_add_attrs
= true;
8528 /* Handle a "alloc_align" attribute; arguments as in
8529 struct attribute_spec.handler. */
8532 handle_alloc_align_attribute (tree
*node
, tree
, tree args
, int,
8535 unsigned arg_count
= type_num_arguments (*node
);
8536 tree position
= TREE_VALUE (args
);
8537 if (position
&& TREE_CODE (position
) != IDENTIFIER_NODE
)
8538 position
= default_conversion (position
);
8540 if (!tree_fits_uhwi_p (position
)
8542 || !IN_RANGE (tree_to_uhwi (position
), 1, arg_count
))
8544 warning (OPT_Wattributes
,
8545 "alloc_align parameter outside range");
8546 *no_add_attrs
= true;
8552 /* Handle a "assume_aligned" attribute; arguments as in
8553 struct attribute_spec.handler. */
8556 handle_assume_aligned_attribute (tree
*, tree
, tree args
, int,
8559 for (; args
; args
= TREE_CHAIN (args
))
8561 tree position
= TREE_VALUE (args
);
8562 if (position
&& TREE_CODE (position
) != IDENTIFIER_NODE
8563 && TREE_CODE (position
) != FUNCTION_DECL
)
8564 position
= default_conversion (position
);
8566 if (TREE_CODE (position
) != INTEGER_CST
)
8568 warning (OPT_Wattributes
,
8569 "assume_aligned parameter not integer constant");
8570 *no_add_attrs
= true;
8577 /* Handle a "fn spec" attribute; arguments as in
8578 struct attribute_spec.handler. */
8581 handle_fnspec_attribute (tree
*node ATTRIBUTE_UNUSED
, tree
ARG_UNUSED (name
),
8582 tree args
, int ARG_UNUSED (flags
),
8583 bool *no_add_attrs ATTRIBUTE_UNUSED
)
8586 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
8587 && !TREE_CHAIN (args
));
8591 /* Handle a "bnd_variable_size" attribute; arguments as in
8592 struct attribute_spec.handler. */
8595 handle_bnd_variable_size_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
8596 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8598 if (TREE_CODE (*node
) != FIELD_DECL
)
8600 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8601 *no_add_attrs
= true;
8607 /* Handle a "bnd_legacy" attribute; arguments as in
8608 struct attribute_spec.handler. */
8611 handle_bnd_legacy (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
8612 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8614 if (TREE_CODE (*node
) != FUNCTION_DECL
)
8616 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8617 *no_add_attrs
= true;
8623 /* Handle a "bnd_instrument" attribute; arguments as in
8624 struct attribute_spec.handler. */
8627 handle_bnd_instrument (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
8628 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8630 if (TREE_CODE (*node
) != FUNCTION_DECL
)
8632 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8633 *no_add_attrs
= true;
8639 /* Handle a "warn_unused" attribute; arguments as in
8640 struct attribute_spec.handler. */
8643 handle_warn_unused_attribute (tree
*node
, tree name
,
8644 tree args ATTRIBUTE_UNUSED
,
8645 int flags ATTRIBUTE_UNUSED
, bool *no_add_attrs
)
8648 /* Do nothing else, just set the attribute. We'll get at
8649 it later with lookup_attribute. */
8653 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8654 *no_add_attrs
= true;
8660 /* Handle an "omp declare simd" attribute; arguments as in
8661 struct attribute_spec.handler. */
8664 handle_omp_declare_simd_attribute (tree
*, tree
, tree
, int, bool *)
8669 /* Handle an "omp declare target" attribute; arguments as in
8670 struct attribute_spec.handler. */
8673 handle_omp_declare_target_attribute (tree
*, tree
, tree
, int, bool *)
8678 /* Handle a "returns_twice" attribute; arguments as in
8679 struct attribute_spec.handler. */
8682 handle_returns_twice_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
8683 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8685 if (TREE_CODE (*node
) == FUNCTION_DECL
)
8686 DECL_IS_RETURNS_TWICE (*node
) = 1;
8689 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8690 *no_add_attrs
= true;
8696 /* Handle a "no_limit_stack" attribute; arguments as in
8697 struct attribute_spec.handler. */
8700 handle_no_limit_stack_attribute (tree
*node
, tree name
,
8701 tree
ARG_UNUSED (args
),
8702 int ARG_UNUSED (flags
),
8707 if (TREE_CODE (decl
) != FUNCTION_DECL
)
8709 error_at (DECL_SOURCE_LOCATION (decl
),
8710 "%qE attribute applies only to functions", name
);
8711 *no_add_attrs
= true;
8713 else if (DECL_INITIAL (decl
))
8715 error_at (DECL_SOURCE_LOCATION (decl
),
8716 "can%'t set %qE attribute after definition", name
);
8717 *no_add_attrs
= true;
8720 DECL_NO_LIMIT_STACK (decl
) = 1;
8725 /* Handle a "pure" attribute; arguments as in
8726 struct attribute_spec.handler. */
8729 handle_pure_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
8730 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8732 if (TREE_CODE (*node
) == FUNCTION_DECL
)
8733 DECL_PURE_P (*node
) = 1;
8734 /* ??? TODO: Support types. */
8737 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8738 *no_add_attrs
= true;
8744 /* Digest an attribute list destined for a transactional memory statement.
8745 ALLOWED is the set of attributes that are allowed for this statement;
8746 return the attribute we parsed. Multiple attributes are never allowed. */
8749 parse_tm_stmt_attr (tree attrs
, int allowed
)
8754 for ( ; attrs
; attrs
= TREE_CHAIN (attrs
))
8756 tree a
= TREE_PURPOSE (attrs
);
8759 if (is_attribute_p ("outer", a
))
8760 m
= TM_STMT_ATTR_OUTER
;
8762 if ((m
& allowed
) == 0)
8764 warning (OPT_Wattributes
, "%qE attribute directive ignored", a
);
8773 else if (m_seen
== m
)
8774 warning (OPT_Wattributes
, "%qE attribute duplicated", a
);
8776 warning (OPT_Wattributes
, "%qE attribute follows %qE", a
, a_seen
);
8782 /* Transform a TM attribute name into a maskable integer and back.
8783 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8784 to how the lack of an attribute is treated. */
8787 tm_attr_to_mask (tree attr
)
8791 if (is_attribute_p ("transaction_safe", attr
))
8792 return TM_ATTR_SAFE
;
8793 if (is_attribute_p ("transaction_callable", attr
))
8794 return TM_ATTR_CALLABLE
;
8795 if (is_attribute_p ("transaction_pure", attr
))
8796 return TM_ATTR_PURE
;
8797 if (is_attribute_p ("transaction_unsafe", attr
))
8798 return TM_ATTR_IRREVOCABLE
;
8799 if (is_attribute_p ("transaction_may_cancel_outer", attr
))
8800 return TM_ATTR_MAY_CANCEL_OUTER
;
8805 tm_mask_to_attr (int mask
)
8811 str
= "transaction_safe";
8813 case TM_ATTR_CALLABLE
:
8814 str
= "transaction_callable";
8817 str
= "transaction_pure";
8819 case TM_ATTR_IRREVOCABLE
:
8820 str
= "transaction_unsafe";
8822 case TM_ATTR_MAY_CANCEL_OUTER
:
8823 str
= "transaction_may_cancel_outer";
8828 return get_identifier (str
);
8831 /* Return the first TM attribute seen in LIST. */
8834 find_tm_attribute (tree list
)
8836 for (; list
; list
= TREE_CHAIN (list
))
8838 tree name
= TREE_PURPOSE (list
);
8839 if (tm_attr_to_mask (name
) != 0)
8845 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8846 Here we accept only function types, and verify that none of the other
8847 function TM attributes are also applied. */
8848 /* ??? We need to accept class types for C++, but not C. This greatly
8849 complicates this function, since we can no longer rely on the extra
8850 processing given by function_type_required. */
8853 handle_tm_attribute (tree
*node
, tree name
, tree args
,
8854 int flags
, bool *no_add_attrs
)
8856 /* Only one path adds the attribute; others don't. */
8857 *no_add_attrs
= true;
8859 switch (TREE_CODE (*node
))
8863 /* Only tm_callable and tm_safe apply to classes. */
8864 if (tm_attr_to_mask (name
) & ~(TM_ATTR_SAFE
| TM_ATTR_CALLABLE
))
8871 tree old_name
= find_tm_attribute (TYPE_ATTRIBUTES (*node
));
8872 if (old_name
== name
)
8874 else if (old_name
!= NULL_TREE
)
8875 error ("type was previously declared %qE", old_name
);
8877 *no_add_attrs
= false;
8883 enum tree_code subcode
= TREE_CODE (TREE_TYPE (*node
));
8884 if (subcode
== FUNCTION_TYPE
|| subcode
== METHOD_TYPE
)
8886 tree fn_tmp
= TREE_TYPE (*node
);
8887 decl_attributes (&fn_tmp
, tree_cons (name
, args
, NULL
), 0);
8888 *node
= build_pointer_type (fn_tmp
);
8895 /* If a function is next, pass it on to be tried next. */
8896 if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
8897 return tree_cons (name
, args
, NULL
);
8900 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8907 /* Handle the TM_WRAP attribute; arguments as in
8908 struct attribute_spec.handler. */
8911 handle_tm_wrap_attribute (tree
*node
, tree name
, tree args
,
8912 int ARG_UNUSED (flags
), bool *no_add_attrs
)
8916 /* We don't need the attribute even on success, since we
8917 record the entry in an external table. */
8918 *no_add_attrs
= true;
8920 if (TREE_CODE (decl
) != FUNCTION_DECL
)
8921 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
8924 tree wrap_decl
= TREE_VALUE (args
);
8925 if (error_operand_p (wrap_decl
))
8927 else if (TREE_CODE (wrap_decl
) != IDENTIFIER_NODE
8928 && TREE_CODE (wrap_decl
) != VAR_DECL
8929 && TREE_CODE (wrap_decl
) != FUNCTION_DECL
)
8930 error ("%qE argument not an identifier", name
);
8933 if (TREE_CODE (wrap_decl
) == IDENTIFIER_NODE
)
8934 wrap_decl
= lookup_name (wrap_decl
);
8935 if (wrap_decl
&& TREE_CODE (wrap_decl
) == FUNCTION_DECL
)
8937 if (lang_hooks
.types_compatible_p (TREE_TYPE (decl
),
8938 TREE_TYPE (wrap_decl
)))
8939 record_tm_replacement (wrap_decl
, decl
);
8941 error ("%qD is not compatible with %qD", wrap_decl
, decl
);
8944 error ("%qE argument is not a function", name
);
8951 /* Ignore the given attribute. Used when this attribute may be usefully
8952 overridden by the target, but is not used generically. */
8955 ignore_attribute (tree
* ARG_UNUSED (node
), tree
ARG_UNUSED (name
),
8956 tree
ARG_UNUSED (args
), int ARG_UNUSED (flags
),
8959 *no_add_attrs
= true;
8963 /* Handle a "no vops" attribute; arguments as in
8964 struct attribute_spec.handler. */
8967 handle_novops_attribute (tree
*node
, tree
ARG_UNUSED (name
),
8968 tree
ARG_UNUSED (args
), int ARG_UNUSED (flags
),
8969 bool *ARG_UNUSED (no_add_attrs
))
8971 gcc_assert (TREE_CODE (*node
) == FUNCTION_DECL
);
8972 DECL_IS_NOVOPS (*node
) = 1;
8976 /* Handle a "deprecated" attribute; arguments as in
8977 struct attribute_spec.handler. */
8980 handle_deprecated_attribute (tree
*node
, tree name
,
8981 tree args
, int flags
,
8984 tree type
= NULL_TREE
;
8986 tree what
= NULL_TREE
;
8989 *no_add_attrs
= true;
8990 else if (TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
8992 error ("deprecated message is not a string");
8993 *no_add_attrs
= true;
8999 type
= TREE_TYPE (decl
);
9001 if (TREE_CODE (decl
) == TYPE_DECL
9002 || TREE_CODE (decl
) == PARM_DECL
9003 || TREE_CODE (decl
) == VAR_DECL
9004 || TREE_CODE (decl
) == FUNCTION_DECL
9005 || TREE_CODE (decl
) == FIELD_DECL
9006 || objc_method_decl (TREE_CODE (decl
)))
9007 TREE_DEPRECATED (decl
) = 1;
9011 else if (TYPE_P (*node
))
9013 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
9014 *node
= build_variant_type_copy (*node
);
9015 TREE_DEPRECATED (*node
) = 1;
9023 *no_add_attrs
= true;
9024 if (type
&& TYPE_NAME (type
))
9026 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
9027 what
= TYPE_NAME (*node
);
9028 else if (TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
9029 && DECL_NAME (TYPE_NAME (type
)))
9030 what
= DECL_NAME (TYPE_NAME (type
));
9033 warning (OPT_Wattributes
, "%qE attribute ignored for %qE", name
, what
);
9035 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
9041 /* Handle a "vector_size" attribute; arguments as in
9042 struct attribute_spec.handler. */
9045 handle_vector_size_attribute (tree
*node
, tree name
, tree args
,
9046 int ARG_UNUSED (flags
),
9049 unsigned HOST_WIDE_INT vecsize
, nunits
;
9050 machine_mode orig_mode
;
9051 tree type
= *node
, new_type
, size
;
9053 *no_add_attrs
= true;
9055 size
= TREE_VALUE (args
);
9056 if (size
&& TREE_CODE (size
) != IDENTIFIER_NODE
9057 && TREE_CODE (size
) != FUNCTION_DECL
)
9058 size
= default_conversion (size
);
9060 if (!tree_fits_uhwi_p (size
))
9062 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
9066 /* Get the vector size (in bytes). */
9067 vecsize
= tree_to_uhwi (size
);
9069 /* We need to provide for vector pointers, vector arrays, and
9070 functions returning vectors. For example:
9072 __attribute__((vector_size(16))) short *foo;
9074 In this case, the mode is SI, but the type being modified is
9075 HI, so we need to look further. */
9077 while (POINTER_TYPE_P (type
)
9078 || TREE_CODE (type
) == FUNCTION_TYPE
9079 || TREE_CODE (type
) == METHOD_TYPE
9080 || TREE_CODE (type
) == ARRAY_TYPE
9081 || TREE_CODE (type
) == OFFSET_TYPE
)
9082 type
= TREE_TYPE (type
);
9084 /* Get the mode of the type being modified. */
9085 orig_mode
= TYPE_MODE (type
);
9087 if ((!INTEGRAL_TYPE_P (type
)
9088 && !SCALAR_FLOAT_TYPE_P (type
)
9089 && !FIXED_POINT_TYPE_P (type
))
9090 || (!SCALAR_FLOAT_MODE_P (orig_mode
)
9091 && GET_MODE_CLASS (orig_mode
) != MODE_INT
9092 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode
))
9093 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
))
9094 || TREE_CODE (type
) == BOOLEAN_TYPE
)
9096 error ("invalid vector type for attribute %qE", name
);
9100 if (vecsize
% tree_to_uhwi (TYPE_SIZE_UNIT (type
)))
9102 error ("vector size not an integral multiple of component size");
9108 error ("zero vector size");
9112 /* Calculate how many units fit in the vector. */
9113 nunits
= vecsize
/ tree_to_uhwi (TYPE_SIZE_UNIT (type
));
9114 if (nunits
& (nunits
- 1))
9116 error ("number of components of the vector not a power of two");
9120 new_type
= build_vector_type (type
, nunits
);
9122 /* Build back pointers if needed. */
9123 *node
= lang_hooks
.types
.reconstruct_complex_type (*node
, new_type
);
9128 /* Handle the "nonnull" attribute. */
9130 handle_nonnull_attribute (tree
*node
, tree
ARG_UNUSED (name
),
9131 tree args
, int ARG_UNUSED (flags
),
9135 unsigned HOST_WIDE_INT attr_arg_num
;
9137 /* If no arguments are specified, all pointer arguments should be
9138 non-null. Verify a full prototype is given so that the arguments
9139 will have the correct types when we actually check them later. */
9142 if (!prototype_p (type
))
9144 error ("nonnull attribute without arguments on a non-prototype");
9145 *no_add_attrs
= true;
9150 /* Argument list specified. Verify that each argument number references
9151 a pointer argument. */
9152 for (attr_arg_num
= 1; args
; attr_arg_num
++, args
= TREE_CHAIN (args
))
9154 unsigned HOST_WIDE_INT arg_num
= 0, ck_num
;
9156 tree arg
= TREE_VALUE (args
);
9157 if (arg
&& TREE_CODE (arg
) != IDENTIFIER_NODE
9158 && TREE_CODE (arg
) != FUNCTION_DECL
)
9159 arg
= default_conversion (arg
);
9161 if (!get_nonnull_operand (arg
, &arg_num
))
9163 error ("nonnull argument has invalid operand number (argument %lu)",
9164 (unsigned long) attr_arg_num
);
9165 *no_add_attrs
= true;
9169 if (prototype_p (type
))
9171 function_args_iterator iter
;
9174 function_args_iter_init (&iter
, type
);
9175 for (ck_num
= 1; ; ck_num
++, function_args_iter_next (&iter
))
9177 argument
= function_args_iter_cond (&iter
);
9178 if (argument
== NULL_TREE
|| ck_num
== arg_num
)
9183 || TREE_CODE (argument
) == VOID_TYPE
)
9185 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9186 (unsigned long) attr_arg_num
, (unsigned long) arg_num
);
9187 *no_add_attrs
= true;
9191 if (TREE_CODE (argument
) != POINTER_TYPE
)
9193 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9194 (unsigned long) attr_arg_num
, (unsigned long) arg_num
);
9195 *no_add_attrs
= true;
9204 /* Check the argument list of a function call for null in argument slots
9205 that are marked as requiring a non-null pointer argument. The NARGS
9206 arguments are passed in the array ARGARRAY.
9210 check_function_nonnull (tree attrs
, int nargs
, tree
*argarray
)
9215 attrs
= lookup_attribute ("nonnull", attrs
);
9216 if (attrs
== NULL_TREE
)
9220 /* See if any of the nonnull attributes has no arguments. If so,
9221 then every pointer argument is checked (in which case the check
9222 for pointer type is done in check_nonnull_arg). */
9223 if (TREE_VALUE (a
) != NULL_TREE
)
9225 a
= lookup_attribute ("nonnull", TREE_CHAIN (a
));
9226 while (a
!= NULL_TREE
&& TREE_VALUE (a
) != NULL_TREE
);
9229 for (i
= 0; i
< nargs
; i
++)
9230 check_function_arguments_recurse (check_nonnull_arg
, NULL
, argarray
[i
],
9234 /* Walk the argument list. If we encounter an argument number we
9235 should check for non-null, do it. */
9236 for (i
= 0; i
< nargs
; i
++)
9238 for (a
= attrs
; ; a
= TREE_CHAIN (a
))
9240 a
= lookup_attribute ("nonnull", a
);
9241 if (a
== NULL_TREE
|| nonnull_check_p (TREE_VALUE (a
), i
+ 1))
9246 check_function_arguments_recurse (check_nonnull_arg
, NULL
,
9247 argarray
[i
], i
+ 1);
9252 /* Check that the Nth argument of a function call (counting backwards
9253 from the end) is a (pointer)0. The NARGS arguments are passed in the
9257 check_function_sentinel (const_tree fntype
, int nargs
, tree
*argarray
)
9259 tree attr
= lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype
));
9266 function_args_iterator iter
;
9269 /* Skip over the named arguments. */
9270 FOREACH_FUNCTION_ARGS (fntype
, t
, iter
)
9277 if (TREE_VALUE (attr
))
9279 tree p
= TREE_VALUE (TREE_VALUE (attr
));
9280 pos
= TREE_INT_CST_LOW (p
);
9283 /* The sentinel must be one of the varargs, i.e.
9284 in position >= the number of fixed arguments. */
9285 if ((nargs
- 1 - pos
) < len
)
9287 warning (OPT_Wformat_
,
9288 "not enough variable arguments to fit a sentinel");
9292 /* Validate the sentinel. */
9293 sentinel
= argarray
[nargs
- 1 - pos
];
9294 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel
))
9295 || !integer_zerop (sentinel
))
9296 /* Although __null (in C++) is only an integer we allow it
9297 nevertheless, as we are guaranteed that it's exactly
9298 as wide as a pointer, and we don't want to force
9299 users to cast the NULL they have written there.
9300 We warn with -Wstrict-null-sentinel, though. */
9301 && (warn_strict_null_sentinel
|| null_node
!= sentinel
))
9302 warning (OPT_Wformat_
, "missing sentinel in function call");
9306 /* Helper for check_function_nonnull; given a list of operands which
9307 must be non-null in ARGS, determine if operand PARAM_NUM should be
9311 nonnull_check_p (tree args
, unsigned HOST_WIDE_INT param_num
)
9313 unsigned HOST_WIDE_INT arg_num
= 0;
9315 for (; args
; args
= TREE_CHAIN (args
))
9317 bool found
= get_nonnull_operand (TREE_VALUE (args
), &arg_num
);
9321 if (arg_num
== param_num
)
9327 /* Check that the function argument PARAM (which is operand number
9328 PARAM_NUM) is non-null. This is called by check_function_nonnull
9329 via check_function_arguments_recurse. */
9332 check_nonnull_arg (void * ARG_UNUSED (ctx
), tree param
,
9333 unsigned HOST_WIDE_INT param_num
)
9335 /* Just skip checking the argument if it's not a pointer. This can
9336 happen if the "nonnull" attribute was given without an operand
9337 list (which means to check every pointer argument). */
9339 if (TREE_CODE (TREE_TYPE (param
)) != POINTER_TYPE
)
9342 if (integer_zerop (param
))
9343 warning (OPT_Wnonnull
, "null argument where non-null required "
9344 "(argument %lu)", (unsigned long) param_num
);
9347 /* Helper for nonnull attribute handling; fetch the operand number
9348 from the attribute argument list. */
9351 get_nonnull_operand (tree arg_num_expr
, unsigned HOST_WIDE_INT
*valp
)
9353 /* Verify the arg number is a small constant. */
9354 if (tree_fits_uhwi_p (arg_num_expr
))
9356 *valp
= TREE_INT_CST_LOW (arg_num_expr
);
9363 /* Handle a "nothrow" attribute; arguments as in
9364 struct attribute_spec.handler. */
9367 handle_nothrow_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
9368 int ARG_UNUSED (flags
), bool *no_add_attrs
)
9370 if (TREE_CODE (*node
) == FUNCTION_DECL
)
9371 TREE_NOTHROW (*node
) = 1;
9372 /* ??? TODO: Support types. */
9375 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
9376 *no_add_attrs
= true;
9382 /* Handle a "cleanup" attribute; arguments as in
9383 struct attribute_spec.handler. */
9386 handle_cleanup_attribute (tree
*node
, tree name
, tree args
,
9387 int ARG_UNUSED (flags
), bool *no_add_attrs
)
9390 tree cleanup_id
, cleanup_decl
;
9392 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9393 for global destructors in C++. This requires infrastructure that
9394 we don't have generically at the moment. It's also not a feature
9395 we'd be missing too much, since we do have attribute constructor. */
9396 if (TREE_CODE (decl
) != VAR_DECL
|| TREE_STATIC (decl
))
9398 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
9399 *no_add_attrs
= true;
9403 /* Verify that the argument is a function in scope. */
9404 /* ??? We could support pointers to functions here as well, if
9405 that was considered desirable. */
9406 cleanup_id
= TREE_VALUE (args
);
9407 if (TREE_CODE (cleanup_id
) != IDENTIFIER_NODE
)
9409 error ("cleanup argument not an identifier");
9410 *no_add_attrs
= true;
9413 cleanup_decl
= lookup_name (cleanup_id
);
9414 if (!cleanup_decl
|| TREE_CODE (cleanup_decl
) != FUNCTION_DECL
)
9416 error ("cleanup argument not a function");
9417 *no_add_attrs
= true;
9421 /* That the function has proper type is checked with the
9422 eventual call to build_function_call. */
9427 /* Handle a "warn_unused_result" attribute. No special handling. */
9430 handle_warn_unused_result_attribute (tree
*node
, tree name
,
9431 tree
ARG_UNUSED (args
),
9432 int ARG_UNUSED (flags
), bool *no_add_attrs
)
9434 /* Ignore the attribute for functions not returning any value. */
9435 if (VOID_TYPE_P (TREE_TYPE (*node
)))
9437 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
9438 *no_add_attrs
= true;
9444 /* Handle a "sentinel" attribute. */
9447 handle_sentinel_attribute (tree
*node
, tree name
, tree args
,
9448 int ARG_UNUSED (flags
), bool *no_add_attrs
)
9450 if (!prototype_p (*node
))
9452 warning (OPT_Wattributes
,
9453 "%qE attribute requires prototypes with named arguments", name
);
9454 *no_add_attrs
= true;
9458 if (!stdarg_p (*node
))
9460 warning (OPT_Wattributes
,
9461 "%qE attribute only applies to variadic functions", name
);
9462 *no_add_attrs
= true;
9468 tree position
= TREE_VALUE (args
);
9469 if (position
&& TREE_CODE (position
) != IDENTIFIER_NODE
9470 && TREE_CODE (position
) != FUNCTION_DECL
)
9471 position
= default_conversion (position
);
9473 if (TREE_CODE (position
) != INTEGER_CST
9474 || !INTEGRAL_TYPE_P (TREE_TYPE (position
)))
9476 warning (OPT_Wattributes
,
9477 "requested position is not an integer constant");
9478 *no_add_attrs
= true;
9482 if (tree_int_cst_lt (position
, integer_zero_node
))
9484 warning (OPT_Wattributes
,
9485 "requested position is less than zero");
9486 *no_add_attrs
= true;
9494 /* Handle a "type_generic" attribute. */
9497 handle_type_generic_attribute (tree
*node
, tree
ARG_UNUSED (name
),
9498 tree
ARG_UNUSED (args
), int ARG_UNUSED (flags
),
9499 bool * ARG_UNUSED (no_add_attrs
))
9501 /* Ensure we have a function type. */
9502 gcc_assert (TREE_CODE (*node
) == FUNCTION_TYPE
);
9504 /* Ensure we have a variadic function. */
9505 gcc_assert (!prototype_p (*node
) || stdarg_p (*node
));
9510 /* Handle a "target" attribute. */
9513 handle_target_attribute (tree
*node
, tree name
, tree args
, int flags
,
9516 /* Ensure we have a function type. */
9517 if (TREE_CODE (*node
) != FUNCTION_DECL
)
9519 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
9520 *no_add_attrs
= true;
9522 else if (! targetm
.target_option
.valid_attribute_p (*node
, name
, args
,
9524 *no_add_attrs
= true;
9529 /* Arguments being collected for optimization. */
9530 typedef const char *const_char_p
; /* For DEF_VEC_P. */
9531 static GTY(()) vec
<const_char_p
, va_gc
> *optimize_args
;
9534 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9535 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9536 false for #pragma GCC optimize. */
9539 parse_optimize_options (tree args
, bool attr_p
)
9544 int saved_flag_strict_aliasing
;
9545 const char **opt_argv
;
9546 struct cl_decoded_option
*decoded_options
;
9547 unsigned int decoded_options_count
;
9550 /* Build up argv vector. Just in case the string is stored away, use garbage
9551 collected strings. */
9552 vec_safe_truncate (optimize_args
, 0);
9553 vec_safe_push (optimize_args
, (const char *) NULL
);
9555 for (ap
= args
; ap
!= NULL_TREE
; ap
= TREE_CHAIN (ap
))
9557 tree value
= TREE_VALUE (ap
);
9559 if (TREE_CODE (value
) == INTEGER_CST
)
9562 sprintf (buffer
, "-O%ld", (long) TREE_INT_CST_LOW (value
));
9563 vec_safe_push (optimize_args
, ggc_strdup (buffer
));
9566 else if (TREE_CODE (value
) == STRING_CST
)
9568 /* Split string into multiple substrings. */
9569 size_t len
= TREE_STRING_LENGTH (value
);
9570 char *p
= ASTRDUP (TREE_STRING_POINTER (value
));
9571 char *end
= p
+ len
;
9575 while (next_p
!= NULL
)
9581 comma
= strchr (p
, ',');
9594 r
= q
= (char *) ggc_alloc_atomic (len2
+ 3);
9596 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9598 if (*p
== '-' && p
[1] != 'O' && p
[1] != 'f')
9602 warning (OPT_Wattributes
,
9603 "bad option %s to optimize attribute", p
);
9605 warning (OPT_Wpragmas
,
9606 "bad option %s to pragma attribute", p
);
9614 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9615 itself is -Os, and any other switch begins with a -f. */
9616 if ((*p
>= '0' && *p
<= '9')
9617 || (p
[0] == 's' && p
[1] == '\0'))
9623 memcpy (r
, p
, len2
);
9625 vec_safe_push (optimize_args
, (const char *) q
);
9631 opt_argc
= optimize_args
->length ();
9632 opt_argv
= (const char **) alloca (sizeof (char *) * (opt_argc
+ 1));
9634 for (i
= 1; i
< opt_argc
; i
++)
9635 opt_argv
[i
] = (*optimize_args
)[i
];
9637 saved_flag_strict_aliasing
= flag_strict_aliasing
;
9639 /* Now parse the options. */
9640 decode_cmdline_options_to_array_default_mask (opt_argc
, opt_argv
,
9642 &decoded_options_count
);
9643 decode_options (&global_options
, &global_options_set
,
9644 decoded_options
, decoded_options_count
,
9645 input_location
, global_dc
);
9647 targetm
.override_options_after_change();
9649 /* Don't allow changing -fstrict-aliasing. */
9650 flag_strict_aliasing
= saved_flag_strict_aliasing
;
9652 optimize_args
->truncate (0);
9656 /* For handling "optimize" attribute. arguments as in
9657 struct attribute_spec.handler. */
9660 handle_optimize_attribute (tree
*node
, tree name
, tree args
,
9661 int ARG_UNUSED (flags
), bool *no_add_attrs
)
9663 /* Ensure we have a function type. */
9664 if (TREE_CODE (*node
) != FUNCTION_DECL
)
9666 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
9667 *no_add_attrs
= true;
9671 struct cl_optimization cur_opts
;
9672 tree old_opts
= DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
);
9674 /* Save current options. */
9675 cl_optimization_save (&cur_opts
, &global_options
);
9677 /* If we previously had some optimization options, use them as the
9680 cl_optimization_restore (&global_options
,
9681 TREE_OPTIMIZATION (old_opts
));
9683 /* Parse options, and update the vector. */
9684 parse_optimize_options (args
, true);
9685 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
)
9686 = build_optimization_node (&global_options
);
9688 /* Restore current options. */
9689 cl_optimization_restore (&global_options
, &cur_opts
);
9695 /* Handle a "no_split_stack" attribute. */
9698 handle_no_split_stack_attribute (tree
*node
, tree name
,
9699 tree
ARG_UNUSED (args
),
9700 int ARG_UNUSED (flags
),
9705 if (TREE_CODE (decl
) != FUNCTION_DECL
)
9707 error_at (DECL_SOURCE_LOCATION (decl
),
9708 "%qE attribute applies only to functions", name
);
9709 *no_add_attrs
= true;
9711 else if (DECL_INITIAL (decl
))
9713 error_at (DECL_SOURCE_LOCATION (decl
),
9714 "can%'t set %qE attribute after definition", name
);
9715 *no_add_attrs
= true;
9721 /* Handle a "returns_nonnull" attribute; arguments as in
9722 struct attribute_spec.handler. */
9725 handle_returns_nonnull_attribute (tree
*node
, tree
, tree
, int,
9728 // Even without a prototype we still have a return type we can check.
9729 if (TREE_CODE (TREE_TYPE (*node
)) != POINTER_TYPE
)
9731 error ("returns_nonnull attribute on a function not returning a pointer");
9732 *no_add_attrs
= true;
9737 /* Handle a "designated_init" attribute; arguments as in
9738 struct attribute_spec.handler. */
9741 handle_designated_init_attribute (tree
*node
, tree name
, tree
, int,
9744 if (TREE_CODE (*node
) != RECORD_TYPE
)
9746 error ("%qE attribute is only valid on %<struct%> type", name
);
9747 *no_add_attrs
= true;
9753 /* Check for valid arguments being passed to a function with FNTYPE.
9754 There are NARGS arguments in the array ARGARRAY. */
9756 check_function_arguments (const_tree fntype
, int nargs
, tree
*argarray
)
9758 /* Check for null being passed in a pointer argument that must be
9759 non-null. We also need to do this if format checking is enabled. */
9762 check_function_nonnull (TYPE_ATTRIBUTES (fntype
), nargs
, argarray
);
9764 /* Check for errors in format strings. */
9766 if (warn_format
|| warn_suggest_attribute_format
)
9767 check_function_format (TYPE_ATTRIBUTES (fntype
), nargs
, argarray
);
9770 check_function_sentinel (fntype
, nargs
, argarray
);
9773 /* Generic argument checking recursion routine. PARAM is the argument to
9774 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9775 once the argument is resolved. CTX is context for the callback. */
9777 check_function_arguments_recurse (void (*callback
)
9778 (void *, tree
, unsigned HOST_WIDE_INT
),
9779 void *ctx
, tree param
,
9780 unsigned HOST_WIDE_INT param_num
)
9782 if (CONVERT_EXPR_P (param
)
9783 && (TYPE_PRECISION (TREE_TYPE (param
))
9784 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param
, 0)))))
9786 /* Strip coercion. */
9787 check_function_arguments_recurse (callback
, ctx
,
9788 TREE_OPERAND (param
, 0), param_num
);
9792 if (TREE_CODE (param
) == CALL_EXPR
)
9794 tree type
= TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param
)));
9796 bool found_format_arg
= false;
9798 /* See if this is a call to a known internationalization function
9799 that modifies a format arg. Such a function may have multiple
9800 format_arg attributes (for example, ngettext). */
9802 for (attrs
= TYPE_ATTRIBUTES (type
);
9804 attrs
= TREE_CHAIN (attrs
))
9805 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs
)))
9808 tree format_num_expr
;
9811 call_expr_arg_iterator iter
;
9813 /* Extract the argument number, which was previously checked
9815 format_num_expr
= TREE_VALUE (TREE_VALUE (attrs
));
9817 format_num
= tree_to_uhwi (format_num_expr
);
9819 for (inner_arg
= first_call_expr_arg (param
, &iter
), i
= 1;
9821 inner_arg
= next_call_expr_arg (&iter
), i
++)
9822 if (i
== format_num
)
9824 check_function_arguments_recurse (callback
, ctx
,
9825 inner_arg
, param_num
);
9826 found_format_arg
= true;
9831 /* If we found a format_arg attribute and did a recursive check,
9832 we are done with checking this argument. Otherwise, we continue
9833 and this will be considered a non-literal. */
9834 if (found_format_arg
)
9838 if (TREE_CODE (param
) == COND_EXPR
)
9840 /* Check both halves of the conditional expression. */
9841 check_function_arguments_recurse (callback
, ctx
,
9842 TREE_OPERAND (param
, 1), param_num
);
9843 check_function_arguments_recurse (callback
, ctx
,
9844 TREE_OPERAND (param
, 2), param_num
);
9848 (*callback
) (ctx
, param
, param_num
);
9851 /* Checks for a builtin function FNDECL that the number of arguments
9852 NARGS against the required number REQUIRED and issues an error if
9853 there is a mismatch. Returns true if the number of arguments is
9854 correct, otherwise false. */
9857 builtin_function_validate_nargs (tree fndecl
, int nargs
, int required
)
9859 if (nargs
< required
)
9861 error_at (input_location
,
9862 "not enough arguments to function %qE", fndecl
);
9865 else if (nargs
> required
)
9867 error_at (input_location
,
9868 "too many arguments to function %qE", fndecl
);
9874 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9875 Returns false if there was an error, otherwise true. */
9878 check_builtin_function_arguments (tree fndecl
, int nargs
, tree
*args
)
9880 if (!DECL_BUILT_IN (fndecl
)
9881 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
9884 switch (DECL_FUNCTION_CODE (fndecl
))
9886 case BUILT_IN_CONSTANT_P
:
9887 return builtin_function_validate_nargs (fndecl
, nargs
, 1);
9889 case BUILT_IN_ISFINITE
:
9890 case BUILT_IN_ISINF
:
9891 case BUILT_IN_ISINF_SIGN
:
9892 case BUILT_IN_ISNAN
:
9893 case BUILT_IN_ISNORMAL
:
9894 if (builtin_function_validate_nargs (fndecl
, nargs
, 1))
9896 if (TREE_CODE (TREE_TYPE (args
[0])) != REAL_TYPE
)
9898 error ("non-floating-point argument in call to "
9899 "function %qE", fndecl
);
9906 case BUILT_IN_ISGREATER
:
9907 case BUILT_IN_ISGREATEREQUAL
:
9908 case BUILT_IN_ISLESS
:
9909 case BUILT_IN_ISLESSEQUAL
:
9910 case BUILT_IN_ISLESSGREATER
:
9911 case BUILT_IN_ISUNORDERED
:
9912 if (builtin_function_validate_nargs (fndecl
, nargs
, 2))
9914 enum tree_code code0
, code1
;
9915 code0
= TREE_CODE (TREE_TYPE (args
[0]));
9916 code1
= TREE_CODE (TREE_TYPE (args
[1]));
9917 if (!((code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
9918 || (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
9919 || (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)))
9921 error ("non-floating-point arguments in call to "
9922 "function %qE", fndecl
);
9929 case BUILT_IN_FPCLASSIFY
:
9930 if (builtin_function_validate_nargs (fndecl
, nargs
, 6))
9935 if (TREE_CODE (args
[i
]) != INTEGER_CST
)
9937 error ("non-const integer argument %u in call to function %qE",
9942 if (TREE_CODE (TREE_TYPE (args
[5])) != REAL_TYPE
)
9944 error ("non-floating-point argument in call to function %qE",
9952 case BUILT_IN_ASSUME_ALIGNED
:
9953 if (builtin_function_validate_nargs (fndecl
, nargs
, 2 + (nargs
> 2)))
9955 if (nargs
>= 3 && TREE_CODE (TREE_TYPE (args
[2])) != INTEGER_TYPE
)
9957 error ("non-integer argument 3 in call to function %qE", fndecl
);
9964 case BUILT_IN_ADD_OVERFLOW
:
9965 case BUILT_IN_SUB_OVERFLOW
:
9966 case BUILT_IN_MUL_OVERFLOW
:
9967 if (builtin_function_validate_nargs (fndecl
, nargs
, 3))
9970 for (i
= 0; i
< 2; i
++)
9971 if (!INTEGRAL_TYPE_P (TREE_TYPE (args
[i
])))
9973 error ("argument %u in call to function %qE does not have "
9974 "integral type", i
+ 1, fndecl
);
9977 if (TREE_CODE (TREE_TYPE (args
[2])) != POINTER_TYPE
9978 || TREE_CODE (TREE_TYPE (TREE_TYPE (args
[2]))) != INTEGER_TYPE
)
9980 error ("argument 3 in call to function %qE does not have "
9981 "pointer to integer type", fndecl
);
9993 /* Function to help qsort sort FIELD_DECLs by name order. */
9996 field_decl_cmp (const void *x_p
, const void *y_p
)
9998 const tree
*const x
= (const tree
*const) x_p
;
9999 const tree
*const y
= (const tree
*const) y_p
;
10001 if (DECL_NAME (*x
) == DECL_NAME (*y
))
10002 /* A nontype is "greater" than a type. */
10003 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
10004 if (DECL_NAME (*x
) == NULL_TREE
)
10006 if (DECL_NAME (*y
) == NULL_TREE
)
10008 if (DECL_NAME (*x
) < DECL_NAME (*y
))
10014 gt_pointer_operator new_value
;
10018 /* This routine compares two fields like field_decl_cmp but using the
10019 pointer operator in resort_data. */
10022 resort_field_decl_cmp (const void *x_p
, const void *y_p
)
10024 const tree
*const x
= (const tree
*const) x_p
;
10025 const tree
*const y
= (const tree
*const) y_p
;
10027 if (DECL_NAME (*x
) == DECL_NAME (*y
))
10028 /* A nontype is "greater" than a type. */
10029 return (TREE_CODE (*y
) == TYPE_DECL
) - (TREE_CODE (*x
) == TYPE_DECL
);
10030 if (DECL_NAME (*x
) == NULL_TREE
)
10032 if (DECL_NAME (*y
) == NULL_TREE
)
10035 tree d1
= DECL_NAME (*x
);
10036 tree d2
= DECL_NAME (*y
);
10037 resort_data
.new_value (&d1
, resort_data
.cookie
);
10038 resort_data
.new_value (&d2
, resort_data
.cookie
);
10045 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10048 resort_sorted_fields (void *obj
,
10049 void * ARG_UNUSED (orig_obj
),
10050 gt_pointer_operator new_value
,
10053 struct sorted_fields_type
*sf
= (struct sorted_fields_type
*) obj
;
10054 resort_data
.new_value
= new_value
;
10055 resort_data
.cookie
= cookie
;
10056 qsort (&sf
->elts
[0], sf
->len
, sizeof (tree
),
10057 resort_field_decl_cmp
);
10060 /* Subroutine of c_parse_error.
10061 Return the result of concatenating LHS and RHS. RHS is really
10062 a string literal, its first character is indicated by RHS_START and
10063 RHS_SIZE is its length (including the terminating NUL character).
10065 The caller is responsible for deleting the returned pointer. */
10068 catenate_strings (const char *lhs
, const char *rhs_start
, int rhs_size
)
10070 const int lhs_size
= strlen (lhs
);
10071 char *result
= XNEWVEC (char, lhs_size
+ rhs_size
);
10072 strncpy (result
, lhs
, lhs_size
);
10073 strncpy (result
+ lhs_size
, rhs_start
, rhs_size
);
10077 /* Issue the error given by GMSGID, indicating that it occurred before
10078 TOKEN, which had the associated VALUE. */
10081 c_parse_error (const char *gmsgid
, enum cpp_ttype token_type
,
10082 tree value
, unsigned char token_flags
)
10084 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10086 char *message
= NULL
;
10088 if (token_type
== CPP_EOF
)
10089 message
= catenate_messages (gmsgid
, " at end of input");
10090 else if (token_type
== CPP_CHAR
10091 || token_type
== CPP_WCHAR
10092 || token_type
== CPP_CHAR16
10093 || token_type
== CPP_CHAR32
)
10095 unsigned int val
= TREE_INT_CST_LOW (value
);
10096 const char *prefix
;
10098 switch (token_type
)
10114 if (val
<= UCHAR_MAX
&& ISGRAPH (val
))
10115 message
= catenate_messages (gmsgid
, " before %s'%c'");
10117 message
= catenate_messages (gmsgid
, " before %s'\\x%x'");
10119 error (message
, prefix
, val
);
10123 else if (token_type
== CPP_CHAR_USERDEF
10124 || token_type
== CPP_WCHAR_USERDEF
10125 || token_type
== CPP_CHAR16_USERDEF
10126 || token_type
== CPP_CHAR32_USERDEF
)
10127 message
= catenate_messages (gmsgid
,
10128 " before user-defined character literal");
10129 else if (token_type
== CPP_STRING_USERDEF
10130 || token_type
== CPP_WSTRING_USERDEF
10131 || token_type
== CPP_STRING16_USERDEF
10132 || token_type
== CPP_STRING32_USERDEF
10133 || token_type
== CPP_UTF8STRING_USERDEF
)
10134 message
= catenate_messages (gmsgid
, " before user-defined string literal");
10135 else if (token_type
== CPP_STRING
10136 || token_type
== CPP_WSTRING
10137 || token_type
== CPP_STRING16
10138 || token_type
== CPP_STRING32
10139 || token_type
== CPP_UTF8STRING
)
10140 message
= catenate_messages (gmsgid
, " before string constant");
10141 else if (token_type
== CPP_NUMBER
)
10142 message
= catenate_messages (gmsgid
, " before numeric constant");
10143 else if (token_type
== CPP_NAME
)
10145 message
= catenate_messages (gmsgid
, " before %qE");
10146 error (message
, value
);
10150 else if (token_type
== CPP_PRAGMA
)
10151 message
= catenate_messages (gmsgid
, " before %<#pragma%>");
10152 else if (token_type
== CPP_PRAGMA_EOL
)
10153 message
= catenate_messages (gmsgid
, " before end of line");
10154 else if (token_type
== CPP_DECLTYPE
)
10155 message
= catenate_messages (gmsgid
, " before %<decltype%>");
10156 else if (token_type
< N_TTYPES
)
10158 message
= catenate_messages (gmsgid
, " before %qs token");
10159 error (message
, cpp_type2name (token_type
, token_flags
));
10171 #undef catenate_messages
10174 /* Return the gcc option code associated with the reason for a cpp
10175 message, or 0 if none. */
10178 c_option_controlling_cpp_error (int reason
)
10180 const struct cpp_reason_option_codes_t
*entry
;
10182 for (entry
= cpp_reason_option_codes
; entry
->reason
!= CPP_W_NONE
; entry
++)
10184 if (entry
->reason
== reason
)
10185 return entry
->option_code
;
10190 /* Callback from cpp_error for PFILE to print diagnostics from the
10191 preprocessor. The diagnostic is of type LEVEL, with REASON set
10192 to the reason code if LEVEL is represents a warning, at location
10193 LOCATION unless this is after lexing and the compiler's location
10194 should be used instead, with column number possibly overridden by
10195 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10196 the arguments. Returns true if a diagnostic was emitted, false
10200 c_cpp_error (cpp_reader
*pfile ATTRIBUTE_UNUSED
, int level
, int reason
,
10201 location_t location
, unsigned int column_override
,
10202 const char *msg
, va_list *ap
)
10204 diagnostic_info diagnostic
;
10205 diagnostic_t dlevel
;
10206 bool save_warn_system_headers
= global_dc
->dc_warn_system_headers
;
10211 case CPP_DL_WARNING_SYSHDR
:
10212 if (flag_no_output
)
10214 global_dc
->dc_warn_system_headers
= 1;
10215 /* Fall through. */
10216 case CPP_DL_WARNING
:
10217 if (flag_no_output
)
10219 dlevel
= DK_WARNING
;
10221 case CPP_DL_PEDWARN
:
10222 if (flag_no_output
&& !flag_pedantic_errors
)
10224 dlevel
= DK_PEDWARN
;
10239 gcc_unreachable ();
10242 location
= input_location
;
10243 diagnostic_set_info_translated (&diagnostic
, msg
, ap
,
10245 if (column_override
)
10246 diagnostic_override_column (&diagnostic
, column_override
);
10247 diagnostic_override_option_index (&diagnostic
,
10248 c_option_controlling_cpp_error (reason
));
10249 ret
= report_diagnostic (&diagnostic
);
10250 if (level
== CPP_DL_WARNING_SYSHDR
)
10251 global_dc
->dc_warn_system_headers
= save_warn_system_headers
;
10255 /* Convert a character from the host to the target execution character
10256 set. cpplib handles this, mostly. */
10259 c_common_to_target_charset (HOST_WIDE_INT c
)
10261 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10262 zero-extended under -fno-signed-char. cpplib insists that characters
10263 and character constants are always unsigned. Hence we must convert
10265 cppchar_t uc
= ((cppchar_t
)c
) & ((((cppchar_t
)1) << CHAR_BIT
)-1);
10267 uc
= cpp_host_to_exec_charset (parse_in
, uc
);
10269 if (flag_signed_char
)
10270 return ((HOST_WIDE_INT
)uc
) << (HOST_BITS_PER_WIDE_INT
- CHAR_TYPE_SIZE
)
10271 >> (HOST_BITS_PER_WIDE_INT
- CHAR_TYPE_SIZE
);
10276 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10277 references with an INDIRECT_REF of a constant at the bottom; much like the
10278 traditional rendering of offsetof as a macro. Return the folded result. */
10281 fold_offsetof_1 (tree expr
)
10285 switch (TREE_CODE (expr
))
10291 error ("cannot apply %<offsetof%> to static data member %qD", expr
);
10292 return error_mark_node
;
10296 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10297 return error_mark_node
;
10301 if (!TREE_CONSTANT (TREE_OPERAND (expr
, 0)))
10303 error ("cannot apply %<offsetof%> to a non constant address");
10304 return error_mark_node
;
10306 return TREE_OPERAND (expr
, 0);
10308 case COMPONENT_REF
:
10309 base
= fold_offsetof_1 (TREE_OPERAND (expr
, 0));
10310 if (base
== error_mark_node
)
10313 t
= TREE_OPERAND (expr
, 1);
10314 if (DECL_C_BIT_FIELD (t
))
10316 error ("attempt to take address of bit-field structure "
10318 return error_mark_node
;
10320 off
= size_binop_loc (input_location
, PLUS_EXPR
, DECL_FIELD_OFFSET (t
),
10321 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t
))
10326 base
= fold_offsetof_1 (TREE_OPERAND (expr
, 0));
10327 if (base
== error_mark_node
)
10330 t
= TREE_OPERAND (expr
, 1);
10332 /* Check if the offset goes beyond the upper bound of the array. */
10333 if (TREE_CODE (t
) == INTEGER_CST
&& tree_int_cst_sgn (t
) >= 0)
10335 tree upbound
= array_ref_up_bound (expr
);
10336 if (upbound
!= NULL_TREE
10337 && TREE_CODE (upbound
) == INTEGER_CST
10338 && !tree_int_cst_equal (upbound
,
10339 TYPE_MAX_VALUE (TREE_TYPE (upbound
))))
10341 upbound
= size_binop (PLUS_EXPR
, upbound
,
10342 build_int_cst (TREE_TYPE (upbound
), 1));
10343 if (tree_int_cst_lt (upbound
, t
))
10347 for (v
= TREE_OPERAND (expr
, 0);
10348 TREE_CODE (v
) == COMPONENT_REF
;
10349 v
= TREE_OPERAND (v
, 0))
10350 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
10353 tree fld_chain
= DECL_CHAIN (TREE_OPERAND (v
, 1));
10354 for (; fld_chain
; fld_chain
= DECL_CHAIN (fld_chain
))
10355 if (TREE_CODE (fld_chain
) == FIELD_DECL
)
10361 /* Don't warn if the array might be considered a poor
10362 man's flexible array member with a very permissive
10363 definition thereof. */
10364 if (TREE_CODE (v
) == ARRAY_REF
10365 || TREE_CODE (v
) == COMPONENT_REF
)
10366 warning (OPT_Warray_bounds
,
10367 "index %E denotes an offset "
10368 "greater than size of %qT",
10369 t
, TREE_TYPE (TREE_OPERAND (expr
, 0)));
10374 t
= convert (sizetype
, t
);
10375 off
= size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (TREE_TYPE (expr
)), t
);
10378 case COMPOUND_EXPR
:
10379 /* Handle static members of volatile structs. */
10380 t
= TREE_OPERAND (expr
, 1);
10381 gcc_assert (TREE_CODE (t
) == VAR_DECL
);
10382 return fold_offsetof_1 (t
);
10385 gcc_unreachable ();
10388 return fold_build_pointer_plus (base
, off
);
10391 /* Likewise, but convert it to the return type of offsetof. */
10394 fold_offsetof (tree expr
)
10396 return convert (size_type_node
, fold_offsetof_1 (expr
));
10399 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10400 expression, because B will always be true. */
10403 warn_for_omitted_condop (location_t location
, tree cond
)
10405 if (truth_value_p (TREE_CODE (cond
)))
10406 warning_at (location
, OPT_Wparentheses
,
10407 "the omitted middle operand in ?: will always be %<true%>, "
10408 "suggest explicit middle operand");
10411 /* Give an error for storing into ARG, which is 'const'. USE indicates
10412 how ARG was being used. */
10415 readonly_error (location_t loc
, tree arg
, enum lvalue_use use
)
10417 gcc_assert (use
== lv_assign
|| use
== lv_increment
|| use
== lv_decrement
10419 /* Using this macro rather than (for example) arrays of messages
10420 ensures that all the format strings are checked at compile
10422 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10423 : (use == lv_increment ? (I) \
10424 : (use == lv_decrement ? (D) : (AS))))
10425 if (TREE_CODE (arg
) == COMPONENT_REF
)
10427 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
10428 error_at (loc
, READONLY_MSG (G_("assignment of member "
10429 "%qD in read-only object"),
10430 G_("increment of member "
10431 "%qD in read-only object"),
10432 G_("decrement of member "
10433 "%qD in read-only object"),
10434 G_("member %qD in read-only object "
10435 "used as %<asm%> output")),
10436 TREE_OPERAND (arg
, 1));
10438 error_at (loc
, READONLY_MSG (G_("assignment of read-only member %qD"),
10439 G_("increment of read-only member %qD"),
10440 G_("decrement of read-only member %qD"),
10441 G_("read-only member %qD used as %<asm%> output")),
10442 TREE_OPERAND (arg
, 1));
10444 else if (TREE_CODE (arg
) == VAR_DECL
)
10445 error_at (loc
, READONLY_MSG (G_("assignment of read-only variable %qD"),
10446 G_("increment of read-only variable %qD"),
10447 G_("decrement of read-only variable %qD"),
10448 G_("read-only variable %qD used as %<asm%> output")),
10450 else if (TREE_CODE (arg
) == PARM_DECL
)
10451 error_at (loc
, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10452 G_("increment of read-only parameter %qD"),
10453 G_("decrement of read-only parameter %qD"),
10454 G_("read-only parameter %qD use as %<asm%> output")),
10456 else if (TREE_CODE (arg
) == RESULT_DECL
)
10458 gcc_assert (c_dialect_cxx ());
10459 error_at (loc
, READONLY_MSG (G_("assignment of "
10460 "read-only named return value %qD"),
10462 "read-only named return value %qD"),
10464 "read-only named return value %qD"),
10465 G_("read-only named return value %qD "
10466 "used as %<asm%>output")),
10469 else if (TREE_CODE (arg
) == FUNCTION_DECL
)
10470 error_at (loc
, READONLY_MSG (G_("assignment of function %qD"),
10471 G_("increment of function %qD"),
10472 G_("decrement of function %qD"),
10473 G_("function %qD used as %<asm%> output")),
10476 error_at (loc
, READONLY_MSG (G_("assignment of read-only location %qE"),
10477 G_("increment of read-only location %qE"),
10478 G_("decrement of read-only location %qE"),
10479 G_("read-only location %qE used as %<asm%> output")),
10483 /* Print an error message for an invalid lvalue. USE says
10484 how the lvalue is being used and so selects the error message. LOC
10485 is the location for the error. */
10488 lvalue_error (location_t loc
, enum lvalue_use use
)
10493 error_at (loc
, "lvalue required as left operand of assignment");
10496 error_at (loc
, "lvalue required as increment operand");
10499 error_at (loc
, "lvalue required as decrement operand");
10502 error_at (loc
, "lvalue required as unary %<&%> operand");
10505 error_at (loc
, "lvalue required in asm statement");
10508 gcc_unreachable ();
10512 /* Print an error message for an invalid indirection of type TYPE.
10513 ERRSTRING is the name of the operator for the indirection. */
10516 invalid_indirection_error (location_t loc
, tree type
, ref_operator errstring
)
10521 gcc_assert (c_dialect_cxx ());
10522 error_at (loc
, "invalid type argument (have %qT)", type
);
10524 case RO_ARRAY_INDEXING
:
10526 "invalid type argument of array indexing (have %qT)",
10529 case RO_UNARY_STAR
:
10531 "invalid type argument of unary %<*%> (have %qT)",
10536 "invalid type argument of %<->%> (have %qT)",
10539 case RO_ARROW_STAR
:
10541 "invalid type argument of %<->*%> (have %qT)",
10544 case RO_IMPLICIT_CONVERSION
:
10546 "invalid type argument of implicit conversion (have %qT)",
10550 gcc_unreachable ();
10554 /* *PTYPE is an incomplete array. Complete it with a domain based on
10555 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10556 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10557 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10560 complete_array_type (tree
*ptype
, tree initial_value
, bool do_default
)
10562 tree maxindex
, type
, main_type
, elt
, unqual_elt
;
10563 int failure
= 0, quals
;
10564 hashval_t hashcode
= 0;
10565 bool overflow_p
= false;
10567 maxindex
= size_zero_node
;
10570 if (TREE_CODE (initial_value
) == STRING_CST
)
10573 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value
)));
10574 maxindex
= size_int (TREE_STRING_LENGTH (initial_value
)/eltsize
- 1);
10576 else if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
10578 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initial_value
);
10580 if (vec_safe_is_empty (v
))
10584 maxindex
= ssize_int (-1);
10589 unsigned HOST_WIDE_INT cnt
;
10590 constructor_elt
*ce
;
10591 bool fold_p
= false;
10594 maxindex
= (*v
)[0].index
, fold_p
= true;
10596 curindex
= maxindex
;
10598 for (cnt
= 1; vec_safe_iterate (v
, cnt
, &ce
); cnt
++)
10600 bool curfold_p
= false;
10602 curindex
= ce
->index
, curfold_p
= true;
10607 /* Since we treat size types now as ordinary
10608 unsigned types, we need an explicit overflow
10610 tree orig
= curindex
;
10611 curindex
= fold_convert (sizetype
, curindex
);
10612 overflow_p
|= tree_int_cst_lt (curindex
, orig
);
10614 curindex
= size_binop (PLUS_EXPR
, curindex
,
10617 if (tree_int_cst_lt (maxindex
, curindex
))
10618 maxindex
= curindex
, fold_p
= curfold_p
;
10622 tree orig
= maxindex
;
10623 maxindex
= fold_convert (sizetype
, maxindex
);
10624 overflow_p
|= tree_int_cst_lt (maxindex
, orig
);
10630 /* Make an error message unless that happened already. */
10631 if (initial_value
!= error_mark_node
)
10643 elt
= TREE_TYPE (type
);
10644 quals
= TYPE_QUALS (strip_array_types (elt
));
10648 unqual_elt
= c_build_qualified_type (elt
, KEEP_QUAL_ADDR_SPACE (quals
));
10650 /* Using build_distinct_type_copy and modifying things afterward instead
10651 of using build_array_type to create a new type preserves all of the
10652 TYPE_LANG_FLAG_? bits that the front end may have set. */
10653 main_type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
10654 TREE_TYPE (main_type
) = unqual_elt
;
10655 TYPE_DOMAIN (main_type
)
10656 = build_range_type (TREE_TYPE (maxindex
),
10657 build_int_cst (TREE_TYPE (maxindex
), 0), maxindex
);
10658 layout_type (main_type
);
10660 /* Make sure we have the canonical MAIN_TYPE. */
10661 hashcode
= iterative_hash_object (TYPE_HASH (unqual_elt
), hashcode
);
10662 hashcode
= iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type
)),
10664 main_type
= type_hash_canon (hashcode
, main_type
);
10666 /* Fix the canonical type. */
10667 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type
))
10668 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type
)))
10669 SET_TYPE_STRUCTURAL_EQUALITY (main_type
);
10670 else if (TYPE_CANONICAL (TREE_TYPE (main_type
)) != TREE_TYPE (main_type
)
10671 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type
))
10672 != TYPE_DOMAIN (main_type
)))
10673 TYPE_CANONICAL (main_type
)
10674 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type
)),
10675 TYPE_CANONICAL (TYPE_DOMAIN (main_type
)));
10677 TYPE_CANONICAL (main_type
) = main_type
;
10682 type
= c_build_qualified_type (main_type
, quals
);
10684 if (COMPLETE_TYPE_P (type
)
10685 && TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
10686 && (overflow_p
|| TREE_OVERFLOW (TYPE_SIZE_UNIT (type
))))
10688 error ("size of array is too large");
10689 /* If we proceed with the array type as it is, we'll eventually
10690 crash in tree_to_[su]hwi(). */
10691 type
= error_mark_node
;
10698 /* Like c_mark_addressable but don't check register qualifier. */
10700 c_common_mark_addressable_vec (tree t
)
10702 while (handled_component_p (t
))
10703 t
= TREE_OPERAND (t
, 0);
10704 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
10706 TREE_ADDRESSABLE (t
) = 1;
10711 /* Used to help initialize the builtin-types.def table. When a type of
10712 the correct size doesn't exist, use error_mark_node instead of NULL.
10713 The later results in segfaults even when a decl using the type doesn't
10717 builtin_type_for_size (int size
, bool unsignedp
)
10719 tree type
= c_common_type_for_size (size
, unsignedp
);
10720 return type
? type
: error_mark_node
;
10723 /* A helper function for resolve_overloaded_builtin in resolving the
10724 overloaded __sync_ builtins. Returns a positive power of 2 if the
10725 first operand of PARAMS is a pointer to a supported data type.
10726 Returns 0 if an error is encountered. */
10729 sync_resolve_size (tree function
, vec
<tree
, va_gc
> *params
)
10736 error ("too few arguments to function %qE", function
);
10740 type
= TREE_TYPE ((*params
)[0]);
10741 if (TREE_CODE (type
) == ARRAY_TYPE
)
10743 /* Force array-to-pointer decay for C++. */
10744 gcc_assert (c_dialect_cxx());
10745 (*params
)[0] = default_conversion ((*params
)[0]);
10746 type
= TREE_TYPE ((*params
)[0]);
10748 if (TREE_CODE (type
) != POINTER_TYPE
)
10751 type
= TREE_TYPE (type
);
10752 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
10755 size
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
10756 if (size
== 1 || size
== 2 || size
== 4 || size
== 8 || size
== 16)
10760 error ("incompatible type for argument %d of %qE", 1, function
);
10764 /* A helper function for resolve_overloaded_builtin. Adds casts to
10765 PARAMS to make arguments match up with those of FUNCTION. Drops
10766 the variadic arguments at the end. Returns false if some error
10767 was encountered; true on success. */
10770 sync_resolve_params (location_t loc
, tree orig_function
, tree function
,
10771 vec
<tree
, va_gc
> *params
, bool orig_format
)
10773 function_args_iterator iter
;
10775 unsigned int parmnum
;
10777 function_args_iter_init (&iter
, TREE_TYPE (function
));
10778 /* We've declared the implementation functions to use "volatile void *"
10779 as the pointer parameter, so we shouldn't get any complaints from the
10780 call to check_function_arguments what ever type the user used. */
10781 function_args_iter_next (&iter
);
10782 ptype
= TREE_TYPE (TREE_TYPE ((*params
)[0]));
10783 ptype
= TYPE_MAIN_VARIANT (ptype
);
10785 /* For the rest of the values, we need to cast these to FTYPE, so that we
10786 don't get warnings for passing pointer types, etc. */
10790 tree val
, arg_type
;
10792 arg_type
= function_args_iter_cond (&iter
);
10793 /* XXX void_type_node belies the abstraction. */
10794 if (arg_type
== void_type_node
)
10798 if (params
->length () <= parmnum
)
10800 error_at (loc
, "too few arguments to function %qE", orig_function
);
10804 /* Only convert parameters if arg_type is unsigned integer type with
10805 new format sync routines, i.e. don't attempt to convert pointer
10806 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10807 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10809 if (TREE_CODE (arg_type
) == INTEGER_TYPE
&& TYPE_UNSIGNED (arg_type
))
10811 /* Ideally for the first conversion we'd use convert_for_assignment
10812 so that we get warnings for anything that doesn't match the pointer
10813 type. This isn't portable across the C and C++ front ends atm. */
10814 val
= (*params
)[parmnum
];
10815 val
= convert (ptype
, val
);
10816 val
= convert (arg_type
, val
);
10817 (*params
)[parmnum
] = val
;
10820 function_args_iter_next (&iter
);
10823 /* __atomic routines are not variadic. */
10824 if (!orig_format
&& params
->length () != parmnum
+ 1)
10826 error_at (loc
, "too many arguments to function %qE", orig_function
);
10830 /* The definition of these primitives is variadic, with the remaining
10831 being "an optional list of variables protected by the memory barrier".
10832 No clue what that's supposed to mean, precisely, but we consider all
10833 call-clobbered variables to be protected so we're safe. */
10834 params
->truncate (parmnum
+ 1);
10839 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10840 RESULT to make it match the type of the first pointer argument in
10844 sync_resolve_return (tree first_param
, tree result
, bool orig_format
)
10846 tree ptype
= TREE_TYPE (TREE_TYPE (first_param
));
10847 tree rtype
= TREE_TYPE (result
);
10848 ptype
= TYPE_MAIN_VARIANT (ptype
);
10850 /* New format doesn't require casting unless the types are the same size. */
10851 if (orig_format
|| tree_int_cst_equal (TYPE_SIZE (ptype
), TYPE_SIZE (rtype
)))
10852 return convert (ptype
, result
);
10857 /* This function verifies the PARAMS to generic atomic FUNCTION.
10858 It returns the size if all the parameters are the same size, otherwise
10859 0 is returned if the parameters are invalid. */
10862 get_atomic_generic_size (location_t loc
, tree function
,
10863 vec
<tree
, va_gc
> *params
)
10865 unsigned int n_param
;
10866 unsigned int n_model
;
10871 /* Determine the parameter makeup. */
10872 switch (DECL_FUNCTION_CODE (function
))
10874 case BUILT_IN_ATOMIC_EXCHANGE
:
10878 case BUILT_IN_ATOMIC_LOAD
:
10879 case BUILT_IN_ATOMIC_STORE
:
10883 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
10888 gcc_unreachable ();
10891 if (vec_safe_length (params
) != n_param
)
10893 error_at (loc
, "incorrect number of arguments to function %qE", function
);
10897 /* Get type of first parameter, and determine its size. */
10898 type_0
= TREE_TYPE ((*params
)[0]);
10899 if (TREE_CODE (type_0
) == ARRAY_TYPE
)
10901 /* Force array-to-pointer decay for C++. */
10902 gcc_assert (c_dialect_cxx());
10903 (*params
)[0] = default_conversion ((*params
)[0]);
10904 type_0
= TREE_TYPE ((*params
)[0]);
10906 if (TREE_CODE (type_0
) != POINTER_TYPE
|| VOID_TYPE_P (TREE_TYPE (type_0
)))
10908 error_at (loc
, "argument 1 of %qE must be a non-void pointer type",
10913 /* Types must be compile time constant sizes. */
10914 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0
)))) != INTEGER_CST
)
10917 "argument 1 of %qE must be a pointer to a constant size type",
10922 size_0
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0
)));
10924 /* Zero size objects are not allowed. */
10928 "argument 1 of %qE must be a pointer to a nonzero size object",
10933 /* Check each other parameter is a pointer and the same size. */
10934 for (x
= 0; x
< n_param
- n_model
; x
++)
10937 tree type
= TREE_TYPE ((*params
)[x
]);
10938 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10939 if (n_param
== 6 && x
== 3)
10941 if (!POINTER_TYPE_P (type
))
10943 error_at (loc
, "argument %d of %qE must be a pointer type", x
+ 1,
10947 tree type_size
= TYPE_SIZE_UNIT (TREE_TYPE (type
));
10948 size
= type_size
? tree_to_uhwi (type_size
) : 0;
10949 if (size
!= size_0
)
10951 error_at (loc
, "size mismatch in argument %d of %qE", x
+ 1,
10957 /* Check memory model parameters for validity. */
10958 for (x
= n_param
- n_model
; x
< n_param
; x
++)
10960 tree p
= (*params
)[x
];
10961 if (TREE_CODE (p
) == INTEGER_CST
)
10963 int i
= tree_to_uhwi (p
);
10964 if (i
< 0 || (memmodel_base (i
) >= MEMMODEL_LAST
))
10966 warning_at (loc
, OPT_Winvalid_memory_model
,
10967 "invalid memory model argument %d of %qE", x
+ 1,
10972 if (!INTEGRAL_TYPE_P (TREE_TYPE (p
)))
10974 error_at (loc
, "non-integer memory model argument %d of %qE", x
+ 1,
10984 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10985 at the beginning of the parameter list PARAMS representing the size of the
10986 objects. This is to match the library ABI requirement. LOC is the location
10987 of the function call.
10988 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10989 returned to allow the external call to be constructed. */
10992 add_atomic_size_parameter (unsigned n
, location_t loc
, tree function
,
10993 vec
<tree
, va_gc
> *params
)
10997 /* Insert a SIZE_T parameter as the first param. If there isn't
10998 enough space, allocate a new vector and recursively re-build with that. */
10999 if (!params
->space (1))
11001 unsigned int z
, len
;
11002 vec
<tree
, va_gc
> *v
;
11005 len
= params
->length ();
11006 vec_alloc (v
, len
+ 1);
11007 v
->quick_push (build_int_cst (size_type_node
, n
));
11008 for (z
= 0; z
< len
; z
++)
11009 v
->quick_push ((*params
)[z
]);
11010 f
= build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
11015 /* Add the size parameter and leave as a function call for processing. */
11016 size_node
= build_int_cst (size_type_node
, n
);
11017 params
->quick_insert (0, size_node
);
11022 /* Return whether atomic operations for naturally aligned N-byte
11023 arguments are supported, whether inline or through libatomic. */
11025 atomic_size_supported_p (int n
)
11036 return targetm
.scalar_mode_supported_p (TImode
);
11043 /* This will process an __atomic_exchange function call, determine whether it
11044 needs to be mapped to the _N variation, or turned into a library call.
11045 LOC is the location of the builtin call.
11046 FUNCTION is the DECL that has been invoked;
11047 PARAMS is the argument list for the call. The return value is non-null
11048 TRUE is returned if it is translated into the proper format for a call to the
11049 external library, and NEW_RETURN is set the tree for that function.
11050 FALSE is returned if processing for the _N variation is required, and
11051 NEW_RETURN is set to the the return value the result is copied into. */
11053 resolve_overloaded_atomic_exchange (location_t loc
, tree function
,
11054 vec
<tree
, va_gc
> *params
, tree
*new_return
)
11056 tree p0
, p1
, p2
, p3
;
11057 tree I_type
, I_type_ptr
;
11058 int n
= get_atomic_generic_size (loc
, function
, params
);
11060 /* Size of 0 is an error condition. */
11063 *new_return
= error_mark_node
;
11067 /* If not a lock-free size, change to the library generic format. */
11068 if (!atomic_size_supported_p (n
))
11070 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
11074 /* Otherwise there is a lockfree match, transform the call from:
11075 void fn(T* mem, T* desired, T* return, model)
11077 *return = (T) (fn (In* mem, (In) *desired, model)) */
11084 /* Create pointer to appropriate size. */
11085 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
11086 I_type_ptr
= build_pointer_type (I_type
);
11088 /* Convert object pointer to required type. */
11089 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
11091 /* Convert new value to required type, and dereference it. */
11092 p1
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
11093 p1
= build1 (VIEW_CONVERT_EXPR
, I_type
, p1
);
11096 /* Move memory model to the 3rd position, and end param list. */
11098 params
->truncate (3);
11100 /* Convert return pointer and dereference it for later assignment. */
11101 *new_return
= build_indirect_ref (loc
, p2
, RO_UNARY_STAR
);
11107 /* This will process an __atomic_compare_exchange function call, determine
11108 whether it needs to be mapped to the _N variation, or turned into a lib call.
11109 LOC is the location of the builtin call.
11110 FUNCTION is the DECL that has been invoked;
11111 PARAMS is the argument list for the call. The return value is non-null
11112 TRUE is returned if it is translated into the proper format for a call to the
11113 external library, and NEW_RETURN is set the tree for that function.
11114 FALSE is returned if processing for the _N variation is required. */
11117 resolve_overloaded_atomic_compare_exchange (location_t loc
, tree function
,
11118 vec
<tree
, va_gc
> *params
,
11122 tree I_type
, I_type_ptr
;
11123 int n
= get_atomic_generic_size (loc
, function
, params
);
11125 /* Size of 0 is an error condition. */
11128 *new_return
= error_mark_node
;
11132 /* If not a lock-free size, change to the library generic format. */
11133 if (!atomic_size_supported_p (n
))
11135 /* The library generic format does not have the weak parameter, so
11136 remove it from the param list. Since a parameter has been removed,
11137 we can be sure that there is room for the SIZE_T parameter, meaning
11138 there will not be a recursive rebuilding of the parameter list, so
11139 there is no danger this will be done twice. */
11142 (*params
)[3] = (*params
)[4];
11143 (*params
)[4] = (*params
)[5];
11144 params
->truncate (5);
11146 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
11150 /* Otherwise, there is a match, so the call needs to be transformed from:
11151 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11153 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11159 /* Create pointer to appropriate size. */
11160 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
11161 I_type_ptr
= build_pointer_type (I_type
);
11163 /* Convert object pointer to required type. */
11164 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
11167 /* Convert expected pointer to required type. */
11168 p1
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p1
);
11171 /* Convert desired value to required type, and dereference it. */
11172 p2
= build_indirect_ref (loc
, p2
, RO_UNARY_STAR
);
11173 p2
= build1 (VIEW_CONVERT_EXPR
, I_type
, p2
);
11176 /* The rest of the parameters are fine. NULL means no special return value
11178 *new_return
= NULL
;
11183 /* This will process an __atomic_load function call, determine whether it
11184 needs to be mapped to the _N variation, or turned into a library call.
11185 LOC is the location of the builtin call.
11186 FUNCTION is the DECL that has been invoked;
11187 PARAMS is the argument list for the call. The return value is non-null
11188 TRUE is returned if it is translated into the proper format for a call to the
11189 external library, and NEW_RETURN is set the tree for that function.
11190 FALSE is returned if processing for the _N variation is required, and
11191 NEW_RETURN is set to the the return value the result is copied into. */
11194 resolve_overloaded_atomic_load (location_t loc
, tree function
,
11195 vec
<tree
, va_gc
> *params
, tree
*new_return
)
11198 tree I_type
, I_type_ptr
;
11199 int n
= get_atomic_generic_size (loc
, function
, params
);
11201 /* Size of 0 is an error condition. */
11204 *new_return
= error_mark_node
;
11208 /* If not a lock-free size, change to the library generic format. */
11209 if (!atomic_size_supported_p (n
))
11211 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
11215 /* Otherwise, there is a match, so the call needs to be transformed from:
11216 void fn(T* mem, T* return, model)
11218 *return = (T) (fn ((In *) mem, model)) */
11224 /* Create pointer to appropriate size. */
11225 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
11226 I_type_ptr
= build_pointer_type (I_type
);
11228 /* Convert object pointer to required type. */
11229 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
11232 /* Move memory model to the 2nd position, and end param list. */
11234 params
->truncate (2);
11236 /* Convert return pointer and dereference it for later assignment. */
11237 *new_return
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
11243 /* This will process an __atomic_store function call, determine whether it
11244 needs to be mapped to the _N variation, or turned into a library call.
11245 LOC is the location of the builtin call.
11246 FUNCTION is the DECL that has been invoked;
11247 PARAMS is the argument list for the call. The return value is non-null
11248 TRUE is returned if it is translated into the proper format for a call to the
11249 external library, and NEW_RETURN is set the tree for that function.
11250 FALSE is returned if processing for the _N variation is required, and
11251 NEW_RETURN is set to the the return value the result is copied into. */
11254 resolve_overloaded_atomic_store (location_t loc
, tree function
,
11255 vec
<tree
, va_gc
> *params
, tree
*new_return
)
11258 tree I_type
, I_type_ptr
;
11259 int n
= get_atomic_generic_size (loc
, function
, params
);
11261 /* Size of 0 is an error condition. */
11264 *new_return
= error_mark_node
;
11268 /* If not a lock-free size, change to the library generic format. */
11269 if (!atomic_size_supported_p (n
))
11271 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
11275 /* Otherwise, there is a match, so the call needs to be transformed from:
11276 void fn(T* mem, T* value, model)
11278 fn ((In *) mem, (In) *value, model) */
11283 /* Create pointer to appropriate size. */
11284 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
11285 I_type_ptr
= build_pointer_type (I_type
);
11287 /* Convert object pointer to required type. */
11288 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
11291 /* Convert new value to required type, and dereference it. */
11292 p1
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
11293 p1
= build1 (VIEW_CONVERT_EXPR
, I_type
, p1
);
11296 /* The memory model is in the right spot already. Return is void. */
11297 *new_return
= NULL_TREE
;
11303 /* Some builtin functions are placeholders for other expressions. This
11304 function should be called immediately after parsing the call expression
11305 before surrounding code has committed to the type of the expression.
11307 LOC is the location of the builtin call.
11309 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11310 PARAMS is the argument list for the call. The return value is non-null
11311 when expansion is complete, and null if normal processing should
11315 resolve_overloaded_builtin (location_t loc
, tree function
,
11316 vec
<tree
, va_gc
> *params
)
11318 enum built_in_function orig_code
= DECL_FUNCTION_CODE (function
);
11319 bool orig_format
= true;
11320 tree new_return
= NULL_TREE
;
11322 switch (DECL_BUILT_IN_CLASS (function
))
11324 case BUILT_IN_NORMAL
:
11327 if (targetm
.resolve_overloaded_builtin
)
11328 return targetm
.resolve_overloaded_builtin (loc
, function
, params
);
11335 /* Handle BUILT_IN_NORMAL here. */
11338 case BUILT_IN_ATOMIC_EXCHANGE
:
11339 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
11340 case BUILT_IN_ATOMIC_LOAD
:
11341 case BUILT_IN_ATOMIC_STORE
:
11343 /* Handle these 4 together so that they can fall through to the next
11344 case if the call is transformed to an _N variant. */
11347 case BUILT_IN_ATOMIC_EXCHANGE
:
11349 if (resolve_overloaded_atomic_exchange (loc
, function
, params
,
11352 /* Change to the _N variant. */
11353 orig_code
= BUILT_IN_ATOMIC_EXCHANGE_N
;
11357 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
11359 if (resolve_overloaded_atomic_compare_exchange (loc
, function
,
11363 /* Change to the _N variant. */
11364 orig_code
= BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
;
11367 case BUILT_IN_ATOMIC_LOAD
:
11369 if (resolve_overloaded_atomic_load (loc
, function
, params
,
11372 /* Change to the _N variant. */
11373 orig_code
= BUILT_IN_ATOMIC_LOAD_N
;
11376 case BUILT_IN_ATOMIC_STORE
:
11378 if (resolve_overloaded_atomic_store (loc
, function
, params
,
11381 /* Change to the _N variant. */
11382 orig_code
= BUILT_IN_ATOMIC_STORE_N
;
11386 gcc_unreachable ();
11388 /* Fallthrough to the normal processing. */
11390 case BUILT_IN_ATOMIC_EXCHANGE_N
:
11391 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
:
11392 case BUILT_IN_ATOMIC_LOAD_N
:
11393 case BUILT_IN_ATOMIC_STORE_N
:
11394 case BUILT_IN_ATOMIC_ADD_FETCH_N
:
11395 case BUILT_IN_ATOMIC_SUB_FETCH_N
:
11396 case BUILT_IN_ATOMIC_AND_FETCH_N
:
11397 case BUILT_IN_ATOMIC_NAND_FETCH_N
:
11398 case BUILT_IN_ATOMIC_XOR_FETCH_N
:
11399 case BUILT_IN_ATOMIC_OR_FETCH_N
:
11400 case BUILT_IN_ATOMIC_FETCH_ADD_N
:
11401 case BUILT_IN_ATOMIC_FETCH_SUB_N
:
11402 case BUILT_IN_ATOMIC_FETCH_AND_N
:
11403 case BUILT_IN_ATOMIC_FETCH_NAND_N
:
11404 case BUILT_IN_ATOMIC_FETCH_XOR_N
:
11405 case BUILT_IN_ATOMIC_FETCH_OR_N
:
11407 orig_format
= false;
11408 /* Fallthru for parameter processing. */
11410 case BUILT_IN_SYNC_FETCH_AND_ADD_N
:
11411 case BUILT_IN_SYNC_FETCH_AND_SUB_N
:
11412 case BUILT_IN_SYNC_FETCH_AND_OR_N
:
11413 case BUILT_IN_SYNC_FETCH_AND_AND_N
:
11414 case BUILT_IN_SYNC_FETCH_AND_XOR_N
:
11415 case BUILT_IN_SYNC_FETCH_AND_NAND_N
:
11416 case BUILT_IN_SYNC_ADD_AND_FETCH_N
:
11417 case BUILT_IN_SYNC_SUB_AND_FETCH_N
:
11418 case BUILT_IN_SYNC_OR_AND_FETCH_N
:
11419 case BUILT_IN_SYNC_AND_AND_FETCH_N
:
11420 case BUILT_IN_SYNC_XOR_AND_FETCH_N
:
11421 case BUILT_IN_SYNC_NAND_AND_FETCH_N
:
11422 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
:
11423 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
:
11424 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
:
11425 case BUILT_IN_SYNC_LOCK_RELEASE_N
:
11427 int n
= sync_resolve_size (function
, params
);
11428 tree new_function
, first_param
, result
;
11429 enum built_in_function fncode
;
11432 return error_mark_node
;
11434 fncode
= (enum built_in_function
)((int)orig_code
+ exact_log2 (n
) + 1);
11435 new_function
= builtin_decl_explicit (fncode
);
11436 if (!sync_resolve_params (loc
, function
, new_function
, params
,
11438 return error_mark_node
;
11440 first_param
= (*params
)[0];
11441 result
= build_function_call_vec (loc
, vNULL
, new_function
, params
,
11443 if (result
== error_mark_node
)
11445 if (orig_code
!= BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11446 && orig_code
!= BUILT_IN_SYNC_LOCK_RELEASE_N
11447 && orig_code
!= BUILT_IN_ATOMIC_STORE_N
)
11448 result
= sync_resolve_return (first_param
, result
, orig_format
);
11450 /* If new_return is set, assign function to that expr and cast the
11451 result to void since the generic interface returned void. */
11454 /* Cast function result from I{1,2,4,8,16} to the required type. */
11455 result
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (new_return
), result
);
11456 result
= build2 (MODIFY_EXPR
, TREE_TYPE (new_return
), new_return
,
11458 TREE_SIDE_EFFECTS (result
) = 1;
11459 protected_set_expr_location (result
, loc
);
11460 result
= convert (void_type_node
, result
);
11470 /* vector_types_compatible_elements_p is used in type checks of vectors
11471 values used as operands of binary operators. Where it returns true, and
11472 the other checks of the caller succeed (being vector types in he first
11473 place, and matching number of elements), we can just treat the types
11474 as essentially the same.
11475 Contrast with vector_targets_convertible_p, which is used for vector
11476 pointer types, and vector_types_convertible_p, which will allow
11477 language-specific matches under the control of flag_lax_vector_conversions,
11478 and might still require a conversion. */
11479 /* True if vector types T1 and T2 can be inputs to the same binary
11480 operator without conversion.
11481 We don't check the overall vector size here because some of our callers
11482 want to give different error messages when the vectors are compatible
11483 except for the element count. */
11486 vector_types_compatible_elements_p (tree t1
, tree t2
)
11488 bool opaque
= TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
);
11489 t1
= TREE_TYPE (t1
);
11490 t2
= TREE_TYPE (t2
);
11492 enum tree_code c1
= TREE_CODE (t1
), c2
= TREE_CODE (t2
);
11494 gcc_assert ((c1
== INTEGER_TYPE
|| c1
== REAL_TYPE
|| c1
== FIXED_POINT_TYPE
)
11495 && (c2
== INTEGER_TYPE
|| c2
== REAL_TYPE
11496 || c2
== FIXED_POINT_TYPE
));
11498 t1
= c_common_signed_type (t1
);
11499 t2
= c_common_signed_type (t2
);
11500 /* Equality works here because c_common_signed_type uses
11501 TYPE_MAIN_VARIANT. */
11504 if (opaque
&& c1
== c2
11505 && (c1
== INTEGER_TYPE
|| c1
== REAL_TYPE
)
11506 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
))
11511 /* Check for missing format attributes on function pointers. LTYPE is
11512 the new type or left-hand side type. RTYPE is the old type or
11513 right-hand side type. Returns TRUE if LTYPE is missing the desired
11517 check_missing_format_attribute (tree ltype
, tree rtype
)
11519 tree
const ttr
= TREE_TYPE (rtype
), ttl
= TREE_TYPE (ltype
);
11522 for (ra
= TYPE_ATTRIBUTES (ttr
); ra
; ra
= TREE_CHAIN (ra
))
11523 if (is_attribute_p ("format", TREE_PURPOSE (ra
)))
11528 for (la
= TYPE_ATTRIBUTES (ttl
); la
; la
= TREE_CHAIN (la
))
11529 if (is_attribute_p ("format", TREE_PURPOSE (la
)))
11537 /* Subscripting with type char is likely to lose on a machine where
11538 chars are signed. So warn on any machine, but optionally. Don't
11539 warn for unsigned char since that type is safe. Don't warn for
11540 signed char because anyone who uses that must have done so
11541 deliberately. Furthermore, we reduce the false positive load by
11542 warning only for non-constant value of type char. */
11545 warn_array_subscript_with_type_char (location_t loc
, tree index
)
11547 if (TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
11548 && TREE_CODE (index
) != INTEGER_CST
)
11549 warning_at (loc
, OPT_Wchar_subscripts
,
11550 "array subscript has type %<char%>");
11553 /* Implement -Wparentheses for the unexpected C precedence rules, to
11554 cover cases like x + y << z which readers are likely to
11555 misinterpret. We have seen an expression in which CODE is a binary
11556 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11557 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11558 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11559 expression was not formed using a binary or unary operator, or it
11560 was enclosed in parentheses. */
11563 warn_about_parentheses (location_t loc
, enum tree_code code
,
11564 enum tree_code code_left
, tree arg_left
,
11565 enum tree_code code_right
, tree arg_right
)
11567 if (!warn_parentheses
)
11570 /* This macro tests that the expression ARG with original tree code
11571 CODE appears to be a boolean expression. or the result of folding a
11572 boolean expression. */
11573 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11574 (truth_value_p (TREE_CODE (ARG)) \
11575 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11576 /* Folding may create 0 or 1 integers from other expressions. */ \
11577 || ((CODE) != INTEGER_CST \
11578 && (integer_onep (ARG) || integer_zerop (ARG))))
11583 if (code_left
== PLUS_EXPR
)
11584 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11585 "suggest parentheses around %<+%> inside %<<<%>");
11586 else if (code_right
== PLUS_EXPR
)
11587 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11588 "suggest parentheses around %<+%> inside %<<<%>");
11589 else if (code_left
== MINUS_EXPR
)
11590 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11591 "suggest parentheses around %<-%> inside %<<<%>");
11592 else if (code_right
== MINUS_EXPR
)
11593 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11594 "suggest parentheses around %<-%> inside %<<<%>");
11598 if (code_left
== PLUS_EXPR
)
11599 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11600 "suggest parentheses around %<+%> inside %<>>%>");
11601 else if (code_right
== PLUS_EXPR
)
11602 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11603 "suggest parentheses around %<+%> inside %<>>%>");
11604 else if (code_left
== MINUS_EXPR
)
11605 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11606 "suggest parentheses around %<-%> inside %<>>%>");
11607 else if (code_right
== MINUS_EXPR
)
11608 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11609 "suggest parentheses around %<-%> inside %<>>%>");
11612 case TRUTH_ORIF_EXPR
:
11613 if (code_left
== TRUTH_ANDIF_EXPR
)
11614 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11615 "suggest parentheses around %<&&%> within %<||%>");
11616 else if (code_right
== TRUTH_ANDIF_EXPR
)
11617 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11618 "suggest parentheses around %<&&%> within %<||%>");
11622 if (code_left
== BIT_AND_EXPR
|| code_left
== BIT_XOR_EXPR
11623 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
11624 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11625 "suggest parentheses around arithmetic in operand of %<|%>");
11626 else if (code_right
== BIT_AND_EXPR
|| code_right
== BIT_XOR_EXPR
11627 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
11628 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11629 "suggest parentheses around arithmetic in operand of %<|%>");
11630 /* Check cases like x|y==z */
11631 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
11632 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11633 "suggest parentheses around comparison in operand of %<|%>");
11634 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
11635 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11636 "suggest parentheses around comparison in operand of %<|%>");
11637 /* Check cases like !x | y */
11638 else if (code_left
== TRUTH_NOT_EXPR
11639 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
11640 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11641 "suggest parentheses around operand of "
11642 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11646 if (code_left
== BIT_AND_EXPR
11647 || code_left
== PLUS_EXPR
|| code_left
== MINUS_EXPR
)
11648 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11649 "suggest parentheses around arithmetic in operand of %<^%>");
11650 else if (code_right
== BIT_AND_EXPR
11651 || code_right
== PLUS_EXPR
|| code_right
== MINUS_EXPR
)
11652 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11653 "suggest parentheses around arithmetic in operand of %<^%>");
11654 /* Check cases like x^y==z */
11655 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
11656 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11657 "suggest parentheses around comparison in operand of %<^%>");
11658 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
11659 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11660 "suggest parentheses around comparison in operand of %<^%>");
11664 if (code_left
== PLUS_EXPR
)
11665 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11666 "suggest parentheses around %<+%> in operand of %<&%>");
11667 else if (code_right
== PLUS_EXPR
)
11668 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11669 "suggest parentheses around %<+%> in operand of %<&%>");
11670 else if (code_left
== MINUS_EXPR
)
11671 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11672 "suggest parentheses around %<-%> in operand of %<&%>");
11673 else if (code_right
== MINUS_EXPR
)
11674 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11675 "suggest parentheses around %<-%> in operand of %<&%>");
11676 /* Check cases like x&y==z */
11677 else if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
11678 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11679 "suggest parentheses around comparison in operand of %<&%>");
11680 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
11681 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11682 "suggest parentheses around comparison in operand of %<&%>");
11683 /* Check cases like !x & y */
11684 else if (code_left
== TRUTH_NOT_EXPR
11685 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right
, arg_right
))
11686 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11687 "suggest parentheses around operand of "
11688 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11692 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
11693 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11694 "suggest parentheses around comparison in operand of %<==%>");
11695 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
11696 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11697 "suggest parentheses around comparison in operand of %<==%>");
11700 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
)
11701 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11702 "suggest parentheses around comparison in operand of %<!=%>");
11703 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
)
11704 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11705 "suggest parentheses around comparison in operand of %<!=%>");
11709 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
11711 if (TREE_CODE_CLASS (code_left
) == tcc_comparison
11712 && code_left
!= NE_EXPR
&& code_left
!= EQ_EXPR
11713 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left
)))
11714 warning_at (EXPR_LOC_OR_LOC (arg_left
, loc
), OPT_Wparentheses
,
11715 "comparisons like %<X<=Y<=Z%> do not "
11716 "have their mathematical meaning");
11717 else if (TREE_CODE_CLASS (code_right
) == tcc_comparison
11718 && code_right
!= NE_EXPR
&& code_right
!= EQ_EXPR
11719 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right
)))
11720 warning_at (EXPR_LOC_OR_LOC (arg_right
, loc
), OPT_Wparentheses
,
11721 "comparisons like %<X<=Y<=Z%> do not "
11722 "have their mathematical meaning");
11726 #undef NOT_A_BOOLEAN_EXPR_P
11729 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11732 warn_for_unused_label (tree label
)
11734 if (!TREE_USED (label
))
11736 if (DECL_INITIAL (label
))
11737 warning (OPT_Wunused_label
, "label %q+D defined but not used", label
);
11739 warning (OPT_Wunused_label
, "label %q+D declared but not defined", label
);
11743 /* Warn for division by zero according to the value of DIVISOR. LOC
11744 is the location of the division operator. */
11747 warn_for_div_by_zero (location_t loc
, tree divisor
)
11749 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11750 about division by zero. Do not issue a warning if DIVISOR has a
11751 floating-point type, since we consider 0.0/0.0 a valid way of
11752 generating a NaN. */
11753 if (c_inhibit_evaluation_warnings
== 0
11754 && (integer_zerop (divisor
) || fixed_zerop (divisor
)))
11755 warning_at (loc
, OPT_Wdiv_by_zero
, "division by zero");
11758 /* Subroutine of build_binary_op. Give warnings for comparisons
11759 between signed and unsigned quantities that may fail. Do the
11760 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11761 so that casts will be considered, but default promotions won't
11764 LOCATION is the location of the comparison operator.
11766 The arguments of this function map directly to local variables
11767 of build_binary_op. */
11770 warn_for_sign_compare (location_t location
,
11771 tree orig_op0
, tree orig_op1
,
11772 tree op0
, tree op1
,
11773 tree result_type
, enum tree_code resultcode
)
11775 int op0_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
11776 int op1_signed
= !TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
11777 int unsignedp0
, unsignedp1
;
11779 /* In C++, check for comparison of different enum types. */
11780 if (c_dialect_cxx()
11781 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
11782 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
11783 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
11784 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
11786 warning_at (location
,
11787 OPT_Wsign_compare
, "comparison between types %qT and %qT",
11788 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
11791 /* Do not warn if the comparison is being done in a signed type,
11792 since the signed type will only be chosen if it can represent
11793 all the values of the unsigned type. */
11794 if (!TYPE_UNSIGNED (result_type
))
11796 /* Do not warn if both operands are unsigned. */
11797 else if (op0_signed
== op1_signed
)
11801 tree sop
, uop
, base_type
;
11805 sop
= orig_op0
, uop
= orig_op1
;
11807 sop
= orig_op1
, uop
= orig_op0
;
11809 STRIP_TYPE_NOPS (sop
);
11810 STRIP_TYPE_NOPS (uop
);
11811 base_type
= (TREE_CODE (result_type
) == COMPLEX_TYPE
11812 ? TREE_TYPE (result_type
) : result_type
);
11814 /* Do not warn if the signed quantity is an unsuffixed integer
11815 literal (or some static constant expression involving such
11816 literals or a conditional expression involving such literals)
11817 and it is non-negative. */
11818 if (tree_expr_nonnegative_warnv_p (sop
, &ovf
))
11820 /* Do not warn if the comparison is an equality operation, the
11821 unsigned quantity is an integral constant, and it would fit
11822 in the result if the result were signed. */
11823 else if (TREE_CODE (uop
) == INTEGER_CST
11824 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
11825 && int_fits_type_p (uop
, c_common_signed_type (base_type
)))
11827 /* In C, do not warn if the unsigned quantity is an enumeration
11828 constant and its maximum value would fit in the result if the
11829 result were signed. */
11830 else if (!c_dialect_cxx() && TREE_CODE (uop
) == INTEGER_CST
11831 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
11832 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop
)),
11833 c_common_signed_type (base_type
)))
11836 warning_at (location
,
11838 "comparison between signed and unsigned integer expressions");
11841 /* Warn if two unsigned values are being compared in a size larger
11842 than their original size, and one (and only one) is the result of
11843 a `~' operator. This comparison will always fail.
11845 Also warn if one operand is a constant, and the constant does not
11846 have all bits set that are set in the ~ operand when it is
11849 op0
= c_common_get_narrower (op0
, &unsignedp0
);
11850 op1
= c_common_get_narrower (op1
, &unsignedp1
);
11852 if ((TREE_CODE (op0
) == BIT_NOT_EXPR
)
11853 ^ (TREE_CODE (op1
) == BIT_NOT_EXPR
))
11855 if (TREE_CODE (op0
) == BIT_NOT_EXPR
)
11856 op0
= c_common_get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
11857 if (TREE_CODE (op1
) == BIT_NOT_EXPR
)
11858 op1
= c_common_get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
11860 if (tree_fits_shwi_p (op0
) || tree_fits_shwi_p (op1
))
11863 HOST_WIDE_INT constant
, mask
;
11867 if (tree_fits_shwi_p (op0
))
11870 unsignedp
= unsignedp1
;
11871 constant
= tree_to_shwi (op0
);
11876 unsignedp
= unsignedp0
;
11877 constant
= tree_to_shwi (op1
);
11880 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
11881 if (bits
< TYPE_PRECISION (result_type
)
11882 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
11884 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
11885 if ((mask
& constant
) != mask
)
11888 warning_at (location
, OPT_Wsign_compare
,
11889 "promoted ~unsigned is always non-zero");
11891 warning_at (location
, OPT_Wsign_compare
,
11892 "comparison of promoted ~unsigned with constant");
11896 else if (unsignedp0
&& unsignedp1
11897 && (TYPE_PRECISION (TREE_TYPE (op0
))
11898 < TYPE_PRECISION (result_type
))
11899 && (TYPE_PRECISION (TREE_TYPE (op1
))
11900 < TYPE_PRECISION (result_type
)))
11901 warning_at (location
, OPT_Wsign_compare
,
11902 "comparison of promoted ~unsigned with unsigned");
11906 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11907 type via c_common_type. If -Wdouble-promotion is in use, and the
11908 conditions for warning have been met, issue a warning. GMSGID is
11909 the warning message. It must have two %T specifiers for the type
11910 that was converted (generally "float") and the type to which it was
11911 converted (generally "double), respectively. LOC is the location
11912 to which the awrning should refer. */
11915 do_warn_double_promotion (tree result_type
, tree type1
, tree type2
,
11916 const char *gmsgid
, location_t loc
)
11920 if (!warn_double_promotion
)
11922 /* If the conversion will not occur at run-time, there is no need to
11924 if (c_inhibit_evaluation_warnings
)
11926 if (TYPE_MAIN_VARIANT (result_type
) != double_type_node
11927 && TYPE_MAIN_VARIANT (result_type
) != complex_double_type_node
)
11929 if (TYPE_MAIN_VARIANT (type1
) == float_type_node
11930 || TYPE_MAIN_VARIANT (type1
) == complex_float_type_node
)
11931 source_type
= type1
;
11932 else if (TYPE_MAIN_VARIANT (type2
) == float_type_node
11933 || TYPE_MAIN_VARIANT (type2
) == complex_float_type_node
)
11934 source_type
= type2
;
11937 warning_at (loc
, OPT_Wdouble_promotion
, gmsgid
, source_type
, result_type
);
11940 /* Setup a TYPE_DECL node as a typedef representation.
11942 X is a TYPE_DECL for a typedef statement. Create a brand new
11943 ..._TYPE node (which will be just a variant of the existing
11944 ..._TYPE node with identical properties) and then install X
11945 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11947 The whole point here is to end up with a situation where each
11948 and every ..._TYPE node the compiler creates will be uniquely
11949 associated with AT MOST one node representing a typedef name.
11950 This way, even though the compiler substitutes corresponding
11951 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11952 early on, later parts of the compiler can always do the reverse
11953 translation and get back the corresponding typedef name. For
11956 typedef struct S MY_TYPE;
11959 Later parts of the compiler might only know that `object' was of
11960 type `struct S' if it were not for code just below. With this
11961 code however, later parts of the compiler see something like:
11963 struct S' == struct S
11964 typedef struct S' MY_TYPE;
11967 And they can then deduce (from the node for type struct S') that
11968 the original object declaration was:
11972 Being able to do this is important for proper support of protoize,
11973 and also for generating precise symbolic debugging information
11974 which takes full account of the programmer's (typedef) vocabulary.
11976 Obviously, we don't want to generate a duplicate ..._TYPE node if
11977 the TYPE_DECL node that we are now processing really represents a
11978 standard built-in type. */
11981 set_underlying_type (tree x
)
11983 if (x
== error_mark_node
)
11985 if (DECL_IS_BUILTIN (x
))
11987 if (TYPE_NAME (TREE_TYPE (x
)) == 0)
11988 TYPE_NAME (TREE_TYPE (x
)) = x
;
11990 else if (TREE_TYPE (x
) != error_mark_node
11991 && DECL_ORIGINAL_TYPE (x
) == NULL_TREE
)
11993 tree tt
= TREE_TYPE (x
);
11994 DECL_ORIGINAL_TYPE (x
) = tt
;
11995 tt
= build_variant_type_copy (tt
);
11996 TYPE_STUB_DECL (tt
) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x
));
11997 TYPE_NAME (tt
) = x
;
11998 TREE_USED (tt
) = TREE_USED (x
);
11999 TREE_TYPE (x
) = tt
;
12003 /* Record the types used by the current global variable declaration
12004 being parsed, so that we can decide later to emit their debug info.
12005 Those types are in types_used_by_cur_var_decl, and we are going to
12006 store them in the types_used_by_vars_hash hash table.
12007 DECL is the declaration of the global variable that has been parsed. */
12010 record_types_used_by_current_var_decl (tree decl
)
12012 gcc_assert (decl
&& DECL_P (decl
) && TREE_STATIC (decl
));
12014 while (types_used_by_cur_var_decl
&& !types_used_by_cur_var_decl
->is_empty ())
12016 tree type
= types_used_by_cur_var_decl
->pop ();
12017 types_used_by_var_decl_insert (type
, decl
);
12021 /* If DECL is a typedef that is declared in the current function,
12022 record it for the purpose of -Wunused-local-typedefs. */
12025 record_locally_defined_typedef (tree decl
)
12027 struct c_language_function
*l
;
12029 if (!warn_unused_local_typedefs
12031 /* if this is not a locally defined typedef then we are not
12033 || !is_typedef_decl (decl
)
12034 || !decl_function_context (decl
))
12037 l
= (struct c_language_function
*) cfun
->language
;
12038 vec_safe_push (l
->local_typedefs
, decl
);
12041 /* If T is a TYPE_DECL declared locally, mark it as used. */
12044 maybe_record_typedef_use (tree t
)
12046 if (!is_typedef_decl (t
))
12049 TREE_USED (t
) = true;
12052 /* Warn if there are some unused locally defined typedefs in the
12053 current function. */
12056 maybe_warn_unused_local_typedefs (void)
12060 /* The number of times we have emitted -Wunused-local-typedefs
12061 warnings. If this is different from errorcount, that means some
12062 unrelated errors have been issued. In which case, we'll avoid
12063 emitting "unused-local-typedefs" warnings. */
12064 static int unused_local_typedefs_warn_count
;
12065 struct c_language_function
*l
;
12070 if ((l
= (struct c_language_function
*) cfun
->language
) == NULL
)
12073 if (warn_unused_local_typedefs
12074 && errorcount
== unused_local_typedefs_warn_count
)
12076 FOR_EACH_VEC_SAFE_ELT (l
->local_typedefs
, i
, decl
)
12077 if (!TREE_USED (decl
))
12078 warning_at (DECL_SOURCE_LOCATION (decl
),
12079 OPT_Wunused_local_typedefs
,
12080 "typedef %qD locally defined but not used", decl
);
12081 unused_local_typedefs_warn_count
= errorcount
;
12084 vec_free (l
->local_typedefs
);
12087 /* Warn about boolean expression compared with an integer value different
12088 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12089 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12090 are the operands of the comparison. The caller must ensure that
12091 either operand is a boolean expression. */
12094 maybe_warn_bool_compare (location_t loc
, enum tree_code code
, tree op0
,
12097 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
12100 tree cst
= (TREE_CODE (op0
) == INTEGER_CST
)
12101 ? op0
: (TREE_CODE (op1
) == INTEGER_CST
) ? op1
: NULL_TREE
;
12105 if (!integer_zerop (cst
) && !integer_onep (cst
))
12107 int sign
= (TREE_CODE (op0
) == INTEGER_CST
12108 ? tree_int_cst_sgn (cst
) : -tree_int_cst_sgn (cst
));
12109 if (code
== EQ_EXPR
12110 || ((code
== GT_EXPR
|| code
== GE_EXPR
) && sign
< 0)
12111 || ((code
== LT_EXPR
|| code
== LE_EXPR
) && sign
> 0))
12112 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
12113 "with boolean expression is always false", cst
);
12115 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
12116 "with boolean expression is always true", cst
);
12118 else if (integer_zerop (cst
) || integer_onep (cst
))
12120 /* If the non-constant operand isn't of a boolean type, we
12121 don't want to warn here. */
12122 tree noncst
= TREE_CODE (op0
) == INTEGER_CST
? op1
: op0
;
12123 /* Handle booleans promoted to integers. */
12124 if (CONVERT_EXPR_P (noncst
)
12125 && TREE_TYPE (noncst
) == integer_type_node
12126 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst
, 0))) == BOOLEAN_TYPE
)
12128 else if (TREE_CODE (TREE_TYPE (noncst
)) != BOOLEAN_TYPE
12129 && !truth_value_p (TREE_CODE (noncst
)))
12131 /* Do some magic to get the right diagnostics. */
12132 bool flag
= TREE_CODE (op0
) == INTEGER_CST
;
12133 flag
= integer_zerop (cst
) ? flag
: !flag
;
12134 if ((code
== GE_EXPR
&& !flag
) || (code
== LE_EXPR
&& flag
))
12135 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
12136 "with boolean expression is always true", cst
);
12137 else if ((code
== LT_EXPR
&& !flag
) || (code
== GT_EXPR
&& flag
))
12138 warning_at (loc
, OPT_Wbool_compare
, "comparison of constant %qE "
12139 "with boolean expression is always false", cst
);
12143 /* The C and C++ parsers both use vectors to hold function arguments.
12144 For efficiency, we keep a cache of unused vectors. This is the
12147 typedef vec
<tree
, va_gc
> *tree_gc_vec
;
12148 static GTY((deletable
)) vec
<tree_gc_vec
, va_gc
> *tree_vector_cache
;
12150 /* Return a new vector from the cache. If the cache is empty,
12151 allocate a new vector. These vectors are GC'ed, so it is OK if the
12152 pointer is not released.. */
12155 make_tree_vector (void)
12157 if (tree_vector_cache
&& !tree_vector_cache
->is_empty ())
12158 return tree_vector_cache
->pop ();
12161 /* Passing 0 to vec::alloc returns NULL, and our callers require
12162 that we always return a non-NULL value. The vector code uses
12163 4 when growing a NULL vector, so we do too. */
12164 vec
<tree
, va_gc
> *v
;
12170 /* Release a vector of trees back to the cache. */
12173 release_tree_vector (vec
<tree
, va_gc
> *vec
)
12178 vec_safe_push (tree_vector_cache
, vec
);
12182 /* Get a new tree vector holding a single tree. */
12185 make_tree_vector_single (tree t
)
12187 vec
<tree
, va_gc
> *ret
= make_tree_vector ();
12188 ret
->quick_push (t
);
12192 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12195 make_tree_vector_from_list (tree list
)
12197 vec
<tree
, va_gc
> *ret
= make_tree_vector ();
12198 for (; list
; list
= TREE_CHAIN (list
))
12199 vec_safe_push (ret
, TREE_VALUE (list
));
12203 /* Get a new tree vector which is a copy of an existing one. */
12206 make_tree_vector_copy (const vec
<tree
, va_gc
> *orig
)
12208 vec
<tree
, va_gc
> *ret
;
12212 ret
= make_tree_vector ();
12213 vec_safe_reserve (ret
, vec_safe_length (orig
));
12214 FOR_EACH_VEC_SAFE_ELT (orig
, ix
, t
)
12215 ret
->quick_push (t
);
12219 /* Return true if KEYWORD starts a type specifier. */
12222 keyword_begins_type_specifier (enum rid keyword
)
12226 case RID_AUTO_TYPE
:
12238 case RID_DFLOAT128
:
12254 if (keyword
>= RID_FIRST_INT_N
12255 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
12256 && int_n_enabled_p
[keyword
-RID_FIRST_INT_N
])
12262 /* Return true if KEYWORD names a type qualifier. */
12265 keyword_is_type_qualifier (enum rid keyword
)
12279 /* Return true if KEYWORD names a storage class specifier.
12281 RID_TYPEDEF is not included in this list despite `typedef' being
12282 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12283 such for syntactic convenience only. */
12286 keyword_is_storage_class_specifier (enum rid keyword
)
12302 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12305 keyword_is_function_specifier (enum rid keyword
)
12319 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12320 declaration-specifier (C99 6.7). */
12323 keyword_is_decl_specifier (enum rid keyword
)
12325 if (keyword_is_storage_class_specifier (keyword
)
12326 || keyword_is_type_qualifier (keyword
)
12327 || keyword_is_function_specifier (keyword
))
12334 case RID_CONSTEXPR
:
12341 /* Initialize language-specific-bits of tree_contains_struct. */
12344 c_common_init_ts (void)
12346 MARK_TS_TYPED (C_MAYBE_CONST_EXPR
);
12347 MARK_TS_TYPED (EXCESS_PRECISION_EXPR
);
12348 MARK_TS_TYPED (ARRAY_NOTATION_REF
);
12351 /* Build a user-defined numeric literal out of an integer constant type VALUE
12352 with identifier SUFFIX. */
12355 build_userdef_literal (tree suffix_id
, tree value
,
12356 enum overflow_type overflow
, tree num_string
)
12358 tree literal
= make_node (USERDEF_LITERAL
);
12359 USERDEF_LITERAL_SUFFIX_ID (literal
) = suffix_id
;
12360 USERDEF_LITERAL_VALUE (literal
) = value
;
12361 USERDEF_LITERAL_OVERFLOW (literal
) = overflow
;
12362 USERDEF_LITERAL_NUM_STRING (literal
) = num_string
;
12366 /* For vector[index], convert the vector to a
12367 pointer of the underlying type. Return true if the resulting
12368 ARRAY_REF should not be an lvalue. */
12371 convert_vector_to_pointer_for_subscript (location_t loc
,
12372 tree
*vecp
, tree index
)
12375 if (TREE_CODE (TREE_TYPE (*vecp
)) == VECTOR_TYPE
)
12377 tree type
= TREE_TYPE (*vecp
);
12380 ret
= !lvalue_p (*vecp
);
12381 if (TREE_CODE (index
) == INTEGER_CST
)
12382 if (!tree_fits_uhwi_p (index
)
12383 || tree_to_uhwi (index
) >= TYPE_VECTOR_SUBPARTS (type
))
12384 warning_at (loc
, OPT_Warray_bounds
, "index value is out of bound");
12388 tree tmp
= create_tmp_var_raw (type
);
12389 DECL_SOURCE_LOCATION (tmp
) = loc
;
12390 *vecp
= c_save_expr (*vecp
);
12391 if (TREE_CODE (*vecp
) == C_MAYBE_CONST_EXPR
)
12393 bool non_const
= C_MAYBE_CONST_EXPR_NON_CONST (*vecp
);
12394 *vecp
= C_MAYBE_CONST_EXPR_EXPR (*vecp
);
12396 = c_wrap_maybe_const (build4 (TARGET_EXPR
, type
, tmp
,
12397 *vecp
, NULL_TREE
, NULL_TREE
),
12401 *vecp
= build4 (TARGET_EXPR
, type
, tmp
, *vecp
,
12402 NULL_TREE
, NULL_TREE
);
12403 SET_EXPR_LOCATION (*vecp
, loc
);
12404 c_common_mark_addressable_vec (tmp
);
12407 c_common_mark_addressable_vec (*vecp
);
12408 type
= build_qualified_type (TREE_TYPE (type
), TYPE_QUALS (type
));
12409 type1
= build_pointer_type (TREE_TYPE (*vecp
));
12410 bool ref_all
= TYPE_REF_CAN_ALIAS_ALL (type1
);
12412 && !DECL_P (*vecp
))
12414 /* If the original vector isn't declared may_alias and it
12415 isn't a bare vector look if the subscripting would
12416 alias the vector we subscript, and if not, force ref-all. */
12417 alias_set_type vecset
= get_alias_set (*vecp
);
12418 alias_set_type sset
= get_alias_set (type
);
12419 if (!alias_sets_must_conflict_p (sset
, vecset
)
12420 && !alias_set_subset_of (sset
, vecset
))
12423 type
= build_pointer_type_for_mode (type
, ptr_mode
, ref_all
);
12424 *vecp
= build1 (ADDR_EXPR
, type1
, *vecp
);
12425 *vecp
= convert (type
, *vecp
);
12430 /* Determine which of the operands, if any, is a scalar that needs to be
12431 converted to a vector, for the range of operations. */
12433 scalar_to_vector (location_t loc
, enum tree_code code
, tree op0
, tree op1
,
12436 tree type0
= TREE_TYPE (op0
);
12437 tree type1
= TREE_TYPE (op1
);
12438 bool integer_only_op
= false;
12439 enum stv_conv ret
= stv_firstarg
;
12441 gcc_assert (TREE_CODE (type0
) == VECTOR_TYPE
12442 || TREE_CODE (type1
) == VECTOR_TYPE
);
12445 /* Most GENERIC binary expressions require homogeneous arguments.
12446 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12447 argument that is a vector and a second one that is a scalar, so
12448 we never return stv_secondarg for them. */
12451 if (TREE_CODE (type0
) == INTEGER_TYPE
12452 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
12454 if (unsafe_conversion_p (loc
, TREE_TYPE (type1
), op0
, false))
12457 error_at (loc
, "conversion of scalar %qT to vector %qT "
12458 "involves truncation", type0
, type1
);
12462 return stv_firstarg
;
12469 integer_only_op
= true;
12470 /* ... fall through ... */
12472 case VEC_COND_EXPR
:
12477 case TRUNC_DIV_EXPR
:
12478 case CEIL_DIV_EXPR
:
12479 case FLOOR_DIV_EXPR
:
12480 case ROUND_DIV_EXPR
:
12481 case EXACT_DIV_EXPR
:
12482 case TRUNC_MOD_EXPR
:
12483 case FLOOR_MOD_EXPR
:
12491 /* What about UNLT_EXPR? */
12492 if (TREE_CODE (type0
) == VECTOR_TYPE
)
12495 ret
= stv_secondarg
;
12496 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12497 tmp
= type0
; type0
= type1
; type1
= tmp
;
12498 tmp
= op0
; op0
= op1
; op1
= tmp
;
12501 if (TREE_CODE (type0
) == INTEGER_TYPE
12502 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
12504 if (unsafe_conversion_p (loc
, TREE_TYPE (type1
), op0
, false))
12507 error_at (loc
, "conversion of scalar %qT to vector %qT "
12508 "involves truncation", type0
, type1
);
12513 else if (!integer_only_op
12514 /* Allow integer --> real conversion if safe. */
12515 && (TREE_CODE (type0
) == REAL_TYPE
12516 || TREE_CODE (type0
) == INTEGER_TYPE
)
12517 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1
)))
12519 if (unsafe_conversion_p (loc
, TREE_TYPE (type1
), op0
, false))
12522 error_at (loc
, "conversion of scalar %qT to vector %qT "
12523 "involves truncation", type0
, type1
);
12532 return stv_nothing
;
12535 /* Return true iff ALIGN is an integral constant that is a fundamental
12536 alignment, as defined by [basic.align] in the c++-11
12541 [A fundamental alignment is represented by an alignment less than or
12542 equal to the greatest alignment supported by the implementation
12543 in all contexts, which is equal to
12544 alignof(max_align_t)]. */
12547 cxx_fundamental_alignment_p (unsigned align
)
12549 return (align
<= MAX (TYPE_ALIGN (long_long_integer_type_node
),
12550 TYPE_ALIGN (long_double_type_node
)));
12553 /* Return true if T is a pointer to a zero-sized aggregate. */
12556 pointer_to_zero_sized_aggr_p (tree t
)
12558 if (!POINTER_TYPE_P (t
))
12561 return (TYPE_SIZE (t
) && integer_zerop (TYPE_SIZE (t
)));
12564 #include "gt-c-family-c-common.h"