svn merge -r215707:216846 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / c-family / c-common.c
blob64f1edba5604569ab238f411d9a7801ae4de4479
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"
64 cpp_reader *parse_in; /* Declared in c-pragma.h. */
66 /* The following symbols are subsumed in the c_global_trees array, and
67 listed here individually for documentation purposes.
69 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
71 tree short_integer_type_node;
72 tree long_integer_type_node;
73 tree long_long_integer_type_node;
75 tree short_unsigned_type_node;
76 tree long_unsigned_type_node;
77 tree long_long_unsigned_type_node;
79 tree truthvalue_type_node;
80 tree truthvalue_false_node;
81 tree truthvalue_true_node;
83 tree ptrdiff_type_node;
85 tree unsigned_char_type_node;
86 tree signed_char_type_node;
87 tree wchar_type_node;
89 tree char16_type_node;
90 tree char32_type_node;
92 tree float_type_node;
93 tree double_type_node;
94 tree long_double_type_node;
96 tree complex_integer_type_node;
97 tree complex_float_type_node;
98 tree complex_double_type_node;
99 tree complex_long_double_type_node;
101 tree dfloat32_type_node;
102 tree dfloat64_type_node;
103 tree_dfloat128_type_node;
105 tree intQI_type_node;
106 tree intHI_type_node;
107 tree intSI_type_node;
108 tree intDI_type_node;
109 tree intTI_type_node;
111 tree unsigned_intQI_type_node;
112 tree unsigned_intHI_type_node;
113 tree unsigned_intSI_type_node;
114 tree unsigned_intDI_type_node;
115 tree unsigned_intTI_type_node;
117 tree widest_integer_literal_type_node;
118 tree widest_unsigned_literal_type_node;
120 Nodes for types `void *' and `const void *'.
122 tree ptr_type_node, const_ptr_type_node;
124 Nodes for types `char *' and `const char *'.
126 tree string_type_node, const_string_type_node;
128 Type `char[SOMENUMBER]'.
129 Used when an array of char is needed and the size is irrelevant.
131 tree char_array_type_node;
133 Type `wchar_t[SOMENUMBER]' or something like it.
134 Used when a wide string literal is created.
136 tree wchar_array_type_node;
138 Type `char16_t[SOMENUMBER]' or something like it.
139 Used when a UTF-16 string literal is created.
141 tree char16_array_type_node;
143 Type `char32_t[SOMENUMBER]' or something like it.
144 Used when a UTF-32 string literal is created.
146 tree char32_array_type_node;
148 Type `int ()' -- used for implicit declaration of functions.
150 tree default_function_type;
152 A VOID_TYPE node, packaged in a TREE_LIST.
154 tree void_list_node;
156 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
157 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
158 VAR_DECLS, but C++ does.)
160 tree function_name_decl_node;
161 tree pretty_function_name_decl_node;
162 tree c99_function_name_decl_node;
164 Stack of nested function name VAR_DECLs.
166 tree saved_function_name_decls;
170 tree c_global_trees[CTI_MAX];
172 /* Switches common to the C front ends. */
174 /* Nonzero means don't output line number information. */
176 char flag_no_line_commands;
178 /* Nonzero causes -E output not to be done, but directives such as
179 #define that have side effects are still obeyed. */
181 char flag_no_output;
183 /* Nonzero means dump macros in some fashion. */
185 char flag_dump_macros;
187 /* Nonzero means pass #include lines through to the output. */
189 char flag_dump_includes;
191 /* Nonzero means process PCH files while preprocessing. */
193 bool flag_pch_preprocess;
195 /* The file name to which we should write a precompiled header, or
196 NULL if no header will be written in this compile. */
198 const char *pch_file;
200 /* Nonzero if an ISO standard was selected. It rejects macros in the
201 user's namespace. */
202 int flag_iso;
204 /* C/ObjC language option variables. */
207 /* Nonzero means allow type mismatches in conditional expressions;
208 just make their values `void'. */
210 int flag_cond_mismatch;
212 /* Nonzero means enable C89 Amendment 1 features. */
214 int flag_isoc94;
216 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
218 int flag_isoc99;
220 /* Nonzero means use the ISO C11 dialect of C. */
222 int flag_isoc11;
224 /* Nonzero means that we have builtin functions, and main is an int. */
226 int flag_hosted = 1;
229 /* ObjC language option variables. */
232 /* Tells the compiler that this is a special run. Do not perform any
233 compiling, instead we are to test some platform dependent features
234 and output a C header file with appropriate definitions. */
236 int print_struct_values;
238 /* Tells the compiler what is the constant string class for ObjC. */
240 const char *constant_string_class_name;
243 /* C++ language option variables. */
246 /* Nonzero means generate separate instantiation control files and
247 juggle them at link time. */
249 int flag_use_repository;
251 /* The C++ dialect being used. C++98 is the default. */
253 enum cxx_dialect cxx_dialect = cxx98;
255 /* Maximum template instantiation depth. This limit exists to limit the
256 time it takes to notice excessively recursive template instantiations.
258 The default is lower than the 1024 recommended by the C++0x standard
259 because G++ runs out of stack before 1024 with highly recursive template
260 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
262 int max_tinst_depth = 900;
264 /* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
266 tree *ridpointers;
268 tree (*make_fname_decl) (location_t, tree, int);
270 /* Nonzero means don't warn about problems that occur when the code is
271 executed. */
272 int c_inhibit_evaluation_warnings;
274 /* Whether we are building a boolean conversion inside
275 convert_for_assignment, or some other late binary operation. If
276 build_binary_op is called for C (from code shared by C and C++) in
277 this case, then the operands have already been folded and the
278 result will not be folded again, so C_MAYBE_CONST_EXPR should not
279 be generated. */
280 bool in_late_binary_op;
282 /* Whether lexing has been completed, so subsequent preprocessor
283 errors should use the compiler's input_location. */
284 bool done_lexing = false;
286 /* Information about how a function name is generated. */
287 struct fname_var_t
289 tree *const decl; /* pointer to the VAR_DECL. */
290 const unsigned rid; /* RID number for the identifier. */
291 const int pretty; /* How pretty is it? */
294 /* The three ways of getting then name of the current function. */
296 const struct fname_var_t fname_vars[] =
298 /* C99 compliant __func__, must be first. */
299 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
300 /* GCC __FUNCTION__ compliant. */
301 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
302 /* GCC __PRETTY_FUNCTION__ compliant. */
303 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
304 {NULL, 0, 0},
307 /* Global visibility options. */
308 struct visibility_flags visibility_options;
310 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
311 static tree check_case_value (location_t, tree);
312 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
314 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
315 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
321 int, bool *);
322 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
323 int, bool *);
324 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
325 bool *);
326 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_always_inline_attribute (tree *, tree, tree, int,
330 bool *);
331 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
338 bool *);
339 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
340 bool *);
341 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_transparent_union_attribute (tree *, tree, tree,
343 int, bool *);
344 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
350 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
351 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
354 static tree handle_visibility_attribute (tree *, tree, tree, int,
355 bool *);
356 static tree handle_tls_model_attribute (tree *, tree, tree, int,
357 bool *);
358 static tree handle_no_instrument_function_attribute (tree *, tree,
359 tree, int, bool *);
360 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
363 bool *);
364 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_deprecated_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_vector_size_attribute (tree *, tree, tree, int,
371 bool *);
372 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
376 bool *);
377 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
384 static tree ignore_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
390 bool *);
391 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
392 bool *);
393 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
395 static void check_function_nonnull (tree, int, tree *);
396 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
397 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
398 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
399 static int resort_field_decl_cmp (const void *, const void *);
401 /* Reserved words. The third field is a mask: keywords are disabled
402 if they match the mask.
404 Masks for languages:
405 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
406 C --std=c99: D_CXXONLY | D_OBJC
407 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
408 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
409 C++ --std=c0x: D_CONLY | D_OBJC
410 ObjC++ is like C++ except that D_OBJC is not set
412 If -fno-asm is used, D_ASM is added to the mask. If
413 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
414 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
415 In C with -Wc++-compat, we warn if D_CXXWARN is set.
417 Note the complication of the D_CXX_OBJC keywords. These are
418 reserved words such as 'class'. In C++, 'class' is a reserved
419 word. In Objective-C++ it is too. In Objective-C, it is a
420 reserved word too, but only if it follows an '@' sign.
422 const struct c_common_resword c_common_reswords[] =
424 { "_Alignas", RID_ALIGNAS, D_CONLY },
425 { "_Alignof", RID_ALIGNOF, D_CONLY },
426 { "_Atomic", RID_ATOMIC, D_CONLY },
427 { "_Bool", RID_BOOL, D_CONLY },
428 { "_Complex", RID_COMPLEX, 0 },
429 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
430 { "_Cilk_sync", RID_CILK_SYNC, 0 },
431 { "_Cilk_for", RID_CILK_FOR, 0 },
432 { "_Imaginary", RID_IMAGINARY, D_CONLY },
433 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
434 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
435 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
436 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
437 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
438 { "_Sat", RID_SAT, D_CONLY | D_EXT },
439 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
440 { "_Noreturn", RID_NORETURN, D_CONLY },
441 { "_Generic", RID_GENERIC, D_CONLY },
442 { "_Thread_local", RID_THREAD, D_CONLY },
443 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
444 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
445 { "__alignof", RID_ALIGNOF, 0 },
446 { "__alignof__", RID_ALIGNOF, 0 },
447 { "__asm", RID_ASM, 0 },
448 { "__asm__", RID_ASM, 0 },
449 { "__attribute", RID_ATTRIBUTE, 0 },
450 { "__attribute__", RID_ATTRIBUTE, 0 },
451 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
452 { "__bases", RID_BASES, D_CXXONLY },
453 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
454 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
455 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
456 { "__builtin_offsetof", RID_OFFSETOF, 0 },
457 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
458 { "__builtin_va_arg", RID_VA_ARG, 0 },
459 { "__complex", RID_COMPLEX, 0 },
460 { "__complex__", RID_COMPLEX, 0 },
461 { "__const", RID_CONST, 0 },
462 { "__const__", RID_CONST, 0 },
463 { "__decltype", RID_DECLTYPE, D_CXXONLY },
464 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
465 { "__extension__", RID_EXTENSION, 0 },
466 { "__func__", RID_C99_FUNCTION_NAME, 0 },
467 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
468 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
469 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
470 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
471 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
472 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
473 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
474 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
475 { "__imag", RID_IMAGPART, 0 },
476 { "__imag__", RID_IMAGPART, 0 },
477 { "__inline", RID_INLINE, 0 },
478 { "__inline__", RID_INLINE, 0 },
479 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
480 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
481 { "__is_class", RID_IS_CLASS, D_CXXONLY },
482 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
483 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
484 { "__is_final", RID_IS_FINAL, D_CXXONLY },
485 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
486 { "__is_pod", RID_IS_POD, D_CXXONLY },
487 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
488 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
489 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
490 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
491 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
492 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
493 { "__is_union", RID_IS_UNION, D_CXXONLY },
494 { "__label__", RID_LABEL, 0 },
495 { "__null", RID_NULL, 0 },
496 { "__real", RID_REALPART, 0 },
497 { "__real__", RID_REALPART, 0 },
498 { "__restrict", RID_RESTRICT, 0 },
499 { "__restrict__", RID_RESTRICT, 0 },
500 { "__signed", RID_SIGNED, 0 },
501 { "__signed__", RID_SIGNED, 0 },
502 { "__thread", RID_THREAD, 0 },
503 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
504 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
505 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
506 { "__typeof", RID_TYPEOF, 0 },
507 { "__typeof__", RID_TYPEOF, 0 },
508 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
509 { "__volatile", RID_VOLATILE, 0 },
510 { "__volatile__", RID_VOLATILE, 0 },
511 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
512 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
513 { "asm", RID_ASM, D_ASM },
514 { "auto", RID_AUTO, 0 },
515 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
516 { "break", RID_BREAK, 0 },
517 { "case", RID_CASE, 0 },
518 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
519 { "char", RID_CHAR, 0 },
520 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
521 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
522 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
523 { "const", RID_CONST, 0 },
524 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
525 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
526 { "continue", RID_CONTINUE, 0 },
527 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "default", RID_DEFAULT, 0 },
529 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
530 { "do", RID_DO, 0 },
531 { "double", RID_DOUBLE, 0 },
532 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
533 { "else", RID_ELSE, 0 },
534 { "enum", RID_ENUM, 0 },
535 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
536 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
537 { "extern", RID_EXTERN, 0 },
538 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
539 { "float", RID_FLOAT, 0 },
540 { "for", RID_FOR, 0 },
541 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
542 { "goto", RID_GOTO, 0 },
543 { "if", RID_IF, 0 },
544 { "inline", RID_INLINE, D_EXT89 },
545 { "int", RID_INT, 0 },
546 { "long", RID_LONG, 0 },
547 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
548 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
549 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
550 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
551 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
552 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
553 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
554 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
555 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
556 { "register", RID_REGISTER, 0 },
557 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
558 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
559 { "return", RID_RETURN, 0 },
560 { "short", RID_SHORT, 0 },
561 { "signed", RID_SIGNED, 0 },
562 { "sizeof", RID_SIZEOF, 0 },
563 { "static", RID_STATIC, 0 },
564 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
565 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
566 { "struct", RID_STRUCT, 0 },
567 { "switch", RID_SWITCH, 0 },
568 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
569 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
570 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
571 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
572 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
573 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
574 { "typedef", RID_TYPEDEF, 0 },
575 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
576 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
577 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
578 { "union", RID_UNION, 0 },
579 { "unsigned", RID_UNSIGNED, 0 },
580 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
581 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
582 { "void", RID_VOID, 0 },
583 { "volatile", RID_VOLATILE, 0 },
584 { "wchar_t", RID_WCHAR, D_CXXONLY },
585 { "while", RID_WHILE, 0 },
586 /* These Objective-C keywords are recognized only immediately after
587 an '@'. */
588 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
589 { "defs", RID_AT_DEFS, D_OBJC },
590 { "encode", RID_AT_ENCODE, D_OBJC },
591 { "end", RID_AT_END, D_OBJC },
592 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
593 { "interface", RID_AT_INTERFACE, D_OBJC },
594 { "protocol", RID_AT_PROTOCOL, D_OBJC },
595 { "selector", RID_AT_SELECTOR, D_OBJC },
596 { "finally", RID_AT_FINALLY, D_OBJC },
597 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
598 { "optional", RID_AT_OPTIONAL, D_OBJC },
599 { "required", RID_AT_REQUIRED, D_OBJC },
600 { "property", RID_AT_PROPERTY, D_OBJC },
601 { "package", RID_AT_PACKAGE, D_OBJC },
602 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
603 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
604 /* These are recognized only in protocol-qualifier context
605 (see above) */
606 { "bycopy", RID_BYCOPY, D_OBJC },
607 { "byref", RID_BYREF, D_OBJC },
608 { "in", RID_IN, D_OBJC },
609 { "inout", RID_INOUT, D_OBJC },
610 { "oneway", RID_ONEWAY, D_OBJC },
611 { "out", RID_OUT, D_OBJC },
612 /* These are recognized inside a property attribute list */
613 { "assign", RID_ASSIGN, D_OBJC },
614 { "copy", RID_COPY, D_OBJC },
615 { "getter", RID_GETTER, D_OBJC },
616 { "nonatomic", RID_NONATOMIC, D_OBJC },
617 { "readonly", RID_READONLY, D_OBJC },
618 { "readwrite", RID_READWRITE, D_OBJC },
619 { "retain", RID_RETAIN, D_OBJC },
620 { "setter", RID_SETTER, D_OBJC },
623 const unsigned int num_c_common_reswords =
624 sizeof c_common_reswords / sizeof (struct c_common_resword);
626 /* Table of machine-independent attributes common to all C-like languages. */
627 const struct attribute_spec c_common_attribute_table[] =
629 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
630 affects_type_identity } */
631 { "packed", 0, 0, false, false, false,
632 handle_packed_attribute , false},
633 { "nocommon", 0, 0, true, false, false,
634 handle_nocommon_attribute, false},
635 { "common", 0, 0, true, false, false,
636 handle_common_attribute, false },
637 /* FIXME: logically, noreturn attributes should be listed as
638 "false, true, true" and apply to function types. But implementing this
639 would require all the places in the compiler that use TREE_THIS_VOLATILE
640 on a decl to identify non-returning functions to be located and fixed
641 to check the function type instead. */
642 { "noreturn", 0, 0, true, false, false,
643 handle_noreturn_attribute, false },
644 { "volatile", 0, 0, true, false, false,
645 handle_noreturn_attribute, false },
646 { "noinline", 0, 0, true, false, false,
647 handle_noinline_attribute, false },
648 { "noclone", 0, 0, true, false, false,
649 handle_noclone_attribute, false },
650 { "leaf", 0, 0, true, false, false,
651 handle_leaf_attribute, false },
652 { "always_inline", 0, 0, true, false, false,
653 handle_always_inline_attribute, false },
654 { "gnu_inline", 0, 0, true, false, false,
655 handle_gnu_inline_attribute, false },
656 { "artificial", 0, 0, true, false, false,
657 handle_artificial_attribute, false },
658 { "flatten", 0, 0, true, false, false,
659 handle_flatten_attribute, false },
660 { "used", 0, 0, true, false, false,
661 handle_used_attribute, false },
662 { "unused", 0, 0, false, false, false,
663 handle_unused_attribute, false },
664 { "externally_visible", 0, 0, true, false, false,
665 handle_externally_visible_attribute, false },
666 { "no_reorder", 0, 0, true, false, false,
667 handle_no_reorder_attribute, false },
668 /* The same comments as for noreturn attributes apply to const ones. */
669 { "const", 0, 0, true, false, false,
670 handle_const_attribute, false },
671 { "transparent_union", 0, 0, false, false, false,
672 handle_transparent_union_attribute, false },
673 { "constructor", 0, 1, true, false, false,
674 handle_constructor_attribute, false },
675 { "destructor", 0, 1, true, false, false,
676 handle_destructor_attribute, false },
677 { "mode", 1, 1, false, true, false,
678 handle_mode_attribute, false },
679 { "section", 1, 1, true, false, false,
680 handle_section_attribute, false },
681 { "aligned", 0, 1, false, false, false,
682 handle_aligned_attribute, false },
683 { "weak", 0, 0, true, false, false,
684 handle_weak_attribute, false },
685 { "ifunc", 1, 1, true, false, false,
686 handle_ifunc_attribute, false },
687 { "alias", 1, 1, true, false, false,
688 handle_alias_attribute, false },
689 { "weakref", 0, 1, true, false, false,
690 handle_weakref_attribute, false },
691 { "no_instrument_function", 0, 0, true, false, false,
692 handle_no_instrument_function_attribute,
693 false },
694 { "malloc", 0, 0, true, false, false,
695 handle_malloc_attribute, false },
696 { "returns_twice", 0, 0, true, false, false,
697 handle_returns_twice_attribute, false },
698 { "no_stack_limit", 0, 0, true, false, false,
699 handle_no_limit_stack_attribute, false },
700 { "pure", 0, 0, true, false, false,
701 handle_pure_attribute, false },
702 { "transaction_callable", 0, 0, false, true, false,
703 handle_tm_attribute, false },
704 { "transaction_unsafe", 0, 0, false, true, false,
705 handle_tm_attribute, false },
706 { "transaction_safe", 0, 0, false, true, false,
707 handle_tm_attribute, false },
708 { "transaction_may_cancel_outer", 0, 0, false, true, false,
709 handle_tm_attribute, false },
710 /* ??? These two attributes didn't make the transition from the
711 Intel language document to the multi-vendor language document. */
712 { "transaction_pure", 0, 0, false, true, false,
713 handle_tm_attribute, false },
714 { "transaction_wrap", 1, 1, true, false, false,
715 handle_tm_wrap_attribute, false },
716 /* For internal use (marking of builtins) only. The name contains space
717 to prevent its usage in source code. */
718 { "no vops", 0, 0, true, false, false,
719 handle_novops_attribute, false },
720 { "deprecated", 0, 1, false, false, false,
721 handle_deprecated_attribute, false },
722 { "vector_size", 1, 1, false, true, false,
723 handle_vector_size_attribute, false },
724 { "visibility", 1, 1, false, false, false,
725 handle_visibility_attribute, false },
726 { "tls_model", 1, 1, true, false, false,
727 handle_tls_model_attribute, false },
728 { "nonnull", 0, -1, false, true, true,
729 handle_nonnull_attribute, false },
730 { "nothrow", 0, 0, true, false, false,
731 handle_nothrow_attribute, false },
732 { "may_alias", 0, 0, false, true, false, NULL, false },
733 { "cleanup", 1, 1, true, false, false,
734 handle_cleanup_attribute, false },
735 { "warn_unused_result", 0, 0, false, true, true,
736 handle_warn_unused_result_attribute, false },
737 { "sentinel", 0, 1, false, true, true,
738 handle_sentinel_attribute, false },
739 /* For internal use (marking of builtins) only. The name contains space
740 to prevent its usage in source code. */
741 { "type generic", 0, 0, false, true, true,
742 handle_type_generic_attribute, false },
743 { "alloc_size", 1, 2, false, true, true,
744 handle_alloc_size_attribute, false },
745 { "cold", 0, 0, true, false, false,
746 handle_cold_attribute, false },
747 { "hot", 0, 0, true, false, false,
748 handle_hot_attribute, false },
749 { "no_address_safety_analysis",
750 0, 0, true, false, false,
751 handle_no_address_safety_analysis_attribute,
752 false },
753 { "no_sanitize_address", 0, 0, true, false, false,
754 handle_no_sanitize_address_attribute,
755 false },
756 { "no_sanitize_undefined", 0, 0, true, false, false,
757 handle_no_sanitize_undefined_attribute,
758 false },
759 { "warning", 1, 1, true, false, false,
760 handle_error_attribute, false },
761 { "error", 1, 1, true, false, false,
762 handle_error_attribute, false },
763 { "target", 1, -1, true, false, false,
764 handle_target_attribute, false },
765 { "optimize", 1, -1, true, false, false,
766 handle_optimize_attribute, false },
767 /* For internal use only. The leading '*' both prevents its usage in
768 source code and signals that it may be overridden by machine tables. */
769 { "*tm regparm", 0, 0, false, true, true,
770 ignore_attribute, false },
771 { "no_split_stack", 0, 0, true, false, false,
772 handle_no_split_stack_attribute, false },
773 /* For internal use (marking of builtins and runtime functions) only.
774 The name contains space to prevent its usage in source code. */
775 { "fn spec", 1, 1, false, true, true,
776 handle_fnspec_attribute, false },
777 { "warn_unused", 0, 0, false, false, false,
778 handle_warn_unused_attribute, false },
779 { "returns_nonnull", 0, 0, false, true, true,
780 handle_returns_nonnull_attribute, false },
781 { "omp declare simd", 0, -1, true, false, false,
782 handle_omp_declare_simd_attribute, false },
783 { "cilk simd function", 0, -1, true, false, false,
784 handle_omp_declare_simd_attribute, false },
785 { "omp declare target", 0, 0, true, false, false,
786 handle_omp_declare_target_attribute, false },
787 { "alloc_align", 1, 1, false, true, true,
788 handle_alloc_align_attribute, false },
789 { "assume_aligned", 1, 2, false, true, true,
790 handle_assume_aligned_attribute, false },
791 { "designated_init", 0, 0, false, true, false,
792 handle_designated_init_attribute, false },
793 { NULL, 0, 0, false, false, false, NULL, false }
796 /* Give the specifications for the format attributes, used by C and all
797 descendants. */
799 const struct attribute_spec c_common_format_attribute_table[] =
801 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
802 affects_type_identity } */
803 { "format", 3, 3, false, true, true,
804 handle_format_attribute, false },
805 { "format_arg", 1, 1, false, true, true,
806 handle_format_arg_attribute, false },
807 { NULL, 0, 0, false, false, false, NULL, false }
810 /* Return identifier for address space AS. */
812 const char *
813 c_addr_space_name (addr_space_t as)
815 int rid = RID_FIRST_ADDR_SPACE + as;
816 gcc_assert (ridpointers [rid]);
817 return IDENTIFIER_POINTER (ridpointers [rid]);
820 /* Push current bindings for the function name VAR_DECLS. */
822 void
823 start_fname_decls (void)
825 unsigned ix;
826 tree saved = NULL_TREE;
828 for (ix = 0; fname_vars[ix].decl; ix++)
830 tree decl = *fname_vars[ix].decl;
832 if (decl)
834 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
835 saved);
836 *fname_vars[ix].decl = NULL_TREE;
839 if (saved || saved_function_name_decls)
840 /* Normally they'll have been NULL, so only push if we've got a
841 stack, or they are non-NULL. */
842 saved_function_name_decls = tree_cons (saved, NULL_TREE,
843 saved_function_name_decls);
846 /* Finish up the current bindings, adding them into the current function's
847 statement tree. This must be done _before_ finish_stmt_tree is called.
848 If there is no current function, we must be at file scope and no statements
849 are involved. Pop the previous bindings. */
851 void
852 finish_fname_decls (void)
854 unsigned ix;
855 tree stmts = NULL_TREE;
856 tree stack = saved_function_name_decls;
858 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
859 append_to_statement_list (TREE_VALUE (stack), &stmts);
861 if (stmts)
863 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
865 if (TREE_CODE (*bodyp) == BIND_EXPR)
866 bodyp = &BIND_EXPR_BODY (*bodyp);
868 append_to_statement_list_force (*bodyp, &stmts);
869 *bodyp = stmts;
872 for (ix = 0; fname_vars[ix].decl; ix++)
873 *fname_vars[ix].decl = NULL_TREE;
875 if (stack)
877 /* We had saved values, restore them. */
878 tree saved;
880 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
882 tree decl = TREE_PURPOSE (saved);
883 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
885 *fname_vars[ix].decl = decl;
887 stack = TREE_CHAIN (stack);
889 saved_function_name_decls = stack;
892 /* Return the text name of the current function, suitably prettified
893 by PRETTY_P. Return string must be freed by caller. */
895 const char *
896 fname_as_string (int pretty_p)
898 const char *name = "top level";
899 char *namep;
900 int vrb = 2, len;
901 cpp_string cstr = { 0, 0 }, strname;
903 if (!pretty_p)
905 name = "";
906 vrb = 0;
909 if (current_function_decl)
910 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
912 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
914 namep = XNEWVEC (char, len);
915 snprintf (namep, len, "\"%s\"", name);
916 strname.text = (unsigned char *) namep;
917 strname.len = len - 1;
919 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
921 XDELETEVEC (namep);
922 return (const char *) cstr.text;
925 return namep;
928 /* Return the VAR_DECL for a const char array naming the current
929 function. If the VAR_DECL has not yet been created, create it
930 now. RID indicates how it should be formatted and IDENTIFIER_NODE
931 ID is its name (unfortunately C and C++ hold the RID values of
932 keywords in different places, so we can't derive RID from ID in
933 this language independent code. LOC is the location of the
934 function. */
936 tree
937 fname_decl (location_t loc, unsigned int rid, tree id)
939 unsigned ix;
940 tree decl = NULL_TREE;
942 for (ix = 0; fname_vars[ix].decl; ix++)
943 if (fname_vars[ix].rid == rid)
944 break;
946 decl = *fname_vars[ix].decl;
947 if (!decl)
949 /* If a tree is built here, it would normally have the lineno of
950 the current statement. Later this tree will be moved to the
951 beginning of the function and this line number will be wrong.
952 To avoid this problem set the lineno to 0 here; that prevents
953 it from appearing in the RTL. */
954 tree stmts;
955 location_t saved_location = input_location;
956 input_location = UNKNOWN_LOCATION;
958 stmts = push_stmt_list ();
959 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
960 stmts = pop_stmt_list (stmts);
961 if (!IS_EMPTY_STMT (stmts))
962 saved_function_name_decls
963 = tree_cons (decl, stmts, saved_function_name_decls);
964 *fname_vars[ix].decl = decl;
965 input_location = saved_location;
967 if (!ix && !current_function_decl)
968 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
970 return decl;
973 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
975 tree
976 fix_string_type (tree value)
978 int length = TREE_STRING_LENGTH (value);
979 int nchars;
980 tree e_type, i_type, a_type;
982 /* Compute the number of elements, for the array type. */
983 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
985 nchars = length;
986 e_type = char_type_node;
988 else if (TREE_TYPE (value) == char16_array_type_node)
990 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
991 e_type = char16_type_node;
993 else if (TREE_TYPE (value) == char32_array_type_node)
995 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
996 e_type = char32_type_node;
998 else
1000 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1001 e_type = wchar_type_node;
1004 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1005 limit in C++98 Annex B is very large (65536) and is not normative,
1006 so we do not diagnose it (warn_overlength_strings is forced off
1007 in c_common_post_options). */
1008 if (warn_overlength_strings)
1010 const int nchars_max = flag_isoc99 ? 4095 : 509;
1011 const int relevant_std = flag_isoc99 ? 99 : 90;
1012 if (nchars - 1 > nchars_max)
1013 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1014 separate the %d from the 'C'. 'ISO' should not be
1015 translated, but it may be moved after 'C%d' in languages
1016 where modifiers follow nouns. */
1017 pedwarn (input_location, OPT_Woverlength_strings,
1018 "string length %qd is greater than the length %qd "
1019 "ISO C%d compilers are required to support",
1020 nchars - 1, nchars_max, relevant_std);
1023 /* Create the array type for the string constant. The ISO C++
1024 standard says that a string literal has type `const char[N]' or
1025 `const wchar_t[N]'. We use the same logic when invoked as a C
1026 front-end with -Wwrite-strings.
1027 ??? We should change the type of an expression depending on the
1028 state of a warning flag. We should just be warning -- see how
1029 this is handled in the C++ front-end for the deprecated implicit
1030 conversion from string literals to `char*' or `wchar_t*'.
1032 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1033 array type being the unqualified version of that type.
1034 Therefore, if we are constructing an array of const char, we must
1035 construct the matching unqualified array type first. The C front
1036 end does not require this, but it does no harm, so we do it
1037 unconditionally. */
1038 i_type = build_index_type (size_int (nchars - 1));
1039 a_type = build_array_type (e_type, i_type);
1040 if (c_dialect_cxx() || warn_write_strings)
1041 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1043 TREE_TYPE (value) = a_type;
1044 TREE_CONSTANT (value) = 1;
1045 TREE_READONLY (value) = 1;
1046 TREE_STATIC (value) = 1;
1047 return value;
1050 /* If DISABLE is true, stop issuing warnings. This is used when
1051 parsing code that we know will not be executed. This function may
1052 be called multiple times, and works as a stack. */
1054 static void
1055 c_disable_warnings (bool disable)
1057 if (disable)
1059 ++c_inhibit_evaluation_warnings;
1060 fold_defer_overflow_warnings ();
1064 /* If ENABLE is true, reenable issuing warnings. */
1066 static void
1067 c_enable_warnings (bool enable)
1069 if (enable)
1071 --c_inhibit_evaluation_warnings;
1072 fold_undefer_and_ignore_overflow_warnings ();
1076 /* Fully fold EXPR, an expression that was not folded (beyond integer
1077 constant expressions and null pointer constants) when being built
1078 up. If IN_INIT, this is in a static initializer and certain
1079 changes are made to the folding done. Clear *MAYBE_CONST if
1080 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1081 expression because it contains an evaluated operator (in C99) or an
1082 operator outside of sizeof returning an integer constant (in C90)
1083 not permitted in constant expressions, or because it contains an
1084 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1085 set to true by callers before calling this function.) Return the
1086 folded expression. Function arguments have already been folded
1087 before calling this function, as have the contents of SAVE_EXPR,
1088 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1089 C_MAYBE_CONST_EXPR. */
1091 tree
1092 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1094 tree ret;
1095 tree eptype = NULL_TREE;
1096 bool dummy = true;
1097 bool maybe_const_itself = true;
1098 location_t loc = EXPR_LOCATION (expr);
1100 /* This function is not relevant to C++ because C++ folds while
1101 parsing, and may need changes to be correct for C++ when C++
1102 stops folding while parsing. */
1103 if (c_dialect_cxx ())
1104 gcc_unreachable ();
1106 if (!maybe_const)
1107 maybe_const = &dummy;
1108 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1110 eptype = TREE_TYPE (expr);
1111 expr = TREE_OPERAND (expr, 0);
1113 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1114 &maybe_const_itself);
1115 if (eptype)
1116 ret = fold_convert_loc (loc, eptype, ret);
1117 *maybe_const &= maybe_const_itself;
1118 return ret;
1121 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1122 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1123 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1124 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1125 both evaluated and unevaluated subexpressions while
1126 *MAYBE_CONST_ITSELF is carried from only evaluated
1127 subexpressions). */
1129 static tree
1130 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1131 bool *maybe_const_itself)
1133 tree ret = expr;
1134 enum tree_code code = TREE_CODE (expr);
1135 enum tree_code_class kind = TREE_CODE_CLASS (code);
1136 location_t loc = EXPR_LOCATION (expr);
1137 tree op0, op1, op2, op3;
1138 tree orig_op0, orig_op1, orig_op2;
1139 bool op0_const = true, op1_const = true, op2_const = true;
1140 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1141 bool nowarning = TREE_NO_WARNING (expr);
1142 bool unused_p;
1144 /* This function is not relevant to C++ because C++ folds while
1145 parsing, and may need changes to be correct for C++ when C++
1146 stops folding while parsing. */
1147 if (c_dialect_cxx ())
1148 gcc_unreachable ();
1150 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1151 anything else not counted as an expression cannot usefully be
1152 folded further at this point. */
1153 if (!IS_EXPR_CODE_CLASS (kind)
1154 || kind == tcc_statement
1155 || code == SAVE_EXPR)
1156 return expr;
1158 /* Operands of variable-length expressions (function calls) have
1159 already been folded, as have __builtin_* function calls, and such
1160 expressions cannot occur in constant expressions. */
1161 if (kind == tcc_vl_exp)
1163 *maybe_const_operands = false;
1164 ret = fold (expr);
1165 goto out;
1168 if (code == C_MAYBE_CONST_EXPR)
1170 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1171 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1172 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1173 *maybe_const_operands = false;
1174 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1175 *maybe_const_itself = false;
1176 if (pre && !in_init)
1177 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1178 else
1179 ret = inner;
1180 goto out;
1183 /* Assignment, increment, decrement, function call and comma
1184 operators, and statement expressions, cannot occur in constant
1185 expressions if evaluated / outside of sizeof. (Function calls
1186 were handled above, though VA_ARG_EXPR is treated like a function
1187 call here, and statement expressions are handled through
1188 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1189 switch (code)
1191 case MODIFY_EXPR:
1192 case PREDECREMENT_EXPR:
1193 case PREINCREMENT_EXPR:
1194 case POSTDECREMENT_EXPR:
1195 case POSTINCREMENT_EXPR:
1196 case COMPOUND_EXPR:
1197 *maybe_const_operands = false;
1198 break;
1200 case VA_ARG_EXPR:
1201 case TARGET_EXPR:
1202 case BIND_EXPR:
1203 case OBJ_TYPE_REF:
1204 *maybe_const_operands = false;
1205 ret = fold (expr);
1206 goto out;
1208 default:
1209 break;
1212 /* Fold individual tree codes as appropriate. */
1213 switch (code)
1215 case COMPOUND_LITERAL_EXPR:
1216 /* Any non-constancy will have been marked in a containing
1217 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1218 goto out;
1220 case COMPONENT_REF:
1221 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1222 op1 = TREE_OPERAND (expr, 1);
1223 op2 = TREE_OPERAND (expr, 2);
1224 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1225 maybe_const_itself);
1226 STRIP_TYPE_NOPS (op0);
1227 if (op0 != orig_op0)
1228 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1229 if (ret != expr)
1231 TREE_READONLY (ret) = TREE_READONLY (expr);
1232 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1234 goto out;
1236 case ARRAY_REF:
1237 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1238 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1239 op2 = TREE_OPERAND (expr, 2);
1240 op3 = TREE_OPERAND (expr, 3);
1241 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1242 maybe_const_itself);
1243 STRIP_TYPE_NOPS (op0);
1244 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1245 maybe_const_itself);
1246 STRIP_TYPE_NOPS (op1);
1247 op1 = decl_constant_value_for_optimization (op1);
1248 if (op0 != orig_op0 || op1 != orig_op1)
1249 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1250 if (ret != expr)
1252 TREE_READONLY (ret) = TREE_READONLY (expr);
1253 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1254 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1256 ret = fold (ret);
1257 goto out;
1259 case COMPOUND_EXPR:
1260 case MODIFY_EXPR:
1261 case PREDECREMENT_EXPR:
1262 case PREINCREMENT_EXPR:
1263 case POSTDECREMENT_EXPR:
1264 case POSTINCREMENT_EXPR:
1265 case PLUS_EXPR:
1266 case MINUS_EXPR:
1267 case MULT_EXPR:
1268 case POINTER_PLUS_EXPR:
1269 case TRUNC_DIV_EXPR:
1270 case CEIL_DIV_EXPR:
1271 case FLOOR_DIV_EXPR:
1272 case TRUNC_MOD_EXPR:
1273 case RDIV_EXPR:
1274 case EXACT_DIV_EXPR:
1275 case LSHIFT_EXPR:
1276 case RSHIFT_EXPR:
1277 case BIT_IOR_EXPR:
1278 case BIT_XOR_EXPR:
1279 case BIT_AND_EXPR:
1280 case LT_EXPR:
1281 case LE_EXPR:
1282 case GT_EXPR:
1283 case GE_EXPR:
1284 case EQ_EXPR:
1285 case NE_EXPR:
1286 case COMPLEX_EXPR:
1287 case TRUTH_AND_EXPR:
1288 case TRUTH_OR_EXPR:
1289 case TRUTH_XOR_EXPR:
1290 case UNORDERED_EXPR:
1291 case ORDERED_EXPR:
1292 case UNLT_EXPR:
1293 case UNLE_EXPR:
1294 case UNGT_EXPR:
1295 case UNGE_EXPR:
1296 case UNEQ_EXPR:
1297 /* Binary operations evaluating both arguments (increment and
1298 decrement are binary internally in GCC). */
1299 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1300 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1301 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1302 maybe_const_itself);
1303 STRIP_TYPE_NOPS (op0);
1304 if (code != MODIFY_EXPR
1305 && code != PREDECREMENT_EXPR
1306 && code != PREINCREMENT_EXPR
1307 && code != POSTDECREMENT_EXPR
1308 && code != POSTINCREMENT_EXPR)
1309 op0 = decl_constant_value_for_optimization (op0);
1310 /* The RHS of a MODIFY_EXPR was fully folded when building that
1311 expression for the sake of conversion warnings. */
1312 if (code != MODIFY_EXPR)
1313 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1314 maybe_const_itself);
1315 STRIP_TYPE_NOPS (op1);
1316 op1 = decl_constant_value_for_optimization (op1);
1317 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1318 ret = in_init
1319 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1320 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1321 else
1322 ret = fold (expr);
1323 if (TREE_OVERFLOW_P (ret)
1324 && !TREE_OVERFLOW_P (op0)
1325 && !TREE_OVERFLOW_P (op1))
1326 overflow_warning (EXPR_LOCATION (expr), ret);
1327 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1328 && TREE_CODE (orig_op1) != INTEGER_CST
1329 && TREE_CODE (op1) == INTEGER_CST
1330 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1331 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1332 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1333 && c_inhibit_evaluation_warnings == 0)
1335 if (tree_int_cst_sgn (op1) < 0)
1336 warning_at (loc, 0, (code == LSHIFT_EXPR
1337 ? G_("left shift count is negative")
1338 : G_("right shift count is negative")));
1339 else if (compare_tree_int (op1,
1340 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1341 >= 0)
1342 warning_at (loc, 0, (code == LSHIFT_EXPR
1343 ? G_("left shift count >= width of type")
1344 : G_("right shift count >= width of type")));
1346 goto out;
1348 case INDIRECT_REF:
1349 case FIX_TRUNC_EXPR:
1350 case FLOAT_EXPR:
1351 CASE_CONVERT:
1352 case ADDR_SPACE_CONVERT_EXPR:
1353 case VIEW_CONVERT_EXPR:
1354 case NON_LVALUE_EXPR:
1355 case NEGATE_EXPR:
1356 case BIT_NOT_EXPR:
1357 case TRUTH_NOT_EXPR:
1358 case ADDR_EXPR:
1359 case CONJ_EXPR:
1360 case REALPART_EXPR:
1361 case IMAGPART_EXPR:
1362 /* Unary operations. */
1363 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1364 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1365 maybe_const_itself);
1366 STRIP_TYPE_NOPS (op0);
1367 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1368 op0 = decl_constant_value_for_optimization (op0);
1369 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1370 not prepared to deal with them if they occur in initializers. */
1371 if (op0 != orig_op0
1372 && code == ADDR_EXPR
1373 && (op1 = get_base_address (op0)) != NULL_TREE
1374 && TREE_CODE (op1) == INDIRECT_REF
1375 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1376 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1377 else if (op0 != orig_op0 || in_init)
1378 ret = in_init
1379 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1380 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1381 else
1382 ret = fold (expr);
1383 if (code == INDIRECT_REF
1384 && ret != expr
1385 && TREE_CODE (ret) == INDIRECT_REF)
1387 TREE_READONLY (ret) = TREE_READONLY (expr);
1388 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1389 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1391 switch (code)
1393 case FIX_TRUNC_EXPR:
1394 case FLOAT_EXPR:
1395 CASE_CONVERT:
1396 /* Don't warn about explicit conversions. We will already
1397 have warned about suspect implicit conversions. */
1398 break;
1400 default:
1401 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1402 overflow_warning (EXPR_LOCATION (expr), ret);
1403 break;
1405 goto out;
1407 case TRUTH_ANDIF_EXPR:
1408 case TRUTH_ORIF_EXPR:
1409 /* Binary operations not necessarily evaluating both
1410 arguments. */
1411 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1412 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1413 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1414 STRIP_TYPE_NOPS (op0);
1416 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1417 ? truthvalue_false_node
1418 : truthvalue_true_node));
1419 c_disable_warnings (unused_p);
1420 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1421 STRIP_TYPE_NOPS (op1);
1422 c_enable_warnings (unused_p);
1424 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1425 ret = in_init
1426 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1427 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1428 else
1429 ret = fold (expr);
1430 *maybe_const_operands &= op0_const;
1431 *maybe_const_itself &= op0_const_self;
1432 if (!(flag_isoc99
1433 && op0_const
1434 && op0_const_self
1435 && (code == TRUTH_ANDIF_EXPR
1436 ? op0 == truthvalue_false_node
1437 : op0 == truthvalue_true_node)))
1438 *maybe_const_operands &= op1_const;
1439 if (!(op0_const
1440 && op0_const_self
1441 && (code == TRUTH_ANDIF_EXPR
1442 ? op0 == truthvalue_false_node
1443 : op0 == truthvalue_true_node)))
1444 *maybe_const_itself &= op1_const_self;
1445 goto out;
1447 case COND_EXPR:
1448 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1449 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1450 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1451 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1453 STRIP_TYPE_NOPS (op0);
1454 c_disable_warnings (op0 == truthvalue_false_node);
1455 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1456 STRIP_TYPE_NOPS (op1);
1457 c_enable_warnings (op0 == truthvalue_false_node);
1459 c_disable_warnings (op0 == truthvalue_true_node);
1460 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1461 STRIP_TYPE_NOPS (op2);
1462 c_enable_warnings (op0 == truthvalue_true_node);
1464 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1465 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1466 else
1467 ret = fold (expr);
1468 *maybe_const_operands &= op0_const;
1469 *maybe_const_itself &= op0_const_self;
1470 if (!(flag_isoc99
1471 && op0_const
1472 && op0_const_self
1473 && op0 == truthvalue_false_node))
1474 *maybe_const_operands &= op1_const;
1475 if (!(op0_const
1476 && op0_const_self
1477 && op0 == truthvalue_false_node))
1478 *maybe_const_itself &= op1_const_self;
1479 if (!(flag_isoc99
1480 && op0_const
1481 && op0_const_self
1482 && op0 == truthvalue_true_node))
1483 *maybe_const_operands &= op2_const;
1484 if (!(op0_const
1485 && op0_const_self
1486 && op0 == truthvalue_true_node))
1487 *maybe_const_itself &= op2_const_self;
1488 goto out;
1490 case EXCESS_PRECISION_EXPR:
1491 /* Each case where an operand with excess precision may be
1492 encountered must remove the EXCESS_PRECISION_EXPR around
1493 inner operands and possibly put one around the whole
1494 expression or possibly convert to the semantic type (which
1495 c_fully_fold does); we cannot tell at this stage which is
1496 appropriate in any particular case. */
1497 gcc_unreachable ();
1499 default:
1500 /* Various codes may appear through folding built-in functions
1501 and their arguments. */
1502 goto out;
1505 out:
1506 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1507 have been done by this point, so remove them again. */
1508 nowarning |= TREE_NO_WARNING (ret);
1509 STRIP_TYPE_NOPS (ret);
1510 if (nowarning && !TREE_NO_WARNING (ret))
1512 if (!CAN_HAVE_LOCATION_P (ret))
1513 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1514 TREE_NO_WARNING (ret) = 1;
1516 if (ret != expr)
1517 protected_set_expr_location (ret, loc);
1518 return ret;
1521 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1522 return EXP. Otherwise, return either EXP or its known constant
1523 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1524 Is the BLKmode test appropriate? */
1526 tree
1527 decl_constant_value_for_optimization (tree exp)
1529 tree ret;
1531 /* This function is only used by C, for c_fully_fold and other
1532 optimization, and may not be correct for C++. */
1533 if (c_dialect_cxx ())
1534 gcc_unreachable ();
1536 if (!optimize
1537 || TREE_CODE (exp) != VAR_DECL
1538 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1539 || DECL_MODE (exp) == BLKmode)
1540 return exp;
1542 ret = decl_constant_value (exp);
1543 /* Avoid unwanted tree sharing between the initializer and current
1544 function's body where the tree can be modified e.g. by the
1545 gimplifier. */
1546 if (ret != exp && TREE_STATIC (exp))
1547 ret = unshare_expr (ret);
1548 return ret;
1551 /* Print a warning if a constant expression had overflow in folding.
1552 Invoke this function on every expression that the language
1553 requires to be a constant expression.
1554 Note the ANSI C standard says it is erroneous for a
1555 constant expression to overflow. */
1557 void
1558 constant_expression_warning (tree value)
1560 if (warn_overflow && pedantic
1561 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1562 || TREE_CODE (value) == FIXED_CST
1563 || TREE_CODE (value) == VECTOR_CST
1564 || TREE_CODE (value) == COMPLEX_CST)
1565 && TREE_OVERFLOW (value))
1566 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1569 /* The same as above but print an unconditional error. */
1570 void
1571 constant_expression_error (tree value)
1573 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1574 || TREE_CODE (value) == FIXED_CST
1575 || TREE_CODE (value) == VECTOR_CST
1576 || TREE_CODE (value) == COMPLEX_CST)
1577 && TREE_OVERFLOW (value))
1578 error ("overflow in constant expression");
1581 /* Print a warning if an expression had overflow in folding and its
1582 operands hadn't.
1584 Invoke this function on every expression that
1585 (1) appears in the source code, and
1586 (2) is a constant expression that overflowed, and
1587 (3) is not already checked by convert_and_check;
1588 however, do not invoke this function on operands of explicit casts
1589 or when the expression is the result of an operator and any operand
1590 already overflowed. */
1592 void
1593 overflow_warning (location_t loc, tree value)
1595 if (c_inhibit_evaluation_warnings != 0)
1596 return;
1598 switch (TREE_CODE (value))
1600 case INTEGER_CST:
1601 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1602 break;
1604 case REAL_CST:
1605 warning_at (loc, OPT_Woverflow,
1606 "floating point overflow in expression");
1607 break;
1609 case FIXED_CST:
1610 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1611 break;
1613 case VECTOR_CST:
1614 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1615 break;
1617 case COMPLEX_CST:
1618 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1619 warning_at (loc, OPT_Woverflow,
1620 "complex integer overflow in expression");
1621 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1622 warning_at (loc, OPT_Woverflow,
1623 "complex floating point overflow in expression");
1624 break;
1626 default:
1627 break;
1631 /* Warn about uses of logical || / && operator in a context where it
1632 is likely that the bitwise equivalent was intended by the
1633 programmer. We have seen an expression in which CODE is a binary
1634 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1635 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1636 void
1637 warn_logical_operator (location_t location, enum tree_code code, tree type,
1638 enum tree_code code_left, tree op_left,
1639 enum tree_code ARG_UNUSED (code_right), tree op_right)
1641 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1642 int in0_p, in1_p, in_p;
1643 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1644 bool strict_overflow_p = false;
1646 if (code != TRUTH_ANDIF_EXPR
1647 && code != TRUTH_AND_EXPR
1648 && code != TRUTH_ORIF_EXPR
1649 && code != TRUTH_OR_EXPR)
1650 return;
1652 /* Warn if &&/|| are being used in a context where it is
1653 likely that the bitwise equivalent was intended by the
1654 programmer. That is, an expression such as op && MASK
1655 where op should not be any boolean expression, nor a
1656 constant, and mask seems to be a non-boolean integer constant. */
1657 if (!truth_value_p (code_left)
1658 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1659 && !CONSTANT_CLASS_P (op_left)
1660 && !TREE_NO_WARNING (op_left)
1661 && TREE_CODE (op_right) == INTEGER_CST
1662 && !integer_zerop (op_right)
1663 && !integer_onep (op_right))
1665 if (or_op)
1666 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1667 " applied to non-boolean constant");
1668 else
1669 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1670 " applied to non-boolean constant");
1671 TREE_NO_WARNING (op_left) = true;
1672 return;
1675 /* We do not warn for constants because they are typical of macro
1676 expansions that test for features. */
1677 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1678 return;
1680 /* This warning only makes sense with logical operands. */
1681 if (!(truth_value_p (TREE_CODE (op_left))
1682 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1683 || !(truth_value_p (TREE_CODE (op_right))
1684 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1685 return;
1687 /* The range computations only work with scalars. */
1688 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1689 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1690 return;
1692 /* We first test whether either side separately is trivially true
1693 (with OR) or trivially false (with AND). If so, do not warn.
1694 This is a common idiom for testing ranges of data types in
1695 portable code. */
1696 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1697 if (!lhs)
1698 return;
1699 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1700 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1702 /* If this is an OR operation, invert both sides; now, the result
1703 should be always false to get a warning. */
1704 if (or_op)
1705 in0_p = !in0_p;
1707 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1708 if (tem && integer_zerop (tem))
1709 return;
1711 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1712 if (!rhs)
1713 return;
1714 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1715 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1717 /* If this is an OR operation, invert both sides; now, the result
1718 should be always false to get a warning. */
1719 if (or_op)
1720 in1_p = !in1_p;
1722 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1723 if (tem && integer_zerop (tem))
1724 return;
1726 /* If both expressions have the same operand, if we can merge the
1727 ranges, and if the range test is always false, then warn. */
1728 if (operand_equal_p (lhs, rhs, 0)
1729 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1730 in1_p, low1, high1)
1731 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1732 type, lhs, in_p, low, high))
1733 && integer_zerop (tem))
1735 if (or_op)
1736 warning_at (location, OPT_Wlogical_op,
1737 "logical %<or%> "
1738 "of collectively exhaustive tests is always true");
1739 else
1740 warning_at (location, OPT_Wlogical_op,
1741 "logical %<and%> "
1742 "of mutually exclusive tests is always false");
1746 /* Warn about logical not used on the left hand side operand of a comparison.
1747 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1748 Do not warn if RHS is of a boolean type. */
1750 void
1751 warn_logical_not_parentheses (location_t location, enum tree_code code,
1752 tree rhs)
1754 if (TREE_CODE_CLASS (code) != tcc_comparison
1755 || TREE_TYPE (rhs) == NULL_TREE
1756 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1757 return;
1759 warning_at (location, OPT_Wlogical_not_parentheses,
1760 "logical not is only applied to the left hand side of "
1761 "comparison");
1764 /* Warn if EXP contains any computations whose results are not used.
1765 Return true if a warning is printed; false otherwise. LOCUS is the
1766 (potential) location of the expression. */
1768 bool
1769 warn_if_unused_value (const_tree exp, location_t locus)
1771 restart:
1772 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1773 return false;
1775 /* Don't warn about void constructs. This includes casting to void,
1776 void function calls, and statement expressions with a final cast
1777 to void. */
1778 if (VOID_TYPE_P (TREE_TYPE (exp)))
1779 return false;
1781 if (EXPR_HAS_LOCATION (exp))
1782 locus = EXPR_LOCATION (exp);
1784 switch (TREE_CODE (exp))
1786 case PREINCREMENT_EXPR:
1787 case POSTINCREMENT_EXPR:
1788 case PREDECREMENT_EXPR:
1789 case POSTDECREMENT_EXPR:
1790 case MODIFY_EXPR:
1791 case INIT_EXPR:
1792 case TARGET_EXPR:
1793 case CALL_EXPR:
1794 case TRY_CATCH_EXPR:
1795 case WITH_CLEANUP_EXPR:
1796 case EXIT_EXPR:
1797 case VA_ARG_EXPR:
1798 return false;
1800 case BIND_EXPR:
1801 /* For a binding, warn if no side effect within it. */
1802 exp = BIND_EXPR_BODY (exp);
1803 goto restart;
1805 case SAVE_EXPR:
1806 case NON_LVALUE_EXPR:
1807 case NOP_EXPR:
1808 exp = TREE_OPERAND (exp, 0);
1809 goto restart;
1811 case TRUTH_ORIF_EXPR:
1812 case TRUTH_ANDIF_EXPR:
1813 /* In && or ||, warn if 2nd operand has no side effect. */
1814 exp = TREE_OPERAND (exp, 1);
1815 goto restart;
1817 case COMPOUND_EXPR:
1818 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1819 return true;
1820 /* Let people do `(foo (), 0)' without a warning. */
1821 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1822 return false;
1823 exp = TREE_OPERAND (exp, 1);
1824 goto restart;
1826 case COND_EXPR:
1827 /* If this is an expression with side effects, don't warn; this
1828 case commonly appears in macro expansions. */
1829 if (TREE_SIDE_EFFECTS (exp))
1830 return false;
1831 goto warn;
1833 case INDIRECT_REF:
1834 /* Don't warn about automatic dereferencing of references, since
1835 the user cannot control it. */
1836 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1838 exp = TREE_OPERAND (exp, 0);
1839 goto restart;
1841 /* Fall through. */
1843 default:
1844 /* Referencing a volatile value is a side effect, so don't warn. */
1845 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1846 && TREE_THIS_VOLATILE (exp))
1847 return false;
1849 /* If this is an expression which has no operands, there is no value
1850 to be unused. There are no such language-independent codes,
1851 but front ends may define such. */
1852 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1853 return false;
1855 warn:
1856 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1861 /* Print a warning about casts that might indicate violation
1862 of strict aliasing rules if -Wstrict-aliasing is used and
1863 strict aliasing mode is in effect. OTYPE is the original
1864 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1866 bool
1867 strict_aliasing_warning (tree otype, tree type, tree expr)
1869 /* Strip pointer conversion chains and get to the correct original type. */
1870 STRIP_NOPS (expr);
1871 otype = TREE_TYPE (expr);
1873 if (!(flag_strict_aliasing
1874 && POINTER_TYPE_P (type)
1875 && POINTER_TYPE_P (otype)
1876 && !VOID_TYPE_P (TREE_TYPE (type)))
1877 /* If the type we are casting to is a ref-all pointer
1878 dereferencing it is always valid. */
1879 || TYPE_REF_CAN_ALIAS_ALL (type))
1880 return false;
1882 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1883 && (DECL_P (TREE_OPERAND (expr, 0))
1884 || handled_component_p (TREE_OPERAND (expr, 0))))
1886 /* Casting the address of an object to non void pointer. Warn
1887 if the cast breaks type based aliasing. */
1888 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1890 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1891 "might break strict-aliasing rules");
1892 return true;
1894 else
1896 /* warn_strict_aliasing >= 3. This includes the default (3).
1897 Only warn if the cast is dereferenced immediately. */
1898 alias_set_type set1 =
1899 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1900 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1902 if (set1 != set2 && set2 != 0
1903 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1905 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1906 "pointer will break strict-aliasing rules");
1907 return true;
1909 else if (warn_strict_aliasing == 2
1910 && !alias_sets_must_conflict_p (set1, set2))
1912 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1913 "pointer might break strict-aliasing rules");
1914 return true;
1918 else
1919 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1921 /* At this level, warn for any conversions, even if an address is
1922 not taken in the same statement. This will likely produce many
1923 false positives, but could be useful to pinpoint problems that
1924 are not revealed at higher levels. */
1925 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1926 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1927 if (!COMPLETE_TYPE_P (type)
1928 || !alias_sets_must_conflict_p (set1, set2))
1930 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1931 "pointer might break strict-aliasing rules");
1932 return true;
1936 return false;
1939 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1940 sizeof as last operand of certain builtins. */
1942 void
1943 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1944 vec<tree, va_gc> *params, tree *sizeof_arg,
1945 bool (*comp_types) (tree, tree))
1947 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1948 bool strop = false, cmp = false;
1949 unsigned int idx = ~0;
1950 location_t loc;
1952 if (TREE_CODE (callee) != FUNCTION_DECL
1953 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1954 || vec_safe_length (params) <= 1)
1955 return;
1957 switch (DECL_FUNCTION_CODE (callee))
1959 case BUILT_IN_STRNCMP:
1960 case BUILT_IN_STRNCASECMP:
1961 cmp = true;
1962 /* FALLTHRU */
1963 case BUILT_IN_STRNCPY:
1964 case BUILT_IN_STRNCPY_CHK:
1965 case BUILT_IN_STRNCAT:
1966 case BUILT_IN_STRNCAT_CHK:
1967 case BUILT_IN_STPNCPY:
1968 case BUILT_IN_STPNCPY_CHK:
1969 strop = true;
1970 /* FALLTHRU */
1971 case BUILT_IN_MEMCPY:
1972 case BUILT_IN_MEMCPY_CHK:
1973 case BUILT_IN_MEMMOVE:
1974 case BUILT_IN_MEMMOVE_CHK:
1975 if (params->length () < 3)
1976 return;
1977 src = (*params)[1];
1978 dest = (*params)[0];
1979 idx = 2;
1980 break;
1981 case BUILT_IN_BCOPY:
1982 if (params->length () < 3)
1983 return;
1984 src = (*params)[0];
1985 dest = (*params)[1];
1986 idx = 2;
1987 break;
1988 case BUILT_IN_MEMCMP:
1989 case BUILT_IN_BCMP:
1990 if (params->length () < 3)
1991 return;
1992 src = (*params)[1];
1993 dest = (*params)[0];
1994 idx = 2;
1995 cmp = true;
1996 break;
1997 case BUILT_IN_MEMSET:
1998 case BUILT_IN_MEMSET_CHK:
1999 if (params->length () < 3)
2000 return;
2001 dest = (*params)[0];
2002 idx = 2;
2003 break;
2004 case BUILT_IN_BZERO:
2005 dest = (*params)[0];
2006 idx = 1;
2007 break;
2008 case BUILT_IN_STRNDUP:
2009 src = (*params)[0];
2010 strop = true;
2011 idx = 1;
2012 break;
2013 case BUILT_IN_MEMCHR:
2014 if (params->length () < 3)
2015 return;
2016 src = (*params)[0];
2017 idx = 2;
2018 break;
2019 case BUILT_IN_SNPRINTF:
2020 case BUILT_IN_SNPRINTF_CHK:
2021 case BUILT_IN_VSNPRINTF:
2022 case BUILT_IN_VSNPRINTF_CHK:
2023 dest = (*params)[0];
2024 idx = 1;
2025 strop = true;
2026 break;
2027 default:
2028 break;
2031 if (idx >= 3)
2032 return;
2034 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2035 return;
2037 type = TYPE_P (sizeof_arg[idx])
2038 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2039 if (!POINTER_TYPE_P (type))
2040 return;
2042 if (dest
2043 && (tem = tree_strip_nop_conversions (dest))
2044 && POINTER_TYPE_P (TREE_TYPE (tem))
2045 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2046 return;
2048 if (src
2049 && (tem = tree_strip_nop_conversions (src))
2050 && POINTER_TYPE_P (TREE_TYPE (tem))
2051 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2052 return;
2054 loc = sizeof_arg_loc[idx];
2056 if (dest && !cmp)
2058 if (!TYPE_P (sizeof_arg[idx])
2059 && operand_equal_p (dest, sizeof_arg[idx], 0)
2060 && comp_types (TREE_TYPE (dest), type))
2062 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2063 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2064 "argument to %<sizeof%> in %qD call is the same "
2065 "expression as the destination; did you mean to "
2066 "remove the addressof?", callee);
2067 else if ((TYPE_PRECISION (TREE_TYPE (type))
2068 == TYPE_PRECISION (char_type_node))
2069 || strop)
2070 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2071 "argument to %<sizeof%> in %qD call is the same "
2072 "expression as the destination; did you mean to "
2073 "provide an explicit length?", callee);
2074 else
2075 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2076 "argument to %<sizeof%> in %qD call is the same "
2077 "expression as the destination; did you mean to "
2078 "dereference it?", callee);
2079 return;
2082 if (POINTER_TYPE_P (TREE_TYPE (dest))
2083 && !strop
2084 && comp_types (TREE_TYPE (dest), type)
2085 && !VOID_TYPE_P (TREE_TYPE (type)))
2087 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2088 "argument to %<sizeof%> in %qD call is the same "
2089 "pointer type %qT as the destination; expected %qT "
2090 "or an explicit length", callee, TREE_TYPE (dest),
2091 TREE_TYPE (TREE_TYPE (dest)));
2092 return;
2096 if (src && !cmp)
2098 if (!TYPE_P (sizeof_arg[idx])
2099 && operand_equal_p (src, sizeof_arg[idx], 0)
2100 && comp_types (TREE_TYPE (src), type))
2102 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2103 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2104 "argument to %<sizeof%> in %qD call is the same "
2105 "expression as the source; did you mean to "
2106 "remove the addressof?", callee);
2107 else if ((TYPE_PRECISION (TREE_TYPE (type))
2108 == TYPE_PRECISION (char_type_node))
2109 || strop)
2110 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2111 "argument to %<sizeof%> in %qD call is the same "
2112 "expression as the source; did you mean to "
2113 "provide an explicit length?", callee);
2114 else
2115 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2116 "argument to %<sizeof%> in %qD call is the same "
2117 "expression as the source; did you mean to "
2118 "dereference it?", callee);
2119 return;
2122 if (POINTER_TYPE_P (TREE_TYPE (src))
2123 && !strop
2124 && comp_types (TREE_TYPE (src), type)
2125 && !VOID_TYPE_P (TREE_TYPE (type)))
2127 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2128 "argument to %<sizeof%> in %qD call is the same "
2129 "pointer type %qT as the source; expected %qT "
2130 "or an explicit length", callee, TREE_TYPE (src),
2131 TREE_TYPE (TREE_TYPE (src)));
2132 return;
2136 if (dest)
2138 if (!TYPE_P (sizeof_arg[idx])
2139 && operand_equal_p (dest, sizeof_arg[idx], 0)
2140 && comp_types (TREE_TYPE (dest), type))
2142 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2143 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2144 "argument to %<sizeof%> in %qD call is the same "
2145 "expression as the first source; did you mean to "
2146 "remove the addressof?", callee);
2147 else if ((TYPE_PRECISION (TREE_TYPE (type))
2148 == TYPE_PRECISION (char_type_node))
2149 || strop)
2150 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2151 "argument to %<sizeof%> in %qD call is the same "
2152 "expression as the first source; did you mean to "
2153 "provide an explicit length?", callee);
2154 else
2155 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2156 "argument to %<sizeof%> in %qD call is the same "
2157 "expression as the first source; did you mean to "
2158 "dereference it?", callee);
2159 return;
2162 if (POINTER_TYPE_P (TREE_TYPE (dest))
2163 && !strop
2164 && comp_types (TREE_TYPE (dest), type)
2165 && !VOID_TYPE_P (TREE_TYPE (type)))
2167 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2168 "argument to %<sizeof%> in %qD call is the same "
2169 "pointer type %qT as the first source; expected %qT "
2170 "or an explicit length", callee, TREE_TYPE (dest),
2171 TREE_TYPE (TREE_TYPE (dest)));
2172 return;
2176 if (src)
2178 if (!TYPE_P (sizeof_arg[idx])
2179 && operand_equal_p (src, sizeof_arg[idx], 0)
2180 && comp_types (TREE_TYPE (src), type))
2182 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2183 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2184 "argument to %<sizeof%> in %qD call is the same "
2185 "expression as the second source; did you mean to "
2186 "remove the addressof?", callee);
2187 else if ((TYPE_PRECISION (TREE_TYPE (type))
2188 == TYPE_PRECISION (char_type_node))
2189 || strop)
2190 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2191 "argument to %<sizeof%> in %qD call is the same "
2192 "expression as the second source; did you mean to "
2193 "provide an explicit length?", callee);
2194 else
2195 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2196 "argument to %<sizeof%> in %qD call is the same "
2197 "expression as the second source; did you mean to "
2198 "dereference it?", callee);
2199 return;
2202 if (POINTER_TYPE_P (TREE_TYPE (src))
2203 && !strop
2204 && comp_types (TREE_TYPE (src), type)
2205 && !VOID_TYPE_P (TREE_TYPE (type)))
2207 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2208 "argument to %<sizeof%> in %qD call is the same "
2209 "pointer type %qT as the second source; expected %qT "
2210 "or an explicit length", callee, TREE_TYPE (src),
2211 TREE_TYPE (TREE_TYPE (src)));
2212 return;
2218 /* Warn for unlikely, improbable, or stupid DECL declarations
2219 of `main'. */
2221 void
2222 check_main_parameter_types (tree decl)
2224 function_args_iterator iter;
2225 tree type;
2226 int argct = 0;
2228 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2230 /* XXX void_type_node belies the abstraction. */
2231 if (type == void_type_node || type == error_mark_node )
2232 break;
2234 tree t = type;
2235 if (TYPE_ATOMIC (t))
2236 pedwarn (input_location, OPT_Wmain,
2237 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2238 type, decl);
2239 while (POINTER_TYPE_P (t))
2241 t = TREE_TYPE (t);
2242 if (TYPE_ATOMIC (t))
2243 pedwarn (input_location, OPT_Wmain,
2244 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2245 type, decl);
2248 ++argct;
2249 switch (argct)
2251 case 1:
2252 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2253 pedwarn (input_location, OPT_Wmain,
2254 "first argument of %q+D should be %<int%>", decl);
2255 break;
2257 case 2:
2258 if (TREE_CODE (type) != POINTER_TYPE
2259 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2260 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2261 != char_type_node))
2262 pedwarn (input_location, OPT_Wmain,
2263 "second argument of %q+D should be %<char **%>", decl);
2264 break;
2266 case 3:
2267 if (TREE_CODE (type) != POINTER_TYPE
2268 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2269 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2270 != char_type_node))
2271 pedwarn (input_location, OPT_Wmain,
2272 "third argument of %q+D should probably be "
2273 "%<char **%>", decl);
2274 break;
2278 /* It is intentional that this message does not mention the third
2279 argument because it's only mentioned in an appendix of the
2280 standard. */
2281 if (argct > 0 && (argct < 2 || argct > 3))
2282 pedwarn (input_location, OPT_Wmain,
2283 "%q+D takes only zero or two arguments", decl);
2285 if (stdarg_p (TREE_TYPE (decl)))
2286 pedwarn (input_location, OPT_Wmain,
2287 "%q+D declared as variadic function", decl);
2290 /* vector_targets_convertible_p is used for vector pointer types. The
2291 callers perform various checks that the qualifiers are satisfactory,
2292 while OTOH vector_targets_convertible_p ignores the number of elements
2293 in the vectors. That's fine with vector pointers as we can consider,
2294 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2295 and that does not require and conversion of the pointer values.
2296 In contrast, vector_types_convertible_p and
2297 vector_types_compatible_elements_p are used for vector value types. */
2298 /* True if pointers to distinct types T1 and T2 can be converted to
2299 each other without an explicit cast. Only returns true for opaque
2300 vector types. */
2301 bool
2302 vector_targets_convertible_p (const_tree t1, const_tree t2)
2304 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2305 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2306 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2307 return true;
2309 return false;
2312 /* vector_types_convertible_p is used for vector value types.
2313 It could in principle call vector_targets_convertible_p as a subroutine,
2314 but then the check for vector type would be duplicated with its callers,
2315 and also the purpose of vector_targets_convertible_p would become
2316 muddled.
2317 Where vector_types_convertible_p returns true, a conversion might still be
2318 needed to make the types match.
2319 In contrast, vector_targets_convertible_p is used for vector pointer
2320 values, and vector_types_compatible_elements_p is used specifically
2321 in the context for binary operators, as a check if use is possible without
2322 conversion. */
2323 /* True if vector types T1 and T2 can be converted to each other
2324 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2325 can only be converted with -flax-vector-conversions yet that is not
2326 in effect, emit a note telling the user about that option if such
2327 a note has not previously been emitted. */
2328 bool
2329 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2331 static bool emitted_lax_note = false;
2332 bool convertible_lax;
2334 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2335 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2336 return true;
2338 convertible_lax =
2339 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2340 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2341 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2342 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2343 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2345 if (!convertible_lax || flag_lax_vector_conversions)
2346 return convertible_lax;
2348 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2349 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2350 return true;
2352 if (emit_lax_note && !emitted_lax_note)
2354 emitted_lax_note = true;
2355 inform (input_location, "use -flax-vector-conversions to permit "
2356 "conversions between vectors with differing "
2357 "element types or numbers of subparts");
2360 return false;
2363 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2364 and have vector types, V0 has the same type as V1, and the number of
2365 elements of V0, V1, MASK is the same.
2367 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2368 called with two arguments. In this case implementation passes the
2369 first argument twice in order to share the same tree code. This fact
2370 could enable the mask-values being twice the vector length. This is
2371 an implementation accident and this semantics is not guaranteed to
2372 the user. */
2373 tree
2374 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2375 bool complain)
2377 tree ret;
2378 bool wrap = true;
2379 bool maybe_const = false;
2380 bool two_arguments = false;
2382 if (v1 == NULL_TREE)
2384 two_arguments = true;
2385 v1 = v0;
2388 if (v0 == error_mark_node || v1 == error_mark_node
2389 || mask == error_mark_node)
2390 return error_mark_node;
2392 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2393 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2395 if (complain)
2396 error_at (loc, "__builtin_shuffle last argument must "
2397 "be an integer vector");
2398 return error_mark_node;
2401 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2402 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2404 if (complain)
2405 error_at (loc, "__builtin_shuffle arguments must be vectors");
2406 return error_mark_node;
2409 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2411 if (complain)
2412 error_at (loc, "__builtin_shuffle argument vectors must be of "
2413 "the same type");
2414 return error_mark_node;
2417 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2418 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2419 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2420 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2422 if (complain)
2423 error_at (loc, "__builtin_shuffle number of elements of the "
2424 "argument vector(s) and the mask vector should "
2425 "be the same");
2426 return error_mark_node;
2429 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2430 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2432 if (complain)
2433 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2434 "must have the same size as inner type of the mask");
2435 return error_mark_node;
2438 if (!c_dialect_cxx ())
2440 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2441 v0 = c_fully_fold (v0, false, &maybe_const);
2442 wrap &= maybe_const;
2444 if (two_arguments)
2445 v1 = v0 = save_expr (v0);
2446 else
2448 v1 = c_fully_fold (v1, false, &maybe_const);
2449 wrap &= maybe_const;
2452 mask = c_fully_fold (mask, false, &maybe_const);
2453 wrap &= maybe_const;
2455 else if (two_arguments)
2456 v1 = v0 = save_expr (v0);
2458 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2460 if (!c_dialect_cxx () && !wrap)
2461 ret = c_wrap_maybe_const (ret, true);
2463 return ret;
2466 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2467 to integral type. */
2469 static tree
2470 c_common_get_narrower (tree op, int *unsignedp_ptr)
2472 op = get_narrower (op, unsignedp_ptr);
2474 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2475 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2477 /* C++0x scoped enumerations don't implicitly convert to integral
2478 type; if we stripped an explicit conversion to a larger type we
2479 need to replace it so common_type will still work. */
2480 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2481 TYPE_UNSIGNED (TREE_TYPE (op)));
2482 op = fold_convert (type, op);
2484 return op;
2487 /* This is a helper function of build_binary_op.
2489 For certain operations if both args were extended from the same
2490 smaller type, do the arithmetic in that type and then extend.
2492 BITWISE indicates a bitwise operation.
2493 For them, this optimization is safe only if
2494 both args are zero-extended or both are sign-extended.
2495 Otherwise, we might change the result.
2496 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2497 but calculated in (unsigned short) it would be (unsigned short)-1.
2499 tree
2500 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2502 int unsigned0, unsigned1;
2503 tree arg0, arg1;
2504 int uns;
2505 tree type;
2507 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2508 excessive narrowing when we call get_narrower below. For
2509 example, suppose that OP0 is of unsigned int extended
2510 from signed char and that RESULT_TYPE is long long int.
2511 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2512 like
2514 (long long int) (unsigned int) signed_char
2516 which get_narrower would narrow down to
2518 (unsigned int) signed char
2520 If we do not cast OP0 first, get_narrower would return
2521 signed_char, which is inconsistent with the case of the
2522 explicit cast. */
2523 op0 = convert (result_type, op0);
2524 op1 = convert (result_type, op1);
2526 arg0 = c_common_get_narrower (op0, &unsigned0);
2527 arg1 = c_common_get_narrower (op1, &unsigned1);
2529 /* UNS is 1 if the operation to be done is an unsigned one. */
2530 uns = TYPE_UNSIGNED (result_type);
2532 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2533 but it *requires* conversion to FINAL_TYPE. */
2535 if ((TYPE_PRECISION (TREE_TYPE (op0))
2536 == TYPE_PRECISION (TREE_TYPE (arg0)))
2537 && TREE_TYPE (op0) != result_type)
2538 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2539 if ((TYPE_PRECISION (TREE_TYPE (op1))
2540 == TYPE_PRECISION (TREE_TYPE (arg1)))
2541 && TREE_TYPE (op1) != result_type)
2542 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2544 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2546 /* For bitwise operations, signedness of nominal type
2547 does not matter. Consider only how operands were extended. */
2548 if (bitwise)
2549 uns = unsigned0;
2551 /* Note that in all three cases below we refrain from optimizing
2552 an unsigned operation on sign-extended args.
2553 That would not be valid. */
2555 /* Both args variable: if both extended in same way
2556 from same width, do it in that width.
2557 Do it unsigned if args were zero-extended. */
2558 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2559 < TYPE_PRECISION (result_type))
2560 && (TYPE_PRECISION (TREE_TYPE (arg1))
2561 == TYPE_PRECISION (TREE_TYPE (arg0)))
2562 && unsigned0 == unsigned1
2563 && (unsigned0 || !uns))
2564 return c_common_signed_or_unsigned_type
2565 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2567 else if (TREE_CODE (arg0) == INTEGER_CST
2568 && (unsigned1 || !uns)
2569 && (TYPE_PRECISION (TREE_TYPE (arg1))
2570 < TYPE_PRECISION (result_type))
2571 && (type
2572 = c_common_signed_or_unsigned_type (unsigned1,
2573 TREE_TYPE (arg1)))
2574 && !POINTER_TYPE_P (type)
2575 && int_fits_type_p (arg0, type))
2576 return type;
2578 else if (TREE_CODE (arg1) == INTEGER_CST
2579 && (unsigned0 || !uns)
2580 && (TYPE_PRECISION (TREE_TYPE (arg0))
2581 < TYPE_PRECISION (result_type))
2582 && (type
2583 = c_common_signed_or_unsigned_type (unsigned0,
2584 TREE_TYPE (arg0)))
2585 && !POINTER_TYPE_P (type)
2586 && int_fits_type_p (arg1, type))
2587 return type;
2589 return result_type;
2592 /* Checks if expression EXPR of real/integer type cannot be converted
2593 to the real/integer type TYPE. Function returns non-zero when:
2594 * EXPR is a constant which cannot be exactly converted to TYPE.
2595 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2596 for EXPR type and TYPE being both integers or both real.
2597 * EXPR is not a constant of real type and TYPE is an integer.
2598 * EXPR is not a constant of integer type which cannot be
2599 exactly converted to real type.
2600 Function allows conversions between types of different signedness and
2601 can return SAFE_CONVERSION (zero) in that case. Function can produce
2602 signedness warnings if PRODUCE_WARNS is true. */
2604 enum conversion_safety
2605 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2607 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2608 tree expr_type = TREE_TYPE (expr);
2609 loc = expansion_point_location_if_in_system_header (loc);
2611 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2613 /* Warn for real constant that is not an exact integer converted
2614 to integer type. */
2615 if (TREE_CODE (expr_type) == REAL_TYPE
2616 && TREE_CODE (type) == INTEGER_TYPE)
2618 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2619 give_warning = UNSAFE_REAL;
2621 /* Warn for an integer constant that does not fit into integer type. */
2622 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2623 && TREE_CODE (type) == INTEGER_TYPE
2624 && !int_fits_type_p (expr, type))
2626 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2627 && tree_int_cst_sgn (expr) < 0)
2629 if (produce_warns)
2630 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2631 " implicitly converted to unsigned type");
2633 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2635 if (produce_warns)
2636 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2637 " constant value to negative integer");
2639 else
2640 give_warning = UNSAFE_OTHER;
2642 else if (TREE_CODE (type) == REAL_TYPE)
2644 /* Warn for an integer constant that does not fit into real type. */
2645 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2647 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2648 if (!exact_real_truncate (TYPE_MODE (type), &a))
2649 give_warning = UNSAFE_REAL;
2651 /* Warn for a real constant that does not fit into a smaller
2652 real type. */
2653 else if (TREE_CODE (expr_type) == REAL_TYPE
2654 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2656 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2657 if (!exact_real_truncate (TYPE_MODE (type), &a))
2658 give_warning = UNSAFE_REAL;
2662 else
2664 /* Warn for real types converted to integer types. */
2665 if (TREE_CODE (expr_type) == REAL_TYPE
2666 && TREE_CODE (type) == INTEGER_TYPE)
2667 give_warning = UNSAFE_REAL;
2669 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2670 && TREE_CODE (type) == INTEGER_TYPE)
2672 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2673 expr = get_unwidened (expr, 0);
2674 expr_type = TREE_TYPE (expr);
2676 /* Don't warn for short y; short x = ((int)y & 0xff); */
2677 if (TREE_CODE (expr) == BIT_AND_EXPR
2678 || TREE_CODE (expr) == BIT_IOR_EXPR
2679 || TREE_CODE (expr) == BIT_XOR_EXPR)
2681 /* If both args were extended from a shortest type,
2682 use that type if that is safe. */
2683 expr_type = shorten_binary_op (expr_type,
2684 TREE_OPERAND (expr, 0),
2685 TREE_OPERAND (expr, 1),
2686 /* bitwise */1);
2688 if (TREE_CODE (expr) == BIT_AND_EXPR)
2690 tree op0 = TREE_OPERAND (expr, 0);
2691 tree op1 = TREE_OPERAND (expr, 1);
2692 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2693 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2695 /* If one of the operands is a non-negative constant
2696 that fits in the target type, then the type of the
2697 other operand does not matter. */
2698 if ((TREE_CODE (op0) == INTEGER_CST
2699 && int_fits_type_p (op0, c_common_signed_type (type))
2700 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2701 || (TREE_CODE (op1) == INTEGER_CST
2702 && int_fits_type_p (op1, c_common_signed_type (type))
2703 && int_fits_type_p (op1,
2704 c_common_unsigned_type (type))))
2705 return SAFE_CONVERSION;
2706 /* If constant is unsigned and fits in the target
2707 type, then the result will also fit. */
2708 else if ((TREE_CODE (op0) == INTEGER_CST
2709 && unsigned0
2710 && int_fits_type_p (op0, type))
2711 || (TREE_CODE (op1) == INTEGER_CST
2712 && unsigned1
2713 && int_fits_type_p (op1, type)))
2714 return SAFE_CONVERSION;
2717 /* Warn for integer types converted to smaller integer types. */
2718 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2719 give_warning = UNSAFE_OTHER;
2721 /* When they are the same width but different signedness,
2722 then the value may change. */
2723 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2724 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2725 /* Even when converted to a bigger type, if the type is
2726 unsigned but expr is signed, then negative values
2727 will be changed. */
2728 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2729 && produce_warns)
2730 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2731 "may change the sign of the result",
2732 type, expr_type);
2735 /* Warn for integer types converted to real types if and only if
2736 all the range of values of the integer type cannot be
2737 represented by the real type. */
2738 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2739 && TREE_CODE (type) == REAL_TYPE)
2741 tree type_low_bound, type_high_bound;
2742 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2744 /* Don't warn about char y = 0xff; float x = (int) y; */
2745 expr = get_unwidened (expr, 0);
2746 expr_type = TREE_TYPE (expr);
2748 type_low_bound = TYPE_MIN_VALUE (expr_type);
2749 type_high_bound = TYPE_MAX_VALUE (expr_type);
2750 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2751 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2753 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2754 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2755 give_warning = UNSAFE_OTHER;
2758 /* Warn for real types converted to smaller real types. */
2759 else if (TREE_CODE (expr_type) == REAL_TYPE
2760 && TREE_CODE (type) == REAL_TYPE
2761 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2762 give_warning = UNSAFE_REAL;
2765 return give_warning;
2768 /* Warns if the conversion of EXPR to TYPE may alter a value.
2769 This is a helper function for warnings_for_convert_and_check. */
2771 static void
2772 conversion_warning (location_t loc, tree type, tree expr)
2774 tree expr_type = TREE_TYPE (expr);
2775 enum conversion_safety conversion_kind;
2777 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2778 return;
2780 /* This may happen, because for LHS op= RHS we preevaluate
2781 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2782 means we could no longer see the code of the EXPR. */
2783 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2784 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2785 if (TREE_CODE (expr) == SAVE_EXPR)
2786 expr = TREE_OPERAND (expr, 0);
2788 switch (TREE_CODE (expr))
2790 case EQ_EXPR:
2791 case NE_EXPR:
2792 case LE_EXPR:
2793 case GE_EXPR:
2794 case LT_EXPR:
2795 case GT_EXPR:
2796 case TRUTH_ANDIF_EXPR:
2797 case TRUTH_ORIF_EXPR:
2798 case TRUTH_AND_EXPR:
2799 case TRUTH_OR_EXPR:
2800 case TRUTH_XOR_EXPR:
2801 case TRUTH_NOT_EXPR:
2802 /* Conversion from boolean to a signed:1 bit-field (which only
2803 can hold the values 0 and -1) doesn't lose information - but
2804 it does change the value. */
2805 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2806 warning_at (loc, OPT_Wconversion,
2807 "conversion to %qT from boolean expression", type);
2808 return;
2810 case REAL_CST:
2811 case INTEGER_CST:
2812 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2813 if (conversion_kind == UNSAFE_REAL)
2814 warning_at (loc, OPT_Wfloat_conversion,
2815 "conversion to %qT alters %qT constant value",
2816 type, expr_type);
2817 else if (conversion_kind)
2818 warning_at (loc, OPT_Wconversion,
2819 "conversion to %qT alters %qT constant value",
2820 type, expr_type);
2821 return;
2823 case COND_EXPR:
2825 /* In case of COND_EXPR, we do not care about the type of
2826 COND_EXPR, only about the conversion of each operand. */
2827 tree op1 = TREE_OPERAND (expr, 1);
2828 tree op2 = TREE_OPERAND (expr, 2);
2830 conversion_warning (loc, type, op1);
2831 conversion_warning (loc, type, op2);
2832 return;
2835 default: /* 'expr' is not a constant. */
2836 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2837 if (conversion_kind == UNSAFE_REAL)
2838 warning_at (loc, OPT_Wfloat_conversion,
2839 "conversion to %qT from %qT may alter its value",
2840 type, expr_type);
2841 else if (conversion_kind)
2842 warning_at (loc, OPT_Wconversion,
2843 "conversion to %qT from %qT may alter its value",
2844 type, expr_type);
2848 /* Produce warnings after a conversion. RESULT is the result of
2849 converting EXPR to TYPE. This is a helper function for
2850 convert_and_check and cp_convert_and_check. */
2852 void
2853 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2854 tree result)
2856 loc = expansion_point_location_if_in_system_header (loc);
2858 if (TREE_CODE (expr) == INTEGER_CST
2859 && (TREE_CODE (type) == INTEGER_TYPE
2860 || TREE_CODE (type) == ENUMERAL_TYPE)
2861 && !int_fits_type_p (expr, type))
2863 /* Do not diagnose overflow in a constant expression merely
2864 because a conversion overflowed. */
2865 if (TREE_OVERFLOW (result))
2866 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2868 if (TYPE_UNSIGNED (type))
2870 /* This detects cases like converting -129 or 256 to
2871 unsigned char. */
2872 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2873 warning_at (loc, OPT_Woverflow,
2874 "large integer implicitly truncated to unsigned type");
2875 else
2876 conversion_warning (loc, type, expr);
2878 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2879 warning_at (loc, OPT_Woverflow,
2880 "overflow in implicit constant conversion");
2881 /* No warning for converting 0x80000000 to int. */
2882 else if (pedantic
2883 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2884 || TYPE_PRECISION (TREE_TYPE (expr))
2885 != TYPE_PRECISION (type)))
2886 warning_at (loc, OPT_Woverflow,
2887 "overflow in implicit constant conversion");
2889 else
2890 conversion_warning (loc, type, expr);
2892 else if ((TREE_CODE (result) == INTEGER_CST
2893 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2894 warning_at (loc, OPT_Woverflow,
2895 "overflow in implicit constant conversion");
2896 else
2897 conversion_warning (loc, type, expr);
2901 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2902 Invoke this function on every expression that is converted implicitly,
2903 i.e. because of language rules and not because of an explicit cast. */
2905 tree
2906 convert_and_check (location_t loc, tree type, tree expr)
2908 tree result;
2909 tree expr_for_warning;
2911 /* Convert from a value with possible excess precision rather than
2912 via the semantic type, but do not warn about values not fitting
2913 exactly in the semantic type. */
2914 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2916 tree orig_type = TREE_TYPE (expr);
2917 expr = TREE_OPERAND (expr, 0);
2918 expr_for_warning = convert (orig_type, expr);
2919 if (orig_type == type)
2920 return expr_for_warning;
2922 else
2923 expr_for_warning = expr;
2925 if (TREE_TYPE (expr) == type)
2926 return expr;
2928 result = convert (type, expr);
2930 if (c_inhibit_evaluation_warnings == 0
2931 && !TREE_OVERFLOW_P (expr)
2932 && result != error_mark_node)
2933 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2935 return result;
2938 /* A node in a list that describes references to variables (EXPR), which are
2939 either read accesses if WRITER is zero, or write accesses, in which case
2940 WRITER is the parent of EXPR. */
2941 struct tlist
2943 struct tlist *next;
2944 tree expr, writer;
2947 /* Used to implement a cache the results of a call to verify_tree. We only
2948 use this for SAVE_EXPRs. */
2949 struct tlist_cache
2951 struct tlist_cache *next;
2952 struct tlist *cache_before_sp;
2953 struct tlist *cache_after_sp;
2954 tree expr;
2957 /* Obstack to use when allocating tlist structures, and corresponding
2958 firstobj. */
2959 static struct obstack tlist_obstack;
2960 static char *tlist_firstobj = 0;
2962 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2963 warnings. */
2964 static struct tlist *warned_ids;
2965 /* SAVE_EXPRs need special treatment. We process them only once and then
2966 cache the results. */
2967 static struct tlist_cache *save_expr_cache;
2969 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2970 static void merge_tlist (struct tlist **, struct tlist *, int);
2971 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2972 static int warning_candidate_p (tree);
2973 static bool candidate_equal_p (const_tree, const_tree);
2974 static void warn_for_collisions (struct tlist *);
2975 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2976 static struct tlist *new_tlist (struct tlist *, tree, tree);
2978 /* Create a new struct tlist and fill in its fields. */
2979 static struct tlist *
2980 new_tlist (struct tlist *next, tree t, tree writer)
2982 struct tlist *l;
2983 l = XOBNEW (&tlist_obstack, struct tlist);
2984 l->next = next;
2985 l->expr = t;
2986 l->writer = writer;
2987 return l;
2990 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2991 is nonnull, we ignore any node we find which has a writer equal to it. */
2993 static void
2994 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2996 while (add)
2998 struct tlist *next = add->next;
2999 if (!copy)
3000 add->next = *to;
3001 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3002 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3003 add = next;
3007 /* Merge the nodes of ADD into TO. This merging process is done so that for
3008 each variable that already exists in TO, no new node is added; however if
3009 there is a write access recorded in ADD, and an occurrence on TO is only
3010 a read access, then the occurrence in TO will be modified to record the
3011 write. */
3013 static void
3014 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3016 struct tlist **end = to;
3018 while (*end)
3019 end = &(*end)->next;
3021 while (add)
3023 int found = 0;
3024 struct tlist *tmp2;
3025 struct tlist *next = add->next;
3027 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3028 if (candidate_equal_p (tmp2->expr, add->expr))
3030 found = 1;
3031 if (!tmp2->writer)
3032 tmp2->writer = add->writer;
3034 if (!found)
3036 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3037 end = &(*end)->next;
3038 *end = 0;
3040 add = next;
3044 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3045 references in list LIST conflict with it, excluding reads if ONLY writers
3046 is nonzero. */
3048 static void
3049 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3050 int only_writes)
3052 struct tlist *tmp;
3054 /* Avoid duplicate warnings. */
3055 for (tmp = warned_ids; tmp; tmp = tmp->next)
3056 if (candidate_equal_p (tmp->expr, written))
3057 return;
3059 while (list)
3061 if (candidate_equal_p (list->expr, written)
3062 && !candidate_equal_p (list->writer, writer)
3063 && (!only_writes || list->writer))
3065 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3066 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3067 OPT_Wsequence_point, "operation on %qE may be undefined",
3068 list->expr);
3070 list = list->next;
3074 /* Given a list LIST of references to variables, find whether any of these
3075 can cause conflicts due to missing sequence points. */
3077 static void
3078 warn_for_collisions (struct tlist *list)
3080 struct tlist *tmp;
3082 for (tmp = list; tmp; tmp = tmp->next)
3084 if (tmp->writer)
3085 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3089 /* Return nonzero if X is a tree that can be verified by the sequence point
3090 warnings. */
3091 static int
3092 warning_candidate_p (tree x)
3094 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3095 return 0;
3097 if (TREE_CODE (x) == BLOCK)
3098 return 0;
3100 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3101 (lvalue_p) crash on TRY/CATCH. */
3102 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3103 return 0;
3105 if (!lvalue_p (x))
3106 return 0;
3108 /* No point to track non-const calls, they will never satisfy
3109 operand_equal_p. */
3110 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3111 return 0;
3113 if (TREE_CODE (x) == STRING_CST)
3114 return 0;
3116 return 1;
3119 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3120 static bool
3121 candidate_equal_p (const_tree x, const_tree y)
3123 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3126 /* Walk the tree X, and record accesses to variables. If X is written by the
3127 parent tree, WRITER is the parent.
3128 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3129 expression or its only operand forces a sequence point, then everything up
3130 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3131 in PNO_SP.
3132 Once we return, we will have emitted warnings if any subexpression before
3133 such a sequence point could be undefined. On a higher level, however, the
3134 sequence point may not be relevant, and we'll merge the two lists.
3136 Example: (b++, a) + b;
3137 The call that processes the COMPOUND_EXPR will store the increment of B
3138 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3139 processes the PLUS_EXPR will need to merge the two lists so that
3140 eventually, all accesses end up on the same list (and we'll warn about the
3141 unordered subexpressions b++ and b.
3143 A note on merging. If we modify the former example so that our expression
3144 becomes
3145 (b++, b) + a
3146 care must be taken not simply to add all three expressions into the final
3147 PNO_SP list. The function merge_tlist takes care of that by merging the
3148 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3149 way, so that no more than one access to B is recorded. */
3151 static void
3152 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3153 tree writer)
3155 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3156 enum tree_code code;
3157 enum tree_code_class cl;
3159 /* X may be NULL if it is the operand of an empty statement expression
3160 ({ }). */
3161 if (x == NULL)
3162 return;
3164 restart:
3165 code = TREE_CODE (x);
3166 cl = TREE_CODE_CLASS (code);
3168 if (warning_candidate_p (x))
3169 *pno_sp = new_tlist (*pno_sp, x, writer);
3171 switch (code)
3173 case CONSTRUCTOR:
3174 case SIZEOF_EXPR:
3175 return;
3177 case COMPOUND_EXPR:
3178 case TRUTH_ANDIF_EXPR:
3179 case TRUTH_ORIF_EXPR:
3180 tmp_before = tmp_nosp = tmp_list3 = 0;
3181 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3182 warn_for_collisions (tmp_nosp);
3183 merge_tlist (pbefore_sp, tmp_before, 0);
3184 merge_tlist (pbefore_sp, tmp_nosp, 0);
3185 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3186 merge_tlist (pbefore_sp, tmp_list3, 0);
3187 return;
3189 case COND_EXPR:
3190 tmp_before = tmp_list2 = 0;
3191 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3192 warn_for_collisions (tmp_list2);
3193 merge_tlist (pbefore_sp, tmp_before, 0);
3194 merge_tlist (pbefore_sp, tmp_list2, 0);
3196 tmp_list3 = tmp_nosp = 0;
3197 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3198 warn_for_collisions (tmp_nosp);
3199 merge_tlist (pbefore_sp, tmp_list3, 0);
3201 tmp_list3 = tmp_list2 = 0;
3202 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3203 warn_for_collisions (tmp_list2);
3204 merge_tlist (pbefore_sp, tmp_list3, 0);
3205 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3206 two first, to avoid warning for (a ? b++ : b++). */
3207 merge_tlist (&tmp_nosp, tmp_list2, 0);
3208 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3209 return;
3211 case PREDECREMENT_EXPR:
3212 case PREINCREMENT_EXPR:
3213 case POSTDECREMENT_EXPR:
3214 case POSTINCREMENT_EXPR:
3215 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3216 return;
3218 case MODIFY_EXPR:
3219 tmp_before = tmp_nosp = tmp_list3 = 0;
3220 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3221 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3222 /* Expressions inside the LHS are not ordered wrt. the sequence points
3223 in the RHS. Example:
3224 *a = (a++, 2)
3225 Despite the fact that the modification of "a" is in the before_sp
3226 list (tmp_before), it conflicts with the use of "a" in the LHS.
3227 We can handle this by adding the contents of tmp_list3
3228 to those of tmp_before, and redoing the collision warnings for that
3229 list. */
3230 add_tlist (&tmp_before, tmp_list3, x, 1);
3231 warn_for_collisions (tmp_before);
3232 /* Exclude the LHS itself here; we first have to merge it into the
3233 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3234 didn't exclude the LHS, we'd get it twice, once as a read and once
3235 as a write. */
3236 add_tlist (pno_sp, tmp_list3, x, 0);
3237 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3239 merge_tlist (pbefore_sp, tmp_before, 0);
3240 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3241 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3242 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3243 return;
3245 case CALL_EXPR:
3246 /* We need to warn about conflicts among arguments and conflicts between
3247 args and the function address. Side effects of the function address,
3248 however, are not ordered by the sequence point of the call. */
3250 call_expr_arg_iterator iter;
3251 tree arg;
3252 tmp_before = tmp_nosp = 0;
3253 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3254 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3256 tmp_list2 = tmp_list3 = 0;
3257 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3258 merge_tlist (&tmp_list3, tmp_list2, 0);
3259 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3261 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3262 warn_for_collisions (tmp_before);
3263 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3264 return;
3267 case TREE_LIST:
3268 /* Scan all the list, e.g. indices of multi dimensional array. */
3269 while (x)
3271 tmp_before = tmp_nosp = 0;
3272 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3273 merge_tlist (&tmp_nosp, tmp_before, 0);
3274 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3275 x = TREE_CHAIN (x);
3277 return;
3279 case SAVE_EXPR:
3281 struct tlist_cache *t;
3282 for (t = save_expr_cache; t; t = t->next)
3283 if (candidate_equal_p (t->expr, x))
3284 break;
3286 if (!t)
3288 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3289 t->next = save_expr_cache;
3290 t->expr = x;
3291 save_expr_cache = t;
3293 tmp_before = tmp_nosp = 0;
3294 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3295 warn_for_collisions (tmp_nosp);
3297 tmp_list3 = 0;
3298 merge_tlist (&tmp_list3, tmp_nosp, 0);
3299 t->cache_before_sp = tmp_before;
3300 t->cache_after_sp = tmp_list3;
3302 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3303 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3304 return;
3307 case ADDR_EXPR:
3308 x = TREE_OPERAND (x, 0);
3309 if (DECL_P (x))
3310 return;
3311 writer = 0;
3312 goto restart;
3314 default:
3315 /* For other expressions, simply recurse on their operands.
3316 Manual tail recursion for unary expressions.
3317 Other non-expressions need not be processed. */
3318 if (cl == tcc_unary)
3320 x = TREE_OPERAND (x, 0);
3321 writer = 0;
3322 goto restart;
3324 else if (IS_EXPR_CODE_CLASS (cl))
3326 int lp;
3327 int max = TREE_OPERAND_LENGTH (x);
3328 for (lp = 0; lp < max; lp++)
3330 tmp_before = tmp_nosp = 0;
3331 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3332 merge_tlist (&tmp_nosp, tmp_before, 0);
3333 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3336 return;
3340 /* Try to warn for undefined behavior in EXPR due to missing sequence
3341 points. */
3343 DEBUG_FUNCTION void
3344 verify_sequence_points (tree expr)
3346 struct tlist *before_sp = 0, *after_sp = 0;
3348 warned_ids = 0;
3349 save_expr_cache = 0;
3350 if (tlist_firstobj == 0)
3352 gcc_obstack_init (&tlist_obstack);
3353 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3356 verify_tree (expr, &before_sp, &after_sp, 0);
3357 warn_for_collisions (after_sp);
3358 obstack_free (&tlist_obstack, tlist_firstobj);
3361 /* Validate the expression after `case' and apply default promotions. */
3363 static tree
3364 check_case_value (location_t loc, tree value)
3366 if (value == NULL_TREE)
3367 return value;
3369 if (TREE_CODE (value) == INTEGER_CST)
3370 /* Promote char or short to int. */
3371 value = perform_integral_promotions (value);
3372 else if (value != error_mark_node)
3374 error_at (loc, "case label does not reduce to an integer constant");
3375 value = error_mark_node;
3378 constant_expression_warning (value);
3380 return value;
3383 /* See if the case values LOW and HIGH are in the range of the original
3384 type (i.e. before the default conversion to int) of the switch testing
3385 expression.
3386 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3387 the type before promoting it. CASE_LOW_P is a pointer to the lower
3388 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3389 if the case is not a case range.
3390 The caller has to make sure that we are not called with NULL for
3391 CASE_LOW_P (i.e. the default case).
3392 Returns true if the case label is in range of ORIG_TYPE (saturated or
3393 untouched) or false if the label is out of range. */
3395 static bool
3396 check_case_bounds (location_t loc, tree type, tree orig_type,
3397 tree *case_low_p, tree *case_high_p)
3399 tree min_value, max_value;
3400 tree case_low = *case_low_p;
3401 tree case_high = case_high_p ? *case_high_p : case_low;
3403 /* If there was a problem with the original type, do nothing. */
3404 if (orig_type == error_mark_node)
3405 return true;
3407 min_value = TYPE_MIN_VALUE (orig_type);
3408 max_value = TYPE_MAX_VALUE (orig_type);
3410 /* Case label is less than minimum for type. */
3411 if (tree_int_cst_compare (case_low, min_value) < 0
3412 && tree_int_cst_compare (case_high, min_value) < 0)
3414 warning_at (loc, 0, "case label value is less than minimum value "
3415 "for type");
3416 return false;
3419 /* Case value is greater than maximum for type. */
3420 if (tree_int_cst_compare (case_low, max_value) > 0
3421 && tree_int_cst_compare (case_high, max_value) > 0)
3423 warning_at (loc, 0, "case label value exceeds maximum value for type");
3424 return false;
3427 /* Saturate lower case label value to minimum. */
3428 if (tree_int_cst_compare (case_high, min_value) >= 0
3429 && tree_int_cst_compare (case_low, min_value) < 0)
3431 warning_at (loc, 0, "lower value in case label range"
3432 " less than minimum value for type");
3433 case_low = min_value;
3436 /* Saturate upper case label value to maximum. */
3437 if (tree_int_cst_compare (case_low, max_value) <= 0
3438 && tree_int_cst_compare (case_high, max_value) > 0)
3440 warning_at (loc, 0, "upper value in case label range"
3441 " exceeds maximum value for type");
3442 case_high = max_value;
3445 if (*case_low_p != case_low)
3446 *case_low_p = convert (type, case_low);
3447 if (case_high_p && *case_high_p != case_high)
3448 *case_high_p = convert (type, case_high);
3450 return true;
3453 /* Return an integer type with BITS bits of precision,
3454 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3456 tree
3457 c_common_type_for_size (unsigned int bits, int unsignedp)
3459 int i;
3461 if (bits == TYPE_PRECISION (integer_type_node))
3462 return unsignedp ? unsigned_type_node : integer_type_node;
3464 if (bits == TYPE_PRECISION (signed_char_type_node))
3465 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3467 if (bits == TYPE_PRECISION (short_integer_type_node))
3468 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3470 if (bits == TYPE_PRECISION (long_integer_type_node))
3471 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3473 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3474 return (unsignedp ? long_long_unsigned_type_node
3475 : long_long_integer_type_node);
3477 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3478 if (int_n_enabled_p[i]
3479 && bits == int_n_data[i].bitsize)
3480 return (unsignedp ? int_n_trees[i].unsigned_type
3481 : int_n_trees[i].signed_type);
3483 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3484 return (unsignedp ? widest_unsigned_literal_type_node
3485 : widest_integer_literal_type_node);
3487 if (bits <= TYPE_PRECISION (intQI_type_node))
3488 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3490 if (bits <= TYPE_PRECISION (intHI_type_node))
3491 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3493 if (bits <= TYPE_PRECISION (intSI_type_node))
3494 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3496 if (bits <= TYPE_PRECISION (intDI_type_node))
3497 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3499 return 0;
3502 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3503 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3504 and saturating if SATP is nonzero, otherwise not saturating. */
3506 tree
3507 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3508 int unsignedp, int satp)
3510 machine_mode mode;
3511 if (ibit == 0)
3512 mode = unsignedp ? UQQmode : QQmode;
3513 else
3514 mode = unsignedp ? UHAmode : HAmode;
3516 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3517 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3518 break;
3520 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3522 sorry ("GCC cannot support operators with integer types and "
3523 "fixed-point types that have too many integral and "
3524 "fractional bits together");
3525 return 0;
3528 return c_common_type_for_mode (mode, satp);
3531 /* Used for communication between c_common_type_for_mode and
3532 c_register_builtin_type. */
3533 tree registered_builtin_types;
3535 /* Return a data type that has machine mode MODE.
3536 If the mode is an integer,
3537 then UNSIGNEDP selects between signed and unsigned types.
3538 If the mode is a fixed-point mode,
3539 then UNSIGNEDP selects between saturating and nonsaturating types. */
3541 tree
3542 c_common_type_for_mode (machine_mode mode, int unsignedp)
3544 tree t;
3545 int i;
3547 if (mode == TYPE_MODE (integer_type_node))
3548 return unsignedp ? unsigned_type_node : integer_type_node;
3550 if (mode == TYPE_MODE (signed_char_type_node))
3551 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3553 if (mode == TYPE_MODE (short_integer_type_node))
3554 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3556 if (mode == TYPE_MODE (long_integer_type_node))
3557 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3559 if (mode == TYPE_MODE (long_long_integer_type_node))
3560 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3562 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3563 if (int_n_enabled_p[i]
3564 && mode == int_n_data[i].m)
3565 return (unsignedp ? int_n_trees[i].unsigned_type
3566 : int_n_trees[i].signed_type);
3568 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3569 return unsignedp ? widest_unsigned_literal_type_node
3570 : widest_integer_literal_type_node;
3572 if (mode == QImode)
3573 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3575 if (mode == HImode)
3576 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3578 if (mode == SImode)
3579 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3581 if (mode == DImode)
3582 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3584 #if HOST_BITS_PER_WIDE_INT >= 64
3585 if (mode == TYPE_MODE (intTI_type_node))
3586 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3587 #endif
3589 if (mode == TYPE_MODE (float_type_node))
3590 return float_type_node;
3592 if (mode == TYPE_MODE (double_type_node))
3593 return double_type_node;
3595 if (mode == TYPE_MODE (long_double_type_node))
3596 return long_double_type_node;
3598 if (mode == TYPE_MODE (void_type_node))
3599 return void_type_node;
3601 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3602 return (unsignedp
3603 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3604 : make_signed_type (GET_MODE_PRECISION (mode)));
3606 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3607 return (unsignedp
3608 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3609 : make_signed_type (GET_MODE_PRECISION (mode)));
3611 if (COMPLEX_MODE_P (mode))
3613 machine_mode inner_mode;
3614 tree inner_type;
3616 if (mode == TYPE_MODE (complex_float_type_node))
3617 return complex_float_type_node;
3618 if (mode == TYPE_MODE (complex_double_type_node))
3619 return complex_double_type_node;
3620 if (mode == TYPE_MODE (complex_long_double_type_node))
3621 return complex_long_double_type_node;
3623 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3624 return complex_integer_type_node;
3626 inner_mode = GET_MODE_INNER (mode);
3627 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3628 if (inner_type != NULL_TREE)
3629 return build_complex_type (inner_type);
3631 else if (VECTOR_MODE_P (mode))
3633 machine_mode inner_mode = GET_MODE_INNER (mode);
3634 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3635 if (inner_type != NULL_TREE)
3636 return build_vector_type_for_mode (inner_type, mode);
3639 if (mode == TYPE_MODE (dfloat32_type_node))
3640 return dfloat32_type_node;
3641 if (mode == TYPE_MODE (dfloat64_type_node))
3642 return dfloat64_type_node;
3643 if (mode == TYPE_MODE (dfloat128_type_node))
3644 return dfloat128_type_node;
3646 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3648 if (mode == TYPE_MODE (short_fract_type_node))
3649 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3650 if (mode == TYPE_MODE (fract_type_node))
3651 return unsignedp ? sat_fract_type_node : fract_type_node;
3652 if (mode == TYPE_MODE (long_fract_type_node))
3653 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3654 if (mode == TYPE_MODE (long_long_fract_type_node))
3655 return unsignedp ? sat_long_long_fract_type_node
3656 : long_long_fract_type_node;
3658 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3659 return unsignedp ? sat_unsigned_short_fract_type_node
3660 : unsigned_short_fract_type_node;
3661 if (mode == TYPE_MODE (unsigned_fract_type_node))
3662 return unsignedp ? sat_unsigned_fract_type_node
3663 : unsigned_fract_type_node;
3664 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3665 return unsignedp ? sat_unsigned_long_fract_type_node
3666 : unsigned_long_fract_type_node;
3667 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3668 return unsignedp ? sat_unsigned_long_long_fract_type_node
3669 : unsigned_long_long_fract_type_node;
3671 if (mode == TYPE_MODE (short_accum_type_node))
3672 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3673 if (mode == TYPE_MODE (accum_type_node))
3674 return unsignedp ? sat_accum_type_node : accum_type_node;
3675 if (mode == TYPE_MODE (long_accum_type_node))
3676 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3677 if (mode == TYPE_MODE (long_long_accum_type_node))
3678 return unsignedp ? sat_long_long_accum_type_node
3679 : long_long_accum_type_node;
3681 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3682 return unsignedp ? sat_unsigned_short_accum_type_node
3683 : unsigned_short_accum_type_node;
3684 if (mode == TYPE_MODE (unsigned_accum_type_node))
3685 return unsignedp ? sat_unsigned_accum_type_node
3686 : unsigned_accum_type_node;
3687 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3688 return unsignedp ? sat_unsigned_long_accum_type_node
3689 : unsigned_long_accum_type_node;
3690 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3691 return unsignedp ? sat_unsigned_long_long_accum_type_node
3692 : unsigned_long_long_accum_type_node;
3694 if (mode == QQmode)
3695 return unsignedp ? sat_qq_type_node : qq_type_node;
3696 if (mode == HQmode)
3697 return unsignedp ? sat_hq_type_node : hq_type_node;
3698 if (mode == SQmode)
3699 return unsignedp ? sat_sq_type_node : sq_type_node;
3700 if (mode == DQmode)
3701 return unsignedp ? sat_dq_type_node : dq_type_node;
3702 if (mode == TQmode)
3703 return unsignedp ? sat_tq_type_node : tq_type_node;
3705 if (mode == UQQmode)
3706 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3707 if (mode == UHQmode)
3708 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3709 if (mode == USQmode)
3710 return unsignedp ? sat_usq_type_node : usq_type_node;
3711 if (mode == UDQmode)
3712 return unsignedp ? sat_udq_type_node : udq_type_node;
3713 if (mode == UTQmode)
3714 return unsignedp ? sat_utq_type_node : utq_type_node;
3716 if (mode == HAmode)
3717 return unsignedp ? sat_ha_type_node : ha_type_node;
3718 if (mode == SAmode)
3719 return unsignedp ? sat_sa_type_node : sa_type_node;
3720 if (mode == DAmode)
3721 return unsignedp ? sat_da_type_node : da_type_node;
3722 if (mode == TAmode)
3723 return unsignedp ? sat_ta_type_node : ta_type_node;
3725 if (mode == UHAmode)
3726 return unsignedp ? sat_uha_type_node : uha_type_node;
3727 if (mode == USAmode)
3728 return unsignedp ? sat_usa_type_node : usa_type_node;
3729 if (mode == UDAmode)
3730 return unsignedp ? sat_uda_type_node : uda_type_node;
3731 if (mode == UTAmode)
3732 return unsignedp ? sat_uta_type_node : uta_type_node;
3735 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3736 if (TYPE_MODE (TREE_VALUE (t)) == mode
3737 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3738 return TREE_VALUE (t);
3740 return 0;
3743 tree
3744 c_common_unsigned_type (tree type)
3746 return c_common_signed_or_unsigned_type (1, type);
3749 /* Return a signed type the same as TYPE in other respects. */
3751 tree
3752 c_common_signed_type (tree type)
3754 return c_common_signed_or_unsigned_type (0, type);
3757 /* Return a type the same as TYPE except unsigned or
3758 signed according to UNSIGNEDP. */
3760 tree
3761 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3763 tree type1;
3764 int i;
3766 /* This block of code emulates the behavior of the old
3767 c_common_unsigned_type. In particular, it returns
3768 long_unsigned_type_node if passed a long, even when a int would
3769 have the same size. This is necessary for warnings to work
3770 correctly in archs where sizeof(int) == sizeof(long) */
3772 type1 = TYPE_MAIN_VARIANT (type);
3773 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3774 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3775 if (type1 == integer_type_node || type1 == unsigned_type_node)
3776 return unsignedp ? unsigned_type_node : integer_type_node;
3777 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3778 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3779 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3780 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3781 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3782 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3784 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3785 if (int_n_enabled_p[i]
3786 && (type1 == int_n_trees[i].unsigned_type
3787 || type1 == int_n_trees[i].signed_type))
3788 return (unsignedp ? int_n_trees[i].unsigned_type
3789 : int_n_trees[i].signed_type);
3791 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3792 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3793 #if HOST_BITS_PER_WIDE_INT >= 64
3794 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3795 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3796 #endif
3797 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3798 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3799 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3800 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3801 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3802 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3803 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3804 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3806 #define C_COMMON_FIXED_TYPES(NAME) \
3807 if (type1 == short_ ## NAME ## _type_node \
3808 || type1 == unsigned_short_ ## NAME ## _type_node) \
3809 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3810 : short_ ## NAME ## _type_node; \
3811 if (type1 == NAME ## _type_node \
3812 || type1 == unsigned_ ## NAME ## _type_node) \
3813 return unsignedp ? unsigned_ ## NAME ## _type_node \
3814 : NAME ## _type_node; \
3815 if (type1 == long_ ## NAME ## _type_node \
3816 || type1 == unsigned_long_ ## NAME ## _type_node) \
3817 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3818 : long_ ## NAME ## _type_node; \
3819 if (type1 == long_long_ ## NAME ## _type_node \
3820 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3821 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3822 : long_long_ ## NAME ## _type_node;
3824 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3825 if (type1 == NAME ## _type_node \
3826 || type1 == u ## NAME ## _type_node) \
3827 return unsignedp ? u ## NAME ## _type_node \
3828 : NAME ## _type_node;
3830 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3831 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3832 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3833 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3834 : sat_ ## short_ ## NAME ## _type_node; \
3835 if (type1 == sat_ ## NAME ## _type_node \
3836 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3837 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3838 : sat_ ## NAME ## _type_node; \
3839 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3840 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3841 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3842 : sat_ ## long_ ## NAME ## _type_node; \
3843 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3844 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3845 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3846 : sat_ ## long_long_ ## NAME ## _type_node;
3848 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3849 if (type1 == sat_ ## NAME ## _type_node \
3850 || type1 == sat_ ## u ## NAME ## _type_node) \
3851 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3852 : sat_ ## NAME ## _type_node;
3854 C_COMMON_FIXED_TYPES (fract);
3855 C_COMMON_FIXED_TYPES_SAT (fract);
3856 C_COMMON_FIXED_TYPES (accum);
3857 C_COMMON_FIXED_TYPES_SAT (accum);
3859 C_COMMON_FIXED_MODE_TYPES (qq);
3860 C_COMMON_FIXED_MODE_TYPES (hq);
3861 C_COMMON_FIXED_MODE_TYPES (sq);
3862 C_COMMON_FIXED_MODE_TYPES (dq);
3863 C_COMMON_FIXED_MODE_TYPES (tq);
3864 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3865 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3866 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3867 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3868 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3869 C_COMMON_FIXED_MODE_TYPES (ha);
3870 C_COMMON_FIXED_MODE_TYPES (sa);
3871 C_COMMON_FIXED_MODE_TYPES (da);
3872 C_COMMON_FIXED_MODE_TYPES (ta);
3873 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3874 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3875 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3876 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3878 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3879 the precision; they have precision set to match their range, but
3880 may use a wider mode to match an ABI. If we change modes, we may
3881 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3882 the precision as well, so as to yield correct results for
3883 bit-field types. C++ does not have these separate bit-field
3884 types, and producing a signed or unsigned variant of an
3885 ENUMERAL_TYPE may cause other problems as well. */
3887 if (!INTEGRAL_TYPE_P (type)
3888 || TYPE_UNSIGNED (type) == unsignedp)
3889 return type;
3891 #define TYPE_OK(node) \
3892 (TYPE_MODE (type) == TYPE_MODE (node) \
3893 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3894 if (TYPE_OK (signed_char_type_node))
3895 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3896 if (TYPE_OK (integer_type_node))
3897 return unsignedp ? unsigned_type_node : integer_type_node;
3898 if (TYPE_OK (short_integer_type_node))
3899 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3900 if (TYPE_OK (long_integer_type_node))
3901 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3902 if (TYPE_OK (long_long_integer_type_node))
3903 return (unsignedp ? long_long_unsigned_type_node
3904 : long_long_integer_type_node);
3906 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3907 if (int_n_enabled_p[i]
3908 && TYPE_MODE (type) == int_n_data[i].m
3909 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3910 return (unsignedp ? int_n_trees[i].unsigned_type
3911 : int_n_trees[i].signed_type);
3913 if (TYPE_OK (widest_integer_literal_type_node))
3914 return (unsignedp ? widest_unsigned_literal_type_node
3915 : widest_integer_literal_type_node);
3917 #if HOST_BITS_PER_WIDE_INT >= 64
3918 if (TYPE_OK (intTI_type_node))
3919 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3920 #endif
3921 if (TYPE_OK (intDI_type_node))
3922 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3923 if (TYPE_OK (intSI_type_node))
3924 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3925 if (TYPE_OK (intHI_type_node))
3926 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3927 if (TYPE_OK (intQI_type_node))
3928 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3929 #undef TYPE_OK
3931 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3934 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3936 tree
3937 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3939 int i;
3941 /* Extended integer types of the same width as a standard type have
3942 lesser rank, so those of the same width as int promote to int or
3943 unsigned int and are valid for printf formats expecting int or
3944 unsigned int. To avoid such special cases, avoid creating
3945 extended integer types for bit-fields if a standard integer type
3946 is available. */
3947 if (width == TYPE_PRECISION (integer_type_node))
3948 return unsignedp ? unsigned_type_node : integer_type_node;
3949 if (width == TYPE_PRECISION (signed_char_type_node))
3950 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3951 if (width == TYPE_PRECISION (short_integer_type_node))
3952 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3953 if (width == TYPE_PRECISION (long_integer_type_node))
3954 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3955 if (width == TYPE_PRECISION (long_long_integer_type_node))
3956 return (unsignedp ? long_long_unsigned_type_node
3957 : long_long_integer_type_node);
3958 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3959 if (int_n_enabled_p[i]
3960 && width == int_n_data[i].bitsize)
3961 return (unsignedp ? int_n_trees[i].unsigned_type
3962 : int_n_trees[i].signed_type);
3963 return build_nonstandard_integer_type (width, unsignedp);
3966 /* The C version of the register_builtin_type langhook. */
3968 void
3969 c_register_builtin_type (tree type, const char* name)
3971 tree decl;
3973 decl = build_decl (UNKNOWN_LOCATION,
3974 TYPE_DECL, get_identifier (name), type);
3975 DECL_ARTIFICIAL (decl) = 1;
3976 if (!TYPE_NAME (type))
3977 TYPE_NAME (type) = decl;
3978 pushdecl (decl);
3980 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3983 /* Print an error message for invalid operands to arith operation
3984 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3985 LOCATION is the location of the message. */
3987 void
3988 binary_op_error (location_t location, enum tree_code code,
3989 tree type0, tree type1)
3991 const char *opname;
3993 switch (code)
3995 case PLUS_EXPR:
3996 opname = "+"; break;
3997 case MINUS_EXPR:
3998 opname = "-"; break;
3999 case MULT_EXPR:
4000 opname = "*"; break;
4001 case MAX_EXPR:
4002 opname = "max"; break;
4003 case MIN_EXPR:
4004 opname = "min"; break;
4005 case EQ_EXPR:
4006 opname = "=="; break;
4007 case NE_EXPR:
4008 opname = "!="; break;
4009 case LE_EXPR:
4010 opname = "<="; break;
4011 case GE_EXPR:
4012 opname = ">="; break;
4013 case LT_EXPR:
4014 opname = "<"; break;
4015 case GT_EXPR:
4016 opname = ">"; break;
4017 case LSHIFT_EXPR:
4018 opname = "<<"; break;
4019 case RSHIFT_EXPR:
4020 opname = ">>"; break;
4021 case TRUNC_MOD_EXPR:
4022 case FLOOR_MOD_EXPR:
4023 opname = "%"; break;
4024 case TRUNC_DIV_EXPR:
4025 case FLOOR_DIV_EXPR:
4026 opname = "/"; break;
4027 case BIT_AND_EXPR:
4028 opname = "&"; break;
4029 case BIT_IOR_EXPR:
4030 opname = "|"; break;
4031 case TRUTH_ANDIF_EXPR:
4032 opname = "&&"; break;
4033 case TRUTH_ORIF_EXPR:
4034 opname = "||"; break;
4035 case BIT_XOR_EXPR:
4036 opname = "^"; break;
4037 default:
4038 gcc_unreachable ();
4040 error_at (location,
4041 "invalid operands to binary %s (have %qT and %qT)", opname,
4042 type0, type1);
4045 /* Given an expression as a tree, return its original type. Do this
4046 by stripping any conversion that preserves the sign and precision. */
4047 static tree
4048 expr_original_type (tree expr)
4050 STRIP_SIGN_NOPS (expr);
4051 return TREE_TYPE (expr);
4054 /* Subroutine of build_binary_op, used for comparison operations.
4055 See if the operands have both been converted from subword integer types
4056 and, if so, perhaps change them both back to their original type.
4057 This function is also responsible for converting the two operands
4058 to the proper common type for comparison.
4060 The arguments of this function are all pointers to local variables
4061 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4062 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4064 LOC is the location of the comparison.
4066 If this function returns nonzero, it means that the comparison has
4067 a constant value. What this function returns is an expression for
4068 that value. */
4070 tree
4071 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4072 tree *restype_ptr, enum tree_code *rescode_ptr)
4074 tree type;
4075 tree op0 = *op0_ptr;
4076 tree op1 = *op1_ptr;
4077 int unsignedp0, unsignedp1;
4078 int real1, real2;
4079 tree primop0, primop1;
4080 enum tree_code code = *rescode_ptr;
4082 /* Throw away any conversions to wider types
4083 already present in the operands. */
4085 primop0 = c_common_get_narrower (op0, &unsignedp0);
4086 primop1 = c_common_get_narrower (op1, &unsignedp1);
4088 /* If primopN is first sign-extended from primopN's precision to opN's
4089 precision, then zero-extended from opN's precision to
4090 *restype_ptr precision, shortenings might be invalid. */
4091 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4092 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4093 && !unsignedp0
4094 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4095 primop0 = op0;
4096 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4097 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4098 && !unsignedp1
4099 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4100 primop1 = op1;
4102 /* Handle the case that OP0 does not *contain* a conversion
4103 but it *requires* conversion to FINAL_TYPE. */
4105 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4106 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4107 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4108 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4110 /* If one of the operands must be floated, we cannot optimize. */
4111 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4112 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4114 /* If first arg is constant, swap the args (changing operation
4115 so value is preserved), for canonicalization. Don't do this if
4116 the second arg is 0. */
4118 if (TREE_CONSTANT (primop0)
4119 && !integer_zerop (primop1) && !real_zerop (primop1)
4120 && !fixed_zerop (primop1))
4122 tree tem = primop0;
4123 int temi = unsignedp0;
4124 primop0 = primop1;
4125 primop1 = tem;
4126 tem = op0;
4127 op0 = op1;
4128 op1 = tem;
4129 *op0_ptr = op0;
4130 *op1_ptr = op1;
4131 unsignedp0 = unsignedp1;
4132 unsignedp1 = temi;
4133 temi = real1;
4134 real1 = real2;
4135 real2 = temi;
4137 switch (code)
4139 case LT_EXPR:
4140 code = GT_EXPR;
4141 break;
4142 case GT_EXPR:
4143 code = LT_EXPR;
4144 break;
4145 case LE_EXPR:
4146 code = GE_EXPR;
4147 break;
4148 case GE_EXPR:
4149 code = LE_EXPR;
4150 break;
4151 default:
4152 break;
4154 *rescode_ptr = code;
4157 /* If comparing an integer against a constant more bits wide,
4158 maybe we can deduce a value of 1 or 0 independent of the data.
4159 Or else truncate the constant now
4160 rather than extend the variable at run time.
4162 This is only interesting if the constant is the wider arg.
4163 Also, it is not safe if the constant is unsigned and the
4164 variable arg is signed, since in this case the variable
4165 would be sign-extended and then regarded as unsigned.
4166 Our technique fails in this case because the lowest/highest
4167 possible unsigned results don't follow naturally from the
4168 lowest/highest possible values of the variable operand.
4169 For just EQ_EXPR and NE_EXPR there is another technique that
4170 could be used: see if the constant can be faithfully represented
4171 in the other operand's type, by truncating it and reextending it
4172 and see if that preserves the constant's value. */
4174 if (!real1 && !real2
4175 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4176 && TREE_CODE (primop1) == INTEGER_CST
4177 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4179 int min_gt, max_gt, min_lt, max_lt;
4180 tree maxval, minval;
4181 /* 1 if comparison is nominally unsigned. */
4182 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4183 tree val;
4185 type = c_common_signed_or_unsigned_type (unsignedp0,
4186 TREE_TYPE (primop0));
4188 maxval = TYPE_MAX_VALUE (type);
4189 minval = TYPE_MIN_VALUE (type);
4191 if (unsignedp && !unsignedp0)
4192 *restype_ptr = c_common_signed_type (*restype_ptr);
4194 if (TREE_TYPE (primop1) != *restype_ptr)
4196 /* Convert primop1 to target type, but do not introduce
4197 additional overflow. We know primop1 is an int_cst. */
4198 primop1 = force_fit_type (*restype_ptr,
4199 wide_int::from
4200 (primop1,
4201 TYPE_PRECISION (*restype_ptr),
4202 TYPE_SIGN (TREE_TYPE (primop1))),
4203 0, TREE_OVERFLOW (primop1));
4205 if (type != *restype_ptr)
4207 minval = convert (*restype_ptr, minval);
4208 maxval = convert (*restype_ptr, maxval);
4211 min_gt = tree_int_cst_lt (primop1, minval);
4212 max_gt = tree_int_cst_lt (primop1, maxval);
4213 min_lt = tree_int_cst_lt (minval, primop1);
4214 max_lt = tree_int_cst_lt (maxval, primop1);
4216 val = 0;
4217 /* This used to be a switch, but Genix compiler can't handle that. */
4218 if (code == NE_EXPR)
4220 if (max_lt || min_gt)
4221 val = truthvalue_true_node;
4223 else if (code == EQ_EXPR)
4225 if (max_lt || min_gt)
4226 val = truthvalue_false_node;
4228 else if (code == LT_EXPR)
4230 if (max_lt)
4231 val = truthvalue_true_node;
4232 if (!min_lt)
4233 val = truthvalue_false_node;
4235 else if (code == GT_EXPR)
4237 if (min_gt)
4238 val = truthvalue_true_node;
4239 if (!max_gt)
4240 val = truthvalue_false_node;
4242 else if (code == LE_EXPR)
4244 if (!max_gt)
4245 val = truthvalue_true_node;
4246 if (min_gt)
4247 val = truthvalue_false_node;
4249 else if (code == GE_EXPR)
4251 if (!min_lt)
4252 val = truthvalue_true_node;
4253 if (max_lt)
4254 val = truthvalue_false_node;
4257 /* If primop0 was sign-extended and unsigned comparison specd,
4258 we did a signed comparison above using the signed type bounds.
4259 But the comparison we output must be unsigned.
4261 Also, for inequalities, VAL is no good; but if the signed
4262 comparison had *any* fixed result, it follows that the
4263 unsigned comparison just tests the sign in reverse
4264 (positive values are LE, negative ones GE).
4265 So we can generate an unsigned comparison
4266 against an extreme value of the signed type. */
4268 if (unsignedp && !unsignedp0)
4270 if (val != 0)
4271 switch (code)
4273 case LT_EXPR:
4274 case GE_EXPR:
4275 primop1 = TYPE_MIN_VALUE (type);
4276 val = 0;
4277 break;
4279 case LE_EXPR:
4280 case GT_EXPR:
4281 primop1 = TYPE_MAX_VALUE (type);
4282 val = 0;
4283 break;
4285 default:
4286 break;
4288 type = c_common_unsigned_type (type);
4291 if (TREE_CODE (primop0) != INTEGER_CST)
4293 if (val == truthvalue_false_node)
4294 warning_at (loc, OPT_Wtype_limits,
4295 "comparison is always false due to limited range of data type");
4296 if (val == truthvalue_true_node)
4297 warning_at (loc, OPT_Wtype_limits,
4298 "comparison is always true due to limited range of data type");
4301 if (val != 0)
4303 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4304 if (TREE_SIDE_EFFECTS (primop0))
4305 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4306 return val;
4309 /* Value is not predetermined, but do the comparison
4310 in the type of the operand that is not constant.
4311 TYPE is already properly set. */
4314 /* If either arg is decimal float and the other is float, find the
4315 proper common type to use for comparison. */
4316 else if (real1 && real2
4317 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4318 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4319 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4321 else if (real1 && real2
4322 && (TYPE_PRECISION (TREE_TYPE (primop0))
4323 == TYPE_PRECISION (TREE_TYPE (primop1))))
4324 type = TREE_TYPE (primop0);
4326 /* If args' natural types are both narrower than nominal type
4327 and both extend in the same manner, compare them
4328 in the type of the wider arg.
4329 Otherwise must actually extend both to the nominal
4330 common type lest different ways of extending
4331 alter the result.
4332 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4334 else if (unsignedp0 == unsignedp1 && real1 == real2
4335 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4336 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4338 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4339 type = c_common_signed_or_unsigned_type (unsignedp0
4340 || TYPE_UNSIGNED (*restype_ptr),
4341 type);
4342 /* Make sure shorter operand is extended the right way
4343 to match the longer operand. */
4344 primop0
4345 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4346 TREE_TYPE (primop0)),
4347 primop0);
4348 primop1
4349 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4350 TREE_TYPE (primop1)),
4351 primop1);
4353 else
4355 /* Here we must do the comparison on the nominal type
4356 using the args exactly as we received them. */
4357 type = *restype_ptr;
4358 primop0 = op0;
4359 primop1 = op1;
4361 if (!real1 && !real2 && integer_zerop (primop1)
4362 && TYPE_UNSIGNED (*restype_ptr))
4364 tree value = 0;
4365 /* All unsigned values are >= 0, so we warn. However,
4366 if OP0 is a constant that is >= 0, the signedness of
4367 the comparison isn't an issue, so suppress the
4368 warning. */
4369 bool warn =
4370 warn_type_limits && !in_system_header_at (loc)
4371 && !(TREE_CODE (primop0) == INTEGER_CST
4372 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4373 primop0)))
4374 /* Do not warn for enumeration types. */
4375 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4377 switch (code)
4379 case GE_EXPR:
4380 if (warn)
4381 warning_at (loc, OPT_Wtype_limits,
4382 "comparison of unsigned expression >= 0 is always true");
4383 value = truthvalue_true_node;
4384 break;
4386 case LT_EXPR:
4387 if (warn)
4388 warning_at (loc, OPT_Wtype_limits,
4389 "comparison of unsigned expression < 0 is always false");
4390 value = truthvalue_false_node;
4391 break;
4393 default:
4394 break;
4397 if (value != 0)
4399 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4400 if (TREE_SIDE_EFFECTS (primop0))
4401 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4402 primop0, value);
4403 return value;
4408 *op0_ptr = convert (type, primop0);
4409 *op1_ptr = convert (type, primop1);
4411 *restype_ptr = truthvalue_type_node;
4413 return 0;
4416 /* Return a tree for the sum or difference (RESULTCODE says which)
4417 of pointer PTROP and integer INTOP. */
4419 tree
4420 pointer_int_sum (location_t loc, enum tree_code resultcode,
4421 tree ptrop, tree intop, bool complain)
4423 tree size_exp, ret;
4425 /* The result is a pointer of the same type that is being added. */
4426 tree result_type = TREE_TYPE (ptrop);
4428 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4430 if (complain && warn_pointer_arith)
4431 pedwarn (loc, OPT_Wpointer_arith,
4432 "pointer of type %<void *%> used in arithmetic");
4433 else if (!complain)
4434 return error_mark_node;
4435 size_exp = integer_one_node;
4437 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4439 if (complain && warn_pointer_arith)
4440 pedwarn (loc, OPT_Wpointer_arith,
4441 "pointer to a function used in arithmetic");
4442 else if (!complain)
4443 return error_mark_node;
4444 size_exp = integer_one_node;
4446 else
4447 size_exp = size_in_bytes (TREE_TYPE (result_type));
4449 /* We are manipulating pointer values, so we don't need to warn
4450 about relying on undefined signed overflow. We disable the
4451 warning here because we use integer types so fold won't know that
4452 they are really pointers. */
4453 fold_defer_overflow_warnings ();
4455 /* If what we are about to multiply by the size of the elements
4456 contains a constant term, apply distributive law
4457 and multiply that constant term separately.
4458 This helps produce common subexpressions. */
4459 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4460 && !TREE_CONSTANT (intop)
4461 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4462 && TREE_CONSTANT (size_exp)
4463 /* If the constant comes from pointer subtraction,
4464 skip this optimization--it would cause an error. */
4465 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4466 /* If the constant is unsigned, and smaller than the pointer size,
4467 then we must skip this optimization. This is because it could cause
4468 an overflow error if the constant is negative but INTOP is not. */
4469 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4470 || (TYPE_PRECISION (TREE_TYPE (intop))
4471 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4473 enum tree_code subcode = resultcode;
4474 tree int_type = TREE_TYPE (intop);
4475 if (TREE_CODE (intop) == MINUS_EXPR)
4476 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4477 /* Convert both subexpression types to the type of intop,
4478 because weird cases involving pointer arithmetic
4479 can result in a sum or difference with different type args. */
4480 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4481 subcode, ptrop,
4482 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4483 intop = convert (int_type, TREE_OPERAND (intop, 0));
4486 /* Convert the integer argument to a type the same size as sizetype
4487 so the multiply won't overflow spuriously. */
4488 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4489 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4490 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4491 TYPE_UNSIGNED (sizetype)), intop);
4493 /* Replace the integer argument with a suitable product by the object size.
4494 Do this multiplication as signed, then convert to the appropriate type
4495 for the pointer operation and disregard an overflow that occurred only
4496 because of the sign-extension change in the latter conversion. */
4498 tree t = build_binary_op (loc,
4499 MULT_EXPR, intop,
4500 convert (TREE_TYPE (intop), size_exp), 1);
4501 intop = convert (sizetype, t);
4502 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4503 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4506 /* Create the sum or difference. */
4507 if (resultcode == MINUS_EXPR)
4508 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4510 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4512 fold_undefer_and_ignore_overflow_warnings ();
4514 return ret;
4517 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4518 and if NON_CONST is known not to be permitted in an evaluated part
4519 of a constant expression. */
4521 tree
4522 c_wrap_maybe_const (tree expr, bool non_const)
4524 bool nowarning = TREE_NO_WARNING (expr);
4525 location_t loc = EXPR_LOCATION (expr);
4527 /* This should never be called for C++. */
4528 if (c_dialect_cxx ())
4529 gcc_unreachable ();
4531 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4532 STRIP_TYPE_NOPS (expr);
4533 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4534 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4535 if (nowarning)
4536 TREE_NO_WARNING (expr) = 1;
4537 protected_set_expr_location (expr, loc);
4539 return expr;
4542 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4543 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4544 around the SAVE_EXPR if needed so that c_fully_fold does not need
4545 to look inside SAVE_EXPRs. */
4547 tree
4548 c_save_expr (tree expr)
4550 bool maybe_const = true;
4551 if (c_dialect_cxx ())
4552 return save_expr (expr);
4553 expr = c_fully_fold (expr, false, &maybe_const);
4554 expr = save_expr (expr);
4555 if (!maybe_const)
4556 expr = c_wrap_maybe_const (expr, true);
4557 return expr;
4560 /* Return whether EXPR is a declaration whose address can never be
4561 NULL. */
4563 bool
4564 decl_with_nonnull_addr_p (const_tree expr)
4566 return (DECL_P (expr)
4567 && (TREE_CODE (expr) == PARM_DECL
4568 || TREE_CODE (expr) == LABEL_DECL
4569 || !DECL_WEAK (expr)));
4572 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4573 or for an `if' or `while' statement or ?..: exp. It should already
4574 have been validated to be of suitable type; otherwise, a bad
4575 diagnostic may result.
4577 The EXPR is located at LOCATION.
4579 This preparation consists of taking the ordinary
4580 representation of an expression expr and producing a valid tree
4581 boolean expression describing whether expr is nonzero. We could
4582 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4583 but we optimize comparisons, &&, ||, and !.
4585 The resulting type should always be `truthvalue_type_node'. */
4587 tree
4588 c_common_truthvalue_conversion (location_t location, tree expr)
4590 switch (TREE_CODE (expr))
4592 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4593 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4594 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4595 case ORDERED_EXPR: case UNORDERED_EXPR:
4596 if (TREE_TYPE (expr) == truthvalue_type_node)
4597 return expr;
4598 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4599 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4600 goto ret;
4602 case TRUTH_ANDIF_EXPR:
4603 case TRUTH_ORIF_EXPR:
4604 case TRUTH_AND_EXPR:
4605 case TRUTH_OR_EXPR:
4606 case TRUTH_XOR_EXPR:
4607 if (TREE_TYPE (expr) == truthvalue_type_node)
4608 return expr;
4609 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4610 c_common_truthvalue_conversion (location,
4611 TREE_OPERAND (expr, 0)),
4612 c_common_truthvalue_conversion (location,
4613 TREE_OPERAND (expr, 1)));
4614 goto ret;
4616 case TRUTH_NOT_EXPR:
4617 if (TREE_TYPE (expr) == truthvalue_type_node)
4618 return expr;
4619 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4620 c_common_truthvalue_conversion (location,
4621 TREE_OPERAND (expr, 0)));
4622 goto ret;
4624 case ERROR_MARK:
4625 return expr;
4627 case INTEGER_CST:
4628 return integer_zerop (expr) ? truthvalue_false_node
4629 : truthvalue_true_node;
4631 case REAL_CST:
4632 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4633 ? truthvalue_true_node
4634 : truthvalue_false_node;
4636 case FIXED_CST:
4637 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4638 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4639 ? truthvalue_true_node
4640 : truthvalue_false_node;
4642 case FUNCTION_DECL:
4643 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4644 /* Fall through. */
4646 case ADDR_EXPR:
4648 tree inner = TREE_OPERAND (expr, 0);
4649 if (decl_with_nonnull_addr_p (inner))
4651 /* Common Ada/Pascal programmer's mistake. */
4652 warning_at (location,
4653 OPT_Waddress,
4654 "the address of %qD will always evaluate as %<true%>",
4655 inner);
4656 return truthvalue_true_node;
4658 break;
4661 case COMPLEX_EXPR:
4662 expr = build_binary_op (EXPR_LOCATION (expr),
4663 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4664 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4665 c_common_truthvalue_conversion (location,
4666 TREE_OPERAND (expr, 0)),
4667 c_common_truthvalue_conversion (location,
4668 TREE_OPERAND (expr, 1)),
4670 goto ret;
4672 case NEGATE_EXPR:
4673 case ABS_EXPR:
4674 case FLOAT_EXPR:
4675 case EXCESS_PRECISION_EXPR:
4676 /* These don't change whether an object is nonzero or zero. */
4677 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4679 case LROTATE_EXPR:
4680 case RROTATE_EXPR:
4681 /* These don't change whether an object is zero or nonzero, but
4682 we can't ignore them if their second arg has side-effects. */
4683 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4685 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4686 TREE_OPERAND (expr, 1),
4687 c_common_truthvalue_conversion
4688 (location, TREE_OPERAND (expr, 0)));
4689 goto ret;
4691 else
4692 return c_common_truthvalue_conversion (location,
4693 TREE_OPERAND (expr, 0));
4695 case COND_EXPR:
4696 /* Distribute the conversion into the arms of a COND_EXPR. */
4697 if (c_dialect_cxx ())
4699 tree op1 = TREE_OPERAND (expr, 1);
4700 tree op2 = TREE_OPERAND (expr, 2);
4701 /* In C++ one of the arms might have void type if it is throw. */
4702 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4703 op1 = c_common_truthvalue_conversion (location, op1);
4704 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4705 op2 = c_common_truthvalue_conversion (location, op2);
4706 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4707 TREE_OPERAND (expr, 0), op1, op2);
4708 goto ret;
4710 else
4712 /* Folding will happen later for C. */
4713 expr = build3 (COND_EXPR, truthvalue_type_node,
4714 TREE_OPERAND (expr, 0),
4715 c_common_truthvalue_conversion (location,
4716 TREE_OPERAND (expr, 1)),
4717 c_common_truthvalue_conversion (location,
4718 TREE_OPERAND (expr, 2)));
4719 goto ret;
4722 CASE_CONVERT:
4724 tree totype = TREE_TYPE (expr);
4725 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4727 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4728 since that affects how `default_conversion' will behave. */
4729 if (TREE_CODE (totype) == REFERENCE_TYPE
4730 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4731 break;
4732 /* Don't strip a conversion from C++0x scoped enum, since they
4733 don't implicitly convert to other types. */
4734 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4735 && ENUM_IS_SCOPED (fromtype))
4736 break;
4737 /* If this isn't narrowing the argument, we can ignore it. */
4738 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4739 return c_common_truthvalue_conversion (location,
4740 TREE_OPERAND (expr, 0));
4742 break;
4744 case MODIFY_EXPR:
4745 if (!TREE_NO_WARNING (expr)
4746 && warn_parentheses)
4748 warning (OPT_Wparentheses,
4749 "suggest parentheses around assignment used as truth value");
4750 TREE_NO_WARNING (expr) = 1;
4752 break;
4754 default:
4755 break;
4758 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4760 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4761 expr = (build_binary_op
4762 (EXPR_LOCATION (expr),
4763 (TREE_SIDE_EFFECTS (expr)
4764 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4765 c_common_truthvalue_conversion
4766 (location,
4767 build_unary_op (location, REALPART_EXPR, t, 0)),
4768 c_common_truthvalue_conversion
4769 (location,
4770 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4771 0));
4772 goto ret;
4775 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4777 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4778 FCONST0 (TYPE_MODE
4779 (TREE_TYPE (expr))));
4780 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4782 else
4783 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4785 ret:
4786 protected_set_expr_location (expr, location);
4787 return expr;
4790 static void def_builtin_1 (enum built_in_function fncode,
4791 const char *name,
4792 enum built_in_class fnclass,
4793 tree fntype, tree libtype,
4794 bool both_p, bool fallback_p, bool nonansi_p,
4795 tree fnattrs, bool implicit_p);
4798 /* Apply the TYPE_QUALS to the new DECL. */
4800 void
4801 c_apply_type_quals_to_decl (int type_quals, tree decl)
4803 tree type = TREE_TYPE (decl);
4805 if (type == error_mark_node)
4806 return;
4808 if ((type_quals & TYPE_QUAL_CONST)
4809 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4810 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4811 constructor can produce constant init, so rely on cp_finish_decl to
4812 clear TREE_READONLY if the variable has non-constant init. */
4813 TREE_READONLY (decl) = 1;
4814 if (type_quals & TYPE_QUAL_VOLATILE)
4816 TREE_SIDE_EFFECTS (decl) = 1;
4817 TREE_THIS_VOLATILE (decl) = 1;
4819 if (type_quals & TYPE_QUAL_RESTRICT)
4821 while (type && TREE_CODE (type) == ARRAY_TYPE)
4822 /* Allow 'restrict' on arrays of pointers.
4823 FIXME currently we just ignore it. */
4824 type = TREE_TYPE (type);
4825 if (!type
4826 || !POINTER_TYPE_P (type)
4827 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4828 error ("invalid use of %<restrict%>");
4832 struct c_type_hasher : ggc_hasher<tree>
4834 static hashval_t hash (tree);
4835 static bool equal (tree, tree);
4838 /* Hash function for the problem of multiple type definitions in
4839 different files. This must hash all types that will compare
4840 equal via comptypes to the same value. In practice it hashes
4841 on some of the simple stuff and leaves the details to comptypes. */
4843 hashval_t
4844 c_type_hasher::hash (tree t)
4846 int n_elements;
4847 int shift, size;
4848 tree t2;
4849 switch (TREE_CODE (t))
4851 /* For pointers, hash on pointee type plus some swizzling. */
4852 case POINTER_TYPE:
4853 return hash (TREE_TYPE (t)) ^ 0x3003003;
4854 /* Hash on number of elements and total size. */
4855 case ENUMERAL_TYPE:
4856 shift = 3;
4857 t2 = TYPE_VALUES (t);
4858 break;
4859 case RECORD_TYPE:
4860 shift = 0;
4861 t2 = TYPE_FIELDS (t);
4862 break;
4863 case QUAL_UNION_TYPE:
4864 shift = 1;
4865 t2 = TYPE_FIELDS (t);
4866 break;
4867 case UNION_TYPE:
4868 shift = 2;
4869 t2 = TYPE_FIELDS (t);
4870 break;
4871 default:
4872 gcc_unreachable ();
4874 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4875 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4876 n_elements = list_length (t2);
4877 /* We might have a VLA here. */
4878 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4879 size = 0;
4880 else
4881 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4882 return ((size << 24) | (n_elements << shift));
4885 bool
4886 c_type_hasher::equal (tree t1, tree t2)
4888 return lang_hooks.types_compatible_p (t1, t2);
4891 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4893 /* Return the typed-based alias set for T, which may be an expression
4894 or a type. Return -1 if we don't do anything special. */
4896 alias_set_type
4897 c_common_get_alias_set (tree t)
4899 tree u;
4901 /* For VLAs, use the alias set of the element type rather than the
4902 default of alias set 0 for types compared structurally. */
4903 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4905 if (TREE_CODE (t) == ARRAY_TYPE)
4906 return get_alias_set (TREE_TYPE (t));
4907 return -1;
4910 /* Permit type-punning when accessing a union, provided the access
4911 is directly through the union. For example, this code does not
4912 permit taking the address of a union member and then storing
4913 through it. Even the type-punning allowed here is a GCC
4914 extension, albeit a common and useful one; the C standard says
4915 that such accesses have implementation-defined behavior. */
4916 for (u = t;
4917 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4918 u = TREE_OPERAND (u, 0))
4919 if (TREE_CODE (u) == COMPONENT_REF
4920 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4921 return 0;
4923 /* That's all the expressions we handle specially. */
4924 if (!TYPE_P (t))
4925 return -1;
4927 /* The C standard guarantees that any object may be accessed via an
4928 lvalue that has character type. */
4929 if (t == char_type_node
4930 || t == signed_char_type_node
4931 || t == unsigned_char_type_node)
4932 return 0;
4934 /* The C standard specifically allows aliasing between signed and
4935 unsigned variants of the same type. We treat the signed
4936 variant as canonical. */
4937 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4939 tree t1 = c_common_signed_type (t);
4941 /* t1 == t can happen for boolean nodes which are always unsigned. */
4942 if (t1 != t)
4943 return get_alias_set (t1);
4946 /* Handle the case of multiple type nodes referring to "the same" type,
4947 which occurs with IMA. These share an alias set. FIXME: Currently only
4948 C90 is handled. (In C99 type compatibility is not transitive, which
4949 complicates things mightily. The alias set splay trees can theoretically
4950 represent this, but insertion is tricky when you consider all the
4951 different orders things might arrive in.) */
4953 if (c_language != clk_c || flag_isoc99)
4954 return -1;
4956 /* Save time if there's only one input file. */
4957 if (num_in_fnames == 1)
4958 return -1;
4960 /* Pointers need special handling if they point to any type that
4961 needs special handling (below). */
4962 if (TREE_CODE (t) == POINTER_TYPE)
4964 tree t2;
4965 /* Find bottom type under any nested POINTERs. */
4966 for (t2 = TREE_TYPE (t);
4967 TREE_CODE (t2) == POINTER_TYPE;
4968 t2 = TREE_TYPE (t2))
4970 if (TREE_CODE (t2) != RECORD_TYPE
4971 && TREE_CODE (t2) != ENUMERAL_TYPE
4972 && TREE_CODE (t2) != QUAL_UNION_TYPE
4973 && TREE_CODE (t2) != UNION_TYPE)
4974 return -1;
4975 if (TYPE_SIZE (t2) == 0)
4976 return -1;
4978 /* These are the only cases that need special handling. */
4979 if (TREE_CODE (t) != RECORD_TYPE
4980 && TREE_CODE (t) != ENUMERAL_TYPE
4981 && TREE_CODE (t) != QUAL_UNION_TYPE
4982 && TREE_CODE (t) != UNION_TYPE
4983 && TREE_CODE (t) != POINTER_TYPE)
4984 return -1;
4985 /* Undefined? */
4986 if (TYPE_SIZE (t) == 0)
4987 return -1;
4989 /* Look up t in hash table. Only one of the compatible types within each
4990 alias set is recorded in the table. */
4991 if (!type_hash_table)
4992 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4993 tree *slot = type_hash_table->find_slot (t, INSERT);
4994 if (*slot != NULL)
4996 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4997 return TYPE_ALIAS_SET ((tree)*slot);
4999 else
5000 /* Our caller will assign and record (in t) a new alias set; all we need
5001 to do is remember t in the hash table. */
5002 *slot = t;
5004 return -1;
5007 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5008 the IS_SIZEOF parameter indicates which operator is being applied.
5009 The COMPLAIN flag controls whether we should diagnose possibly
5010 ill-formed constructs or not. LOC is the location of the SIZEOF or
5011 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5012 a type in any context should be returned, rather than the normal
5013 alignment for that type. */
5015 tree
5016 c_sizeof_or_alignof_type (location_t loc,
5017 tree type, bool is_sizeof, bool min_alignof,
5018 int complain)
5020 const char *op_name;
5021 tree value = NULL;
5022 enum tree_code type_code = TREE_CODE (type);
5024 op_name = is_sizeof ? "sizeof" : "__alignof__";
5026 if (type_code == FUNCTION_TYPE)
5028 if (is_sizeof)
5030 if (complain && warn_pointer_arith)
5031 pedwarn (loc, OPT_Wpointer_arith,
5032 "invalid application of %<sizeof%> to a function type");
5033 else if (!complain)
5034 return error_mark_node;
5035 value = size_one_node;
5037 else
5039 if (complain)
5041 if (c_dialect_cxx ())
5042 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5043 "%<alignof%> applied to a function type");
5044 else
5045 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5046 "%<_Alignof%> applied to a function type");
5048 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5051 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5053 if (type_code == VOID_TYPE
5054 && complain && warn_pointer_arith)
5055 pedwarn (loc, OPT_Wpointer_arith,
5056 "invalid application of %qs to a void type", op_name);
5057 else if (!complain)
5058 return error_mark_node;
5059 value = size_one_node;
5061 else if (!COMPLETE_TYPE_P (type)
5062 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5064 if (complain)
5065 error_at (loc, "invalid application of %qs to incomplete type %qT",
5066 op_name, type);
5067 return error_mark_node;
5069 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5070 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5072 if (complain)
5073 error_at (loc, "invalid application of %qs to array type %qT of "
5074 "incomplete element type", op_name, type);
5075 return error_mark_node;
5077 else
5079 if (is_sizeof)
5080 /* Convert in case a char is more than one unit. */
5081 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5082 size_int (TYPE_PRECISION (char_type_node)
5083 / BITS_PER_UNIT));
5084 else if (min_alignof)
5085 value = size_int (min_align_of_type (type));
5086 else
5087 value = size_int (TYPE_ALIGN_UNIT (type));
5090 /* VALUE will have the middle-end integer type sizetype.
5091 However, we should really return a value of type `size_t',
5092 which is just a typedef for an ordinary integer type. */
5093 value = fold_convert_loc (loc, size_type_node, value);
5095 return value;
5098 /* Implement the __alignof keyword: Return the minimum required
5099 alignment of EXPR, measured in bytes. For VAR_DECLs,
5100 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5101 from an "aligned" __attribute__ specification). LOC is the
5102 location of the ALIGNOF operator. */
5104 tree
5105 c_alignof_expr (location_t loc, tree expr)
5107 tree t;
5109 if (VAR_OR_FUNCTION_DECL_P (expr))
5110 t = size_int (DECL_ALIGN_UNIT (expr));
5112 else if (TREE_CODE (expr) == COMPONENT_REF
5113 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5115 error_at (loc, "%<__alignof%> applied to a bit-field");
5116 t = size_one_node;
5118 else if (TREE_CODE (expr) == COMPONENT_REF
5119 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5120 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5122 else if (TREE_CODE (expr) == INDIRECT_REF)
5124 tree t = TREE_OPERAND (expr, 0);
5125 tree best = t;
5126 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5128 while (CONVERT_EXPR_P (t)
5129 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5131 int thisalign;
5133 t = TREE_OPERAND (t, 0);
5134 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5135 if (thisalign > bestalign)
5136 best = t, bestalign = thisalign;
5138 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5140 else
5141 return c_alignof (loc, TREE_TYPE (expr));
5143 return fold_convert_loc (loc, size_type_node, t);
5146 /* Handle C and C++ default attributes. */
5148 enum built_in_attribute
5150 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5151 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5152 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5153 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5154 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5155 #include "builtin-attrs.def"
5156 #undef DEF_ATTR_NULL_TREE
5157 #undef DEF_ATTR_INT
5158 #undef DEF_ATTR_STRING
5159 #undef DEF_ATTR_IDENT
5160 #undef DEF_ATTR_TREE_LIST
5161 ATTR_LAST
5164 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5166 static void c_init_attributes (void);
5168 enum c_builtin_type
5170 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5171 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5172 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5173 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5174 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5175 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5176 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5177 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5178 ARG6) NAME,
5179 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5180 ARG6, ARG7) NAME,
5181 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5182 ARG6, ARG7, ARG8) NAME,
5183 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5184 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5185 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5186 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5187 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5188 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5189 NAME,
5190 #define DEF_FUNCTION_TYPE_VAR_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5191 ARG6, ARG7, ARG8) NAME,
5192 #define DEF_FUNCTION_TYPE_VAR_12(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5193 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11, \
5194 ARG12) NAME,
5195 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5196 #include "builtin-types.def"
5197 #undef DEF_PRIMITIVE_TYPE
5198 #undef DEF_FUNCTION_TYPE_0
5199 #undef DEF_FUNCTION_TYPE_1
5200 #undef DEF_FUNCTION_TYPE_2
5201 #undef DEF_FUNCTION_TYPE_3
5202 #undef DEF_FUNCTION_TYPE_4
5203 #undef DEF_FUNCTION_TYPE_5
5204 #undef DEF_FUNCTION_TYPE_6
5205 #undef DEF_FUNCTION_TYPE_7
5206 #undef DEF_FUNCTION_TYPE_8
5207 #undef DEF_FUNCTION_TYPE_VAR_0
5208 #undef DEF_FUNCTION_TYPE_VAR_1
5209 #undef DEF_FUNCTION_TYPE_VAR_2
5210 #undef DEF_FUNCTION_TYPE_VAR_3
5211 #undef DEF_FUNCTION_TYPE_VAR_4
5212 #undef DEF_FUNCTION_TYPE_VAR_5
5213 #undef DEF_FUNCTION_TYPE_VAR_8
5214 #undef DEF_FUNCTION_TYPE_VAR_12
5215 #undef DEF_POINTER_TYPE
5216 BT_LAST
5219 typedef enum c_builtin_type builtin_type;
5221 /* A temporary array for c_common_nodes_and_builtins. Used in
5222 communication with def_fn_type. */
5223 static tree builtin_types[(int) BT_LAST + 1];
5225 /* A helper function for c_common_nodes_and_builtins. Build function type
5226 for DEF with return type RET and N arguments. If VAR is true, then the
5227 function should be variadic after those N arguments.
5229 Takes special care not to ICE if any of the types involved are
5230 error_mark_node, which indicates that said type is not in fact available
5231 (see builtin_type_for_size). In which case the function type as a whole
5232 should be error_mark_node. */
5234 static void
5235 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5237 tree t;
5238 tree *args = XALLOCAVEC (tree, n);
5239 va_list list;
5240 int i;
5242 va_start (list, n);
5243 for (i = 0; i < n; ++i)
5245 builtin_type a = (builtin_type) va_arg (list, int);
5246 t = builtin_types[a];
5247 if (t == error_mark_node)
5248 goto egress;
5249 args[i] = t;
5252 t = builtin_types[ret];
5253 if (t == error_mark_node)
5254 goto egress;
5255 if (var)
5256 t = build_varargs_function_type_array (t, n, args);
5257 else
5258 t = build_function_type_array (t, n, args);
5260 egress:
5261 builtin_types[def] = t;
5262 va_end (list);
5265 /* Build builtin functions common to both C and C++ language
5266 frontends. */
5268 static void
5269 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5271 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5272 builtin_types[ENUM] = VALUE;
5273 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5274 def_fn_type (ENUM, RETURN, 0, 0);
5275 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5276 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5277 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5278 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5279 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5280 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5281 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5282 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5283 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5284 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5285 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5286 ARG6) \
5287 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5288 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5289 ARG6, ARG7) \
5290 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5291 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5292 ARG6, ARG7, ARG8) \
5293 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5294 ARG7, ARG8);
5295 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5296 def_fn_type (ENUM, RETURN, 1, 0);
5297 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5298 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5299 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5300 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5301 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5302 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5303 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5304 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5305 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5306 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5307 #define DEF_FUNCTION_TYPE_VAR_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5308 ARG6, ARG7, ARG8) \
5309 def_fn_type (ENUM, RETURN, 1, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5310 ARG7, ARG8);
5311 #define DEF_FUNCTION_TYPE_VAR_12(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5312 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11, ARG12) \
5313 def_fn_type (ENUM, RETURN, 1, 12, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5314 ARG7, ARG8, ARG9, ARG10, ARG11, ARG12);
5315 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5316 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5318 #include "builtin-types.def"
5320 #undef DEF_PRIMITIVE_TYPE
5321 #undef DEF_FUNCTION_TYPE_0
5322 #undef DEF_FUNCTION_TYPE_1
5323 #undef DEF_FUNCTION_TYPE_2
5324 #undef DEF_FUNCTION_TYPE_3
5325 #undef DEF_FUNCTION_TYPE_4
5326 #undef DEF_FUNCTION_TYPE_5
5327 #undef DEF_FUNCTION_TYPE_6
5328 #undef DEF_FUNCTION_TYPE_7
5329 #undef DEF_FUNCTION_TYPE_8
5330 #undef DEF_FUNCTION_TYPE_VAR_0
5331 #undef DEF_FUNCTION_TYPE_VAR_1
5332 #undef DEF_FUNCTION_TYPE_VAR_2
5333 #undef DEF_FUNCTION_TYPE_VAR_3
5334 #undef DEF_FUNCTION_TYPE_VAR_4
5335 #undef DEF_FUNCTION_TYPE_VAR_5
5336 #undef DEF_FUNCTION_TYPE_VAR_8
5337 #undef DEF_FUNCTION_TYPE_VAR_12
5338 #undef DEF_POINTER_TYPE
5339 builtin_types[(int) BT_LAST] = NULL_TREE;
5341 c_init_attributes ();
5343 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5344 NONANSI_P, ATTRS, IMPLICIT, COND) \
5345 if (NAME && COND) \
5346 def_builtin_1 (ENUM, NAME, CLASS, \
5347 builtin_types[(int) TYPE], \
5348 builtin_types[(int) LIBTYPE], \
5349 BOTH_P, FALLBACK_P, NONANSI_P, \
5350 built_in_attributes[(int) ATTRS], IMPLICIT);
5351 #include "builtins.def"
5352 #undef DEF_BUILTIN
5354 targetm.init_builtins ();
5356 build_common_builtin_nodes ();
5358 if (flag_cilkplus)
5359 cilk_init_builtins ();
5362 /* Like get_identifier, but avoid warnings about null arguments when
5363 the argument may be NULL for targets where GCC lacks stdint.h type
5364 information. */
5366 static inline tree
5367 c_get_ident (const char *id)
5369 return get_identifier (id);
5372 /* Build tree nodes and builtin functions common to both C and C++ language
5373 frontends. */
5375 void
5376 c_common_nodes_and_builtins (void)
5378 int char16_type_size;
5379 int char32_type_size;
5380 int wchar_type_size;
5381 tree array_domain_type;
5382 tree va_list_ref_type_node;
5383 tree va_list_arg_type_node;
5384 int i;
5386 build_common_tree_nodes (flag_signed_char, flag_short_double);
5388 /* Define `int' and `char' first so that dbx will output them first. */
5389 record_builtin_type (RID_INT, NULL, integer_type_node);
5390 record_builtin_type (RID_CHAR, "char", char_type_node);
5392 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5393 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5394 but not C. Are the conditionals here needed? */
5395 if (c_dialect_cxx ())
5396 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5397 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5398 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5399 record_builtin_type (RID_MAX, "long unsigned int",
5400 long_unsigned_type_node);
5402 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5404 char name[25];
5406 sprintf (name, "__int%d", int_n_data[i].bitsize);
5407 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), xstrdup (name),
5408 int_n_trees[i].signed_type);
5409 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5410 record_builtin_type (RID_MAX, xstrdup (name),
5411 int_n_trees[i].unsigned_type);
5414 if (c_dialect_cxx ())
5415 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5416 record_builtin_type (RID_MAX, "long long int",
5417 long_long_integer_type_node);
5418 record_builtin_type (RID_MAX, "long long unsigned int",
5419 long_long_unsigned_type_node);
5420 if (c_dialect_cxx ())
5421 record_builtin_type (RID_MAX, "long long unsigned",
5422 long_long_unsigned_type_node);
5423 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5424 record_builtin_type (RID_MAX, "short unsigned int",
5425 short_unsigned_type_node);
5426 if (c_dialect_cxx ())
5427 record_builtin_type (RID_MAX, "unsigned short",
5428 short_unsigned_type_node);
5430 /* Define both `signed char' and `unsigned char'. */
5431 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5432 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5434 /* These are types that c_common_type_for_size and
5435 c_common_type_for_mode use. */
5436 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5437 TYPE_DECL, NULL_TREE,
5438 intQI_type_node));
5439 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5440 TYPE_DECL, NULL_TREE,
5441 intHI_type_node));
5442 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5443 TYPE_DECL, NULL_TREE,
5444 intSI_type_node));
5445 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5446 TYPE_DECL, NULL_TREE,
5447 intDI_type_node));
5448 #if HOST_BITS_PER_WIDE_INT >= 64
5449 /* Note that this is different than the __int128 type that's part of
5450 the generic __intN support. */
5451 if (targetm.scalar_mode_supported_p (TImode))
5452 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5453 TYPE_DECL,
5454 get_identifier ("__int128_t"),
5455 intTI_type_node));
5456 #endif
5457 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5458 TYPE_DECL, NULL_TREE,
5459 unsigned_intQI_type_node));
5460 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5461 TYPE_DECL, NULL_TREE,
5462 unsigned_intHI_type_node));
5463 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5464 TYPE_DECL, NULL_TREE,
5465 unsigned_intSI_type_node));
5466 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5467 TYPE_DECL, NULL_TREE,
5468 unsigned_intDI_type_node));
5469 #if HOST_BITS_PER_WIDE_INT >= 64
5470 if (targetm.scalar_mode_supported_p (TImode))
5471 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5472 TYPE_DECL,
5473 get_identifier ("__uint128_t"),
5474 unsigned_intTI_type_node));
5475 #endif
5477 /* Create the widest literal types. */
5478 widest_integer_literal_type_node
5479 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5480 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5481 TYPE_DECL, NULL_TREE,
5482 widest_integer_literal_type_node));
5484 widest_unsigned_literal_type_node
5485 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5486 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5487 TYPE_DECL, NULL_TREE,
5488 widest_unsigned_literal_type_node));
5490 signed_size_type_node = c_common_signed_type (size_type_node);
5492 pid_type_node =
5493 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5495 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5496 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5497 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5499 /* Only supported decimal floating point extension if the target
5500 actually supports underlying modes. */
5501 if (targetm.scalar_mode_supported_p (SDmode)
5502 && targetm.scalar_mode_supported_p (DDmode)
5503 && targetm.scalar_mode_supported_p (TDmode))
5505 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5506 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5507 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5510 if (targetm.fixed_point_supported_p ())
5512 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5513 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5514 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5515 record_builtin_type (RID_MAX, "long long _Fract",
5516 long_long_fract_type_node);
5517 record_builtin_type (RID_MAX, "unsigned short _Fract",
5518 unsigned_short_fract_type_node);
5519 record_builtin_type (RID_MAX, "unsigned _Fract",
5520 unsigned_fract_type_node);
5521 record_builtin_type (RID_MAX, "unsigned long _Fract",
5522 unsigned_long_fract_type_node);
5523 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5524 unsigned_long_long_fract_type_node);
5525 record_builtin_type (RID_MAX, "_Sat short _Fract",
5526 sat_short_fract_type_node);
5527 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5528 record_builtin_type (RID_MAX, "_Sat long _Fract",
5529 sat_long_fract_type_node);
5530 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5531 sat_long_long_fract_type_node);
5532 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5533 sat_unsigned_short_fract_type_node);
5534 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5535 sat_unsigned_fract_type_node);
5536 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5537 sat_unsigned_long_fract_type_node);
5538 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5539 sat_unsigned_long_long_fract_type_node);
5540 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5541 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5542 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5543 record_builtin_type (RID_MAX, "long long _Accum",
5544 long_long_accum_type_node);
5545 record_builtin_type (RID_MAX, "unsigned short _Accum",
5546 unsigned_short_accum_type_node);
5547 record_builtin_type (RID_MAX, "unsigned _Accum",
5548 unsigned_accum_type_node);
5549 record_builtin_type (RID_MAX, "unsigned long _Accum",
5550 unsigned_long_accum_type_node);
5551 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5552 unsigned_long_long_accum_type_node);
5553 record_builtin_type (RID_MAX, "_Sat short _Accum",
5554 sat_short_accum_type_node);
5555 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5556 record_builtin_type (RID_MAX, "_Sat long _Accum",
5557 sat_long_accum_type_node);
5558 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5559 sat_long_long_accum_type_node);
5560 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5561 sat_unsigned_short_accum_type_node);
5562 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5563 sat_unsigned_accum_type_node);
5564 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5565 sat_unsigned_long_accum_type_node);
5566 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5567 sat_unsigned_long_long_accum_type_node);
5571 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5572 TYPE_DECL,
5573 get_identifier ("complex int"),
5574 complex_integer_type_node));
5575 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5576 TYPE_DECL,
5577 get_identifier ("complex float"),
5578 complex_float_type_node));
5579 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5580 TYPE_DECL,
5581 get_identifier ("complex double"),
5582 complex_double_type_node));
5583 lang_hooks.decls.pushdecl
5584 (build_decl (UNKNOWN_LOCATION,
5585 TYPE_DECL, get_identifier ("complex long double"),
5586 complex_long_double_type_node));
5588 if (c_dialect_cxx ())
5589 /* For C++, make fileptr_type_node a distinct void * type until
5590 FILE type is defined. */
5591 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5593 record_builtin_type (RID_VOID, NULL, void_type_node);
5595 /* Set the TYPE_NAME for any variants that were built before
5596 record_builtin_type gave names to the built-in types. */
5598 tree void_name = TYPE_NAME (void_type_node);
5599 TYPE_NAME (void_type_node) = NULL_TREE;
5600 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5601 = void_name;
5602 TYPE_NAME (void_type_node) = void_name;
5605 void_list_node = build_void_list_node ();
5607 /* Make a type to be the domain of a few array types
5608 whose domains don't really matter.
5609 200 is small enough that it always fits in size_t
5610 and large enough that it can hold most function names for the
5611 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5612 array_domain_type = build_index_type (size_int (200));
5614 /* Make a type for arrays of characters.
5615 With luck nothing will ever really depend on the length of this
5616 array type. */
5617 char_array_type_node
5618 = build_array_type (char_type_node, array_domain_type);
5620 string_type_node = build_pointer_type (char_type_node);
5621 const_string_type_node
5622 = build_pointer_type (build_qualified_type
5623 (char_type_node, TYPE_QUAL_CONST));
5625 /* This is special for C++ so functions can be overloaded. */
5626 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5627 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5628 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5629 underlying_wchar_type_node = wchar_type_node;
5630 if (c_dialect_cxx ())
5632 if (TYPE_UNSIGNED (wchar_type_node))
5633 wchar_type_node = make_unsigned_type (wchar_type_size);
5634 else
5635 wchar_type_node = make_signed_type (wchar_type_size);
5636 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5639 /* This is for wide string constants. */
5640 wchar_array_type_node
5641 = build_array_type (wchar_type_node, array_domain_type);
5643 /* Define 'char16_t'. */
5644 char16_type_node = get_identifier (CHAR16_TYPE);
5645 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5646 char16_type_size = TYPE_PRECISION (char16_type_node);
5647 if (c_dialect_cxx ())
5649 char16_type_node = make_unsigned_type (char16_type_size);
5651 if (cxx_dialect >= cxx11)
5652 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5655 /* This is for UTF-16 string constants. */
5656 char16_array_type_node
5657 = build_array_type (char16_type_node, array_domain_type);
5659 /* Define 'char32_t'. */
5660 char32_type_node = get_identifier (CHAR32_TYPE);
5661 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5662 char32_type_size = TYPE_PRECISION (char32_type_node);
5663 if (c_dialect_cxx ())
5665 char32_type_node = make_unsigned_type (char32_type_size);
5667 if (cxx_dialect >= cxx11)
5668 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5671 /* This is for UTF-32 string constants. */
5672 char32_array_type_node
5673 = build_array_type (char32_type_node, array_domain_type);
5675 wint_type_node =
5676 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5678 intmax_type_node =
5679 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5680 uintmax_type_node =
5681 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5683 if (SIG_ATOMIC_TYPE)
5684 sig_atomic_type_node =
5685 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5686 if (INT8_TYPE)
5687 int8_type_node =
5688 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5689 if (INT16_TYPE)
5690 int16_type_node =
5691 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5692 if (INT32_TYPE)
5693 int32_type_node =
5694 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5695 if (INT64_TYPE)
5696 int64_type_node =
5697 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5698 if (UINT8_TYPE)
5699 uint8_type_node =
5700 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5701 if (UINT16_TYPE)
5702 c_uint16_type_node = uint16_type_node =
5703 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5704 if (UINT32_TYPE)
5705 c_uint32_type_node = uint32_type_node =
5706 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5707 if (UINT64_TYPE)
5708 c_uint64_type_node = uint64_type_node =
5709 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5710 if (INT_LEAST8_TYPE)
5711 int_least8_type_node =
5712 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5713 if (INT_LEAST16_TYPE)
5714 int_least16_type_node =
5715 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5716 if (INT_LEAST32_TYPE)
5717 int_least32_type_node =
5718 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5719 if (INT_LEAST64_TYPE)
5720 int_least64_type_node =
5721 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5722 if (UINT_LEAST8_TYPE)
5723 uint_least8_type_node =
5724 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5725 if (UINT_LEAST16_TYPE)
5726 uint_least16_type_node =
5727 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5728 if (UINT_LEAST32_TYPE)
5729 uint_least32_type_node =
5730 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5731 if (UINT_LEAST64_TYPE)
5732 uint_least64_type_node =
5733 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5734 if (INT_FAST8_TYPE)
5735 int_fast8_type_node =
5736 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5737 if (INT_FAST16_TYPE)
5738 int_fast16_type_node =
5739 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5740 if (INT_FAST32_TYPE)
5741 int_fast32_type_node =
5742 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5743 if (INT_FAST64_TYPE)
5744 int_fast64_type_node =
5745 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5746 if (UINT_FAST8_TYPE)
5747 uint_fast8_type_node =
5748 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5749 if (UINT_FAST16_TYPE)
5750 uint_fast16_type_node =
5751 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5752 if (UINT_FAST32_TYPE)
5753 uint_fast32_type_node =
5754 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5755 if (UINT_FAST64_TYPE)
5756 uint_fast64_type_node =
5757 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5758 if (INTPTR_TYPE)
5759 intptr_type_node =
5760 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5761 if (UINTPTR_TYPE)
5762 uintptr_type_node =
5763 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5765 default_function_type
5766 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5767 ptrdiff_type_node
5768 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5769 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5771 lang_hooks.decls.pushdecl
5772 (build_decl (UNKNOWN_LOCATION,
5773 TYPE_DECL, get_identifier ("__builtin_va_list"),
5774 va_list_type_node));
5775 if (targetm.enum_va_list_p)
5777 int l;
5778 const char *pname;
5779 tree ptype;
5781 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5783 lang_hooks.decls.pushdecl
5784 (build_decl (UNKNOWN_LOCATION,
5785 TYPE_DECL, get_identifier (pname),
5786 ptype));
5791 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5793 va_list_arg_type_node = va_list_ref_type_node =
5794 build_pointer_type (TREE_TYPE (va_list_type_node));
5796 else
5798 va_list_arg_type_node = va_list_type_node;
5799 va_list_ref_type_node = build_reference_type (va_list_type_node);
5802 if (!flag_preprocess_only)
5803 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5805 main_identifier_node = get_identifier ("main");
5807 /* Create the built-in __null node. It is important that this is
5808 not shared. */
5809 null_node = make_int_cst (1, 1);
5810 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5812 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5813 memset (builtin_types, 0, sizeof (builtin_types));
5816 /* The number of named compound-literals generated thus far. */
5817 static GTY(()) int compound_literal_number;
5819 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5821 void
5822 set_compound_literal_name (tree decl)
5824 char *name;
5825 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5826 compound_literal_number);
5827 compound_literal_number++;
5828 DECL_NAME (decl) = get_identifier (name);
5831 tree
5832 build_va_arg (location_t loc, tree expr, tree type)
5834 expr = build1 (VA_ARG_EXPR, type, expr);
5835 SET_EXPR_LOCATION (expr, loc);
5836 return expr;
5840 /* Linked list of disabled built-in functions. */
5842 typedef struct disabled_builtin
5844 const char *name;
5845 struct disabled_builtin *next;
5846 } disabled_builtin;
5847 static disabled_builtin *disabled_builtins = NULL;
5849 static bool builtin_function_disabled_p (const char *);
5851 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5852 begins with "__builtin_", give an error. */
5854 void
5855 disable_builtin_function (const char *name)
5857 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5858 error ("cannot disable built-in function %qs", name);
5859 else
5861 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5862 new_disabled_builtin->name = name;
5863 new_disabled_builtin->next = disabled_builtins;
5864 disabled_builtins = new_disabled_builtin;
5869 /* Return true if the built-in function NAME has been disabled, false
5870 otherwise. */
5872 static bool
5873 builtin_function_disabled_p (const char *name)
5875 disabled_builtin *p;
5876 for (p = disabled_builtins; p != NULL; p = p->next)
5878 if (strcmp (name, p->name) == 0)
5879 return true;
5881 return false;
5885 /* Worker for DEF_BUILTIN.
5886 Possibly define a builtin function with one or two names.
5887 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5888 nonansi_p and flag_no_nonansi_builtin. */
5890 static void
5891 def_builtin_1 (enum built_in_function fncode,
5892 const char *name,
5893 enum built_in_class fnclass,
5894 tree fntype, tree libtype,
5895 bool both_p, bool fallback_p, bool nonansi_p,
5896 tree fnattrs, bool implicit_p)
5898 tree decl;
5899 const char *libname;
5901 if (fntype == error_mark_node)
5902 return;
5904 gcc_assert ((!both_p && !fallback_p)
5905 || !strncmp (name, "__builtin_",
5906 strlen ("__builtin_")));
5908 libname = name + strlen ("__builtin_");
5909 decl = add_builtin_function (name, fntype, fncode, fnclass,
5910 (fallback_p ? libname : NULL),
5911 fnattrs);
5913 set_builtin_decl (fncode, decl, implicit_p);
5915 if (both_p
5916 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5917 && !(nonansi_p && flag_no_nonansi_builtin))
5918 add_builtin_function (libname, libtype, fncode, fnclass,
5919 NULL, fnattrs);
5922 /* Nonzero if the type T promotes to int. This is (nearly) the
5923 integral promotions defined in ISO C99 6.3.1.1/2. */
5925 bool
5926 c_promoting_integer_type_p (const_tree t)
5928 switch (TREE_CODE (t))
5930 case INTEGER_TYPE:
5931 return (TYPE_MAIN_VARIANT (t) == char_type_node
5932 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5933 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5934 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5935 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5936 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5938 case ENUMERAL_TYPE:
5939 /* ??? Technically all enumerations not larger than an int
5940 promote to an int. But this is used along code paths
5941 that only want to notice a size change. */
5942 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5944 case BOOLEAN_TYPE:
5945 return 1;
5947 default:
5948 return 0;
5952 /* Return 1 if PARMS specifies a fixed number of parameters
5953 and none of their types is affected by default promotions. */
5956 self_promoting_args_p (const_tree parms)
5958 const_tree t;
5959 for (t = parms; t; t = TREE_CHAIN (t))
5961 tree type = TREE_VALUE (t);
5963 if (type == error_mark_node)
5964 continue;
5966 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5967 return 0;
5969 if (type == 0)
5970 return 0;
5972 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5973 return 0;
5975 if (c_promoting_integer_type_p (type))
5976 return 0;
5978 return 1;
5981 /* Recursively remove any '*' or '&' operator from TYPE. */
5982 tree
5983 strip_pointer_operator (tree t)
5985 while (POINTER_TYPE_P (t))
5986 t = TREE_TYPE (t);
5987 return t;
5990 /* Recursively remove pointer or array type from TYPE. */
5991 tree
5992 strip_pointer_or_array_types (tree t)
5994 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5995 t = TREE_TYPE (t);
5996 return t;
5999 /* Used to compare case labels. K1 and K2 are actually tree nodes
6000 representing case labels, or NULL_TREE for a `default' label.
6001 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6002 K2, and 0 if K1 and K2 are equal. */
6005 case_compare (splay_tree_key k1, splay_tree_key k2)
6007 /* Consider a NULL key (such as arises with a `default' label) to be
6008 smaller than anything else. */
6009 if (!k1)
6010 return k2 ? -1 : 0;
6011 else if (!k2)
6012 return k1 ? 1 : 0;
6014 return tree_int_cst_compare ((tree) k1, (tree) k2);
6017 /* Process a case label, located at LOC, for the range LOW_VALUE
6018 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6019 then this case label is actually a `default' label. If only
6020 HIGH_VALUE is NULL_TREE, then case label was declared using the
6021 usual C/C++ syntax, rather than the GNU case range extension.
6022 CASES is a tree containing all the case ranges processed so far;
6023 COND is the condition for the switch-statement itself. Returns the
6024 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6025 is created. */
6027 tree
6028 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6029 tree low_value, tree high_value)
6031 tree type;
6032 tree label;
6033 tree case_label;
6034 splay_tree_node node;
6036 /* Create the LABEL_DECL itself. */
6037 label = create_artificial_label (loc);
6039 /* If there was an error processing the switch condition, bail now
6040 before we get more confused. */
6041 if (!cond || cond == error_mark_node)
6042 goto error_out;
6044 if ((low_value && TREE_TYPE (low_value)
6045 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6046 || (high_value && TREE_TYPE (high_value)
6047 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6049 error_at (loc, "pointers are not permitted as case values");
6050 goto error_out;
6053 /* Case ranges are a GNU extension. */
6054 if (high_value)
6055 pedwarn (loc, OPT_Wpedantic,
6056 "range expressions in switch statements are non-standard");
6058 type = TREE_TYPE (cond);
6059 if (low_value)
6061 low_value = check_case_value (loc, low_value);
6062 low_value = convert_and_check (loc, type, low_value);
6063 if (low_value == error_mark_node)
6064 goto error_out;
6066 if (high_value)
6068 high_value = check_case_value (loc, high_value);
6069 high_value = convert_and_check (loc, type, high_value);
6070 if (high_value == error_mark_node)
6071 goto error_out;
6074 if (low_value && high_value)
6076 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6077 really a case range, even though it was written that way.
6078 Remove the HIGH_VALUE to simplify later processing. */
6079 if (tree_int_cst_equal (low_value, high_value))
6080 high_value = NULL_TREE;
6081 else if (!tree_int_cst_lt (low_value, high_value))
6082 warning_at (loc, 0, "empty range specified");
6085 /* See if the case is in range of the type of the original testing
6086 expression. If both low_value and high_value are out of range,
6087 don't insert the case label and return NULL_TREE. */
6088 if (low_value
6089 && !check_case_bounds (loc, type, orig_type,
6090 &low_value, high_value ? &high_value : NULL))
6091 return NULL_TREE;
6093 /* Look up the LOW_VALUE in the table of case labels we already
6094 have. */
6095 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6096 /* If there was not an exact match, check for overlapping ranges.
6097 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6098 that's a `default' label and the only overlap is an exact match. */
6099 if (!node && (low_value || high_value))
6101 splay_tree_node low_bound;
6102 splay_tree_node high_bound;
6104 /* Even though there wasn't an exact match, there might be an
6105 overlap between this case range and another case range.
6106 Since we've (inductively) not allowed any overlapping case
6107 ranges, we simply need to find the greatest low case label
6108 that is smaller that LOW_VALUE, and the smallest low case
6109 label that is greater than LOW_VALUE. If there is an overlap
6110 it will occur in one of these two ranges. */
6111 low_bound = splay_tree_predecessor (cases,
6112 (splay_tree_key) low_value);
6113 high_bound = splay_tree_successor (cases,
6114 (splay_tree_key) low_value);
6116 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6117 the LOW_VALUE, so there is no need to check unless the
6118 LOW_BOUND is in fact itself a case range. */
6119 if (low_bound
6120 && CASE_HIGH ((tree) low_bound->value)
6121 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6122 low_value) >= 0)
6123 node = low_bound;
6124 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6125 range is bigger than the low end of the current range, so we
6126 are only interested if the current range is a real range, and
6127 not an ordinary case label. */
6128 else if (high_bound
6129 && high_value
6130 && (tree_int_cst_compare ((tree) high_bound->key,
6131 high_value)
6132 <= 0))
6133 node = high_bound;
6135 /* If there was an overlap, issue an error. */
6136 if (node)
6138 tree duplicate = CASE_LABEL ((tree) node->value);
6140 if (high_value)
6142 error_at (loc, "duplicate (or overlapping) case value");
6143 error_at (DECL_SOURCE_LOCATION (duplicate),
6144 "this is the first entry overlapping that value");
6146 else if (low_value)
6148 error_at (loc, "duplicate case value") ;
6149 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6151 else
6153 error_at (loc, "multiple default labels in one switch");
6154 error_at (DECL_SOURCE_LOCATION (duplicate),
6155 "this is the first default label");
6157 goto error_out;
6160 /* Add a CASE_LABEL to the statement-tree. */
6161 case_label = add_stmt (build_case_label (low_value, high_value, label));
6162 /* Register this case label in the splay tree. */
6163 splay_tree_insert (cases,
6164 (splay_tree_key) low_value,
6165 (splay_tree_value) case_label);
6167 return case_label;
6169 error_out:
6170 /* Add a label so that the back-end doesn't think that the beginning of
6171 the switch is unreachable. Note that we do not add a case label, as
6172 that just leads to duplicates and thence to failure later on. */
6173 if (!cases->root)
6175 tree t = create_artificial_label (loc);
6176 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6178 return error_mark_node;
6181 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6182 Used to verify that case values match up with enumerator values. */
6184 static void
6185 match_case_to_enum_1 (tree key, tree type, tree label)
6187 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6189 if (tree_fits_uhwi_p (key))
6190 print_dec (key, buf, UNSIGNED);
6191 else if (tree_fits_shwi_p (key))
6192 print_dec (key, buf, SIGNED);
6193 else
6194 print_hex (key, buf);
6196 if (TYPE_NAME (type) == 0)
6197 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6198 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6199 "case value %qs not in enumerated type",
6200 buf);
6201 else
6202 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6203 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6204 "case value %qs not in enumerated type %qT",
6205 buf, type);
6208 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6209 Used to verify that case values match up with enumerator values. */
6211 static int
6212 match_case_to_enum (splay_tree_node node, void *data)
6214 tree label = (tree) node->value;
6215 tree type = (tree) data;
6217 /* Skip default case. */
6218 if (!CASE_LOW (label))
6219 return 0;
6221 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6222 when we did our enum->case scan. Reset our scratch bit after. */
6223 if (!CASE_LOW_SEEN (label))
6224 match_case_to_enum_1 (CASE_LOW (label), type, label);
6225 else
6226 CASE_LOW_SEEN (label) = 0;
6228 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6229 not set, that means that CASE_HIGH did not appear when we did our
6230 enum->case scan. Reset our scratch bit after. */
6231 if (CASE_HIGH (label))
6233 if (!CASE_HIGH_SEEN (label))
6234 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6235 else
6236 CASE_HIGH_SEEN (label) = 0;
6239 return 0;
6242 /* Handle -Wswitch*. Called from the front end after parsing the
6243 switch construct. */
6244 /* ??? Should probably be somewhere generic, since other languages
6245 besides C and C++ would want this. At the moment, however, C/C++
6246 are the only tree-ssa languages that support enumerations at all,
6247 so the point is moot. */
6249 void
6250 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6251 tree type, tree cond)
6253 splay_tree_node default_node;
6254 splay_tree_node node;
6255 tree chain;
6257 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6258 return;
6260 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6261 if (!default_node)
6262 warning_at (switch_location, OPT_Wswitch_default,
6263 "switch missing default case");
6265 /* From here on, we only care about about enumerated types. */
6266 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6267 return;
6269 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6270 if (!warn_switch_enum && !warn_switch)
6271 return;
6273 /* Check the cases. Warn about case values which are not members of
6274 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6275 there is no default case, check that exactly all enumeration
6276 literals are covered by the cases. */
6278 /* Clearing COND if it is not an integer constant simplifies
6279 the tests inside the loop below. */
6280 if (TREE_CODE (cond) != INTEGER_CST)
6281 cond = NULL_TREE;
6283 /* The time complexity here is O(N*lg(N)) worst case, but for the
6284 common case of monotonically increasing enumerators, it is
6285 O(N), since the nature of the splay tree will keep the next
6286 element adjacent to the root at all times. */
6288 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6290 tree value = TREE_VALUE (chain);
6291 if (TREE_CODE (value) == CONST_DECL)
6292 value = DECL_INITIAL (value);
6293 node = splay_tree_lookup (cases, (splay_tree_key) value);
6294 if (node)
6296 /* Mark the CASE_LOW part of the case entry as seen. */
6297 tree label = (tree) node->value;
6298 CASE_LOW_SEEN (label) = 1;
6299 continue;
6302 /* Even though there wasn't an exact match, there might be a
6303 case range which includes the enumerator's value. */
6304 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6305 if (node && CASE_HIGH ((tree) node->value))
6307 tree label = (tree) node->value;
6308 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6309 if (cmp >= 0)
6311 /* If we match the upper bound exactly, mark the CASE_HIGH
6312 part of the case entry as seen. */
6313 if (cmp == 0)
6314 CASE_HIGH_SEEN (label) = 1;
6315 continue;
6319 /* We've now determined that this enumerated literal isn't
6320 handled by the case labels of the switch statement. */
6322 /* If the switch expression is a constant, we only really care
6323 about whether that constant is handled by the switch. */
6324 if (cond && tree_int_cst_compare (cond, value))
6325 continue;
6327 /* If there is a default_node, the only relevant option is
6328 Wswitch-enum. Otherwise, if both are enabled then we prefer
6329 to warn using -Wswitch because -Wswitch is enabled by -Wall
6330 while -Wswitch-enum is explicit. */
6331 warning_at (switch_location,
6332 (default_node || !warn_switch
6333 ? OPT_Wswitch_enum
6334 : OPT_Wswitch),
6335 "enumeration value %qE not handled in switch",
6336 TREE_PURPOSE (chain));
6339 /* Warn if there are case expressions that don't correspond to
6340 enumerators. This can occur since C and C++ don't enforce
6341 type-checking of assignments to enumeration variables.
6343 The time complexity here is now always O(N) worst case, since
6344 we should have marked both the lower bound and upper bound of
6345 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6346 above. This scan also resets those fields. */
6348 splay_tree_foreach (cases, match_case_to_enum, type);
6351 /* Finish an expression taking the address of LABEL (an
6352 IDENTIFIER_NODE). Returns an expression for the address.
6354 LOC is the location for the expression returned. */
6356 tree
6357 finish_label_address_expr (tree label, location_t loc)
6359 tree result;
6361 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6363 if (label == error_mark_node)
6364 return error_mark_node;
6366 label = lookup_label (label);
6367 if (label == NULL_TREE)
6368 result = null_pointer_node;
6369 else
6371 TREE_USED (label) = 1;
6372 result = build1 (ADDR_EXPR, ptr_type_node, label);
6373 /* The current function is not necessarily uninlinable.
6374 Computed gotos are incompatible with inlining, but the value
6375 here could be used only in a diagnostic, for example. */
6376 protected_set_expr_location (result, loc);
6379 return result;
6383 /* Given a boolean expression ARG, return a tree representing an increment
6384 or decrement (as indicated by CODE) of ARG. The front end must check for
6385 invalid cases (e.g., decrement in C++). */
6386 tree
6387 boolean_increment (enum tree_code code, tree arg)
6389 tree val;
6390 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6392 arg = stabilize_reference (arg);
6393 switch (code)
6395 case PREINCREMENT_EXPR:
6396 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6397 break;
6398 case POSTINCREMENT_EXPR:
6399 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6400 arg = save_expr (arg);
6401 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6402 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6403 break;
6404 case PREDECREMENT_EXPR:
6405 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6406 invert_truthvalue_loc (input_location, arg));
6407 break;
6408 case POSTDECREMENT_EXPR:
6409 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6410 invert_truthvalue_loc (input_location, arg));
6411 arg = save_expr (arg);
6412 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6413 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6414 break;
6415 default:
6416 gcc_unreachable ();
6418 TREE_SIDE_EFFECTS (val) = 1;
6419 return val;
6422 /* Built-in macros for stddef.h and stdint.h, that require macros
6423 defined in this file. */
6424 void
6425 c_stddef_cpp_builtins(void)
6427 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6428 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6429 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6430 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6431 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6432 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6433 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6434 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6435 if (SIG_ATOMIC_TYPE)
6436 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6437 if (INT8_TYPE)
6438 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6439 if (INT16_TYPE)
6440 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6441 if (INT32_TYPE)
6442 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6443 if (INT64_TYPE)
6444 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6445 if (UINT8_TYPE)
6446 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6447 if (UINT16_TYPE)
6448 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6449 if (UINT32_TYPE)
6450 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6451 if (UINT64_TYPE)
6452 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6453 if (INT_LEAST8_TYPE)
6454 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6455 if (INT_LEAST16_TYPE)
6456 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6457 if (INT_LEAST32_TYPE)
6458 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6459 if (INT_LEAST64_TYPE)
6460 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6461 if (UINT_LEAST8_TYPE)
6462 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6463 if (UINT_LEAST16_TYPE)
6464 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6465 if (UINT_LEAST32_TYPE)
6466 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6467 if (UINT_LEAST64_TYPE)
6468 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6469 if (INT_FAST8_TYPE)
6470 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6471 if (INT_FAST16_TYPE)
6472 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6473 if (INT_FAST32_TYPE)
6474 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6475 if (INT_FAST64_TYPE)
6476 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6477 if (UINT_FAST8_TYPE)
6478 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6479 if (UINT_FAST16_TYPE)
6480 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6481 if (UINT_FAST32_TYPE)
6482 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6483 if (UINT_FAST64_TYPE)
6484 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6485 if (INTPTR_TYPE)
6486 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6487 if (UINTPTR_TYPE)
6488 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6491 static void
6492 c_init_attributes (void)
6494 /* Fill in the built_in_attributes array. */
6495 #define DEF_ATTR_NULL_TREE(ENUM) \
6496 built_in_attributes[(int) ENUM] = NULL_TREE;
6497 #define DEF_ATTR_INT(ENUM, VALUE) \
6498 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6499 #define DEF_ATTR_STRING(ENUM, VALUE) \
6500 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6501 #define DEF_ATTR_IDENT(ENUM, STRING) \
6502 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6503 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6504 built_in_attributes[(int) ENUM] \
6505 = tree_cons (built_in_attributes[(int) PURPOSE], \
6506 built_in_attributes[(int) VALUE], \
6507 built_in_attributes[(int) CHAIN]);
6508 #include "builtin-attrs.def"
6509 #undef DEF_ATTR_NULL_TREE
6510 #undef DEF_ATTR_INT
6511 #undef DEF_ATTR_IDENT
6512 #undef DEF_ATTR_TREE_LIST
6515 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6516 identifier as an argument, so the front end shouldn't look it up. */
6518 bool
6519 attribute_takes_identifier_p (const_tree attr_id)
6521 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6522 if (spec == NULL)
6523 /* Unknown attribute that we'll end up ignoring, return true so we
6524 don't complain about an identifier argument. */
6525 return true;
6526 else if (!strcmp ("mode", spec->name)
6527 || !strcmp ("format", spec->name)
6528 || !strcmp ("cleanup", spec->name))
6529 return true;
6530 else
6531 return targetm.attribute_takes_identifier_p (attr_id);
6534 /* Attribute handlers common to C front ends. */
6536 /* Handle a "packed" attribute; arguments as in
6537 struct attribute_spec.handler. */
6539 static tree
6540 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6541 int flags, bool *no_add_attrs)
6543 if (TYPE_P (*node))
6545 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6546 *node = build_variant_type_copy (*node);
6547 TYPE_PACKED (*node) = 1;
6549 else if (TREE_CODE (*node) == FIELD_DECL)
6551 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6552 /* Still pack bitfields. */
6553 && ! DECL_INITIAL (*node))
6554 warning (OPT_Wattributes,
6555 "%qE attribute ignored for field of type %qT",
6556 name, TREE_TYPE (*node));
6557 else
6558 DECL_PACKED (*node) = 1;
6560 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6561 used for DECL_REGISTER. It wouldn't mean anything anyway.
6562 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6563 that changes what the typedef is typing. */
6564 else
6566 warning (OPT_Wattributes, "%qE attribute ignored", name);
6567 *no_add_attrs = true;
6570 return NULL_TREE;
6573 /* Handle a "nocommon" attribute; arguments as in
6574 struct attribute_spec.handler. */
6576 static tree
6577 handle_nocommon_attribute (tree *node, tree name,
6578 tree ARG_UNUSED (args),
6579 int ARG_UNUSED (flags), bool *no_add_attrs)
6581 if (TREE_CODE (*node) == VAR_DECL)
6582 DECL_COMMON (*node) = 0;
6583 else
6585 warning (OPT_Wattributes, "%qE attribute ignored", name);
6586 *no_add_attrs = true;
6589 return NULL_TREE;
6592 /* Handle a "common" attribute; arguments as in
6593 struct attribute_spec.handler. */
6595 static tree
6596 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6597 int ARG_UNUSED (flags), bool *no_add_attrs)
6599 if (TREE_CODE (*node) == VAR_DECL)
6600 DECL_COMMON (*node) = 1;
6601 else
6603 warning (OPT_Wattributes, "%qE attribute ignored", name);
6604 *no_add_attrs = true;
6607 return NULL_TREE;
6610 /* Handle a "noreturn" attribute; arguments as in
6611 struct attribute_spec.handler. */
6613 static tree
6614 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6615 int ARG_UNUSED (flags), bool *no_add_attrs)
6617 tree type = TREE_TYPE (*node);
6619 /* See FIXME comment in c_common_attribute_table. */
6620 if (TREE_CODE (*node) == FUNCTION_DECL
6621 || objc_method_decl (TREE_CODE (*node)))
6622 TREE_THIS_VOLATILE (*node) = 1;
6623 else if (TREE_CODE (type) == POINTER_TYPE
6624 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6625 TREE_TYPE (*node)
6626 = (build_qualified_type
6627 (build_pointer_type
6628 (build_type_variant (TREE_TYPE (type),
6629 TYPE_READONLY (TREE_TYPE (type)), 1)),
6630 TYPE_QUALS (type)));
6631 else
6633 warning (OPT_Wattributes, "%qE attribute ignored", name);
6634 *no_add_attrs = true;
6637 return NULL_TREE;
6640 /* Handle a "hot" and attribute; arguments as in
6641 struct attribute_spec.handler. */
6643 static tree
6644 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6645 int ARG_UNUSED (flags), bool *no_add_attrs)
6647 if (TREE_CODE (*node) == FUNCTION_DECL
6648 || TREE_CODE (*node) == LABEL_DECL)
6650 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6652 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6653 "with attribute %qs", name, "cold");
6654 *no_add_attrs = true;
6656 /* Most of the rest of the hot processing is done later with
6657 lookup_attribute. */
6659 else
6661 warning (OPT_Wattributes, "%qE attribute ignored", name);
6662 *no_add_attrs = true;
6665 return NULL_TREE;
6668 /* Handle a "cold" and attribute; arguments as in
6669 struct attribute_spec.handler. */
6671 static tree
6672 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6673 int ARG_UNUSED (flags), bool *no_add_attrs)
6675 if (TREE_CODE (*node) == FUNCTION_DECL
6676 || TREE_CODE (*node) == LABEL_DECL)
6678 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6680 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6681 "with attribute %qs", name, "hot");
6682 *no_add_attrs = true;
6684 /* Most of the rest of the cold processing is done later with
6685 lookup_attribute. */
6687 else
6689 warning (OPT_Wattributes, "%qE attribute ignored", name);
6690 *no_add_attrs = true;
6693 return NULL_TREE;
6696 /* Handle a "no_sanitize_address" attribute; arguments as in
6697 struct attribute_spec.handler. */
6699 static tree
6700 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6701 bool *no_add_attrs)
6703 if (TREE_CODE (*node) != FUNCTION_DECL)
6705 warning (OPT_Wattributes, "%qE attribute ignored", name);
6706 *no_add_attrs = true;
6709 return NULL_TREE;
6712 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6713 struct attribute_spec.handler. */
6715 static tree
6716 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6717 bool *no_add_attrs)
6719 if (TREE_CODE (*node) != FUNCTION_DECL)
6720 warning (OPT_Wattributes, "%qE attribute ignored", name);
6721 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6722 DECL_ATTRIBUTES (*node)
6723 = tree_cons (get_identifier ("no_sanitize_address"),
6724 NULL_TREE, DECL_ATTRIBUTES (*node));
6725 *no_add_attrs = true;
6726 return NULL_TREE;
6729 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6730 struct attribute_spec.handler. */
6732 static tree
6733 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6734 bool *no_add_attrs)
6736 if (TREE_CODE (*node) != FUNCTION_DECL)
6738 warning (OPT_Wattributes, "%qE attribute ignored", name);
6739 *no_add_attrs = true;
6742 return NULL_TREE;
6745 /* Handle a "noinline" attribute; arguments as in
6746 struct attribute_spec.handler. */
6748 static tree
6749 handle_noinline_attribute (tree *node, tree name,
6750 tree ARG_UNUSED (args),
6751 int ARG_UNUSED (flags), bool *no_add_attrs)
6753 if (TREE_CODE (*node) == FUNCTION_DECL)
6755 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6757 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6758 "with attribute %qs", name, "always_inline");
6759 *no_add_attrs = true;
6761 else
6762 DECL_UNINLINABLE (*node) = 1;
6764 else
6766 warning (OPT_Wattributes, "%qE attribute ignored", name);
6767 *no_add_attrs = true;
6770 return NULL_TREE;
6773 /* Handle a "noclone" attribute; arguments as in
6774 struct attribute_spec.handler. */
6776 static tree
6777 handle_noclone_attribute (tree *node, tree name,
6778 tree ARG_UNUSED (args),
6779 int ARG_UNUSED (flags), bool *no_add_attrs)
6781 if (TREE_CODE (*node) != FUNCTION_DECL)
6783 warning (OPT_Wattributes, "%qE attribute ignored", name);
6784 *no_add_attrs = true;
6787 return NULL_TREE;
6790 /* Handle a "always_inline" attribute; arguments as in
6791 struct attribute_spec.handler. */
6793 static tree
6794 handle_always_inline_attribute (tree *node, tree name,
6795 tree ARG_UNUSED (args),
6796 int ARG_UNUSED (flags),
6797 bool *no_add_attrs)
6799 if (TREE_CODE (*node) == FUNCTION_DECL)
6801 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6803 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6804 "with %qs attribute", name, "noinline");
6805 *no_add_attrs = true;
6807 else
6808 /* Set the attribute and mark it for disregarding inline
6809 limits. */
6810 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6812 else
6814 warning (OPT_Wattributes, "%qE attribute ignored", name);
6815 *no_add_attrs = true;
6818 return NULL_TREE;
6821 /* Handle a "gnu_inline" attribute; arguments as in
6822 struct attribute_spec.handler. */
6824 static tree
6825 handle_gnu_inline_attribute (tree *node, tree name,
6826 tree ARG_UNUSED (args),
6827 int ARG_UNUSED (flags),
6828 bool *no_add_attrs)
6830 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6832 /* Do nothing else, just set the attribute. We'll get at
6833 it later with lookup_attribute. */
6835 else
6837 warning (OPT_Wattributes, "%qE attribute ignored", name);
6838 *no_add_attrs = true;
6841 return NULL_TREE;
6844 /* Handle a "leaf" attribute; arguments as in
6845 struct attribute_spec.handler. */
6847 static tree
6848 handle_leaf_attribute (tree *node, tree name,
6849 tree ARG_UNUSED (args),
6850 int ARG_UNUSED (flags), bool *no_add_attrs)
6852 if (TREE_CODE (*node) != FUNCTION_DECL)
6854 warning (OPT_Wattributes, "%qE attribute ignored", name);
6855 *no_add_attrs = true;
6857 if (!TREE_PUBLIC (*node))
6859 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6860 *no_add_attrs = true;
6863 return NULL_TREE;
6866 /* Handle an "artificial" attribute; arguments as in
6867 struct attribute_spec.handler. */
6869 static tree
6870 handle_artificial_attribute (tree *node, tree name,
6871 tree ARG_UNUSED (args),
6872 int ARG_UNUSED (flags),
6873 bool *no_add_attrs)
6875 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6877 /* Do nothing else, just set the attribute. We'll get at
6878 it later with lookup_attribute. */
6880 else
6882 warning (OPT_Wattributes, "%qE attribute ignored", name);
6883 *no_add_attrs = true;
6886 return NULL_TREE;
6889 /* Handle a "flatten" attribute; arguments as in
6890 struct attribute_spec.handler. */
6892 static tree
6893 handle_flatten_attribute (tree *node, tree name,
6894 tree args ATTRIBUTE_UNUSED,
6895 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6897 if (TREE_CODE (*node) == FUNCTION_DECL)
6898 /* Do nothing else, just set the attribute. We'll get at
6899 it later with lookup_attribute. */
6901 else
6903 warning (OPT_Wattributes, "%qE attribute ignored", name);
6904 *no_add_attrs = true;
6907 return NULL_TREE;
6910 /* Handle a "warning" or "error" attribute; arguments as in
6911 struct attribute_spec.handler. */
6913 static tree
6914 handle_error_attribute (tree *node, tree name, tree args,
6915 int ARG_UNUSED (flags), bool *no_add_attrs)
6917 if (TREE_CODE (*node) == FUNCTION_DECL
6918 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6919 /* Do nothing else, just set the attribute. We'll get at
6920 it later with lookup_attribute. */
6922 else
6924 warning (OPT_Wattributes, "%qE attribute ignored", name);
6925 *no_add_attrs = true;
6928 return NULL_TREE;
6931 /* Handle a "used" attribute; arguments as in
6932 struct attribute_spec.handler. */
6934 static tree
6935 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6936 int ARG_UNUSED (flags), bool *no_add_attrs)
6938 tree node = *pnode;
6940 if (TREE_CODE (node) == FUNCTION_DECL
6941 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6942 || (TREE_CODE (node) == TYPE_DECL))
6944 TREE_USED (node) = 1;
6945 DECL_PRESERVE_P (node) = 1;
6946 if (TREE_CODE (node) == VAR_DECL)
6947 DECL_READ_P (node) = 1;
6949 else
6951 warning (OPT_Wattributes, "%qE attribute ignored", name);
6952 *no_add_attrs = true;
6955 return NULL_TREE;
6958 /* Handle a "unused" attribute; arguments as in
6959 struct attribute_spec.handler. */
6961 static tree
6962 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6963 int flags, bool *no_add_attrs)
6965 if (DECL_P (*node))
6967 tree decl = *node;
6969 if (TREE_CODE (decl) == PARM_DECL
6970 || TREE_CODE (decl) == VAR_DECL
6971 || TREE_CODE (decl) == FUNCTION_DECL
6972 || TREE_CODE (decl) == LABEL_DECL
6973 || TREE_CODE (decl) == TYPE_DECL)
6975 TREE_USED (decl) = 1;
6976 if (TREE_CODE (decl) == VAR_DECL
6977 || TREE_CODE (decl) == PARM_DECL)
6978 DECL_READ_P (decl) = 1;
6980 else
6982 warning (OPT_Wattributes, "%qE attribute ignored", name);
6983 *no_add_attrs = true;
6986 else
6988 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6989 *node = build_variant_type_copy (*node);
6990 TREE_USED (*node) = 1;
6993 return NULL_TREE;
6996 /* Handle a "externally_visible" attribute; arguments as in
6997 struct attribute_spec.handler. */
6999 static tree
7000 handle_externally_visible_attribute (tree *pnode, tree name,
7001 tree ARG_UNUSED (args),
7002 int ARG_UNUSED (flags),
7003 bool *no_add_attrs)
7005 tree node = *pnode;
7007 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7009 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7010 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7012 warning (OPT_Wattributes,
7013 "%qE attribute have effect only on public objects", name);
7014 *no_add_attrs = true;
7017 else
7019 warning (OPT_Wattributes, "%qE attribute ignored", name);
7020 *no_add_attrs = true;
7023 return NULL_TREE;
7026 /* Handle the "no_reorder" attribute. Arguments as in
7027 struct attribute_spec.handler. */
7029 static tree
7030 handle_no_reorder_attribute (tree *pnode,
7031 tree name,
7032 tree,
7033 int,
7034 bool *no_add_attrs)
7036 tree node = *pnode;
7038 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7039 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7041 warning (OPT_Wattributes,
7042 "%qE attribute only affects top level objects",
7043 name);
7044 *no_add_attrs = true;
7047 return NULL_TREE;
7050 /* Handle a "const" attribute; arguments as in
7051 struct attribute_spec.handler. */
7053 static tree
7054 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7055 int ARG_UNUSED (flags), bool *no_add_attrs)
7057 tree type = TREE_TYPE (*node);
7059 /* See FIXME comment on noreturn in c_common_attribute_table. */
7060 if (TREE_CODE (*node) == FUNCTION_DECL)
7061 TREE_READONLY (*node) = 1;
7062 else if (TREE_CODE (type) == POINTER_TYPE
7063 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7064 TREE_TYPE (*node)
7065 = (build_qualified_type
7066 (build_pointer_type
7067 (build_type_variant (TREE_TYPE (type), 1,
7068 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7069 TYPE_QUALS (type)));
7070 else
7072 warning (OPT_Wattributes, "%qE attribute ignored", name);
7073 *no_add_attrs = true;
7076 return NULL_TREE;
7079 /* Handle a "transparent_union" attribute; arguments as in
7080 struct attribute_spec.handler. */
7082 static tree
7083 handle_transparent_union_attribute (tree *node, tree name,
7084 tree ARG_UNUSED (args), int flags,
7085 bool *no_add_attrs)
7087 tree type;
7089 *no_add_attrs = true;
7092 if (TREE_CODE (*node) == TYPE_DECL
7093 && ! (flags & ATTR_FLAG_CXX11))
7094 node = &TREE_TYPE (*node);
7095 type = *node;
7097 if (TREE_CODE (type) == UNION_TYPE)
7099 /* Make sure that the first field will work for a transparent union.
7100 If the type isn't complete yet, leave the check to the code in
7101 finish_struct. */
7102 if (TYPE_SIZE (type))
7104 tree first = first_field (type);
7105 if (first == NULL_TREE
7106 || DECL_ARTIFICIAL (first)
7107 || TYPE_MODE (type) != DECL_MODE (first))
7108 goto ignored;
7111 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7113 /* If the type isn't complete yet, setting the flag
7114 on a variant wouldn't ever be checked. */
7115 if (!TYPE_SIZE (type))
7116 goto ignored;
7118 /* build_duplicate_type doesn't work for C++. */
7119 if (c_dialect_cxx ())
7120 goto ignored;
7122 /* A type variant isn't good enough, since we don't a cast
7123 to such a type removed as a no-op. */
7124 *node = type = build_duplicate_type (type);
7127 TYPE_TRANSPARENT_AGGR (type) = 1;
7128 return NULL_TREE;
7131 ignored:
7132 warning (OPT_Wattributes, "%qE attribute ignored", name);
7133 return NULL_TREE;
7136 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7137 get the requested priority for a constructor or destructor,
7138 possibly issuing diagnostics for invalid or reserved
7139 priorities. */
7141 static priority_type
7142 get_priority (tree args, bool is_destructor)
7144 HOST_WIDE_INT pri;
7145 tree arg;
7147 if (!args)
7148 return DEFAULT_INIT_PRIORITY;
7150 if (!SUPPORTS_INIT_PRIORITY)
7152 if (is_destructor)
7153 error ("destructor priorities are not supported");
7154 else
7155 error ("constructor priorities are not supported");
7156 return DEFAULT_INIT_PRIORITY;
7159 arg = TREE_VALUE (args);
7160 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7161 goto invalid;
7162 if (arg == error_mark_node)
7163 return DEFAULT_INIT_PRIORITY;
7164 arg = default_conversion (arg);
7165 if (!tree_fits_shwi_p (arg)
7166 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7167 goto invalid;
7169 pri = tree_to_shwi (arg);
7170 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7171 goto invalid;
7173 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7175 if (is_destructor)
7176 warning (0,
7177 "destructor priorities from 0 to %d are reserved "
7178 "for the implementation",
7179 MAX_RESERVED_INIT_PRIORITY);
7180 else
7181 warning (0,
7182 "constructor priorities from 0 to %d are reserved "
7183 "for the implementation",
7184 MAX_RESERVED_INIT_PRIORITY);
7186 return pri;
7188 invalid:
7189 if (is_destructor)
7190 error ("destructor priorities must be integers from 0 to %d inclusive",
7191 MAX_INIT_PRIORITY);
7192 else
7193 error ("constructor priorities must be integers from 0 to %d inclusive",
7194 MAX_INIT_PRIORITY);
7195 return DEFAULT_INIT_PRIORITY;
7198 /* Handle a "constructor" attribute; arguments as in
7199 struct attribute_spec.handler. */
7201 static tree
7202 handle_constructor_attribute (tree *node, tree name, tree args,
7203 int ARG_UNUSED (flags),
7204 bool *no_add_attrs)
7206 tree decl = *node;
7207 tree type = TREE_TYPE (decl);
7209 if (TREE_CODE (decl) == FUNCTION_DECL
7210 && TREE_CODE (type) == FUNCTION_TYPE
7211 && decl_function_context (decl) == 0)
7213 priority_type priority;
7214 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7215 priority = get_priority (args, /*is_destructor=*/false);
7216 SET_DECL_INIT_PRIORITY (decl, priority);
7217 TREE_USED (decl) = 1;
7219 else
7221 warning (OPT_Wattributes, "%qE attribute ignored", name);
7222 *no_add_attrs = true;
7225 return NULL_TREE;
7228 /* Handle a "destructor" attribute; arguments as in
7229 struct attribute_spec.handler. */
7231 static tree
7232 handle_destructor_attribute (tree *node, tree name, tree args,
7233 int ARG_UNUSED (flags),
7234 bool *no_add_attrs)
7236 tree decl = *node;
7237 tree type = TREE_TYPE (decl);
7239 if (TREE_CODE (decl) == FUNCTION_DECL
7240 && TREE_CODE (type) == FUNCTION_TYPE
7241 && decl_function_context (decl) == 0)
7243 priority_type priority;
7244 DECL_STATIC_DESTRUCTOR (decl) = 1;
7245 priority = get_priority (args, /*is_destructor=*/true);
7246 SET_DECL_FINI_PRIORITY (decl, priority);
7247 TREE_USED (decl) = 1;
7249 else
7251 warning (OPT_Wattributes, "%qE attribute ignored", name);
7252 *no_add_attrs = true;
7255 return NULL_TREE;
7258 /* Nonzero if the mode is a valid vector mode for this architecture.
7259 This returns nonzero even if there is no hardware support for the
7260 vector mode, but we can emulate with narrower modes. */
7262 static int
7263 vector_mode_valid_p (machine_mode mode)
7265 enum mode_class mclass = GET_MODE_CLASS (mode);
7266 machine_mode innermode;
7268 /* Doh! What's going on? */
7269 if (mclass != MODE_VECTOR_INT
7270 && mclass != MODE_VECTOR_FLOAT
7271 && mclass != MODE_VECTOR_FRACT
7272 && mclass != MODE_VECTOR_UFRACT
7273 && mclass != MODE_VECTOR_ACCUM
7274 && mclass != MODE_VECTOR_UACCUM)
7275 return 0;
7277 /* Hardware support. Woo hoo! */
7278 if (targetm.vector_mode_supported_p (mode))
7279 return 1;
7281 innermode = GET_MODE_INNER (mode);
7283 /* We should probably return 1 if requesting V4DI and we have no DI,
7284 but we have V2DI, but this is probably very unlikely. */
7286 /* If we have support for the inner mode, we can safely emulate it.
7287 We may not have V2DI, but me can emulate with a pair of DIs. */
7288 return targetm.scalar_mode_supported_p (innermode);
7292 /* Handle a "mode" attribute; arguments as in
7293 struct attribute_spec.handler. */
7295 static tree
7296 handle_mode_attribute (tree *node, tree name, tree args,
7297 int ARG_UNUSED (flags), bool *no_add_attrs)
7299 tree type = *node;
7300 tree ident = TREE_VALUE (args);
7302 *no_add_attrs = true;
7304 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7305 warning (OPT_Wattributes, "%qE attribute ignored", name);
7306 else
7308 int j;
7309 const char *p = IDENTIFIER_POINTER (ident);
7310 int len = strlen (p);
7311 machine_mode mode = VOIDmode;
7312 tree typefm;
7313 bool valid_mode;
7315 if (len > 4 && p[0] == '_' && p[1] == '_'
7316 && p[len - 1] == '_' && p[len - 2] == '_')
7318 char *newp = (char *) alloca (len - 1);
7320 strcpy (newp, &p[2]);
7321 newp[len - 4] = '\0';
7322 p = newp;
7325 /* Change this type to have a type with the specified mode.
7326 First check for the special modes. */
7327 if (!strcmp (p, "byte"))
7328 mode = byte_mode;
7329 else if (!strcmp (p, "word"))
7330 mode = word_mode;
7331 else if (!strcmp (p, "pointer"))
7332 mode = ptr_mode;
7333 else if (!strcmp (p, "libgcc_cmp_return"))
7334 mode = targetm.libgcc_cmp_return_mode ();
7335 else if (!strcmp (p, "libgcc_shift_count"))
7336 mode = targetm.libgcc_shift_count_mode ();
7337 else if (!strcmp (p, "unwind_word"))
7338 mode = targetm.unwind_word_mode ();
7339 else
7340 for (j = 0; j < NUM_MACHINE_MODES; j++)
7341 if (!strcmp (p, GET_MODE_NAME (j)))
7343 mode = (machine_mode) j;
7344 break;
7347 if (mode == VOIDmode)
7349 error ("unknown machine mode %qE", ident);
7350 return NULL_TREE;
7353 valid_mode = false;
7354 switch (GET_MODE_CLASS (mode))
7356 case MODE_INT:
7357 case MODE_PARTIAL_INT:
7358 case MODE_FLOAT:
7359 case MODE_DECIMAL_FLOAT:
7360 case MODE_FRACT:
7361 case MODE_UFRACT:
7362 case MODE_ACCUM:
7363 case MODE_UACCUM:
7364 valid_mode = targetm.scalar_mode_supported_p (mode);
7365 break;
7367 case MODE_COMPLEX_INT:
7368 case MODE_COMPLEX_FLOAT:
7369 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7370 break;
7372 case MODE_VECTOR_INT:
7373 case MODE_VECTOR_FLOAT:
7374 case MODE_VECTOR_FRACT:
7375 case MODE_VECTOR_UFRACT:
7376 case MODE_VECTOR_ACCUM:
7377 case MODE_VECTOR_UACCUM:
7378 warning (OPT_Wattributes, "specifying vector types with "
7379 "__attribute__ ((mode)) is deprecated");
7380 warning (OPT_Wattributes,
7381 "use __attribute__ ((vector_size)) instead");
7382 valid_mode = vector_mode_valid_p (mode);
7383 break;
7385 default:
7386 break;
7388 if (!valid_mode)
7390 error ("unable to emulate %qs", p);
7391 return NULL_TREE;
7394 if (POINTER_TYPE_P (type))
7396 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7397 tree (*fn)(tree, machine_mode, bool);
7399 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7401 error ("invalid pointer mode %qs", p);
7402 return NULL_TREE;
7405 if (TREE_CODE (type) == POINTER_TYPE)
7406 fn = build_pointer_type_for_mode;
7407 else
7408 fn = build_reference_type_for_mode;
7409 typefm = fn (TREE_TYPE (type), mode, false);
7411 else
7413 /* For fixed-point modes, we need to test if the signness of type
7414 and the machine mode are consistent. */
7415 if (ALL_FIXED_POINT_MODE_P (mode)
7416 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7418 error ("signedness of type and machine mode %qs don%'t match", p);
7419 return NULL_TREE;
7421 /* For fixed-point modes, we need to pass saturating info. */
7422 typefm = lang_hooks.types.type_for_mode (mode,
7423 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7424 : TYPE_UNSIGNED (type));
7427 if (typefm == NULL_TREE)
7429 error ("no data type for mode %qs", p);
7430 return NULL_TREE;
7432 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7434 /* For enumeral types, copy the precision from the integer
7435 type returned above. If not an INTEGER_TYPE, we can't use
7436 this mode for this type. */
7437 if (TREE_CODE (typefm) != INTEGER_TYPE)
7439 error ("cannot use mode %qs for enumeral types", p);
7440 return NULL_TREE;
7443 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7445 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7446 typefm = type;
7448 else
7450 /* We cannot build a type variant, as there's code that assumes
7451 that TYPE_MAIN_VARIANT has the same mode. This includes the
7452 debug generators. Instead, create a subrange type. This
7453 results in all of the enumeral values being emitted only once
7454 in the original, and the subtype gets them by reference. */
7455 if (TYPE_UNSIGNED (type))
7456 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7457 else
7458 typefm = make_signed_type (TYPE_PRECISION (typefm));
7459 TREE_TYPE (typefm) = type;
7462 else if (VECTOR_MODE_P (mode)
7463 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7464 : TREE_CODE (type) != TREE_CODE (typefm))
7466 error ("mode %qs applied to inappropriate type", p);
7467 return NULL_TREE;
7470 *node = typefm;
7473 return NULL_TREE;
7476 /* Handle a "section" attribute; arguments as in
7477 struct attribute_spec.handler. */
7479 static tree
7480 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7481 int ARG_UNUSED (flags), bool *no_add_attrs)
7483 tree decl = *node;
7485 if (targetm_common.have_named_sections)
7487 user_defined_section_attribute = true;
7489 if ((TREE_CODE (decl) == FUNCTION_DECL
7490 || TREE_CODE (decl) == VAR_DECL)
7491 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7493 if (TREE_CODE (decl) == VAR_DECL
7494 && current_function_decl != NULL_TREE
7495 && !TREE_STATIC (decl))
7497 error_at (DECL_SOURCE_LOCATION (decl),
7498 "section attribute cannot be specified for "
7499 "local variables");
7500 *no_add_attrs = true;
7503 /* The decl may have already been given a section attribute
7504 from a previous declaration. Ensure they match. */
7505 else if (DECL_SECTION_NAME (decl) != NULL
7506 && strcmp (DECL_SECTION_NAME (decl),
7507 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7509 error ("section of %q+D conflicts with previous declaration",
7510 *node);
7511 *no_add_attrs = true;
7513 else if (TREE_CODE (decl) == VAR_DECL
7514 && !targetm.have_tls && targetm.emutls.tmpl_section
7515 && DECL_THREAD_LOCAL_P (decl))
7517 error ("section of %q+D cannot be overridden", *node);
7518 *no_add_attrs = true;
7520 else
7521 set_decl_section_name (decl,
7522 TREE_STRING_POINTER (TREE_VALUE (args)));
7524 else
7526 error ("section attribute not allowed for %q+D", *node);
7527 *no_add_attrs = true;
7530 else
7532 error_at (DECL_SOURCE_LOCATION (*node),
7533 "section attributes are not supported for this target");
7534 *no_add_attrs = true;
7537 return NULL_TREE;
7540 /* Check whether ALIGN is a valid user-specified alignment. If so,
7541 return its base-2 log; if not, output an error and return -1. If
7542 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7543 no error. */
7545 check_user_alignment (const_tree align, bool allow_zero)
7547 int i;
7549 if (error_operand_p (align))
7550 return -1;
7551 if (TREE_CODE (align) != INTEGER_CST
7552 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7554 error ("requested alignment is not an integer constant");
7555 return -1;
7557 else if (allow_zero && integer_zerop (align))
7558 return -1;
7559 else if (tree_int_cst_sgn (align) == -1
7560 || (i = tree_log2 (align)) == -1)
7562 error ("requested alignment is not a positive power of 2");
7563 return -1;
7565 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7567 error ("requested alignment is too large");
7568 return -1;
7570 return i;
7574 If in c++-11, check if the c++-11 alignment constraint with respect
7575 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7576 c++-11 mode, does nothing.
7578 [dcl.align]2/ says:
7580 [* if the constant expression evaluates to a fundamental alignment,
7581 the alignment requirement of the declared entity shall be the
7582 specified fundamental alignment.
7584 * if the constant expression evaluates to an extended alignment
7585 and the implementation supports that alignment in the context
7586 of the declaration, the alignment of the declared entity shall
7587 be that alignment
7589 * if the constant expression evaluates to an extended alignment
7590 and the implementation does not support that alignment in the
7591 context of the declaration, the program is ill-formed]. */
7593 static bool
7594 check_cxx_fundamental_alignment_constraints (tree node,
7595 unsigned align_log,
7596 int flags)
7598 bool alignment_too_large_p = false;
7599 unsigned requested_alignment = 1U << align_log;
7600 unsigned max_align = 0;
7602 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7603 || (node == NULL_TREE || node == error_mark_node))
7604 return true;
7606 if (cxx_fundamental_alignment_p (requested_alignment))
7607 return true;
7609 if (DECL_P (node))
7611 if (TREE_STATIC (node))
7613 /* For file scope variables and static members, the target
7614 supports alignments that are at most
7615 MAX_OFILE_ALIGNMENT. */
7616 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7617 alignment_too_large_p = true;
7619 else
7621 #ifdef BIGGEST_FIELD_ALIGNMENT
7622 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7623 #else
7624 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7625 #endif
7626 /* For non-static members, the target supports either
7627 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7628 if it is defined or BIGGEST_ALIGNMENT. */
7629 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7630 if (TREE_CODE (node) == FIELD_DECL
7631 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7632 alignment_too_large_p = true;
7633 #undef MAX_TARGET_FIELD_ALIGNMENT
7634 /* For stack variables, the target supports at most
7635 MAX_STACK_ALIGNMENT. */
7636 else if (decl_function_context (node) != NULL
7637 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7638 alignment_too_large_p = true;
7641 else if (TYPE_P (node))
7643 /* Let's be liberal for types. */
7644 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7645 alignment_too_large_p = true;
7648 if (alignment_too_large_p)
7649 pedwarn (input_location, OPT_Wattributes,
7650 "requested alignment %d is larger than %d",
7651 requested_alignment, max_align);
7653 return !alignment_too_large_p;
7656 /* Handle a "aligned" attribute; arguments as in
7657 struct attribute_spec.handler. */
7659 static tree
7660 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7661 int flags, bool *no_add_attrs)
7663 tree decl = NULL_TREE;
7664 tree *type = NULL;
7665 int is_type = 0;
7666 tree align_expr;
7667 int i;
7669 if (args)
7671 align_expr = TREE_VALUE (args);
7672 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7673 && TREE_CODE (align_expr) != FUNCTION_DECL)
7674 align_expr = default_conversion (align_expr);
7676 else
7677 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7679 if (DECL_P (*node))
7681 decl = *node;
7682 type = &TREE_TYPE (decl);
7683 is_type = TREE_CODE (*node) == TYPE_DECL;
7685 else if (TYPE_P (*node))
7686 type = node, is_type = 1;
7688 if ((i = check_user_alignment (align_expr, false)) == -1
7689 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7690 *no_add_attrs = true;
7691 else if (is_type)
7693 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7694 /* OK, modify the type in place. */;
7695 /* If we have a TYPE_DECL, then copy the type, so that we
7696 don't accidentally modify a builtin type. See pushdecl. */
7697 else if (decl && TREE_TYPE (decl) != error_mark_node
7698 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7700 tree tt = TREE_TYPE (decl);
7701 *type = build_variant_type_copy (*type);
7702 DECL_ORIGINAL_TYPE (decl) = tt;
7703 TYPE_NAME (*type) = decl;
7704 TREE_USED (*type) = TREE_USED (decl);
7705 TREE_TYPE (decl) = *type;
7707 else
7708 *type = build_variant_type_copy (*type);
7710 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7711 TYPE_USER_ALIGN (*type) = 1;
7713 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7714 && TREE_CODE (decl) != FIELD_DECL)
7716 error ("alignment may not be specified for %q+D", decl);
7717 *no_add_attrs = true;
7719 else if (DECL_USER_ALIGN (decl)
7720 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7721 /* C++-11 [dcl.align/4]:
7723 When multiple alignment-specifiers are specified for an
7724 entity, the alignment requirement shall be set to the
7725 strictest specified alignment.
7727 This formally comes from the c++11 specification but we are
7728 doing it for the GNU attribute syntax as well. */
7729 *no_add_attrs = true;
7730 else if (TREE_CODE (decl) == FUNCTION_DECL
7731 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7733 if (DECL_USER_ALIGN (decl))
7734 error ("alignment for %q+D was previously specified as %d "
7735 "and may not be decreased", decl,
7736 DECL_ALIGN (decl) / BITS_PER_UNIT);
7737 else
7738 error ("alignment for %q+D must be at least %d", decl,
7739 DECL_ALIGN (decl) / BITS_PER_UNIT);
7740 *no_add_attrs = true;
7742 else
7744 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7745 DECL_USER_ALIGN (decl) = 1;
7748 return NULL_TREE;
7751 /* Handle a "weak" attribute; arguments as in
7752 struct attribute_spec.handler. */
7754 static tree
7755 handle_weak_attribute (tree *node, tree name,
7756 tree ARG_UNUSED (args),
7757 int ARG_UNUSED (flags),
7758 bool * ARG_UNUSED (no_add_attrs))
7760 if (TREE_CODE (*node) == FUNCTION_DECL
7761 && DECL_DECLARED_INLINE_P (*node))
7763 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7764 *no_add_attrs = true;
7766 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7768 error ("indirect function %q+D cannot be declared weak", *node);
7769 *no_add_attrs = true;
7770 return NULL_TREE;
7772 else if (TREE_CODE (*node) == FUNCTION_DECL
7773 || TREE_CODE (*node) == VAR_DECL)
7774 declare_weak (*node);
7775 else
7776 warning (OPT_Wattributes, "%qE attribute ignored", name);
7778 return NULL_TREE;
7781 /* Handle an "alias" or "ifunc" attribute; arguments as in
7782 struct attribute_spec.handler, except that IS_ALIAS tells us
7783 whether this is an alias as opposed to ifunc attribute. */
7785 static tree
7786 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7787 bool *no_add_attrs)
7789 tree decl = *node;
7791 if (TREE_CODE (decl) != FUNCTION_DECL
7792 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7794 warning (OPT_Wattributes, "%qE attribute ignored", name);
7795 *no_add_attrs = true;
7797 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7798 || (TREE_CODE (decl) != FUNCTION_DECL
7799 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7800 /* A static variable declaration is always a tentative definition,
7801 but the alias is a non-tentative definition which overrides. */
7802 || (TREE_CODE (decl) != FUNCTION_DECL
7803 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7805 error ("%q+D defined both normally and as %qE attribute", decl, name);
7806 *no_add_attrs = true;
7807 return NULL_TREE;
7809 else if (!is_alias
7810 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7811 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7813 error ("weak %q+D cannot be defined %qE", decl, name);
7814 *no_add_attrs = true;
7815 return NULL_TREE;
7818 /* Note that the very first time we process a nested declaration,
7819 decl_function_context will not be set. Indeed, *would* never
7820 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7821 we do below. After such frobbery, pushdecl would set the context.
7822 In any case, this is never what we want. */
7823 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7825 tree id;
7827 id = TREE_VALUE (args);
7828 if (TREE_CODE (id) != STRING_CST)
7830 error ("attribute %qE argument not a string", name);
7831 *no_add_attrs = true;
7832 return NULL_TREE;
7834 id = get_identifier (TREE_STRING_POINTER (id));
7835 /* This counts as a use of the object pointed to. */
7836 TREE_USED (id) = 1;
7838 if (TREE_CODE (decl) == FUNCTION_DECL)
7839 DECL_INITIAL (decl) = error_mark_node;
7840 else
7841 TREE_STATIC (decl) = 1;
7843 if (!is_alias)
7844 /* ifuncs are also aliases, so set that attribute too. */
7845 DECL_ATTRIBUTES (decl)
7846 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7848 else
7850 warning (OPT_Wattributes, "%qE attribute ignored", name);
7851 *no_add_attrs = true;
7854 if (decl_in_symtab_p (*node))
7856 struct symtab_node *n = symtab_node::get (decl);
7857 if (n && n->refuse_visibility_changes)
7859 if (is_alias)
7860 error ("%+D declared alias after being used", decl);
7861 else
7862 error ("%+D declared ifunc after being used", decl);
7867 return NULL_TREE;
7870 /* Handle an "alias" or "ifunc" attribute; arguments as in
7871 struct attribute_spec.handler. */
7873 static tree
7874 handle_ifunc_attribute (tree *node, tree name, tree args,
7875 int ARG_UNUSED (flags), bool *no_add_attrs)
7877 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7880 /* Handle an "alias" or "ifunc" attribute; arguments as in
7881 struct attribute_spec.handler. */
7883 static tree
7884 handle_alias_attribute (tree *node, tree name, tree args,
7885 int ARG_UNUSED (flags), bool *no_add_attrs)
7887 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7890 /* Handle a "weakref" attribute; arguments as in struct
7891 attribute_spec.handler. */
7893 static tree
7894 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7895 int flags, bool *no_add_attrs)
7897 tree attr = NULL_TREE;
7899 /* We must ignore the attribute when it is associated with
7900 local-scoped decls, since attribute alias is ignored and many
7901 such symbols do not even have a DECL_WEAK field. */
7902 if (decl_function_context (*node)
7903 || current_function_decl
7904 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7906 warning (OPT_Wattributes, "%qE attribute ignored", name);
7907 *no_add_attrs = true;
7908 return NULL_TREE;
7911 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7913 error ("indirect function %q+D cannot be declared weakref", *node);
7914 *no_add_attrs = true;
7915 return NULL_TREE;
7918 /* The idea here is that `weakref("name")' mutates into `weakref,
7919 alias("name")', and weakref without arguments, in turn,
7920 implicitly adds weak. */
7922 if (args)
7924 attr = tree_cons (get_identifier ("alias"), args, attr);
7925 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7927 *no_add_attrs = true;
7929 decl_attributes (node, attr, flags);
7931 else
7933 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7934 error_at (DECL_SOURCE_LOCATION (*node),
7935 "weakref attribute must appear before alias attribute");
7937 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7938 and that isn't supported; and because it wants to add it to
7939 the list of weak decls, which isn't helpful. */
7940 DECL_WEAK (*node) = 1;
7943 if (decl_in_symtab_p (*node))
7945 struct symtab_node *n = symtab_node::get (*node);
7946 if (n && n->refuse_visibility_changes)
7947 error ("%+D declared weakref after being used", *node);
7950 return NULL_TREE;
7953 /* Handle an "visibility" attribute; arguments as in
7954 struct attribute_spec.handler. */
7956 static tree
7957 handle_visibility_attribute (tree *node, tree name, tree args,
7958 int ARG_UNUSED (flags),
7959 bool *ARG_UNUSED (no_add_attrs))
7961 tree decl = *node;
7962 tree id = TREE_VALUE (args);
7963 enum symbol_visibility vis;
7965 if (TYPE_P (*node))
7967 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7968 /* OK */;
7969 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7971 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7972 name);
7973 return NULL_TREE;
7975 else if (TYPE_FIELDS (*node))
7977 error ("%qE attribute ignored because %qT is already defined",
7978 name, *node);
7979 return NULL_TREE;
7982 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7984 warning (OPT_Wattributes, "%qE attribute ignored", name);
7985 return NULL_TREE;
7988 if (TREE_CODE (id) != STRING_CST)
7990 error ("visibility argument not a string");
7991 return NULL_TREE;
7994 /* If this is a type, set the visibility on the type decl. */
7995 if (TYPE_P (decl))
7997 decl = TYPE_NAME (decl);
7998 if (!decl)
7999 return NULL_TREE;
8000 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8002 warning (OPT_Wattributes, "%qE attribute ignored on types",
8003 name);
8004 return NULL_TREE;
8008 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8009 vis = VISIBILITY_DEFAULT;
8010 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8011 vis = VISIBILITY_INTERNAL;
8012 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8013 vis = VISIBILITY_HIDDEN;
8014 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8015 vis = VISIBILITY_PROTECTED;
8016 else
8018 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8019 vis = VISIBILITY_DEFAULT;
8022 if (DECL_VISIBILITY_SPECIFIED (decl)
8023 && vis != DECL_VISIBILITY (decl))
8025 tree attributes = (TYPE_P (*node)
8026 ? TYPE_ATTRIBUTES (*node)
8027 : DECL_ATTRIBUTES (decl));
8028 if (lookup_attribute ("visibility", attributes))
8029 error ("%qD redeclared with different visibility", decl);
8030 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8031 && lookup_attribute ("dllimport", attributes))
8032 error ("%qD was declared %qs which implies default visibility",
8033 decl, "dllimport");
8034 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8035 && lookup_attribute ("dllexport", attributes))
8036 error ("%qD was declared %qs which implies default visibility",
8037 decl, "dllexport");
8040 DECL_VISIBILITY (decl) = vis;
8041 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8043 /* Go ahead and attach the attribute to the node as well. This is needed
8044 so we can determine whether we have VISIBILITY_DEFAULT because the
8045 visibility was not specified, or because it was explicitly overridden
8046 from the containing scope. */
8048 return NULL_TREE;
8051 /* Determine the ELF symbol visibility for DECL, which is either a
8052 variable or a function. It is an error to use this function if a
8053 definition of DECL is not available in this translation unit.
8054 Returns true if the final visibility has been determined by this
8055 function; false if the caller is free to make additional
8056 modifications. */
8058 bool
8059 c_determine_visibility (tree decl)
8061 gcc_assert (TREE_CODE (decl) == VAR_DECL
8062 || TREE_CODE (decl) == FUNCTION_DECL);
8064 /* If the user explicitly specified the visibility with an
8065 attribute, honor that. DECL_VISIBILITY will have been set during
8066 the processing of the attribute. We check for an explicit
8067 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8068 to distinguish the use of an attribute from the use of a "#pragma
8069 GCC visibility push(...)"; in the latter case we still want other
8070 considerations to be able to overrule the #pragma. */
8071 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8072 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8073 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8074 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8075 return true;
8077 /* Set default visibility to whatever the user supplied with
8078 visibility_specified depending on #pragma GCC visibility. */
8079 if (!DECL_VISIBILITY_SPECIFIED (decl))
8081 if (visibility_options.inpragma
8082 || DECL_VISIBILITY (decl) != default_visibility)
8084 DECL_VISIBILITY (decl) = default_visibility;
8085 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8086 /* If visibility changed and DECL already has DECL_RTL, ensure
8087 symbol flags are updated. */
8088 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8089 || TREE_CODE (decl) == FUNCTION_DECL)
8090 && DECL_RTL_SET_P (decl))
8091 make_decl_rtl (decl);
8094 return false;
8097 /* Handle an "tls_model" attribute; arguments as in
8098 struct attribute_spec.handler. */
8100 static tree
8101 handle_tls_model_attribute (tree *node, tree name, tree args,
8102 int ARG_UNUSED (flags), bool *no_add_attrs)
8104 tree id;
8105 tree decl = *node;
8106 enum tls_model kind;
8108 *no_add_attrs = true;
8110 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8112 warning (OPT_Wattributes, "%qE attribute ignored", name);
8113 return NULL_TREE;
8116 kind = DECL_TLS_MODEL (decl);
8117 id = TREE_VALUE (args);
8118 if (TREE_CODE (id) != STRING_CST)
8120 error ("tls_model argument not a string");
8121 return NULL_TREE;
8124 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8125 kind = TLS_MODEL_LOCAL_EXEC;
8126 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8127 kind = TLS_MODEL_INITIAL_EXEC;
8128 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8129 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8130 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8131 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8132 else
8133 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8135 set_decl_tls_model (decl, kind);
8136 return NULL_TREE;
8139 /* Handle a "no_instrument_function" attribute; arguments as in
8140 struct attribute_spec.handler. */
8142 static tree
8143 handle_no_instrument_function_attribute (tree *node, tree name,
8144 tree ARG_UNUSED (args),
8145 int ARG_UNUSED (flags),
8146 bool *no_add_attrs)
8148 tree decl = *node;
8150 if (TREE_CODE (decl) != FUNCTION_DECL)
8152 error_at (DECL_SOURCE_LOCATION (decl),
8153 "%qE attribute applies only to functions", name);
8154 *no_add_attrs = true;
8156 else
8157 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8159 return NULL_TREE;
8162 /* Handle a "malloc" attribute; arguments as in
8163 struct attribute_spec.handler. */
8165 static tree
8166 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8167 int ARG_UNUSED (flags), bool *no_add_attrs)
8169 if (TREE_CODE (*node) == FUNCTION_DECL
8170 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8171 DECL_IS_MALLOC (*node) = 1;
8172 else
8174 warning (OPT_Wattributes, "%qE attribute ignored", name);
8175 *no_add_attrs = true;
8178 return NULL_TREE;
8181 /* Handle a "alloc_size" attribute; arguments as in
8182 struct attribute_spec.handler. */
8184 static tree
8185 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8186 int ARG_UNUSED (flags), bool *no_add_attrs)
8188 unsigned arg_count = type_num_arguments (*node);
8189 for (; args; args = TREE_CHAIN (args))
8191 tree position = TREE_VALUE (args);
8192 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8193 && TREE_CODE (position) != FUNCTION_DECL)
8194 position = default_conversion (position);
8196 if (!tree_fits_uhwi_p (position)
8197 || !arg_count
8198 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8200 warning (OPT_Wattributes,
8201 "alloc_size parameter outside range");
8202 *no_add_attrs = true;
8203 return NULL_TREE;
8206 return NULL_TREE;
8209 /* Handle a "alloc_align" attribute; arguments as in
8210 struct attribute_spec.handler. */
8212 static tree
8213 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8214 bool *no_add_attrs)
8216 unsigned arg_count = type_num_arguments (*node);
8217 tree position = TREE_VALUE (args);
8218 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8219 position = default_conversion (position);
8221 if (!tree_fits_uhwi_p (position)
8222 || !arg_count
8223 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8225 warning (OPT_Wattributes,
8226 "alloc_align parameter outside range");
8227 *no_add_attrs = true;
8228 return NULL_TREE;
8230 return NULL_TREE;
8233 /* Handle a "assume_aligned" attribute; arguments as in
8234 struct attribute_spec.handler. */
8236 static tree
8237 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8238 bool *no_add_attrs)
8240 for (; args; args = TREE_CHAIN (args))
8242 tree position = TREE_VALUE (args);
8243 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8244 && TREE_CODE (position) != FUNCTION_DECL)
8245 position = default_conversion (position);
8247 if (TREE_CODE (position) != INTEGER_CST)
8249 warning (OPT_Wattributes,
8250 "assume_aligned parameter not integer constant");
8251 *no_add_attrs = true;
8252 return NULL_TREE;
8255 return NULL_TREE;
8258 /* Handle a "fn spec" attribute; arguments as in
8259 struct attribute_spec.handler. */
8261 static tree
8262 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8263 tree args, int ARG_UNUSED (flags),
8264 bool *no_add_attrs ATTRIBUTE_UNUSED)
8266 gcc_assert (args
8267 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8268 && !TREE_CHAIN (args));
8269 return NULL_TREE;
8272 /* Handle a "warn_unused" attribute; arguments as in
8273 struct attribute_spec.handler. */
8275 static tree
8276 handle_warn_unused_attribute (tree *node, tree name,
8277 tree args ATTRIBUTE_UNUSED,
8278 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8280 if (TYPE_P (*node))
8281 /* Do nothing else, just set the attribute. We'll get at
8282 it later with lookup_attribute. */
8284 else
8286 warning (OPT_Wattributes, "%qE attribute ignored", name);
8287 *no_add_attrs = true;
8290 return NULL_TREE;
8293 /* Handle an "omp declare simd" attribute; arguments as in
8294 struct attribute_spec.handler. */
8296 static tree
8297 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8299 return NULL_TREE;
8302 /* Handle an "omp declare target" attribute; arguments as in
8303 struct attribute_spec.handler. */
8305 static tree
8306 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8308 return NULL_TREE;
8311 /* Handle a "returns_twice" attribute; arguments as in
8312 struct attribute_spec.handler. */
8314 static tree
8315 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8316 int ARG_UNUSED (flags), bool *no_add_attrs)
8318 if (TREE_CODE (*node) == FUNCTION_DECL)
8319 DECL_IS_RETURNS_TWICE (*node) = 1;
8320 else
8322 warning (OPT_Wattributes, "%qE attribute ignored", name);
8323 *no_add_attrs = true;
8326 return NULL_TREE;
8329 /* Handle a "no_limit_stack" attribute; arguments as in
8330 struct attribute_spec.handler. */
8332 static tree
8333 handle_no_limit_stack_attribute (tree *node, tree name,
8334 tree ARG_UNUSED (args),
8335 int ARG_UNUSED (flags),
8336 bool *no_add_attrs)
8338 tree decl = *node;
8340 if (TREE_CODE (decl) != FUNCTION_DECL)
8342 error_at (DECL_SOURCE_LOCATION (decl),
8343 "%qE attribute applies only to functions", name);
8344 *no_add_attrs = true;
8346 else if (DECL_INITIAL (decl))
8348 error_at (DECL_SOURCE_LOCATION (decl),
8349 "can%'t set %qE attribute after definition", name);
8350 *no_add_attrs = true;
8352 else
8353 DECL_NO_LIMIT_STACK (decl) = 1;
8355 return NULL_TREE;
8358 /* Handle a "pure" attribute; arguments as in
8359 struct attribute_spec.handler. */
8361 static tree
8362 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8363 int ARG_UNUSED (flags), bool *no_add_attrs)
8365 if (TREE_CODE (*node) == FUNCTION_DECL)
8366 DECL_PURE_P (*node) = 1;
8367 /* ??? TODO: Support types. */
8368 else
8370 warning (OPT_Wattributes, "%qE attribute ignored", name);
8371 *no_add_attrs = true;
8374 return NULL_TREE;
8377 /* Digest an attribute list destined for a transactional memory statement.
8378 ALLOWED is the set of attributes that are allowed for this statement;
8379 return the attribute we parsed. Multiple attributes are never allowed. */
8382 parse_tm_stmt_attr (tree attrs, int allowed)
8384 tree a_seen = NULL;
8385 int m_seen = 0;
8387 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8389 tree a = TREE_PURPOSE (attrs);
8390 int m = 0;
8392 if (is_attribute_p ("outer", a))
8393 m = TM_STMT_ATTR_OUTER;
8395 if ((m & allowed) == 0)
8397 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8398 continue;
8401 if (m_seen == 0)
8403 a_seen = a;
8404 m_seen = m;
8406 else if (m_seen == m)
8407 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8408 else
8409 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8412 return m_seen;
8415 /* Transform a TM attribute name into a maskable integer and back.
8416 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8417 to how the lack of an attribute is treated. */
8420 tm_attr_to_mask (tree attr)
8422 if (attr == NULL)
8423 return 0;
8424 if (is_attribute_p ("transaction_safe", attr))
8425 return TM_ATTR_SAFE;
8426 if (is_attribute_p ("transaction_callable", attr))
8427 return TM_ATTR_CALLABLE;
8428 if (is_attribute_p ("transaction_pure", attr))
8429 return TM_ATTR_PURE;
8430 if (is_attribute_p ("transaction_unsafe", attr))
8431 return TM_ATTR_IRREVOCABLE;
8432 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8433 return TM_ATTR_MAY_CANCEL_OUTER;
8434 return 0;
8437 tree
8438 tm_mask_to_attr (int mask)
8440 const char *str;
8441 switch (mask)
8443 case TM_ATTR_SAFE:
8444 str = "transaction_safe";
8445 break;
8446 case TM_ATTR_CALLABLE:
8447 str = "transaction_callable";
8448 break;
8449 case TM_ATTR_PURE:
8450 str = "transaction_pure";
8451 break;
8452 case TM_ATTR_IRREVOCABLE:
8453 str = "transaction_unsafe";
8454 break;
8455 case TM_ATTR_MAY_CANCEL_OUTER:
8456 str = "transaction_may_cancel_outer";
8457 break;
8458 default:
8459 gcc_unreachable ();
8461 return get_identifier (str);
8464 /* Return the first TM attribute seen in LIST. */
8466 tree
8467 find_tm_attribute (tree list)
8469 for (; list ; list = TREE_CHAIN (list))
8471 tree name = TREE_PURPOSE (list);
8472 if (tm_attr_to_mask (name) != 0)
8473 return name;
8475 return NULL_TREE;
8478 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8479 Here we accept only function types, and verify that none of the other
8480 function TM attributes are also applied. */
8481 /* ??? We need to accept class types for C++, but not C. This greatly
8482 complicates this function, since we can no longer rely on the extra
8483 processing given by function_type_required. */
8485 static tree
8486 handle_tm_attribute (tree *node, tree name, tree args,
8487 int flags, bool *no_add_attrs)
8489 /* Only one path adds the attribute; others don't. */
8490 *no_add_attrs = true;
8492 switch (TREE_CODE (*node))
8494 case RECORD_TYPE:
8495 case UNION_TYPE:
8496 /* Only tm_callable and tm_safe apply to classes. */
8497 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8498 goto ignored;
8499 /* FALLTHRU */
8501 case FUNCTION_TYPE:
8502 case METHOD_TYPE:
8504 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8505 if (old_name == name)
8507 else if (old_name != NULL_TREE)
8508 error ("type was previously declared %qE", old_name);
8509 else
8510 *no_add_attrs = false;
8512 break;
8514 case POINTER_TYPE:
8516 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8517 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8519 tree fn_tmp = TREE_TYPE (*node);
8520 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8521 *node = build_pointer_type (fn_tmp);
8522 break;
8525 /* FALLTHRU */
8527 default:
8528 /* If a function is next, pass it on to be tried next. */
8529 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8530 return tree_cons (name, args, NULL);
8532 ignored:
8533 warning (OPT_Wattributes, "%qE attribute ignored", name);
8534 break;
8537 return NULL_TREE;
8540 /* Handle the TM_WRAP attribute; arguments as in
8541 struct attribute_spec.handler. */
8543 static tree
8544 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8545 int ARG_UNUSED (flags), bool *no_add_attrs)
8547 tree decl = *node;
8549 /* We don't need the attribute even on success, since we
8550 record the entry in an external table. */
8551 *no_add_attrs = true;
8553 if (TREE_CODE (decl) != FUNCTION_DECL)
8554 warning (OPT_Wattributes, "%qE attribute ignored", name);
8555 else
8557 tree wrap_decl = TREE_VALUE (args);
8558 if (error_operand_p (wrap_decl))
8560 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8561 && TREE_CODE (wrap_decl) != VAR_DECL
8562 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8563 error ("%qE argument not an identifier", name);
8564 else
8566 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8567 wrap_decl = lookup_name (wrap_decl);
8568 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8570 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8571 TREE_TYPE (wrap_decl)))
8572 record_tm_replacement (wrap_decl, decl);
8573 else
8574 error ("%qD is not compatible with %qD", wrap_decl, decl);
8576 else
8577 error ("%qE argument is not a function", name);
8581 return NULL_TREE;
8584 /* Ignore the given attribute. Used when this attribute may be usefully
8585 overridden by the target, but is not used generically. */
8587 static tree
8588 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8589 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8590 bool *no_add_attrs)
8592 *no_add_attrs = true;
8593 return NULL_TREE;
8596 /* Handle a "no vops" attribute; arguments as in
8597 struct attribute_spec.handler. */
8599 static tree
8600 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8601 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8602 bool *ARG_UNUSED (no_add_attrs))
8604 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8605 DECL_IS_NOVOPS (*node) = 1;
8606 return NULL_TREE;
8609 /* Handle a "deprecated" attribute; arguments as in
8610 struct attribute_spec.handler. */
8612 static tree
8613 handle_deprecated_attribute (tree *node, tree name,
8614 tree args, int flags,
8615 bool *no_add_attrs)
8617 tree type = NULL_TREE;
8618 int warn = 0;
8619 tree what = NULL_TREE;
8621 if (!args)
8622 *no_add_attrs = true;
8623 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8625 error ("deprecated message is not a string");
8626 *no_add_attrs = true;
8629 if (DECL_P (*node))
8631 tree decl = *node;
8632 type = TREE_TYPE (decl);
8634 if (TREE_CODE (decl) == TYPE_DECL
8635 || TREE_CODE (decl) == PARM_DECL
8636 || TREE_CODE (decl) == VAR_DECL
8637 || TREE_CODE (decl) == FUNCTION_DECL
8638 || TREE_CODE (decl) == FIELD_DECL
8639 || objc_method_decl (TREE_CODE (decl)))
8640 TREE_DEPRECATED (decl) = 1;
8641 else
8642 warn = 1;
8644 else if (TYPE_P (*node))
8646 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8647 *node = build_variant_type_copy (*node);
8648 TREE_DEPRECATED (*node) = 1;
8649 type = *node;
8651 else
8652 warn = 1;
8654 if (warn)
8656 *no_add_attrs = true;
8657 if (type && TYPE_NAME (type))
8659 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8660 what = TYPE_NAME (*node);
8661 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8662 && DECL_NAME (TYPE_NAME (type)))
8663 what = DECL_NAME (TYPE_NAME (type));
8665 if (what)
8666 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8667 else
8668 warning (OPT_Wattributes, "%qE attribute ignored", name);
8671 return NULL_TREE;
8674 /* Handle a "vector_size" attribute; arguments as in
8675 struct attribute_spec.handler. */
8677 static tree
8678 handle_vector_size_attribute (tree *node, tree name, tree args,
8679 int ARG_UNUSED (flags),
8680 bool *no_add_attrs)
8682 unsigned HOST_WIDE_INT vecsize, nunits;
8683 machine_mode orig_mode;
8684 tree type = *node, new_type, size;
8686 *no_add_attrs = true;
8688 size = TREE_VALUE (args);
8689 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8690 && TREE_CODE (size) != FUNCTION_DECL)
8691 size = default_conversion (size);
8693 if (!tree_fits_uhwi_p (size))
8695 warning (OPT_Wattributes, "%qE attribute ignored", name);
8696 return NULL_TREE;
8699 /* Get the vector size (in bytes). */
8700 vecsize = tree_to_uhwi (size);
8702 /* We need to provide for vector pointers, vector arrays, and
8703 functions returning vectors. For example:
8705 __attribute__((vector_size(16))) short *foo;
8707 In this case, the mode is SI, but the type being modified is
8708 HI, so we need to look further. */
8710 while (POINTER_TYPE_P (type)
8711 || TREE_CODE (type) == FUNCTION_TYPE
8712 || TREE_CODE (type) == METHOD_TYPE
8713 || TREE_CODE (type) == ARRAY_TYPE
8714 || TREE_CODE (type) == OFFSET_TYPE)
8715 type = TREE_TYPE (type);
8717 /* Get the mode of the type being modified. */
8718 orig_mode = TYPE_MODE (type);
8720 if ((!INTEGRAL_TYPE_P (type)
8721 && !SCALAR_FLOAT_TYPE_P (type)
8722 && !FIXED_POINT_TYPE_P (type))
8723 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8724 && GET_MODE_CLASS (orig_mode) != MODE_INT
8725 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8726 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8727 || TREE_CODE (type) == BOOLEAN_TYPE)
8729 error ("invalid vector type for attribute %qE", name);
8730 return NULL_TREE;
8733 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8735 error ("vector size not an integral multiple of component size");
8736 return NULL;
8739 if (vecsize == 0)
8741 error ("zero vector size");
8742 return NULL;
8745 /* Calculate how many units fit in the vector. */
8746 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8747 if (nunits & (nunits - 1))
8749 error ("number of components of the vector not a power of two");
8750 return NULL_TREE;
8753 new_type = build_vector_type (type, nunits);
8755 /* Build back pointers if needed. */
8756 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8758 return NULL_TREE;
8761 /* Handle the "nonnull" attribute. */
8762 static tree
8763 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8764 tree args, int ARG_UNUSED (flags),
8765 bool *no_add_attrs)
8767 tree type = *node;
8768 unsigned HOST_WIDE_INT attr_arg_num;
8770 /* If no arguments are specified, all pointer arguments should be
8771 non-null. Verify a full prototype is given so that the arguments
8772 will have the correct types when we actually check them later. */
8773 if (!args)
8775 if (!prototype_p (type))
8777 error ("nonnull attribute without arguments on a non-prototype");
8778 *no_add_attrs = true;
8780 return NULL_TREE;
8783 /* Argument list specified. Verify that each argument number references
8784 a pointer argument. */
8785 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8787 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8789 tree arg = TREE_VALUE (args);
8790 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8791 && TREE_CODE (arg) != FUNCTION_DECL)
8792 arg = default_conversion (arg);
8794 if (!get_nonnull_operand (arg, &arg_num))
8796 error ("nonnull argument has invalid operand number (argument %lu)",
8797 (unsigned long) attr_arg_num);
8798 *no_add_attrs = true;
8799 return NULL_TREE;
8802 if (prototype_p (type))
8804 function_args_iterator iter;
8805 tree argument;
8807 function_args_iter_init (&iter, type);
8808 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8810 argument = function_args_iter_cond (&iter);
8811 if (argument == NULL_TREE || ck_num == arg_num)
8812 break;
8815 if (!argument
8816 || TREE_CODE (argument) == VOID_TYPE)
8818 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8819 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8820 *no_add_attrs = true;
8821 return NULL_TREE;
8824 if (TREE_CODE (argument) != POINTER_TYPE)
8826 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8827 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8828 *no_add_attrs = true;
8829 return NULL_TREE;
8834 return NULL_TREE;
8837 /* Check the argument list of a function call for null in argument slots
8838 that are marked as requiring a non-null pointer argument. The NARGS
8839 arguments are passed in the array ARGARRAY.
8842 static void
8843 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8845 tree a;
8846 int i;
8848 attrs = lookup_attribute ("nonnull", attrs);
8849 if (attrs == NULL_TREE)
8850 return;
8852 a = attrs;
8853 /* See if any of the nonnull attributes has no arguments. If so,
8854 then every pointer argument is checked (in which case the check
8855 for pointer type is done in check_nonnull_arg). */
8856 if (TREE_VALUE (a) != NULL_TREE)
8858 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8859 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8861 if (a != NULL_TREE)
8862 for (i = 0; i < nargs; i++)
8863 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8864 i + 1);
8865 else
8867 /* Walk the argument list. If we encounter an argument number we
8868 should check for non-null, do it. */
8869 for (i = 0; i < nargs; i++)
8871 for (a = attrs; ; a = TREE_CHAIN (a))
8873 a = lookup_attribute ("nonnull", a);
8874 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8875 break;
8878 if (a != NULL_TREE)
8879 check_function_arguments_recurse (check_nonnull_arg, NULL,
8880 argarray[i], i + 1);
8885 /* Check that the Nth argument of a function call (counting backwards
8886 from the end) is a (pointer)0. The NARGS arguments are passed in the
8887 array ARGARRAY. */
8889 static void
8890 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8892 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8894 if (attr)
8896 int len = 0;
8897 int pos = 0;
8898 tree sentinel;
8899 function_args_iterator iter;
8900 tree t;
8902 /* Skip over the named arguments. */
8903 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8905 if (len == nargs)
8906 break;
8907 len++;
8910 if (TREE_VALUE (attr))
8912 tree p = TREE_VALUE (TREE_VALUE (attr));
8913 pos = TREE_INT_CST_LOW (p);
8916 /* The sentinel must be one of the varargs, i.e.
8917 in position >= the number of fixed arguments. */
8918 if ((nargs - 1 - pos) < len)
8920 warning (OPT_Wformat_,
8921 "not enough variable arguments to fit a sentinel");
8922 return;
8925 /* Validate the sentinel. */
8926 sentinel = argarray[nargs - 1 - pos];
8927 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8928 || !integer_zerop (sentinel))
8929 /* Although __null (in C++) is only an integer we allow it
8930 nevertheless, as we are guaranteed that it's exactly
8931 as wide as a pointer, and we don't want to force
8932 users to cast the NULL they have written there.
8933 We warn with -Wstrict-null-sentinel, though. */
8934 && (warn_strict_null_sentinel || null_node != sentinel))
8935 warning (OPT_Wformat_, "missing sentinel in function call");
8939 /* Helper for check_function_nonnull; given a list of operands which
8940 must be non-null in ARGS, determine if operand PARAM_NUM should be
8941 checked. */
8943 static bool
8944 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8946 unsigned HOST_WIDE_INT arg_num = 0;
8948 for (; args; args = TREE_CHAIN (args))
8950 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8952 gcc_assert (found);
8954 if (arg_num == param_num)
8955 return true;
8957 return false;
8960 /* Check that the function argument PARAM (which is operand number
8961 PARAM_NUM) is non-null. This is called by check_function_nonnull
8962 via check_function_arguments_recurse. */
8964 static void
8965 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8966 unsigned HOST_WIDE_INT param_num)
8968 /* Just skip checking the argument if it's not a pointer. This can
8969 happen if the "nonnull" attribute was given without an operand
8970 list (which means to check every pointer argument). */
8972 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8973 return;
8975 if (integer_zerop (param))
8976 warning (OPT_Wnonnull, "null argument where non-null required "
8977 "(argument %lu)", (unsigned long) param_num);
8980 /* Helper for nonnull attribute handling; fetch the operand number
8981 from the attribute argument list. */
8983 static bool
8984 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8986 /* Verify the arg number is a small constant. */
8987 if (tree_fits_uhwi_p (arg_num_expr))
8989 *valp = TREE_INT_CST_LOW (arg_num_expr);
8990 return true;
8992 else
8993 return false;
8996 /* Handle a "nothrow" attribute; arguments as in
8997 struct attribute_spec.handler. */
8999 static tree
9000 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9001 int ARG_UNUSED (flags), bool *no_add_attrs)
9003 if (TREE_CODE (*node) == FUNCTION_DECL)
9004 TREE_NOTHROW (*node) = 1;
9005 /* ??? TODO: Support types. */
9006 else
9008 warning (OPT_Wattributes, "%qE attribute ignored", name);
9009 *no_add_attrs = true;
9012 return NULL_TREE;
9015 /* Handle a "cleanup" attribute; arguments as in
9016 struct attribute_spec.handler. */
9018 static tree
9019 handle_cleanup_attribute (tree *node, tree name, tree args,
9020 int ARG_UNUSED (flags), bool *no_add_attrs)
9022 tree decl = *node;
9023 tree cleanup_id, cleanup_decl;
9025 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9026 for global destructors in C++. This requires infrastructure that
9027 we don't have generically at the moment. It's also not a feature
9028 we'd be missing too much, since we do have attribute constructor. */
9029 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9031 warning (OPT_Wattributes, "%qE attribute ignored", name);
9032 *no_add_attrs = true;
9033 return NULL_TREE;
9036 /* Verify that the argument is a function in scope. */
9037 /* ??? We could support pointers to functions here as well, if
9038 that was considered desirable. */
9039 cleanup_id = TREE_VALUE (args);
9040 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9042 error ("cleanup argument not an identifier");
9043 *no_add_attrs = true;
9044 return NULL_TREE;
9046 cleanup_decl = lookup_name (cleanup_id);
9047 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9049 error ("cleanup argument not a function");
9050 *no_add_attrs = true;
9051 return NULL_TREE;
9054 /* That the function has proper type is checked with the
9055 eventual call to build_function_call. */
9057 return NULL_TREE;
9060 /* Handle a "warn_unused_result" attribute. No special handling. */
9062 static tree
9063 handle_warn_unused_result_attribute (tree *node, tree name,
9064 tree ARG_UNUSED (args),
9065 int ARG_UNUSED (flags), bool *no_add_attrs)
9067 /* Ignore the attribute for functions not returning any value. */
9068 if (VOID_TYPE_P (TREE_TYPE (*node)))
9070 warning (OPT_Wattributes, "%qE attribute ignored", name);
9071 *no_add_attrs = true;
9074 return NULL_TREE;
9077 /* Handle a "sentinel" attribute. */
9079 static tree
9080 handle_sentinel_attribute (tree *node, tree name, tree args,
9081 int ARG_UNUSED (flags), bool *no_add_attrs)
9083 if (!prototype_p (*node))
9085 warning (OPT_Wattributes,
9086 "%qE attribute requires prototypes with named arguments", name);
9087 *no_add_attrs = true;
9089 else
9091 if (!stdarg_p (*node))
9093 warning (OPT_Wattributes,
9094 "%qE attribute only applies to variadic functions", name);
9095 *no_add_attrs = true;
9099 if (args)
9101 tree position = TREE_VALUE (args);
9102 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9103 && TREE_CODE (position) != FUNCTION_DECL)
9104 position = default_conversion (position);
9106 if (TREE_CODE (position) != INTEGER_CST
9107 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9109 warning (OPT_Wattributes,
9110 "requested position is not an integer constant");
9111 *no_add_attrs = true;
9113 else
9115 if (tree_int_cst_lt (position, integer_zero_node))
9117 warning (OPT_Wattributes,
9118 "requested position is less than zero");
9119 *no_add_attrs = true;
9124 return NULL_TREE;
9127 /* Handle a "type_generic" attribute. */
9129 static tree
9130 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9131 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9132 bool * ARG_UNUSED (no_add_attrs))
9134 /* Ensure we have a function type. */
9135 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9137 /* Ensure we have a variadic function. */
9138 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9140 return NULL_TREE;
9143 /* Handle a "target" attribute. */
9145 static tree
9146 handle_target_attribute (tree *node, tree name, tree args, int flags,
9147 bool *no_add_attrs)
9149 /* Ensure we have a function type. */
9150 if (TREE_CODE (*node) != FUNCTION_DECL)
9152 warning (OPT_Wattributes, "%qE attribute ignored", name);
9153 *no_add_attrs = true;
9155 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9156 flags))
9157 *no_add_attrs = true;
9159 return NULL_TREE;
9162 /* Arguments being collected for optimization. */
9163 typedef const char *const_char_p; /* For DEF_VEC_P. */
9164 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9167 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9168 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9169 false for #pragma GCC optimize. */
9171 bool
9172 parse_optimize_options (tree args, bool attr_p)
9174 bool ret = true;
9175 unsigned opt_argc;
9176 unsigned i;
9177 int saved_flag_strict_aliasing;
9178 const char **opt_argv;
9179 struct cl_decoded_option *decoded_options;
9180 unsigned int decoded_options_count;
9181 tree ap;
9183 /* Build up argv vector. Just in case the string is stored away, use garbage
9184 collected strings. */
9185 vec_safe_truncate (optimize_args, 0);
9186 vec_safe_push (optimize_args, (const char *) NULL);
9188 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9190 tree value = TREE_VALUE (ap);
9192 if (TREE_CODE (value) == INTEGER_CST)
9194 char buffer[20];
9195 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9196 vec_safe_push (optimize_args, ggc_strdup (buffer));
9199 else if (TREE_CODE (value) == STRING_CST)
9201 /* Split string into multiple substrings. */
9202 size_t len = TREE_STRING_LENGTH (value);
9203 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9204 char *end = p + len;
9205 char *comma;
9206 char *next_p = p;
9208 while (next_p != NULL)
9210 size_t len2;
9211 char *q, *r;
9213 p = next_p;
9214 comma = strchr (p, ',');
9215 if (comma)
9217 len2 = comma - p;
9218 *comma = '\0';
9219 next_p = comma+1;
9221 else
9223 len2 = end - p;
9224 next_p = NULL;
9227 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9229 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9230 options. */
9231 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9233 ret = false;
9234 if (attr_p)
9235 warning (OPT_Wattributes,
9236 "bad option %s to optimize attribute", p);
9237 else
9238 warning (OPT_Wpragmas,
9239 "bad option %s to pragma attribute", p);
9240 continue;
9243 if (*p != '-')
9245 *r++ = '-';
9247 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9248 itself is -Os, and any other switch begins with a -f. */
9249 if ((*p >= '0' && *p <= '9')
9250 || (p[0] == 's' && p[1] == '\0'))
9251 *r++ = 'O';
9252 else if (*p != 'O')
9253 *r++ = 'f';
9256 memcpy (r, p, len2);
9257 r[len2] = '\0';
9258 vec_safe_push (optimize_args, (const char *) q);
9264 opt_argc = optimize_args->length ();
9265 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9267 for (i = 1; i < opt_argc; i++)
9268 opt_argv[i] = (*optimize_args)[i];
9270 saved_flag_strict_aliasing = flag_strict_aliasing;
9272 /* Now parse the options. */
9273 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9274 &decoded_options,
9275 &decoded_options_count);
9276 decode_options (&global_options, &global_options_set,
9277 decoded_options, decoded_options_count,
9278 input_location, global_dc);
9280 targetm.override_options_after_change();
9282 /* Don't allow changing -fstrict-aliasing. */
9283 flag_strict_aliasing = saved_flag_strict_aliasing;
9285 optimize_args->truncate (0);
9286 return ret;
9289 /* For handling "optimize" attribute. arguments as in
9290 struct attribute_spec.handler. */
9292 static tree
9293 handle_optimize_attribute (tree *node, tree name, tree args,
9294 int ARG_UNUSED (flags), bool *no_add_attrs)
9296 /* Ensure we have a function type. */
9297 if (TREE_CODE (*node) != FUNCTION_DECL)
9299 warning (OPT_Wattributes, "%qE attribute ignored", name);
9300 *no_add_attrs = true;
9302 else
9304 struct cl_optimization cur_opts;
9305 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9307 /* Save current options. */
9308 cl_optimization_save (&cur_opts, &global_options);
9310 /* If we previously had some optimization options, use them as the
9311 default. */
9312 if (old_opts)
9313 cl_optimization_restore (&global_options,
9314 TREE_OPTIMIZATION (old_opts));
9316 /* Parse options, and update the vector. */
9317 parse_optimize_options (args, true);
9318 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9319 = build_optimization_node (&global_options);
9321 /* Restore current options. */
9322 cl_optimization_restore (&global_options, &cur_opts);
9325 return NULL_TREE;
9328 /* Handle a "no_split_stack" attribute. */
9330 static tree
9331 handle_no_split_stack_attribute (tree *node, tree name,
9332 tree ARG_UNUSED (args),
9333 int ARG_UNUSED (flags),
9334 bool *no_add_attrs)
9336 tree decl = *node;
9338 if (TREE_CODE (decl) != FUNCTION_DECL)
9340 error_at (DECL_SOURCE_LOCATION (decl),
9341 "%qE attribute applies only to functions", name);
9342 *no_add_attrs = true;
9344 else if (DECL_INITIAL (decl))
9346 error_at (DECL_SOURCE_LOCATION (decl),
9347 "can%'t set %qE attribute after definition", name);
9348 *no_add_attrs = true;
9351 return NULL_TREE;
9354 /* Handle a "returns_nonnull" attribute; arguments as in
9355 struct attribute_spec.handler. */
9357 static tree
9358 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9359 bool *no_add_attrs)
9361 // Even without a prototype we still have a return type we can check.
9362 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9364 error ("returns_nonnull attribute on a function not returning a pointer");
9365 *no_add_attrs = true;
9367 return NULL_TREE;
9370 /* Handle a "designated_init" attribute; arguments as in
9371 struct attribute_spec.handler. */
9373 static tree
9374 handle_designated_init_attribute (tree *node, tree name, tree, int,
9375 bool *no_add_attrs)
9377 if (TREE_CODE (*node) != RECORD_TYPE)
9379 error ("%qE attribute is only valid on %<struct%> type", name);
9380 *no_add_attrs = true;
9382 return NULL_TREE;
9386 /* Check for valid arguments being passed to a function with FNTYPE.
9387 There are NARGS arguments in the array ARGARRAY. */
9388 void
9389 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9391 /* Check for null being passed in a pointer argument that must be
9392 non-null. We also need to do this if format checking is enabled. */
9394 if (warn_nonnull)
9395 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9397 /* Check for errors in format strings. */
9399 if (warn_format || warn_suggest_attribute_format)
9400 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9402 if (warn_format)
9403 check_function_sentinel (fntype, nargs, argarray);
9406 /* Generic argument checking recursion routine. PARAM is the argument to
9407 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9408 once the argument is resolved. CTX is context for the callback. */
9409 void
9410 check_function_arguments_recurse (void (*callback)
9411 (void *, tree, unsigned HOST_WIDE_INT),
9412 void *ctx, tree param,
9413 unsigned HOST_WIDE_INT param_num)
9415 if (CONVERT_EXPR_P (param)
9416 && (TYPE_PRECISION (TREE_TYPE (param))
9417 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9419 /* Strip coercion. */
9420 check_function_arguments_recurse (callback, ctx,
9421 TREE_OPERAND (param, 0), param_num);
9422 return;
9425 if (TREE_CODE (param) == CALL_EXPR)
9427 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9428 tree attrs;
9429 bool found_format_arg = false;
9431 /* See if this is a call to a known internationalization function
9432 that modifies a format arg. Such a function may have multiple
9433 format_arg attributes (for example, ngettext). */
9435 for (attrs = TYPE_ATTRIBUTES (type);
9436 attrs;
9437 attrs = TREE_CHAIN (attrs))
9438 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9440 tree inner_arg;
9441 tree format_num_expr;
9442 int format_num;
9443 int i;
9444 call_expr_arg_iterator iter;
9446 /* Extract the argument number, which was previously checked
9447 to be valid. */
9448 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9450 format_num = tree_to_uhwi (format_num_expr);
9452 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9453 inner_arg != 0;
9454 inner_arg = next_call_expr_arg (&iter), i++)
9455 if (i == format_num)
9457 check_function_arguments_recurse (callback, ctx,
9458 inner_arg, param_num);
9459 found_format_arg = true;
9460 break;
9464 /* If we found a format_arg attribute and did a recursive check,
9465 we are done with checking this argument. Otherwise, we continue
9466 and this will be considered a non-literal. */
9467 if (found_format_arg)
9468 return;
9471 if (TREE_CODE (param) == COND_EXPR)
9473 /* Check both halves of the conditional expression. */
9474 check_function_arguments_recurse (callback, ctx,
9475 TREE_OPERAND (param, 1), param_num);
9476 check_function_arguments_recurse (callback, ctx,
9477 TREE_OPERAND (param, 2), param_num);
9478 return;
9481 (*callback) (ctx, param, param_num);
9484 /* Checks for a builtin function FNDECL that the number of arguments
9485 NARGS against the required number REQUIRED and issues an error if
9486 there is a mismatch. Returns true if the number of arguments is
9487 correct, otherwise false. */
9489 static bool
9490 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9492 if (nargs < required)
9494 error_at (input_location,
9495 "not enough arguments to function %qE", fndecl);
9496 return false;
9498 else if (nargs > required)
9500 error_at (input_location,
9501 "too many arguments to function %qE", fndecl);
9502 return false;
9504 return true;
9507 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9508 Returns false if there was an error, otherwise true. */
9510 bool
9511 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9513 if (!DECL_BUILT_IN (fndecl)
9514 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9515 return true;
9517 switch (DECL_FUNCTION_CODE (fndecl))
9519 case BUILT_IN_CONSTANT_P:
9520 return builtin_function_validate_nargs (fndecl, nargs, 1);
9522 case BUILT_IN_ISFINITE:
9523 case BUILT_IN_ISINF:
9524 case BUILT_IN_ISINF_SIGN:
9525 case BUILT_IN_ISNAN:
9526 case BUILT_IN_ISNORMAL:
9527 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9529 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9531 error ("non-floating-point argument in call to "
9532 "function %qE", fndecl);
9533 return false;
9535 return true;
9537 return false;
9539 case BUILT_IN_ISGREATER:
9540 case BUILT_IN_ISGREATEREQUAL:
9541 case BUILT_IN_ISLESS:
9542 case BUILT_IN_ISLESSEQUAL:
9543 case BUILT_IN_ISLESSGREATER:
9544 case BUILT_IN_ISUNORDERED:
9545 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9547 enum tree_code code0, code1;
9548 code0 = TREE_CODE (TREE_TYPE (args[0]));
9549 code1 = TREE_CODE (TREE_TYPE (args[1]));
9550 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9551 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9552 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9554 error ("non-floating-point arguments in call to "
9555 "function %qE", fndecl);
9556 return false;
9558 return true;
9560 return false;
9562 case BUILT_IN_FPCLASSIFY:
9563 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9565 unsigned i;
9567 for (i=0; i<5; i++)
9568 if (TREE_CODE (args[i]) != INTEGER_CST)
9570 error ("non-const integer argument %u in call to function %qE",
9571 i+1, fndecl);
9572 return false;
9575 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9577 error ("non-floating-point argument in call to function %qE",
9578 fndecl);
9579 return false;
9581 return true;
9583 return false;
9585 case BUILT_IN_ASSUME_ALIGNED:
9586 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9588 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9590 error ("non-integer argument 3 in call to function %qE", fndecl);
9591 return false;
9593 return true;
9595 return false;
9597 default:
9598 return true;
9602 /* Function to help qsort sort FIELD_DECLs by name order. */
9605 field_decl_cmp (const void *x_p, const void *y_p)
9607 const tree *const x = (const tree *const) x_p;
9608 const tree *const y = (const tree *const) y_p;
9610 if (DECL_NAME (*x) == DECL_NAME (*y))
9611 /* A nontype is "greater" than a type. */
9612 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9613 if (DECL_NAME (*x) == NULL_TREE)
9614 return -1;
9615 if (DECL_NAME (*y) == NULL_TREE)
9616 return 1;
9617 if (DECL_NAME (*x) < DECL_NAME (*y))
9618 return -1;
9619 return 1;
9622 static struct {
9623 gt_pointer_operator new_value;
9624 void *cookie;
9625 } resort_data;
9627 /* This routine compares two fields like field_decl_cmp but using the
9628 pointer operator in resort_data. */
9630 static int
9631 resort_field_decl_cmp (const void *x_p, const void *y_p)
9633 const tree *const x = (const tree *const) x_p;
9634 const tree *const y = (const tree *const) y_p;
9636 if (DECL_NAME (*x) == DECL_NAME (*y))
9637 /* A nontype is "greater" than a type. */
9638 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9639 if (DECL_NAME (*x) == NULL_TREE)
9640 return -1;
9641 if (DECL_NAME (*y) == NULL_TREE)
9642 return 1;
9644 tree d1 = DECL_NAME (*x);
9645 tree d2 = DECL_NAME (*y);
9646 resort_data.new_value (&d1, resort_data.cookie);
9647 resort_data.new_value (&d2, resort_data.cookie);
9648 if (d1 < d2)
9649 return -1;
9651 return 1;
9654 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9656 void
9657 resort_sorted_fields (void *obj,
9658 void * ARG_UNUSED (orig_obj),
9659 gt_pointer_operator new_value,
9660 void *cookie)
9662 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9663 resort_data.new_value = new_value;
9664 resort_data.cookie = cookie;
9665 qsort (&sf->elts[0], sf->len, sizeof (tree),
9666 resort_field_decl_cmp);
9669 /* Subroutine of c_parse_error.
9670 Return the result of concatenating LHS and RHS. RHS is really
9671 a string literal, its first character is indicated by RHS_START and
9672 RHS_SIZE is its length (including the terminating NUL character).
9674 The caller is responsible for deleting the returned pointer. */
9676 static char *
9677 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9679 const int lhs_size = strlen (lhs);
9680 char *result = XNEWVEC (char, lhs_size + rhs_size);
9681 strncpy (result, lhs, lhs_size);
9682 strncpy (result + lhs_size, rhs_start, rhs_size);
9683 return result;
9686 /* Issue the error given by GMSGID, indicating that it occurred before
9687 TOKEN, which had the associated VALUE. */
9689 void
9690 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9691 tree value, unsigned char token_flags)
9693 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9695 char *message = NULL;
9697 if (token_type == CPP_EOF)
9698 message = catenate_messages (gmsgid, " at end of input");
9699 else if (token_type == CPP_CHAR
9700 || token_type == CPP_WCHAR
9701 || token_type == CPP_CHAR16
9702 || token_type == CPP_CHAR32)
9704 unsigned int val = TREE_INT_CST_LOW (value);
9705 const char *prefix;
9707 switch (token_type)
9709 default:
9710 prefix = "";
9711 break;
9712 case CPP_WCHAR:
9713 prefix = "L";
9714 break;
9715 case CPP_CHAR16:
9716 prefix = "u";
9717 break;
9718 case CPP_CHAR32:
9719 prefix = "U";
9720 break;
9723 if (val <= UCHAR_MAX && ISGRAPH (val))
9724 message = catenate_messages (gmsgid, " before %s'%c'");
9725 else
9726 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9728 error (message, prefix, val);
9729 free (message);
9730 message = NULL;
9732 else if (token_type == CPP_CHAR_USERDEF
9733 || token_type == CPP_WCHAR_USERDEF
9734 || token_type == CPP_CHAR16_USERDEF
9735 || token_type == CPP_CHAR32_USERDEF)
9736 message = catenate_messages (gmsgid,
9737 " before user-defined character literal");
9738 else if (token_type == CPP_STRING_USERDEF
9739 || token_type == CPP_WSTRING_USERDEF
9740 || token_type == CPP_STRING16_USERDEF
9741 || token_type == CPP_STRING32_USERDEF
9742 || token_type == CPP_UTF8STRING_USERDEF)
9743 message = catenate_messages (gmsgid, " before user-defined string literal");
9744 else if (token_type == CPP_STRING
9745 || token_type == CPP_WSTRING
9746 || token_type == CPP_STRING16
9747 || token_type == CPP_STRING32
9748 || token_type == CPP_UTF8STRING)
9749 message = catenate_messages (gmsgid, " before string constant");
9750 else if (token_type == CPP_NUMBER)
9751 message = catenate_messages (gmsgid, " before numeric constant");
9752 else if (token_type == CPP_NAME)
9754 message = catenate_messages (gmsgid, " before %qE");
9755 error (message, value);
9756 free (message);
9757 message = NULL;
9759 else if (token_type == CPP_PRAGMA)
9760 message = catenate_messages (gmsgid, " before %<#pragma%>");
9761 else if (token_type == CPP_PRAGMA_EOL)
9762 message = catenate_messages (gmsgid, " before end of line");
9763 else if (token_type == CPP_DECLTYPE)
9764 message = catenate_messages (gmsgid, " before %<decltype%>");
9765 else if (token_type < N_TTYPES)
9767 message = catenate_messages (gmsgid, " before %qs token");
9768 error (message, cpp_type2name (token_type, token_flags));
9769 free (message);
9770 message = NULL;
9772 else
9773 error (gmsgid);
9775 if (message)
9777 error (message);
9778 free (message);
9780 #undef catenate_messages
9783 /* Return the gcc option code associated with the reason for a cpp
9784 message, or 0 if none. */
9786 static int
9787 c_option_controlling_cpp_error (int reason)
9789 const struct cpp_reason_option_codes_t *entry;
9791 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
9793 if (entry->reason == reason)
9794 return entry->option_code;
9796 return 0;
9799 /* Callback from cpp_error for PFILE to print diagnostics from the
9800 preprocessor. The diagnostic is of type LEVEL, with REASON set
9801 to the reason code if LEVEL is represents a warning, at location
9802 LOCATION unless this is after lexing and the compiler's location
9803 should be used instead, with column number possibly overridden by
9804 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9805 the arguments. Returns true if a diagnostic was emitted, false
9806 otherwise. */
9808 bool
9809 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9810 location_t location, unsigned int column_override,
9811 const char *msg, va_list *ap)
9813 diagnostic_info diagnostic;
9814 diagnostic_t dlevel;
9815 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9816 bool ret;
9818 switch (level)
9820 case CPP_DL_WARNING_SYSHDR:
9821 if (flag_no_output)
9822 return false;
9823 global_dc->dc_warn_system_headers = 1;
9824 /* Fall through. */
9825 case CPP_DL_WARNING:
9826 if (flag_no_output)
9827 return false;
9828 dlevel = DK_WARNING;
9829 break;
9830 case CPP_DL_PEDWARN:
9831 if (flag_no_output && !flag_pedantic_errors)
9832 return false;
9833 dlevel = DK_PEDWARN;
9834 break;
9835 case CPP_DL_ERROR:
9836 dlevel = DK_ERROR;
9837 break;
9838 case CPP_DL_ICE:
9839 dlevel = DK_ICE;
9840 break;
9841 case CPP_DL_NOTE:
9842 dlevel = DK_NOTE;
9843 break;
9844 case CPP_DL_FATAL:
9845 dlevel = DK_FATAL;
9846 break;
9847 default:
9848 gcc_unreachable ();
9850 if (done_lexing)
9851 location = input_location;
9852 diagnostic_set_info_translated (&diagnostic, msg, ap,
9853 location, dlevel);
9854 if (column_override)
9855 diagnostic_override_column (&diagnostic, column_override);
9856 diagnostic_override_option_index (&diagnostic,
9857 c_option_controlling_cpp_error (reason));
9858 ret = report_diagnostic (&diagnostic);
9859 if (level == CPP_DL_WARNING_SYSHDR)
9860 global_dc->dc_warn_system_headers = save_warn_system_headers;
9861 return ret;
9864 /* Convert a character from the host to the target execution character
9865 set. cpplib handles this, mostly. */
9867 HOST_WIDE_INT
9868 c_common_to_target_charset (HOST_WIDE_INT c)
9870 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9871 zero-extended under -fno-signed-char. cpplib insists that characters
9872 and character constants are always unsigned. Hence we must convert
9873 back and forth. */
9874 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9876 uc = cpp_host_to_exec_charset (parse_in, uc);
9878 if (flag_signed_char)
9879 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9880 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9881 else
9882 return uc;
9885 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9886 references with an INDIRECT_REF of a constant at the bottom; much like the
9887 traditional rendering of offsetof as a macro. Return the folded result. */
9889 tree
9890 fold_offsetof_1 (tree expr)
9892 tree base, off, t;
9894 switch (TREE_CODE (expr))
9896 case ERROR_MARK:
9897 return expr;
9899 case VAR_DECL:
9900 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9901 return error_mark_node;
9903 case CALL_EXPR:
9904 case TARGET_EXPR:
9905 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9906 return error_mark_node;
9908 case NOP_EXPR:
9909 case INDIRECT_REF:
9910 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9912 error ("cannot apply %<offsetof%> to a non constant address");
9913 return error_mark_node;
9915 return TREE_OPERAND (expr, 0);
9917 case COMPONENT_REF:
9918 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9919 if (base == error_mark_node)
9920 return base;
9922 t = TREE_OPERAND (expr, 1);
9923 if (DECL_C_BIT_FIELD (t))
9925 error ("attempt to take address of bit-field structure "
9926 "member %qD", t);
9927 return error_mark_node;
9929 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9930 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
9931 / BITS_PER_UNIT));
9932 break;
9934 case ARRAY_REF:
9935 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9936 if (base == error_mark_node)
9937 return base;
9939 t = TREE_OPERAND (expr, 1);
9941 /* Check if the offset goes beyond the upper bound of the array. */
9942 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9944 tree upbound = array_ref_up_bound (expr);
9945 if (upbound != NULL_TREE
9946 && TREE_CODE (upbound) == INTEGER_CST
9947 && !tree_int_cst_equal (upbound,
9948 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9950 upbound = size_binop (PLUS_EXPR, upbound,
9951 build_int_cst (TREE_TYPE (upbound), 1));
9952 if (tree_int_cst_lt (upbound, t))
9954 tree v;
9956 for (v = TREE_OPERAND (expr, 0);
9957 TREE_CODE (v) == COMPONENT_REF;
9958 v = TREE_OPERAND (v, 0))
9959 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9960 == RECORD_TYPE)
9962 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9963 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9964 if (TREE_CODE (fld_chain) == FIELD_DECL)
9965 break;
9967 if (fld_chain)
9968 break;
9970 /* Don't warn if the array might be considered a poor
9971 man's flexible array member with a very permissive
9972 definition thereof. */
9973 if (TREE_CODE (v) == ARRAY_REF
9974 || TREE_CODE (v) == COMPONENT_REF)
9975 warning (OPT_Warray_bounds,
9976 "index %E denotes an offset "
9977 "greater than size of %qT",
9978 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9983 t = convert (sizetype, t);
9984 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9985 break;
9987 case COMPOUND_EXPR:
9988 /* Handle static members of volatile structs. */
9989 t = TREE_OPERAND (expr, 1);
9990 gcc_assert (TREE_CODE (t) == VAR_DECL);
9991 return fold_offsetof_1 (t);
9993 default:
9994 gcc_unreachable ();
9997 return fold_build_pointer_plus (base, off);
10000 /* Likewise, but convert it to the return type of offsetof. */
10002 tree
10003 fold_offsetof (tree expr)
10005 return convert (size_type_node, fold_offsetof_1 (expr));
10008 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10009 expression, because B will always be true. */
10011 void
10012 warn_for_omitted_condop (location_t location, tree cond)
10014 if (truth_value_p (TREE_CODE (cond)))
10015 warning_at (location, OPT_Wparentheses,
10016 "the omitted middle operand in ?: will always be %<true%>, "
10017 "suggest explicit middle operand");
10020 /* Give an error for storing into ARG, which is 'const'. USE indicates
10021 how ARG was being used. */
10023 void
10024 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10026 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10027 || use == lv_asm);
10028 /* Using this macro rather than (for example) arrays of messages
10029 ensures that all the format strings are checked at compile
10030 time. */
10031 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10032 : (use == lv_increment ? (I) \
10033 : (use == lv_decrement ? (D) : (AS))))
10034 if (TREE_CODE (arg) == COMPONENT_REF)
10036 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10037 error_at (loc, READONLY_MSG (G_("assignment of member "
10038 "%qD in read-only object"),
10039 G_("increment of member "
10040 "%qD in read-only object"),
10041 G_("decrement of member "
10042 "%qD in read-only object"),
10043 G_("member %qD in read-only object "
10044 "used as %<asm%> output")),
10045 TREE_OPERAND (arg, 1));
10046 else
10047 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10048 G_("increment of read-only member %qD"),
10049 G_("decrement of read-only member %qD"),
10050 G_("read-only member %qD used as %<asm%> output")),
10051 TREE_OPERAND (arg, 1));
10053 else if (TREE_CODE (arg) == VAR_DECL)
10054 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10055 G_("increment of read-only variable %qD"),
10056 G_("decrement of read-only variable %qD"),
10057 G_("read-only variable %qD used as %<asm%> output")),
10058 arg);
10059 else if (TREE_CODE (arg) == PARM_DECL)
10060 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10061 G_("increment of read-only parameter %qD"),
10062 G_("decrement of read-only parameter %qD"),
10063 G_("read-only parameter %qD use as %<asm%> output")),
10064 arg);
10065 else if (TREE_CODE (arg) == RESULT_DECL)
10067 gcc_assert (c_dialect_cxx ());
10068 error_at (loc, READONLY_MSG (G_("assignment of "
10069 "read-only named return value %qD"),
10070 G_("increment of "
10071 "read-only named return value %qD"),
10072 G_("decrement of "
10073 "read-only named return value %qD"),
10074 G_("read-only named return value %qD "
10075 "used as %<asm%>output")),
10076 arg);
10078 else if (TREE_CODE (arg) == FUNCTION_DECL)
10079 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10080 G_("increment of function %qD"),
10081 G_("decrement of function %qD"),
10082 G_("function %qD used as %<asm%> output")),
10083 arg);
10084 else
10085 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10086 G_("increment of read-only location %qE"),
10087 G_("decrement of read-only location %qE"),
10088 G_("read-only location %qE used as %<asm%> output")),
10089 arg);
10092 /* Print an error message for an invalid lvalue. USE says
10093 how the lvalue is being used and so selects the error message. LOC
10094 is the location for the error. */
10096 void
10097 lvalue_error (location_t loc, enum lvalue_use use)
10099 switch (use)
10101 case lv_assign:
10102 error_at (loc, "lvalue required as left operand of assignment");
10103 break;
10104 case lv_increment:
10105 error_at (loc, "lvalue required as increment operand");
10106 break;
10107 case lv_decrement:
10108 error_at (loc, "lvalue required as decrement operand");
10109 break;
10110 case lv_addressof:
10111 error_at (loc, "lvalue required as unary %<&%> operand");
10112 break;
10113 case lv_asm:
10114 error_at (loc, "lvalue required in asm statement");
10115 break;
10116 default:
10117 gcc_unreachable ();
10121 /* Print an error message for an invalid indirection of type TYPE.
10122 ERRSTRING is the name of the operator for the indirection. */
10124 void
10125 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10127 switch (errstring)
10129 case RO_NULL:
10130 gcc_assert (c_dialect_cxx ());
10131 error_at (loc, "invalid type argument (have %qT)", type);
10132 break;
10133 case RO_ARRAY_INDEXING:
10134 error_at (loc,
10135 "invalid type argument of array indexing (have %qT)",
10136 type);
10137 break;
10138 case RO_UNARY_STAR:
10139 error_at (loc,
10140 "invalid type argument of unary %<*%> (have %qT)",
10141 type);
10142 break;
10143 case RO_ARROW:
10144 error_at (loc,
10145 "invalid type argument of %<->%> (have %qT)",
10146 type);
10147 break;
10148 case RO_ARROW_STAR:
10149 error_at (loc,
10150 "invalid type argument of %<->*%> (have %qT)",
10151 type);
10152 break;
10153 case RO_IMPLICIT_CONVERSION:
10154 error_at (loc,
10155 "invalid type argument of implicit conversion (have %qT)",
10156 type);
10157 break;
10158 default:
10159 gcc_unreachable ();
10163 /* *PTYPE is an incomplete array. Complete it with a domain based on
10164 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10165 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10166 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10169 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10171 tree maxindex, type, main_type, elt, unqual_elt;
10172 int failure = 0, quals;
10173 hashval_t hashcode = 0;
10174 bool overflow_p = false;
10176 maxindex = size_zero_node;
10177 if (initial_value)
10179 if (TREE_CODE (initial_value) == STRING_CST)
10181 int eltsize
10182 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10183 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10185 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10187 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10189 if (vec_safe_is_empty (v))
10191 if (pedantic)
10192 failure = 3;
10193 maxindex = ssize_int (-1);
10195 else
10197 tree curindex;
10198 unsigned HOST_WIDE_INT cnt;
10199 constructor_elt *ce;
10200 bool fold_p = false;
10202 if ((*v)[0].index)
10203 maxindex = (*v)[0].index, fold_p = true;
10205 curindex = maxindex;
10207 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10209 bool curfold_p = false;
10210 if (ce->index)
10211 curindex = ce->index, curfold_p = true;
10212 else
10214 if (fold_p)
10216 /* Since we treat size types now as ordinary
10217 unsigned types, we need an explicit overflow
10218 check. */
10219 tree orig = curindex;
10220 curindex = fold_convert (sizetype, curindex);
10221 overflow_p |= tree_int_cst_lt (curindex, orig);
10223 curindex = size_binop (PLUS_EXPR, curindex,
10224 size_one_node);
10226 if (tree_int_cst_lt (maxindex, curindex))
10227 maxindex = curindex, fold_p = curfold_p;
10229 if (fold_p)
10231 tree orig = maxindex;
10232 maxindex = fold_convert (sizetype, maxindex);
10233 overflow_p |= tree_int_cst_lt (maxindex, orig);
10237 else
10239 /* Make an error message unless that happened already. */
10240 if (initial_value != error_mark_node)
10241 failure = 1;
10244 else
10246 failure = 2;
10247 if (!do_default)
10248 return failure;
10251 type = *ptype;
10252 elt = TREE_TYPE (type);
10253 quals = TYPE_QUALS (strip_array_types (elt));
10254 if (quals == 0)
10255 unqual_elt = elt;
10256 else
10257 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10259 /* Using build_distinct_type_copy and modifying things afterward instead
10260 of using build_array_type to create a new type preserves all of the
10261 TYPE_LANG_FLAG_? bits that the front end may have set. */
10262 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10263 TREE_TYPE (main_type) = unqual_elt;
10264 TYPE_DOMAIN (main_type)
10265 = build_range_type (TREE_TYPE (maxindex),
10266 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10267 layout_type (main_type);
10269 /* Make sure we have the canonical MAIN_TYPE. */
10270 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10271 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10272 hashcode);
10273 main_type = type_hash_canon (hashcode, main_type);
10275 /* Fix the canonical type. */
10276 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10277 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10278 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10279 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10280 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10281 != TYPE_DOMAIN (main_type)))
10282 TYPE_CANONICAL (main_type)
10283 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10284 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10285 else
10286 TYPE_CANONICAL (main_type) = main_type;
10288 if (quals == 0)
10289 type = main_type;
10290 else
10291 type = c_build_qualified_type (main_type, quals);
10293 if (COMPLETE_TYPE_P (type)
10294 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10295 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10297 error ("size of array is too large");
10298 /* If we proceed with the array type as it is, we'll eventually
10299 crash in tree_to_[su]hwi(). */
10300 type = error_mark_node;
10303 *ptype = type;
10304 return failure;
10307 /* Like c_mark_addressable but don't check register qualifier. */
10308 void
10309 c_common_mark_addressable_vec (tree t)
10311 while (handled_component_p (t))
10312 t = TREE_OPERAND (t, 0);
10313 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10314 return;
10315 TREE_ADDRESSABLE (t) = 1;
10320 /* Used to help initialize the builtin-types.def table. When a type of
10321 the correct size doesn't exist, use error_mark_node instead of NULL.
10322 The later results in segfaults even when a decl using the type doesn't
10323 get invoked. */
10325 tree
10326 builtin_type_for_size (int size, bool unsignedp)
10328 tree type = c_common_type_for_size (size, unsignedp);
10329 return type ? type : error_mark_node;
10332 /* A helper function for resolve_overloaded_builtin in resolving the
10333 overloaded __sync_ builtins. Returns a positive power of 2 if the
10334 first operand of PARAMS is a pointer to a supported data type.
10335 Returns 0 if an error is encountered. */
10337 static int
10338 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10340 tree type;
10341 int size;
10343 if (!params)
10345 error ("too few arguments to function %qE", function);
10346 return 0;
10349 type = TREE_TYPE ((*params)[0]);
10350 if (TREE_CODE (type) == ARRAY_TYPE)
10352 /* Force array-to-pointer decay for C++. */
10353 gcc_assert (c_dialect_cxx());
10354 (*params)[0] = default_conversion ((*params)[0]);
10355 type = TREE_TYPE ((*params)[0]);
10357 if (TREE_CODE (type) != POINTER_TYPE)
10358 goto incompatible;
10360 type = TREE_TYPE (type);
10361 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10362 goto incompatible;
10364 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10365 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10366 return size;
10368 incompatible:
10369 error ("incompatible type for argument %d of %qE", 1, function);
10370 return 0;
10373 /* A helper function for resolve_overloaded_builtin. Adds casts to
10374 PARAMS to make arguments match up with those of FUNCTION. Drops
10375 the variadic arguments at the end. Returns false if some error
10376 was encountered; true on success. */
10378 static bool
10379 sync_resolve_params (location_t loc, tree orig_function, tree function,
10380 vec<tree, va_gc> *params, bool orig_format)
10382 function_args_iterator iter;
10383 tree ptype;
10384 unsigned int parmnum;
10386 function_args_iter_init (&iter, TREE_TYPE (function));
10387 /* We've declared the implementation functions to use "volatile void *"
10388 as the pointer parameter, so we shouldn't get any complaints from the
10389 call to check_function_arguments what ever type the user used. */
10390 function_args_iter_next (&iter);
10391 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10392 ptype = TYPE_MAIN_VARIANT (ptype);
10394 /* For the rest of the values, we need to cast these to FTYPE, so that we
10395 don't get warnings for passing pointer types, etc. */
10396 parmnum = 0;
10397 while (1)
10399 tree val, arg_type;
10401 arg_type = function_args_iter_cond (&iter);
10402 /* XXX void_type_node belies the abstraction. */
10403 if (arg_type == void_type_node)
10404 break;
10406 ++parmnum;
10407 if (params->length () <= parmnum)
10409 error_at (loc, "too few arguments to function %qE", orig_function);
10410 return false;
10413 /* Only convert parameters if arg_type is unsigned integer type with
10414 new format sync routines, i.e. don't attempt to convert pointer
10415 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10416 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10417 kinds). */
10418 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10420 /* Ideally for the first conversion we'd use convert_for_assignment
10421 so that we get warnings for anything that doesn't match the pointer
10422 type. This isn't portable across the C and C++ front ends atm. */
10423 val = (*params)[parmnum];
10424 val = convert (ptype, val);
10425 val = convert (arg_type, val);
10426 (*params)[parmnum] = val;
10429 function_args_iter_next (&iter);
10432 /* __atomic routines are not variadic. */
10433 if (!orig_format && params->length () != parmnum + 1)
10435 error_at (loc, "too many arguments to function %qE", orig_function);
10436 return false;
10439 /* The definition of these primitives is variadic, with the remaining
10440 being "an optional list of variables protected by the memory barrier".
10441 No clue what that's supposed to mean, precisely, but we consider all
10442 call-clobbered variables to be protected so we're safe. */
10443 params->truncate (parmnum + 1);
10445 return true;
10448 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10449 RESULT to make it match the type of the first pointer argument in
10450 PARAMS. */
10452 static tree
10453 sync_resolve_return (tree first_param, tree result, bool orig_format)
10455 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10456 tree rtype = TREE_TYPE (result);
10457 ptype = TYPE_MAIN_VARIANT (ptype);
10459 /* New format doesn't require casting unless the types are the same size. */
10460 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10461 return convert (ptype, result);
10462 else
10463 return result;
10466 /* This function verifies the PARAMS to generic atomic FUNCTION.
10467 It returns the size if all the parameters are the same size, otherwise
10468 0 is returned if the parameters are invalid. */
10470 static int
10471 get_atomic_generic_size (location_t loc, tree function,
10472 vec<tree, va_gc> *params)
10474 unsigned int n_param;
10475 unsigned int n_model;
10476 unsigned int x;
10477 int size_0;
10478 tree type_0;
10480 /* Determine the parameter makeup. */
10481 switch (DECL_FUNCTION_CODE (function))
10483 case BUILT_IN_ATOMIC_EXCHANGE:
10484 n_param = 4;
10485 n_model = 1;
10486 break;
10487 case BUILT_IN_ATOMIC_LOAD:
10488 case BUILT_IN_ATOMIC_STORE:
10489 n_param = 3;
10490 n_model = 1;
10491 break;
10492 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10493 n_param = 6;
10494 n_model = 2;
10495 break;
10496 default:
10497 gcc_unreachable ();
10500 if (vec_safe_length (params) != n_param)
10502 error_at (loc, "incorrect number of arguments to function %qE", function);
10503 return 0;
10506 /* Get type of first parameter, and determine its size. */
10507 type_0 = TREE_TYPE ((*params)[0]);
10508 if (TREE_CODE (type_0) == ARRAY_TYPE)
10510 /* Force array-to-pointer decay for C++. */
10511 gcc_assert (c_dialect_cxx());
10512 (*params)[0] = default_conversion ((*params)[0]);
10513 type_0 = TREE_TYPE ((*params)[0]);
10515 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10517 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10518 function);
10519 return 0;
10522 /* Types must be compile time constant sizes. */
10523 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10525 error_at (loc,
10526 "argument 1 of %qE must be a pointer to a constant size type",
10527 function);
10528 return 0;
10531 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10533 /* Zero size objects are not allowed. */
10534 if (size_0 == 0)
10536 error_at (loc,
10537 "argument 1 of %qE must be a pointer to a nonzero size object",
10538 function);
10539 return 0;
10542 /* Check each other parameter is a pointer and the same size. */
10543 for (x = 0; x < n_param - n_model; x++)
10545 int size;
10546 tree type = TREE_TYPE ((*params)[x]);
10547 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10548 if (n_param == 6 && x == 3)
10549 continue;
10550 if (!POINTER_TYPE_P (type))
10552 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10553 function);
10554 return 0;
10556 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10557 size = type_size ? tree_to_uhwi (type_size) : 0;
10558 if (size != size_0)
10560 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10561 function);
10562 return 0;
10566 /* Check memory model parameters for validity. */
10567 for (x = n_param - n_model ; x < n_param; x++)
10569 tree p = (*params)[x];
10570 if (TREE_CODE (p) == INTEGER_CST)
10572 int i = tree_to_uhwi (p);
10573 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10575 warning_at (loc, OPT_Winvalid_memory_model,
10576 "invalid memory model argument %d of %qE", x + 1,
10577 function);
10580 else
10581 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10583 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10584 function);
10585 return 0;
10589 return size_0;
10593 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10594 at the beginning of the parameter list PARAMS representing the size of the
10595 objects. This is to match the library ABI requirement. LOC is the location
10596 of the function call.
10597 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10598 returned to allow the external call to be constructed. */
10600 static tree
10601 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10602 vec<tree, va_gc> *params)
10604 tree size_node;
10606 /* Insert a SIZE_T parameter as the first param. If there isn't
10607 enough space, allocate a new vector and recursively re-build with that. */
10608 if (!params->space (1))
10610 unsigned int z, len;
10611 vec<tree, va_gc> *v;
10612 tree f;
10614 len = params->length ();
10615 vec_alloc (v, len + 1);
10616 v->quick_push (build_int_cst (size_type_node, n));
10617 for (z = 0; z < len; z++)
10618 v->quick_push ((*params)[z]);
10619 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10620 vec_free (v);
10621 return f;
10624 /* Add the size parameter and leave as a function call for processing. */
10625 size_node = build_int_cst (size_type_node, n);
10626 params->quick_insert (0, size_node);
10627 return NULL_TREE;
10631 /* Return whether atomic operations for naturally aligned N-byte
10632 arguments are supported, whether inline or through libatomic. */
10633 static bool
10634 atomic_size_supported_p (int n)
10636 switch (n)
10638 case 1:
10639 case 2:
10640 case 4:
10641 case 8:
10642 return true;
10644 case 16:
10645 return targetm.scalar_mode_supported_p (TImode);
10647 default:
10648 return false;
10652 /* This will process an __atomic_exchange function call, determine whether it
10653 needs to be mapped to the _N variation, or turned into a library call.
10654 LOC is the location of the builtin call.
10655 FUNCTION is the DECL that has been invoked;
10656 PARAMS is the argument list for the call. The return value is non-null
10657 TRUE is returned if it is translated into the proper format for a call to the
10658 external library, and NEW_RETURN is set the tree for that function.
10659 FALSE is returned if processing for the _N variation is required, and
10660 NEW_RETURN is set to the the return value the result is copied into. */
10661 static bool
10662 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10663 vec<tree, va_gc> *params, tree *new_return)
10665 tree p0, p1, p2, p3;
10666 tree I_type, I_type_ptr;
10667 int n = get_atomic_generic_size (loc, function, params);
10669 /* Size of 0 is an error condition. */
10670 if (n == 0)
10672 *new_return = error_mark_node;
10673 return true;
10676 /* If not a lock-free size, change to the library generic format. */
10677 if (!atomic_size_supported_p (n))
10679 *new_return = add_atomic_size_parameter (n, loc, function, params);
10680 return true;
10683 /* Otherwise there is a lockfree match, transform the call from:
10684 void fn(T* mem, T* desired, T* return, model)
10685 into
10686 *return = (T) (fn (In* mem, (In) *desired, model)) */
10688 p0 = (*params)[0];
10689 p1 = (*params)[1];
10690 p2 = (*params)[2];
10691 p3 = (*params)[3];
10693 /* Create pointer to appropriate size. */
10694 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10695 I_type_ptr = build_pointer_type (I_type);
10697 /* Convert object pointer to required type. */
10698 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10699 (*params)[0] = p0;
10700 /* Convert new value to required type, and dereference it. */
10701 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10702 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10703 (*params)[1] = p1;
10705 /* Move memory model to the 3rd position, and end param list. */
10706 (*params)[2] = p3;
10707 params->truncate (3);
10709 /* Convert return pointer and dereference it for later assignment. */
10710 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10712 return false;
10716 /* This will process an __atomic_compare_exchange function call, determine
10717 whether it needs to be mapped to the _N variation, or turned into a lib call.
10718 LOC is the location of the builtin call.
10719 FUNCTION is the DECL that has been invoked;
10720 PARAMS is the argument list for the call. The return value is non-null
10721 TRUE is returned if it is translated into the proper format for a call to the
10722 external library, and NEW_RETURN is set the tree for that function.
10723 FALSE is returned if processing for the _N variation is required. */
10725 static bool
10726 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10727 vec<tree, va_gc> *params,
10728 tree *new_return)
10730 tree p0, p1, p2;
10731 tree I_type, I_type_ptr;
10732 int n = get_atomic_generic_size (loc, function, params);
10734 /* Size of 0 is an error condition. */
10735 if (n == 0)
10737 *new_return = error_mark_node;
10738 return true;
10741 /* If not a lock-free size, change to the library generic format. */
10742 if (!atomic_size_supported_p (n))
10744 /* The library generic format does not have the weak parameter, so
10745 remove it from the param list. Since a parameter has been removed,
10746 we can be sure that there is room for the SIZE_T parameter, meaning
10747 there will not be a recursive rebuilding of the parameter list, so
10748 there is no danger this will be done twice. */
10749 if (n > 0)
10751 (*params)[3] = (*params)[4];
10752 (*params)[4] = (*params)[5];
10753 params->truncate (5);
10755 *new_return = add_atomic_size_parameter (n, loc, function, params);
10756 return true;
10759 /* Otherwise, there is a match, so the call needs to be transformed from:
10760 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10761 into
10762 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10764 p0 = (*params)[0];
10765 p1 = (*params)[1];
10766 p2 = (*params)[2];
10768 /* Create pointer to appropriate size. */
10769 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10770 I_type_ptr = build_pointer_type (I_type);
10772 /* Convert object pointer to required type. */
10773 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10774 (*params)[0] = p0;
10776 /* Convert expected pointer to required type. */
10777 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10778 (*params)[1] = p1;
10780 /* Convert desired value to required type, and dereference it. */
10781 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10782 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10783 (*params)[2] = p2;
10785 /* The rest of the parameters are fine. NULL means no special return value
10786 processing.*/
10787 *new_return = NULL;
10788 return false;
10792 /* This will process an __atomic_load function call, determine whether it
10793 needs to be mapped to the _N variation, or turned into a library call.
10794 LOC is the location of the builtin call.
10795 FUNCTION is the DECL that has been invoked;
10796 PARAMS is the argument list for the call. The return value is non-null
10797 TRUE is returned if it is translated into the proper format for a call to the
10798 external library, and NEW_RETURN is set the tree for that function.
10799 FALSE is returned if processing for the _N variation is required, and
10800 NEW_RETURN is set to the the return value the result is copied into. */
10802 static bool
10803 resolve_overloaded_atomic_load (location_t loc, tree function,
10804 vec<tree, va_gc> *params, tree *new_return)
10806 tree p0, p1, p2;
10807 tree I_type, I_type_ptr;
10808 int n = get_atomic_generic_size (loc, function, params);
10810 /* Size of 0 is an error condition. */
10811 if (n == 0)
10813 *new_return = error_mark_node;
10814 return true;
10817 /* If not a lock-free size, change to the library generic format. */
10818 if (!atomic_size_supported_p (n))
10820 *new_return = add_atomic_size_parameter (n, loc, function, params);
10821 return true;
10824 /* Otherwise, there is a match, so the call needs to be transformed from:
10825 void fn(T* mem, T* return, model)
10826 into
10827 *return = (T) (fn ((In *) mem, model)) */
10829 p0 = (*params)[0];
10830 p1 = (*params)[1];
10831 p2 = (*params)[2];
10833 /* Create pointer to appropriate size. */
10834 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10835 I_type_ptr = build_pointer_type (I_type);
10837 /* Convert object pointer to required type. */
10838 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10839 (*params)[0] = p0;
10841 /* Move memory model to the 2nd position, and end param list. */
10842 (*params)[1] = p2;
10843 params->truncate (2);
10845 /* Convert return pointer and dereference it for later assignment. */
10846 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10848 return false;
10852 /* This will process an __atomic_store function call, determine whether it
10853 needs to be mapped to the _N variation, or turned into a library call.
10854 LOC is the location of the builtin call.
10855 FUNCTION is the DECL that has been invoked;
10856 PARAMS is the argument list for the call. The return value is non-null
10857 TRUE is returned if it is translated into the proper format for a call to the
10858 external library, and NEW_RETURN is set the tree for that function.
10859 FALSE is returned if processing for the _N variation is required, and
10860 NEW_RETURN is set to the the return value the result is copied into. */
10862 static bool
10863 resolve_overloaded_atomic_store (location_t loc, tree function,
10864 vec<tree, va_gc> *params, tree *new_return)
10866 tree p0, p1;
10867 tree I_type, I_type_ptr;
10868 int n = get_atomic_generic_size (loc, function, params);
10870 /* Size of 0 is an error condition. */
10871 if (n == 0)
10873 *new_return = error_mark_node;
10874 return true;
10877 /* If not a lock-free size, change to the library generic format. */
10878 if (!atomic_size_supported_p (n))
10880 *new_return = add_atomic_size_parameter (n, loc, function, params);
10881 return true;
10884 /* Otherwise, there is a match, so the call needs to be transformed from:
10885 void fn(T* mem, T* value, model)
10886 into
10887 fn ((In *) mem, (In) *value, model) */
10889 p0 = (*params)[0];
10890 p1 = (*params)[1];
10892 /* Create pointer to appropriate size. */
10893 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10894 I_type_ptr = build_pointer_type (I_type);
10896 /* Convert object pointer to required type. */
10897 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10898 (*params)[0] = p0;
10900 /* Convert new value to required type, and dereference it. */
10901 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10902 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10903 (*params)[1] = p1;
10905 /* The memory model is in the right spot already. Return is void. */
10906 *new_return = NULL_TREE;
10908 return false;
10912 /* Some builtin functions are placeholders for other expressions. This
10913 function should be called immediately after parsing the call expression
10914 before surrounding code has committed to the type of the expression.
10916 LOC is the location of the builtin call.
10918 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10919 PARAMS is the argument list for the call. The return value is non-null
10920 when expansion is complete, and null if normal processing should
10921 continue. */
10923 tree
10924 resolve_overloaded_builtin (location_t loc, tree function,
10925 vec<tree, va_gc> *params)
10927 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10928 bool orig_format = true;
10929 tree new_return = NULL_TREE;
10931 switch (DECL_BUILT_IN_CLASS (function))
10933 case BUILT_IN_NORMAL:
10934 break;
10935 case BUILT_IN_MD:
10936 if (targetm.resolve_overloaded_builtin)
10937 return targetm.resolve_overloaded_builtin (loc, function, params);
10938 else
10939 return NULL_TREE;
10940 default:
10941 return NULL_TREE;
10944 /* Handle BUILT_IN_NORMAL here. */
10945 switch (orig_code)
10947 case BUILT_IN_ATOMIC_EXCHANGE:
10948 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10949 case BUILT_IN_ATOMIC_LOAD:
10950 case BUILT_IN_ATOMIC_STORE:
10952 /* Handle these 4 together so that they can fall through to the next
10953 case if the call is transformed to an _N variant. */
10954 switch (orig_code)
10956 case BUILT_IN_ATOMIC_EXCHANGE:
10958 if (resolve_overloaded_atomic_exchange (loc, function, params,
10959 &new_return))
10960 return new_return;
10961 /* Change to the _N variant. */
10962 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10963 break;
10966 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10968 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10969 params,
10970 &new_return))
10971 return new_return;
10972 /* Change to the _N variant. */
10973 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10974 break;
10976 case BUILT_IN_ATOMIC_LOAD:
10978 if (resolve_overloaded_atomic_load (loc, function, params,
10979 &new_return))
10980 return new_return;
10981 /* Change to the _N variant. */
10982 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10983 break;
10985 case BUILT_IN_ATOMIC_STORE:
10987 if (resolve_overloaded_atomic_store (loc, function, params,
10988 &new_return))
10989 return new_return;
10990 /* Change to the _N variant. */
10991 orig_code = BUILT_IN_ATOMIC_STORE_N;
10992 break;
10994 default:
10995 gcc_unreachable ();
10997 /* Fallthrough to the normal processing. */
10999 case BUILT_IN_ATOMIC_EXCHANGE_N:
11000 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11001 case BUILT_IN_ATOMIC_LOAD_N:
11002 case BUILT_IN_ATOMIC_STORE_N:
11003 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11004 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11005 case BUILT_IN_ATOMIC_AND_FETCH_N:
11006 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11007 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11008 case BUILT_IN_ATOMIC_OR_FETCH_N:
11009 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11010 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11011 case BUILT_IN_ATOMIC_FETCH_AND_N:
11012 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11013 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11014 case BUILT_IN_ATOMIC_FETCH_OR_N:
11016 orig_format = false;
11017 /* Fallthru for parameter processing. */
11019 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11020 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11021 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11022 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11023 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11024 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11025 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11026 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11027 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11028 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11029 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11030 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11031 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11032 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11033 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11034 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11036 int n = sync_resolve_size (function, params);
11037 tree new_function, first_param, result;
11038 enum built_in_function fncode;
11040 if (n == 0)
11041 return error_mark_node;
11043 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11044 new_function = builtin_decl_explicit (fncode);
11045 if (!sync_resolve_params (loc, function, new_function, params,
11046 orig_format))
11047 return error_mark_node;
11049 first_param = (*params)[0];
11050 result = build_function_call_vec (loc, vNULL, new_function, params,
11051 NULL);
11052 if (result == error_mark_node)
11053 return result;
11054 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11055 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11056 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11057 result = sync_resolve_return (first_param, result, orig_format);
11059 /* If new_return is set, assign function to that expr and cast the
11060 result to void since the generic interface returned void. */
11061 if (new_return)
11063 /* Cast function result from I{1,2,4,8,16} to the required type. */
11064 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11065 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11066 result);
11067 TREE_SIDE_EFFECTS (result) = 1;
11068 protected_set_expr_location (result, loc);
11069 result = convert (void_type_node, result);
11071 return result;
11074 default:
11075 return NULL_TREE;
11079 /* vector_types_compatible_elements_p is used in type checks of vectors
11080 values used as operands of binary operators. Where it returns true, and
11081 the other checks of the caller succeed (being vector types in he first
11082 place, and matching number of elements), we can just treat the types
11083 as essentially the same.
11084 Contrast with vector_targets_convertible_p, which is used for vector
11085 pointer types, and vector_types_convertible_p, which will allow
11086 language-specific matches under the control of flag_lax_vector_conversions,
11087 and might still require a conversion. */
11088 /* True if vector types T1 and T2 can be inputs to the same binary
11089 operator without conversion.
11090 We don't check the overall vector size here because some of our callers
11091 want to give different error messages when the vectors are compatible
11092 except for the element count. */
11094 bool
11095 vector_types_compatible_elements_p (tree t1, tree t2)
11097 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11098 t1 = TREE_TYPE (t1);
11099 t2 = TREE_TYPE (t2);
11101 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11103 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11104 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11105 || c2 == FIXED_POINT_TYPE));
11107 t1 = c_common_signed_type (t1);
11108 t2 = c_common_signed_type (t2);
11109 /* Equality works here because c_common_signed_type uses
11110 TYPE_MAIN_VARIANT. */
11111 if (t1 == t2)
11112 return true;
11113 if (opaque && c1 == c2
11114 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11115 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11116 return true;
11117 return false;
11120 /* Check for missing format attributes on function pointers. LTYPE is
11121 the new type or left-hand side type. RTYPE is the old type or
11122 right-hand side type. Returns TRUE if LTYPE is missing the desired
11123 attribute. */
11125 bool
11126 check_missing_format_attribute (tree ltype, tree rtype)
11128 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11129 tree ra;
11131 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11132 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11133 break;
11134 if (ra)
11136 tree la;
11137 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11138 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11139 break;
11140 return !la;
11142 else
11143 return false;
11146 /* Subscripting with type char is likely to lose on a machine where
11147 chars are signed. So warn on any machine, but optionally. Don't
11148 warn for unsigned char since that type is safe. Don't warn for
11149 signed char because anyone who uses that must have done so
11150 deliberately. Furthermore, we reduce the false positive load by
11151 warning only for non-constant value of type char. */
11153 void
11154 warn_array_subscript_with_type_char (tree index)
11156 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11157 && TREE_CODE (index) != INTEGER_CST)
11158 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11161 /* Implement -Wparentheses for the unexpected C precedence rules, to
11162 cover cases like x + y << z which readers are likely to
11163 misinterpret. We have seen an expression in which CODE is a binary
11164 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11165 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11166 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11167 expression was not formed using a binary or unary operator, or it
11168 was enclosed in parentheses. */
11170 void
11171 warn_about_parentheses (location_t loc, enum tree_code code,
11172 enum tree_code code_left, tree arg_left,
11173 enum tree_code code_right, tree arg_right)
11175 if (!warn_parentheses)
11176 return;
11178 /* This macro tests that the expression ARG with original tree code
11179 CODE appears to be a boolean expression. or the result of folding a
11180 boolean expression. */
11181 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11182 (truth_value_p (TREE_CODE (ARG)) \
11183 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11184 /* Folding may create 0 or 1 integers from other expressions. */ \
11185 || ((CODE) != INTEGER_CST \
11186 && (integer_onep (ARG) || integer_zerop (ARG))))
11188 switch (code)
11190 case LSHIFT_EXPR:
11191 if (code_left == PLUS_EXPR)
11192 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11193 "suggest parentheses around %<+%> inside %<<<%>");
11194 else if (code_right == PLUS_EXPR)
11195 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11196 "suggest parentheses around %<+%> inside %<<<%>");
11197 else if (code_left == MINUS_EXPR)
11198 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11199 "suggest parentheses around %<-%> inside %<<<%>");
11200 else if (code_right == MINUS_EXPR)
11201 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11202 "suggest parentheses around %<-%> inside %<<<%>");
11203 return;
11205 case RSHIFT_EXPR:
11206 if (code_left == PLUS_EXPR)
11207 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11208 "suggest parentheses around %<+%> inside %<>>%>");
11209 else if (code_right == PLUS_EXPR)
11210 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11211 "suggest parentheses around %<+%> inside %<>>%>");
11212 else if (code_left == MINUS_EXPR)
11213 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11214 "suggest parentheses around %<-%> inside %<>>%>");
11215 else if (code_right == MINUS_EXPR)
11216 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11217 "suggest parentheses around %<-%> inside %<>>%>");
11218 return;
11220 case TRUTH_ORIF_EXPR:
11221 if (code_left == TRUTH_ANDIF_EXPR)
11222 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11223 "suggest parentheses around %<&&%> within %<||%>");
11224 else if (code_right == TRUTH_ANDIF_EXPR)
11225 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11226 "suggest parentheses around %<&&%> within %<||%>");
11227 return;
11229 case BIT_IOR_EXPR:
11230 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11231 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11232 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11233 "suggest parentheses around arithmetic in operand of %<|%>");
11234 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11235 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11236 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11237 "suggest parentheses around arithmetic in operand of %<|%>");
11238 /* Check cases like x|y==z */
11239 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11240 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11241 "suggest parentheses around comparison in operand of %<|%>");
11242 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11243 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11244 "suggest parentheses around comparison in operand of %<|%>");
11245 /* Check cases like !x | y */
11246 else if (code_left == TRUTH_NOT_EXPR
11247 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11248 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11249 "suggest parentheses around operand of "
11250 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11251 return;
11253 case BIT_XOR_EXPR:
11254 if (code_left == BIT_AND_EXPR
11255 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11256 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11257 "suggest parentheses around arithmetic in operand of %<^%>");
11258 else if (code_right == BIT_AND_EXPR
11259 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11260 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11261 "suggest parentheses around arithmetic in operand of %<^%>");
11262 /* Check cases like x^y==z */
11263 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11264 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11265 "suggest parentheses around comparison in operand of %<^%>");
11266 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11267 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11268 "suggest parentheses around comparison in operand of %<^%>");
11269 return;
11271 case BIT_AND_EXPR:
11272 if (code_left == PLUS_EXPR)
11273 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11274 "suggest parentheses around %<+%> in operand of %<&%>");
11275 else if (code_right == PLUS_EXPR)
11276 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11277 "suggest parentheses around %<+%> in operand of %<&%>");
11278 else if (code_left == MINUS_EXPR)
11279 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11280 "suggest parentheses around %<-%> in operand of %<&%>");
11281 else if (code_right == MINUS_EXPR)
11282 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11283 "suggest parentheses around %<-%> in operand of %<&%>");
11284 /* Check cases like x&y==z */
11285 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11286 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11287 "suggest parentheses around comparison in operand of %<&%>");
11288 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11289 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11290 "suggest parentheses around comparison in operand of %<&%>");
11291 /* Check cases like !x & y */
11292 else if (code_left == TRUTH_NOT_EXPR
11293 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11294 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11295 "suggest parentheses around operand of "
11296 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11297 return;
11299 case EQ_EXPR:
11300 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11301 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11302 "suggest parentheses around comparison in operand of %<==%>");
11303 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11304 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11305 "suggest parentheses around comparison in operand of %<==%>");
11306 return;
11307 case NE_EXPR:
11308 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11309 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11310 "suggest parentheses around comparison in operand of %<!=%>");
11311 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11312 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11313 "suggest parentheses around comparison in operand of %<!=%>");
11314 return;
11316 default:
11317 if (TREE_CODE_CLASS (code) == tcc_comparison)
11319 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11320 && code_left != NE_EXPR && code_left != EQ_EXPR
11321 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11322 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11323 "comparisons like %<X<=Y<=Z%> do not "
11324 "have their mathematical meaning");
11325 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11326 && code_right != NE_EXPR && code_right != EQ_EXPR
11327 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11328 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11329 "comparisons like %<X<=Y<=Z%> do not "
11330 "have their mathematical meaning");
11332 return;
11334 #undef NOT_A_BOOLEAN_EXPR_P
11337 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11339 void
11340 warn_for_unused_label (tree label)
11342 if (!TREE_USED (label))
11344 if (DECL_INITIAL (label))
11345 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11346 else
11347 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11351 /* Warn for division by zero according to the value of DIVISOR. LOC
11352 is the location of the division operator. */
11354 void
11355 warn_for_div_by_zero (location_t loc, tree divisor)
11357 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11358 about division by zero. Do not issue a warning if DIVISOR has a
11359 floating-point type, since we consider 0.0/0.0 a valid way of
11360 generating a NaN. */
11361 if (c_inhibit_evaluation_warnings == 0
11362 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11363 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11366 /* Subroutine of build_binary_op. Give warnings for comparisons
11367 between signed and unsigned quantities that may fail. Do the
11368 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11369 so that casts will be considered, but default promotions won't
11372 LOCATION is the location of the comparison operator.
11374 The arguments of this function map directly to local variables
11375 of build_binary_op. */
11377 void
11378 warn_for_sign_compare (location_t location,
11379 tree orig_op0, tree orig_op1,
11380 tree op0, tree op1,
11381 tree result_type, enum tree_code resultcode)
11383 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11384 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11385 int unsignedp0, unsignedp1;
11387 /* In C++, check for comparison of different enum types. */
11388 if (c_dialect_cxx()
11389 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11390 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11391 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11392 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11394 warning_at (location,
11395 OPT_Wsign_compare, "comparison between types %qT and %qT",
11396 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11399 /* Do not warn if the comparison is being done in a signed type,
11400 since the signed type will only be chosen if it can represent
11401 all the values of the unsigned type. */
11402 if (!TYPE_UNSIGNED (result_type))
11403 /* OK */;
11404 /* Do not warn if both operands are unsigned. */
11405 else if (op0_signed == op1_signed)
11406 /* OK */;
11407 else
11409 tree sop, uop, base_type;
11410 bool ovf;
11412 if (op0_signed)
11413 sop = orig_op0, uop = orig_op1;
11414 else
11415 sop = orig_op1, uop = orig_op0;
11417 STRIP_TYPE_NOPS (sop);
11418 STRIP_TYPE_NOPS (uop);
11419 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11420 ? TREE_TYPE (result_type) : result_type);
11422 /* Do not warn if the signed quantity is an unsuffixed integer
11423 literal (or some static constant expression involving such
11424 literals or a conditional expression involving such literals)
11425 and it is non-negative. */
11426 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11427 /* OK */;
11428 /* Do not warn if the comparison is an equality operation, the
11429 unsigned quantity is an integral constant, and it would fit
11430 in the result if the result were signed. */
11431 else if (TREE_CODE (uop) == INTEGER_CST
11432 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11433 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11434 /* OK */;
11435 /* In C, do not warn if the unsigned quantity is an enumeration
11436 constant and its maximum value would fit in the result if the
11437 result were signed. */
11438 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11439 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11440 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11441 c_common_signed_type (base_type)))
11442 /* OK */;
11443 else
11444 warning_at (location,
11445 OPT_Wsign_compare,
11446 "comparison between signed and unsigned integer expressions");
11449 /* Warn if two unsigned values are being compared in a size larger
11450 than their original size, and one (and only one) is the result of
11451 a `~' operator. This comparison will always fail.
11453 Also warn if one operand is a constant, and the constant does not
11454 have all bits set that are set in the ~ operand when it is
11455 extended. */
11457 op0 = c_common_get_narrower (op0, &unsignedp0);
11458 op1 = c_common_get_narrower (op1, &unsignedp1);
11460 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11461 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11463 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11464 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11465 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11466 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11468 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11470 tree primop;
11471 HOST_WIDE_INT constant, mask;
11472 int unsignedp;
11473 unsigned int bits;
11475 if (tree_fits_shwi_p (op0))
11477 primop = op1;
11478 unsignedp = unsignedp1;
11479 constant = tree_to_shwi (op0);
11481 else
11483 primop = op0;
11484 unsignedp = unsignedp0;
11485 constant = tree_to_shwi (op1);
11488 bits = TYPE_PRECISION (TREE_TYPE (primop));
11489 if (bits < TYPE_PRECISION (result_type)
11490 && bits < HOST_BITS_PER_LONG && unsignedp)
11492 mask = (~ (HOST_WIDE_INT) 0) << bits;
11493 if ((mask & constant) != mask)
11495 if (constant == 0)
11496 warning_at (location, OPT_Wsign_compare,
11497 "promoted ~unsigned is always non-zero");
11498 else
11499 warning_at (location, OPT_Wsign_compare,
11500 "comparison of promoted ~unsigned with constant");
11504 else if (unsignedp0 && unsignedp1
11505 && (TYPE_PRECISION (TREE_TYPE (op0))
11506 < TYPE_PRECISION (result_type))
11507 && (TYPE_PRECISION (TREE_TYPE (op1))
11508 < TYPE_PRECISION (result_type)))
11509 warning_at (location, OPT_Wsign_compare,
11510 "comparison of promoted ~unsigned with unsigned");
11514 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11515 type via c_common_type. If -Wdouble-promotion is in use, and the
11516 conditions for warning have been met, issue a warning. GMSGID is
11517 the warning message. It must have two %T specifiers for the type
11518 that was converted (generally "float") and the type to which it was
11519 converted (generally "double), respectively. LOC is the location
11520 to which the awrning should refer. */
11522 void
11523 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11524 const char *gmsgid, location_t loc)
11526 tree source_type;
11528 if (!warn_double_promotion)
11529 return;
11530 /* If the conversion will not occur at run-time, there is no need to
11531 warn about it. */
11532 if (c_inhibit_evaluation_warnings)
11533 return;
11534 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11535 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11536 return;
11537 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11538 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11539 source_type = type1;
11540 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11541 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11542 source_type = type2;
11543 else
11544 return;
11545 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11548 /* Setup a TYPE_DECL node as a typedef representation.
11550 X is a TYPE_DECL for a typedef statement. Create a brand new
11551 ..._TYPE node (which will be just a variant of the existing
11552 ..._TYPE node with identical properties) and then install X
11553 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11555 The whole point here is to end up with a situation where each
11556 and every ..._TYPE node the compiler creates will be uniquely
11557 associated with AT MOST one node representing a typedef name.
11558 This way, even though the compiler substitutes corresponding
11559 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11560 early on, later parts of the compiler can always do the reverse
11561 translation and get back the corresponding typedef name. For
11562 example, given:
11564 typedef struct S MY_TYPE;
11565 MY_TYPE object;
11567 Later parts of the compiler might only know that `object' was of
11568 type `struct S' if it were not for code just below. With this
11569 code however, later parts of the compiler see something like:
11571 struct S' == struct S
11572 typedef struct S' MY_TYPE;
11573 struct S' object;
11575 And they can then deduce (from the node for type struct S') that
11576 the original object declaration was:
11578 MY_TYPE object;
11580 Being able to do this is important for proper support of protoize,
11581 and also for generating precise symbolic debugging information
11582 which takes full account of the programmer's (typedef) vocabulary.
11584 Obviously, we don't want to generate a duplicate ..._TYPE node if
11585 the TYPE_DECL node that we are now processing really represents a
11586 standard built-in type. */
11588 void
11589 set_underlying_type (tree x)
11591 if (x == error_mark_node)
11592 return;
11593 if (DECL_IS_BUILTIN (x))
11595 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11596 TYPE_NAME (TREE_TYPE (x)) = x;
11598 else if (TREE_TYPE (x) != error_mark_node
11599 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11601 tree tt = TREE_TYPE (x);
11602 DECL_ORIGINAL_TYPE (x) = tt;
11603 tt = build_variant_type_copy (tt);
11604 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11605 TYPE_NAME (tt) = x;
11606 TREE_USED (tt) = TREE_USED (x);
11607 TREE_TYPE (x) = tt;
11611 /* Record the types used by the current global variable declaration
11612 being parsed, so that we can decide later to emit their debug info.
11613 Those types are in types_used_by_cur_var_decl, and we are going to
11614 store them in the types_used_by_vars_hash hash table.
11615 DECL is the declaration of the global variable that has been parsed. */
11617 void
11618 record_types_used_by_current_var_decl (tree decl)
11620 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11622 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11624 tree type = types_used_by_cur_var_decl->pop ();
11625 types_used_by_var_decl_insert (type, decl);
11629 /* If DECL is a typedef that is declared in the current function,
11630 record it for the purpose of -Wunused-local-typedefs. */
11632 void
11633 record_locally_defined_typedef (tree decl)
11635 struct c_language_function *l;
11637 if (!warn_unused_local_typedefs
11638 || cfun == NULL
11639 /* if this is not a locally defined typedef then we are not
11640 interested. */
11641 || !is_typedef_decl (decl)
11642 || !decl_function_context (decl))
11643 return;
11645 l = (struct c_language_function *) cfun->language;
11646 vec_safe_push (l->local_typedefs, decl);
11649 /* If T is a TYPE_DECL declared locally, mark it as used. */
11651 void
11652 maybe_record_typedef_use (tree t)
11654 if (!is_typedef_decl (t))
11655 return;
11657 TREE_USED (t) = true;
11660 /* Warn if there are some unused locally defined typedefs in the
11661 current function. */
11663 void
11664 maybe_warn_unused_local_typedefs (void)
11666 int i;
11667 tree decl;
11668 /* The number of times we have emitted -Wunused-local-typedefs
11669 warnings. If this is different from errorcount, that means some
11670 unrelated errors have been issued. In which case, we'll avoid
11671 emitting "unused-local-typedefs" warnings. */
11672 static int unused_local_typedefs_warn_count;
11673 struct c_language_function *l;
11675 if (cfun == NULL)
11676 return;
11678 if ((l = (struct c_language_function *) cfun->language) == NULL)
11679 return;
11681 if (warn_unused_local_typedefs
11682 && errorcount == unused_local_typedefs_warn_count)
11684 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11685 if (!TREE_USED (decl))
11686 warning_at (DECL_SOURCE_LOCATION (decl),
11687 OPT_Wunused_local_typedefs,
11688 "typedef %qD locally defined but not used", decl);
11689 unused_local_typedefs_warn_count = errorcount;
11692 vec_free (l->local_typedefs);
11695 /* Warn about boolean expression compared with an integer value different
11696 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11697 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11698 are the operands of the comparison. The caller must ensure that
11699 either operand is a boolean expression. */
11701 void
11702 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11703 tree op1)
11705 if (TREE_CODE_CLASS (code) != tcc_comparison)
11706 return;
11708 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11709 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11710 if (!cst)
11711 return;
11713 if (!integer_zerop (cst) && !integer_onep (cst))
11715 int sign = (TREE_CODE (op0) == INTEGER_CST)
11716 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11717 if (code == EQ_EXPR
11718 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11719 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11720 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11721 "with boolean expression is always false", cst);
11722 else
11723 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11724 "with boolean expression is always true", cst);
11728 /* The C and C++ parsers both use vectors to hold function arguments.
11729 For efficiency, we keep a cache of unused vectors. This is the
11730 cache. */
11732 typedef vec<tree, va_gc> *tree_gc_vec;
11733 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11735 /* Return a new vector from the cache. If the cache is empty,
11736 allocate a new vector. These vectors are GC'ed, so it is OK if the
11737 pointer is not released.. */
11739 vec<tree, va_gc> *
11740 make_tree_vector (void)
11742 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11743 return tree_vector_cache->pop ();
11744 else
11746 /* Passing 0 to vec::alloc returns NULL, and our callers require
11747 that we always return a non-NULL value. The vector code uses
11748 4 when growing a NULL vector, so we do too. */
11749 vec<tree, va_gc> *v;
11750 vec_alloc (v, 4);
11751 return v;
11755 /* Release a vector of trees back to the cache. */
11757 void
11758 release_tree_vector (vec<tree, va_gc> *vec)
11760 if (vec != NULL)
11762 vec->truncate (0);
11763 vec_safe_push (tree_vector_cache, vec);
11767 /* Get a new tree vector holding a single tree. */
11769 vec<tree, va_gc> *
11770 make_tree_vector_single (tree t)
11772 vec<tree, va_gc> *ret = make_tree_vector ();
11773 ret->quick_push (t);
11774 return ret;
11777 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11779 vec<tree, va_gc> *
11780 make_tree_vector_from_list (tree list)
11782 vec<tree, va_gc> *ret = make_tree_vector ();
11783 for (; list; list = TREE_CHAIN (list))
11784 vec_safe_push (ret, TREE_VALUE (list));
11785 return ret;
11788 /* Get a new tree vector which is a copy of an existing one. */
11790 vec<tree, va_gc> *
11791 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11793 vec<tree, va_gc> *ret;
11794 unsigned int ix;
11795 tree t;
11797 ret = make_tree_vector ();
11798 vec_safe_reserve (ret, vec_safe_length (orig));
11799 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11800 ret->quick_push (t);
11801 return ret;
11804 /* Return true if KEYWORD starts a type specifier. */
11806 bool
11807 keyword_begins_type_specifier (enum rid keyword)
11809 switch (keyword)
11811 case RID_AUTO_TYPE:
11812 case RID_INT:
11813 case RID_CHAR:
11814 case RID_FLOAT:
11815 case RID_DOUBLE:
11816 case RID_VOID:
11817 case RID_UNSIGNED:
11818 case RID_LONG:
11819 case RID_SHORT:
11820 case RID_SIGNED:
11821 case RID_DFLOAT32:
11822 case RID_DFLOAT64:
11823 case RID_DFLOAT128:
11824 case RID_FRACT:
11825 case RID_ACCUM:
11826 case RID_BOOL:
11827 case RID_WCHAR:
11828 case RID_CHAR16:
11829 case RID_CHAR32:
11830 case RID_SAT:
11831 case RID_COMPLEX:
11832 case RID_TYPEOF:
11833 case RID_STRUCT:
11834 case RID_CLASS:
11835 case RID_UNION:
11836 case RID_ENUM:
11837 return true;
11838 default:
11839 if (keyword >= RID_FIRST_INT_N
11840 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
11841 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
11842 return true;
11843 return false;
11847 /* Return true if KEYWORD names a type qualifier. */
11849 bool
11850 keyword_is_type_qualifier (enum rid keyword)
11852 switch (keyword)
11854 case RID_CONST:
11855 case RID_VOLATILE:
11856 case RID_RESTRICT:
11857 case RID_ATOMIC:
11858 return true;
11859 default:
11860 return false;
11864 /* Return true if KEYWORD names a storage class specifier.
11866 RID_TYPEDEF is not included in this list despite `typedef' being
11867 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11868 such for syntactic convenience only. */
11870 bool
11871 keyword_is_storage_class_specifier (enum rid keyword)
11873 switch (keyword)
11875 case RID_STATIC:
11876 case RID_EXTERN:
11877 case RID_REGISTER:
11878 case RID_AUTO:
11879 case RID_MUTABLE:
11880 case RID_THREAD:
11881 return true;
11882 default:
11883 return false;
11887 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11889 static bool
11890 keyword_is_function_specifier (enum rid keyword)
11892 switch (keyword)
11894 case RID_INLINE:
11895 case RID_NORETURN:
11896 case RID_VIRTUAL:
11897 case RID_EXPLICIT:
11898 return true;
11899 default:
11900 return false;
11904 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11905 declaration-specifier (C99 6.7). */
11907 bool
11908 keyword_is_decl_specifier (enum rid keyword)
11910 if (keyword_is_storage_class_specifier (keyword)
11911 || keyword_is_type_qualifier (keyword)
11912 || keyword_is_function_specifier (keyword))
11913 return true;
11915 switch (keyword)
11917 case RID_TYPEDEF:
11918 case RID_FRIEND:
11919 case RID_CONSTEXPR:
11920 return true;
11921 default:
11922 return false;
11926 /* Initialize language-specific-bits of tree_contains_struct. */
11928 void
11929 c_common_init_ts (void)
11931 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11932 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11933 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11936 /* Build a user-defined numeric literal out of an integer constant type VALUE
11937 with identifier SUFFIX. */
11939 tree
11940 build_userdef_literal (tree suffix_id, tree value,
11941 enum overflow_type overflow, tree num_string)
11943 tree literal = make_node (USERDEF_LITERAL);
11944 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11945 USERDEF_LITERAL_VALUE (literal) = value;
11946 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11947 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11948 return literal;
11951 /* For vector[index], convert the vector to a
11952 pointer of the underlying type. */
11953 void
11954 convert_vector_to_pointer_for_subscript (location_t loc,
11955 tree* vecp, tree index)
11957 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11959 tree type = TREE_TYPE (*vecp);
11960 tree type1;
11962 if (TREE_CODE (index) == INTEGER_CST)
11963 if (!tree_fits_uhwi_p (index)
11964 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
11965 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11967 c_common_mark_addressable_vec (*vecp);
11968 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11969 type1 = build_pointer_type (TREE_TYPE (*vecp));
11970 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
11971 if (!ref_all
11972 && !DECL_P (*vecp))
11974 /* If the original vector isn't declared may_alias and it
11975 isn't a bare vector look if the subscripting would
11976 alias the vector we subscript, and if not, force ref-all. */
11977 alias_set_type vecset = get_alias_set (*vecp);
11978 alias_set_type sset = get_alias_set (type);
11979 if (!alias_sets_must_conflict_p (sset, vecset)
11980 && !alias_set_subset_of (sset, vecset))
11981 ref_all = true;
11983 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
11984 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11985 *vecp = convert (type, *vecp);
11989 /* Determine which of the operands, if any, is a scalar that needs to be
11990 converted to a vector, for the range of operations. */
11991 enum stv_conv
11992 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11993 bool complain)
11995 tree type0 = TREE_TYPE (op0);
11996 tree type1 = TREE_TYPE (op1);
11997 bool integer_only_op = false;
11998 enum stv_conv ret = stv_firstarg;
12000 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12001 || TREE_CODE (type1) == VECTOR_TYPE);
12002 switch (code)
12004 /* Most GENERIC binary expressions require homogeneous arguments.
12005 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12006 argument that is a vector and a second one that is a scalar, so
12007 we never return stv_secondarg for them. */
12008 case RSHIFT_EXPR:
12009 case LSHIFT_EXPR:
12010 if (TREE_CODE (type0) == INTEGER_TYPE
12011 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12013 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12015 if (complain)
12016 error_at (loc, "conversion of scalar %qT to vector %qT "
12017 "involves truncation", type0, type1);
12018 return stv_error;
12020 else
12021 return stv_firstarg;
12023 break;
12025 case BIT_IOR_EXPR:
12026 case BIT_XOR_EXPR:
12027 case BIT_AND_EXPR:
12028 integer_only_op = true;
12029 /* ... fall through ... */
12031 case VEC_COND_EXPR:
12033 case PLUS_EXPR:
12034 case MINUS_EXPR:
12035 case MULT_EXPR:
12036 case TRUNC_DIV_EXPR:
12037 case CEIL_DIV_EXPR:
12038 case FLOOR_DIV_EXPR:
12039 case ROUND_DIV_EXPR:
12040 case EXACT_DIV_EXPR:
12041 case TRUNC_MOD_EXPR:
12042 case FLOOR_MOD_EXPR:
12043 case RDIV_EXPR:
12044 case EQ_EXPR:
12045 case NE_EXPR:
12046 case LE_EXPR:
12047 case GE_EXPR:
12048 case LT_EXPR:
12049 case GT_EXPR:
12050 /* What about UNLT_EXPR? */
12051 if (TREE_CODE (type0) == VECTOR_TYPE)
12053 tree tmp;
12054 ret = stv_secondarg;
12055 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12056 tmp = type0; type0 = type1; type1 = tmp;
12057 tmp = op0; op0 = op1; op1 = tmp;
12060 if (TREE_CODE (type0) == INTEGER_TYPE
12061 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12063 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12065 if (complain)
12066 error_at (loc, "conversion of scalar %qT to vector %qT "
12067 "involves truncation", type0, type1);
12068 return stv_error;
12070 return ret;
12072 else if (!integer_only_op
12073 /* Allow integer --> real conversion if safe. */
12074 && (TREE_CODE (type0) == REAL_TYPE
12075 || TREE_CODE (type0) == INTEGER_TYPE)
12076 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12078 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12080 if (complain)
12081 error_at (loc, "conversion of scalar %qT to vector %qT "
12082 "involves truncation", type0, type1);
12083 return stv_error;
12085 return ret;
12087 default:
12088 break;
12091 return stv_nothing;
12094 /* Return true iff ALIGN is an integral constant that is a fundamental
12095 alignment, as defined by [basic.align] in the c++-11
12096 specifications.
12098 That is:
12100 [A fundamental alignment is represented by an alignment less than or
12101 equal to the greatest alignment supported by the implementation
12102 in all contexts, which is equal to
12103 alignof(max_align_t)]. */
12105 bool
12106 cxx_fundamental_alignment_p (unsigned align)
12108 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12109 TYPE_ALIGN (long_double_type_node)));
12112 /* Return true if T is a pointer to a zero-sized aggregate. */
12114 bool
12115 pointer_to_zero_sized_aggr_p (tree t)
12117 if (!POINTER_TYPE_P (t))
12118 return false;
12119 t = TREE_TYPE (t);
12120 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12123 #include "gt-c-family-c-common.h"