* symtab.c (symtab_node::equal_address_to): New function.
[official-gcc.git] / gcc / c-family / c-common.c
blob1066c6b96a799f152427f9f352a3709063683bca
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "c-common.h"
24 #include "tm.h"
25 #include "intl.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "varasm.h"
33 #include "trans-mem.h"
34 #include "flags.h"
35 #include "c-pragma.h"
36 #include "c-objc.h"
37 #include "tm_p.h"
38 #include "obstack.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "diagnostic.h"
46 #include "tree-iterator.h"
47 #include "hashtab.h"
48 #include "opts.h"
49 #include "hash-map.h"
50 #include "is-a.h"
51 #include "plugin-api.h"
52 #include "vec.h"
53 #include "hash-set.h"
54 #include "machmode.h"
55 #include "hard-reg-set.h"
56 #include "input.h"
57 #include "function.h"
58 #include "ipa-ref.h"
59 #include "cgraph.h"
60 #include "target-def.h"
61 #include "gimplify.h"
62 #include "wide-int-print.h"
63 #include "gimple-expr.h"
65 cpp_reader *parse_in; /* Declared in c-pragma.h. */
67 /* The following symbols are subsumed in the c_global_trees array, and
68 listed here individually for documentation purposes.
70 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
72 tree short_integer_type_node;
73 tree long_integer_type_node;
74 tree long_long_integer_type_node;
76 tree short_unsigned_type_node;
77 tree long_unsigned_type_node;
78 tree long_long_unsigned_type_node;
80 tree truthvalue_type_node;
81 tree truthvalue_false_node;
82 tree truthvalue_true_node;
84 tree ptrdiff_type_node;
86 tree unsigned_char_type_node;
87 tree signed_char_type_node;
88 tree wchar_type_node;
90 tree char16_type_node;
91 tree char32_type_node;
93 tree float_type_node;
94 tree double_type_node;
95 tree long_double_type_node;
97 tree complex_integer_type_node;
98 tree complex_float_type_node;
99 tree complex_double_type_node;
100 tree complex_long_double_type_node;
102 tree dfloat32_type_node;
103 tree dfloat64_type_node;
104 tree_dfloat128_type_node;
106 tree intQI_type_node;
107 tree intHI_type_node;
108 tree intSI_type_node;
109 tree intDI_type_node;
110 tree intTI_type_node;
112 tree unsigned_intQI_type_node;
113 tree unsigned_intHI_type_node;
114 tree unsigned_intSI_type_node;
115 tree unsigned_intDI_type_node;
116 tree unsigned_intTI_type_node;
118 tree widest_integer_literal_type_node;
119 tree widest_unsigned_literal_type_node;
121 Nodes for types `void *' and `const void *'.
123 tree ptr_type_node, const_ptr_type_node;
125 Nodes for types `char *' and `const char *'.
127 tree string_type_node, const_string_type_node;
129 Type `char[SOMENUMBER]'.
130 Used when an array of char is needed and the size is irrelevant.
132 tree char_array_type_node;
134 Type `wchar_t[SOMENUMBER]' or something like it.
135 Used when a wide string literal is created.
137 tree wchar_array_type_node;
139 Type `char16_t[SOMENUMBER]' or something like it.
140 Used when a UTF-16 string literal is created.
142 tree char16_array_type_node;
144 Type `char32_t[SOMENUMBER]' or something like it.
145 Used when a UTF-32 string literal is created.
147 tree char32_array_type_node;
149 Type `int ()' -- used for implicit declaration of functions.
151 tree default_function_type;
153 A VOID_TYPE node, packaged in a TREE_LIST.
155 tree void_list_node;
157 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
158 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
159 VAR_DECLS, but C++ does.)
161 tree function_name_decl_node;
162 tree pretty_function_name_decl_node;
163 tree c99_function_name_decl_node;
165 Stack of nested function name VAR_DECLs.
167 tree saved_function_name_decls;
171 tree c_global_trees[CTI_MAX];
173 /* Switches common to the C front ends. */
175 /* Nonzero means don't output line number information. */
177 char flag_no_line_commands;
179 /* Nonzero causes -E output not to be done, but directives such as
180 #define that have side effects are still obeyed. */
182 char flag_no_output;
184 /* Nonzero means dump macros in some fashion. */
186 char flag_dump_macros;
188 /* Nonzero means pass #include lines through to the output. */
190 char flag_dump_includes;
192 /* Nonzero means process PCH files while preprocessing. */
194 bool flag_pch_preprocess;
196 /* The file name to which we should write a precompiled header, or
197 NULL if no header will be written in this compile. */
199 const char *pch_file;
201 /* Nonzero if an ISO standard was selected. It rejects macros in the
202 user's namespace. */
203 int flag_iso;
205 /* C/ObjC language option variables. */
208 /* Nonzero means allow type mismatches in conditional expressions;
209 just make their values `void'. */
211 int flag_cond_mismatch;
213 /* Nonzero means enable C89 Amendment 1 features. */
215 int flag_isoc94;
217 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
219 int flag_isoc99;
221 /* Nonzero means use the ISO C11 dialect of C. */
223 int flag_isoc11;
225 /* Nonzero means that we have builtin functions, and main is an int. */
227 int flag_hosted = 1;
230 /* ObjC language option variables. */
233 /* Tells the compiler that this is a special run. Do not perform any
234 compiling, instead we are to test some platform dependent features
235 and output a C header file with appropriate definitions. */
237 int print_struct_values;
239 /* Tells the compiler what is the constant string class for ObjC. */
241 const char *constant_string_class_name;
244 /* C++ language option variables. */
247 /* Nonzero means generate separate instantiation control files and
248 juggle them at link time. */
250 int flag_use_repository;
252 /* The C++ dialect being used. C++98 is the default. */
254 enum cxx_dialect cxx_dialect = cxx98;
256 /* Maximum template instantiation depth. This limit exists to limit the
257 time it takes to notice excessively recursive template instantiations.
259 The default is lower than the 1024 recommended by the C++0x standard
260 because G++ runs out of stack before 1024 with highly recursive template
261 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
263 int max_tinst_depth = 900;
265 /* The elements of `ridpointers' are identifier nodes for the reserved
266 type names and storage classes. It is indexed by a RID_... value. */
267 tree *ridpointers;
269 tree (*make_fname_decl) (location_t, tree, int);
271 /* Nonzero means don't warn about problems that occur when the code is
272 executed. */
273 int c_inhibit_evaluation_warnings;
275 /* Whether we are building a boolean conversion inside
276 convert_for_assignment, or some other late binary operation. If
277 build_binary_op is called for C (from code shared by C and C++) in
278 this case, then the operands have already been folded and the
279 result will not be folded again, so C_MAYBE_CONST_EXPR should not
280 be generated. */
281 bool in_late_binary_op;
283 /* Whether lexing has been completed, so subsequent preprocessor
284 errors should use the compiler's input_location. */
285 bool done_lexing = false;
287 /* Information about how a function name is generated. */
288 struct fname_var_t
290 tree *const decl; /* pointer to the VAR_DECL. */
291 const unsigned rid; /* RID number for the identifier. */
292 const int pretty; /* How pretty is it? */
295 /* The three ways of getting then name of the current function. */
297 const struct fname_var_t fname_vars[] =
299 /* C99 compliant __func__, must be first. */
300 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
301 /* GCC __FUNCTION__ compliant. */
302 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
303 /* GCC __PRETTY_FUNCTION__ compliant. */
304 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
305 {NULL, 0, 0},
308 /* Global visibility options. */
309 struct visibility_flags visibility_options;
311 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
312 static tree check_case_value (location_t, tree);
313 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
315 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
322 int, bool *);
323 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
324 int, bool *);
325 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
326 bool *);
327 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_always_inline_attribute (tree *, tree, tree, int,
331 bool *);
332 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
339 bool *);
340 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
341 bool *);
342 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_transparent_union_attribute (tree *, tree, tree,
344 int, bool *);
345 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
351 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
352 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
355 static tree handle_visibility_attribute (tree *, tree, tree, int,
356 bool *);
357 static tree handle_tls_model_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_no_instrument_function_attribute (tree *, tree,
360 tree, int, bool *);
361 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_deprecated_attribute (tree *, tree, tree, int,
370 bool *);
371 static tree handle_vector_size_attribute (tree *, tree, tree, int,
372 bool *);
373 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
385 static tree ignore_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
391 bool *);
392 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
393 bool *);
394 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
397 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
399 static void check_function_nonnull (tree, int, tree *);
400 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
401 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
402 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
403 static int resort_field_decl_cmp (const void *, const void *);
405 /* Reserved words. The third field is a mask: keywords are disabled
406 if they match the mask.
408 Masks for languages:
409 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
410 C --std=c99: D_CXXONLY | D_OBJC
411 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
412 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
413 C++ --std=c0x: D_CONLY | D_OBJC
414 ObjC++ is like C++ except that D_OBJC is not set
416 If -fno-asm is used, D_ASM is added to the mask. If
417 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
418 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
419 In C with -Wc++-compat, we warn if D_CXXWARN is set.
421 Note the complication of the D_CXX_OBJC keywords. These are
422 reserved words such as 'class'. In C++, 'class' is a reserved
423 word. In Objective-C++ it is too. In Objective-C, it is a
424 reserved word too, but only if it follows an '@' sign.
426 const struct c_common_resword c_common_reswords[] =
428 { "_Alignas", RID_ALIGNAS, D_CONLY },
429 { "_Alignof", RID_ALIGNOF, D_CONLY },
430 { "_Atomic", RID_ATOMIC, D_CONLY },
431 { "_Bool", RID_BOOL, D_CONLY },
432 { "_Complex", RID_COMPLEX, 0 },
433 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
434 { "_Cilk_sync", RID_CILK_SYNC, 0 },
435 { "_Cilk_for", RID_CILK_FOR, 0 },
436 { "_Imaginary", RID_IMAGINARY, D_CONLY },
437 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
438 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
439 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
440 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
441 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
442 { "_Sat", RID_SAT, D_CONLY | D_EXT },
443 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
444 { "_Noreturn", RID_NORETURN, D_CONLY },
445 { "_Generic", RID_GENERIC, D_CONLY },
446 { "_Thread_local", RID_THREAD, D_CONLY },
447 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
448 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
449 { "__alignof", RID_ALIGNOF, 0 },
450 { "__alignof__", RID_ALIGNOF, 0 },
451 { "__asm", RID_ASM, 0 },
452 { "__asm__", RID_ASM, 0 },
453 { "__attribute", RID_ATTRIBUTE, 0 },
454 { "__attribute__", RID_ATTRIBUTE, 0 },
455 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
456 { "__bases", RID_BASES, D_CXXONLY },
457 { "__builtin_call_with_static_chain",
458 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
459 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
460 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
461 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
462 { "__builtin_offsetof", RID_OFFSETOF, 0 },
463 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
464 { "__builtin_va_arg", RID_VA_ARG, 0 },
465 { "__complex", RID_COMPLEX, 0 },
466 { "__complex__", RID_COMPLEX, 0 },
467 { "__const", RID_CONST, 0 },
468 { "__const__", RID_CONST, 0 },
469 { "__decltype", RID_DECLTYPE, D_CXXONLY },
470 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
471 { "__extension__", RID_EXTENSION, 0 },
472 { "__func__", RID_C99_FUNCTION_NAME, 0 },
473 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
474 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
475 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
476 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
477 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
478 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
479 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
480 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
481 { "__imag", RID_IMAGPART, 0 },
482 { "__imag__", RID_IMAGPART, 0 },
483 { "__inline", RID_INLINE, 0 },
484 { "__inline__", RID_INLINE, 0 },
485 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
486 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
487 { "__is_class", RID_IS_CLASS, D_CXXONLY },
488 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
489 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
490 { "__is_final", RID_IS_FINAL, D_CXXONLY },
491 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
492 { "__is_pod", RID_IS_POD, D_CXXONLY },
493 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
494 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
495 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
496 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
497 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
498 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
499 { "__is_union", RID_IS_UNION, D_CXXONLY },
500 { "__label__", RID_LABEL, 0 },
501 { "__null", RID_NULL, 0 },
502 { "__real", RID_REALPART, 0 },
503 { "__real__", RID_REALPART, 0 },
504 { "__restrict", RID_RESTRICT, 0 },
505 { "__restrict__", RID_RESTRICT, 0 },
506 { "__signed", RID_SIGNED, 0 },
507 { "__signed__", RID_SIGNED, 0 },
508 { "__thread", RID_THREAD, 0 },
509 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
510 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
511 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
512 { "__typeof", RID_TYPEOF, 0 },
513 { "__typeof__", RID_TYPEOF, 0 },
514 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
515 { "__volatile", RID_VOLATILE, 0 },
516 { "__volatile__", RID_VOLATILE, 0 },
517 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
518 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
519 { "asm", RID_ASM, D_ASM },
520 { "auto", RID_AUTO, 0 },
521 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
522 { "break", RID_BREAK, 0 },
523 { "case", RID_CASE, 0 },
524 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
525 { "char", RID_CHAR, 0 },
526 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
527 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
529 { "const", RID_CONST, 0 },
530 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
531 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
532 { "continue", RID_CONTINUE, 0 },
533 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
534 { "default", RID_DEFAULT, 0 },
535 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
536 { "do", RID_DO, 0 },
537 { "double", RID_DOUBLE, 0 },
538 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
539 { "else", RID_ELSE, 0 },
540 { "enum", RID_ENUM, 0 },
541 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
542 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
543 { "extern", RID_EXTERN, 0 },
544 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
545 { "float", RID_FLOAT, 0 },
546 { "for", RID_FOR, 0 },
547 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
548 { "goto", RID_GOTO, 0 },
549 { "if", RID_IF, 0 },
550 { "inline", RID_INLINE, D_EXT89 },
551 { "int", RID_INT, 0 },
552 { "long", RID_LONG, 0 },
553 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
554 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
555 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
556 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
557 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
558 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
559 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
560 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
561 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
562 { "register", RID_REGISTER, 0 },
563 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
564 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
565 { "return", RID_RETURN, 0 },
566 { "short", RID_SHORT, 0 },
567 { "signed", RID_SIGNED, 0 },
568 { "sizeof", RID_SIZEOF, 0 },
569 { "static", RID_STATIC, 0 },
570 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
571 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
572 { "struct", RID_STRUCT, 0 },
573 { "switch", RID_SWITCH, 0 },
574 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
575 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
576 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
577 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
578 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
579 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
580 { "typedef", RID_TYPEDEF, 0 },
581 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
582 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
583 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
584 { "union", RID_UNION, 0 },
585 { "unsigned", RID_UNSIGNED, 0 },
586 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
587 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
588 { "void", RID_VOID, 0 },
589 { "volatile", RID_VOLATILE, 0 },
590 { "wchar_t", RID_WCHAR, D_CXXONLY },
591 { "while", RID_WHILE, 0 },
592 /* These Objective-C keywords are recognized only immediately after
593 an '@'. */
594 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
595 { "defs", RID_AT_DEFS, D_OBJC },
596 { "encode", RID_AT_ENCODE, D_OBJC },
597 { "end", RID_AT_END, D_OBJC },
598 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
599 { "interface", RID_AT_INTERFACE, D_OBJC },
600 { "protocol", RID_AT_PROTOCOL, D_OBJC },
601 { "selector", RID_AT_SELECTOR, D_OBJC },
602 { "finally", RID_AT_FINALLY, D_OBJC },
603 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
604 { "optional", RID_AT_OPTIONAL, D_OBJC },
605 { "required", RID_AT_REQUIRED, D_OBJC },
606 { "property", RID_AT_PROPERTY, D_OBJC },
607 { "package", RID_AT_PACKAGE, D_OBJC },
608 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
609 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
610 /* These are recognized only in protocol-qualifier context
611 (see above) */
612 { "bycopy", RID_BYCOPY, D_OBJC },
613 { "byref", RID_BYREF, D_OBJC },
614 { "in", RID_IN, D_OBJC },
615 { "inout", RID_INOUT, D_OBJC },
616 { "oneway", RID_ONEWAY, D_OBJC },
617 { "out", RID_OUT, D_OBJC },
618 /* These are recognized inside a property attribute list */
619 { "assign", RID_ASSIGN, D_OBJC },
620 { "copy", RID_COPY, D_OBJC },
621 { "getter", RID_GETTER, D_OBJC },
622 { "nonatomic", RID_NONATOMIC, D_OBJC },
623 { "readonly", RID_READONLY, D_OBJC },
624 { "readwrite", RID_READWRITE, D_OBJC },
625 { "retain", RID_RETAIN, D_OBJC },
626 { "setter", RID_SETTER, D_OBJC },
629 const unsigned int num_c_common_reswords =
630 sizeof c_common_reswords / sizeof (struct c_common_resword);
632 /* Table of machine-independent attributes common to all C-like languages.
634 All attributes referencing arguments should be additionally processed
635 in chkp_copy_function_type_adding_bounds for correct instrumentation
636 by Pointer Bounds Checker.
637 Current list of processed common attributes: nonnull. */
638 const struct attribute_spec c_common_attribute_table[] =
640 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
641 affects_type_identity } */
642 { "packed", 0, 0, false, false, false,
643 handle_packed_attribute , false},
644 { "nocommon", 0, 0, true, false, false,
645 handle_nocommon_attribute, false},
646 { "common", 0, 0, true, false, false,
647 handle_common_attribute, false },
648 /* FIXME: logically, noreturn attributes should be listed as
649 "false, true, true" and apply to function types. But implementing this
650 would require all the places in the compiler that use TREE_THIS_VOLATILE
651 on a decl to identify non-returning functions to be located and fixed
652 to check the function type instead. */
653 { "noreturn", 0, 0, true, false, false,
654 handle_noreturn_attribute, false },
655 { "volatile", 0, 0, true, false, false,
656 handle_noreturn_attribute, false },
657 { "noinline", 0, 0, true, false, false,
658 handle_noinline_attribute, false },
659 { "noclone", 0, 0, true, false, false,
660 handle_noclone_attribute, false },
661 { "leaf", 0, 0, true, false, false,
662 handle_leaf_attribute, false },
663 { "always_inline", 0, 0, true, false, false,
664 handle_always_inline_attribute, false },
665 { "gnu_inline", 0, 0, true, false, false,
666 handle_gnu_inline_attribute, false },
667 { "artificial", 0, 0, true, false, false,
668 handle_artificial_attribute, false },
669 { "flatten", 0, 0, true, false, false,
670 handle_flatten_attribute, false },
671 { "used", 0, 0, true, false, false,
672 handle_used_attribute, false },
673 { "unused", 0, 0, false, false, false,
674 handle_unused_attribute, false },
675 { "externally_visible", 0, 0, true, false, false,
676 handle_externally_visible_attribute, false },
677 { "no_reorder", 0, 0, true, false, false,
678 handle_no_reorder_attribute, false },
679 /* The same comments as for noreturn attributes apply to const ones. */
680 { "const", 0, 0, true, false, false,
681 handle_const_attribute, false },
682 { "transparent_union", 0, 0, false, false, false,
683 handle_transparent_union_attribute, false },
684 { "constructor", 0, 1, true, false, false,
685 handle_constructor_attribute, false },
686 { "destructor", 0, 1, true, false, false,
687 handle_destructor_attribute, false },
688 { "mode", 1, 1, false, true, false,
689 handle_mode_attribute, false },
690 { "section", 1, 1, true, false, false,
691 handle_section_attribute, false },
692 { "aligned", 0, 1, false, false, false,
693 handle_aligned_attribute, false },
694 { "weak", 0, 0, true, false, false,
695 handle_weak_attribute, false },
696 { "ifunc", 1, 1, true, false, false,
697 handle_ifunc_attribute, false },
698 { "alias", 1, 1, true, false, false,
699 handle_alias_attribute, false },
700 { "weakref", 0, 1, true, false, false,
701 handle_weakref_attribute, false },
702 { "no_instrument_function", 0, 0, true, false, false,
703 handle_no_instrument_function_attribute,
704 false },
705 { "malloc", 0, 0, true, false, false,
706 handle_malloc_attribute, false },
707 { "returns_twice", 0, 0, true, false, false,
708 handle_returns_twice_attribute, false },
709 { "no_stack_limit", 0, 0, true, false, false,
710 handle_no_limit_stack_attribute, false },
711 { "pure", 0, 0, true, false, false,
712 handle_pure_attribute, false },
713 { "transaction_callable", 0, 0, false, true, false,
714 handle_tm_attribute, false },
715 { "transaction_unsafe", 0, 0, false, true, false,
716 handle_tm_attribute, false },
717 { "transaction_safe", 0, 0, false, true, false,
718 handle_tm_attribute, false },
719 { "transaction_may_cancel_outer", 0, 0, false, true, false,
720 handle_tm_attribute, false },
721 /* ??? These two attributes didn't make the transition from the
722 Intel language document to the multi-vendor language document. */
723 { "transaction_pure", 0, 0, false, true, false,
724 handle_tm_attribute, false },
725 { "transaction_wrap", 1, 1, true, false, false,
726 handle_tm_wrap_attribute, false },
727 /* For internal use (marking of builtins) only. The name contains space
728 to prevent its usage in source code. */
729 { "no vops", 0, 0, true, false, false,
730 handle_novops_attribute, false },
731 { "deprecated", 0, 1, false, false, false,
732 handle_deprecated_attribute, false },
733 { "vector_size", 1, 1, false, true, false,
734 handle_vector_size_attribute, false },
735 { "visibility", 1, 1, false, false, false,
736 handle_visibility_attribute, false },
737 { "tls_model", 1, 1, true, false, false,
738 handle_tls_model_attribute, false },
739 { "nonnull", 0, -1, false, true, true,
740 handle_nonnull_attribute, false },
741 { "nothrow", 0, 0, true, false, false,
742 handle_nothrow_attribute, false },
743 { "may_alias", 0, 0, false, true, false, NULL, false },
744 { "cleanup", 1, 1, true, false, false,
745 handle_cleanup_attribute, false },
746 { "warn_unused_result", 0, 0, false, true, true,
747 handle_warn_unused_result_attribute, false },
748 { "sentinel", 0, 1, false, true, true,
749 handle_sentinel_attribute, false },
750 /* For internal use (marking of builtins) only. The name contains space
751 to prevent its usage in source code. */
752 { "type generic", 0, 0, false, true, true,
753 handle_type_generic_attribute, false },
754 { "alloc_size", 1, 2, false, true, true,
755 handle_alloc_size_attribute, false },
756 { "cold", 0, 0, true, false, false,
757 handle_cold_attribute, false },
758 { "hot", 0, 0, true, false, false,
759 handle_hot_attribute, false },
760 { "no_address_safety_analysis",
761 0, 0, true, false, false,
762 handle_no_address_safety_analysis_attribute,
763 false },
764 { "no_sanitize_address", 0, 0, true, false, false,
765 handle_no_sanitize_address_attribute,
766 false },
767 { "no_sanitize_undefined", 0, 0, true, false, false,
768 handle_no_sanitize_undefined_attribute,
769 false },
770 { "warning", 1, 1, true, false, false,
771 handle_error_attribute, false },
772 { "error", 1, 1, true, false, false,
773 handle_error_attribute, false },
774 { "target", 1, -1, true, false, false,
775 handle_target_attribute, false },
776 { "optimize", 1, -1, true, false, false,
777 handle_optimize_attribute, false },
778 /* For internal use only. The leading '*' both prevents its usage in
779 source code and signals that it may be overridden by machine tables. */
780 { "*tm regparm", 0, 0, false, true, true,
781 ignore_attribute, false },
782 { "no_split_stack", 0, 0, true, false, false,
783 handle_no_split_stack_attribute, false },
784 /* For internal use (marking of builtins and runtime functions) only.
785 The name contains space to prevent its usage in source code. */
786 { "fn spec", 1, 1, false, true, true,
787 handle_fnspec_attribute, false },
788 { "warn_unused", 0, 0, false, false, false,
789 handle_warn_unused_attribute, false },
790 { "returns_nonnull", 0, 0, false, true, true,
791 handle_returns_nonnull_attribute, false },
792 { "omp declare simd", 0, -1, true, false, false,
793 handle_omp_declare_simd_attribute, false },
794 { "cilk simd function", 0, -1, true, false, false,
795 handle_omp_declare_simd_attribute, false },
796 { "omp declare target", 0, 0, true, false, false,
797 handle_omp_declare_target_attribute, false },
798 { "alloc_align", 1, 1, false, true, true,
799 handle_alloc_align_attribute, false },
800 { "assume_aligned", 1, 2, false, true, true,
801 handle_assume_aligned_attribute, false },
802 { "designated_init", 0, 0, false, true, false,
803 handle_designated_init_attribute, false },
804 { "bnd_variable_size", 0, 0, true, false, false,
805 handle_bnd_variable_size_attribute, false },
806 { "bnd_legacy", 0, 0, true, false, false,
807 handle_bnd_legacy, false },
808 { "bnd_instrument", 0, 0, true, false, false,
809 handle_bnd_instrument, false },
810 { NULL, 0, 0, false, false, false, NULL, false }
813 /* Give the specifications for the format attributes, used by C and all
814 descendants.
816 All attributes referencing arguments should be additionally processed
817 in chkp_copy_function_type_adding_bounds for correct instrumentation
818 by Pointer Bounds Checker.
819 Current list of processed format attributes: format, format_arg. */
820 const struct attribute_spec c_common_format_attribute_table[] =
822 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
823 affects_type_identity } */
824 { "format", 3, 3, false, true, true,
825 handle_format_attribute, false },
826 { "format_arg", 1, 1, false, true, true,
827 handle_format_arg_attribute, false },
828 { NULL, 0, 0, false, false, false, NULL, false }
831 /* Return identifier for address space AS. */
833 const char *
834 c_addr_space_name (addr_space_t as)
836 int rid = RID_FIRST_ADDR_SPACE + as;
837 gcc_assert (ridpointers [rid]);
838 return IDENTIFIER_POINTER (ridpointers [rid]);
841 /* Push current bindings for the function name VAR_DECLS. */
843 void
844 start_fname_decls (void)
846 unsigned ix;
847 tree saved = NULL_TREE;
849 for (ix = 0; fname_vars[ix].decl; ix++)
851 tree decl = *fname_vars[ix].decl;
853 if (decl)
855 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
856 saved);
857 *fname_vars[ix].decl = NULL_TREE;
860 if (saved || saved_function_name_decls)
861 /* Normally they'll have been NULL, so only push if we've got a
862 stack, or they are non-NULL. */
863 saved_function_name_decls = tree_cons (saved, NULL_TREE,
864 saved_function_name_decls);
867 /* Finish up the current bindings, adding them into the current function's
868 statement tree. This must be done _before_ finish_stmt_tree is called.
869 If there is no current function, we must be at file scope and no statements
870 are involved. Pop the previous bindings. */
872 void
873 finish_fname_decls (void)
875 unsigned ix;
876 tree stmts = NULL_TREE;
877 tree stack = saved_function_name_decls;
879 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
880 append_to_statement_list (TREE_VALUE (stack), &stmts);
882 if (stmts)
884 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
886 if (TREE_CODE (*bodyp) == BIND_EXPR)
887 bodyp = &BIND_EXPR_BODY (*bodyp);
889 append_to_statement_list_force (*bodyp, &stmts);
890 *bodyp = stmts;
893 for (ix = 0; fname_vars[ix].decl; ix++)
894 *fname_vars[ix].decl = NULL_TREE;
896 if (stack)
898 /* We had saved values, restore them. */
899 tree saved;
901 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
903 tree decl = TREE_PURPOSE (saved);
904 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
906 *fname_vars[ix].decl = decl;
908 stack = TREE_CHAIN (stack);
910 saved_function_name_decls = stack;
913 /* Return the text name of the current function, suitably prettified
914 by PRETTY_P. Return string must be freed by caller. */
916 const char *
917 fname_as_string (int pretty_p)
919 const char *name = "top level";
920 char *namep;
921 int vrb = 2, len;
922 cpp_string cstr = { 0, 0 }, strname;
924 if (!pretty_p)
926 name = "";
927 vrb = 0;
930 if (current_function_decl)
931 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
933 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
935 namep = XNEWVEC (char, len);
936 snprintf (namep, len, "\"%s\"", name);
937 strname.text = (unsigned char *) namep;
938 strname.len = len - 1;
940 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
942 XDELETEVEC (namep);
943 return (const char *) cstr.text;
946 return namep;
949 /* Return the VAR_DECL for a const char array naming the current
950 function. If the VAR_DECL has not yet been created, create it
951 now. RID indicates how it should be formatted and IDENTIFIER_NODE
952 ID is its name (unfortunately C and C++ hold the RID values of
953 keywords in different places, so we can't derive RID from ID in
954 this language independent code. LOC is the location of the
955 function. */
957 tree
958 fname_decl (location_t loc, unsigned int rid, tree id)
960 unsigned ix;
961 tree decl = NULL_TREE;
963 for (ix = 0; fname_vars[ix].decl; ix++)
964 if (fname_vars[ix].rid == rid)
965 break;
967 decl = *fname_vars[ix].decl;
968 if (!decl)
970 /* If a tree is built here, it would normally have the lineno of
971 the current statement. Later this tree will be moved to the
972 beginning of the function and this line number will be wrong.
973 To avoid this problem set the lineno to 0 here; that prevents
974 it from appearing in the RTL. */
975 tree stmts;
976 location_t saved_location = input_location;
977 input_location = UNKNOWN_LOCATION;
979 stmts = push_stmt_list ();
980 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
981 stmts = pop_stmt_list (stmts);
982 if (!IS_EMPTY_STMT (stmts))
983 saved_function_name_decls
984 = tree_cons (decl, stmts, saved_function_name_decls);
985 *fname_vars[ix].decl = decl;
986 input_location = saved_location;
988 if (!ix && !current_function_decl)
989 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
991 return decl;
994 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
996 tree
997 fix_string_type (tree value)
999 int length = TREE_STRING_LENGTH (value);
1000 int nchars;
1001 tree e_type, i_type, a_type;
1003 /* Compute the number of elements, for the array type. */
1004 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1006 nchars = length;
1007 e_type = char_type_node;
1009 else if (TREE_TYPE (value) == char16_array_type_node)
1011 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1012 e_type = char16_type_node;
1014 else if (TREE_TYPE (value) == char32_array_type_node)
1016 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1017 e_type = char32_type_node;
1019 else
1021 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1022 e_type = wchar_type_node;
1025 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1026 limit in C++98 Annex B is very large (65536) and is not normative,
1027 so we do not diagnose it (warn_overlength_strings is forced off
1028 in c_common_post_options). */
1029 if (warn_overlength_strings)
1031 const int nchars_max = flag_isoc99 ? 4095 : 509;
1032 const int relevant_std = flag_isoc99 ? 99 : 90;
1033 if (nchars - 1 > nchars_max)
1034 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1035 separate the %d from the 'C'. 'ISO' should not be
1036 translated, but it may be moved after 'C%d' in languages
1037 where modifiers follow nouns. */
1038 pedwarn (input_location, OPT_Woverlength_strings,
1039 "string length %qd is greater than the length %qd "
1040 "ISO C%d compilers are required to support",
1041 nchars - 1, nchars_max, relevant_std);
1044 /* Create the array type for the string constant. The ISO C++
1045 standard says that a string literal has type `const char[N]' or
1046 `const wchar_t[N]'. We use the same logic when invoked as a C
1047 front-end with -Wwrite-strings.
1048 ??? We should change the type of an expression depending on the
1049 state of a warning flag. We should just be warning -- see how
1050 this is handled in the C++ front-end for the deprecated implicit
1051 conversion from string literals to `char*' or `wchar_t*'.
1053 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1054 array type being the unqualified version of that type.
1055 Therefore, if we are constructing an array of const char, we must
1056 construct the matching unqualified array type first. The C front
1057 end does not require this, but it does no harm, so we do it
1058 unconditionally. */
1059 i_type = build_index_type (size_int (nchars - 1));
1060 a_type = build_array_type (e_type, i_type);
1061 if (c_dialect_cxx() || warn_write_strings)
1062 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1064 TREE_TYPE (value) = a_type;
1065 TREE_CONSTANT (value) = 1;
1066 TREE_READONLY (value) = 1;
1067 TREE_STATIC (value) = 1;
1068 return value;
1071 /* If DISABLE is true, stop issuing warnings. This is used when
1072 parsing code that we know will not be executed. This function may
1073 be called multiple times, and works as a stack. */
1075 static void
1076 c_disable_warnings (bool disable)
1078 if (disable)
1080 ++c_inhibit_evaluation_warnings;
1081 fold_defer_overflow_warnings ();
1085 /* If ENABLE is true, reenable issuing warnings. */
1087 static void
1088 c_enable_warnings (bool enable)
1090 if (enable)
1092 --c_inhibit_evaluation_warnings;
1093 fold_undefer_and_ignore_overflow_warnings ();
1097 /* Fully fold EXPR, an expression that was not folded (beyond integer
1098 constant expressions and null pointer constants) when being built
1099 up. If IN_INIT, this is in a static initializer and certain
1100 changes are made to the folding done. Clear *MAYBE_CONST if
1101 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1102 expression because it contains an evaluated operator (in C99) or an
1103 operator outside of sizeof returning an integer constant (in C90)
1104 not permitted in constant expressions, or because it contains an
1105 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1106 set to true by callers before calling this function.) Return the
1107 folded expression. Function arguments have already been folded
1108 before calling this function, as have the contents of SAVE_EXPR,
1109 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1110 C_MAYBE_CONST_EXPR. */
1112 tree
1113 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1115 tree ret;
1116 tree eptype = NULL_TREE;
1117 bool dummy = true;
1118 bool maybe_const_itself = true;
1119 location_t loc = EXPR_LOCATION (expr);
1121 /* This function is not relevant to C++ because C++ folds while
1122 parsing, and may need changes to be correct for C++ when C++
1123 stops folding while parsing. */
1124 if (c_dialect_cxx ())
1125 gcc_unreachable ();
1127 if (!maybe_const)
1128 maybe_const = &dummy;
1129 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1131 eptype = TREE_TYPE (expr);
1132 expr = TREE_OPERAND (expr, 0);
1134 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1135 &maybe_const_itself);
1136 if (eptype)
1137 ret = fold_convert_loc (loc, eptype, ret);
1138 *maybe_const &= maybe_const_itself;
1139 return ret;
1142 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1143 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1144 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1145 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1146 both evaluated and unevaluated subexpressions while
1147 *MAYBE_CONST_ITSELF is carried from only evaluated
1148 subexpressions). */
1150 static tree
1151 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1152 bool *maybe_const_itself)
1154 tree ret = expr;
1155 enum tree_code code = TREE_CODE (expr);
1156 enum tree_code_class kind = TREE_CODE_CLASS (code);
1157 location_t loc = EXPR_LOCATION (expr);
1158 tree op0, op1, op2, op3;
1159 tree orig_op0, orig_op1, orig_op2;
1160 bool op0_const = true, op1_const = true, op2_const = true;
1161 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1162 bool nowarning = TREE_NO_WARNING (expr);
1163 bool unused_p;
1165 /* This function is not relevant to C++ because C++ folds while
1166 parsing, and may need changes to be correct for C++ when C++
1167 stops folding while parsing. */
1168 if (c_dialect_cxx ())
1169 gcc_unreachable ();
1171 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1172 anything else not counted as an expression cannot usefully be
1173 folded further at this point. */
1174 if (!IS_EXPR_CODE_CLASS (kind)
1175 || kind == tcc_statement
1176 || code == SAVE_EXPR)
1177 return expr;
1179 /* Operands of variable-length expressions (function calls) have
1180 already been folded, as have __builtin_* function calls, and such
1181 expressions cannot occur in constant expressions. */
1182 if (kind == tcc_vl_exp)
1184 *maybe_const_operands = false;
1185 ret = fold (expr);
1186 goto out;
1189 if (code == C_MAYBE_CONST_EXPR)
1191 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1192 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1193 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1194 *maybe_const_operands = false;
1195 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1196 *maybe_const_itself = false;
1197 if (pre && !in_init)
1198 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1199 else
1200 ret = inner;
1201 goto out;
1204 /* Assignment, increment, decrement, function call and comma
1205 operators, and statement expressions, cannot occur in constant
1206 expressions if evaluated / outside of sizeof. (Function calls
1207 were handled above, though VA_ARG_EXPR is treated like a function
1208 call here, and statement expressions are handled through
1209 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1210 switch (code)
1212 case MODIFY_EXPR:
1213 case PREDECREMENT_EXPR:
1214 case PREINCREMENT_EXPR:
1215 case POSTDECREMENT_EXPR:
1216 case POSTINCREMENT_EXPR:
1217 case COMPOUND_EXPR:
1218 *maybe_const_operands = false;
1219 break;
1221 case VA_ARG_EXPR:
1222 case TARGET_EXPR:
1223 case BIND_EXPR:
1224 case OBJ_TYPE_REF:
1225 *maybe_const_operands = false;
1226 ret = fold (expr);
1227 goto out;
1229 default:
1230 break;
1233 /* Fold individual tree codes as appropriate. */
1234 switch (code)
1236 case COMPOUND_LITERAL_EXPR:
1237 /* Any non-constancy will have been marked in a containing
1238 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1239 goto out;
1241 case COMPONENT_REF:
1242 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1243 op1 = TREE_OPERAND (expr, 1);
1244 op2 = TREE_OPERAND (expr, 2);
1245 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1246 maybe_const_itself);
1247 STRIP_TYPE_NOPS (op0);
1248 if (op0 != orig_op0)
1249 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1250 if (ret != expr)
1252 TREE_READONLY (ret) = TREE_READONLY (expr);
1253 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1255 goto out;
1257 case ARRAY_REF:
1258 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1259 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1260 op2 = TREE_OPERAND (expr, 2);
1261 op3 = TREE_OPERAND (expr, 3);
1262 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1263 maybe_const_itself);
1264 STRIP_TYPE_NOPS (op0);
1265 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1266 maybe_const_itself);
1267 STRIP_TYPE_NOPS (op1);
1268 op1 = decl_constant_value_for_optimization (op1);
1269 if (op0 != orig_op0 || op1 != orig_op1)
1270 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1271 if (ret != expr)
1273 TREE_READONLY (ret) = TREE_READONLY (expr);
1274 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1275 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1277 ret = fold (ret);
1278 goto out;
1280 case COMPOUND_EXPR:
1281 case MODIFY_EXPR:
1282 case PREDECREMENT_EXPR:
1283 case PREINCREMENT_EXPR:
1284 case POSTDECREMENT_EXPR:
1285 case POSTINCREMENT_EXPR:
1286 case PLUS_EXPR:
1287 case MINUS_EXPR:
1288 case MULT_EXPR:
1289 case POINTER_PLUS_EXPR:
1290 case TRUNC_DIV_EXPR:
1291 case CEIL_DIV_EXPR:
1292 case FLOOR_DIV_EXPR:
1293 case TRUNC_MOD_EXPR:
1294 case RDIV_EXPR:
1295 case EXACT_DIV_EXPR:
1296 case LSHIFT_EXPR:
1297 case RSHIFT_EXPR:
1298 case BIT_IOR_EXPR:
1299 case BIT_XOR_EXPR:
1300 case BIT_AND_EXPR:
1301 case LT_EXPR:
1302 case LE_EXPR:
1303 case GT_EXPR:
1304 case GE_EXPR:
1305 case EQ_EXPR:
1306 case NE_EXPR:
1307 case COMPLEX_EXPR:
1308 case TRUTH_AND_EXPR:
1309 case TRUTH_OR_EXPR:
1310 case TRUTH_XOR_EXPR:
1311 case UNORDERED_EXPR:
1312 case ORDERED_EXPR:
1313 case UNLT_EXPR:
1314 case UNLE_EXPR:
1315 case UNGT_EXPR:
1316 case UNGE_EXPR:
1317 case UNEQ_EXPR:
1318 /* Binary operations evaluating both arguments (increment and
1319 decrement are binary internally in GCC). */
1320 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1321 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1322 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1323 maybe_const_itself);
1324 STRIP_TYPE_NOPS (op0);
1325 if (code != MODIFY_EXPR
1326 && code != PREDECREMENT_EXPR
1327 && code != PREINCREMENT_EXPR
1328 && code != POSTDECREMENT_EXPR
1329 && code != POSTINCREMENT_EXPR)
1330 op0 = decl_constant_value_for_optimization (op0);
1331 /* The RHS of a MODIFY_EXPR was fully folded when building that
1332 expression for the sake of conversion warnings. */
1333 if (code != MODIFY_EXPR)
1334 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1335 maybe_const_itself);
1336 STRIP_TYPE_NOPS (op1);
1337 op1 = decl_constant_value_for_optimization (op1);
1338 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1339 ret = in_init
1340 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1341 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1342 else
1343 ret = fold (expr);
1344 if (TREE_OVERFLOW_P (ret)
1345 && !TREE_OVERFLOW_P (op0)
1346 && !TREE_OVERFLOW_P (op1))
1347 overflow_warning (EXPR_LOCATION (expr), ret);
1348 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1349 && TREE_CODE (orig_op1) != INTEGER_CST
1350 && TREE_CODE (op1) == INTEGER_CST
1351 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1352 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1353 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1354 && c_inhibit_evaluation_warnings == 0)
1356 if (tree_int_cst_sgn (op1) < 0)
1357 warning_at (loc, 0, (code == LSHIFT_EXPR
1358 ? G_("left shift count is negative")
1359 : G_("right shift count is negative")));
1360 else if (compare_tree_int (op1,
1361 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1362 >= 0)
1363 warning_at (loc, 0, (code == LSHIFT_EXPR
1364 ? G_("left shift count >= width of type")
1365 : G_("right shift count >= width of type")));
1367 goto out;
1369 case INDIRECT_REF:
1370 case FIX_TRUNC_EXPR:
1371 case FLOAT_EXPR:
1372 CASE_CONVERT:
1373 case ADDR_SPACE_CONVERT_EXPR:
1374 case VIEW_CONVERT_EXPR:
1375 case NON_LVALUE_EXPR:
1376 case NEGATE_EXPR:
1377 case BIT_NOT_EXPR:
1378 case TRUTH_NOT_EXPR:
1379 case ADDR_EXPR:
1380 case CONJ_EXPR:
1381 case REALPART_EXPR:
1382 case IMAGPART_EXPR:
1383 /* Unary operations. */
1384 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1385 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1386 maybe_const_itself);
1387 STRIP_TYPE_NOPS (op0);
1388 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1389 op0 = decl_constant_value_for_optimization (op0);
1390 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1391 not prepared to deal with them if they occur in initializers. */
1392 if (op0 != orig_op0
1393 && code == ADDR_EXPR
1394 && (op1 = get_base_address (op0)) != NULL_TREE
1395 && TREE_CODE (op1) == INDIRECT_REF
1396 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1397 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1398 else if (op0 != orig_op0 || in_init)
1399 ret = in_init
1400 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1401 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1402 else
1403 ret = fold (expr);
1404 if (code == INDIRECT_REF
1405 && ret != expr
1406 && TREE_CODE (ret) == INDIRECT_REF)
1408 TREE_READONLY (ret) = TREE_READONLY (expr);
1409 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1410 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1412 switch (code)
1414 case FIX_TRUNC_EXPR:
1415 case FLOAT_EXPR:
1416 CASE_CONVERT:
1417 /* Don't warn about explicit conversions. We will already
1418 have warned about suspect implicit conversions. */
1419 break;
1421 default:
1422 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1423 overflow_warning (EXPR_LOCATION (expr), ret);
1424 break;
1426 goto out;
1428 case TRUTH_ANDIF_EXPR:
1429 case TRUTH_ORIF_EXPR:
1430 /* Binary operations not necessarily evaluating both
1431 arguments. */
1432 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1433 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1434 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1435 STRIP_TYPE_NOPS (op0);
1437 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1438 ? truthvalue_false_node
1439 : truthvalue_true_node));
1440 c_disable_warnings (unused_p);
1441 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1442 STRIP_TYPE_NOPS (op1);
1443 c_enable_warnings (unused_p);
1445 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1446 ret = in_init
1447 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1448 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1449 else
1450 ret = fold (expr);
1451 *maybe_const_operands &= op0_const;
1452 *maybe_const_itself &= op0_const_self;
1453 if (!(flag_isoc99
1454 && op0_const
1455 && op0_const_self
1456 && (code == TRUTH_ANDIF_EXPR
1457 ? op0 == truthvalue_false_node
1458 : op0 == truthvalue_true_node)))
1459 *maybe_const_operands &= op1_const;
1460 if (!(op0_const
1461 && op0_const_self
1462 && (code == TRUTH_ANDIF_EXPR
1463 ? op0 == truthvalue_false_node
1464 : op0 == truthvalue_true_node)))
1465 *maybe_const_itself &= op1_const_self;
1466 goto out;
1468 case COND_EXPR:
1469 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1470 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1471 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1472 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1474 STRIP_TYPE_NOPS (op0);
1475 c_disable_warnings (op0 == truthvalue_false_node);
1476 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1477 STRIP_TYPE_NOPS (op1);
1478 c_enable_warnings (op0 == truthvalue_false_node);
1480 c_disable_warnings (op0 == truthvalue_true_node);
1481 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1482 STRIP_TYPE_NOPS (op2);
1483 c_enable_warnings (op0 == truthvalue_true_node);
1485 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1486 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1487 else
1488 ret = fold (expr);
1489 *maybe_const_operands &= op0_const;
1490 *maybe_const_itself &= op0_const_self;
1491 if (!(flag_isoc99
1492 && op0_const
1493 && op0_const_self
1494 && op0 == truthvalue_false_node))
1495 *maybe_const_operands &= op1_const;
1496 if (!(op0_const
1497 && op0_const_self
1498 && op0 == truthvalue_false_node))
1499 *maybe_const_itself &= op1_const_self;
1500 if (!(flag_isoc99
1501 && op0_const
1502 && op0_const_self
1503 && op0 == truthvalue_true_node))
1504 *maybe_const_operands &= op2_const;
1505 if (!(op0_const
1506 && op0_const_self
1507 && op0 == truthvalue_true_node))
1508 *maybe_const_itself &= op2_const_self;
1509 goto out;
1511 case EXCESS_PRECISION_EXPR:
1512 /* Each case where an operand with excess precision may be
1513 encountered must remove the EXCESS_PRECISION_EXPR around
1514 inner operands and possibly put one around the whole
1515 expression or possibly convert to the semantic type (which
1516 c_fully_fold does); we cannot tell at this stage which is
1517 appropriate in any particular case. */
1518 gcc_unreachable ();
1520 default:
1521 /* Various codes may appear through folding built-in functions
1522 and their arguments. */
1523 goto out;
1526 out:
1527 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1528 have been done by this point, so remove them again. */
1529 nowarning |= TREE_NO_WARNING (ret);
1530 STRIP_TYPE_NOPS (ret);
1531 if (nowarning && !TREE_NO_WARNING (ret))
1533 if (!CAN_HAVE_LOCATION_P (ret))
1534 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1535 TREE_NO_WARNING (ret) = 1;
1537 if (ret != expr)
1538 protected_set_expr_location (ret, loc);
1539 return ret;
1542 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1543 return EXP. Otherwise, return either EXP or its known constant
1544 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1545 Is the BLKmode test appropriate? */
1547 tree
1548 decl_constant_value_for_optimization (tree exp)
1550 tree ret;
1552 /* This function is only used by C, for c_fully_fold and other
1553 optimization, and may not be correct for C++. */
1554 if (c_dialect_cxx ())
1555 gcc_unreachable ();
1557 if (!optimize
1558 || TREE_CODE (exp) != VAR_DECL
1559 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1560 || DECL_MODE (exp) == BLKmode)
1561 return exp;
1563 ret = decl_constant_value (exp);
1564 /* Avoid unwanted tree sharing between the initializer and current
1565 function's body where the tree can be modified e.g. by the
1566 gimplifier. */
1567 if (ret != exp && TREE_STATIC (exp))
1568 ret = unshare_expr (ret);
1569 return ret;
1572 /* Print a warning if a constant expression had overflow in folding.
1573 Invoke this function on every expression that the language
1574 requires to be a constant expression.
1575 Note the ANSI C standard says it is erroneous for a
1576 constant expression to overflow. */
1578 void
1579 constant_expression_warning (tree value)
1581 if (warn_overflow && pedantic
1582 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1583 || TREE_CODE (value) == FIXED_CST
1584 || TREE_CODE (value) == VECTOR_CST
1585 || TREE_CODE (value) == COMPLEX_CST)
1586 && TREE_OVERFLOW (value))
1587 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1590 /* The same as above but print an unconditional error. */
1591 void
1592 constant_expression_error (tree value)
1594 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1595 || TREE_CODE (value) == FIXED_CST
1596 || TREE_CODE (value) == VECTOR_CST
1597 || TREE_CODE (value) == COMPLEX_CST)
1598 && TREE_OVERFLOW (value))
1599 error ("overflow in constant expression");
1602 /* Print a warning if an expression had overflow in folding and its
1603 operands hadn't.
1605 Invoke this function on every expression that
1606 (1) appears in the source code, and
1607 (2) is a constant expression that overflowed, and
1608 (3) is not already checked by convert_and_check;
1609 however, do not invoke this function on operands of explicit casts
1610 or when the expression is the result of an operator and any operand
1611 already overflowed. */
1613 void
1614 overflow_warning (location_t loc, tree value)
1616 if (c_inhibit_evaluation_warnings != 0)
1617 return;
1619 switch (TREE_CODE (value))
1621 case INTEGER_CST:
1622 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1623 break;
1625 case REAL_CST:
1626 warning_at (loc, OPT_Woverflow,
1627 "floating point overflow in expression");
1628 break;
1630 case FIXED_CST:
1631 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1632 break;
1634 case VECTOR_CST:
1635 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1636 break;
1638 case COMPLEX_CST:
1639 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1640 warning_at (loc, OPT_Woverflow,
1641 "complex integer overflow in expression");
1642 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1643 warning_at (loc, OPT_Woverflow,
1644 "complex floating point overflow in expression");
1645 break;
1647 default:
1648 break;
1652 /* Warn about uses of logical || / && operator in a context where it
1653 is likely that the bitwise equivalent was intended by the
1654 programmer. We have seen an expression in which CODE is a binary
1655 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1656 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1657 void
1658 warn_logical_operator (location_t location, enum tree_code code, tree type,
1659 enum tree_code code_left, tree op_left,
1660 enum tree_code ARG_UNUSED (code_right), tree op_right)
1662 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1663 int in0_p, in1_p, in_p;
1664 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1665 bool strict_overflow_p = false;
1667 if (code != TRUTH_ANDIF_EXPR
1668 && code != TRUTH_AND_EXPR
1669 && code != TRUTH_ORIF_EXPR
1670 && code != TRUTH_OR_EXPR)
1671 return;
1673 /* Warn if &&/|| are being used in a context where it is
1674 likely that the bitwise equivalent was intended by the
1675 programmer. That is, an expression such as op && MASK
1676 where op should not be any boolean expression, nor a
1677 constant, and mask seems to be a non-boolean integer constant. */
1678 if (!truth_value_p (code_left)
1679 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1680 && !CONSTANT_CLASS_P (op_left)
1681 && !TREE_NO_WARNING (op_left)
1682 && TREE_CODE (op_right) == INTEGER_CST
1683 && !integer_zerop (op_right)
1684 && !integer_onep (op_right))
1686 if (or_op)
1687 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1688 " applied to non-boolean constant");
1689 else
1690 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1691 " applied to non-boolean constant");
1692 TREE_NO_WARNING (op_left) = true;
1693 return;
1696 /* We do not warn for constants because they are typical of macro
1697 expansions that test for features. */
1698 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1699 return;
1701 /* This warning only makes sense with logical operands. */
1702 if (!(truth_value_p (TREE_CODE (op_left))
1703 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1704 || !(truth_value_p (TREE_CODE (op_right))
1705 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1706 return;
1708 /* The range computations only work with scalars. */
1709 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1710 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1711 return;
1713 /* We first test whether either side separately is trivially true
1714 (with OR) or trivially false (with AND). If so, do not warn.
1715 This is a common idiom for testing ranges of data types in
1716 portable code. */
1717 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1718 if (!lhs)
1719 return;
1720 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1721 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1723 /* If this is an OR operation, invert both sides; now, the result
1724 should be always false to get a warning. */
1725 if (or_op)
1726 in0_p = !in0_p;
1728 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1729 if (tem && integer_zerop (tem))
1730 return;
1732 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1733 if (!rhs)
1734 return;
1735 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1736 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1738 /* If this is an OR operation, invert both sides; now, the result
1739 should be always false to get a warning. */
1740 if (or_op)
1741 in1_p = !in1_p;
1743 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1744 if (tem && integer_zerop (tem))
1745 return;
1747 /* If both expressions have the same operand, if we can merge the
1748 ranges, and if the range test is always false, then warn. */
1749 if (operand_equal_p (lhs, rhs, 0)
1750 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1751 in1_p, low1, high1)
1752 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1753 type, lhs, in_p, low, high))
1754 && integer_zerop (tem))
1756 if (or_op)
1757 warning_at (location, OPT_Wlogical_op,
1758 "logical %<or%> "
1759 "of collectively exhaustive tests is always true");
1760 else
1761 warning_at (location, OPT_Wlogical_op,
1762 "logical %<and%> "
1763 "of mutually exclusive tests is always false");
1767 /* Warn about logical not used on the left hand side operand of a comparison.
1768 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1769 Do not warn if RHS is of a boolean type. */
1771 void
1772 warn_logical_not_parentheses (location_t location, enum tree_code code,
1773 tree rhs)
1775 if (TREE_CODE_CLASS (code) != tcc_comparison
1776 || TREE_TYPE (rhs) == NULL_TREE
1777 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1778 return;
1780 warning_at (location, OPT_Wlogical_not_parentheses,
1781 "logical not is only applied to the left hand side of "
1782 "comparison");
1785 /* Warn if EXP contains any computations whose results are not used.
1786 Return true if a warning is printed; false otherwise. LOCUS is the
1787 (potential) location of the expression. */
1789 bool
1790 warn_if_unused_value (const_tree exp, location_t locus)
1792 restart:
1793 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1794 return false;
1796 /* Don't warn about void constructs. This includes casting to void,
1797 void function calls, and statement expressions with a final cast
1798 to void. */
1799 if (VOID_TYPE_P (TREE_TYPE (exp)))
1800 return false;
1802 if (EXPR_HAS_LOCATION (exp))
1803 locus = EXPR_LOCATION (exp);
1805 switch (TREE_CODE (exp))
1807 case PREINCREMENT_EXPR:
1808 case POSTINCREMENT_EXPR:
1809 case PREDECREMENT_EXPR:
1810 case POSTDECREMENT_EXPR:
1811 case MODIFY_EXPR:
1812 case INIT_EXPR:
1813 case TARGET_EXPR:
1814 case CALL_EXPR:
1815 case TRY_CATCH_EXPR:
1816 case WITH_CLEANUP_EXPR:
1817 case EXIT_EXPR:
1818 case VA_ARG_EXPR:
1819 return false;
1821 case BIND_EXPR:
1822 /* For a binding, warn if no side effect within it. */
1823 exp = BIND_EXPR_BODY (exp);
1824 goto restart;
1826 case SAVE_EXPR:
1827 case NON_LVALUE_EXPR:
1828 case NOP_EXPR:
1829 exp = TREE_OPERAND (exp, 0);
1830 goto restart;
1832 case TRUTH_ORIF_EXPR:
1833 case TRUTH_ANDIF_EXPR:
1834 /* In && or ||, warn if 2nd operand has no side effect. */
1835 exp = TREE_OPERAND (exp, 1);
1836 goto restart;
1838 case COMPOUND_EXPR:
1839 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1840 return true;
1841 /* Let people do `(foo (), 0)' without a warning. */
1842 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1843 return false;
1844 exp = TREE_OPERAND (exp, 1);
1845 goto restart;
1847 case COND_EXPR:
1848 /* If this is an expression with side effects, don't warn; this
1849 case commonly appears in macro expansions. */
1850 if (TREE_SIDE_EFFECTS (exp))
1851 return false;
1852 goto warn;
1854 case INDIRECT_REF:
1855 /* Don't warn about automatic dereferencing of references, since
1856 the user cannot control it. */
1857 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1859 exp = TREE_OPERAND (exp, 0);
1860 goto restart;
1862 /* Fall through. */
1864 default:
1865 /* Referencing a volatile value is a side effect, so don't warn. */
1866 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1867 && TREE_THIS_VOLATILE (exp))
1868 return false;
1870 /* If this is an expression which has no operands, there is no value
1871 to be unused. There are no such language-independent codes,
1872 but front ends may define such. */
1873 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1874 return false;
1876 warn:
1877 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1882 /* Print a warning about casts that might indicate violation
1883 of strict aliasing rules if -Wstrict-aliasing is used and
1884 strict aliasing mode is in effect. OTYPE is the original
1885 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1887 bool
1888 strict_aliasing_warning (tree otype, tree type, tree expr)
1890 /* Strip pointer conversion chains and get to the correct original type. */
1891 STRIP_NOPS (expr);
1892 otype = TREE_TYPE (expr);
1894 if (!(flag_strict_aliasing
1895 && POINTER_TYPE_P (type)
1896 && POINTER_TYPE_P (otype)
1897 && !VOID_TYPE_P (TREE_TYPE (type)))
1898 /* If the type we are casting to is a ref-all pointer
1899 dereferencing it is always valid. */
1900 || TYPE_REF_CAN_ALIAS_ALL (type))
1901 return false;
1903 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1904 && (DECL_P (TREE_OPERAND (expr, 0))
1905 || handled_component_p (TREE_OPERAND (expr, 0))))
1907 /* Casting the address of an object to non void pointer. Warn
1908 if the cast breaks type based aliasing. */
1909 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1911 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1912 "might break strict-aliasing rules");
1913 return true;
1915 else
1917 /* warn_strict_aliasing >= 3. This includes the default (3).
1918 Only warn if the cast is dereferenced immediately. */
1919 alias_set_type set1 =
1920 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1921 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1923 if (set1 != set2 && set2 != 0
1924 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1926 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1927 "pointer will break strict-aliasing rules");
1928 return true;
1930 else if (warn_strict_aliasing == 2
1931 && !alias_sets_must_conflict_p (set1, set2))
1933 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1934 "pointer might break strict-aliasing rules");
1935 return true;
1939 else
1940 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1942 /* At this level, warn for any conversions, even if an address is
1943 not taken in the same statement. This will likely produce many
1944 false positives, but could be useful to pinpoint problems that
1945 are not revealed at higher levels. */
1946 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1947 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1948 if (!COMPLETE_TYPE_P (type)
1949 || !alias_sets_must_conflict_p (set1, set2))
1951 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1952 "pointer might break strict-aliasing rules");
1953 return true;
1957 return false;
1960 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1961 sizeof as last operand of certain builtins. */
1963 void
1964 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1965 vec<tree, va_gc> *params, tree *sizeof_arg,
1966 bool (*comp_types) (tree, tree))
1968 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1969 bool strop = false, cmp = false;
1970 unsigned int idx = ~0;
1971 location_t loc;
1973 if (TREE_CODE (callee) != FUNCTION_DECL
1974 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1975 || vec_safe_length (params) <= 1)
1976 return;
1978 switch (DECL_FUNCTION_CODE (callee))
1980 case BUILT_IN_STRNCMP:
1981 case BUILT_IN_STRNCASECMP:
1982 cmp = true;
1983 /* FALLTHRU */
1984 case BUILT_IN_STRNCPY:
1985 case BUILT_IN_STRNCPY_CHK:
1986 case BUILT_IN_STRNCAT:
1987 case BUILT_IN_STRNCAT_CHK:
1988 case BUILT_IN_STPNCPY:
1989 case BUILT_IN_STPNCPY_CHK:
1990 strop = true;
1991 /* FALLTHRU */
1992 case BUILT_IN_MEMCPY:
1993 case BUILT_IN_MEMCPY_CHK:
1994 case BUILT_IN_MEMMOVE:
1995 case BUILT_IN_MEMMOVE_CHK:
1996 if (params->length () < 3)
1997 return;
1998 src = (*params)[1];
1999 dest = (*params)[0];
2000 idx = 2;
2001 break;
2002 case BUILT_IN_BCOPY:
2003 if (params->length () < 3)
2004 return;
2005 src = (*params)[0];
2006 dest = (*params)[1];
2007 idx = 2;
2008 break;
2009 case BUILT_IN_MEMCMP:
2010 case BUILT_IN_BCMP:
2011 if (params->length () < 3)
2012 return;
2013 src = (*params)[1];
2014 dest = (*params)[0];
2015 idx = 2;
2016 cmp = true;
2017 break;
2018 case BUILT_IN_MEMSET:
2019 case BUILT_IN_MEMSET_CHK:
2020 if (params->length () < 3)
2021 return;
2022 dest = (*params)[0];
2023 idx = 2;
2024 break;
2025 case BUILT_IN_BZERO:
2026 dest = (*params)[0];
2027 idx = 1;
2028 break;
2029 case BUILT_IN_STRNDUP:
2030 src = (*params)[0];
2031 strop = true;
2032 idx = 1;
2033 break;
2034 case BUILT_IN_MEMCHR:
2035 if (params->length () < 3)
2036 return;
2037 src = (*params)[0];
2038 idx = 2;
2039 break;
2040 case BUILT_IN_SNPRINTF:
2041 case BUILT_IN_SNPRINTF_CHK:
2042 case BUILT_IN_VSNPRINTF:
2043 case BUILT_IN_VSNPRINTF_CHK:
2044 dest = (*params)[0];
2045 idx = 1;
2046 strop = true;
2047 break;
2048 default:
2049 break;
2052 if (idx >= 3)
2053 return;
2055 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2056 return;
2058 type = TYPE_P (sizeof_arg[idx])
2059 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2060 if (!POINTER_TYPE_P (type))
2061 return;
2063 if (dest
2064 && (tem = tree_strip_nop_conversions (dest))
2065 && POINTER_TYPE_P (TREE_TYPE (tem))
2066 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2067 return;
2069 if (src
2070 && (tem = tree_strip_nop_conversions (src))
2071 && POINTER_TYPE_P (TREE_TYPE (tem))
2072 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2073 return;
2075 loc = sizeof_arg_loc[idx];
2077 if (dest && !cmp)
2079 if (!TYPE_P (sizeof_arg[idx])
2080 && operand_equal_p (dest, sizeof_arg[idx], 0)
2081 && comp_types (TREE_TYPE (dest), type))
2083 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2084 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2085 "argument to %<sizeof%> in %qD call is the same "
2086 "expression as the destination; did you mean to "
2087 "remove the addressof?", callee);
2088 else if ((TYPE_PRECISION (TREE_TYPE (type))
2089 == TYPE_PRECISION (char_type_node))
2090 || strop)
2091 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2092 "argument to %<sizeof%> in %qD call is the same "
2093 "expression as the destination; did you mean to "
2094 "provide an explicit length?", callee);
2095 else
2096 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2097 "argument to %<sizeof%> in %qD call is the same "
2098 "expression as the destination; did you mean to "
2099 "dereference it?", callee);
2100 return;
2103 if (POINTER_TYPE_P (TREE_TYPE (dest))
2104 && !strop
2105 && comp_types (TREE_TYPE (dest), type)
2106 && !VOID_TYPE_P (TREE_TYPE (type)))
2108 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2109 "argument to %<sizeof%> in %qD call is the same "
2110 "pointer type %qT as the destination; expected %qT "
2111 "or an explicit length", callee, TREE_TYPE (dest),
2112 TREE_TYPE (TREE_TYPE (dest)));
2113 return;
2117 if (src && !cmp)
2119 if (!TYPE_P (sizeof_arg[idx])
2120 && operand_equal_p (src, sizeof_arg[idx], 0)
2121 && comp_types (TREE_TYPE (src), type))
2123 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2124 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2125 "argument to %<sizeof%> in %qD call is the same "
2126 "expression as the source; did you mean to "
2127 "remove the addressof?", callee);
2128 else if ((TYPE_PRECISION (TREE_TYPE (type))
2129 == TYPE_PRECISION (char_type_node))
2130 || strop)
2131 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2132 "argument to %<sizeof%> in %qD call is the same "
2133 "expression as the source; did you mean to "
2134 "provide an explicit length?", callee);
2135 else
2136 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2137 "argument to %<sizeof%> in %qD call is the same "
2138 "expression as the source; did you mean to "
2139 "dereference it?", callee);
2140 return;
2143 if (POINTER_TYPE_P (TREE_TYPE (src))
2144 && !strop
2145 && comp_types (TREE_TYPE (src), type)
2146 && !VOID_TYPE_P (TREE_TYPE (type)))
2148 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2149 "argument to %<sizeof%> in %qD call is the same "
2150 "pointer type %qT as the source; expected %qT "
2151 "or an explicit length", callee, TREE_TYPE (src),
2152 TREE_TYPE (TREE_TYPE (src)));
2153 return;
2157 if (dest)
2159 if (!TYPE_P (sizeof_arg[idx])
2160 && operand_equal_p (dest, sizeof_arg[idx], 0)
2161 && comp_types (TREE_TYPE (dest), type))
2163 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2164 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2165 "argument to %<sizeof%> in %qD call is the same "
2166 "expression as the first source; did you mean to "
2167 "remove the addressof?", callee);
2168 else if ((TYPE_PRECISION (TREE_TYPE (type))
2169 == TYPE_PRECISION (char_type_node))
2170 || strop)
2171 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2172 "argument to %<sizeof%> in %qD call is the same "
2173 "expression as the first source; did you mean to "
2174 "provide an explicit length?", callee);
2175 else
2176 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2177 "argument to %<sizeof%> in %qD call is the same "
2178 "expression as the first source; did you mean to "
2179 "dereference it?", callee);
2180 return;
2183 if (POINTER_TYPE_P (TREE_TYPE (dest))
2184 && !strop
2185 && comp_types (TREE_TYPE (dest), type)
2186 && !VOID_TYPE_P (TREE_TYPE (type)))
2188 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2189 "argument to %<sizeof%> in %qD call is the same "
2190 "pointer type %qT as the first source; expected %qT "
2191 "or an explicit length", callee, TREE_TYPE (dest),
2192 TREE_TYPE (TREE_TYPE (dest)));
2193 return;
2197 if (src)
2199 if (!TYPE_P (sizeof_arg[idx])
2200 && operand_equal_p (src, sizeof_arg[idx], 0)
2201 && comp_types (TREE_TYPE (src), type))
2203 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2204 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2205 "argument to %<sizeof%> in %qD call is the same "
2206 "expression as the second source; did you mean to "
2207 "remove the addressof?", callee);
2208 else if ((TYPE_PRECISION (TREE_TYPE (type))
2209 == TYPE_PRECISION (char_type_node))
2210 || strop)
2211 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2212 "argument to %<sizeof%> in %qD call is the same "
2213 "expression as the second source; did you mean to "
2214 "provide an explicit length?", callee);
2215 else
2216 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2217 "argument to %<sizeof%> in %qD call is the same "
2218 "expression as the second source; did you mean to "
2219 "dereference it?", callee);
2220 return;
2223 if (POINTER_TYPE_P (TREE_TYPE (src))
2224 && !strop
2225 && comp_types (TREE_TYPE (src), type)
2226 && !VOID_TYPE_P (TREE_TYPE (type)))
2228 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2229 "argument to %<sizeof%> in %qD call is the same "
2230 "pointer type %qT as the second source; expected %qT "
2231 "or an explicit length", callee, TREE_TYPE (src),
2232 TREE_TYPE (TREE_TYPE (src)));
2233 return;
2239 /* Warn for unlikely, improbable, or stupid DECL declarations
2240 of `main'. */
2242 void
2243 check_main_parameter_types (tree decl)
2245 function_args_iterator iter;
2246 tree type;
2247 int argct = 0;
2249 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2251 /* XXX void_type_node belies the abstraction. */
2252 if (type == void_type_node || type == error_mark_node )
2253 break;
2255 tree t = type;
2256 if (TYPE_ATOMIC (t))
2257 pedwarn (input_location, OPT_Wmain,
2258 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2259 type, decl);
2260 while (POINTER_TYPE_P (t))
2262 t = TREE_TYPE (t);
2263 if (TYPE_ATOMIC (t))
2264 pedwarn (input_location, OPT_Wmain,
2265 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2266 type, decl);
2269 ++argct;
2270 switch (argct)
2272 case 1:
2273 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2274 pedwarn (input_location, OPT_Wmain,
2275 "first argument of %q+D should be %<int%>", decl);
2276 break;
2278 case 2:
2279 if (TREE_CODE (type) != POINTER_TYPE
2280 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2281 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2282 != char_type_node))
2283 pedwarn (input_location, OPT_Wmain,
2284 "second argument of %q+D should be %<char **%>", decl);
2285 break;
2287 case 3:
2288 if (TREE_CODE (type) != POINTER_TYPE
2289 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2290 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2291 != char_type_node))
2292 pedwarn (input_location, OPT_Wmain,
2293 "third argument of %q+D should probably be "
2294 "%<char **%>", decl);
2295 break;
2299 /* It is intentional that this message does not mention the third
2300 argument because it's only mentioned in an appendix of the
2301 standard. */
2302 if (argct > 0 && (argct < 2 || argct > 3))
2303 pedwarn (input_location, OPT_Wmain,
2304 "%q+D takes only zero or two arguments", decl);
2306 if (stdarg_p (TREE_TYPE (decl)))
2307 pedwarn (input_location, OPT_Wmain,
2308 "%q+D declared as variadic function", decl);
2311 /* vector_targets_convertible_p is used for vector pointer types. The
2312 callers perform various checks that the qualifiers are satisfactory,
2313 while OTOH vector_targets_convertible_p ignores the number of elements
2314 in the vectors. That's fine with vector pointers as we can consider,
2315 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2316 and that does not require and conversion of the pointer values.
2317 In contrast, vector_types_convertible_p and
2318 vector_types_compatible_elements_p are used for vector value types. */
2319 /* True if pointers to distinct types T1 and T2 can be converted to
2320 each other without an explicit cast. Only returns true for opaque
2321 vector types. */
2322 bool
2323 vector_targets_convertible_p (const_tree t1, const_tree t2)
2325 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2326 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2327 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2328 return true;
2330 return false;
2333 /* vector_types_convertible_p is used for vector value types.
2334 It could in principle call vector_targets_convertible_p as a subroutine,
2335 but then the check for vector type would be duplicated with its callers,
2336 and also the purpose of vector_targets_convertible_p would become
2337 muddled.
2338 Where vector_types_convertible_p returns true, a conversion might still be
2339 needed to make the types match.
2340 In contrast, vector_targets_convertible_p is used for vector pointer
2341 values, and vector_types_compatible_elements_p is used specifically
2342 in the context for binary operators, as a check if use is possible without
2343 conversion. */
2344 /* True if vector types T1 and T2 can be converted to each other
2345 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2346 can only be converted with -flax-vector-conversions yet that is not
2347 in effect, emit a note telling the user about that option if such
2348 a note has not previously been emitted. */
2349 bool
2350 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2352 static bool emitted_lax_note = false;
2353 bool convertible_lax;
2355 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2356 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2357 return true;
2359 convertible_lax =
2360 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2361 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2362 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2363 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2364 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2366 if (!convertible_lax || flag_lax_vector_conversions)
2367 return convertible_lax;
2369 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2370 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2371 return true;
2373 if (emit_lax_note && !emitted_lax_note)
2375 emitted_lax_note = true;
2376 inform (input_location, "use -flax-vector-conversions to permit "
2377 "conversions between vectors with differing "
2378 "element types or numbers of subparts");
2381 return false;
2384 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2385 and have vector types, V0 has the same type as V1, and the number of
2386 elements of V0, V1, MASK is the same.
2388 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2389 called with two arguments. In this case implementation passes the
2390 first argument twice in order to share the same tree code. This fact
2391 could enable the mask-values being twice the vector length. This is
2392 an implementation accident and this semantics is not guaranteed to
2393 the user. */
2394 tree
2395 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2396 bool complain)
2398 tree ret;
2399 bool wrap = true;
2400 bool maybe_const = false;
2401 bool two_arguments = false;
2403 if (v1 == NULL_TREE)
2405 two_arguments = true;
2406 v1 = v0;
2409 if (v0 == error_mark_node || v1 == error_mark_node
2410 || mask == error_mark_node)
2411 return error_mark_node;
2413 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2414 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2416 if (complain)
2417 error_at (loc, "__builtin_shuffle last argument must "
2418 "be an integer vector");
2419 return error_mark_node;
2422 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2423 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2425 if (complain)
2426 error_at (loc, "__builtin_shuffle arguments must be vectors");
2427 return error_mark_node;
2430 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2432 if (complain)
2433 error_at (loc, "__builtin_shuffle argument vectors must be of "
2434 "the same type");
2435 return error_mark_node;
2438 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2439 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2440 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2441 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2443 if (complain)
2444 error_at (loc, "__builtin_shuffle number of elements of the "
2445 "argument vector(s) and the mask vector should "
2446 "be the same");
2447 return error_mark_node;
2450 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2451 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2453 if (complain)
2454 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2455 "must have the same size as inner type of the mask");
2456 return error_mark_node;
2459 if (!c_dialect_cxx ())
2461 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2462 v0 = c_fully_fold (v0, false, &maybe_const);
2463 wrap &= maybe_const;
2465 if (two_arguments)
2466 v1 = v0 = save_expr (v0);
2467 else
2469 v1 = c_fully_fold (v1, false, &maybe_const);
2470 wrap &= maybe_const;
2473 mask = c_fully_fold (mask, false, &maybe_const);
2474 wrap &= maybe_const;
2476 else if (two_arguments)
2477 v1 = v0 = save_expr (v0);
2479 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2481 if (!c_dialect_cxx () && !wrap)
2482 ret = c_wrap_maybe_const (ret, true);
2484 return ret;
2487 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2488 to integral type. */
2490 static tree
2491 c_common_get_narrower (tree op, int *unsignedp_ptr)
2493 op = get_narrower (op, unsignedp_ptr);
2495 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2496 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2498 /* C++0x scoped enumerations don't implicitly convert to integral
2499 type; if we stripped an explicit conversion to a larger type we
2500 need to replace it so common_type will still work. */
2501 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2502 TYPE_UNSIGNED (TREE_TYPE (op)));
2503 op = fold_convert (type, op);
2505 return op;
2508 /* This is a helper function of build_binary_op.
2510 For certain operations if both args were extended from the same
2511 smaller type, do the arithmetic in that type and then extend.
2513 BITWISE indicates a bitwise operation.
2514 For them, this optimization is safe only if
2515 both args are zero-extended or both are sign-extended.
2516 Otherwise, we might change the result.
2517 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2518 but calculated in (unsigned short) it would be (unsigned short)-1.
2520 tree
2521 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2523 int unsigned0, unsigned1;
2524 tree arg0, arg1;
2525 int uns;
2526 tree type;
2528 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2529 excessive narrowing when we call get_narrower below. For
2530 example, suppose that OP0 is of unsigned int extended
2531 from signed char and that RESULT_TYPE is long long int.
2532 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2533 like
2535 (long long int) (unsigned int) signed_char
2537 which get_narrower would narrow down to
2539 (unsigned int) signed char
2541 If we do not cast OP0 first, get_narrower would return
2542 signed_char, which is inconsistent with the case of the
2543 explicit cast. */
2544 op0 = convert (result_type, op0);
2545 op1 = convert (result_type, op1);
2547 arg0 = c_common_get_narrower (op0, &unsigned0);
2548 arg1 = c_common_get_narrower (op1, &unsigned1);
2550 /* UNS is 1 if the operation to be done is an unsigned one. */
2551 uns = TYPE_UNSIGNED (result_type);
2553 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2554 but it *requires* conversion to FINAL_TYPE. */
2556 if ((TYPE_PRECISION (TREE_TYPE (op0))
2557 == TYPE_PRECISION (TREE_TYPE (arg0)))
2558 && TREE_TYPE (op0) != result_type)
2559 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2560 if ((TYPE_PRECISION (TREE_TYPE (op1))
2561 == TYPE_PRECISION (TREE_TYPE (arg1)))
2562 && TREE_TYPE (op1) != result_type)
2563 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2565 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2567 /* For bitwise operations, signedness of nominal type
2568 does not matter. Consider only how operands were extended. */
2569 if (bitwise)
2570 uns = unsigned0;
2572 /* Note that in all three cases below we refrain from optimizing
2573 an unsigned operation on sign-extended args.
2574 That would not be valid. */
2576 /* Both args variable: if both extended in same way
2577 from same width, do it in that width.
2578 Do it unsigned if args were zero-extended. */
2579 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2580 < TYPE_PRECISION (result_type))
2581 && (TYPE_PRECISION (TREE_TYPE (arg1))
2582 == TYPE_PRECISION (TREE_TYPE (arg0)))
2583 && unsigned0 == unsigned1
2584 && (unsigned0 || !uns))
2585 return c_common_signed_or_unsigned_type
2586 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2588 else if (TREE_CODE (arg0) == INTEGER_CST
2589 && (unsigned1 || !uns)
2590 && (TYPE_PRECISION (TREE_TYPE (arg1))
2591 < TYPE_PRECISION (result_type))
2592 && (type
2593 = c_common_signed_or_unsigned_type (unsigned1,
2594 TREE_TYPE (arg1)))
2595 && !POINTER_TYPE_P (type)
2596 && int_fits_type_p (arg0, type))
2597 return type;
2599 else if (TREE_CODE (arg1) == INTEGER_CST
2600 && (unsigned0 || !uns)
2601 && (TYPE_PRECISION (TREE_TYPE (arg0))
2602 < TYPE_PRECISION (result_type))
2603 && (type
2604 = c_common_signed_or_unsigned_type (unsigned0,
2605 TREE_TYPE (arg0)))
2606 && !POINTER_TYPE_P (type)
2607 && int_fits_type_p (arg1, type))
2608 return type;
2610 return result_type;
2613 /* Checks if expression EXPR of real/integer type cannot be converted
2614 to the real/integer type TYPE. Function returns non-zero when:
2615 * EXPR is a constant which cannot be exactly converted to TYPE.
2616 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2617 for EXPR type and TYPE being both integers or both real.
2618 * EXPR is not a constant of real type and TYPE is an integer.
2619 * EXPR is not a constant of integer type which cannot be
2620 exactly converted to real type.
2621 Function allows conversions between types of different signedness and
2622 can return SAFE_CONVERSION (zero) in that case. Function can produce
2623 signedness warnings if PRODUCE_WARNS is true. */
2625 enum conversion_safety
2626 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2628 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2629 tree expr_type = TREE_TYPE (expr);
2630 loc = expansion_point_location_if_in_system_header (loc);
2632 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2634 /* Warn for real constant that is not an exact integer converted
2635 to integer type. */
2636 if (TREE_CODE (expr_type) == REAL_TYPE
2637 && TREE_CODE (type) == INTEGER_TYPE)
2639 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2640 give_warning = UNSAFE_REAL;
2642 /* Warn for an integer constant that does not fit into integer type. */
2643 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2644 && TREE_CODE (type) == INTEGER_TYPE
2645 && !int_fits_type_p (expr, type))
2647 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2648 && tree_int_cst_sgn (expr) < 0)
2650 if (produce_warns)
2651 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2652 " implicitly converted to unsigned type");
2654 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2656 if (produce_warns)
2657 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2658 " constant value to negative integer");
2660 else
2661 give_warning = UNSAFE_OTHER;
2663 else if (TREE_CODE (type) == REAL_TYPE)
2665 /* Warn for an integer constant that does not fit into real type. */
2666 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2668 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2669 if (!exact_real_truncate (TYPE_MODE (type), &a))
2670 give_warning = UNSAFE_REAL;
2672 /* Warn for a real constant that does not fit into a smaller
2673 real type. */
2674 else if (TREE_CODE (expr_type) == REAL_TYPE
2675 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2677 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2678 if (!exact_real_truncate (TYPE_MODE (type), &a))
2679 give_warning = UNSAFE_REAL;
2683 else
2685 /* Warn for real types converted to integer types. */
2686 if (TREE_CODE (expr_type) == REAL_TYPE
2687 && TREE_CODE (type) == INTEGER_TYPE)
2688 give_warning = UNSAFE_REAL;
2690 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2691 && TREE_CODE (type) == INTEGER_TYPE)
2693 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2694 expr = get_unwidened (expr, 0);
2695 expr_type = TREE_TYPE (expr);
2697 /* Don't warn for short y; short x = ((int)y & 0xff); */
2698 if (TREE_CODE (expr) == BIT_AND_EXPR
2699 || TREE_CODE (expr) == BIT_IOR_EXPR
2700 || TREE_CODE (expr) == BIT_XOR_EXPR)
2702 /* If both args were extended from a shortest type,
2703 use that type if that is safe. */
2704 expr_type = shorten_binary_op (expr_type,
2705 TREE_OPERAND (expr, 0),
2706 TREE_OPERAND (expr, 1),
2707 /* bitwise */1);
2709 if (TREE_CODE (expr) == BIT_AND_EXPR)
2711 tree op0 = TREE_OPERAND (expr, 0);
2712 tree op1 = TREE_OPERAND (expr, 1);
2713 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2714 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2716 /* If one of the operands is a non-negative constant
2717 that fits in the target type, then the type of the
2718 other operand does not matter. */
2719 if ((TREE_CODE (op0) == INTEGER_CST
2720 && int_fits_type_p (op0, c_common_signed_type (type))
2721 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2722 || (TREE_CODE (op1) == INTEGER_CST
2723 && int_fits_type_p (op1, c_common_signed_type (type))
2724 && int_fits_type_p (op1,
2725 c_common_unsigned_type (type))))
2726 return SAFE_CONVERSION;
2727 /* If constant is unsigned and fits in the target
2728 type, then the result will also fit. */
2729 else if ((TREE_CODE (op0) == INTEGER_CST
2730 && unsigned0
2731 && int_fits_type_p (op0, type))
2732 || (TREE_CODE (op1) == INTEGER_CST
2733 && unsigned1
2734 && int_fits_type_p (op1, type)))
2735 return SAFE_CONVERSION;
2738 /* Warn for integer types converted to smaller integer types. */
2739 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2740 give_warning = UNSAFE_OTHER;
2742 /* When they are the same width but different signedness,
2743 then the value may change. */
2744 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2745 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2746 /* Even when converted to a bigger type, if the type is
2747 unsigned but expr is signed, then negative values
2748 will be changed. */
2749 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2750 && produce_warns)
2751 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2752 "may change the sign of the result",
2753 type, expr_type);
2756 /* Warn for integer types converted to real types if and only if
2757 all the range of values of the integer type cannot be
2758 represented by the real type. */
2759 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2760 && TREE_CODE (type) == REAL_TYPE)
2762 tree type_low_bound, type_high_bound;
2763 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2765 /* Don't warn about char y = 0xff; float x = (int) y; */
2766 expr = get_unwidened (expr, 0);
2767 expr_type = TREE_TYPE (expr);
2769 type_low_bound = TYPE_MIN_VALUE (expr_type);
2770 type_high_bound = TYPE_MAX_VALUE (expr_type);
2771 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2772 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2774 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2775 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2776 give_warning = UNSAFE_OTHER;
2779 /* Warn for real types converted to smaller real types. */
2780 else if (TREE_CODE (expr_type) == REAL_TYPE
2781 && TREE_CODE (type) == REAL_TYPE
2782 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2783 give_warning = UNSAFE_REAL;
2786 return give_warning;
2789 /* Warns if the conversion of EXPR to TYPE may alter a value.
2790 This is a helper function for warnings_for_convert_and_check. */
2792 static void
2793 conversion_warning (location_t loc, tree type, tree expr)
2795 tree expr_type = TREE_TYPE (expr);
2796 enum conversion_safety conversion_kind;
2798 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2799 return;
2801 /* This may happen, because for LHS op= RHS we preevaluate
2802 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2803 means we could no longer see the code of the EXPR. */
2804 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2805 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2806 if (TREE_CODE (expr) == SAVE_EXPR)
2807 expr = TREE_OPERAND (expr, 0);
2809 switch (TREE_CODE (expr))
2811 case EQ_EXPR:
2812 case NE_EXPR:
2813 case LE_EXPR:
2814 case GE_EXPR:
2815 case LT_EXPR:
2816 case GT_EXPR:
2817 case TRUTH_ANDIF_EXPR:
2818 case TRUTH_ORIF_EXPR:
2819 case TRUTH_AND_EXPR:
2820 case TRUTH_OR_EXPR:
2821 case TRUTH_XOR_EXPR:
2822 case TRUTH_NOT_EXPR:
2823 /* Conversion from boolean to a signed:1 bit-field (which only
2824 can hold the values 0 and -1) doesn't lose information - but
2825 it does change the value. */
2826 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2827 warning_at (loc, OPT_Wconversion,
2828 "conversion to %qT from boolean expression", type);
2829 return;
2831 case REAL_CST:
2832 case INTEGER_CST:
2833 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2834 if (conversion_kind == UNSAFE_REAL)
2835 warning_at (loc, OPT_Wfloat_conversion,
2836 "conversion to %qT alters %qT constant value",
2837 type, expr_type);
2838 else if (conversion_kind)
2839 warning_at (loc, OPT_Wconversion,
2840 "conversion to %qT alters %qT constant value",
2841 type, expr_type);
2842 return;
2844 case COND_EXPR:
2846 /* In case of COND_EXPR, we do not care about the type of
2847 COND_EXPR, only about the conversion of each operand. */
2848 tree op1 = TREE_OPERAND (expr, 1);
2849 tree op2 = TREE_OPERAND (expr, 2);
2851 conversion_warning (loc, type, op1);
2852 conversion_warning (loc, type, op2);
2853 return;
2856 default: /* 'expr' is not a constant. */
2857 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2858 if (conversion_kind == UNSAFE_REAL)
2859 warning_at (loc, OPT_Wfloat_conversion,
2860 "conversion to %qT from %qT may alter its value",
2861 type, expr_type);
2862 else if (conversion_kind)
2863 warning_at (loc, OPT_Wconversion,
2864 "conversion to %qT from %qT may alter its value",
2865 type, expr_type);
2869 /* Produce warnings after a conversion. RESULT is the result of
2870 converting EXPR to TYPE. This is a helper function for
2871 convert_and_check and cp_convert_and_check. */
2873 void
2874 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2875 tree result)
2877 loc = expansion_point_location_if_in_system_header (loc);
2879 if (TREE_CODE (expr) == INTEGER_CST
2880 && (TREE_CODE (type) == INTEGER_TYPE
2881 || TREE_CODE (type) == ENUMERAL_TYPE)
2882 && !int_fits_type_p (expr, type))
2884 /* Do not diagnose overflow in a constant expression merely
2885 because a conversion overflowed. */
2886 if (TREE_OVERFLOW (result))
2887 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2889 if (TYPE_UNSIGNED (type))
2891 /* This detects cases like converting -129 or 256 to
2892 unsigned char. */
2893 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2894 warning_at (loc, OPT_Woverflow,
2895 "large integer implicitly truncated to unsigned type");
2896 else
2897 conversion_warning (loc, type, expr);
2899 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2900 warning_at (loc, OPT_Woverflow,
2901 "overflow in implicit constant conversion");
2902 /* No warning for converting 0x80000000 to int. */
2903 else if (pedantic
2904 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2905 || TYPE_PRECISION (TREE_TYPE (expr))
2906 != TYPE_PRECISION (type)))
2907 warning_at (loc, OPT_Woverflow,
2908 "overflow in implicit constant conversion");
2910 else
2911 conversion_warning (loc, type, expr);
2913 else if ((TREE_CODE (result) == INTEGER_CST
2914 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2915 warning_at (loc, OPT_Woverflow,
2916 "overflow in implicit constant conversion");
2917 else
2918 conversion_warning (loc, type, expr);
2922 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2923 Invoke this function on every expression that is converted implicitly,
2924 i.e. because of language rules and not because of an explicit cast. */
2926 tree
2927 convert_and_check (location_t loc, tree type, tree expr)
2929 tree result;
2930 tree expr_for_warning;
2932 /* Convert from a value with possible excess precision rather than
2933 via the semantic type, but do not warn about values not fitting
2934 exactly in the semantic type. */
2935 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2937 tree orig_type = TREE_TYPE (expr);
2938 expr = TREE_OPERAND (expr, 0);
2939 expr_for_warning = convert (orig_type, expr);
2940 if (orig_type == type)
2941 return expr_for_warning;
2943 else
2944 expr_for_warning = expr;
2946 if (TREE_TYPE (expr) == type)
2947 return expr;
2949 result = convert (type, expr);
2951 if (c_inhibit_evaluation_warnings == 0
2952 && !TREE_OVERFLOW_P (expr)
2953 && result != error_mark_node)
2954 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2956 return result;
2959 /* A node in a list that describes references to variables (EXPR), which are
2960 either read accesses if WRITER is zero, or write accesses, in which case
2961 WRITER is the parent of EXPR. */
2962 struct tlist
2964 struct tlist *next;
2965 tree expr, writer;
2968 /* Used to implement a cache the results of a call to verify_tree. We only
2969 use this for SAVE_EXPRs. */
2970 struct tlist_cache
2972 struct tlist_cache *next;
2973 struct tlist *cache_before_sp;
2974 struct tlist *cache_after_sp;
2975 tree expr;
2978 /* Obstack to use when allocating tlist structures, and corresponding
2979 firstobj. */
2980 static struct obstack tlist_obstack;
2981 static char *tlist_firstobj = 0;
2983 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2984 warnings. */
2985 static struct tlist *warned_ids;
2986 /* SAVE_EXPRs need special treatment. We process them only once and then
2987 cache the results. */
2988 static struct tlist_cache *save_expr_cache;
2990 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2991 static void merge_tlist (struct tlist **, struct tlist *, int);
2992 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2993 static int warning_candidate_p (tree);
2994 static bool candidate_equal_p (const_tree, const_tree);
2995 static void warn_for_collisions (struct tlist *);
2996 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2997 static struct tlist *new_tlist (struct tlist *, tree, tree);
2999 /* Create a new struct tlist and fill in its fields. */
3000 static struct tlist *
3001 new_tlist (struct tlist *next, tree t, tree writer)
3003 struct tlist *l;
3004 l = XOBNEW (&tlist_obstack, struct tlist);
3005 l->next = next;
3006 l->expr = t;
3007 l->writer = writer;
3008 return l;
3011 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3012 is nonnull, we ignore any node we find which has a writer equal to it. */
3014 static void
3015 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3017 while (add)
3019 struct tlist *next = add->next;
3020 if (!copy)
3021 add->next = *to;
3022 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3023 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3024 add = next;
3028 /* Merge the nodes of ADD into TO. This merging process is done so that for
3029 each variable that already exists in TO, no new node is added; however if
3030 there is a write access recorded in ADD, and an occurrence on TO is only
3031 a read access, then the occurrence in TO will be modified to record the
3032 write. */
3034 static void
3035 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3037 struct tlist **end = to;
3039 while (*end)
3040 end = &(*end)->next;
3042 while (add)
3044 int found = 0;
3045 struct tlist *tmp2;
3046 struct tlist *next = add->next;
3048 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3049 if (candidate_equal_p (tmp2->expr, add->expr))
3051 found = 1;
3052 if (!tmp2->writer)
3053 tmp2->writer = add->writer;
3055 if (!found)
3057 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3058 end = &(*end)->next;
3059 *end = 0;
3061 add = next;
3065 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3066 references in list LIST conflict with it, excluding reads if ONLY writers
3067 is nonzero. */
3069 static void
3070 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3071 int only_writes)
3073 struct tlist *tmp;
3075 /* Avoid duplicate warnings. */
3076 for (tmp = warned_ids; tmp; tmp = tmp->next)
3077 if (candidate_equal_p (tmp->expr, written))
3078 return;
3080 while (list)
3082 if (candidate_equal_p (list->expr, written)
3083 && !candidate_equal_p (list->writer, writer)
3084 && (!only_writes || list->writer))
3086 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3087 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3088 OPT_Wsequence_point, "operation on %qE may be undefined",
3089 list->expr);
3091 list = list->next;
3095 /* Given a list LIST of references to variables, find whether any of these
3096 can cause conflicts due to missing sequence points. */
3098 static void
3099 warn_for_collisions (struct tlist *list)
3101 struct tlist *tmp;
3103 for (tmp = list; tmp; tmp = tmp->next)
3105 if (tmp->writer)
3106 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3110 /* Return nonzero if X is a tree that can be verified by the sequence point
3111 warnings. */
3112 static int
3113 warning_candidate_p (tree x)
3115 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3116 return 0;
3118 if (TREE_CODE (x) == BLOCK)
3119 return 0;
3121 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3122 (lvalue_p) crash on TRY/CATCH. */
3123 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3124 return 0;
3126 if (!lvalue_p (x))
3127 return 0;
3129 /* No point to track non-const calls, they will never satisfy
3130 operand_equal_p. */
3131 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3132 return 0;
3134 if (TREE_CODE (x) == STRING_CST)
3135 return 0;
3137 return 1;
3140 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3141 static bool
3142 candidate_equal_p (const_tree x, const_tree y)
3144 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3147 /* Walk the tree X, and record accesses to variables. If X is written by the
3148 parent tree, WRITER is the parent.
3149 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3150 expression or its only operand forces a sequence point, then everything up
3151 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3152 in PNO_SP.
3153 Once we return, we will have emitted warnings if any subexpression before
3154 such a sequence point could be undefined. On a higher level, however, the
3155 sequence point may not be relevant, and we'll merge the two lists.
3157 Example: (b++, a) + b;
3158 The call that processes the COMPOUND_EXPR will store the increment of B
3159 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3160 processes the PLUS_EXPR will need to merge the two lists so that
3161 eventually, all accesses end up on the same list (and we'll warn about the
3162 unordered subexpressions b++ and b.
3164 A note on merging. If we modify the former example so that our expression
3165 becomes
3166 (b++, b) + a
3167 care must be taken not simply to add all three expressions into the final
3168 PNO_SP list. The function merge_tlist takes care of that by merging the
3169 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3170 way, so that no more than one access to B is recorded. */
3172 static void
3173 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3174 tree writer)
3176 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3177 enum tree_code code;
3178 enum tree_code_class cl;
3180 /* X may be NULL if it is the operand of an empty statement expression
3181 ({ }). */
3182 if (x == NULL)
3183 return;
3185 restart:
3186 code = TREE_CODE (x);
3187 cl = TREE_CODE_CLASS (code);
3189 if (warning_candidate_p (x))
3190 *pno_sp = new_tlist (*pno_sp, x, writer);
3192 switch (code)
3194 case CONSTRUCTOR:
3195 case SIZEOF_EXPR:
3196 return;
3198 case COMPOUND_EXPR:
3199 case TRUTH_ANDIF_EXPR:
3200 case TRUTH_ORIF_EXPR:
3201 tmp_before = tmp_nosp = tmp_list3 = 0;
3202 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3203 warn_for_collisions (tmp_nosp);
3204 merge_tlist (pbefore_sp, tmp_before, 0);
3205 merge_tlist (pbefore_sp, tmp_nosp, 0);
3206 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3207 merge_tlist (pbefore_sp, tmp_list3, 0);
3208 return;
3210 case COND_EXPR:
3211 tmp_before = tmp_list2 = 0;
3212 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3213 warn_for_collisions (tmp_list2);
3214 merge_tlist (pbefore_sp, tmp_before, 0);
3215 merge_tlist (pbefore_sp, tmp_list2, 0);
3217 tmp_list3 = tmp_nosp = 0;
3218 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3219 warn_for_collisions (tmp_nosp);
3220 merge_tlist (pbefore_sp, tmp_list3, 0);
3222 tmp_list3 = tmp_list2 = 0;
3223 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3224 warn_for_collisions (tmp_list2);
3225 merge_tlist (pbefore_sp, tmp_list3, 0);
3226 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3227 two first, to avoid warning for (a ? b++ : b++). */
3228 merge_tlist (&tmp_nosp, tmp_list2, 0);
3229 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3230 return;
3232 case PREDECREMENT_EXPR:
3233 case PREINCREMENT_EXPR:
3234 case POSTDECREMENT_EXPR:
3235 case POSTINCREMENT_EXPR:
3236 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3237 return;
3239 case MODIFY_EXPR:
3240 tmp_before = tmp_nosp = tmp_list3 = 0;
3241 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3242 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3243 /* Expressions inside the LHS are not ordered wrt. the sequence points
3244 in the RHS. Example:
3245 *a = (a++, 2)
3246 Despite the fact that the modification of "a" is in the before_sp
3247 list (tmp_before), it conflicts with the use of "a" in the LHS.
3248 We can handle this by adding the contents of tmp_list3
3249 to those of tmp_before, and redoing the collision warnings for that
3250 list. */
3251 add_tlist (&tmp_before, tmp_list3, x, 1);
3252 warn_for_collisions (tmp_before);
3253 /* Exclude the LHS itself here; we first have to merge it into the
3254 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3255 didn't exclude the LHS, we'd get it twice, once as a read and once
3256 as a write. */
3257 add_tlist (pno_sp, tmp_list3, x, 0);
3258 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3260 merge_tlist (pbefore_sp, tmp_before, 0);
3261 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3262 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3263 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3264 return;
3266 case CALL_EXPR:
3267 /* We need to warn about conflicts among arguments and conflicts between
3268 args and the function address. Side effects of the function address,
3269 however, are not ordered by the sequence point of the call. */
3271 call_expr_arg_iterator iter;
3272 tree arg;
3273 tmp_before = tmp_nosp = 0;
3274 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3275 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3277 tmp_list2 = tmp_list3 = 0;
3278 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3279 merge_tlist (&tmp_list3, tmp_list2, 0);
3280 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3282 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3283 warn_for_collisions (tmp_before);
3284 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3285 return;
3288 case TREE_LIST:
3289 /* Scan all the list, e.g. indices of multi dimensional array. */
3290 while (x)
3292 tmp_before = tmp_nosp = 0;
3293 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3294 merge_tlist (&tmp_nosp, tmp_before, 0);
3295 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3296 x = TREE_CHAIN (x);
3298 return;
3300 case SAVE_EXPR:
3302 struct tlist_cache *t;
3303 for (t = save_expr_cache; t; t = t->next)
3304 if (candidate_equal_p (t->expr, x))
3305 break;
3307 if (!t)
3309 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3310 t->next = save_expr_cache;
3311 t->expr = x;
3312 save_expr_cache = t;
3314 tmp_before = tmp_nosp = 0;
3315 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3316 warn_for_collisions (tmp_nosp);
3318 tmp_list3 = 0;
3319 merge_tlist (&tmp_list3, tmp_nosp, 0);
3320 t->cache_before_sp = tmp_before;
3321 t->cache_after_sp = tmp_list3;
3323 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3324 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3325 return;
3328 case ADDR_EXPR:
3329 x = TREE_OPERAND (x, 0);
3330 if (DECL_P (x))
3331 return;
3332 writer = 0;
3333 goto restart;
3335 default:
3336 /* For other expressions, simply recurse on their operands.
3337 Manual tail recursion for unary expressions.
3338 Other non-expressions need not be processed. */
3339 if (cl == tcc_unary)
3341 x = TREE_OPERAND (x, 0);
3342 writer = 0;
3343 goto restart;
3345 else if (IS_EXPR_CODE_CLASS (cl))
3347 int lp;
3348 int max = TREE_OPERAND_LENGTH (x);
3349 for (lp = 0; lp < max; lp++)
3351 tmp_before = tmp_nosp = 0;
3352 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3353 merge_tlist (&tmp_nosp, tmp_before, 0);
3354 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3357 return;
3361 /* Try to warn for undefined behavior in EXPR due to missing sequence
3362 points. */
3364 DEBUG_FUNCTION void
3365 verify_sequence_points (tree expr)
3367 struct tlist *before_sp = 0, *after_sp = 0;
3369 warned_ids = 0;
3370 save_expr_cache = 0;
3371 if (tlist_firstobj == 0)
3373 gcc_obstack_init (&tlist_obstack);
3374 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3377 verify_tree (expr, &before_sp, &after_sp, 0);
3378 warn_for_collisions (after_sp);
3379 obstack_free (&tlist_obstack, tlist_firstobj);
3382 /* Validate the expression after `case' and apply default promotions. */
3384 static tree
3385 check_case_value (location_t loc, tree value)
3387 if (value == NULL_TREE)
3388 return value;
3390 if (TREE_CODE (value) == INTEGER_CST)
3391 /* Promote char or short to int. */
3392 value = perform_integral_promotions (value);
3393 else if (value != error_mark_node)
3395 error_at (loc, "case label does not reduce to an integer constant");
3396 value = error_mark_node;
3399 constant_expression_warning (value);
3401 return value;
3404 /* See if the case values LOW and HIGH are in the range of the original
3405 type (i.e. before the default conversion to int) of the switch testing
3406 expression.
3407 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3408 the type before promoting it. CASE_LOW_P is a pointer to the lower
3409 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3410 if the case is not a case range.
3411 The caller has to make sure that we are not called with NULL for
3412 CASE_LOW_P (i.e. the default case).
3413 Returns true if the case label is in range of ORIG_TYPE (saturated or
3414 untouched) or false if the label is out of range. */
3416 static bool
3417 check_case_bounds (location_t loc, tree type, tree orig_type,
3418 tree *case_low_p, tree *case_high_p)
3420 tree min_value, max_value;
3421 tree case_low = *case_low_p;
3422 tree case_high = case_high_p ? *case_high_p : case_low;
3424 /* If there was a problem with the original type, do nothing. */
3425 if (orig_type == error_mark_node)
3426 return true;
3428 min_value = TYPE_MIN_VALUE (orig_type);
3429 max_value = TYPE_MAX_VALUE (orig_type);
3431 /* Case label is less than minimum for type. */
3432 if (tree_int_cst_compare (case_low, min_value) < 0
3433 && tree_int_cst_compare (case_high, min_value) < 0)
3435 warning_at (loc, 0, "case label value is less than minimum value "
3436 "for type");
3437 return false;
3440 /* Case value is greater than maximum for type. */
3441 if (tree_int_cst_compare (case_low, max_value) > 0
3442 && tree_int_cst_compare (case_high, max_value) > 0)
3444 warning_at (loc, 0, "case label value exceeds maximum value for type");
3445 return false;
3448 /* Saturate lower case label value to minimum. */
3449 if (tree_int_cst_compare (case_high, min_value) >= 0
3450 && tree_int_cst_compare (case_low, min_value) < 0)
3452 warning_at (loc, 0, "lower value in case label range"
3453 " less than minimum value for type");
3454 case_low = min_value;
3457 /* Saturate upper case label value to maximum. */
3458 if (tree_int_cst_compare (case_low, max_value) <= 0
3459 && tree_int_cst_compare (case_high, max_value) > 0)
3461 warning_at (loc, 0, "upper value in case label range"
3462 " exceeds maximum value for type");
3463 case_high = max_value;
3466 if (*case_low_p != case_low)
3467 *case_low_p = convert (type, case_low);
3468 if (case_high_p && *case_high_p != case_high)
3469 *case_high_p = convert (type, case_high);
3471 return true;
3474 /* Return an integer type with BITS bits of precision,
3475 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3477 tree
3478 c_common_type_for_size (unsigned int bits, int unsignedp)
3480 int i;
3482 if (bits == TYPE_PRECISION (integer_type_node))
3483 return unsignedp ? unsigned_type_node : integer_type_node;
3485 if (bits == TYPE_PRECISION (signed_char_type_node))
3486 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3488 if (bits == TYPE_PRECISION (short_integer_type_node))
3489 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3491 if (bits == TYPE_PRECISION (long_integer_type_node))
3492 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3494 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3495 return (unsignedp ? long_long_unsigned_type_node
3496 : long_long_integer_type_node);
3498 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3499 if (int_n_enabled_p[i]
3500 && bits == int_n_data[i].bitsize)
3501 return (unsignedp ? int_n_trees[i].unsigned_type
3502 : int_n_trees[i].signed_type);
3504 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3505 return (unsignedp ? widest_unsigned_literal_type_node
3506 : widest_integer_literal_type_node);
3508 if (bits <= TYPE_PRECISION (intQI_type_node))
3509 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3511 if (bits <= TYPE_PRECISION (intHI_type_node))
3512 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3514 if (bits <= TYPE_PRECISION (intSI_type_node))
3515 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3517 if (bits <= TYPE_PRECISION (intDI_type_node))
3518 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3520 return 0;
3523 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3524 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3525 and saturating if SATP is nonzero, otherwise not saturating. */
3527 tree
3528 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3529 int unsignedp, int satp)
3531 machine_mode mode;
3532 if (ibit == 0)
3533 mode = unsignedp ? UQQmode : QQmode;
3534 else
3535 mode = unsignedp ? UHAmode : HAmode;
3537 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3538 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3539 break;
3541 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3543 sorry ("GCC cannot support operators with integer types and "
3544 "fixed-point types that have too many integral and "
3545 "fractional bits together");
3546 return 0;
3549 return c_common_type_for_mode (mode, satp);
3552 /* Used for communication between c_common_type_for_mode and
3553 c_register_builtin_type. */
3554 tree registered_builtin_types;
3556 /* Return a data type that has machine mode MODE.
3557 If the mode is an integer,
3558 then UNSIGNEDP selects between signed and unsigned types.
3559 If the mode is a fixed-point mode,
3560 then UNSIGNEDP selects between saturating and nonsaturating types. */
3562 tree
3563 c_common_type_for_mode (machine_mode mode, int unsignedp)
3565 tree t;
3566 int i;
3568 if (mode == TYPE_MODE (integer_type_node))
3569 return unsignedp ? unsigned_type_node : integer_type_node;
3571 if (mode == TYPE_MODE (signed_char_type_node))
3572 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3574 if (mode == TYPE_MODE (short_integer_type_node))
3575 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3577 if (mode == TYPE_MODE (long_integer_type_node))
3578 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3580 if (mode == TYPE_MODE (long_long_integer_type_node))
3581 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3583 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3584 if (int_n_enabled_p[i]
3585 && mode == int_n_data[i].m)
3586 return (unsignedp ? int_n_trees[i].unsigned_type
3587 : int_n_trees[i].signed_type);
3589 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3590 return unsignedp ? widest_unsigned_literal_type_node
3591 : widest_integer_literal_type_node;
3593 if (mode == QImode)
3594 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3596 if (mode == HImode)
3597 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3599 if (mode == SImode)
3600 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3602 if (mode == DImode)
3603 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3605 #if HOST_BITS_PER_WIDE_INT >= 64
3606 if (mode == TYPE_MODE (intTI_type_node))
3607 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3608 #endif
3610 if (mode == TYPE_MODE (float_type_node))
3611 return float_type_node;
3613 if (mode == TYPE_MODE (double_type_node))
3614 return double_type_node;
3616 if (mode == TYPE_MODE (long_double_type_node))
3617 return long_double_type_node;
3619 if (mode == TYPE_MODE (void_type_node))
3620 return void_type_node;
3622 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3623 return (unsignedp
3624 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3625 : make_signed_type (GET_MODE_PRECISION (mode)));
3627 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3628 return (unsignedp
3629 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3630 : make_signed_type (GET_MODE_PRECISION (mode)));
3632 if (COMPLEX_MODE_P (mode))
3634 machine_mode inner_mode;
3635 tree inner_type;
3637 if (mode == TYPE_MODE (complex_float_type_node))
3638 return complex_float_type_node;
3639 if (mode == TYPE_MODE (complex_double_type_node))
3640 return complex_double_type_node;
3641 if (mode == TYPE_MODE (complex_long_double_type_node))
3642 return complex_long_double_type_node;
3644 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3645 return complex_integer_type_node;
3647 inner_mode = GET_MODE_INNER (mode);
3648 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3649 if (inner_type != NULL_TREE)
3650 return build_complex_type (inner_type);
3652 else if (VECTOR_MODE_P (mode))
3654 machine_mode inner_mode = GET_MODE_INNER (mode);
3655 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3656 if (inner_type != NULL_TREE)
3657 return build_vector_type_for_mode (inner_type, mode);
3660 if (mode == TYPE_MODE (dfloat32_type_node))
3661 return dfloat32_type_node;
3662 if (mode == TYPE_MODE (dfloat64_type_node))
3663 return dfloat64_type_node;
3664 if (mode == TYPE_MODE (dfloat128_type_node))
3665 return dfloat128_type_node;
3667 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3669 if (mode == TYPE_MODE (short_fract_type_node))
3670 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3671 if (mode == TYPE_MODE (fract_type_node))
3672 return unsignedp ? sat_fract_type_node : fract_type_node;
3673 if (mode == TYPE_MODE (long_fract_type_node))
3674 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3675 if (mode == TYPE_MODE (long_long_fract_type_node))
3676 return unsignedp ? sat_long_long_fract_type_node
3677 : long_long_fract_type_node;
3679 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3680 return unsignedp ? sat_unsigned_short_fract_type_node
3681 : unsigned_short_fract_type_node;
3682 if (mode == TYPE_MODE (unsigned_fract_type_node))
3683 return unsignedp ? sat_unsigned_fract_type_node
3684 : unsigned_fract_type_node;
3685 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3686 return unsignedp ? sat_unsigned_long_fract_type_node
3687 : unsigned_long_fract_type_node;
3688 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3689 return unsignedp ? sat_unsigned_long_long_fract_type_node
3690 : unsigned_long_long_fract_type_node;
3692 if (mode == TYPE_MODE (short_accum_type_node))
3693 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3694 if (mode == TYPE_MODE (accum_type_node))
3695 return unsignedp ? sat_accum_type_node : accum_type_node;
3696 if (mode == TYPE_MODE (long_accum_type_node))
3697 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3698 if (mode == TYPE_MODE (long_long_accum_type_node))
3699 return unsignedp ? sat_long_long_accum_type_node
3700 : long_long_accum_type_node;
3702 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3703 return unsignedp ? sat_unsigned_short_accum_type_node
3704 : unsigned_short_accum_type_node;
3705 if (mode == TYPE_MODE (unsigned_accum_type_node))
3706 return unsignedp ? sat_unsigned_accum_type_node
3707 : unsigned_accum_type_node;
3708 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3709 return unsignedp ? sat_unsigned_long_accum_type_node
3710 : unsigned_long_accum_type_node;
3711 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3712 return unsignedp ? sat_unsigned_long_long_accum_type_node
3713 : unsigned_long_long_accum_type_node;
3715 if (mode == QQmode)
3716 return unsignedp ? sat_qq_type_node : qq_type_node;
3717 if (mode == HQmode)
3718 return unsignedp ? sat_hq_type_node : hq_type_node;
3719 if (mode == SQmode)
3720 return unsignedp ? sat_sq_type_node : sq_type_node;
3721 if (mode == DQmode)
3722 return unsignedp ? sat_dq_type_node : dq_type_node;
3723 if (mode == TQmode)
3724 return unsignedp ? sat_tq_type_node : tq_type_node;
3726 if (mode == UQQmode)
3727 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3728 if (mode == UHQmode)
3729 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3730 if (mode == USQmode)
3731 return unsignedp ? sat_usq_type_node : usq_type_node;
3732 if (mode == UDQmode)
3733 return unsignedp ? sat_udq_type_node : udq_type_node;
3734 if (mode == UTQmode)
3735 return unsignedp ? sat_utq_type_node : utq_type_node;
3737 if (mode == HAmode)
3738 return unsignedp ? sat_ha_type_node : ha_type_node;
3739 if (mode == SAmode)
3740 return unsignedp ? sat_sa_type_node : sa_type_node;
3741 if (mode == DAmode)
3742 return unsignedp ? sat_da_type_node : da_type_node;
3743 if (mode == TAmode)
3744 return unsignedp ? sat_ta_type_node : ta_type_node;
3746 if (mode == UHAmode)
3747 return unsignedp ? sat_uha_type_node : uha_type_node;
3748 if (mode == USAmode)
3749 return unsignedp ? sat_usa_type_node : usa_type_node;
3750 if (mode == UDAmode)
3751 return unsignedp ? sat_uda_type_node : uda_type_node;
3752 if (mode == UTAmode)
3753 return unsignedp ? sat_uta_type_node : uta_type_node;
3756 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3757 if (TYPE_MODE (TREE_VALUE (t)) == mode
3758 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3759 return TREE_VALUE (t);
3761 return 0;
3764 tree
3765 c_common_unsigned_type (tree type)
3767 return c_common_signed_or_unsigned_type (1, type);
3770 /* Return a signed type the same as TYPE in other respects. */
3772 tree
3773 c_common_signed_type (tree type)
3775 return c_common_signed_or_unsigned_type (0, type);
3778 /* Return a type the same as TYPE except unsigned or
3779 signed according to UNSIGNEDP. */
3781 tree
3782 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3784 tree type1;
3785 int i;
3787 /* This block of code emulates the behavior of the old
3788 c_common_unsigned_type. In particular, it returns
3789 long_unsigned_type_node if passed a long, even when a int would
3790 have the same size. This is necessary for warnings to work
3791 correctly in archs where sizeof(int) == sizeof(long) */
3793 type1 = TYPE_MAIN_VARIANT (type);
3794 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3795 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3796 if (type1 == integer_type_node || type1 == unsigned_type_node)
3797 return unsignedp ? unsigned_type_node : integer_type_node;
3798 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3799 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3800 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3801 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3802 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3803 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3805 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3806 if (int_n_enabled_p[i]
3807 && (type1 == int_n_trees[i].unsigned_type
3808 || type1 == int_n_trees[i].signed_type))
3809 return (unsignedp ? int_n_trees[i].unsigned_type
3810 : int_n_trees[i].signed_type);
3812 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3813 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3814 #if HOST_BITS_PER_WIDE_INT >= 64
3815 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3816 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3817 #endif
3818 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3819 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3820 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3821 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3822 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3823 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3824 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3825 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3827 #define C_COMMON_FIXED_TYPES(NAME) \
3828 if (type1 == short_ ## NAME ## _type_node \
3829 || type1 == unsigned_short_ ## NAME ## _type_node) \
3830 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3831 : short_ ## NAME ## _type_node; \
3832 if (type1 == NAME ## _type_node \
3833 || type1 == unsigned_ ## NAME ## _type_node) \
3834 return unsignedp ? unsigned_ ## NAME ## _type_node \
3835 : NAME ## _type_node; \
3836 if (type1 == long_ ## NAME ## _type_node \
3837 || type1 == unsigned_long_ ## NAME ## _type_node) \
3838 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3839 : long_ ## NAME ## _type_node; \
3840 if (type1 == long_long_ ## NAME ## _type_node \
3841 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3842 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3843 : long_long_ ## NAME ## _type_node;
3845 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3846 if (type1 == NAME ## _type_node \
3847 || type1 == u ## NAME ## _type_node) \
3848 return unsignedp ? u ## NAME ## _type_node \
3849 : NAME ## _type_node;
3851 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3852 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3853 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3854 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3855 : sat_ ## short_ ## NAME ## _type_node; \
3856 if (type1 == sat_ ## NAME ## _type_node \
3857 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3858 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3859 : sat_ ## NAME ## _type_node; \
3860 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3861 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3862 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3863 : sat_ ## long_ ## NAME ## _type_node; \
3864 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3865 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3866 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3867 : sat_ ## long_long_ ## NAME ## _type_node;
3869 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3870 if (type1 == sat_ ## NAME ## _type_node \
3871 || type1 == sat_ ## u ## NAME ## _type_node) \
3872 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3873 : sat_ ## NAME ## _type_node;
3875 C_COMMON_FIXED_TYPES (fract);
3876 C_COMMON_FIXED_TYPES_SAT (fract);
3877 C_COMMON_FIXED_TYPES (accum);
3878 C_COMMON_FIXED_TYPES_SAT (accum);
3880 C_COMMON_FIXED_MODE_TYPES (qq);
3881 C_COMMON_FIXED_MODE_TYPES (hq);
3882 C_COMMON_FIXED_MODE_TYPES (sq);
3883 C_COMMON_FIXED_MODE_TYPES (dq);
3884 C_COMMON_FIXED_MODE_TYPES (tq);
3885 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3886 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3887 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3888 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3889 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3890 C_COMMON_FIXED_MODE_TYPES (ha);
3891 C_COMMON_FIXED_MODE_TYPES (sa);
3892 C_COMMON_FIXED_MODE_TYPES (da);
3893 C_COMMON_FIXED_MODE_TYPES (ta);
3894 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3895 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3896 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3897 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3899 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3900 the precision; they have precision set to match their range, but
3901 may use a wider mode to match an ABI. If we change modes, we may
3902 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3903 the precision as well, so as to yield correct results for
3904 bit-field types. C++ does not have these separate bit-field
3905 types, and producing a signed or unsigned variant of an
3906 ENUMERAL_TYPE may cause other problems as well. */
3908 if (!INTEGRAL_TYPE_P (type)
3909 || TYPE_UNSIGNED (type) == unsignedp)
3910 return type;
3912 #define TYPE_OK(node) \
3913 (TYPE_MODE (type) == TYPE_MODE (node) \
3914 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3915 if (TYPE_OK (signed_char_type_node))
3916 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3917 if (TYPE_OK (integer_type_node))
3918 return unsignedp ? unsigned_type_node : integer_type_node;
3919 if (TYPE_OK (short_integer_type_node))
3920 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3921 if (TYPE_OK (long_integer_type_node))
3922 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3923 if (TYPE_OK (long_long_integer_type_node))
3924 return (unsignedp ? long_long_unsigned_type_node
3925 : long_long_integer_type_node);
3927 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3928 if (int_n_enabled_p[i]
3929 && TYPE_MODE (type) == int_n_data[i].m
3930 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3931 return (unsignedp ? int_n_trees[i].unsigned_type
3932 : int_n_trees[i].signed_type);
3934 if (TYPE_OK (widest_integer_literal_type_node))
3935 return (unsignedp ? widest_unsigned_literal_type_node
3936 : widest_integer_literal_type_node);
3938 #if HOST_BITS_PER_WIDE_INT >= 64
3939 if (TYPE_OK (intTI_type_node))
3940 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3941 #endif
3942 if (TYPE_OK (intDI_type_node))
3943 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3944 if (TYPE_OK (intSI_type_node))
3945 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3946 if (TYPE_OK (intHI_type_node))
3947 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3948 if (TYPE_OK (intQI_type_node))
3949 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3950 #undef TYPE_OK
3952 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3955 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3957 tree
3958 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3960 int i;
3962 /* Extended integer types of the same width as a standard type have
3963 lesser rank, so those of the same width as int promote to int or
3964 unsigned int and are valid for printf formats expecting int or
3965 unsigned int. To avoid such special cases, avoid creating
3966 extended integer types for bit-fields if a standard integer type
3967 is available. */
3968 if (width == TYPE_PRECISION (integer_type_node))
3969 return unsignedp ? unsigned_type_node : integer_type_node;
3970 if (width == TYPE_PRECISION (signed_char_type_node))
3971 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3972 if (width == TYPE_PRECISION (short_integer_type_node))
3973 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3974 if (width == TYPE_PRECISION (long_integer_type_node))
3975 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3976 if (width == TYPE_PRECISION (long_long_integer_type_node))
3977 return (unsignedp ? long_long_unsigned_type_node
3978 : long_long_integer_type_node);
3979 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3980 if (int_n_enabled_p[i]
3981 && width == int_n_data[i].bitsize)
3982 return (unsignedp ? int_n_trees[i].unsigned_type
3983 : int_n_trees[i].signed_type);
3984 return build_nonstandard_integer_type (width, unsignedp);
3987 /* The C version of the register_builtin_type langhook. */
3989 void
3990 c_register_builtin_type (tree type, const char* name)
3992 tree decl;
3994 decl = build_decl (UNKNOWN_LOCATION,
3995 TYPE_DECL, get_identifier (name), type);
3996 DECL_ARTIFICIAL (decl) = 1;
3997 if (!TYPE_NAME (type))
3998 TYPE_NAME (type) = decl;
3999 pushdecl (decl);
4001 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4004 /* Print an error message for invalid operands to arith operation
4005 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4006 LOCATION is the location of the message. */
4008 void
4009 binary_op_error (location_t location, enum tree_code code,
4010 tree type0, tree type1)
4012 const char *opname;
4014 switch (code)
4016 case PLUS_EXPR:
4017 opname = "+"; break;
4018 case MINUS_EXPR:
4019 opname = "-"; break;
4020 case MULT_EXPR:
4021 opname = "*"; break;
4022 case MAX_EXPR:
4023 opname = "max"; break;
4024 case MIN_EXPR:
4025 opname = "min"; break;
4026 case EQ_EXPR:
4027 opname = "=="; break;
4028 case NE_EXPR:
4029 opname = "!="; break;
4030 case LE_EXPR:
4031 opname = "<="; break;
4032 case GE_EXPR:
4033 opname = ">="; break;
4034 case LT_EXPR:
4035 opname = "<"; break;
4036 case GT_EXPR:
4037 opname = ">"; break;
4038 case LSHIFT_EXPR:
4039 opname = "<<"; break;
4040 case RSHIFT_EXPR:
4041 opname = ">>"; break;
4042 case TRUNC_MOD_EXPR:
4043 case FLOOR_MOD_EXPR:
4044 opname = "%"; break;
4045 case TRUNC_DIV_EXPR:
4046 case FLOOR_DIV_EXPR:
4047 opname = "/"; break;
4048 case BIT_AND_EXPR:
4049 opname = "&"; break;
4050 case BIT_IOR_EXPR:
4051 opname = "|"; break;
4052 case TRUTH_ANDIF_EXPR:
4053 opname = "&&"; break;
4054 case TRUTH_ORIF_EXPR:
4055 opname = "||"; break;
4056 case BIT_XOR_EXPR:
4057 opname = "^"; break;
4058 default:
4059 gcc_unreachable ();
4061 error_at (location,
4062 "invalid operands to binary %s (have %qT and %qT)", opname,
4063 type0, type1);
4066 /* Given an expression as a tree, return its original type. Do this
4067 by stripping any conversion that preserves the sign and precision. */
4068 static tree
4069 expr_original_type (tree expr)
4071 STRIP_SIGN_NOPS (expr);
4072 return TREE_TYPE (expr);
4075 /* Subroutine of build_binary_op, used for comparison operations.
4076 See if the operands have both been converted from subword integer types
4077 and, if so, perhaps change them both back to their original type.
4078 This function is also responsible for converting the two operands
4079 to the proper common type for comparison.
4081 The arguments of this function are all pointers to local variables
4082 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4083 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4085 LOC is the location of the comparison.
4087 If this function returns nonzero, it means that the comparison has
4088 a constant value. What this function returns is an expression for
4089 that value. */
4091 tree
4092 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4093 tree *restype_ptr, enum tree_code *rescode_ptr)
4095 tree type;
4096 tree op0 = *op0_ptr;
4097 tree op1 = *op1_ptr;
4098 int unsignedp0, unsignedp1;
4099 int real1, real2;
4100 tree primop0, primop1;
4101 enum tree_code code = *rescode_ptr;
4103 /* Throw away any conversions to wider types
4104 already present in the operands. */
4106 primop0 = c_common_get_narrower (op0, &unsignedp0);
4107 primop1 = c_common_get_narrower (op1, &unsignedp1);
4109 /* If primopN is first sign-extended from primopN's precision to opN's
4110 precision, then zero-extended from opN's precision to
4111 *restype_ptr precision, shortenings might be invalid. */
4112 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4113 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4114 && !unsignedp0
4115 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4116 primop0 = op0;
4117 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4118 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4119 && !unsignedp1
4120 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4121 primop1 = op1;
4123 /* Handle the case that OP0 does not *contain* a conversion
4124 but it *requires* conversion to FINAL_TYPE. */
4126 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4127 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4128 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4129 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4131 /* If one of the operands must be floated, we cannot optimize. */
4132 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4133 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4135 /* If first arg is constant, swap the args (changing operation
4136 so value is preserved), for canonicalization. Don't do this if
4137 the second arg is 0. */
4139 if (TREE_CONSTANT (primop0)
4140 && !integer_zerop (primop1) && !real_zerop (primop1)
4141 && !fixed_zerop (primop1))
4143 tree tem = primop0;
4144 int temi = unsignedp0;
4145 primop0 = primop1;
4146 primop1 = tem;
4147 tem = op0;
4148 op0 = op1;
4149 op1 = tem;
4150 *op0_ptr = op0;
4151 *op1_ptr = op1;
4152 unsignedp0 = unsignedp1;
4153 unsignedp1 = temi;
4154 temi = real1;
4155 real1 = real2;
4156 real2 = temi;
4158 switch (code)
4160 case LT_EXPR:
4161 code = GT_EXPR;
4162 break;
4163 case GT_EXPR:
4164 code = LT_EXPR;
4165 break;
4166 case LE_EXPR:
4167 code = GE_EXPR;
4168 break;
4169 case GE_EXPR:
4170 code = LE_EXPR;
4171 break;
4172 default:
4173 break;
4175 *rescode_ptr = code;
4178 /* If comparing an integer against a constant more bits wide,
4179 maybe we can deduce a value of 1 or 0 independent of the data.
4180 Or else truncate the constant now
4181 rather than extend the variable at run time.
4183 This is only interesting if the constant is the wider arg.
4184 Also, it is not safe if the constant is unsigned and the
4185 variable arg is signed, since in this case the variable
4186 would be sign-extended and then regarded as unsigned.
4187 Our technique fails in this case because the lowest/highest
4188 possible unsigned results don't follow naturally from the
4189 lowest/highest possible values of the variable operand.
4190 For just EQ_EXPR and NE_EXPR there is another technique that
4191 could be used: see if the constant can be faithfully represented
4192 in the other operand's type, by truncating it and reextending it
4193 and see if that preserves the constant's value. */
4195 if (!real1 && !real2
4196 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4197 && TREE_CODE (primop1) == INTEGER_CST
4198 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4200 int min_gt, max_gt, min_lt, max_lt;
4201 tree maxval, minval;
4202 /* 1 if comparison is nominally unsigned. */
4203 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4204 tree val;
4206 type = c_common_signed_or_unsigned_type (unsignedp0,
4207 TREE_TYPE (primop0));
4209 maxval = TYPE_MAX_VALUE (type);
4210 minval = TYPE_MIN_VALUE (type);
4212 if (unsignedp && !unsignedp0)
4213 *restype_ptr = c_common_signed_type (*restype_ptr);
4215 if (TREE_TYPE (primop1) != *restype_ptr)
4217 /* Convert primop1 to target type, but do not introduce
4218 additional overflow. We know primop1 is an int_cst. */
4219 primop1 = force_fit_type (*restype_ptr,
4220 wide_int::from
4221 (primop1,
4222 TYPE_PRECISION (*restype_ptr),
4223 TYPE_SIGN (TREE_TYPE (primop1))),
4224 0, TREE_OVERFLOW (primop1));
4226 if (type != *restype_ptr)
4228 minval = convert (*restype_ptr, minval);
4229 maxval = convert (*restype_ptr, maxval);
4232 min_gt = tree_int_cst_lt (primop1, minval);
4233 max_gt = tree_int_cst_lt (primop1, maxval);
4234 min_lt = tree_int_cst_lt (minval, primop1);
4235 max_lt = tree_int_cst_lt (maxval, primop1);
4237 val = 0;
4238 /* This used to be a switch, but Genix compiler can't handle that. */
4239 if (code == NE_EXPR)
4241 if (max_lt || min_gt)
4242 val = truthvalue_true_node;
4244 else if (code == EQ_EXPR)
4246 if (max_lt || min_gt)
4247 val = truthvalue_false_node;
4249 else if (code == LT_EXPR)
4251 if (max_lt)
4252 val = truthvalue_true_node;
4253 if (!min_lt)
4254 val = truthvalue_false_node;
4256 else if (code == GT_EXPR)
4258 if (min_gt)
4259 val = truthvalue_true_node;
4260 if (!max_gt)
4261 val = truthvalue_false_node;
4263 else if (code == LE_EXPR)
4265 if (!max_gt)
4266 val = truthvalue_true_node;
4267 if (min_gt)
4268 val = truthvalue_false_node;
4270 else if (code == GE_EXPR)
4272 if (!min_lt)
4273 val = truthvalue_true_node;
4274 if (max_lt)
4275 val = truthvalue_false_node;
4278 /* If primop0 was sign-extended and unsigned comparison specd,
4279 we did a signed comparison above using the signed type bounds.
4280 But the comparison we output must be unsigned.
4282 Also, for inequalities, VAL is no good; but if the signed
4283 comparison had *any* fixed result, it follows that the
4284 unsigned comparison just tests the sign in reverse
4285 (positive values are LE, negative ones GE).
4286 So we can generate an unsigned comparison
4287 against an extreme value of the signed type. */
4289 if (unsignedp && !unsignedp0)
4291 if (val != 0)
4292 switch (code)
4294 case LT_EXPR:
4295 case GE_EXPR:
4296 primop1 = TYPE_MIN_VALUE (type);
4297 val = 0;
4298 break;
4300 case LE_EXPR:
4301 case GT_EXPR:
4302 primop1 = TYPE_MAX_VALUE (type);
4303 val = 0;
4304 break;
4306 default:
4307 break;
4309 type = c_common_unsigned_type (type);
4312 if (TREE_CODE (primop0) != INTEGER_CST)
4314 if (val == truthvalue_false_node)
4315 warning_at (loc, OPT_Wtype_limits,
4316 "comparison is always false due to limited range of data type");
4317 if (val == truthvalue_true_node)
4318 warning_at (loc, OPT_Wtype_limits,
4319 "comparison is always true due to limited range of data type");
4322 if (val != 0)
4324 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4325 if (TREE_SIDE_EFFECTS (primop0))
4326 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4327 return val;
4330 /* Value is not predetermined, but do the comparison
4331 in the type of the operand that is not constant.
4332 TYPE is already properly set. */
4335 /* If either arg is decimal float and the other is float, find the
4336 proper common type to use for comparison. */
4337 else if (real1 && real2
4338 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4339 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4340 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4342 /* If either arg is decimal float and the other is float, fail. */
4343 else if (real1 && real2
4344 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4345 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4346 return 0;
4348 else if (real1 && real2
4349 && (TYPE_PRECISION (TREE_TYPE (primop0))
4350 == TYPE_PRECISION (TREE_TYPE (primop1))))
4351 type = TREE_TYPE (primop0);
4353 /* If args' natural types are both narrower than nominal type
4354 and both extend in the same manner, compare them
4355 in the type of the wider arg.
4356 Otherwise must actually extend both to the nominal
4357 common type lest different ways of extending
4358 alter the result.
4359 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4361 else if (unsignedp0 == unsignedp1 && real1 == real2
4362 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4363 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4365 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4366 type = c_common_signed_or_unsigned_type (unsignedp0
4367 || TYPE_UNSIGNED (*restype_ptr),
4368 type);
4369 /* Make sure shorter operand is extended the right way
4370 to match the longer operand. */
4371 primop0
4372 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4373 TREE_TYPE (primop0)),
4374 primop0);
4375 primop1
4376 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4377 TREE_TYPE (primop1)),
4378 primop1);
4380 else
4382 /* Here we must do the comparison on the nominal type
4383 using the args exactly as we received them. */
4384 type = *restype_ptr;
4385 primop0 = op0;
4386 primop1 = op1;
4388 if (!real1 && !real2 && integer_zerop (primop1)
4389 && TYPE_UNSIGNED (*restype_ptr))
4391 tree value = 0;
4392 /* All unsigned values are >= 0, so we warn. However,
4393 if OP0 is a constant that is >= 0, the signedness of
4394 the comparison isn't an issue, so suppress the
4395 warning. */
4396 bool warn =
4397 warn_type_limits && !in_system_header_at (loc)
4398 && !(TREE_CODE (primop0) == INTEGER_CST
4399 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4400 primop0)))
4401 /* Do not warn for enumeration types. */
4402 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4404 switch (code)
4406 case GE_EXPR:
4407 if (warn)
4408 warning_at (loc, OPT_Wtype_limits,
4409 "comparison of unsigned expression >= 0 is always true");
4410 value = truthvalue_true_node;
4411 break;
4413 case LT_EXPR:
4414 if (warn)
4415 warning_at (loc, OPT_Wtype_limits,
4416 "comparison of unsigned expression < 0 is always false");
4417 value = truthvalue_false_node;
4418 break;
4420 default:
4421 break;
4424 if (value != 0)
4426 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4427 if (TREE_SIDE_EFFECTS (primop0))
4428 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4429 primop0, value);
4430 return value;
4435 *op0_ptr = convert (type, primop0);
4436 *op1_ptr = convert (type, primop1);
4438 *restype_ptr = truthvalue_type_node;
4440 return 0;
4443 /* Return a tree for the sum or difference (RESULTCODE says which)
4444 of pointer PTROP and integer INTOP. */
4446 tree
4447 pointer_int_sum (location_t loc, enum tree_code resultcode,
4448 tree ptrop, tree intop, bool complain)
4450 tree size_exp, ret;
4452 /* The result is a pointer of the same type that is being added. */
4453 tree result_type = TREE_TYPE (ptrop);
4455 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4457 if (complain && warn_pointer_arith)
4458 pedwarn (loc, OPT_Wpointer_arith,
4459 "pointer of type %<void *%> used in arithmetic");
4460 else if (!complain)
4461 return error_mark_node;
4462 size_exp = integer_one_node;
4464 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4466 if (complain && warn_pointer_arith)
4467 pedwarn (loc, OPT_Wpointer_arith,
4468 "pointer to a function used in arithmetic");
4469 else if (!complain)
4470 return error_mark_node;
4471 size_exp = integer_one_node;
4473 else
4474 size_exp = size_in_bytes (TREE_TYPE (result_type));
4476 /* We are manipulating pointer values, so we don't need to warn
4477 about relying on undefined signed overflow. We disable the
4478 warning here because we use integer types so fold won't know that
4479 they are really pointers. */
4480 fold_defer_overflow_warnings ();
4482 /* If what we are about to multiply by the size of the elements
4483 contains a constant term, apply distributive law
4484 and multiply that constant term separately.
4485 This helps produce common subexpressions. */
4486 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4487 && !TREE_CONSTANT (intop)
4488 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4489 && TREE_CONSTANT (size_exp)
4490 /* If the constant comes from pointer subtraction,
4491 skip this optimization--it would cause an error. */
4492 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4493 /* If the constant is unsigned, and smaller than the pointer size,
4494 then we must skip this optimization. This is because it could cause
4495 an overflow error if the constant is negative but INTOP is not. */
4496 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4497 || (TYPE_PRECISION (TREE_TYPE (intop))
4498 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4500 enum tree_code subcode = resultcode;
4501 tree int_type = TREE_TYPE (intop);
4502 if (TREE_CODE (intop) == MINUS_EXPR)
4503 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4504 /* Convert both subexpression types to the type of intop,
4505 because weird cases involving pointer arithmetic
4506 can result in a sum or difference with different type args. */
4507 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4508 subcode, ptrop,
4509 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4510 intop = convert (int_type, TREE_OPERAND (intop, 0));
4513 /* Convert the integer argument to a type the same size as sizetype
4514 so the multiply won't overflow spuriously. */
4515 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4516 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4517 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4518 TYPE_UNSIGNED (sizetype)), intop);
4520 /* Replace the integer argument with a suitable product by the object size.
4521 Do this multiplication as signed, then convert to the appropriate type
4522 for the pointer operation and disregard an overflow that occurred only
4523 because of the sign-extension change in the latter conversion. */
4525 tree t = build_binary_op (loc,
4526 MULT_EXPR, intop,
4527 convert (TREE_TYPE (intop), size_exp), 1);
4528 intop = convert (sizetype, t);
4529 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4530 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4533 /* Create the sum or difference. */
4534 if (resultcode == MINUS_EXPR)
4535 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4537 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4539 fold_undefer_and_ignore_overflow_warnings ();
4541 return ret;
4544 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4545 and if NON_CONST is known not to be permitted in an evaluated part
4546 of a constant expression. */
4548 tree
4549 c_wrap_maybe_const (tree expr, bool non_const)
4551 bool nowarning = TREE_NO_WARNING (expr);
4552 location_t loc = EXPR_LOCATION (expr);
4554 /* This should never be called for C++. */
4555 if (c_dialect_cxx ())
4556 gcc_unreachable ();
4558 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4559 STRIP_TYPE_NOPS (expr);
4560 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4561 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4562 if (nowarning)
4563 TREE_NO_WARNING (expr) = 1;
4564 protected_set_expr_location (expr, loc);
4566 return expr;
4569 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4570 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4571 around the SAVE_EXPR if needed so that c_fully_fold does not need
4572 to look inside SAVE_EXPRs. */
4574 tree
4575 c_save_expr (tree expr)
4577 bool maybe_const = true;
4578 if (c_dialect_cxx ())
4579 return save_expr (expr);
4580 expr = c_fully_fold (expr, false, &maybe_const);
4581 expr = save_expr (expr);
4582 if (!maybe_const)
4583 expr = c_wrap_maybe_const (expr, true);
4584 return expr;
4587 /* Return whether EXPR is a declaration whose address can never be
4588 NULL. */
4590 bool
4591 decl_with_nonnull_addr_p (const_tree expr)
4593 return (DECL_P (expr)
4594 && (TREE_CODE (expr) == PARM_DECL
4595 || TREE_CODE (expr) == LABEL_DECL
4596 || !DECL_WEAK (expr)));
4599 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4600 or for an `if' or `while' statement or ?..: exp. It should already
4601 have been validated to be of suitable type; otherwise, a bad
4602 diagnostic may result.
4604 The EXPR is located at LOCATION.
4606 This preparation consists of taking the ordinary
4607 representation of an expression expr and producing a valid tree
4608 boolean expression describing whether expr is nonzero. We could
4609 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4610 but we optimize comparisons, &&, ||, and !.
4612 The resulting type should always be `truthvalue_type_node'. */
4614 tree
4615 c_common_truthvalue_conversion (location_t location, tree expr)
4617 switch (TREE_CODE (expr))
4619 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4620 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4621 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4622 case ORDERED_EXPR: case UNORDERED_EXPR:
4623 if (TREE_TYPE (expr) == truthvalue_type_node)
4624 return expr;
4625 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4626 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4627 goto ret;
4629 case TRUTH_ANDIF_EXPR:
4630 case TRUTH_ORIF_EXPR:
4631 case TRUTH_AND_EXPR:
4632 case TRUTH_OR_EXPR:
4633 case TRUTH_XOR_EXPR:
4634 if (TREE_TYPE (expr) == truthvalue_type_node)
4635 return expr;
4636 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4637 c_common_truthvalue_conversion (location,
4638 TREE_OPERAND (expr, 0)),
4639 c_common_truthvalue_conversion (location,
4640 TREE_OPERAND (expr, 1)));
4641 goto ret;
4643 case TRUTH_NOT_EXPR:
4644 if (TREE_TYPE (expr) == truthvalue_type_node)
4645 return expr;
4646 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4647 c_common_truthvalue_conversion (location,
4648 TREE_OPERAND (expr, 0)));
4649 goto ret;
4651 case ERROR_MARK:
4652 return expr;
4654 case INTEGER_CST:
4655 return integer_zerop (expr) ? truthvalue_false_node
4656 : truthvalue_true_node;
4658 case REAL_CST:
4659 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4660 ? truthvalue_true_node
4661 : truthvalue_false_node;
4663 case FIXED_CST:
4664 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4665 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4666 ? truthvalue_true_node
4667 : truthvalue_false_node;
4669 case FUNCTION_DECL:
4670 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4671 /* Fall through. */
4673 case ADDR_EXPR:
4675 tree inner = TREE_OPERAND (expr, 0);
4676 if (decl_with_nonnull_addr_p (inner))
4678 /* Common Ada/Pascal programmer's mistake. */
4679 warning_at (location,
4680 OPT_Waddress,
4681 "the address of %qD will always evaluate as %<true%>",
4682 inner);
4683 return truthvalue_true_node;
4685 break;
4688 case COMPLEX_EXPR:
4689 expr = build_binary_op (EXPR_LOCATION (expr),
4690 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4691 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4692 c_common_truthvalue_conversion (location,
4693 TREE_OPERAND (expr, 0)),
4694 c_common_truthvalue_conversion (location,
4695 TREE_OPERAND (expr, 1)),
4697 goto ret;
4699 case NEGATE_EXPR:
4700 case ABS_EXPR:
4701 case FLOAT_EXPR:
4702 case EXCESS_PRECISION_EXPR:
4703 /* These don't change whether an object is nonzero or zero. */
4704 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4706 case LROTATE_EXPR:
4707 case RROTATE_EXPR:
4708 /* These don't change whether an object is zero or nonzero, but
4709 we can't ignore them if their second arg has side-effects. */
4710 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4712 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4713 TREE_OPERAND (expr, 1),
4714 c_common_truthvalue_conversion
4715 (location, TREE_OPERAND (expr, 0)));
4716 goto ret;
4718 else
4719 return c_common_truthvalue_conversion (location,
4720 TREE_OPERAND (expr, 0));
4722 case COND_EXPR:
4723 /* Distribute the conversion into the arms of a COND_EXPR. */
4724 if (c_dialect_cxx ())
4726 tree op1 = TREE_OPERAND (expr, 1);
4727 tree op2 = TREE_OPERAND (expr, 2);
4728 /* In C++ one of the arms might have void type if it is throw. */
4729 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4730 op1 = c_common_truthvalue_conversion (location, op1);
4731 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4732 op2 = c_common_truthvalue_conversion (location, op2);
4733 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4734 TREE_OPERAND (expr, 0), op1, op2);
4735 goto ret;
4737 else
4739 /* Folding will happen later for C. */
4740 expr = build3 (COND_EXPR, truthvalue_type_node,
4741 TREE_OPERAND (expr, 0),
4742 c_common_truthvalue_conversion (location,
4743 TREE_OPERAND (expr, 1)),
4744 c_common_truthvalue_conversion (location,
4745 TREE_OPERAND (expr, 2)));
4746 goto ret;
4749 CASE_CONVERT:
4751 tree totype = TREE_TYPE (expr);
4752 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4754 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4755 since that affects how `default_conversion' will behave. */
4756 if (TREE_CODE (totype) == REFERENCE_TYPE
4757 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4758 break;
4759 /* Don't strip a conversion from C++0x scoped enum, since they
4760 don't implicitly convert to other types. */
4761 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4762 && ENUM_IS_SCOPED (fromtype))
4763 break;
4764 /* If this isn't narrowing the argument, we can ignore it. */
4765 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4766 return c_common_truthvalue_conversion (location,
4767 TREE_OPERAND (expr, 0));
4769 break;
4771 case MODIFY_EXPR:
4772 if (!TREE_NO_WARNING (expr)
4773 && warn_parentheses)
4775 warning (OPT_Wparentheses,
4776 "suggest parentheses around assignment used as truth value");
4777 TREE_NO_WARNING (expr) = 1;
4779 break;
4781 default:
4782 break;
4785 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4787 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4788 expr = (build_binary_op
4789 (EXPR_LOCATION (expr),
4790 (TREE_SIDE_EFFECTS (expr)
4791 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4792 c_common_truthvalue_conversion
4793 (location,
4794 build_unary_op (location, REALPART_EXPR, t, 0)),
4795 c_common_truthvalue_conversion
4796 (location,
4797 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4798 0));
4799 goto ret;
4802 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4804 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4805 FCONST0 (TYPE_MODE
4806 (TREE_TYPE (expr))));
4807 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4809 else
4810 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4812 ret:
4813 protected_set_expr_location (expr, location);
4814 return expr;
4817 static void def_builtin_1 (enum built_in_function fncode,
4818 const char *name,
4819 enum built_in_class fnclass,
4820 tree fntype, tree libtype,
4821 bool both_p, bool fallback_p, bool nonansi_p,
4822 tree fnattrs, bool implicit_p);
4825 /* Apply the TYPE_QUALS to the new DECL. */
4827 void
4828 c_apply_type_quals_to_decl (int type_quals, tree decl)
4830 tree type = TREE_TYPE (decl);
4832 if (type == error_mark_node)
4833 return;
4835 if ((type_quals & TYPE_QUAL_CONST)
4836 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4837 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4838 constructor can produce constant init, so rely on cp_finish_decl to
4839 clear TREE_READONLY if the variable has non-constant init. */
4840 TREE_READONLY (decl) = 1;
4841 if (type_quals & TYPE_QUAL_VOLATILE)
4843 TREE_SIDE_EFFECTS (decl) = 1;
4844 TREE_THIS_VOLATILE (decl) = 1;
4846 if (type_quals & TYPE_QUAL_RESTRICT)
4848 while (type && TREE_CODE (type) == ARRAY_TYPE)
4849 /* Allow 'restrict' on arrays of pointers.
4850 FIXME currently we just ignore it. */
4851 type = TREE_TYPE (type);
4852 if (!type
4853 || !POINTER_TYPE_P (type)
4854 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4855 error ("invalid use of %<restrict%>");
4859 struct c_type_hasher : ggc_hasher<tree>
4861 static hashval_t hash (tree);
4862 static bool equal (tree, tree);
4865 /* Hash function for the problem of multiple type definitions in
4866 different files. This must hash all types that will compare
4867 equal via comptypes to the same value. In practice it hashes
4868 on some of the simple stuff and leaves the details to comptypes. */
4870 hashval_t
4871 c_type_hasher::hash (tree t)
4873 int n_elements;
4874 int shift, size;
4875 tree t2;
4876 switch (TREE_CODE (t))
4878 /* For pointers, hash on pointee type plus some swizzling. */
4879 case POINTER_TYPE:
4880 return hash (TREE_TYPE (t)) ^ 0x3003003;
4881 /* Hash on number of elements and total size. */
4882 case ENUMERAL_TYPE:
4883 shift = 3;
4884 t2 = TYPE_VALUES (t);
4885 break;
4886 case RECORD_TYPE:
4887 shift = 0;
4888 t2 = TYPE_FIELDS (t);
4889 break;
4890 case QUAL_UNION_TYPE:
4891 shift = 1;
4892 t2 = TYPE_FIELDS (t);
4893 break;
4894 case UNION_TYPE:
4895 shift = 2;
4896 t2 = TYPE_FIELDS (t);
4897 break;
4898 default:
4899 gcc_unreachable ();
4901 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4902 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4903 n_elements = list_length (t2);
4904 /* We might have a VLA here. */
4905 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4906 size = 0;
4907 else
4908 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4909 return ((size << 24) | (n_elements << shift));
4912 bool
4913 c_type_hasher::equal (tree t1, tree t2)
4915 return lang_hooks.types_compatible_p (t1, t2);
4918 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4920 /* Return the typed-based alias set for T, which may be an expression
4921 or a type. Return -1 if we don't do anything special. */
4923 alias_set_type
4924 c_common_get_alias_set (tree t)
4926 tree u;
4928 /* For VLAs, use the alias set of the element type rather than the
4929 default of alias set 0 for types compared structurally. */
4930 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4932 if (TREE_CODE (t) == ARRAY_TYPE)
4933 return get_alias_set (TREE_TYPE (t));
4934 return -1;
4937 /* Permit type-punning when accessing a union, provided the access
4938 is directly through the union. For example, this code does not
4939 permit taking the address of a union member and then storing
4940 through it. Even the type-punning allowed here is a GCC
4941 extension, albeit a common and useful one; the C standard says
4942 that such accesses have implementation-defined behavior. */
4943 for (u = t;
4944 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4945 u = TREE_OPERAND (u, 0))
4946 if (TREE_CODE (u) == COMPONENT_REF
4947 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4948 return 0;
4950 /* That's all the expressions we handle specially. */
4951 if (!TYPE_P (t))
4952 return -1;
4954 /* The C standard guarantees that any object may be accessed via an
4955 lvalue that has character type. */
4956 if (t == char_type_node
4957 || t == signed_char_type_node
4958 || t == unsigned_char_type_node)
4959 return 0;
4961 /* The C standard specifically allows aliasing between signed and
4962 unsigned variants of the same type. We treat the signed
4963 variant as canonical. */
4964 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4966 tree t1 = c_common_signed_type (t);
4968 /* t1 == t can happen for boolean nodes which are always unsigned. */
4969 if (t1 != t)
4970 return get_alias_set (t1);
4973 /* Handle the case of multiple type nodes referring to "the same" type,
4974 which occurs with IMA. These share an alias set. FIXME: Currently only
4975 C90 is handled. (In C99 type compatibility is not transitive, which
4976 complicates things mightily. The alias set splay trees can theoretically
4977 represent this, but insertion is tricky when you consider all the
4978 different orders things might arrive in.) */
4980 if (c_language != clk_c || flag_isoc99)
4981 return -1;
4983 /* Save time if there's only one input file. */
4984 if (num_in_fnames == 1)
4985 return -1;
4987 /* Pointers need special handling if they point to any type that
4988 needs special handling (below). */
4989 if (TREE_CODE (t) == POINTER_TYPE)
4991 tree t2;
4992 /* Find bottom type under any nested POINTERs. */
4993 for (t2 = TREE_TYPE (t);
4994 TREE_CODE (t2) == POINTER_TYPE;
4995 t2 = TREE_TYPE (t2))
4997 if (TREE_CODE (t2) != RECORD_TYPE
4998 && TREE_CODE (t2) != ENUMERAL_TYPE
4999 && TREE_CODE (t2) != QUAL_UNION_TYPE
5000 && TREE_CODE (t2) != UNION_TYPE)
5001 return -1;
5002 if (TYPE_SIZE (t2) == 0)
5003 return -1;
5005 /* These are the only cases that need special handling. */
5006 if (TREE_CODE (t) != RECORD_TYPE
5007 && TREE_CODE (t) != ENUMERAL_TYPE
5008 && TREE_CODE (t) != QUAL_UNION_TYPE
5009 && TREE_CODE (t) != UNION_TYPE
5010 && TREE_CODE (t) != POINTER_TYPE)
5011 return -1;
5012 /* Undefined? */
5013 if (TYPE_SIZE (t) == 0)
5014 return -1;
5016 /* Look up t in hash table. Only one of the compatible types within each
5017 alias set is recorded in the table. */
5018 if (!type_hash_table)
5019 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5020 tree *slot = type_hash_table->find_slot (t, INSERT);
5021 if (*slot != NULL)
5023 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5024 return TYPE_ALIAS_SET ((tree)*slot);
5026 else
5027 /* Our caller will assign and record (in t) a new alias set; all we need
5028 to do is remember t in the hash table. */
5029 *slot = t;
5031 return -1;
5034 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5035 the IS_SIZEOF parameter indicates which operator is being applied.
5036 The COMPLAIN flag controls whether we should diagnose possibly
5037 ill-formed constructs or not. LOC is the location of the SIZEOF or
5038 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5039 a type in any context should be returned, rather than the normal
5040 alignment for that type. */
5042 tree
5043 c_sizeof_or_alignof_type (location_t loc,
5044 tree type, bool is_sizeof, bool min_alignof,
5045 int complain)
5047 const char *op_name;
5048 tree value = NULL;
5049 enum tree_code type_code = TREE_CODE (type);
5051 op_name = is_sizeof ? "sizeof" : "__alignof__";
5053 if (type_code == FUNCTION_TYPE)
5055 if (is_sizeof)
5057 if (complain && warn_pointer_arith)
5058 pedwarn (loc, OPT_Wpointer_arith,
5059 "invalid application of %<sizeof%> to a function type");
5060 else if (!complain)
5061 return error_mark_node;
5062 value = size_one_node;
5064 else
5066 if (complain)
5068 if (c_dialect_cxx ())
5069 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5070 "%<alignof%> applied to a function type");
5071 else
5072 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5073 "%<_Alignof%> applied to a function type");
5075 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5078 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5080 if (type_code == VOID_TYPE
5081 && complain && warn_pointer_arith)
5082 pedwarn (loc, OPT_Wpointer_arith,
5083 "invalid application of %qs to a void type", op_name);
5084 else if (!complain)
5085 return error_mark_node;
5086 value = size_one_node;
5088 else if (!COMPLETE_TYPE_P (type)
5089 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5091 if (complain)
5092 error_at (loc, "invalid application of %qs to incomplete type %qT",
5093 op_name, type);
5094 return error_mark_node;
5096 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5097 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5099 if (complain)
5100 error_at (loc, "invalid application of %qs to array type %qT of "
5101 "incomplete element type", op_name, type);
5102 return error_mark_node;
5104 else
5106 if (is_sizeof)
5107 /* Convert in case a char is more than one unit. */
5108 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5109 size_int (TYPE_PRECISION (char_type_node)
5110 / BITS_PER_UNIT));
5111 else if (min_alignof)
5112 value = size_int (min_align_of_type (type));
5113 else
5114 value = size_int (TYPE_ALIGN_UNIT (type));
5117 /* VALUE will have the middle-end integer type sizetype.
5118 However, we should really return a value of type `size_t',
5119 which is just a typedef for an ordinary integer type. */
5120 value = fold_convert_loc (loc, size_type_node, value);
5122 return value;
5125 /* Implement the __alignof keyword: Return the minimum required
5126 alignment of EXPR, measured in bytes. For VAR_DECLs,
5127 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5128 from an "aligned" __attribute__ specification). LOC is the
5129 location of the ALIGNOF operator. */
5131 tree
5132 c_alignof_expr (location_t loc, tree expr)
5134 tree t;
5136 if (VAR_OR_FUNCTION_DECL_P (expr))
5137 t = size_int (DECL_ALIGN_UNIT (expr));
5139 else if (TREE_CODE (expr) == COMPONENT_REF
5140 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5142 error_at (loc, "%<__alignof%> applied to a bit-field");
5143 t = size_one_node;
5145 else if (TREE_CODE (expr) == COMPONENT_REF
5146 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5147 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5149 else if (TREE_CODE (expr) == INDIRECT_REF)
5151 tree t = TREE_OPERAND (expr, 0);
5152 tree best = t;
5153 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5155 while (CONVERT_EXPR_P (t)
5156 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5158 int thisalign;
5160 t = TREE_OPERAND (t, 0);
5161 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5162 if (thisalign > bestalign)
5163 best = t, bestalign = thisalign;
5165 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5167 else
5168 return c_alignof (loc, TREE_TYPE (expr));
5170 return fold_convert_loc (loc, size_type_node, t);
5173 /* Handle C and C++ default attributes. */
5175 enum built_in_attribute
5177 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5178 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5179 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5180 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5181 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5182 #include "builtin-attrs.def"
5183 #undef DEF_ATTR_NULL_TREE
5184 #undef DEF_ATTR_INT
5185 #undef DEF_ATTR_STRING
5186 #undef DEF_ATTR_IDENT
5187 #undef DEF_ATTR_TREE_LIST
5188 ATTR_LAST
5191 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5193 static void c_init_attributes (void);
5195 enum c_builtin_type
5197 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5198 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5199 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5200 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5201 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5202 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5203 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5204 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5205 ARG6) NAME,
5206 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5207 ARG6, ARG7) NAME,
5208 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5209 ARG6, ARG7, ARG8) NAME,
5210 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5211 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5212 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5213 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5214 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5215 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5216 NAME,
5217 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5218 #include "builtin-types.def"
5219 #undef DEF_PRIMITIVE_TYPE
5220 #undef DEF_FUNCTION_TYPE_0
5221 #undef DEF_FUNCTION_TYPE_1
5222 #undef DEF_FUNCTION_TYPE_2
5223 #undef DEF_FUNCTION_TYPE_3
5224 #undef DEF_FUNCTION_TYPE_4
5225 #undef DEF_FUNCTION_TYPE_5
5226 #undef DEF_FUNCTION_TYPE_6
5227 #undef DEF_FUNCTION_TYPE_7
5228 #undef DEF_FUNCTION_TYPE_8
5229 #undef DEF_FUNCTION_TYPE_VAR_0
5230 #undef DEF_FUNCTION_TYPE_VAR_1
5231 #undef DEF_FUNCTION_TYPE_VAR_2
5232 #undef DEF_FUNCTION_TYPE_VAR_3
5233 #undef DEF_FUNCTION_TYPE_VAR_4
5234 #undef DEF_FUNCTION_TYPE_VAR_5
5235 #undef DEF_POINTER_TYPE
5236 BT_LAST
5239 typedef enum c_builtin_type builtin_type;
5241 /* A temporary array for c_common_nodes_and_builtins. Used in
5242 communication with def_fn_type. */
5243 static tree builtin_types[(int) BT_LAST + 1];
5245 /* A helper function for c_common_nodes_and_builtins. Build function type
5246 for DEF with return type RET and N arguments. If VAR is true, then the
5247 function should be variadic after those N arguments.
5249 Takes special care not to ICE if any of the types involved are
5250 error_mark_node, which indicates that said type is not in fact available
5251 (see builtin_type_for_size). In which case the function type as a whole
5252 should be error_mark_node. */
5254 static void
5255 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5257 tree t;
5258 tree *args = XALLOCAVEC (tree, n);
5259 va_list list;
5260 int i;
5262 va_start (list, n);
5263 for (i = 0; i < n; ++i)
5265 builtin_type a = (builtin_type) va_arg (list, int);
5266 t = builtin_types[a];
5267 if (t == error_mark_node)
5268 goto egress;
5269 args[i] = t;
5272 t = builtin_types[ret];
5273 if (t == error_mark_node)
5274 goto egress;
5275 if (var)
5276 t = build_varargs_function_type_array (t, n, args);
5277 else
5278 t = build_function_type_array (t, n, args);
5280 egress:
5281 builtin_types[def] = t;
5282 va_end (list);
5285 /* Build builtin functions common to both C and C++ language
5286 frontends. */
5288 static void
5289 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5291 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5292 builtin_types[ENUM] = VALUE;
5293 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5294 def_fn_type (ENUM, RETURN, 0, 0);
5295 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5296 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5297 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5298 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5299 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5300 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5301 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5302 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5303 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5304 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5305 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5306 ARG6) \
5307 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5308 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5309 ARG6, ARG7) \
5310 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5311 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5312 ARG6, ARG7, ARG8) \
5313 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5314 ARG7, ARG8);
5315 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5316 def_fn_type (ENUM, RETURN, 1, 0);
5317 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5318 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5319 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5320 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5321 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5322 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5323 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5324 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5325 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5326 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5327 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5328 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5330 #include "builtin-types.def"
5332 #undef DEF_PRIMITIVE_TYPE
5333 #undef DEF_FUNCTION_TYPE_0
5334 #undef DEF_FUNCTION_TYPE_1
5335 #undef DEF_FUNCTION_TYPE_2
5336 #undef DEF_FUNCTION_TYPE_3
5337 #undef DEF_FUNCTION_TYPE_4
5338 #undef DEF_FUNCTION_TYPE_5
5339 #undef DEF_FUNCTION_TYPE_6
5340 #undef DEF_FUNCTION_TYPE_7
5341 #undef DEF_FUNCTION_TYPE_8
5342 #undef DEF_FUNCTION_TYPE_VAR_0
5343 #undef DEF_FUNCTION_TYPE_VAR_1
5344 #undef DEF_FUNCTION_TYPE_VAR_2
5345 #undef DEF_FUNCTION_TYPE_VAR_3
5346 #undef DEF_FUNCTION_TYPE_VAR_4
5347 #undef DEF_FUNCTION_TYPE_VAR_5
5348 #undef DEF_POINTER_TYPE
5349 builtin_types[(int) BT_LAST] = NULL_TREE;
5351 c_init_attributes ();
5353 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5354 NONANSI_P, ATTRS, IMPLICIT, COND) \
5355 if (NAME && COND) \
5356 def_builtin_1 (ENUM, NAME, CLASS, \
5357 builtin_types[(int) TYPE], \
5358 builtin_types[(int) LIBTYPE], \
5359 BOTH_P, FALLBACK_P, NONANSI_P, \
5360 built_in_attributes[(int) ATTRS], IMPLICIT);
5361 #include "builtins.def"
5362 #undef DEF_BUILTIN
5364 targetm.init_builtins ();
5366 build_common_builtin_nodes ();
5368 if (flag_cilkplus)
5369 cilk_init_builtins ();
5372 /* Like get_identifier, but avoid warnings about null arguments when
5373 the argument may be NULL for targets where GCC lacks stdint.h type
5374 information. */
5376 static inline tree
5377 c_get_ident (const char *id)
5379 return get_identifier (id);
5382 /* Build tree nodes and builtin functions common to both C and C++ language
5383 frontends. */
5385 void
5386 c_common_nodes_and_builtins (void)
5388 int char16_type_size;
5389 int char32_type_size;
5390 int wchar_type_size;
5391 tree array_domain_type;
5392 tree va_list_ref_type_node;
5393 tree va_list_arg_type_node;
5394 int i;
5396 build_common_tree_nodes (flag_signed_char, flag_short_double);
5398 /* Define `int' and `char' first so that dbx will output them first. */
5399 record_builtin_type (RID_INT, NULL, integer_type_node);
5400 record_builtin_type (RID_CHAR, "char", char_type_node);
5402 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5403 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5404 but not C. Are the conditionals here needed? */
5405 if (c_dialect_cxx ())
5406 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5407 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5408 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5409 record_builtin_type (RID_MAX, "long unsigned int",
5410 long_unsigned_type_node);
5412 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5414 char name[25];
5416 sprintf (name, "__int%d", int_n_data[i].bitsize);
5417 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), xstrdup (name),
5418 int_n_trees[i].signed_type);
5419 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5420 record_builtin_type (RID_MAX, xstrdup (name),
5421 int_n_trees[i].unsigned_type);
5424 if (c_dialect_cxx ())
5425 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5426 record_builtin_type (RID_MAX, "long long int",
5427 long_long_integer_type_node);
5428 record_builtin_type (RID_MAX, "long long unsigned int",
5429 long_long_unsigned_type_node);
5430 if (c_dialect_cxx ())
5431 record_builtin_type (RID_MAX, "long long unsigned",
5432 long_long_unsigned_type_node);
5433 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5434 record_builtin_type (RID_MAX, "short unsigned int",
5435 short_unsigned_type_node);
5436 if (c_dialect_cxx ())
5437 record_builtin_type (RID_MAX, "unsigned short",
5438 short_unsigned_type_node);
5440 /* Define both `signed char' and `unsigned char'. */
5441 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5442 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5444 /* These are types that c_common_type_for_size and
5445 c_common_type_for_mode use. */
5446 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5447 TYPE_DECL, NULL_TREE,
5448 intQI_type_node));
5449 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5450 TYPE_DECL, NULL_TREE,
5451 intHI_type_node));
5452 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5453 TYPE_DECL, NULL_TREE,
5454 intSI_type_node));
5455 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5456 TYPE_DECL, NULL_TREE,
5457 intDI_type_node));
5458 #if HOST_BITS_PER_WIDE_INT >= 64
5459 /* Note that this is different than the __int128 type that's part of
5460 the generic __intN support. */
5461 if (targetm.scalar_mode_supported_p (TImode))
5462 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5463 TYPE_DECL,
5464 get_identifier ("__int128_t"),
5465 intTI_type_node));
5466 #endif
5467 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5468 TYPE_DECL, NULL_TREE,
5469 unsigned_intQI_type_node));
5470 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5471 TYPE_DECL, NULL_TREE,
5472 unsigned_intHI_type_node));
5473 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5474 TYPE_DECL, NULL_TREE,
5475 unsigned_intSI_type_node));
5476 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5477 TYPE_DECL, NULL_TREE,
5478 unsigned_intDI_type_node));
5479 #if HOST_BITS_PER_WIDE_INT >= 64
5480 if (targetm.scalar_mode_supported_p (TImode))
5481 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5482 TYPE_DECL,
5483 get_identifier ("__uint128_t"),
5484 unsigned_intTI_type_node));
5485 #endif
5487 /* Create the widest literal types. */
5488 widest_integer_literal_type_node
5489 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5490 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5491 TYPE_DECL, NULL_TREE,
5492 widest_integer_literal_type_node));
5494 widest_unsigned_literal_type_node
5495 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5496 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5497 TYPE_DECL, NULL_TREE,
5498 widest_unsigned_literal_type_node));
5500 signed_size_type_node = c_common_signed_type (size_type_node);
5502 pid_type_node =
5503 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5505 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5506 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5507 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5509 /* Only supported decimal floating point extension if the target
5510 actually supports underlying modes. */
5511 if (targetm.scalar_mode_supported_p (SDmode)
5512 && targetm.scalar_mode_supported_p (DDmode)
5513 && targetm.scalar_mode_supported_p (TDmode))
5515 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5516 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5517 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5520 if (targetm.fixed_point_supported_p ())
5522 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5523 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5524 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5525 record_builtin_type (RID_MAX, "long long _Fract",
5526 long_long_fract_type_node);
5527 record_builtin_type (RID_MAX, "unsigned short _Fract",
5528 unsigned_short_fract_type_node);
5529 record_builtin_type (RID_MAX, "unsigned _Fract",
5530 unsigned_fract_type_node);
5531 record_builtin_type (RID_MAX, "unsigned long _Fract",
5532 unsigned_long_fract_type_node);
5533 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5534 unsigned_long_long_fract_type_node);
5535 record_builtin_type (RID_MAX, "_Sat short _Fract",
5536 sat_short_fract_type_node);
5537 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5538 record_builtin_type (RID_MAX, "_Sat long _Fract",
5539 sat_long_fract_type_node);
5540 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5541 sat_long_long_fract_type_node);
5542 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5543 sat_unsigned_short_fract_type_node);
5544 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5545 sat_unsigned_fract_type_node);
5546 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5547 sat_unsigned_long_fract_type_node);
5548 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5549 sat_unsigned_long_long_fract_type_node);
5550 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5551 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5552 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5553 record_builtin_type (RID_MAX, "long long _Accum",
5554 long_long_accum_type_node);
5555 record_builtin_type (RID_MAX, "unsigned short _Accum",
5556 unsigned_short_accum_type_node);
5557 record_builtin_type (RID_MAX, "unsigned _Accum",
5558 unsigned_accum_type_node);
5559 record_builtin_type (RID_MAX, "unsigned long _Accum",
5560 unsigned_long_accum_type_node);
5561 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5562 unsigned_long_long_accum_type_node);
5563 record_builtin_type (RID_MAX, "_Sat short _Accum",
5564 sat_short_accum_type_node);
5565 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5566 record_builtin_type (RID_MAX, "_Sat long _Accum",
5567 sat_long_accum_type_node);
5568 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5569 sat_long_long_accum_type_node);
5570 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5571 sat_unsigned_short_accum_type_node);
5572 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5573 sat_unsigned_accum_type_node);
5574 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5575 sat_unsigned_long_accum_type_node);
5576 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5577 sat_unsigned_long_long_accum_type_node);
5581 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5582 TYPE_DECL,
5583 get_identifier ("complex int"),
5584 complex_integer_type_node));
5585 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5586 TYPE_DECL,
5587 get_identifier ("complex float"),
5588 complex_float_type_node));
5589 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5590 TYPE_DECL,
5591 get_identifier ("complex double"),
5592 complex_double_type_node));
5593 lang_hooks.decls.pushdecl
5594 (build_decl (UNKNOWN_LOCATION,
5595 TYPE_DECL, get_identifier ("complex long double"),
5596 complex_long_double_type_node));
5598 if (c_dialect_cxx ())
5599 /* For C++, make fileptr_type_node a distinct void * type until
5600 FILE type is defined. */
5601 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5603 record_builtin_type (RID_VOID, NULL, void_type_node);
5605 /* Set the TYPE_NAME for any variants that were built before
5606 record_builtin_type gave names to the built-in types. */
5608 tree void_name = TYPE_NAME (void_type_node);
5609 TYPE_NAME (void_type_node) = NULL_TREE;
5610 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5611 = void_name;
5612 TYPE_NAME (void_type_node) = void_name;
5615 void_list_node = build_void_list_node ();
5617 /* Make a type to be the domain of a few array types
5618 whose domains don't really matter.
5619 200 is small enough that it always fits in size_t
5620 and large enough that it can hold most function names for the
5621 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5622 array_domain_type = build_index_type (size_int (200));
5624 /* Make a type for arrays of characters.
5625 With luck nothing will ever really depend on the length of this
5626 array type. */
5627 char_array_type_node
5628 = build_array_type (char_type_node, array_domain_type);
5630 string_type_node = build_pointer_type (char_type_node);
5631 const_string_type_node
5632 = build_pointer_type (build_qualified_type
5633 (char_type_node, TYPE_QUAL_CONST));
5635 /* This is special for C++ so functions can be overloaded. */
5636 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5637 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5638 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5639 underlying_wchar_type_node = wchar_type_node;
5640 if (c_dialect_cxx ())
5642 if (TYPE_UNSIGNED (wchar_type_node))
5643 wchar_type_node = make_unsigned_type (wchar_type_size);
5644 else
5645 wchar_type_node = make_signed_type (wchar_type_size);
5646 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5649 /* This is for wide string constants. */
5650 wchar_array_type_node
5651 = build_array_type (wchar_type_node, array_domain_type);
5653 /* Define 'char16_t'. */
5654 char16_type_node = get_identifier (CHAR16_TYPE);
5655 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5656 char16_type_size = TYPE_PRECISION (char16_type_node);
5657 if (c_dialect_cxx ())
5659 char16_type_node = make_unsigned_type (char16_type_size);
5661 if (cxx_dialect >= cxx11)
5662 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5665 /* This is for UTF-16 string constants. */
5666 char16_array_type_node
5667 = build_array_type (char16_type_node, array_domain_type);
5669 /* Define 'char32_t'. */
5670 char32_type_node = get_identifier (CHAR32_TYPE);
5671 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5672 char32_type_size = TYPE_PRECISION (char32_type_node);
5673 if (c_dialect_cxx ())
5675 char32_type_node = make_unsigned_type (char32_type_size);
5677 if (cxx_dialect >= cxx11)
5678 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5681 /* This is for UTF-32 string constants. */
5682 char32_array_type_node
5683 = build_array_type (char32_type_node, array_domain_type);
5685 wint_type_node =
5686 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5688 intmax_type_node =
5689 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5690 uintmax_type_node =
5691 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5693 if (SIG_ATOMIC_TYPE)
5694 sig_atomic_type_node =
5695 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5696 if (INT8_TYPE)
5697 int8_type_node =
5698 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5699 if (INT16_TYPE)
5700 int16_type_node =
5701 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5702 if (INT32_TYPE)
5703 int32_type_node =
5704 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5705 if (INT64_TYPE)
5706 int64_type_node =
5707 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5708 if (UINT8_TYPE)
5709 uint8_type_node =
5710 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5711 if (UINT16_TYPE)
5712 c_uint16_type_node = uint16_type_node =
5713 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5714 if (UINT32_TYPE)
5715 c_uint32_type_node = uint32_type_node =
5716 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5717 if (UINT64_TYPE)
5718 c_uint64_type_node = uint64_type_node =
5719 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5720 if (INT_LEAST8_TYPE)
5721 int_least8_type_node =
5722 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5723 if (INT_LEAST16_TYPE)
5724 int_least16_type_node =
5725 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5726 if (INT_LEAST32_TYPE)
5727 int_least32_type_node =
5728 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5729 if (INT_LEAST64_TYPE)
5730 int_least64_type_node =
5731 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5732 if (UINT_LEAST8_TYPE)
5733 uint_least8_type_node =
5734 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5735 if (UINT_LEAST16_TYPE)
5736 uint_least16_type_node =
5737 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5738 if (UINT_LEAST32_TYPE)
5739 uint_least32_type_node =
5740 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5741 if (UINT_LEAST64_TYPE)
5742 uint_least64_type_node =
5743 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5744 if (INT_FAST8_TYPE)
5745 int_fast8_type_node =
5746 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5747 if (INT_FAST16_TYPE)
5748 int_fast16_type_node =
5749 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5750 if (INT_FAST32_TYPE)
5751 int_fast32_type_node =
5752 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5753 if (INT_FAST64_TYPE)
5754 int_fast64_type_node =
5755 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5756 if (UINT_FAST8_TYPE)
5757 uint_fast8_type_node =
5758 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5759 if (UINT_FAST16_TYPE)
5760 uint_fast16_type_node =
5761 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5762 if (UINT_FAST32_TYPE)
5763 uint_fast32_type_node =
5764 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5765 if (UINT_FAST64_TYPE)
5766 uint_fast64_type_node =
5767 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5768 if (INTPTR_TYPE)
5769 intptr_type_node =
5770 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5771 if (UINTPTR_TYPE)
5772 uintptr_type_node =
5773 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5775 default_function_type
5776 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5777 ptrdiff_type_node
5778 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5779 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5781 lang_hooks.decls.pushdecl
5782 (build_decl (UNKNOWN_LOCATION,
5783 TYPE_DECL, get_identifier ("__builtin_va_list"),
5784 va_list_type_node));
5785 if (targetm.enum_va_list_p)
5787 int l;
5788 const char *pname;
5789 tree ptype;
5791 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5793 lang_hooks.decls.pushdecl
5794 (build_decl (UNKNOWN_LOCATION,
5795 TYPE_DECL, get_identifier (pname),
5796 ptype));
5801 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5803 va_list_arg_type_node = va_list_ref_type_node =
5804 build_pointer_type (TREE_TYPE (va_list_type_node));
5806 else
5808 va_list_arg_type_node = va_list_type_node;
5809 va_list_ref_type_node = build_reference_type (va_list_type_node);
5812 if (!flag_preprocess_only)
5813 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5815 main_identifier_node = get_identifier ("main");
5817 /* Create the built-in __null node. It is important that this is
5818 not shared. */
5819 null_node = make_int_cst (1, 1);
5820 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5822 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5823 memset (builtin_types, 0, sizeof (builtin_types));
5826 /* The number of named compound-literals generated thus far. */
5827 static GTY(()) int compound_literal_number;
5829 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5831 void
5832 set_compound_literal_name (tree decl)
5834 char *name;
5835 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5836 compound_literal_number);
5837 compound_literal_number++;
5838 DECL_NAME (decl) = get_identifier (name);
5841 tree
5842 build_va_arg (location_t loc, tree expr, tree type)
5844 expr = build1 (VA_ARG_EXPR, type, expr);
5845 SET_EXPR_LOCATION (expr, loc);
5846 return expr;
5850 /* Linked list of disabled built-in functions. */
5852 typedef struct disabled_builtin
5854 const char *name;
5855 struct disabled_builtin *next;
5856 } disabled_builtin;
5857 static disabled_builtin *disabled_builtins = NULL;
5859 static bool builtin_function_disabled_p (const char *);
5861 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5862 begins with "__builtin_", give an error. */
5864 void
5865 disable_builtin_function (const char *name)
5867 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5868 error ("cannot disable built-in function %qs", name);
5869 else
5871 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5872 new_disabled_builtin->name = name;
5873 new_disabled_builtin->next = disabled_builtins;
5874 disabled_builtins = new_disabled_builtin;
5879 /* Return true if the built-in function NAME has been disabled, false
5880 otherwise. */
5882 static bool
5883 builtin_function_disabled_p (const char *name)
5885 disabled_builtin *p;
5886 for (p = disabled_builtins; p != NULL; p = p->next)
5888 if (strcmp (name, p->name) == 0)
5889 return true;
5891 return false;
5895 /* Worker for DEF_BUILTIN.
5896 Possibly define a builtin function with one or two names.
5897 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5898 nonansi_p and flag_no_nonansi_builtin. */
5900 static void
5901 def_builtin_1 (enum built_in_function fncode,
5902 const char *name,
5903 enum built_in_class fnclass,
5904 tree fntype, tree libtype,
5905 bool both_p, bool fallback_p, bool nonansi_p,
5906 tree fnattrs, bool implicit_p)
5908 tree decl;
5909 const char *libname;
5911 if (fntype == error_mark_node)
5912 return;
5914 gcc_assert ((!both_p && !fallback_p)
5915 || !strncmp (name, "__builtin_",
5916 strlen ("__builtin_")));
5918 libname = name + strlen ("__builtin_");
5919 decl = add_builtin_function (name, fntype, fncode, fnclass,
5920 (fallback_p ? libname : NULL),
5921 fnattrs);
5923 set_builtin_decl (fncode, decl, implicit_p);
5925 if (both_p
5926 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5927 && !(nonansi_p && flag_no_nonansi_builtin))
5928 add_builtin_function (libname, libtype, fncode, fnclass,
5929 NULL, fnattrs);
5932 /* Nonzero if the type T promotes to int. This is (nearly) the
5933 integral promotions defined in ISO C99 6.3.1.1/2. */
5935 bool
5936 c_promoting_integer_type_p (const_tree t)
5938 switch (TREE_CODE (t))
5940 case INTEGER_TYPE:
5941 return (TYPE_MAIN_VARIANT (t) == char_type_node
5942 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5943 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5944 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5945 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5946 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5948 case ENUMERAL_TYPE:
5949 /* ??? Technically all enumerations not larger than an int
5950 promote to an int. But this is used along code paths
5951 that only want to notice a size change. */
5952 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5954 case BOOLEAN_TYPE:
5955 return 1;
5957 default:
5958 return 0;
5962 /* Return 1 if PARMS specifies a fixed number of parameters
5963 and none of their types is affected by default promotions. */
5966 self_promoting_args_p (const_tree parms)
5968 const_tree t;
5969 for (t = parms; t; t = TREE_CHAIN (t))
5971 tree type = TREE_VALUE (t);
5973 if (type == error_mark_node)
5974 continue;
5976 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5977 return 0;
5979 if (type == 0)
5980 return 0;
5982 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5983 return 0;
5985 if (c_promoting_integer_type_p (type))
5986 return 0;
5988 return 1;
5991 /* Recursively remove any '*' or '&' operator from TYPE. */
5992 tree
5993 strip_pointer_operator (tree t)
5995 while (POINTER_TYPE_P (t))
5996 t = TREE_TYPE (t);
5997 return t;
6000 /* Recursively remove pointer or array type from TYPE. */
6001 tree
6002 strip_pointer_or_array_types (tree t)
6004 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6005 t = TREE_TYPE (t);
6006 return t;
6009 /* Used to compare case labels. K1 and K2 are actually tree nodes
6010 representing case labels, or NULL_TREE for a `default' label.
6011 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6012 K2, and 0 if K1 and K2 are equal. */
6015 case_compare (splay_tree_key k1, splay_tree_key k2)
6017 /* Consider a NULL key (such as arises with a `default' label) to be
6018 smaller than anything else. */
6019 if (!k1)
6020 return k2 ? -1 : 0;
6021 else if (!k2)
6022 return k1 ? 1 : 0;
6024 return tree_int_cst_compare ((tree) k1, (tree) k2);
6027 /* Process a case label, located at LOC, for the range LOW_VALUE
6028 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6029 then this case label is actually a `default' label. If only
6030 HIGH_VALUE is NULL_TREE, then case label was declared using the
6031 usual C/C++ syntax, rather than the GNU case range extension.
6032 CASES is a tree containing all the case ranges processed so far;
6033 COND is the condition for the switch-statement itself. Returns the
6034 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6035 is created. */
6037 tree
6038 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6039 tree low_value, tree high_value)
6041 tree type;
6042 tree label;
6043 tree case_label;
6044 splay_tree_node node;
6046 /* Create the LABEL_DECL itself. */
6047 label = create_artificial_label (loc);
6049 /* If there was an error processing the switch condition, bail now
6050 before we get more confused. */
6051 if (!cond || cond == error_mark_node)
6052 goto error_out;
6054 if ((low_value && TREE_TYPE (low_value)
6055 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6056 || (high_value && TREE_TYPE (high_value)
6057 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6059 error_at (loc, "pointers are not permitted as case values");
6060 goto error_out;
6063 /* Case ranges are a GNU extension. */
6064 if (high_value)
6065 pedwarn (loc, OPT_Wpedantic,
6066 "range expressions in switch statements are non-standard");
6068 type = TREE_TYPE (cond);
6069 if (low_value)
6071 low_value = check_case_value (loc, low_value);
6072 low_value = convert_and_check (loc, type, low_value);
6073 if (low_value == error_mark_node)
6074 goto error_out;
6076 if (high_value)
6078 high_value = check_case_value (loc, high_value);
6079 high_value = convert_and_check (loc, type, high_value);
6080 if (high_value == error_mark_node)
6081 goto error_out;
6084 if (low_value && high_value)
6086 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6087 really a case range, even though it was written that way.
6088 Remove the HIGH_VALUE to simplify later processing. */
6089 if (tree_int_cst_equal (low_value, high_value))
6090 high_value = NULL_TREE;
6091 else if (!tree_int_cst_lt (low_value, high_value))
6092 warning_at (loc, 0, "empty range specified");
6095 /* See if the case is in range of the type of the original testing
6096 expression. If both low_value and high_value are out of range,
6097 don't insert the case label and return NULL_TREE. */
6098 if (low_value
6099 && !check_case_bounds (loc, type, orig_type,
6100 &low_value, high_value ? &high_value : NULL))
6101 return NULL_TREE;
6103 /* Look up the LOW_VALUE in the table of case labels we already
6104 have. */
6105 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6106 /* If there was not an exact match, check for overlapping ranges.
6107 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6108 that's a `default' label and the only overlap is an exact match. */
6109 if (!node && (low_value || high_value))
6111 splay_tree_node low_bound;
6112 splay_tree_node high_bound;
6114 /* Even though there wasn't an exact match, there might be an
6115 overlap between this case range and another case range.
6116 Since we've (inductively) not allowed any overlapping case
6117 ranges, we simply need to find the greatest low case label
6118 that is smaller that LOW_VALUE, and the smallest low case
6119 label that is greater than LOW_VALUE. If there is an overlap
6120 it will occur in one of these two ranges. */
6121 low_bound = splay_tree_predecessor (cases,
6122 (splay_tree_key) low_value);
6123 high_bound = splay_tree_successor (cases,
6124 (splay_tree_key) low_value);
6126 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6127 the LOW_VALUE, so there is no need to check unless the
6128 LOW_BOUND is in fact itself a case range. */
6129 if (low_bound
6130 && CASE_HIGH ((tree) low_bound->value)
6131 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6132 low_value) >= 0)
6133 node = low_bound;
6134 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6135 range is bigger than the low end of the current range, so we
6136 are only interested if the current range is a real range, and
6137 not an ordinary case label. */
6138 else if (high_bound
6139 && high_value
6140 && (tree_int_cst_compare ((tree) high_bound->key,
6141 high_value)
6142 <= 0))
6143 node = high_bound;
6145 /* If there was an overlap, issue an error. */
6146 if (node)
6148 tree duplicate = CASE_LABEL ((tree) node->value);
6150 if (high_value)
6152 error_at (loc, "duplicate (or overlapping) case value");
6153 error_at (DECL_SOURCE_LOCATION (duplicate),
6154 "this is the first entry overlapping that value");
6156 else if (low_value)
6158 error_at (loc, "duplicate case value") ;
6159 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6161 else
6163 error_at (loc, "multiple default labels in one switch");
6164 error_at (DECL_SOURCE_LOCATION (duplicate),
6165 "this is the first default label");
6167 goto error_out;
6170 /* Add a CASE_LABEL to the statement-tree. */
6171 case_label = add_stmt (build_case_label (low_value, high_value, label));
6172 /* Register this case label in the splay tree. */
6173 splay_tree_insert (cases,
6174 (splay_tree_key) low_value,
6175 (splay_tree_value) case_label);
6177 return case_label;
6179 error_out:
6180 /* Add a label so that the back-end doesn't think that the beginning of
6181 the switch is unreachable. Note that we do not add a case label, as
6182 that just leads to duplicates and thence to failure later on. */
6183 if (!cases->root)
6185 tree t = create_artificial_label (loc);
6186 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6188 return error_mark_node;
6191 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6192 Used to verify that case values match up with enumerator values. */
6194 static void
6195 match_case_to_enum_1 (tree key, tree type, tree label)
6197 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6199 if (tree_fits_uhwi_p (key))
6200 print_dec (key, buf, UNSIGNED);
6201 else if (tree_fits_shwi_p (key))
6202 print_dec (key, buf, SIGNED);
6203 else
6204 print_hex (key, buf);
6206 if (TYPE_NAME (type) == 0)
6207 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6208 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6209 "case value %qs not in enumerated type",
6210 buf);
6211 else
6212 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6213 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6214 "case value %qs not in enumerated type %qT",
6215 buf, type);
6218 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6219 Used to verify that case values match up with enumerator values. */
6221 static int
6222 match_case_to_enum (splay_tree_node node, void *data)
6224 tree label = (tree) node->value;
6225 tree type = (tree) data;
6227 /* Skip default case. */
6228 if (!CASE_LOW (label))
6229 return 0;
6231 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6232 when we did our enum->case scan. Reset our scratch bit after. */
6233 if (!CASE_LOW_SEEN (label))
6234 match_case_to_enum_1 (CASE_LOW (label), type, label);
6235 else
6236 CASE_LOW_SEEN (label) = 0;
6238 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6239 not set, that means that CASE_HIGH did not appear when we did our
6240 enum->case scan. Reset our scratch bit after. */
6241 if (CASE_HIGH (label))
6243 if (!CASE_HIGH_SEEN (label))
6244 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6245 else
6246 CASE_HIGH_SEEN (label) = 0;
6249 return 0;
6252 /* Handle -Wswitch*. Called from the front end after parsing the
6253 switch construct. */
6254 /* ??? Should probably be somewhere generic, since other languages
6255 besides C and C++ would want this. At the moment, however, C/C++
6256 are the only tree-ssa languages that support enumerations at all,
6257 so the point is moot. */
6259 void
6260 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6261 tree type, tree cond)
6263 splay_tree_node default_node;
6264 splay_tree_node node;
6265 tree chain;
6267 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6268 return;
6270 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6271 if (!default_node)
6272 warning_at (switch_location, OPT_Wswitch_default,
6273 "switch missing default case");
6275 /* From here on, we only care about about enumerated types. */
6276 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6277 return;
6279 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6280 if (!warn_switch_enum && !warn_switch)
6281 return;
6283 /* Check the cases. Warn about case values which are not members of
6284 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6285 there is no default case, check that exactly all enumeration
6286 literals are covered by the cases. */
6288 /* Clearing COND if it is not an integer constant simplifies
6289 the tests inside the loop below. */
6290 if (TREE_CODE (cond) != INTEGER_CST)
6291 cond = NULL_TREE;
6293 /* The time complexity here is O(N*lg(N)) worst case, but for the
6294 common case of monotonically increasing enumerators, it is
6295 O(N), since the nature of the splay tree will keep the next
6296 element adjacent to the root at all times. */
6298 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6300 tree value = TREE_VALUE (chain);
6301 if (TREE_CODE (value) == CONST_DECL)
6302 value = DECL_INITIAL (value);
6303 node = splay_tree_lookup (cases, (splay_tree_key) value);
6304 if (node)
6306 /* Mark the CASE_LOW part of the case entry as seen. */
6307 tree label = (tree) node->value;
6308 CASE_LOW_SEEN (label) = 1;
6309 continue;
6312 /* Even though there wasn't an exact match, there might be a
6313 case range which includes the enumerator's value. */
6314 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6315 if (node && CASE_HIGH ((tree) node->value))
6317 tree label = (tree) node->value;
6318 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6319 if (cmp >= 0)
6321 /* If we match the upper bound exactly, mark the CASE_HIGH
6322 part of the case entry as seen. */
6323 if (cmp == 0)
6324 CASE_HIGH_SEEN (label) = 1;
6325 continue;
6329 /* We've now determined that this enumerated literal isn't
6330 handled by the case labels of the switch statement. */
6332 /* If the switch expression is a constant, we only really care
6333 about whether that constant is handled by the switch. */
6334 if (cond && tree_int_cst_compare (cond, value))
6335 continue;
6337 /* If there is a default_node, the only relevant option is
6338 Wswitch-enum. Otherwise, if both are enabled then we prefer
6339 to warn using -Wswitch because -Wswitch is enabled by -Wall
6340 while -Wswitch-enum is explicit. */
6341 warning_at (switch_location,
6342 (default_node || !warn_switch
6343 ? OPT_Wswitch_enum
6344 : OPT_Wswitch),
6345 "enumeration value %qE not handled in switch",
6346 TREE_PURPOSE (chain));
6349 /* Warn if there are case expressions that don't correspond to
6350 enumerators. This can occur since C and C++ don't enforce
6351 type-checking of assignments to enumeration variables.
6353 The time complexity here is now always O(N) worst case, since
6354 we should have marked both the lower bound and upper bound of
6355 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6356 above. This scan also resets those fields. */
6358 splay_tree_foreach (cases, match_case_to_enum, type);
6361 /* Finish an expression taking the address of LABEL (an
6362 IDENTIFIER_NODE). Returns an expression for the address.
6364 LOC is the location for the expression returned. */
6366 tree
6367 finish_label_address_expr (tree label, location_t loc)
6369 tree result;
6371 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6373 if (label == error_mark_node)
6374 return error_mark_node;
6376 label = lookup_label (label);
6377 if (label == NULL_TREE)
6378 result = null_pointer_node;
6379 else
6381 TREE_USED (label) = 1;
6382 result = build1 (ADDR_EXPR, ptr_type_node, label);
6383 /* The current function is not necessarily uninlinable.
6384 Computed gotos are incompatible with inlining, but the value
6385 here could be used only in a diagnostic, for example. */
6386 protected_set_expr_location (result, loc);
6389 return result;
6393 /* Given a boolean expression ARG, return a tree representing an increment
6394 or decrement (as indicated by CODE) of ARG. The front end must check for
6395 invalid cases (e.g., decrement in C++). */
6396 tree
6397 boolean_increment (enum tree_code code, tree arg)
6399 tree val;
6400 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6402 arg = stabilize_reference (arg);
6403 switch (code)
6405 case PREINCREMENT_EXPR:
6406 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6407 break;
6408 case POSTINCREMENT_EXPR:
6409 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6410 arg = save_expr (arg);
6411 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6412 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6413 break;
6414 case PREDECREMENT_EXPR:
6415 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6416 invert_truthvalue_loc (input_location, arg));
6417 break;
6418 case POSTDECREMENT_EXPR:
6419 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6420 invert_truthvalue_loc (input_location, arg));
6421 arg = save_expr (arg);
6422 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6423 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6424 break;
6425 default:
6426 gcc_unreachable ();
6428 TREE_SIDE_EFFECTS (val) = 1;
6429 return val;
6432 /* Built-in macros for stddef.h and stdint.h, that require macros
6433 defined in this file. */
6434 void
6435 c_stddef_cpp_builtins(void)
6437 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6438 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6439 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6440 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6441 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6442 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6443 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6444 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6445 if (SIG_ATOMIC_TYPE)
6446 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6447 if (INT8_TYPE)
6448 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6449 if (INT16_TYPE)
6450 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6451 if (INT32_TYPE)
6452 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6453 if (INT64_TYPE)
6454 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6455 if (UINT8_TYPE)
6456 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6457 if (UINT16_TYPE)
6458 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6459 if (UINT32_TYPE)
6460 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6461 if (UINT64_TYPE)
6462 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6463 if (INT_LEAST8_TYPE)
6464 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6465 if (INT_LEAST16_TYPE)
6466 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6467 if (INT_LEAST32_TYPE)
6468 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6469 if (INT_LEAST64_TYPE)
6470 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6471 if (UINT_LEAST8_TYPE)
6472 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6473 if (UINT_LEAST16_TYPE)
6474 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6475 if (UINT_LEAST32_TYPE)
6476 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6477 if (UINT_LEAST64_TYPE)
6478 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6479 if (INT_FAST8_TYPE)
6480 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6481 if (INT_FAST16_TYPE)
6482 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6483 if (INT_FAST32_TYPE)
6484 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6485 if (INT_FAST64_TYPE)
6486 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6487 if (UINT_FAST8_TYPE)
6488 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6489 if (UINT_FAST16_TYPE)
6490 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6491 if (UINT_FAST32_TYPE)
6492 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6493 if (UINT_FAST64_TYPE)
6494 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6495 if (INTPTR_TYPE)
6496 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6497 if (UINTPTR_TYPE)
6498 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6501 static void
6502 c_init_attributes (void)
6504 /* Fill in the built_in_attributes array. */
6505 #define DEF_ATTR_NULL_TREE(ENUM) \
6506 built_in_attributes[(int) ENUM] = NULL_TREE;
6507 #define DEF_ATTR_INT(ENUM, VALUE) \
6508 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6509 #define DEF_ATTR_STRING(ENUM, VALUE) \
6510 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6511 #define DEF_ATTR_IDENT(ENUM, STRING) \
6512 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6513 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6514 built_in_attributes[(int) ENUM] \
6515 = tree_cons (built_in_attributes[(int) PURPOSE], \
6516 built_in_attributes[(int) VALUE], \
6517 built_in_attributes[(int) CHAIN]);
6518 #include "builtin-attrs.def"
6519 #undef DEF_ATTR_NULL_TREE
6520 #undef DEF_ATTR_INT
6521 #undef DEF_ATTR_IDENT
6522 #undef DEF_ATTR_TREE_LIST
6525 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6526 identifier as an argument, so the front end shouldn't look it up. */
6528 bool
6529 attribute_takes_identifier_p (const_tree attr_id)
6531 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6532 if (spec == NULL)
6533 /* Unknown attribute that we'll end up ignoring, return true so we
6534 don't complain about an identifier argument. */
6535 return true;
6536 else if (!strcmp ("mode", spec->name)
6537 || !strcmp ("format", spec->name)
6538 || !strcmp ("cleanup", spec->name))
6539 return true;
6540 else
6541 return targetm.attribute_takes_identifier_p (attr_id);
6544 /* Attribute handlers common to C front ends. */
6546 /* Handle a "packed" attribute; arguments as in
6547 struct attribute_spec.handler. */
6549 static tree
6550 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6551 int flags, bool *no_add_attrs)
6553 if (TYPE_P (*node))
6555 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6556 *node = build_variant_type_copy (*node);
6557 TYPE_PACKED (*node) = 1;
6559 else if (TREE_CODE (*node) == FIELD_DECL)
6561 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6562 /* Still pack bitfields. */
6563 && ! DECL_INITIAL (*node))
6564 warning (OPT_Wattributes,
6565 "%qE attribute ignored for field of type %qT",
6566 name, TREE_TYPE (*node));
6567 else
6568 DECL_PACKED (*node) = 1;
6570 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6571 used for DECL_REGISTER. It wouldn't mean anything anyway.
6572 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6573 that changes what the typedef is typing. */
6574 else
6576 warning (OPT_Wattributes, "%qE attribute ignored", name);
6577 *no_add_attrs = true;
6580 return NULL_TREE;
6583 /* Handle a "nocommon" attribute; arguments as in
6584 struct attribute_spec.handler. */
6586 static tree
6587 handle_nocommon_attribute (tree *node, tree name,
6588 tree ARG_UNUSED (args),
6589 int ARG_UNUSED (flags), bool *no_add_attrs)
6591 if (TREE_CODE (*node) == VAR_DECL)
6592 DECL_COMMON (*node) = 0;
6593 else
6595 warning (OPT_Wattributes, "%qE attribute ignored", name);
6596 *no_add_attrs = true;
6599 return NULL_TREE;
6602 /* Handle a "common" attribute; arguments as in
6603 struct attribute_spec.handler. */
6605 static tree
6606 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6607 int ARG_UNUSED (flags), bool *no_add_attrs)
6609 if (TREE_CODE (*node) == VAR_DECL)
6610 DECL_COMMON (*node) = 1;
6611 else
6613 warning (OPT_Wattributes, "%qE attribute ignored", name);
6614 *no_add_attrs = true;
6617 return NULL_TREE;
6620 /* Handle a "noreturn" attribute; arguments as in
6621 struct attribute_spec.handler. */
6623 static tree
6624 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6625 int ARG_UNUSED (flags), bool *no_add_attrs)
6627 tree type = TREE_TYPE (*node);
6629 /* See FIXME comment in c_common_attribute_table. */
6630 if (TREE_CODE (*node) == FUNCTION_DECL
6631 || objc_method_decl (TREE_CODE (*node)))
6632 TREE_THIS_VOLATILE (*node) = 1;
6633 else if (TREE_CODE (type) == POINTER_TYPE
6634 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6635 TREE_TYPE (*node)
6636 = (build_qualified_type
6637 (build_pointer_type
6638 (build_type_variant (TREE_TYPE (type),
6639 TYPE_READONLY (TREE_TYPE (type)), 1)),
6640 TYPE_QUALS (type)));
6641 else
6643 warning (OPT_Wattributes, "%qE attribute ignored", name);
6644 *no_add_attrs = true;
6647 return NULL_TREE;
6650 /* Handle a "hot" and attribute; arguments as in
6651 struct attribute_spec.handler. */
6653 static tree
6654 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6655 int ARG_UNUSED (flags), bool *no_add_attrs)
6657 if (TREE_CODE (*node) == FUNCTION_DECL
6658 || TREE_CODE (*node) == LABEL_DECL)
6660 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6662 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6663 "with attribute %qs", name, "cold");
6664 *no_add_attrs = true;
6666 /* Most of the rest of the hot processing is done later with
6667 lookup_attribute. */
6669 else
6671 warning (OPT_Wattributes, "%qE attribute ignored", name);
6672 *no_add_attrs = true;
6675 return NULL_TREE;
6678 /* Handle a "cold" and attribute; arguments as in
6679 struct attribute_spec.handler. */
6681 static tree
6682 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6683 int ARG_UNUSED (flags), bool *no_add_attrs)
6685 if (TREE_CODE (*node) == FUNCTION_DECL
6686 || TREE_CODE (*node) == LABEL_DECL)
6688 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6690 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6691 "with attribute %qs", name, "hot");
6692 *no_add_attrs = true;
6694 /* Most of the rest of the cold processing is done later with
6695 lookup_attribute. */
6697 else
6699 warning (OPT_Wattributes, "%qE attribute ignored", name);
6700 *no_add_attrs = true;
6703 return NULL_TREE;
6706 /* Handle a "no_sanitize_address" attribute; arguments as in
6707 struct attribute_spec.handler. */
6709 static tree
6710 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6711 bool *no_add_attrs)
6713 if (TREE_CODE (*node) != FUNCTION_DECL)
6715 warning (OPT_Wattributes, "%qE attribute ignored", name);
6716 *no_add_attrs = true;
6719 return NULL_TREE;
6722 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6723 struct attribute_spec.handler. */
6725 static tree
6726 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6727 bool *no_add_attrs)
6729 if (TREE_CODE (*node) != FUNCTION_DECL)
6730 warning (OPT_Wattributes, "%qE attribute ignored", name);
6731 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6732 DECL_ATTRIBUTES (*node)
6733 = tree_cons (get_identifier ("no_sanitize_address"),
6734 NULL_TREE, DECL_ATTRIBUTES (*node));
6735 *no_add_attrs = true;
6736 return NULL_TREE;
6739 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6740 struct attribute_spec.handler. */
6742 static tree
6743 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6744 bool *no_add_attrs)
6746 if (TREE_CODE (*node) != FUNCTION_DECL)
6748 warning (OPT_Wattributes, "%qE attribute ignored", name);
6749 *no_add_attrs = true;
6752 return NULL_TREE;
6755 /* Handle a "noinline" attribute; arguments as in
6756 struct attribute_spec.handler. */
6758 static tree
6759 handle_noinline_attribute (tree *node, tree name,
6760 tree ARG_UNUSED (args),
6761 int ARG_UNUSED (flags), bool *no_add_attrs)
6763 if (TREE_CODE (*node) == FUNCTION_DECL)
6765 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6767 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6768 "with attribute %qs", name, "always_inline");
6769 *no_add_attrs = true;
6771 else
6772 DECL_UNINLINABLE (*node) = 1;
6774 else
6776 warning (OPT_Wattributes, "%qE attribute ignored", name);
6777 *no_add_attrs = true;
6780 return NULL_TREE;
6783 /* Handle a "noclone" attribute; arguments as in
6784 struct attribute_spec.handler. */
6786 static tree
6787 handle_noclone_attribute (tree *node, tree name,
6788 tree ARG_UNUSED (args),
6789 int ARG_UNUSED (flags), bool *no_add_attrs)
6791 if (TREE_CODE (*node) != FUNCTION_DECL)
6793 warning (OPT_Wattributes, "%qE attribute ignored", name);
6794 *no_add_attrs = true;
6797 return NULL_TREE;
6800 /* Handle a "always_inline" attribute; arguments as in
6801 struct attribute_spec.handler. */
6803 static tree
6804 handle_always_inline_attribute (tree *node, tree name,
6805 tree ARG_UNUSED (args),
6806 int ARG_UNUSED (flags),
6807 bool *no_add_attrs)
6809 if (TREE_CODE (*node) == FUNCTION_DECL)
6811 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6813 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6814 "with %qs attribute", name, "noinline");
6815 *no_add_attrs = true;
6817 else
6818 /* Set the attribute and mark it for disregarding inline
6819 limits. */
6820 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6822 else
6824 warning (OPT_Wattributes, "%qE attribute ignored", name);
6825 *no_add_attrs = true;
6828 return NULL_TREE;
6831 /* Handle a "gnu_inline" attribute; arguments as in
6832 struct attribute_spec.handler. */
6834 static tree
6835 handle_gnu_inline_attribute (tree *node, tree name,
6836 tree ARG_UNUSED (args),
6837 int ARG_UNUSED (flags),
6838 bool *no_add_attrs)
6840 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6842 /* Do nothing else, just set the attribute. We'll get at
6843 it later with lookup_attribute. */
6845 else
6847 warning (OPT_Wattributes, "%qE attribute ignored", name);
6848 *no_add_attrs = true;
6851 return NULL_TREE;
6854 /* Handle a "leaf" attribute; arguments as in
6855 struct attribute_spec.handler. */
6857 static tree
6858 handle_leaf_attribute (tree *node, tree name,
6859 tree ARG_UNUSED (args),
6860 int ARG_UNUSED (flags), bool *no_add_attrs)
6862 if (TREE_CODE (*node) != FUNCTION_DECL)
6864 warning (OPT_Wattributes, "%qE attribute ignored", name);
6865 *no_add_attrs = true;
6867 if (!TREE_PUBLIC (*node))
6869 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6870 *no_add_attrs = true;
6873 return NULL_TREE;
6876 /* Handle an "artificial" attribute; arguments as in
6877 struct attribute_spec.handler. */
6879 static tree
6880 handle_artificial_attribute (tree *node, tree name,
6881 tree ARG_UNUSED (args),
6882 int ARG_UNUSED (flags),
6883 bool *no_add_attrs)
6885 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6887 /* Do nothing else, just set the attribute. We'll get at
6888 it later with lookup_attribute. */
6890 else
6892 warning (OPT_Wattributes, "%qE attribute ignored", name);
6893 *no_add_attrs = true;
6896 return NULL_TREE;
6899 /* Handle a "flatten" attribute; arguments as in
6900 struct attribute_spec.handler. */
6902 static tree
6903 handle_flatten_attribute (tree *node, tree name,
6904 tree args ATTRIBUTE_UNUSED,
6905 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6907 if (TREE_CODE (*node) == FUNCTION_DECL)
6908 /* Do nothing else, just set the attribute. We'll get at
6909 it later with lookup_attribute. */
6911 else
6913 warning (OPT_Wattributes, "%qE attribute ignored", name);
6914 *no_add_attrs = true;
6917 return NULL_TREE;
6920 /* Handle a "warning" or "error" attribute; arguments as in
6921 struct attribute_spec.handler. */
6923 static tree
6924 handle_error_attribute (tree *node, tree name, tree args,
6925 int ARG_UNUSED (flags), bool *no_add_attrs)
6927 if (TREE_CODE (*node) == FUNCTION_DECL
6928 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6929 /* Do nothing else, just set the attribute. We'll get at
6930 it later with lookup_attribute. */
6932 else
6934 warning (OPT_Wattributes, "%qE attribute ignored", name);
6935 *no_add_attrs = true;
6938 return NULL_TREE;
6941 /* Handle a "used" attribute; arguments as in
6942 struct attribute_spec.handler. */
6944 static tree
6945 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6946 int ARG_UNUSED (flags), bool *no_add_attrs)
6948 tree node = *pnode;
6950 if (TREE_CODE (node) == FUNCTION_DECL
6951 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6952 || (TREE_CODE (node) == TYPE_DECL))
6954 TREE_USED (node) = 1;
6955 DECL_PRESERVE_P (node) = 1;
6956 if (TREE_CODE (node) == VAR_DECL)
6957 DECL_READ_P (node) = 1;
6959 else
6961 warning (OPT_Wattributes, "%qE attribute ignored", name);
6962 *no_add_attrs = true;
6965 return NULL_TREE;
6968 /* Handle a "unused" attribute; arguments as in
6969 struct attribute_spec.handler. */
6971 static tree
6972 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6973 int flags, bool *no_add_attrs)
6975 if (DECL_P (*node))
6977 tree decl = *node;
6979 if (TREE_CODE (decl) == PARM_DECL
6980 || TREE_CODE (decl) == VAR_DECL
6981 || TREE_CODE (decl) == FUNCTION_DECL
6982 || TREE_CODE (decl) == LABEL_DECL
6983 || TREE_CODE (decl) == TYPE_DECL)
6985 TREE_USED (decl) = 1;
6986 if (TREE_CODE (decl) == VAR_DECL
6987 || TREE_CODE (decl) == PARM_DECL)
6988 DECL_READ_P (decl) = 1;
6990 else
6992 warning (OPT_Wattributes, "%qE attribute ignored", name);
6993 *no_add_attrs = true;
6996 else
6998 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6999 *node = build_variant_type_copy (*node);
7000 TREE_USED (*node) = 1;
7003 return NULL_TREE;
7006 /* Handle a "externally_visible" attribute; arguments as in
7007 struct attribute_spec.handler. */
7009 static tree
7010 handle_externally_visible_attribute (tree *pnode, tree name,
7011 tree ARG_UNUSED (args),
7012 int ARG_UNUSED (flags),
7013 bool *no_add_attrs)
7015 tree node = *pnode;
7017 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7019 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7020 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7022 warning (OPT_Wattributes,
7023 "%qE attribute have effect only on public objects", name);
7024 *no_add_attrs = true;
7027 else
7029 warning (OPT_Wattributes, "%qE attribute ignored", name);
7030 *no_add_attrs = true;
7033 return NULL_TREE;
7036 /* Handle the "no_reorder" attribute. Arguments as in
7037 struct attribute_spec.handler. */
7039 static tree
7040 handle_no_reorder_attribute (tree *pnode,
7041 tree name,
7042 tree,
7043 int,
7044 bool *no_add_attrs)
7046 tree node = *pnode;
7048 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7049 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7051 warning (OPT_Wattributes,
7052 "%qE attribute only affects top level objects",
7053 name);
7054 *no_add_attrs = true;
7057 return NULL_TREE;
7060 /* Handle a "const" attribute; arguments as in
7061 struct attribute_spec.handler. */
7063 static tree
7064 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7065 int ARG_UNUSED (flags), bool *no_add_attrs)
7067 tree type = TREE_TYPE (*node);
7069 /* See FIXME comment on noreturn in c_common_attribute_table. */
7070 if (TREE_CODE (*node) == FUNCTION_DECL)
7071 TREE_READONLY (*node) = 1;
7072 else if (TREE_CODE (type) == POINTER_TYPE
7073 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7074 TREE_TYPE (*node)
7075 = (build_qualified_type
7076 (build_pointer_type
7077 (build_type_variant (TREE_TYPE (type), 1,
7078 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7079 TYPE_QUALS (type)));
7080 else
7082 warning (OPT_Wattributes, "%qE attribute ignored", name);
7083 *no_add_attrs = true;
7086 return NULL_TREE;
7089 /* Handle a "transparent_union" attribute; arguments as in
7090 struct attribute_spec.handler. */
7092 static tree
7093 handle_transparent_union_attribute (tree *node, tree name,
7094 tree ARG_UNUSED (args), int flags,
7095 bool *no_add_attrs)
7097 tree type;
7099 *no_add_attrs = true;
7102 if (TREE_CODE (*node) == TYPE_DECL
7103 && ! (flags & ATTR_FLAG_CXX11))
7104 node = &TREE_TYPE (*node);
7105 type = *node;
7107 if (TREE_CODE (type) == UNION_TYPE)
7109 /* Make sure that the first field will work for a transparent union.
7110 If the type isn't complete yet, leave the check to the code in
7111 finish_struct. */
7112 if (TYPE_SIZE (type))
7114 tree first = first_field (type);
7115 if (first == NULL_TREE
7116 || DECL_ARTIFICIAL (first)
7117 || TYPE_MODE (type) != DECL_MODE (first))
7118 goto ignored;
7121 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7123 /* If the type isn't complete yet, setting the flag
7124 on a variant wouldn't ever be checked. */
7125 if (!TYPE_SIZE (type))
7126 goto ignored;
7128 /* build_duplicate_type doesn't work for C++. */
7129 if (c_dialect_cxx ())
7130 goto ignored;
7132 /* A type variant isn't good enough, since we don't a cast
7133 to such a type removed as a no-op. */
7134 *node = type = build_duplicate_type (type);
7137 TYPE_TRANSPARENT_AGGR (type) = 1;
7138 return NULL_TREE;
7141 ignored:
7142 warning (OPT_Wattributes, "%qE attribute ignored", name);
7143 return NULL_TREE;
7146 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7147 get the requested priority for a constructor or destructor,
7148 possibly issuing diagnostics for invalid or reserved
7149 priorities. */
7151 static priority_type
7152 get_priority (tree args, bool is_destructor)
7154 HOST_WIDE_INT pri;
7155 tree arg;
7157 if (!args)
7158 return DEFAULT_INIT_PRIORITY;
7160 if (!SUPPORTS_INIT_PRIORITY)
7162 if (is_destructor)
7163 error ("destructor priorities are not supported");
7164 else
7165 error ("constructor priorities are not supported");
7166 return DEFAULT_INIT_PRIORITY;
7169 arg = TREE_VALUE (args);
7170 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7171 goto invalid;
7172 if (arg == error_mark_node)
7173 return DEFAULT_INIT_PRIORITY;
7174 arg = default_conversion (arg);
7175 if (!tree_fits_shwi_p (arg)
7176 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7177 goto invalid;
7179 pri = tree_to_shwi (arg);
7180 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7181 goto invalid;
7183 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7185 if (is_destructor)
7186 warning (0,
7187 "destructor priorities from 0 to %d are reserved "
7188 "for the implementation",
7189 MAX_RESERVED_INIT_PRIORITY);
7190 else
7191 warning (0,
7192 "constructor priorities from 0 to %d are reserved "
7193 "for the implementation",
7194 MAX_RESERVED_INIT_PRIORITY);
7196 return pri;
7198 invalid:
7199 if (is_destructor)
7200 error ("destructor priorities must be integers from 0 to %d inclusive",
7201 MAX_INIT_PRIORITY);
7202 else
7203 error ("constructor priorities must be integers from 0 to %d inclusive",
7204 MAX_INIT_PRIORITY);
7205 return DEFAULT_INIT_PRIORITY;
7208 /* Handle a "constructor" attribute; arguments as in
7209 struct attribute_spec.handler. */
7211 static tree
7212 handle_constructor_attribute (tree *node, tree name, tree args,
7213 int ARG_UNUSED (flags),
7214 bool *no_add_attrs)
7216 tree decl = *node;
7217 tree type = TREE_TYPE (decl);
7219 if (TREE_CODE (decl) == FUNCTION_DECL
7220 && TREE_CODE (type) == FUNCTION_TYPE
7221 && decl_function_context (decl) == 0)
7223 priority_type priority;
7224 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7225 priority = get_priority (args, /*is_destructor=*/false);
7226 SET_DECL_INIT_PRIORITY (decl, priority);
7227 TREE_USED (decl) = 1;
7229 else
7231 warning (OPT_Wattributes, "%qE attribute ignored", name);
7232 *no_add_attrs = true;
7235 return NULL_TREE;
7238 /* Handle a "destructor" attribute; arguments as in
7239 struct attribute_spec.handler. */
7241 static tree
7242 handle_destructor_attribute (tree *node, tree name, tree args,
7243 int ARG_UNUSED (flags),
7244 bool *no_add_attrs)
7246 tree decl = *node;
7247 tree type = TREE_TYPE (decl);
7249 if (TREE_CODE (decl) == FUNCTION_DECL
7250 && TREE_CODE (type) == FUNCTION_TYPE
7251 && decl_function_context (decl) == 0)
7253 priority_type priority;
7254 DECL_STATIC_DESTRUCTOR (decl) = 1;
7255 priority = get_priority (args, /*is_destructor=*/true);
7256 SET_DECL_FINI_PRIORITY (decl, priority);
7257 TREE_USED (decl) = 1;
7259 else
7261 warning (OPT_Wattributes, "%qE attribute ignored", name);
7262 *no_add_attrs = true;
7265 return NULL_TREE;
7268 /* Nonzero if the mode is a valid vector mode for this architecture.
7269 This returns nonzero even if there is no hardware support for the
7270 vector mode, but we can emulate with narrower modes. */
7272 static int
7273 vector_mode_valid_p (machine_mode mode)
7275 enum mode_class mclass = GET_MODE_CLASS (mode);
7276 machine_mode innermode;
7278 /* Doh! What's going on? */
7279 if (mclass != MODE_VECTOR_INT
7280 && mclass != MODE_VECTOR_FLOAT
7281 && mclass != MODE_VECTOR_FRACT
7282 && mclass != MODE_VECTOR_UFRACT
7283 && mclass != MODE_VECTOR_ACCUM
7284 && mclass != MODE_VECTOR_UACCUM)
7285 return 0;
7287 /* Hardware support. Woo hoo! */
7288 if (targetm.vector_mode_supported_p (mode))
7289 return 1;
7291 innermode = GET_MODE_INNER (mode);
7293 /* We should probably return 1 if requesting V4DI and we have no DI,
7294 but we have V2DI, but this is probably very unlikely. */
7296 /* If we have support for the inner mode, we can safely emulate it.
7297 We may not have V2DI, but me can emulate with a pair of DIs. */
7298 return targetm.scalar_mode_supported_p (innermode);
7302 /* Handle a "mode" attribute; arguments as in
7303 struct attribute_spec.handler. */
7305 static tree
7306 handle_mode_attribute (tree *node, tree name, tree args,
7307 int ARG_UNUSED (flags), bool *no_add_attrs)
7309 tree type = *node;
7310 tree ident = TREE_VALUE (args);
7312 *no_add_attrs = true;
7314 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7315 warning (OPT_Wattributes, "%qE attribute ignored", name);
7316 else
7318 int j;
7319 const char *p = IDENTIFIER_POINTER (ident);
7320 int len = strlen (p);
7321 machine_mode mode = VOIDmode;
7322 tree typefm;
7323 bool valid_mode;
7325 if (len > 4 && p[0] == '_' && p[1] == '_'
7326 && p[len - 1] == '_' && p[len - 2] == '_')
7328 char *newp = (char *) alloca (len - 1);
7330 strcpy (newp, &p[2]);
7331 newp[len - 4] = '\0';
7332 p = newp;
7335 /* Change this type to have a type with the specified mode.
7336 First check for the special modes. */
7337 if (!strcmp (p, "byte"))
7338 mode = byte_mode;
7339 else if (!strcmp (p, "word"))
7340 mode = word_mode;
7341 else if (!strcmp (p, "pointer"))
7342 mode = ptr_mode;
7343 else if (!strcmp (p, "libgcc_cmp_return"))
7344 mode = targetm.libgcc_cmp_return_mode ();
7345 else if (!strcmp (p, "libgcc_shift_count"))
7346 mode = targetm.libgcc_shift_count_mode ();
7347 else if (!strcmp (p, "unwind_word"))
7348 mode = targetm.unwind_word_mode ();
7349 else
7350 for (j = 0; j < NUM_MACHINE_MODES; j++)
7351 if (!strcmp (p, GET_MODE_NAME (j)))
7353 mode = (machine_mode) j;
7354 break;
7357 if (mode == VOIDmode)
7359 error ("unknown machine mode %qE", ident);
7360 return NULL_TREE;
7363 valid_mode = false;
7364 switch (GET_MODE_CLASS (mode))
7366 case MODE_INT:
7367 case MODE_PARTIAL_INT:
7368 case MODE_FLOAT:
7369 case MODE_DECIMAL_FLOAT:
7370 case MODE_FRACT:
7371 case MODE_UFRACT:
7372 case MODE_ACCUM:
7373 case MODE_UACCUM:
7374 valid_mode = targetm.scalar_mode_supported_p (mode);
7375 break;
7377 case MODE_COMPLEX_INT:
7378 case MODE_COMPLEX_FLOAT:
7379 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7380 break;
7382 case MODE_VECTOR_INT:
7383 case MODE_VECTOR_FLOAT:
7384 case MODE_VECTOR_FRACT:
7385 case MODE_VECTOR_UFRACT:
7386 case MODE_VECTOR_ACCUM:
7387 case MODE_VECTOR_UACCUM:
7388 warning (OPT_Wattributes, "specifying vector types with "
7389 "__attribute__ ((mode)) is deprecated");
7390 warning (OPT_Wattributes,
7391 "use __attribute__ ((vector_size)) instead");
7392 valid_mode = vector_mode_valid_p (mode);
7393 break;
7395 default:
7396 break;
7398 if (!valid_mode)
7400 error ("unable to emulate %qs", p);
7401 return NULL_TREE;
7404 if (POINTER_TYPE_P (type))
7406 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7407 tree (*fn)(tree, machine_mode, bool);
7409 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7411 error ("invalid pointer mode %qs", p);
7412 return NULL_TREE;
7415 if (TREE_CODE (type) == POINTER_TYPE)
7416 fn = build_pointer_type_for_mode;
7417 else
7418 fn = build_reference_type_for_mode;
7419 typefm = fn (TREE_TYPE (type), mode, false);
7421 else
7423 /* For fixed-point modes, we need to test if the signness of type
7424 and the machine mode are consistent. */
7425 if (ALL_FIXED_POINT_MODE_P (mode)
7426 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7428 error ("signedness of type and machine mode %qs don%'t match", p);
7429 return NULL_TREE;
7431 /* For fixed-point modes, we need to pass saturating info. */
7432 typefm = lang_hooks.types.type_for_mode (mode,
7433 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7434 : TYPE_UNSIGNED (type));
7437 if (typefm == NULL_TREE)
7439 error ("no data type for mode %qs", p);
7440 return NULL_TREE;
7442 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7444 /* For enumeral types, copy the precision from the integer
7445 type returned above. If not an INTEGER_TYPE, we can't use
7446 this mode for this type. */
7447 if (TREE_CODE (typefm) != INTEGER_TYPE)
7449 error ("cannot use mode %qs for enumeral types", p);
7450 return NULL_TREE;
7453 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7455 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7456 typefm = type;
7458 else
7460 /* We cannot build a type variant, as there's code that assumes
7461 that TYPE_MAIN_VARIANT has the same mode. This includes the
7462 debug generators. Instead, create a subrange type. This
7463 results in all of the enumeral values being emitted only once
7464 in the original, and the subtype gets them by reference. */
7465 if (TYPE_UNSIGNED (type))
7466 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7467 else
7468 typefm = make_signed_type (TYPE_PRECISION (typefm));
7469 TREE_TYPE (typefm) = type;
7472 else if (VECTOR_MODE_P (mode)
7473 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7474 : TREE_CODE (type) != TREE_CODE (typefm))
7476 error ("mode %qs applied to inappropriate type", p);
7477 return NULL_TREE;
7480 *node = typefm;
7483 return NULL_TREE;
7486 /* Handle a "section" attribute; arguments as in
7487 struct attribute_spec.handler. */
7489 static tree
7490 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7491 int ARG_UNUSED (flags), bool *no_add_attrs)
7493 tree decl = *node;
7495 if (targetm_common.have_named_sections)
7497 user_defined_section_attribute = true;
7499 if ((TREE_CODE (decl) == FUNCTION_DECL
7500 || TREE_CODE (decl) == VAR_DECL)
7501 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7503 if (TREE_CODE (decl) == VAR_DECL
7504 && current_function_decl != NULL_TREE
7505 && !TREE_STATIC (decl))
7507 error_at (DECL_SOURCE_LOCATION (decl),
7508 "section attribute cannot be specified for "
7509 "local variables");
7510 *no_add_attrs = true;
7513 /* The decl may have already been given a section attribute
7514 from a previous declaration. Ensure they match. */
7515 else if (DECL_SECTION_NAME (decl) != NULL
7516 && strcmp (DECL_SECTION_NAME (decl),
7517 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7519 error ("section of %q+D conflicts with previous declaration",
7520 *node);
7521 *no_add_attrs = true;
7523 else if (TREE_CODE (decl) == VAR_DECL
7524 && !targetm.have_tls && targetm.emutls.tmpl_section
7525 && DECL_THREAD_LOCAL_P (decl))
7527 error ("section of %q+D cannot be overridden", *node);
7528 *no_add_attrs = true;
7530 else
7531 set_decl_section_name (decl,
7532 TREE_STRING_POINTER (TREE_VALUE (args)));
7534 else
7536 error ("section attribute not allowed for %q+D", *node);
7537 *no_add_attrs = true;
7540 else
7542 error_at (DECL_SOURCE_LOCATION (*node),
7543 "section attributes are not supported for this target");
7544 *no_add_attrs = true;
7547 return NULL_TREE;
7550 /* Check whether ALIGN is a valid user-specified alignment. If so,
7551 return its base-2 log; if not, output an error and return -1. If
7552 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7553 no error. */
7555 check_user_alignment (const_tree align, bool allow_zero)
7557 int i;
7559 if (error_operand_p (align))
7560 return -1;
7561 if (TREE_CODE (align) != INTEGER_CST
7562 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7564 error ("requested alignment is not an integer constant");
7565 return -1;
7567 else if (allow_zero && integer_zerop (align))
7568 return -1;
7569 else if (tree_int_cst_sgn (align) == -1
7570 || (i = tree_log2 (align)) == -1)
7572 error ("requested alignment is not a positive power of 2");
7573 return -1;
7575 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7577 error ("requested alignment is too large");
7578 return -1;
7580 return i;
7584 If in c++-11, check if the c++-11 alignment constraint with respect
7585 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7586 c++-11 mode, does nothing.
7588 [dcl.align]2/ says:
7590 [* if the constant expression evaluates to a fundamental alignment,
7591 the alignment requirement of the declared entity shall be the
7592 specified fundamental alignment.
7594 * if the constant expression evaluates to an extended alignment
7595 and the implementation supports that alignment in the context
7596 of the declaration, the alignment of the declared entity shall
7597 be that alignment
7599 * if the constant expression evaluates to an extended alignment
7600 and the implementation does not support that alignment in the
7601 context of the declaration, the program is ill-formed]. */
7603 static bool
7604 check_cxx_fundamental_alignment_constraints (tree node,
7605 unsigned align_log,
7606 int flags)
7608 bool alignment_too_large_p = false;
7609 unsigned requested_alignment = 1U << align_log;
7610 unsigned max_align = 0;
7612 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7613 || (node == NULL_TREE || node == error_mark_node))
7614 return true;
7616 if (cxx_fundamental_alignment_p (requested_alignment))
7617 return true;
7619 if (DECL_P (node))
7621 if (TREE_STATIC (node))
7623 /* For file scope variables and static members, the target
7624 supports alignments that are at most
7625 MAX_OFILE_ALIGNMENT. */
7626 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7627 alignment_too_large_p = true;
7629 else
7631 #ifdef BIGGEST_FIELD_ALIGNMENT
7632 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7633 #else
7634 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7635 #endif
7636 /* For non-static members, the target supports either
7637 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7638 if it is defined or BIGGEST_ALIGNMENT. */
7639 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7640 if (TREE_CODE (node) == FIELD_DECL
7641 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7642 alignment_too_large_p = true;
7643 #undef MAX_TARGET_FIELD_ALIGNMENT
7644 /* For stack variables, the target supports at most
7645 MAX_STACK_ALIGNMENT. */
7646 else if (decl_function_context (node) != NULL
7647 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7648 alignment_too_large_p = true;
7651 else if (TYPE_P (node))
7653 /* Let's be liberal for types. */
7654 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7655 alignment_too_large_p = true;
7658 if (alignment_too_large_p)
7659 pedwarn (input_location, OPT_Wattributes,
7660 "requested alignment %d is larger than %d",
7661 requested_alignment, max_align);
7663 return !alignment_too_large_p;
7666 /* Handle a "aligned" attribute; arguments as in
7667 struct attribute_spec.handler. */
7669 static tree
7670 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7671 int flags, bool *no_add_attrs)
7673 tree decl = NULL_TREE;
7674 tree *type = NULL;
7675 int is_type = 0;
7676 tree align_expr;
7677 int i;
7679 if (args)
7681 align_expr = TREE_VALUE (args);
7682 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7683 && TREE_CODE (align_expr) != FUNCTION_DECL)
7684 align_expr = default_conversion (align_expr);
7686 else
7687 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7689 if (DECL_P (*node))
7691 decl = *node;
7692 type = &TREE_TYPE (decl);
7693 is_type = TREE_CODE (*node) == TYPE_DECL;
7695 else if (TYPE_P (*node))
7696 type = node, is_type = 1;
7698 if ((i = check_user_alignment (align_expr, false)) == -1
7699 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7700 *no_add_attrs = true;
7701 else if (is_type)
7703 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7704 /* OK, modify the type in place. */;
7705 /* If we have a TYPE_DECL, then copy the type, so that we
7706 don't accidentally modify a builtin type. See pushdecl. */
7707 else if (decl && TREE_TYPE (decl) != error_mark_node
7708 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7710 tree tt = TREE_TYPE (decl);
7711 *type = build_variant_type_copy (*type);
7712 DECL_ORIGINAL_TYPE (decl) = tt;
7713 TYPE_NAME (*type) = decl;
7714 TREE_USED (*type) = TREE_USED (decl);
7715 TREE_TYPE (decl) = *type;
7717 else
7718 *type = build_variant_type_copy (*type);
7720 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7721 TYPE_USER_ALIGN (*type) = 1;
7723 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7724 && TREE_CODE (decl) != FIELD_DECL)
7726 error ("alignment may not be specified for %q+D", decl);
7727 *no_add_attrs = true;
7729 else if (DECL_USER_ALIGN (decl)
7730 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7731 /* C++-11 [dcl.align/4]:
7733 When multiple alignment-specifiers are specified for an
7734 entity, the alignment requirement shall be set to the
7735 strictest specified alignment.
7737 This formally comes from the c++11 specification but we are
7738 doing it for the GNU attribute syntax as well. */
7739 *no_add_attrs = true;
7740 else if (TREE_CODE (decl) == FUNCTION_DECL
7741 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7743 if (DECL_USER_ALIGN (decl))
7744 error ("alignment for %q+D was previously specified as %d "
7745 "and may not be decreased", decl,
7746 DECL_ALIGN (decl) / BITS_PER_UNIT);
7747 else
7748 error ("alignment for %q+D must be at least %d", decl,
7749 DECL_ALIGN (decl) / BITS_PER_UNIT);
7750 *no_add_attrs = true;
7752 else
7754 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7755 DECL_USER_ALIGN (decl) = 1;
7758 return NULL_TREE;
7761 /* Handle a "weak" attribute; arguments as in
7762 struct attribute_spec.handler. */
7764 static tree
7765 handle_weak_attribute (tree *node, tree name,
7766 tree ARG_UNUSED (args),
7767 int ARG_UNUSED (flags),
7768 bool * ARG_UNUSED (no_add_attrs))
7770 if (TREE_CODE (*node) == FUNCTION_DECL
7771 && DECL_DECLARED_INLINE_P (*node))
7773 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7774 *no_add_attrs = true;
7776 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7778 error ("indirect function %q+D cannot be declared weak", *node);
7779 *no_add_attrs = true;
7780 return NULL_TREE;
7782 else if (TREE_CODE (*node) == FUNCTION_DECL
7783 || TREE_CODE (*node) == VAR_DECL)
7785 struct symtab_node *n = symtab_node::get (*node);
7786 if (n && n->refuse_visibility_changes)
7787 error ("%+D declared weak after being used", *node);
7788 declare_weak (*node);
7790 else
7791 warning (OPT_Wattributes, "%qE attribute ignored", name);
7793 return NULL_TREE;
7796 /* Handle an "alias" or "ifunc" attribute; arguments as in
7797 struct attribute_spec.handler, except that IS_ALIAS tells us
7798 whether this is an alias as opposed to ifunc attribute. */
7800 static tree
7801 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7802 bool *no_add_attrs)
7804 tree decl = *node;
7806 if (TREE_CODE (decl) != FUNCTION_DECL
7807 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7809 warning (OPT_Wattributes, "%qE attribute ignored", name);
7810 *no_add_attrs = true;
7812 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7813 || (TREE_CODE (decl) != FUNCTION_DECL
7814 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7815 /* A static variable declaration is always a tentative definition,
7816 but the alias is a non-tentative definition which overrides. */
7817 || (TREE_CODE (decl) != FUNCTION_DECL
7818 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7820 error ("%q+D defined both normally and as %qE attribute", decl, name);
7821 *no_add_attrs = true;
7822 return NULL_TREE;
7824 else if (!is_alias
7825 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7826 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7828 error ("weak %q+D cannot be defined %qE", decl, name);
7829 *no_add_attrs = true;
7830 return NULL_TREE;
7833 /* Note that the very first time we process a nested declaration,
7834 decl_function_context will not be set. Indeed, *would* never
7835 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7836 we do below. After such frobbery, pushdecl would set the context.
7837 In any case, this is never what we want. */
7838 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7840 tree id;
7842 id = TREE_VALUE (args);
7843 if (TREE_CODE (id) != STRING_CST)
7845 error ("attribute %qE argument not a string", name);
7846 *no_add_attrs = true;
7847 return NULL_TREE;
7849 id = get_identifier (TREE_STRING_POINTER (id));
7850 /* This counts as a use of the object pointed to. */
7851 TREE_USED (id) = 1;
7853 if (TREE_CODE (decl) == FUNCTION_DECL)
7854 DECL_INITIAL (decl) = error_mark_node;
7855 else
7856 TREE_STATIC (decl) = 1;
7858 if (!is_alias)
7859 /* ifuncs are also aliases, so set that attribute too. */
7860 DECL_ATTRIBUTES (decl)
7861 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7863 else
7865 warning (OPT_Wattributes, "%qE attribute ignored", name);
7866 *no_add_attrs = true;
7869 if (decl_in_symtab_p (*node))
7871 struct symtab_node *n = symtab_node::get (decl);
7872 if (n && n->refuse_visibility_changes)
7874 if (is_alias)
7875 error ("%+D declared alias after being used", decl);
7876 else
7877 error ("%+D declared ifunc after being used", decl);
7882 return NULL_TREE;
7885 /* Handle an "alias" or "ifunc" attribute; arguments as in
7886 struct attribute_spec.handler. */
7888 static tree
7889 handle_ifunc_attribute (tree *node, tree name, tree args,
7890 int ARG_UNUSED (flags), bool *no_add_attrs)
7892 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7895 /* Handle an "alias" or "ifunc" attribute; arguments as in
7896 struct attribute_spec.handler. */
7898 static tree
7899 handle_alias_attribute (tree *node, tree name, tree args,
7900 int ARG_UNUSED (flags), bool *no_add_attrs)
7902 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7905 /* Handle a "weakref" attribute; arguments as in struct
7906 attribute_spec.handler. */
7908 static tree
7909 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7910 int flags, bool *no_add_attrs)
7912 tree attr = NULL_TREE;
7914 /* We must ignore the attribute when it is associated with
7915 local-scoped decls, since attribute alias is ignored and many
7916 such symbols do not even have a DECL_WEAK field. */
7917 if (decl_function_context (*node)
7918 || current_function_decl
7919 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7921 warning (OPT_Wattributes, "%qE attribute ignored", name);
7922 *no_add_attrs = true;
7923 return NULL_TREE;
7926 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7928 error ("indirect function %q+D cannot be declared weakref", *node);
7929 *no_add_attrs = true;
7930 return NULL_TREE;
7933 /* The idea here is that `weakref("name")' mutates into `weakref,
7934 alias("name")', and weakref without arguments, in turn,
7935 implicitly adds weak. */
7937 if (args)
7939 attr = tree_cons (get_identifier ("alias"), args, attr);
7940 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7942 *no_add_attrs = true;
7944 decl_attributes (node, attr, flags);
7946 else
7948 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7949 error_at (DECL_SOURCE_LOCATION (*node),
7950 "weakref attribute must appear before alias attribute");
7952 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7953 and that isn't supported; and because it wants to add it to
7954 the list of weak decls, which isn't helpful. */
7955 DECL_WEAK (*node) = 1;
7958 if (decl_in_symtab_p (*node))
7960 struct symtab_node *n = symtab_node::get (*node);
7961 if (n && n->refuse_visibility_changes)
7962 error ("%+D declared weakref after being used", *node);
7965 return NULL_TREE;
7968 /* Handle an "visibility" attribute; arguments as in
7969 struct attribute_spec.handler. */
7971 static tree
7972 handle_visibility_attribute (tree *node, tree name, tree args,
7973 int ARG_UNUSED (flags),
7974 bool *ARG_UNUSED (no_add_attrs))
7976 tree decl = *node;
7977 tree id = TREE_VALUE (args);
7978 enum symbol_visibility vis;
7980 if (TYPE_P (*node))
7982 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7983 /* OK */;
7984 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7986 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7987 name);
7988 return NULL_TREE;
7990 else if (TYPE_FIELDS (*node))
7992 error ("%qE attribute ignored because %qT is already defined",
7993 name, *node);
7994 return NULL_TREE;
7997 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7999 warning (OPT_Wattributes, "%qE attribute ignored", name);
8000 return NULL_TREE;
8003 if (TREE_CODE (id) != STRING_CST)
8005 error ("visibility argument not a string");
8006 return NULL_TREE;
8009 /* If this is a type, set the visibility on the type decl. */
8010 if (TYPE_P (decl))
8012 decl = TYPE_NAME (decl);
8013 if (!decl)
8014 return NULL_TREE;
8015 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8017 warning (OPT_Wattributes, "%qE attribute ignored on types",
8018 name);
8019 return NULL_TREE;
8023 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8024 vis = VISIBILITY_DEFAULT;
8025 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8026 vis = VISIBILITY_INTERNAL;
8027 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8028 vis = VISIBILITY_HIDDEN;
8029 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8030 vis = VISIBILITY_PROTECTED;
8031 else
8033 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8034 vis = VISIBILITY_DEFAULT;
8037 if (DECL_VISIBILITY_SPECIFIED (decl)
8038 && vis != DECL_VISIBILITY (decl))
8040 tree attributes = (TYPE_P (*node)
8041 ? TYPE_ATTRIBUTES (*node)
8042 : DECL_ATTRIBUTES (decl));
8043 if (lookup_attribute ("visibility", attributes))
8044 error ("%qD redeclared with different visibility", decl);
8045 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8046 && lookup_attribute ("dllimport", attributes))
8047 error ("%qD was declared %qs which implies default visibility",
8048 decl, "dllimport");
8049 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8050 && lookup_attribute ("dllexport", attributes))
8051 error ("%qD was declared %qs which implies default visibility",
8052 decl, "dllexport");
8055 DECL_VISIBILITY (decl) = vis;
8056 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8058 /* Go ahead and attach the attribute to the node as well. This is needed
8059 so we can determine whether we have VISIBILITY_DEFAULT because the
8060 visibility was not specified, or because it was explicitly overridden
8061 from the containing scope. */
8063 return NULL_TREE;
8066 /* Determine the ELF symbol visibility for DECL, which is either a
8067 variable or a function. It is an error to use this function if a
8068 definition of DECL is not available in this translation unit.
8069 Returns true if the final visibility has been determined by this
8070 function; false if the caller is free to make additional
8071 modifications. */
8073 bool
8074 c_determine_visibility (tree decl)
8076 gcc_assert (TREE_CODE (decl) == VAR_DECL
8077 || TREE_CODE (decl) == FUNCTION_DECL);
8079 /* If the user explicitly specified the visibility with an
8080 attribute, honor that. DECL_VISIBILITY will have been set during
8081 the processing of the attribute. We check for an explicit
8082 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8083 to distinguish the use of an attribute from the use of a "#pragma
8084 GCC visibility push(...)"; in the latter case we still want other
8085 considerations to be able to overrule the #pragma. */
8086 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8087 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8088 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8089 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8090 return true;
8092 /* Set default visibility to whatever the user supplied with
8093 visibility_specified depending on #pragma GCC visibility. */
8094 if (!DECL_VISIBILITY_SPECIFIED (decl))
8096 if (visibility_options.inpragma
8097 || DECL_VISIBILITY (decl) != default_visibility)
8099 DECL_VISIBILITY (decl) = default_visibility;
8100 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8101 /* If visibility changed and DECL already has DECL_RTL, ensure
8102 symbol flags are updated. */
8103 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8104 || TREE_CODE (decl) == FUNCTION_DECL)
8105 && DECL_RTL_SET_P (decl))
8106 make_decl_rtl (decl);
8109 return false;
8112 /* Handle an "tls_model" attribute; arguments as in
8113 struct attribute_spec.handler. */
8115 static tree
8116 handle_tls_model_attribute (tree *node, tree name, tree args,
8117 int ARG_UNUSED (flags), bool *no_add_attrs)
8119 tree id;
8120 tree decl = *node;
8121 enum tls_model kind;
8123 *no_add_attrs = true;
8125 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8127 warning (OPT_Wattributes, "%qE attribute ignored", name);
8128 return NULL_TREE;
8131 kind = DECL_TLS_MODEL (decl);
8132 id = TREE_VALUE (args);
8133 if (TREE_CODE (id) != STRING_CST)
8135 error ("tls_model argument not a string");
8136 return NULL_TREE;
8139 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8140 kind = TLS_MODEL_LOCAL_EXEC;
8141 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8142 kind = TLS_MODEL_INITIAL_EXEC;
8143 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8144 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8145 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8146 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8147 else
8148 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8150 set_decl_tls_model (decl, kind);
8151 return NULL_TREE;
8154 /* Handle a "no_instrument_function" attribute; arguments as in
8155 struct attribute_spec.handler. */
8157 static tree
8158 handle_no_instrument_function_attribute (tree *node, tree name,
8159 tree ARG_UNUSED (args),
8160 int ARG_UNUSED (flags),
8161 bool *no_add_attrs)
8163 tree decl = *node;
8165 if (TREE_CODE (decl) != FUNCTION_DECL)
8167 error_at (DECL_SOURCE_LOCATION (decl),
8168 "%qE attribute applies only to functions", name);
8169 *no_add_attrs = true;
8171 else
8172 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8174 return NULL_TREE;
8177 /* Handle a "malloc" attribute; arguments as in
8178 struct attribute_spec.handler. */
8180 static tree
8181 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8182 int ARG_UNUSED (flags), bool *no_add_attrs)
8184 if (TREE_CODE (*node) == FUNCTION_DECL
8185 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8186 DECL_IS_MALLOC (*node) = 1;
8187 else
8189 warning (OPT_Wattributes, "%qE attribute ignored", name);
8190 *no_add_attrs = true;
8193 return NULL_TREE;
8196 /* Handle a "alloc_size" attribute; arguments as in
8197 struct attribute_spec.handler. */
8199 static tree
8200 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8201 int ARG_UNUSED (flags), bool *no_add_attrs)
8203 unsigned arg_count = type_num_arguments (*node);
8204 for (; args; args = TREE_CHAIN (args))
8206 tree position = TREE_VALUE (args);
8207 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8208 && TREE_CODE (position) != FUNCTION_DECL)
8209 position = default_conversion (position);
8211 if (!tree_fits_uhwi_p (position)
8212 || !arg_count
8213 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8215 warning (OPT_Wattributes,
8216 "alloc_size parameter outside range");
8217 *no_add_attrs = true;
8218 return NULL_TREE;
8221 return NULL_TREE;
8224 /* Handle a "alloc_align" attribute; arguments as in
8225 struct attribute_spec.handler. */
8227 static tree
8228 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8229 bool *no_add_attrs)
8231 unsigned arg_count = type_num_arguments (*node);
8232 tree position = TREE_VALUE (args);
8233 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8234 position = default_conversion (position);
8236 if (!tree_fits_uhwi_p (position)
8237 || !arg_count
8238 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8240 warning (OPT_Wattributes,
8241 "alloc_align parameter outside range");
8242 *no_add_attrs = true;
8243 return NULL_TREE;
8245 return NULL_TREE;
8248 /* Handle a "assume_aligned" attribute; arguments as in
8249 struct attribute_spec.handler. */
8251 static tree
8252 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8253 bool *no_add_attrs)
8255 for (; args; args = TREE_CHAIN (args))
8257 tree position = TREE_VALUE (args);
8258 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8259 && TREE_CODE (position) != FUNCTION_DECL)
8260 position = default_conversion (position);
8262 if (TREE_CODE (position) != INTEGER_CST)
8264 warning (OPT_Wattributes,
8265 "assume_aligned parameter not integer constant");
8266 *no_add_attrs = true;
8267 return NULL_TREE;
8270 return NULL_TREE;
8273 /* Handle a "fn spec" attribute; arguments as in
8274 struct attribute_spec.handler. */
8276 static tree
8277 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8278 tree args, int ARG_UNUSED (flags),
8279 bool *no_add_attrs ATTRIBUTE_UNUSED)
8281 gcc_assert (args
8282 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8283 && !TREE_CHAIN (args));
8284 return NULL_TREE;
8287 /* Handle a "bnd_variable_size" attribute; arguments as in
8288 struct attribute_spec.handler. */
8290 static tree
8291 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8292 int ARG_UNUSED (flags), bool *no_add_attrs)
8294 if (TREE_CODE (*node) != FIELD_DECL)
8296 warning (OPT_Wattributes, "%qE attribute ignored", name);
8297 *no_add_attrs = true;
8300 return NULL_TREE;
8303 /* Handle a "bnd_legacy" attribute; arguments as in
8304 struct attribute_spec.handler. */
8306 static tree
8307 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8308 int ARG_UNUSED (flags), bool *no_add_attrs)
8310 if (TREE_CODE (*node) != FUNCTION_DECL)
8312 warning (OPT_Wattributes, "%qE attribute ignored", name);
8313 *no_add_attrs = true;
8316 return NULL_TREE;
8319 /* Handle a "bnd_instrument" attribute; arguments as in
8320 struct attribute_spec.handler. */
8322 static tree
8323 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8324 int ARG_UNUSED (flags), bool *no_add_attrs)
8326 if (TREE_CODE (*node) != FUNCTION_DECL)
8328 warning (OPT_Wattributes, "%qE attribute ignored", name);
8329 *no_add_attrs = true;
8332 return NULL_TREE;
8335 /* Handle a "warn_unused" attribute; arguments as in
8336 struct attribute_spec.handler. */
8338 static tree
8339 handle_warn_unused_attribute (tree *node, tree name,
8340 tree args ATTRIBUTE_UNUSED,
8341 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8343 if (TYPE_P (*node))
8344 /* Do nothing else, just set the attribute. We'll get at
8345 it later with lookup_attribute. */
8347 else
8349 warning (OPT_Wattributes, "%qE attribute ignored", name);
8350 *no_add_attrs = true;
8353 return NULL_TREE;
8356 /* Handle an "omp declare simd" attribute; arguments as in
8357 struct attribute_spec.handler. */
8359 static tree
8360 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8362 return NULL_TREE;
8365 /* Handle an "omp declare target" attribute; arguments as in
8366 struct attribute_spec.handler. */
8368 static tree
8369 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8371 return NULL_TREE;
8374 /* Handle a "returns_twice" attribute; arguments as in
8375 struct attribute_spec.handler. */
8377 static tree
8378 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8379 int ARG_UNUSED (flags), bool *no_add_attrs)
8381 if (TREE_CODE (*node) == FUNCTION_DECL)
8382 DECL_IS_RETURNS_TWICE (*node) = 1;
8383 else
8385 warning (OPT_Wattributes, "%qE attribute ignored", name);
8386 *no_add_attrs = true;
8389 return NULL_TREE;
8392 /* Handle a "no_limit_stack" attribute; arguments as in
8393 struct attribute_spec.handler. */
8395 static tree
8396 handle_no_limit_stack_attribute (tree *node, tree name,
8397 tree ARG_UNUSED (args),
8398 int ARG_UNUSED (flags),
8399 bool *no_add_attrs)
8401 tree decl = *node;
8403 if (TREE_CODE (decl) != FUNCTION_DECL)
8405 error_at (DECL_SOURCE_LOCATION (decl),
8406 "%qE attribute applies only to functions", name);
8407 *no_add_attrs = true;
8409 else if (DECL_INITIAL (decl))
8411 error_at (DECL_SOURCE_LOCATION (decl),
8412 "can%'t set %qE attribute after definition", name);
8413 *no_add_attrs = true;
8415 else
8416 DECL_NO_LIMIT_STACK (decl) = 1;
8418 return NULL_TREE;
8421 /* Handle a "pure" attribute; arguments as in
8422 struct attribute_spec.handler. */
8424 static tree
8425 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8426 int ARG_UNUSED (flags), bool *no_add_attrs)
8428 if (TREE_CODE (*node) == FUNCTION_DECL)
8429 DECL_PURE_P (*node) = 1;
8430 /* ??? TODO: Support types. */
8431 else
8433 warning (OPT_Wattributes, "%qE attribute ignored", name);
8434 *no_add_attrs = true;
8437 return NULL_TREE;
8440 /* Digest an attribute list destined for a transactional memory statement.
8441 ALLOWED is the set of attributes that are allowed for this statement;
8442 return the attribute we parsed. Multiple attributes are never allowed. */
8445 parse_tm_stmt_attr (tree attrs, int allowed)
8447 tree a_seen = NULL;
8448 int m_seen = 0;
8450 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8452 tree a = TREE_PURPOSE (attrs);
8453 int m = 0;
8455 if (is_attribute_p ("outer", a))
8456 m = TM_STMT_ATTR_OUTER;
8458 if ((m & allowed) == 0)
8460 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8461 continue;
8464 if (m_seen == 0)
8466 a_seen = a;
8467 m_seen = m;
8469 else if (m_seen == m)
8470 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8471 else
8472 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8475 return m_seen;
8478 /* Transform a TM attribute name into a maskable integer and back.
8479 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8480 to how the lack of an attribute is treated. */
8483 tm_attr_to_mask (tree attr)
8485 if (attr == NULL)
8486 return 0;
8487 if (is_attribute_p ("transaction_safe", attr))
8488 return TM_ATTR_SAFE;
8489 if (is_attribute_p ("transaction_callable", attr))
8490 return TM_ATTR_CALLABLE;
8491 if (is_attribute_p ("transaction_pure", attr))
8492 return TM_ATTR_PURE;
8493 if (is_attribute_p ("transaction_unsafe", attr))
8494 return TM_ATTR_IRREVOCABLE;
8495 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8496 return TM_ATTR_MAY_CANCEL_OUTER;
8497 return 0;
8500 tree
8501 tm_mask_to_attr (int mask)
8503 const char *str;
8504 switch (mask)
8506 case TM_ATTR_SAFE:
8507 str = "transaction_safe";
8508 break;
8509 case TM_ATTR_CALLABLE:
8510 str = "transaction_callable";
8511 break;
8512 case TM_ATTR_PURE:
8513 str = "transaction_pure";
8514 break;
8515 case TM_ATTR_IRREVOCABLE:
8516 str = "transaction_unsafe";
8517 break;
8518 case TM_ATTR_MAY_CANCEL_OUTER:
8519 str = "transaction_may_cancel_outer";
8520 break;
8521 default:
8522 gcc_unreachable ();
8524 return get_identifier (str);
8527 /* Return the first TM attribute seen in LIST. */
8529 tree
8530 find_tm_attribute (tree list)
8532 for (; list ; list = TREE_CHAIN (list))
8534 tree name = TREE_PURPOSE (list);
8535 if (tm_attr_to_mask (name) != 0)
8536 return name;
8538 return NULL_TREE;
8541 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8542 Here we accept only function types, and verify that none of the other
8543 function TM attributes are also applied. */
8544 /* ??? We need to accept class types for C++, but not C. This greatly
8545 complicates this function, since we can no longer rely on the extra
8546 processing given by function_type_required. */
8548 static tree
8549 handle_tm_attribute (tree *node, tree name, tree args,
8550 int flags, bool *no_add_attrs)
8552 /* Only one path adds the attribute; others don't. */
8553 *no_add_attrs = true;
8555 switch (TREE_CODE (*node))
8557 case RECORD_TYPE:
8558 case UNION_TYPE:
8559 /* Only tm_callable and tm_safe apply to classes. */
8560 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8561 goto ignored;
8562 /* FALLTHRU */
8564 case FUNCTION_TYPE:
8565 case METHOD_TYPE:
8567 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8568 if (old_name == name)
8570 else if (old_name != NULL_TREE)
8571 error ("type was previously declared %qE", old_name);
8572 else
8573 *no_add_attrs = false;
8575 break;
8577 case POINTER_TYPE:
8579 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8580 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8582 tree fn_tmp = TREE_TYPE (*node);
8583 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8584 *node = build_pointer_type (fn_tmp);
8585 break;
8588 /* FALLTHRU */
8590 default:
8591 /* If a function is next, pass it on to be tried next. */
8592 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8593 return tree_cons (name, args, NULL);
8595 ignored:
8596 warning (OPT_Wattributes, "%qE attribute ignored", name);
8597 break;
8600 return NULL_TREE;
8603 /* Handle the TM_WRAP attribute; arguments as in
8604 struct attribute_spec.handler. */
8606 static tree
8607 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8608 int ARG_UNUSED (flags), bool *no_add_attrs)
8610 tree decl = *node;
8612 /* We don't need the attribute even on success, since we
8613 record the entry in an external table. */
8614 *no_add_attrs = true;
8616 if (TREE_CODE (decl) != FUNCTION_DECL)
8617 warning (OPT_Wattributes, "%qE attribute ignored", name);
8618 else
8620 tree wrap_decl = TREE_VALUE (args);
8621 if (error_operand_p (wrap_decl))
8623 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8624 && TREE_CODE (wrap_decl) != VAR_DECL
8625 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8626 error ("%qE argument not an identifier", name);
8627 else
8629 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8630 wrap_decl = lookup_name (wrap_decl);
8631 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8633 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8634 TREE_TYPE (wrap_decl)))
8635 record_tm_replacement (wrap_decl, decl);
8636 else
8637 error ("%qD is not compatible with %qD", wrap_decl, decl);
8639 else
8640 error ("%qE argument is not a function", name);
8644 return NULL_TREE;
8647 /* Ignore the given attribute. Used when this attribute may be usefully
8648 overridden by the target, but is not used generically. */
8650 static tree
8651 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8652 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8653 bool *no_add_attrs)
8655 *no_add_attrs = true;
8656 return NULL_TREE;
8659 /* Handle a "no vops" attribute; arguments as in
8660 struct attribute_spec.handler. */
8662 static tree
8663 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8664 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8665 bool *ARG_UNUSED (no_add_attrs))
8667 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8668 DECL_IS_NOVOPS (*node) = 1;
8669 return NULL_TREE;
8672 /* Handle a "deprecated" attribute; arguments as in
8673 struct attribute_spec.handler. */
8675 static tree
8676 handle_deprecated_attribute (tree *node, tree name,
8677 tree args, int flags,
8678 bool *no_add_attrs)
8680 tree type = NULL_TREE;
8681 int warn = 0;
8682 tree what = NULL_TREE;
8684 if (!args)
8685 *no_add_attrs = true;
8686 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8688 error ("deprecated message is not a string");
8689 *no_add_attrs = true;
8692 if (DECL_P (*node))
8694 tree decl = *node;
8695 type = TREE_TYPE (decl);
8697 if (TREE_CODE (decl) == TYPE_DECL
8698 || TREE_CODE (decl) == PARM_DECL
8699 || TREE_CODE (decl) == VAR_DECL
8700 || TREE_CODE (decl) == FUNCTION_DECL
8701 || TREE_CODE (decl) == FIELD_DECL
8702 || objc_method_decl (TREE_CODE (decl)))
8703 TREE_DEPRECATED (decl) = 1;
8704 else
8705 warn = 1;
8707 else if (TYPE_P (*node))
8709 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8710 *node = build_variant_type_copy (*node);
8711 TREE_DEPRECATED (*node) = 1;
8712 type = *node;
8714 else
8715 warn = 1;
8717 if (warn)
8719 *no_add_attrs = true;
8720 if (type && TYPE_NAME (type))
8722 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8723 what = TYPE_NAME (*node);
8724 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8725 && DECL_NAME (TYPE_NAME (type)))
8726 what = DECL_NAME (TYPE_NAME (type));
8728 if (what)
8729 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8730 else
8731 warning (OPT_Wattributes, "%qE attribute ignored", name);
8734 return NULL_TREE;
8737 /* Handle a "vector_size" attribute; arguments as in
8738 struct attribute_spec.handler. */
8740 static tree
8741 handle_vector_size_attribute (tree *node, tree name, tree args,
8742 int ARG_UNUSED (flags),
8743 bool *no_add_attrs)
8745 unsigned HOST_WIDE_INT vecsize, nunits;
8746 machine_mode orig_mode;
8747 tree type = *node, new_type, size;
8749 *no_add_attrs = true;
8751 size = TREE_VALUE (args);
8752 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8753 && TREE_CODE (size) != FUNCTION_DECL)
8754 size = default_conversion (size);
8756 if (!tree_fits_uhwi_p (size))
8758 warning (OPT_Wattributes, "%qE attribute ignored", name);
8759 return NULL_TREE;
8762 /* Get the vector size (in bytes). */
8763 vecsize = tree_to_uhwi (size);
8765 /* We need to provide for vector pointers, vector arrays, and
8766 functions returning vectors. For example:
8768 __attribute__((vector_size(16))) short *foo;
8770 In this case, the mode is SI, but the type being modified is
8771 HI, so we need to look further. */
8773 while (POINTER_TYPE_P (type)
8774 || TREE_CODE (type) == FUNCTION_TYPE
8775 || TREE_CODE (type) == METHOD_TYPE
8776 || TREE_CODE (type) == ARRAY_TYPE
8777 || TREE_CODE (type) == OFFSET_TYPE)
8778 type = TREE_TYPE (type);
8780 /* Get the mode of the type being modified. */
8781 orig_mode = TYPE_MODE (type);
8783 if ((!INTEGRAL_TYPE_P (type)
8784 && !SCALAR_FLOAT_TYPE_P (type)
8785 && !FIXED_POINT_TYPE_P (type))
8786 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8787 && GET_MODE_CLASS (orig_mode) != MODE_INT
8788 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8789 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8790 || TREE_CODE (type) == BOOLEAN_TYPE)
8792 error ("invalid vector type for attribute %qE", name);
8793 return NULL_TREE;
8796 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8798 error ("vector size not an integral multiple of component size");
8799 return NULL;
8802 if (vecsize == 0)
8804 error ("zero vector size");
8805 return NULL;
8808 /* Calculate how many units fit in the vector. */
8809 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8810 if (nunits & (nunits - 1))
8812 error ("number of components of the vector not a power of two");
8813 return NULL_TREE;
8816 new_type = build_vector_type (type, nunits);
8818 /* Build back pointers if needed. */
8819 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8821 return NULL_TREE;
8824 /* Handle the "nonnull" attribute. */
8825 static tree
8826 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8827 tree args, int ARG_UNUSED (flags),
8828 bool *no_add_attrs)
8830 tree type = *node;
8831 unsigned HOST_WIDE_INT attr_arg_num;
8833 /* If no arguments are specified, all pointer arguments should be
8834 non-null. Verify a full prototype is given so that the arguments
8835 will have the correct types when we actually check them later. */
8836 if (!args)
8838 if (!prototype_p (type))
8840 error ("nonnull attribute without arguments on a non-prototype");
8841 *no_add_attrs = true;
8843 return NULL_TREE;
8846 /* Argument list specified. Verify that each argument number references
8847 a pointer argument. */
8848 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8850 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8852 tree arg = TREE_VALUE (args);
8853 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8854 && TREE_CODE (arg) != FUNCTION_DECL)
8855 arg = default_conversion (arg);
8857 if (!get_nonnull_operand (arg, &arg_num))
8859 error ("nonnull argument has invalid operand number (argument %lu)",
8860 (unsigned long) attr_arg_num);
8861 *no_add_attrs = true;
8862 return NULL_TREE;
8865 if (prototype_p (type))
8867 function_args_iterator iter;
8868 tree argument;
8870 function_args_iter_init (&iter, type);
8871 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8873 argument = function_args_iter_cond (&iter);
8874 if (argument == NULL_TREE || ck_num == arg_num)
8875 break;
8878 if (!argument
8879 || TREE_CODE (argument) == VOID_TYPE)
8881 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8882 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8883 *no_add_attrs = true;
8884 return NULL_TREE;
8887 if (TREE_CODE (argument) != POINTER_TYPE)
8889 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8890 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8891 *no_add_attrs = true;
8892 return NULL_TREE;
8897 return NULL_TREE;
8900 /* Check the argument list of a function call for null in argument slots
8901 that are marked as requiring a non-null pointer argument. The NARGS
8902 arguments are passed in the array ARGARRAY.
8905 static void
8906 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8908 tree a;
8909 int i;
8911 attrs = lookup_attribute ("nonnull", attrs);
8912 if (attrs == NULL_TREE)
8913 return;
8915 a = attrs;
8916 /* See if any of the nonnull attributes has no arguments. If so,
8917 then every pointer argument is checked (in which case the check
8918 for pointer type is done in check_nonnull_arg). */
8919 if (TREE_VALUE (a) != NULL_TREE)
8921 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8922 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8924 if (a != NULL_TREE)
8925 for (i = 0; i < nargs; i++)
8926 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8927 i + 1);
8928 else
8930 /* Walk the argument list. If we encounter an argument number we
8931 should check for non-null, do it. */
8932 for (i = 0; i < nargs; i++)
8934 for (a = attrs; ; a = TREE_CHAIN (a))
8936 a = lookup_attribute ("nonnull", a);
8937 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8938 break;
8941 if (a != NULL_TREE)
8942 check_function_arguments_recurse (check_nonnull_arg, NULL,
8943 argarray[i], i + 1);
8948 /* Check that the Nth argument of a function call (counting backwards
8949 from the end) is a (pointer)0. The NARGS arguments are passed in the
8950 array ARGARRAY. */
8952 static void
8953 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8955 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8957 if (attr)
8959 int len = 0;
8960 int pos = 0;
8961 tree sentinel;
8962 function_args_iterator iter;
8963 tree t;
8965 /* Skip over the named arguments. */
8966 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8968 if (len == nargs)
8969 break;
8970 len++;
8973 if (TREE_VALUE (attr))
8975 tree p = TREE_VALUE (TREE_VALUE (attr));
8976 pos = TREE_INT_CST_LOW (p);
8979 /* The sentinel must be one of the varargs, i.e.
8980 in position >= the number of fixed arguments. */
8981 if ((nargs - 1 - pos) < len)
8983 warning (OPT_Wformat_,
8984 "not enough variable arguments to fit a sentinel");
8985 return;
8988 /* Validate the sentinel. */
8989 sentinel = argarray[nargs - 1 - pos];
8990 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8991 || !integer_zerop (sentinel))
8992 /* Although __null (in C++) is only an integer we allow it
8993 nevertheless, as we are guaranteed that it's exactly
8994 as wide as a pointer, and we don't want to force
8995 users to cast the NULL they have written there.
8996 We warn with -Wstrict-null-sentinel, though. */
8997 && (warn_strict_null_sentinel || null_node != sentinel))
8998 warning (OPT_Wformat_, "missing sentinel in function call");
9002 /* Helper for check_function_nonnull; given a list of operands which
9003 must be non-null in ARGS, determine if operand PARAM_NUM should be
9004 checked. */
9006 static bool
9007 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9009 unsigned HOST_WIDE_INT arg_num = 0;
9011 for (; args; args = TREE_CHAIN (args))
9013 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9015 gcc_assert (found);
9017 if (arg_num == param_num)
9018 return true;
9020 return false;
9023 /* Check that the function argument PARAM (which is operand number
9024 PARAM_NUM) is non-null. This is called by check_function_nonnull
9025 via check_function_arguments_recurse. */
9027 static void
9028 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9029 unsigned HOST_WIDE_INT param_num)
9031 /* Just skip checking the argument if it's not a pointer. This can
9032 happen if the "nonnull" attribute was given without an operand
9033 list (which means to check every pointer argument). */
9035 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9036 return;
9038 if (integer_zerop (param))
9039 warning (OPT_Wnonnull, "null argument where non-null required "
9040 "(argument %lu)", (unsigned long) param_num);
9043 /* Helper for nonnull attribute handling; fetch the operand number
9044 from the attribute argument list. */
9046 static bool
9047 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9049 /* Verify the arg number is a small constant. */
9050 if (tree_fits_uhwi_p (arg_num_expr))
9052 *valp = TREE_INT_CST_LOW (arg_num_expr);
9053 return true;
9055 else
9056 return false;
9059 /* Handle a "nothrow" attribute; arguments as in
9060 struct attribute_spec.handler. */
9062 static tree
9063 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9064 int ARG_UNUSED (flags), bool *no_add_attrs)
9066 if (TREE_CODE (*node) == FUNCTION_DECL)
9067 TREE_NOTHROW (*node) = 1;
9068 /* ??? TODO: Support types. */
9069 else
9071 warning (OPT_Wattributes, "%qE attribute ignored", name);
9072 *no_add_attrs = true;
9075 return NULL_TREE;
9078 /* Handle a "cleanup" attribute; arguments as in
9079 struct attribute_spec.handler. */
9081 static tree
9082 handle_cleanup_attribute (tree *node, tree name, tree args,
9083 int ARG_UNUSED (flags), bool *no_add_attrs)
9085 tree decl = *node;
9086 tree cleanup_id, cleanup_decl;
9088 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9089 for global destructors in C++. This requires infrastructure that
9090 we don't have generically at the moment. It's also not a feature
9091 we'd be missing too much, since we do have attribute constructor. */
9092 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9094 warning (OPT_Wattributes, "%qE attribute ignored", name);
9095 *no_add_attrs = true;
9096 return NULL_TREE;
9099 /* Verify that the argument is a function in scope. */
9100 /* ??? We could support pointers to functions here as well, if
9101 that was considered desirable. */
9102 cleanup_id = TREE_VALUE (args);
9103 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9105 error ("cleanup argument not an identifier");
9106 *no_add_attrs = true;
9107 return NULL_TREE;
9109 cleanup_decl = lookup_name (cleanup_id);
9110 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9112 error ("cleanup argument not a function");
9113 *no_add_attrs = true;
9114 return NULL_TREE;
9117 /* That the function has proper type is checked with the
9118 eventual call to build_function_call. */
9120 return NULL_TREE;
9123 /* Handle a "warn_unused_result" attribute. No special handling. */
9125 static tree
9126 handle_warn_unused_result_attribute (tree *node, tree name,
9127 tree ARG_UNUSED (args),
9128 int ARG_UNUSED (flags), bool *no_add_attrs)
9130 /* Ignore the attribute for functions not returning any value. */
9131 if (VOID_TYPE_P (TREE_TYPE (*node)))
9133 warning (OPT_Wattributes, "%qE attribute ignored", name);
9134 *no_add_attrs = true;
9137 return NULL_TREE;
9140 /* Handle a "sentinel" attribute. */
9142 static tree
9143 handle_sentinel_attribute (tree *node, tree name, tree args,
9144 int ARG_UNUSED (flags), bool *no_add_attrs)
9146 if (!prototype_p (*node))
9148 warning (OPT_Wattributes,
9149 "%qE attribute requires prototypes with named arguments", name);
9150 *no_add_attrs = true;
9152 else
9154 if (!stdarg_p (*node))
9156 warning (OPT_Wattributes,
9157 "%qE attribute only applies to variadic functions", name);
9158 *no_add_attrs = true;
9162 if (args)
9164 tree position = TREE_VALUE (args);
9165 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9166 && TREE_CODE (position) != FUNCTION_DECL)
9167 position = default_conversion (position);
9169 if (TREE_CODE (position) != INTEGER_CST
9170 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9172 warning (OPT_Wattributes,
9173 "requested position is not an integer constant");
9174 *no_add_attrs = true;
9176 else
9178 if (tree_int_cst_lt (position, integer_zero_node))
9180 warning (OPT_Wattributes,
9181 "requested position is less than zero");
9182 *no_add_attrs = true;
9187 return NULL_TREE;
9190 /* Handle a "type_generic" attribute. */
9192 static tree
9193 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9194 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9195 bool * ARG_UNUSED (no_add_attrs))
9197 /* Ensure we have a function type. */
9198 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9200 /* Ensure we have a variadic function. */
9201 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9203 return NULL_TREE;
9206 /* Handle a "target" attribute. */
9208 static tree
9209 handle_target_attribute (tree *node, tree name, tree args, int flags,
9210 bool *no_add_attrs)
9212 /* Ensure we have a function type. */
9213 if (TREE_CODE (*node) != FUNCTION_DECL)
9215 warning (OPT_Wattributes, "%qE attribute ignored", name);
9216 *no_add_attrs = true;
9218 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9219 flags))
9220 *no_add_attrs = true;
9222 return NULL_TREE;
9225 /* Arguments being collected for optimization. */
9226 typedef const char *const_char_p; /* For DEF_VEC_P. */
9227 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9230 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9231 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9232 false for #pragma GCC optimize. */
9234 bool
9235 parse_optimize_options (tree args, bool attr_p)
9237 bool ret = true;
9238 unsigned opt_argc;
9239 unsigned i;
9240 int saved_flag_strict_aliasing;
9241 const char **opt_argv;
9242 struct cl_decoded_option *decoded_options;
9243 unsigned int decoded_options_count;
9244 tree ap;
9246 /* Build up argv vector. Just in case the string is stored away, use garbage
9247 collected strings. */
9248 vec_safe_truncate (optimize_args, 0);
9249 vec_safe_push (optimize_args, (const char *) NULL);
9251 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9253 tree value = TREE_VALUE (ap);
9255 if (TREE_CODE (value) == INTEGER_CST)
9257 char buffer[20];
9258 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9259 vec_safe_push (optimize_args, ggc_strdup (buffer));
9262 else if (TREE_CODE (value) == STRING_CST)
9264 /* Split string into multiple substrings. */
9265 size_t len = TREE_STRING_LENGTH (value);
9266 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9267 char *end = p + len;
9268 char *comma;
9269 char *next_p = p;
9271 while (next_p != NULL)
9273 size_t len2;
9274 char *q, *r;
9276 p = next_p;
9277 comma = strchr (p, ',');
9278 if (comma)
9280 len2 = comma - p;
9281 *comma = '\0';
9282 next_p = comma+1;
9284 else
9286 len2 = end - p;
9287 next_p = NULL;
9290 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9292 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9293 options. */
9294 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9296 ret = false;
9297 if (attr_p)
9298 warning (OPT_Wattributes,
9299 "bad option %s to optimize attribute", p);
9300 else
9301 warning (OPT_Wpragmas,
9302 "bad option %s to pragma attribute", p);
9303 continue;
9306 if (*p != '-')
9308 *r++ = '-';
9310 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9311 itself is -Os, and any other switch begins with a -f. */
9312 if ((*p >= '0' && *p <= '9')
9313 || (p[0] == 's' && p[1] == '\0'))
9314 *r++ = 'O';
9315 else if (*p != 'O')
9316 *r++ = 'f';
9319 memcpy (r, p, len2);
9320 r[len2] = '\0';
9321 vec_safe_push (optimize_args, (const char *) q);
9327 opt_argc = optimize_args->length ();
9328 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9330 for (i = 1; i < opt_argc; i++)
9331 opt_argv[i] = (*optimize_args)[i];
9333 saved_flag_strict_aliasing = flag_strict_aliasing;
9335 /* Now parse the options. */
9336 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9337 &decoded_options,
9338 &decoded_options_count);
9339 decode_options (&global_options, &global_options_set,
9340 decoded_options, decoded_options_count,
9341 input_location, global_dc);
9343 targetm.override_options_after_change();
9345 /* Don't allow changing -fstrict-aliasing. */
9346 flag_strict_aliasing = saved_flag_strict_aliasing;
9348 optimize_args->truncate (0);
9349 return ret;
9352 /* For handling "optimize" attribute. arguments as in
9353 struct attribute_spec.handler. */
9355 static tree
9356 handle_optimize_attribute (tree *node, tree name, tree args,
9357 int ARG_UNUSED (flags), bool *no_add_attrs)
9359 /* Ensure we have a function type. */
9360 if (TREE_CODE (*node) != FUNCTION_DECL)
9362 warning (OPT_Wattributes, "%qE attribute ignored", name);
9363 *no_add_attrs = true;
9365 else
9367 struct cl_optimization cur_opts;
9368 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9370 /* Save current options. */
9371 cl_optimization_save (&cur_opts, &global_options);
9373 /* If we previously had some optimization options, use them as the
9374 default. */
9375 if (old_opts)
9376 cl_optimization_restore (&global_options,
9377 TREE_OPTIMIZATION (old_opts));
9379 /* Parse options, and update the vector. */
9380 parse_optimize_options (args, true);
9381 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9382 = build_optimization_node (&global_options);
9384 /* Restore current options. */
9385 cl_optimization_restore (&global_options, &cur_opts);
9388 return NULL_TREE;
9391 /* Handle a "no_split_stack" attribute. */
9393 static tree
9394 handle_no_split_stack_attribute (tree *node, tree name,
9395 tree ARG_UNUSED (args),
9396 int ARG_UNUSED (flags),
9397 bool *no_add_attrs)
9399 tree decl = *node;
9401 if (TREE_CODE (decl) != FUNCTION_DECL)
9403 error_at (DECL_SOURCE_LOCATION (decl),
9404 "%qE attribute applies only to functions", name);
9405 *no_add_attrs = true;
9407 else if (DECL_INITIAL (decl))
9409 error_at (DECL_SOURCE_LOCATION (decl),
9410 "can%'t set %qE attribute after definition", name);
9411 *no_add_attrs = true;
9414 return NULL_TREE;
9417 /* Handle a "returns_nonnull" attribute; arguments as in
9418 struct attribute_spec.handler. */
9420 static tree
9421 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9422 bool *no_add_attrs)
9424 // Even without a prototype we still have a return type we can check.
9425 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9427 error ("returns_nonnull attribute on a function not returning a pointer");
9428 *no_add_attrs = true;
9430 return NULL_TREE;
9433 /* Handle a "designated_init" attribute; arguments as in
9434 struct attribute_spec.handler. */
9436 static tree
9437 handle_designated_init_attribute (tree *node, tree name, tree, int,
9438 bool *no_add_attrs)
9440 if (TREE_CODE (*node) != RECORD_TYPE)
9442 error ("%qE attribute is only valid on %<struct%> type", name);
9443 *no_add_attrs = true;
9445 return NULL_TREE;
9449 /* Check for valid arguments being passed to a function with FNTYPE.
9450 There are NARGS arguments in the array ARGARRAY. */
9451 void
9452 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9454 /* Check for null being passed in a pointer argument that must be
9455 non-null. We also need to do this if format checking is enabled. */
9457 if (warn_nonnull)
9458 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9460 /* Check for errors in format strings. */
9462 if (warn_format || warn_suggest_attribute_format)
9463 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9465 if (warn_format)
9466 check_function_sentinel (fntype, nargs, argarray);
9469 /* Generic argument checking recursion routine. PARAM is the argument to
9470 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9471 once the argument is resolved. CTX is context for the callback. */
9472 void
9473 check_function_arguments_recurse (void (*callback)
9474 (void *, tree, unsigned HOST_WIDE_INT),
9475 void *ctx, tree param,
9476 unsigned HOST_WIDE_INT param_num)
9478 if (CONVERT_EXPR_P (param)
9479 && (TYPE_PRECISION (TREE_TYPE (param))
9480 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9482 /* Strip coercion. */
9483 check_function_arguments_recurse (callback, ctx,
9484 TREE_OPERAND (param, 0), param_num);
9485 return;
9488 if (TREE_CODE (param) == CALL_EXPR)
9490 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9491 tree attrs;
9492 bool found_format_arg = false;
9494 /* See if this is a call to a known internationalization function
9495 that modifies a format arg. Such a function may have multiple
9496 format_arg attributes (for example, ngettext). */
9498 for (attrs = TYPE_ATTRIBUTES (type);
9499 attrs;
9500 attrs = TREE_CHAIN (attrs))
9501 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9503 tree inner_arg;
9504 tree format_num_expr;
9505 int format_num;
9506 int i;
9507 call_expr_arg_iterator iter;
9509 /* Extract the argument number, which was previously checked
9510 to be valid. */
9511 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9513 format_num = tree_to_uhwi (format_num_expr);
9515 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9516 inner_arg != 0;
9517 inner_arg = next_call_expr_arg (&iter), i++)
9518 if (i == format_num)
9520 check_function_arguments_recurse (callback, ctx,
9521 inner_arg, param_num);
9522 found_format_arg = true;
9523 break;
9527 /* If we found a format_arg attribute and did a recursive check,
9528 we are done with checking this argument. Otherwise, we continue
9529 and this will be considered a non-literal. */
9530 if (found_format_arg)
9531 return;
9534 if (TREE_CODE (param) == COND_EXPR)
9536 /* Check both halves of the conditional expression. */
9537 check_function_arguments_recurse (callback, ctx,
9538 TREE_OPERAND (param, 1), param_num);
9539 check_function_arguments_recurse (callback, ctx,
9540 TREE_OPERAND (param, 2), param_num);
9541 return;
9544 (*callback) (ctx, param, param_num);
9547 /* Checks for a builtin function FNDECL that the number of arguments
9548 NARGS against the required number REQUIRED and issues an error if
9549 there is a mismatch. Returns true if the number of arguments is
9550 correct, otherwise false. */
9552 static bool
9553 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9555 if (nargs < required)
9557 error_at (input_location,
9558 "not enough arguments to function %qE", fndecl);
9559 return false;
9561 else if (nargs > required)
9563 error_at (input_location,
9564 "too many arguments to function %qE", fndecl);
9565 return false;
9567 return true;
9570 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9571 Returns false if there was an error, otherwise true. */
9573 bool
9574 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9576 if (!DECL_BUILT_IN (fndecl)
9577 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9578 return true;
9580 switch (DECL_FUNCTION_CODE (fndecl))
9582 case BUILT_IN_CONSTANT_P:
9583 return builtin_function_validate_nargs (fndecl, nargs, 1);
9585 case BUILT_IN_ISFINITE:
9586 case BUILT_IN_ISINF:
9587 case BUILT_IN_ISINF_SIGN:
9588 case BUILT_IN_ISNAN:
9589 case BUILT_IN_ISNORMAL:
9590 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9592 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9594 error ("non-floating-point argument in call to "
9595 "function %qE", fndecl);
9596 return false;
9598 return true;
9600 return false;
9602 case BUILT_IN_ISGREATER:
9603 case BUILT_IN_ISGREATEREQUAL:
9604 case BUILT_IN_ISLESS:
9605 case BUILT_IN_ISLESSEQUAL:
9606 case BUILT_IN_ISLESSGREATER:
9607 case BUILT_IN_ISUNORDERED:
9608 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9610 enum tree_code code0, code1;
9611 code0 = TREE_CODE (TREE_TYPE (args[0]));
9612 code1 = TREE_CODE (TREE_TYPE (args[1]));
9613 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9614 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9615 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9617 error ("non-floating-point arguments in call to "
9618 "function %qE", fndecl);
9619 return false;
9621 return true;
9623 return false;
9625 case BUILT_IN_FPCLASSIFY:
9626 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9628 unsigned i;
9630 for (i=0; i<5; i++)
9631 if (TREE_CODE (args[i]) != INTEGER_CST)
9633 error ("non-const integer argument %u in call to function %qE",
9634 i+1, fndecl);
9635 return false;
9638 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9640 error ("non-floating-point argument in call to function %qE",
9641 fndecl);
9642 return false;
9644 return true;
9646 return false;
9648 case BUILT_IN_ASSUME_ALIGNED:
9649 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9651 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9653 error ("non-integer argument 3 in call to function %qE", fndecl);
9654 return false;
9656 return true;
9658 return false;
9660 case BUILT_IN_ADD_OVERFLOW:
9661 case BUILT_IN_SUB_OVERFLOW:
9662 case BUILT_IN_MUL_OVERFLOW:
9663 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9665 unsigned i;
9666 for (i = 0; i < 2; i++)
9667 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9669 error ("argument %u in call to function %qE does not have "
9670 "integral type", i + 1, fndecl);
9671 return false;
9673 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9674 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9676 error ("argument 3 in call to function %qE does not have "
9677 "pointer to integer type", fndecl);
9678 return false;
9680 return true;
9682 return false;
9684 default:
9685 return true;
9689 /* Function to help qsort sort FIELD_DECLs by name order. */
9692 field_decl_cmp (const void *x_p, const void *y_p)
9694 const tree *const x = (const tree *const) x_p;
9695 const tree *const y = (const tree *const) y_p;
9697 if (DECL_NAME (*x) == DECL_NAME (*y))
9698 /* A nontype is "greater" than a type. */
9699 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9700 if (DECL_NAME (*x) == NULL_TREE)
9701 return -1;
9702 if (DECL_NAME (*y) == NULL_TREE)
9703 return 1;
9704 if (DECL_NAME (*x) < DECL_NAME (*y))
9705 return -1;
9706 return 1;
9709 static struct {
9710 gt_pointer_operator new_value;
9711 void *cookie;
9712 } resort_data;
9714 /* This routine compares two fields like field_decl_cmp but using the
9715 pointer operator in resort_data. */
9717 static int
9718 resort_field_decl_cmp (const void *x_p, const void *y_p)
9720 const tree *const x = (const tree *const) x_p;
9721 const tree *const y = (const tree *const) y_p;
9723 if (DECL_NAME (*x) == DECL_NAME (*y))
9724 /* A nontype is "greater" than a type. */
9725 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9726 if (DECL_NAME (*x) == NULL_TREE)
9727 return -1;
9728 if (DECL_NAME (*y) == NULL_TREE)
9729 return 1;
9731 tree d1 = DECL_NAME (*x);
9732 tree d2 = DECL_NAME (*y);
9733 resort_data.new_value (&d1, resort_data.cookie);
9734 resort_data.new_value (&d2, resort_data.cookie);
9735 if (d1 < d2)
9736 return -1;
9738 return 1;
9741 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9743 void
9744 resort_sorted_fields (void *obj,
9745 void * ARG_UNUSED (orig_obj),
9746 gt_pointer_operator new_value,
9747 void *cookie)
9749 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9750 resort_data.new_value = new_value;
9751 resort_data.cookie = cookie;
9752 qsort (&sf->elts[0], sf->len, sizeof (tree),
9753 resort_field_decl_cmp);
9756 /* Subroutine of c_parse_error.
9757 Return the result of concatenating LHS and RHS. RHS is really
9758 a string literal, its first character is indicated by RHS_START and
9759 RHS_SIZE is its length (including the terminating NUL character).
9761 The caller is responsible for deleting the returned pointer. */
9763 static char *
9764 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9766 const int lhs_size = strlen (lhs);
9767 char *result = XNEWVEC (char, lhs_size + rhs_size);
9768 strncpy (result, lhs, lhs_size);
9769 strncpy (result + lhs_size, rhs_start, rhs_size);
9770 return result;
9773 /* Issue the error given by GMSGID, indicating that it occurred before
9774 TOKEN, which had the associated VALUE. */
9776 void
9777 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9778 tree value, unsigned char token_flags)
9780 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9782 char *message = NULL;
9784 if (token_type == CPP_EOF)
9785 message = catenate_messages (gmsgid, " at end of input");
9786 else if (token_type == CPP_CHAR
9787 || token_type == CPP_WCHAR
9788 || token_type == CPP_CHAR16
9789 || token_type == CPP_CHAR32)
9791 unsigned int val = TREE_INT_CST_LOW (value);
9792 const char *prefix;
9794 switch (token_type)
9796 default:
9797 prefix = "";
9798 break;
9799 case CPP_WCHAR:
9800 prefix = "L";
9801 break;
9802 case CPP_CHAR16:
9803 prefix = "u";
9804 break;
9805 case CPP_CHAR32:
9806 prefix = "U";
9807 break;
9810 if (val <= UCHAR_MAX && ISGRAPH (val))
9811 message = catenate_messages (gmsgid, " before %s'%c'");
9812 else
9813 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9815 error (message, prefix, val);
9816 free (message);
9817 message = NULL;
9819 else if (token_type == CPP_CHAR_USERDEF
9820 || token_type == CPP_WCHAR_USERDEF
9821 || token_type == CPP_CHAR16_USERDEF
9822 || token_type == CPP_CHAR32_USERDEF)
9823 message = catenate_messages (gmsgid,
9824 " before user-defined character literal");
9825 else if (token_type == CPP_STRING_USERDEF
9826 || token_type == CPP_WSTRING_USERDEF
9827 || token_type == CPP_STRING16_USERDEF
9828 || token_type == CPP_STRING32_USERDEF
9829 || token_type == CPP_UTF8STRING_USERDEF)
9830 message = catenate_messages (gmsgid, " before user-defined string literal");
9831 else if (token_type == CPP_STRING
9832 || token_type == CPP_WSTRING
9833 || token_type == CPP_STRING16
9834 || token_type == CPP_STRING32
9835 || token_type == CPP_UTF8STRING)
9836 message = catenate_messages (gmsgid, " before string constant");
9837 else if (token_type == CPP_NUMBER)
9838 message = catenate_messages (gmsgid, " before numeric constant");
9839 else if (token_type == CPP_NAME)
9841 message = catenate_messages (gmsgid, " before %qE");
9842 error (message, value);
9843 free (message);
9844 message = NULL;
9846 else if (token_type == CPP_PRAGMA)
9847 message = catenate_messages (gmsgid, " before %<#pragma%>");
9848 else if (token_type == CPP_PRAGMA_EOL)
9849 message = catenate_messages (gmsgid, " before end of line");
9850 else if (token_type == CPP_DECLTYPE)
9851 message = catenate_messages (gmsgid, " before %<decltype%>");
9852 else if (token_type < N_TTYPES)
9854 message = catenate_messages (gmsgid, " before %qs token");
9855 error (message, cpp_type2name (token_type, token_flags));
9856 free (message);
9857 message = NULL;
9859 else
9860 error (gmsgid);
9862 if (message)
9864 error (message);
9865 free (message);
9867 #undef catenate_messages
9870 /* Return the gcc option code associated with the reason for a cpp
9871 message, or 0 if none. */
9873 static int
9874 c_option_controlling_cpp_error (int reason)
9876 const struct cpp_reason_option_codes_t *entry;
9878 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
9880 if (entry->reason == reason)
9881 return entry->option_code;
9883 return 0;
9886 /* Callback from cpp_error for PFILE to print diagnostics from the
9887 preprocessor. The diagnostic is of type LEVEL, with REASON set
9888 to the reason code if LEVEL is represents a warning, at location
9889 LOCATION unless this is after lexing and the compiler's location
9890 should be used instead, with column number possibly overridden by
9891 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9892 the arguments. Returns true if a diagnostic was emitted, false
9893 otherwise. */
9895 bool
9896 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9897 location_t location, unsigned int column_override,
9898 const char *msg, va_list *ap)
9900 diagnostic_info diagnostic;
9901 diagnostic_t dlevel;
9902 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9903 bool ret;
9905 switch (level)
9907 case CPP_DL_WARNING_SYSHDR:
9908 if (flag_no_output)
9909 return false;
9910 global_dc->dc_warn_system_headers = 1;
9911 /* Fall through. */
9912 case CPP_DL_WARNING:
9913 if (flag_no_output)
9914 return false;
9915 dlevel = DK_WARNING;
9916 break;
9917 case CPP_DL_PEDWARN:
9918 if (flag_no_output && !flag_pedantic_errors)
9919 return false;
9920 dlevel = DK_PEDWARN;
9921 break;
9922 case CPP_DL_ERROR:
9923 dlevel = DK_ERROR;
9924 break;
9925 case CPP_DL_ICE:
9926 dlevel = DK_ICE;
9927 break;
9928 case CPP_DL_NOTE:
9929 dlevel = DK_NOTE;
9930 break;
9931 case CPP_DL_FATAL:
9932 dlevel = DK_FATAL;
9933 break;
9934 default:
9935 gcc_unreachable ();
9937 if (done_lexing)
9938 location = input_location;
9939 diagnostic_set_info_translated (&diagnostic, msg, ap,
9940 location, dlevel);
9941 if (column_override)
9942 diagnostic_override_column (&diagnostic, column_override);
9943 diagnostic_override_option_index (&diagnostic,
9944 c_option_controlling_cpp_error (reason));
9945 ret = report_diagnostic (&diagnostic);
9946 if (level == CPP_DL_WARNING_SYSHDR)
9947 global_dc->dc_warn_system_headers = save_warn_system_headers;
9948 return ret;
9951 /* Convert a character from the host to the target execution character
9952 set. cpplib handles this, mostly. */
9954 HOST_WIDE_INT
9955 c_common_to_target_charset (HOST_WIDE_INT c)
9957 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9958 zero-extended under -fno-signed-char. cpplib insists that characters
9959 and character constants are always unsigned. Hence we must convert
9960 back and forth. */
9961 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9963 uc = cpp_host_to_exec_charset (parse_in, uc);
9965 if (flag_signed_char)
9966 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9967 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9968 else
9969 return uc;
9972 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9973 references with an INDIRECT_REF of a constant at the bottom; much like the
9974 traditional rendering of offsetof as a macro. Return the folded result. */
9976 tree
9977 fold_offsetof_1 (tree expr)
9979 tree base, off, t;
9981 switch (TREE_CODE (expr))
9983 case ERROR_MARK:
9984 return expr;
9986 case VAR_DECL:
9987 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9988 return error_mark_node;
9990 case CALL_EXPR:
9991 case TARGET_EXPR:
9992 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9993 return error_mark_node;
9995 case NOP_EXPR:
9996 case INDIRECT_REF:
9997 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9999 error ("cannot apply %<offsetof%> to a non constant address");
10000 return error_mark_node;
10002 return TREE_OPERAND (expr, 0);
10004 case COMPONENT_REF:
10005 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10006 if (base == error_mark_node)
10007 return base;
10009 t = TREE_OPERAND (expr, 1);
10010 if (DECL_C_BIT_FIELD (t))
10012 error ("attempt to take address of bit-field structure "
10013 "member %qD", t);
10014 return error_mark_node;
10016 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10017 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10018 / BITS_PER_UNIT));
10019 break;
10021 case ARRAY_REF:
10022 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10023 if (base == error_mark_node)
10024 return base;
10026 t = TREE_OPERAND (expr, 1);
10028 /* Check if the offset goes beyond the upper bound of the array. */
10029 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10031 tree upbound = array_ref_up_bound (expr);
10032 if (upbound != NULL_TREE
10033 && TREE_CODE (upbound) == INTEGER_CST
10034 && !tree_int_cst_equal (upbound,
10035 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10037 upbound = size_binop (PLUS_EXPR, upbound,
10038 build_int_cst (TREE_TYPE (upbound), 1));
10039 if (tree_int_cst_lt (upbound, t))
10041 tree v;
10043 for (v = TREE_OPERAND (expr, 0);
10044 TREE_CODE (v) == COMPONENT_REF;
10045 v = TREE_OPERAND (v, 0))
10046 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10047 == RECORD_TYPE)
10049 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10050 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10051 if (TREE_CODE (fld_chain) == FIELD_DECL)
10052 break;
10054 if (fld_chain)
10055 break;
10057 /* Don't warn if the array might be considered a poor
10058 man's flexible array member with a very permissive
10059 definition thereof. */
10060 if (TREE_CODE (v) == ARRAY_REF
10061 || TREE_CODE (v) == COMPONENT_REF)
10062 warning (OPT_Warray_bounds,
10063 "index %E denotes an offset "
10064 "greater than size of %qT",
10065 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10070 t = convert (sizetype, t);
10071 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10072 break;
10074 case COMPOUND_EXPR:
10075 /* Handle static members of volatile structs. */
10076 t = TREE_OPERAND (expr, 1);
10077 gcc_assert (TREE_CODE (t) == VAR_DECL);
10078 return fold_offsetof_1 (t);
10080 default:
10081 gcc_unreachable ();
10084 return fold_build_pointer_plus (base, off);
10087 /* Likewise, but convert it to the return type of offsetof. */
10089 tree
10090 fold_offsetof (tree expr)
10092 return convert (size_type_node, fold_offsetof_1 (expr));
10095 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10096 expression, because B will always be true. */
10098 void
10099 warn_for_omitted_condop (location_t location, tree cond)
10101 if (truth_value_p (TREE_CODE (cond)))
10102 warning_at (location, OPT_Wparentheses,
10103 "the omitted middle operand in ?: will always be %<true%>, "
10104 "suggest explicit middle operand");
10107 /* Give an error for storing into ARG, which is 'const'. USE indicates
10108 how ARG was being used. */
10110 void
10111 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10113 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10114 || use == lv_asm);
10115 /* Using this macro rather than (for example) arrays of messages
10116 ensures that all the format strings are checked at compile
10117 time. */
10118 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10119 : (use == lv_increment ? (I) \
10120 : (use == lv_decrement ? (D) : (AS))))
10121 if (TREE_CODE (arg) == COMPONENT_REF)
10123 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10124 error_at (loc, READONLY_MSG (G_("assignment of member "
10125 "%qD in read-only object"),
10126 G_("increment of member "
10127 "%qD in read-only object"),
10128 G_("decrement of member "
10129 "%qD in read-only object"),
10130 G_("member %qD in read-only object "
10131 "used as %<asm%> output")),
10132 TREE_OPERAND (arg, 1));
10133 else
10134 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10135 G_("increment of read-only member %qD"),
10136 G_("decrement of read-only member %qD"),
10137 G_("read-only member %qD used as %<asm%> output")),
10138 TREE_OPERAND (arg, 1));
10140 else if (TREE_CODE (arg) == VAR_DECL)
10141 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10142 G_("increment of read-only variable %qD"),
10143 G_("decrement of read-only variable %qD"),
10144 G_("read-only variable %qD used as %<asm%> output")),
10145 arg);
10146 else if (TREE_CODE (arg) == PARM_DECL)
10147 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10148 G_("increment of read-only parameter %qD"),
10149 G_("decrement of read-only parameter %qD"),
10150 G_("read-only parameter %qD use as %<asm%> output")),
10151 arg);
10152 else if (TREE_CODE (arg) == RESULT_DECL)
10154 gcc_assert (c_dialect_cxx ());
10155 error_at (loc, READONLY_MSG (G_("assignment of "
10156 "read-only named return value %qD"),
10157 G_("increment of "
10158 "read-only named return value %qD"),
10159 G_("decrement of "
10160 "read-only named return value %qD"),
10161 G_("read-only named return value %qD "
10162 "used as %<asm%>output")),
10163 arg);
10165 else if (TREE_CODE (arg) == FUNCTION_DECL)
10166 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10167 G_("increment of function %qD"),
10168 G_("decrement of function %qD"),
10169 G_("function %qD used as %<asm%> output")),
10170 arg);
10171 else
10172 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10173 G_("increment of read-only location %qE"),
10174 G_("decrement of read-only location %qE"),
10175 G_("read-only location %qE used as %<asm%> output")),
10176 arg);
10179 /* Print an error message for an invalid lvalue. USE says
10180 how the lvalue is being used and so selects the error message. LOC
10181 is the location for the error. */
10183 void
10184 lvalue_error (location_t loc, enum lvalue_use use)
10186 switch (use)
10188 case lv_assign:
10189 error_at (loc, "lvalue required as left operand of assignment");
10190 break;
10191 case lv_increment:
10192 error_at (loc, "lvalue required as increment operand");
10193 break;
10194 case lv_decrement:
10195 error_at (loc, "lvalue required as decrement operand");
10196 break;
10197 case lv_addressof:
10198 error_at (loc, "lvalue required as unary %<&%> operand");
10199 break;
10200 case lv_asm:
10201 error_at (loc, "lvalue required in asm statement");
10202 break;
10203 default:
10204 gcc_unreachable ();
10208 /* Print an error message for an invalid indirection of type TYPE.
10209 ERRSTRING is the name of the operator for the indirection. */
10211 void
10212 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10214 switch (errstring)
10216 case RO_NULL:
10217 gcc_assert (c_dialect_cxx ());
10218 error_at (loc, "invalid type argument (have %qT)", type);
10219 break;
10220 case RO_ARRAY_INDEXING:
10221 error_at (loc,
10222 "invalid type argument of array indexing (have %qT)",
10223 type);
10224 break;
10225 case RO_UNARY_STAR:
10226 error_at (loc,
10227 "invalid type argument of unary %<*%> (have %qT)",
10228 type);
10229 break;
10230 case RO_ARROW:
10231 error_at (loc,
10232 "invalid type argument of %<->%> (have %qT)",
10233 type);
10234 break;
10235 case RO_ARROW_STAR:
10236 error_at (loc,
10237 "invalid type argument of %<->*%> (have %qT)",
10238 type);
10239 break;
10240 case RO_IMPLICIT_CONVERSION:
10241 error_at (loc,
10242 "invalid type argument of implicit conversion (have %qT)",
10243 type);
10244 break;
10245 default:
10246 gcc_unreachable ();
10250 /* *PTYPE is an incomplete array. Complete it with a domain based on
10251 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10252 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10253 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10256 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10258 tree maxindex, type, main_type, elt, unqual_elt;
10259 int failure = 0, quals;
10260 hashval_t hashcode = 0;
10261 bool overflow_p = false;
10263 maxindex = size_zero_node;
10264 if (initial_value)
10266 if (TREE_CODE (initial_value) == STRING_CST)
10268 int eltsize
10269 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10270 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10272 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10274 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10276 if (vec_safe_is_empty (v))
10278 if (pedantic)
10279 failure = 3;
10280 maxindex = ssize_int (-1);
10282 else
10284 tree curindex;
10285 unsigned HOST_WIDE_INT cnt;
10286 constructor_elt *ce;
10287 bool fold_p = false;
10289 if ((*v)[0].index)
10290 maxindex = (*v)[0].index, fold_p = true;
10292 curindex = maxindex;
10294 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10296 bool curfold_p = false;
10297 if (ce->index)
10298 curindex = ce->index, curfold_p = true;
10299 else
10301 if (fold_p)
10303 /* Since we treat size types now as ordinary
10304 unsigned types, we need an explicit overflow
10305 check. */
10306 tree orig = curindex;
10307 curindex = fold_convert (sizetype, curindex);
10308 overflow_p |= tree_int_cst_lt (curindex, orig);
10310 curindex = size_binop (PLUS_EXPR, curindex,
10311 size_one_node);
10313 if (tree_int_cst_lt (maxindex, curindex))
10314 maxindex = curindex, fold_p = curfold_p;
10316 if (fold_p)
10318 tree orig = maxindex;
10319 maxindex = fold_convert (sizetype, maxindex);
10320 overflow_p |= tree_int_cst_lt (maxindex, orig);
10324 else
10326 /* Make an error message unless that happened already. */
10327 if (initial_value != error_mark_node)
10328 failure = 1;
10331 else
10333 failure = 2;
10334 if (!do_default)
10335 return failure;
10338 type = *ptype;
10339 elt = TREE_TYPE (type);
10340 quals = TYPE_QUALS (strip_array_types (elt));
10341 if (quals == 0)
10342 unqual_elt = elt;
10343 else
10344 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10346 /* Using build_distinct_type_copy and modifying things afterward instead
10347 of using build_array_type to create a new type preserves all of the
10348 TYPE_LANG_FLAG_? bits that the front end may have set. */
10349 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10350 TREE_TYPE (main_type) = unqual_elt;
10351 TYPE_DOMAIN (main_type)
10352 = build_range_type (TREE_TYPE (maxindex),
10353 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10354 layout_type (main_type);
10356 /* Make sure we have the canonical MAIN_TYPE. */
10357 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10358 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10359 hashcode);
10360 main_type = type_hash_canon (hashcode, main_type);
10362 /* Fix the canonical type. */
10363 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10364 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10365 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10366 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10367 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10368 != TYPE_DOMAIN (main_type)))
10369 TYPE_CANONICAL (main_type)
10370 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10371 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10372 else
10373 TYPE_CANONICAL (main_type) = main_type;
10375 if (quals == 0)
10376 type = main_type;
10377 else
10378 type = c_build_qualified_type (main_type, quals);
10380 if (COMPLETE_TYPE_P (type)
10381 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10382 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10384 error ("size of array is too large");
10385 /* If we proceed with the array type as it is, we'll eventually
10386 crash in tree_to_[su]hwi(). */
10387 type = error_mark_node;
10390 *ptype = type;
10391 return failure;
10394 /* Like c_mark_addressable but don't check register qualifier. */
10395 void
10396 c_common_mark_addressable_vec (tree t)
10398 while (handled_component_p (t))
10399 t = TREE_OPERAND (t, 0);
10400 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10401 return;
10402 TREE_ADDRESSABLE (t) = 1;
10407 /* Used to help initialize the builtin-types.def table. When a type of
10408 the correct size doesn't exist, use error_mark_node instead of NULL.
10409 The later results in segfaults even when a decl using the type doesn't
10410 get invoked. */
10412 tree
10413 builtin_type_for_size (int size, bool unsignedp)
10415 tree type = c_common_type_for_size (size, unsignedp);
10416 return type ? type : error_mark_node;
10419 /* A helper function for resolve_overloaded_builtin in resolving the
10420 overloaded __sync_ builtins. Returns a positive power of 2 if the
10421 first operand of PARAMS is a pointer to a supported data type.
10422 Returns 0 if an error is encountered. */
10424 static int
10425 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10427 tree type;
10428 int size;
10430 if (!params)
10432 error ("too few arguments to function %qE", function);
10433 return 0;
10436 type = TREE_TYPE ((*params)[0]);
10437 if (TREE_CODE (type) == ARRAY_TYPE)
10439 /* Force array-to-pointer decay for C++. */
10440 gcc_assert (c_dialect_cxx());
10441 (*params)[0] = default_conversion ((*params)[0]);
10442 type = TREE_TYPE ((*params)[0]);
10444 if (TREE_CODE (type) != POINTER_TYPE)
10445 goto incompatible;
10447 type = TREE_TYPE (type);
10448 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10449 goto incompatible;
10451 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10452 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10453 return size;
10455 incompatible:
10456 error ("incompatible type for argument %d of %qE", 1, function);
10457 return 0;
10460 /* A helper function for resolve_overloaded_builtin. Adds casts to
10461 PARAMS to make arguments match up with those of FUNCTION. Drops
10462 the variadic arguments at the end. Returns false if some error
10463 was encountered; true on success. */
10465 static bool
10466 sync_resolve_params (location_t loc, tree orig_function, tree function,
10467 vec<tree, va_gc> *params, bool orig_format)
10469 function_args_iterator iter;
10470 tree ptype;
10471 unsigned int parmnum;
10473 function_args_iter_init (&iter, TREE_TYPE (function));
10474 /* We've declared the implementation functions to use "volatile void *"
10475 as the pointer parameter, so we shouldn't get any complaints from the
10476 call to check_function_arguments what ever type the user used. */
10477 function_args_iter_next (&iter);
10478 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10479 ptype = TYPE_MAIN_VARIANT (ptype);
10481 /* For the rest of the values, we need to cast these to FTYPE, so that we
10482 don't get warnings for passing pointer types, etc. */
10483 parmnum = 0;
10484 while (1)
10486 tree val, arg_type;
10488 arg_type = function_args_iter_cond (&iter);
10489 /* XXX void_type_node belies the abstraction. */
10490 if (arg_type == void_type_node)
10491 break;
10493 ++parmnum;
10494 if (params->length () <= parmnum)
10496 error_at (loc, "too few arguments to function %qE", orig_function);
10497 return false;
10500 /* Only convert parameters if arg_type is unsigned integer type with
10501 new format sync routines, i.e. don't attempt to convert pointer
10502 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10503 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10504 kinds). */
10505 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10507 /* Ideally for the first conversion we'd use convert_for_assignment
10508 so that we get warnings for anything that doesn't match the pointer
10509 type. This isn't portable across the C and C++ front ends atm. */
10510 val = (*params)[parmnum];
10511 val = convert (ptype, val);
10512 val = convert (arg_type, val);
10513 (*params)[parmnum] = val;
10516 function_args_iter_next (&iter);
10519 /* __atomic routines are not variadic. */
10520 if (!orig_format && params->length () != parmnum + 1)
10522 error_at (loc, "too many arguments to function %qE", orig_function);
10523 return false;
10526 /* The definition of these primitives is variadic, with the remaining
10527 being "an optional list of variables protected by the memory barrier".
10528 No clue what that's supposed to mean, precisely, but we consider all
10529 call-clobbered variables to be protected so we're safe. */
10530 params->truncate (parmnum + 1);
10532 return true;
10535 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10536 RESULT to make it match the type of the first pointer argument in
10537 PARAMS. */
10539 static tree
10540 sync_resolve_return (tree first_param, tree result, bool orig_format)
10542 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10543 tree rtype = TREE_TYPE (result);
10544 ptype = TYPE_MAIN_VARIANT (ptype);
10546 /* New format doesn't require casting unless the types are the same size. */
10547 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10548 return convert (ptype, result);
10549 else
10550 return result;
10553 /* This function verifies the PARAMS to generic atomic FUNCTION.
10554 It returns the size if all the parameters are the same size, otherwise
10555 0 is returned if the parameters are invalid. */
10557 static int
10558 get_atomic_generic_size (location_t loc, tree function,
10559 vec<tree, va_gc> *params)
10561 unsigned int n_param;
10562 unsigned int n_model;
10563 unsigned int x;
10564 int size_0;
10565 tree type_0;
10567 /* Determine the parameter makeup. */
10568 switch (DECL_FUNCTION_CODE (function))
10570 case BUILT_IN_ATOMIC_EXCHANGE:
10571 n_param = 4;
10572 n_model = 1;
10573 break;
10574 case BUILT_IN_ATOMIC_LOAD:
10575 case BUILT_IN_ATOMIC_STORE:
10576 n_param = 3;
10577 n_model = 1;
10578 break;
10579 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10580 n_param = 6;
10581 n_model = 2;
10582 break;
10583 default:
10584 gcc_unreachable ();
10587 if (vec_safe_length (params) != n_param)
10589 error_at (loc, "incorrect number of arguments to function %qE", function);
10590 return 0;
10593 /* Get type of first parameter, and determine its size. */
10594 type_0 = TREE_TYPE ((*params)[0]);
10595 if (TREE_CODE (type_0) == ARRAY_TYPE)
10597 /* Force array-to-pointer decay for C++. */
10598 gcc_assert (c_dialect_cxx());
10599 (*params)[0] = default_conversion ((*params)[0]);
10600 type_0 = TREE_TYPE ((*params)[0]);
10602 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10604 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10605 function);
10606 return 0;
10609 /* Types must be compile time constant sizes. */
10610 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10612 error_at (loc,
10613 "argument 1 of %qE must be a pointer to a constant size type",
10614 function);
10615 return 0;
10618 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10620 /* Zero size objects are not allowed. */
10621 if (size_0 == 0)
10623 error_at (loc,
10624 "argument 1 of %qE must be a pointer to a nonzero size object",
10625 function);
10626 return 0;
10629 /* Check each other parameter is a pointer and the same size. */
10630 for (x = 0; x < n_param - n_model; x++)
10632 int size;
10633 tree type = TREE_TYPE ((*params)[x]);
10634 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10635 if (n_param == 6 && x == 3)
10636 continue;
10637 if (!POINTER_TYPE_P (type))
10639 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10640 function);
10641 return 0;
10643 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10644 size = type_size ? tree_to_uhwi (type_size) : 0;
10645 if (size != size_0)
10647 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10648 function);
10649 return 0;
10653 /* Check memory model parameters for validity. */
10654 for (x = n_param - n_model ; x < n_param; x++)
10656 tree p = (*params)[x];
10657 if (TREE_CODE (p) == INTEGER_CST)
10659 int i = tree_to_uhwi (p);
10660 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10662 warning_at (loc, OPT_Winvalid_memory_model,
10663 "invalid memory model argument %d of %qE", x + 1,
10664 function);
10667 else
10668 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10670 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10671 function);
10672 return 0;
10676 return size_0;
10680 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10681 at the beginning of the parameter list PARAMS representing the size of the
10682 objects. This is to match the library ABI requirement. LOC is the location
10683 of the function call.
10684 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10685 returned to allow the external call to be constructed. */
10687 static tree
10688 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10689 vec<tree, va_gc> *params)
10691 tree size_node;
10693 /* Insert a SIZE_T parameter as the first param. If there isn't
10694 enough space, allocate a new vector and recursively re-build with that. */
10695 if (!params->space (1))
10697 unsigned int z, len;
10698 vec<tree, va_gc> *v;
10699 tree f;
10701 len = params->length ();
10702 vec_alloc (v, len + 1);
10703 v->quick_push (build_int_cst (size_type_node, n));
10704 for (z = 0; z < len; z++)
10705 v->quick_push ((*params)[z]);
10706 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10707 vec_free (v);
10708 return f;
10711 /* Add the size parameter and leave as a function call for processing. */
10712 size_node = build_int_cst (size_type_node, n);
10713 params->quick_insert (0, size_node);
10714 return NULL_TREE;
10718 /* Return whether atomic operations for naturally aligned N-byte
10719 arguments are supported, whether inline or through libatomic. */
10720 static bool
10721 atomic_size_supported_p (int n)
10723 switch (n)
10725 case 1:
10726 case 2:
10727 case 4:
10728 case 8:
10729 return true;
10731 case 16:
10732 return targetm.scalar_mode_supported_p (TImode);
10734 default:
10735 return false;
10739 /* This will process an __atomic_exchange function call, determine whether it
10740 needs to be mapped to the _N variation, or turned into a library call.
10741 LOC is the location of the builtin call.
10742 FUNCTION is the DECL that has been invoked;
10743 PARAMS is the argument list for the call. The return value is non-null
10744 TRUE is returned if it is translated into the proper format for a call to the
10745 external library, and NEW_RETURN is set the tree for that function.
10746 FALSE is returned if processing for the _N variation is required, and
10747 NEW_RETURN is set to the the return value the result is copied into. */
10748 static bool
10749 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10750 vec<tree, va_gc> *params, tree *new_return)
10752 tree p0, p1, p2, p3;
10753 tree I_type, I_type_ptr;
10754 int n = get_atomic_generic_size (loc, function, params);
10756 /* Size of 0 is an error condition. */
10757 if (n == 0)
10759 *new_return = error_mark_node;
10760 return true;
10763 /* If not a lock-free size, change to the library generic format. */
10764 if (!atomic_size_supported_p (n))
10766 *new_return = add_atomic_size_parameter (n, loc, function, params);
10767 return true;
10770 /* Otherwise there is a lockfree match, transform the call from:
10771 void fn(T* mem, T* desired, T* return, model)
10772 into
10773 *return = (T) (fn (In* mem, (In) *desired, model)) */
10775 p0 = (*params)[0];
10776 p1 = (*params)[1];
10777 p2 = (*params)[2];
10778 p3 = (*params)[3];
10780 /* Create pointer to appropriate size. */
10781 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10782 I_type_ptr = build_pointer_type (I_type);
10784 /* Convert object pointer to required type. */
10785 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10786 (*params)[0] = p0;
10787 /* Convert new value to required type, and dereference it. */
10788 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10789 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10790 (*params)[1] = p1;
10792 /* Move memory model to the 3rd position, and end param list. */
10793 (*params)[2] = p3;
10794 params->truncate (3);
10796 /* Convert return pointer and dereference it for later assignment. */
10797 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10799 return false;
10803 /* This will process an __atomic_compare_exchange function call, determine
10804 whether it needs to be mapped to the _N variation, or turned into a lib call.
10805 LOC is the location of the builtin call.
10806 FUNCTION is the DECL that has been invoked;
10807 PARAMS is the argument list for the call. The return value is non-null
10808 TRUE is returned if it is translated into the proper format for a call to the
10809 external library, and NEW_RETURN is set the tree for that function.
10810 FALSE is returned if processing for the _N variation is required. */
10812 static bool
10813 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10814 vec<tree, va_gc> *params,
10815 tree *new_return)
10817 tree p0, p1, p2;
10818 tree I_type, I_type_ptr;
10819 int n = get_atomic_generic_size (loc, function, params);
10821 /* Size of 0 is an error condition. */
10822 if (n == 0)
10824 *new_return = error_mark_node;
10825 return true;
10828 /* If not a lock-free size, change to the library generic format. */
10829 if (!atomic_size_supported_p (n))
10831 /* The library generic format does not have the weak parameter, so
10832 remove it from the param list. Since a parameter has been removed,
10833 we can be sure that there is room for the SIZE_T parameter, meaning
10834 there will not be a recursive rebuilding of the parameter list, so
10835 there is no danger this will be done twice. */
10836 if (n > 0)
10838 (*params)[3] = (*params)[4];
10839 (*params)[4] = (*params)[5];
10840 params->truncate (5);
10842 *new_return = add_atomic_size_parameter (n, loc, function, params);
10843 return true;
10846 /* Otherwise, there is a match, so the call needs to be transformed from:
10847 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10848 into
10849 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10851 p0 = (*params)[0];
10852 p1 = (*params)[1];
10853 p2 = (*params)[2];
10855 /* Create pointer to appropriate size. */
10856 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10857 I_type_ptr = build_pointer_type (I_type);
10859 /* Convert object pointer to required type. */
10860 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10861 (*params)[0] = p0;
10863 /* Convert expected pointer to required type. */
10864 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10865 (*params)[1] = p1;
10867 /* Convert desired value to required type, and dereference it. */
10868 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10869 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10870 (*params)[2] = p2;
10872 /* The rest of the parameters are fine. NULL means no special return value
10873 processing.*/
10874 *new_return = NULL;
10875 return false;
10879 /* This will process an __atomic_load function call, determine whether it
10880 needs to be mapped to the _N variation, or turned into a library call.
10881 LOC is the location of the builtin call.
10882 FUNCTION is the DECL that has been invoked;
10883 PARAMS is the argument list for the call. The return value is non-null
10884 TRUE is returned if it is translated into the proper format for a call to the
10885 external library, and NEW_RETURN is set the tree for that function.
10886 FALSE is returned if processing for the _N variation is required, and
10887 NEW_RETURN is set to the the return value the result is copied into. */
10889 static bool
10890 resolve_overloaded_atomic_load (location_t loc, tree function,
10891 vec<tree, va_gc> *params, tree *new_return)
10893 tree p0, p1, p2;
10894 tree I_type, I_type_ptr;
10895 int n = get_atomic_generic_size (loc, function, params);
10897 /* Size of 0 is an error condition. */
10898 if (n == 0)
10900 *new_return = error_mark_node;
10901 return true;
10904 /* If not a lock-free size, change to the library generic format. */
10905 if (!atomic_size_supported_p (n))
10907 *new_return = add_atomic_size_parameter (n, loc, function, params);
10908 return true;
10911 /* Otherwise, there is a match, so the call needs to be transformed from:
10912 void fn(T* mem, T* return, model)
10913 into
10914 *return = (T) (fn ((In *) mem, model)) */
10916 p0 = (*params)[0];
10917 p1 = (*params)[1];
10918 p2 = (*params)[2];
10920 /* Create pointer to appropriate size. */
10921 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10922 I_type_ptr = build_pointer_type (I_type);
10924 /* Convert object pointer to required type. */
10925 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10926 (*params)[0] = p0;
10928 /* Move memory model to the 2nd position, and end param list. */
10929 (*params)[1] = p2;
10930 params->truncate (2);
10932 /* Convert return pointer and dereference it for later assignment. */
10933 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10935 return false;
10939 /* This will process an __atomic_store function call, determine whether it
10940 needs to be mapped to the _N variation, or turned into a library call.
10941 LOC is the location of the builtin call.
10942 FUNCTION is the DECL that has been invoked;
10943 PARAMS is the argument list for the call. The return value is non-null
10944 TRUE is returned if it is translated into the proper format for a call to the
10945 external library, and NEW_RETURN is set the tree for that function.
10946 FALSE is returned if processing for the _N variation is required, and
10947 NEW_RETURN is set to the the return value the result is copied into. */
10949 static bool
10950 resolve_overloaded_atomic_store (location_t loc, tree function,
10951 vec<tree, va_gc> *params, tree *new_return)
10953 tree p0, p1;
10954 tree I_type, I_type_ptr;
10955 int n = get_atomic_generic_size (loc, function, params);
10957 /* Size of 0 is an error condition. */
10958 if (n == 0)
10960 *new_return = error_mark_node;
10961 return true;
10964 /* If not a lock-free size, change to the library generic format. */
10965 if (!atomic_size_supported_p (n))
10967 *new_return = add_atomic_size_parameter (n, loc, function, params);
10968 return true;
10971 /* Otherwise, there is a match, so the call needs to be transformed from:
10972 void fn(T* mem, T* value, model)
10973 into
10974 fn ((In *) mem, (In) *value, model) */
10976 p0 = (*params)[0];
10977 p1 = (*params)[1];
10979 /* Create pointer to appropriate size. */
10980 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10981 I_type_ptr = build_pointer_type (I_type);
10983 /* Convert object pointer to required type. */
10984 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10985 (*params)[0] = p0;
10987 /* Convert new value to required type, and dereference it. */
10988 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10989 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10990 (*params)[1] = p1;
10992 /* The memory model is in the right spot already. Return is void. */
10993 *new_return = NULL_TREE;
10995 return false;
10999 /* Some builtin functions are placeholders for other expressions. This
11000 function should be called immediately after parsing the call expression
11001 before surrounding code has committed to the type of the expression.
11003 LOC is the location of the builtin call.
11005 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11006 PARAMS is the argument list for the call. The return value is non-null
11007 when expansion is complete, and null if normal processing should
11008 continue. */
11010 tree
11011 resolve_overloaded_builtin (location_t loc, tree function,
11012 vec<tree, va_gc> *params)
11014 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11015 bool orig_format = true;
11016 tree new_return = NULL_TREE;
11018 switch (DECL_BUILT_IN_CLASS (function))
11020 case BUILT_IN_NORMAL:
11021 break;
11022 case BUILT_IN_MD:
11023 if (targetm.resolve_overloaded_builtin)
11024 return targetm.resolve_overloaded_builtin (loc, function, params);
11025 else
11026 return NULL_TREE;
11027 default:
11028 return NULL_TREE;
11031 /* Handle BUILT_IN_NORMAL here. */
11032 switch (orig_code)
11034 case BUILT_IN_ATOMIC_EXCHANGE:
11035 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11036 case BUILT_IN_ATOMIC_LOAD:
11037 case BUILT_IN_ATOMIC_STORE:
11039 /* Handle these 4 together so that they can fall through to the next
11040 case if the call is transformed to an _N variant. */
11041 switch (orig_code)
11043 case BUILT_IN_ATOMIC_EXCHANGE:
11045 if (resolve_overloaded_atomic_exchange (loc, function, params,
11046 &new_return))
11047 return new_return;
11048 /* Change to the _N variant. */
11049 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11050 break;
11053 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11055 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11056 params,
11057 &new_return))
11058 return new_return;
11059 /* Change to the _N variant. */
11060 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11061 break;
11063 case BUILT_IN_ATOMIC_LOAD:
11065 if (resolve_overloaded_atomic_load (loc, function, params,
11066 &new_return))
11067 return new_return;
11068 /* Change to the _N variant. */
11069 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11070 break;
11072 case BUILT_IN_ATOMIC_STORE:
11074 if (resolve_overloaded_atomic_store (loc, function, params,
11075 &new_return))
11076 return new_return;
11077 /* Change to the _N variant. */
11078 orig_code = BUILT_IN_ATOMIC_STORE_N;
11079 break;
11081 default:
11082 gcc_unreachable ();
11084 /* Fallthrough to the normal processing. */
11086 case BUILT_IN_ATOMIC_EXCHANGE_N:
11087 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11088 case BUILT_IN_ATOMIC_LOAD_N:
11089 case BUILT_IN_ATOMIC_STORE_N:
11090 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11091 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11092 case BUILT_IN_ATOMIC_AND_FETCH_N:
11093 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11094 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11095 case BUILT_IN_ATOMIC_OR_FETCH_N:
11096 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11097 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11098 case BUILT_IN_ATOMIC_FETCH_AND_N:
11099 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11100 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11101 case BUILT_IN_ATOMIC_FETCH_OR_N:
11103 orig_format = false;
11104 /* Fallthru for parameter processing. */
11106 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11107 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11108 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11109 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11110 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11111 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11112 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11113 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11114 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11115 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11116 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11117 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11118 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11119 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11120 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11121 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11123 int n = sync_resolve_size (function, params);
11124 tree new_function, first_param, result;
11125 enum built_in_function fncode;
11127 if (n == 0)
11128 return error_mark_node;
11130 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11131 new_function = builtin_decl_explicit (fncode);
11132 if (!sync_resolve_params (loc, function, new_function, params,
11133 orig_format))
11134 return error_mark_node;
11136 first_param = (*params)[0];
11137 result = build_function_call_vec (loc, vNULL, new_function, params,
11138 NULL);
11139 if (result == error_mark_node)
11140 return result;
11141 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11142 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11143 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11144 result = sync_resolve_return (first_param, result, orig_format);
11146 /* If new_return is set, assign function to that expr and cast the
11147 result to void since the generic interface returned void. */
11148 if (new_return)
11150 /* Cast function result from I{1,2,4,8,16} to the required type. */
11151 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11152 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11153 result);
11154 TREE_SIDE_EFFECTS (result) = 1;
11155 protected_set_expr_location (result, loc);
11156 result = convert (void_type_node, result);
11158 return result;
11161 default:
11162 return NULL_TREE;
11166 /* vector_types_compatible_elements_p is used in type checks of vectors
11167 values used as operands of binary operators. Where it returns true, and
11168 the other checks of the caller succeed (being vector types in he first
11169 place, and matching number of elements), we can just treat the types
11170 as essentially the same.
11171 Contrast with vector_targets_convertible_p, which is used for vector
11172 pointer types, and vector_types_convertible_p, which will allow
11173 language-specific matches under the control of flag_lax_vector_conversions,
11174 and might still require a conversion. */
11175 /* True if vector types T1 and T2 can be inputs to the same binary
11176 operator without conversion.
11177 We don't check the overall vector size here because some of our callers
11178 want to give different error messages when the vectors are compatible
11179 except for the element count. */
11181 bool
11182 vector_types_compatible_elements_p (tree t1, tree t2)
11184 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11185 t1 = TREE_TYPE (t1);
11186 t2 = TREE_TYPE (t2);
11188 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11190 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11191 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11192 || c2 == FIXED_POINT_TYPE));
11194 t1 = c_common_signed_type (t1);
11195 t2 = c_common_signed_type (t2);
11196 /* Equality works here because c_common_signed_type uses
11197 TYPE_MAIN_VARIANT. */
11198 if (t1 == t2)
11199 return true;
11200 if (opaque && c1 == c2
11201 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11202 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11203 return true;
11204 return false;
11207 /* Check for missing format attributes on function pointers. LTYPE is
11208 the new type or left-hand side type. RTYPE is the old type or
11209 right-hand side type. Returns TRUE if LTYPE is missing the desired
11210 attribute. */
11212 bool
11213 check_missing_format_attribute (tree ltype, tree rtype)
11215 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11216 tree ra;
11218 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11219 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11220 break;
11221 if (ra)
11223 tree la;
11224 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11225 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11226 break;
11227 return !la;
11229 else
11230 return false;
11233 /* Subscripting with type char is likely to lose on a machine where
11234 chars are signed. So warn on any machine, but optionally. Don't
11235 warn for unsigned char since that type is safe. Don't warn for
11236 signed char because anyone who uses that must have done so
11237 deliberately. Furthermore, we reduce the false positive load by
11238 warning only for non-constant value of type char. */
11240 void
11241 warn_array_subscript_with_type_char (tree index)
11243 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11244 && TREE_CODE (index) != INTEGER_CST)
11245 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11248 /* Implement -Wparentheses for the unexpected C precedence rules, to
11249 cover cases like x + y << z which readers are likely to
11250 misinterpret. We have seen an expression in which CODE is a binary
11251 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11252 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11253 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11254 expression was not formed using a binary or unary operator, or it
11255 was enclosed in parentheses. */
11257 void
11258 warn_about_parentheses (location_t loc, enum tree_code code,
11259 enum tree_code code_left, tree arg_left,
11260 enum tree_code code_right, tree arg_right)
11262 if (!warn_parentheses)
11263 return;
11265 /* This macro tests that the expression ARG with original tree code
11266 CODE appears to be a boolean expression. or the result of folding a
11267 boolean expression. */
11268 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11269 (truth_value_p (TREE_CODE (ARG)) \
11270 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11271 /* Folding may create 0 or 1 integers from other expressions. */ \
11272 || ((CODE) != INTEGER_CST \
11273 && (integer_onep (ARG) || integer_zerop (ARG))))
11275 switch (code)
11277 case LSHIFT_EXPR:
11278 if (code_left == PLUS_EXPR)
11279 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11280 "suggest parentheses around %<+%> inside %<<<%>");
11281 else if (code_right == PLUS_EXPR)
11282 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11283 "suggest parentheses around %<+%> inside %<<<%>");
11284 else if (code_left == MINUS_EXPR)
11285 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11286 "suggest parentheses around %<-%> inside %<<<%>");
11287 else if (code_right == MINUS_EXPR)
11288 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11289 "suggest parentheses around %<-%> inside %<<<%>");
11290 return;
11292 case RSHIFT_EXPR:
11293 if (code_left == PLUS_EXPR)
11294 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11295 "suggest parentheses around %<+%> inside %<>>%>");
11296 else if (code_right == PLUS_EXPR)
11297 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11298 "suggest parentheses around %<+%> inside %<>>%>");
11299 else if (code_left == MINUS_EXPR)
11300 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11301 "suggest parentheses around %<-%> inside %<>>%>");
11302 else if (code_right == MINUS_EXPR)
11303 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11304 "suggest parentheses around %<-%> inside %<>>%>");
11305 return;
11307 case TRUTH_ORIF_EXPR:
11308 if (code_left == TRUTH_ANDIF_EXPR)
11309 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11310 "suggest parentheses around %<&&%> within %<||%>");
11311 else if (code_right == TRUTH_ANDIF_EXPR)
11312 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11313 "suggest parentheses around %<&&%> within %<||%>");
11314 return;
11316 case BIT_IOR_EXPR:
11317 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11318 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11319 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11320 "suggest parentheses around arithmetic in operand of %<|%>");
11321 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11322 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11323 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11324 "suggest parentheses around arithmetic in operand of %<|%>");
11325 /* Check cases like x|y==z */
11326 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11327 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11328 "suggest parentheses around comparison in operand of %<|%>");
11329 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11330 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11331 "suggest parentheses around comparison in operand of %<|%>");
11332 /* Check cases like !x | y */
11333 else if (code_left == TRUTH_NOT_EXPR
11334 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11335 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11336 "suggest parentheses around operand of "
11337 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11338 return;
11340 case BIT_XOR_EXPR:
11341 if (code_left == BIT_AND_EXPR
11342 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11343 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11344 "suggest parentheses around arithmetic in operand of %<^%>");
11345 else if (code_right == BIT_AND_EXPR
11346 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11347 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11348 "suggest parentheses around arithmetic in operand of %<^%>");
11349 /* Check cases like x^y==z */
11350 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11351 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11352 "suggest parentheses around comparison in operand of %<^%>");
11353 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11354 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11355 "suggest parentheses around comparison in operand of %<^%>");
11356 return;
11358 case BIT_AND_EXPR:
11359 if (code_left == PLUS_EXPR)
11360 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11361 "suggest parentheses around %<+%> in operand of %<&%>");
11362 else if (code_right == PLUS_EXPR)
11363 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11364 "suggest parentheses around %<+%> in operand of %<&%>");
11365 else if (code_left == MINUS_EXPR)
11366 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11367 "suggest parentheses around %<-%> in operand of %<&%>");
11368 else if (code_right == MINUS_EXPR)
11369 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11370 "suggest parentheses around %<-%> in operand of %<&%>");
11371 /* Check cases like x&y==z */
11372 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11373 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11374 "suggest parentheses around comparison in operand of %<&%>");
11375 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11376 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11377 "suggest parentheses around comparison in operand of %<&%>");
11378 /* Check cases like !x & y */
11379 else if (code_left == TRUTH_NOT_EXPR
11380 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11381 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11382 "suggest parentheses around operand of "
11383 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11384 return;
11386 case EQ_EXPR:
11387 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11388 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11389 "suggest parentheses around comparison in operand of %<==%>");
11390 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11391 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11392 "suggest parentheses around comparison in operand of %<==%>");
11393 return;
11394 case NE_EXPR:
11395 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11396 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11397 "suggest parentheses around comparison in operand of %<!=%>");
11398 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11399 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11400 "suggest parentheses around comparison in operand of %<!=%>");
11401 return;
11403 default:
11404 if (TREE_CODE_CLASS (code) == tcc_comparison)
11406 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11407 && code_left != NE_EXPR && code_left != EQ_EXPR
11408 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11409 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11410 "comparisons like %<X<=Y<=Z%> do not "
11411 "have their mathematical meaning");
11412 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11413 && code_right != NE_EXPR && code_right != EQ_EXPR
11414 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11415 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11416 "comparisons like %<X<=Y<=Z%> do not "
11417 "have their mathematical meaning");
11419 return;
11421 #undef NOT_A_BOOLEAN_EXPR_P
11424 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11426 void
11427 warn_for_unused_label (tree label)
11429 if (!TREE_USED (label))
11431 if (DECL_INITIAL (label))
11432 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11433 else
11434 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11438 /* Warn for division by zero according to the value of DIVISOR. LOC
11439 is the location of the division operator. */
11441 void
11442 warn_for_div_by_zero (location_t loc, tree divisor)
11444 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11445 about division by zero. Do not issue a warning if DIVISOR has a
11446 floating-point type, since we consider 0.0/0.0 a valid way of
11447 generating a NaN. */
11448 if (c_inhibit_evaluation_warnings == 0
11449 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11450 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11453 /* Subroutine of build_binary_op. Give warnings for comparisons
11454 between signed and unsigned quantities that may fail. Do the
11455 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11456 so that casts will be considered, but default promotions won't
11459 LOCATION is the location of the comparison operator.
11461 The arguments of this function map directly to local variables
11462 of build_binary_op. */
11464 void
11465 warn_for_sign_compare (location_t location,
11466 tree orig_op0, tree orig_op1,
11467 tree op0, tree op1,
11468 tree result_type, enum tree_code resultcode)
11470 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11471 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11472 int unsignedp0, unsignedp1;
11474 /* In C++, check for comparison of different enum types. */
11475 if (c_dialect_cxx()
11476 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11477 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11478 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11479 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11481 warning_at (location,
11482 OPT_Wsign_compare, "comparison between types %qT and %qT",
11483 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11486 /* Do not warn if the comparison is being done in a signed type,
11487 since the signed type will only be chosen if it can represent
11488 all the values of the unsigned type. */
11489 if (!TYPE_UNSIGNED (result_type))
11490 /* OK */;
11491 /* Do not warn if both operands are unsigned. */
11492 else if (op0_signed == op1_signed)
11493 /* OK */;
11494 else
11496 tree sop, uop, base_type;
11497 bool ovf;
11499 if (op0_signed)
11500 sop = orig_op0, uop = orig_op1;
11501 else
11502 sop = orig_op1, uop = orig_op0;
11504 STRIP_TYPE_NOPS (sop);
11505 STRIP_TYPE_NOPS (uop);
11506 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11507 ? TREE_TYPE (result_type) : result_type);
11509 /* Do not warn if the signed quantity is an unsuffixed integer
11510 literal (or some static constant expression involving such
11511 literals or a conditional expression involving such literals)
11512 and it is non-negative. */
11513 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11514 /* OK */;
11515 /* Do not warn if the comparison is an equality operation, the
11516 unsigned quantity is an integral constant, and it would fit
11517 in the result if the result were signed. */
11518 else if (TREE_CODE (uop) == INTEGER_CST
11519 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11520 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11521 /* OK */;
11522 /* In C, do not warn if the unsigned quantity is an enumeration
11523 constant and its maximum value would fit in the result if the
11524 result were signed. */
11525 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11526 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11527 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11528 c_common_signed_type (base_type)))
11529 /* OK */;
11530 else
11531 warning_at (location,
11532 OPT_Wsign_compare,
11533 "comparison between signed and unsigned integer expressions");
11536 /* Warn if two unsigned values are being compared in a size larger
11537 than their original size, and one (and only one) is the result of
11538 a `~' operator. This comparison will always fail.
11540 Also warn if one operand is a constant, and the constant does not
11541 have all bits set that are set in the ~ operand when it is
11542 extended. */
11544 op0 = c_common_get_narrower (op0, &unsignedp0);
11545 op1 = c_common_get_narrower (op1, &unsignedp1);
11547 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11548 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11550 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11551 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11552 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11553 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11555 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11557 tree primop;
11558 HOST_WIDE_INT constant, mask;
11559 int unsignedp;
11560 unsigned int bits;
11562 if (tree_fits_shwi_p (op0))
11564 primop = op1;
11565 unsignedp = unsignedp1;
11566 constant = tree_to_shwi (op0);
11568 else
11570 primop = op0;
11571 unsignedp = unsignedp0;
11572 constant = tree_to_shwi (op1);
11575 bits = TYPE_PRECISION (TREE_TYPE (primop));
11576 if (bits < TYPE_PRECISION (result_type)
11577 && bits < HOST_BITS_PER_LONG && unsignedp)
11579 mask = (~ (HOST_WIDE_INT) 0) << bits;
11580 if ((mask & constant) != mask)
11582 if (constant == 0)
11583 warning_at (location, OPT_Wsign_compare,
11584 "promoted ~unsigned is always non-zero");
11585 else
11586 warning_at (location, OPT_Wsign_compare,
11587 "comparison of promoted ~unsigned with constant");
11591 else if (unsignedp0 && unsignedp1
11592 && (TYPE_PRECISION (TREE_TYPE (op0))
11593 < TYPE_PRECISION (result_type))
11594 && (TYPE_PRECISION (TREE_TYPE (op1))
11595 < TYPE_PRECISION (result_type)))
11596 warning_at (location, OPT_Wsign_compare,
11597 "comparison of promoted ~unsigned with unsigned");
11601 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11602 type via c_common_type. If -Wdouble-promotion is in use, and the
11603 conditions for warning have been met, issue a warning. GMSGID is
11604 the warning message. It must have two %T specifiers for the type
11605 that was converted (generally "float") and the type to which it was
11606 converted (generally "double), respectively. LOC is the location
11607 to which the awrning should refer. */
11609 void
11610 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11611 const char *gmsgid, location_t loc)
11613 tree source_type;
11615 if (!warn_double_promotion)
11616 return;
11617 /* If the conversion will not occur at run-time, there is no need to
11618 warn about it. */
11619 if (c_inhibit_evaluation_warnings)
11620 return;
11621 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11622 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11623 return;
11624 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11625 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11626 source_type = type1;
11627 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11628 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11629 source_type = type2;
11630 else
11631 return;
11632 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11635 /* Setup a TYPE_DECL node as a typedef representation.
11637 X is a TYPE_DECL for a typedef statement. Create a brand new
11638 ..._TYPE node (which will be just a variant of the existing
11639 ..._TYPE node with identical properties) and then install X
11640 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11642 The whole point here is to end up with a situation where each
11643 and every ..._TYPE node the compiler creates will be uniquely
11644 associated with AT MOST one node representing a typedef name.
11645 This way, even though the compiler substitutes corresponding
11646 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11647 early on, later parts of the compiler can always do the reverse
11648 translation and get back the corresponding typedef name. For
11649 example, given:
11651 typedef struct S MY_TYPE;
11652 MY_TYPE object;
11654 Later parts of the compiler might only know that `object' was of
11655 type `struct S' if it were not for code just below. With this
11656 code however, later parts of the compiler see something like:
11658 struct S' == struct S
11659 typedef struct S' MY_TYPE;
11660 struct S' object;
11662 And they can then deduce (from the node for type struct S') that
11663 the original object declaration was:
11665 MY_TYPE object;
11667 Being able to do this is important for proper support of protoize,
11668 and also for generating precise symbolic debugging information
11669 which takes full account of the programmer's (typedef) vocabulary.
11671 Obviously, we don't want to generate a duplicate ..._TYPE node if
11672 the TYPE_DECL node that we are now processing really represents a
11673 standard built-in type. */
11675 void
11676 set_underlying_type (tree x)
11678 if (x == error_mark_node)
11679 return;
11680 if (DECL_IS_BUILTIN (x))
11682 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11683 TYPE_NAME (TREE_TYPE (x)) = x;
11685 else if (TREE_TYPE (x) != error_mark_node
11686 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11688 tree tt = TREE_TYPE (x);
11689 DECL_ORIGINAL_TYPE (x) = tt;
11690 tt = build_variant_type_copy (tt);
11691 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11692 TYPE_NAME (tt) = x;
11693 TREE_USED (tt) = TREE_USED (x);
11694 TREE_TYPE (x) = tt;
11698 /* Record the types used by the current global variable declaration
11699 being parsed, so that we can decide later to emit their debug info.
11700 Those types are in types_used_by_cur_var_decl, and we are going to
11701 store them in the types_used_by_vars_hash hash table.
11702 DECL is the declaration of the global variable that has been parsed. */
11704 void
11705 record_types_used_by_current_var_decl (tree decl)
11707 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11709 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11711 tree type = types_used_by_cur_var_decl->pop ();
11712 types_used_by_var_decl_insert (type, decl);
11716 /* If DECL is a typedef that is declared in the current function,
11717 record it for the purpose of -Wunused-local-typedefs. */
11719 void
11720 record_locally_defined_typedef (tree decl)
11722 struct c_language_function *l;
11724 if (!warn_unused_local_typedefs
11725 || cfun == NULL
11726 /* if this is not a locally defined typedef then we are not
11727 interested. */
11728 || !is_typedef_decl (decl)
11729 || !decl_function_context (decl))
11730 return;
11732 l = (struct c_language_function *) cfun->language;
11733 vec_safe_push (l->local_typedefs, decl);
11736 /* If T is a TYPE_DECL declared locally, mark it as used. */
11738 void
11739 maybe_record_typedef_use (tree t)
11741 if (!is_typedef_decl (t))
11742 return;
11744 TREE_USED (t) = true;
11747 /* Warn if there are some unused locally defined typedefs in the
11748 current function. */
11750 void
11751 maybe_warn_unused_local_typedefs (void)
11753 int i;
11754 tree decl;
11755 /* The number of times we have emitted -Wunused-local-typedefs
11756 warnings. If this is different from errorcount, that means some
11757 unrelated errors have been issued. In which case, we'll avoid
11758 emitting "unused-local-typedefs" warnings. */
11759 static int unused_local_typedefs_warn_count;
11760 struct c_language_function *l;
11762 if (cfun == NULL)
11763 return;
11765 if ((l = (struct c_language_function *) cfun->language) == NULL)
11766 return;
11768 if (warn_unused_local_typedefs
11769 && errorcount == unused_local_typedefs_warn_count)
11771 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11772 if (!TREE_USED (decl))
11773 warning_at (DECL_SOURCE_LOCATION (decl),
11774 OPT_Wunused_local_typedefs,
11775 "typedef %qD locally defined but not used", decl);
11776 unused_local_typedefs_warn_count = errorcount;
11779 vec_free (l->local_typedefs);
11782 /* Warn about boolean expression compared with an integer value different
11783 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11784 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11785 are the operands of the comparison. The caller must ensure that
11786 either operand is a boolean expression. */
11788 void
11789 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11790 tree op1)
11792 if (TREE_CODE_CLASS (code) != tcc_comparison)
11793 return;
11795 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11796 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11797 if (!cst)
11798 return;
11800 if (!integer_zerop (cst) && !integer_onep (cst))
11802 int sign = (TREE_CODE (op0) == INTEGER_CST)
11803 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11804 if (code == EQ_EXPR
11805 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11806 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11807 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11808 "with boolean expression is always false", cst);
11809 else
11810 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11811 "with boolean expression is always true", cst);
11815 /* The C and C++ parsers both use vectors to hold function arguments.
11816 For efficiency, we keep a cache of unused vectors. This is the
11817 cache. */
11819 typedef vec<tree, va_gc> *tree_gc_vec;
11820 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11822 /* Return a new vector from the cache. If the cache is empty,
11823 allocate a new vector. These vectors are GC'ed, so it is OK if the
11824 pointer is not released.. */
11826 vec<tree, va_gc> *
11827 make_tree_vector (void)
11829 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11830 return tree_vector_cache->pop ();
11831 else
11833 /* Passing 0 to vec::alloc returns NULL, and our callers require
11834 that we always return a non-NULL value. The vector code uses
11835 4 when growing a NULL vector, so we do too. */
11836 vec<tree, va_gc> *v;
11837 vec_alloc (v, 4);
11838 return v;
11842 /* Release a vector of trees back to the cache. */
11844 void
11845 release_tree_vector (vec<tree, va_gc> *vec)
11847 if (vec != NULL)
11849 vec->truncate (0);
11850 vec_safe_push (tree_vector_cache, vec);
11854 /* Get a new tree vector holding a single tree. */
11856 vec<tree, va_gc> *
11857 make_tree_vector_single (tree t)
11859 vec<tree, va_gc> *ret = make_tree_vector ();
11860 ret->quick_push (t);
11861 return ret;
11864 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11866 vec<tree, va_gc> *
11867 make_tree_vector_from_list (tree list)
11869 vec<tree, va_gc> *ret = make_tree_vector ();
11870 for (; list; list = TREE_CHAIN (list))
11871 vec_safe_push (ret, TREE_VALUE (list));
11872 return ret;
11875 /* Get a new tree vector which is a copy of an existing one. */
11877 vec<tree, va_gc> *
11878 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11880 vec<tree, va_gc> *ret;
11881 unsigned int ix;
11882 tree t;
11884 ret = make_tree_vector ();
11885 vec_safe_reserve (ret, vec_safe_length (orig));
11886 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11887 ret->quick_push (t);
11888 return ret;
11891 /* Return true if KEYWORD starts a type specifier. */
11893 bool
11894 keyword_begins_type_specifier (enum rid keyword)
11896 switch (keyword)
11898 case RID_AUTO_TYPE:
11899 case RID_INT:
11900 case RID_CHAR:
11901 case RID_FLOAT:
11902 case RID_DOUBLE:
11903 case RID_VOID:
11904 case RID_UNSIGNED:
11905 case RID_LONG:
11906 case RID_SHORT:
11907 case RID_SIGNED:
11908 case RID_DFLOAT32:
11909 case RID_DFLOAT64:
11910 case RID_DFLOAT128:
11911 case RID_FRACT:
11912 case RID_ACCUM:
11913 case RID_BOOL:
11914 case RID_WCHAR:
11915 case RID_CHAR16:
11916 case RID_CHAR32:
11917 case RID_SAT:
11918 case RID_COMPLEX:
11919 case RID_TYPEOF:
11920 case RID_STRUCT:
11921 case RID_CLASS:
11922 case RID_UNION:
11923 case RID_ENUM:
11924 return true;
11925 default:
11926 if (keyword >= RID_FIRST_INT_N
11927 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
11928 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
11929 return true;
11930 return false;
11934 /* Return true if KEYWORD names a type qualifier. */
11936 bool
11937 keyword_is_type_qualifier (enum rid keyword)
11939 switch (keyword)
11941 case RID_CONST:
11942 case RID_VOLATILE:
11943 case RID_RESTRICT:
11944 case RID_ATOMIC:
11945 return true;
11946 default:
11947 return false;
11951 /* Return true if KEYWORD names a storage class specifier.
11953 RID_TYPEDEF is not included in this list despite `typedef' being
11954 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11955 such for syntactic convenience only. */
11957 bool
11958 keyword_is_storage_class_specifier (enum rid keyword)
11960 switch (keyword)
11962 case RID_STATIC:
11963 case RID_EXTERN:
11964 case RID_REGISTER:
11965 case RID_AUTO:
11966 case RID_MUTABLE:
11967 case RID_THREAD:
11968 return true;
11969 default:
11970 return false;
11974 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11976 static bool
11977 keyword_is_function_specifier (enum rid keyword)
11979 switch (keyword)
11981 case RID_INLINE:
11982 case RID_NORETURN:
11983 case RID_VIRTUAL:
11984 case RID_EXPLICIT:
11985 return true;
11986 default:
11987 return false;
11991 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11992 declaration-specifier (C99 6.7). */
11994 bool
11995 keyword_is_decl_specifier (enum rid keyword)
11997 if (keyword_is_storage_class_specifier (keyword)
11998 || keyword_is_type_qualifier (keyword)
11999 || keyword_is_function_specifier (keyword))
12000 return true;
12002 switch (keyword)
12004 case RID_TYPEDEF:
12005 case RID_FRIEND:
12006 case RID_CONSTEXPR:
12007 return true;
12008 default:
12009 return false;
12013 /* Initialize language-specific-bits of tree_contains_struct. */
12015 void
12016 c_common_init_ts (void)
12018 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12019 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12020 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12023 /* Build a user-defined numeric literal out of an integer constant type VALUE
12024 with identifier SUFFIX. */
12026 tree
12027 build_userdef_literal (tree suffix_id, tree value,
12028 enum overflow_type overflow, tree num_string)
12030 tree literal = make_node (USERDEF_LITERAL);
12031 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12032 USERDEF_LITERAL_VALUE (literal) = value;
12033 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12034 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12035 return literal;
12038 /* For vector[index], convert the vector to a
12039 pointer of the underlying type. Return true if the resulting
12040 ARRAY_REF should not be an lvalue. */
12042 bool
12043 convert_vector_to_pointer_for_subscript (location_t loc,
12044 tree *vecp, tree index)
12046 bool ret = false;
12047 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12049 tree type = TREE_TYPE (*vecp);
12050 tree type1;
12052 ret = !lvalue_p (*vecp);
12053 if (TREE_CODE (index) == INTEGER_CST)
12054 if (!tree_fits_uhwi_p (index)
12055 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12056 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12058 if (ret)
12060 tree tmp = create_tmp_var_raw (type);
12061 DECL_SOURCE_LOCATION (tmp) = loc;
12062 *vecp = c_save_expr (*vecp);
12063 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12065 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12066 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12067 *vecp
12068 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12069 *vecp, NULL_TREE, NULL_TREE),
12070 non_const);
12072 else
12073 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12074 NULL_TREE, NULL_TREE);
12075 SET_EXPR_LOCATION (*vecp, loc);
12076 c_common_mark_addressable_vec (tmp);
12078 else
12079 c_common_mark_addressable_vec (*vecp);
12080 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12081 type1 = build_pointer_type (TREE_TYPE (*vecp));
12082 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12083 if (!ref_all
12084 && !DECL_P (*vecp))
12086 /* If the original vector isn't declared may_alias and it
12087 isn't a bare vector look if the subscripting would
12088 alias the vector we subscript, and if not, force ref-all. */
12089 alias_set_type vecset = get_alias_set (*vecp);
12090 alias_set_type sset = get_alias_set (type);
12091 if (!alias_sets_must_conflict_p (sset, vecset)
12092 && !alias_set_subset_of (sset, vecset))
12093 ref_all = true;
12095 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12096 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12097 *vecp = convert (type, *vecp);
12099 return ret;
12102 /* Determine which of the operands, if any, is a scalar that needs to be
12103 converted to a vector, for the range of operations. */
12104 enum stv_conv
12105 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12106 bool complain)
12108 tree type0 = TREE_TYPE (op0);
12109 tree type1 = TREE_TYPE (op1);
12110 bool integer_only_op = false;
12111 enum stv_conv ret = stv_firstarg;
12113 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12114 || TREE_CODE (type1) == VECTOR_TYPE);
12115 switch (code)
12117 /* Most GENERIC binary expressions require homogeneous arguments.
12118 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12119 argument that is a vector and a second one that is a scalar, so
12120 we never return stv_secondarg for them. */
12121 case RSHIFT_EXPR:
12122 case LSHIFT_EXPR:
12123 if (TREE_CODE (type0) == INTEGER_TYPE
12124 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12126 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12128 if (complain)
12129 error_at (loc, "conversion of scalar %qT to vector %qT "
12130 "involves truncation", type0, type1);
12131 return stv_error;
12133 else
12134 return stv_firstarg;
12136 break;
12138 case BIT_IOR_EXPR:
12139 case BIT_XOR_EXPR:
12140 case BIT_AND_EXPR:
12141 integer_only_op = true;
12142 /* ... fall through ... */
12144 case VEC_COND_EXPR:
12146 case PLUS_EXPR:
12147 case MINUS_EXPR:
12148 case MULT_EXPR:
12149 case TRUNC_DIV_EXPR:
12150 case CEIL_DIV_EXPR:
12151 case FLOOR_DIV_EXPR:
12152 case ROUND_DIV_EXPR:
12153 case EXACT_DIV_EXPR:
12154 case TRUNC_MOD_EXPR:
12155 case FLOOR_MOD_EXPR:
12156 case RDIV_EXPR:
12157 case EQ_EXPR:
12158 case NE_EXPR:
12159 case LE_EXPR:
12160 case GE_EXPR:
12161 case LT_EXPR:
12162 case GT_EXPR:
12163 /* What about UNLT_EXPR? */
12164 if (TREE_CODE (type0) == VECTOR_TYPE)
12166 tree tmp;
12167 ret = stv_secondarg;
12168 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12169 tmp = type0; type0 = type1; type1 = tmp;
12170 tmp = op0; op0 = op1; op1 = tmp;
12173 if (TREE_CODE (type0) == INTEGER_TYPE
12174 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12176 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12178 if (complain)
12179 error_at (loc, "conversion of scalar %qT to vector %qT "
12180 "involves truncation", type0, type1);
12181 return stv_error;
12183 return ret;
12185 else if (!integer_only_op
12186 /* Allow integer --> real conversion if safe. */
12187 && (TREE_CODE (type0) == REAL_TYPE
12188 || TREE_CODE (type0) == INTEGER_TYPE)
12189 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12191 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12193 if (complain)
12194 error_at (loc, "conversion of scalar %qT to vector %qT "
12195 "involves truncation", type0, type1);
12196 return stv_error;
12198 return ret;
12200 default:
12201 break;
12204 return stv_nothing;
12207 /* Return true iff ALIGN is an integral constant that is a fundamental
12208 alignment, as defined by [basic.align] in the c++-11
12209 specifications.
12211 That is:
12213 [A fundamental alignment is represented by an alignment less than or
12214 equal to the greatest alignment supported by the implementation
12215 in all contexts, which is equal to
12216 alignof(max_align_t)]. */
12218 bool
12219 cxx_fundamental_alignment_p (unsigned align)
12221 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12222 TYPE_ALIGN (long_double_type_node)));
12225 /* Return true if T is a pointer to a zero-sized aggregate. */
12227 bool
12228 pointer_to_zero_sized_aggr_p (tree t)
12230 if (!POINTER_TYPE_P (t))
12231 return false;
12232 t = TREE_TYPE (t);
12233 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12236 #include "gt-c-family-c-common.h"