* gcc.dg/store-motion-fgcse-sm.c (dg-final): Cleanup
[official-gcc.git] / gcc / c-family / c-common.c
blobbff8c2a5a9dc42393412a3264f86c6b4d8e02973
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)
7784 declare_weak (*node);
7785 else
7786 warning (OPT_Wattributes, "%qE attribute ignored", name);
7788 return NULL_TREE;
7791 /* Handle an "alias" or "ifunc" attribute; arguments as in
7792 struct attribute_spec.handler, except that IS_ALIAS tells us
7793 whether this is an alias as opposed to ifunc attribute. */
7795 static tree
7796 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7797 bool *no_add_attrs)
7799 tree decl = *node;
7801 if (TREE_CODE (decl) != FUNCTION_DECL
7802 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7804 warning (OPT_Wattributes, "%qE attribute ignored", name);
7805 *no_add_attrs = true;
7807 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7808 || (TREE_CODE (decl) != FUNCTION_DECL
7809 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7810 /* A static variable declaration is always a tentative definition,
7811 but the alias is a non-tentative definition which overrides. */
7812 || (TREE_CODE (decl) != FUNCTION_DECL
7813 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7815 error ("%q+D defined both normally and as %qE attribute", decl, name);
7816 *no_add_attrs = true;
7817 return NULL_TREE;
7819 else if (!is_alias
7820 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7821 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7823 error ("weak %q+D cannot be defined %qE", decl, name);
7824 *no_add_attrs = true;
7825 return NULL_TREE;
7828 /* Note that the very first time we process a nested declaration,
7829 decl_function_context will not be set. Indeed, *would* never
7830 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7831 we do below. After such frobbery, pushdecl would set the context.
7832 In any case, this is never what we want. */
7833 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7835 tree id;
7837 id = TREE_VALUE (args);
7838 if (TREE_CODE (id) != STRING_CST)
7840 error ("attribute %qE argument not a string", name);
7841 *no_add_attrs = true;
7842 return NULL_TREE;
7844 id = get_identifier (TREE_STRING_POINTER (id));
7845 /* This counts as a use of the object pointed to. */
7846 TREE_USED (id) = 1;
7848 if (TREE_CODE (decl) == FUNCTION_DECL)
7849 DECL_INITIAL (decl) = error_mark_node;
7850 else
7851 TREE_STATIC (decl) = 1;
7853 if (!is_alias)
7854 /* ifuncs are also aliases, so set that attribute too. */
7855 DECL_ATTRIBUTES (decl)
7856 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7858 else
7860 warning (OPT_Wattributes, "%qE attribute ignored", name);
7861 *no_add_attrs = true;
7864 if (decl_in_symtab_p (*node))
7866 struct symtab_node *n = symtab_node::get (decl);
7867 if (n && n->refuse_visibility_changes)
7869 if (is_alias)
7870 error ("%+D declared alias after being used", decl);
7871 else
7872 error ("%+D declared ifunc after being used", decl);
7877 return NULL_TREE;
7880 /* Handle an "alias" or "ifunc" attribute; arguments as in
7881 struct attribute_spec.handler. */
7883 static tree
7884 handle_ifunc_attribute (tree *node, tree name, tree args,
7885 int ARG_UNUSED (flags), bool *no_add_attrs)
7887 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7890 /* Handle an "alias" or "ifunc" attribute; arguments as in
7891 struct attribute_spec.handler. */
7893 static tree
7894 handle_alias_attribute (tree *node, tree name, tree args,
7895 int ARG_UNUSED (flags), bool *no_add_attrs)
7897 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7900 /* Handle a "weakref" attribute; arguments as in struct
7901 attribute_spec.handler. */
7903 static tree
7904 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7905 int flags, bool *no_add_attrs)
7907 tree attr = NULL_TREE;
7909 /* We must ignore the attribute when it is associated with
7910 local-scoped decls, since attribute alias is ignored and many
7911 such symbols do not even have a DECL_WEAK field. */
7912 if (decl_function_context (*node)
7913 || current_function_decl
7914 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7916 warning (OPT_Wattributes, "%qE attribute ignored", name);
7917 *no_add_attrs = true;
7918 return NULL_TREE;
7921 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7923 error ("indirect function %q+D cannot be declared weakref", *node);
7924 *no_add_attrs = true;
7925 return NULL_TREE;
7928 /* The idea here is that `weakref("name")' mutates into `weakref,
7929 alias("name")', and weakref without arguments, in turn,
7930 implicitly adds weak. */
7932 if (args)
7934 attr = tree_cons (get_identifier ("alias"), args, attr);
7935 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7937 *no_add_attrs = true;
7939 decl_attributes (node, attr, flags);
7941 else
7943 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7944 error_at (DECL_SOURCE_LOCATION (*node),
7945 "weakref attribute must appear before alias attribute");
7947 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7948 and that isn't supported; and because it wants to add it to
7949 the list of weak decls, which isn't helpful. */
7950 DECL_WEAK (*node) = 1;
7953 if (decl_in_symtab_p (*node))
7955 struct symtab_node *n = symtab_node::get (*node);
7956 if (n && n->refuse_visibility_changes)
7957 error ("%+D declared weakref after being used", *node);
7960 return NULL_TREE;
7963 /* Handle an "visibility" attribute; arguments as in
7964 struct attribute_spec.handler. */
7966 static tree
7967 handle_visibility_attribute (tree *node, tree name, tree args,
7968 int ARG_UNUSED (flags),
7969 bool *ARG_UNUSED (no_add_attrs))
7971 tree decl = *node;
7972 tree id = TREE_VALUE (args);
7973 enum symbol_visibility vis;
7975 if (TYPE_P (*node))
7977 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7978 /* OK */;
7979 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7981 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7982 name);
7983 return NULL_TREE;
7985 else if (TYPE_FIELDS (*node))
7987 error ("%qE attribute ignored because %qT is already defined",
7988 name, *node);
7989 return NULL_TREE;
7992 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7994 warning (OPT_Wattributes, "%qE attribute ignored", name);
7995 return NULL_TREE;
7998 if (TREE_CODE (id) != STRING_CST)
8000 error ("visibility argument not a string");
8001 return NULL_TREE;
8004 /* If this is a type, set the visibility on the type decl. */
8005 if (TYPE_P (decl))
8007 decl = TYPE_NAME (decl);
8008 if (!decl)
8009 return NULL_TREE;
8010 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8012 warning (OPT_Wattributes, "%qE attribute ignored on types",
8013 name);
8014 return NULL_TREE;
8018 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8019 vis = VISIBILITY_DEFAULT;
8020 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8021 vis = VISIBILITY_INTERNAL;
8022 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8023 vis = VISIBILITY_HIDDEN;
8024 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8025 vis = VISIBILITY_PROTECTED;
8026 else
8028 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8029 vis = VISIBILITY_DEFAULT;
8032 if (DECL_VISIBILITY_SPECIFIED (decl)
8033 && vis != DECL_VISIBILITY (decl))
8035 tree attributes = (TYPE_P (*node)
8036 ? TYPE_ATTRIBUTES (*node)
8037 : DECL_ATTRIBUTES (decl));
8038 if (lookup_attribute ("visibility", attributes))
8039 error ("%qD redeclared with different visibility", decl);
8040 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8041 && lookup_attribute ("dllimport", attributes))
8042 error ("%qD was declared %qs which implies default visibility",
8043 decl, "dllimport");
8044 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8045 && lookup_attribute ("dllexport", attributes))
8046 error ("%qD was declared %qs which implies default visibility",
8047 decl, "dllexport");
8050 DECL_VISIBILITY (decl) = vis;
8051 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8053 /* Go ahead and attach the attribute to the node as well. This is needed
8054 so we can determine whether we have VISIBILITY_DEFAULT because the
8055 visibility was not specified, or because it was explicitly overridden
8056 from the containing scope. */
8058 return NULL_TREE;
8061 /* Determine the ELF symbol visibility for DECL, which is either a
8062 variable or a function. It is an error to use this function if a
8063 definition of DECL is not available in this translation unit.
8064 Returns true if the final visibility has been determined by this
8065 function; false if the caller is free to make additional
8066 modifications. */
8068 bool
8069 c_determine_visibility (tree decl)
8071 gcc_assert (TREE_CODE (decl) == VAR_DECL
8072 || TREE_CODE (decl) == FUNCTION_DECL);
8074 /* If the user explicitly specified the visibility with an
8075 attribute, honor that. DECL_VISIBILITY will have been set during
8076 the processing of the attribute. We check for an explicit
8077 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8078 to distinguish the use of an attribute from the use of a "#pragma
8079 GCC visibility push(...)"; in the latter case we still want other
8080 considerations to be able to overrule the #pragma. */
8081 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8082 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8083 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8084 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8085 return true;
8087 /* Set default visibility to whatever the user supplied with
8088 visibility_specified depending on #pragma GCC visibility. */
8089 if (!DECL_VISIBILITY_SPECIFIED (decl))
8091 if (visibility_options.inpragma
8092 || DECL_VISIBILITY (decl) != default_visibility)
8094 DECL_VISIBILITY (decl) = default_visibility;
8095 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8096 /* If visibility changed and DECL already has DECL_RTL, ensure
8097 symbol flags are updated. */
8098 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8099 || TREE_CODE (decl) == FUNCTION_DECL)
8100 && DECL_RTL_SET_P (decl))
8101 make_decl_rtl (decl);
8104 return false;
8107 /* Handle an "tls_model" attribute; arguments as in
8108 struct attribute_spec.handler. */
8110 static tree
8111 handle_tls_model_attribute (tree *node, tree name, tree args,
8112 int ARG_UNUSED (flags), bool *no_add_attrs)
8114 tree id;
8115 tree decl = *node;
8116 enum tls_model kind;
8118 *no_add_attrs = true;
8120 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8122 warning (OPT_Wattributes, "%qE attribute ignored", name);
8123 return NULL_TREE;
8126 kind = DECL_TLS_MODEL (decl);
8127 id = TREE_VALUE (args);
8128 if (TREE_CODE (id) != STRING_CST)
8130 error ("tls_model argument not a string");
8131 return NULL_TREE;
8134 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8135 kind = TLS_MODEL_LOCAL_EXEC;
8136 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8137 kind = TLS_MODEL_INITIAL_EXEC;
8138 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8139 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8140 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8141 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8142 else
8143 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8145 set_decl_tls_model (decl, kind);
8146 return NULL_TREE;
8149 /* Handle a "no_instrument_function" attribute; arguments as in
8150 struct attribute_spec.handler. */
8152 static tree
8153 handle_no_instrument_function_attribute (tree *node, tree name,
8154 tree ARG_UNUSED (args),
8155 int ARG_UNUSED (flags),
8156 bool *no_add_attrs)
8158 tree decl = *node;
8160 if (TREE_CODE (decl) != FUNCTION_DECL)
8162 error_at (DECL_SOURCE_LOCATION (decl),
8163 "%qE attribute applies only to functions", name);
8164 *no_add_attrs = true;
8166 else
8167 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8169 return NULL_TREE;
8172 /* Handle a "malloc" attribute; arguments as in
8173 struct attribute_spec.handler. */
8175 static tree
8176 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8177 int ARG_UNUSED (flags), bool *no_add_attrs)
8179 if (TREE_CODE (*node) == FUNCTION_DECL
8180 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8181 DECL_IS_MALLOC (*node) = 1;
8182 else
8184 warning (OPT_Wattributes, "%qE attribute ignored", name);
8185 *no_add_attrs = true;
8188 return NULL_TREE;
8191 /* Handle a "alloc_size" attribute; arguments as in
8192 struct attribute_spec.handler. */
8194 static tree
8195 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8196 int ARG_UNUSED (flags), bool *no_add_attrs)
8198 unsigned arg_count = type_num_arguments (*node);
8199 for (; args; args = TREE_CHAIN (args))
8201 tree position = TREE_VALUE (args);
8202 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8203 && TREE_CODE (position) != FUNCTION_DECL)
8204 position = default_conversion (position);
8206 if (!tree_fits_uhwi_p (position)
8207 || !arg_count
8208 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8210 warning (OPT_Wattributes,
8211 "alloc_size parameter outside range");
8212 *no_add_attrs = true;
8213 return NULL_TREE;
8216 return NULL_TREE;
8219 /* Handle a "alloc_align" attribute; arguments as in
8220 struct attribute_spec.handler. */
8222 static tree
8223 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8224 bool *no_add_attrs)
8226 unsigned arg_count = type_num_arguments (*node);
8227 tree position = TREE_VALUE (args);
8228 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8229 position = default_conversion (position);
8231 if (!tree_fits_uhwi_p (position)
8232 || !arg_count
8233 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8235 warning (OPT_Wattributes,
8236 "alloc_align parameter outside range");
8237 *no_add_attrs = true;
8238 return NULL_TREE;
8240 return NULL_TREE;
8243 /* Handle a "assume_aligned" attribute; arguments as in
8244 struct attribute_spec.handler. */
8246 static tree
8247 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8248 bool *no_add_attrs)
8250 for (; args; args = TREE_CHAIN (args))
8252 tree position = TREE_VALUE (args);
8253 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8254 && TREE_CODE (position) != FUNCTION_DECL)
8255 position = default_conversion (position);
8257 if (TREE_CODE (position) != INTEGER_CST)
8259 warning (OPT_Wattributes,
8260 "assume_aligned parameter not integer constant");
8261 *no_add_attrs = true;
8262 return NULL_TREE;
8265 return NULL_TREE;
8268 /* Handle a "fn spec" attribute; arguments as in
8269 struct attribute_spec.handler. */
8271 static tree
8272 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8273 tree args, int ARG_UNUSED (flags),
8274 bool *no_add_attrs ATTRIBUTE_UNUSED)
8276 gcc_assert (args
8277 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8278 && !TREE_CHAIN (args));
8279 return NULL_TREE;
8282 /* Handle a "bnd_variable_size" attribute; arguments as in
8283 struct attribute_spec.handler. */
8285 static tree
8286 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8287 int ARG_UNUSED (flags), bool *no_add_attrs)
8289 if (TREE_CODE (*node) != FIELD_DECL)
8291 warning (OPT_Wattributes, "%qE attribute ignored", name);
8292 *no_add_attrs = true;
8295 return NULL_TREE;
8298 /* Handle a "bnd_legacy" attribute; arguments as in
8299 struct attribute_spec.handler. */
8301 static tree
8302 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8303 int ARG_UNUSED (flags), bool *no_add_attrs)
8305 if (TREE_CODE (*node) != FUNCTION_DECL)
8307 warning (OPT_Wattributes, "%qE attribute ignored", name);
8308 *no_add_attrs = true;
8311 return NULL_TREE;
8314 /* Handle a "bnd_instrument" attribute; arguments as in
8315 struct attribute_spec.handler. */
8317 static tree
8318 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8319 int ARG_UNUSED (flags), bool *no_add_attrs)
8321 if (TREE_CODE (*node) != FUNCTION_DECL)
8323 warning (OPT_Wattributes, "%qE attribute ignored", name);
8324 *no_add_attrs = true;
8327 return NULL_TREE;
8330 /* Handle a "warn_unused" attribute; arguments as in
8331 struct attribute_spec.handler. */
8333 static tree
8334 handle_warn_unused_attribute (tree *node, tree name,
8335 tree args ATTRIBUTE_UNUSED,
8336 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8338 if (TYPE_P (*node))
8339 /* Do nothing else, just set the attribute. We'll get at
8340 it later with lookup_attribute. */
8342 else
8344 warning (OPT_Wattributes, "%qE attribute ignored", name);
8345 *no_add_attrs = true;
8348 return NULL_TREE;
8351 /* Handle an "omp declare simd" attribute; arguments as in
8352 struct attribute_spec.handler. */
8354 static tree
8355 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8357 return NULL_TREE;
8360 /* Handle an "omp declare target" attribute; arguments as in
8361 struct attribute_spec.handler. */
8363 static tree
8364 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8366 return NULL_TREE;
8369 /* Handle a "returns_twice" attribute; arguments as in
8370 struct attribute_spec.handler. */
8372 static tree
8373 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8374 int ARG_UNUSED (flags), bool *no_add_attrs)
8376 if (TREE_CODE (*node) == FUNCTION_DECL)
8377 DECL_IS_RETURNS_TWICE (*node) = 1;
8378 else
8380 warning (OPT_Wattributes, "%qE attribute ignored", name);
8381 *no_add_attrs = true;
8384 return NULL_TREE;
8387 /* Handle a "no_limit_stack" attribute; arguments as in
8388 struct attribute_spec.handler. */
8390 static tree
8391 handle_no_limit_stack_attribute (tree *node, tree name,
8392 tree ARG_UNUSED (args),
8393 int ARG_UNUSED (flags),
8394 bool *no_add_attrs)
8396 tree decl = *node;
8398 if (TREE_CODE (decl) != FUNCTION_DECL)
8400 error_at (DECL_SOURCE_LOCATION (decl),
8401 "%qE attribute applies only to functions", name);
8402 *no_add_attrs = true;
8404 else if (DECL_INITIAL (decl))
8406 error_at (DECL_SOURCE_LOCATION (decl),
8407 "can%'t set %qE attribute after definition", name);
8408 *no_add_attrs = true;
8410 else
8411 DECL_NO_LIMIT_STACK (decl) = 1;
8413 return NULL_TREE;
8416 /* Handle a "pure" attribute; arguments as in
8417 struct attribute_spec.handler. */
8419 static tree
8420 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8421 int ARG_UNUSED (flags), bool *no_add_attrs)
8423 if (TREE_CODE (*node) == FUNCTION_DECL)
8424 DECL_PURE_P (*node) = 1;
8425 /* ??? TODO: Support types. */
8426 else
8428 warning (OPT_Wattributes, "%qE attribute ignored", name);
8429 *no_add_attrs = true;
8432 return NULL_TREE;
8435 /* Digest an attribute list destined for a transactional memory statement.
8436 ALLOWED is the set of attributes that are allowed for this statement;
8437 return the attribute we parsed. Multiple attributes are never allowed. */
8440 parse_tm_stmt_attr (tree attrs, int allowed)
8442 tree a_seen = NULL;
8443 int m_seen = 0;
8445 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8447 tree a = TREE_PURPOSE (attrs);
8448 int m = 0;
8450 if (is_attribute_p ("outer", a))
8451 m = TM_STMT_ATTR_OUTER;
8453 if ((m & allowed) == 0)
8455 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8456 continue;
8459 if (m_seen == 0)
8461 a_seen = a;
8462 m_seen = m;
8464 else if (m_seen == m)
8465 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8466 else
8467 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8470 return m_seen;
8473 /* Transform a TM attribute name into a maskable integer and back.
8474 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8475 to how the lack of an attribute is treated. */
8478 tm_attr_to_mask (tree attr)
8480 if (attr == NULL)
8481 return 0;
8482 if (is_attribute_p ("transaction_safe", attr))
8483 return TM_ATTR_SAFE;
8484 if (is_attribute_p ("transaction_callable", attr))
8485 return TM_ATTR_CALLABLE;
8486 if (is_attribute_p ("transaction_pure", attr))
8487 return TM_ATTR_PURE;
8488 if (is_attribute_p ("transaction_unsafe", attr))
8489 return TM_ATTR_IRREVOCABLE;
8490 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8491 return TM_ATTR_MAY_CANCEL_OUTER;
8492 return 0;
8495 tree
8496 tm_mask_to_attr (int mask)
8498 const char *str;
8499 switch (mask)
8501 case TM_ATTR_SAFE:
8502 str = "transaction_safe";
8503 break;
8504 case TM_ATTR_CALLABLE:
8505 str = "transaction_callable";
8506 break;
8507 case TM_ATTR_PURE:
8508 str = "transaction_pure";
8509 break;
8510 case TM_ATTR_IRREVOCABLE:
8511 str = "transaction_unsafe";
8512 break;
8513 case TM_ATTR_MAY_CANCEL_OUTER:
8514 str = "transaction_may_cancel_outer";
8515 break;
8516 default:
8517 gcc_unreachable ();
8519 return get_identifier (str);
8522 /* Return the first TM attribute seen in LIST. */
8524 tree
8525 find_tm_attribute (tree list)
8527 for (; list ; list = TREE_CHAIN (list))
8529 tree name = TREE_PURPOSE (list);
8530 if (tm_attr_to_mask (name) != 0)
8531 return name;
8533 return NULL_TREE;
8536 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8537 Here we accept only function types, and verify that none of the other
8538 function TM attributes are also applied. */
8539 /* ??? We need to accept class types for C++, but not C. This greatly
8540 complicates this function, since we can no longer rely on the extra
8541 processing given by function_type_required. */
8543 static tree
8544 handle_tm_attribute (tree *node, tree name, tree args,
8545 int flags, bool *no_add_attrs)
8547 /* Only one path adds the attribute; others don't. */
8548 *no_add_attrs = true;
8550 switch (TREE_CODE (*node))
8552 case RECORD_TYPE:
8553 case UNION_TYPE:
8554 /* Only tm_callable and tm_safe apply to classes. */
8555 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8556 goto ignored;
8557 /* FALLTHRU */
8559 case FUNCTION_TYPE:
8560 case METHOD_TYPE:
8562 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8563 if (old_name == name)
8565 else if (old_name != NULL_TREE)
8566 error ("type was previously declared %qE", old_name);
8567 else
8568 *no_add_attrs = false;
8570 break;
8572 case POINTER_TYPE:
8574 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8575 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8577 tree fn_tmp = TREE_TYPE (*node);
8578 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8579 *node = build_pointer_type (fn_tmp);
8580 break;
8583 /* FALLTHRU */
8585 default:
8586 /* If a function is next, pass it on to be tried next. */
8587 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8588 return tree_cons (name, args, NULL);
8590 ignored:
8591 warning (OPT_Wattributes, "%qE attribute ignored", name);
8592 break;
8595 return NULL_TREE;
8598 /* Handle the TM_WRAP attribute; arguments as in
8599 struct attribute_spec.handler. */
8601 static tree
8602 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8603 int ARG_UNUSED (flags), bool *no_add_attrs)
8605 tree decl = *node;
8607 /* We don't need the attribute even on success, since we
8608 record the entry in an external table. */
8609 *no_add_attrs = true;
8611 if (TREE_CODE (decl) != FUNCTION_DECL)
8612 warning (OPT_Wattributes, "%qE attribute ignored", name);
8613 else
8615 tree wrap_decl = TREE_VALUE (args);
8616 if (error_operand_p (wrap_decl))
8618 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8619 && TREE_CODE (wrap_decl) != VAR_DECL
8620 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8621 error ("%qE argument not an identifier", name);
8622 else
8624 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8625 wrap_decl = lookup_name (wrap_decl);
8626 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8628 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8629 TREE_TYPE (wrap_decl)))
8630 record_tm_replacement (wrap_decl, decl);
8631 else
8632 error ("%qD is not compatible with %qD", wrap_decl, decl);
8634 else
8635 error ("%qE argument is not a function", name);
8639 return NULL_TREE;
8642 /* Ignore the given attribute. Used when this attribute may be usefully
8643 overridden by the target, but is not used generically. */
8645 static tree
8646 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8647 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8648 bool *no_add_attrs)
8650 *no_add_attrs = true;
8651 return NULL_TREE;
8654 /* Handle a "no vops" attribute; arguments as in
8655 struct attribute_spec.handler. */
8657 static tree
8658 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8659 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8660 bool *ARG_UNUSED (no_add_attrs))
8662 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8663 DECL_IS_NOVOPS (*node) = 1;
8664 return NULL_TREE;
8667 /* Handle a "deprecated" attribute; arguments as in
8668 struct attribute_spec.handler. */
8670 static tree
8671 handle_deprecated_attribute (tree *node, tree name,
8672 tree args, int flags,
8673 bool *no_add_attrs)
8675 tree type = NULL_TREE;
8676 int warn = 0;
8677 tree what = NULL_TREE;
8679 if (!args)
8680 *no_add_attrs = true;
8681 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8683 error ("deprecated message is not a string");
8684 *no_add_attrs = true;
8687 if (DECL_P (*node))
8689 tree decl = *node;
8690 type = TREE_TYPE (decl);
8692 if (TREE_CODE (decl) == TYPE_DECL
8693 || TREE_CODE (decl) == PARM_DECL
8694 || TREE_CODE (decl) == VAR_DECL
8695 || TREE_CODE (decl) == FUNCTION_DECL
8696 || TREE_CODE (decl) == FIELD_DECL
8697 || objc_method_decl (TREE_CODE (decl)))
8698 TREE_DEPRECATED (decl) = 1;
8699 else
8700 warn = 1;
8702 else if (TYPE_P (*node))
8704 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8705 *node = build_variant_type_copy (*node);
8706 TREE_DEPRECATED (*node) = 1;
8707 type = *node;
8709 else
8710 warn = 1;
8712 if (warn)
8714 *no_add_attrs = true;
8715 if (type && TYPE_NAME (type))
8717 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8718 what = TYPE_NAME (*node);
8719 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8720 && DECL_NAME (TYPE_NAME (type)))
8721 what = DECL_NAME (TYPE_NAME (type));
8723 if (what)
8724 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8725 else
8726 warning (OPT_Wattributes, "%qE attribute ignored", name);
8729 return NULL_TREE;
8732 /* Handle a "vector_size" attribute; arguments as in
8733 struct attribute_spec.handler. */
8735 static tree
8736 handle_vector_size_attribute (tree *node, tree name, tree args,
8737 int ARG_UNUSED (flags),
8738 bool *no_add_attrs)
8740 unsigned HOST_WIDE_INT vecsize, nunits;
8741 machine_mode orig_mode;
8742 tree type = *node, new_type, size;
8744 *no_add_attrs = true;
8746 size = TREE_VALUE (args);
8747 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8748 && TREE_CODE (size) != FUNCTION_DECL)
8749 size = default_conversion (size);
8751 if (!tree_fits_uhwi_p (size))
8753 warning (OPT_Wattributes, "%qE attribute ignored", name);
8754 return NULL_TREE;
8757 /* Get the vector size (in bytes). */
8758 vecsize = tree_to_uhwi (size);
8760 /* We need to provide for vector pointers, vector arrays, and
8761 functions returning vectors. For example:
8763 __attribute__((vector_size(16))) short *foo;
8765 In this case, the mode is SI, but the type being modified is
8766 HI, so we need to look further. */
8768 while (POINTER_TYPE_P (type)
8769 || TREE_CODE (type) == FUNCTION_TYPE
8770 || TREE_CODE (type) == METHOD_TYPE
8771 || TREE_CODE (type) == ARRAY_TYPE
8772 || TREE_CODE (type) == OFFSET_TYPE)
8773 type = TREE_TYPE (type);
8775 /* Get the mode of the type being modified. */
8776 orig_mode = TYPE_MODE (type);
8778 if ((!INTEGRAL_TYPE_P (type)
8779 && !SCALAR_FLOAT_TYPE_P (type)
8780 && !FIXED_POINT_TYPE_P (type))
8781 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8782 && GET_MODE_CLASS (orig_mode) != MODE_INT
8783 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8784 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8785 || TREE_CODE (type) == BOOLEAN_TYPE)
8787 error ("invalid vector type for attribute %qE", name);
8788 return NULL_TREE;
8791 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8793 error ("vector size not an integral multiple of component size");
8794 return NULL;
8797 if (vecsize == 0)
8799 error ("zero vector size");
8800 return NULL;
8803 /* Calculate how many units fit in the vector. */
8804 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8805 if (nunits & (nunits - 1))
8807 error ("number of components of the vector not a power of two");
8808 return NULL_TREE;
8811 new_type = build_vector_type (type, nunits);
8813 /* Build back pointers if needed. */
8814 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8816 return NULL_TREE;
8819 /* Handle the "nonnull" attribute. */
8820 static tree
8821 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8822 tree args, int ARG_UNUSED (flags),
8823 bool *no_add_attrs)
8825 tree type = *node;
8826 unsigned HOST_WIDE_INT attr_arg_num;
8828 /* If no arguments are specified, all pointer arguments should be
8829 non-null. Verify a full prototype is given so that the arguments
8830 will have the correct types when we actually check them later. */
8831 if (!args)
8833 if (!prototype_p (type))
8835 error ("nonnull attribute without arguments on a non-prototype");
8836 *no_add_attrs = true;
8838 return NULL_TREE;
8841 /* Argument list specified. Verify that each argument number references
8842 a pointer argument. */
8843 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8845 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8847 tree arg = TREE_VALUE (args);
8848 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8849 && TREE_CODE (arg) != FUNCTION_DECL)
8850 arg = default_conversion (arg);
8852 if (!get_nonnull_operand (arg, &arg_num))
8854 error ("nonnull argument has invalid operand number (argument %lu)",
8855 (unsigned long) attr_arg_num);
8856 *no_add_attrs = true;
8857 return NULL_TREE;
8860 if (prototype_p (type))
8862 function_args_iterator iter;
8863 tree argument;
8865 function_args_iter_init (&iter, type);
8866 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8868 argument = function_args_iter_cond (&iter);
8869 if (argument == NULL_TREE || ck_num == arg_num)
8870 break;
8873 if (!argument
8874 || TREE_CODE (argument) == VOID_TYPE)
8876 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8877 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8878 *no_add_attrs = true;
8879 return NULL_TREE;
8882 if (TREE_CODE (argument) != POINTER_TYPE)
8884 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8885 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8886 *no_add_attrs = true;
8887 return NULL_TREE;
8892 return NULL_TREE;
8895 /* Check the argument list of a function call for null in argument slots
8896 that are marked as requiring a non-null pointer argument. The NARGS
8897 arguments are passed in the array ARGARRAY.
8900 static void
8901 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8903 tree a;
8904 int i;
8906 attrs = lookup_attribute ("nonnull", attrs);
8907 if (attrs == NULL_TREE)
8908 return;
8910 a = attrs;
8911 /* See if any of the nonnull attributes has no arguments. If so,
8912 then every pointer argument is checked (in which case the check
8913 for pointer type is done in check_nonnull_arg). */
8914 if (TREE_VALUE (a) != NULL_TREE)
8916 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8917 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8919 if (a != NULL_TREE)
8920 for (i = 0; i < nargs; i++)
8921 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8922 i + 1);
8923 else
8925 /* Walk the argument list. If we encounter an argument number we
8926 should check for non-null, do it. */
8927 for (i = 0; i < nargs; i++)
8929 for (a = attrs; ; a = TREE_CHAIN (a))
8931 a = lookup_attribute ("nonnull", a);
8932 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8933 break;
8936 if (a != NULL_TREE)
8937 check_function_arguments_recurse (check_nonnull_arg, NULL,
8938 argarray[i], i + 1);
8943 /* Check that the Nth argument of a function call (counting backwards
8944 from the end) is a (pointer)0. The NARGS arguments are passed in the
8945 array ARGARRAY. */
8947 static void
8948 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8950 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8952 if (attr)
8954 int len = 0;
8955 int pos = 0;
8956 tree sentinel;
8957 function_args_iterator iter;
8958 tree t;
8960 /* Skip over the named arguments. */
8961 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8963 if (len == nargs)
8964 break;
8965 len++;
8968 if (TREE_VALUE (attr))
8970 tree p = TREE_VALUE (TREE_VALUE (attr));
8971 pos = TREE_INT_CST_LOW (p);
8974 /* The sentinel must be one of the varargs, i.e.
8975 in position >= the number of fixed arguments. */
8976 if ((nargs - 1 - pos) < len)
8978 warning (OPT_Wformat_,
8979 "not enough variable arguments to fit a sentinel");
8980 return;
8983 /* Validate the sentinel. */
8984 sentinel = argarray[nargs - 1 - pos];
8985 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8986 || !integer_zerop (sentinel))
8987 /* Although __null (in C++) is only an integer we allow it
8988 nevertheless, as we are guaranteed that it's exactly
8989 as wide as a pointer, and we don't want to force
8990 users to cast the NULL they have written there.
8991 We warn with -Wstrict-null-sentinel, though. */
8992 && (warn_strict_null_sentinel || null_node != sentinel))
8993 warning (OPT_Wformat_, "missing sentinel in function call");
8997 /* Helper for check_function_nonnull; given a list of operands which
8998 must be non-null in ARGS, determine if operand PARAM_NUM should be
8999 checked. */
9001 static bool
9002 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9004 unsigned HOST_WIDE_INT arg_num = 0;
9006 for (; args; args = TREE_CHAIN (args))
9008 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9010 gcc_assert (found);
9012 if (arg_num == param_num)
9013 return true;
9015 return false;
9018 /* Check that the function argument PARAM (which is operand number
9019 PARAM_NUM) is non-null. This is called by check_function_nonnull
9020 via check_function_arguments_recurse. */
9022 static void
9023 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9024 unsigned HOST_WIDE_INT param_num)
9026 /* Just skip checking the argument if it's not a pointer. This can
9027 happen if the "nonnull" attribute was given without an operand
9028 list (which means to check every pointer argument). */
9030 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9031 return;
9033 if (integer_zerop (param))
9034 warning (OPT_Wnonnull, "null argument where non-null required "
9035 "(argument %lu)", (unsigned long) param_num);
9038 /* Helper for nonnull attribute handling; fetch the operand number
9039 from the attribute argument list. */
9041 static bool
9042 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9044 /* Verify the arg number is a small constant. */
9045 if (tree_fits_uhwi_p (arg_num_expr))
9047 *valp = TREE_INT_CST_LOW (arg_num_expr);
9048 return true;
9050 else
9051 return false;
9054 /* Handle a "nothrow" attribute; arguments as in
9055 struct attribute_spec.handler. */
9057 static tree
9058 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9059 int ARG_UNUSED (flags), bool *no_add_attrs)
9061 if (TREE_CODE (*node) == FUNCTION_DECL)
9062 TREE_NOTHROW (*node) = 1;
9063 /* ??? TODO: Support types. */
9064 else
9066 warning (OPT_Wattributes, "%qE attribute ignored", name);
9067 *no_add_attrs = true;
9070 return NULL_TREE;
9073 /* Handle a "cleanup" attribute; arguments as in
9074 struct attribute_spec.handler. */
9076 static tree
9077 handle_cleanup_attribute (tree *node, tree name, tree args,
9078 int ARG_UNUSED (flags), bool *no_add_attrs)
9080 tree decl = *node;
9081 tree cleanup_id, cleanup_decl;
9083 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9084 for global destructors in C++. This requires infrastructure that
9085 we don't have generically at the moment. It's also not a feature
9086 we'd be missing too much, since we do have attribute constructor. */
9087 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9089 warning (OPT_Wattributes, "%qE attribute ignored", name);
9090 *no_add_attrs = true;
9091 return NULL_TREE;
9094 /* Verify that the argument is a function in scope. */
9095 /* ??? We could support pointers to functions here as well, if
9096 that was considered desirable. */
9097 cleanup_id = TREE_VALUE (args);
9098 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9100 error ("cleanup argument not an identifier");
9101 *no_add_attrs = true;
9102 return NULL_TREE;
9104 cleanup_decl = lookup_name (cleanup_id);
9105 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9107 error ("cleanup argument not a function");
9108 *no_add_attrs = true;
9109 return NULL_TREE;
9112 /* That the function has proper type is checked with the
9113 eventual call to build_function_call. */
9115 return NULL_TREE;
9118 /* Handle a "warn_unused_result" attribute. No special handling. */
9120 static tree
9121 handle_warn_unused_result_attribute (tree *node, tree name,
9122 tree ARG_UNUSED (args),
9123 int ARG_UNUSED (flags), bool *no_add_attrs)
9125 /* Ignore the attribute for functions not returning any value. */
9126 if (VOID_TYPE_P (TREE_TYPE (*node)))
9128 warning (OPT_Wattributes, "%qE attribute ignored", name);
9129 *no_add_attrs = true;
9132 return NULL_TREE;
9135 /* Handle a "sentinel" attribute. */
9137 static tree
9138 handle_sentinel_attribute (tree *node, tree name, tree args,
9139 int ARG_UNUSED (flags), bool *no_add_attrs)
9141 if (!prototype_p (*node))
9143 warning (OPT_Wattributes,
9144 "%qE attribute requires prototypes with named arguments", name);
9145 *no_add_attrs = true;
9147 else
9149 if (!stdarg_p (*node))
9151 warning (OPT_Wattributes,
9152 "%qE attribute only applies to variadic functions", name);
9153 *no_add_attrs = true;
9157 if (args)
9159 tree position = TREE_VALUE (args);
9160 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9161 && TREE_CODE (position) != FUNCTION_DECL)
9162 position = default_conversion (position);
9164 if (TREE_CODE (position) != INTEGER_CST
9165 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9167 warning (OPT_Wattributes,
9168 "requested position is not an integer constant");
9169 *no_add_attrs = true;
9171 else
9173 if (tree_int_cst_lt (position, integer_zero_node))
9175 warning (OPT_Wattributes,
9176 "requested position is less than zero");
9177 *no_add_attrs = true;
9182 return NULL_TREE;
9185 /* Handle a "type_generic" attribute. */
9187 static tree
9188 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9189 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9190 bool * ARG_UNUSED (no_add_attrs))
9192 /* Ensure we have a function type. */
9193 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9195 /* Ensure we have a variadic function. */
9196 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9198 return NULL_TREE;
9201 /* Handle a "target" attribute. */
9203 static tree
9204 handle_target_attribute (tree *node, tree name, tree args, int flags,
9205 bool *no_add_attrs)
9207 /* Ensure we have a function type. */
9208 if (TREE_CODE (*node) != FUNCTION_DECL)
9210 warning (OPT_Wattributes, "%qE attribute ignored", name);
9211 *no_add_attrs = true;
9213 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9214 flags))
9215 *no_add_attrs = true;
9217 return NULL_TREE;
9220 /* Arguments being collected for optimization. */
9221 typedef const char *const_char_p; /* For DEF_VEC_P. */
9222 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9225 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9226 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9227 false for #pragma GCC optimize. */
9229 bool
9230 parse_optimize_options (tree args, bool attr_p)
9232 bool ret = true;
9233 unsigned opt_argc;
9234 unsigned i;
9235 int saved_flag_strict_aliasing;
9236 const char **opt_argv;
9237 struct cl_decoded_option *decoded_options;
9238 unsigned int decoded_options_count;
9239 tree ap;
9241 /* Build up argv vector. Just in case the string is stored away, use garbage
9242 collected strings. */
9243 vec_safe_truncate (optimize_args, 0);
9244 vec_safe_push (optimize_args, (const char *) NULL);
9246 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9248 tree value = TREE_VALUE (ap);
9250 if (TREE_CODE (value) == INTEGER_CST)
9252 char buffer[20];
9253 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9254 vec_safe_push (optimize_args, ggc_strdup (buffer));
9257 else if (TREE_CODE (value) == STRING_CST)
9259 /* Split string into multiple substrings. */
9260 size_t len = TREE_STRING_LENGTH (value);
9261 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9262 char *end = p + len;
9263 char *comma;
9264 char *next_p = p;
9266 while (next_p != NULL)
9268 size_t len2;
9269 char *q, *r;
9271 p = next_p;
9272 comma = strchr (p, ',');
9273 if (comma)
9275 len2 = comma - p;
9276 *comma = '\0';
9277 next_p = comma+1;
9279 else
9281 len2 = end - p;
9282 next_p = NULL;
9285 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9287 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9288 options. */
9289 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9291 ret = false;
9292 if (attr_p)
9293 warning (OPT_Wattributes,
9294 "bad option %s to optimize attribute", p);
9295 else
9296 warning (OPT_Wpragmas,
9297 "bad option %s to pragma attribute", p);
9298 continue;
9301 if (*p != '-')
9303 *r++ = '-';
9305 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9306 itself is -Os, and any other switch begins with a -f. */
9307 if ((*p >= '0' && *p <= '9')
9308 || (p[0] == 's' && p[1] == '\0'))
9309 *r++ = 'O';
9310 else if (*p != 'O')
9311 *r++ = 'f';
9314 memcpy (r, p, len2);
9315 r[len2] = '\0';
9316 vec_safe_push (optimize_args, (const char *) q);
9322 opt_argc = optimize_args->length ();
9323 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9325 for (i = 1; i < opt_argc; i++)
9326 opt_argv[i] = (*optimize_args)[i];
9328 saved_flag_strict_aliasing = flag_strict_aliasing;
9330 /* Now parse the options. */
9331 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9332 &decoded_options,
9333 &decoded_options_count);
9334 decode_options (&global_options, &global_options_set,
9335 decoded_options, decoded_options_count,
9336 input_location, global_dc);
9338 targetm.override_options_after_change();
9340 /* Don't allow changing -fstrict-aliasing. */
9341 flag_strict_aliasing = saved_flag_strict_aliasing;
9343 optimize_args->truncate (0);
9344 return ret;
9347 /* For handling "optimize" attribute. arguments as in
9348 struct attribute_spec.handler. */
9350 static tree
9351 handle_optimize_attribute (tree *node, tree name, tree args,
9352 int ARG_UNUSED (flags), bool *no_add_attrs)
9354 /* Ensure we have a function type. */
9355 if (TREE_CODE (*node) != FUNCTION_DECL)
9357 warning (OPT_Wattributes, "%qE attribute ignored", name);
9358 *no_add_attrs = true;
9360 else
9362 struct cl_optimization cur_opts;
9363 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9365 /* Save current options. */
9366 cl_optimization_save (&cur_opts, &global_options);
9368 /* If we previously had some optimization options, use them as the
9369 default. */
9370 if (old_opts)
9371 cl_optimization_restore (&global_options,
9372 TREE_OPTIMIZATION (old_opts));
9374 /* Parse options, and update the vector. */
9375 parse_optimize_options (args, true);
9376 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9377 = build_optimization_node (&global_options);
9379 /* Restore current options. */
9380 cl_optimization_restore (&global_options, &cur_opts);
9383 return NULL_TREE;
9386 /* Handle a "no_split_stack" attribute. */
9388 static tree
9389 handle_no_split_stack_attribute (tree *node, tree name,
9390 tree ARG_UNUSED (args),
9391 int ARG_UNUSED (flags),
9392 bool *no_add_attrs)
9394 tree decl = *node;
9396 if (TREE_CODE (decl) != FUNCTION_DECL)
9398 error_at (DECL_SOURCE_LOCATION (decl),
9399 "%qE attribute applies only to functions", name);
9400 *no_add_attrs = true;
9402 else if (DECL_INITIAL (decl))
9404 error_at (DECL_SOURCE_LOCATION (decl),
9405 "can%'t set %qE attribute after definition", name);
9406 *no_add_attrs = true;
9409 return NULL_TREE;
9412 /* Handle a "returns_nonnull" attribute; arguments as in
9413 struct attribute_spec.handler. */
9415 static tree
9416 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9417 bool *no_add_attrs)
9419 // Even without a prototype we still have a return type we can check.
9420 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9422 error ("returns_nonnull attribute on a function not returning a pointer");
9423 *no_add_attrs = true;
9425 return NULL_TREE;
9428 /* Handle a "designated_init" attribute; arguments as in
9429 struct attribute_spec.handler. */
9431 static tree
9432 handle_designated_init_attribute (tree *node, tree name, tree, int,
9433 bool *no_add_attrs)
9435 if (TREE_CODE (*node) != RECORD_TYPE)
9437 error ("%qE attribute is only valid on %<struct%> type", name);
9438 *no_add_attrs = true;
9440 return NULL_TREE;
9444 /* Check for valid arguments being passed to a function with FNTYPE.
9445 There are NARGS arguments in the array ARGARRAY. */
9446 void
9447 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9449 /* Check for null being passed in a pointer argument that must be
9450 non-null. We also need to do this if format checking is enabled. */
9452 if (warn_nonnull)
9453 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9455 /* Check for errors in format strings. */
9457 if (warn_format || warn_suggest_attribute_format)
9458 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9460 if (warn_format)
9461 check_function_sentinel (fntype, nargs, argarray);
9464 /* Generic argument checking recursion routine. PARAM is the argument to
9465 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9466 once the argument is resolved. CTX is context for the callback. */
9467 void
9468 check_function_arguments_recurse (void (*callback)
9469 (void *, tree, unsigned HOST_WIDE_INT),
9470 void *ctx, tree param,
9471 unsigned HOST_WIDE_INT param_num)
9473 if (CONVERT_EXPR_P (param)
9474 && (TYPE_PRECISION (TREE_TYPE (param))
9475 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9477 /* Strip coercion. */
9478 check_function_arguments_recurse (callback, ctx,
9479 TREE_OPERAND (param, 0), param_num);
9480 return;
9483 if (TREE_CODE (param) == CALL_EXPR)
9485 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9486 tree attrs;
9487 bool found_format_arg = false;
9489 /* See if this is a call to a known internationalization function
9490 that modifies a format arg. Such a function may have multiple
9491 format_arg attributes (for example, ngettext). */
9493 for (attrs = TYPE_ATTRIBUTES (type);
9494 attrs;
9495 attrs = TREE_CHAIN (attrs))
9496 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9498 tree inner_arg;
9499 tree format_num_expr;
9500 int format_num;
9501 int i;
9502 call_expr_arg_iterator iter;
9504 /* Extract the argument number, which was previously checked
9505 to be valid. */
9506 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9508 format_num = tree_to_uhwi (format_num_expr);
9510 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9511 inner_arg != 0;
9512 inner_arg = next_call_expr_arg (&iter), i++)
9513 if (i == format_num)
9515 check_function_arguments_recurse (callback, ctx,
9516 inner_arg, param_num);
9517 found_format_arg = true;
9518 break;
9522 /* If we found a format_arg attribute and did a recursive check,
9523 we are done with checking this argument. Otherwise, we continue
9524 and this will be considered a non-literal. */
9525 if (found_format_arg)
9526 return;
9529 if (TREE_CODE (param) == COND_EXPR)
9531 /* Check both halves of the conditional expression. */
9532 check_function_arguments_recurse (callback, ctx,
9533 TREE_OPERAND (param, 1), param_num);
9534 check_function_arguments_recurse (callback, ctx,
9535 TREE_OPERAND (param, 2), param_num);
9536 return;
9539 (*callback) (ctx, param, param_num);
9542 /* Checks for a builtin function FNDECL that the number of arguments
9543 NARGS against the required number REQUIRED and issues an error if
9544 there is a mismatch. Returns true if the number of arguments is
9545 correct, otherwise false. */
9547 static bool
9548 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9550 if (nargs < required)
9552 error_at (input_location,
9553 "not enough arguments to function %qE", fndecl);
9554 return false;
9556 else if (nargs > required)
9558 error_at (input_location,
9559 "too many arguments to function %qE", fndecl);
9560 return false;
9562 return true;
9565 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9566 Returns false if there was an error, otherwise true. */
9568 bool
9569 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9571 if (!DECL_BUILT_IN (fndecl)
9572 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9573 return true;
9575 switch (DECL_FUNCTION_CODE (fndecl))
9577 case BUILT_IN_CONSTANT_P:
9578 return builtin_function_validate_nargs (fndecl, nargs, 1);
9580 case BUILT_IN_ISFINITE:
9581 case BUILT_IN_ISINF:
9582 case BUILT_IN_ISINF_SIGN:
9583 case BUILT_IN_ISNAN:
9584 case BUILT_IN_ISNORMAL:
9585 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9587 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9589 error ("non-floating-point argument in call to "
9590 "function %qE", fndecl);
9591 return false;
9593 return true;
9595 return false;
9597 case BUILT_IN_ISGREATER:
9598 case BUILT_IN_ISGREATEREQUAL:
9599 case BUILT_IN_ISLESS:
9600 case BUILT_IN_ISLESSEQUAL:
9601 case BUILT_IN_ISLESSGREATER:
9602 case BUILT_IN_ISUNORDERED:
9603 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9605 enum tree_code code0, code1;
9606 code0 = TREE_CODE (TREE_TYPE (args[0]));
9607 code1 = TREE_CODE (TREE_TYPE (args[1]));
9608 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9609 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9610 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9612 error ("non-floating-point arguments in call to "
9613 "function %qE", fndecl);
9614 return false;
9616 return true;
9618 return false;
9620 case BUILT_IN_FPCLASSIFY:
9621 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9623 unsigned i;
9625 for (i=0; i<5; i++)
9626 if (TREE_CODE (args[i]) != INTEGER_CST)
9628 error ("non-const integer argument %u in call to function %qE",
9629 i+1, fndecl);
9630 return false;
9633 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9635 error ("non-floating-point argument in call to function %qE",
9636 fndecl);
9637 return false;
9639 return true;
9641 return false;
9643 case BUILT_IN_ASSUME_ALIGNED:
9644 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9646 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9648 error ("non-integer argument 3 in call to function %qE", fndecl);
9649 return false;
9651 return true;
9653 return false;
9655 case BUILT_IN_ADD_OVERFLOW:
9656 case BUILT_IN_SUB_OVERFLOW:
9657 case BUILT_IN_MUL_OVERFLOW:
9658 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9660 unsigned i;
9661 for (i = 0; i < 2; i++)
9662 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9664 error ("argument %u in call to function %qE does not have "
9665 "integral type", i + 1, fndecl);
9666 return false;
9668 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9669 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9671 error ("argument 3 in call to function %qE does not have "
9672 "pointer to integer type", fndecl);
9673 return false;
9675 return true;
9677 return false;
9679 default:
9680 return true;
9684 /* Function to help qsort sort FIELD_DECLs by name order. */
9687 field_decl_cmp (const void *x_p, const void *y_p)
9689 const tree *const x = (const tree *const) x_p;
9690 const tree *const y = (const tree *const) y_p;
9692 if (DECL_NAME (*x) == DECL_NAME (*y))
9693 /* A nontype is "greater" than a type. */
9694 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9695 if (DECL_NAME (*x) == NULL_TREE)
9696 return -1;
9697 if (DECL_NAME (*y) == NULL_TREE)
9698 return 1;
9699 if (DECL_NAME (*x) < DECL_NAME (*y))
9700 return -1;
9701 return 1;
9704 static struct {
9705 gt_pointer_operator new_value;
9706 void *cookie;
9707 } resort_data;
9709 /* This routine compares two fields like field_decl_cmp but using the
9710 pointer operator in resort_data. */
9712 static int
9713 resort_field_decl_cmp (const void *x_p, const void *y_p)
9715 const tree *const x = (const tree *const) x_p;
9716 const tree *const y = (const tree *const) y_p;
9718 if (DECL_NAME (*x) == DECL_NAME (*y))
9719 /* A nontype is "greater" than a type. */
9720 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9721 if (DECL_NAME (*x) == NULL_TREE)
9722 return -1;
9723 if (DECL_NAME (*y) == NULL_TREE)
9724 return 1;
9726 tree d1 = DECL_NAME (*x);
9727 tree d2 = DECL_NAME (*y);
9728 resort_data.new_value (&d1, resort_data.cookie);
9729 resort_data.new_value (&d2, resort_data.cookie);
9730 if (d1 < d2)
9731 return -1;
9733 return 1;
9736 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9738 void
9739 resort_sorted_fields (void *obj,
9740 void * ARG_UNUSED (orig_obj),
9741 gt_pointer_operator new_value,
9742 void *cookie)
9744 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9745 resort_data.new_value = new_value;
9746 resort_data.cookie = cookie;
9747 qsort (&sf->elts[0], sf->len, sizeof (tree),
9748 resort_field_decl_cmp);
9751 /* Subroutine of c_parse_error.
9752 Return the result of concatenating LHS and RHS. RHS is really
9753 a string literal, its first character is indicated by RHS_START and
9754 RHS_SIZE is its length (including the terminating NUL character).
9756 The caller is responsible for deleting the returned pointer. */
9758 static char *
9759 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9761 const int lhs_size = strlen (lhs);
9762 char *result = XNEWVEC (char, lhs_size + rhs_size);
9763 strncpy (result, lhs, lhs_size);
9764 strncpy (result + lhs_size, rhs_start, rhs_size);
9765 return result;
9768 /* Issue the error given by GMSGID, indicating that it occurred before
9769 TOKEN, which had the associated VALUE. */
9771 void
9772 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9773 tree value, unsigned char token_flags)
9775 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9777 char *message = NULL;
9779 if (token_type == CPP_EOF)
9780 message = catenate_messages (gmsgid, " at end of input");
9781 else if (token_type == CPP_CHAR
9782 || token_type == CPP_WCHAR
9783 || token_type == CPP_CHAR16
9784 || token_type == CPP_CHAR32)
9786 unsigned int val = TREE_INT_CST_LOW (value);
9787 const char *prefix;
9789 switch (token_type)
9791 default:
9792 prefix = "";
9793 break;
9794 case CPP_WCHAR:
9795 prefix = "L";
9796 break;
9797 case CPP_CHAR16:
9798 prefix = "u";
9799 break;
9800 case CPP_CHAR32:
9801 prefix = "U";
9802 break;
9805 if (val <= UCHAR_MAX && ISGRAPH (val))
9806 message = catenate_messages (gmsgid, " before %s'%c'");
9807 else
9808 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9810 error (message, prefix, val);
9811 free (message);
9812 message = NULL;
9814 else if (token_type == CPP_CHAR_USERDEF
9815 || token_type == CPP_WCHAR_USERDEF
9816 || token_type == CPP_CHAR16_USERDEF
9817 || token_type == CPP_CHAR32_USERDEF)
9818 message = catenate_messages (gmsgid,
9819 " before user-defined character literal");
9820 else if (token_type == CPP_STRING_USERDEF
9821 || token_type == CPP_WSTRING_USERDEF
9822 || token_type == CPP_STRING16_USERDEF
9823 || token_type == CPP_STRING32_USERDEF
9824 || token_type == CPP_UTF8STRING_USERDEF)
9825 message = catenate_messages (gmsgid, " before user-defined string literal");
9826 else if (token_type == CPP_STRING
9827 || token_type == CPP_WSTRING
9828 || token_type == CPP_STRING16
9829 || token_type == CPP_STRING32
9830 || token_type == CPP_UTF8STRING)
9831 message = catenate_messages (gmsgid, " before string constant");
9832 else if (token_type == CPP_NUMBER)
9833 message = catenate_messages (gmsgid, " before numeric constant");
9834 else if (token_type == CPP_NAME)
9836 message = catenate_messages (gmsgid, " before %qE");
9837 error (message, value);
9838 free (message);
9839 message = NULL;
9841 else if (token_type == CPP_PRAGMA)
9842 message = catenate_messages (gmsgid, " before %<#pragma%>");
9843 else if (token_type == CPP_PRAGMA_EOL)
9844 message = catenate_messages (gmsgid, " before end of line");
9845 else if (token_type == CPP_DECLTYPE)
9846 message = catenate_messages (gmsgid, " before %<decltype%>");
9847 else if (token_type < N_TTYPES)
9849 message = catenate_messages (gmsgid, " before %qs token");
9850 error (message, cpp_type2name (token_type, token_flags));
9851 free (message);
9852 message = NULL;
9854 else
9855 error (gmsgid);
9857 if (message)
9859 error (message);
9860 free (message);
9862 #undef catenate_messages
9865 /* Return the gcc option code associated with the reason for a cpp
9866 message, or 0 if none. */
9868 static int
9869 c_option_controlling_cpp_error (int reason)
9871 const struct cpp_reason_option_codes_t *entry;
9873 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
9875 if (entry->reason == reason)
9876 return entry->option_code;
9878 return 0;
9881 /* Callback from cpp_error for PFILE to print diagnostics from the
9882 preprocessor. The diagnostic is of type LEVEL, with REASON set
9883 to the reason code if LEVEL is represents a warning, at location
9884 LOCATION unless this is after lexing and the compiler's location
9885 should be used instead, with column number possibly overridden by
9886 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9887 the arguments. Returns true if a diagnostic was emitted, false
9888 otherwise. */
9890 bool
9891 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9892 location_t location, unsigned int column_override,
9893 const char *msg, va_list *ap)
9895 diagnostic_info diagnostic;
9896 diagnostic_t dlevel;
9897 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9898 bool ret;
9900 switch (level)
9902 case CPP_DL_WARNING_SYSHDR:
9903 if (flag_no_output)
9904 return false;
9905 global_dc->dc_warn_system_headers = 1;
9906 /* Fall through. */
9907 case CPP_DL_WARNING:
9908 if (flag_no_output)
9909 return false;
9910 dlevel = DK_WARNING;
9911 break;
9912 case CPP_DL_PEDWARN:
9913 if (flag_no_output && !flag_pedantic_errors)
9914 return false;
9915 dlevel = DK_PEDWARN;
9916 break;
9917 case CPP_DL_ERROR:
9918 dlevel = DK_ERROR;
9919 break;
9920 case CPP_DL_ICE:
9921 dlevel = DK_ICE;
9922 break;
9923 case CPP_DL_NOTE:
9924 dlevel = DK_NOTE;
9925 break;
9926 case CPP_DL_FATAL:
9927 dlevel = DK_FATAL;
9928 break;
9929 default:
9930 gcc_unreachable ();
9932 if (done_lexing)
9933 location = input_location;
9934 diagnostic_set_info_translated (&diagnostic, msg, ap,
9935 location, dlevel);
9936 if (column_override)
9937 diagnostic_override_column (&diagnostic, column_override);
9938 diagnostic_override_option_index (&diagnostic,
9939 c_option_controlling_cpp_error (reason));
9940 ret = report_diagnostic (&diagnostic);
9941 if (level == CPP_DL_WARNING_SYSHDR)
9942 global_dc->dc_warn_system_headers = save_warn_system_headers;
9943 return ret;
9946 /* Convert a character from the host to the target execution character
9947 set. cpplib handles this, mostly. */
9949 HOST_WIDE_INT
9950 c_common_to_target_charset (HOST_WIDE_INT c)
9952 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9953 zero-extended under -fno-signed-char. cpplib insists that characters
9954 and character constants are always unsigned. Hence we must convert
9955 back and forth. */
9956 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9958 uc = cpp_host_to_exec_charset (parse_in, uc);
9960 if (flag_signed_char)
9961 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9962 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9963 else
9964 return uc;
9967 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9968 references with an INDIRECT_REF of a constant at the bottom; much like the
9969 traditional rendering of offsetof as a macro. Return the folded result. */
9971 tree
9972 fold_offsetof_1 (tree expr)
9974 tree base, off, t;
9976 switch (TREE_CODE (expr))
9978 case ERROR_MARK:
9979 return expr;
9981 case VAR_DECL:
9982 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9983 return error_mark_node;
9985 case CALL_EXPR:
9986 case TARGET_EXPR:
9987 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9988 return error_mark_node;
9990 case NOP_EXPR:
9991 case INDIRECT_REF:
9992 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9994 error ("cannot apply %<offsetof%> to a non constant address");
9995 return error_mark_node;
9997 return TREE_OPERAND (expr, 0);
9999 case COMPONENT_REF:
10000 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10001 if (base == error_mark_node)
10002 return base;
10004 t = TREE_OPERAND (expr, 1);
10005 if (DECL_C_BIT_FIELD (t))
10007 error ("attempt to take address of bit-field structure "
10008 "member %qD", t);
10009 return error_mark_node;
10011 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10012 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10013 / BITS_PER_UNIT));
10014 break;
10016 case ARRAY_REF:
10017 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10018 if (base == error_mark_node)
10019 return base;
10021 t = TREE_OPERAND (expr, 1);
10023 /* Check if the offset goes beyond the upper bound of the array. */
10024 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10026 tree upbound = array_ref_up_bound (expr);
10027 if (upbound != NULL_TREE
10028 && TREE_CODE (upbound) == INTEGER_CST
10029 && !tree_int_cst_equal (upbound,
10030 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10032 upbound = size_binop (PLUS_EXPR, upbound,
10033 build_int_cst (TREE_TYPE (upbound), 1));
10034 if (tree_int_cst_lt (upbound, t))
10036 tree v;
10038 for (v = TREE_OPERAND (expr, 0);
10039 TREE_CODE (v) == COMPONENT_REF;
10040 v = TREE_OPERAND (v, 0))
10041 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10042 == RECORD_TYPE)
10044 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10045 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10046 if (TREE_CODE (fld_chain) == FIELD_DECL)
10047 break;
10049 if (fld_chain)
10050 break;
10052 /* Don't warn if the array might be considered a poor
10053 man's flexible array member with a very permissive
10054 definition thereof. */
10055 if (TREE_CODE (v) == ARRAY_REF
10056 || TREE_CODE (v) == COMPONENT_REF)
10057 warning (OPT_Warray_bounds,
10058 "index %E denotes an offset "
10059 "greater than size of %qT",
10060 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10065 t = convert (sizetype, t);
10066 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10067 break;
10069 case COMPOUND_EXPR:
10070 /* Handle static members of volatile structs. */
10071 t = TREE_OPERAND (expr, 1);
10072 gcc_assert (TREE_CODE (t) == VAR_DECL);
10073 return fold_offsetof_1 (t);
10075 default:
10076 gcc_unreachable ();
10079 return fold_build_pointer_plus (base, off);
10082 /* Likewise, but convert it to the return type of offsetof. */
10084 tree
10085 fold_offsetof (tree expr)
10087 return convert (size_type_node, fold_offsetof_1 (expr));
10090 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10091 expression, because B will always be true. */
10093 void
10094 warn_for_omitted_condop (location_t location, tree cond)
10096 if (truth_value_p (TREE_CODE (cond)))
10097 warning_at (location, OPT_Wparentheses,
10098 "the omitted middle operand in ?: will always be %<true%>, "
10099 "suggest explicit middle operand");
10102 /* Give an error for storing into ARG, which is 'const'. USE indicates
10103 how ARG was being used. */
10105 void
10106 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10108 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10109 || use == lv_asm);
10110 /* Using this macro rather than (for example) arrays of messages
10111 ensures that all the format strings are checked at compile
10112 time. */
10113 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10114 : (use == lv_increment ? (I) \
10115 : (use == lv_decrement ? (D) : (AS))))
10116 if (TREE_CODE (arg) == COMPONENT_REF)
10118 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10119 error_at (loc, READONLY_MSG (G_("assignment of member "
10120 "%qD in read-only object"),
10121 G_("increment of member "
10122 "%qD in read-only object"),
10123 G_("decrement of member "
10124 "%qD in read-only object"),
10125 G_("member %qD in read-only object "
10126 "used as %<asm%> output")),
10127 TREE_OPERAND (arg, 1));
10128 else
10129 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10130 G_("increment of read-only member %qD"),
10131 G_("decrement of read-only member %qD"),
10132 G_("read-only member %qD used as %<asm%> output")),
10133 TREE_OPERAND (arg, 1));
10135 else if (TREE_CODE (arg) == VAR_DECL)
10136 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10137 G_("increment of read-only variable %qD"),
10138 G_("decrement of read-only variable %qD"),
10139 G_("read-only variable %qD used as %<asm%> output")),
10140 arg);
10141 else if (TREE_CODE (arg) == PARM_DECL)
10142 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10143 G_("increment of read-only parameter %qD"),
10144 G_("decrement of read-only parameter %qD"),
10145 G_("read-only parameter %qD use as %<asm%> output")),
10146 arg);
10147 else if (TREE_CODE (arg) == RESULT_DECL)
10149 gcc_assert (c_dialect_cxx ());
10150 error_at (loc, READONLY_MSG (G_("assignment of "
10151 "read-only named return value %qD"),
10152 G_("increment of "
10153 "read-only named return value %qD"),
10154 G_("decrement of "
10155 "read-only named return value %qD"),
10156 G_("read-only named return value %qD "
10157 "used as %<asm%>output")),
10158 arg);
10160 else if (TREE_CODE (arg) == FUNCTION_DECL)
10161 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10162 G_("increment of function %qD"),
10163 G_("decrement of function %qD"),
10164 G_("function %qD used as %<asm%> output")),
10165 arg);
10166 else
10167 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10168 G_("increment of read-only location %qE"),
10169 G_("decrement of read-only location %qE"),
10170 G_("read-only location %qE used as %<asm%> output")),
10171 arg);
10174 /* Print an error message for an invalid lvalue. USE says
10175 how the lvalue is being used and so selects the error message. LOC
10176 is the location for the error. */
10178 void
10179 lvalue_error (location_t loc, enum lvalue_use use)
10181 switch (use)
10183 case lv_assign:
10184 error_at (loc, "lvalue required as left operand of assignment");
10185 break;
10186 case lv_increment:
10187 error_at (loc, "lvalue required as increment operand");
10188 break;
10189 case lv_decrement:
10190 error_at (loc, "lvalue required as decrement operand");
10191 break;
10192 case lv_addressof:
10193 error_at (loc, "lvalue required as unary %<&%> operand");
10194 break;
10195 case lv_asm:
10196 error_at (loc, "lvalue required in asm statement");
10197 break;
10198 default:
10199 gcc_unreachable ();
10203 /* Print an error message for an invalid indirection of type TYPE.
10204 ERRSTRING is the name of the operator for the indirection. */
10206 void
10207 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10209 switch (errstring)
10211 case RO_NULL:
10212 gcc_assert (c_dialect_cxx ());
10213 error_at (loc, "invalid type argument (have %qT)", type);
10214 break;
10215 case RO_ARRAY_INDEXING:
10216 error_at (loc,
10217 "invalid type argument of array indexing (have %qT)",
10218 type);
10219 break;
10220 case RO_UNARY_STAR:
10221 error_at (loc,
10222 "invalid type argument of unary %<*%> (have %qT)",
10223 type);
10224 break;
10225 case RO_ARROW:
10226 error_at (loc,
10227 "invalid type argument of %<->%> (have %qT)",
10228 type);
10229 break;
10230 case RO_ARROW_STAR:
10231 error_at (loc,
10232 "invalid type argument of %<->*%> (have %qT)",
10233 type);
10234 break;
10235 case RO_IMPLICIT_CONVERSION:
10236 error_at (loc,
10237 "invalid type argument of implicit conversion (have %qT)",
10238 type);
10239 break;
10240 default:
10241 gcc_unreachable ();
10245 /* *PTYPE is an incomplete array. Complete it with a domain based on
10246 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10247 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10248 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10251 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10253 tree maxindex, type, main_type, elt, unqual_elt;
10254 int failure = 0, quals;
10255 hashval_t hashcode = 0;
10256 bool overflow_p = false;
10258 maxindex = size_zero_node;
10259 if (initial_value)
10261 if (TREE_CODE (initial_value) == STRING_CST)
10263 int eltsize
10264 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10265 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10267 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10269 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10271 if (vec_safe_is_empty (v))
10273 if (pedantic)
10274 failure = 3;
10275 maxindex = ssize_int (-1);
10277 else
10279 tree curindex;
10280 unsigned HOST_WIDE_INT cnt;
10281 constructor_elt *ce;
10282 bool fold_p = false;
10284 if ((*v)[0].index)
10285 maxindex = (*v)[0].index, fold_p = true;
10287 curindex = maxindex;
10289 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10291 bool curfold_p = false;
10292 if (ce->index)
10293 curindex = ce->index, curfold_p = true;
10294 else
10296 if (fold_p)
10298 /* Since we treat size types now as ordinary
10299 unsigned types, we need an explicit overflow
10300 check. */
10301 tree orig = curindex;
10302 curindex = fold_convert (sizetype, curindex);
10303 overflow_p |= tree_int_cst_lt (curindex, orig);
10305 curindex = size_binop (PLUS_EXPR, curindex,
10306 size_one_node);
10308 if (tree_int_cst_lt (maxindex, curindex))
10309 maxindex = curindex, fold_p = curfold_p;
10311 if (fold_p)
10313 tree orig = maxindex;
10314 maxindex = fold_convert (sizetype, maxindex);
10315 overflow_p |= tree_int_cst_lt (maxindex, orig);
10319 else
10321 /* Make an error message unless that happened already. */
10322 if (initial_value != error_mark_node)
10323 failure = 1;
10326 else
10328 failure = 2;
10329 if (!do_default)
10330 return failure;
10333 type = *ptype;
10334 elt = TREE_TYPE (type);
10335 quals = TYPE_QUALS (strip_array_types (elt));
10336 if (quals == 0)
10337 unqual_elt = elt;
10338 else
10339 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10341 /* Using build_distinct_type_copy and modifying things afterward instead
10342 of using build_array_type to create a new type preserves all of the
10343 TYPE_LANG_FLAG_? bits that the front end may have set. */
10344 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10345 TREE_TYPE (main_type) = unqual_elt;
10346 TYPE_DOMAIN (main_type)
10347 = build_range_type (TREE_TYPE (maxindex),
10348 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10349 layout_type (main_type);
10351 /* Make sure we have the canonical MAIN_TYPE. */
10352 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10353 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10354 hashcode);
10355 main_type = type_hash_canon (hashcode, main_type);
10357 /* Fix the canonical type. */
10358 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10359 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10360 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10361 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10362 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10363 != TYPE_DOMAIN (main_type)))
10364 TYPE_CANONICAL (main_type)
10365 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10366 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10367 else
10368 TYPE_CANONICAL (main_type) = main_type;
10370 if (quals == 0)
10371 type = main_type;
10372 else
10373 type = c_build_qualified_type (main_type, quals);
10375 if (COMPLETE_TYPE_P (type)
10376 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10377 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10379 error ("size of array is too large");
10380 /* If we proceed with the array type as it is, we'll eventually
10381 crash in tree_to_[su]hwi(). */
10382 type = error_mark_node;
10385 *ptype = type;
10386 return failure;
10389 /* Like c_mark_addressable but don't check register qualifier. */
10390 void
10391 c_common_mark_addressable_vec (tree t)
10393 while (handled_component_p (t))
10394 t = TREE_OPERAND (t, 0);
10395 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10396 return;
10397 TREE_ADDRESSABLE (t) = 1;
10402 /* Used to help initialize the builtin-types.def table. When a type of
10403 the correct size doesn't exist, use error_mark_node instead of NULL.
10404 The later results in segfaults even when a decl using the type doesn't
10405 get invoked. */
10407 tree
10408 builtin_type_for_size (int size, bool unsignedp)
10410 tree type = c_common_type_for_size (size, unsignedp);
10411 return type ? type : error_mark_node;
10414 /* A helper function for resolve_overloaded_builtin in resolving the
10415 overloaded __sync_ builtins. Returns a positive power of 2 if the
10416 first operand of PARAMS is a pointer to a supported data type.
10417 Returns 0 if an error is encountered. */
10419 static int
10420 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10422 tree type;
10423 int size;
10425 if (!params)
10427 error ("too few arguments to function %qE", function);
10428 return 0;
10431 type = TREE_TYPE ((*params)[0]);
10432 if (TREE_CODE (type) == ARRAY_TYPE)
10434 /* Force array-to-pointer decay for C++. */
10435 gcc_assert (c_dialect_cxx());
10436 (*params)[0] = default_conversion ((*params)[0]);
10437 type = TREE_TYPE ((*params)[0]);
10439 if (TREE_CODE (type) != POINTER_TYPE)
10440 goto incompatible;
10442 type = TREE_TYPE (type);
10443 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10444 goto incompatible;
10446 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10447 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10448 return size;
10450 incompatible:
10451 error ("incompatible type for argument %d of %qE", 1, function);
10452 return 0;
10455 /* A helper function for resolve_overloaded_builtin. Adds casts to
10456 PARAMS to make arguments match up with those of FUNCTION. Drops
10457 the variadic arguments at the end. Returns false if some error
10458 was encountered; true on success. */
10460 static bool
10461 sync_resolve_params (location_t loc, tree orig_function, tree function,
10462 vec<tree, va_gc> *params, bool orig_format)
10464 function_args_iterator iter;
10465 tree ptype;
10466 unsigned int parmnum;
10468 function_args_iter_init (&iter, TREE_TYPE (function));
10469 /* We've declared the implementation functions to use "volatile void *"
10470 as the pointer parameter, so we shouldn't get any complaints from the
10471 call to check_function_arguments what ever type the user used. */
10472 function_args_iter_next (&iter);
10473 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10474 ptype = TYPE_MAIN_VARIANT (ptype);
10476 /* For the rest of the values, we need to cast these to FTYPE, so that we
10477 don't get warnings for passing pointer types, etc. */
10478 parmnum = 0;
10479 while (1)
10481 tree val, arg_type;
10483 arg_type = function_args_iter_cond (&iter);
10484 /* XXX void_type_node belies the abstraction. */
10485 if (arg_type == void_type_node)
10486 break;
10488 ++parmnum;
10489 if (params->length () <= parmnum)
10491 error_at (loc, "too few arguments to function %qE", orig_function);
10492 return false;
10495 /* Only convert parameters if arg_type is unsigned integer type with
10496 new format sync routines, i.e. don't attempt to convert pointer
10497 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10498 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10499 kinds). */
10500 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10502 /* Ideally for the first conversion we'd use convert_for_assignment
10503 so that we get warnings for anything that doesn't match the pointer
10504 type. This isn't portable across the C and C++ front ends atm. */
10505 val = (*params)[parmnum];
10506 val = convert (ptype, val);
10507 val = convert (arg_type, val);
10508 (*params)[parmnum] = val;
10511 function_args_iter_next (&iter);
10514 /* __atomic routines are not variadic. */
10515 if (!orig_format && params->length () != parmnum + 1)
10517 error_at (loc, "too many arguments to function %qE", orig_function);
10518 return false;
10521 /* The definition of these primitives is variadic, with the remaining
10522 being "an optional list of variables protected by the memory barrier".
10523 No clue what that's supposed to mean, precisely, but we consider all
10524 call-clobbered variables to be protected so we're safe. */
10525 params->truncate (parmnum + 1);
10527 return true;
10530 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10531 RESULT to make it match the type of the first pointer argument in
10532 PARAMS. */
10534 static tree
10535 sync_resolve_return (tree first_param, tree result, bool orig_format)
10537 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10538 tree rtype = TREE_TYPE (result);
10539 ptype = TYPE_MAIN_VARIANT (ptype);
10541 /* New format doesn't require casting unless the types are the same size. */
10542 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10543 return convert (ptype, result);
10544 else
10545 return result;
10548 /* This function verifies the PARAMS to generic atomic FUNCTION.
10549 It returns the size if all the parameters are the same size, otherwise
10550 0 is returned if the parameters are invalid. */
10552 static int
10553 get_atomic_generic_size (location_t loc, tree function,
10554 vec<tree, va_gc> *params)
10556 unsigned int n_param;
10557 unsigned int n_model;
10558 unsigned int x;
10559 int size_0;
10560 tree type_0;
10562 /* Determine the parameter makeup. */
10563 switch (DECL_FUNCTION_CODE (function))
10565 case BUILT_IN_ATOMIC_EXCHANGE:
10566 n_param = 4;
10567 n_model = 1;
10568 break;
10569 case BUILT_IN_ATOMIC_LOAD:
10570 case BUILT_IN_ATOMIC_STORE:
10571 n_param = 3;
10572 n_model = 1;
10573 break;
10574 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10575 n_param = 6;
10576 n_model = 2;
10577 break;
10578 default:
10579 gcc_unreachable ();
10582 if (vec_safe_length (params) != n_param)
10584 error_at (loc, "incorrect number of arguments to function %qE", function);
10585 return 0;
10588 /* Get type of first parameter, and determine its size. */
10589 type_0 = TREE_TYPE ((*params)[0]);
10590 if (TREE_CODE (type_0) == ARRAY_TYPE)
10592 /* Force array-to-pointer decay for C++. */
10593 gcc_assert (c_dialect_cxx());
10594 (*params)[0] = default_conversion ((*params)[0]);
10595 type_0 = TREE_TYPE ((*params)[0]);
10597 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10599 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10600 function);
10601 return 0;
10604 /* Types must be compile time constant sizes. */
10605 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10607 error_at (loc,
10608 "argument 1 of %qE must be a pointer to a constant size type",
10609 function);
10610 return 0;
10613 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10615 /* Zero size objects are not allowed. */
10616 if (size_0 == 0)
10618 error_at (loc,
10619 "argument 1 of %qE must be a pointer to a nonzero size object",
10620 function);
10621 return 0;
10624 /* Check each other parameter is a pointer and the same size. */
10625 for (x = 0; x < n_param - n_model; x++)
10627 int size;
10628 tree type = TREE_TYPE ((*params)[x]);
10629 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10630 if (n_param == 6 && x == 3)
10631 continue;
10632 if (!POINTER_TYPE_P (type))
10634 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10635 function);
10636 return 0;
10638 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10639 size = type_size ? tree_to_uhwi (type_size) : 0;
10640 if (size != size_0)
10642 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10643 function);
10644 return 0;
10648 /* Check memory model parameters for validity. */
10649 for (x = n_param - n_model ; x < n_param; x++)
10651 tree p = (*params)[x];
10652 if (TREE_CODE (p) == INTEGER_CST)
10654 int i = tree_to_uhwi (p);
10655 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10657 warning_at (loc, OPT_Winvalid_memory_model,
10658 "invalid memory model argument %d of %qE", x + 1,
10659 function);
10662 else
10663 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10665 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10666 function);
10667 return 0;
10671 return size_0;
10675 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10676 at the beginning of the parameter list PARAMS representing the size of the
10677 objects. This is to match the library ABI requirement. LOC is the location
10678 of the function call.
10679 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10680 returned to allow the external call to be constructed. */
10682 static tree
10683 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10684 vec<tree, va_gc> *params)
10686 tree size_node;
10688 /* Insert a SIZE_T parameter as the first param. If there isn't
10689 enough space, allocate a new vector and recursively re-build with that. */
10690 if (!params->space (1))
10692 unsigned int z, len;
10693 vec<tree, va_gc> *v;
10694 tree f;
10696 len = params->length ();
10697 vec_alloc (v, len + 1);
10698 v->quick_push (build_int_cst (size_type_node, n));
10699 for (z = 0; z < len; z++)
10700 v->quick_push ((*params)[z]);
10701 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10702 vec_free (v);
10703 return f;
10706 /* Add the size parameter and leave as a function call for processing. */
10707 size_node = build_int_cst (size_type_node, n);
10708 params->quick_insert (0, size_node);
10709 return NULL_TREE;
10713 /* Return whether atomic operations for naturally aligned N-byte
10714 arguments are supported, whether inline or through libatomic. */
10715 static bool
10716 atomic_size_supported_p (int n)
10718 switch (n)
10720 case 1:
10721 case 2:
10722 case 4:
10723 case 8:
10724 return true;
10726 case 16:
10727 return targetm.scalar_mode_supported_p (TImode);
10729 default:
10730 return false;
10734 /* This will process an __atomic_exchange function call, determine whether it
10735 needs to be mapped to the _N variation, or turned into a library call.
10736 LOC is the location of the builtin call.
10737 FUNCTION is the DECL that has been invoked;
10738 PARAMS is the argument list for the call. The return value is non-null
10739 TRUE is returned if it is translated into the proper format for a call to the
10740 external library, and NEW_RETURN is set the tree for that function.
10741 FALSE is returned if processing for the _N variation is required, and
10742 NEW_RETURN is set to the the return value the result is copied into. */
10743 static bool
10744 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10745 vec<tree, va_gc> *params, tree *new_return)
10747 tree p0, p1, p2, p3;
10748 tree I_type, I_type_ptr;
10749 int n = get_atomic_generic_size (loc, function, params);
10751 /* Size of 0 is an error condition. */
10752 if (n == 0)
10754 *new_return = error_mark_node;
10755 return true;
10758 /* If not a lock-free size, change to the library generic format. */
10759 if (!atomic_size_supported_p (n))
10761 *new_return = add_atomic_size_parameter (n, loc, function, params);
10762 return true;
10765 /* Otherwise there is a lockfree match, transform the call from:
10766 void fn(T* mem, T* desired, T* return, model)
10767 into
10768 *return = (T) (fn (In* mem, (In) *desired, model)) */
10770 p0 = (*params)[0];
10771 p1 = (*params)[1];
10772 p2 = (*params)[2];
10773 p3 = (*params)[3];
10775 /* Create pointer to appropriate size. */
10776 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10777 I_type_ptr = build_pointer_type (I_type);
10779 /* Convert object pointer to required type. */
10780 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10781 (*params)[0] = p0;
10782 /* Convert new value to required type, and dereference it. */
10783 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10784 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10785 (*params)[1] = p1;
10787 /* Move memory model to the 3rd position, and end param list. */
10788 (*params)[2] = p3;
10789 params->truncate (3);
10791 /* Convert return pointer and dereference it for later assignment. */
10792 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10794 return false;
10798 /* This will process an __atomic_compare_exchange function call, determine
10799 whether it needs to be mapped to the _N variation, or turned into a lib call.
10800 LOC is the location of the builtin call.
10801 FUNCTION is the DECL that has been invoked;
10802 PARAMS is the argument list for the call. The return value is non-null
10803 TRUE is returned if it is translated into the proper format for a call to the
10804 external library, and NEW_RETURN is set the tree for that function.
10805 FALSE is returned if processing for the _N variation is required. */
10807 static bool
10808 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10809 vec<tree, va_gc> *params,
10810 tree *new_return)
10812 tree p0, p1, p2;
10813 tree I_type, I_type_ptr;
10814 int n = get_atomic_generic_size (loc, function, params);
10816 /* Size of 0 is an error condition. */
10817 if (n == 0)
10819 *new_return = error_mark_node;
10820 return true;
10823 /* If not a lock-free size, change to the library generic format. */
10824 if (!atomic_size_supported_p (n))
10826 /* The library generic format does not have the weak parameter, so
10827 remove it from the param list. Since a parameter has been removed,
10828 we can be sure that there is room for the SIZE_T parameter, meaning
10829 there will not be a recursive rebuilding of the parameter list, so
10830 there is no danger this will be done twice. */
10831 if (n > 0)
10833 (*params)[3] = (*params)[4];
10834 (*params)[4] = (*params)[5];
10835 params->truncate (5);
10837 *new_return = add_atomic_size_parameter (n, loc, function, params);
10838 return true;
10841 /* Otherwise, there is a match, so the call needs to be transformed from:
10842 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10843 into
10844 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10846 p0 = (*params)[0];
10847 p1 = (*params)[1];
10848 p2 = (*params)[2];
10850 /* Create pointer to appropriate size. */
10851 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10852 I_type_ptr = build_pointer_type (I_type);
10854 /* Convert object pointer to required type. */
10855 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10856 (*params)[0] = p0;
10858 /* Convert expected pointer to required type. */
10859 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10860 (*params)[1] = p1;
10862 /* Convert desired value to required type, and dereference it. */
10863 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10864 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10865 (*params)[2] = p2;
10867 /* The rest of the parameters are fine. NULL means no special return value
10868 processing.*/
10869 *new_return = NULL;
10870 return false;
10874 /* This will process an __atomic_load function call, determine whether it
10875 needs to be mapped to the _N variation, or turned into a library call.
10876 LOC is the location of the builtin call.
10877 FUNCTION is the DECL that has been invoked;
10878 PARAMS is the argument list for the call. The return value is non-null
10879 TRUE is returned if it is translated into the proper format for a call to the
10880 external library, and NEW_RETURN is set the tree for that function.
10881 FALSE is returned if processing for the _N variation is required, and
10882 NEW_RETURN is set to the the return value the result is copied into. */
10884 static bool
10885 resolve_overloaded_atomic_load (location_t loc, tree function,
10886 vec<tree, va_gc> *params, tree *new_return)
10888 tree p0, p1, p2;
10889 tree I_type, I_type_ptr;
10890 int n = get_atomic_generic_size (loc, function, params);
10892 /* Size of 0 is an error condition. */
10893 if (n == 0)
10895 *new_return = error_mark_node;
10896 return true;
10899 /* If not a lock-free size, change to the library generic format. */
10900 if (!atomic_size_supported_p (n))
10902 *new_return = add_atomic_size_parameter (n, loc, function, params);
10903 return true;
10906 /* Otherwise, there is a match, so the call needs to be transformed from:
10907 void fn(T* mem, T* return, model)
10908 into
10909 *return = (T) (fn ((In *) mem, model)) */
10911 p0 = (*params)[0];
10912 p1 = (*params)[1];
10913 p2 = (*params)[2];
10915 /* Create pointer to appropriate size. */
10916 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10917 I_type_ptr = build_pointer_type (I_type);
10919 /* Convert object pointer to required type. */
10920 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10921 (*params)[0] = p0;
10923 /* Move memory model to the 2nd position, and end param list. */
10924 (*params)[1] = p2;
10925 params->truncate (2);
10927 /* Convert return pointer and dereference it for later assignment. */
10928 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10930 return false;
10934 /* This will process an __atomic_store function call, determine whether it
10935 needs to be mapped to the _N variation, or turned into a library call.
10936 LOC is the location of the builtin call.
10937 FUNCTION is the DECL that has been invoked;
10938 PARAMS is the argument list for the call. The return value is non-null
10939 TRUE is returned if it is translated into the proper format for a call to the
10940 external library, and NEW_RETURN is set the tree for that function.
10941 FALSE is returned if processing for the _N variation is required, and
10942 NEW_RETURN is set to the the return value the result is copied into. */
10944 static bool
10945 resolve_overloaded_atomic_store (location_t loc, tree function,
10946 vec<tree, va_gc> *params, tree *new_return)
10948 tree p0, p1;
10949 tree I_type, I_type_ptr;
10950 int n = get_atomic_generic_size (loc, function, params);
10952 /* Size of 0 is an error condition. */
10953 if (n == 0)
10955 *new_return = error_mark_node;
10956 return true;
10959 /* If not a lock-free size, change to the library generic format. */
10960 if (!atomic_size_supported_p (n))
10962 *new_return = add_atomic_size_parameter (n, loc, function, params);
10963 return true;
10966 /* Otherwise, there is a match, so the call needs to be transformed from:
10967 void fn(T* mem, T* value, model)
10968 into
10969 fn ((In *) mem, (In) *value, model) */
10971 p0 = (*params)[0];
10972 p1 = (*params)[1];
10974 /* Create pointer to appropriate size. */
10975 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10976 I_type_ptr = build_pointer_type (I_type);
10978 /* Convert object pointer to required type. */
10979 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10980 (*params)[0] = p0;
10982 /* Convert new value to required type, and dereference it. */
10983 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10984 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10985 (*params)[1] = p1;
10987 /* The memory model is in the right spot already. Return is void. */
10988 *new_return = NULL_TREE;
10990 return false;
10994 /* Some builtin functions are placeholders for other expressions. This
10995 function should be called immediately after parsing the call expression
10996 before surrounding code has committed to the type of the expression.
10998 LOC is the location of the builtin call.
11000 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11001 PARAMS is the argument list for the call. The return value is non-null
11002 when expansion is complete, and null if normal processing should
11003 continue. */
11005 tree
11006 resolve_overloaded_builtin (location_t loc, tree function,
11007 vec<tree, va_gc> *params)
11009 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11010 bool orig_format = true;
11011 tree new_return = NULL_TREE;
11013 switch (DECL_BUILT_IN_CLASS (function))
11015 case BUILT_IN_NORMAL:
11016 break;
11017 case BUILT_IN_MD:
11018 if (targetm.resolve_overloaded_builtin)
11019 return targetm.resolve_overloaded_builtin (loc, function, params);
11020 else
11021 return NULL_TREE;
11022 default:
11023 return NULL_TREE;
11026 /* Handle BUILT_IN_NORMAL here. */
11027 switch (orig_code)
11029 case BUILT_IN_ATOMIC_EXCHANGE:
11030 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11031 case BUILT_IN_ATOMIC_LOAD:
11032 case BUILT_IN_ATOMIC_STORE:
11034 /* Handle these 4 together so that they can fall through to the next
11035 case if the call is transformed to an _N variant. */
11036 switch (orig_code)
11038 case BUILT_IN_ATOMIC_EXCHANGE:
11040 if (resolve_overloaded_atomic_exchange (loc, function, params,
11041 &new_return))
11042 return new_return;
11043 /* Change to the _N variant. */
11044 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11045 break;
11048 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11050 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11051 params,
11052 &new_return))
11053 return new_return;
11054 /* Change to the _N variant. */
11055 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11056 break;
11058 case BUILT_IN_ATOMIC_LOAD:
11060 if (resolve_overloaded_atomic_load (loc, function, params,
11061 &new_return))
11062 return new_return;
11063 /* Change to the _N variant. */
11064 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11065 break;
11067 case BUILT_IN_ATOMIC_STORE:
11069 if (resolve_overloaded_atomic_store (loc, function, params,
11070 &new_return))
11071 return new_return;
11072 /* Change to the _N variant. */
11073 orig_code = BUILT_IN_ATOMIC_STORE_N;
11074 break;
11076 default:
11077 gcc_unreachable ();
11079 /* Fallthrough to the normal processing. */
11081 case BUILT_IN_ATOMIC_EXCHANGE_N:
11082 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11083 case BUILT_IN_ATOMIC_LOAD_N:
11084 case BUILT_IN_ATOMIC_STORE_N:
11085 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11086 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11087 case BUILT_IN_ATOMIC_AND_FETCH_N:
11088 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11089 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11090 case BUILT_IN_ATOMIC_OR_FETCH_N:
11091 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11092 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11093 case BUILT_IN_ATOMIC_FETCH_AND_N:
11094 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11095 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11096 case BUILT_IN_ATOMIC_FETCH_OR_N:
11098 orig_format = false;
11099 /* Fallthru for parameter processing. */
11101 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11102 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11103 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11104 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11105 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11106 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11107 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11108 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11109 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11110 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11111 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11112 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11113 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11114 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11115 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11116 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11118 int n = sync_resolve_size (function, params);
11119 tree new_function, first_param, result;
11120 enum built_in_function fncode;
11122 if (n == 0)
11123 return error_mark_node;
11125 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11126 new_function = builtin_decl_explicit (fncode);
11127 if (!sync_resolve_params (loc, function, new_function, params,
11128 orig_format))
11129 return error_mark_node;
11131 first_param = (*params)[0];
11132 result = build_function_call_vec (loc, vNULL, new_function, params,
11133 NULL);
11134 if (result == error_mark_node)
11135 return result;
11136 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11137 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11138 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11139 result = sync_resolve_return (first_param, result, orig_format);
11141 /* If new_return is set, assign function to that expr and cast the
11142 result to void since the generic interface returned void. */
11143 if (new_return)
11145 /* Cast function result from I{1,2,4,8,16} to the required type. */
11146 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11147 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11148 result);
11149 TREE_SIDE_EFFECTS (result) = 1;
11150 protected_set_expr_location (result, loc);
11151 result = convert (void_type_node, result);
11153 return result;
11156 default:
11157 return NULL_TREE;
11161 /* vector_types_compatible_elements_p is used in type checks of vectors
11162 values used as operands of binary operators. Where it returns true, and
11163 the other checks of the caller succeed (being vector types in he first
11164 place, and matching number of elements), we can just treat the types
11165 as essentially the same.
11166 Contrast with vector_targets_convertible_p, which is used for vector
11167 pointer types, and vector_types_convertible_p, which will allow
11168 language-specific matches under the control of flag_lax_vector_conversions,
11169 and might still require a conversion. */
11170 /* True if vector types T1 and T2 can be inputs to the same binary
11171 operator without conversion.
11172 We don't check the overall vector size here because some of our callers
11173 want to give different error messages when the vectors are compatible
11174 except for the element count. */
11176 bool
11177 vector_types_compatible_elements_p (tree t1, tree t2)
11179 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11180 t1 = TREE_TYPE (t1);
11181 t2 = TREE_TYPE (t2);
11183 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11185 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11186 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11187 || c2 == FIXED_POINT_TYPE));
11189 t1 = c_common_signed_type (t1);
11190 t2 = c_common_signed_type (t2);
11191 /* Equality works here because c_common_signed_type uses
11192 TYPE_MAIN_VARIANT. */
11193 if (t1 == t2)
11194 return true;
11195 if (opaque && c1 == c2
11196 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11197 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11198 return true;
11199 return false;
11202 /* Check for missing format attributes on function pointers. LTYPE is
11203 the new type or left-hand side type. RTYPE is the old type or
11204 right-hand side type. Returns TRUE if LTYPE is missing the desired
11205 attribute. */
11207 bool
11208 check_missing_format_attribute (tree ltype, tree rtype)
11210 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11211 tree ra;
11213 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11214 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11215 break;
11216 if (ra)
11218 tree la;
11219 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11220 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11221 break;
11222 return !la;
11224 else
11225 return false;
11228 /* Subscripting with type char is likely to lose on a machine where
11229 chars are signed. So warn on any machine, but optionally. Don't
11230 warn for unsigned char since that type is safe. Don't warn for
11231 signed char because anyone who uses that must have done so
11232 deliberately. Furthermore, we reduce the false positive load by
11233 warning only for non-constant value of type char. */
11235 void
11236 warn_array_subscript_with_type_char (tree index)
11238 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11239 && TREE_CODE (index) != INTEGER_CST)
11240 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11243 /* Implement -Wparentheses for the unexpected C precedence rules, to
11244 cover cases like x + y << z which readers are likely to
11245 misinterpret. We have seen an expression in which CODE is a binary
11246 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11247 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11248 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11249 expression was not formed using a binary or unary operator, or it
11250 was enclosed in parentheses. */
11252 void
11253 warn_about_parentheses (location_t loc, enum tree_code code,
11254 enum tree_code code_left, tree arg_left,
11255 enum tree_code code_right, tree arg_right)
11257 if (!warn_parentheses)
11258 return;
11260 /* This macro tests that the expression ARG with original tree code
11261 CODE appears to be a boolean expression. or the result of folding a
11262 boolean expression. */
11263 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11264 (truth_value_p (TREE_CODE (ARG)) \
11265 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11266 /* Folding may create 0 or 1 integers from other expressions. */ \
11267 || ((CODE) != INTEGER_CST \
11268 && (integer_onep (ARG) || integer_zerop (ARG))))
11270 switch (code)
11272 case LSHIFT_EXPR:
11273 if (code_left == PLUS_EXPR)
11274 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11275 "suggest parentheses around %<+%> inside %<<<%>");
11276 else if (code_right == PLUS_EXPR)
11277 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11278 "suggest parentheses around %<+%> inside %<<<%>");
11279 else if (code_left == MINUS_EXPR)
11280 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11281 "suggest parentheses around %<-%> inside %<<<%>");
11282 else if (code_right == MINUS_EXPR)
11283 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11284 "suggest parentheses around %<-%> inside %<<<%>");
11285 return;
11287 case RSHIFT_EXPR:
11288 if (code_left == PLUS_EXPR)
11289 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11290 "suggest parentheses around %<+%> inside %<>>%>");
11291 else if (code_right == PLUS_EXPR)
11292 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11293 "suggest parentheses around %<+%> inside %<>>%>");
11294 else if (code_left == MINUS_EXPR)
11295 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11296 "suggest parentheses around %<-%> inside %<>>%>");
11297 else if (code_right == MINUS_EXPR)
11298 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11299 "suggest parentheses around %<-%> inside %<>>%>");
11300 return;
11302 case TRUTH_ORIF_EXPR:
11303 if (code_left == TRUTH_ANDIF_EXPR)
11304 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11305 "suggest parentheses around %<&&%> within %<||%>");
11306 else if (code_right == TRUTH_ANDIF_EXPR)
11307 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11308 "suggest parentheses around %<&&%> within %<||%>");
11309 return;
11311 case BIT_IOR_EXPR:
11312 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11313 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11314 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11315 "suggest parentheses around arithmetic in operand of %<|%>");
11316 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11317 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11318 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11319 "suggest parentheses around arithmetic in operand of %<|%>");
11320 /* Check cases like x|y==z */
11321 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11322 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11323 "suggest parentheses around comparison in operand of %<|%>");
11324 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11325 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11326 "suggest parentheses around comparison in operand of %<|%>");
11327 /* Check cases like !x | y */
11328 else if (code_left == TRUTH_NOT_EXPR
11329 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11330 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11331 "suggest parentheses around operand of "
11332 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11333 return;
11335 case BIT_XOR_EXPR:
11336 if (code_left == BIT_AND_EXPR
11337 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11338 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11339 "suggest parentheses around arithmetic in operand of %<^%>");
11340 else if (code_right == BIT_AND_EXPR
11341 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11342 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11343 "suggest parentheses around arithmetic in operand of %<^%>");
11344 /* Check cases like x^y==z */
11345 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11346 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11347 "suggest parentheses around comparison in operand of %<^%>");
11348 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11349 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11350 "suggest parentheses around comparison in operand of %<^%>");
11351 return;
11353 case BIT_AND_EXPR:
11354 if (code_left == PLUS_EXPR)
11355 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11356 "suggest parentheses around %<+%> in operand of %<&%>");
11357 else if (code_right == PLUS_EXPR)
11358 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11359 "suggest parentheses around %<+%> in operand of %<&%>");
11360 else if (code_left == MINUS_EXPR)
11361 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11362 "suggest parentheses around %<-%> in operand of %<&%>");
11363 else if (code_right == MINUS_EXPR)
11364 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11365 "suggest parentheses around %<-%> in operand of %<&%>");
11366 /* Check cases like x&y==z */
11367 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11368 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11369 "suggest parentheses around comparison in operand of %<&%>");
11370 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11371 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11372 "suggest parentheses around comparison in operand of %<&%>");
11373 /* Check cases like !x & y */
11374 else if (code_left == TRUTH_NOT_EXPR
11375 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11376 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11377 "suggest parentheses around operand of "
11378 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11379 return;
11381 case EQ_EXPR:
11382 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11383 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11384 "suggest parentheses around comparison in operand of %<==%>");
11385 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11386 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11387 "suggest parentheses around comparison in operand of %<==%>");
11388 return;
11389 case NE_EXPR:
11390 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11391 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11392 "suggest parentheses around comparison in operand of %<!=%>");
11393 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11394 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11395 "suggest parentheses around comparison in operand of %<!=%>");
11396 return;
11398 default:
11399 if (TREE_CODE_CLASS (code) == tcc_comparison)
11401 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11402 && code_left != NE_EXPR && code_left != EQ_EXPR
11403 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11404 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11405 "comparisons like %<X<=Y<=Z%> do not "
11406 "have their mathematical meaning");
11407 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11408 && code_right != NE_EXPR && code_right != EQ_EXPR
11409 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11410 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11411 "comparisons like %<X<=Y<=Z%> do not "
11412 "have their mathematical meaning");
11414 return;
11416 #undef NOT_A_BOOLEAN_EXPR_P
11419 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11421 void
11422 warn_for_unused_label (tree label)
11424 if (!TREE_USED (label))
11426 if (DECL_INITIAL (label))
11427 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11428 else
11429 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11433 /* Warn for division by zero according to the value of DIVISOR. LOC
11434 is the location of the division operator. */
11436 void
11437 warn_for_div_by_zero (location_t loc, tree divisor)
11439 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11440 about division by zero. Do not issue a warning if DIVISOR has a
11441 floating-point type, since we consider 0.0/0.0 a valid way of
11442 generating a NaN. */
11443 if (c_inhibit_evaluation_warnings == 0
11444 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11445 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11448 /* Subroutine of build_binary_op. Give warnings for comparisons
11449 between signed and unsigned quantities that may fail. Do the
11450 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11451 so that casts will be considered, but default promotions won't
11454 LOCATION is the location of the comparison operator.
11456 The arguments of this function map directly to local variables
11457 of build_binary_op. */
11459 void
11460 warn_for_sign_compare (location_t location,
11461 tree orig_op0, tree orig_op1,
11462 tree op0, tree op1,
11463 tree result_type, enum tree_code resultcode)
11465 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11466 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11467 int unsignedp0, unsignedp1;
11469 /* In C++, check for comparison of different enum types. */
11470 if (c_dialect_cxx()
11471 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11472 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11473 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11474 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11476 warning_at (location,
11477 OPT_Wsign_compare, "comparison between types %qT and %qT",
11478 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11481 /* Do not warn if the comparison is being done in a signed type,
11482 since the signed type will only be chosen if it can represent
11483 all the values of the unsigned type. */
11484 if (!TYPE_UNSIGNED (result_type))
11485 /* OK */;
11486 /* Do not warn if both operands are unsigned. */
11487 else if (op0_signed == op1_signed)
11488 /* OK */;
11489 else
11491 tree sop, uop, base_type;
11492 bool ovf;
11494 if (op0_signed)
11495 sop = orig_op0, uop = orig_op1;
11496 else
11497 sop = orig_op1, uop = orig_op0;
11499 STRIP_TYPE_NOPS (sop);
11500 STRIP_TYPE_NOPS (uop);
11501 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11502 ? TREE_TYPE (result_type) : result_type);
11504 /* Do not warn if the signed quantity is an unsuffixed integer
11505 literal (or some static constant expression involving such
11506 literals or a conditional expression involving such literals)
11507 and it is non-negative. */
11508 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11509 /* OK */;
11510 /* Do not warn if the comparison is an equality operation, the
11511 unsigned quantity is an integral constant, and it would fit
11512 in the result if the result were signed. */
11513 else if (TREE_CODE (uop) == INTEGER_CST
11514 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11515 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11516 /* OK */;
11517 /* In C, do not warn if the unsigned quantity is an enumeration
11518 constant and its maximum value would fit in the result if the
11519 result were signed. */
11520 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11521 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11522 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11523 c_common_signed_type (base_type)))
11524 /* OK */;
11525 else
11526 warning_at (location,
11527 OPT_Wsign_compare,
11528 "comparison between signed and unsigned integer expressions");
11531 /* Warn if two unsigned values are being compared in a size larger
11532 than their original size, and one (and only one) is the result of
11533 a `~' operator. This comparison will always fail.
11535 Also warn if one operand is a constant, and the constant does not
11536 have all bits set that are set in the ~ operand when it is
11537 extended. */
11539 op0 = c_common_get_narrower (op0, &unsignedp0);
11540 op1 = c_common_get_narrower (op1, &unsignedp1);
11542 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11543 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11545 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11546 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11547 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11548 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11550 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11552 tree primop;
11553 HOST_WIDE_INT constant, mask;
11554 int unsignedp;
11555 unsigned int bits;
11557 if (tree_fits_shwi_p (op0))
11559 primop = op1;
11560 unsignedp = unsignedp1;
11561 constant = tree_to_shwi (op0);
11563 else
11565 primop = op0;
11566 unsignedp = unsignedp0;
11567 constant = tree_to_shwi (op1);
11570 bits = TYPE_PRECISION (TREE_TYPE (primop));
11571 if (bits < TYPE_PRECISION (result_type)
11572 && bits < HOST_BITS_PER_LONG && unsignedp)
11574 mask = (~ (HOST_WIDE_INT) 0) << bits;
11575 if ((mask & constant) != mask)
11577 if (constant == 0)
11578 warning_at (location, OPT_Wsign_compare,
11579 "promoted ~unsigned is always non-zero");
11580 else
11581 warning_at (location, OPT_Wsign_compare,
11582 "comparison of promoted ~unsigned with constant");
11586 else if (unsignedp0 && unsignedp1
11587 && (TYPE_PRECISION (TREE_TYPE (op0))
11588 < TYPE_PRECISION (result_type))
11589 && (TYPE_PRECISION (TREE_TYPE (op1))
11590 < TYPE_PRECISION (result_type)))
11591 warning_at (location, OPT_Wsign_compare,
11592 "comparison of promoted ~unsigned with unsigned");
11596 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11597 type via c_common_type. If -Wdouble-promotion is in use, and the
11598 conditions for warning have been met, issue a warning. GMSGID is
11599 the warning message. It must have two %T specifiers for the type
11600 that was converted (generally "float") and the type to which it was
11601 converted (generally "double), respectively. LOC is the location
11602 to which the awrning should refer. */
11604 void
11605 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11606 const char *gmsgid, location_t loc)
11608 tree source_type;
11610 if (!warn_double_promotion)
11611 return;
11612 /* If the conversion will not occur at run-time, there is no need to
11613 warn about it. */
11614 if (c_inhibit_evaluation_warnings)
11615 return;
11616 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11617 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11618 return;
11619 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11620 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11621 source_type = type1;
11622 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11623 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11624 source_type = type2;
11625 else
11626 return;
11627 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11630 /* Setup a TYPE_DECL node as a typedef representation.
11632 X is a TYPE_DECL for a typedef statement. Create a brand new
11633 ..._TYPE node (which will be just a variant of the existing
11634 ..._TYPE node with identical properties) and then install X
11635 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11637 The whole point here is to end up with a situation where each
11638 and every ..._TYPE node the compiler creates will be uniquely
11639 associated with AT MOST one node representing a typedef name.
11640 This way, even though the compiler substitutes corresponding
11641 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11642 early on, later parts of the compiler can always do the reverse
11643 translation and get back the corresponding typedef name. For
11644 example, given:
11646 typedef struct S MY_TYPE;
11647 MY_TYPE object;
11649 Later parts of the compiler might only know that `object' was of
11650 type `struct S' if it were not for code just below. With this
11651 code however, later parts of the compiler see something like:
11653 struct S' == struct S
11654 typedef struct S' MY_TYPE;
11655 struct S' object;
11657 And they can then deduce (from the node for type struct S') that
11658 the original object declaration was:
11660 MY_TYPE object;
11662 Being able to do this is important for proper support of protoize,
11663 and also for generating precise symbolic debugging information
11664 which takes full account of the programmer's (typedef) vocabulary.
11666 Obviously, we don't want to generate a duplicate ..._TYPE node if
11667 the TYPE_DECL node that we are now processing really represents a
11668 standard built-in type. */
11670 void
11671 set_underlying_type (tree x)
11673 if (x == error_mark_node)
11674 return;
11675 if (DECL_IS_BUILTIN (x))
11677 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11678 TYPE_NAME (TREE_TYPE (x)) = x;
11680 else if (TREE_TYPE (x) != error_mark_node
11681 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11683 tree tt = TREE_TYPE (x);
11684 DECL_ORIGINAL_TYPE (x) = tt;
11685 tt = build_variant_type_copy (tt);
11686 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11687 TYPE_NAME (tt) = x;
11688 TREE_USED (tt) = TREE_USED (x);
11689 TREE_TYPE (x) = tt;
11693 /* Record the types used by the current global variable declaration
11694 being parsed, so that we can decide later to emit their debug info.
11695 Those types are in types_used_by_cur_var_decl, and we are going to
11696 store them in the types_used_by_vars_hash hash table.
11697 DECL is the declaration of the global variable that has been parsed. */
11699 void
11700 record_types_used_by_current_var_decl (tree decl)
11702 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11704 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11706 tree type = types_used_by_cur_var_decl->pop ();
11707 types_used_by_var_decl_insert (type, decl);
11711 /* If DECL is a typedef that is declared in the current function,
11712 record it for the purpose of -Wunused-local-typedefs. */
11714 void
11715 record_locally_defined_typedef (tree decl)
11717 struct c_language_function *l;
11719 if (!warn_unused_local_typedefs
11720 || cfun == NULL
11721 /* if this is not a locally defined typedef then we are not
11722 interested. */
11723 || !is_typedef_decl (decl)
11724 || !decl_function_context (decl))
11725 return;
11727 l = (struct c_language_function *) cfun->language;
11728 vec_safe_push (l->local_typedefs, decl);
11731 /* If T is a TYPE_DECL declared locally, mark it as used. */
11733 void
11734 maybe_record_typedef_use (tree t)
11736 if (!is_typedef_decl (t))
11737 return;
11739 TREE_USED (t) = true;
11742 /* Warn if there are some unused locally defined typedefs in the
11743 current function. */
11745 void
11746 maybe_warn_unused_local_typedefs (void)
11748 int i;
11749 tree decl;
11750 /* The number of times we have emitted -Wunused-local-typedefs
11751 warnings. If this is different from errorcount, that means some
11752 unrelated errors have been issued. In which case, we'll avoid
11753 emitting "unused-local-typedefs" warnings. */
11754 static int unused_local_typedefs_warn_count;
11755 struct c_language_function *l;
11757 if (cfun == NULL)
11758 return;
11760 if ((l = (struct c_language_function *) cfun->language) == NULL)
11761 return;
11763 if (warn_unused_local_typedefs
11764 && errorcount == unused_local_typedefs_warn_count)
11766 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11767 if (!TREE_USED (decl))
11768 warning_at (DECL_SOURCE_LOCATION (decl),
11769 OPT_Wunused_local_typedefs,
11770 "typedef %qD locally defined but not used", decl);
11771 unused_local_typedefs_warn_count = errorcount;
11774 vec_free (l->local_typedefs);
11777 /* Warn about boolean expression compared with an integer value different
11778 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11779 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11780 are the operands of the comparison. The caller must ensure that
11781 either operand is a boolean expression. */
11783 void
11784 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11785 tree op1)
11787 if (TREE_CODE_CLASS (code) != tcc_comparison)
11788 return;
11790 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11791 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11792 if (!cst)
11793 return;
11795 if (!integer_zerop (cst) && !integer_onep (cst))
11797 int sign = (TREE_CODE (op0) == INTEGER_CST)
11798 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11799 if (code == EQ_EXPR
11800 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11801 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11802 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11803 "with boolean expression is always false", cst);
11804 else
11805 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11806 "with boolean expression is always true", cst);
11810 /* The C and C++ parsers both use vectors to hold function arguments.
11811 For efficiency, we keep a cache of unused vectors. This is the
11812 cache. */
11814 typedef vec<tree, va_gc> *tree_gc_vec;
11815 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11817 /* Return a new vector from the cache. If the cache is empty,
11818 allocate a new vector. These vectors are GC'ed, so it is OK if the
11819 pointer is not released.. */
11821 vec<tree, va_gc> *
11822 make_tree_vector (void)
11824 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11825 return tree_vector_cache->pop ();
11826 else
11828 /* Passing 0 to vec::alloc returns NULL, and our callers require
11829 that we always return a non-NULL value. The vector code uses
11830 4 when growing a NULL vector, so we do too. */
11831 vec<tree, va_gc> *v;
11832 vec_alloc (v, 4);
11833 return v;
11837 /* Release a vector of trees back to the cache. */
11839 void
11840 release_tree_vector (vec<tree, va_gc> *vec)
11842 if (vec != NULL)
11844 vec->truncate (0);
11845 vec_safe_push (tree_vector_cache, vec);
11849 /* Get a new tree vector holding a single tree. */
11851 vec<tree, va_gc> *
11852 make_tree_vector_single (tree t)
11854 vec<tree, va_gc> *ret = make_tree_vector ();
11855 ret->quick_push (t);
11856 return ret;
11859 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11861 vec<tree, va_gc> *
11862 make_tree_vector_from_list (tree list)
11864 vec<tree, va_gc> *ret = make_tree_vector ();
11865 for (; list; list = TREE_CHAIN (list))
11866 vec_safe_push (ret, TREE_VALUE (list));
11867 return ret;
11870 /* Get a new tree vector which is a copy of an existing one. */
11872 vec<tree, va_gc> *
11873 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11875 vec<tree, va_gc> *ret;
11876 unsigned int ix;
11877 tree t;
11879 ret = make_tree_vector ();
11880 vec_safe_reserve (ret, vec_safe_length (orig));
11881 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11882 ret->quick_push (t);
11883 return ret;
11886 /* Return true if KEYWORD starts a type specifier. */
11888 bool
11889 keyword_begins_type_specifier (enum rid keyword)
11891 switch (keyword)
11893 case RID_AUTO_TYPE:
11894 case RID_INT:
11895 case RID_CHAR:
11896 case RID_FLOAT:
11897 case RID_DOUBLE:
11898 case RID_VOID:
11899 case RID_UNSIGNED:
11900 case RID_LONG:
11901 case RID_SHORT:
11902 case RID_SIGNED:
11903 case RID_DFLOAT32:
11904 case RID_DFLOAT64:
11905 case RID_DFLOAT128:
11906 case RID_FRACT:
11907 case RID_ACCUM:
11908 case RID_BOOL:
11909 case RID_WCHAR:
11910 case RID_CHAR16:
11911 case RID_CHAR32:
11912 case RID_SAT:
11913 case RID_COMPLEX:
11914 case RID_TYPEOF:
11915 case RID_STRUCT:
11916 case RID_CLASS:
11917 case RID_UNION:
11918 case RID_ENUM:
11919 return true;
11920 default:
11921 if (keyword >= RID_FIRST_INT_N
11922 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
11923 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
11924 return true;
11925 return false;
11929 /* Return true if KEYWORD names a type qualifier. */
11931 bool
11932 keyword_is_type_qualifier (enum rid keyword)
11934 switch (keyword)
11936 case RID_CONST:
11937 case RID_VOLATILE:
11938 case RID_RESTRICT:
11939 case RID_ATOMIC:
11940 return true;
11941 default:
11942 return false;
11946 /* Return true if KEYWORD names a storage class specifier.
11948 RID_TYPEDEF is not included in this list despite `typedef' being
11949 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11950 such for syntactic convenience only. */
11952 bool
11953 keyword_is_storage_class_specifier (enum rid keyword)
11955 switch (keyword)
11957 case RID_STATIC:
11958 case RID_EXTERN:
11959 case RID_REGISTER:
11960 case RID_AUTO:
11961 case RID_MUTABLE:
11962 case RID_THREAD:
11963 return true;
11964 default:
11965 return false;
11969 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11971 static bool
11972 keyword_is_function_specifier (enum rid keyword)
11974 switch (keyword)
11976 case RID_INLINE:
11977 case RID_NORETURN:
11978 case RID_VIRTUAL:
11979 case RID_EXPLICIT:
11980 return true;
11981 default:
11982 return false;
11986 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11987 declaration-specifier (C99 6.7). */
11989 bool
11990 keyword_is_decl_specifier (enum rid keyword)
11992 if (keyword_is_storage_class_specifier (keyword)
11993 || keyword_is_type_qualifier (keyword)
11994 || keyword_is_function_specifier (keyword))
11995 return true;
11997 switch (keyword)
11999 case RID_TYPEDEF:
12000 case RID_FRIEND:
12001 case RID_CONSTEXPR:
12002 return true;
12003 default:
12004 return false;
12008 /* Initialize language-specific-bits of tree_contains_struct. */
12010 void
12011 c_common_init_ts (void)
12013 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12014 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12015 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12018 /* Build a user-defined numeric literal out of an integer constant type VALUE
12019 with identifier SUFFIX. */
12021 tree
12022 build_userdef_literal (tree suffix_id, tree value,
12023 enum overflow_type overflow, tree num_string)
12025 tree literal = make_node (USERDEF_LITERAL);
12026 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12027 USERDEF_LITERAL_VALUE (literal) = value;
12028 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12029 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12030 return literal;
12033 /* For vector[index], convert the vector to a
12034 pointer of the underlying type. Return true if the resulting
12035 ARRAY_REF should not be an lvalue. */
12037 bool
12038 convert_vector_to_pointer_for_subscript (location_t loc,
12039 tree *vecp, tree index)
12041 bool ret = false;
12042 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12044 tree type = TREE_TYPE (*vecp);
12045 tree type1;
12047 ret = !lvalue_p (*vecp);
12048 if (TREE_CODE (index) == INTEGER_CST)
12049 if (!tree_fits_uhwi_p (index)
12050 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12051 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12053 if (ret)
12055 tree tmp = create_tmp_var_raw (type, NULL);
12056 DECL_SOURCE_LOCATION (tmp) = loc;
12057 *vecp = c_save_expr (*vecp);
12058 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12060 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12061 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12062 *vecp
12063 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12064 *vecp, NULL_TREE, NULL_TREE),
12065 non_const);
12067 else
12068 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12069 NULL_TREE, NULL_TREE);
12070 SET_EXPR_LOCATION (*vecp, loc);
12071 c_common_mark_addressable_vec (tmp);
12073 else
12074 c_common_mark_addressable_vec (*vecp);
12075 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12076 type1 = build_pointer_type (TREE_TYPE (*vecp));
12077 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12078 if (!ref_all
12079 && !DECL_P (*vecp))
12081 /* If the original vector isn't declared may_alias and it
12082 isn't a bare vector look if the subscripting would
12083 alias the vector we subscript, and if not, force ref-all. */
12084 alias_set_type vecset = get_alias_set (*vecp);
12085 alias_set_type sset = get_alias_set (type);
12086 if (!alias_sets_must_conflict_p (sset, vecset)
12087 && !alias_set_subset_of (sset, vecset))
12088 ref_all = true;
12090 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12091 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12092 *vecp = convert (type, *vecp);
12094 return ret;
12097 /* Determine which of the operands, if any, is a scalar that needs to be
12098 converted to a vector, for the range of operations. */
12099 enum stv_conv
12100 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12101 bool complain)
12103 tree type0 = TREE_TYPE (op0);
12104 tree type1 = TREE_TYPE (op1);
12105 bool integer_only_op = false;
12106 enum stv_conv ret = stv_firstarg;
12108 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12109 || TREE_CODE (type1) == VECTOR_TYPE);
12110 switch (code)
12112 /* Most GENERIC binary expressions require homogeneous arguments.
12113 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12114 argument that is a vector and a second one that is a scalar, so
12115 we never return stv_secondarg for them. */
12116 case RSHIFT_EXPR:
12117 case LSHIFT_EXPR:
12118 if (TREE_CODE (type0) == INTEGER_TYPE
12119 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12121 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12123 if (complain)
12124 error_at (loc, "conversion of scalar %qT to vector %qT "
12125 "involves truncation", type0, type1);
12126 return stv_error;
12128 else
12129 return stv_firstarg;
12131 break;
12133 case BIT_IOR_EXPR:
12134 case BIT_XOR_EXPR:
12135 case BIT_AND_EXPR:
12136 integer_only_op = true;
12137 /* ... fall through ... */
12139 case VEC_COND_EXPR:
12141 case PLUS_EXPR:
12142 case MINUS_EXPR:
12143 case MULT_EXPR:
12144 case TRUNC_DIV_EXPR:
12145 case CEIL_DIV_EXPR:
12146 case FLOOR_DIV_EXPR:
12147 case ROUND_DIV_EXPR:
12148 case EXACT_DIV_EXPR:
12149 case TRUNC_MOD_EXPR:
12150 case FLOOR_MOD_EXPR:
12151 case RDIV_EXPR:
12152 case EQ_EXPR:
12153 case NE_EXPR:
12154 case LE_EXPR:
12155 case GE_EXPR:
12156 case LT_EXPR:
12157 case GT_EXPR:
12158 /* What about UNLT_EXPR? */
12159 if (TREE_CODE (type0) == VECTOR_TYPE)
12161 tree tmp;
12162 ret = stv_secondarg;
12163 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12164 tmp = type0; type0 = type1; type1 = tmp;
12165 tmp = op0; op0 = op1; op1 = tmp;
12168 if (TREE_CODE (type0) == INTEGER_TYPE
12169 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12171 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12173 if (complain)
12174 error_at (loc, "conversion of scalar %qT to vector %qT "
12175 "involves truncation", type0, type1);
12176 return stv_error;
12178 return ret;
12180 else if (!integer_only_op
12181 /* Allow integer --> real conversion if safe. */
12182 && (TREE_CODE (type0) == REAL_TYPE
12183 || TREE_CODE (type0) == INTEGER_TYPE)
12184 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12186 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12188 if (complain)
12189 error_at (loc, "conversion of scalar %qT to vector %qT "
12190 "involves truncation", type0, type1);
12191 return stv_error;
12193 return ret;
12195 default:
12196 break;
12199 return stv_nothing;
12202 /* Return true iff ALIGN is an integral constant that is a fundamental
12203 alignment, as defined by [basic.align] in the c++-11
12204 specifications.
12206 That is:
12208 [A fundamental alignment is represented by an alignment less than or
12209 equal to the greatest alignment supported by the implementation
12210 in all contexts, which is equal to
12211 alignof(max_align_t)]. */
12213 bool
12214 cxx_fundamental_alignment_p (unsigned align)
12216 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12217 TYPE_ALIGN (long_double_type_node)));
12220 /* Return true if T is a pointer to a zero-sized aggregate. */
12222 bool
12223 pointer_to_zero_sized_aggr_p (tree t)
12225 if (!POINTER_TYPE_P (t))
12226 return false;
12227 t = TREE_TYPE (t);
12228 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12231 #include "gt-c-family-c-common.h"