Merge from trunk
[official-gcc.git] / gcc / c-family / c-common.c
blob12f7de36c4e922e6a72a53f49dbb625038a54af1
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 #define GCC_C_COMMON_C
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "input.h"
26 #include "c-common.h"
27 #include "tm.h"
28 #include "intl.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "calls.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "varasm.h"
36 #include "trans-mem.h"
37 #include "flags.h"
38 #include "c-pragma.h"
39 #include "c-objc.h"
40 #include "tm_p.h"
41 #include "obstack.h"
42 #include "cpplib.h"
43 #include "target.h"
44 #include "common/common-target.h"
45 #include "langhooks.h"
46 #include "tree-inline.h"
47 #include "toplev.h"
48 #include "diagnostic.h"
49 #include "tree-iterator.h"
50 #include "hashtab.h"
51 #include "opts.h"
52 #include "hash-map.h"
53 #include "is-a.h"
54 #include "plugin-api.h"
55 #include "vec.h"
56 #include "hash-set.h"
57 #include "machmode.h"
58 #include "hard-reg-set.h"
59 #include "input.h"
60 #include "function.h"
61 #include "ipa-ref.h"
62 #include "cgraph.h"
63 #include "target-def.h"
64 #include "gimplify.h"
65 #include "wide-int-print.h"
66 #include "gimple-expr.h"
68 cpp_reader *parse_in; /* Declared in c-pragma.h. */
70 /* The following symbols are subsumed in the c_global_trees array, and
71 listed here individually for documentation purposes.
73 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
75 tree short_integer_type_node;
76 tree long_integer_type_node;
77 tree long_long_integer_type_node;
79 tree short_unsigned_type_node;
80 tree long_unsigned_type_node;
81 tree long_long_unsigned_type_node;
83 tree truthvalue_type_node;
84 tree truthvalue_false_node;
85 tree truthvalue_true_node;
87 tree ptrdiff_type_node;
89 tree unsigned_char_type_node;
90 tree signed_char_type_node;
91 tree wchar_type_node;
93 tree char16_type_node;
94 tree char32_type_node;
96 tree float_type_node;
97 tree double_type_node;
98 tree long_double_type_node;
100 tree complex_integer_type_node;
101 tree complex_float_type_node;
102 tree complex_double_type_node;
103 tree complex_long_double_type_node;
105 tree dfloat32_type_node;
106 tree dfloat64_type_node;
107 tree_dfloat128_type_node;
109 tree intQI_type_node;
110 tree intHI_type_node;
111 tree intSI_type_node;
112 tree intDI_type_node;
113 tree intTI_type_node;
115 tree unsigned_intQI_type_node;
116 tree unsigned_intHI_type_node;
117 tree unsigned_intSI_type_node;
118 tree unsigned_intDI_type_node;
119 tree unsigned_intTI_type_node;
121 tree widest_integer_literal_type_node;
122 tree widest_unsigned_literal_type_node;
124 Nodes for types `void *' and `const void *'.
126 tree ptr_type_node, const_ptr_type_node;
128 Nodes for types `char *' and `const char *'.
130 tree string_type_node, const_string_type_node;
132 Type `char[SOMENUMBER]'.
133 Used when an array of char is needed and the size is irrelevant.
135 tree char_array_type_node;
137 Type `wchar_t[SOMENUMBER]' or something like it.
138 Used when a wide string literal is created.
140 tree wchar_array_type_node;
142 Type `char16_t[SOMENUMBER]' or something like it.
143 Used when a UTF-16 string literal is created.
145 tree char16_array_type_node;
147 Type `char32_t[SOMENUMBER]' or something like it.
148 Used when a UTF-32 string literal is created.
150 tree char32_array_type_node;
152 Type `int ()' -- used for implicit declaration of functions.
154 tree default_function_type;
156 A VOID_TYPE node, packaged in a TREE_LIST.
158 tree void_list_node;
160 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
161 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
162 VAR_DECLS, but C++ does.)
164 tree function_name_decl_node;
165 tree pretty_function_name_decl_node;
166 tree c99_function_name_decl_node;
168 Stack of nested function name VAR_DECLs.
170 tree saved_function_name_decls;
174 tree c_global_trees[CTI_MAX];
176 /* Switches common to the C front ends. */
178 /* Nonzero means don't output line number information. */
180 char flag_no_line_commands;
182 /* Nonzero causes -E output not to be done, but directives such as
183 #define that have side effects are still obeyed. */
185 char flag_no_output;
187 /* Nonzero means dump macros in some fashion. */
189 char flag_dump_macros;
191 /* Nonzero means pass #include lines through to the output. */
193 char flag_dump_includes;
195 /* Nonzero means process PCH files while preprocessing. */
197 bool flag_pch_preprocess;
199 /* The file name to which we should write a precompiled header, or
200 NULL if no header will be written in this compile. */
202 const char *pch_file;
204 /* Nonzero if an ISO standard was selected. It rejects macros in the
205 user's namespace. */
206 int flag_iso;
208 /* C/ObjC language option variables. */
211 /* Nonzero means allow type mismatches in conditional expressions;
212 just make their values `void'. */
214 int flag_cond_mismatch;
216 /* Nonzero means enable C89 Amendment 1 features. */
218 int flag_isoc94;
220 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
222 int flag_isoc99;
224 /* Nonzero means use the ISO C11 dialect of C. */
226 int flag_isoc11;
228 /* Nonzero means that we have builtin functions, and main is an int. */
230 int flag_hosted = 1;
233 /* ObjC language option variables. */
236 /* Tells the compiler that this is a special run. Do not perform any
237 compiling, instead we are to test some platform dependent features
238 and output a C header file with appropriate definitions. */
240 int print_struct_values;
242 /* Tells the compiler what is the constant string class for ObjC. */
244 const char *constant_string_class_name;
247 /* C++ language option variables. */
250 /* Nonzero means generate separate instantiation control files and
251 juggle them at link time. */
253 int flag_use_repository;
255 /* The C++ dialect being used. C++11 is the default. */
257 enum cxx_dialect cxx_dialect = cxx98;
259 /* Maximum template instantiation depth. This limit exists to limit the
260 time it takes to notice excessively recursive template instantiations.
262 The default is lower than the 1024 recommended by the C++0x standard
263 because G++ runs out of stack before 1024 with highly recursive template
264 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
266 int max_tinst_depth = 900;
268 /* The elements of `ridpointers' are identifier nodes for the reserved
269 type names and storage classes. It is indexed by a RID_... value. */
270 tree *ridpointers;
272 tree (*make_fname_decl) (location_t, tree, int);
274 /* Nonzero means don't warn about problems that occur when the code is
275 executed. */
276 int c_inhibit_evaluation_warnings;
278 /* Whether we are building a boolean conversion inside
279 convert_for_assignment, or some other late binary operation. If
280 build_binary_op is called for C (from code shared by C and C++) in
281 this case, then the operands have already been folded and the
282 result will not be folded again, so C_MAYBE_CONST_EXPR should not
283 be generated. */
284 bool in_late_binary_op;
286 /* Whether lexing has been completed, so subsequent preprocessor
287 errors should use the compiler's input_location. */
288 bool done_lexing = false;
290 /* Information about how a function name is generated. */
291 struct fname_var_t
293 tree *const decl; /* pointer to the VAR_DECL. */
294 const unsigned rid; /* RID number for the identifier. */
295 const int pretty; /* How pretty is it? */
298 /* The three ways of getting then name of the current function. */
300 const struct fname_var_t fname_vars[] =
302 /* C99 compliant __func__, must be first. */
303 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
304 /* GCC __FUNCTION__ compliant. */
305 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
306 /* GCC __PRETTY_FUNCTION__ compliant. */
307 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
308 {NULL, 0, 0},
311 /* Global visibility options. */
312 struct visibility_flags visibility_options;
314 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
315 static tree check_case_value (location_t, tree);
316 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
318 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
325 int, bool *);
326 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
327 int, bool *);
328 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
329 bool *);
330 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_always_inline_attribute (tree *, tree, tree, int,
336 bool *);
337 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
344 bool *);
345 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
346 bool *);
347 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_transparent_union_attribute (tree *, tree, tree,
349 int, bool *);
350 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
356 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
357 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
360 static tree handle_visibility_attribute (tree *, tree, tree, int,
361 bool *);
362 static tree handle_tls_model_attribute (tree *, tree, tree, int,
363 bool *);
364 static tree handle_no_instrument_function_attribute (tree *, tree,
365 tree, int, bool *);
366 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_deprecated_attribute (tree *, tree, tree, int,
375 bool *);
376 static tree handle_vector_size_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
382 bool *);
383 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
390 static tree ignore_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
394 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
396 bool *);
397 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
398 bool *);
399 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
400 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
401 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
402 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
404 static void check_function_nonnull (tree, int, tree *);
405 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
406 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
407 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
408 static int resort_field_decl_cmp (const void *, const void *);
410 /* Reserved words. The third field is a mask: keywords are disabled
411 if they match the mask.
413 Masks for languages:
414 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
415 C --std=c99: D_CXXONLY | D_OBJC
416 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
417 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
418 C++ --std=c0x: D_CONLY | D_OBJC
419 ObjC++ is like C++ except that D_OBJC is not set
421 If -fno-asm is used, D_ASM is added to the mask. If
422 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
423 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
424 In C with -Wc++-compat, we warn if D_CXXWARN is set.
426 Note the complication of the D_CXX_OBJC keywords. These are
427 reserved words such as 'class'. In C++, 'class' is a reserved
428 word. In Objective-C++ it is too. In Objective-C, it is a
429 reserved word too, but only if it follows an '@' sign.
431 const struct c_common_resword c_common_reswords[] =
433 { "_Alignas", RID_ALIGNAS, D_CONLY },
434 { "_Alignof", RID_ALIGNOF, D_CONLY },
435 { "_Atomic", RID_ATOMIC, D_CONLY },
436 { "_Bool", RID_BOOL, D_CONLY },
437 { "_Complex", RID_COMPLEX, 0 },
438 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
439 { "_Cilk_sync", RID_CILK_SYNC, 0 },
440 { "_Cilk_for", RID_CILK_FOR, 0 },
441 { "_Imaginary", RID_IMAGINARY, D_CONLY },
442 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
443 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
444 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
445 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
446 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
447 { "_Sat", RID_SAT, D_CONLY | D_EXT },
448 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
449 { "_Noreturn", RID_NORETURN, D_CONLY },
450 { "_Generic", RID_GENERIC, D_CONLY },
451 { "_Thread_local", RID_THREAD, D_CONLY },
452 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
453 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
454 { "__alignof", RID_ALIGNOF, 0 },
455 { "__alignof__", RID_ALIGNOF, 0 },
456 { "__asm", RID_ASM, 0 },
457 { "__asm__", RID_ASM, 0 },
458 { "__attribute", RID_ATTRIBUTE, 0 },
459 { "__attribute__", RID_ATTRIBUTE, 0 },
460 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
461 { "__bases", RID_BASES, D_CXXONLY },
462 { "__builtin_call_with_static_chain",
463 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
464 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
465 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
466 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
467 { "__builtin_offsetof", RID_OFFSETOF, 0 },
468 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
469 { "__builtin_va_arg", RID_VA_ARG, 0 },
470 { "__complex", RID_COMPLEX, 0 },
471 { "__complex__", RID_COMPLEX, 0 },
472 { "__const", RID_CONST, 0 },
473 { "__const__", RID_CONST, 0 },
474 { "__decltype", RID_DECLTYPE, D_CXXONLY },
475 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
476 { "__extension__", RID_EXTENSION, 0 },
477 { "__func__", RID_C99_FUNCTION_NAME, 0 },
478 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
479 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
480 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
481 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
482 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
483 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
484 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
485 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
486 { "__imag", RID_IMAGPART, 0 },
487 { "__imag__", RID_IMAGPART, 0 },
488 { "__inline", RID_INLINE, 0 },
489 { "__inline__", RID_INLINE, 0 },
490 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
491 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
492 { "__is_class", RID_IS_CLASS, D_CXXONLY },
493 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
494 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
495 { "__is_final", RID_IS_FINAL, D_CXXONLY },
496 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
497 { "__is_pod", RID_IS_POD, D_CXXONLY },
498 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
499 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
500 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
501 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
502 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
503 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
504 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
505 { "__is_union", RID_IS_UNION, D_CXXONLY },
506 { "__label__", RID_LABEL, 0 },
507 { "__null", RID_NULL, 0 },
508 { "__real", RID_REALPART, 0 },
509 { "__real__", RID_REALPART, 0 },
510 { "__restrict", RID_RESTRICT, 0 },
511 { "__restrict__", RID_RESTRICT, 0 },
512 { "__signed", RID_SIGNED, 0 },
513 { "__signed__", RID_SIGNED, 0 },
514 { "__thread", RID_THREAD, 0 },
515 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
516 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
517 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
518 { "__typeof", RID_TYPEOF, 0 },
519 { "__typeof__", RID_TYPEOF, 0 },
520 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
521 { "__volatile", RID_VOLATILE, 0 },
522 { "__volatile__", RID_VOLATILE, 0 },
523 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
524 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
525 { "asm", RID_ASM, D_ASM },
526 { "auto", RID_AUTO, 0 },
527 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
528 { "break", RID_BREAK, 0 },
529 { "case", RID_CASE, 0 },
530 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
531 { "char", RID_CHAR, 0 },
532 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
533 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
534 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
535 { "const", RID_CONST, 0 },
536 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
537 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
538 { "continue", RID_CONTINUE, 0 },
539 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
540 { "default", RID_DEFAULT, 0 },
541 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
542 { "do", RID_DO, 0 },
543 { "double", RID_DOUBLE, 0 },
544 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
545 { "else", RID_ELSE, 0 },
546 { "enum", RID_ENUM, 0 },
547 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
548 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
549 { "extern", RID_EXTERN, 0 },
550 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
551 { "float", RID_FLOAT, 0 },
552 { "for", RID_FOR, 0 },
553 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
554 { "goto", RID_GOTO, 0 },
555 { "if", RID_IF, 0 },
556 { "inline", RID_INLINE, D_EXT89 },
557 { "int", RID_INT, 0 },
558 { "long", RID_LONG, 0 },
559 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
560 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
561 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
562 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
563 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
564 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
565 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
566 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
567 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
568 { "register", RID_REGISTER, 0 },
569 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
570 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
571 { "return", RID_RETURN, 0 },
572 { "short", RID_SHORT, 0 },
573 { "signed", RID_SIGNED, 0 },
574 { "sizeof", RID_SIZEOF, 0 },
575 { "static", RID_STATIC, 0 },
576 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
577 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
578 { "struct", RID_STRUCT, 0 },
579 { "switch", RID_SWITCH, 0 },
580 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
581 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
582 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
583 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
584 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
585 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
586 { "typedef", RID_TYPEDEF, 0 },
587 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
588 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
589 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
590 { "union", RID_UNION, 0 },
591 { "unsigned", RID_UNSIGNED, 0 },
592 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
593 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
594 { "void", RID_VOID, 0 },
595 { "volatile", RID_VOLATILE, 0 },
596 { "wchar_t", RID_WCHAR, D_CXXONLY },
597 { "while", RID_WHILE, 0 },
599 /* Concepts-related keywords */
600 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
601 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
603 /* These Objective-C keywords are recognized only immediately after
604 an '@'. */
605 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
606 { "defs", RID_AT_DEFS, D_OBJC },
607 { "encode", RID_AT_ENCODE, D_OBJC },
608 { "end", RID_AT_END, D_OBJC },
609 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
610 { "interface", RID_AT_INTERFACE, D_OBJC },
611 { "protocol", RID_AT_PROTOCOL, D_OBJC },
612 { "selector", RID_AT_SELECTOR, D_OBJC },
613 { "finally", RID_AT_FINALLY, D_OBJC },
614 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
615 { "optional", RID_AT_OPTIONAL, D_OBJC },
616 { "required", RID_AT_REQUIRED, D_OBJC },
617 { "property", RID_AT_PROPERTY, D_OBJC },
618 { "package", RID_AT_PACKAGE, D_OBJC },
619 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
620 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
621 /* These are recognized only in protocol-qualifier context
622 (see above) */
623 { "bycopy", RID_BYCOPY, D_OBJC },
624 { "byref", RID_BYREF, D_OBJC },
625 { "in", RID_IN, D_OBJC },
626 { "inout", RID_INOUT, D_OBJC },
627 { "oneway", RID_ONEWAY, D_OBJC },
628 { "out", RID_OUT, D_OBJC },
629 /* These are recognized inside a property attribute list */
630 { "assign", RID_ASSIGN, D_OBJC },
631 { "copy", RID_COPY, D_OBJC },
632 { "getter", RID_GETTER, D_OBJC },
633 { "nonatomic", RID_NONATOMIC, D_OBJC },
634 { "readonly", RID_READONLY, D_OBJC },
635 { "readwrite", RID_READWRITE, D_OBJC },
636 { "retain", RID_RETAIN, D_OBJC },
637 { "setter", RID_SETTER, D_OBJC },
640 const unsigned int num_c_common_reswords =
641 sizeof c_common_reswords / sizeof (struct c_common_resword);
643 /* Table of machine-independent attributes common to all C-like languages.
645 All attributes referencing arguments should be additionally processed
646 in chkp_copy_function_type_adding_bounds for correct instrumentation
647 by Pointer Bounds Checker.
648 Current list of processed common attributes: nonnull. */
649 const struct attribute_spec c_common_attribute_table[] =
651 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
652 affects_type_identity } */
653 { "packed", 0, 0, false, false, false,
654 handle_packed_attribute , false},
655 { "nocommon", 0, 0, true, false, false,
656 handle_nocommon_attribute, false},
657 { "common", 0, 0, true, false, false,
658 handle_common_attribute, false },
659 /* FIXME: logically, noreturn attributes should be listed as
660 "false, true, true" and apply to function types. But implementing this
661 would require all the places in the compiler that use TREE_THIS_VOLATILE
662 on a decl to identify non-returning functions to be located and fixed
663 to check the function type instead. */
664 { "noreturn", 0, 0, true, false, false,
665 handle_noreturn_attribute, false },
666 { "volatile", 0, 0, true, false, false,
667 handle_noreturn_attribute, false },
668 { "stack_protect", 0, 0, true, false, false,
669 handle_stack_protect_attribute, false },
670 { "noinline", 0, 0, true, false, false,
671 handle_noinline_attribute, false },
672 { "noclone", 0, 0, true, false, false,
673 handle_noclone_attribute, false },
674 { "no_icf", 0, 0, true, false, false,
675 handle_noicf_attribute, false },
676 { "leaf", 0, 0, true, false, false,
677 handle_leaf_attribute, false },
678 { "always_inline", 0, 0, true, false, false,
679 handle_always_inline_attribute, false },
680 { "gnu_inline", 0, 0, true, false, false,
681 handle_gnu_inline_attribute, false },
682 { "artificial", 0, 0, true, false, false,
683 handle_artificial_attribute, false },
684 { "flatten", 0, 0, true, false, false,
685 handle_flatten_attribute, false },
686 { "used", 0, 0, true, false, false,
687 handle_used_attribute, false },
688 { "unused", 0, 0, false, false, false,
689 handle_unused_attribute, false },
690 { "externally_visible", 0, 0, true, false, false,
691 handle_externally_visible_attribute, false },
692 { "no_reorder", 0, 0, true, false, false,
693 handle_no_reorder_attribute, false },
694 /* The same comments as for noreturn attributes apply to const ones. */
695 { "const", 0, 0, true, false, false,
696 handle_const_attribute, false },
697 { "transparent_union", 0, 0, false, false, false,
698 handle_transparent_union_attribute, false },
699 { "constructor", 0, 1, true, false, false,
700 handle_constructor_attribute, false },
701 { "destructor", 0, 1, true, false, false,
702 handle_destructor_attribute, false },
703 { "mode", 1, 1, false, true, false,
704 handle_mode_attribute, false },
705 { "section", 1, 1, true, false, false,
706 handle_section_attribute, false },
707 { "aligned", 0, 1, false, false, false,
708 handle_aligned_attribute, false },
709 { "weak", 0, 0, true, false, false,
710 handle_weak_attribute, false },
711 { "ifunc", 1, 1, true, false, false,
712 handle_ifunc_attribute, false },
713 { "alias", 1, 1, true, false, false,
714 handle_alias_attribute, false },
715 { "weakref", 0, 1, true, false, false,
716 handle_weakref_attribute, false },
717 { "no_instrument_function", 0, 0, true, false, false,
718 handle_no_instrument_function_attribute,
719 false },
720 { "malloc", 0, 0, true, false, false,
721 handle_malloc_attribute, false },
722 { "returns_twice", 0, 0, true, false, false,
723 handle_returns_twice_attribute, false },
724 { "no_stack_limit", 0, 0, true, false, false,
725 handle_no_limit_stack_attribute, false },
726 { "pure", 0, 0, true, false, false,
727 handle_pure_attribute, false },
728 { "transaction_callable", 0, 0, false, true, false,
729 handle_tm_attribute, false },
730 { "transaction_unsafe", 0, 0, false, true, false,
731 handle_tm_attribute, false },
732 { "transaction_safe", 0, 0, false, true, false,
733 handle_tm_attribute, false },
734 { "transaction_may_cancel_outer", 0, 0, false, true, false,
735 handle_tm_attribute, false },
736 /* ??? These two attributes didn't make the transition from the
737 Intel language document to the multi-vendor language document. */
738 { "transaction_pure", 0, 0, false, true, false,
739 handle_tm_attribute, false },
740 { "transaction_wrap", 1, 1, true, false, false,
741 handle_tm_wrap_attribute, false },
742 /* For internal use (marking of builtins) only. The name contains space
743 to prevent its usage in source code. */
744 { "no vops", 0, 0, true, false, false,
745 handle_novops_attribute, false },
746 { "deprecated", 0, 1, false, false, false,
747 handle_deprecated_attribute, false },
748 { "vector_size", 1, 1, false, true, false,
749 handle_vector_size_attribute, false },
750 { "visibility", 1, 1, false, false, false,
751 handle_visibility_attribute, false },
752 { "tls_model", 1, 1, true, false, false,
753 handle_tls_model_attribute, false },
754 { "nonnull", 0, -1, false, true, true,
755 handle_nonnull_attribute, false },
756 { "nothrow", 0, 0, true, false, false,
757 handle_nothrow_attribute, false },
758 { "may_alias", 0, 0, false, true, false, NULL, false },
759 { "cleanup", 1, 1, true, false, false,
760 handle_cleanup_attribute, false },
761 { "warn_unused_result", 0, 0, false, true, true,
762 handle_warn_unused_result_attribute, false },
763 { "sentinel", 0, 1, false, true, true,
764 handle_sentinel_attribute, false },
765 /* For internal use (marking of builtins) only. The name contains space
766 to prevent its usage in source code. */
767 { "type generic", 0, 0, false, true, true,
768 handle_type_generic_attribute, false },
769 { "alloc_size", 1, 2, false, true, true,
770 handle_alloc_size_attribute, false },
771 { "cold", 0, 0, true, false, false,
772 handle_cold_attribute, false },
773 { "hot", 0, 0, true, false, false,
774 handle_hot_attribute, false },
775 { "no_address_safety_analysis",
776 0, 0, true, false, false,
777 handle_no_address_safety_analysis_attribute,
778 false },
779 { "no_sanitize_address", 0, 0, true, false, false,
780 handle_no_sanitize_address_attribute,
781 false },
782 { "no_sanitize_thread", 0, 0, true, false, false,
783 handle_no_sanitize_address_attribute,
784 false },
785 { "no_sanitize_undefined", 0, 0, true, false, false,
786 handle_no_sanitize_undefined_attribute,
787 false },
788 { "warning", 1, 1, true, false, false,
789 handle_error_attribute, false },
790 { "error", 1, 1, true, false, false,
791 handle_error_attribute, false },
792 { "target", 1, -1, true, false, false,
793 handle_target_attribute, false },
794 { "optimize", 1, -1, true, false, false,
795 handle_optimize_attribute, false },
796 /* For internal use only. The leading '*' both prevents its usage in
797 source code and signals that it may be overridden by machine tables. */
798 { "*tm regparm", 0, 0, false, true, true,
799 ignore_attribute, false },
800 { "no_split_stack", 0, 0, true, false, false,
801 handle_no_split_stack_attribute, false },
802 /* For internal use (marking of builtins and runtime functions) only.
803 The name contains space to prevent its usage in source code. */
804 { "fn spec", 1, 1, false, true, true,
805 handle_fnspec_attribute, false },
806 { "warn_unused", 0, 0, false, false, false,
807 handle_warn_unused_attribute, false },
808 { "returns_nonnull", 0, 0, false, true, true,
809 handle_returns_nonnull_attribute, false },
810 { "omp declare simd", 0, -1, true, false, false,
811 handle_omp_declare_simd_attribute, false },
812 { "cilk simd function", 0, -1, true, false, false,
813 handle_omp_declare_simd_attribute, false },
814 { "omp declare target", 0, 0, true, false, false,
815 handle_omp_declare_target_attribute, false },
816 { "alloc_align", 1, 1, false, true, true,
817 handle_alloc_align_attribute, false },
818 { "assume_aligned", 1, 2, false, true, true,
819 handle_assume_aligned_attribute, false },
820 { "designated_init", 0, 0, false, true, false,
821 handle_designated_init_attribute, false },
822 { "bnd_variable_size", 0, 0, true, false, false,
823 handle_bnd_variable_size_attribute, false },
824 { "bnd_legacy", 0, 0, true, false, false,
825 handle_bnd_legacy, false },
826 { "bnd_instrument", 0, 0, true, false, false,
827 handle_bnd_instrument, false },
828 { NULL, 0, 0, false, false, false, NULL, false }
831 /* Give the specifications for the format attributes, used by C and all
832 descendants.
834 All attributes referencing arguments should be additionally processed
835 in chkp_copy_function_type_adding_bounds for correct instrumentation
836 by Pointer Bounds Checker.
837 Current list of processed format attributes: format, format_arg. */
838 const struct attribute_spec c_common_format_attribute_table[] =
840 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
841 affects_type_identity } */
842 { "format", 3, 3, false, true, true,
843 handle_format_attribute, false },
844 { "format_arg", 1, 1, false, true, true,
845 handle_format_arg_attribute, false },
846 { NULL, 0, 0, false, false, false, NULL, false }
849 /* Return identifier for address space AS. */
851 const char *
852 c_addr_space_name (addr_space_t as)
854 int rid = RID_FIRST_ADDR_SPACE + as;
855 gcc_assert (ridpointers [rid]);
856 return IDENTIFIER_POINTER (ridpointers [rid]);
859 /* Push current bindings for the function name VAR_DECLS. */
861 void
862 start_fname_decls (void)
864 unsigned ix;
865 tree saved = NULL_TREE;
867 for (ix = 0; fname_vars[ix].decl; ix++)
869 tree decl = *fname_vars[ix].decl;
871 if (decl)
873 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
874 saved);
875 *fname_vars[ix].decl = NULL_TREE;
878 if (saved || saved_function_name_decls)
879 /* Normally they'll have been NULL, so only push if we've got a
880 stack, or they are non-NULL. */
881 saved_function_name_decls = tree_cons (saved, NULL_TREE,
882 saved_function_name_decls);
885 /* Finish up the current bindings, adding them into the current function's
886 statement tree. This must be done _before_ finish_stmt_tree is called.
887 If there is no current function, we must be at file scope and no statements
888 are involved. Pop the previous bindings. */
890 void
891 finish_fname_decls (void)
893 unsigned ix;
894 tree stmts = NULL_TREE;
895 tree stack = saved_function_name_decls;
897 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
898 append_to_statement_list (TREE_VALUE (stack), &stmts);
900 if (stmts)
902 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
904 if (TREE_CODE (*bodyp) == BIND_EXPR)
905 bodyp = &BIND_EXPR_BODY (*bodyp);
907 append_to_statement_list_force (*bodyp, &stmts);
908 *bodyp = stmts;
911 for (ix = 0; fname_vars[ix].decl; ix++)
912 *fname_vars[ix].decl = NULL_TREE;
914 if (stack)
916 /* We had saved values, restore them. */
917 tree saved;
919 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
921 tree decl = TREE_PURPOSE (saved);
922 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
924 *fname_vars[ix].decl = decl;
926 stack = TREE_CHAIN (stack);
928 saved_function_name_decls = stack;
931 /* Return the text name of the current function, suitably prettified
932 by PRETTY_P. Return string must be freed by caller. */
934 const char *
935 fname_as_string (int pretty_p)
937 const char *name = "top level";
938 char *namep;
939 int vrb = 2, len;
940 cpp_string cstr = { 0, 0 }, strname;
942 if (!pretty_p)
944 name = "";
945 vrb = 0;
948 if (current_function_decl)
949 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
951 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
953 namep = XNEWVEC (char, len);
954 snprintf (namep, len, "\"%s\"", name);
955 strname.text = (unsigned char *) namep;
956 strname.len = len - 1;
958 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
960 XDELETEVEC (namep);
961 return (const char *) cstr.text;
964 return namep;
967 /* Return the VAR_DECL for a const char array naming the current
968 function. If the VAR_DECL has not yet been created, create it
969 now. RID indicates how it should be formatted and IDENTIFIER_NODE
970 ID is its name (unfortunately C and C++ hold the RID values of
971 keywords in different places, so we can't derive RID from ID in
972 this language independent code. LOC is the location of the
973 function. */
975 tree
976 fname_decl (location_t loc, unsigned int rid, tree id)
978 unsigned ix;
979 tree decl = NULL_TREE;
981 for (ix = 0; fname_vars[ix].decl; ix++)
982 if (fname_vars[ix].rid == rid)
983 break;
985 decl = *fname_vars[ix].decl;
986 if (!decl)
988 /* If a tree is built here, it would normally have the lineno of
989 the current statement. Later this tree will be moved to the
990 beginning of the function and this line number will be wrong.
991 To avoid this problem set the lineno to 0 here; that prevents
992 it from appearing in the RTL. */
993 tree stmts;
994 location_t saved_location = input_location;
995 input_location = UNKNOWN_LOCATION;
997 stmts = push_stmt_list ();
998 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
999 stmts = pop_stmt_list (stmts);
1000 if (!IS_EMPTY_STMT (stmts))
1001 saved_function_name_decls
1002 = tree_cons (decl, stmts, saved_function_name_decls);
1003 *fname_vars[ix].decl = decl;
1004 input_location = saved_location;
1006 if (!ix && !current_function_decl)
1007 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1009 return decl;
1012 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1014 tree
1015 fix_string_type (tree value)
1017 int length = TREE_STRING_LENGTH (value);
1018 int nchars;
1019 tree e_type, i_type, a_type;
1021 /* Compute the number of elements, for the array type. */
1022 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1024 nchars = length;
1025 e_type = char_type_node;
1027 else if (TREE_TYPE (value) == char16_array_type_node)
1029 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1030 e_type = char16_type_node;
1032 else if (TREE_TYPE (value) == char32_array_type_node)
1034 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1035 e_type = char32_type_node;
1037 else
1039 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1040 e_type = wchar_type_node;
1043 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1044 limit in C++98 Annex B is very large (65536) and is not normative,
1045 so we do not diagnose it (warn_overlength_strings is forced off
1046 in c_common_post_options). */
1047 if (warn_overlength_strings)
1049 const int nchars_max = flag_isoc99 ? 4095 : 509;
1050 const int relevant_std = flag_isoc99 ? 99 : 90;
1051 if (nchars - 1 > nchars_max)
1052 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1053 separate the %d from the 'C'. 'ISO' should not be
1054 translated, but it may be moved after 'C%d' in languages
1055 where modifiers follow nouns. */
1056 pedwarn (input_location, OPT_Woverlength_strings,
1057 "string length %qd is greater than the length %qd "
1058 "ISO C%d compilers are required to support",
1059 nchars - 1, nchars_max, relevant_std);
1062 /* Create the array type for the string constant. The ISO C++
1063 standard says that a string literal has type `const char[N]' or
1064 `const wchar_t[N]'. We use the same logic when invoked as a C
1065 front-end with -Wwrite-strings.
1066 ??? We should change the type of an expression depending on the
1067 state of a warning flag. We should just be warning -- see how
1068 this is handled in the C++ front-end for the deprecated implicit
1069 conversion from string literals to `char*' or `wchar_t*'.
1071 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1072 array type being the unqualified version of that type.
1073 Therefore, if we are constructing an array of const char, we must
1074 construct the matching unqualified array type first. The C front
1075 end does not require this, but it does no harm, so we do it
1076 unconditionally. */
1077 i_type = build_index_type (size_int (nchars - 1));
1078 a_type = build_array_type (e_type, i_type);
1079 if (c_dialect_cxx() || warn_write_strings)
1080 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1082 TREE_TYPE (value) = a_type;
1083 TREE_CONSTANT (value) = 1;
1084 TREE_READONLY (value) = 1;
1085 TREE_STATIC (value) = 1;
1086 return value;
1089 /* If DISABLE is true, stop issuing warnings. This is used when
1090 parsing code that we know will not be executed. This function may
1091 be called multiple times, and works as a stack. */
1093 static void
1094 c_disable_warnings (bool disable)
1096 if (disable)
1098 ++c_inhibit_evaluation_warnings;
1099 fold_defer_overflow_warnings ();
1103 /* If ENABLE is true, reenable issuing warnings. */
1105 static void
1106 c_enable_warnings (bool enable)
1108 if (enable)
1110 --c_inhibit_evaluation_warnings;
1111 fold_undefer_and_ignore_overflow_warnings ();
1115 /* Fully fold EXPR, an expression that was not folded (beyond integer
1116 constant expressions and null pointer constants) when being built
1117 up. If IN_INIT, this is in a static initializer and certain
1118 changes are made to the folding done. Clear *MAYBE_CONST if
1119 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1120 expression because it contains an evaluated operator (in C99) or an
1121 operator outside of sizeof returning an integer constant (in C90)
1122 not permitted in constant expressions, or because it contains an
1123 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1124 set to true by callers before calling this function.) Return the
1125 folded expression. Function arguments have already been folded
1126 before calling this function, as have the contents of SAVE_EXPR,
1127 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1128 C_MAYBE_CONST_EXPR. */
1130 tree
1131 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1133 tree ret;
1134 tree eptype = NULL_TREE;
1135 bool dummy = true;
1136 bool maybe_const_itself = true;
1137 location_t loc = EXPR_LOCATION (expr);
1139 /* This function is not relevant to C++ because C++ folds while
1140 parsing, and may need changes to be correct for C++ when C++
1141 stops folding while parsing. */
1142 if (c_dialect_cxx ())
1143 gcc_unreachable ();
1145 if (!maybe_const)
1146 maybe_const = &dummy;
1147 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1149 eptype = TREE_TYPE (expr);
1150 expr = TREE_OPERAND (expr, 0);
1152 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1153 &maybe_const_itself);
1154 if (eptype)
1155 ret = fold_convert_loc (loc, eptype, ret);
1156 *maybe_const &= maybe_const_itself;
1157 return ret;
1160 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1161 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1162 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1163 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1164 both evaluated and unevaluated subexpressions while
1165 *MAYBE_CONST_ITSELF is carried from only evaluated
1166 subexpressions). */
1168 static tree
1169 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1170 bool *maybe_const_itself)
1172 tree ret = expr;
1173 enum tree_code code = TREE_CODE (expr);
1174 enum tree_code_class kind = TREE_CODE_CLASS (code);
1175 location_t loc = EXPR_LOCATION (expr);
1176 tree op0, op1, op2, op3;
1177 tree orig_op0, orig_op1, orig_op2;
1178 bool op0_const = true, op1_const = true, op2_const = true;
1179 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1180 bool nowarning = TREE_NO_WARNING (expr);
1181 bool unused_p;
1183 /* This function is not relevant to C++ because C++ folds while
1184 parsing, and may need changes to be correct for C++ when C++
1185 stops folding while parsing. */
1186 if (c_dialect_cxx ())
1187 gcc_unreachable ();
1189 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1190 anything else not counted as an expression cannot usefully be
1191 folded further at this point. */
1192 if (!IS_EXPR_CODE_CLASS (kind)
1193 || kind == tcc_statement
1194 || code == SAVE_EXPR)
1195 return expr;
1197 /* Operands of variable-length expressions (function calls) have
1198 already been folded, as have __builtin_* function calls, and such
1199 expressions cannot occur in constant expressions. */
1200 if (kind == tcc_vl_exp)
1202 *maybe_const_operands = false;
1203 ret = fold (expr);
1204 goto out;
1207 if (code == C_MAYBE_CONST_EXPR)
1209 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1210 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1211 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1212 *maybe_const_operands = false;
1213 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1214 *maybe_const_itself = false;
1215 if (pre && !in_init)
1216 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1217 else
1218 ret = inner;
1219 goto out;
1222 /* Assignment, increment, decrement, function call and comma
1223 operators, and statement expressions, cannot occur in constant
1224 expressions if evaluated / outside of sizeof. (Function calls
1225 were handled above, though VA_ARG_EXPR is treated like a function
1226 call here, and statement expressions are handled through
1227 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1228 switch (code)
1230 case MODIFY_EXPR:
1231 case PREDECREMENT_EXPR:
1232 case PREINCREMENT_EXPR:
1233 case POSTDECREMENT_EXPR:
1234 case POSTINCREMENT_EXPR:
1235 case COMPOUND_EXPR:
1236 *maybe_const_operands = false;
1237 break;
1239 case VA_ARG_EXPR:
1240 case TARGET_EXPR:
1241 case BIND_EXPR:
1242 case OBJ_TYPE_REF:
1243 *maybe_const_operands = false;
1244 ret = fold (expr);
1245 goto out;
1247 default:
1248 break;
1251 /* Fold individual tree codes as appropriate. */
1252 switch (code)
1254 case COMPOUND_LITERAL_EXPR:
1255 /* Any non-constancy will have been marked in a containing
1256 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1257 goto out;
1259 case COMPONENT_REF:
1260 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1261 op1 = TREE_OPERAND (expr, 1);
1262 op2 = TREE_OPERAND (expr, 2);
1263 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1264 maybe_const_itself);
1265 STRIP_TYPE_NOPS (op0);
1266 if (op0 != orig_op0)
1267 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1268 if (ret != expr)
1270 TREE_READONLY (ret) = TREE_READONLY (expr);
1271 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1273 goto out;
1275 case ARRAY_REF:
1276 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1277 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1278 op2 = TREE_OPERAND (expr, 2);
1279 op3 = TREE_OPERAND (expr, 3);
1280 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1281 maybe_const_itself);
1282 STRIP_TYPE_NOPS (op0);
1283 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1284 maybe_const_itself);
1285 STRIP_TYPE_NOPS (op1);
1286 op1 = decl_constant_value_for_optimization (op1);
1287 if (op0 != orig_op0 || op1 != orig_op1)
1288 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1289 if (ret != expr)
1291 TREE_READONLY (ret) = TREE_READONLY (expr);
1292 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1293 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1295 ret = fold (ret);
1296 goto out;
1298 case COMPOUND_EXPR:
1299 case MODIFY_EXPR:
1300 case PREDECREMENT_EXPR:
1301 case PREINCREMENT_EXPR:
1302 case POSTDECREMENT_EXPR:
1303 case POSTINCREMENT_EXPR:
1304 case PLUS_EXPR:
1305 case MINUS_EXPR:
1306 case MULT_EXPR:
1307 case POINTER_PLUS_EXPR:
1308 case TRUNC_DIV_EXPR:
1309 case CEIL_DIV_EXPR:
1310 case FLOOR_DIV_EXPR:
1311 case TRUNC_MOD_EXPR:
1312 case RDIV_EXPR:
1313 case EXACT_DIV_EXPR:
1314 case LSHIFT_EXPR:
1315 case RSHIFT_EXPR:
1316 case BIT_IOR_EXPR:
1317 case BIT_XOR_EXPR:
1318 case BIT_AND_EXPR:
1319 case LT_EXPR:
1320 case LE_EXPR:
1321 case GT_EXPR:
1322 case GE_EXPR:
1323 case EQ_EXPR:
1324 case NE_EXPR:
1325 case COMPLEX_EXPR:
1326 case TRUTH_AND_EXPR:
1327 case TRUTH_OR_EXPR:
1328 case TRUTH_XOR_EXPR:
1329 case UNORDERED_EXPR:
1330 case ORDERED_EXPR:
1331 case UNLT_EXPR:
1332 case UNLE_EXPR:
1333 case UNGT_EXPR:
1334 case UNGE_EXPR:
1335 case UNEQ_EXPR:
1336 /* Binary operations evaluating both arguments (increment and
1337 decrement are binary internally in GCC). */
1338 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1339 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1340 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1341 maybe_const_itself);
1342 STRIP_TYPE_NOPS (op0);
1343 if (code != MODIFY_EXPR
1344 && code != PREDECREMENT_EXPR
1345 && code != PREINCREMENT_EXPR
1346 && code != POSTDECREMENT_EXPR
1347 && code != POSTINCREMENT_EXPR)
1348 op0 = decl_constant_value_for_optimization (op0);
1349 /* The RHS of a MODIFY_EXPR was fully folded when building that
1350 expression for the sake of conversion warnings. */
1351 if (code != MODIFY_EXPR)
1352 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1353 maybe_const_itself);
1354 STRIP_TYPE_NOPS (op1);
1355 op1 = decl_constant_value_for_optimization (op1);
1356 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1357 ret = in_init
1358 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1359 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1360 else
1361 ret = fold (expr);
1362 if (TREE_OVERFLOW_P (ret)
1363 && !TREE_OVERFLOW_P (op0)
1364 && !TREE_OVERFLOW_P (op1))
1365 overflow_warning (EXPR_LOCATION (expr), ret);
1366 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1367 && TREE_CODE (orig_op1) != INTEGER_CST
1368 && TREE_CODE (op1) == INTEGER_CST
1369 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1370 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1371 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1372 && c_inhibit_evaluation_warnings == 0)
1374 if (tree_int_cst_sgn (op1) < 0)
1375 warning_at (loc, 0, (code == LSHIFT_EXPR
1376 ? G_("left shift count is negative")
1377 : G_("right shift count is negative")));
1378 else if (compare_tree_int (op1,
1379 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1380 >= 0)
1381 warning_at (loc, 0, (code == LSHIFT_EXPR
1382 ? G_("left shift count >= width of type")
1383 : G_("right shift count >= width of type")));
1385 if ((code == TRUNC_DIV_EXPR
1386 || code == CEIL_DIV_EXPR
1387 || code == FLOOR_DIV_EXPR
1388 || code == EXACT_DIV_EXPR
1389 || code == TRUNC_MOD_EXPR)
1390 && TREE_CODE (orig_op1) != INTEGER_CST
1391 && TREE_CODE (op1) == INTEGER_CST
1392 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1393 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1394 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1395 warn_for_div_by_zero (loc, op1);
1396 goto out;
1398 case INDIRECT_REF:
1399 case FIX_TRUNC_EXPR:
1400 case FLOAT_EXPR:
1401 CASE_CONVERT:
1402 case ADDR_SPACE_CONVERT_EXPR:
1403 case VIEW_CONVERT_EXPR:
1404 case NON_LVALUE_EXPR:
1405 case NEGATE_EXPR:
1406 case BIT_NOT_EXPR:
1407 case TRUTH_NOT_EXPR:
1408 case ADDR_EXPR:
1409 case CONJ_EXPR:
1410 case REALPART_EXPR:
1411 case IMAGPART_EXPR:
1412 /* Unary operations. */
1413 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1414 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1415 maybe_const_itself);
1416 STRIP_TYPE_NOPS (op0);
1417 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1418 op0 = decl_constant_value_for_optimization (op0);
1419 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1420 not prepared to deal with them if they occur in initializers. */
1421 if (op0 != orig_op0
1422 && code == ADDR_EXPR
1423 && (op1 = get_base_address (op0)) != NULL_TREE
1424 && TREE_CODE (op1) == INDIRECT_REF
1425 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1426 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1427 else if (op0 != orig_op0 || in_init)
1428 ret = in_init
1429 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1430 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1431 else
1432 ret = fold (expr);
1433 if (code == INDIRECT_REF
1434 && ret != expr
1435 && TREE_CODE (ret) == INDIRECT_REF)
1437 TREE_READONLY (ret) = TREE_READONLY (expr);
1438 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1439 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1441 switch (code)
1443 case FIX_TRUNC_EXPR:
1444 case FLOAT_EXPR:
1445 CASE_CONVERT:
1446 /* Don't warn about explicit conversions. We will already
1447 have warned about suspect implicit conversions. */
1448 break;
1450 default:
1451 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1452 overflow_warning (EXPR_LOCATION (expr), ret);
1453 break;
1455 goto out;
1457 case TRUTH_ANDIF_EXPR:
1458 case TRUTH_ORIF_EXPR:
1459 /* Binary operations not necessarily evaluating both
1460 arguments. */
1461 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1462 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1463 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1464 STRIP_TYPE_NOPS (op0);
1466 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1467 ? truthvalue_false_node
1468 : truthvalue_true_node));
1469 c_disable_warnings (unused_p);
1470 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1471 STRIP_TYPE_NOPS (op1);
1472 c_enable_warnings (unused_p);
1474 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1475 ret = in_init
1476 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1477 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1478 else
1479 ret = fold (expr);
1480 *maybe_const_operands &= op0_const;
1481 *maybe_const_itself &= op0_const_self;
1482 if (!(flag_isoc99
1483 && op0_const
1484 && op0_const_self
1485 && (code == TRUTH_ANDIF_EXPR
1486 ? op0 == truthvalue_false_node
1487 : op0 == truthvalue_true_node)))
1488 *maybe_const_operands &= op1_const;
1489 if (!(op0_const
1490 && op0_const_self
1491 && (code == TRUTH_ANDIF_EXPR
1492 ? op0 == truthvalue_false_node
1493 : op0 == truthvalue_true_node)))
1494 *maybe_const_itself &= op1_const_self;
1495 goto out;
1497 case COND_EXPR:
1498 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1499 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1500 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1501 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1503 STRIP_TYPE_NOPS (op0);
1504 c_disable_warnings (op0 == truthvalue_false_node);
1505 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1506 STRIP_TYPE_NOPS (op1);
1507 c_enable_warnings (op0 == truthvalue_false_node);
1509 c_disable_warnings (op0 == truthvalue_true_node);
1510 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1511 STRIP_TYPE_NOPS (op2);
1512 c_enable_warnings (op0 == truthvalue_true_node);
1514 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1515 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1516 else
1517 ret = fold (expr);
1518 *maybe_const_operands &= op0_const;
1519 *maybe_const_itself &= op0_const_self;
1520 if (!(flag_isoc99
1521 && op0_const
1522 && op0_const_self
1523 && op0 == truthvalue_false_node))
1524 *maybe_const_operands &= op1_const;
1525 if (!(op0_const
1526 && op0_const_self
1527 && op0 == truthvalue_false_node))
1528 *maybe_const_itself &= op1_const_self;
1529 if (!(flag_isoc99
1530 && op0_const
1531 && op0_const_self
1532 && op0 == truthvalue_true_node))
1533 *maybe_const_operands &= op2_const;
1534 if (!(op0_const
1535 && op0_const_self
1536 && op0 == truthvalue_true_node))
1537 *maybe_const_itself &= op2_const_self;
1538 goto out;
1540 case EXCESS_PRECISION_EXPR:
1541 /* Each case where an operand with excess precision may be
1542 encountered must remove the EXCESS_PRECISION_EXPR around
1543 inner operands and possibly put one around the whole
1544 expression or possibly convert to the semantic type (which
1545 c_fully_fold does); we cannot tell at this stage which is
1546 appropriate in any particular case. */
1547 gcc_unreachable ();
1549 default:
1550 /* Various codes may appear through folding built-in functions
1551 and their arguments. */
1552 goto out;
1555 out:
1556 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1557 have been done by this point, so remove them again. */
1558 nowarning |= TREE_NO_WARNING (ret);
1559 STRIP_TYPE_NOPS (ret);
1560 if (nowarning && !TREE_NO_WARNING (ret))
1562 if (!CAN_HAVE_LOCATION_P (ret))
1563 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1564 TREE_NO_WARNING (ret) = 1;
1566 if (ret != expr)
1567 protected_set_expr_location (ret, loc);
1568 return ret;
1571 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1572 return EXP. Otherwise, return either EXP or its known constant
1573 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1574 Is the BLKmode test appropriate? */
1576 tree
1577 decl_constant_value_for_optimization (tree exp)
1579 tree ret;
1581 /* This function is only used by C, for c_fully_fold and other
1582 optimization, and may not be correct for C++. */
1583 if (c_dialect_cxx ())
1584 gcc_unreachable ();
1586 if (!optimize
1587 || TREE_CODE (exp) != VAR_DECL
1588 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1589 || DECL_MODE (exp) == BLKmode)
1590 return exp;
1592 ret = decl_constant_value (exp);
1593 /* Avoid unwanted tree sharing between the initializer and current
1594 function's body where the tree can be modified e.g. by the
1595 gimplifier. */
1596 if (ret != exp && TREE_STATIC (exp))
1597 ret = unshare_expr (ret);
1598 return ret;
1601 /* Print a warning if a constant expression had overflow in folding.
1602 Invoke this function on every expression that the language
1603 requires to be a constant expression.
1604 Note the ANSI C standard says it is erroneous for a
1605 constant expression to overflow. */
1607 void
1608 constant_expression_warning (tree value)
1610 if (warn_overflow && pedantic
1611 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1612 || TREE_CODE (value) == FIXED_CST
1613 || TREE_CODE (value) == VECTOR_CST
1614 || TREE_CODE (value) == COMPLEX_CST)
1615 && TREE_OVERFLOW (value))
1616 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1619 /* The same as above but print an unconditional error. */
1620 void
1621 constant_expression_error (tree value)
1623 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1624 || TREE_CODE (value) == FIXED_CST
1625 || TREE_CODE (value) == VECTOR_CST
1626 || TREE_CODE (value) == COMPLEX_CST)
1627 && TREE_OVERFLOW (value))
1628 error ("overflow in constant expression");
1631 /* Print a warning if an expression had overflow in folding and its
1632 operands hadn't.
1634 Invoke this function on every expression that
1635 (1) appears in the source code, and
1636 (2) is a constant expression that overflowed, and
1637 (3) is not already checked by convert_and_check;
1638 however, do not invoke this function on operands of explicit casts
1639 or when the expression is the result of an operator and any operand
1640 already overflowed. */
1642 void
1643 overflow_warning (location_t loc, tree value)
1645 if (c_inhibit_evaluation_warnings != 0)
1646 return;
1648 switch (TREE_CODE (value))
1650 case INTEGER_CST:
1651 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1652 break;
1654 case REAL_CST:
1655 warning_at (loc, OPT_Woverflow,
1656 "floating point overflow in expression");
1657 break;
1659 case FIXED_CST:
1660 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1661 break;
1663 case VECTOR_CST:
1664 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1665 break;
1667 case COMPLEX_CST:
1668 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1669 warning_at (loc, OPT_Woverflow,
1670 "complex integer overflow in expression");
1671 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1672 warning_at (loc, OPT_Woverflow,
1673 "complex floating point overflow in expression");
1674 break;
1676 default:
1677 break;
1681 /* Warn about uses of logical || / && operator in a context where it
1682 is likely that the bitwise equivalent was intended by the
1683 programmer. We have seen an expression in which CODE is a binary
1684 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1685 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1686 void
1687 warn_logical_operator (location_t location, enum tree_code code, tree type,
1688 enum tree_code code_left, tree op_left,
1689 enum tree_code ARG_UNUSED (code_right), tree op_right)
1691 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1692 int in0_p, in1_p, in_p;
1693 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1694 bool strict_overflow_p = false;
1696 if (code != TRUTH_ANDIF_EXPR
1697 && code != TRUTH_AND_EXPR
1698 && code != TRUTH_ORIF_EXPR
1699 && code != TRUTH_OR_EXPR)
1700 return;
1702 /* Warn if &&/|| are being used in a context where it is
1703 likely that the bitwise equivalent was intended by the
1704 programmer. That is, an expression such as op && MASK
1705 where op should not be any boolean expression, nor a
1706 constant, and mask seems to be a non-boolean integer constant. */
1707 if (!truth_value_p (code_left)
1708 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1709 && !CONSTANT_CLASS_P (op_left)
1710 && !TREE_NO_WARNING (op_left)
1711 && TREE_CODE (op_right) == INTEGER_CST
1712 && !integer_zerop (op_right)
1713 && !integer_onep (op_right))
1715 if (or_op)
1716 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1717 " applied to non-boolean constant");
1718 else
1719 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1720 " applied to non-boolean constant");
1721 TREE_NO_WARNING (op_left) = true;
1722 return;
1725 /* We do not warn for constants because they are typical of macro
1726 expansions that test for features. */
1727 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1728 return;
1730 /* This warning only makes sense with logical operands. */
1731 if (!(truth_value_p (TREE_CODE (op_left))
1732 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1733 || !(truth_value_p (TREE_CODE (op_right))
1734 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1735 return;
1737 /* The range computations only work with scalars. */
1738 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1739 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1740 return;
1742 /* We first test whether either side separately is trivially true
1743 (with OR) or trivially false (with AND). If so, do not warn.
1744 This is a common idiom for testing ranges of data types in
1745 portable code. */
1746 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1747 if (!lhs)
1748 return;
1749 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1750 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1752 /* If this is an OR operation, invert both sides; now, the result
1753 should be always false to get a warning. */
1754 if (or_op)
1755 in0_p = !in0_p;
1757 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1758 if (tem && integer_zerop (tem))
1759 return;
1761 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1762 if (!rhs)
1763 return;
1764 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1765 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1767 /* If this is an OR operation, invert both sides; now, the result
1768 should be always false to get a warning. */
1769 if (or_op)
1770 in1_p = !in1_p;
1772 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1773 if (tem && integer_zerop (tem))
1774 return;
1776 /* If both expressions have the same operand, if we can merge the
1777 ranges, and if the range test is always false, then warn. */
1778 if (operand_equal_p (lhs, rhs, 0)
1779 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1780 in1_p, low1, high1)
1781 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1782 type, lhs, in_p, low, high))
1783 && integer_zerop (tem))
1785 if (or_op)
1786 warning_at (location, OPT_Wlogical_op,
1787 "logical %<or%> "
1788 "of collectively exhaustive tests is always true");
1789 else
1790 warning_at (location, OPT_Wlogical_op,
1791 "logical %<and%> "
1792 "of mutually exclusive tests is always false");
1796 /* Warn about logical not used on the left hand side operand of a comparison.
1797 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1798 Do not warn if RHS is of a boolean type. */
1800 void
1801 warn_logical_not_parentheses (location_t location, enum tree_code code,
1802 tree rhs)
1804 if (TREE_CODE_CLASS (code) != tcc_comparison
1805 || TREE_TYPE (rhs) == NULL_TREE
1806 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1807 return;
1809 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1810 !(x == 0) or !(y != 0). */
1811 if ((code == EQ_EXPR || code == NE_EXPR)
1812 && integer_zerop (rhs))
1813 return;
1815 warning_at (location, OPT_Wlogical_not_parentheses,
1816 "logical not is only applied to the left hand side of "
1817 "comparison");
1820 /* Warn if EXP contains any computations whose results are not used.
1821 Return true if a warning is printed; false otherwise. LOCUS is the
1822 (potential) location of the expression. */
1824 bool
1825 warn_if_unused_value (const_tree exp, location_t locus)
1827 restart:
1828 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1829 return false;
1831 /* Don't warn about void constructs. This includes casting to void,
1832 void function calls, and statement expressions with a final cast
1833 to void. */
1834 if (VOID_TYPE_P (TREE_TYPE (exp)))
1835 return false;
1837 if (EXPR_HAS_LOCATION (exp))
1838 locus = EXPR_LOCATION (exp);
1840 switch (TREE_CODE (exp))
1842 case PREINCREMENT_EXPR:
1843 case POSTINCREMENT_EXPR:
1844 case PREDECREMENT_EXPR:
1845 case POSTDECREMENT_EXPR:
1846 case MODIFY_EXPR:
1847 case INIT_EXPR:
1848 case TARGET_EXPR:
1849 case CALL_EXPR:
1850 case TRY_CATCH_EXPR:
1851 case WITH_CLEANUP_EXPR:
1852 case EXIT_EXPR:
1853 case VA_ARG_EXPR:
1854 return false;
1856 case BIND_EXPR:
1857 /* For a binding, warn if no side effect within it. */
1858 exp = BIND_EXPR_BODY (exp);
1859 goto restart;
1861 case SAVE_EXPR:
1862 case NON_LVALUE_EXPR:
1863 case NOP_EXPR:
1864 exp = TREE_OPERAND (exp, 0);
1865 goto restart;
1867 case TRUTH_ORIF_EXPR:
1868 case TRUTH_ANDIF_EXPR:
1869 /* In && or ||, warn if 2nd operand has no side effect. */
1870 exp = TREE_OPERAND (exp, 1);
1871 goto restart;
1873 case COMPOUND_EXPR:
1874 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1875 return true;
1876 /* Let people do `(foo (), 0)' without a warning. */
1877 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1878 return false;
1879 exp = TREE_OPERAND (exp, 1);
1880 goto restart;
1882 case COND_EXPR:
1883 /* If this is an expression with side effects, don't warn; this
1884 case commonly appears in macro expansions. */
1885 if (TREE_SIDE_EFFECTS (exp))
1886 return false;
1887 goto warn;
1889 case INDIRECT_REF:
1890 /* Don't warn about automatic dereferencing of references, since
1891 the user cannot control it. */
1892 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1894 exp = TREE_OPERAND (exp, 0);
1895 goto restart;
1897 /* Fall through. */
1899 default:
1900 /* Referencing a volatile value is a side effect, so don't warn. */
1901 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1902 && TREE_THIS_VOLATILE (exp))
1903 return false;
1905 /* If this is an expression which has no operands, there is no value
1906 to be unused. There are no such language-independent codes,
1907 but front ends may define such. */
1908 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1909 return false;
1911 warn:
1912 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1917 /* Print a warning about casts that might indicate violation
1918 of strict aliasing rules if -Wstrict-aliasing is used and
1919 strict aliasing mode is in effect. OTYPE is the original
1920 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1922 bool
1923 strict_aliasing_warning (tree otype, tree type, tree expr)
1925 /* Strip pointer conversion chains and get to the correct original type. */
1926 STRIP_NOPS (expr);
1927 otype = TREE_TYPE (expr);
1929 if (!(flag_strict_aliasing
1930 && POINTER_TYPE_P (type)
1931 && POINTER_TYPE_P (otype)
1932 && !VOID_TYPE_P (TREE_TYPE (type)))
1933 /* If the type we are casting to is a ref-all pointer
1934 dereferencing it is always valid. */
1935 || TYPE_REF_CAN_ALIAS_ALL (type))
1936 return false;
1938 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1939 && (DECL_P (TREE_OPERAND (expr, 0))
1940 || handled_component_p (TREE_OPERAND (expr, 0))))
1942 /* Casting the address of an object to non void pointer. Warn
1943 if the cast breaks type based aliasing. */
1944 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1946 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1947 "might break strict-aliasing rules");
1948 return true;
1950 else
1952 /* warn_strict_aliasing >= 3. This includes the default (3).
1953 Only warn if the cast is dereferenced immediately. */
1954 alias_set_type set1 =
1955 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1956 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1958 if (set1 != set2 && set2 != 0
1959 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1961 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1962 "pointer will break strict-aliasing rules");
1963 return true;
1965 else if (warn_strict_aliasing == 2
1966 && !alias_sets_must_conflict_p (set1, set2))
1968 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1969 "pointer might break strict-aliasing rules");
1970 return true;
1974 else
1975 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1977 /* At this level, warn for any conversions, even if an address is
1978 not taken in the same statement. This will likely produce many
1979 false positives, but could be useful to pinpoint problems that
1980 are not revealed at higher levels. */
1981 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1982 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1983 if (!COMPLETE_TYPE_P (type)
1984 || !alias_sets_must_conflict_p (set1, set2))
1986 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1987 "pointer might break strict-aliasing rules");
1988 return true;
1992 return false;
1995 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1996 sizeof as last operand of certain builtins. */
1998 void
1999 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
2000 vec<tree, va_gc> *params, tree *sizeof_arg,
2001 bool (*comp_types) (tree, tree))
2003 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
2004 bool strop = false, cmp = false;
2005 unsigned int idx = ~0;
2006 location_t loc;
2008 if (TREE_CODE (callee) != FUNCTION_DECL
2009 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2010 || vec_safe_length (params) <= 1)
2011 return;
2013 switch (DECL_FUNCTION_CODE (callee))
2015 case BUILT_IN_STRNCMP:
2016 case BUILT_IN_STRNCASECMP:
2017 cmp = true;
2018 /* FALLTHRU */
2019 case BUILT_IN_STRNCPY:
2020 case BUILT_IN_STRNCPY_CHK:
2021 case BUILT_IN_STRNCAT:
2022 case BUILT_IN_STRNCAT_CHK:
2023 case BUILT_IN_STPNCPY:
2024 case BUILT_IN_STPNCPY_CHK:
2025 strop = true;
2026 /* FALLTHRU */
2027 case BUILT_IN_MEMCPY:
2028 case BUILT_IN_MEMCPY_CHK:
2029 case BUILT_IN_MEMMOVE:
2030 case BUILT_IN_MEMMOVE_CHK:
2031 if (params->length () < 3)
2032 return;
2033 src = (*params)[1];
2034 dest = (*params)[0];
2035 idx = 2;
2036 break;
2037 case BUILT_IN_BCOPY:
2038 if (params->length () < 3)
2039 return;
2040 src = (*params)[0];
2041 dest = (*params)[1];
2042 idx = 2;
2043 break;
2044 case BUILT_IN_MEMCMP:
2045 case BUILT_IN_BCMP:
2046 if (params->length () < 3)
2047 return;
2048 src = (*params)[1];
2049 dest = (*params)[0];
2050 idx = 2;
2051 cmp = true;
2052 break;
2053 case BUILT_IN_MEMSET:
2054 case BUILT_IN_MEMSET_CHK:
2055 if (params->length () < 3)
2056 return;
2057 dest = (*params)[0];
2058 idx = 2;
2059 break;
2060 case BUILT_IN_BZERO:
2061 dest = (*params)[0];
2062 idx = 1;
2063 break;
2064 case BUILT_IN_STRNDUP:
2065 src = (*params)[0];
2066 strop = true;
2067 idx = 1;
2068 break;
2069 case BUILT_IN_MEMCHR:
2070 if (params->length () < 3)
2071 return;
2072 src = (*params)[0];
2073 idx = 2;
2074 break;
2075 case BUILT_IN_SNPRINTF:
2076 case BUILT_IN_SNPRINTF_CHK:
2077 case BUILT_IN_VSNPRINTF:
2078 case BUILT_IN_VSNPRINTF_CHK:
2079 dest = (*params)[0];
2080 idx = 1;
2081 strop = true;
2082 break;
2083 default:
2084 break;
2087 if (idx >= 3)
2088 return;
2090 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2091 return;
2093 type = TYPE_P (sizeof_arg[idx])
2094 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2095 if (!POINTER_TYPE_P (type))
2096 return;
2098 if (dest
2099 && (tem = tree_strip_nop_conversions (dest))
2100 && POINTER_TYPE_P (TREE_TYPE (tem))
2101 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2102 return;
2104 if (src
2105 && (tem = tree_strip_nop_conversions (src))
2106 && POINTER_TYPE_P (TREE_TYPE (tem))
2107 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2108 return;
2110 loc = sizeof_arg_loc[idx];
2112 if (dest && !cmp)
2114 if (!TYPE_P (sizeof_arg[idx])
2115 && operand_equal_p (dest, sizeof_arg[idx], 0)
2116 && comp_types (TREE_TYPE (dest), type))
2118 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2119 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2120 "argument to %<sizeof%> in %qD call is the same "
2121 "expression as the destination; did you mean to "
2122 "remove the addressof?", callee);
2123 else if ((TYPE_PRECISION (TREE_TYPE (type))
2124 == TYPE_PRECISION (char_type_node))
2125 || strop)
2126 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2127 "argument to %<sizeof%> in %qD call is the same "
2128 "expression as the destination; did you mean to "
2129 "provide an explicit length?", callee);
2130 else
2131 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2132 "argument to %<sizeof%> in %qD call is the same "
2133 "expression as the destination; did you mean to "
2134 "dereference it?", callee);
2135 return;
2138 if (POINTER_TYPE_P (TREE_TYPE (dest))
2139 && !strop
2140 && comp_types (TREE_TYPE (dest), type)
2141 && !VOID_TYPE_P (TREE_TYPE (type)))
2143 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2144 "argument to %<sizeof%> in %qD call is the same "
2145 "pointer type %qT as the destination; expected %qT "
2146 "or an explicit length", callee, TREE_TYPE (dest),
2147 TREE_TYPE (TREE_TYPE (dest)));
2148 return;
2152 if (src && !cmp)
2154 if (!TYPE_P (sizeof_arg[idx])
2155 && operand_equal_p (src, sizeof_arg[idx], 0)
2156 && comp_types (TREE_TYPE (src), type))
2158 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2159 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2160 "argument to %<sizeof%> in %qD call is the same "
2161 "expression as the source; did you mean to "
2162 "remove the addressof?", callee);
2163 else if ((TYPE_PRECISION (TREE_TYPE (type))
2164 == TYPE_PRECISION (char_type_node))
2165 || strop)
2166 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2167 "argument to %<sizeof%> in %qD call is the same "
2168 "expression as the source; did you mean to "
2169 "provide an explicit length?", callee);
2170 else
2171 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2172 "argument to %<sizeof%> in %qD call is the same "
2173 "expression as the source; did you mean to "
2174 "dereference it?", callee);
2175 return;
2178 if (POINTER_TYPE_P (TREE_TYPE (src))
2179 && !strop
2180 && comp_types (TREE_TYPE (src), type)
2181 && !VOID_TYPE_P (TREE_TYPE (type)))
2183 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2184 "argument to %<sizeof%> in %qD call is the same "
2185 "pointer type %qT as the source; expected %qT "
2186 "or an explicit length", callee, TREE_TYPE (src),
2187 TREE_TYPE (TREE_TYPE (src)));
2188 return;
2192 if (dest)
2194 if (!TYPE_P (sizeof_arg[idx])
2195 && operand_equal_p (dest, sizeof_arg[idx], 0)
2196 && comp_types (TREE_TYPE (dest), type))
2198 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2199 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2200 "argument to %<sizeof%> in %qD call is the same "
2201 "expression as the first source; did you mean to "
2202 "remove the addressof?", callee);
2203 else if ((TYPE_PRECISION (TREE_TYPE (type))
2204 == TYPE_PRECISION (char_type_node))
2205 || strop)
2206 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2207 "argument to %<sizeof%> in %qD call is the same "
2208 "expression as the first source; did you mean to "
2209 "provide an explicit length?", callee);
2210 else
2211 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2212 "argument to %<sizeof%> in %qD call is the same "
2213 "expression as the first source; did you mean to "
2214 "dereference it?", callee);
2215 return;
2218 if (POINTER_TYPE_P (TREE_TYPE (dest))
2219 && !strop
2220 && comp_types (TREE_TYPE (dest), type)
2221 && !VOID_TYPE_P (TREE_TYPE (type)))
2223 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2224 "argument to %<sizeof%> in %qD call is the same "
2225 "pointer type %qT as the first source; expected %qT "
2226 "or an explicit length", callee, TREE_TYPE (dest),
2227 TREE_TYPE (TREE_TYPE (dest)));
2228 return;
2232 if (src)
2234 if (!TYPE_P (sizeof_arg[idx])
2235 && operand_equal_p (src, sizeof_arg[idx], 0)
2236 && comp_types (TREE_TYPE (src), type))
2238 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2239 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2240 "argument to %<sizeof%> in %qD call is the same "
2241 "expression as the second source; did you mean to "
2242 "remove the addressof?", callee);
2243 else if ((TYPE_PRECISION (TREE_TYPE (type))
2244 == TYPE_PRECISION (char_type_node))
2245 || strop)
2246 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2247 "argument to %<sizeof%> in %qD call is the same "
2248 "expression as the second source; did you mean to "
2249 "provide an explicit length?", callee);
2250 else
2251 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2252 "argument to %<sizeof%> in %qD call is the same "
2253 "expression as the second source; did you mean to "
2254 "dereference it?", callee);
2255 return;
2258 if (POINTER_TYPE_P (TREE_TYPE (src))
2259 && !strop
2260 && comp_types (TREE_TYPE (src), type)
2261 && !VOID_TYPE_P (TREE_TYPE (type)))
2263 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2264 "argument to %<sizeof%> in %qD call is the same "
2265 "pointer type %qT as the second source; expected %qT "
2266 "or an explicit length", callee, TREE_TYPE (src),
2267 TREE_TYPE (TREE_TYPE (src)));
2268 return;
2274 /* Warn for unlikely, improbable, or stupid DECL declarations
2275 of `main'. */
2277 void
2278 check_main_parameter_types (tree decl)
2280 function_args_iterator iter;
2281 tree type;
2282 int argct = 0;
2284 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2286 /* XXX void_type_node belies the abstraction. */
2287 if (type == void_type_node || type == error_mark_node )
2288 break;
2290 tree t = type;
2291 if (TYPE_ATOMIC (t))
2292 pedwarn (input_location, OPT_Wmain,
2293 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2294 type, decl);
2295 while (POINTER_TYPE_P (t))
2297 t = TREE_TYPE (t);
2298 if (TYPE_ATOMIC (t))
2299 pedwarn (input_location, OPT_Wmain,
2300 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2301 type, decl);
2304 ++argct;
2305 switch (argct)
2307 case 1:
2308 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2309 pedwarn (input_location, OPT_Wmain,
2310 "first argument of %q+D should be %<int%>", decl);
2311 break;
2313 case 2:
2314 if (TREE_CODE (type) != POINTER_TYPE
2315 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2316 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2317 != char_type_node))
2318 pedwarn (input_location, OPT_Wmain,
2319 "second argument of %q+D should be %<char **%>", decl);
2320 break;
2322 case 3:
2323 if (TREE_CODE (type) != POINTER_TYPE
2324 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2325 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2326 != char_type_node))
2327 pedwarn (input_location, OPT_Wmain,
2328 "third argument of %q+D should probably be "
2329 "%<char **%>", decl);
2330 break;
2334 /* It is intentional that this message does not mention the third
2335 argument because it's only mentioned in an appendix of the
2336 standard. */
2337 if (argct > 0 && (argct < 2 || argct > 3))
2338 pedwarn (input_location, OPT_Wmain,
2339 "%q+D takes only zero or two arguments", decl);
2341 if (stdarg_p (TREE_TYPE (decl)))
2342 pedwarn (input_location, OPT_Wmain,
2343 "%q+D declared as variadic function", decl);
2346 /* vector_targets_convertible_p is used for vector pointer types. The
2347 callers perform various checks that the qualifiers are satisfactory,
2348 while OTOH vector_targets_convertible_p ignores the number of elements
2349 in the vectors. That's fine with vector pointers as we can consider,
2350 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2351 and that does not require and conversion of the pointer values.
2352 In contrast, vector_types_convertible_p and
2353 vector_types_compatible_elements_p are used for vector value types. */
2354 /* True if pointers to distinct types T1 and T2 can be converted to
2355 each other without an explicit cast. Only returns true for opaque
2356 vector types. */
2357 bool
2358 vector_targets_convertible_p (const_tree t1, const_tree t2)
2360 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2361 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2362 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2363 return true;
2365 return false;
2368 /* vector_types_convertible_p is used for vector value types.
2369 It could in principle call vector_targets_convertible_p as a subroutine,
2370 but then the check for vector type would be duplicated with its callers,
2371 and also the purpose of vector_targets_convertible_p would become
2372 muddled.
2373 Where vector_types_convertible_p returns true, a conversion might still be
2374 needed to make the types match.
2375 In contrast, vector_targets_convertible_p is used for vector pointer
2376 values, and vector_types_compatible_elements_p is used specifically
2377 in the context for binary operators, as a check if use is possible without
2378 conversion. */
2379 /* True if vector types T1 and T2 can be converted to each other
2380 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2381 can only be converted with -flax-vector-conversions yet that is not
2382 in effect, emit a note telling the user about that option if such
2383 a note has not previously been emitted. */
2384 bool
2385 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2387 static bool emitted_lax_note = false;
2388 bool convertible_lax;
2390 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2391 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2392 return true;
2394 convertible_lax =
2395 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2396 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2397 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2398 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2399 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2401 if (!convertible_lax || flag_lax_vector_conversions)
2402 return convertible_lax;
2404 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2405 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2406 return true;
2408 if (emit_lax_note && !emitted_lax_note)
2410 emitted_lax_note = true;
2411 inform (input_location, "use -flax-vector-conversions to permit "
2412 "conversions between vectors with differing "
2413 "element types or numbers of subparts");
2416 return false;
2419 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2420 and have vector types, V0 has the same type as V1, and the number of
2421 elements of V0, V1, MASK is the same.
2423 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2424 called with two arguments. In this case implementation passes the
2425 first argument twice in order to share the same tree code. This fact
2426 could enable the mask-values being twice the vector length. This is
2427 an implementation accident and this semantics is not guaranteed to
2428 the user. */
2429 tree
2430 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2431 bool complain)
2433 tree ret;
2434 bool wrap = true;
2435 bool maybe_const = false;
2436 bool two_arguments = false;
2438 if (v1 == NULL_TREE)
2440 two_arguments = true;
2441 v1 = v0;
2444 if (v0 == error_mark_node || v1 == error_mark_node
2445 || mask == error_mark_node)
2446 return error_mark_node;
2448 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2449 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2451 if (complain)
2452 error_at (loc, "__builtin_shuffle last argument must "
2453 "be an integer vector");
2454 return error_mark_node;
2457 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2458 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2460 if (complain)
2461 error_at (loc, "__builtin_shuffle arguments must be vectors");
2462 return error_mark_node;
2465 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2467 if (complain)
2468 error_at (loc, "__builtin_shuffle argument vectors must be of "
2469 "the same type");
2470 return error_mark_node;
2473 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2474 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2475 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2476 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2478 if (complain)
2479 error_at (loc, "__builtin_shuffle number of elements of the "
2480 "argument vector(s) and the mask vector should "
2481 "be the same");
2482 return error_mark_node;
2485 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2486 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2488 if (complain)
2489 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2490 "must have the same size as inner type of the mask");
2491 return error_mark_node;
2494 if (!c_dialect_cxx ())
2496 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2497 v0 = c_fully_fold (v0, false, &maybe_const);
2498 wrap &= maybe_const;
2500 if (two_arguments)
2501 v1 = v0 = save_expr (v0);
2502 else
2504 v1 = c_fully_fold (v1, false, &maybe_const);
2505 wrap &= maybe_const;
2508 mask = c_fully_fold (mask, false, &maybe_const);
2509 wrap &= maybe_const;
2511 else if (two_arguments)
2512 v1 = v0 = save_expr (v0);
2514 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2516 if (!c_dialect_cxx () && !wrap)
2517 ret = c_wrap_maybe_const (ret, true);
2519 return ret;
2522 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2523 to integral type. */
2525 static tree
2526 c_common_get_narrower (tree op, int *unsignedp_ptr)
2528 op = get_narrower (op, unsignedp_ptr);
2530 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2531 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2533 /* C++0x scoped enumerations don't implicitly convert to integral
2534 type; if we stripped an explicit conversion to a larger type we
2535 need to replace it so common_type will still work. */
2536 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2537 TYPE_UNSIGNED (TREE_TYPE (op)));
2538 op = fold_convert (type, op);
2540 return op;
2543 /* This is a helper function of build_binary_op.
2545 For certain operations if both args were extended from the same
2546 smaller type, do the arithmetic in that type and then extend.
2548 BITWISE indicates a bitwise operation.
2549 For them, this optimization is safe only if
2550 both args are zero-extended or both are sign-extended.
2551 Otherwise, we might change the result.
2552 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2553 but calculated in (unsigned short) it would be (unsigned short)-1.
2555 tree
2556 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2558 int unsigned0, unsigned1;
2559 tree arg0, arg1;
2560 int uns;
2561 tree type;
2563 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2564 excessive narrowing when we call get_narrower below. For
2565 example, suppose that OP0 is of unsigned int extended
2566 from signed char and that RESULT_TYPE is long long int.
2567 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2568 like
2570 (long long int) (unsigned int) signed_char
2572 which get_narrower would narrow down to
2574 (unsigned int) signed char
2576 If we do not cast OP0 first, get_narrower would return
2577 signed_char, which is inconsistent with the case of the
2578 explicit cast. */
2579 op0 = convert (result_type, op0);
2580 op1 = convert (result_type, op1);
2582 arg0 = c_common_get_narrower (op0, &unsigned0);
2583 arg1 = c_common_get_narrower (op1, &unsigned1);
2585 /* UNS is 1 if the operation to be done is an unsigned one. */
2586 uns = TYPE_UNSIGNED (result_type);
2588 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2589 but it *requires* conversion to FINAL_TYPE. */
2591 if ((TYPE_PRECISION (TREE_TYPE (op0))
2592 == TYPE_PRECISION (TREE_TYPE (arg0)))
2593 && TREE_TYPE (op0) != result_type)
2594 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2595 if ((TYPE_PRECISION (TREE_TYPE (op1))
2596 == TYPE_PRECISION (TREE_TYPE (arg1)))
2597 && TREE_TYPE (op1) != result_type)
2598 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2600 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2602 /* For bitwise operations, signedness of nominal type
2603 does not matter. Consider only how operands were extended. */
2604 if (bitwise)
2605 uns = unsigned0;
2607 /* Note that in all three cases below we refrain from optimizing
2608 an unsigned operation on sign-extended args.
2609 That would not be valid. */
2611 /* Both args variable: if both extended in same way
2612 from same width, do it in that width.
2613 Do it unsigned if args were zero-extended. */
2614 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2615 < TYPE_PRECISION (result_type))
2616 && (TYPE_PRECISION (TREE_TYPE (arg1))
2617 == TYPE_PRECISION (TREE_TYPE (arg0)))
2618 && unsigned0 == unsigned1
2619 && (unsigned0 || !uns))
2620 return c_common_signed_or_unsigned_type
2621 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2623 else if (TREE_CODE (arg0) == INTEGER_CST
2624 && (unsigned1 || !uns)
2625 && (TYPE_PRECISION (TREE_TYPE (arg1))
2626 < TYPE_PRECISION (result_type))
2627 && (type
2628 = c_common_signed_or_unsigned_type (unsigned1,
2629 TREE_TYPE (arg1)))
2630 && !POINTER_TYPE_P (type)
2631 && int_fits_type_p (arg0, type))
2632 return type;
2634 else if (TREE_CODE (arg1) == INTEGER_CST
2635 && (unsigned0 || !uns)
2636 && (TYPE_PRECISION (TREE_TYPE (arg0))
2637 < TYPE_PRECISION (result_type))
2638 && (type
2639 = c_common_signed_or_unsigned_type (unsigned0,
2640 TREE_TYPE (arg0)))
2641 && !POINTER_TYPE_P (type)
2642 && int_fits_type_p (arg1, type))
2643 return type;
2645 return result_type;
2648 /* Checks if expression EXPR of real/integer type cannot be converted
2649 to the real/integer type TYPE. Function returns non-zero when:
2650 * EXPR is a constant which cannot be exactly converted to TYPE.
2651 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2652 for EXPR type and TYPE being both integers or both real.
2653 * EXPR is not a constant of real type and TYPE is an integer.
2654 * EXPR is not a constant of integer type which cannot be
2655 exactly converted to real type.
2656 Function allows conversions between types of different signedness and
2657 can return SAFE_CONVERSION (zero) in that case. Function can produce
2658 signedness warnings if PRODUCE_WARNS is true. */
2660 enum conversion_safety
2661 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2663 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2664 tree expr_type = TREE_TYPE (expr);
2665 loc = expansion_point_location_if_in_system_header (loc);
2667 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2669 /* Warn for real constant that is not an exact integer converted
2670 to integer type. */
2671 if (TREE_CODE (expr_type) == REAL_TYPE
2672 && TREE_CODE (type) == INTEGER_TYPE)
2674 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2675 give_warning = UNSAFE_REAL;
2677 /* Warn for an integer constant that does not fit into integer type. */
2678 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2679 && TREE_CODE (type) == INTEGER_TYPE
2680 && !int_fits_type_p (expr, type))
2682 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2683 && tree_int_cst_sgn (expr) < 0)
2685 if (produce_warns)
2686 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2687 " implicitly converted to unsigned type");
2689 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2691 if (produce_warns)
2692 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2693 " constant value to negative integer");
2695 else
2696 give_warning = UNSAFE_OTHER;
2698 else if (TREE_CODE (type) == REAL_TYPE)
2700 /* Warn for an integer constant that does not fit into real type. */
2701 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2703 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2704 if (!exact_real_truncate (TYPE_MODE (type), &a))
2705 give_warning = UNSAFE_REAL;
2707 /* Warn for a real constant that does not fit into a smaller
2708 real type. */
2709 else if (TREE_CODE (expr_type) == REAL_TYPE
2710 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2712 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2713 if (!exact_real_truncate (TYPE_MODE (type), &a))
2714 give_warning = UNSAFE_REAL;
2718 else
2720 /* Warn for real types converted to integer types. */
2721 if (TREE_CODE (expr_type) == REAL_TYPE
2722 && TREE_CODE (type) == INTEGER_TYPE)
2723 give_warning = UNSAFE_REAL;
2725 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2726 && TREE_CODE (type) == INTEGER_TYPE)
2728 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2729 expr = get_unwidened (expr, 0);
2730 expr_type = TREE_TYPE (expr);
2732 /* Don't warn for short y; short x = ((int)y & 0xff); */
2733 if (TREE_CODE (expr) == BIT_AND_EXPR
2734 || TREE_CODE (expr) == BIT_IOR_EXPR
2735 || TREE_CODE (expr) == BIT_XOR_EXPR)
2737 /* If both args were extended from a shortest type,
2738 use that type if that is safe. */
2739 expr_type = shorten_binary_op (expr_type,
2740 TREE_OPERAND (expr, 0),
2741 TREE_OPERAND (expr, 1),
2742 /* bitwise */1);
2744 if (TREE_CODE (expr) == BIT_AND_EXPR)
2746 tree op0 = TREE_OPERAND (expr, 0);
2747 tree op1 = TREE_OPERAND (expr, 1);
2748 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2749 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2751 /* If one of the operands is a non-negative constant
2752 that fits in the target type, then the type of the
2753 other operand does not matter. */
2754 if ((TREE_CODE (op0) == INTEGER_CST
2755 && int_fits_type_p (op0, c_common_signed_type (type))
2756 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2757 || (TREE_CODE (op1) == INTEGER_CST
2758 && int_fits_type_p (op1, c_common_signed_type (type))
2759 && int_fits_type_p (op1,
2760 c_common_unsigned_type (type))))
2761 return SAFE_CONVERSION;
2762 /* If constant is unsigned and fits in the target
2763 type, then the result will also fit. */
2764 else if ((TREE_CODE (op0) == INTEGER_CST
2765 && unsigned0
2766 && int_fits_type_p (op0, type))
2767 || (TREE_CODE (op1) == INTEGER_CST
2768 && unsigned1
2769 && int_fits_type_p (op1, type)))
2770 return SAFE_CONVERSION;
2773 /* Warn for integer types converted to smaller integer types. */
2774 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2775 give_warning = UNSAFE_OTHER;
2777 /* When they are the same width but different signedness,
2778 then the value may change. */
2779 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2780 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2781 /* Even when converted to a bigger type, if the type is
2782 unsigned but expr is signed, then negative values
2783 will be changed. */
2784 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2785 && produce_warns)
2786 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2787 "may change the sign of the result",
2788 type, expr_type);
2791 /* Warn for integer types converted to real types if and only if
2792 all the range of values of the integer type cannot be
2793 represented by the real type. */
2794 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2795 && TREE_CODE (type) == REAL_TYPE)
2797 tree type_low_bound, type_high_bound;
2798 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2800 /* Don't warn about char y = 0xff; float x = (int) y; */
2801 expr = get_unwidened (expr, 0);
2802 expr_type = TREE_TYPE (expr);
2804 type_low_bound = TYPE_MIN_VALUE (expr_type);
2805 type_high_bound = TYPE_MAX_VALUE (expr_type);
2806 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2807 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2809 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2810 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2811 give_warning = UNSAFE_OTHER;
2814 /* Warn for real types converted to smaller real types. */
2815 else if (TREE_CODE (expr_type) == REAL_TYPE
2816 && TREE_CODE (type) == REAL_TYPE
2817 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2818 give_warning = UNSAFE_REAL;
2821 return give_warning;
2824 /* Warns if the conversion of EXPR to TYPE may alter a value.
2825 This is a helper function for warnings_for_convert_and_check. */
2827 static void
2828 conversion_warning (location_t loc, tree type, tree expr)
2830 tree expr_type = TREE_TYPE (expr);
2831 enum conversion_safety conversion_kind;
2833 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2834 return;
2836 /* This may happen, because for LHS op= RHS we preevaluate
2837 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2838 means we could no longer see the code of the EXPR. */
2839 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2840 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2841 if (TREE_CODE (expr) == SAVE_EXPR)
2842 expr = TREE_OPERAND (expr, 0);
2844 switch (TREE_CODE (expr))
2846 case EQ_EXPR:
2847 case NE_EXPR:
2848 case LE_EXPR:
2849 case GE_EXPR:
2850 case LT_EXPR:
2851 case GT_EXPR:
2852 case TRUTH_ANDIF_EXPR:
2853 case TRUTH_ORIF_EXPR:
2854 case TRUTH_AND_EXPR:
2855 case TRUTH_OR_EXPR:
2856 case TRUTH_XOR_EXPR:
2857 case TRUTH_NOT_EXPR:
2858 /* Conversion from boolean to a signed:1 bit-field (which only
2859 can hold the values 0 and -1) doesn't lose information - but
2860 it does change the value. */
2861 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2862 warning_at (loc, OPT_Wconversion,
2863 "conversion to %qT from boolean expression", type);
2864 return;
2866 case REAL_CST:
2867 case INTEGER_CST:
2868 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2869 if (conversion_kind == UNSAFE_REAL)
2870 warning_at (loc, OPT_Wfloat_conversion,
2871 "conversion to %qT alters %qT constant value",
2872 type, expr_type);
2873 else if (conversion_kind)
2874 warning_at (loc, OPT_Wconversion,
2875 "conversion to %qT alters %qT constant value",
2876 type, expr_type);
2877 return;
2879 case COND_EXPR:
2881 /* In case of COND_EXPR, we do not care about the type of
2882 COND_EXPR, only about the conversion of each operand. */
2883 tree op1 = TREE_OPERAND (expr, 1);
2884 tree op2 = TREE_OPERAND (expr, 2);
2886 conversion_warning (loc, type, op1);
2887 conversion_warning (loc, type, op2);
2888 return;
2891 default: /* 'expr' is not a constant. */
2892 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2893 if (conversion_kind == UNSAFE_REAL)
2894 warning_at (loc, OPT_Wfloat_conversion,
2895 "conversion to %qT from %qT may alter its value",
2896 type, expr_type);
2897 else if (conversion_kind)
2898 warning_at (loc, OPT_Wconversion,
2899 "conversion to %qT from %qT may alter its value",
2900 type, expr_type);
2904 /* Produce warnings after a conversion. RESULT is the result of
2905 converting EXPR to TYPE. This is a helper function for
2906 convert_and_check and cp_convert_and_check. */
2908 void
2909 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2910 tree result)
2912 loc = expansion_point_location_if_in_system_header (loc);
2914 if (TREE_CODE (expr) == INTEGER_CST
2915 && (TREE_CODE (type) == INTEGER_TYPE
2916 || TREE_CODE (type) == ENUMERAL_TYPE)
2917 && !int_fits_type_p (expr, type))
2919 /* Do not diagnose overflow in a constant expression merely
2920 because a conversion overflowed. */
2921 if (TREE_OVERFLOW (result))
2922 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2924 if (TYPE_UNSIGNED (type))
2926 /* This detects cases like converting -129 or 256 to
2927 unsigned char. */
2928 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2929 warning_at (loc, OPT_Woverflow,
2930 "large integer implicitly truncated to unsigned type");
2931 else
2932 conversion_warning (loc, type, expr);
2934 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2935 warning_at (loc, OPT_Woverflow,
2936 "overflow in implicit constant conversion");
2937 /* No warning for converting 0x80000000 to int. */
2938 else if (pedantic
2939 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2940 || TYPE_PRECISION (TREE_TYPE (expr))
2941 != TYPE_PRECISION (type)))
2942 warning_at (loc, OPT_Woverflow,
2943 "overflow in implicit constant conversion");
2945 else
2946 conversion_warning (loc, type, expr);
2948 else if ((TREE_CODE (result) == INTEGER_CST
2949 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2950 warning_at (loc, OPT_Woverflow,
2951 "overflow in implicit constant conversion");
2952 else
2953 conversion_warning (loc, type, expr);
2957 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2958 Invoke this function on every expression that is converted implicitly,
2959 i.e. because of language rules and not because of an explicit cast. */
2961 tree
2962 convert_and_check (location_t loc, tree type, tree expr)
2964 tree result;
2965 tree expr_for_warning;
2967 /* Convert from a value with possible excess precision rather than
2968 via the semantic type, but do not warn about values not fitting
2969 exactly in the semantic type. */
2970 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2972 tree orig_type = TREE_TYPE (expr);
2973 expr = TREE_OPERAND (expr, 0);
2974 expr_for_warning = convert (orig_type, expr);
2975 if (orig_type == type)
2976 return expr_for_warning;
2978 else
2979 expr_for_warning = expr;
2981 if (TREE_TYPE (expr) == type)
2982 return expr;
2984 result = convert (type, expr);
2986 if (c_inhibit_evaluation_warnings == 0
2987 && !TREE_OVERFLOW_P (expr)
2988 && result != error_mark_node)
2989 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2991 return result;
2994 /* A node in a list that describes references to variables (EXPR), which are
2995 either read accesses if WRITER is zero, or write accesses, in which case
2996 WRITER is the parent of EXPR. */
2997 struct tlist
2999 struct tlist *next;
3000 tree expr, writer;
3003 /* Used to implement a cache the results of a call to verify_tree. We only
3004 use this for SAVE_EXPRs. */
3005 struct tlist_cache
3007 struct tlist_cache *next;
3008 struct tlist *cache_before_sp;
3009 struct tlist *cache_after_sp;
3010 tree expr;
3013 /* Obstack to use when allocating tlist structures, and corresponding
3014 firstobj. */
3015 static struct obstack tlist_obstack;
3016 static char *tlist_firstobj = 0;
3018 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3019 warnings. */
3020 static struct tlist *warned_ids;
3021 /* SAVE_EXPRs need special treatment. We process them only once and then
3022 cache the results. */
3023 static struct tlist_cache *save_expr_cache;
3025 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3026 static void merge_tlist (struct tlist **, struct tlist *, int);
3027 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3028 static int warning_candidate_p (tree);
3029 static bool candidate_equal_p (const_tree, const_tree);
3030 static void warn_for_collisions (struct tlist *);
3031 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3032 static struct tlist *new_tlist (struct tlist *, tree, tree);
3034 /* Create a new struct tlist and fill in its fields. */
3035 static struct tlist *
3036 new_tlist (struct tlist *next, tree t, tree writer)
3038 struct tlist *l;
3039 l = XOBNEW (&tlist_obstack, struct tlist);
3040 l->next = next;
3041 l->expr = t;
3042 l->writer = writer;
3043 return l;
3046 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3047 is nonnull, we ignore any node we find which has a writer equal to it. */
3049 static void
3050 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3052 while (add)
3054 struct tlist *next = add->next;
3055 if (!copy)
3056 add->next = *to;
3057 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3058 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3059 add = next;
3063 /* Merge the nodes of ADD into TO. This merging process is done so that for
3064 each variable that already exists in TO, no new node is added; however if
3065 there is a write access recorded in ADD, and an occurrence on TO is only
3066 a read access, then the occurrence in TO will be modified to record the
3067 write. */
3069 static void
3070 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3072 struct tlist **end = to;
3074 while (*end)
3075 end = &(*end)->next;
3077 while (add)
3079 int found = 0;
3080 struct tlist *tmp2;
3081 struct tlist *next = add->next;
3083 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3084 if (candidate_equal_p (tmp2->expr, add->expr))
3086 found = 1;
3087 if (!tmp2->writer)
3088 tmp2->writer = add->writer;
3090 if (!found)
3092 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3093 end = &(*end)->next;
3094 *end = 0;
3096 add = next;
3100 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3101 references in list LIST conflict with it, excluding reads if ONLY writers
3102 is nonzero. */
3104 static void
3105 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3106 int only_writes)
3108 struct tlist *tmp;
3110 /* Avoid duplicate warnings. */
3111 for (tmp = warned_ids; tmp; tmp = tmp->next)
3112 if (candidate_equal_p (tmp->expr, written))
3113 return;
3115 while (list)
3117 if (candidate_equal_p (list->expr, written)
3118 && !candidate_equal_p (list->writer, writer)
3119 && (!only_writes || list->writer))
3121 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3122 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3123 OPT_Wsequence_point, "operation on %qE may be undefined",
3124 list->expr);
3126 list = list->next;
3130 /* Given a list LIST of references to variables, find whether any of these
3131 can cause conflicts due to missing sequence points. */
3133 static void
3134 warn_for_collisions (struct tlist *list)
3136 struct tlist *tmp;
3138 for (tmp = list; tmp; tmp = tmp->next)
3140 if (tmp->writer)
3141 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3145 /* Return nonzero if X is a tree that can be verified by the sequence point
3146 warnings. */
3147 static int
3148 warning_candidate_p (tree x)
3150 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3151 return 0;
3153 if (TREE_CODE (x) == BLOCK)
3154 return 0;
3156 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3157 (lvalue_p) crash on TRY/CATCH. */
3158 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3159 return 0;
3161 if (!lvalue_p (x))
3162 return 0;
3164 /* No point to track non-const calls, they will never satisfy
3165 operand_equal_p. */
3166 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3167 return 0;
3169 if (TREE_CODE (x) == STRING_CST)
3170 return 0;
3172 return 1;
3175 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3176 static bool
3177 candidate_equal_p (const_tree x, const_tree y)
3179 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3182 /* Walk the tree X, and record accesses to variables. If X is written by the
3183 parent tree, WRITER is the parent.
3184 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3185 expression or its only operand forces a sequence point, then everything up
3186 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3187 in PNO_SP.
3188 Once we return, we will have emitted warnings if any subexpression before
3189 such a sequence point could be undefined. On a higher level, however, the
3190 sequence point may not be relevant, and we'll merge the two lists.
3192 Example: (b++, a) + b;
3193 The call that processes the COMPOUND_EXPR will store the increment of B
3194 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3195 processes the PLUS_EXPR will need to merge the two lists so that
3196 eventually, all accesses end up on the same list (and we'll warn about the
3197 unordered subexpressions b++ and b.
3199 A note on merging. If we modify the former example so that our expression
3200 becomes
3201 (b++, b) + a
3202 care must be taken not simply to add all three expressions into the final
3203 PNO_SP list. The function merge_tlist takes care of that by merging the
3204 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3205 way, so that no more than one access to B is recorded. */
3207 static void
3208 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3209 tree writer)
3211 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3212 enum tree_code code;
3213 enum tree_code_class cl;
3215 /* X may be NULL if it is the operand of an empty statement expression
3216 ({ }). */
3217 if (x == NULL)
3218 return;
3220 restart:
3221 code = TREE_CODE (x);
3222 cl = TREE_CODE_CLASS (code);
3224 if (warning_candidate_p (x))
3225 *pno_sp = new_tlist (*pno_sp, x, writer);
3227 switch (code)
3229 case CONSTRUCTOR:
3230 case SIZEOF_EXPR:
3231 return;
3233 case COMPOUND_EXPR:
3234 case TRUTH_ANDIF_EXPR:
3235 case TRUTH_ORIF_EXPR:
3236 tmp_before = tmp_nosp = tmp_list3 = 0;
3237 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3238 warn_for_collisions (tmp_nosp);
3239 merge_tlist (pbefore_sp, tmp_before, 0);
3240 merge_tlist (pbefore_sp, tmp_nosp, 0);
3241 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3242 merge_tlist (pbefore_sp, tmp_list3, 0);
3243 return;
3245 case COND_EXPR:
3246 tmp_before = tmp_list2 = 0;
3247 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3248 warn_for_collisions (tmp_list2);
3249 merge_tlist (pbefore_sp, tmp_before, 0);
3250 merge_tlist (pbefore_sp, tmp_list2, 0);
3252 tmp_list3 = tmp_nosp = 0;
3253 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3254 warn_for_collisions (tmp_nosp);
3255 merge_tlist (pbefore_sp, tmp_list3, 0);
3257 tmp_list3 = tmp_list2 = 0;
3258 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3259 warn_for_collisions (tmp_list2);
3260 merge_tlist (pbefore_sp, tmp_list3, 0);
3261 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3262 two first, to avoid warning for (a ? b++ : b++). */
3263 merge_tlist (&tmp_nosp, tmp_list2, 0);
3264 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3265 return;
3267 case PREDECREMENT_EXPR:
3268 case PREINCREMENT_EXPR:
3269 case POSTDECREMENT_EXPR:
3270 case POSTINCREMENT_EXPR:
3271 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3272 return;
3274 case MODIFY_EXPR:
3275 tmp_before = tmp_nosp = tmp_list3 = 0;
3276 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3277 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3278 /* Expressions inside the LHS are not ordered wrt. the sequence points
3279 in the RHS. Example:
3280 *a = (a++, 2)
3281 Despite the fact that the modification of "a" is in the before_sp
3282 list (tmp_before), it conflicts with the use of "a" in the LHS.
3283 We can handle this by adding the contents of tmp_list3
3284 to those of tmp_before, and redoing the collision warnings for that
3285 list. */
3286 add_tlist (&tmp_before, tmp_list3, x, 1);
3287 warn_for_collisions (tmp_before);
3288 /* Exclude the LHS itself here; we first have to merge it into the
3289 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3290 didn't exclude the LHS, we'd get it twice, once as a read and once
3291 as a write. */
3292 add_tlist (pno_sp, tmp_list3, x, 0);
3293 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3295 merge_tlist (pbefore_sp, tmp_before, 0);
3296 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3297 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3298 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3299 return;
3301 case CALL_EXPR:
3302 /* We need to warn about conflicts among arguments and conflicts between
3303 args and the function address. Side effects of the function address,
3304 however, are not ordered by the sequence point of the call. */
3306 call_expr_arg_iterator iter;
3307 tree arg;
3308 tmp_before = tmp_nosp = 0;
3309 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3310 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3312 tmp_list2 = tmp_list3 = 0;
3313 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3314 merge_tlist (&tmp_list3, tmp_list2, 0);
3315 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3317 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3318 warn_for_collisions (tmp_before);
3319 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3320 return;
3323 case TREE_LIST:
3324 /* Scan all the list, e.g. indices of multi dimensional array. */
3325 while (x)
3327 tmp_before = tmp_nosp = 0;
3328 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3329 merge_tlist (&tmp_nosp, tmp_before, 0);
3330 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3331 x = TREE_CHAIN (x);
3333 return;
3335 case SAVE_EXPR:
3337 struct tlist_cache *t;
3338 for (t = save_expr_cache; t; t = t->next)
3339 if (candidate_equal_p (t->expr, x))
3340 break;
3342 if (!t)
3344 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3345 t->next = save_expr_cache;
3346 t->expr = x;
3347 save_expr_cache = t;
3349 tmp_before = tmp_nosp = 0;
3350 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3351 warn_for_collisions (tmp_nosp);
3353 tmp_list3 = 0;
3354 merge_tlist (&tmp_list3, tmp_nosp, 0);
3355 t->cache_before_sp = tmp_before;
3356 t->cache_after_sp = tmp_list3;
3358 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3359 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3360 return;
3363 case ADDR_EXPR:
3364 x = TREE_OPERAND (x, 0);
3365 if (DECL_P (x))
3366 return;
3367 writer = 0;
3368 goto restart;
3370 default:
3371 /* For other expressions, simply recurse on their operands.
3372 Manual tail recursion for unary expressions.
3373 Other non-expressions need not be processed. */
3374 if (cl == tcc_unary)
3376 x = TREE_OPERAND (x, 0);
3377 writer = 0;
3378 goto restart;
3380 else if (IS_EXPR_CODE_CLASS (cl))
3382 int lp;
3383 int max = TREE_OPERAND_LENGTH (x);
3384 for (lp = 0; lp < max; lp++)
3386 tmp_before = tmp_nosp = 0;
3387 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3388 merge_tlist (&tmp_nosp, tmp_before, 0);
3389 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3392 return;
3396 /* Try to warn for undefined behavior in EXPR due to missing sequence
3397 points. */
3399 DEBUG_FUNCTION void
3400 verify_sequence_points (tree expr)
3402 struct tlist *before_sp = 0, *after_sp = 0;
3404 warned_ids = 0;
3405 save_expr_cache = 0;
3406 if (tlist_firstobj == 0)
3408 gcc_obstack_init (&tlist_obstack);
3409 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3412 verify_tree (expr, &before_sp, &after_sp, 0);
3413 warn_for_collisions (after_sp);
3414 obstack_free (&tlist_obstack, tlist_firstobj);
3417 /* Validate the expression after `case' and apply default promotions. */
3419 static tree
3420 check_case_value (location_t loc, tree value)
3422 if (value == NULL_TREE)
3423 return value;
3425 if (TREE_CODE (value) == INTEGER_CST)
3426 /* Promote char or short to int. */
3427 value = perform_integral_promotions (value);
3428 else if (value != error_mark_node)
3430 error_at (loc, "case label does not reduce to an integer constant");
3431 value = error_mark_node;
3434 constant_expression_warning (value);
3436 return value;
3439 /* See if the case values LOW and HIGH are in the range of the original
3440 type (i.e. before the default conversion to int) of the switch testing
3441 expression.
3442 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3443 the type before promoting it. CASE_LOW_P is a pointer to the lower
3444 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3445 if the case is not a case range.
3446 The caller has to make sure that we are not called with NULL for
3447 CASE_LOW_P (i.e. the default case).
3448 Returns true if the case label is in range of ORIG_TYPE (saturated or
3449 untouched) or false if the label is out of range. */
3451 static bool
3452 check_case_bounds (location_t loc, tree type, tree orig_type,
3453 tree *case_low_p, tree *case_high_p)
3455 tree min_value, max_value;
3456 tree case_low = *case_low_p;
3457 tree case_high = case_high_p ? *case_high_p : case_low;
3459 /* If there was a problem with the original type, do nothing. */
3460 if (orig_type == error_mark_node)
3461 return true;
3463 min_value = TYPE_MIN_VALUE (orig_type);
3464 max_value = TYPE_MAX_VALUE (orig_type);
3466 /* Case label is less than minimum for type. */
3467 if (tree_int_cst_compare (case_low, min_value) < 0
3468 && tree_int_cst_compare (case_high, min_value) < 0)
3470 warning_at (loc, 0, "case label value is less than minimum value "
3471 "for type");
3472 return false;
3475 /* Case value is greater than maximum for type. */
3476 if (tree_int_cst_compare (case_low, max_value) > 0
3477 && tree_int_cst_compare (case_high, max_value) > 0)
3479 warning_at (loc, 0, "case label value exceeds maximum value for type");
3480 return false;
3483 /* Saturate lower case label value to minimum. */
3484 if (tree_int_cst_compare (case_high, min_value) >= 0
3485 && tree_int_cst_compare (case_low, min_value) < 0)
3487 warning_at (loc, 0, "lower value in case label range"
3488 " less than minimum value for type");
3489 case_low = min_value;
3492 /* Saturate upper case label value to maximum. */
3493 if (tree_int_cst_compare (case_low, max_value) <= 0
3494 && tree_int_cst_compare (case_high, max_value) > 0)
3496 warning_at (loc, 0, "upper value in case label range"
3497 " exceeds maximum value for type");
3498 case_high = max_value;
3501 if (*case_low_p != case_low)
3502 *case_low_p = convert (type, case_low);
3503 if (case_high_p && *case_high_p != case_high)
3504 *case_high_p = convert (type, case_high);
3506 return true;
3509 /* Return an integer type with BITS bits of precision,
3510 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3512 tree
3513 c_common_type_for_size (unsigned int bits, int unsignedp)
3515 int i;
3517 if (bits == TYPE_PRECISION (integer_type_node))
3518 return unsignedp ? unsigned_type_node : integer_type_node;
3520 if (bits == TYPE_PRECISION (signed_char_type_node))
3521 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3523 if (bits == TYPE_PRECISION (short_integer_type_node))
3524 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3526 if (bits == TYPE_PRECISION (long_integer_type_node))
3527 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3529 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3530 return (unsignedp ? long_long_unsigned_type_node
3531 : long_long_integer_type_node);
3533 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3534 if (int_n_enabled_p[i]
3535 && bits == int_n_data[i].bitsize)
3536 return (unsignedp ? int_n_trees[i].unsigned_type
3537 : int_n_trees[i].signed_type);
3539 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3540 return (unsignedp ? widest_unsigned_literal_type_node
3541 : widest_integer_literal_type_node);
3543 if (bits <= TYPE_PRECISION (intQI_type_node))
3544 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3546 if (bits <= TYPE_PRECISION (intHI_type_node))
3547 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3549 if (bits <= TYPE_PRECISION (intSI_type_node))
3550 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3552 if (bits <= TYPE_PRECISION (intDI_type_node))
3553 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3555 return 0;
3558 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3559 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3560 and saturating if SATP is nonzero, otherwise not saturating. */
3562 tree
3563 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3564 int unsignedp, int satp)
3566 machine_mode mode;
3567 if (ibit == 0)
3568 mode = unsignedp ? UQQmode : QQmode;
3569 else
3570 mode = unsignedp ? UHAmode : HAmode;
3572 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3573 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3574 break;
3576 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3578 sorry ("GCC cannot support operators with integer types and "
3579 "fixed-point types that have too many integral and "
3580 "fractional bits together");
3581 return 0;
3584 return c_common_type_for_mode (mode, satp);
3587 /* Used for communication between c_common_type_for_mode and
3588 c_register_builtin_type. */
3589 tree registered_builtin_types;
3591 /* Return a data type that has machine mode MODE.
3592 If the mode is an integer,
3593 then UNSIGNEDP selects between signed and unsigned types.
3594 If the mode is a fixed-point mode,
3595 then UNSIGNEDP selects between saturating and nonsaturating types. */
3597 tree
3598 c_common_type_for_mode (machine_mode mode, int unsignedp)
3600 tree t;
3601 int i;
3603 if (mode == TYPE_MODE (integer_type_node))
3604 return unsignedp ? unsigned_type_node : integer_type_node;
3606 if (mode == TYPE_MODE (signed_char_type_node))
3607 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3609 if (mode == TYPE_MODE (short_integer_type_node))
3610 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3612 if (mode == TYPE_MODE (long_integer_type_node))
3613 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3615 if (mode == TYPE_MODE (long_long_integer_type_node))
3616 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3618 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3619 if (int_n_enabled_p[i]
3620 && mode == int_n_data[i].m)
3621 return (unsignedp ? int_n_trees[i].unsigned_type
3622 : int_n_trees[i].signed_type);
3624 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3625 return unsignedp ? widest_unsigned_literal_type_node
3626 : widest_integer_literal_type_node;
3628 if (mode == QImode)
3629 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3631 if (mode == HImode)
3632 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3634 if (mode == SImode)
3635 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3637 if (mode == DImode)
3638 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3640 #if HOST_BITS_PER_WIDE_INT >= 64
3641 if (mode == TYPE_MODE (intTI_type_node))
3642 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3643 #endif
3645 if (mode == TYPE_MODE (float_type_node))
3646 return float_type_node;
3648 if (mode == TYPE_MODE (double_type_node))
3649 return double_type_node;
3651 if (mode == TYPE_MODE (long_double_type_node))
3652 return long_double_type_node;
3654 if (mode == TYPE_MODE (void_type_node))
3655 return void_type_node;
3657 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3658 return (unsignedp
3659 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3660 : make_signed_type (GET_MODE_PRECISION (mode)));
3662 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3663 return (unsignedp
3664 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3665 : make_signed_type (GET_MODE_PRECISION (mode)));
3667 if (COMPLEX_MODE_P (mode))
3669 machine_mode inner_mode;
3670 tree inner_type;
3672 if (mode == TYPE_MODE (complex_float_type_node))
3673 return complex_float_type_node;
3674 if (mode == TYPE_MODE (complex_double_type_node))
3675 return complex_double_type_node;
3676 if (mode == TYPE_MODE (complex_long_double_type_node))
3677 return complex_long_double_type_node;
3679 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3680 return complex_integer_type_node;
3682 inner_mode = GET_MODE_INNER (mode);
3683 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3684 if (inner_type != NULL_TREE)
3685 return build_complex_type (inner_type);
3687 else if (VECTOR_MODE_P (mode))
3689 machine_mode inner_mode = GET_MODE_INNER (mode);
3690 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3691 if (inner_type != NULL_TREE)
3692 return build_vector_type_for_mode (inner_type, mode);
3695 if (mode == TYPE_MODE (dfloat32_type_node))
3696 return dfloat32_type_node;
3697 if (mode == TYPE_MODE (dfloat64_type_node))
3698 return dfloat64_type_node;
3699 if (mode == TYPE_MODE (dfloat128_type_node))
3700 return dfloat128_type_node;
3702 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3704 if (mode == TYPE_MODE (short_fract_type_node))
3705 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3706 if (mode == TYPE_MODE (fract_type_node))
3707 return unsignedp ? sat_fract_type_node : fract_type_node;
3708 if (mode == TYPE_MODE (long_fract_type_node))
3709 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3710 if (mode == TYPE_MODE (long_long_fract_type_node))
3711 return unsignedp ? sat_long_long_fract_type_node
3712 : long_long_fract_type_node;
3714 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3715 return unsignedp ? sat_unsigned_short_fract_type_node
3716 : unsigned_short_fract_type_node;
3717 if (mode == TYPE_MODE (unsigned_fract_type_node))
3718 return unsignedp ? sat_unsigned_fract_type_node
3719 : unsigned_fract_type_node;
3720 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3721 return unsignedp ? sat_unsigned_long_fract_type_node
3722 : unsigned_long_fract_type_node;
3723 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3724 return unsignedp ? sat_unsigned_long_long_fract_type_node
3725 : unsigned_long_long_fract_type_node;
3727 if (mode == TYPE_MODE (short_accum_type_node))
3728 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3729 if (mode == TYPE_MODE (accum_type_node))
3730 return unsignedp ? sat_accum_type_node : accum_type_node;
3731 if (mode == TYPE_MODE (long_accum_type_node))
3732 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3733 if (mode == TYPE_MODE (long_long_accum_type_node))
3734 return unsignedp ? sat_long_long_accum_type_node
3735 : long_long_accum_type_node;
3737 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3738 return unsignedp ? sat_unsigned_short_accum_type_node
3739 : unsigned_short_accum_type_node;
3740 if (mode == TYPE_MODE (unsigned_accum_type_node))
3741 return unsignedp ? sat_unsigned_accum_type_node
3742 : unsigned_accum_type_node;
3743 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3744 return unsignedp ? sat_unsigned_long_accum_type_node
3745 : unsigned_long_accum_type_node;
3746 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3747 return unsignedp ? sat_unsigned_long_long_accum_type_node
3748 : unsigned_long_long_accum_type_node;
3750 if (mode == QQmode)
3751 return unsignedp ? sat_qq_type_node : qq_type_node;
3752 if (mode == HQmode)
3753 return unsignedp ? sat_hq_type_node : hq_type_node;
3754 if (mode == SQmode)
3755 return unsignedp ? sat_sq_type_node : sq_type_node;
3756 if (mode == DQmode)
3757 return unsignedp ? sat_dq_type_node : dq_type_node;
3758 if (mode == TQmode)
3759 return unsignedp ? sat_tq_type_node : tq_type_node;
3761 if (mode == UQQmode)
3762 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3763 if (mode == UHQmode)
3764 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3765 if (mode == USQmode)
3766 return unsignedp ? sat_usq_type_node : usq_type_node;
3767 if (mode == UDQmode)
3768 return unsignedp ? sat_udq_type_node : udq_type_node;
3769 if (mode == UTQmode)
3770 return unsignedp ? sat_utq_type_node : utq_type_node;
3772 if (mode == HAmode)
3773 return unsignedp ? sat_ha_type_node : ha_type_node;
3774 if (mode == SAmode)
3775 return unsignedp ? sat_sa_type_node : sa_type_node;
3776 if (mode == DAmode)
3777 return unsignedp ? sat_da_type_node : da_type_node;
3778 if (mode == TAmode)
3779 return unsignedp ? sat_ta_type_node : ta_type_node;
3781 if (mode == UHAmode)
3782 return unsignedp ? sat_uha_type_node : uha_type_node;
3783 if (mode == USAmode)
3784 return unsignedp ? sat_usa_type_node : usa_type_node;
3785 if (mode == UDAmode)
3786 return unsignedp ? sat_uda_type_node : uda_type_node;
3787 if (mode == UTAmode)
3788 return unsignedp ? sat_uta_type_node : uta_type_node;
3791 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3792 if (TYPE_MODE (TREE_VALUE (t)) == mode
3793 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3794 return TREE_VALUE (t);
3796 return 0;
3799 tree
3800 c_common_unsigned_type (tree type)
3802 return c_common_signed_or_unsigned_type (1, type);
3805 /* Return a signed type the same as TYPE in other respects. */
3807 tree
3808 c_common_signed_type (tree type)
3810 return c_common_signed_or_unsigned_type (0, type);
3813 /* Return a type the same as TYPE except unsigned or
3814 signed according to UNSIGNEDP. */
3816 tree
3817 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3819 tree type1;
3820 int i;
3822 /* This block of code emulates the behavior of the old
3823 c_common_unsigned_type. In particular, it returns
3824 long_unsigned_type_node if passed a long, even when a int would
3825 have the same size. This is necessary for warnings to work
3826 correctly in archs where sizeof(int) == sizeof(long) */
3828 type1 = TYPE_MAIN_VARIANT (type);
3829 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3830 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3831 if (type1 == integer_type_node || type1 == unsigned_type_node)
3832 return unsignedp ? unsigned_type_node : integer_type_node;
3833 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3834 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3835 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3836 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3837 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3838 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3840 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3841 if (int_n_enabled_p[i]
3842 && (type1 == int_n_trees[i].unsigned_type
3843 || type1 == int_n_trees[i].signed_type))
3844 return (unsignedp ? int_n_trees[i].unsigned_type
3845 : int_n_trees[i].signed_type);
3847 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3848 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3849 #if HOST_BITS_PER_WIDE_INT >= 64
3850 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3851 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3852 #endif
3853 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3854 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3855 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3856 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3857 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3858 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3859 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3860 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3862 #define C_COMMON_FIXED_TYPES(NAME) \
3863 if (type1 == short_ ## NAME ## _type_node \
3864 || type1 == unsigned_short_ ## NAME ## _type_node) \
3865 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3866 : short_ ## NAME ## _type_node; \
3867 if (type1 == NAME ## _type_node \
3868 || type1 == unsigned_ ## NAME ## _type_node) \
3869 return unsignedp ? unsigned_ ## NAME ## _type_node \
3870 : NAME ## _type_node; \
3871 if (type1 == long_ ## NAME ## _type_node \
3872 || type1 == unsigned_long_ ## NAME ## _type_node) \
3873 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3874 : long_ ## NAME ## _type_node; \
3875 if (type1 == long_long_ ## NAME ## _type_node \
3876 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3877 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3878 : long_long_ ## NAME ## _type_node;
3880 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3881 if (type1 == NAME ## _type_node \
3882 || type1 == u ## NAME ## _type_node) \
3883 return unsignedp ? u ## NAME ## _type_node \
3884 : NAME ## _type_node;
3886 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3887 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3888 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3889 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3890 : sat_ ## short_ ## NAME ## _type_node; \
3891 if (type1 == sat_ ## NAME ## _type_node \
3892 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3893 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3894 : sat_ ## NAME ## _type_node; \
3895 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3896 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3897 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3898 : sat_ ## long_ ## NAME ## _type_node; \
3899 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3900 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3901 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3902 : sat_ ## long_long_ ## NAME ## _type_node;
3904 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3905 if (type1 == sat_ ## NAME ## _type_node \
3906 || type1 == sat_ ## u ## NAME ## _type_node) \
3907 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3908 : sat_ ## NAME ## _type_node;
3910 C_COMMON_FIXED_TYPES (fract);
3911 C_COMMON_FIXED_TYPES_SAT (fract);
3912 C_COMMON_FIXED_TYPES (accum);
3913 C_COMMON_FIXED_TYPES_SAT (accum);
3915 C_COMMON_FIXED_MODE_TYPES (qq);
3916 C_COMMON_FIXED_MODE_TYPES (hq);
3917 C_COMMON_FIXED_MODE_TYPES (sq);
3918 C_COMMON_FIXED_MODE_TYPES (dq);
3919 C_COMMON_FIXED_MODE_TYPES (tq);
3920 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3921 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3922 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3923 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3924 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3925 C_COMMON_FIXED_MODE_TYPES (ha);
3926 C_COMMON_FIXED_MODE_TYPES (sa);
3927 C_COMMON_FIXED_MODE_TYPES (da);
3928 C_COMMON_FIXED_MODE_TYPES (ta);
3929 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3930 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3931 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3932 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3934 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3935 the precision; they have precision set to match their range, but
3936 may use a wider mode to match an ABI. If we change modes, we may
3937 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3938 the precision as well, so as to yield correct results for
3939 bit-field types. C++ does not have these separate bit-field
3940 types, and producing a signed or unsigned variant of an
3941 ENUMERAL_TYPE may cause other problems as well. */
3943 if (!INTEGRAL_TYPE_P (type)
3944 || TYPE_UNSIGNED (type) == unsignedp)
3945 return type;
3947 #define TYPE_OK(node) \
3948 (TYPE_MODE (type) == TYPE_MODE (node) \
3949 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3950 if (TYPE_OK (signed_char_type_node))
3951 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3952 if (TYPE_OK (integer_type_node))
3953 return unsignedp ? unsigned_type_node : integer_type_node;
3954 if (TYPE_OK (short_integer_type_node))
3955 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3956 if (TYPE_OK (long_integer_type_node))
3957 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3958 if (TYPE_OK (long_long_integer_type_node))
3959 return (unsignedp ? long_long_unsigned_type_node
3960 : long_long_integer_type_node);
3962 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3963 if (int_n_enabled_p[i]
3964 && TYPE_MODE (type) == int_n_data[i].m
3965 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3966 return (unsignedp ? int_n_trees[i].unsigned_type
3967 : int_n_trees[i].signed_type);
3969 if (TYPE_OK (widest_integer_literal_type_node))
3970 return (unsignedp ? widest_unsigned_literal_type_node
3971 : widest_integer_literal_type_node);
3973 #if HOST_BITS_PER_WIDE_INT >= 64
3974 if (TYPE_OK (intTI_type_node))
3975 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3976 #endif
3977 if (TYPE_OK (intDI_type_node))
3978 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3979 if (TYPE_OK (intSI_type_node))
3980 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3981 if (TYPE_OK (intHI_type_node))
3982 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3983 if (TYPE_OK (intQI_type_node))
3984 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3985 #undef TYPE_OK
3987 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3990 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3992 tree
3993 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3995 int i;
3997 /* Extended integer types of the same width as a standard type have
3998 lesser rank, so those of the same width as int promote to int or
3999 unsigned int and are valid for printf formats expecting int or
4000 unsigned int. To avoid such special cases, avoid creating
4001 extended integer types for bit-fields if a standard integer type
4002 is available. */
4003 if (width == TYPE_PRECISION (integer_type_node))
4004 return unsignedp ? unsigned_type_node : integer_type_node;
4005 if (width == TYPE_PRECISION (signed_char_type_node))
4006 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4007 if (width == TYPE_PRECISION (short_integer_type_node))
4008 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4009 if (width == TYPE_PRECISION (long_integer_type_node))
4010 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4011 if (width == TYPE_PRECISION (long_long_integer_type_node))
4012 return (unsignedp ? long_long_unsigned_type_node
4013 : long_long_integer_type_node);
4014 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4015 if (int_n_enabled_p[i]
4016 && width == int_n_data[i].bitsize)
4017 return (unsignedp ? int_n_trees[i].unsigned_type
4018 : int_n_trees[i].signed_type);
4019 return build_nonstandard_integer_type (width, unsignedp);
4022 /* The C version of the register_builtin_type langhook. */
4024 void
4025 c_register_builtin_type (tree type, const char* name)
4027 tree decl;
4029 decl = build_decl (UNKNOWN_LOCATION,
4030 TYPE_DECL, get_identifier (name), type);
4031 DECL_ARTIFICIAL (decl) = 1;
4032 if (!TYPE_NAME (type))
4033 TYPE_NAME (type) = decl;
4034 pushdecl (decl);
4036 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4039 /* Print an error message for invalid operands to arith operation
4040 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4041 LOCATION is the location of the message. */
4043 void
4044 binary_op_error (location_t location, enum tree_code code,
4045 tree type0, tree type1)
4047 const char *opname;
4049 switch (code)
4051 case PLUS_EXPR:
4052 opname = "+"; break;
4053 case MINUS_EXPR:
4054 opname = "-"; break;
4055 case MULT_EXPR:
4056 opname = "*"; break;
4057 case MAX_EXPR:
4058 opname = "max"; break;
4059 case MIN_EXPR:
4060 opname = "min"; break;
4061 case EQ_EXPR:
4062 opname = "=="; break;
4063 case NE_EXPR:
4064 opname = "!="; break;
4065 case LE_EXPR:
4066 opname = "<="; break;
4067 case GE_EXPR:
4068 opname = ">="; break;
4069 case LT_EXPR:
4070 opname = "<"; break;
4071 case GT_EXPR:
4072 opname = ">"; break;
4073 case LSHIFT_EXPR:
4074 opname = "<<"; break;
4075 case RSHIFT_EXPR:
4076 opname = ">>"; break;
4077 case TRUNC_MOD_EXPR:
4078 case FLOOR_MOD_EXPR:
4079 opname = "%"; break;
4080 case TRUNC_DIV_EXPR:
4081 case FLOOR_DIV_EXPR:
4082 opname = "/"; break;
4083 case BIT_AND_EXPR:
4084 opname = "&"; break;
4085 case BIT_IOR_EXPR:
4086 opname = "|"; break;
4087 case TRUTH_ANDIF_EXPR:
4088 opname = "&&"; break;
4089 case TRUTH_ORIF_EXPR:
4090 opname = "||"; break;
4091 case BIT_XOR_EXPR:
4092 opname = "^"; break;
4093 default:
4094 gcc_unreachable ();
4096 error_at (location,
4097 "invalid operands to binary %s (have %qT and %qT)", opname,
4098 type0, type1);
4101 /* Given an expression as a tree, return its original type. Do this
4102 by stripping any conversion that preserves the sign and precision. */
4103 static tree
4104 expr_original_type (tree expr)
4106 STRIP_SIGN_NOPS (expr);
4107 return TREE_TYPE (expr);
4110 /* Subroutine of build_binary_op, used for comparison operations.
4111 See if the operands have both been converted from subword integer types
4112 and, if so, perhaps change them both back to their original type.
4113 This function is also responsible for converting the two operands
4114 to the proper common type for comparison.
4116 The arguments of this function are all pointers to local variables
4117 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4118 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4120 LOC is the location of the comparison.
4122 If this function returns nonzero, it means that the comparison has
4123 a constant value. What this function returns is an expression for
4124 that value. */
4126 tree
4127 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4128 tree *restype_ptr, enum tree_code *rescode_ptr)
4130 tree type;
4131 tree op0 = *op0_ptr;
4132 tree op1 = *op1_ptr;
4133 int unsignedp0, unsignedp1;
4134 int real1, real2;
4135 tree primop0, primop1;
4136 enum tree_code code = *rescode_ptr;
4138 /* Throw away any conversions to wider types
4139 already present in the operands. */
4141 primop0 = c_common_get_narrower (op0, &unsignedp0);
4142 primop1 = c_common_get_narrower (op1, &unsignedp1);
4144 /* If primopN is first sign-extended from primopN's precision to opN's
4145 precision, then zero-extended from opN's precision to
4146 *restype_ptr precision, shortenings might be invalid. */
4147 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4148 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4149 && !unsignedp0
4150 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4151 primop0 = op0;
4152 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4153 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4154 && !unsignedp1
4155 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4156 primop1 = op1;
4158 /* Handle the case that OP0 does not *contain* a conversion
4159 but it *requires* conversion to FINAL_TYPE. */
4161 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4162 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4163 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4164 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4166 /* If one of the operands must be floated, we cannot optimize. */
4167 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4168 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4170 /* If first arg is constant, swap the args (changing operation
4171 so value is preserved), for canonicalization. Don't do this if
4172 the second arg is 0. */
4174 if (TREE_CONSTANT (primop0)
4175 && !integer_zerop (primop1) && !real_zerop (primop1)
4176 && !fixed_zerop (primop1))
4178 tree tem = primop0;
4179 int temi = unsignedp0;
4180 primop0 = primop1;
4181 primop1 = tem;
4182 tem = op0;
4183 op0 = op1;
4184 op1 = tem;
4185 *op0_ptr = op0;
4186 *op1_ptr = op1;
4187 unsignedp0 = unsignedp1;
4188 unsignedp1 = temi;
4189 temi = real1;
4190 real1 = real2;
4191 real2 = temi;
4193 switch (code)
4195 case LT_EXPR:
4196 code = GT_EXPR;
4197 break;
4198 case GT_EXPR:
4199 code = LT_EXPR;
4200 break;
4201 case LE_EXPR:
4202 code = GE_EXPR;
4203 break;
4204 case GE_EXPR:
4205 code = LE_EXPR;
4206 break;
4207 default:
4208 break;
4210 *rescode_ptr = code;
4213 /* If comparing an integer against a constant more bits wide,
4214 maybe we can deduce a value of 1 or 0 independent of the data.
4215 Or else truncate the constant now
4216 rather than extend the variable at run time.
4218 This is only interesting if the constant is the wider arg.
4219 Also, it is not safe if the constant is unsigned and the
4220 variable arg is signed, since in this case the variable
4221 would be sign-extended and then regarded as unsigned.
4222 Our technique fails in this case because the lowest/highest
4223 possible unsigned results don't follow naturally from the
4224 lowest/highest possible values of the variable operand.
4225 For just EQ_EXPR and NE_EXPR there is another technique that
4226 could be used: see if the constant can be faithfully represented
4227 in the other operand's type, by truncating it and reextending it
4228 and see if that preserves the constant's value. */
4230 if (!real1 && !real2
4231 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4232 && TREE_CODE (primop1) == INTEGER_CST
4233 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4235 int min_gt, max_gt, min_lt, max_lt;
4236 tree maxval, minval;
4237 /* 1 if comparison is nominally unsigned. */
4238 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4239 tree val;
4241 type = c_common_signed_or_unsigned_type (unsignedp0,
4242 TREE_TYPE (primop0));
4244 maxval = TYPE_MAX_VALUE (type);
4245 minval = TYPE_MIN_VALUE (type);
4247 if (unsignedp && !unsignedp0)
4248 *restype_ptr = c_common_signed_type (*restype_ptr);
4250 if (TREE_TYPE (primop1) != *restype_ptr)
4252 /* Convert primop1 to target type, but do not introduce
4253 additional overflow. We know primop1 is an int_cst. */
4254 primop1 = force_fit_type (*restype_ptr,
4255 wide_int::from
4256 (primop1,
4257 TYPE_PRECISION (*restype_ptr),
4258 TYPE_SIGN (TREE_TYPE (primop1))),
4259 0, TREE_OVERFLOW (primop1));
4261 if (type != *restype_ptr)
4263 minval = convert (*restype_ptr, minval);
4264 maxval = convert (*restype_ptr, maxval);
4267 min_gt = tree_int_cst_lt (primop1, minval);
4268 max_gt = tree_int_cst_lt (primop1, maxval);
4269 min_lt = tree_int_cst_lt (minval, primop1);
4270 max_lt = tree_int_cst_lt (maxval, primop1);
4272 val = 0;
4273 /* This used to be a switch, but Genix compiler can't handle that. */
4274 if (code == NE_EXPR)
4276 if (max_lt || min_gt)
4277 val = truthvalue_true_node;
4279 else if (code == EQ_EXPR)
4281 if (max_lt || min_gt)
4282 val = truthvalue_false_node;
4284 else if (code == LT_EXPR)
4286 if (max_lt)
4287 val = truthvalue_true_node;
4288 if (!min_lt)
4289 val = truthvalue_false_node;
4291 else if (code == GT_EXPR)
4293 if (min_gt)
4294 val = truthvalue_true_node;
4295 if (!max_gt)
4296 val = truthvalue_false_node;
4298 else if (code == LE_EXPR)
4300 if (!max_gt)
4301 val = truthvalue_true_node;
4302 if (min_gt)
4303 val = truthvalue_false_node;
4305 else if (code == GE_EXPR)
4307 if (!min_lt)
4308 val = truthvalue_true_node;
4309 if (max_lt)
4310 val = truthvalue_false_node;
4313 /* If primop0 was sign-extended and unsigned comparison specd,
4314 we did a signed comparison above using the signed type bounds.
4315 But the comparison we output must be unsigned.
4317 Also, for inequalities, VAL is no good; but if the signed
4318 comparison had *any* fixed result, it follows that the
4319 unsigned comparison just tests the sign in reverse
4320 (positive values are LE, negative ones GE).
4321 So we can generate an unsigned comparison
4322 against an extreme value of the signed type. */
4324 if (unsignedp && !unsignedp0)
4326 if (val != 0)
4327 switch (code)
4329 case LT_EXPR:
4330 case GE_EXPR:
4331 primop1 = TYPE_MIN_VALUE (type);
4332 val = 0;
4333 break;
4335 case LE_EXPR:
4336 case GT_EXPR:
4337 primop1 = TYPE_MAX_VALUE (type);
4338 val = 0;
4339 break;
4341 default:
4342 break;
4344 type = c_common_unsigned_type (type);
4347 if (TREE_CODE (primop0) != INTEGER_CST)
4349 if (val == truthvalue_false_node)
4350 warning_at (loc, OPT_Wtype_limits,
4351 "comparison is always false due to limited range of data type");
4352 if (val == truthvalue_true_node)
4353 warning_at (loc, OPT_Wtype_limits,
4354 "comparison is always true due to limited range of data type");
4357 if (val != 0)
4359 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4360 if (TREE_SIDE_EFFECTS (primop0))
4361 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4362 return val;
4365 /* Value is not predetermined, but do the comparison
4366 in the type of the operand that is not constant.
4367 TYPE is already properly set. */
4370 /* If either arg is decimal float and the other is float, find the
4371 proper common type to use for comparison. */
4372 else if (real1 && real2
4373 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4374 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4375 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4377 /* If either arg is decimal float and the other is float, fail. */
4378 else if (real1 && real2
4379 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4380 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4381 return 0;
4383 else if (real1 && real2
4384 && (TYPE_PRECISION (TREE_TYPE (primop0))
4385 == TYPE_PRECISION (TREE_TYPE (primop1))))
4386 type = TREE_TYPE (primop0);
4388 /* If args' natural types are both narrower than nominal type
4389 and both extend in the same manner, compare them
4390 in the type of the wider arg.
4391 Otherwise must actually extend both to the nominal
4392 common type lest different ways of extending
4393 alter the result.
4394 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4396 else if (unsignedp0 == unsignedp1 && real1 == real2
4397 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4398 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4400 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4401 type = c_common_signed_or_unsigned_type (unsignedp0
4402 || TYPE_UNSIGNED (*restype_ptr),
4403 type);
4404 /* Make sure shorter operand is extended the right way
4405 to match the longer operand. */
4406 primop0
4407 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4408 TREE_TYPE (primop0)),
4409 primop0);
4410 primop1
4411 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4412 TREE_TYPE (primop1)),
4413 primop1);
4415 else
4417 /* Here we must do the comparison on the nominal type
4418 using the args exactly as we received them. */
4419 type = *restype_ptr;
4420 primop0 = op0;
4421 primop1 = op1;
4423 if (!real1 && !real2 && integer_zerop (primop1)
4424 && TYPE_UNSIGNED (*restype_ptr))
4426 tree value = 0;
4427 /* All unsigned values are >= 0, so we warn. However,
4428 if OP0 is a constant that is >= 0, the signedness of
4429 the comparison isn't an issue, so suppress the
4430 warning. */
4431 bool warn =
4432 warn_type_limits && !in_system_header_at (loc)
4433 && !(TREE_CODE (primop0) == INTEGER_CST
4434 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4435 primop0)))
4436 /* Do not warn for enumeration types. */
4437 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4439 switch (code)
4441 case GE_EXPR:
4442 if (warn)
4443 warning_at (loc, OPT_Wtype_limits,
4444 "comparison of unsigned expression >= 0 is always true");
4445 value = truthvalue_true_node;
4446 break;
4448 case LT_EXPR:
4449 if (warn)
4450 warning_at (loc, OPT_Wtype_limits,
4451 "comparison of unsigned expression < 0 is always false");
4452 value = truthvalue_false_node;
4453 break;
4455 default:
4456 break;
4459 if (value != 0)
4461 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4462 if (TREE_SIDE_EFFECTS (primop0))
4463 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4464 primop0, value);
4465 return value;
4470 *op0_ptr = convert (type, primop0);
4471 *op1_ptr = convert (type, primop1);
4473 *restype_ptr = truthvalue_type_node;
4475 return 0;
4478 /* Return a tree for the sum or difference (RESULTCODE says which)
4479 of pointer PTROP and integer INTOP. */
4481 tree
4482 pointer_int_sum (location_t loc, enum tree_code resultcode,
4483 tree ptrop, tree intop, bool complain)
4485 tree size_exp, ret;
4487 /* The result is a pointer of the same type that is being added. */
4488 tree result_type = TREE_TYPE (ptrop);
4490 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4492 if (complain && warn_pointer_arith)
4493 pedwarn (loc, OPT_Wpointer_arith,
4494 "pointer of type %<void *%> used in arithmetic");
4495 else if (!complain)
4496 return error_mark_node;
4497 size_exp = integer_one_node;
4499 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4501 if (complain && warn_pointer_arith)
4502 pedwarn (loc, OPT_Wpointer_arith,
4503 "pointer to a function used in arithmetic");
4504 else if (!complain)
4505 return error_mark_node;
4506 size_exp = integer_one_node;
4508 else
4509 size_exp = size_in_bytes (TREE_TYPE (result_type));
4511 /* We are manipulating pointer values, so we don't need to warn
4512 about relying on undefined signed overflow. We disable the
4513 warning here because we use integer types so fold won't know that
4514 they are really pointers. */
4515 fold_defer_overflow_warnings ();
4517 /* If what we are about to multiply by the size of the elements
4518 contains a constant term, apply distributive law
4519 and multiply that constant term separately.
4520 This helps produce common subexpressions. */
4521 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4522 && !TREE_CONSTANT (intop)
4523 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4524 && TREE_CONSTANT (size_exp)
4525 /* If the constant comes from pointer subtraction,
4526 skip this optimization--it would cause an error. */
4527 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4528 /* If the constant is unsigned, and smaller than the pointer size,
4529 then we must skip this optimization. This is because it could cause
4530 an overflow error if the constant is negative but INTOP is not. */
4531 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4532 || (TYPE_PRECISION (TREE_TYPE (intop))
4533 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4535 enum tree_code subcode = resultcode;
4536 tree int_type = TREE_TYPE (intop);
4537 if (TREE_CODE (intop) == MINUS_EXPR)
4538 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4539 /* Convert both subexpression types to the type of intop,
4540 because weird cases involving pointer arithmetic
4541 can result in a sum or difference with different type args. */
4542 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4543 subcode, ptrop,
4544 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4545 intop = convert (int_type, TREE_OPERAND (intop, 0));
4548 /* Convert the integer argument to a type the same size as sizetype
4549 so the multiply won't overflow spuriously. */
4550 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4551 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4552 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4553 TYPE_UNSIGNED (sizetype)), intop);
4555 /* Replace the integer argument with a suitable product by the object size.
4556 Do this multiplication as signed, then convert to the appropriate type
4557 for the pointer operation and disregard an overflow that occurred only
4558 because of the sign-extension change in the latter conversion. */
4560 tree t = build_binary_op (loc,
4561 MULT_EXPR, intop,
4562 convert (TREE_TYPE (intop), size_exp), 1);
4563 intop = convert (sizetype, t);
4564 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4565 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4568 /* Create the sum or difference. */
4569 if (resultcode == MINUS_EXPR)
4570 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4572 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4574 fold_undefer_and_ignore_overflow_warnings ();
4576 return ret;
4579 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4580 and if NON_CONST is known not to be permitted in an evaluated part
4581 of a constant expression. */
4583 tree
4584 c_wrap_maybe_const (tree expr, bool non_const)
4586 bool nowarning = TREE_NO_WARNING (expr);
4587 location_t loc = EXPR_LOCATION (expr);
4589 /* This should never be called for C++. */
4590 if (c_dialect_cxx ())
4591 gcc_unreachable ();
4593 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4594 STRIP_TYPE_NOPS (expr);
4595 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4596 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4597 if (nowarning)
4598 TREE_NO_WARNING (expr) = 1;
4599 protected_set_expr_location (expr, loc);
4601 return expr;
4604 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4605 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4606 around the SAVE_EXPR if needed so that c_fully_fold does not need
4607 to look inside SAVE_EXPRs. */
4609 tree
4610 c_save_expr (tree expr)
4612 bool maybe_const = true;
4613 if (c_dialect_cxx ())
4614 return save_expr (expr);
4615 expr = c_fully_fold (expr, false, &maybe_const);
4616 expr = save_expr (expr);
4617 if (!maybe_const)
4618 expr = c_wrap_maybe_const (expr, true);
4619 return expr;
4622 /* Return whether EXPR is a declaration whose address can never be
4623 NULL. */
4625 bool
4626 decl_with_nonnull_addr_p (const_tree expr)
4628 return (DECL_P (expr)
4629 && (TREE_CODE (expr) == PARM_DECL
4630 || TREE_CODE (expr) == LABEL_DECL
4631 || !DECL_WEAK (expr)));
4634 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4635 or for an `if' or `while' statement or ?..: exp. It should already
4636 have been validated to be of suitable type; otherwise, a bad
4637 diagnostic may result.
4639 The EXPR is located at LOCATION.
4641 This preparation consists of taking the ordinary
4642 representation of an expression expr and producing a valid tree
4643 boolean expression describing whether expr is nonzero. We could
4644 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4645 but we optimize comparisons, &&, ||, and !.
4647 The resulting type should always be `truthvalue_type_node'. */
4649 tree
4650 c_common_truthvalue_conversion (location_t location, tree expr)
4652 switch (TREE_CODE (expr))
4654 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4655 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4656 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4657 case ORDERED_EXPR: case UNORDERED_EXPR:
4658 if (TREE_TYPE (expr) == truthvalue_type_node)
4659 return expr;
4660 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4661 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4662 goto ret;
4664 case TRUTH_ANDIF_EXPR:
4665 case TRUTH_ORIF_EXPR:
4666 case TRUTH_AND_EXPR:
4667 case TRUTH_OR_EXPR:
4668 case TRUTH_XOR_EXPR:
4669 if (TREE_TYPE (expr) == truthvalue_type_node)
4670 return expr;
4671 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4672 c_common_truthvalue_conversion (location,
4673 TREE_OPERAND (expr, 0)),
4674 c_common_truthvalue_conversion (location,
4675 TREE_OPERAND (expr, 1)));
4676 goto ret;
4678 case TRUTH_NOT_EXPR:
4679 if (TREE_TYPE (expr) == truthvalue_type_node)
4680 return expr;
4681 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4682 c_common_truthvalue_conversion (location,
4683 TREE_OPERAND (expr, 0)));
4684 goto ret;
4686 case ERROR_MARK:
4687 return expr;
4689 case INTEGER_CST:
4690 return integer_zerop (expr) ? truthvalue_false_node
4691 : truthvalue_true_node;
4693 case REAL_CST:
4694 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4695 ? truthvalue_true_node
4696 : truthvalue_false_node;
4698 case FIXED_CST:
4699 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4700 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4701 ? truthvalue_true_node
4702 : truthvalue_false_node;
4704 case FUNCTION_DECL:
4705 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4706 /* Fall through. */
4708 case ADDR_EXPR:
4710 tree inner = TREE_OPERAND (expr, 0);
4711 if (decl_with_nonnull_addr_p (inner))
4713 /* Common Ada/Pascal programmer's mistake. */
4714 warning_at (location,
4715 OPT_Waddress,
4716 "the address of %qD will always evaluate as %<true%>",
4717 inner);
4718 return truthvalue_true_node;
4720 break;
4723 case COMPLEX_EXPR:
4724 expr = build_binary_op (EXPR_LOCATION (expr),
4725 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4726 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4727 c_common_truthvalue_conversion (location,
4728 TREE_OPERAND (expr, 0)),
4729 c_common_truthvalue_conversion (location,
4730 TREE_OPERAND (expr, 1)),
4732 goto ret;
4734 case NEGATE_EXPR:
4735 case ABS_EXPR:
4736 case FLOAT_EXPR:
4737 case EXCESS_PRECISION_EXPR:
4738 /* These don't change whether an object is nonzero or zero. */
4739 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4741 case LROTATE_EXPR:
4742 case RROTATE_EXPR:
4743 /* These don't change whether an object is zero or nonzero, but
4744 we can't ignore them if their second arg has side-effects. */
4745 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4747 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4748 TREE_OPERAND (expr, 1),
4749 c_common_truthvalue_conversion
4750 (location, TREE_OPERAND (expr, 0)));
4751 goto ret;
4753 else
4754 return c_common_truthvalue_conversion (location,
4755 TREE_OPERAND (expr, 0));
4757 case COND_EXPR:
4758 /* Distribute the conversion into the arms of a COND_EXPR. */
4759 if (c_dialect_cxx ())
4761 tree op1 = TREE_OPERAND (expr, 1);
4762 tree op2 = TREE_OPERAND (expr, 2);
4763 /* In C++ one of the arms might have void type if it is throw. */
4764 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4765 op1 = c_common_truthvalue_conversion (location, op1);
4766 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4767 op2 = c_common_truthvalue_conversion (location, op2);
4768 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4769 TREE_OPERAND (expr, 0), op1, op2);
4770 goto ret;
4772 else
4774 /* Folding will happen later for C. */
4775 expr = build3 (COND_EXPR, truthvalue_type_node,
4776 TREE_OPERAND (expr, 0),
4777 c_common_truthvalue_conversion (location,
4778 TREE_OPERAND (expr, 1)),
4779 c_common_truthvalue_conversion (location,
4780 TREE_OPERAND (expr, 2)));
4781 goto ret;
4784 CASE_CONVERT:
4786 tree totype = TREE_TYPE (expr);
4787 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4789 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4790 since that affects how `default_conversion' will behave. */
4791 if (TREE_CODE (totype) == REFERENCE_TYPE
4792 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4793 break;
4794 /* Don't strip a conversion from C++0x scoped enum, since they
4795 don't implicitly convert to other types. */
4796 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4797 && ENUM_IS_SCOPED (fromtype))
4798 break;
4799 /* If this isn't narrowing the argument, we can ignore it. */
4800 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4801 return c_common_truthvalue_conversion (location,
4802 TREE_OPERAND (expr, 0));
4804 break;
4806 case MODIFY_EXPR:
4807 if (!TREE_NO_WARNING (expr)
4808 && warn_parentheses)
4810 warning (OPT_Wparentheses,
4811 "suggest parentheses around assignment used as truth value");
4812 TREE_NO_WARNING (expr) = 1;
4814 break;
4816 default:
4817 break;
4820 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4822 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4823 expr = (build_binary_op
4824 (EXPR_LOCATION (expr),
4825 (TREE_SIDE_EFFECTS (expr)
4826 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4827 c_common_truthvalue_conversion
4828 (location,
4829 build_unary_op (location, REALPART_EXPR, t, 0)),
4830 c_common_truthvalue_conversion
4831 (location,
4832 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4833 0));
4834 goto ret;
4837 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4839 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4840 FCONST0 (TYPE_MODE
4841 (TREE_TYPE (expr))));
4842 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4844 else
4845 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4847 ret:
4848 protected_set_expr_location (expr, location);
4849 return expr;
4852 static void def_builtin_1 (enum built_in_function fncode,
4853 const char *name,
4854 enum built_in_class fnclass,
4855 tree fntype, tree libtype,
4856 bool both_p, bool fallback_p, bool nonansi_p,
4857 tree fnattrs, bool implicit_p);
4860 /* Apply the TYPE_QUALS to the new DECL. */
4862 void
4863 c_apply_type_quals_to_decl (int type_quals, tree decl)
4865 tree type = TREE_TYPE (decl);
4867 if (type == error_mark_node)
4868 return;
4870 if ((type_quals & TYPE_QUAL_CONST)
4871 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4872 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4873 constructor can produce constant init, so rely on cp_finish_decl to
4874 clear TREE_READONLY if the variable has non-constant init. */
4875 TREE_READONLY (decl) = 1;
4876 if (type_quals & TYPE_QUAL_VOLATILE)
4878 TREE_SIDE_EFFECTS (decl) = 1;
4879 TREE_THIS_VOLATILE (decl) = 1;
4881 if (type_quals & TYPE_QUAL_RESTRICT)
4883 while (type && TREE_CODE (type) == ARRAY_TYPE)
4884 /* Allow 'restrict' on arrays of pointers.
4885 FIXME currently we just ignore it. */
4886 type = TREE_TYPE (type);
4887 if (!type
4888 || !POINTER_TYPE_P (type)
4889 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4890 error ("invalid use of %<restrict%>");
4894 struct c_type_hasher : ggc_hasher<tree>
4896 static hashval_t hash (tree);
4897 static bool equal (tree, tree);
4900 /* Hash function for the problem of multiple type definitions in
4901 different files. This must hash all types that will compare
4902 equal via comptypes to the same value. In practice it hashes
4903 on some of the simple stuff and leaves the details to comptypes. */
4905 hashval_t
4906 c_type_hasher::hash (tree t)
4908 int n_elements;
4909 int shift, size;
4910 tree t2;
4911 switch (TREE_CODE (t))
4913 /* For pointers, hash on pointee type plus some swizzling. */
4914 case POINTER_TYPE:
4915 return hash (TREE_TYPE (t)) ^ 0x3003003;
4916 /* Hash on number of elements and total size. */
4917 case ENUMERAL_TYPE:
4918 shift = 3;
4919 t2 = TYPE_VALUES (t);
4920 break;
4921 case RECORD_TYPE:
4922 shift = 0;
4923 t2 = TYPE_FIELDS (t);
4924 break;
4925 case QUAL_UNION_TYPE:
4926 shift = 1;
4927 t2 = TYPE_FIELDS (t);
4928 break;
4929 case UNION_TYPE:
4930 shift = 2;
4931 t2 = TYPE_FIELDS (t);
4932 break;
4933 default:
4934 gcc_unreachable ();
4936 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4937 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4938 n_elements = list_length (t2);
4939 /* We might have a VLA here. */
4940 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4941 size = 0;
4942 else
4943 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4944 return ((size << 24) | (n_elements << shift));
4947 bool
4948 c_type_hasher::equal (tree t1, tree t2)
4950 return lang_hooks.types_compatible_p (t1, t2);
4953 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4955 /* Return the typed-based alias set for T, which may be an expression
4956 or a type. Return -1 if we don't do anything special. */
4958 alias_set_type
4959 c_common_get_alias_set (tree t)
4961 tree u;
4963 /* For VLAs, use the alias set of the element type rather than the
4964 default of alias set 0 for types compared structurally. */
4965 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4967 if (TREE_CODE (t) == ARRAY_TYPE)
4968 return get_alias_set (TREE_TYPE (t));
4969 return -1;
4972 /* Permit type-punning when accessing a union, provided the access
4973 is directly through the union. For example, this code does not
4974 permit taking the address of a union member and then storing
4975 through it. Even the type-punning allowed here is a GCC
4976 extension, albeit a common and useful one; the C standard says
4977 that such accesses have implementation-defined behavior. */
4978 for (u = t;
4979 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4980 u = TREE_OPERAND (u, 0))
4981 if (TREE_CODE (u) == COMPONENT_REF
4982 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4983 return 0;
4985 /* That's all the expressions we handle specially. */
4986 if (!TYPE_P (t))
4987 return -1;
4989 /* The C standard guarantees that any object may be accessed via an
4990 lvalue that has character type. */
4991 if (t == char_type_node
4992 || t == signed_char_type_node
4993 || t == unsigned_char_type_node)
4994 return 0;
4996 /* The C standard specifically allows aliasing between signed and
4997 unsigned variants of the same type. We treat the signed
4998 variant as canonical. */
4999 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5001 tree t1 = c_common_signed_type (t);
5003 /* t1 == t can happen for boolean nodes which are always unsigned. */
5004 if (t1 != t)
5005 return get_alias_set (t1);
5008 /* Handle the case of multiple type nodes referring to "the same" type,
5009 which occurs with IMA. These share an alias set. FIXME: Currently only
5010 C90 is handled. (In C99 type compatibility is not transitive, which
5011 complicates things mightily. The alias set splay trees can theoretically
5012 represent this, but insertion is tricky when you consider all the
5013 different orders things might arrive in.) */
5015 if (c_language != clk_c || flag_isoc99)
5016 return -1;
5018 /* Save time if there's only one input file. */
5019 if (num_in_fnames == 1)
5020 return -1;
5022 /* Pointers need special handling if they point to any type that
5023 needs special handling (below). */
5024 if (TREE_CODE (t) == POINTER_TYPE)
5026 tree t2;
5027 /* Find bottom type under any nested POINTERs. */
5028 for (t2 = TREE_TYPE (t);
5029 TREE_CODE (t2) == POINTER_TYPE;
5030 t2 = TREE_TYPE (t2))
5032 if (TREE_CODE (t2) != RECORD_TYPE
5033 && TREE_CODE (t2) != ENUMERAL_TYPE
5034 && TREE_CODE (t2) != QUAL_UNION_TYPE
5035 && TREE_CODE (t2) != UNION_TYPE)
5036 return -1;
5037 if (TYPE_SIZE (t2) == 0)
5038 return -1;
5040 /* These are the only cases that need special handling. */
5041 if (TREE_CODE (t) != RECORD_TYPE
5042 && TREE_CODE (t) != ENUMERAL_TYPE
5043 && TREE_CODE (t) != QUAL_UNION_TYPE
5044 && TREE_CODE (t) != UNION_TYPE
5045 && TREE_CODE (t) != POINTER_TYPE)
5046 return -1;
5047 /* Undefined? */
5048 if (TYPE_SIZE (t) == 0)
5049 return -1;
5051 /* Look up t in hash table. Only one of the compatible types within each
5052 alias set is recorded in the table. */
5053 if (!type_hash_table)
5054 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5055 tree *slot = type_hash_table->find_slot (t, INSERT);
5056 if (*slot != NULL)
5058 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5059 return TYPE_ALIAS_SET ((tree)*slot);
5061 else
5062 /* Our caller will assign and record (in t) a new alias set; all we need
5063 to do is remember t in the hash table. */
5064 *slot = t;
5066 return -1;
5069 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5070 the IS_SIZEOF parameter indicates which operator is being applied.
5071 The COMPLAIN flag controls whether we should diagnose possibly
5072 ill-formed constructs or not. LOC is the location of the SIZEOF or
5073 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5074 a type in any context should be returned, rather than the normal
5075 alignment for that type. */
5077 tree
5078 c_sizeof_or_alignof_type (location_t loc,
5079 tree type, bool is_sizeof, bool min_alignof,
5080 int complain)
5082 const char *op_name;
5083 tree value = NULL;
5084 enum tree_code type_code = TREE_CODE (type);
5086 op_name = is_sizeof ? "sizeof" : "__alignof__";
5088 if (type_code == FUNCTION_TYPE)
5090 if (is_sizeof)
5092 if (complain && warn_pointer_arith)
5093 pedwarn (loc, OPT_Wpointer_arith,
5094 "invalid application of %<sizeof%> to a function type");
5095 else if (!complain)
5096 return error_mark_node;
5097 value = size_one_node;
5099 else
5101 if (complain)
5103 if (c_dialect_cxx ())
5104 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5105 "%<alignof%> applied to a function type");
5106 else
5107 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5108 "%<_Alignof%> applied to a function type");
5110 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5113 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5115 if (type_code == VOID_TYPE
5116 && complain && warn_pointer_arith)
5117 pedwarn (loc, OPT_Wpointer_arith,
5118 "invalid application of %qs to a void type", op_name);
5119 else if (!complain)
5120 return error_mark_node;
5121 value = size_one_node;
5123 else if (!COMPLETE_TYPE_P (type)
5124 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5126 if (complain)
5127 error_at (loc, "invalid application of %qs to incomplete type %qT",
5128 op_name, type);
5129 return error_mark_node;
5131 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5132 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5134 if (complain)
5135 error_at (loc, "invalid application of %qs to array type %qT of "
5136 "incomplete element type", op_name, type);
5137 return error_mark_node;
5139 else
5141 if (is_sizeof)
5142 /* Convert in case a char is more than one unit. */
5143 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5144 size_int (TYPE_PRECISION (char_type_node)
5145 / BITS_PER_UNIT));
5146 else if (min_alignof)
5147 value = size_int (min_align_of_type (type));
5148 else
5149 value = size_int (TYPE_ALIGN_UNIT (type));
5152 /* VALUE will have the middle-end integer type sizetype.
5153 However, we should really return a value of type `size_t',
5154 which is just a typedef for an ordinary integer type. */
5155 value = fold_convert_loc (loc, size_type_node, value);
5157 return value;
5160 /* Implement the __alignof keyword: Return the minimum required
5161 alignment of EXPR, measured in bytes. For VAR_DECLs,
5162 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5163 from an "aligned" __attribute__ specification). LOC is the
5164 location of the ALIGNOF operator. */
5166 tree
5167 c_alignof_expr (location_t loc, tree expr)
5169 tree t;
5171 if (VAR_OR_FUNCTION_DECL_P (expr))
5172 t = size_int (DECL_ALIGN_UNIT (expr));
5174 else if (TREE_CODE (expr) == COMPONENT_REF
5175 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5177 error_at (loc, "%<__alignof%> applied to a bit-field");
5178 t = size_one_node;
5180 else if (TREE_CODE (expr) == COMPONENT_REF
5181 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5182 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5184 else if (TREE_CODE (expr) == INDIRECT_REF)
5186 tree t = TREE_OPERAND (expr, 0);
5187 tree best = t;
5188 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5190 while (CONVERT_EXPR_P (t)
5191 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5193 int thisalign;
5195 t = TREE_OPERAND (t, 0);
5196 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5197 if (thisalign > bestalign)
5198 best = t, bestalign = thisalign;
5200 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5202 else
5203 return c_alignof (loc, TREE_TYPE (expr));
5205 return fold_convert_loc (loc, size_type_node, t);
5208 /* Handle C and C++ default attributes. */
5210 enum built_in_attribute
5212 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5213 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5214 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5215 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5216 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5217 #include "builtin-attrs.def"
5218 #undef DEF_ATTR_NULL_TREE
5219 #undef DEF_ATTR_INT
5220 #undef DEF_ATTR_STRING
5221 #undef DEF_ATTR_IDENT
5222 #undef DEF_ATTR_TREE_LIST
5223 ATTR_LAST
5226 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5228 static void c_init_attributes (void);
5230 enum c_builtin_type
5232 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5233 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5234 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5235 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5236 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5237 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5238 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5239 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5240 ARG6) NAME,
5241 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5242 ARG6, ARG7) NAME,
5243 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5244 ARG6, ARG7, ARG8) NAME,
5245 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5246 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5247 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5248 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5249 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5250 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5251 NAME,
5252 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5253 ARG6, ARG7) NAME,
5254 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5255 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5256 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5257 #include "builtin-types.def"
5258 #undef DEF_PRIMITIVE_TYPE
5259 #undef DEF_FUNCTION_TYPE_0
5260 #undef DEF_FUNCTION_TYPE_1
5261 #undef DEF_FUNCTION_TYPE_2
5262 #undef DEF_FUNCTION_TYPE_3
5263 #undef DEF_FUNCTION_TYPE_4
5264 #undef DEF_FUNCTION_TYPE_5
5265 #undef DEF_FUNCTION_TYPE_6
5266 #undef DEF_FUNCTION_TYPE_7
5267 #undef DEF_FUNCTION_TYPE_8
5268 #undef DEF_FUNCTION_TYPE_VAR_0
5269 #undef DEF_FUNCTION_TYPE_VAR_1
5270 #undef DEF_FUNCTION_TYPE_VAR_2
5271 #undef DEF_FUNCTION_TYPE_VAR_3
5272 #undef DEF_FUNCTION_TYPE_VAR_4
5273 #undef DEF_FUNCTION_TYPE_VAR_5
5274 #undef DEF_FUNCTION_TYPE_VAR_7
5275 #undef DEF_FUNCTION_TYPE_VAR_11
5276 #undef DEF_POINTER_TYPE
5277 BT_LAST
5280 typedef enum c_builtin_type builtin_type;
5282 /* A temporary array for c_common_nodes_and_builtins. Used in
5283 communication with def_fn_type. */
5284 static tree builtin_types[(int) BT_LAST + 1];
5286 /* A helper function for c_common_nodes_and_builtins. Build function type
5287 for DEF with return type RET and N arguments. If VAR is true, then the
5288 function should be variadic after those N arguments.
5290 Takes special care not to ICE if any of the types involved are
5291 error_mark_node, which indicates that said type is not in fact available
5292 (see builtin_type_for_size). In which case the function type as a whole
5293 should be error_mark_node. */
5295 static void
5296 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5298 tree t;
5299 tree *args = XALLOCAVEC (tree, n);
5300 va_list list;
5301 int i;
5303 va_start (list, n);
5304 for (i = 0; i < n; ++i)
5306 builtin_type a = (builtin_type) va_arg (list, int);
5307 t = builtin_types[a];
5308 if (t == error_mark_node)
5309 goto egress;
5310 args[i] = t;
5313 t = builtin_types[ret];
5314 if (t == error_mark_node)
5315 goto egress;
5316 if (var)
5317 t = build_varargs_function_type_array (t, n, args);
5318 else
5319 t = build_function_type_array (t, n, args);
5321 egress:
5322 builtin_types[def] = t;
5323 va_end (list);
5326 /* Build builtin functions common to both C and C++ language
5327 frontends. */
5329 static void
5330 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5332 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5333 builtin_types[ENUM] = VALUE;
5334 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5335 def_fn_type (ENUM, RETURN, 0, 0);
5336 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5337 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5338 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5339 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5340 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5341 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5342 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5343 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5344 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5345 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5346 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5347 ARG6) \
5348 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5349 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5350 ARG6, ARG7) \
5351 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5352 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5353 ARG6, ARG7, ARG8) \
5354 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5355 ARG7, ARG8);
5356 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5357 def_fn_type (ENUM, RETURN, 1, 0);
5358 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5359 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5360 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5361 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5362 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5363 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5364 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5365 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5366 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5367 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5368 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5369 ARG6, ARG7) \
5370 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5371 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5372 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5373 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5374 ARG7, ARG8, ARG9, ARG10, ARG11);
5375 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5376 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5378 #include "builtin-types.def"
5380 #undef DEF_PRIMITIVE_TYPE
5381 #undef DEF_FUNCTION_TYPE_0
5382 #undef DEF_FUNCTION_TYPE_1
5383 #undef DEF_FUNCTION_TYPE_2
5384 #undef DEF_FUNCTION_TYPE_3
5385 #undef DEF_FUNCTION_TYPE_4
5386 #undef DEF_FUNCTION_TYPE_5
5387 #undef DEF_FUNCTION_TYPE_6
5388 #undef DEF_FUNCTION_TYPE_7
5389 #undef DEF_FUNCTION_TYPE_8
5390 #undef DEF_FUNCTION_TYPE_VAR_0
5391 #undef DEF_FUNCTION_TYPE_VAR_1
5392 #undef DEF_FUNCTION_TYPE_VAR_2
5393 #undef DEF_FUNCTION_TYPE_VAR_3
5394 #undef DEF_FUNCTION_TYPE_VAR_4
5395 #undef DEF_FUNCTION_TYPE_VAR_5
5396 #undef DEF_FUNCTION_TYPE_VAR_7
5397 #undef DEF_FUNCTION_TYPE_VAR_11
5398 #undef DEF_POINTER_TYPE
5399 builtin_types[(int) BT_LAST] = NULL_TREE;
5401 c_init_attributes ();
5403 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5404 NONANSI_P, ATTRS, IMPLICIT, COND) \
5405 if (NAME && COND) \
5406 def_builtin_1 (ENUM, NAME, CLASS, \
5407 builtin_types[(int) TYPE], \
5408 builtin_types[(int) LIBTYPE], \
5409 BOTH_P, FALLBACK_P, NONANSI_P, \
5410 built_in_attributes[(int) ATTRS], IMPLICIT);
5411 #include "builtins.def"
5412 #undef DEF_BUILTIN
5414 targetm.init_builtins ();
5416 build_common_builtin_nodes ();
5418 if (flag_cilkplus)
5419 cilk_init_builtins ();
5422 /* Like get_identifier, but avoid warnings about null arguments when
5423 the argument may be NULL for targets where GCC lacks stdint.h type
5424 information. */
5426 static inline tree
5427 c_get_ident (const char *id)
5429 return get_identifier (id);
5432 /* Build tree nodes and builtin functions common to both C and C++ language
5433 frontends. */
5435 void
5436 c_common_nodes_and_builtins (void)
5438 int char16_type_size;
5439 int char32_type_size;
5440 int wchar_type_size;
5441 tree array_domain_type;
5442 tree va_list_ref_type_node;
5443 tree va_list_arg_type_node;
5444 int i;
5446 build_common_tree_nodes (flag_signed_char, flag_short_double);
5448 /* Define `int' and `char' first so that dbx will output them first. */
5449 record_builtin_type (RID_INT, NULL, integer_type_node);
5450 record_builtin_type (RID_CHAR, "char", char_type_node);
5452 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5453 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5454 but not C. Are the conditionals here needed? */
5455 if (c_dialect_cxx ())
5456 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5457 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5458 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5459 record_builtin_type (RID_MAX, "long unsigned int",
5460 long_unsigned_type_node);
5462 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5464 char name[25];
5466 sprintf (name, "__int%d", int_n_data[i].bitsize);
5467 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5468 int_n_trees[i].signed_type);
5469 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5470 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5473 if (c_dialect_cxx ())
5474 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5475 record_builtin_type (RID_MAX, "long long int",
5476 long_long_integer_type_node);
5477 record_builtin_type (RID_MAX, "long long unsigned int",
5478 long_long_unsigned_type_node);
5479 if (c_dialect_cxx ())
5480 record_builtin_type (RID_MAX, "long long unsigned",
5481 long_long_unsigned_type_node);
5482 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5483 record_builtin_type (RID_MAX, "short unsigned int",
5484 short_unsigned_type_node);
5485 if (c_dialect_cxx ())
5486 record_builtin_type (RID_MAX, "unsigned short",
5487 short_unsigned_type_node);
5489 /* Define both `signed char' and `unsigned char'. */
5490 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5491 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5493 /* These are types that c_common_type_for_size and
5494 c_common_type_for_mode use. */
5495 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5496 TYPE_DECL, NULL_TREE,
5497 intQI_type_node));
5498 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5499 TYPE_DECL, NULL_TREE,
5500 intHI_type_node));
5501 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5502 TYPE_DECL, NULL_TREE,
5503 intSI_type_node));
5504 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5505 TYPE_DECL, NULL_TREE,
5506 intDI_type_node));
5507 #if HOST_BITS_PER_WIDE_INT >= 64
5508 /* Note that this is different than the __int128 type that's part of
5509 the generic __intN support. */
5510 if (targetm.scalar_mode_supported_p (TImode))
5511 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5512 TYPE_DECL,
5513 get_identifier ("__int128_t"),
5514 intTI_type_node));
5515 #endif
5516 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5517 TYPE_DECL, NULL_TREE,
5518 unsigned_intQI_type_node));
5519 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5520 TYPE_DECL, NULL_TREE,
5521 unsigned_intHI_type_node));
5522 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5523 TYPE_DECL, NULL_TREE,
5524 unsigned_intSI_type_node));
5525 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5526 TYPE_DECL, NULL_TREE,
5527 unsigned_intDI_type_node));
5528 #if HOST_BITS_PER_WIDE_INT >= 64
5529 if (targetm.scalar_mode_supported_p (TImode))
5530 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5531 TYPE_DECL,
5532 get_identifier ("__uint128_t"),
5533 unsigned_intTI_type_node));
5534 #endif
5536 /* Create the widest literal types. */
5537 widest_integer_literal_type_node
5538 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5539 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5540 TYPE_DECL, NULL_TREE,
5541 widest_integer_literal_type_node));
5543 widest_unsigned_literal_type_node
5544 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5545 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5546 TYPE_DECL, NULL_TREE,
5547 widest_unsigned_literal_type_node));
5549 signed_size_type_node = c_common_signed_type (size_type_node);
5551 pid_type_node =
5552 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5554 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5555 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5556 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5558 /* Only supported decimal floating point extension if the target
5559 actually supports underlying modes. */
5560 if (targetm.scalar_mode_supported_p (SDmode)
5561 && targetm.scalar_mode_supported_p (DDmode)
5562 && targetm.scalar_mode_supported_p (TDmode))
5564 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5565 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5566 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5569 if (targetm.fixed_point_supported_p ())
5571 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5572 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5573 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5574 record_builtin_type (RID_MAX, "long long _Fract",
5575 long_long_fract_type_node);
5576 record_builtin_type (RID_MAX, "unsigned short _Fract",
5577 unsigned_short_fract_type_node);
5578 record_builtin_type (RID_MAX, "unsigned _Fract",
5579 unsigned_fract_type_node);
5580 record_builtin_type (RID_MAX, "unsigned long _Fract",
5581 unsigned_long_fract_type_node);
5582 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5583 unsigned_long_long_fract_type_node);
5584 record_builtin_type (RID_MAX, "_Sat short _Fract",
5585 sat_short_fract_type_node);
5586 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5587 record_builtin_type (RID_MAX, "_Sat long _Fract",
5588 sat_long_fract_type_node);
5589 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5590 sat_long_long_fract_type_node);
5591 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5592 sat_unsigned_short_fract_type_node);
5593 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5594 sat_unsigned_fract_type_node);
5595 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5596 sat_unsigned_long_fract_type_node);
5597 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5598 sat_unsigned_long_long_fract_type_node);
5599 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5600 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5601 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5602 record_builtin_type (RID_MAX, "long long _Accum",
5603 long_long_accum_type_node);
5604 record_builtin_type (RID_MAX, "unsigned short _Accum",
5605 unsigned_short_accum_type_node);
5606 record_builtin_type (RID_MAX, "unsigned _Accum",
5607 unsigned_accum_type_node);
5608 record_builtin_type (RID_MAX, "unsigned long _Accum",
5609 unsigned_long_accum_type_node);
5610 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5611 unsigned_long_long_accum_type_node);
5612 record_builtin_type (RID_MAX, "_Sat short _Accum",
5613 sat_short_accum_type_node);
5614 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5615 record_builtin_type (RID_MAX, "_Sat long _Accum",
5616 sat_long_accum_type_node);
5617 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5618 sat_long_long_accum_type_node);
5619 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5620 sat_unsigned_short_accum_type_node);
5621 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5622 sat_unsigned_accum_type_node);
5623 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5624 sat_unsigned_long_accum_type_node);
5625 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5626 sat_unsigned_long_long_accum_type_node);
5630 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5631 TYPE_DECL,
5632 get_identifier ("complex int"),
5633 complex_integer_type_node));
5634 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5635 TYPE_DECL,
5636 get_identifier ("complex float"),
5637 complex_float_type_node));
5638 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5639 TYPE_DECL,
5640 get_identifier ("complex double"),
5641 complex_double_type_node));
5642 lang_hooks.decls.pushdecl
5643 (build_decl (UNKNOWN_LOCATION,
5644 TYPE_DECL, get_identifier ("complex long double"),
5645 complex_long_double_type_node));
5647 if (c_dialect_cxx ())
5648 /* For C++, make fileptr_type_node a distinct void * type until
5649 FILE type is defined. */
5650 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5652 record_builtin_type (RID_VOID, NULL, void_type_node);
5654 /* Set the TYPE_NAME for any variants that were built before
5655 record_builtin_type gave names to the built-in types. */
5657 tree void_name = TYPE_NAME (void_type_node);
5658 TYPE_NAME (void_type_node) = NULL_TREE;
5659 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5660 = void_name;
5661 TYPE_NAME (void_type_node) = void_name;
5664 void_list_node = build_void_list_node ();
5666 /* Make a type to be the domain of a few array types
5667 whose domains don't really matter.
5668 200 is small enough that it always fits in size_t
5669 and large enough that it can hold most function names for the
5670 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5671 array_domain_type = build_index_type (size_int (200));
5673 /* Make a type for arrays of characters.
5674 With luck nothing will ever really depend on the length of this
5675 array type. */
5676 char_array_type_node
5677 = build_array_type (char_type_node, array_domain_type);
5679 string_type_node = build_pointer_type (char_type_node);
5680 const_string_type_node
5681 = build_pointer_type (build_qualified_type
5682 (char_type_node, TYPE_QUAL_CONST));
5684 /* This is special for C++ so functions can be overloaded. */
5685 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5686 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5687 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5688 underlying_wchar_type_node = wchar_type_node;
5689 if (c_dialect_cxx ())
5691 if (TYPE_UNSIGNED (wchar_type_node))
5692 wchar_type_node = make_unsigned_type (wchar_type_size);
5693 else
5694 wchar_type_node = make_signed_type (wchar_type_size);
5695 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5698 /* This is for wide string constants. */
5699 wchar_array_type_node
5700 = build_array_type (wchar_type_node, array_domain_type);
5702 /* Define 'char16_t'. */
5703 char16_type_node = get_identifier (CHAR16_TYPE);
5704 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5705 char16_type_size = TYPE_PRECISION (char16_type_node);
5706 if (c_dialect_cxx ())
5708 char16_type_node = make_unsigned_type (char16_type_size);
5710 if (cxx_dialect >= cxx11)
5711 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5714 /* This is for UTF-16 string constants. */
5715 char16_array_type_node
5716 = build_array_type (char16_type_node, array_domain_type);
5718 /* Define 'char32_t'. */
5719 char32_type_node = get_identifier (CHAR32_TYPE);
5720 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5721 char32_type_size = TYPE_PRECISION (char32_type_node);
5722 if (c_dialect_cxx ())
5724 char32_type_node = make_unsigned_type (char32_type_size);
5726 if (cxx_dialect >= cxx11)
5727 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5730 /* This is for UTF-32 string constants. */
5731 char32_array_type_node
5732 = build_array_type (char32_type_node, array_domain_type);
5734 wint_type_node =
5735 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5737 intmax_type_node =
5738 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5739 uintmax_type_node =
5740 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5742 if (SIG_ATOMIC_TYPE)
5743 sig_atomic_type_node =
5744 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5745 if (INT8_TYPE)
5746 int8_type_node =
5747 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5748 if (INT16_TYPE)
5749 int16_type_node =
5750 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5751 if (INT32_TYPE)
5752 int32_type_node =
5753 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5754 if (INT64_TYPE)
5755 int64_type_node =
5756 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5757 if (UINT8_TYPE)
5758 uint8_type_node =
5759 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5760 if (UINT16_TYPE)
5761 c_uint16_type_node = uint16_type_node =
5762 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5763 if (UINT32_TYPE)
5764 c_uint32_type_node = uint32_type_node =
5765 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5766 if (UINT64_TYPE)
5767 c_uint64_type_node = uint64_type_node =
5768 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5769 if (INT_LEAST8_TYPE)
5770 int_least8_type_node =
5771 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5772 if (INT_LEAST16_TYPE)
5773 int_least16_type_node =
5774 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5775 if (INT_LEAST32_TYPE)
5776 int_least32_type_node =
5777 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5778 if (INT_LEAST64_TYPE)
5779 int_least64_type_node =
5780 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5781 if (UINT_LEAST8_TYPE)
5782 uint_least8_type_node =
5783 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5784 if (UINT_LEAST16_TYPE)
5785 uint_least16_type_node =
5786 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5787 if (UINT_LEAST32_TYPE)
5788 uint_least32_type_node =
5789 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5790 if (UINT_LEAST64_TYPE)
5791 uint_least64_type_node =
5792 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5793 if (INT_FAST8_TYPE)
5794 int_fast8_type_node =
5795 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5796 if (INT_FAST16_TYPE)
5797 int_fast16_type_node =
5798 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5799 if (INT_FAST32_TYPE)
5800 int_fast32_type_node =
5801 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5802 if (INT_FAST64_TYPE)
5803 int_fast64_type_node =
5804 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5805 if (UINT_FAST8_TYPE)
5806 uint_fast8_type_node =
5807 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5808 if (UINT_FAST16_TYPE)
5809 uint_fast16_type_node =
5810 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5811 if (UINT_FAST32_TYPE)
5812 uint_fast32_type_node =
5813 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5814 if (UINT_FAST64_TYPE)
5815 uint_fast64_type_node =
5816 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5817 if (INTPTR_TYPE)
5818 intptr_type_node =
5819 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5820 if (UINTPTR_TYPE)
5821 uintptr_type_node =
5822 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5824 default_function_type
5825 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5826 ptrdiff_type_node
5827 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5828 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5830 lang_hooks.decls.pushdecl
5831 (build_decl (UNKNOWN_LOCATION,
5832 TYPE_DECL, get_identifier ("__builtin_va_list"),
5833 va_list_type_node));
5834 if (targetm.enum_va_list_p)
5836 int l;
5837 const char *pname;
5838 tree ptype;
5840 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5842 lang_hooks.decls.pushdecl
5843 (build_decl (UNKNOWN_LOCATION,
5844 TYPE_DECL, get_identifier (pname),
5845 ptype));
5850 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5852 va_list_arg_type_node = va_list_ref_type_node =
5853 build_pointer_type (TREE_TYPE (va_list_type_node));
5855 else
5857 va_list_arg_type_node = va_list_type_node;
5858 va_list_ref_type_node = build_reference_type (va_list_type_node);
5861 if (!flag_preprocess_only)
5862 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5864 main_identifier_node = get_identifier ("main");
5866 /* Create the built-in __null node. It is important that this is
5867 not shared. */
5868 null_node = make_int_cst (1, 1);
5869 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5871 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5872 memset (builtin_types, 0, sizeof (builtin_types));
5875 /* The number of named compound-literals generated thus far. */
5876 static GTY(()) int compound_literal_number;
5878 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5880 void
5881 set_compound_literal_name (tree decl)
5883 char *name;
5884 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5885 compound_literal_number);
5886 compound_literal_number++;
5887 DECL_NAME (decl) = get_identifier (name);
5890 tree
5891 build_va_arg (location_t loc, tree expr, tree type)
5893 expr = build1 (VA_ARG_EXPR, type, expr);
5894 SET_EXPR_LOCATION (expr, loc);
5895 return expr;
5899 /* Linked list of disabled built-in functions. */
5901 typedef struct disabled_builtin
5903 const char *name;
5904 struct disabled_builtin *next;
5905 } disabled_builtin;
5906 static disabled_builtin *disabled_builtins = NULL;
5908 static bool builtin_function_disabled_p (const char *);
5910 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5911 begins with "__builtin_", give an error. */
5913 void
5914 disable_builtin_function (const char *name)
5916 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5917 error ("cannot disable built-in function %qs", name);
5918 else
5920 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5921 new_disabled_builtin->name = name;
5922 new_disabled_builtin->next = disabled_builtins;
5923 disabled_builtins = new_disabled_builtin;
5928 /* Return true if the built-in function NAME has been disabled, false
5929 otherwise. */
5931 static bool
5932 builtin_function_disabled_p (const char *name)
5934 disabled_builtin *p;
5935 for (p = disabled_builtins; p != NULL; p = p->next)
5937 if (strcmp (name, p->name) == 0)
5938 return true;
5940 return false;
5944 /* Worker for DEF_BUILTIN.
5945 Possibly define a builtin function with one or two names.
5946 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5947 nonansi_p and flag_no_nonansi_builtin. */
5949 static void
5950 def_builtin_1 (enum built_in_function fncode,
5951 const char *name,
5952 enum built_in_class fnclass,
5953 tree fntype, tree libtype,
5954 bool both_p, bool fallback_p, bool nonansi_p,
5955 tree fnattrs, bool implicit_p)
5957 tree decl;
5958 const char *libname;
5960 if (fntype == error_mark_node)
5961 return;
5963 gcc_assert ((!both_p && !fallback_p)
5964 || !strncmp (name, "__builtin_",
5965 strlen ("__builtin_")));
5967 libname = name + strlen ("__builtin_");
5968 decl = add_builtin_function (name, fntype, fncode, fnclass,
5969 (fallback_p ? libname : NULL),
5970 fnattrs);
5972 set_builtin_decl (fncode, decl, implicit_p);
5974 if (both_p
5975 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5976 && !(nonansi_p && flag_no_nonansi_builtin))
5977 add_builtin_function (libname, libtype, fncode, fnclass,
5978 NULL, fnattrs);
5981 /* Nonzero if the type T promotes to int. This is (nearly) the
5982 integral promotions defined in ISO C99 6.3.1.1/2. */
5984 bool
5985 c_promoting_integer_type_p (const_tree t)
5987 switch (TREE_CODE (t))
5989 case INTEGER_TYPE:
5990 return (TYPE_MAIN_VARIANT (t) == char_type_node
5991 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5992 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5993 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5994 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5995 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5997 case ENUMERAL_TYPE:
5998 /* ??? Technically all enumerations not larger than an int
5999 promote to an int. But this is used along code paths
6000 that only want to notice a size change. */
6001 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6003 case BOOLEAN_TYPE:
6004 return 1;
6006 default:
6007 return 0;
6011 /* Return 1 if PARMS specifies a fixed number of parameters
6012 and none of their types is affected by default promotions. */
6015 self_promoting_args_p (const_tree parms)
6017 const_tree t;
6018 for (t = parms; t; t = TREE_CHAIN (t))
6020 tree type = TREE_VALUE (t);
6022 if (type == error_mark_node)
6023 continue;
6025 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6026 return 0;
6028 if (type == 0)
6029 return 0;
6031 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6032 return 0;
6034 if (c_promoting_integer_type_p (type))
6035 return 0;
6037 return 1;
6040 /* Recursively remove any '*' or '&' operator from TYPE. */
6041 tree
6042 strip_pointer_operator (tree t)
6044 while (POINTER_TYPE_P (t))
6045 t = TREE_TYPE (t);
6046 return t;
6049 /* Recursively remove pointer or array type from TYPE. */
6050 tree
6051 strip_pointer_or_array_types (tree t)
6053 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6054 t = TREE_TYPE (t);
6055 return t;
6058 /* Used to compare case labels. K1 and K2 are actually tree nodes
6059 representing case labels, or NULL_TREE for a `default' label.
6060 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6061 K2, and 0 if K1 and K2 are equal. */
6064 case_compare (splay_tree_key k1, splay_tree_key k2)
6066 /* Consider a NULL key (such as arises with a `default' label) to be
6067 smaller than anything else. */
6068 if (!k1)
6069 return k2 ? -1 : 0;
6070 else if (!k2)
6071 return k1 ? 1 : 0;
6073 return tree_int_cst_compare ((tree) k1, (tree) k2);
6076 /* Process a case label, located at LOC, for the range LOW_VALUE
6077 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6078 then this case label is actually a `default' label. If only
6079 HIGH_VALUE is NULL_TREE, then case label was declared using the
6080 usual C/C++ syntax, rather than the GNU case range extension.
6081 CASES is a tree containing all the case ranges processed so far;
6082 COND is the condition for the switch-statement itself. Returns the
6083 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6084 is created. */
6086 tree
6087 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6088 tree low_value, tree high_value)
6090 tree type;
6091 tree label;
6092 tree case_label;
6093 splay_tree_node node;
6095 /* Create the LABEL_DECL itself. */
6096 label = create_artificial_label (loc);
6098 /* If there was an error processing the switch condition, bail now
6099 before we get more confused. */
6100 if (!cond || cond == error_mark_node)
6101 goto error_out;
6103 if ((low_value && TREE_TYPE (low_value)
6104 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6105 || (high_value && TREE_TYPE (high_value)
6106 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6108 error_at (loc, "pointers are not permitted as case values");
6109 goto error_out;
6112 /* Case ranges are a GNU extension. */
6113 if (high_value)
6114 pedwarn (loc, OPT_Wpedantic,
6115 "range expressions in switch statements are non-standard");
6117 type = TREE_TYPE (cond);
6118 if (low_value)
6120 low_value = check_case_value (loc, low_value);
6121 low_value = convert_and_check (loc, type, low_value);
6122 if (low_value == error_mark_node)
6123 goto error_out;
6125 if (high_value)
6127 high_value = check_case_value (loc, high_value);
6128 high_value = convert_and_check (loc, type, high_value);
6129 if (high_value == error_mark_node)
6130 goto error_out;
6133 if (low_value && high_value)
6135 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6136 really a case range, even though it was written that way.
6137 Remove the HIGH_VALUE to simplify later processing. */
6138 if (tree_int_cst_equal (low_value, high_value))
6139 high_value = NULL_TREE;
6140 else if (!tree_int_cst_lt (low_value, high_value))
6141 warning_at (loc, 0, "empty range specified");
6144 /* See if the case is in range of the type of the original testing
6145 expression. If both low_value and high_value are out of range,
6146 don't insert the case label and return NULL_TREE. */
6147 if (low_value
6148 && !check_case_bounds (loc, type, orig_type,
6149 &low_value, high_value ? &high_value : NULL))
6150 return NULL_TREE;
6152 /* Look up the LOW_VALUE in the table of case labels we already
6153 have. */
6154 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6155 /* If there was not an exact match, check for overlapping ranges.
6156 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6157 that's a `default' label and the only overlap is an exact match. */
6158 if (!node && (low_value || high_value))
6160 splay_tree_node low_bound;
6161 splay_tree_node high_bound;
6163 /* Even though there wasn't an exact match, there might be an
6164 overlap between this case range and another case range.
6165 Since we've (inductively) not allowed any overlapping case
6166 ranges, we simply need to find the greatest low case label
6167 that is smaller that LOW_VALUE, and the smallest low case
6168 label that is greater than LOW_VALUE. If there is an overlap
6169 it will occur in one of these two ranges. */
6170 low_bound = splay_tree_predecessor (cases,
6171 (splay_tree_key) low_value);
6172 high_bound = splay_tree_successor (cases,
6173 (splay_tree_key) low_value);
6175 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6176 the LOW_VALUE, so there is no need to check unless the
6177 LOW_BOUND is in fact itself a case range. */
6178 if (low_bound
6179 && CASE_HIGH ((tree) low_bound->value)
6180 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6181 low_value) >= 0)
6182 node = low_bound;
6183 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6184 range is bigger than the low end of the current range, so we
6185 are only interested if the current range is a real range, and
6186 not an ordinary case label. */
6187 else if (high_bound
6188 && high_value
6189 && (tree_int_cst_compare ((tree) high_bound->key,
6190 high_value)
6191 <= 0))
6192 node = high_bound;
6194 /* If there was an overlap, issue an error. */
6195 if (node)
6197 tree duplicate = CASE_LABEL ((tree) node->value);
6199 if (high_value)
6201 error_at (loc, "duplicate (or overlapping) case value");
6202 error_at (DECL_SOURCE_LOCATION (duplicate),
6203 "this is the first entry overlapping that value");
6205 else if (low_value)
6207 error_at (loc, "duplicate case value") ;
6208 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6210 else
6212 error_at (loc, "multiple default labels in one switch");
6213 error_at (DECL_SOURCE_LOCATION (duplicate),
6214 "this is the first default label");
6216 goto error_out;
6219 /* Add a CASE_LABEL to the statement-tree. */
6220 case_label = add_stmt (build_case_label (low_value, high_value, label));
6221 /* Register this case label in the splay tree. */
6222 splay_tree_insert (cases,
6223 (splay_tree_key) low_value,
6224 (splay_tree_value) case_label);
6226 return case_label;
6228 error_out:
6229 /* Add a label so that the back-end doesn't think that the beginning of
6230 the switch is unreachable. Note that we do not add a case label, as
6231 that just leads to duplicates and thence to failure later on. */
6232 if (!cases->root)
6234 tree t = create_artificial_label (loc);
6235 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6237 return error_mark_node;
6240 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6241 Used to verify that case values match up with enumerator values. */
6243 static void
6244 match_case_to_enum_1 (tree key, tree type, tree label)
6246 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6248 if (tree_fits_uhwi_p (key))
6249 print_dec (key, buf, UNSIGNED);
6250 else if (tree_fits_shwi_p (key))
6251 print_dec (key, buf, SIGNED);
6252 else
6253 print_hex (key, buf);
6255 if (TYPE_NAME (type) == 0)
6256 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6257 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6258 "case value %qs not in enumerated type",
6259 buf);
6260 else
6261 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6262 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6263 "case value %qs not in enumerated type %qT",
6264 buf, type);
6267 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6268 Used to verify that case values match up with enumerator values. */
6270 static int
6271 match_case_to_enum (splay_tree_node node, void *data)
6273 tree label = (tree) node->value;
6274 tree type = (tree) data;
6276 /* Skip default case. */
6277 if (!CASE_LOW (label))
6278 return 0;
6280 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6281 when we did our enum->case scan. Reset our scratch bit after. */
6282 if (!CASE_LOW_SEEN (label))
6283 match_case_to_enum_1 (CASE_LOW (label), type, label);
6284 else
6285 CASE_LOW_SEEN (label) = 0;
6287 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6288 not set, that means that CASE_HIGH did not appear when we did our
6289 enum->case scan. Reset our scratch bit after. */
6290 if (CASE_HIGH (label))
6292 if (!CASE_HIGH_SEEN (label))
6293 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6294 else
6295 CASE_HIGH_SEEN (label) = 0;
6298 return 0;
6301 /* Handle -Wswitch*. Called from the front end after parsing the
6302 switch construct. */
6303 /* ??? Should probably be somewhere generic, since other languages
6304 besides C and C++ would want this. At the moment, however, C/C++
6305 are the only tree-ssa languages that support enumerations at all,
6306 so the point is moot. */
6308 void
6309 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6310 tree type, tree cond)
6312 splay_tree_node default_node;
6313 splay_tree_node node;
6314 tree chain;
6316 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6317 return;
6319 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6320 if (!default_node)
6321 warning_at (switch_location, OPT_Wswitch_default,
6322 "switch missing default case");
6324 /* From here on, we only care about about enumerated types. */
6325 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6326 return;
6328 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6329 if (!warn_switch_enum && !warn_switch)
6330 return;
6332 /* Check the cases. Warn about case values which are not members of
6333 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6334 there is no default case, check that exactly all enumeration
6335 literals are covered by the cases. */
6337 /* Clearing COND if it is not an integer constant simplifies
6338 the tests inside the loop below. */
6339 if (TREE_CODE (cond) != INTEGER_CST)
6340 cond = NULL_TREE;
6342 /* The time complexity here is O(N*lg(N)) worst case, but for the
6343 common case of monotonically increasing enumerators, it is
6344 O(N), since the nature of the splay tree will keep the next
6345 element adjacent to the root at all times. */
6347 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6349 tree value = TREE_VALUE (chain);
6350 if (TREE_CODE (value) == CONST_DECL)
6351 value = DECL_INITIAL (value);
6352 node = splay_tree_lookup (cases, (splay_tree_key) value);
6353 if (node)
6355 /* Mark the CASE_LOW part of the case entry as seen. */
6356 tree label = (tree) node->value;
6357 CASE_LOW_SEEN (label) = 1;
6358 continue;
6361 /* Even though there wasn't an exact match, there might be a
6362 case range which includes the enumerator's value. */
6363 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6364 if (node && CASE_HIGH ((tree) node->value))
6366 tree label = (tree) node->value;
6367 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6368 if (cmp >= 0)
6370 /* If we match the upper bound exactly, mark the CASE_HIGH
6371 part of the case entry as seen. */
6372 if (cmp == 0)
6373 CASE_HIGH_SEEN (label) = 1;
6374 continue;
6378 /* We've now determined that this enumerated literal isn't
6379 handled by the case labels of the switch statement. */
6381 /* If the switch expression is a constant, we only really care
6382 about whether that constant is handled by the switch. */
6383 if (cond && tree_int_cst_compare (cond, value))
6384 continue;
6386 /* If there is a default_node, the only relevant option is
6387 Wswitch-enum. Otherwise, if both are enabled then we prefer
6388 to warn using -Wswitch because -Wswitch is enabled by -Wall
6389 while -Wswitch-enum is explicit. */
6390 warning_at (switch_location,
6391 (default_node || !warn_switch
6392 ? OPT_Wswitch_enum
6393 : OPT_Wswitch),
6394 "enumeration value %qE not handled in switch",
6395 TREE_PURPOSE (chain));
6398 /* Warn if there are case expressions that don't correspond to
6399 enumerators. This can occur since C and C++ don't enforce
6400 type-checking of assignments to enumeration variables.
6402 The time complexity here is now always O(N) worst case, since
6403 we should have marked both the lower bound and upper bound of
6404 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6405 above. This scan also resets those fields. */
6407 splay_tree_foreach (cases, match_case_to_enum, type);
6410 /* Finish an expression taking the address of LABEL (an
6411 IDENTIFIER_NODE). Returns an expression for the address.
6413 LOC is the location for the expression returned. */
6415 tree
6416 finish_label_address_expr (tree label, location_t loc)
6418 tree result;
6420 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6422 if (label == error_mark_node)
6423 return error_mark_node;
6425 label = lookup_label (label);
6426 if (label == NULL_TREE)
6427 result = null_pointer_node;
6428 else
6430 TREE_USED (label) = 1;
6431 result = build1 (ADDR_EXPR, ptr_type_node, label);
6432 /* The current function is not necessarily uninlinable.
6433 Computed gotos are incompatible with inlining, but the value
6434 here could be used only in a diagnostic, for example. */
6435 protected_set_expr_location (result, loc);
6438 return result;
6442 /* Given a boolean expression ARG, return a tree representing an increment
6443 or decrement (as indicated by CODE) of ARG. The front end must check for
6444 invalid cases (e.g., decrement in C++). */
6445 tree
6446 boolean_increment (enum tree_code code, tree arg)
6448 tree val;
6449 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6451 arg = stabilize_reference (arg);
6452 switch (code)
6454 case PREINCREMENT_EXPR:
6455 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6456 break;
6457 case POSTINCREMENT_EXPR:
6458 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6459 arg = save_expr (arg);
6460 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6461 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6462 break;
6463 case PREDECREMENT_EXPR:
6464 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6465 invert_truthvalue_loc (input_location, arg));
6466 break;
6467 case POSTDECREMENT_EXPR:
6468 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6469 invert_truthvalue_loc (input_location, arg));
6470 arg = save_expr (arg);
6471 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6472 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6473 break;
6474 default:
6475 gcc_unreachable ();
6477 TREE_SIDE_EFFECTS (val) = 1;
6478 return val;
6481 /* Built-in macros for stddef.h and stdint.h, that require macros
6482 defined in this file. */
6483 void
6484 c_stddef_cpp_builtins(void)
6486 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6487 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6488 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6489 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6490 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6491 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6492 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6493 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6494 if (SIG_ATOMIC_TYPE)
6495 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6496 if (INT8_TYPE)
6497 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6498 if (INT16_TYPE)
6499 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6500 if (INT32_TYPE)
6501 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6502 if (INT64_TYPE)
6503 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6504 if (UINT8_TYPE)
6505 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6506 if (UINT16_TYPE)
6507 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6508 if (UINT32_TYPE)
6509 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6510 if (UINT64_TYPE)
6511 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6512 if (INT_LEAST8_TYPE)
6513 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6514 if (INT_LEAST16_TYPE)
6515 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6516 if (INT_LEAST32_TYPE)
6517 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6518 if (INT_LEAST64_TYPE)
6519 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6520 if (UINT_LEAST8_TYPE)
6521 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6522 if (UINT_LEAST16_TYPE)
6523 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6524 if (UINT_LEAST32_TYPE)
6525 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6526 if (UINT_LEAST64_TYPE)
6527 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6528 if (INT_FAST8_TYPE)
6529 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6530 if (INT_FAST16_TYPE)
6531 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6532 if (INT_FAST32_TYPE)
6533 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6534 if (INT_FAST64_TYPE)
6535 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6536 if (UINT_FAST8_TYPE)
6537 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6538 if (UINT_FAST16_TYPE)
6539 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6540 if (UINT_FAST32_TYPE)
6541 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6542 if (UINT_FAST64_TYPE)
6543 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6544 if (INTPTR_TYPE)
6545 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6546 if (UINTPTR_TYPE)
6547 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6550 static void
6551 c_init_attributes (void)
6553 /* Fill in the built_in_attributes array. */
6554 #define DEF_ATTR_NULL_TREE(ENUM) \
6555 built_in_attributes[(int) ENUM] = NULL_TREE;
6556 #define DEF_ATTR_INT(ENUM, VALUE) \
6557 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6558 #define DEF_ATTR_STRING(ENUM, VALUE) \
6559 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6560 #define DEF_ATTR_IDENT(ENUM, STRING) \
6561 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6562 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6563 built_in_attributes[(int) ENUM] \
6564 = tree_cons (built_in_attributes[(int) PURPOSE], \
6565 built_in_attributes[(int) VALUE], \
6566 built_in_attributes[(int) CHAIN]);
6567 #include "builtin-attrs.def"
6568 #undef DEF_ATTR_NULL_TREE
6569 #undef DEF_ATTR_INT
6570 #undef DEF_ATTR_IDENT
6571 #undef DEF_ATTR_TREE_LIST
6574 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6575 identifier as an argument, so the front end shouldn't look it up. */
6577 bool
6578 attribute_takes_identifier_p (const_tree attr_id)
6580 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6581 if (spec == NULL)
6582 /* Unknown attribute that we'll end up ignoring, return true so we
6583 don't complain about an identifier argument. */
6584 return true;
6585 else if (!strcmp ("mode", spec->name)
6586 || !strcmp ("format", spec->name)
6587 || !strcmp ("cleanup", spec->name))
6588 return true;
6589 else
6590 return targetm.attribute_takes_identifier_p (attr_id);
6593 /* Attribute handlers common to C front ends. */
6595 /* Handle a "packed" attribute; arguments as in
6596 struct attribute_spec.handler. */
6598 static tree
6599 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6600 int flags, bool *no_add_attrs)
6602 if (TYPE_P (*node))
6604 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6605 *node = build_variant_type_copy (*node);
6606 TYPE_PACKED (*node) = 1;
6608 else if (TREE_CODE (*node) == FIELD_DECL)
6610 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6611 /* Still pack bitfields. */
6612 && ! DECL_INITIAL (*node))
6613 warning (OPT_Wattributes,
6614 "%qE attribute ignored for field of type %qT",
6615 name, TREE_TYPE (*node));
6616 else
6617 DECL_PACKED (*node) = 1;
6619 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6620 used for DECL_REGISTER. It wouldn't mean anything anyway.
6621 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6622 that changes what the typedef is typing. */
6623 else
6625 warning (OPT_Wattributes, "%qE attribute ignored", name);
6626 *no_add_attrs = true;
6629 return NULL_TREE;
6632 /* Handle a "nocommon" attribute; arguments as in
6633 struct attribute_spec.handler. */
6635 static tree
6636 handle_nocommon_attribute (tree *node, tree name,
6637 tree ARG_UNUSED (args),
6638 int ARG_UNUSED (flags), bool *no_add_attrs)
6640 if (TREE_CODE (*node) == VAR_DECL)
6641 DECL_COMMON (*node) = 0;
6642 else
6644 warning (OPT_Wattributes, "%qE attribute ignored", name);
6645 *no_add_attrs = true;
6648 return NULL_TREE;
6651 /* Handle a "common" attribute; arguments as in
6652 struct attribute_spec.handler. */
6654 static tree
6655 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6656 int ARG_UNUSED (flags), bool *no_add_attrs)
6658 if (TREE_CODE (*node) == VAR_DECL)
6659 DECL_COMMON (*node) = 1;
6660 else
6662 warning (OPT_Wattributes, "%qE attribute ignored", name);
6663 *no_add_attrs = true;
6666 return NULL_TREE;
6669 /* Handle a "noreturn" attribute; arguments as in
6670 struct attribute_spec.handler. */
6672 static tree
6673 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6674 int ARG_UNUSED (flags), bool *no_add_attrs)
6676 tree type = TREE_TYPE (*node);
6678 /* See FIXME comment in c_common_attribute_table. */
6679 if (TREE_CODE (*node) == FUNCTION_DECL
6680 || objc_method_decl (TREE_CODE (*node)))
6681 TREE_THIS_VOLATILE (*node) = 1;
6682 else if (TREE_CODE (type) == POINTER_TYPE
6683 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6684 TREE_TYPE (*node)
6685 = (build_qualified_type
6686 (build_pointer_type
6687 (build_type_variant (TREE_TYPE (type),
6688 TYPE_READONLY (TREE_TYPE (type)), 1)),
6689 TYPE_QUALS (type)));
6690 else
6692 warning (OPT_Wattributes, "%qE attribute ignored", name);
6693 *no_add_attrs = true;
6696 return NULL_TREE;
6699 /* Handle a "hot" and attribute; arguments as in
6700 struct attribute_spec.handler. */
6702 static tree
6703 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6704 int ARG_UNUSED (flags), bool *no_add_attrs)
6706 if (TREE_CODE (*node) == FUNCTION_DECL
6707 || TREE_CODE (*node) == LABEL_DECL)
6709 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6711 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6712 "with attribute %qs", name, "cold");
6713 *no_add_attrs = true;
6715 /* Most of the rest of the hot processing is done later with
6716 lookup_attribute. */
6718 else
6720 warning (OPT_Wattributes, "%qE attribute ignored", name);
6721 *no_add_attrs = true;
6724 return NULL_TREE;
6727 /* Handle a "cold" and attribute; arguments as in
6728 struct attribute_spec.handler. */
6730 static tree
6731 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6732 int ARG_UNUSED (flags), bool *no_add_attrs)
6734 if (TREE_CODE (*node) == FUNCTION_DECL
6735 || TREE_CODE (*node) == LABEL_DECL)
6737 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6739 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6740 "with attribute %qs", name, "hot");
6741 *no_add_attrs = true;
6743 /* Most of the rest of the cold processing is done later with
6744 lookup_attribute. */
6746 else
6748 warning (OPT_Wattributes, "%qE attribute ignored", name);
6749 *no_add_attrs = true;
6752 return NULL_TREE;
6755 /* Handle a "no_sanitize_address" attribute; arguments as in
6756 struct attribute_spec.handler. */
6758 static tree
6759 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6760 bool *no_add_attrs)
6762 if (TREE_CODE (*node) != FUNCTION_DECL)
6764 warning (OPT_Wattributes, "%qE attribute ignored", name);
6765 *no_add_attrs = true;
6768 return NULL_TREE;
6771 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6772 struct attribute_spec.handler. */
6774 static tree
6775 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6776 bool *no_add_attrs)
6778 if (TREE_CODE (*node) != FUNCTION_DECL)
6779 warning (OPT_Wattributes, "%qE attribute ignored", name);
6780 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6781 DECL_ATTRIBUTES (*node)
6782 = tree_cons (get_identifier ("no_sanitize_address"),
6783 NULL_TREE, DECL_ATTRIBUTES (*node));
6784 *no_add_attrs = true;
6785 return NULL_TREE;
6788 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6789 struct attribute_spec.handler. */
6791 static tree
6792 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6793 bool *no_add_attrs)
6795 if (TREE_CODE (*node) != FUNCTION_DECL)
6797 warning (OPT_Wattributes, "%qE attribute ignored", name);
6798 *no_add_attrs = true;
6801 return NULL_TREE;
6804 /* Handle a "stack_protect" attribute; arguments as in
6805 struct attribute_spec.handler. */
6806 static tree
6807 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6808 bool *no_add_attrs)
6810 if (TREE_CODE (*node) != FUNCTION_DECL)
6812 warning (OPT_Wattributes, "%qE attribute ignored", name);
6813 *no_add_attrs = true;
6815 else
6816 DECL_ATTRIBUTES (*node)
6817 = tree_cons (get_identifier ("stack_protect"),
6818 NULL_TREE, DECL_ATTRIBUTES (*node));
6820 return NULL_TREE;
6823 /* Handle a "noinline" attribute; arguments as in
6824 struct attribute_spec.handler. */
6826 static tree
6827 handle_noinline_attribute (tree *node, tree name,
6828 tree ARG_UNUSED (args),
6829 int ARG_UNUSED (flags), bool *no_add_attrs)
6831 if (TREE_CODE (*node) == FUNCTION_DECL)
6833 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6835 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6836 "with attribute %qs", name, "always_inline");
6837 *no_add_attrs = true;
6839 else
6840 DECL_UNINLINABLE (*node) = 1;
6842 else
6844 warning (OPT_Wattributes, "%qE attribute ignored", name);
6845 *no_add_attrs = true;
6848 return NULL_TREE;
6851 /* Handle a "noclone" attribute; arguments as in
6852 struct attribute_spec.handler. */
6854 static tree
6855 handle_noclone_attribute (tree *node, tree name,
6856 tree ARG_UNUSED (args),
6857 int ARG_UNUSED (flags), bool *no_add_attrs)
6859 if (TREE_CODE (*node) != FUNCTION_DECL)
6861 warning (OPT_Wattributes, "%qE attribute ignored", name);
6862 *no_add_attrs = true;
6865 return NULL_TREE;
6868 /* Handle a "no_icf" attribute; arguments as in
6869 struct attribute_spec.handler. */
6871 static tree
6872 handle_noicf_attribute (tree *node, tree name,
6873 tree ARG_UNUSED (args),
6874 int ARG_UNUSED (flags), bool *no_add_attrs)
6876 if (TREE_CODE (*node) != FUNCTION_DECL)
6878 warning (OPT_Wattributes, "%qE attribute ignored", name);
6879 *no_add_attrs = true;
6882 return NULL_TREE;
6886 /* Handle a "always_inline" attribute; arguments as in
6887 struct attribute_spec.handler. */
6889 static tree
6890 handle_always_inline_attribute (tree *node, tree name,
6891 tree ARG_UNUSED (args),
6892 int ARG_UNUSED (flags),
6893 bool *no_add_attrs)
6895 if (TREE_CODE (*node) == FUNCTION_DECL)
6897 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6899 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6900 "with %qs attribute", name, "noinline");
6901 *no_add_attrs = true;
6903 else
6904 /* Set the attribute and mark it for disregarding inline
6905 limits. */
6906 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6908 else
6910 warning (OPT_Wattributes, "%qE attribute ignored", name);
6911 *no_add_attrs = true;
6914 return NULL_TREE;
6917 /* Handle a "gnu_inline" attribute; arguments as in
6918 struct attribute_spec.handler. */
6920 static tree
6921 handle_gnu_inline_attribute (tree *node, tree name,
6922 tree ARG_UNUSED (args),
6923 int ARG_UNUSED (flags),
6924 bool *no_add_attrs)
6926 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6928 /* Do nothing else, just set the attribute. We'll get at
6929 it later with lookup_attribute. */
6931 else
6933 warning (OPT_Wattributes, "%qE attribute ignored", name);
6934 *no_add_attrs = true;
6937 return NULL_TREE;
6940 /* Handle a "leaf" attribute; arguments as in
6941 struct attribute_spec.handler. */
6943 static tree
6944 handle_leaf_attribute (tree *node, tree name,
6945 tree ARG_UNUSED (args),
6946 int ARG_UNUSED (flags), bool *no_add_attrs)
6948 if (TREE_CODE (*node) != FUNCTION_DECL)
6950 warning (OPT_Wattributes, "%qE attribute ignored", name);
6951 *no_add_attrs = true;
6953 if (!TREE_PUBLIC (*node))
6955 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6956 *no_add_attrs = true;
6959 return NULL_TREE;
6962 /* Handle an "artificial" attribute; arguments as in
6963 struct attribute_spec.handler. */
6965 static tree
6966 handle_artificial_attribute (tree *node, tree name,
6967 tree ARG_UNUSED (args),
6968 int ARG_UNUSED (flags),
6969 bool *no_add_attrs)
6971 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6973 /* Do nothing else, just set the attribute. We'll get at
6974 it later with lookup_attribute. */
6976 else
6978 warning (OPT_Wattributes, "%qE attribute ignored", name);
6979 *no_add_attrs = true;
6982 return NULL_TREE;
6985 /* Handle a "flatten" attribute; arguments as in
6986 struct attribute_spec.handler. */
6988 static tree
6989 handle_flatten_attribute (tree *node, tree name,
6990 tree args ATTRIBUTE_UNUSED,
6991 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6993 if (TREE_CODE (*node) == FUNCTION_DECL)
6994 /* Do nothing else, just set the attribute. We'll get at
6995 it later with lookup_attribute. */
6997 else
6999 warning (OPT_Wattributes, "%qE attribute ignored", name);
7000 *no_add_attrs = true;
7003 return NULL_TREE;
7006 /* Handle a "warning" or "error" attribute; arguments as in
7007 struct attribute_spec.handler. */
7009 static tree
7010 handle_error_attribute (tree *node, tree name, tree args,
7011 int ARG_UNUSED (flags), bool *no_add_attrs)
7013 if (TREE_CODE (*node) == FUNCTION_DECL
7014 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7015 /* Do nothing else, just set the attribute. We'll get at
7016 it later with lookup_attribute. */
7018 else
7020 warning (OPT_Wattributes, "%qE attribute ignored", name);
7021 *no_add_attrs = true;
7024 return NULL_TREE;
7027 /* Handle a "used" attribute; arguments as in
7028 struct attribute_spec.handler. */
7030 static tree
7031 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7032 int ARG_UNUSED (flags), bool *no_add_attrs)
7034 tree node = *pnode;
7036 if (TREE_CODE (node) == FUNCTION_DECL
7037 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7038 || (TREE_CODE (node) == TYPE_DECL))
7040 TREE_USED (node) = 1;
7041 DECL_PRESERVE_P (node) = 1;
7042 if (TREE_CODE (node) == VAR_DECL)
7043 DECL_READ_P (node) = 1;
7045 else
7047 warning (OPT_Wattributes, "%qE attribute ignored", name);
7048 *no_add_attrs = true;
7051 return NULL_TREE;
7054 /* Handle a "unused" attribute; arguments as in
7055 struct attribute_spec.handler. */
7057 static tree
7058 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7059 int flags, bool *no_add_attrs)
7061 if (DECL_P (*node))
7063 tree decl = *node;
7065 if (TREE_CODE (decl) == PARM_DECL
7066 || TREE_CODE (decl) == VAR_DECL
7067 || TREE_CODE (decl) == FUNCTION_DECL
7068 || TREE_CODE (decl) == LABEL_DECL
7069 || TREE_CODE (decl) == TYPE_DECL)
7071 TREE_USED (decl) = 1;
7072 if (TREE_CODE (decl) == VAR_DECL
7073 || TREE_CODE (decl) == PARM_DECL)
7074 DECL_READ_P (decl) = 1;
7076 else
7078 warning (OPT_Wattributes, "%qE attribute ignored", name);
7079 *no_add_attrs = true;
7082 else
7084 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7085 *node = build_variant_type_copy (*node);
7086 TREE_USED (*node) = 1;
7089 return NULL_TREE;
7092 /* Handle a "externally_visible" attribute; arguments as in
7093 struct attribute_spec.handler. */
7095 static tree
7096 handle_externally_visible_attribute (tree *pnode, tree name,
7097 tree ARG_UNUSED (args),
7098 int ARG_UNUSED (flags),
7099 bool *no_add_attrs)
7101 tree node = *pnode;
7103 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7105 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7106 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7108 warning (OPT_Wattributes,
7109 "%qE attribute have effect only on public objects", name);
7110 *no_add_attrs = true;
7113 else
7115 warning (OPT_Wattributes, "%qE attribute ignored", name);
7116 *no_add_attrs = true;
7119 return NULL_TREE;
7122 /* Handle the "no_reorder" attribute. Arguments as in
7123 struct attribute_spec.handler. */
7125 static tree
7126 handle_no_reorder_attribute (tree *pnode,
7127 tree name,
7128 tree,
7129 int,
7130 bool *no_add_attrs)
7132 tree node = *pnode;
7134 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7135 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7137 warning (OPT_Wattributes,
7138 "%qE attribute only affects top level objects",
7139 name);
7140 *no_add_attrs = true;
7143 return NULL_TREE;
7146 /* Handle a "const" attribute; arguments as in
7147 struct attribute_spec.handler. */
7149 static tree
7150 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7151 int ARG_UNUSED (flags), bool *no_add_attrs)
7153 tree type = TREE_TYPE (*node);
7155 /* See FIXME comment on noreturn in c_common_attribute_table. */
7156 if (TREE_CODE (*node) == FUNCTION_DECL)
7157 TREE_READONLY (*node) = 1;
7158 else if (TREE_CODE (type) == POINTER_TYPE
7159 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7160 TREE_TYPE (*node)
7161 = (build_qualified_type
7162 (build_pointer_type
7163 (build_type_variant (TREE_TYPE (type), 1,
7164 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7165 TYPE_QUALS (type)));
7166 else
7168 warning (OPT_Wattributes, "%qE attribute ignored", name);
7169 *no_add_attrs = true;
7172 return NULL_TREE;
7175 /* Handle a "transparent_union" attribute; arguments as in
7176 struct attribute_spec.handler. */
7178 static tree
7179 handle_transparent_union_attribute (tree *node, tree name,
7180 tree ARG_UNUSED (args), int flags,
7181 bool *no_add_attrs)
7183 tree type;
7185 *no_add_attrs = true;
7188 if (TREE_CODE (*node) == TYPE_DECL
7189 && ! (flags & ATTR_FLAG_CXX11))
7190 node = &TREE_TYPE (*node);
7191 type = *node;
7193 if (TREE_CODE (type) == UNION_TYPE)
7195 /* Make sure that the first field will work for a transparent union.
7196 If the type isn't complete yet, leave the check to the code in
7197 finish_struct. */
7198 if (TYPE_SIZE (type))
7200 tree first = first_field (type);
7201 if (first == NULL_TREE
7202 || DECL_ARTIFICIAL (first)
7203 || TYPE_MODE (type) != DECL_MODE (first))
7204 goto ignored;
7207 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7209 /* If the type isn't complete yet, setting the flag
7210 on a variant wouldn't ever be checked. */
7211 if (!TYPE_SIZE (type))
7212 goto ignored;
7214 /* build_duplicate_type doesn't work for C++. */
7215 if (c_dialect_cxx ())
7216 goto ignored;
7218 /* A type variant isn't good enough, since we don't a cast
7219 to such a type removed as a no-op. */
7220 *node = type = build_duplicate_type (type);
7223 TYPE_TRANSPARENT_AGGR (type) = 1;
7224 return NULL_TREE;
7227 ignored:
7228 warning (OPT_Wattributes, "%qE attribute ignored", name);
7229 return NULL_TREE;
7232 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7233 get the requested priority for a constructor or destructor,
7234 possibly issuing diagnostics for invalid or reserved
7235 priorities. */
7237 static priority_type
7238 get_priority (tree args, bool is_destructor)
7240 HOST_WIDE_INT pri;
7241 tree arg;
7243 if (!args)
7244 return DEFAULT_INIT_PRIORITY;
7246 if (!SUPPORTS_INIT_PRIORITY)
7248 if (is_destructor)
7249 error ("destructor priorities are not supported");
7250 else
7251 error ("constructor priorities are not supported");
7252 return DEFAULT_INIT_PRIORITY;
7255 arg = TREE_VALUE (args);
7256 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7257 goto invalid;
7258 if (arg == error_mark_node)
7259 return DEFAULT_INIT_PRIORITY;
7260 arg = default_conversion (arg);
7261 if (!tree_fits_shwi_p (arg)
7262 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7263 goto invalid;
7265 pri = tree_to_shwi (arg);
7266 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7267 goto invalid;
7269 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7271 if (is_destructor)
7272 warning (0,
7273 "destructor priorities from 0 to %d are reserved "
7274 "for the implementation",
7275 MAX_RESERVED_INIT_PRIORITY);
7276 else
7277 warning (0,
7278 "constructor priorities from 0 to %d are reserved "
7279 "for the implementation",
7280 MAX_RESERVED_INIT_PRIORITY);
7282 return pri;
7284 invalid:
7285 if (is_destructor)
7286 error ("destructor priorities must be integers from 0 to %d inclusive",
7287 MAX_INIT_PRIORITY);
7288 else
7289 error ("constructor priorities must be integers from 0 to %d inclusive",
7290 MAX_INIT_PRIORITY);
7291 return DEFAULT_INIT_PRIORITY;
7294 /* Handle a "constructor" attribute; arguments as in
7295 struct attribute_spec.handler. */
7297 static tree
7298 handle_constructor_attribute (tree *node, tree name, tree args,
7299 int ARG_UNUSED (flags),
7300 bool *no_add_attrs)
7302 tree decl = *node;
7303 tree type = TREE_TYPE (decl);
7305 if (TREE_CODE (decl) == FUNCTION_DECL
7306 && TREE_CODE (type) == FUNCTION_TYPE
7307 && decl_function_context (decl) == 0)
7309 priority_type priority;
7310 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7311 priority = get_priority (args, /*is_destructor=*/false);
7312 SET_DECL_INIT_PRIORITY (decl, priority);
7313 TREE_USED (decl) = 1;
7315 else
7317 warning (OPT_Wattributes, "%qE attribute ignored", name);
7318 *no_add_attrs = true;
7321 return NULL_TREE;
7324 /* Handle a "destructor" attribute; arguments as in
7325 struct attribute_spec.handler. */
7327 static tree
7328 handle_destructor_attribute (tree *node, tree name, tree args,
7329 int ARG_UNUSED (flags),
7330 bool *no_add_attrs)
7332 tree decl = *node;
7333 tree type = TREE_TYPE (decl);
7335 if (TREE_CODE (decl) == FUNCTION_DECL
7336 && TREE_CODE (type) == FUNCTION_TYPE
7337 && decl_function_context (decl) == 0)
7339 priority_type priority;
7340 DECL_STATIC_DESTRUCTOR (decl) = 1;
7341 priority = get_priority (args, /*is_destructor=*/true);
7342 SET_DECL_FINI_PRIORITY (decl, priority);
7343 TREE_USED (decl) = 1;
7345 else
7347 warning (OPT_Wattributes, "%qE attribute ignored", name);
7348 *no_add_attrs = true;
7351 return NULL_TREE;
7354 /* Nonzero if the mode is a valid vector mode for this architecture.
7355 This returns nonzero even if there is no hardware support for the
7356 vector mode, but we can emulate with narrower modes. */
7358 static int
7359 vector_mode_valid_p (machine_mode mode)
7361 enum mode_class mclass = GET_MODE_CLASS (mode);
7362 machine_mode innermode;
7364 /* Doh! What's going on? */
7365 if (mclass != MODE_VECTOR_INT
7366 && mclass != MODE_VECTOR_FLOAT
7367 && mclass != MODE_VECTOR_FRACT
7368 && mclass != MODE_VECTOR_UFRACT
7369 && mclass != MODE_VECTOR_ACCUM
7370 && mclass != MODE_VECTOR_UACCUM)
7371 return 0;
7373 /* Hardware support. Woo hoo! */
7374 if (targetm.vector_mode_supported_p (mode))
7375 return 1;
7377 innermode = GET_MODE_INNER (mode);
7379 /* We should probably return 1 if requesting V4DI and we have no DI,
7380 but we have V2DI, but this is probably very unlikely. */
7382 /* If we have support for the inner mode, we can safely emulate it.
7383 We may not have V2DI, but me can emulate with a pair of DIs. */
7384 return targetm.scalar_mode_supported_p (innermode);
7388 /* Handle a "mode" attribute; arguments as in
7389 struct attribute_spec.handler. */
7391 static tree
7392 handle_mode_attribute (tree *node, tree name, tree args,
7393 int ARG_UNUSED (flags), bool *no_add_attrs)
7395 tree type = *node;
7396 tree ident = TREE_VALUE (args);
7398 *no_add_attrs = true;
7400 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7401 warning (OPT_Wattributes, "%qE attribute ignored", name);
7402 else
7404 int j;
7405 const char *p = IDENTIFIER_POINTER (ident);
7406 int len = strlen (p);
7407 machine_mode mode = VOIDmode;
7408 tree typefm;
7409 bool valid_mode;
7411 if (len > 4 && p[0] == '_' && p[1] == '_'
7412 && p[len - 1] == '_' && p[len - 2] == '_')
7414 char *newp = (char *) alloca (len - 1);
7416 strcpy (newp, &p[2]);
7417 newp[len - 4] = '\0';
7418 p = newp;
7421 /* Change this type to have a type with the specified mode.
7422 First check for the special modes. */
7423 if (!strcmp (p, "byte"))
7424 mode = byte_mode;
7425 else if (!strcmp (p, "word"))
7426 mode = word_mode;
7427 else if (!strcmp (p, "pointer"))
7428 mode = ptr_mode;
7429 else if (!strcmp (p, "libgcc_cmp_return"))
7430 mode = targetm.libgcc_cmp_return_mode ();
7431 else if (!strcmp (p, "libgcc_shift_count"))
7432 mode = targetm.libgcc_shift_count_mode ();
7433 else if (!strcmp (p, "unwind_word"))
7434 mode = targetm.unwind_word_mode ();
7435 else
7436 for (j = 0; j < NUM_MACHINE_MODES; j++)
7437 if (!strcmp (p, GET_MODE_NAME (j)))
7439 mode = (machine_mode) j;
7440 break;
7443 if (mode == VOIDmode)
7445 error ("unknown machine mode %qE", ident);
7446 return NULL_TREE;
7449 valid_mode = false;
7450 switch (GET_MODE_CLASS (mode))
7452 case MODE_INT:
7453 case MODE_PARTIAL_INT:
7454 case MODE_FLOAT:
7455 case MODE_DECIMAL_FLOAT:
7456 case MODE_FRACT:
7457 case MODE_UFRACT:
7458 case MODE_ACCUM:
7459 case MODE_UACCUM:
7460 valid_mode = targetm.scalar_mode_supported_p (mode);
7461 break;
7463 case MODE_COMPLEX_INT:
7464 case MODE_COMPLEX_FLOAT:
7465 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7466 break;
7468 case MODE_VECTOR_INT:
7469 case MODE_VECTOR_FLOAT:
7470 case MODE_VECTOR_FRACT:
7471 case MODE_VECTOR_UFRACT:
7472 case MODE_VECTOR_ACCUM:
7473 case MODE_VECTOR_UACCUM:
7474 warning (OPT_Wattributes, "specifying vector types with "
7475 "__attribute__ ((mode)) is deprecated");
7476 warning (OPT_Wattributes,
7477 "use __attribute__ ((vector_size)) instead");
7478 valid_mode = vector_mode_valid_p (mode);
7479 break;
7481 default:
7482 break;
7484 if (!valid_mode)
7486 error ("unable to emulate %qs", p);
7487 return NULL_TREE;
7490 if (POINTER_TYPE_P (type))
7492 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7493 tree (*fn)(tree, machine_mode, bool);
7495 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7497 error ("invalid pointer mode %qs", p);
7498 return NULL_TREE;
7501 if (TREE_CODE (type) == POINTER_TYPE)
7502 fn = build_pointer_type_for_mode;
7503 else
7504 fn = build_reference_type_for_mode;
7505 typefm = fn (TREE_TYPE (type), mode, false);
7507 else
7509 /* For fixed-point modes, we need to test if the signness of type
7510 and the machine mode are consistent. */
7511 if (ALL_FIXED_POINT_MODE_P (mode)
7512 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7514 error ("signedness of type and machine mode %qs don%'t match", p);
7515 return NULL_TREE;
7517 /* For fixed-point modes, we need to pass saturating info. */
7518 typefm = lang_hooks.types.type_for_mode (mode,
7519 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7520 : TYPE_UNSIGNED (type));
7523 if (typefm == NULL_TREE)
7525 error ("no data type for mode %qs", p);
7526 return NULL_TREE;
7528 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7530 /* For enumeral types, copy the precision from the integer
7531 type returned above. If not an INTEGER_TYPE, we can't use
7532 this mode for this type. */
7533 if (TREE_CODE (typefm) != INTEGER_TYPE)
7535 error ("cannot use mode %qs for enumeral types", p);
7536 return NULL_TREE;
7539 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7541 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7542 typefm = type;
7544 else
7546 /* We cannot build a type variant, as there's code that assumes
7547 that TYPE_MAIN_VARIANT has the same mode. This includes the
7548 debug generators. Instead, create a subrange type. This
7549 results in all of the enumeral values being emitted only once
7550 in the original, and the subtype gets them by reference. */
7551 if (TYPE_UNSIGNED (type))
7552 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7553 else
7554 typefm = make_signed_type (TYPE_PRECISION (typefm));
7555 TREE_TYPE (typefm) = type;
7558 else if (VECTOR_MODE_P (mode)
7559 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7560 : TREE_CODE (type) != TREE_CODE (typefm))
7562 error ("mode %qs applied to inappropriate type", p);
7563 return NULL_TREE;
7566 *node = typefm;
7569 return NULL_TREE;
7572 /* Handle a "section" attribute; arguments as in
7573 struct attribute_spec.handler. */
7575 static tree
7576 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7577 int ARG_UNUSED (flags), bool *no_add_attrs)
7579 tree decl = *node;
7581 if (targetm_common.have_named_sections)
7583 user_defined_section_attribute = true;
7585 if ((TREE_CODE (decl) == FUNCTION_DECL
7586 || TREE_CODE (decl) == VAR_DECL)
7587 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7589 if (TREE_CODE (decl) == VAR_DECL
7590 && current_function_decl != NULL_TREE
7591 && !TREE_STATIC (decl))
7593 error_at (DECL_SOURCE_LOCATION (decl),
7594 "section attribute cannot be specified for "
7595 "local variables");
7596 *no_add_attrs = true;
7599 /* The decl may have already been given a section attribute
7600 from a previous declaration. Ensure they match. */
7601 else if (DECL_SECTION_NAME (decl) != NULL
7602 && strcmp (DECL_SECTION_NAME (decl),
7603 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7605 error ("section of %q+D conflicts with previous declaration",
7606 *node);
7607 *no_add_attrs = true;
7609 else if (TREE_CODE (decl) == VAR_DECL
7610 && !targetm.have_tls && targetm.emutls.tmpl_section
7611 && DECL_THREAD_LOCAL_P (decl))
7613 error ("section of %q+D cannot be overridden", *node);
7614 *no_add_attrs = true;
7616 else
7617 set_decl_section_name (decl,
7618 TREE_STRING_POINTER (TREE_VALUE (args)));
7620 else
7622 error ("section attribute not allowed for %q+D", *node);
7623 *no_add_attrs = true;
7626 else
7628 error_at (DECL_SOURCE_LOCATION (*node),
7629 "section attributes are not supported for this target");
7630 *no_add_attrs = true;
7633 return NULL_TREE;
7636 /* Check whether ALIGN is a valid user-specified alignment. If so,
7637 return its base-2 log; if not, output an error and return -1. If
7638 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7639 no error. */
7641 check_user_alignment (const_tree align, bool allow_zero)
7643 int i;
7645 if (error_operand_p (align))
7646 return -1;
7647 if (TREE_CODE (align) != INTEGER_CST
7648 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7650 error ("requested alignment is not an integer constant");
7651 return -1;
7653 else if (allow_zero && integer_zerop (align))
7654 return -1;
7655 else if (tree_int_cst_sgn (align) == -1
7656 || (i = tree_log2 (align)) == -1)
7658 error ("requested alignment is not a positive power of 2");
7659 return -1;
7661 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7663 error ("requested alignment is too large");
7664 return -1;
7666 return i;
7670 If in c++-11, check if the c++-11 alignment constraint with respect
7671 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7672 c++-11 mode, does nothing.
7674 [dcl.align]2/ says:
7676 [* if the constant expression evaluates to a fundamental alignment,
7677 the alignment requirement of the declared entity shall be the
7678 specified fundamental alignment.
7680 * if the constant expression evaluates to an extended alignment
7681 and the implementation supports that alignment in the context
7682 of the declaration, the alignment of the declared entity shall
7683 be that alignment
7685 * if the constant expression evaluates to an extended alignment
7686 and the implementation does not support that alignment in the
7687 context of the declaration, the program is ill-formed]. */
7689 static bool
7690 check_cxx_fundamental_alignment_constraints (tree node,
7691 unsigned align_log,
7692 int flags)
7694 bool alignment_too_large_p = false;
7695 unsigned requested_alignment = 1U << align_log;
7696 unsigned max_align = 0;
7698 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7699 || (node == NULL_TREE || node == error_mark_node))
7700 return true;
7702 if (cxx_fundamental_alignment_p (requested_alignment))
7703 return true;
7705 if (DECL_P (node))
7707 if (TREE_STATIC (node))
7709 /* For file scope variables and static members, the target
7710 supports alignments that are at most
7711 MAX_OFILE_ALIGNMENT. */
7712 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7713 alignment_too_large_p = true;
7715 else
7717 #ifdef BIGGEST_FIELD_ALIGNMENT
7718 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7719 #else
7720 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7721 #endif
7722 /* For non-static members, the target supports either
7723 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7724 if it is defined or BIGGEST_ALIGNMENT. */
7725 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7726 if (TREE_CODE (node) == FIELD_DECL
7727 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7728 alignment_too_large_p = true;
7729 #undef MAX_TARGET_FIELD_ALIGNMENT
7730 /* For stack variables, the target supports at most
7731 MAX_STACK_ALIGNMENT. */
7732 else if (decl_function_context (node) != NULL
7733 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7734 alignment_too_large_p = true;
7737 else if (TYPE_P (node))
7739 /* Let's be liberal for types. */
7740 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7741 alignment_too_large_p = true;
7744 if (alignment_too_large_p)
7745 pedwarn (input_location, OPT_Wattributes,
7746 "requested alignment %d is larger than %d",
7747 requested_alignment, max_align);
7749 return !alignment_too_large_p;
7752 /* Handle a "aligned" attribute; arguments as in
7753 struct attribute_spec.handler. */
7755 static tree
7756 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7757 int flags, bool *no_add_attrs)
7759 tree decl = NULL_TREE;
7760 tree *type = NULL;
7761 int is_type = 0;
7762 tree align_expr;
7763 int i;
7765 if (args)
7767 align_expr = TREE_VALUE (args);
7768 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7769 && TREE_CODE (align_expr) != FUNCTION_DECL)
7770 align_expr = default_conversion (align_expr);
7772 else
7773 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7775 if (DECL_P (*node))
7777 decl = *node;
7778 type = &TREE_TYPE (decl);
7779 is_type = TREE_CODE (*node) == TYPE_DECL;
7781 else if (TYPE_P (*node))
7782 type = node, is_type = 1;
7784 if ((i = check_user_alignment (align_expr, false)) == -1
7785 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7786 *no_add_attrs = true;
7787 else if (is_type)
7789 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7790 /* OK, modify the type in place. */;
7791 /* If we have a TYPE_DECL, then copy the type, so that we
7792 don't accidentally modify a builtin type. See pushdecl. */
7793 else if (decl && TREE_TYPE (decl) != error_mark_node
7794 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7796 tree tt = TREE_TYPE (decl);
7797 *type = build_variant_type_copy (*type);
7798 DECL_ORIGINAL_TYPE (decl) = tt;
7799 TYPE_NAME (*type) = decl;
7800 TREE_USED (*type) = TREE_USED (decl);
7801 TREE_TYPE (decl) = *type;
7803 else
7804 *type = build_variant_type_copy (*type);
7806 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7807 TYPE_USER_ALIGN (*type) = 1;
7809 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7810 && TREE_CODE (decl) != FIELD_DECL)
7812 error ("alignment may not be specified for %q+D", decl);
7813 *no_add_attrs = true;
7815 else if (DECL_USER_ALIGN (decl)
7816 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7817 /* C++-11 [dcl.align/4]:
7819 When multiple alignment-specifiers are specified for an
7820 entity, the alignment requirement shall be set to the
7821 strictest specified alignment.
7823 This formally comes from the c++11 specification but we are
7824 doing it for the GNU attribute syntax as well. */
7825 *no_add_attrs = true;
7826 else if (TREE_CODE (decl) == FUNCTION_DECL
7827 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7829 if (DECL_USER_ALIGN (decl))
7830 error ("alignment for %q+D was previously specified as %d "
7831 "and may not be decreased", decl,
7832 DECL_ALIGN (decl) / BITS_PER_UNIT);
7833 else
7834 error ("alignment for %q+D must be at least %d", decl,
7835 DECL_ALIGN (decl) / BITS_PER_UNIT);
7836 *no_add_attrs = true;
7838 else
7840 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7841 DECL_USER_ALIGN (decl) = 1;
7844 return NULL_TREE;
7847 /* Handle a "weak" attribute; arguments as in
7848 struct attribute_spec.handler. */
7850 static tree
7851 handle_weak_attribute (tree *node, tree name,
7852 tree ARG_UNUSED (args),
7853 int ARG_UNUSED (flags),
7854 bool * ARG_UNUSED (no_add_attrs))
7856 if (TREE_CODE (*node) == FUNCTION_DECL
7857 && DECL_DECLARED_INLINE_P (*node))
7859 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7860 *no_add_attrs = true;
7862 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7864 error ("indirect function %q+D cannot be declared weak", *node);
7865 *no_add_attrs = true;
7866 return NULL_TREE;
7868 else if (TREE_CODE (*node) == FUNCTION_DECL
7869 || TREE_CODE (*node) == VAR_DECL)
7871 struct symtab_node *n = symtab_node::get (*node);
7872 if (n && n->refuse_visibility_changes)
7873 error ("%+D declared weak after being used", *node);
7874 declare_weak (*node);
7876 else
7877 warning (OPT_Wattributes, "%qE attribute ignored", name);
7879 return NULL_TREE;
7882 /* Handle an "alias" or "ifunc" attribute; arguments as in
7883 struct attribute_spec.handler, except that IS_ALIAS tells us
7884 whether this is an alias as opposed to ifunc attribute. */
7886 static tree
7887 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7888 bool *no_add_attrs)
7890 tree decl = *node;
7892 if (TREE_CODE (decl) != FUNCTION_DECL
7893 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7895 warning (OPT_Wattributes, "%qE attribute ignored", name);
7896 *no_add_attrs = true;
7898 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7899 || (TREE_CODE (decl) != FUNCTION_DECL
7900 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7901 /* A static variable declaration is always a tentative definition,
7902 but the alias is a non-tentative definition which overrides. */
7903 || (TREE_CODE (decl) != FUNCTION_DECL
7904 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7906 error ("%q+D defined both normally and as %qE attribute", decl, name);
7907 *no_add_attrs = true;
7908 return NULL_TREE;
7910 else if (!is_alias
7911 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7912 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7914 error ("weak %q+D cannot be defined %qE", decl, name);
7915 *no_add_attrs = true;
7916 return NULL_TREE;
7919 /* Note that the very first time we process a nested declaration,
7920 decl_function_context will not be set. Indeed, *would* never
7921 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7922 we do below. After such frobbery, pushdecl would set the context.
7923 In any case, this is never what we want. */
7924 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7926 tree id;
7928 id = TREE_VALUE (args);
7929 if (TREE_CODE (id) != STRING_CST)
7931 error ("attribute %qE argument not a string", name);
7932 *no_add_attrs = true;
7933 return NULL_TREE;
7935 id = get_identifier (TREE_STRING_POINTER (id));
7936 /* This counts as a use of the object pointed to. */
7937 TREE_USED (id) = 1;
7939 if (TREE_CODE (decl) == FUNCTION_DECL)
7940 DECL_INITIAL (decl) = error_mark_node;
7941 else
7942 TREE_STATIC (decl) = 1;
7944 if (!is_alias)
7945 /* ifuncs are also aliases, so set that attribute too. */
7946 DECL_ATTRIBUTES (decl)
7947 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7949 else
7951 warning (OPT_Wattributes, "%qE attribute ignored", name);
7952 *no_add_attrs = true;
7955 if (decl_in_symtab_p (*node))
7957 struct symtab_node *n = symtab_node::get (decl);
7958 if (n && n->refuse_visibility_changes)
7960 if (is_alias)
7961 error ("%+D declared alias after being used", decl);
7962 else
7963 error ("%+D declared ifunc after being used", decl);
7968 return NULL_TREE;
7971 /* Handle an "alias" or "ifunc" attribute; arguments as in
7972 struct attribute_spec.handler. */
7974 static tree
7975 handle_ifunc_attribute (tree *node, tree name, tree args,
7976 int ARG_UNUSED (flags), bool *no_add_attrs)
7978 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7981 /* Handle an "alias" or "ifunc" attribute; arguments as in
7982 struct attribute_spec.handler. */
7984 static tree
7985 handle_alias_attribute (tree *node, tree name, tree args,
7986 int ARG_UNUSED (flags), bool *no_add_attrs)
7988 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7991 /* Handle a "weakref" attribute; arguments as in struct
7992 attribute_spec.handler. */
7994 static tree
7995 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7996 int flags, bool *no_add_attrs)
7998 tree attr = NULL_TREE;
8000 /* We must ignore the attribute when it is associated with
8001 local-scoped decls, since attribute alias is ignored and many
8002 such symbols do not even have a DECL_WEAK field. */
8003 if (decl_function_context (*node)
8004 || current_function_decl
8005 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
8007 warning (OPT_Wattributes, "%qE attribute ignored", name);
8008 *no_add_attrs = true;
8009 return NULL_TREE;
8012 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8014 error ("indirect function %q+D cannot be declared weakref", *node);
8015 *no_add_attrs = true;
8016 return NULL_TREE;
8019 /* The idea here is that `weakref("name")' mutates into `weakref,
8020 alias("name")', and weakref without arguments, in turn,
8021 implicitly adds weak. */
8023 if (args)
8025 attr = tree_cons (get_identifier ("alias"), args, attr);
8026 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8028 *no_add_attrs = true;
8030 decl_attributes (node, attr, flags);
8032 else
8034 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8035 error_at (DECL_SOURCE_LOCATION (*node),
8036 "weakref attribute must appear before alias attribute");
8038 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8039 and that isn't supported; and because it wants to add it to
8040 the list of weak decls, which isn't helpful. */
8041 DECL_WEAK (*node) = 1;
8044 if (decl_in_symtab_p (*node))
8046 struct symtab_node *n = symtab_node::get (*node);
8047 if (n && n->refuse_visibility_changes)
8048 error ("%+D declared weakref after being used", *node);
8051 return NULL_TREE;
8054 /* Handle an "visibility" attribute; arguments as in
8055 struct attribute_spec.handler. */
8057 static tree
8058 handle_visibility_attribute (tree *node, tree name, tree args,
8059 int ARG_UNUSED (flags),
8060 bool *ARG_UNUSED (no_add_attrs))
8062 tree decl = *node;
8063 tree id = TREE_VALUE (args);
8064 enum symbol_visibility vis;
8066 if (TYPE_P (*node))
8068 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8069 /* OK */;
8070 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8072 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8073 name);
8074 return NULL_TREE;
8076 else if (TYPE_FIELDS (*node))
8078 error ("%qE attribute ignored because %qT is already defined",
8079 name, *node);
8080 return NULL_TREE;
8083 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8085 warning (OPT_Wattributes, "%qE attribute ignored", name);
8086 return NULL_TREE;
8089 if (TREE_CODE (id) != STRING_CST)
8091 error ("visibility argument not a string");
8092 return NULL_TREE;
8095 /* If this is a type, set the visibility on the type decl. */
8096 if (TYPE_P (decl))
8098 decl = TYPE_NAME (decl);
8099 if (!decl)
8100 return NULL_TREE;
8101 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8103 warning (OPT_Wattributes, "%qE attribute ignored on types",
8104 name);
8105 return NULL_TREE;
8109 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8110 vis = VISIBILITY_DEFAULT;
8111 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8112 vis = VISIBILITY_INTERNAL;
8113 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8114 vis = VISIBILITY_HIDDEN;
8115 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8116 vis = VISIBILITY_PROTECTED;
8117 else
8119 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8120 vis = VISIBILITY_DEFAULT;
8123 if (DECL_VISIBILITY_SPECIFIED (decl)
8124 && vis != DECL_VISIBILITY (decl))
8126 tree attributes = (TYPE_P (*node)
8127 ? TYPE_ATTRIBUTES (*node)
8128 : DECL_ATTRIBUTES (decl));
8129 if (lookup_attribute ("visibility", attributes))
8130 error ("%qD redeclared with different visibility", decl);
8131 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8132 && lookup_attribute ("dllimport", attributes))
8133 error ("%qD was declared %qs which implies default visibility",
8134 decl, "dllimport");
8135 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8136 && lookup_attribute ("dllexport", attributes))
8137 error ("%qD was declared %qs which implies default visibility",
8138 decl, "dllexport");
8141 DECL_VISIBILITY (decl) = vis;
8142 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8144 /* Go ahead and attach the attribute to the node as well. This is needed
8145 so we can determine whether we have VISIBILITY_DEFAULT because the
8146 visibility was not specified, or because it was explicitly overridden
8147 from the containing scope. */
8149 return NULL_TREE;
8152 /* Determine the ELF symbol visibility for DECL, which is either a
8153 variable or a function. It is an error to use this function if a
8154 definition of DECL is not available in this translation unit.
8155 Returns true if the final visibility has been determined by this
8156 function; false if the caller is free to make additional
8157 modifications. */
8159 bool
8160 c_determine_visibility (tree decl)
8162 gcc_assert (TREE_CODE (decl) == VAR_DECL
8163 || TREE_CODE (decl) == FUNCTION_DECL);
8165 /* If the user explicitly specified the visibility with an
8166 attribute, honor that. DECL_VISIBILITY will have been set during
8167 the processing of the attribute. We check for an explicit
8168 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8169 to distinguish the use of an attribute from the use of a "#pragma
8170 GCC visibility push(...)"; in the latter case we still want other
8171 considerations to be able to overrule the #pragma. */
8172 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8173 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8174 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8175 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8176 return true;
8178 /* Set default visibility to whatever the user supplied with
8179 visibility_specified depending on #pragma GCC visibility. */
8180 if (!DECL_VISIBILITY_SPECIFIED (decl))
8182 if (visibility_options.inpragma
8183 || DECL_VISIBILITY (decl) != default_visibility)
8185 DECL_VISIBILITY (decl) = default_visibility;
8186 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8187 /* If visibility changed and DECL already has DECL_RTL, ensure
8188 symbol flags are updated. */
8189 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8190 || TREE_CODE (decl) == FUNCTION_DECL)
8191 && DECL_RTL_SET_P (decl))
8192 make_decl_rtl (decl);
8195 return false;
8198 /* Handle an "tls_model" attribute; arguments as in
8199 struct attribute_spec.handler. */
8201 static tree
8202 handle_tls_model_attribute (tree *node, tree name, tree args,
8203 int ARG_UNUSED (flags), bool *no_add_attrs)
8205 tree id;
8206 tree decl = *node;
8207 enum tls_model kind;
8209 *no_add_attrs = true;
8211 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8213 warning (OPT_Wattributes, "%qE attribute ignored", name);
8214 return NULL_TREE;
8217 kind = DECL_TLS_MODEL (decl);
8218 id = TREE_VALUE (args);
8219 if (TREE_CODE (id) != STRING_CST)
8221 error ("tls_model argument not a string");
8222 return NULL_TREE;
8225 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8226 kind = TLS_MODEL_LOCAL_EXEC;
8227 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8228 kind = TLS_MODEL_INITIAL_EXEC;
8229 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8230 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8231 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8232 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8233 else
8234 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8236 set_decl_tls_model (decl, kind);
8237 return NULL_TREE;
8240 /* Handle a "no_instrument_function" attribute; arguments as in
8241 struct attribute_spec.handler. */
8243 static tree
8244 handle_no_instrument_function_attribute (tree *node, tree name,
8245 tree ARG_UNUSED (args),
8246 int ARG_UNUSED (flags),
8247 bool *no_add_attrs)
8249 tree decl = *node;
8251 if (TREE_CODE (decl) != FUNCTION_DECL)
8253 error_at (DECL_SOURCE_LOCATION (decl),
8254 "%qE attribute applies only to functions", name);
8255 *no_add_attrs = true;
8257 else
8258 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8260 return NULL_TREE;
8263 /* Handle a "malloc" attribute; arguments as in
8264 struct attribute_spec.handler. */
8266 static tree
8267 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8268 int ARG_UNUSED (flags), bool *no_add_attrs)
8270 if (TREE_CODE (*node) == FUNCTION_DECL
8271 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8272 DECL_IS_MALLOC (*node) = 1;
8273 else
8275 warning (OPT_Wattributes, "%qE attribute ignored", name);
8276 *no_add_attrs = true;
8279 return NULL_TREE;
8282 /* Handle a "alloc_size" attribute; arguments as in
8283 struct attribute_spec.handler. */
8285 static tree
8286 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8287 int ARG_UNUSED (flags), bool *no_add_attrs)
8289 unsigned arg_count = type_num_arguments (*node);
8290 for (; args; args = TREE_CHAIN (args))
8292 tree position = TREE_VALUE (args);
8293 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8294 && TREE_CODE (position) != FUNCTION_DECL)
8295 position = default_conversion (position);
8297 if (!tree_fits_uhwi_p (position)
8298 || !arg_count
8299 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8301 warning (OPT_Wattributes,
8302 "alloc_size parameter outside range");
8303 *no_add_attrs = true;
8304 return NULL_TREE;
8307 return NULL_TREE;
8310 /* Handle a "alloc_align" attribute; arguments as in
8311 struct attribute_spec.handler. */
8313 static tree
8314 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8315 bool *no_add_attrs)
8317 unsigned arg_count = type_num_arguments (*node);
8318 tree position = TREE_VALUE (args);
8319 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8320 position = default_conversion (position);
8322 if (!tree_fits_uhwi_p (position)
8323 || !arg_count
8324 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8326 warning (OPT_Wattributes,
8327 "alloc_align parameter outside range");
8328 *no_add_attrs = true;
8329 return NULL_TREE;
8331 return NULL_TREE;
8334 /* Handle a "assume_aligned" attribute; arguments as in
8335 struct attribute_spec.handler. */
8337 static tree
8338 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8339 bool *no_add_attrs)
8341 for (; args; args = TREE_CHAIN (args))
8343 tree position = TREE_VALUE (args);
8344 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8345 && TREE_CODE (position) != FUNCTION_DECL)
8346 position = default_conversion (position);
8348 if (TREE_CODE (position) != INTEGER_CST)
8350 warning (OPT_Wattributes,
8351 "assume_aligned parameter not integer constant");
8352 *no_add_attrs = true;
8353 return NULL_TREE;
8356 return NULL_TREE;
8359 /* Handle a "fn spec" attribute; arguments as in
8360 struct attribute_spec.handler. */
8362 static tree
8363 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8364 tree args, int ARG_UNUSED (flags),
8365 bool *no_add_attrs ATTRIBUTE_UNUSED)
8367 gcc_assert (args
8368 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8369 && !TREE_CHAIN (args));
8370 return NULL_TREE;
8373 /* Handle a "bnd_variable_size" attribute; arguments as in
8374 struct attribute_spec.handler. */
8376 static tree
8377 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8378 int ARG_UNUSED (flags), bool *no_add_attrs)
8380 if (TREE_CODE (*node) != FIELD_DECL)
8382 warning (OPT_Wattributes, "%qE attribute ignored", name);
8383 *no_add_attrs = true;
8386 return NULL_TREE;
8389 /* Handle a "bnd_legacy" attribute; arguments as in
8390 struct attribute_spec.handler. */
8392 static tree
8393 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8394 int ARG_UNUSED (flags), bool *no_add_attrs)
8396 if (TREE_CODE (*node) != FUNCTION_DECL)
8398 warning (OPT_Wattributes, "%qE attribute ignored", name);
8399 *no_add_attrs = true;
8402 return NULL_TREE;
8405 /* Handle a "bnd_instrument" attribute; arguments as in
8406 struct attribute_spec.handler. */
8408 static tree
8409 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8410 int ARG_UNUSED (flags), bool *no_add_attrs)
8412 if (TREE_CODE (*node) != FUNCTION_DECL)
8414 warning (OPT_Wattributes, "%qE attribute ignored", name);
8415 *no_add_attrs = true;
8418 return NULL_TREE;
8421 /* Handle a "warn_unused" attribute; arguments as in
8422 struct attribute_spec.handler. */
8424 static tree
8425 handle_warn_unused_attribute (tree *node, tree name,
8426 tree args ATTRIBUTE_UNUSED,
8427 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8429 if (TYPE_P (*node))
8430 /* Do nothing else, just set the attribute. We'll get at
8431 it later with lookup_attribute. */
8433 else
8435 warning (OPT_Wattributes, "%qE attribute ignored", name);
8436 *no_add_attrs = true;
8439 return NULL_TREE;
8442 /* Handle an "omp declare simd" attribute; arguments as in
8443 struct attribute_spec.handler. */
8445 static tree
8446 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8448 return NULL_TREE;
8451 /* Handle an "omp declare target" attribute; arguments as in
8452 struct attribute_spec.handler. */
8454 static tree
8455 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8457 return NULL_TREE;
8460 /* Handle a "returns_twice" attribute; arguments as in
8461 struct attribute_spec.handler. */
8463 static tree
8464 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8465 int ARG_UNUSED (flags), bool *no_add_attrs)
8467 if (TREE_CODE (*node) == FUNCTION_DECL)
8468 DECL_IS_RETURNS_TWICE (*node) = 1;
8469 else
8471 warning (OPT_Wattributes, "%qE attribute ignored", name);
8472 *no_add_attrs = true;
8475 return NULL_TREE;
8478 /* Handle a "no_limit_stack" attribute; arguments as in
8479 struct attribute_spec.handler. */
8481 static tree
8482 handle_no_limit_stack_attribute (tree *node, tree name,
8483 tree ARG_UNUSED (args),
8484 int ARG_UNUSED (flags),
8485 bool *no_add_attrs)
8487 tree decl = *node;
8489 if (TREE_CODE (decl) != FUNCTION_DECL)
8491 error_at (DECL_SOURCE_LOCATION (decl),
8492 "%qE attribute applies only to functions", name);
8493 *no_add_attrs = true;
8495 else if (DECL_INITIAL (decl))
8497 error_at (DECL_SOURCE_LOCATION (decl),
8498 "can%'t set %qE attribute after definition", name);
8499 *no_add_attrs = true;
8501 else
8502 DECL_NO_LIMIT_STACK (decl) = 1;
8504 return NULL_TREE;
8507 /* Handle a "pure" attribute; arguments as in
8508 struct attribute_spec.handler. */
8510 static tree
8511 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8512 int ARG_UNUSED (flags), bool *no_add_attrs)
8514 if (TREE_CODE (*node) == FUNCTION_DECL)
8515 DECL_PURE_P (*node) = 1;
8516 /* ??? TODO: Support types. */
8517 else
8519 warning (OPT_Wattributes, "%qE attribute ignored", name);
8520 *no_add_attrs = true;
8523 return NULL_TREE;
8526 /* Digest an attribute list destined for a transactional memory statement.
8527 ALLOWED is the set of attributes that are allowed for this statement;
8528 return the attribute we parsed. Multiple attributes are never allowed. */
8531 parse_tm_stmt_attr (tree attrs, int allowed)
8533 tree a_seen = NULL;
8534 int m_seen = 0;
8536 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8538 tree a = TREE_PURPOSE (attrs);
8539 int m = 0;
8541 if (is_attribute_p ("outer", a))
8542 m = TM_STMT_ATTR_OUTER;
8544 if ((m & allowed) == 0)
8546 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8547 continue;
8550 if (m_seen == 0)
8552 a_seen = a;
8553 m_seen = m;
8555 else if (m_seen == m)
8556 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8557 else
8558 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8561 return m_seen;
8564 /* Transform a TM attribute name into a maskable integer and back.
8565 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8566 to how the lack of an attribute is treated. */
8569 tm_attr_to_mask (tree attr)
8571 if (attr == NULL)
8572 return 0;
8573 if (is_attribute_p ("transaction_safe", attr))
8574 return TM_ATTR_SAFE;
8575 if (is_attribute_p ("transaction_callable", attr))
8576 return TM_ATTR_CALLABLE;
8577 if (is_attribute_p ("transaction_pure", attr))
8578 return TM_ATTR_PURE;
8579 if (is_attribute_p ("transaction_unsafe", attr))
8580 return TM_ATTR_IRREVOCABLE;
8581 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8582 return TM_ATTR_MAY_CANCEL_OUTER;
8583 return 0;
8586 tree
8587 tm_mask_to_attr (int mask)
8589 const char *str;
8590 switch (mask)
8592 case TM_ATTR_SAFE:
8593 str = "transaction_safe";
8594 break;
8595 case TM_ATTR_CALLABLE:
8596 str = "transaction_callable";
8597 break;
8598 case TM_ATTR_PURE:
8599 str = "transaction_pure";
8600 break;
8601 case TM_ATTR_IRREVOCABLE:
8602 str = "transaction_unsafe";
8603 break;
8604 case TM_ATTR_MAY_CANCEL_OUTER:
8605 str = "transaction_may_cancel_outer";
8606 break;
8607 default:
8608 gcc_unreachable ();
8610 return get_identifier (str);
8613 /* Return the first TM attribute seen in LIST. */
8615 tree
8616 find_tm_attribute (tree list)
8618 for (; list ; list = TREE_CHAIN (list))
8620 tree name = TREE_PURPOSE (list);
8621 if (tm_attr_to_mask (name) != 0)
8622 return name;
8624 return NULL_TREE;
8627 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8628 Here we accept only function types, and verify that none of the other
8629 function TM attributes are also applied. */
8630 /* ??? We need to accept class types for C++, but not C. This greatly
8631 complicates this function, since we can no longer rely on the extra
8632 processing given by function_type_required. */
8634 static tree
8635 handle_tm_attribute (tree *node, tree name, tree args,
8636 int flags, bool *no_add_attrs)
8638 /* Only one path adds the attribute; others don't. */
8639 *no_add_attrs = true;
8641 switch (TREE_CODE (*node))
8643 case RECORD_TYPE:
8644 case UNION_TYPE:
8645 /* Only tm_callable and tm_safe apply to classes. */
8646 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8647 goto ignored;
8648 /* FALLTHRU */
8650 case FUNCTION_TYPE:
8651 case METHOD_TYPE:
8653 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8654 if (old_name == name)
8656 else if (old_name != NULL_TREE)
8657 error ("type was previously declared %qE", old_name);
8658 else
8659 *no_add_attrs = false;
8661 break;
8663 case POINTER_TYPE:
8665 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8666 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8668 tree fn_tmp = TREE_TYPE (*node);
8669 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8670 *node = build_pointer_type (fn_tmp);
8671 break;
8674 /* FALLTHRU */
8676 default:
8677 /* If a function is next, pass it on to be tried next. */
8678 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8679 return tree_cons (name, args, NULL);
8681 ignored:
8682 warning (OPT_Wattributes, "%qE attribute ignored", name);
8683 break;
8686 return NULL_TREE;
8689 /* Handle the TM_WRAP attribute; arguments as in
8690 struct attribute_spec.handler. */
8692 static tree
8693 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8694 int ARG_UNUSED (flags), bool *no_add_attrs)
8696 tree decl = *node;
8698 /* We don't need the attribute even on success, since we
8699 record the entry in an external table. */
8700 *no_add_attrs = true;
8702 if (TREE_CODE (decl) != FUNCTION_DECL)
8703 warning (OPT_Wattributes, "%qE attribute ignored", name);
8704 else
8706 tree wrap_decl = TREE_VALUE (args);
8707 if (error_operand_p (wrap_decl))
8709 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8710 && TREE_CODE (wrap_decl) != VAR_DECL
8711 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8712 error ("%qE argument not an identifier", name);
8713 else
8715 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8716 wrap_decl = lookup_name (wrap_decl);
8717 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8719 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8720 TREE_TYPE (wrap_decl)))
8721 record_tm_replacement (wrap_decl, decl);
8722 else
8723 error ("%qD is not compatible with %qD", wrap_decl, decl);
8725 else
8726 error ("%qE argument is not a function", name);
8730 return NULL_TREE;
8733 /* Ignore the given attribute. Used when this attribute may be usefully
8734 overridden by the target, but is not used generically. */
8736 static tree
8737 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8738 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8739 bool *no_add_attrs)
8741 *no_add_attrs = true;
8742 return NULL_TREE;
8745 /* Handle a "no vops" attribute; arguments as in
8746 struct attribute_spec.handler. */
8748 static tree
8749 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8750 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8751 bool *ARG_UNUSED (no_add_attrs))
8753 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8754 DECL_IS_NOVOPS (*node) = 1;
8755 return NULL_TREE;
8758 /* Handle a "deprecated" attribute; arguments as in
8759 struct attribute_spec.handler. */
8761 static tree
8762 handle_deprecated_attribute (tree *node, tree name,
8763 tree args, int flags,
8764 bool *no_add_attrs)
8766 tree type = NULL_TREE;
8767 int warn = 0;
8768 tree what = NULL_TREE;
8770 if (!args)
8771 *no_add_attrs = true;
8772 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8774 error ("deprecated message is not a string");
8775 *no_add_attrs = true;
8778 if (DECL_P (*node))
8780 tree decl = *node;
8781 type = TREE_TYPE (decl);
8783 if (TREE_CODE (decl) == TYPE_DECL
8784 || TREE_CODE (decl) == PARM_DECL
8785 || TREE_CODE (decl) == VAR_DECL
8786 || TREE_CODE (decl) == FUNCTION_DECL
8787 || TREE_CODE (decl) == FIELD_DECL
8788 || objc_method_decl (TREE_CODE (decl)))
8789 TREE_DEPRECATED (decl) = 1;
8790 else
8791 warn = 1;
8793 else if (TYPE_P (*node))
8795 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8796 *node = build_variant_type_copy (*node);
8797 TREE_DEPRECATED (*node) = 1;
8798 type = *node;
8800 else
8801 warn = 1;
8803 if (warn)
8805 *no_add_attrs = true;
8806 if (type && TYPE_NAME (type))
8808 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8809 what = TYPE_NAME (*node);
8810 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8811 && DECL_NAME (TYPE_NAME (type)))
8812 what = DECL_NAME (TYPE_NAME (type));
8814 if (what)
8815 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8816 else
8817 warning (OPT_Wattributes, "%qE attribute ignored", name);
8820 return NULL_TREE;
8823 /* Handle a "vector_size" attribute; arguments as in
8824 struct attribute_spec.handler. */
8826 static tree
8827 handle_vector_size_attribute (tree *node, tree name, tree args,
8828 int ARG_UNUSED (flags),
8829 bool *no_add_attrs)
8831 unsigned HOST_WIDE_INT vecsize, nunits;
8832 machine_mode orig_mode;
8833 tree type = *node, new_type, size;
8835 *no_add_attrs = true;
8837 size = TREE_VALUE (args);
8838 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8839 && TREE_CODE (size) != FUNCTION_DECL)
8840 size = default_conversion (size);
8842 if (!tree_fits_uhwi_p (size))
8844 warning (OPT_Wattributes, "%qE attribute ignored", name);
8845 return NULL_TREE;
8848 /* Get the vector size (in bytes). */
8849 vecsize = tree_to_uhwi (size);
8851 /* We need to provide for vector pointers, vector arrays, and
8852 functions returning vectors. For example:
8854 __attribute__((vector_size(16))) short *foo;
8856 In this case, the mode is SI, but the type being modified is
8857 HI, so we need to look further. */
8859 while (POINTER_TYPE_P (type)
8860 || TREE_CODE (type) == FUNCTION_TYPE
8861 || TREE_CODE (type) == METHOD_TYPE
8862 || TREE_CODE (type) == ARRAY_TYPE
8863 || TREE_CODE (type) == OFFSET_TYPE)
8864 type = TREE_TYPE (type);
8866 /* Get the mode of the type being modified. */
8867 orig_mode = TYPE_MODE (type);
8869 if ((!INTEGRAL_TYPE_P (type)
8870 && !SCALAR_FLOAT_TYPE_P (type)
8871 && !FIXED_POINT_TYPE_P (type))
8872 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8873 && GET_MODE_CLASS (orig_mode) != MODE_INT
8874 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8875 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8876 || TREE_CODE (type) == BOOLEAN_TYPE)
8878 error ("invalid vector type for attribute %qE", name);
8879 return NULL_TREE;
8882 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8884 error ("vector size not an integral multiple of component size");
8885 return NULL;
8888 if (vecsize == 0)
8890 error ("zero vector size");
8891 return NULL;
8894 /* Calculate how many units fit in the vector. */
8895 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8896 if (nunits & (nunits - 1))
8898 error ("number of components of the vector not a power of two");
8899 return NULL_TREE;
8902 new_type = build_vector_type (type, nunits);
8904 /* Build back pointers if needed. */
8905 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8907 return NULL_TREE;
8910 /* Handle the "nonnull" attribute. */
8911 static tree
8912 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8913 tree args, int ARG_UNUSED (flags),
8914 bool *no_add_attrs)
8916 tree type = *node;
8917 unsigned HOST_WIDE_INT attr_arg_num;
8919 /* If no arguments are specified, all pointer arguments should be
8920 non-null. Verify a full prototype is given so that the arguments
8921 will have the correct types when we actually check them later. */
8922 if (!args)
8924 if (!prototype_p (type))
8926 error ("nonnull attribute without arguments on a non-prototype");
8927 *no_add_attrs = true;
8929 return NULL_TREE;
8932 /* Argument list specified. Verify that each argument number references
8933 a pointer argument. */
8934 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8936 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8938 tree arg = TREE_VALUE (args);
8939 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8940 && TREE_CODE (arg) != FUNCTION_DECL)
8941 arg = default_conversion (arg);
8943 if (!get_nonnull_operand (arg, &arg_num))
8945 error ("nonnull argument has invalid operand number (argument %lu)",
8946 (unsigned long) attr_arg_num);
8947 *no_add_attrs = true;
8948 return NULL_TREE;
8951 if (prototype_p (type))
8953 function_args_iterator iter;
8954 tree argument;
8956 function_args_iter_init (&iter, type);
8957 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8959 argument = function_args_iter_cond (&iter);
8960 if (argument == NULL_TREE || ck_num == arg_num)
8961 break;
8964 if (!argument
8965 || TREE_CODE (argument) == VOID_TYPE)
8967 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8968 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8969 *no_add_attrs = true;
8970 return NULL_TREE;
8973 if (TREE_CODE (argument) != POINTER_TYPE)
8975 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8976 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8977 *no_add_attrs = true;
8978 return NULL_TREE;
8983 return NULL_TREE;
8986 /* Check the argument list of a function call for null in argument slots
8987 that are marked as requiring a non-null pointer argument. The NARGS
8988 arguments are passed in the array ARGARRAY.
8991 static void
8992 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8994 tree a;
8995 int i;
8997 attrs = lookup_attribute ("nonnull", attrs);
8998 if (attrs == NULL_TREE)
8999 return;
9001 a = attrs;
9002 /* See if any of the nonnull attributes has no arguments. If so,
9003 then every pointer argument is checked (in which case the check
9004 for pointer type is done in check_nonnull_arg). */
9005 if (TREE_VALUE (a) != NULL_TREE)
9007 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9008 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9010 if (a != NULL_TREE)
9011 for (i = 0; i < nargs; i++)
9012 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9013 i + 1);
9014 else
9016 /* Walk the argument list. If we encounter an argument number we
9017 should check for non-null, do it. */
9018 for (i = 0; i < nargs; i++)
9020 for (a = attrs; ; a = TREE_CHAIN (a))
9022 a = lookup_attribute ("nonnull", a);
9023 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9024 break;
9027 if (a != NULL_TREE)
9028 check_function_arguments_recurse (check_nonnull_arg, NULL,
9029 argarray[i], i + 1);
9034 /* Check that the Nth argument of a function call (counting backwards
9035 from the end) is a (pointer)0. The NARGS arguments are passed in the
9036 array ARGARRAY. */
9038 static void
9039 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9041 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9043 if (attr)
9045 int len = 0;
9046 int pos = 0;
9047 tree sentinel;
9048 function_args_iterator iter;
9049 tree t;
9051 /* Skip over the named arguments. */
9052 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9054 if (len == nargs)
9055 break;
9056 len++;
9059 if (TREE_VALUE (attr))
9061 tree p = TREE_VALUE (TREE_VALUE (attr));
9062 pos = TREE_INT_CST_LOW (p);
9065 /* The sentinel must be one of the varargs, i.e.
9066 in position >= the number of fixed arguments. */
9067 if ((nargs - 1 - pos) < len)
9069 warning (OPT_Wformat_,
9070 "not enough variable arguments to fit a sentinel");
9071 return;
9074 /* Validate the sentinel. */
9075 sentinel = argarray[nargs - 1 - pos];
9076 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9077 || !integer_zerop (sentinel))
9078 /* Although __null (in C++) is only an integer we allow it
9079 nevertheless, as we are guaranteed that it's exactly
9080 as wide as a pointer, and we don't want to force
9081 users to cast the NULL they have written there.
9082 We warn with -Wstrict-null-sentinel, though. */
9083 && (warn_strict_null_sentinel || null_node != sentinel))
9084 warning (OPT_Wformat_, "missing sentinel in function call");
9088 /* Helper for check_function_nonnull; given a list of operands which
9089 must be non-null in ARGS, determine if operand PARAM_NUM should be
9090 checked. */
9092 static bool
9093 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9095 unsigned HOST_WIDE_INT arg_num = 0;
9097 for (; args; args = TREE_CHAIN (args))
9099 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9101 gcc_assert (found);
9103 if (arg_num == param_num)
9104 return true;
9106 return false;
9109 /* Check that the function argument PARAM (which is operand number
9110 PARAM_NUM) is non-null. This is called by check_function_nonnull
9111 via check_function_arguments_recurse. */
9113 static void
9114 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9115 unsigned HOST_WIDE_INT param_num)
9117 /* Just skip checking the argument if it's not a pointer. This can
9118 happen if the "nonnull" attribute was given without an operand
9119 list (which means to check every pointer argument). */
9121 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9122 return;
9124 if (integer_zerop (param))
9125 warning (OPT_Wnonnull, "null argument where non-null required "
9126 "(argument %lu)", (unsigned long) param_num);
9129 /* Helper for nonnull attribute handling; fetch the operand number
9130 from the attribute argument list. */
9132 static bool
9133 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9135 /* Verify the arg number is a small constant. */
9136 if (tree_fits_uhwi_p (arg_num_expr))
9138 *valp = TREE_INT_CST_LOW (arg_num_expr);
9139 return true;
9141 else
9142 return false;
9145 /* Handle a "nothrow" attribute; arguments as in
9146 struct attribute_spec.handler. */
9148 static tree
9149 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9150 int ARG_UNUSED (flags), bool *no_add_attrs)
9152 if (TREE_CODE (*node) == FUNCTION_DECL)
9153 TREE_NOTHROW (*node) = 1;
9154 /* ??? TODO: Support types. */
9155 else
9157 warning (OPT_Wattributes, "%qE attribute ignored", name);
9158 *no_add_attrs = true;
9161 return NULL_TREE;
9164 /* Handle a "cleanup" attribute; arguments as in
9165 struct attribute_spec.handler. */
9167 static tree
9168 handle_cleanup_attribute (tree *node, tree name, tree args,
9169 int ARG_UNUSED (flags), bool *no_add_attrs)
9171 tree decl = *node;
9172 tree cleanup_id, cleanup_decl;
9174 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9175 for global destructors in C++. This requires infrastructure that
9176 we don't have generically at the moment. It's also not a feature
9177 we'd be missing too much, since we do have attribute constructor. */
9178 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9180 warning (OPT_Wattributes, "%qE attribute ignored", name);
9181 *no_add_attrs = true;
9182 return NULL_TREE;
9185 /* Verify that the argument is a function in scope. */
9186 /* ??? We could support pointers to functions here as well, if
9187 that was considered desirable. */
9188 cleanup_id = TREE_VALUE (args);
9189 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9191 error ("cleanup argument not an identifier");
9192 *no_add_attrs = true;
9193 return NULL_TREE;
9195 cleanup_decl = lookup_name (cleanup_id);
9196 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9198 error ("cleanup argument not a function");
9199 *no_add_attrs = true;
9200 return NULL_TREE;
9203 /* That the function has proper type is checked with the
9204 eventual call to build_function_call. */
9206 return NULL_TREE;
9209 /* Handle a "warn_unused_result" attribute. No special handling. */
9211 static tree
9212 handle_warn_unused_result_attribute (tree *node, tree name,
9213 tree ARG_UNUSED (args),
9214 int ARG_UNUSED (flags), bool *no_add_attrs)
9216 /* Ignore the attribute for functions not returning any value. */
9217 if (VOID_TYPE_P (TREE_TYPE (*node)))
9219 warning (OPT_Wattributes, "%qE attribute ignored", name);
9220 *no_add_attrs = true;
9223 return NULL_TREE;
9226 /* Handle a "sentinel" attribute. */
9228 static tree
9229 handle_sentinel_attribute (tree *node, tree name, tree args,
9230 int ARG_UNUSED (flags), bool *no_add_attrs)
9232 if (!prototype_p (*node))
9234 warning (OPT_Wattributes,
9235 "%qE attribute requires prototypes with named arguments", name);
9236 *no_add_attrs = true;
9238 else
9240 if (!stdarg_p (*node))
9242 warning (OPT_Wattributes,
9243 "%qE attribute only applies to variadic functions", name);
9244 *no_add_attrs = true;
9248 if (args)
9250 tree position = TREE_VALUE (args);
9251 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9252 && TREE_CODE (position) != FUNCTION_DECL)
9253 position = default_conversion (position);
9255 if (TREE_CODE (position) != INTEGER_CST
9256 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9258 warning (OPT_Wattributes,
9259 "requested position is not an integer constant");
9260 *no_add_attrs = true;
9262 else
9264 if (tree_int_cst_lt (position, integer_zero_node))
9266 warning (OPT_Wattributes,
9267 "requested position is less than zero");
9268 *no_add_attrs = true;
9273 return NULL_TREE;
9276 /* Handle a "type_generic" attribute. */
9278 static tree
9279 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9280 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9281 bool * ARG_UNUSED (no_add_attrs))
9283 /* Ensure we have a function type. */
9284 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9286 /* Ensure we have a variadic function. */
9287 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9289 return NULL_TREE;
9292 /* Handle a "target" attribute. */
9294 static tree
9295 handle_target_attribute (tree *node, tree name, tree args, int flags,
9296 bool *no_add_attrs)
9298 /* Ensure we have a function type. */
9299 if (TREE_CODE (*node) != FUNCTION_DECL)
9301 warning (OPT_Wattributes, "%qE attribute ignored", name);
9302 *no_add_attrs = true;
9304 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9305 flags))
9306 *no_add_attrs = true;
9308 return NULL_TREE;
9311 /* Arguments being collected for optimization. */
9312 typedef const char *const_char_p; /* For DEF_VEC_P. */
9313 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9316 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9317 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9318 false for #pragma GCC optimize. */
9320 bool
9321 parse_optimize_options (tree args, bool attr_p)
9323 bool ret = true;
9324 unsigned opt_argc;
9325 unsigned i;
9326 int saved_flag_strict_aliasing;
9327 const char **opt_argv;
9328 struct cl_decoded_option *decoded_options;
9329 unsigned int decoded_options_count;
9330 tree ap;
9332 /* Build up argv vector. Just in case the string is stored away, use garbage
9333 collected strings. */
9334 vec_safe_truncate (optimize_args, 0);
9335 vec_safe_push (optimize_args, (const char *) NULL);
9337 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9339 tree value = TREE_VALUE (ap);
9341 if (TREE_CODE (value) == INTEGER_CST)
9343 char buffer[20];
9344 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9345 vec_safe_push (optimize_args, ggc_strdup (buffer));
9348 else if (TREE_CODE (value) == STRING_CST)
9350 /* Split string into multiple substrings. */
9351 size_t len = TREE_STRING_LENGTH (value);
9352 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9353 char *end = p + len;
9354 char *comma;
9355 char *next_p = p;
9357 while (next_p != NULL)
9359 size_t len2;
9360 char *q, *r;
9362 p = next_p;
9363 comma = strchr (p, ',');
9364 if (comma)
9366 len2 = comma - p;
9367 *comma = '\0';
9368 next_p = comma+1;
9370 else
9372 len2 = end - p;
9373 next_p = NULL;
9376 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9378 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9379 options. */
9380 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9382 ret = false;
9383 if (attr_p)
9384 warning (OPT_Wattributes,
9385 "bad option %s to optimize attribute", p);
9386 else
9387 warning (OPT_Wpragmas,
9388 "bad option %s to pragma attribute", p);
9389 continue;
9392 if (*p != '-')
9394 *r++ = '-';
9396 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9397 itself is -Os, and any other switch begins with a -f. */
9398 if ((*p >= '0' && *p <= '9')
9399 || (p[0] == 's' && p[1] == '\0'))
9400 *r++ = 'O';
9401 else if (*p != 'O')
9402 *r++ = 'f';
9405 memcpy (r, p, len2);
9406 r[len2] = '\0';
9407 vec_safe_push (optimize_args, (const char *) q);
9413 opt_argc = optimize_args->length ();
9414 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9416 for (i = 1; i < opt_argc; i++)
9417 opt_argv[i] = (*optimize_args)[i];
9419 saved_flag_strict_aliasing = flag_strict_aliasing;
9421 /* Now parse the options. */
9422 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9423 &decoded_options,
9424 &decoded_options_count);
9425 decode_options (&global_options, &global_options_set,
9426 decoded_options, decoded_options_count,
9427 input_location, global_dc);
9429 targetm.override_options_after_change();
9431 /* Don't allow changing -fstrict-aliasing. */
9432 flag_strict_aliasing = saved_flag_strict_aliasing;
9434 optimize_args->truncate (0);
9435 return ret;
9438 /* For handling "optimize" attribute. arguments as in
9439 struct attribute_spec.handler. */
9441 static tree
9442 handle_optimize_attribute (tree *node, tree name, tree args,
9443 int ARG_UNUSED (flags), bool *no_add_attrs)
9445 /* Ensure we have a function type. */
9446 if (TREE_CODE (*node) != FUNCTION_DECL)
9448 warning (OPT_Wattributes, "%qE attribute ignored", name);
9449 *no_add_attrs = true;
9451 else
9453 struct cl_optimization cur_opts;
9454 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9456 /* Save current options. */
9457 cl_optimization_save (&cur_opts, &global_options);
9459 /* If we previously had some optimization options, use them as the
9460 default. */
9461 if (old_opts)
9462 cl_optimization_restore (&global_options,
9463 TREE_OPTIMIZATION (old_opts));
9465 /* Parse options, and update the vector. */
9466 parse_optimize_options (args, true);
9467 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9468 = build_optimization_node (&global_options);
9470 /* Restore current options. */
9471 cl_optimization_restore (&global_options, &cur_opts);
9474 return NULL_TREE;
9477 /* Handle a "no_split_stack" attribute. */
9479 static tree
9480 handle_no_split_stack_attribute (tree *node, tree name,
9481 tree ARG_UNUSED (args),
9482 int ARG_UNUSED (flags),
9483 bool *no_add_attrs)
9485 tree decl = *node;
9487 if (TREE_CODE (decl) != FUNCTION_DECL)
9489 error_at (DECL_SOURCE_LOCATION (decl),
9490 "%qE attribute applies only to functions", name);
9491 *no_add_attrs = true;
9493 else if (DECL_INITIAL (decl))
9495 error_at (DECL_SOURCE_LOCATION (decl),
9496 "can%'t set %qE attribute after definition", name);
9497 *no_add_attrs = true;
9500 return NULL_TREE;
9503 /* Handle a "returns_nonnull" attribute; arguments as in
9504 struct attribute_spec.handler. */
9506 static tree
9507 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9508 bool *no_add_attrs)
9510 // Even without a prototype we still have a return type we can check.
9511 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9513 error ("returns_nonnull attribute on a function not returning a pointer");
9514 *no_add_attrs = true;
9516 return NULL_TREE;
9519 /* Handle a "designated_init" attribute; arguments as in
9520 struct attribute_spec.handler. */
9522 static tree
9523 handle_designated_init_attribute (tree *node, tree name, tree, int,
9524 bool *no_add_attrs)
9526 if (TREE_CODE (*node) != RECORD_TYPE)
9528 error ("%qE attribute is only valid on %<struct%> type", name);
9529 *no_add_attrs = true;
9531 return NULL_TREE;
9535 /* Check for valid arguments being passed to a function with FNTYPE.
9536 There are NARGS arguments in the array ARGARRAY. */
9537 void
9538 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9540 /* Check for null being passed in a pointer argument that must be
9541 non-null. We also need to do this if format checking is enabled. */
9543 if (warn_nonnull)
9544 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9546 /* Check for errors in format strings. */
9548 if (warn_format || warn_suggest_attribute_format)
9549 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9551 if (warn_format)
9552 check_function_sentinel (fntype, nargs, argarray);
9555 /* Generic argument checking recursion routine. PARAM is the argument to
9556 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9557 once the argument is resolved. CTX is context for the callback. */
9558 void
9559 check_function_arguments_recurse (void (*callback)
9560 (void *, tree, unsigned HOST_WIDE_INT),
9561 void *ctx, tree param,
9562 unsigned HOST_WIDE_INT param_num)
9564 if (CONVERT_EXPR_P (param)
9565 && (TYPE_PRECISION (TREE_TYPE (param))
9566 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9568 /* Strip coercion. */
9569 check_function_arguments_recurse (callback, ctx,
9570 TREE_OPERAND (param, 0), param_num);
9571 return;
9574 if (TREE_CODE (param) == CALL_EXPR)
9576 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9577 tree attrs;
9578 bool found_format_arg = false;
9580 /* See if this is a call to a known internationalization function
9581 that modifies a format arg. Such a function may have multiple
9582 format_arg attributes (for example, ngettext). */
9584 for (attrs = TYPE_ATTRIBUTES (type);
9585 attrs;
9586 attrs = TREE_CHAIN (attrs))
9587 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9589 tree inner_arg;
9590 tree format_num_expr;
9591 int format_num;
9592 int i;
9593 call_expr_arg_iterator iter;
9595 /* Extract the argument number, which was previously checked
9596 to be valid. */
9597 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9599 format_num = tree_to_uhwi (format_num_expr);
9601 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9602 inner_arg != 0;
9603 inner_arg = next_call_expr_arg (&iter), i++)
9604 if (i == format_num)
9606 check_function_arguments_recurse (callback, ctx,
9607 inner_arg, param_num);
9608 found_format_arg = true;
9609 break;
9613 /* If we found a format_arg attribute and did a recursive check,
9614 we are done with checking this argument. Otherwise, we continue
9615 and this will be considered a non-literal. */
9616 if (found_format_arg)
9617 return;
9620 if (TREE_CODE (param) == COND_EXPR)
9622 /* Check both halves of the conditional expression. */
9623 check_function_arguments_recurse (callback, ctx,
9624 TREE_OPERAND (param, 1), param_num);
9625 check_function_arguments_recurse (callback, ctx,
9626 TREE_OPERAND (param, 2), param_num);
9627 return;
9630 (*callback) (ctx, param, param_num);
9633 /* Checks for a builtin function FNDECL that the number of arguments
9634 NARGS against the required number REQUIRED and issues an error if
9635 there is a mismatch. Returns true if the number of arguments is
9636 correct, otherwise false. */
9638 static bool
9639 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9641 if (nargs < required)
9643 error_at (input_location,
9644 "not enough arguments to function %qE", fndecl);
9645 return false;
9647 else if (nargs > required)
9649 error_at (input_location,
9650 "too many arguments to function %qE", fndecl);
9651 return false;
9653 return true;
9656 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9657 Returns false if there was an error, otherwise true. */
9659 bool
9660 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9662 if (!DECL_BUILT_IN (fndecl)
9663 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9664 return true;
9666 switch (DECL_FUNCTION_CODE (fndecl))
9668 case BUILT_IN_CONSTANT_P:
9669 return builtin_function_validate_nargs (fndecl, nargs, 1);
9671 case BUILT_IN_ISFINITE:
9672 case BUILT_IN_ISINF:
9673 case BUILT_IN_ISINF_SIGN:
9674 case BUILT_IN_ISNAN:
9675 case BUILT_IN_ISNORMAL:
9676 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9678 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9680 error ("non-floating-point argument in call to "
9681 "function %qE", fndecl);
9682 return false;
9684 return true;
9686 return false;
9688 case BUILT_IN_ISGREATER:
9689 case BUILT_IN_ISGREATEREQUAL:
9690 case BUILT_IN_ISLESS:
9691 case BUILT_IN_ISLESSEQUAL:
9692 case BUILT_IN_ISLESSGREATER:
9693 case BUILT_IN_ISUNORDERED:
9694 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9696 enum tree_code code0, code1;
9697 code0 = TREE_CODE (TREE_TYPE (args[0]));
9698 code1 = TREE_CODE (TREE_TYPE (args[1]));
9699 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9700 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9701 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9703 error ("non-floating-point arguments in call to "
9704 "function %qE", fndecl);
9705 return false;
9707 return true;
9709 return false;
9711 case BUILT_IN_FPCLASSIFY:
9712 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9714 unsigned i;
9716 for (i=0; i<5; i++)
9717 if (TREE_CODE (args[i]) != INTEGER_CST)
9719 error ("non-const integer argument %u in call to function %qE",
9720 i+1, fndecl);
9721 return false;
9724 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9726 error ("non-floating-point argument in call to function %qE",
9727 fndecl);
9728 return false;
9730 return true;
9732 return false;
9734 case BUILT_IN_ASSUME_ALIGNED:
9735 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9737 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9739 error ("non-integer argument 3 in call to function %qE", fndecl);
9740 return false;
9742 return true;
9744 return false;
9746 case BUILT_IN_ADD_OVERFLOW:
9747 case BUILT_IN_SUB_OVERFLOW:
9748 case BUILT_IN_MUL_OVERFLOW:
9749 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9751 unsigned i;
9752 for (i = 0; i < 2; i++)
9753 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9755 error ("argument %u in call to function %qE does not have "
9756 "integral type", i + 1, fndecl);
9757 return false;
9759 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9760 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9762 error ("argument 3 in call to function %qE does not have "
9763 "pointer to integer type", fndecl);
9764 return false;
9766 return true;
9768 return false;
9770 default:
9771 return true;
9775 /* Function to help qsort sort FIELD_DECLs by name order. */
9778 field_decl_cmp (const void *x_p, const void *y_p)
9780 const tree *const x = (const tree *const) x_p;
9781 const tree *const y = (const tree *const) y_p;
9783 if (DECL_NAME (*x) == DECL_NAME (*y))
9784 /* A nontype is "greater" than a type. */
9785 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9786 if (DECL_NAME (*x) == NULL_TREE)
9787 return -1;
9788 if (DECL_NAME (*y) == NULL_TREE)
9789 return 1;
9790 if (DECL_NAME (*x) < DECL_NAME (*y))
9791 return -1;
9792 return 1;
9795 static struct {
9796 gt_pointer_operator new_value;
9797 void *cookie;
9798 } resort_data;
9800 /* This routine compares two fields like field_decl_cmp but using the
9801 pointer operator in resort_data. */
9803 static int
9804 resort_field_decl_cmp (const void *x_p, const void *y_p)
9806 const tree *const x = (const tree *const) x_p;
9807 const tree *const y = (const tree *const) y_p;
9809 if (DECL_NAME (*x) == DECL_NAME (*y))
9810 /* A nontype is "greater" than a type. */
9811 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9812 if (DECL_NAME (*x) == NULL_TREE)
9813 return -1;
9814 if (DECL_NAME (*y) == NULL_TREE)
9815 return 1;
9817 tree d1 = DECL_NAME (*x);
9818 tree d2 = DECL_NAME (*y);
9819 resort_data.new_value (&d1, resort_data.cookie);
9820 resort_data.new_value (&d2, resort_data.cookie);
9821 if (d1 < d2)
9822 return -1;
9824 return 1;
9827 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9829 void
9830 resort_sorted_fields (void *obj,
9831 void * ARG_UNUSED (orig_obj),
9832 gt_pointer_operator new_value,
9833 void *cookie)
9835 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9836 resort_data.new_value = new_value;
9837 resort_data.cookie = cookie;
9838 qsort (&sf->elts[0], sf->len, sizeof (tree),
9839 resort_field_decl_cmp);
9842 /* Subroutine of c_parse_error.
9843 Return the result of concatenating LHS and RHS. RHS is really
9844 a string literal, its first character is indicated by RHS_START and
9845 RHS_SIZE is its length (including the terminating NUL character).
9847 The caller is responsible for deleting the returned pointer. */
9849 static char *
9850 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9852 const int lhs_size = strlen (lhs);
9853 char *result = XNEWVEC (char, lhs_size + rhs_size);
9854 strncpy (result, lhs, lhs_size);
9855 strncpy (result + lhs_size, rhs_start, rhs_size);
9856 return result;
9859 /* Issue the error given by GMSGID, indicating that it occurred before
9860 TOKEN, which had the associated VALUE. */
9862 void
9863 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9864 tree value, unsigned char token_flags)
9866 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9868 char *message = NULL;
9870 if (token_type == CPP_EOF)
9871 message = catenate_messages (gmsgid, " at end of input");
9872 else if (token_type == CPP_CHAR
9873 || token_type == CPP_WCHAR
9874 || token_type == CPP_CHAR16
9875 || token_type == CPP_CHAR32)
9877 unsigned int val = TREE_INT_CST_LOW (value);
9878 const char *prefix;
9880 switch (token_type)
9882 default:
9883 prefix = "";
9884 break;
9885 case CPP_WCHAR:
9886 prefix = "L";
9887 break;
9888 case CPP_CHAR16:
9889 prefix = "u";
9890 break;
9891 case CPP_CHAR32:
9892 prefix = "U";
9893 break;
9896 if (val <= UCHAR_MAX && ISGRAPH (val))
9897 message = catenate_messages (gmsgid, " before %s'%c'");
9898 else
9899 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9901 error (message, prefix, val);
9902 free (message);
9903 message = NULL;
9905 else if (token_type == CPP_CHAR_USERDEF
9906 || token_type == CPP_WCHAR_USERDEF
9907 || token_type == CPP_CHAR16_USERDEF
9908 || token_type == CPP_CHAR32_USERDEF)
9909 message = catenate_messages (gmsgid,
9910 " before user-defined character literal");
9911 else if (token_type == CPP_STRING_USERDEF
9912 || token_type == CPP_WSTRING_USERDEF
9913 || token_type == CPP_STRING16_USERDEF
9914 || token_type == CPP_STRING32_USERDEF
9915 || token_type == CPP_UTF8STRING_USERDEF)
9916 message = catenate_messages (gmsgid, " before user-defined string literal");
9917 else if (token_type == CPP_STRING
9918 || token_type == CPP_WSTRING
9919 || token_type == CPP_STRING16
9920 || token_type == CPP_STRING32
9921 || token_type == CPP_UTF8STRING)
9922 message = catenate_messages (gmsgid, " before string constant");
9923 else if (token_type == CPP_NUMBER)
9924 message = catenate_messages (gmsgid, " before numeric constant");
9925 else if (token_type == CPP_NAME)
9927 message = catenate_messages (gmsgid, " before %qE");
9928 error (message, value);
9929 free (message);
9930 message = NULL;
9932 else if (token_type == CPP_PRAGMA)
9933 message = catenate_messages (gmsgid, " before %<#pragma%>");
9934 else if (token_type == CPP_PRAGMA_EOL)
9935 message = catenate_messages (gmsgid, " before end of line");
9936 else if (token_type == CPP_DECLTYPE)
9937 message = catenate_messages (gmsgid, " before %<decltype%>");
9938 else if (token_type < N_TTYPES)
9940 message = catenate_messages (gmsgid, " before %qs token");
9941 error (message, cpp_type2name (token_type, token_flags));
9942 free (message);
9943 message = NULL;
9945 else
9946 error (gmsgid);
9948 if (message)
9950 error (message);
9951 free (message);
9953 #undef catenate_messages
9956 /* Return the gcc option code associated with the reason for a cpp
9957 message, or 0 if none. */
9959 static int
9960 c_option_controlling_cpp_error (int reason)
9962 const struct cpp_reason_option_codes_t *entry;
9964 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
9966 if (entry->reason == reason)
9967 return entry->option_code;
9969 return 0;
9972 /* Callback from cpp_error for PFILE to print diagnostics from the
9973 preprocessor. The diagnostic is of type LEVEL, with REASON set
9974 to the reason code if LEVEL is represents a warning, at location
9975 LOCATION unless this is after lexing and the compiler's location
9976 should be used instead, with column number possibly overridden by
9977 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9978 the arguments. Returns true if a diagnostic was emitted, false
9979 otherwise. */
9981 bool
9982 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9983 location_t location, unsigned int column_override,
9984 const char *msg, va_list *ap)
9986 diagnostic_info diagnostic;
9987 diagnostic_t dlevel;
9988 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9989 bool ret;
9991 switch (level)
9993 case CPP_DL_WARNING_SYSHDR:
9994 if (flag_no_output)
9995 return false;
9996 global_dc->dc_warn_system_headers = 1;
9997 /* Fall through. */
9998 case CPP_DL_WARNING:
9999 if (flag_no_output)
10000 return false;
10001 dlevel = DK_WARNING;
10002 break;
10003 case CPP_DL_PEDWARN:
10004 if (flag_no_output && !flag_pedantic_errors)
10005 return false;
10006 dlevel = DK_PEDWARN;
10007 break;
10008 case CPP_DL_ERROR:
10009 dlevel = DK_ERROR;
10010 break;
10011 case CPP_DL_ICE:
10012 dlevel = DK_ICE;
10013 break;
10014 case CPP_DL_NOTE:
10015 dlevel = DK_NOTE;
10016 break;
10017 case CPP_DL_FATAL:
10018 dlevel = DK_FATAL;
10019 break;
10020 default:
10021 gcc_unreachable ();
10023 if (done_lexing)
10024 location = input_location;
10025 diagnostic_set_info_translated (&diagnostic, msg, ap,
10026 location, dlevel);
10027 if (column_override)
10028 diagnostic_override_column (&diagnostic, column_override);
10029 diagnostic_override_option_index (&diagnostic,
10030 c_option_controlling_cpp_error (reason));
10031 ret = report_diagnostic (&diagnostic);
10032 if (level == CPP_DL_WARNING_SYSHDR)
10033 global_dc->dc_warn_system_headers = save_warn_system_headers;
10034 return ret;
10037 /* Convert a character from the host to the target execution character
10038 set. cpplib handles this, mostly. */
10040 HOST_WIDE_INT
10041 c_common_to_target_charset (HOST_WIDE_INT c)
10043 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10044 zero-extended under -fno-signed-char. cpplib insists that characters
10045 and character constants are always unsigned. Hence we must convert
10046 back and forth. */
10047 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10049 uc = cpp_host_to_exec_charset (parse_in, uc);
10051 if (flag_signed_char)
10052 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10053 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10054 else
10055 return uc;
10058 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10059 references with an INDIRECT_REF of a constant at the bottom; much like the
10060 traditional rendering of offsetof as a macro. Return the folded result. */
10062 tree
10063 fold_offsetof_1 (tree expr)
10065 tree base, off, t;
10067 switch (TREE_CODE (expr))
10069 case ERROR_MARK:
10070 return expr;
10072 case VAR_DECL:
10073 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10074 return error_mark_node;
10076 case CALL_EXPR:
10077 case TARGET_EXPR:
10078 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10079 return error_mark_node;
10081 case NOP_EXPR:
10082 case INDIRECT_REF:
10083 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10085 error ("cannot apply %<offsetof%> to a non constant address");
10086 return error_mark_node;
10088 return TREE_OPERAND (expr, 0);
10090 case COMPONENT_REF:
10091 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10092 if (base == error_mark_node)
10093 return base;
10095 t = TREE_OPERAND (expr, 1);
10096 if (DECL_C_BIT_FIELD (t))
10098 error ("attempt to take address of bit-field structure "
10099 "member %qD", t);
10100 return error_mark_node;
10102 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10103 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10104 / BITS_PER_UNIT));
10105 break;
10107 case ARRAY_REF:
10108 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10109 if (base == error_mark_node)
10110 return base;
10112 t = TREE_OPERAND (expr, 1);
10114 /* Check if the offset goes beyond the upper bound of the array. */
10115 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10117 tree upbound = array_ref_up_bound (expr);
10118 if (upbound != NULL_TREE
10119 && TREE_CODE (upbound) == INTEGER_CST
10120 && !tree_int_cst_equal (upbound,
10121 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10123 upbound = size_binop (PLUS_EXPR, upbound,
10124 build_int_cst (TREE_TYPE (upbound), 1));
10125 if (tree_int_cst_lt (upbound, t))
10127 tree v;
10129 for (v = TREE_OPERAND (expr, 0);
10130 TREE_CODE (v) == COMPONENT_REF;
10131 v = TREE_OPERAND (v, 0))
10132 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10133 == RECORD_TYPE)
10135 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10136 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10137 if (TREE_CODE (fld_chain) == FIELD_DECL)
10138 break;
10140 if (fld_chain)
10141 break;
10143 /* Don't warn if the array might be considered a poor
10144 man's flexible array member with a very permissive
10145 definition thereof. */
10146 if (TREE_CODE (v) == ARRAY_REF
10147 || TREE_CODE (v) == COMPONENT_REF)
10148 warning (OPT_Warray_bounds,
10149 "index %E denotes an offset "
10150 "greater than size of %qT",
10151 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10156 t = convert (sizetype, t);
10157 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10158 break;
10160 case COMPOUND_EXPR:
10161 /* Handle static members of volatile structs. */
10162 t = TREE_OPERAND (expr, 1);
10163 gcc_assert (TREE_CODE (t) == VAR_DECL);
10164 return fold_offsetof_1 (t);
10166 default:
10167 gcc_unreachable ();
10170 return fold_build_pointer_plus (base, off);
10173 /* Likewise, but convert it to the return type of offsetof. */
10175 tree
10176 fold_offsetof (tree expr)
10178 return convert (size_type_node, fold_offsetof_1 (expr));
10181 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10182 expression, because B will always be true. */
10184 void
10185 warn_for_omitted_condop (location_t location, tree cond)
10187 if (truth_value_p (TREE_CODE (cond)))
10188 warning_at (location, OPT_Wparentheses,
10189 "the omitted middle operand in ?: will always be %<true%>, "
10190 "suggest explicit middle operand");
10193 /* Give an error for storing into ARG, which is 'const'. USE indicates
10194 how ARG was being used. */
10196 void
10197 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10199 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10200 || use == lv_asm);
10201 /* Using this macro rather than (for example) arrays of messages
10202 ensures that all the format strings are checked at compile
10203 time. */
10204 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10205 : (use == lv_increment ? (I) \
10206 : (use == lv_decrement ? (D) : (AS))))
10207 if (TREE_CODE (arg) == COMPONENT_REF)
10209 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10210 error_at (loc, READONLY_MSG (G_("assignment of member "
10211 "%qD in read-only object"),
10212 G_("increment of member "
10213 "%qD in read-only object"),
10214 G_("decrement of member "
10215 "%qD in read-only object"),
10216 G_("member %qD in read-only object "
10217 "used as %<asm%> output")),
10218 TREE_OPERAND (arg, 1));
10219 else
10220 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10221 G_("increment of read-only member %qD"),
10222 G_("decrement of read-only member %qD"),
10223 G_("read-only member %qD used as %<asm%> output")),
10224 TREE_OPERAND (arg, 1));
10226 else if (TREE_CODE (arg) == VAR_DECL)
10227 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10228 G_("increment of read-only variable %qD"),
10229 G_("decrement of read-only variable %qD"),
10230 G_("read-only variable %qD used as %<asm%> output")),
10231 arg);
10232 else if (TREE_CODE (arg) == PARM_DECL)
10233 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10234 G_("increment of read-only parameter %qD"),
10235 G_("decrement of read-only parameter %qD"),
10236 G_("read-only parameter %qD use as %<asm%> output")),
10237 arg);
10238 else if (TREE_CODE (arg) == RESULT_DECL)
10240 gcc_assert (c_dialect_cxx ());
10241 error_at (loc, READONLY_MSG (G_("assignment of "
10242 "read-only named return value %qD"),
10243 G_("increment of "
10244 "read-only named return value %qD"),
10245 G_("decrement of "
10246 "read-only named return value %qD"),
10247 G_("read-only named return value %qD "
10248 "used as %<asm%>output")),
10249 arg);
10251 else if (TREE_CODE (arg) == FUNCTION_DECL)
10252 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10253 G_("increment of function %qD"),
10254 G_("decrement of function %qD"),
10255 G_("function %qD used as %<asm%> output")),
10256 arg);
10257 else
10258 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10259 G_("increment of read-only location %qE"),
10260 G_("decrement of read-only location %qE"),
10261 G_("read-only location %qE used as %<asm%> output")),
10262 arg);
10265 /* Print an error message for an invalid lvalue. USE says
10266 how the lvalue is being used and so selects the error message. LOC
10267 is the location for the error. */
10269 void
10270 lvalue_error (location_t loc, enum lvalue_use use)
10272 switch (use)
10274 case lv_assign:
10275 error_at (loc, "lvalue required as left operand of assignment");
10276 break;
10277 case lv_increment:
10278 error_at (loc, "lvalue required as increment operand");
10279 break;
10280 case lv_decrement:
10281 error_at (loc, "lvalue required as decrement operand");
10282 break;
10283 case lv_addressof:
10284 error_at (loc, "lvalue required as unary %<&%> operand");
10285 break;
10286 case lv_asm:
10287 error_at (loc, "lvalue required in asm statement");
10288 break;
10289 default:
10290 gcc_unreachable ();
10294 /* Print an error message for an invalid indirection of type TYPE.
10295 ERRSTRING is the name of the operator for the indirection. */
10297 void
10298 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10300 switch (errstring)
10302 case RO_NULL:
10303 gcc_assert (c_dialect_cxx ());
10304 error_at (loc, "invalid type argument (have %qT)", type);
10305 break;
10306 case RO_ARRAY_INDEXING:
10307 error_at (loc,
10308 "invalid type argument of array indexing (have %qT)",
10309 type);
10310 break;
10311 case RO_UNARY_STAR:
10312 error_at (loc,
10313 "invalid type argument of unary %<*%> (have %qT)",
10314 type);
10315 break;
10316 case RO_ARROW:
10317 error_at (loc,
10318 "invalid type argument of %<->%> (have %qT)",
10319 type);
10320 break;
10321 case RO_ARROW_STAR:
10322 error_at (loc,
10323 "invalid type argument of %<->*%> (have %qT)",
10324 type);
10325 break;
10326 case RO_IMPLICIT_CONVERSION:
10327 error_at (loc,
10328 "invalid type argument of implicit conversion (have %qT)",
10329 type);
10330 break;
10331 default:
10332 gcc_unreachable ();
10336 /* *PTYPE is an incomplete array. Complete it with a domain based on
10337 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10338 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10339 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10342 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10344 tree maxindex, type, main_type, elt, unqual_elt;
10345 int failure = 0, quals;
10346 hashval_t hashcode = 0;
10347 bool overflow_p = false;
10349 maxindex = size_zero_node;
10350 if (initial_value)
10352 if (TREE_CODE (initial_value) == STRING_CST)
10354 int eltsize
10355 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10356 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10358 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10360 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10362 if (vec_safe_is_empty (v))
10364 if (pedantic)
10365 failure = 3;
10366 maxindex = ssize_int (-1);
10368 else
10370 tree curindex;
10371 unsigned HOST_WIDE_INT cnt;
10372 constructor_elt *ce;
10373 bool fold_p = false;
10375 if ((*v)[0].index)
10376 maxindex = (*v)[0].index, fold_p = true;
10378 curindex = maxindex;
10380 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10382 bool curfold_p = false;
10383 if (ce->index)
10384 curindex = ce->index, curfold_p = true;
10385 else
10387 if (fold_p)
10389 /* Since we treat size types now as ordinary
10390 unsigned types, we need an explicit overflow
10391 check. */
10392 tree orig = curindex;
10393 curindex = fold_convert (sizetype, curindex);
10394 overflow_p |= tree_int_cst_lt (curindex, orig);
10396 curindex = size_binop (PLUS_EXPR, curindex,
10397 size_one_node);
10399 if (tree_int_cst_lt (maxindex, curindex))
10400 maxindex = curindex, fold_p = curfold_p;
10402 if (fold_p)
10404 tree orig = maxindex;
10405 maxindex = fold_convert (sizetype, maxindex);
10406 overflow_p |= tree_int_cst_lt (maxindex, orig);
10410 else
10412 /* Make an error message unless that happened already. */
10413 if (initial_value != error_mark_node)
10414 failure = 1;
10417 else
10419 failure = 2;
10420 if (!do_default)
10421 return failure;
10424 type = *ptype;
10425 elt = TREE_TYPE (type);
10426 quals = TYPE_QUALS (strip_array_types (elt));
10427 if (quals == 0)
10428 unqual_elt = elt;
10429 else
10430 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10432 /* Using build_distinct_type_copy and modifying things afterward instead
10433 of using build_array_type to create a new type preserves all of the
10434 TYPE_LANG_FLAG_? bits that the front end may have set. */
10435 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10436 TREE_TYPE (main_type) = unqual_elt;
10437 TYPE_DOMAIN (main_type)
10438 = build_range_type (TREE_TYPE (maxindex),
10439 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10440 layout_type (main_type);
10442 /* Make sure we have the canonical MAIN_TYPE. */
10443 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10444 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10445 hashcode);
10446 main_type = type_hash_canon (hashcode, main_type);
10448 /* Fix the canonical type. */
10449 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10450 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10451 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10452 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10453 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10454 != TYPE_DOMAIN (main_type)))
10455 TYPE_CANONICAL (main_type)
10456 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10457 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10458 else
10459 TYPE_CANONICAL (main_type) = main_type;
10461 if (quals == 0)
10462 type = main_type;
10463 else
10464 type = c_build_qualified_type (main_type, quals);
10466 if (COMPLETE_TYPE_P (type)
10467 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10468 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10470 error ("size of array is too large");
10471 /* If we proceed with the array type as it is, we'll eventually
10472 crash in tree_to_[su]hwi(). */
10473 type = error_mark_node;
10476 *ptype = type;
10477 return failure;
10480 /* Like c_mark_addressable but don't check register qualifier. */
10481 void
10482 c_common_mark_addressable_vec (tree t)
10484 while (handled_component_p (t))
10485 t = TREE_OPERAND (t, 0);
10486 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10487 return;
10488 TREE_ADDRESSABLE (t) = 1;
10493 /* Used to help initialize the builtin-types.def table. When a type of
10494 the correct size doesn't exist, use error_mark_node instead of NULL.
10495 The later results in segfaults even when a decl using the type doesn't
10496 get invoked. */
10498 tree
10499 builtin_type_for_size (int size, bool unsignedp)
10501 tree type = c_common_type_for_size (size, unsignedp);
10502 return type ? type : error_mark_node;
10505 /* A helper function for resolve_overloaded_builtin in resolving the
10506 overloaded __sync_ builtins. Returns a positive power of 2 if the
10507 first operand of PARAMS is a pointer to a supported data type.
10508 Returns 0 if an error is encountered. */
10510 static int
10511 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10513 tree type;
10514 int size;
10516 if (!params)
10518 error ("too few arguments to function %qE", function);
10519 return 0;
10522 type = TREE_TYPE ((*params)[0]);
10523 if (TREE_CODE (type) == ARRAY_TYPE)
10525 /* Force array-to-pointer decay for C++. */
10526 gcc_assert (c_dialect_cxx());
10527 (*params)[0] = default_conversion ((*params)[0]);
10528 type = TREE_TYPE ((*params)[0]);
10530 if (TREE_CODE (type) != POINTER_TYPE)
10531 goto incompatible;
10533 type = TREE_TYPE (type);
10534 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10535 goto incompatible;
10537 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10538 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10539 return size;
10541 incompatible:
10542 error ("incompatible type for argument %d of %qE", 1, function);
10543 return 0;
10546 /* A helper function for resolve_overloaded_builtin. Adds casts to
10547 PARAMS to make arguments match up with those of FUNCTION. Drops
10548 the variadic arguments at the end. Returns false if some error
10549 was encountered; true on success. */
10551 static bool
10552 sync_resolve_params (location_t loc, tree orig_function, tree function,
10553 vec<tree, va_gc> *params, bool orig_format)
10555 function_args_iterator iter;
10556 tree ptype;
10557 unsigned int parmnum;
10559 function_args_iter_init (&iter, TREE_TYPE (function));
10560 /* We've declared the implementation functions to use "volatile void *"
10561 as the pointer parameter, so we shouldn't get any complaints from the
10562 call to check_function_arguments what ever type the user used. */
10563 function_args_iter_next (&iter);
10564 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10565 ptype = TYPE_MAIN_VARIANT (ptype);
10567 /* For the rest of the values, we need to cast these to FTYPE, so that we
10568 don't get warnings for passing pointer types, etc. */
10569 parmnum = 0;
10570 while (1)
10572 tree val, arg_type;
10574 arg_type = function_args_iter_cond (&iter);
10575 /* XXX void_type_node belies the abstraction. */
10576 if (arg_type == void_type_node)
10577 break;
10579 ++parmnum;
10580 if (params->length () <= parmnum)
10582 error_at (loc, "too few arguments to function %qE", orig_function);
10583 return false;
10586 /* Only convert parameters if arg_type is unsigned integer type with
10587 new format sync routines, i.e. don't attempt to convert pointer
10588 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10589 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10590 kinds). */
10591 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10593 /* Ideally for the first conversion we'd use convert_for_assignment
10594 so that we get warnings for anything that doesn't match the pointer
10595 type. This isn't portable across the C and C++ front ends atm. */
10596 val = (*params)[parmnum];
10597 val = convert (ptype, val);
10598 val = convert (arg_type, val);
10599 (*params)[parmnum] = val;
10602 function_args_iter_next (&iter);
10605 /* __atomic routines are not variadic. */
10606 if (!orig_format && params->length () != parmnum + 1)
10608 error_at (loc, "too many arguments to function %qE", orig_function);
10609 return false;
10612 /* The definition of these primitives is variadic, with the remaining
10613 being "an optional list of variables protected by the memory barrier".
10614 No clue what that's supposed to mean, precisely, but we consider all
10615 call-clobbered variables to be protected so we're safe. */
10616 params->truncate (parmnum + 1);
10618 return true;
10621 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10622 RESULT to make it match the type of the first pointer argument in
10623 PARAMS. */
10625 static tree
10626 sync_resolve_return (tree first_param, tree result, bool orig_format)
10628 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10629 tree rtype = TREE_TYPE (result);
10630 ptype = TYPE_MAIN_VARIANT (ptype);
10632 /* New format doesn't require casting unless the types are the same size. */
10633 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10634 return convert (ptype, result);
10635 else
10636 return result;
10639 /* This function verifies the PARAMS to generic atomic FUNCTION.
10640 It returns the size if all the parameters are the same size, otherwise
10641 0 is returned if the parameters are invalid. */
10643 static int
10644 get_atomic_generic_size (location_t loc, tree function,
10645 vec<tree, va_gc> *params)
10647 unsigned int n_param;
10648 unsigned int n_model;
10649 unsigned int x;
10650 int size_0;
10651 tree type_0;
10653 /* Determine the parameter makeup. */
10654 switch (DECL_FUNCTION_CODE (function))
10656 case BUILT_IN_ATOMIC_EXCHANGE:
10657 n_param = 4;
10658 n_model = 1;
10659 break;
10660 case BUILT_IN_ATOMIC_LOAD:
10661 case BUILT_IN_ATOMIC_STORE:
10662 n_param = 3;
10663 n_model = 1;
10664 break;
10665 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10666 n_param = 6;
10667 n_model = 2;
10668 break;
10669 default:
10670 gcc_unreachable ();
10673 if (vec_safe_length (params) != n_param)
10675 error_at (loc, "incorrect number of arguments to function %qE", function);
10676 return 0;
10679 /* Get type of first parameter, and determine its size. */
10680 type_0 = TREE_TYPE ((*params)[0]);
10681 if (TREE_CODE (type_0) == ARRAY_TYPE)
10683 /* Force array-to-pointer decay for C++. */
10684 gcc_assert (c_dialect_cxx());
10685 (*params)[0] = default_conversion ((*params)[0]);
10686 type_0 = TREE_TYPE ((*params)[0]);
10688 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10690 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10691 function);
10692 return 0;
10695 /* Types must be compile time constant sizes. */
10696 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10698 error_at (loc,
10699 "argument 1 of %qE must be a pointer to a constant size type",
10700 function);
10701 return 0;
10704 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10706 /* Zero size objects are not allowed. */
10707 if (size_0 == 0)
10709 error_at (loc,
10710 "argument 1 of %qE must be a pointer to a nonzero size object",
10711 function);
10712 return 0;
10715 /* Check each other parameter is a pointer and the same size. */
10716 for (x = 0; x < n_param - n_model; x++)
10718 int size;
10719 tree type = TREE_TYPE ((*params)[x]);
10720 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10721 if (n_param == 6 && x == 3)
10722 continue;
10723 if (!POINTER_TYPE_P (type))
10725 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10726 function);
10727 return 0;
10729 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10730 size = type_size ? tree_to_uhwi (type_size) : 0;
10731 if (size != size_0)
10733 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10734 function);
10735 return 0;
10739 /* Check memory model parameters for validity. */
10740 for (x = n_param - n_model ; x < n_param; x++)
10742 tree p = (*params)[x];
10743 if (TREE_CODE (p) == INTEGER_CST)
10745 int i = tree_to_uhwi (p);
10746 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10748 warning_at (loc, OPT_Winvalid_memory_model,
10749 "invalid memory model argument %d of %qE", x + 1,
10750 function);
10753 else
10754 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10756 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10757 function);
10758 return 0;
10762 return size_0;
10766 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10767 at the beginning of the parameter list PARAMS representing the size of the
10768 objects. This is to match the library ABI requirement. LOC is the location
10769 of the function call.
10770 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10771 returned to allow the external call to be constructed. */
10773 static tree
10774 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10775 vec<tree, va_gc> *params)
10777 tree size_node;
10779 /* Insert a SIZE_T parameter as the first param. If there isn't
10780 enough space, allocate a new vector and recursively re-build with that. */
10781 if (!params->space (1))
10783 unsigned int z, len;
10784 vec<tree, va_gc> *v;
10785 tree f;
10787 len = params->length ();
10788 vec_alloc (v, len + 1);
10789 v->quick_push (build_int_cst (size_type_node, n));
10790 for (z = 0; z < len; z++)
10791 v->quick_push ((*params)[z]);
10792 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10793 vec_free (v);
10794 return f;
10797 /* Add the size parameter and leave as a function call for processing. */
10798 size_node = build_int_cst (size_type_node, n);
10799 params->quick_insert (0, size_node);
10800 return NULL_TREE;
10804 /* Return whether atomic operations for naturally aligned N-byte
10805 arguments are supported, whether inline or through libatomic. */
10806 static bool
10807 atomic_size_supported_p (int n)
10809 switch (n)
10811 case 1:
10812 case 2:
10813 case 4:
10814 case 8:
10815 return true;
10817 case 16:
10818 return targetm.scalar_mode_supported_p (TImode);
10820 default:
10821 return false;
10825 /* This will process an __atomic_exchange function call, determine whether it
10826 needs to be mapped to the _N variation, or turned into a library call.
10827 LOC is the location of the builtin call.
10828 FUNCTION is the DECL that has been invoked;
10829 PARAMS is the argument list for the call. The return value is non-null
10830 TRUE is returned if it is translated into the proper format for a call to the
10831 external library, and NEW_RETURN is set the tree for that function.
10832 FALSE is returned if processing for the _N variation is required, and
10833 NEW_RETURN is set to the the return value the result is copied into. */
10834 static bool
10835 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10836 vec<tree, va_gc> *params, tree *new_return)
10838 tree p0, p1, p2, p3;
10839 tree I_type, I_type_ptr;
10840 int n = get_atomic_generic_size (loc, function, params);
10842 /* Size of 0 is an error condition. */
10843 if (n == 0)
10845 *new_return = error_mark_node;
10846 return true;
10849 /* If not a lock-free size, change to the library generic format. */
10850 if (!atomic_size_supported_p (n))
10852 *new_return = add_atomic_size_parameter (n, loc, function, params);
10853 return true;
10856 /* Otherwise there is a lockfree match, transform the call from:
10857 void fn(T* mem, T* desired, T* return, model)
10858 into
10859 *return = (T) (fn (In* mem, (In) *desired, model)) */
10861 p0 = (*params)[0];
10862 p1 = (*params)[1];
10863 p2 = (*params)[2];
10864 p3 = (*params)[3];
10866 /* Create pointer to appropriate size. */
10867 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10868 I_type_ptr = build_pointer_type (I_type);
10870 /* Convert object pointer to required type. */
10871 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10872 (*params)[0] = p0;
10873 /* Convert new value to required type, and dereference it. */
10874 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10875 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10876 (*params)[1] = p1;
10878 /* Move memory model to the 3rd position, and end param list. */
10879 (*params)[2] = p3;
10880 params->truncate (3);
10882 /* Convert return pointer and dereference it for later assignment. */
10883 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10885 return false;
10889 /* This will process an __atomic_compare_exchange function call, determine
10890 whether it needs to be mapped to the _N variation, or turned into a lib call.
10891 LOC is the location of the builtin call.
10892 FUNCTION is the DECL that has been invoked;
10893 PARAMS is the argument list for the call. The return value is non-null
10894 TRUE is returned if it is translated into the proper format for a call to the
10895 external library, and NEW_RETURN is set the tree for that function.
10896 FALSE is returned if processing for the _N variation is required. */
10898 static bool
10899 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10900 vec<tree, va_gc> *params,
10901 tree *new_return)
10903 tree p0, p1, p2;
10904 tree I_type, I_type_ptr;
10905 int n = get_atomic_generic_size (loc, function, params);
10907 /* Size of 0 is an error condition. */
10908 if (n == 0)
10910 *new_return = error_mark_node;
10911 return true;
10914 /* If not a lock-free size, change to the library generic format. */
10915 if (!atomic_size_supported_p (n))
10917 /* The library generic format does not have the weak parameter, so
10918 remove it from the param list. Since a parameter has been removed,
10919 we can be sure that there is room for the SIZE_T parameter, meaning
10920 there will not be a recursive rebuilding of the parameter list, so
10921 there is no danger this will be done twice. */
10922 if (n > 0)
10924 (*params)[3] = (*params)[4];
10925 (*params)[4] = (*params)[5];
10926 params->truncate (5);
10928 *new_return = add_atomic_size_parameter (n, loc, function, params);
10929 return true;
10932 /* Otherwise, there is a match, so the call needs to be transformed from:
10933 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10934 into
10935 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10937 p0 = (*params)[0];
10938 p1 = (*params)[1];
10939 p2 = (*params)[2];
10941 /* Create pointer to appropriate size. */
10942 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10943 I_type_ptr = build_pointer_type (I_type);
10945 /* Convert object pointer to required type. */
10946 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10947 (*params)[0] = p0;
10949 /* Convert expected pointer to required type. */
10950 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10951 (*params)[1] = p1;
10953 /* Convert desired value to required type, and dereference it. */
10954 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10955 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10956 (*params)[2] = p2;
10958 /* The rest of the parameters are fine. NULL means no special return value
10959 processing.*/
10960 *new_return = NULL;
10961 return false;
10965 /* This will process an __atomic_load function call, determine whether it
10966 needs to be mapped to the _N variation, or turned into a library call.
10967 LOC is the location of the builtin call.
10968 FUNCTION is the DECL that has been invoked;
10969 PARAMS is the argument list for the call. The return value is non-null
10970 TRUE is returned if it is translated into the proper format for a call to the
10971 external library, and NEW_RETURN is set the tree for that function.
10972 FALSE is returned if processing for the _N variation is required, and
10973 NEW_RETURN is set to the the return value the result is copied into. */
10975 static bool
10976 resolve_overloaded_atomic_load (location_t loc, tree function,
10977 vec<tree, va_gc> *params, tree *new_return)
10979 tree p0, p1, p2;
10980 tree I_type, I_type_ptr;
10981 int n = get_atomic_generic_size (loc, function, params);
10983 /* Size of 0 is an error condition. */
10984 if (n == 0)
10986 *new_return = error_mark_node;
10987 return true;
10990 /* If not a lock-free size, change to the library generic format. */
10991 if (!atomic_size_supported_p (n))
10993 *new_return = add_atomic_size_parameter (n, loc, function, params);
10994 return true;
10997 /* Otherwise, there is a match, so the call needs to be transformed from:
10998 void fn(T* mem, T* return, model)
10999 into
11000 *return = (T) (fn ((In *) mem, model)) */
11002 p0 = (*params)[0];
11003 p1 = (*params)[1];
11004 p2 = (*params)[2];
11006 /* Create pointer to appropriate size. */
11007 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11008 I_type_ptr = build_pointer_type (I_type);
11010 /* Convert object pointer to required type. */
11011 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11012 (*params)[0] = p0;
11014 /* Move memory model to the 2nd position, and end param list. */
11015 (*params)[1] = p2;
11016 params->truncate (2);
11018 /* Convert return pointer and dereference it for later assignment. */
11019 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11021 return false;
11025 /* This will process an __atomic_store function call, determine whether it
11026 needs to be mapped to the _N variation, or turned into a library call.
11027 LOC is the location of the builtin call.
11028 FUNCTION is the DECL that has been invoked;
11029 PARAMS is the argument list for the call. The return value is non-null
11030 TRUE is returned if it is translated into the proper format for a call to the
11031 external library, and NEW_RETURN is set the tree for that function.
11032 FALSE is returned if processing for the _N variation is required, and
11033 NEW_RETURN is set to the the return value the result is copied into. */
11035 static bool
11036 resolve_overloaded_atomic_store (location_t loc, tree function,
11037 vec<tree, va_gc> *params, tree *new_return)
11039 tree p0, p1;
11040 tree I_type, I_type_ptr;
11041 int n = get_atomic_generic_size (loc, function, params);
11043 /* Size of 0 is an error condition. */
11044 if (n == 0)
11046 *new_return = error_mark_node;
11047 return true;
11050 /* If not a lock-free size, change to the library generic format. */
11051 if (!atomic_size_supported_p (n))
11053 *new_return = add_atomic_size_parameter (n, loc, function, params);
11054 return true;
11057 /* Otherwise, there is a match, so the call needs to be transformed from:
11058 void fn(T* mem, T* value, model)
11059 into
11060 fn ((In *) mem, (In) *value, model) */
11062 p0 = (*params)[0];
11063 p1 = (*params)[1];
11065 /* Create pointer to appropriate size. */
11066 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11067 I_type_ptr = build_pointer_type (I_type);
11069 /* Convert object pointer to required type. */
11070 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11071 (*params)[0] = p0;
11073 /* Convert new value to required type, and dereference it. */
11074 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11075 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11076 (*params)[1] = p1;
11078 /* The memory model is in the right spot already. Return is void. */
11079 *new_return = NULL_TREE;
11081 return false;
11085 /* Some builtin functions are placeholders for other expressions. This
11086 function should be called immediately after parsing the call expression
11087 before surrounding code has committed to the type of the expression.
11089 LOC is the location of the builtin call.
11091 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11092 PARAMS is the argument list for the call. The return value is non-null
11093 when expansion is complete, and null if normal processing should
11094 continue. */
11096 tree
11097 resolve_overloaded_builtin (location_t loc, tree function,
11098 vec<tree, va_gc> *params)
11100 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11101 bool orig_format = true;
11102 tree new_return = NULL_TREE;
11104 switch (DECL_BUILT_IN_CLASS (function))
11106 case BUILT_IN_NORMAL:
11107 break;
11108 case BUILT_IN_MD:
11109 if (targetm.resolve_overloaded_builtin)
11110 return targetm.resolve_overloaded_builtin (loc, function, params);
11111 else
11112 return NULL_TREE;
11113 default:
11114 return NULL_TREE;
11117 /* Handle BUILT_IN_NORMAL here. */
11118 switch (orig_code)
11120 case BUILT_IN_ATOMIC_EXCHANGE:
11121 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11122 case BUILT_IN_ATOMIC_LOAD:
11123 case BUILT_IN_ATOMIC_STORE:
11125 /* Handle these 4 together so that they can fall through to the next
11126 case if the call is transformed to an _N variant. */
11127 switch (orig_code)
11129 case BUILT_IN_ATOMIC_EXCHANGE:
11131 if (resolve_overloaded_atomic_exchange (loc, function, params,
11132 &new_return))
11133 return new_return;
11134 /* Change to the _N variant. */
11135 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11136 break;
11139 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11141 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11142 params,
11143 &new_return))
11144 return new_return;
11145 /* Change to the _N variant. */
11146 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11147 break;
11149 case BUILT_IN_ATOMIC_LOAD:
11151 if (resolve_overloaded_atomic_load (loc, function, params,
11152 &new_return))
11153 return new_return;
11154 /* Change to the _N variant. */
11155 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11156 break;
11158 case BUILT_IN_ATOMIC_STORE:
11160 if (resolve_overloaded_atomic_store (loc, function, params,
11161 &new_return))
11162 return new_return;
11163 /* Change to the _N variant. */
11164 orig_code = BUILT_IN_ATOMIC_STORE_N;
11165 break;
11167 default:
11168 gcc_unreachable ();
11170 /* Fallthrough to the normal processing. */
11172 case BUILT_IN_ATOMIC_EXCHANGE_N:
11173 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11174 case BUILT_IN_ATOMIC_LOAD_N:
11175 case BUILT_IN_ATOMIC_STORE_N:
11176 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11177 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11178 case BUILT_IN_ATOMIC_AND_FETCH_N:
11179 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11180 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11181 case BUILT_IN_ATOMIC_OR_FETCH_N:
11182 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11183 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11184 case BUILT_IN_ATOMIC_FETCH_AND_N:
11185 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11186 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11187 case BUILT_IN_ATOMIC_FETCH_OR_N:
11189 orig_format = false;
11190 /* Fallthru for parameter processing. */
11192 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11193 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11194 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11195 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11196 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11197 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11198 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11199 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11200 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11201 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11202 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11203 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11204 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11205 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11206 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11207 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11209 int n = sync_resolve_size (function, params);
11210 tree new_function, first_param, result;
11211 enum built_in_function fncode;
11213 if (n == 0)
11214 return error_mark_node;
11216 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11217 new_function = builtin_decl_explicit (fncode);
11218 if (!sync_resolve_params (loc, function, new_function, params,
11219 orig_format))
11220 return error_mark_node;
11222 first_param = (*params)[0];
11223 result = build_function_call_vec (loc, vNULL, new_function, params,
11224 NULL);
11225 if (result == error_mark_node)
11226 return result;
11227 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11228 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11229 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11230 result = sync_resolve_return (first_param, result, orig_format);
11232 /* If new_return is set, assign function to that expr and cast the
11233 result to void since the generic interface returned void. */
11234 if (new_return)
11236 /* Cast function result from I{1,2,4,8,16} to the required type. */
11237 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11238 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11239 result);
11240 TREE_SIDE_EFFECTS (result) = 1;
11241 protected_set_expr_location (result, loc);
11242 result = convert (void_type_node, result);
11244 return result;
11247 default:
11248 return NULL_TREE;
11252 /* vector_types_compatible_elements_p is used in type checks of vectors
11253 values used as operands of binary operators. Where it returns true, and
11254 the other checks of the caller succeed (being vector types in he first
11255 place, and matching number of elements), we can just treat the types
11256 as essentially the same.
11257 Contrast with vector_targets_convertible_p, which is used for vector
11258 pointer types, and vector_types_convertible_p, which will allow
11259 language-specific matches under the control of flag_lax_vector_conversions,
11260 and might still require a conversion. */
11261 /* True if vector types T1 and T2 can be inputs to the same binary
11262 operator without conversion.
11263 We don't check the overall vector size here because some of our callers
11264 want to give different error messages when the vectors are compatible
11265 except for the element count. */
11267 bool
11268 vector_types_compatible_elements_p (tree t1, tree t2)
11270 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11271 t1 = TREE_TYPE (t1);
11272 t2 = TREE_TYPE (t2);
11274 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11276 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11277 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11278 || c2 == FIXED_POINT_TYPE));
11280 t1 = c_common_signed_type (t1);
11281 t2 = c_common_signed_type (t2);
11282 /* Equality works here because c_common_signed_type uses
11283 TYPE_MAIN_VARIANT. */
11284 if (t1 == t2)
11285 return true;
11286 if (opaque && c1 == c2
11287 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11288 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11289 return true;
11290 return false;
11293 /* Check for missing format attributes on function pointers. LTYPE is
11294 the new type or left-hand side type. RTYPE is the old type or
11295 right-hand side type. Returns TRUE if LTYPE is missing the desired
11296 attribute. */
11298 bool
11299 check_missing_format_attribute (tree ltype, tree rtype)
11301 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11302 tree ra;
11304 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11305 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11306 break;
11307 if (ra)
11309 tree la;
11310 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11311 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11312 break;
11313 return !la;
11315 else
11316 return false;
11319 /* Subscripting with type char is likely to lose on a machine where
11320 chars are signed. So warn on any machine, but optionally. Don't
11321 warn for unsigned char since that type is safe. Don't warn for
11322 signed char because anyone who uses that must have done so
11323 deliberately. Furthermore, we reduce the false positive load by
11324 warning only for non-constant value of type char. */
11326 void
11327 warn_array_subscript_with_type_char (location_t loc, tree index)
11329 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11330 && TREE_CODE (index) != INTEGER_CST)
11331 warning_at (loc, OPT_Wchar_subscripts,
11332 "array subscript has type %<char%>");
11335 /* Implement -Wparentheses for the unexpected C precedence rules, to
11336 cover cases like x + y << z which readers are likely to
11337 misinterpret. We have seen an expression in which CODE is a binary
11338 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11339 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11340 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11341 expression was not formed using a binary or unary operator, or it
11342 was enclosed in parentheses. */
11344 void
11345 warn_about_parentheses (location_t loc, enum tree_code code,
11346 enum tree_code code_left, tree arg_left,
11347 enum tree_code code_right, tree arg_right)
11349 if (!warn_parentheses)
11350 return;
11352 /* This macro tests that the expression ARG with original tree code
11353 CODE appears to be a boolean expression. or the result of folding a
11354 boolean expression. */
11355 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11356 (truth_value_p (TREE_CODE (ARG)) \
11357 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11358 /* Folding may create 0 or 1 integers from other expressions. */ \
11359 || ((CODE) != INTEGER_CST \
11360 && (integer_onep (ARG) || integer_zerop (ARG))))
11362 switch (code)
11364 case LSHIFT_EXPR:
11365 if (code_left == PLUS_EXPR)
11366 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11367 "suggest parentheses around %<+%> inside %<<<%>");
11368 else if (code_right == PLUS_EXPR)
11369 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11370 "suggest parentheses around %<+%> inside %<<<%>");
11371 else if (code_left == MINUS_EXPR)
11372 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11373 "suggest parentheses around %<-%> inside %<<<%>");
11374 else if (code_right == MINUS_EXPR)
11375 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11376 "suggest parentheses around %<-%> inside %<<<%>");
11377 return;
11379 case RSHIFT_EXPR:
11380 if (code_left == PLUS_EXPR)
11381 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11382 "suggest parentheses around %<+%> inside %<>>%>");
11383 else if (code_right == PLUS_EXPR)
11384 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11385 "suggest parentheses around %<+%> inside %<>>%>");
11386 else if (code_left == MINUS_EXPR)
11387 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11388 "suggest parentheses around %<-%> inside %<>>%>");
11389 else if (code_right == MINUS_EXPR)
11390 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11391 "suggest parentheses around %<-%> inside %<>>%>");
11392 return;
11394 case TRUTH_ORIF_EXPR:
11395 if (code_left == TRUTH_ANDIF_EXPR)
11396 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11397 "suggest parentheses around %<&&%> within %<||%>");
11398 else if (code_right == TRUTH_ANDIF_EXPR)
11399 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11400 "suggest parentheses around %<&&%> within %<||%>");
11401 return;
11403 case BIT_IOR_EXPR:
11404 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11405 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11406 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11407 "suggest parentheses around arithmetic in operand of %<|%>");
11408 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11409 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11410 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11411 "suggest parentheses around arithmetic in operand of %<|%>");
11412 /* Check cases like x|y==z */
11413 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11414 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11415 "suggest parentheses around comparison in operand of %<|%>");
11416 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11417 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11418 "suggest parentheses around comparison in operand of %<|%>");
11419 /* Check cases like !x | y */
11420 else if (code_left == TRUTH_NOT_EXPR
11421 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11422 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11423 "suggest parentheses around operand of "
11424 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11425 return;
11427 case BIT_XOR_EXPR:
11428 if (code_left == BIT_AND_EXPR
11429 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11430 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11431 "suggest parentheses around arithmetic in operand of %<^%>");
11432 else if (code_right == BIT_AND_EXPR
11433 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11434 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11435 "suggest parentheses around arithmetic in operand of %<^%>");
11436 /* Check cases like x^y==z */
11437 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11438 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11439 "suggest parentheses around comparison in operand of %<^%>");
11440 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11441 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11442 "suggest parentheses around comparison in operand of %<^%>");
11443 return;
11445 case BIT_AND_EXPR:
11446 if (code_left == PLUS_EXPR)
11447 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11448 "suggest parentheses around %<+%> in operand of %<&%>");
11449 else if (code_right == PLUS_EXPR)
11450 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11451 "suggest parentheses around %<+%> in operand of %<&%>");
11452 else if (code_left == MINUS_EXPR)
11453 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11454 "suggest parentheses around %<-%> in operand of %<&%>");
11455 else if (code_right == MINUS_EXPR)
11456 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11457 "suggest parentheses around %<-%> in operand of %<&%>");
11458 /* Check cases like x&y==z */
11459 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11460 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11461 "suggest parentheses around comparison in operand of %<&%>");
11462 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11463 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11464 "suggest parentheses around comparison in operand of %<&%>");
11465 /* Check cases like !x & y */
11466 else if (code_left == TRUTH_NOT_EXPR
11467 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11468 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11469 "suggest parentheses around operand of "
11470 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11471 return;
11473 case EQ_EXPR:
11474 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11475 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11476 "suggest parentheses around comparison in operand of %<==%>");
11477 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11478 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11479 "suggest parentheses around comparison in operand of %<==%>");
11480 return;
11481 case NE_EXPR:
11482 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11483 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11484 "suggest parentheses around comparison in operand of %<!=%>");
11485 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11486 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11487 "suggest parentheses around comparison in operand of %<!=%>");
11488 return;
11490 default:
11491 if (TREE_CODE_CLASS (code) == tcc_comparison)
11493 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11494 && code_left != NE_EXPR && code_left != EQ_EXPR
11495 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11496 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11497 "comparisons like %<X<=Y<=Z%> do not "
11498 "have their mathematical meaning");
11499 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11500 && code_right != NE_EXPR && code_right != EQ_EXPR
11501 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11502 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11503 "comparisons like %<X<=Y<=Z%> do not "
11504 "have their mathematical meaning");
11506 return;
11508 #undef NOT_A_BOOLEAN_EXPR_P
11511 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11513 void
11514 warn_for_unused_label (tree label)
11516 if (!TREE_USED (label))
11518 if (DECL_INITIAL (label))
11519 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11520 else
11521 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11525 /* Warn for division by zero according to the value of DIVISOR. LOC
11526 is the location of the division operator. */
11528 void
11529 warn_for_div_by_zero (location_t loc, tree divisor)
11531 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11532 about division by zero. Do not issue a warning if DIVISOR has a
11533 floating-point type, since we consider 0.0/0.0 a valid way of
11534 generating a NaN. */
11535 if (c_inhibit_evaluation_warnings == 0
11536 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11537 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11540 /* Subroutine of build_binary_op. Give warnings for comparisons
11541 between signed and unsigned quantities that may fail. Do the
11542 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11543 so that casts will be considered, but default promotions won't
11546 LOCATION is the location of the comparison operator.
11548 The arguments of this function map directly to local variables
11549 of build_binary_op. */
11551 void
11552 warn_for_sign_compare (location_t location,
11553 tree orig_op0, tree orig_op1,
11554 tree op0, tree op1,
11555 tree result_type, enum tree_code resultcode)
11557 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11558 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11559 int unsignedp0, unsignedp1;
11561 /* In C++, check for comparison of different enum types. */
11562 if (c_dialect_cxx()
11563 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11564 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11565 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11566 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11568 warning_at (location,
11569 OPT_Wsign_compare, "comparison between types %qT and %qT",
11570 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11573 /* Do not warn if the comparison is being done in a signed type,
11574 since the signed type will only be chosen if it can represent
11575 all the values of the unsigned type. */
11576 if (!TYPE_UNSIGNED (result_type))
11577 /* OK */;
11578 /* Do not warn if both operands are unsigned. */
11579 else if (op0_signed == op1_signed)
11580 /* OK */;
11581 else
11583 tree sop, uop, base_type;
11584 bool ovf;
11586 if (op0_signed)
11587 sop = orig_op0, uop = orig_op1;
11588 else
11589 sop = orig_op1, uop = orig_op0;
11591 STRIP_TYPE_NOPS (sop);
11592 STRIP_TYPE_NOPS (uop);
11593 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11594 ? TREE_TYPE (result_type) : result_type);
11596 /* Do not warn if the signed quantity is an unsuffixed integer
11597 literal (or some static constant expression involving such
11598 literals or a conditional expression involving such literals)
11599 and it is non-negative. */
11600 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11601 /* OK */;
11602 /* Do not warn if the comparison is an equality operation, the
11603 unsigned quantity is an integral constant, and it would fit
11604 in the result if the result were signed. */
11605 else if (TREE_CODE (uop) == INTEGER_CST
11606 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11607 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11608 /* OK */;
11609 /* In C, do not warn if the unsigned quantity is an enumeration
11610 constant and its maximum value would fit in the result if the
11611 result were signed. */
11612 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11613 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11614 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11615 c_common_signed_type (base_type)))
11616 /* OK */;
11617 else
11618 warning_at (location,
11619 OPT_Wsign_compare,
11620 "comparison between signed and unsigned integer expressions");
11623 /* Warn if two unsigned values are being compared in a size larger
11624 than their original size, and one (and only one) is the result of
11625 a `~' operator. This comparison will always fail.
11627 Also warn if one operand is a constant, and the constant does not
11628 have all bits set that are set in the ~ operand when it is
11629 extended. */
11631 op0 = c_common_get_narrower (op0, &unsignedp0);
11632 op1 = c_common_get_narrower (op1, &unsignedp1);
11634 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11635 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11637 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11638 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11639 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11640 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11642 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11644 tree primop;
11645 HOST_WIDE_INT constant, mask;
11646 int unsignedp;
11647 unsigned int bits;
11649 if (tree_fits_shwi_p (op0))
11651 primop = op1;
11652 unsignedp = unsignedp1;
11653 constant = tree_to_shwi (op0);
11655 else
11657 primop = op0;
11658 unsignedp = unsignedp0;
11659 constant = tree_to_shwi (op1);
11662 bits = TYPE_PRECISION (TREE_TYPE (primop));
11663 if (bits < TYPE_PRECISION (result_type)
11664 && bits < HOST_BITS_PER_LONG && unsignedp)
11666 mask = (~ (HOST_WIDE_INT) 0) << bits;
11667 if ((mask & constant) != mask)
11669 if (constant == 0)
11670 warning_at (location, OPT_Wsign_compare,
11671 "promoted ~unsigned is always non-zero");
11672 else
11673 warning_at (location, OPT_Wsign_compare,
11674 "comparison of promoted ~unsigned with constant");
11678 else if (unsignedp0 && unsignedp1
11679 && (TYPE_PRECISION (TREE_TYPE (op0))
11680 < TYPE_PRECISION (result_type))
11681 && (TYPE_PRECISION (TREE_TYPE (op1))
11682 < TYPE_PRECISION (result_type)))
11683 warning_at (location, OPT_Wsign_compare,
11684 "comparison of promoted ~unsigned with unsigned");
11688 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11689 type via c_common_type. If -Wdouble-promotion is in use, and the
11690 conditions for warning have been met, issue a warning. GMSGID is
11691 the warning message. It must have two %T specifiers for the type
11692 that was converted (generally "float") and the type to which it was
11693 converted (generally "double), respectively. LOC is the location
11694 to which the awrning should refer. */
11696 void
11697 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11698 const char *gmsgid, location_t loc)
11700 tree source_type;
11702 if (!warn_double_promotion)
11703 return;
11704 /* If the conversion will not occur at run-time, there is no need to
11705 warn about it. */
11706 if (c_inhibit_evaluation_warnings)
11707 return;
11708 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11709 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11710 return;
11711 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11712 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11713 source_type = type1;
11714 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11715 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11716 source_type = type2;
11717 else
11718 return;
11719 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11722 /* Setup a TYPE_DECL node as a typedef representation.
11724 X is a TYPE_DECL for a typedef statement. Create a brand new
11725 ..._TYPE node (which will be just a variant of the existing
11726 ..._TYPE node with identical properties) and then install X
11727 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11729 The whole point here is to end up with a situation where each
11730 and every ..._TYPE node the compiler creates will be uniquely
11731 associated with AT MOST one node representing a typedef name.
11732 This way, even though the compiler substitutes corresponding
11733 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11734 early on, later parts of the compiler can always do the reverse
11735 translation and get back the corresponding typedef name. For
11736 example, given:
11738 typedef struct S MY_TYPE;
11739 MY_TYPE object;
11741 Later parts of the compiler might only know that `object' was of
11742 type `struct S' if it were not for code just below. With this
11743 code however, later parts of the compiler see something like:
11745 struct S' == struct S
11746 typedef struct S' MY_TYPE;
11747 struct S' object;
11749 And they can then deduce (from the node for type struct S') that
11750 the original object declaration was:
11752 MY_TYPE object;
11754 Being able to do this is important for proper support of protoize,
11755 and also for generating precise symbolic debugging information
11756 which takes full account of the programmer's (typedef) vocabulary.
11758 Obviously, we don't want to generate a duplicate ..._TYPE node if
11759 the TYPE_DECL node that we are now processing really represents a
11760 standard built-in type. */
11762 void
11763 set_underlying_type (tree x)
11765 if (x == error_mark_node)
11766 return;
11767 if (DECL_IS_BUILTIN (x))
11769 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11770 TYPE_NAME (TREE_TYPE (x)) = x;
11772 else if (TREE_TYPE (x) != error_mark_node
11773 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11775 tree tt = TREE_TYPE (x);
11776 DECL_ORIGINAL_TYPE (x) = tt;
11777 tt = build_variant_type_copy (tt);
11778 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11779 TYPE_NAME (tt) = x;
11780 TREE_USED (tt) = TREE_USED (x);
11781 TREE_TYPE (x) = tt;
11785 /* Record the types used by the current global variable declaration
11786 being parsed, so that we can decide later to emit their debug info.
11787 Those types are in types_used_by_cur_var_decl, and we are going to
11788 store them in the types_used_by_vars_hash hash table.
11789 DECL is the declaration of the global variable that has been parsed. */
11791 void
11792 record_types_used_by_current_var_decl (tree decl)
11794 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11796 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11798 tree type = types_used_by_cur_var_decl->pop ();
11799 types_used_by_var_decl_insert (type, decl);
11803 /* If DECL is a typedef that is declared in the current function,
11804 record it for the purpose of -Wunused-local-typedefs. */
11806 void
11807 record_locally_defined_typedef (tree decl)
11809 struct c_language_function *l;
11811 if (!warn_unused_local_typedefs
11812 || cfun == NULL
11813 /* if this is not a locally defined typedef then we are not
11814 interested. */
11815 || !is_typedef_decl (decl)
11816 || !decl_function_context (decl))
11817 return;
11819 l = (struct c_language_function *) cfun->language;
11820 vec_safe_push (l->local_typedefs, decl);
11823 /* If T is a TYPE_DECL declared locally, mark it as used. */
11825 void
11826 maybe_record_typedef_use (tree t)
11828 if (!is_typedef_decl (t))
11829 return;
11831 TREE_USED (t) = true;
11834 /* Warn if there are some unused locally defined typedefs in the
11835 current function. */
11837 void
11838 maybe_warn_unused_local_typedefs (void)
11840 int i;
11841 tree decl;
11842 /* The number of times we have emitted -Wunused-local-typedefs
11843 warnings. If this is different from errorcount, that means some
11844 unrelated errors have been issued. In which case, we'll avoid
11845 emitting "unused-local-typedefs" warnings. */
11846 static int unused_local_typedefs_warn_count;
11847 struct c_language_function *l;
11849 if (cfun == NULL)
11850 return;
11852 if ((l = (struct c_language_function *) cfun->language) == NULL)
11853 return;
11855 if (warn_unused_local_typedefs
11856 && errorcount == unused_local_typedefs_warn_count)
11858 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11859 if (!TREE_USED (decl))
11860 warning_at (DECL_SOURCE_LOCATION (decl),
11861 OPT_Wunused_local_typedefs,
11862 "typedef %qD locally defined but not used", decl);
11863 unused_local_typedefs_warn_count = errorcount;
11866 vec_free (l->local_typedefs);
11869 /* Warn about boolean expression compared with an integer value different
11870 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11871 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11872 are the operands of the comparison. The caller must ensure that
11873 either operand is a boolean expression. */
11875 void
11876 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11877 tree op1)
11879 if (TREE_CODE_CLASS (code) != tcc_comparison)
11880 return;
11882 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11883 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11884 if (!cst)
11885 return;
11887 if (!integer_zerop (cst) && !integer_onep (cst))
11889 int sign = (TREE_CODE (op0) == INTEGER_CST)
11890 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11891 if (code == EQ_EXPR
11892 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11893 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11894 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11895 "with boolean expression is always false", cst);
11896 else
11897 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11898 "with boolean expression is always true", cst);
11902 /* The C and C++ parsers both use vectors to hold function arguments.
11903 For efficiency, we keep a cache of unused vectors. This is the
11904 cache. */
11906 typedef vec<tree, va_gc> *tree_gc_vec;
11907 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11909 /* Return a new vector from the cache. If the cache is empty,
11910 allocate a new vector. These vectors are GC'ed, so it is OK if the
11911 pointer is not released.. */
11913 vec<tree, va_gc> *
11914 make_tree_vector (void)
11916 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11917 return tree_vector_cache->pop ();
11918 else
11920 /* Passing 0 to vec::alloc returns NULL, and our callers require
11921 that we always return a non-NULL value. The vector code uses
11922 4 when growing a NULL vector, so we do too. */
11923 vec<tree, va_gc> *v;
11924 vec_alloc (v, 4);
11925 return v;
11929 /* Release a vector of trees back to the cache. */
11931 void
11932 release_tree_vector (vec<tree, va_gc> *vec)
11934 if (vec != NULL)
11936 vec->truncate (0);
11937 vec_safe_push (tree_vector_cache, vec);
11941 /* Get a new tree vector holding a single tree. */
11943 vec<tree, va_gc> *
11944 make_tree_vector_single (tree t)
11946 vec<tree, va_gc> *ret = make_tree_vector ();
11947 ret->quick_push (t);
11948 return ret;
11951 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11953 vec<tree, va_gc> *
11954 make_tree_vector_from_list (tree list)
11956 vec<tree, va_gc> *ret = make_tree_vector ();
11957 for (; list; list = TREE_CHAIN (list))
11958 vec_safe_push (ret, TREE_VALUE (list));
11959 return ret;
11962 /* Get a new tree vector which is a copy of an existing one. */
11964 vec<tree, va_gc> *
11965 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11967 vec<tree, va_gc> *ret;
11968 unsigned int ix;
11969 tree t;
11971 ret = make_tree_vector ();
11972 vec_safe_reserve (ret, vec_safe_length (orig));
11973 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11974 ret->quick_push (t);
11975 return ret;
11978 /* Return true if KEYWORD starts a type specifier. */
11980 bool
11981 keyword_begins_type_specifier (enum rid keyword)
11983 switch (keyword)
11985 case RID_AUTO_TYPE:
11986 case RID_INT:
11987 case RID_CHAR:
11988 case RID_FLOAT:
11989 case RID_DOUBLE:
11990 case RID_VOID:
11991 case RID_UNSIGNED:
11992 case RID_LONG:
11993 case RID_SHORT:
11994 case RID_SIGNED:
11995 case RID_DFLOAT32:
11996 case RID_DFLOAT64:
11997 case RID_DFLOAT128:
11998 case RID_FRACT:
11999 case RID_ACCUM:
12000 case RID_BOOL:
12001 case RID_WCHAR:
12002 case RID_CHAR16:
12003 case RID_CHAR32:
12004 case RID_SAT:
12005 case RID_COMPLEX:
12006 case RID_TYPEOF:
12007 case RID_STRUCT:
12008 case RID_CLASS:
12009 case RID_UNION:
12010 case RID_ENUM:
12011 return true;
12012 default:
12013 if (keyword >= RID_FIRST_INT_N
12014 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12015 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12016 return true;
12017 return false;
12021 /* Return true if KEYWORD names a type qualifier. */
12023 bool
12024 keyword_is_type_qualifier (enum rid keyword)
12026 switch (keyword)
12028 case RID_CONST:
12029 case RID_VOLATILE:
12030 case RID_RESTRICT:
12031 case RID_ATOMIC:
12032 return true;
12033 default:
12034 return false;
12038 /* Return true if KEYWORD names a storage class specifier.
12040 RID_TYPEDEF is not included in this list despite `typedef' being
12041 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12042 such for syntactic convenience only. */
12044 bool
12045 keyword_is_storage_class_specifier (enum rid keyword)
12047 switch (keyword)
12049 case RID_STATIC:
12050 case RID_EXTERN:
12051 case RID_REGISTER:
12052 case RID_AUTO:
12053 case RID_MUTABLE:
12054 case RID_THREAD:
12055 return true;
12056 default:
12057 return false;
12061 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12063 static bool
12064 keyword_is_function_specifier (enum rid keyword)
12066 switch (keyword)
12068 case RID_INLINE:
12069 case RID_NORETURN:
12070 case RID_VIRTUAL:
12071 case RID_EXPLICIT:
12072 return true;
12073 default:
12074 return false;
12078 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12079 declaration-specifier (C99 6.7). */
12081 bool
12082 keyword_is_decl_specifier (enum rid keyword)
12084 if (keyword_is_storage_class_specifier (keyword)
12085 || keyword_is_type_qualifier (keyword)
12086 || keyword_is_function_specifier (keyword))
12087 return true;
12089 switch (keyword)
12091 case RID_TYPEDEF:
12092 case RID_FRIEND:
12093 case RID_CONSTEXPR:
12094 return true;
12095 default:
12096 return false;
12100 /* Initialize language-specific-bits of tree_contains_struct. */
12102 void
12103 c_common_init_ts (void)
12105 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12106 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12107 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12110 /* Build a user-defined numeric literal out of an integer constant type VALUE
12111 with identifier SUFFIX. */
12113 tree
12114 build_userdef_literal (tree suffix_id, tree value,
12115 enum overflow_type overflow, tree num_string)
12117 tree literal = make_node (USERDEF_LITERAL);
12118 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12119 USERDEF_LITERAL_VALUE (literal) = value;
12120 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12121 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12122 return literal;
12125 /* For vector[index], convert the vector to a
12126 pointer of the underlying type. Return true if the resulting
12127 ARRAY_REF should not be an lvalue. */
12129 bool
12130 convert_vector_to_pointer_for_subscript (location_t loc,
12131 tree *vecp, tree index)
12133 bool ret = false;
12134 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12136 tree type = TREE_TYPE (*vecp);
12137 tree type1;
12139 ret = !lvalue_p (*vecp);
12140 if (TREE_CODE (index) == INTEGER_CST)
12141 if (!tree_fits_uhwi_p (index)
12142 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12143 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12145 if (ret)
12147 tree tmp = create_tmp_var_raw (type);
12148 DECL_SOURCE_LOCATION (tmp) = loc;
12149 *vecp = c_save_expr (*vecp);
12150 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12152 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12153 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12154 *vecp
12155 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12156 *vecp, NULL_TREE, NULL_TREE),
12157 non_const);
12159 else
12160 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12161 NULL_TREE, NULL_TREE);
12162 SET_EXPR_LOCATION (*vecp, loc);
12163 c_common_mark_addressable_vec (tmp);
12165 else
12166 c_common_mark_addressable_vec (*vecp);
12167 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12168 type1 = build_pointer_type (TREE_TYPE (*vecp));
12169 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12170 if (!ref_all
12171 && !DECL_P (*vecp))
12173 /* If the original vector isn't declared may_alias and it
12174 isn't a bare vector look if the subscripting would
12175 alias the vector we subscript, and if not, force ref-all. */
12176 alias_set_type vecset = get_alias_set (*vecp);
12177 alias_set_type sset = get_alias_set (type);
12178 if (!alias_sets_must_conflict_p (sset, vecset)
12179 && !alias_set_subset_of (sset, vecset))
12180 ref_all = true;
12182 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12183 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12184 *vecp = convert (type, *vecp);
12186 return ret;
12189 /* Determine which of the operands, if any, is a scalar that needs to be
12190 converted to a vector, for the range of operations. */
12191 enum stv_conv
12192 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12193 bool complain)
12195 tree type0 = TREE_TYPE (op0);
12196 tree type1 = TREE_TYPE (op1);
12197 bool integer_only_op = false;
12198 enum stv_conv ret = stv_firstarg;
12200 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12201 || TREE_CODE (type1) == VECTOR_TYPE);
12202 switch (code)
12204 /* Most GENERIC binary expressions require homogeneous arguments.
12205 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12206 argument that is a vector and a second one that is a scalar, so
12207 we never return stv_secondarg for them. */
12208 case RSHIFT_EXPR:
12209 case LSHIFT_EXPR:
12210 if (TREE_CODE (type0) == INTEGER_TYPE
12211 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12213 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12215 if (complain)
12216 error_at (loc, "conversion of scalar %qT to vector %qT "
12217 "involves truncation", type0, type1);
12218 return stv_error;
12220 else
12221 return stv_firstarg;
12223 break;
12225 case BIT_IOR_EXPR:
12226 case BIT_XOR_EXPR:
12227 case BIT_AND_EXPR:
12228 integer_only_op = true;
12229 /* ... fall through ... */
12231 case VEC_COND_EXPR:
12233 case PLUS_EXPR:
12234 case MINUS_EXPR:
12235 case MULT_EXPR:
12236 case TRUNC_DIV_EXPR:
12237 case CEIL_DIV_EXPR:
12238 case FLOOR_DIV_EXPR:
12239 case ROUND_DIV_EXPR:
12240 case EXACT_DIV_EXPR:
12241 case TRUNC_MOD_EXPR:
12242 case FLOOR_MOD_EXPR:
12243 case RDIV_EXPR:
12244 case EQ_EXPR:
12245 case NE_EXPR:
12246 case LE_EXPR:
12247 case GE_EXPR:
12248 case LT_EXPR:
12249 case GT_EXPR:
12250 /* What about UNLT_EXPR? */
12251 if (TREE_CODE (type0) == VECTOR_TYPE)
12253 tree tmp;
12254 ret = stv_secondarg;
12255 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12256 tmp = type0; type0 = type1; type1 = tmp;
12257 tmp = op0; op0 = op1; op1 = tmp;
12260 if (TREE_CODE (type0) == INTEGER_TYPE
12261 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12263 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12265 if (complain)
12266 error_at (loc, "conversion of scalar %qT to vector %qT "
12267 "involves truncation", type0, type1);
12268 return stv_error;
12270 return ret;
12272 else if (!integer_only_op
12273 /* Allow integer --> real conversion if safe. */
12274 && (TREE_CODE (type0) == REAL_TYPE
12275 || TREE_CODE (type0) == INTEGER_TYPE)
12276 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12278 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12280 if (complain)
12281 error_at (loc, "conversion of scalar %qT to vector %qT "
12282 "involves truncation", type0, type1);
12283 return stv_error;
12285 return ret;
12287 default:
12288 break;
12291 return stv_nothing;
12294 /* Return true iff ALIGN is an integral constant that is a fundamental
12295 alignment, as defined by [basic.align] in the c++-11
12296 specifications.
12298 That is:
12300 [A fundamental alignment is represented by an alignment less than or
12301 equal to the greatest alignment supported by the implementation
12302 in all contexts, which is equal to
12303 alignof(max_align_t)]. */
12305 bool
12306 cxx_fundamental_alignment_p (unsigned align)
12308 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12309 TYPE_ALIGN (long_double_type_node)));
12312 /* Return true if T is a pointer to a zero-sized aggregate. */
12314 bool
12315 pointer_to_zero_sized_aggr_p (tree t)
12317 if (!POINTER_TYPE_P (t))
12318 return false;
12319 t = TREE_TYPE (t);
12320 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12323 #include "gt-c-family-c-common.h"