Merged revisions 209304,209307,209332,209338-209339,209343,209346,209351,209354,20936...
[official-gcc.git] / gcc-4_9 / gcc / c-family / c-common.c
blobb652cc2489e5c8f918ce3f44040cb7ee6b952118
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "intl.h"
25 #include "tree.h"
26 #include "fold-const.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "varasm.h"
32 #include "trans-mem.h"
33 #include "flags.h"
34 #include "c-pragma.h"
35 #include "c-common.h"
36 #include "c-objc.h"
37 #include "tm_p.h"
38 #include "obstack.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "diagnostic.h"
46 #include "tree-iterator.h"
47 #include "hashtab.h"
48 #include "opts.h"
49 #include "cgraph.h"
50 #include "target-def.h"
51 #include "gimplify.h"
53 cpp_reader *parse_in; /* Declared in c-pragma.h. */
55 /* The following symbols are subsumed in the c_global_trees array, and
56 listed here individually for documentation purposes.
58 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
60 tree short_integer_type_node;
61 tree long_integer_type_node;
62 tree long_long_integer_type_node;
63 tree int128_integer_type_node;
65 tree short_unsigned_type_node;
66 tree long_unsigned_type_node;
67 tree long_long_unsigned_type_node;
68 tree int128_unsigned_type_node;
70 tree truthvalue_type_node;
71 tree truthvalue_false_node;
72 tree truthvalue_true_node;
74 tree ptrdiff_type_node;
76 tree unsigned_char_type_node;
77 tree signed_char_type_node;
78 tree wchar_type_node;
80 tree char16_type_node;
81 tree char32_type_node;
83 tree float_type_node;
84 tree double_type_node;
85 tree long_double_type_node;
87 tree complex_integer_type_node;
88 tree complex_float_type_node;
89 tree complex_double_type_node;
90 tree complex_long_double_type_node;
92 tree dfloat32_type_node;
93 tree dfloat64_type_node;
94 tree_dfloat128_type_node;
96 tree intQI_type_node;
97 tree intHI_type_node;
98 tree intSI_type_node;
99 tree intDI_type_node;
100 tree intTI_type_node;
102 tree unsigned_intQI_type_node;
103 tree unsigned_intHI_type_node;
104 tree unsigned_intSI_type_node;
105 tree unsigned_intDI_type_node;
106 tree unsigned_intTI_type_node;
108 tree widest_integer_literal_type_node;
109 tree widest_unsigned_literal_type_node;
111 Nodes for types `void *' and `const void *'.
113 tree ptr_type_node, const_ptr_type_node;
115 Nodes for types `char *' and `const char *'.
117 tree string_type_node, const_string_type_node;
119 Type `char[SOMENUMBER]'.
120 Used when an array of char is needed and the size is irrelevant.
122 tree char_array_type_node;
124 Type `wchar_t[SOMENUMBER]' or something like it.
125 Used when a wide string literal is created.
127 tree wchar_array_type_node;
129 Type `char16_t[SOMENUMBER]' or something like it.
130 Used when a UTF-16 string literal is created.
132 tree char16_array_type_node;
134 Type `char32_t[SOMENUMBER]' or something like it.
135 Used when a UTF-32 string literal is created.
137 tree char32_array_type_node;
139 Type `int ()' -- used for implicit declaration of functions.
141 tree default_function_type;
143 A VOID_TYPE node, packaged in a TREE_LIST.
145 tree void_list_node;
147 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
148 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
149 VAR_DECLS, but C++ does.)
151 tree function_name_decl_node;
152 tree pretty_function_name_decl_node;
153 tree c99_function_name_decl_node;
155 Stack of nested function name VAR_DECLs.
157 tree saved_function_name_decls;
161 tree c_global_trees[CTI_MAX];
163 /* Switches common to the C front ends. */
165 /* Nonzero means don't output line number information. */
167 char flag_no_line_commands;
169 /* Nonzero causes -E output not to be done, but directives such as
170 #define that have side effects are still obeyed. */
172 char flag_no_output;
174 /* Nonzero means dump macros in some fashion. */
176 char flag_dump_macros;
178 /* Nonzero means pass #include lines through to the output. */
180 char flag_dump_includes;
182 /* Nonzero means process PCH files while preprocessing. */
184 bool flag_pch_preprocess;
186 /* The file name to which we should write a precompiled header, or
187 NULL if no header will be written in this compile. */
189 const char *pch_file;
191 /* Nonzero if an ISO standard was selected. It rejects macros in the
192 user's namespace. */
193 int flag_iso;
195 /* C/ObjC language option variables. */
198 /* Nonzero means allow type mismatches in conditional expressions;
199 just make their values `void'. */
201 int flag_cond_mismatch;
203 /* Nonzero means enable C89 Amendment 1 features. */
205 int flag_isoc94;
207 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
209 int flag_isoc99;
211 /* Nonzero means use the ISO C11 dialect of C. */
213 int flag_isoc11;
215 /* Nonzero means that we have builtin functions, and main is an int. */
217 int flag_hosted = 1;
220 /* ObjC language option variables. */
223 /* Tells the compiler that this is a special run. Do not perform any
224 compiling, instead we are to test some platform dependent features
225 and output a C header file with appropriate definitions. */
227 int print_struct_values;
229 /* Tells the compiler what is the constant string class for ObjC. */
231 const char *constant_string_class_name;
234 /* C++ language option variables. */
237 /* Nonzero means generate separate instantiation control files and
238 juggle them at link time. */
240 int flag_use_repository;
242 /* The C++ dialect being used. C++98 is the default. */
244 enum cxx_dialect cxx_dialect = cxx98;
246 /* Maximum template instantiation depth. This limit exists to limit the
247 time it takes to notice excessively recursive template instantiations.
249 The default is lower than the 1024 recommended by the C++0x standard
250 because G++ runs out of stack before 1024 with highly recursive template
251 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
253 int max_tinst_depth = 900;
255 /* The elements of `ridpointers' are identifier nodes for the reserved
256 type names and storage classes. It is indexed by a RID_... value. */
257 tree *ridpointers;
259 tree (*make_fname_decl) (location_t, tree, int);
261 /* Nonzero means don't warn about problems that occur when the code is
262 executed. */
263 int c_inhibit_evaluation_warnings;
265 /* Whether we are building a boolean conversion inside
266 convert_for_assignment, or some other late binary operation. If
267 build_binary_op is called for C (from code shared by C and C++) in
268 this case, then the operands have already been folded and the
269 result will not be folded again, so C_MAYBE_CONST_EXPR should not
270 be generated. */
271 bool in_late_binary_op;
273 /* Whether lexing has been completed, so subsequent preprocessor
274 errors should use the compiler's input_location. */
275 bool done_lexing = false;
277 /* Information about how a function name is generated. */
278 struct fname_var_t
280 tree *const decl; /* pointer to the VAR_DECL. */
281 const unsigned rid; /* RID number for the identifier. */
282 const int pretty; /* How pretty is it? */
285 /* The three ways of getting then name of the current function. */
287 const struct fname_var_t fname_vars[] =
289 /* C99 compliant __func__, must be first. */
290 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
291 /* GCC __FUNCTION__ compliant. */
292 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
293 /* GCC __PRETTY_FUNCTION__ compliant. */
294 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
295 {NULL, 0, 0},
298 /* Global visibility options. */
299 struct visibility_flags visibility_options;
301 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
302 static tree check_case_value (tree);
303 static bool check_case_bounds (tree, tree, tree *, tree *);
305 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
306 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
312 int, bool *);
313 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
314 int, bool *);
315 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
316 bool *);
317 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_always_inline_attribute (tree *, tree, tree, int,
321 bool *);
322 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
329 bool *);
330 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_transparent_union_attribute (tree *, tree, tree,
332 int, bool *);
333 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
339 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
340 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
343 static tree handle_visibility_attribute (tree *, tree, tree, int,
344 bool *);
345 static tree handle_tls_model_attribute (tree *, tree, tree, int,
346 bool *);
347 static tree handle_no_instrument_function_attribute (tree *, tree,
348 tree, int, bool *);
349 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_deprecated_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_vector_size_attribute (tree *, tree, tree, int,
360 bool *);
361 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
365 bool *);
366 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
373 static tree ignore_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
379 bool *);
380 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
381 bool *);
383 static tree handle_always_patch_for_instrumentation_attribute (tree *, tree,
384 tree, int,
385 bool *);
386 static tree handle_never_patch_for_instrumentation_attribute (tree *, tree,
387 tree, int,
388 bool *);
390 static void check_function_nonnull (tree, int, tree *);
391 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
392 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
393 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
394 static int resort_field_decl_cmp (const void *, const void *);
396 /* Reserved words. The third field is a mask: keywords are disabled
397 if they match the mask.
399 Masks for languages:
400 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
401 C --std=c99: D_CXXONLY | D_OBJC
402 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
403 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
404 C++ --std=c0x: D_CONLY | D_OBJC
405 ObjC++ is like C++ except that D_OBJC is not set
407 If -fno-asm is used, D_ASM is added to the mask. If
408 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
409 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
410 In C with -Wc++-compat, we warn if D_CXXWARN is set.
412 Note the complication of the D_CXX_OBJC keywords. These are
413 reserved words such as 'class'. In C++, 'class' is a reserved
414 word. In Objective-C++ it is too. In Objective-C, it is a
415 reserved word too, but only if it follows an '@' sign.
417 const struct c_common_resword c_common_reswords[] =
419 { "_Alignas", RID_ALIGNAS, D_CONLY },
420 { "_Alignof", RID_ALIGNOF, D_CONLY },
421 { "_Atomic", RID_ATOMIC, D_CONLY },
422 { "_Bool", RID_BOOL, D_CONLY },
423 { "_Complex", RID_COMPLEX, 0 },
424 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
425 { "_Cilk_sync", RID_CILK_SYNC, 0 },
426 { "_Imaginary", RID_IMAGINARY, D_CONLY },
427 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
428 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
429 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
430 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
431 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
432 { "_Sat", RID_SAT, D_CONLY | D_EXT },
433 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
434 { "_Noreturn", RID_NORETURN, D_CONLY },
435 { "_Generic", RID_GENERIC, D_CONLY },
436 { "_Thread_local", RID_THREAD, D_CONLY },
437 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
438 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
439 { "__alignof", RID_ALIGNOF, 0 },
440 { "__alignof__", RID_ALIGNOF, 0 },
441 { "__asm", RID_ASM, 0 },
442 { "__asm__", RID_ASM, 0 },
443 { "__attribute", RID_ATTRIBUTE, 0 },
444 { "__attribute__", RID_ATTRIBUTE, 0 },
445 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
446 { "__bases", RID_BASES, D_CXXONLY },
447 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
448 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
449 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
450 { "__builtin_offsetof", RID_OFFSETOF, 0 },
451 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
452 { "__builtin_va_arg", RID_VA_ARG, 0 },
453 { "__complex", RID_COMPLEX, 0 },
454 { "__complex__", RID_COMPLEX, 0 },
455 { "__const", RID_CONST, 0 },
456 { "__const__", RID_CONST, 0 },
457 { "__decltype", RID_DECLTYPE, D_CXXONLY },
458 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
459 { "__extension__", RID_EXTENSION, 0 },
460 { "__func__", RID_C99_FUNCTION_NAME, 0 },
461 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
462 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
463 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
464 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
465 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
466 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
467 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
468 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
469 { "__imag", RID_IMAGPART, 0 },
470 { "__imag__", RID_IMAGPART, 0 },
471 { "__inline", RID_INLINE, 0 },
472 { "__inline__", RID_INLINE, 0 },
473 { "__int128", RID_INT128, 0 },
474 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
475 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
476 { "__is_class", RID_IS_CLASS, D_CXXONLY },
477 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
478 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
479 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
480 { "__is_final", RID_IS_FINAL, D_CXXONLY },
481 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
482 { "__is_pod", RID_IS_POD, D_CXXONLY },
483 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
484 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
485 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
486 { "__is_union", RID_IS_UNION, D_CXXONLY },
487 { "__label__", RID_LABEL, 0 },
488 { "__null", RID_NULL, 0 },
489 { "__real", RID_REALPART, 0 },
490 { "__real__", RID_REALPART, 0 },
491 { "__restrict", RID_RESTRICT, 0 },
492 { "__restrict__", RID_RESTRICT, 0 },
493 { "__signed", RID_SIGNED, 0 },
494 { "__signed__", RID_SIGNED, 0 },
495 { "__thread", RID_THREAD, 0 },
496 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
497 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
498 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
499 { "__typeof", RID_TYPEOF, 0 },
500 { "__typeof__", RID_TYPEOF, 0 },
501 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
502 { "__volatile", RID_VOLATILE, 0 },
503 { "__volatile__", RID_VOLATILE, 0 },
504 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
505 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
506 { "asm", RID_ASM, D_ASM },
507 { "auto", RID_AUTO, 0 },
508 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
509 { "break", RID_BREAK, 0 },
510 { "case", RID_CASE, 0 },
511 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
512 { "char", RID_CHAR, 0 },
513 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
514 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
515 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
516 { "const", RID_CONST, 0 },
517 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
518 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
519 { "continue", RID_CONTINUE, 0 },
520 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
521 { "default", RID_DEFAULT, 0 },
522 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
523 { "do", RID_DO, 0 },
524 { "double", RID_DOUBLE, 0 },
525 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
526 { "else", RID_ELSE, 0 },
527 { "enum", RID_ENUM, 0 },
528 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
529 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
530 { "extern", RID_EXTERN, 0 },
531 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
532 { "float", RID_FLOAT, 0 },
533 { "for", RID_FOR, 0 },
534 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
535 { "goto", RID_GOTO, 0 },
536 { "if", RID_IF, 0 },
537 { "inline", RID_INLINE, D_EXT89 },
538 { "int", RID_INT, 0 },
539 { "long", RID_LONG, 0 },
540 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
541 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
542 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
543 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
544 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
545 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
546 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
547 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
548 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
549 { "register", RID_REGISTER, 0 },
550 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
551 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
552 { "return", RID_RETURN, 0 },
553 { "short", RID_SHORT, 0 },
554 { "signed", RID_SIGNED, 0 },
555 { "sizeof", RID_SIZEOF, 0 },
556 { "static", RID_STATIC, 0 },
557 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
558 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
559 { "struct", RID_STRUCT, 0 },
560 { "switch", RID_SWITCH, 0 },
561 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
562 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
563 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
564 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
565 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
566 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
567 { "typedef", RID_TYPEDEF, 0 },
568 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
569 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
570 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
571 { "union", RID_UNION, 0 },
572 { "unsigned", RID_UNSIGNED, 0 },
573 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
574 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
575 { "void", RID_VOID, 0 },
576 { "volatile", RID_VOLATILE, 0 },
577 { "wchar_t", RID_WCHAR, D_CXXONLY },
578 { "while", RID_WHILE, 0 },
579 /* These Objective-C keywords are recognized only immediately after
580 an '@'. */
581 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
582 { "defs", RID_AT_DEFS, D_OBJC },
583 { "encode", RID_AT_ENCODE, D_OBJC },
584 { "end", RID_AT_END, D_OBJC },
585 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
586 { "interface", RID_AT_INTERFACE, D_OBJC },
587 { "protocol", RID_AT_PROTOCOL, D_OBJC },
588 { "selector", RID_AT_SELECTOR, D_OBJC },
589 { "finally", RID_AT_FINALLY, D_OBJC },
590 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
591 { "optional", RID_AT_OPTIONAL, D_OBJC },
592 { "required", RID_AT_REQUIRED, D_OBJC },
593 { "property", RID_AT_PROPERTY, D_OBJC },
594 { "package", RID_AT_PACKAGE, D_OBJC },
595 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
596 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
597 /* These are recognized only in protocol-qualifier context
598 (see above) */
599 { "bycopy", RID_BYCOPY, D_OBJC },
600 { "byref", RID_BYREF, D_OBJC },
601 { "in", RID_IN, D_OBJC },
602 { "inout", RID_INOUT, D_OBJC },
603 { "oneway", RID_ONEWAY, D_OBJC },
604 { "out", RID_OUT, D_OBJC },
605 /* These are recognized inside a property attribute list */
606 { "assign", RID_ASSIGN, D_OBJC },
607 { "copy", RID_COPY, D_OBJC },
608 { "getter", RID_GETTER, D_OBJC },
609 { "nonatomic", RID_NONATOMIC, D_OBJC },
610 { "readonly", RID_READONLY, D_OBJC },
611 { "readwrite", RID_READWRITE, D_OBJC },
612 { "retain", RID_RETAIN, D_OBJC },
613 { "setter", RID_SETTER, D_OBJC },
616 const unsigned int num_c_common_reswords =
617 sizeof c_common_reswords / sizeof (struct c_common_resword);
619 /* Table of machine-independent attributes common to all C-like languages. */
620 const struct attribute_spec c_common_attribute_table[] =
622 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
623 affects_type_identity } */
624 { "packed", 0, 0, false, false, false,
625 handle_packed_attribute , false},
626 { "nocommon", 0, 0, true, false, false,
627 handle_nocommon_attribute, false},
628 { "common", 0, 0, true, false, false,
629 handle_common_attribute, false },
630 /* FIXME: logically, noreturn attributes should be listed as
631 "false, true, true" and apply to function types. But implementing this
632 would require all the places in the compiler that use TREE_THIS_VOLATILE
633 on a decl to identify non-returning functions to be located and fixed
634 to check the function type instead. */
635 { "noreturn", 0, 0, true, false, false,
636 handle_noreturn_attribute, false },
637 { "volatile", 0, 0, true, false, false,
638 handle_noreturn_attribute, false },
639 { "noinline", 0, 0, true, false, false,
640 handle_noinline_attribute, false },
641 { "noclone", 0, 0, true, false, false,
642 handle_noclone_attribute, false },
643 { "leaf", 0, 0, true, false, false,
644 handle_leaf_attribute, false },
645 { "always_inline", 0, 0, true, false, false,
646 handle_always_inline_attribute, false },
647 { "gnu_inline", 0, 0, true, false, false,
648 handle_gnu_inline_attribute, false },
649 { "artificial", 0, 0, true, false, false,
650 handle_artificial_attribute, false },
651 { "flatten", 0, 0, true, false, false,
652 handle_flatten_attribute, false },
653 { "used", 0, 0, true, false, false,
654 handle_used_attribute, false },
655 { "unused", 0, 0, false, false, false,
656 handle_unused_attribute, false },
657 { "externally_visible", 0, 0, true, false, false,
658 handle_externally_visible_attribute, false },
659 /* The same comments as for noreturn attributes apply to const ones. */
660 { "const", 0, 0, true, false, false,
661 handle_const_attribute, false },
662 { "transparent_union", 0, 0, false, false, false,
663 handle_transparent_union_attribute, false },
664 { "constructor", 0, 1, true, false, false,
665 handle_constructor_attribute, false },
666 { "destructor", 0, 1, true, false, false,
667 handle_destructor_attribute, false },
668 { "mode", 1, 1, false, true, false,
669 handle_mode_attribute, false },
670 { "section", 1, 1, true, false, false,
671 handle_section_attribute, false },
672 { "aligned", 0, 1, false, false, false,
673 handle_aligned_attribute, false },
674 { "weak", 0, 0, true, false, false,
675 handle_weak_attribute, false },
676 { "ifunc", 1, 1, true, false, false,
677 handle_ifunc_attribute, false },
678 { "alias", 1, 1, true, false, false,
679 handle_alias_attribute, false },
680 { "weakref", 0, 1, true, false, false,
681 handle_weakref_attribute, false },
682 { "no_instrument_function", 0, 0, true, false, false,
683 handle_no_instrument_function_attribute,
684 false },
685 { "malloc", 0, 0, true, false, false,
686 handle_malloc_attribute, false },
687 { "returns_twice", 0, 0, true, false, false,
688 handle_returns_twice_attribute, false },
689 { "no_stack_limit", 0, 0, true, false, false,
690 handle_no_limit_stack_attribute, false },
691 { "pure", 0, 0, true, false, false,
692 handle_pure_attribute, false },
693 { "transaction_callable", 0, 0, false, true, false,
694 handle_tm_attribute, false },
695 { "transaction_unsafe", 0, 0, false, true, false,
696 handle_tm_attribute, false },
697 { "transaction_safe", 0, 0, false, true, false,
698 handle_tm_attribute, false },
699 { "transaction_may_cancel_outer", 0, 0, false, true, false,
700 handle_tm_attribute, false },
701 /* ??? These two attributes didn't make the transition from the
702 Intel language document to the multi-vendor language document. */
703 { "transaction_pure", 0, 0, false, true, false,
704 handle_tm_attribute, false },
705 { "transaction_wrap", 1, 1, true, false, false,
706 handle_tm_wrap_attribute, false },
707 /* For internal use (marking of builtins) only. The name contains space
708 to prevent its usage in source code. */
709 { "no vops", 0, 0, true, false, false,
710 handle_novops_attribute, false },
711 { "deprecated", 0, 1, false, false, false,
712 handle_deprecated_attribute, false },
713 { "vector_size", 1, 1, false, true, false,
714 handle_vector_size_attribute, false },
715 { "visibility", 1, 1, false, false, false,
716 handle_visibility_attribute, false },
717 { "tls_model", 1, 1, true, false, false,
718 handle_tls_model_attribute, false },
719 { "nonnull", 0, -1, false, true, true,
720 handle_nonnull_attribute, false },
721 { "nothrow", 0, 0, true, false, false,
722 handle_nothrow_attribute, false },
723 { "may_alias", 0, 0, false, true, false, NULL, false },
724 { "cleanup", 1, 1, true, false, false,
725 handle_cleanup_attribute, false },
726 { "warn_unused_result", 0, 0, false, true, true,
727 handle_warn_unused_result_attribute, false },
728 { "sentinel", 0, 1, false, true, true,
729 handle_sentinel_attribute, false },
730 /* For internal use (marking of builtins) only. The name contains space
731 to prevent its usage in source code. */
732 { "type generic", 0, 0, false, true, true,
733 handle_type_generic_attribute, false },
734 { "alloc_size", 1, 2, false, true, true,
735 handle_alloc_size_attribute, false },
736 { "cold", 0, 0, true, false, false,
737 handle_cold_attribute, false },
738 { "hot", 0, 0, true, false, false,
739 handle_hot_attribute, false },
740 { "no_address_safety_analysis",
741 0, 0, true, false, false,
742 handle_no_address_safety_analysis_attribute,
743 false },
744 { "no_sanitize_address", 0, 0, true, false, false,
745 handle_no_sanitize_address_attribute,
746 false },
747 { "no_sanitize_undefined", 0, 0, true, false, false,
748 handle_no_sanitize_undefined_attribute,
749 false },
750 { "warning", 1, 1, true, false, false,
751 handle_error_attribute, false },
752 { "error", 1, 1, true, false, false,
753 handle_error_attribute, false },
754 { "target", 1, -1, true, false, false,
755 handle_target_attribute, false },
756 { "optimize", 1, -1, true, false, false,
757 handle_optimize_attribute, false },
758 /* For internal use only. The leading '*' both prevents its usage in
759 source code and signals that it may be overridden by machine tables. */
760 { "*tm regparm", 0, 0, false, true, true,
761 ignore_attribute, false },
762 { "no_split_stack", 0, 0, true, false, false,
763 handle_no_split_stack_attribute, false },
764 /* For internal use (marking of builtins and runtime functions) only.
765 The name contains space to prevent its usage in source code. */
766 { "fn spec", 1, 1, false, true, true,
767 handle_fnspec_attribute, false },
768 { "always_patch_for_instrumentation", 0, 0, true, false, false,
769 handle_always_patch_for_instrumentation_attribute,
770 false },
771 { "never_patch_for_instrumentation", 0, 0, true, false, false,
772 handle_never_patch_for_instrumentation_attribute,
773 false },
775 { "warn_unused", 0, 0, false, false, false,
776 handle_warn_unused_attribute, false },
777 { "returns_nonnull", 0, 0, false, true, true,
778 handle_returns_nonnull_attribute, false },
779 { "omp declare simd", 0, -1, true, false, false,
780 handle_omp_declare_simd_attribute, false },
781 { "cilk simd function", 0, -1, true, false, false,
782 handle_omp_declare_simd_attribute, false },
783 { "omp declare target", 0, 0, true, false, false,
784 handle_omp_declare_target_attribute, false },
785 { "alloc_align", 1, 1, false, true, true,
786 handle_alloc_align_attribute, false },
787 { "assume_aligned", 1, 2, false, true, true,
788 handle_assume_aligned_attribute, false },
789 { NULL, 0, 0, false, false, false, NULL, false }
792 /* Give the specifications for the format attributes, used by C and all
793 descendants. */
795 const struct attribute_spec c_common_format_attribute_table[] =
797 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
798 affects_type_identity } */
799 { "format", 3, 3, false, true, true,
800 handle_format_attribute, false },
801 { "format_arg", 1, 1, false, true, true,
802 handle_format_arg_attribute, false },
803 { NULL, 0, 0, false, false, false, NULL, false }
806 /* Return identifier for address space AS. */
808 const char *
809 c_addr_space_name (addr_space_t as)
811 int rid = RID_FIRST_ADDR_SPACE + as;
812 gcc_assert (ridpointers [rid]);
813 return IDENTIFIER_POINTER (ridpointers [rid]);
816 /* Push current bindings for the function name VAR_DECLS. */
818 void
819 start_fname_decls (void)
821 unsigned ix;
822 tree saved = NULL_TREE;
824 for (ix = 0; fname_vars[ix].decl; ix++)
826 tree decl = *fname_vars[ix].decl;
828 if (decl)
830 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
831 saved);
832 *fname_vars[ix].decl = NULL_TREE;
835 if (saved || saved_function_name_decls)
836 /* Normally they'll have been NULL, so only push if we've got a
837 stack, or they are non-NULL. */
838 saved_function_name_decls = tree_cons (saved, NULL_TREE,
839 saved_function_name_decls);
842 /* Finish up the current bindings, adding them into the current function's
843 statement tree. This must be done _before_ finish_stmt_tree is called.
844 If there is no current function, we must be at file scope and no statements
845 are involved. Pop the previous bindings. */
847 void
848 finish_fname_decls (void)
850 unsigned ix;
851 tree stmts = NULL_TREE;
852 tree stack = saved_function_name_decls;
854 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
855 append_to_statement_list (TREE_VALUE (stack), &stmts);
857 if (stmts)
859 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
861 if (TREE_CODE (*bodyp) == BIND_EXPR)
862 bodyp = &BIND_EXPR_BODY (*bodyp);
864 append_to_statement_list_force (*bodyp, &stmts);
865 *bodyp = stmts;
868 for (ix = 0; fname_vars[ix].decl; ix++)
869 *fname_vars[ix].decl = NULL_TREE;
871 if (stack)
873 /* We had saved values, restore them. */
874 tree saved;
876 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
878 tree decl = TREE_PURPOSE (saved);
879 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
881 *fname_vars[ix].decl = decl;
883 stack = TREE_CHAIN (stack);
885 saved_function_name_decls = stack;
888 /* Return the text name of the current function, suitably prettified
889 by PRETTY_P. Return string must be freed by caller. */
891 const char *
892 fname_as_string (int pretty_p)
894 const char *name = "top level";
895 char *namep;
896 int vrb = 2, len;
897 cpp_string cstr = { 0, 0 }, strname;
899 if (!pretty_p)
901 name = "";
902 vrb = 0;
905 if (current_function_decl)
906 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
908 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
910 namep = XNEWVEC (char, len);
911 snprintf (namep, len, "\"%s\"", name);
912 strname.text = (unsigned char *) namep;
913 strname.len = len - 1;
915 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
917 XDELETEVEC (namep);
918 return (const char *) cstr.text;
921 return namep;
924 /* Return the VAR_DECL for a const char array naming the current
925 function. If the VAR_DECL has not yet been created, create it
926 now. RID indicates how it should be formatted and IDENTIFIER_NODE
927 ID is its name (unfortunately C and C++ hold the RID values of
928 keywords in different places, so we can't derive RID from ID in
929 this language independent code. LOC is the location of the
930 function. */
932 tree
933 fname_decl (location_t loc, unsigned int rid, tree id)
935 unsigned ix;
936 tree decl = NULL_TREE;
938 for (ix = 0; fname_vars[ix].decl; ix++)
939 if (fname_vars[ix].rid == rid)
940 break;
942 decl = *fname_vars[ix].decl;
943 if (!decl)
945 /* If a tree is built here, it would normally have the lineno of
946 the current statement. Later this tree will be moved to the
947 beginning of the function and this line number will be wrong.
948 To avoid this problem set the lineno to 0 here; that prevents
949 it from appearing in the RTL. */
950 tree stmts;
951 location_t saved_location = input_location;
952 input_location = UNKNOWN_LOCATION;
954 stmts = push_stmt_list ();
955 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
956 stmts = pop_stmt_list (stmts);
957 if (!IS_EMPTY_STMT (stmts))
958 saved_function_name_decls
959 = tree_cons (decl, stmts, saved_function_name_decls);
960 *fname_vars[ix].decl = decl;
961 input_location = saved_location;
963 if (!ix && !current_function_decl)
964 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
966 return decl;
969 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
971 tree
972 fix_string_type (tree value)
974 int length = TREE_STRING_LENGTH (value);
975 int nchars;
976 tree e_type, i_type, a_type;
978 /* Compute the number of elements, for the array type. */
979 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
981 nchars = length;
982 e_type = char_type_node;
984 else if (TREE_TYPE (value) == char16_array_type_node)
986 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
987 e_type = char16_type_node;
989 else if (TREE_TYPE (value) == char32_array_type_node)
991 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
992 e_type = char32_type_node;
994 else
996 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
997 e_type = wchar_type_node;
1000 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1001 limit in C++98 Annex B is very large (65536) and is not normative,
1002 so we do not diagnose it (warn_overlength_strings is forced off
1003 in c_common_post_options). */
1004 if (warn_overlength_strings)
1006 const int nchars_max = flag_isoc99 ? 4095 : 509;
1007 const int relevant_std = flag_isoc99 ? 99 : 90;
1008 if (nchars - 1 > nchars_max)
1009 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1010 separate the %d from the 'C'. 'ISO' should not be
1011 translated, but it may be moved after 'C%d' in languages
1012 where modifiers follow nouns. */
1013 pedwarn (input_location, OPT_Woverlength_strings,
1014 "string length %qd is greater than the length %qd "
1015 "ISO C%d compilers are required to support",
1016 nchars - 1, nchars_max, relevant_std);
1019 /* Create the array type for the string constant. The ISO C++
1020 standard says that a string literal has type `const char[N]' or
1021 `const wchar_t[N]'. We use the same logic when invoked as a C
1022 front-end with -Wwrite-strings.
1023 ??? We should change the type of an expression depending on the
1024 state of a warning flag. We should just be warning -- see how
1025 this is handled in the C++ front-end for the deprecated implicit
1026 conversion from string literals to `char*' or `wchar_t*'.
1028 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1029 array type being the unqualified version of that type.
1030 Therefore, if we are constructing an array of const char, we must
1031 construct the matching unqualified array type first. The C front
1032 end does not require this, but it does no harm, so we do it
1033 unconditionally. */
1034 i_type = build_index_type (size_int (nchars - 1));
1035 a_type = build_array_type (e_type, i_type);
1036 if (c_dialect_cxx() || warn_write_strings)
1037 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1039 TREE_TYPE (value) = a_type;
1040 TREE_CONSTANT (value) = 1;
1041 TREE_READONLY (value) = 1;
1042 TREE_STATIC (value) = 1;
1043 return value;
1046 /* If DISABLE is true, stop issuing warnings. This is used when
1047 parsing code that we know will not be executed. This function may
1048 be called multiple times, and works as a stack. */
1050 static void
1051 c_disable_warnings (bool disable)
1053 if (disable)
1055 ++c_inhibit_evaluation_warnings;
1056 fold_defer_overflow_warnings ();
1060 /* If ENABLE is true, reenable issuing warnings. */
1062 static void
1063 c_enable_warnings (bool enable)
1065 if (enable)
1067 --c_inhibit_evaluation_warnings;
1068 fold_undefer_and_ignore_overflow_warnings ();
1072 /* Fully fold EXPR, an expression that was not folded (beyond integer
1073 constant expressions and null pointer constants) when being built
1074 up. If IN_INIT, this is in a static initializer and certain
1075 changes are made to the folding done. Clear *MAYBE_CONST if
1076 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1077 expression because it contains an evaluated operator (in C99) or an
1078 operator outside of sizeof returning an integer constant (in C90)
1079 not permitted in constant expressions, or because it contains an
1080 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1081 set to true by callers before calling this function.) Return the
1082 folded expression. Function arguments have already been folded
1083 before calling this function, as have the contents of SAVE_EXPR,
1084 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1085 C_MAYBE_CONST_EXPR. */
1087 tree
1088 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1090 tree ret;
1091 tree eptype = NULL_TREE;
1092 bool dummy = true;
1093 bool maybe_const_itself = true;
1094 location_t loc = EXPR_LOCATION (expr);
1096 /* This function is not relevant to C++ because C++ folds while
1097 parsing, and may need changes to be correct for C++ when C++
1098 stops folding while parsing. */
1099 if (c_dialect_cxx ())
1100 gcc_unreachable ();
1102 if (!maybe_const)
1103 maybe_const = &dummy;
1104 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1106 eptype = TREE_TYPE (expr);
1107 expr = TREE_OPERAND (expr, 0);
1109 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1110 &maybe_const_itself);
1111 if (eptype)
1112 ret = fold_convert_loc (loc, eptype, ret);
1113 *maybe_const &= maybe_const_itself;
1114 return ret;
1117 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1118 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1119 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1120 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1121 both evaluated and unevaluated subexpressions while
1122 *MAYBE_CONST_ITSELF is carried from only evaluated
1123 subexpressions). */
1125 static tree
1126 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1127 bool *maybe_const_itself)
1129 tree ret = expr;
1130 enum tree_code code = TREE_CODE (expr);
1131 enum tree_code_class kind = TREE_CODE_CLASS (code);
1132 location_t loc = EXPR_LOCATION (expr);
1133 tree op0, op1, op2, op3;
1134 tree orig_op0, orig_op1, orig_op2;
1135 bool op0_const = true, op1_const = true, op2_const = true;
1136 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1137 bool nowarning = TREE_NO_WARNING (expr);
1138 bool unused_p;
1140 /* This function is not relevant to C++ because C++ folds while
1141 parsing, and may need changes to be correct for C++ when C++
1142 stops folding while parsing. */
1143 if (c_dialect_cxx ())
1144 gcc_unreachable ();
1146 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1147 anything else not counted as an expression cannot usefully be
1148 folded further at this point. */
1149 if (!IS_EXPR_CODE_CLASS (kind)
1150 || kind == tcc_statement
1151 || code == SAVE_EXPR)
1152 return expr;
1154 /* Operands of variable-length expressions (function calls) have
1155 already been folded, as have __builtin_* function calls, and such
1156 expressions cannot occur in constant expressions. */
1157 if (kind == tcc_vl_exp)
1159 *maybe_const_operands = false;
1160 ret = fold (expr);
1161 goto out;
1164 if (code == C_MAYBE_CONST_EXPR)
1166 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1167 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1168 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1169 *maybe_const_operands = false;
1170 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1171 *maybe_const_itself = false;
1172 if (pre && !in_init)
1173 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1174 else
1175 ret = inner;
1176 goto out;
1179 /* Assignment, increment, decrement, function call and comma
1180 operators, and statement expressions, cannot occur in constant
1181 expressions if evaluated / outside of sizeof. (Function calls
1182 were handled above, though VA_ARG_EXPR is treated like a function
1183 call here, and statement expressions are handled through
1184 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1185 switch (code)
1187 case MODIFY_EXPR:
1188 case PREDECREMENT_EXPR:
1189 case PREINCREMENT_EXPR:
1190 case POSTDECREMENT_EXPR:
1191 case POSTINCREMENT_EXPR:
1192 case COMPOUND_EXPR:
1193 *maybe_const_operands = false;
1194 break;
1196 case VA_ARG_EXPR:
1197 case TARGET_EXPR:
1198 case BIND_EXPR:
1199 case OBJ_TYPE_REF:
1200 *maybe_const_operands = false;
1201 ret = fold (expr);
1202 goto out;
1204 default:
1205 break;
1208 /* Fold individual tree codes as appropriate. */
1209 switch (code)
1211 case COMPOUND_LITERAL_EXPR:
1212 /* Any non-constancy will have been marked in a containing
1213 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1214 goto out;
1216 case COMPONENT_REF:
1217 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1218 op1 = TREE_OPERAND (expr, 1);
1219 op2 = TREE_OPERAND (expr, 2);
1220 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1221 maybe_const_itself);
1222 STRIP_TYPE_NOPS (op0);
1223 if (op0 != orig_op0)
1224 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1225 if (ret != expr)
1227 TREE_READONLY (ret) = TREE_READONLY (expr);
1228 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1230 goto out;
1232 case ARRAY_REF:
1233 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1234 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1235 op2 = TREE_OPERAND (expr, 2);
1236 op3 = TREE_OPERAND (expr, 3);
1237 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1238 maybe_const_itself);
1239 STRIP_TYPE_NOPS (op0);
1240 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1241 maybe_const_itself);
1242 STRIP_TYPE_NOPS (op1);
1243 op1 = decl_constant_value_for_optimization (op1);
1244 if (op0 != orig_op0 || op1 != orig_op1)
1245 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1246 if (ret != expr)
1248 TREE_READONLY (ret) = TREE_READONLY (expr);
1249 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1250 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1252 ret = fold (ret);
1253 goto out;
1255 case COMPOUND_EXPR:
1256 case MODIFY_EXPR:
1257 case PREDECREMENT_EXPR:
1258 case PREINCREMENT_EXPR:
1259 case POSTDECREMENT_EXPR:
1260 case POSTINCREMENT_EXPR:
1261 case PLUS_EXPR:
1262 case MINUS_EXPR:
1263 case MULT_EXPR:
1264 case POINTER_PLUS_EXPR:
1265 case TRUNC_DIV_EXPR:
1266 case CEIL_DIV_EXPR:
1267 case FLOOR_DIV_EXPR:
1268 case TRUNC_MOD_EXPR:
1269 case RDIV_EXPR:
1270 case EXACT_DIV_EXPR:
1271 case LSHIFT_EXPR:
1272 case RSHIFT_EXPR:
1273 case BIT_IOR_EXPR:
1274 case BIT_XOR_EXPR:
1275 case BIT_AND_EXPR:
1276 case LT_EXPR:
1277 case LE_EXPR:
1278 case GT_EXPR:
1279 case GE_EXPR:
1280 case EQ_EXPR:
1281 case NE_EXPR:
1282 case COMPLEX_EXPR:
1283 case TRUTH_AND_EXPR:
1284 case TRUTH_OR_EXPR:
1285 case TRUTH_XOR_EXPR:
1286 case UNORDERED_EXPR:
1287 case ORDERED_EXPR:
1288 case UNLT_EXPR:
1289 case UNLE_EXPR:
1290 case UNGT_EXPR:
1291 case UNGE_EXPR:
1292 case UNEQ_EXPR:
1293 /* Binary operations evaluating both arguments (increment and
1294 decrement are binary internally in GCC). */
1295 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1296 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1297 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1298 maybe_const_itself);
1299 STRIP_TYPE_NOPS (op0);
1300 if (code != MODIFY_EXPR
1301 && code != PREDECREMENT_EXPR
1302 && code != PREINCREMENT_EXPR
1303 && code != POSTDECREMENT_EXPR
1304 && code != POSTINCREMENT_EXPR)
1305 op0 = decl_constant_value_for_optimization (op0);
1306 /* The RHS of a MODIFY_EXPR was fully folded when building that
1307 expression for the sake of conversion warnings. */
1308 if (code != MODIFY_EXPR)
1309 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1310 maybe_const_itself);
1311 STRIP_TYPE_NOPS (op1);
1312 op1 = decl_constant_value_for_optimization (op1);
1313 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1314 ret = in_init
1315 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1316 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1317 else
1318 ret = fold (expr);
1319 if (TREE_OVERFLOW_P (ret)
1320 && !TREE_OVERFLOW_P (op0)
1321 && !TREE_OVERFLOW_P (op1))
1322 overflow_warning (EXPR_LOCATION (expr), ret);
1323 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1324 && TREE_CODE (orig_op1) != INTEGER_CST
1325 && TREE_CODE (op1) == INTEGER_CST
1326 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1327 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1328 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1329 && c_inhibit_evaluation_warnings == 0)
1331 if (tree_int_cst_sgn (op1) < 0)
1332 warning_at (loc, 0, (code == LSHIFT_EXPR
1333 ? G_("left shift count is negative")
1334 : G_("right shift count is negative")));
1335 else if (compare_tree_int (op1,
1336 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1337 >= 0)
1338 warning_at (loc, 0, (code == LSHIFT_EXPR
1339 ? G_("left shift count >= width of type")
1340 : G_("right shift count >= width of type")));
1342 goto out;
1344 case INDIRECT_REF:
1345 case FIX_TRUNC_EXPR:
1346 case FLOAT_EXPR:
1347 CASE_CONVERT:
1348 case ADDR_SPACE_CONVERT_EXPR:
1349 case VIEW_CONVERT_EXPR:
1350 case NON_LVALUE_EXPR:
1351 case NEGATE_EXPR:
1352 case BIT_NOT_EXPR:
1353 case TRUTH_NOT_EXPR:
1354 case ADDR_EXPR:
1355 case CONJ_EXPR:
1356 case REALPART_EXPR:
1357 case IMAGPART_EXPR:
1358 /* Unary operations. */
1359 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1360 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1361 maybe_const_itself);
1362 STRIP_TYPE_NOPS (op0);
1363 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1364 op0 = decl_constant_value_for_optimization (op0);
1365 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1366 not prepared to deal with them if they occur in initializers. */
1367 if (op0 != orig_op0
1368 && code == ADDR_EXPR
1369 && (op1 = get_base_address (op0)) != NULL_TREE
1370 && TREE_CODE (op1) == INDIRECT_REF
1371 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1372 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1373 else if (op0 != orig_op0 || in_init)
1374 ret = in_init
1375 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1376 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1377 else
1378 ret = fold (expr);
1379 if (code == INDIRECT_REF
1380 && ret != expr
1381 && TREE_CODE (ret) == INDIRECT_REF)
1383 TREE_READONLY (ret) = TREE_READONLY (expr);
1384 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1385 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1387 switch (code)
1389 case FIX_TRUNC_EXPR:
1390 case FLOAT_EXPR:
1391 CASE_CONVERT:
1392 /* Don't warn about explicit conversions. We will already
1393 have warned about suspect implicit conversions. */
1394 break;
1396 default:
1397 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1398 overflow_warning (EXPR_LOCATION (expr), ret);
1399 break;
1401 goto out;
1403 case TRUTH_ANDIF_EXPR:
1404 case TRUTH_ORIF_EXPR:
1405 /* Binary operations not necessarily evaluating both
1406 arguments. */
1407 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1408 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1409 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1410 STRIP_TYPE_NOPS (op0);
1412 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1413 ? truthvalue_false_node
1414 : truthvalue_true_node));
1415 c_disable_warnings (unused_p);
1416 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1417 STRIP_TYPE_NOPS (op1);
1418 c_enable_warnings (unused_p);
1420 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1421 ret = in_init
1422 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1423 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1424 else
1425 ret = fold (expr);
1426 *maybe_const_operands &= op0_const;
1427 *maybe_const_itself &= op0_const_self;
1428 if (!(flag_isoc99
1429 && op0_const
1430 && op0_const_self
1431 && (code == TRUTH_ANDIF_EXPR
1432 ? op0 == truthvalue_false_node
1433 : op0 == truthvalue_true_node)))
1434 *maybe_const_operands &= op1_const;
1435 if (!(op0_const
1436 && op0_const_self
1437 && (code == TRUTH_ANDIF_EXPR
1438 ? op0 == truthvalue_false_node
1439 : op0 == truthvalue_true_node)))
1440 *maybe_const_itself &= op1_const_self;
1441 goto out;
1443 case COND_EXPR:
1444 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1445 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1446 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1447 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1449 STRIP_TYPE_NOPS (op0);
1450 c_disable_warnings (op0 == truthvalue_false_node);
1451 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1452 STRIP_TYPE_NOPS (op1);
1453 c_enable_warnings (op0 == truthvalue_false_node);
1455 c_disable_warnings (op0 == truthvalue_true_node);
1456 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1457 STRIP_TYPE_NOPS (op2);
1458 c_enable_warnings (op0 == truthvalue_true_node);
1460 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1461 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1462 else
1463 ret = fold (expr);
1464 *maybe_const_operands &= op0_const;
1465 *maybe_const_itself &= op0_const_self;
1466 if (!(flag_isoc99
1467 && op0_const
1468 && op0_const_self
1469 && op0 == truthvalue_false_node))
1470 *maybe_const_operands &= op1_const;
1471 if (!(op0_const
1472 && op0_const_self
1473 && op0 == truthvalue_false_node))
1474 *maybe_const_itself &= op1_const_self;
1475 if (!(flag_isoc99
1476 && op0_const
1477 && op0_const_self
1478 && op0 == truthvalue_true_node))
1479 *maybe_const_operands &= op2_const;
1480 if (!(op0_const
1481 && op0_const_self
1482 && op0 == truthvalue_true_node))
1483 *maybe_const_itself &= op2_const_self;
1484 goto out;
1486 case EXCESS_PRECISION_EXPR:
1487 /* Each case where an operand with excess precision may be
1488 encountered must remove the EXCESS_PRECISION_EXPR around
1489 inner operands and possibly put one around the whole
1490 expression or possibly convert to the semantic type (which
1491 c_fully_fold does); we cannot tell at this stage which is
1492 appropriate in any particular case. */
1493 gcc_unreachable ();
1495 default:
1496 /* Various codes may appear through folding built-in functions
1497 and their arguments. */
1498 goto out;
1501 out:
1502 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1503 have been done by this point, so remove them again. */
1504 nowarning |= TREE_NO_WARNING (ret);
1505 STRIP_TYPE_NOPS (ret);
1506 if (nowarning && !TREE_NO_WARNING (ret))
1508 if (!CAN_HAVE_LOCATION_P (ret))
1509 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1510 TREE_NO_WARNING (ret) = 1;
1512 if (ret != expr)
1513 protected_set_expr_location (ret, loc);
1514 return ret;
1517 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1518 return EXP. Otherwise, return either EXP or its known constant
1519 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1520 Is the BLKmode test appropriate? */
1522 tree
1523 decl_constant_value_for_optimization (tree exp)
1525 tree ret;
1527 /* This function is only used by C, for c_fully_fold and other
1528 optimization, and may not be correct for C++. */
1529 if (c_dialect_cxx ())
1530 gcc_unreachable ();
1532 if (!optimize
1533 || TREE_CODE (exp) != VAR_DECL
1534 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1535 || DECL_MODE (exp) == BLKmode)
1536 return exp;
1538 ret = decl_constant_value (exp);
1539 /* Avoid unwanted tree sharing between the initializer and current
1540 function's body where the tree can be modified e.g. by the
1541 gimplifier. */
1542 if (ret != exp && TREE_STATIC (exp))
1543 ret = unshare_expr (ret);
1544 return ret;
1547 /* Print a warning if a constant expression had overflow in folding.
1548 Invoke this function on every expression that the language
1549 requires to be a constant expression.
1550 Note the ANSI C standard says it is erroneous for a
1551 constant expression to overflow. */
1553 void
1554 constant_expression_warning (tree value)
1556 if (warn_overflow && pedantic
1557 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1558 || TREE_CODE (value) == FIXED_CST
1559 || TREE_CODE (value) == VECTOR_CST
1560 || TREE_CODE (value) == COMPLEX_CST)
1561 && TREE_OVERFLOW (value))
1562 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1565 /* The same as above but print an unconditional error. */
1566 void
1567 constant_expression_error (tree value)
1569 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1570 || TREE_CODE (value) == FIXED_CST
1571 || TREE_CODE (value) == VECTOR_CST
1572 || TREE_CODE (value) == COMPLEX_CST)
1573 && TREE_OVERFLOW (value))
1574 error ("overflow in constant expression");
1577 /* Print a warning if an expression had overflow in folding and its
1578 operands hadn't.
1580 Invoke this function on every expression that
1581 (1) appears in the source code, and
1582 (2) is a constant expression that overflowed, and
1583 (3) is not already checked by convert_and_check;
1584 however, do not invoke this function on operands of explicit casts
1585 or when the expression is the result of an operator and any operand
1586 already overflowed. */
1588 void
1589 overflow_warning (location_t loc, tree value)
1591 if (c_inhibit_evaluation_warnings != 0)
1592 return;
1594 switch (TREE_CODE (value))
1596 case INTEGER_CST:
1597 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1598 break;
1600 case REAL_CST:
1601 warning_at (loc, OPT_Woverflow,
1602 "floating point overflow in expression");
1603 break;
1605 case FIXED_CST:
1606 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1607 break;
1609 case VECTOR_CST:
1610 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1611 break;
1613 case COMPLEX_CST:
1614 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1615 warning_at (loc, OPT_Woverflow,
1616 "complex integer overflow in expression");
1617 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1618 warning_at (loc, OPT_Woverflow,
1619 "complex floating point overflow in expression");
1620 break;
1622 default:
1623 break;
1627 /* Warn about uses of logical || / && operator in a context where it
1628 is likely that the bitwise equivalent was intended by the
1629 programmer. We have seen an expression in which CODE is a binary
1630 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1631 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1632 void
1633 warn_logical_operator (location_t location, enum tree_code code, tree type,
1634 enum tree_code code_left, tree op_left,
1635 enum tree_code ARG_UNUSED (code_right), tree op_right)
1637 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1638 int in0_p, in1_p, in_p;
1639 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1640 bool strict_overflow_p = false;
1642 if (code != TRUTH_ANDIF_EXPR
1643 && code != TRUTH_AND_EXPR
1644 && code != TRUTH_ORIF_EXPR
1645 && code != TRUTH_OR_EXPR)
1646 return;
1648 /* Warn if &&/|| are being used in a context where it is
1649 likely that the bitwise equivalent was intended by the
1650 programmer. That is, an expression such as op && MASK
1651 where op should not be any boolean expression, nor a
1652 constant, and mask seems to be a non-boolean integer constant. */
1653 if (!truth_value_p (code_left)
1654 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1655 && !CONSTANT_CLASS_P (op_left)
1656 && !TREE_NO_WARNING (op_left)
1657 && TREE_CODE (op_right) == INTEGER_CST
1658 && !integer_zerop (op_right)
1659 && !integer_onep (op_right))
1661 if (or_op)
1662 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1663 " applied to non-boolean constant");
1664 else
1665 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1666 " applied to non-boolean constant");
1667 TREE_NO_WARNING (op_left) = true;
1668 return;
1671 /* We do not warn for constants because they are typical of macro
1672 expansions that test for features. */
1673 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1674 return;
1676 /* This warning only makes sense with logical operands. */
1677 if (!(truth_value_p (TREE_CODE (op_left))
1678 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1679 || !(truth_value_p (TREE_CODE (op_right))
1680 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1681 return;
1684 /* We first test whether either side separately is trivially true
1685 (with OR) or trivially false (with AND). If so, do not warn.
1686 This is a common idiom for testing ranges of data types in
1687 portable code. */
1688 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1689 if (!lhs)
1690 return;
1691 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1692 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1694 /* If this is an OR operation, invert both sides; now, the result
1695 should be always false to get a warning. */
1696 if (or_op)
1697 in0_p = !in0_p;
1699 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1700 if (tem && integer_zerop (tem))
1701 return;
1703 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1704 if (!rhs)
1705 return;
1706 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1707 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1709 /* If this is an OR operation, invert both sides; now, the result
1710 should be always false to get a warning. */
1711 if (or_op)
1712 in1_p = !in1_p;
1714 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1715 if (tem && integer_zerop (tem))
1716 return;
1718 /* If both expressions have the same operand, if we can merge the
1719 ranges, and if the range test is always false, then warn. */
1720 if (operand_equal_p (lhs, rhs, 0)
1721 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1722 in1_p, low1, high1)
1723 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1724 type, lhs, in_p, low, high))
1725 && integer_zerop (tem))
1727 if (or_op)
1728 warning_at (location, OPT_Wlogical_op,
1729 "logical %<or%> "
1730 "of collectively exhaustive tests is always true");
1731 else
1732 warning_at (location, OPT_Wlogical_op,
1733 "logical %<and%> "
1734 "of mutually exclusive tests is always false");
1739 /* Warn if EXP contains any computations whose results are not used.
1740 Return true if a warning is printed; false otherwise. LOCUS is the
1741 (potential) location of the expression. */
1743 bool
1744 warn_if_unused_value (const_tree exp, location_t locus)
1746 restart:
1747 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1748 return false;
1750 /* Don't warn about void constructs. This includes casting to void,
1751 void function calls, and statement expressions with a final cast
1752 to void. */
1753 if (VOID_TYPE_P (TREE_TYPE (exp)))
1754 return false;
1756 if (EXPR_HAS_LOCATION (exp))
1757 locus = EXPR_LOCATION (exp);
1759 switch (TREE_CODE (exp))
1761 case PREINCREMENT_EXPR:
1762 case POSTINCREMENT_EXPR:
1763 case PREDECREMENT_EXPR:
1764 case POSTDECREMENT_EXPR:
1765 case MODIFY_EXPR:
1766 case INIT_EXPR:
1767 case TARGET_EXPR:
1768 case CALL_EXPR:
1769 case TRY_CATCH_EXPR:
1770 case WITH_CLEANUP_EXPR:
1771 case EXIT_EXPR:
1772 case VA_ARG_EXPR:
1773 return false;
1775 case BIND_EXPR:
1776 /* For a binding, warn if no side effect within it. */
1777 exp = BIND_EXPR_BODY (exp);
1778 goto restart;
1780 case SAVE_EXPR:
1781 case NON_LVALUE_EXPR:
1782 case NOP_EXPR:
1783 exp = TREE_OPERAND (exp, 0);
1784 goto restart;
1786 case TRUTH_ORIF_EXPR:
1787 case TRUTH_ANDIF_EXPR:
1788 /* In && or ||, warn if 2nd operand has no side effect. */
1789 exp = TREE_OPERAND (exp, 1);
1790 goto restart;
1792 case COMPOUND_EXPR:
1793 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1794 return true;
1795 /* Let people do `(foo (), 0)' without a warning. */
1796 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1797 return false;
1798 exp = TREE_OPERAND (exp, 1);
1799 goto restart;
1801 case COND_EXPR:
1802 /* If this is an expression with side effects, don't warn; this
1803 case commonly appears in macro expansions. */
1804 if (TREE_SIDE_EFFECTS (exp))
1805 return false;
1806 goto warn;
1808 case INDIRECT_REF:
1809 /* Don't warn about automatic dereferencing of references, since
1810 the user cannot control it. */
1811 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1813 exp = TREE_OPERAND (exp, 0);
1814 goto restart;
1816 /* Fall through. */
1818 default:
1819 /* Referencing a volatile value is a side effect, so don't warn. */
1820 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1821 && TREE_THIS_VOLATILE (exp))
1822 return false;
1824 /* If this is an expression which has no operands, there is no value
1825 to be unused. There are no such language-independent codes,
1826 but front ends may define such. */
1827 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1828 return false;
1830 warn:
1831 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1836 /* Print a warning about casts that might indicate violation
1837 of strict aliasing rules if -Wstrict-aliasing is used and
1838 strict aliasing mode is in effect. OTYPE is the original
1839 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1841 bool
1842 strict_aliasing_warning (tree otype, tree type, tree expr)
1844 /* Strip pointer conversion chains and get to the correct original type. */
1845 STRIP_NOPS (expr);
1846 otype = TREE_TYPE (expr);
1848 if (!(flag_strict_aliasing
1849 && POINTER_TYPE_P (type)
1850 && POINTER_TYPE_P (otype)
1851 && !VOID_TYPE_P (TREE_TYPE (type)))
1852 /* If the type we are casting to is a ref-all pointer
1853 dereferencing it is always valid. */
1854 || TYPE_REF_CAN_ALIAS_ALL (type))
1855 return false;
1857 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1858 && (DECL_P (TREE_OPERAND (expr, 0))
1859 || handled_component_p (TREE_OPERAND (expr, 0))))
1861 /* Casting the address of an object to non void pointer. Warn
1862 if the cast breaks type based aliasing. */
1863 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1865 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1866 "might break strict-aliasing rules");
1867 return true;
1869 else
1871 /* warn_strict_aliasing >= 3. This includes the default (3).
1872 Only warn if the cast is dereferenced immediately. */
1873 alias_set_type set1 =
1874 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1875 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1877 if (set1 != set2 && set2 != 0
1878 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1880 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1881 "pointer will break strict-aliasing rules");
1882 return true;
1884 else if (warn_strict_aliasing == 2
1885 && !alias_sets_must_conflict_p (set1, set2))
1887 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1888 "pointer might break strict-aliasing rules");
1889 return true;
1893 else
1894 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1896 /* At this level, warn for any conversions, even if an address is
1897 not taken in the same statement. This will likely produce many
1898 false positives, but could be useful to pinpoint problems that
1899 are not revealed at higher levels. */
1900 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1901 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1902 if (!COMPLETE_TYPE_P (type)
1903 || !alias_sets_must_conflict_p (set1, set2))
1905 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1906 "pointer might break strict-aliasing rules");
1907 return true;
1911 return false;
1914 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1915 sizeof as last operand of certain builtins. */
1917 void
1918 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1919 vec<tree, va_gc> *params, tree *sizeof_arg,
1920 bool (*comp_types) (tree, tree))
1922 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1923 bool strop = false, cmp = false;
1924 unsigned int idx = ~0;
1925 location_t loc;
1927 if (TREE_CODE (callee) != FUNCTION_DECL
1928 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1929 || vec_safe_length (params) <= 1)
1930 return;
1932 switch (DECL_FUNCTION_CODE (callee))
1934 case BUILT_IN_STRNCMP:
1935 case BUILT_IN_STRNCASECMP:
1936 cmp = true;
1937 /* FALLTHRU */
1938 case BUILT_IN_STRNCPY:
1939 case BUILT_IN_STRNCPY_CHK:
1940 case BUILT_IN_STRNCAT:
1941 case BUILT_IN_STRNCAT_CHK:
1942 case BUILT_IN_STPNCPY:
1943 case BUILT_IN_STPNCPY_CHK:
1944 strop = true;
1945 /* FALLTHRU */
1946 case BUILT_IN_MEMCPY:
1947 case BUILT_IN_MEMCPY_CHK:
1948 case BUILT_IN_MEMMOVE:
1949 case BUILT_IN_MEMMOVE_CHK:
1950 if (params->length () < 3)
1951 return;
1952 src = (*params)[1];
1953 dest = (*params)[0];
1954 idx = 2;
1955 break;
1956 case BUILT_IN_BCOPY:
1957 if (params->length () < 3)
1958 return;
1959 src = (*params)[0];
1960 dest = (*params)[1];
1961 idx = 2;
1962 break;
1963 case BUILT_IN_MEMCMP:
1964 case BUILT_IN_BCMP:
1965 if (params->length () < 3)
1966 return;
1967 src = (*params)[1];
1968 dest = (*params)[0];
1969 idx = 2;
1970 cmp = true;
1971 break;
1972 case BUILT_IN_MEMSET:
1973 case BUILT_IN_MEMSET_CHK:
1974 if (params->length () < 3)
1975 return;
1976 dest = (*params)[0];
1977 idx = 2;
1978 break;
1979 case BUILT_IN_BZERO:
1980 dest = (*params)[0];
1981 idx = 1;
1982 break;
1983 case BUILT_IN_STRNDUP:
1984 src = (*params)[0];
1985 strop = true;
1986 idx = 1;
1987 break;
1988 case BUILT_IN_MEMCHR:
1989 if (params->length () < 3)
1990 return;
1991 src = (*params)[0];
1992 idx = 2;
1993 break;
1994 case BUILT_IN_SNPRINTF:
1995 case BUILT_IN_SNPRINTF_CHK:
1996 case BUILT_IN_VSNPRINTF:
1997 case BUILT_IN_VSNPRINTF_CHK:
1998 dest = (*params)[0];
1999 idx = 1;
2000 strop = true;
2001 break;
2002 default:
2003 break;
2006 if (idx >= 3)
2007 return;
2009 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2010 return;
2012 type = TYPE_P (sizeof_arg[idx])
2013 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2014 if (!POINTER_TYPE_P (type))
2015 return;
2017 if (dest
2018 && (tem = tree_strip_nop_conversions (dest))
2019 && POINTER_TYPE_P (TREE_TYPE (tem))
2020 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2021 return;
2023 if (src
2024 && (tem = tree_strip_nop_conversions (src))
2025 && POINTER_TYPE_P (TREE_TYPE (tem))
2026 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2027 return;
2029 loc = sizeof_arg_loc[idx];
2031 if (dest && !cmp)
2033 if (!TYPE_P (sizeof_arg[idx])
2034 && operand_equal_p (dest, sizeof_arg[idx], 0)
2035 && comp_types (TREE_TYPE (dest), type))
2037 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2038 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2039 "argument to %<sizeof%> in %qD call is the same "
2040 "expression as the destination; did you mean to "
2041 "remove the addressof?", callee);
2042 else if ((TYPE_PRECISION (TREE_TYPE (type))
2043 == TYPE_PRECISION (char_type_node))
2044 || strop)
2045 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2046 "argument to %<sizeof%> in %qD call is the same "
2047 "expression as the destination; did you mean to "
2048 "provide an explicit length?", callee);
2049 else
2050 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2051 "argument to %<sizeof%> in %qD call is the same "
2052 "expression as the destination; did you mean to "
2053 "dereference it?", callee);
2054 return;
2057 if (POINTER_TYPE_P (TREE_TYPE (dest))
2058 && !strop
2059 && comp_types (TREE_TYPE (dest), type)
2060 && !VOID_TYPE_P (TREE_TYPE (type)))
2062 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2063 "argument to %<sizeof%> in %qD call is the same "
2064 "pointer type %qT as the destination; expected %qT "
2065 "or an explicit length", callee, TREE_TYPE (dest),
2066 TREE_TYPE (TREE_TYPE (dest)));
2067 return;
2071 if (src && !cmp)
2073 if (!TYPE_P (sizeof_arg[idx])
2074 && operand_equal_p (src, sizeof_arg[idx], 0)
2075 && comp_types (TREE_TYPE (src), type))
2077 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2078 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2079 "argument to %<sizeof%> in %qD call is the same "
2080 "expression as the source; did you mean to "
2081 "remove the addressof?", callee);
2082 else if ((TYPE_PRECISION (TREE_TYPE (type))
2083 == TYPE_PRECISION (char_type_node))
2084 || strop)
2085 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2086 "argument to %<sizeof%> in %qD call is the same "
2087 "expression as the source; did you mean to "
2088 "provide an explicit length?", callee);
2089 else
2090 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2091 "argument to %<sizeof%> in %qD call is the same "
2092 "expression as the source; did you mean to "
2093 "dereference it?", callee);
2094 return;
2097 if (POINTER_TYPE_P (TREE_TYPE (src))
2098 && !strop
2099 && comp_types (TREE_TYPE (src), type)
2100 && !VOID_TYPE_P (TREE_TYPE (type)))
2102 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2103 "argument to %<sizeof%> in %qD call is the same "
2104 "pointer type %qT as the source; expected %qT "
2105 "or an explicit length", callee, TREE_TYPE (src),
2106 TREE_TYPE (TREE_TYPE (src)));
2107 return;
2111 if (dest)
2113 if (!TYPE_P (sizeof_arg[idx])
2114 && operand_equal_p (dest, sizeof_arg[idx], 0)
2115 && comp_types (TREE_TYPE (dest), type))
2117 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2118 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2119 "argument to %<sizeof%> in %qD call is the same "
2120 "expression as the first source; did you mean to "
2121 "remove the addressof?", callee);
2122 else if ((TYPE_PRECISION (TREE_TYPE (type))
2123 == TYPE_PRECISION (char_type_node))
2124 || strop)
2125 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2126 "argument to %<sizeof%> in %qD call is the same "
2127 "expression as the first source; did you mean to "
2128 "provide an explicit length?", callee);
2129 else
2130 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2131 "argument to %<sizeof%> in %qD call is the same "
2132 "expression as the first source; did you mean to "
2133 "dereference it?", callee);
2134 return;
2137 if (POINTER_TYPE_P (TREE_TYPE (dest))
2138 && !strop
2139 && comp_types (TREE_TYPE (dest), type)
2140 && !VOID_TYPE_P (TREE_TYPE (type)))
2142 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2143 "argument to %<sizeof%> in %qD call is the same "
2144 "pointer type %qT as the first source; expected %qT "
2145 "or an explicit length", callee, TREE_TYPE (dest),
2146 TREE_TYPE (TREE_TYPE (dest)));
2147 return;
2151 if (src)
2153 if (!TYPE_P (sizeof_arg[idx])
2154 && operand_equal_p (src, sizeof_arg[idx], 0)
2155 && comp_types (TREE_TYPE (src), type))
2157 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2158 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2159 "argument to %<sizeof%> in %qD call is the same "
2160 "expression as the second source; did you mean to "
2161 "remove the addressof?", callee);
2162 else if ((TYPE_PRECISION (TREE_TYPE (type))
2163 == TYPE_PRECISION (char_type_node))
2164 || strop)
2165 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2166 "argument to %<sizeof%> in %qD call is the same "
2167 "expression as the second source; did you mean to "
2168 "provide an explicit length?", callee);
2169 else
2170 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2171 "argument to %<sizeof%> in %qD call is the same "
2172 "expression as the second source; did you mean to "
2173 "dereference it?", callee);
2174 return;
2177 if (POINTER_TYPE_P (TREE_TYPE (src))
2178 && !strop
2179 && comp_types (TREE_TYPE (src), type)
2180 && !VOID_TYPE_P (TREE_TYPE (type)))
2182 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2183 "argument to %<sizeof%> in %qD call is the same "
2184 "pointer type %qT as the second source; expected %qT "
2185 "or an explicit length", callee, TREE_TYPE (src),
2186 TREE_TYPE (TREE_TYPE (src)));
2187 return;
2193 /* Warn for unlikely, improbable, or stupid DECL declarations
2194 of `main'. */
2196 void
2197 check_main_parameter_types (tree decl)
2199 function_args_iterator iter;
2200 tree type;
2201 int argct = 0;
2203 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2205 /* XXX void_type_node belies the abstraction. */
2206 if (type == void_type_node || type == error_mark_node )
2207 break;
2209 ++argct;
2210 switch (argct)
2212 case 1:
2213 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2214 pedwarn (input_location, OPT_Wmain,
2215 "first argument of %q+D should be %<int%>", decl);
2216 break;
2218 case 2:
2219 if (TREE_CODE (type) != POINTER_TYPE
2220 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2221 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2222 != char_type_node))
2223 pedwarn (input_location, OPT_Wmain,
2224 "second argument of %q+D should be %<char **%>", decl);
2225 break;
2227 case 3:
2228 if (TREE_CODE (type) != POINTER_TYPE
2229 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2230 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2231 != char_type_node))
2232 pedwarn (input_location, OPT_Wmain,
2233 "third argument of %q+D should probably be "
2234 "%<char **%>", decl);
2235 break;
2239 /* It is intentional that this message does not mention the third
2240 argument because it's only mentioned in an appendix of the
2241 standard. */
2242 if (argct > 0 && (argct < 2 || argct > 3))
2243 pedwarn (input_location, OPT_Wmain,
2244 "%q+D takes only zero or two arguments", decl);
2247 /* vector_targets_convertible_p is used for vector pointer types. The
2248 callers perform various checks that the qualifiers are satisfactory,
2249 while OTOH vector_targets_convertible_p ignores the number of elements
2250 in the vectors. That's fine with vector pointers as we can consider,
2251 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2252 and that does not require and conversion of the pointer values.
2253 In contrast, vector_types_convertible_p and
2254 vector_types_compatible_elements_p are used for vector value types. */
2255 /* True if pointers to distinct types T1 and T2 can be converted to
2256 each other without an explicit cast. Only returns true for opaque
2257 vector types. */
2258 bool
2259 vector_targets_convertible_p (const_tree t1, const_tree t2)
2261 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2262 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2263 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2264 return true;
2266 return false;
2269 /* vector_types_convertible_p is used for vector value types.
2270 It could in principle call vector_targets_convertible_p as a subroutine,
2271 but then the check for vector type would be duplicated with its callers,
2272 and also the purpose of vector_targets_convertible_p would become
2273 muddled.
2274 Where vector_types_convertible_p returns true, a conversion might still be
2275 needed to make the types match.
2276 In contrast, vector_targets_convertible_p is used for vector pointer
2277 values, and vector_types_compatible_elements_p is used specifically
2278 in the context for binary operators, as a check if use is possible without
2279 conversion. */
2280 /* True if vector types T1 and T2 can be converted to each other
2281 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2282 can only be converted with -flax-vector-conversions yet that is not
2283 in effect, emit a note telling the user about that option if such
2284 a note has not previously been emitted. */
2285 bool
2286 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2288 static bool emitted_lax_note = false;
2289 bool convertible_lax;
2291 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2292 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2293 return true;
2295 convertible_lax =
2296 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2297 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2298 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2299 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2300 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2302 if (!convertible_lax || flag_lax_vector_conversions)
2303 return convertible_lax;
2305 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2306 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2307 return true;
2309 if (emit_lax_note && !emitted_lax_note)
2311 emitted_lax_note = true;
2312 inform (input_location, "use -flax-vector-conversions to permit "
2313 "conversions between vectors with differing "
2314 "element types or numbers of subparts");
2317 return false;
2320 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2321 and have vector types, V0 has the same type as V1, and the number of
2322 elements of V0, V1, MASK is the same.
2324 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2325 called with two arguments. In this case implementation passes the
2326 first argument twice in order to share the same tree code. This fact
2327 could enable the mask-values being twice the vector length. This is
2328 an implementation accident and this semantics is not guaranteed to
2329 the user. */
2330 tree
2331 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2332 bool complain)
2334 tree ret;
2335 bool wrap = true;
2336 bool maybe_const = false;
2337 bool two_arguments = false;
2339 if (v1 == NULL_TREE)
2341 two_arguments = true;
2342 v1 = v0;
2345 if (v0 == error_mark_node || v1 == error_mark_node
2346 || mask == error_mark_node)
2347 return error_mark_node;
2349 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2350 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2352 if (complain)
2353 error_at (loc, "__builtin_shuffle last argument must "
2354 "be an integer vector");
2355 return error_mark_node;
2358 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2359 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2361 if (complain)
2362 error_at (loc, "__builtin_shuffle arguments must be vectors");
2363 return error_mark_node;
2366 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2368 if (complain)
2369 error_at (loc, "__builtin_shuffle argument vectors must be of "
2370 "the same type");
2371 return error_mark_node;
2374 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2375 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2376 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2377 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2379 if (complain)
2380 error_at (loc, "__builtin_shuffle number of elements of the "
2381 "argument vector(s) and the mask vector should "
2382 "be the same");
2383 return error_mark_node;
2386 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2387 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2389 if (complain)
2390 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2391 "must have the same size as inner type of the mask");
2392 return error_mark_node;
2395 if (!c_dialect_cxx ())
2397 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2398 v0 = c_fully_fold (v0, false, &maybe_const);
2399 wrap &= maybe_const;
2401 if (two_arguments)
2402 v1 = v0 = save_expr (v0);
2403 else
2405 v1 = c_fully_fold (v1, false, &maybe_const);
2406 wrap &= maybe_const;
2409 mask = c_fully_fold (mask, false, &maybe_const);
2410 wrap &= maybe_const;
2412 else if (two_arguments)
2413 v1 = v0 = save_expr (v0);
2415 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2417 if (!c_dialect_cxx () && !wrap)
2418 ret = c_wrap_maybe_const (ret, true);
2420 return ret;
2423 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2424 to integral type. */
2426 static tree
2427 c_common_get_narrower (tree op, int *unsignedp_ptr)
2429 op = get_narrower (op, unsignedp_ptr);
2431 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2432 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2434 /* C++0x scoped enumerations don't implicitly convert to integral
2435 type; if we stripped an explicit conversion to a larger type we
2436 need to replace it so common_type will still work. */
2437 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2438 TYPE_UNSIGNED (TREE_TYPE (op)));
2439 op = fold_convert (type, op);
2441 return op;
2444 /* This is a helper function of build_binary_op.
2446 For certain operations if both args were extended from the same
2447 smaller type, do the arithmetic in that type and then extend.
2449 BITWISE indicates a bitwise operation.
2450 For them, this optimization is safe only if
2451 both args are zero-extended or both are sign-extended.
2452 Otherwise, we might change the result.
2453 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2454 but calculated in (unsigned short) it would be (unsigned short)-1.
2456 tree
2457 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2459 int unsigned0, unsigned1;
2460 tree arg0, arg1;
2461 int uns;
2462 tree type;
2464 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2465 excessive narrowing when we call get_narrower below. For
2466 example, suppose that OP0 is of unsigned int extended
2467 from signed char and that RESULT_TYPE is long long int.
2468 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2469 like
2471 (long long int) (unsigned int) signed_char
2473 which get_narrower would narrow down to
2475 (unsigned int) signed char
2477 If we do not cast OP0 first, get_narrower would return
2478 signed_char, which is inconsistent with the case of the
2479 explicit cast. */
2480 op0 = convert (result_type, op0);
2481 op1 = convert (result_type, op1);
2483 arg0 = c_common_get_narrower (op0, &unsigned0);
2484 arg1 = c_common_get_narrower (op1, &unsigned1);
2486 /* UNS is 1 if the operation to be done is an unsigned one. */
2487 uns = TYPE_UNSIGNED (result_type);
2489 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2490 but it *requires* conversion to FINAL_TYPE. */
2492 if ((TYPE_PRECISION (TREE_TYPE (op0))
2493 == TYPE_PRECISION (TREE_TYPE (arg0)))
2494 && TREE_TYPE (op0) != result_type)
2495 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2496 if ((TYPE_PRECISION (TREE_TYPE (op1))
2497 == TYPE_PRECISION (TREE_TYPE (arg1)))
2498 && TREE_TYPE (op1) != result_type)
2499 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2501 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2503 /* For bitwise operations, signedness of nominal type
2504 does not matter. Consider only how operands were extended. */
2505 if (bitwise)
2506 uns = unsigned0;
2508 /* Note that in all three cases below we refrain from optimizing
2509 an unsigned operation on sign-extended args.
2510 That would not be valid. */
2512 /* Both args variable: if both extended in same way
2513 from same width, do it in that width.
2514 Do it unsigned if args were zero-extended. */
2515 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2516 < TYPE_PRECISION (result_type))
2517 && (TYPE_PRECISION (TREE_TYPE (arg1))
2518 == TYPE_PRECISION (TREE_TYPE (arg0)))
2519 && unsigned0 == unsigned1
2520 && (unsigned0 || !uns))
2521 return c_common_signed_or_unsigned_type
2522 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2524 else if (TREE_CODE (arg0) == INTEGER_CST
2525 && (unsigned1 || !uns)
2526 && (TYPE_PRECISION (TREE_TYPE (arg1))
2527 < TYPE_PRECISION (result_type))
2528 && (type
2529 = c_common_signed_or_unsigned_type (unsigned1,
2530 TREE_TYPE (arg1)))
2531 && !POINTER_TYPE_P (type)
2532 && int_fits_type_p (arg0, type))
2533 return type;
2535 else if (TREE_CODE (arg1) == INTEGER_CST
2536 && (unsigned0 || !uns)
2537 && (TYPE_PRECISION (TREE_TYPE (arg0))
2538 < TYPE_PRECISION (result_type))
2539 && (type
2540 = c_common_signed_or_unsigned_type (unsigned0,
2541 TREE_TYPE (arg0)))
2542 && !POINTER_TYPE_P (type)
2543 && int_fits_type_p (arg1, type))
2544 return type;
2546 return result_type;
2549 /* Checks if expression EXPR of real/integer type cannot be converted
2550 to the real/integer type TYPE. Function returns non-zero when:
2551 * EXPR is a constant which cannot be exactly converted to TYPE.
2552 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2553 for EXPR type and TYPE being both integers or both real.
2554 * EXPR is not a constant of real type and TYPE is an integer.
2555 * EXPR is not a constant of integer type which cannot be
2556 exactly converted to real type.
2557 Function allows conversions between types of different signedness and
2558 can return SAFE_CONVERSION (zero) in that case. Function can produce
2559 signedness warnings if PRODUCE_WARNS is true. */
2561 enum conversion_safety
2562 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2564 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2565 tree expr_type = TREE_TYPE (expr);
2566 loc = expansion_point_location_if_in_system_header (loc);
2568 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2570 /* Warn for real constant that is not an exact integer converted
2571 to integer type. */
2572 if (TREE_CODE (expr_type) == REAL_TYPE
2573 && TREE_CODE (type) == INTEGER_TYPE)
2575 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2576 give_warning = UNSAFE_REAL;
2578 /* Warn for an integer constant that does not fit into integer type. */
2579 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2580 && TREE_CODE (type) == INTEGER_TYPE
2581 && !int_fits_type_p (expr, type))
2583 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2584 && tree_int_cst_sgn (expr) < 0)
2586 if (produce_warns)
2587 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2588 " implicitly converted to unsigned type");
2590 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2592 if (produce_warns)
2593 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2594 " constant value to negative integer");
2596 else
2597 give_warning = UNSAFE_OTHER;
2599 else if (TREE_CODE (type) == REAL_TYPE)
2601 /* Warn for an integer constant that does not fit into real type. */
2602 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2604 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2605 if (!exact_real_truncate (TYPE_MODE (type), &a))
2606 give_warning = UNSAFE_REAL;
2608 /* Warn for a real constant that does not fit into a smaller
2609 real type. */
2610 else if (TREE_CODE (expr_type) == REAL_TYPE
2611 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2613 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2614 if (!exact_real_truncate (TYPE_MODE (type), &a))
2615 give_warning = UNSAFE_REAL;
2619 else
2621 /* Warn for real types converted to integer types. */
2622 if (TREE_CODE (expr_type) == REAL_TYPE
2623 && TREE_CODE (type) == INTEGER_TYPE)
2624 give_warning = UNSAFE_REAL;
2626 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2627 && TREE_CODE (type) == INTEGER_TYPE)
2629 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2630 expr = get_unwidened (expr, 0);
2631 expr_type = TREE_TYPE (expr);
2633 /* Don't warn for short y; short x = ((int)y & 0xff); */
2634 if (TREE_CODE (expr) == BIT_AND_EXPR
2635 || TREE_CODE (expr) == BIT_IOR_EXPR
2636 || TREE_CODE (expr) == BIT_XOR_EXPR)
2638 /* If both args were extended from a shortest type,
2639 use that type if that is safe. */
2640 expr_type = shorten_binary_op (expr_type,
2641 TREE_OPERAND (expr, 0),
2642 TREE_OPERAND (expr, 1),
2643 /* bitwise */1);
2645 if (TREE_CODE (expr) == BIT_AND_EXPR)
2647 tree op0 = TREE_OPERAND (expr, 0);
2648 tree op1 = TREE_OPERAND (expr, 1);
2649 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2650 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2652 /* If one of the operands is a non-negative constant
2653 that fits in the target type, then the type of the
2654 other operand does not matter. */
2655 if ((TREE_CODE (op0) == INTEGER_CST
2656 && int_fits_type_p (op0, c_common_signed_type (type))
2657 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2658 || (TREE_CODE (op1) == INTEGER_CST
2659 && int_fits_type_p (op1, c_common_signed_type (type))
2660 && int_fits_type_p (op1,
2661 c_common_unsigned_type (type))))
2662 return SAFE_CONVERSION;
2663 /* If constant is unsigned and fits in the target
2664 type, then the result will also fit. */
2665 else if ((TREE_CODE (op0) == INTEGER_CST
2666 && unsigned0
2667 && int_fits_type_p (op0, type))
2668 || (TREE_CODE (op1) == INTEGER_CST
2669 && unsigned1
2670 && int_fits_type_p (op1, type)))
2671 return SAFE_CONVERSION;
2674 /* Warn for integer types converted to smaller integer types. */
2675 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2676 give_warning = UNSAFE_OTHER;
2678 /* When they are the same width but different signedness,
2679 then the value may change. */
2680 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2681 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2682 /* Even when converted to a bigger type, if the type is
2683 unsigned but expr is signed, then negative values
2684 will be changed. */
2685 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2686 && produce_warns)
2687 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2688 "may change the sign of the result",
2689 type, expr_type);
2692 /* Warn for integer types converted to real types if and only if
2693 all the range of values of the integer type cannot be
2694 represented by the real type. */
2695 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2696 && TREE_CODE (type) == REAL_TYPE)
2698 tree type_low_bound, type_high_bound;
2699 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2701 /* Don't warn about char y = 0xff; float x = (int) y; */
2702 expr = get_unwidened (expr, 0);
2703 expr_type = TREE_TYPE (expr);
2705 type_low_bound = TYPE_MIN_VALUE (expr_type);
2706 type_high_bound = TYPE_MAX_VALUE (expr_type);
2707 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2708 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2710 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2711 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2712 give_warning = UNSAFE_OTHER;
2715 /* Warn for real types converted to smaller real types. */
2716 else if (TREE_CODE (expr_type) == REAL_TYPE
2717 && TREE_CODE (type) == REAL_TYPE
2718 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2719 give_warning = UNSAFE_REAL;
2722 return give_warning;
2725 /* Warns if the conversion of EXPR to TYPE may alter a value.
2726 This is a helper function for warnings_for_convert_and_check. */
2728 static void
2729 conversion_warning (location_t loc, tree type, tree expr)
2731 tree expr_type = TREE_TYPE (expr);
2732 enum conversion_safety conversion_kind;
2734 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2735 return;
2737 /* This may happen, because for LHS op= RHS we preevaluate
2738 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2739 means we could no longer see the code of the EXPR. */
2740 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2741 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2742 if (TREE_CODE (expr) == SAVE_EXPR)
2743 expr = TREE_OPERAND (expr, 0);
2745 switch (TREE_CODE (expr))
2747 case EQ_EXPR:
2748 case NE_EXPR:
2749 case LE_EXPR:
2750 case GE_EXPR:
2751 case LT_EXPR:
2752 case GT_EXPR:
2753 case TRUTH_ANDIF_EXPR:
2754 case TRUTH_ORIF_EXPR:
2755 case TRUTH_AND_EXPR:
2756 case TRUTH_OR_EXPR:
2757 case TRUTH_XOR_EXPR:
2758 case TRUTH_NOT_EXPR:
2759 /* Conversion from boolean to a signed:1 bit-field (which only
2760 can hold the values 0 and -1) doesn't lose information - but
2761 it does change the value. */
2762 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2763 warning_at (loc, OPT_Wconversion,
2764 "conversion to %qT from boolean expression", type);
2765 return;
2767 case REAL_CST:
2768 case INTEGER_CST:
2769 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2770 if (conversion_kind == UNSAFE_REAL)
2771 warning_at (loc, OPT_Wfloat_conversion,
2772 "conversion to %qT alters %qT constant value",
2773 type, expr_type);
2774 else if (conversion_kind)
2775 warning_at (loc, OPT_Wconversion,
2776 "conversion to %qT alters %qT constant value",
2777 type, expr_type);
2778 return;
2780 case COND_EXPR:
2782 /* In case of COND_EXPR, we do not care about the type of
2783 COND_EXPR, only about the conversion of each operand. */
2784 tree op1 = TREE_OPERAND (expr, 1);
2785 tree op2 = TREE_OPERAND (expr, 2);
2787 conversion_warning (loc, type, op1);
2788 conversion_warning (loc, type, op2);
2789 return;
2792 default: /* 'expr' is not a constant. */
2793 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2794 if (conversion_kind == UNSAFE_REAL)
2795 warning_at (loc, OPT_Wfloat_conversion,
2796 "conversion to %qT from %qT may alter its value",
2797 type, expr_type);
2798 else if (conversion_kind)
2799 warning_at (loc, OPT_Wconversion,
2800 "conversion to %qT from %qT may alter its value",
2801 type, expr_type);
2805 /* Produce warnings after a conversion. RESULT is the result of
2806 converting EXPR to TYPE. This is a helper function for
2807 convert_and_check and cp_convert_and_check. */
2809 void
2810 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2811 tree result)
2813 loc = expansion_point_location_if_in_system_header (loc);
2815 if (TREE_CODE (expr) == INTEGER_CST
2816 && (TREE_CODE (type) == INTEGER_TYPE
2817 || TREE_CODE (type) == ENUMERAL_TYPE)
2818 && !int_fits_type_p (expr, type))
2820 /* Do not diagnose overflow in a constant expression merely
2821 because a conversion overflowed. */
2822 if (TREE_OVERFLOW (result))
2823 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2825 if (TYPE_UNSIGNED (type))
2827 /* This detects cases like converting -129 or 256 to
2828 unsigned char. */
2829 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2830 warning_at (loc, OPT_Woverflow,
2831 "large integer implicitly truncated to unsigned type");
2832 else
2833 conversion_warning (loc, type, expr);
2835 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2836 warning_at (loc, OPT_Woverflow,
2837 "overflow in implicit constant conversion");
2838 /* No warning for converting 0x80000000 to int. */
2839 else if (pedantic
2840 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2841 || TYPE_PRECISION (TREE_TYPE (expr))
2842 != TYPE_PRECISION (type)))
2843 warning_at (loc, OPT_Woverflow,
2844 "overflow in implicit constant conversion");
2846 else
2847 conversion_warning (loc, type, expr);
2849 else if ((TREE_CODE (result) == INTEGER_CST
2850 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2851 warning_at (loc, OPT_Woverflow,
2852 "overflow in implicit constant conversion");
2853 else
2854 conversion_warning (loc, type, expr);
2858 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2859 Invoke this function on every expression that is converted implicitly,
2860 i.e. because of language rules and not because of an explicit cast. */
2862 tree
2863 convert_and_check (location_t loc, tree type, tree expr)
2865 tree result;
2866 tree expr_for_warning;
2868 /* Convert from a value with possible excess precision rather than
2869 via the semantic type, but do not warn about values not fitting
2870 exactly in the semantic type. */
2871 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2873 tree orig_type = TREE_TYPE (expr);
2874 expr = TREE_OPERAND (expr, 0);
2875 expr_for_warning = convert (orig_type, expr);
2876 if (orig_type == type)
2877 return expr_for_warning;
2879 else
2880 expr_for_warning = expr;
2882 if (TREE_TYPE (expr) == type)
2883 return expr;
2885 result = convert (type, expr);
2887 if (c_inhibit_evaluation_warnings == 0
2888 && !TREE_OVERFLOW_P (expr)
2889 && result != error_mark_node)
2890 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2892 return result;
2895 /* A node in a list that describes references to variables (EXPR), which are
2896 either read accesses if WRITER is zero, or write accesses, in which case
2897 WRITER is the parent of EXPR. */
2898 struct tlist
2900 struct tlist *next;
2901 tree expr, writer;
2904 /* Used to implement a cache the results of a call to verify_tree. We only
2905 use this for SAVE_EXPRs. */
2906 struct tlist_cache
2908 struct tlist_cache *next;
2909 struct tlist *cache_before_sp;
2910 struct tlist *cache_after_sp;
2911 tree expr;
2914 /* Obstack to use when allocating tlist structures, and corresponding
2915 firstobj. */
2916 static struct obstack tlist_obstack;
2917 static char *tlist_firstobj = 0;
2919 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2920 warnings. */
2921 static struct tlist *warned_ids;
2922 /* SAVE_EXPRs need special treatment. We process them only once and then
2923 cache the results. */
2924 static struct tlist_cache *save_expr_cache;
2926 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2927 static void merge_tlist (struct tlist **, struct tlist *, int);
2928 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2929 static int warning_candidate_p (tree);
2930 static bool candidate_equal_p (const_tree, const_tree);
2931 static void warn_for_collisions (struct tlist *);
2932 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2933 static struct tlist *new_tlist (struct tlist *, tree, tree);
2935 /* Create a new struct tlist and fill in its fields. */
2936 static struct tlist *
2937 new_tlist (struct tlist *next, tree t, tree writer)
2939 struct tlist *l;
2940 l = XOBNEW (&tlist_obstack, struct tlist);
2941 l->next = next;
2942 l->expr = t;
2943 l->writer = writer;
2944 return l;
2947 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2948 is nonnull, we ignore any node we find which has a writer equal to it. */
2950 static void
2951 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2953 while (add)
2955 struct tlist *next = add->next;
2956 if (!copy)
2957 add->next = *to;
2958 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2959 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2960 add = next;
2964 /* Merge the nodes of ADD into TO. This merging process is done so that for
2965 each variable that already exists in TO, no new node is added; however if
2966 there is a write access recorded in ADD, and an occurrence on TO is only
2967 a read access, then the occurrence in TO will be modified to record the
2968 write. */
2970 static void
2971 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2973 struct tlist **end = to;
2975 while (*end)
2976 end = &(*end)->next;
2978 while (add)
2980 int found = 0;
2981 struct tlist *tmp2;
2982 struct tlist *next = add->next;
2984 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2985 if (candidate_equal_p (tmp2->expr, add->expr))
2987 found = 1;
2988 if (!tmp2->writer)
2989 tmp2->writer = add->writer;
2991 if (!found)
2993 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2994 end = &(*end)->next;
2995 *end = 0;
2997 add = next;
3001 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3002 references in list LIST conflict with it, excluding reads if ONLY writers
3003 is nonzero. */
3005 static void
3006 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3007 int only_writes)
3009 struct tlist *tmp;
3011 /* Avoid duplicate warnings. */
3012 for (tmp = warned_ids; tmp; tmp = tmp->next)
3013 if (candidate_equal_p (tmp->expr, written))
3014 return;
3016 while (list)
3018 if (candidate_equal_p (list->expr, written)
3019 && !candidate_equal_p (list->writer, writer)
3020 && (!only_writes || list->writer))
3022 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3023 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3024 OPT_Wsequence_point, "operation on %qE may be undefined",
3025 list->expr);
3027 list = list->next;
3031 /* Given a list LIST of references to variables, find whether any of these
3032 can cause conflicts due to missing sequence points. */
3034 static void
3035 warn_for_collisions (struct tlist *list)
3037 struct tlist *tmp;
3039 for (tmp = list; tmp; tmp = tmp->next)
3041 if (tmp->writer)
3042 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3046 /* Return nonzero if X is a tree that can be verified by the sequence point
3047 warnings. */
3048 static int
3049 warning_candidate_p (tree x)
3051 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3052 return 0;
3054 if (TREE_CODE (x) == BLOCK)
3055 return 0;
3057 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3058 (lvalue_p) crash on TRY/CATCH. */
3059 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3060 return 0;
3062 if (!lvalue_p (x))
3063 return 0;
3065 /* No point to track non-const calls, they will never satisfy
3066 operand_equal_p. */
3067 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3068 return 0;
3070 if (TREE_CODE (x) == STRING_CST)
3071 return 0;
3073 return 1;
3076 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3077 static bool
3078 candidate_equal_p (const_tree x, const_tree y)
3080 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3083 /* Walk the tree X, and record accesses to variables. If X is written by the
3084 parent tree, WRITER is the parent.
3085 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3086 expression or its only operand forces a sequence point, then everything up
3087 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3088 in PNO_SP.
3089 Once we return, we will have emitted warnings if any subexpression before
3090 such a sequence point could be undefined. On a higher level, however, the
3091 sequence point may not be relevant, and we'll merge the two lists.
3093 Example: (b++, a) + b;
3094 The call that processes the COMPOUND_EXPR will store the increment of B
3095 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3096 processes the PLUS_EXPR will need to merge the two lists so that
3097 eventually, all accesses end up on the same list (and we'll warn about the
3098 unordered subexpressions b++ and b.
3100 A note on merging. If we modify the former example so that our expression
3101 becomes
3102 (b++, b) + a
3103 care must be taken not simply to add all three expressions into the final
3104 PNO_SP list. The function merge_tlist takes care of that by merging the
3105 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3106 way, so that no more than one access to B is recorded. */
3108 static void
3109 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3110 tree writer)
3112 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3113 enum tree_code code;
3114 enum tree_code_class cl;
3116 /* X may be NULL if it is the operand of an empty statement expression
3117 ({ }). */
3118 if (x == NULL)
3119 return;
3121 restart:
3122 code = TREE_CODE (x);
3123 cl = TREE_CODE_CLASS (code);
3125 if (warning_candidate_p (x))
3126 *pno_sp = new_tlist (*pno_sp, x, writer);
3128 switch (code)
3130 case CONSTRUCTOR:
3131 case SIZEOF_EXPR:
3132 return;
3134 case COMPOUND_EXPR:
3135 case TRUTH_ANDIF_EXPR:
3136 case TRUTH_ORIF_EXPR:
3137 tmp_before = tmp_nosp = tmp_list3 = 0;
3138 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3139 warn_for_collisions (tmp_nosp);
3140 merge_tlist (pbefore_sp, tmp_before, 0);
3141 merge_tlist (pbefore_sp, tmp_nosp, 0);
3142 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3143 merge_tlist (pbefore_sp, tmp_list3, 0);
3144 return;
3146 case COND_EXPR:
3147 tmp_before = tmp_list2 = 0;
3148 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3149 warn_for_collisions (tmp_list2);
3150 merge_tlist (pbefore_sp, tmp_before, 0);
3151 merge_tlist (pbefore_sp, tmp_list2, 0);
3153 tmp_list3 = tmp_nosp = 0;
3154 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3155 warn_for_collisions (tmp_nosp);
3156 merge_tlist (pbefore_sp, tmp_list3, 0);
3158 tmp_list3 = tmp_list2 = 0;
3159 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3160 warn_for_collisions (tmp_list2);
3161 merge_tlist (pbefore_sp, tmp_list3, 0);
3162 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3163 two first, to avoid warning for (a ? b++ : b++). */
3164 merge_tlist (&tmp_nosp, tmp_list2, 0);
3165 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3166 return;
3168 case PREDECREMENT_EXPR:
3169 case PREINCREMENT_EXPR:
3170 case POSTDECREMENT_EXPR:
3171 case POSTINCREMENT_EXPR:
3172 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3173 return;
3175 case MODIFY_EXPR:
3176 tmp_before = tmp_nosp = tmp_list3 = 0;
3177 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3178 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3179 /* Expressions inside the LHS are not ordered wrt. the sequence points
3180 in the RHS. Example:
3181 *a = (a++, 2)
3182 Despite the fact that the modification of "a" is in the before_sp
3183 list (tmp_before), it conflicts with the use of "a" in the LHS.
3184 We can handle this by adding the contents of tmp_list3
3185 to those of tmp_before, and redoing the collision warnings for that
3186 list. */
3187 add_tlist (&tmp_before, tmp_list3, x, 1);
3188 warn_for_collisions (tmp_before);
3189 /* Exclude the LHS itself here; we first have to merge it into the
3190 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3191 didn't exclude the LHS, we'd get it twice, once as a read and once
3192 as a write. */
3193 add_tlist (pno_sp, tmp_list3, x, 0);
3194 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3196 merge_tlist (pbefore_sp, tmp_before, 0);
3197 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3198 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3199 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3200 return;
3202 case CALL_EXPR:
3203 /* We need to warn about conflicts among arguments and conflicts between
3204 args and the function address. Side effects of the function address,
3205 however, are not ordered by the sequence point of the call. */
3207 call_expr_arg_iterator iter;
3208 tree arg;
3209 tmp_before = tmp_nosp = 0;
3210 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3211 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3213 tmp_list2 = tmp_list3 = 0;
3214 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3215 merge_tlist (&tmp_list3, tmp_list2, 0);
3216 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3218 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3219 warn_for_collisions (tmp_before);
3220 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3221 return;
3224 case TREE_LIST:
3225 /* Scan all the list, e.g. indices of multi dimensional array. */
3226 while (x)
3228 tmp_before = tmp_nosp = 0;
3229 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3230 merge_tlist (&tmp_nosp, tmp_before, 0);
3231 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3232 x = TREE_CHAIN (x);
3234 return;
3236 case SAVE_EXPR:
3238 struct tlist_cache *t;
3239 for (t = save_expr_cache; t; t = t->next)
3240 if (candidate_equal_p (t->expr, x))
3241 break;
3243 if (!t)
3245 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3246 t->next = save_expr_cache;
3247 t->expr = x;
3248 save_expr_cache = t;
3250 tmp_before = tmp_nosp = 0;
3251 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3252 warn_for_collisions (tmp_nosp);
3254 tmp_list3 = 0;
3255 merge_tlist (&tmp_list3, tmp_nosp, 0);
3256 t->cache_before_sp = tmp_before;
3257 t->cache_after_sp = tmp_list3;
3259 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3260 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3261 return;
3264 case ADDR_EXPR:
3265 x = TREE_OPERAND (x, 0);
3266 if (DECL_P (x))
3267 return;
3268 writer = 0;
3269 goto restart;
3271 default:
3272 /* For other expressions, simply recurse on their operands.
3273 Manual tail recursion for unary expressions.
3274 Other non-expressions need not be processed. */
3275 if (cl == tcc_unary)
3277 x = TREE_OPERAND (x, 0);
3278 writer = 0;
3279 goto restart;
3281 else if (IS_EXPR_CODE_CLASS (cl))
3283 int lp;
3284 int max = TREE_OPERAND_LENGTH (x);
3285 for (lp = 0; lp < max; lp++)
3287 tmp_before = tmp_nosp = 0;
3288 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3289 merge_tlist (&tmp_nosp, tmp_before, 0);
3290 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3293 return;
3297 /* Try to warn for undefined behavior in EXPR due to missing sequence
3298 points. */
3300 DEBUG_FUNCTION void
3301 verify_sequence_points (tree expr)
3303 struct tlist *before_sp = 0, *after_sp = 0;
3305 warned_ids = 0;
3306 save_expr_cache = 0;
3307 if (tlist_firstobj == 0)
3309 gcc_obstack_init (&tlist_obstack);
3310 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3313 verify_tree (expr, &before_sp, &after_sp, 0);
3314 warn_for_collisions (after_sp);
3315 obstack_free (&tlist_obstack, tlist_firstobj);
3318 /* Validate the expression after `case' and apply default promotions. */
3320 static tree
3321 check_case_value (tree value)
3323 if (value == NULL_TREE)
3324 return value;
3326 if (TREE_CODE (value) == INTEGER_CST)
3327 /* Promote char or short to int. */
3328 value = perform_integral_promotions (value);
3329 else if (value != error_mark_node)
3331 error ("case label does not reduce to an integer constant");
3332 value = error_mark_node;
3335 constant_expression_warning (value);
3337 return value;
3340 /* See if the case values LOW and HIGH are in the range of the original
3341 type (i.e. before the default conversion to int) of the switch testing
3342 expression.
3343 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3344 the type before promoting it. CASE_LOW_P is a pointer to the lower
3345 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3346 if the case is not a case range.
3347 The caller has to make sure that we are not called with NULL for
3348 CASE_LOW_P (i.e. the default case).
3349 Returns true if the case label is in range of ORIG_TYPE (saturated or
3350 untouched) or false if the label is out of range. */
3352 static bool
3353 check_case_bounds (tree type, tree orig_type,
3354 tree *case_low_p, tree *case_high_p)
3356 tree min_value, max_value;
3357 tree case_low = *case_low_p;
3358 tree case_high = case_high_p ? *case_high_p : case_low;
3360 /* If there was a problem with the original type, do nothing. */
3361 if (orig_type == error_mark_node)
3362 return true;
3364 min_value = TYPE_MIN_VALUE (orig_type);
3365 max_value = TYPE_MAX_VALUE (orig_type);
3367 /* Case label is less than minimum for type. */
3368 if (tree_int_cst_compare (case_low, min_value) < 0
3369 && tree_int_cst_compare (case_high, min_value) < 0)
3371 warning (0, "case label value is less than minimum value for type");
3372 return false;
3375 /* Case value is greater than maximum for type. */
3376 if (tree_int_cst_compare (case_low, max_value) > 0
3377 && tree_int_cst_compare (case_high, max_value) > 0)
3379 warning (0, "case label value exceeds maximum value for type");
3380 return false;
3383 /* Saturate lower case label value to minimum. */
3384 if (tree_int_cst_compare (case_high, min_value) >= 0
3385 && tree_int_cst_compare (case_low, min_value) < 0)
3387 warning (0, "lower value in case label range"
3388 " less than minimum value for type");
3389 case_low = min_value;
3392 /* Saturate upper case label value to maximum. */
3393 if (tree_int_cst_compare (case_low, max_value) <= 0
3394 && tree_int_cst_compare (case_high, max_value) > 0)
3396 warning (0, "upper value in case label range"
3397 " exceeds maximum value for type");
3398 case_high = max_value;
3401 if (*case_low_p != case_low)
3402 *case_low_p = convert (type, case_low);
3403 if (case_high_p && *case_high_p != case_high)
3404 *case_high_p = convert (type, case_high);
3406 return true;
3409 /* Return an integer type with BITS bits of precision,
3410 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3412 tree
3413 c_common_type_for_size (unsigned int bits, int unsignedp)
3415 if (bits == TYPE_PRECISION (integer_type_node))
3416 return unsignedp ? unsigned_type_node : integer_type_node;
3418 if (bits == TYPE_PRECISION (signed_char_type_node))
3419 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3421 if (bits == TYPE_PRECISION (short_integer_type_node))
3422 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3424 if (bits == TYPE_PRECISION (long_integer_type_node))
3425 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3427 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3428 return (unsignedp ? long_long_unsigned_type_node
3429 : long_long_integer_type_node);
3431 if (int128_integer_type_node
3432 && bits == TYPE_PRECISION (int128_integer_type_node))
3433 return (unsignedp ? int128_unsigned_type_node
3434 : int128_integer_type_node);
3436 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3437 return (unsignedp ? widest_unsigned_literal_type_node
3438 : widest_integer_literal_type_node);
3440 if (bits <= TYPE_PRECISION (intQI_type_node))
3441 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3443 if (bits <= TYPE_PRECISION (intHI_type_node))
3444 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3446 if (bits <= TYPE_PRECISION (intSI_type_node))
3447 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3449 if (bits <= TYPE_PRECISION (intDI_type_node))
3450 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3452 return 0;
3455 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3456 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3457 and saturating if SATP is nonzero, otherwise not saturating. */
3459 tree
3460 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3461 int unsignedp, int satp)
3463 enum machine_mode mode;
3464 if (ibit == 0)
3465 mode = unsignedp ? UQQmode : QQmode;
3466 else
3467 mode = unsignedp ? UHAmode : HAmode;
3469 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3470 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3471 break;
3473 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3475 sorry ("GCC cannot support operators with integer types and "
3476 "fixed-point types that have too many integral and "
3477 "fractional bits together");
3478 return 0;
3481 return c_common_type_for_mode (mode, satp);
3484 /* Used for communication between c_common_type_for_mode and
3485 c_register_builtin_type. */
3486 static GTY(()) tree registered_builtin_types;
3488 /* Return a data type that has machine mode MODE.
3489 If the mode is an integer,
3490 then UNSIGNEDP selects between signed and unsigned types.
3491 If the mode is a fixed-point mode,
3492 then UNSIGNEDP selects between saturating and nonsaturating types. */
3494 tree
3495 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3497 tree t;
3499 if (mode == TYPE_MODE (integer_type_node))
3500 return unsignedp ? unsigned_type_node : integer_type_node;
3502 if (mode == TYPE_MODE (signed_char_type_node))
3503 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3505 if (mode == TYPE_MODE (short_integer_type_node))
3506 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3508 if (mode == TYPE_MODE (long_integer_type_node))
3509 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3511 if (mode == TYPE_MODE (long_long_integer_type_node))
3512 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3514 if (int128_integer_type_node
3515 && mode == TYPE_MODE (int128_integer_type_node))
3516 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3518 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3519 return unsignedp ? widest_unsigned_literal_type_node
3520 : widest_integer_literal_type_node;
3522 if (mode == QImode)
3523 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3525 if (mode == HImode)
3526 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3528 if (mode == SImode)
3529 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3531 if (mode == DImode)
3532 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3534 #if HOST_BITS_PER_WIDE_INT >= 64
3535 if (mode == TYPE_MODE (intTI_type_node))
3536 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3537 #endif
3539 if (mode == TYPE_MODE (float_type_node))
3540 return float_type_node;
3542 if (mode == TYPE_MODE (double_type_node))
3543 return double_type_node;
3545 if (mode == TYPE_MODE (long_double_type_node))
3546 return long_double_type_node;
3548 if (mode == TYPE_MODE (void_type_node))
3549 return void_type_node;
3551 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3552 return (unsignedp
3553 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3554 : make_signed_type (GET_MODE_PRECISION (mode)));
3556 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3557 return (unsignedp
3558 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3559 : make_signed_type (GET_MODE_PRECISION (mode)));
3561 if (COMPLEX_MODE_P (mode))
3563 enum machine_mode inner_mode;
3564 tree inner_type;
3566 if (mode == TYPE_MODE (complex_float_type_node))
3567 return complex_float_type_node;
3568 if (mode == TYPE_MODE (complex_double_type_node))
3569 return complex_double_type_node;
3570 if (mode == TYPE_MODE (complex_long_double_type_node))
3571 return complex_long_double_type_node;
3573 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3574 return complex_integer_type_node;
3576 inner_mode = GET_MODE_INNER (mode);
3577 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3578 if (inner_type != NULL_TREE)
3579 return build_complex_type (inner_type);
3581 else if (VECTOR_MODE_P (mode))
3583 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3584 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3585 if (inner_type != NULL_TREE)
3586 return build_vector_type_for_mode (inner_type, mode);
3589 if (mode == TYPE_MODE (dfloat32_type_node))
3590 return dfloat32_type_node;
3591 if (mode == TYPE_MODE (dfloat64_type_node))
3592 return dfloat64_type_node;
3593 if (mode == TYPE_MODE (dfloat128_type_node))
3594 return dfloat128_type_node;
3596 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3598 if (mode == TYPE_MODE (short_fract_type_node))
3599 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3600 if (mode == TYPE_MODE (fract_type_node))
3601 return unsignedp ? sat_fract_type_node : fract_type_node;
3602 if (mode == TYPE_MODE (long_fract_type_node))
3603 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3604 if (mode == TYPE_MODE (long_long_fract_type_node))
3605 return unsignedp ? sat_long_long_fract_type_node
3606 : long_long_fract_type_node;
3608 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3609 return unsignedp ? sat_unsigned_short_fract_type_node
3610 : unsigned_short_fract_type_node;
3611 if (mode == TYPE_MODE (unsigned_fract_type_node))
3612 return unsignedp ? sat_unsigned_fract_type_node
3613 : unsigned_fract_type_node;
3614 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3615 return unsignedp ? sat_unsigned_long_fract_type_node
3616 : unsigned_long_fract_type_node;
3617 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3618 return unsignedp ? sat_unsigned_long_long_fract_type_node
3619 : unsigned_long_long_fract_type_node;
3621 if (mode == TYPE_MODE (short_accum_type_node))
3622 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3623 if (mode == TYPE_MODE (accum_type_node))
3624 return unsignedp ? sat_accum_type_node : accum_type_node;
3625 if (mode == TYPE_MODE (long_accum_type_node))
3626 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3627 if (mode == TYPE_MODE (long_long_accum_type_node))
3628 return unsignedp ? sat_long_long_accum_type_node
3629 : long_long_accum_type_node;
3631 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3632 return unsignedp ? sat_unsigned_short_accum_type_node
3633 : unsigned_short_accum_type_node;
3634 if (mode == TYPE_MODE (unsigned_accum_type_node))
3635 return unsignedp ? sat_unsigned_accum_type_node
3636 : unsigned_accum_type_node;
3637 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3638 return unsignedp ? sat_unsigned_long_accum_type_node
3639 : unsigned_long_accum_type_node;
3640 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3641 return unsignedp ? sat_unsigned_long_long_accum_type_node
3642 : unsigned_long_long_accum_type_node;
3644 if (mode == QQmode)
3645 return unsignedp ? sat_qq_type_node : qq_type_node;
3646 if (mode == HQmode)
3647 return unsignedp ? sat_hq_type_node : hq_type_node;
3648 if (mode == SQmode)
3649 return unsignedp ? sat_sq_type_node : sq_type_node;
3650 if (mode == DQmode)
3651 return unsignedp ? sat_dq_type_node : dq_type_node;
3652 if (mode == TQmode)
3653 return unsignedp ? sat_tq_type_node : tq_type_node;
3655 if (mode == UQQmode)
3656 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3657 if (mode == UHQmode)
3658 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3659 if (mode == USQmode)
3660 return unsignedp ? sat_usq_type_node : usq_type_node;
3661 if (mode == UDQmode)
3662 return unsignedp ? sat_udq_type_node : udq_type_node;
3663 if (mode == UTQmode)
3664 return unsignedp ? sat_utq_type_node : utq_type_node;
3666 if (mode == HAmode)
3667 return unsignedp ? sat_ha_type_node : ha_type_node;
3668 if (mode == SAmode)
3669 return unsignedp ? sat_sa_type_node : sa_type_node;
3670 if (mode == DAmode)
3671 return unsignedp ? sat_da_type_node : da_type_node;
3672 if (mode == TAmode)
3673 return unsignedp ? sat_ta_type_node : ta_type_node;
3675 if (mode == UHAmode)
3676 return unsignedp ? sat_uha_type_node : uha_type_node;
3677 if (mode == USAmode)
3678 return unsignedp ? sat_usa_type_node : usa_type_node;
3679 if (mode == UDAmode)
3680 return unsignedp ? sat_uda_type_node : uda_type_node;
3681 if (mode == UTAmode)
3682 return unsignedp ? sat_uta_type_node : uta_type_node;
3685 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3686 if (TYPE_MODE (TREE_VALUE (t)) == mode
3687 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3688 return TREE_VALUE (t);
3690 return 0;
3693 tree
3694 c_common_unsigned_type (tree type)
3696 return c_common_signed_or_unsigned_type (1, type);
3699 /* Return a signed type the same as TYPE in other respects. */
3701 tree
3702 c_common_signed_type (tree type)
3704 return c_common_signed_or_unsigned_type (0, type);
3707 /* Return a type the same as TYPE except unsigned or
3708 signed according to UNSIGNEDP. */
3710 tree
3711 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3713 tree type1;
3715 /* This block of code emulates the behavior of the old
3716 c_common_unsigned_type. In particular, it returns
3717 long_unsigned_type_node if passed a long, even when a int would
3718 have the same size. This is necessary for warnings to work
3719 correctly in archs where sizeof(int) == sizeof(long) */
3721 type1 = TYPE_MAIN_VARIANT (type);
3722 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3723 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3724 if (type1 == integer_type_node || type1 == unsigned_type_node)
3725 return unsignedp ? unsigned_type_node : integer_type_node;
3726 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3727 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3728 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3729 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3730 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3731 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3732 if (int128_integer_type_node
3733 && (type1 == int128_integer_type_node
3734 || type1 == int128_unsigned_type_node))
3735 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3736 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3737 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3738 #if HOST_BITS_PER_WIDE_INT >= 64
3739 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3740 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3741 #endif
3742 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3743 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3744 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3745 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3746 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3747 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3748 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3749 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3751 #define C_COMMON_FIXED_TYPES(NAME) \
3752 if (type1 == short_ ## NAME ## _type_node \
3753 || type1 == unsigned_short_ ## NAME ## _type_node) \
3754 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3755 : short_ ## NAME ## _type_node; \
3756 if (type1 == NAME ## _type_node \
3757 || type1 == unsigned_ ## NAME ## _type_node) \
3758 return unsignedp ? unsigned_ ## NAME ## _type_node \
3759 : NAME ## _type_node; \
3760 if (type1 == long_ ## NAME ## _type_node \
3761 || type1 == unsigned_long_ ## NAME ## _type_node) \
3762 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3763 : long_ ## NAME ## _type_node; \
3764 if (type1 == long_long_ ## NAME ## _type_node \
3765 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3766 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3767 : long_long_ ## NAME ## _type_node;
3769 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3770 if (type1 == NAME ## _type_node \
3771 || type1 == u ## NAME ## _type_node) \
3772 return unsignedp ? u ## NAME ## _type_node \
3773 : NAME ## _type_node;
3775 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3776 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3777 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3778 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3779 : sat_ ## short_ ## NAME ## _type_node; \
3780 if (type1 == sat_ ## NAME ## _type_node \
3781 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3782 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3783 : sat_ ## NAME ## _type_node; \
3784 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3785 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3786 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3787 : sat_ ## long_ ## NAME ## _type_node; \
3788 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3789 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3790 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3791 : sat_ ## long_long_ ## NAME ## _type_node;
3793 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3794 if (type1 == sat_ ## NAME ## _type_node \
3795 || type1 == sat_ ## u ## NAME ## _type_node) \
3796 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3797 : sat_ ## NAME ## _type_node;
3799 C_COMMON_FIXED_TYPES (fract);
3800 C_COMMON_FIXED_TYPES_SAT (fract);
3801 C_COMMON_FIXED_TYPES (accum);
3802 C_COMMON_FIXED_TYPES_SAT (accum);
3804 C_COMMON_FIXED_MODE_TYPES (qq);
3805 C_COMMON_FIXED_MODE_TYPES (hq);
3806 C_COMMON_FIXED_MODE_TYPES (sq);
3807 C_COMMON_FIXED_MODE_TYPES (dq);
3808 C_COMMON_FIXED_MODE_TYPES (tq);
3809 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3810 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3811 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3812 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3813 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3814 C_COMMON_FIXED_MODE_TYPES (ha);
3815 C_COMMON_FIXED_MODE_TYPES (sa);
3816 C_COMMON_FIXED_MODE_TYPES (da);
3817 C_COMMON_FIXED_MODE_TYPES (ta);
3818 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3819 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3820 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3821 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3823 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3824 the precision; they have precision set to match their range, but
3825 may use a wider mode to match an ABI. If we change modes, we may
3826 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3827 the precision as well, so as to yield correct results for
3828 bit-field types. C++ does not have these separate bit-field
3829 types, and producing a signed or unsigned variant of an
3830 ENUMERAL_TYPE may cause other problems as well. */
3832 if (!INTEGRAL_TYPE_P (type)
3833 || TYPE_UNSIGNED (type) == unsignedp)
3834 return type;
3836 #define TYPE_OK(node) \
3837 (TYPE_MODE (type) == TYPE_MODE (node) \
3838 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3839 if (TYPE_OK (signed_char_type_node))
3840 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3841 if (TYPE_OK (integer_type_node))
3842 return unsignedp ? unsigned_type_node : integer_type_node;
3843 if (TYPE_OK (short_integer_type_node))
3844 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3845 if (TYPE_OK (long_integer_type_node))
3846 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3847 if (TYPE_OK (long_long_integer_type_node))
3848 return (unsignedp ? long_long_unsigned_type_node
3849 : long_long_integer_type_node);
3850 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3851 return (unsignedp ? int128_unsigned_type_node
3852 : int128_integer_type_node);
3853 if (TYPE_OK (widest_integer_literal_type_node))
3854 return (unsignedp ? widest_unsigned_literal_type_node
3855 : widest_integer_literal_type_node);
3857 #if HOST_BITS_PER_WIDE_INT >= 64
3858 if (TYPE_OK (intTI_type_node))
3859 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3860 #endif
3861 if (TYPE_OK (intDI_type_node))
3862 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3863 if (TYPE_OK (intSI_type_node))
3864 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3865 if (TYPE_OK (intHI_type_node))
3866 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3867 if (TYPE_OK (intQI_type_node))
3868 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3869 #undef TYPE_OK
3871 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3874 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3876 tree
3877 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3879 /* Extended integer types of the same width as a standard type have
3880 lesser rank, so those of the same width as int promote to int or
3881 unsigned int and are valid for printf formats expecting int or
3882 unsigned int. To avoid such special cases, avoid creating
3883 extended integer types for bit-fields if a standard integer type
3884 is available. */
3885 if (width == TYPE_PRECISION (integer_type_node))
3886 return unsignedp ? unsigned_type_node : integer_type_node;
3887 if (width == TYPE_PRECISION (signed_char_type_node))
3888 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3889 if (width == TYPE_PRECISION (short_integer_type_node))
3890 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3891 if (width == TYPE_PRECISION (long_integer_type_node))
3892 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3893 if (width == TYPE_PRECISION (long_long_integer_type_node))
3894 return (unsignedp ? long_long_unsigned_type_node
3895 : long_long_integer_type_node);
3896 if (int128_integer_type_node
3897 && width == TYPE_PRECISION (int128_integer_type_node))
3898 return (unsignedp ? int128_unsigned_type_node
3899 : int128_integer_type_node);
3900 return build_nonstandard_integer_type (width, unsignedp);
3903 /* The C version of the register_builtin_type langhook. */
3905 void
3906 c_register_builtin_type (tree type, const char* name)
3908 tree decl;
3910 decl = build_decl (UNKNOWN_LOCATION,
3911 TYPE_DECL, get_identifier (name), type);
3912 DECL_ARTIFICIAL (decl) = 1;
3913 if (!TYPE_NAME (type))
3914 TYPE_NAME (type) = decl;
3915 pushdecl (decl);
3917 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3920 /* Print an error message for invalid operands to arith operation
3921 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3922 LOCATION is the location of the message. */
3924 void
3925 binary_op_error (location_t location, enum tree_code code,
3926 tree type0, tree type1)
3928 const char *opname;
3930 switch (code)
3932 case PLUS_EXPR:
3933 opname = "+"; break;
3934 case MINUS_EXPR:
3935 opname = "-"; break;
3936 case MULT_EXPR:
3937 opname = "*"; break;
3938 case MAX_EXPR:
3939 opname = "max"; break;
3940 case MIN_EXPR:
3941 opname = "min"; break;
3942 case EQ_EXPR:
3943 opname = "=="; break;
3944 case NE_EXPR:
3945 opname = "!="; break;
3946 case LE_EXPR:
3947 opname = "<="; break;
3948 case GE_EXPR:
3949 opname = ">="; break;
3950 case LT_EXPR:
3951 opname = "<"; break;
3952 case GT_EXPR:
3953 opname = ">"; break;
3954 case LSHIFT_EXPR:
3955 opname = "<<"; break;
3956 case RSHIFT_EXPR:
3957 opname = ">>"; break;
3958 case TRUNC_MOD_EXPR:
3959 case FLOOR_MOD_EXPR:
3960 opname = "%"; break;
3961 case TRUNC_DIV_EXPR:
3962 case FLOOR_DIV_EXPR:
3963 opname = "/"; break;
3964 case BIT_AND_EXPR:
3965 opname = "&"; break;
3966 case BIT_IOR_EXPR:
3967 opname = "|"; break;
3968 case TRUTH_ANDIF_EXPR:
3969 opname = "&&"; break;
3970 case TRUTH_ORIF_EXPR:
3971 opname = "||"; break;
3972 case BIT_XOR_EXPR:
3973 opname = "^"; break;
3974 default:
3975 gcc_unreachable ();
3977 error_at (location,
3978 "invalid operands to binary %s (have %qT and %qT)", opname,
3979 type0, type1);
3982 /* Given an expression as a tree, return its original type. Do this
3983 by stripping any conversion that preserves the sign and precision. */
3984 static tree
3985 expr_original_type (tree expr)
3987 STRIP_SIGN_NOPS (expr);
3988 return TREE_TYPE (expr);
3991 /* Subroutine of build_binary_op, used for comparison operations.
3992 See if the operands have both been converted from subword integer types
3993 and, if so, perhaps change them both back to their original type.
3994 This function is also responsible for converting the two operands
3995 to the proper common type for comparison.
3997 The arguments of this function are all pointers to local variables
3998 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3999 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4001 LOC is the location of the comparison.
4003 If this function returns nonzero, it means that the comparison has
4004 a constant value. What this function returns is an expression for
4005 that value. */
4007 tree
4008 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4009 tree *restype_ptr, enum tree_code *rescode_ptr)
4011 tree type;
4012 tree op0 = *op0_ptr;
4013 tree op1 = *op1_ptr;
4014 int unsignedp0, unsignedp1;
4015 int real1, real2;
4016 tree primop0, primop1;
4017 enum tree_code code = *rescode_ptr;
4019 /* Throw away any conversions to wider types
4020 already present in the operands. */
4022 primop0 = c_common_get_narrower (op0, &unsignedp0);
4023 primop1 = c_common_get_narrower (op1, &unsignedp1);
4025 /* If primopN is first sign-extended from primopN's precision to opN's
4026 precision, then zero-extended from opN's precision to
4027 *restype_ptr precision, shortenings might be invalid. */
4028 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4029 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4030 && !unsignedp0
4031 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4032 primop0 = op0;
4033 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4034 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4035 && !unsignedp1
4036 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4037 primop1 = op1;
4039 /* Handle the case that OP0 does not *contain* a conversion
4040 but it *requires* conversion to FINAL_TYPE. */
4042 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4043 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4044 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4045 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4047 /* If one of the operands must be floated, we cannot optimize. */
4048 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4049 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4051 /* If first arg is constant, swap the args (changing operation
4052 so value is preserved), for canonicalization. Don't do this if
4053 the second arg is 0. */
4055 if (TREE_CONSTANT (primop0)
4056 && !integer_zerop (primop1) && !real_zerop (primop1)
4057 && !fixed_zerop (primop1))
4059 tree tem = primop0;
4060 int temi = unsignedp0;
4061 primop0 = primop1;
4062 primop1 = tem;
4063 tem = op0;
4064 op0 = op1;
4065 op1 = tem;
4066 *op0_ptr = op0;
4067 *op1_ptr = op1;
4068 unsignedp0 = unsignedp1;
4069 unsignedp1 = temi;
4070 temi = real1;
4071 real1 = real2;
4072 real2 = temi;
4074 switch (code)
4076 case LT_EXPR:
4077 code = GT_EXPR;
4078 break;
4079 case GT_EXPR:
4080 code = LT_EXPR;
4081 break;
4082 case LE_EXPR:
4083 code = GE_EXPR;
4084 break;
4085 case GE_EXPR:
4086 code = LE_EXPR;
4087 break;
4088 default:
4089 break;
4091 *rescode_ptr = code;
4094 /* If comparing an integer against a constant more bits wide,
4095 maybe we can deduce a value of 1 or 0 independent of the data.
4096 Or else truncate the constant now
4097 rather than extend the variable at run time.
4099 This is only interesting if the constant is the wider arg.
4100 Also, it is not safe if the constant is unsigned and the
4101 variable arg is signed, since in this case the variable
4102 would be sign-extended and then regarded as unsigned.
4103 Our technique fails in this case because the lowest/highest
4104 possible unsigned results don't follow naturally from the
4105 lowest/highest possible values of the variable operand.
4106 For just EQ_EXPR and NE_EXPR there is another technique that
4107 could be used: see if the constant can be faithfully represented
4108 in the other operand's type, by truncating it and reextending it
4109 and see if that preserves the constant's value. */
4111 if (!real1 && !real2
4112 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4113 && TREE_CODE (primop1) == INTEGER_CST
4114 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4116 int min_gt, max_gt, min_lt, max_lt;
4117 tree maxval, minval;
4118 /* 1 if comparison is nominally unsigned. */
4119 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4120 tree val;
4122 type = c_common_signed_or_unsigned_type (unsignedp0,
4123 TREE_TYPE (primop0));
4125 maxval = TYPE_MAX_VALUE (type);
4126 minval = TYPE_MIN_VALUE (type);
4128 if (unsignedp && !unsignedp0)
4129 *restype_ptr = c_common_signed_type (*restype_ptr);
4131 if (TREE_TYPE (primop1) != *restype_ptr)
4133 /* Convert primop1 to target type, but do not introduce
4134 additional overflow. We know primop1 is an int_cst. */
4135 primop1 = force_fit_type_double (*restype_ptr,
4136 tree_to_double_int (primop1),
4137 0, TREE_OVERFLOW (primop1));
4139 if (type != *restype_ptr)
4141 minval = convert (*restype_ptr, minval);
4142 maxval = convert (*restype_ptr, maxval);
4145 if (unsignedp && unsignedp0)
4147 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4148 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4149 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4150 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4152 else
4154 min_gt = INT_CST_LT (primop1, minval);
4155 max_gt = INT_CST_LT (primop1, maxval);
4156 min_lt = INT_CST_LT (minval, primop1);
4157 max_lt = INT_CST_LT (maxval, primop1);
4160 val = 0;
4161 /* This used to be a switch, but Genix compiler can't handle that. */
4162 if (code == NE_EXPR)
4164 if (max_lt || min_gt)
4165 val = truthvalue_true_node;
4167 else if (code == EQ_EXPR)
4169 if (max_lt || min_gt)
4170 val = truthvalue_false_node;
4172 else if (code == LT_EXPR)
4174 if (max_lt)
4175 val = truthvalue_true_node;
4176 if (!min_lt)
4177 val = truthvalue_false_node;
4179 else if (code == GT_EXPR)
4181 if (min_gt)
4182 val = truthvalue_true_node;
4183 if (!max_gt)
4184 val = truthvalue_false_node;
4186 else if (code == LE_EXPR)
4188 if (!max_gt)
4189 val = truthvalue_true_node;
4190 if (min_gt)
4191 val = truthvalue_false_node;
4193 else if (code == GE_EXPR)
4195 if (!min_lt)
4196 val = truthvalue_true_node;
4197 if (max_lt)
4198 val = truthvalue_false_node;
4201 /* If primop0 was sign-extended and unsigned comparison specd,
4202 we did a signed comparison above using the signed type bounds.
4203 But the comparison we output must be unsigned.
4205 Also, for inequalities, VAL is no good; but if the signed
4206 comparison had *any* fixed result, it follows that the
4207 unsigned comparison just tests the sign in reverse
4208 (positive values are LE, negative ones GE).
4209 So we can generate an unsigned comparison
4210 against an extreme value of the signed type. */
4212 if (unsignedp && !unsignedp0)
4214 if (val != 0)
4215 switch (code)
4217 case LT_EXPR:
4218 case GE_EXPR:
4219 primop1 = TYPE_MIN_VALUE (type);
4220 val = 0;
4221 break;
4223 case LE_EXPR:
4224 case GT_EXPR:
4225 primop1 = TYPE_MAX_VALUE (type);
4226 val = 0;
4227 break;
4229 default:
4230 break;
4232 type = c_common_unsigned_type (type);
4235 if (TREE_CODE (primop0) != INTEGER_CST)
4237 if (val == truthvalue_false_node)
4238 warning_at (loc, OPT_Wtype_limits,
4239 "comparison is always false due to limited range of data type");
4240 if (val == truthvalue_true_node)
4241 warning_at (loc, OPT_Wtype_limits,
4242 "comparison is always true due to limited range of data type");
4245 if (val != 0)
4247 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4248 if (TREE_SIDE_EFFECTS (primop0))
4249 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4250 return val;
4253 /* Value is not predetermined, but do the comparison
4254 in the type of the operand that is not constant.
4255 TYPE is already properly set. */
4258 /* If either arg is decimal float and the other is float, find the
4259 proper common type to use for comparison. */
4260 else if (real1 && real2
4261 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4262 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4263 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4265 else if (real1 && real2
4266 && (TYPE_PRECISION (TREE_TYPE (primop0))
4267 == TYPE_PRECISION (TREE_TYPE (primop1))))
4268 type = TREE_TYPE (primop0);
4270 /* If args' natural types are both narrower than nominal type
4271 and both extend in the same manner, compare them
4272 in the type of the wider arg.
4273 Otherwise must actually extend both to the nominal
4274 common type lest different ways of extending
4275 alter the result.
4276 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4278 else if (unsignedp0 == unsignedp1 && real1 == real2
4279 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4280 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4282 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4283 type = c_common_signed_or_unsigned_type (unsignedp0
4284 || TYPE_UNSIGNED (*restype_ptr),
4285 type);
4286 /* Make sure shorter operand is extended the right way
4287 to match the longer operand. */
4288 primop0
4289 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4290 TREE_TYPE (primop0)),
4291 primop0);
4292 primop1
4293 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4294 TREE_TYPE (primop1)),
4295 primop1);
4297 else
4299 /* Here we must do the comparison on the nominal type
4300 using the args exactly as we received them. */
4301 type = *restype_ptr;
4302 primop0 = op0;
4303 primop1 = op1;
4305 if (!real1 && !real2 && integer_zerop (primop1)
4306 && TYPE_UNSIGNED (*restype_ptr))
4308 tree value = 0;
4309 /* All unsigned values are >= 0, so we warn. However,
4310 if OP0 is a constant that is >= 0, the signedness of
4311 the comparison isn't an issue, so suppress the
4312 warning. */
4313 bool warn =
4314 warn_type_limits && !in_system_header_at (loc)
4315 && !(TREE_CODE (primop0) == INTEGER_CST
4316 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4317 primop0)))
4318 /* Do not warn for enumeration types. */
4319 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4321 switch (code)
4323 case GE_EXPR:
4324 if (warn)
4325 warning_at (loc, OPT_Wtype_limits,
4326 "comparison of unsigned expression >= 0 is always true");
4327 value = truthvalue_true_node;
4328 break;
4330 case LT_EXPR:
4331 if (warn)
4332 warning_at (loc, OPT_Wtype_limits,
4333 "comparison of unsigned expression < 0 is always false");
4334 value = truthvalue_false_node;
4335 break;
4337 default:
4338 break;
4341 if (value != 0)
4343 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4344 if (TREE_SIDE_EFFECTS (primop0))
4345 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4346 primop0, value);
4347 return value;
4352 *op0_ptr = convert (type, primop0);
4353 *op1_ptr = convert (type, primop1);
4355 *restype_ptr = truthvalue_type_node;
4357 return 0;
4360 /* Return a tree for the sum or difference (RESULTCODE says which)
4361 of pointer PTROP and integer INTOP. */
4363 tree
4364 pointer_int_sum (location_t loc, enum tree_code resultcode,
4365 tree ptrop, tree intop, bool complain)
4367 tree size_exp, ret;
4369 /* The result is a pointer of the same type that is being added. */
4370 tree result_type = TREE_TYPE (ptrop);
4372 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4374 if (complain && warn_pointer_arith)
4375 pedwarn (loc, OPT_Wpointer_arith,
4376 "pointer of type %<void *%> used in arithmetic");
4377 else if (!complain)
4378 return error_mark_node;
4379 size_exp = integer_one_node;
4381 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4383 if (complain && warn_pointer_arith)
4384 pedwarn (loc, OPT_Wpointer_arith,
4385 "pointer to a function used in arithmetic");
4386 else if (!complain)
4387 return error_mark_node;
4388 size_exp = integer_one_node;
4390 else
4391 size_exp = size_in_bytes (TREE_TYPE (result_type));
4393 /* We are manipulating pointer values, so we don't need to warn
4394 about relying on undefined signed overflow. We disable the
4395 warning here because we use integer types so fold won't know that
4396 they are really pointers. */
4397 fold_defer_overflow_warnings ();
4399 /* If what we are about to multiply by the size of the elements
4400 contains a constant term, apply distributive law
4401 and multiply that constant term separately.
4402 This helps produce common subexpressions. */
4403 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4404 && !TREE_CONSTANT (intop)
4405 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4406 && TREE_CONSTANT (size_exp)
4407 /* If the constant comes from pointer subtraction,
4408 skip this optimization--it would cause an error. */
4409 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4410 /* If the constant is unsigned, and smaller than the pointer size,
4411 then we must skip this optimization. This is because it could cause
4412 an overflow error if the constant is negative but INTOP is not. */
4413 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4414 || (TYPE_PRECISION (TREE_TYPE (intop))
4415 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4417 enum tree_code subcode = resultcode;
4418 tree int_type = TREE_TYPE (intop);
4419 if (TREE_CODE (intop) == MINUS_EXPR)
4420 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4421 /* Convert both subexpression types to the type of intop,
4422 because weird cases involving pointer arithmetic
4423 can result in a sum or difference with different type args. */
4424 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4425 subcode, ptrop,
4426 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4427 intop = convert (int_type, TREE_OPERAND (intop, 0));
4430 /* Convert the integer argument to a type the same size as sizetype
4431 so the multiply won't overflow spuriously. */
4432 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4433 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4434 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4435 TYPE_UNSIGNED (sizetype)), intop);
4437 /* Replace the integer argument with a suitable product by the object size.
4438 Do this multiplication as signed, then convert to the appropriate type
4439 for the pointer operation and disregard an overflow that occurred only
4440 because of the sign-extension change in the latter conversion. */
4442 tree t = build_binary_op (loc,
4443 MULT_EXPR, intop,
4444 convert (TREE_TYPE (intop), size_exp), 1);
4445 intop = convert (sizetype, t);
4446 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4447 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4448 TREE_INT_CST_HIGH (intop));
4451 /* Create the sum or difference. */
4452 if (resultcode == MINUS_EXPR)
4453 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4455 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4457 fold_undefer_and_ignore_overflow_warnings ();
4459 return ret;
4462 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4463 and if NON_CONST is known not to be permitted in an evaluated part
4464 of a constant expression. */
4466 tree
4467 c_wrap_maybe_const (tree expr, bool non_const)
4469 bool nowarning = TREE_NO_WARNING (expr);
4470 location_t loc = EXPR_LOCATION (expr);
4472 /* This should never be called for C++. */
4473 if (c_dialect_cxx ())
4474 gcc_unreachable ();
4476 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4477 STRIP_TYPE_NOPS (expr);
4478 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4479 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4480 if (nowarning)
4481 TREE_NO_WARNING (expr) = 1;
4482 protected_set_expr_location (expr, loc);
4484 return expr;
4487 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4488 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4489 around the SAVE_EXPR if needed so that c_fully_fold does not need
4490 to look inside SAVE_EXPRs. */
4492 tree
4493 c_save_expr (tree expr)
4495 bool maybe_const = true;
4496 if (c_dialect_cxx ())
4497 return save_expr (expr);
4498 expr = c_fully_fold (expr, false, &maybe_const);
4499 expr = save_expr (expr);
4500 if (!maybe_const)
4501 expr = c_wrap_maybe_const (expr, true);
4502 return expr;
4505 /* Return whether EXPR is a declaration whose address can never be
4506 NULL. */
4508 bool
4509 decl_with_nonnull_addr_p (const_tree expr)
4511 return (DECL_P (expr)
4512 && (TREE_CODE (expr) == PARM_DECL
4513 || TREE_CODE (expr) == LABEL_DECL
4514 || !DECL_WEAK (expr)));
4517 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4518 or for an `if' or `while' statement or ?..: exp. It should already
4519 have been validated to be of suitable type; otherwise, a bad
4520 diagnostic may result.
4522 The EXPR is located at LOCATION.
4524 This preparation consists of taking the ordinary
4525 representation of an expression expr and producing a valid tree
4526 boolean expression describing whether expr is nonzero. We could
4527 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4528 but we optimize comparisons, &&, ||, and !.
4530 The resulting type should always be `truthvalue_type_node'. */
4532 tree
4533 c_common_truthvalue_conversion (location_t location, tree expr)
4535 switch (TREE_CODE (expr))
4537 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4538 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4539 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4540 case ORDERED_EXPR: case UNORDERED_EXPR:
4541 if (TREE_TYPE (expr) == truthvalue_type_node)
4542 return expr;
4543 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4544 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4545 goto ret;
4547 case TRUTH_ANDIF_EXPR:
4548 case TRUTH_ORIF_EXPR:
4549 case TRUTH_AND_EXPR:
4550 case TRUTH_OR_EXPR:
4551 case TRUTH_XOR_EXPR:
4552 if (TREE_TYPE (expr) == truthvalue_type_node)
4553 return expr;
4554 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4555 c_common_truthvalue_conversion (location,
4556 TREE_OPERAND (expr, 0)),
4557 c_common_truthvalue_conversion (location,
4558 TREE_OPERAND (expr, 1)));
4559 goto ret;
4561 case TRUTH_NOT_EXPR:
4562 if (TREE_TYPE (expr) == truthvalue_type_node)
4563 return expr;
4564 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4565 c_common_truthvalue_conversion (location,
4566 TREE_OPERAND (expr, 0)));
4567 goto ret;
4569 case ERROR_MARK:
4570 return expr;
4572 case INTEGER_CST:
4573 return integer_zerop (expr) ? truthvalue_false_node
4574 : truthvalue_true_node;
4576 case REAL_CST:
4577 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4578 ? truthvalue_true_node
4579 : truthvalue_false_node;
4581 case FIXED_CST:
4582 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4583 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4584 ? truthvalue_true_node
4585 : truthvalue_false_node;
4587 case FUNCTION_DECL:
4588 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4589 /* Fall through. */
4591 case ADDR_EXPR:
4593 tree inner = TREE_OPERAND (expr, 0);
4594 if (decl_with_nonnull_addr_p (inner))
4596 /* Common Ada/Pascal programmer's mistake. */
4597 warning_at (location,
4598 OPT_Waddress,
4599 "the address of %qD will always evaluate as %<true%>",
4600 inner);
4601 return truthvalue_true_node;
4603 break;
4606 case COMPLEX_EXPR:
4607 expr = build_binary_op (EXPR_LOCATION (expr),
4608 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4609 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4610 c_common_truthvalue_conversion (location,
4611 TREE_OPERAND (expr, 0)),
4612 c_common_truthvalue_conversion (location,
4613 TREE_OPERAND (expr, 1)),
4615 goto ret;
4617 case NEGATE_EXPR:
4618 case ABS_EXPR:
4619 case FLOAT_EXPR:
4620 case EXCESS_PRECISION_EXPR:
4621 /* These don't change whether an object is nonzero or zero. */
4622 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4624 case LROTATE_EXPR:
4625 case RROTATE_EXPR:
4626 /* These don't change whether an object is zero or nonzero, but
4627 we can't ignore them if their second arg has side-effects. */
4628 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4630 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4631 TREE_OPERAND (expr, 1),
4632 c_common_truthvalue_conversion
4633 (location, TREE_OPERAND (expr, 0)));
4634 goto ret;
4636 else
4637 return c_common_truthvalue_conversion (location,
4638 TREE_OPERAND (expr, 0));
4640 case COND_EXPR:
4641 /* Distribute the conversion into the arms of a COND_EXPR. */
4642 if (c_dialect_cxx ())
4644 tree op1 = TREE_OPERAND (expr, 1);
4645 tree op2 = TREE_OPERAND (expr, 2);
4646 /* In C++ one of the arms might have void type if it is throw. */
4647 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4648 op1 = c_common_truthvalue_conversion (location, op1);
4649 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4650 op2 = c_common_truthvalue_conversion (location, op2);
4651 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4652 TREE_OPERAND (expr, 0), op1, op2);
4653 goto ret;
4655 else
4657 /* Folding will happen later for C. */
4658 expr = build3 (COND_EXPR, truthvalue_type_node,
4659 TREE_OPERAND (expr, 0),
4660 c_common_truthvalue_conversion (location,
4661 TREE_OPERAND (expr, 1)),
4662 c_common_truthvalue_conversion (location,
4663 TREE_OPERAND (expr, 2)));
4664 goto ret;
4667 CASE_CONVERT:
4669 tree totype = TREE_TYPE (expr);
4670 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4672 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4673 since that affects how `default_conversion' will behave. */
4674 if (TREE_CODE (totype) == REFERENCE_TYPE
4675 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4676 break;
4677 /* Don't strip a conversion from C++0x scoped enum, since they
4678 don't implicitly convert to other types. */
4679 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4680 && ENUM_IS_SCOPED (fromtype))
4681 break;
4682 /* If this isn't narrowing the argument, we can ignore it. */
4683 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4684 return c_common_truthvalue_conversion (location,
4685 TREE_OPERAND (expr, 0));
4687 break;
4689 case MODIFY_EXPR:
4690 if (!TREE_NO_WARNING (expr)
4691 && warn_parentheses)
4693 warning (OPT_Wparentheses,
4694 "suggest parentheses around assignment used as truth value");
4695 TREE_NO_WARNING (expr) = 1;
4697 break;
4699 default:
4700 break;
4703 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4705 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4706 expr = (build_binary_op
4707 (EXPR_LOCATION (expr),
4708 (TREE_SIDE_EFFECTS (expr)
4709 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4710 c_common_truthvalue_conversion
4711 (location,
4712 build_unary_op (location, REALPART_EXPR, t, 0)),
4713 c_common_truthvalue_conversion
4714 (location,
4715 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4716 0));
4717 goto ret;
4720 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4722 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4723 FCONST0 (TYPE_MODE
4724 (TREE_TYPE (expr))));
4725 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4727 else
4728 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4730 ret:
4731 protected_set_expr_location (expr, location);
4732 return expr;
4735 static void def_builtin_1 (enum built_in_function fncode,
4736 const char *name,
4737 enum built_in_class fnclass,
4738 tree fntype, tree libtype,
4739 bool both_p, bool fallback_p, bool nonansi_p,
4740 tree fnattrs, bool implicit_p);
4743 /* Apply the TYPE_QUALS to the new DECL. */
4745 void
4746 c_apply_type_quals_to_decl (int type_quals, tree decl)
4748 tree type = TREE_TYPE (decl);
4750 if (type == error_mark_node)
4751 return;
4753 if ((type_quals & TYPE_QUAL_CONST)
4754 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4755 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4756 constructor can produce constant init, so rely on cp_finish_decl to
4757 clear TREE_READONLY if the variable has non-constant init. */
4758 TREE_READONLY (decl) = 1;
4759 if (type_quals & TYPE_QUAL_VOLATILE)
4761 TREE_SIDE_EFFECTS (decl) = 1;
4762 TREE_THIS_VOLATILE (decl) = 1;
4764 if (type_quals & TYPE_QUAL_RESTRICT)
4766 while (type && TREE_CODE (type) == ARRAY_TYPE)
4767 /* Allow 'restrict' on arrays of pointers.
4768 FIXME currently we just ignore it. */
4769 type = TREE_TYPE (type);
4770 if (!type
4771 || !POINTER_TYPE_P (type)
4772 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4773 error ("invalid use of %<restrict%>");
4777 /* Hash function for the problem of multiple type definitions in
4778 different files. This must hash all types that will compare
4779 equal via comptypes to the same value. In practice it hashes
4780 on some of the simple stuff and leaves the details to comptypes. */
4782 static hashval_t
4783 c_type_hash (const void *p)
4785 int n_elements;
4786 int shift, size;
4787 const_tree const t = (const_tree) p;
4788 tree t2;
4789 switch (TREE_CODE (t))
4791 /* For pointers, hash on pointee type plus some swizzling. */
4792 case POINTER_TYPE:
4793 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4794 /* Hash on number of elements and total size. */
4795 case ENUMERAL_TYPE:
4796 shift = 3;
4797 t2 = TYPE_VALUES (t);
4798 break;
4799 case RECORD_TYPE:
4800 shift = 0;
4801 t2 = TYPE_FIELDS (t);
4802 break;
4803 case QUAL_UNION_TYPE:
4804 shift = 1;
4805 t2 = TYPE_FIELDS (t);
4806 break;
4807 case UNION_TYPE:
4808 shift = 2;
4809 t2 = TYPE_FIELDS (t);
4810 break;
4811 default:
4812 gcc_unreachable ();
4814 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4815 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4816 n_elements = list_length (t2);
4817 /* We might have a VLA here. */
4818 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4819 size = 0;
4820 else
4821 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4822 return ((size << 24) | (n_elements << shift));
4825 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4827 /* Return the typed-based alias set for T, which may be an expression
4828 or a type. Return -1 if we don't do anything special. */
4830 alias_set_type
4831 c_common_get_alias_set (tree t)
4833 tree u;
4834 PTR *slot;
4836 /* For VLAs, use the alias set of the element type rather than the
4837 default of alias set 0 for types compared structurally. */
4838 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4840 if (TREE_CODE (t) == ARRAY_TYPE)
4841 return get_alias_set (TREE_TYPE (t));
4842 return -1;
4845 /* Permit type-punning when accessing a union, provided the access
4846 is directly through the union. For example, this code does not
4847 permit taking the address of a union member and then storing
4848 through it. Even the type-punning allowed here is a GCC
4849 extension, albeit a common and useful one; the C standard says
4850 that such accesses have implementation-defined behavior. */
4851 for (u = t;
4852 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4853 u = TREE_OPERAND (u, 0))
4854 if (TREE_CODE (u) == COMPONENT_REF
4855 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4856 return 0;
4858 /* That's all the expressions we handle specially. */
4859 if (!TYPE_P (t))
4860 return -1;
4862 /* The C standard guarantees that any object may be accessed via an
4863 lvalue that has character type. */
4864 if (t == char_type_node
4865 || t == signed_char_type_node
4866 || t == unsigned_char_type_node)
4867 return 0;
4869 /* The C standard specifically allows aliasing between signed and
4870 unsigned variants of the same type. We treat the signed
4871 variant as canonical. */
4872 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4874 tree t1 = c_common_signed_type (t);
4876 /* t1 == t can happen for boolean nodes which are always unsigned. */
4877 if (t1 != t)
4878 return get_alias_set (t1);
4881 /* Handle the case of multiple type nodes referring to "the same" type,
4882 which occurs with IMA. These share an alias set. FIXME: Currently only
4883 C90 is handled. (In C99 type compatibility is not transitive, which
4884 complicates things mightily. The alias set splay trees can theoretically
4885 represent this, but insertion is tricky when you consider all the
4886 different orders things might arrive in.) */
4888 if (c_language != clk_c || flag_isoc99)
4889 return -1;
4891 /* Save time if there's only one input file. */
4892 if (num_in_fnames == 1)
4893 return -1;
4895 /* Pointers need special handling if they point to any type that
4896 needs special handling (below). */
4897 if (TREE_CODE (t) == POINTER_TYPE)
4899 tree t2;
4900 /* Find bottom type under any nested POINTERs. */
4901 for (t2 = TREE_TYPE (t);
4902 TREE_CODE (t2) == POINTER_TYPE;
4903 t2 = TREE_TYPE (t2))
4905 if (TREE_CODE (t2) != RECORD_TYPE
4906 && TREE_CODE (t2) != ENUMERAL_TYPE
4907 && TREE_CODE (t2) != QUAL_UNION_TYPE
4908 && TREE_CODE (t2) != UNION_TYPE)
4909 return -1;
4910 if (TYPE_SIZE (t2) == 0)
4911 return -1;
4913 /* These are the only cases that need special handling. */
4914 if (TREE_CODE (t) != RECORD_TYPE
4915 && TREE_CODE (t) != ENUMERAL_TYPE
4916 && TREE_CODE (t) != QUAL_UNION_TYPE
4917 && TREE_CODE (t) != UNION_TYPE
4918 && TREE_CODE (t) != POINTER_TYPE)
4919 return -1;
4920 /* Undefined? */
4921 if (TYPE_SIZE (t) == 0)
4922 return -1;
4924 /* Look up t in hash table. Only one of the compatible types within each
4925 alias set is recorded in the table. */
4926 if (!type_hash_table)
4927 type_hash_table = htab_create_ggc (1021, c_type_hash,
4928 (htab_eq) lang_hooks.types_compatible_p,
4929 NULL);
4930 slot = htab_find_slot (type_hash_table, t, INSERT);
4931 if (*slot != NULL)
4933 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4934 return TYPE_ALIAS_SET ((tree)*slot);
4936 else
4937 /* Our caller will assign and record (in t) a new alias set; all we need
4938 to do is remember t in the hash table. */
4939 *slot = t;
4941 return -1;
4944 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4945 the IS_SIZEOF parameter indicates which operator is being applied.
4946 The COMPLAIN flag controls whether we should diagnose possibly
4947 ill-formed constructs or not. LOC is the location of the SIZEOF or
4948 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4949 a type in any context should be returned, rather than the normal
4950 alignment for that type. */
4952 tree
4953 c_sizeof_or_alignof_type (location_t loc,
4954 tree type, bool is_sizeof, bool min_alignof,
4955 int complain)
4957 const char *op_name;
4958 tree value = NULL;
4959 enum tree_code type_code = TREE_CODE (type);
4961 op_name = is_sizeof ? "sizeof" : "__alignof__";
4963 if (type_code == FUNCTION_TYPE)
4965 if (is_sizeof)
4967 if (complain && warn_pointer_arith)
4968 pedwarn (loc, OPT_Wpointer_arith,
4969 "invalid application of %<sizeof%> to a function type");
4970 else if (!complain)
4971 return error_mark_node;
4972 value = size_one_node;
4974 else
4976 if (complain)
4978 if (c_dialect_cxx ())
4979 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4980 "%<alignof%> applied to a function type");
4981 else
4982 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4983 "%<_Alignof%> applied to a function type");
4985 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4988 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4990 if (type_code == VOID_TYPE
4991 && complain && warn_pointer_arith)
4992 pedwarn (loc, OPT_Wpointer_arith,
4993 "invalid application of %qs to a void type", op_name);
4994 else if (!complain)
4995 return error_mark_node;
4996 value = size_one_node;
4998 else if (!COMPLETE_TYPE_P (type)
4999 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5001 if (complain)
5002 error_at (loc, "invalid application of %qs to incomplete type %qT",
5003 op_name, type);
5004 return error_mark_node;
5006 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5007 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5009 if (complain)
5010 error_at (loc, "invalid application of %qs to array type %qT of "
5011 "incomplete element type", op_name, type);
5012 return error_mark_node;
5014 else
5016 if (is_sizeof)
5017 /* Convert in case a char is more than one unit. */
5018 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5019 size_int (TYPE_PRECISION (char_type_node)
5020 / BITS_PER_UNIT));
5021 else if (min_alignof)
5023 unsigned int align = TYPE_ALIGN (type);
5024 align = MIN (align, BIGGEST_ALIGNMENT);
5025 #ifdef BIGGEST_FIELD_ALIGNMENT
5026 align = MIN (align, BIGGEST_FIELD_ALIGNMENT);
5027 #endif
5028 unsigned int field_align = align;
5029 #ifdef ADJUST_FIELD_ALIGN
5030 tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
5031 type);
5032 field_align = ADJUST_FIELD_ALIGN (field, field_align);
5033 #endif
5034 align = MIN (align, field_align);
5035 value = size_int (align / BITS_PER_UNIT);
5037 else
5038 value = size_int (TYPE_ALIGN_UNIT (type));
5041 /* VALUE will have the middle-end integer type sizetype.
5042 However, we should really return a value of type `size_t',
5043 which is just a typedef for an ordinary integer type. */
5044 value = fold_convert_loc (loc, size_type_node, value);
5046 return value;
5049 /* Implement the __alignof keyword: Return the minimum required
5050 alignment of EXPR, measured in bytes. For VAR_DECLs,
5051 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5052 from an "aligned" __attribute__ specification). LOC is the
5053 location of the ALIGNOF operator. */
5055 tree
5056 c_alignof_expr (location_t loc, tree expr)
5058 tree t;
5060 if (VAR_OR_FUNCTION_DECL_P (expr))
5061 t = size_int (DECL_ALIGN_UNIT (expr));
5063 else if (TREE_CODE (expr) == COMPONENT_REF
5064 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5066 error_at (loc, "%<__alignof%> applied to a bit-field");
5067 t = size_one_node;
5069 else if (TREE_CODE (expr) == COMPONENT_REF
5070 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5071 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5073 else if (TREE_CODE (expr) == INDIRECT_REF)
5075 tree t = TREE_OPERAND (expr, 0);
5076 tree best = t;
5077 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5079 while (CONVERT_EXPR_P (t)
5080 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5082 int thisalign;
5084 t = TREE_OPERAND (t, 0);
5085 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5086 if (thisalign > bestalign)
5087 best = t, bestalign = thisalign;
5089 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5091 else
5092 return c_alignof (loc, TREE_TYPE (expr));
5094 return fold_convert_loc (loc, size_type_node, t);
5097 /* Handle C and C++ default attributes. */
5099 enum built_in_attribute
5101 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5102 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5103 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5104 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5105 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5106 #include "builtin-attrs.def"
5107 #undef DEF_ATTR_NULL_TREE
5108 #undef DEF_ATTR_INT
5109 #undef DEF_ATTR_STRING
5110 #undef DEF_ATTR_IDENT
5111 #undef DEF_ATTR_TREE_LIST
5112 ATTR_LAST
5115 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5117 static void c_init_attributes (void);
5119 enum c_builtin_type
5121 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5122 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5123 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5124 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5125 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5126 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5127 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5128 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5129 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5130 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5131 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5132 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5133 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5134 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5135 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5136 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5137 NAME,
5138 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5139 #include "builtin-types.def"
5140 #undef DEF_PRIMITIVE_TYPE
5141 #undef DEF_FUNCTION_TYPE_0
5142 #undef DEF_FUNCTION_TYPE_1
5143 #undef DEF_FUNCTION_TYPE_2
5144 #undef DEF_FUNCTION_TYPE_3
5145 #undef DEF_FUNCTION_TYPE_4
5146 #undef DEF_FUNCTION_TYPE_5
5147 #undef DEF_FUNCTION_TYPE_6
5148 #undef DEF_FUNCTION_TYPE_7
5149 #undef DEF_FUNCTION_TYPE_8
5150 #undef DEF_FUNCTION_TYPE_VAR_0
5151 #undef DEF_FUNCTION_TYPE_VAR_1
5152 #undef DEF_FUNCTION_TYPE_VAR_2
5153 #undef DEF_FUNCTION_TYPE_VAR_3
5154 #undef DEF_FUNCTION_TYPE_VAR_4
5155 #undef DEF_FUNCTION_TYPE_VAR_5
5156 #undef DEF_POINTER_TYPE
5157 BT_LAST
5160 typedef enum c_builtin_type builtin_type;
5162 /* A temporary array for c_common_nodes_and_builtins. Used in
5163 communication with def_fn_type. */
5164 static tree builtin_types[(int) BT_LAST + 1];
5166 /* A helper function for c_common_nodes_and_builtins. Build function type
5167 for DEF with return type RET and N arguments. If VAR is true, then the
5168 function should be variadic after those N arguments.
5170 Takes special care not to ICE if any of the types involved are
5171 error_mark_node, which indicates that said type is not in fact available
5172 (see builtin_type_for_size). In which case the function type as a whole
5173 should be error_mark_node. */
5175 static void
5176 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5178 tree t;
5179 tree *args = XALLOCAVEC (tree, n);
5180 va_list list;
5181 int i;
5183 va_start (list, n);
5184 for (i = 0; i < n; ++i)
5186 builtin_type a = (builtin_type) va_arg (list, int);
5187 t = builtin_types[a];
5188 if (t == error_mark_node)
5189 goto egress;
5190 args[i] = t;
5193 t = builtin_types[ret];
5194 if (t == error_mark_node)
5195 goto egress;
5196 if (var)
5197 t = build_varargs_function_type_array (t, n, args);
5198 else
5199 t = build_function_type_array (t, n, args);
5201 egress:
5202 builtin_types[def] = t;
5203 va_end (list);
5206 /* Build builtin functions common to both C and C++ language
5207 frontends. */
5209 static void
5210 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5212 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5213 builtin_types[ENUM] = VALUE;
5214 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5215 def_fn_type (ENUM, RETURN, 0, 0);
5216 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5217 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5218 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5219 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5220 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5221 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5222 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5223 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5224 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5225 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5226 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5227 ARG6) \
5228 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5229 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5230 ARG6, ARG7) \
5231 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5232 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5233 ARG6, ARG7, ARG8) \
5234 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5235 ARG7, ARG8);
5236 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5237 def_fn_type (ENUM, RETURN, 1, 0);
5238 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5239 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5240 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5241 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5242 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5243 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5244 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5245 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5246 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5247 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5248 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5249 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5251 #include "builtin-types.def"
5253 #undef DEF_PRIMITIVE_TYPE
5254 #undef DEF_FUNCTION_TYPE_1
5255 #undef DEF_FUNCTION_TYPE_2
5256 #undef DEF_FUNCTION_TYPE_3
5257 #undef DEF_FUNCTION_TYPE_4
5258 #undef DEF_FUNCTION_TYPE_5
5259 #undef DEF_FUNCTION_TYPE_6
5260 #undef DEF_FUNCTION_TYPE_VAR_0
5261 #undef DEF_FUNCTION_TYPE_VAR_1
5262 #undef DEF_FUNCTION_TYPE_VAR_2
5263 #undef DEF_FUNCTION_TYPE_VAR_3
5264 #undef DEF_FUNCTION_TYPE_VAR_4
5265 #undef DEF_FUNCTION_TYPE_VAR_5
5266 #undef DEF_POINTER_TYPE
5267 builtin_types[(int) BT_LAST] = NULL_TREE;
5269 c_init_attributes ();
5271 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5272 NONANSI_P, ATTRS, IMPLICIT, COND) \
5273 if (NAME && COND) \
5274 def_builtin_1 (ENUM, NAME, CLASS, \
5275 builtin_types[(int) TYPE], \
5276 builtin_types[(int) LIBTYPE], \
5277 BOTH_P, FALLBACK_P, NONANSI_P, \
5278 built_in_attributes[(int) ATTRS], IMPLICIT);
5279 #include "builtins.def"
5280 #undef DEF_BUILTIN
5282 targetm.init_builtins ();
5284 build_common_builtin_nodes ();
5286 if (flag_cilkplus)
5287 cilk_init_builtins ();
5290 /* Like get_identifier, but avoid warnings about null arguments when
5291 the argument may be NULL for targets where GCC lacks stdint.h type
5292 information. */
5294 static inline tree
5295 c_get_ident (const char *id)
5297 return get_identifier (id);
5300 /* Build tree nodes and builtin functions common to both C and C++ language
5301 frontends. */
5303 void
5304 c_common_nodes_and_builtins (void)
5306 int char16_type_size;
5307 int char32_type_size;
5308 int wchar_type_size;
5309 tree array_domain_type;
5310 tree va_list_ref_type_node;
5311 tree va_list_arg_type_node;
5313 build_common_tree_nodes (flag_signed_char, flag_short_double);
5315 /* Define `int' and `char' first so that dbx will output them first. */
5316 record_builtin_type (RID_INT, NULL, integer_type_node);
5317 record_builtin_type (RID_CHAR, "char", char_type_node);
5319 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5320 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5321 but not C. Are the conditionals here needed? */
5322 if (c_dialect_cxx ())
5323 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5324 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5325 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5326 record_builtin_type (RID_MAX, "long unsigned int",
5327 long_unsigned_type_node);
5328 if (int128_integer_type_node != NULL_TREE)
5330 record_builtin_type (RID_INT128, "__int128",
5331 int128_integer_type_node);
5332 record_builtin_type (RID_MAX, "__int128 unsigned",
5333 int128_unsigned_type_node);
5335 if (c_dialect_cxx ())
5336 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5337 record_builtin_type (RID_MAX, "long long int",
5338 long_long_integer_type_node);
5339 record_builtin_type (RID_MAX, "long long unsigned int",
5340 long_long_unsigned_type_node);
5341 if (c_dialect_cxx ())
5342 record_builtin_type (RID_MAX, "long long unsigned",
5343 long_long_unsigned_type_node);
5344 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5345 record_builtin_type (RID_MAX, "short unsigned int",
5346 short_unsigned_type_node);
5347 if (c_dialect_cxx ())
5348 record_builtin_type (RID_MAX, "unsigned short",
5349 short_unsigned_type_node);
5351 /* Define both `signed char' and `unsigned char'. */
5352 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5353 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5355 /* These are types that c_common_type_for_size and
5356 c_common_type_for_mode use. */
5357 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5358 TYPE_DECL, NULL_TREE,
5359 intQI_type_node));
5360 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5361 TYPE_DECL, NULL_TREE,
5362 intHI_type_node));
5363 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5364 TYPE_DECL, NULL_TREE,
5365 intSI_type_node));
5366 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5367 TYPE_DECL, NULL_TREE,
5368 intDI_type_node));
5369 #if HOST_BITS_PER_WIDE_INT >= 64
5370 if (targetm.scalar_mode_supported_p (TImode))
5371 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5372 TYPE_DECL,
5373 get_identifier ("__int128_t"),
5374 intTI_type_node));
5375 #endif
5376 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5377 TYPE_DECL, NULL_TREE,
5378 unsigned_intQI_type_node));
5379 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5380 TYPE_DECL, NULL_TREE,
5381 unsigned_intHI_type_node));
5382 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5383 TYPE_DECL, NULL_TREE,
5384 unsigned_intSI_type_node));
5385 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5386 TYPE_DECL, NULL_TREE,
5387 unsigned_intDI_type_node));
5388 #if HOST_BITS_PER_WIDE_INT >= 64
5389 if (targetm.scalar_mode_supported_p (TImode))
5390 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5391 TYPE_DECL,
5392 get_identifier ("__uint128_t"),
5393 unsigned_intTI_type_node));
5394 #endif
5396 /* Create the widest literal types. */
5397 widest_integer_literal_type_node
5398 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5399 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5400 TYPE_DECL, NULL_TREE,
5401 widest_integer_literal_type_node));
5403 widest_unsigned_literal_type_node
5404 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5405 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5406 TYPE_DECL, NULL_TREE,
5407 widest_unsigned_literal_type_node));
5409 signed_size_type_node = c_common_signed_type (size_type_node);
5411 pid_type_node =
5412 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5414 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5415 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5416 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5418 /* Only supported decimal floating point extension if the target
5419 actually supports underlying modes. */
5420 if (targetm.scalar_mode_supported_p (SDmode)
5421 && targetm.scalar_mode_supported_p (DDmode)
5422 && targetm.scalar_mode_supported_p (TDmode))
5424 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5425 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5426 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5429 if (targetm.fixed_point_supported_p ())
5431 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5432 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5433 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5434 record_builtin_type (RID_MAX, "long long _Fract",
5435 long_long_fract_type_node);
5436 record_builtin_type (RID_MAX, "unsigned short _Fract",
5437 unsigned_short_fract_type_node);
5438 record_builtin_type (RID_MAX, "unsigned _Fract",
5439 unsigned_fract_type_node);
5440 record_builtin_type (RID_MAX, "unsigned long _Fract",
5441 unsigned_long_fract_type_node);
5442 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5443 unsigned_long_long_fract_type_node);
5444 record_builtin_type (RID_MAX, "_Sat short _Fract",
5445 sat_short_fract_type_node);
5446 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5447 record_builtin_type (RID_MAX, "_Sat long _Fract",
5448 sat_long_fract_type_node);
5449 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5450 sat_long_long_fract_type_node);
5451 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5452 sat_unsigned_short_fract_type_node);
5453 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5454 sat_unsigned_fract_type_node);
5455 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5456 sat_unsigned_long_fract_type_node);
5457 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5458 sat_unsigned_long_long_fract_type_node);
5459 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5460 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5461 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5462 record_builtin_type (RID_MAX, "long long _Accum",
5463 long_long_accum_type_node);
5464 record_builtin_type (RID_MAX, "unsigned short _Accum",
5465 unsigned_short_accum_type_node);
5466 record_builtin_type (RID_MAX, "unsigned _Accum",
5467 unsigned_accum_type_node);
5468 record_builtin_type (RID_MAX, "unsigned long _Accum",
5469 unsigned_long_accum_type_node);
5470 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5471 unsigned_long_long_accum_type_node);
5472 record_builtin_type (RID_MAX, "_Sat short _Accum",
5473 sat_short_accum_type_node);
5474 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5475 record_builtin_type (RID_MAX, "_Sat long _Accum",
5476 sat_long_accum_type_node);
5477 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5478 sat_long_long_accum_type_node);
5479 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5480 sat_unsigned_short_accum_type_node);
5481 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5482 sat_unsigned_accum_type_node);
5483 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5484 sat_unsigned_long_accum_type_node);
5485 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5486 sat_unsigned_long_long_accum_type_node);
5490 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5491 TYPE_DECL,
5492 get_identifier ("complex int"),
5493 complex_integer_type_node));
5494 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5495 TYPE_DECL,
5496 get_identifier ("complex float"),
5497 complex_float_type_node));
5498 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5499 TYPE_DECL,
5500 get_identifier ("complex double"),
5501 complex_double_type_node));
5502 lang_hooks.decls.pushdecl
5503 (build_decl (UNKNOWN_LOCATION,
5504 TYPE_DECL, get_identifier ("complex long double"),
5505 complex_long_double_type_node));
5507 if (c_dialect_cxx ())
5508 /* For C++, make fileptr_type_node a distinct void * type until
5509 FILE type is defined. */
5510 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5512 record_builtin_type (RID_VOID, NULL, void_type_node);
5514 /* Set the TYPE_NAME for any variants that were built before
5515 record_builtin_type gave names to the built-in types. */
5517 tree void_name = TYPE_NAME (void_type_node);
5518 TYPE_NAME (void_type_node) = NULL_TREE;
5519 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5520 = void_name;
5521 TYPE_NAME (void_type_node) = void_name;
5524 /* This node must not be shared. */
5525 void_zero_node = make_node (INTEGER_CST);
5526 TREE_TYPE (void_zero_node) = void_type_node;
5528 void_list_node = build_void_list_node ();
5530 /* Make a type to be the domain of a few array types
5531 whose domains don't really matter.
5532 200 is small enough that it always fits in size_t
5533 and large enough that it can hold most function names for the
5534 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5535 array_domain_type = build_index_type (size_int (200));
5537 /* Make a type for arrays of characters.
5538 With luck nothing will ever really depend on the length of this
5539 array type. */
5540 char_array_type_node
5541 = build_array_type (char_type_node, array_domain_type);
5543 string_type_node = build_pointer_type (char_type_node);
5544 const_string_type_node
5545 = build_pointer_type (build_qualified_type
5546 (char_type_node, TYPE_QUAL_CONST));
5548 /* This is special for C++ so functions can be overloaded. */
5549 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5550 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5551 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5552 underlying_wchar_type_node = wchar_type_node;
5553 if (c_dialect_cxx ())
5555 if (TYPE_UNSIGNED (wchar_type_node))
5556 wchar_type_node = make_unsigned_type (wchar_type_size);
5557 else
5558 wchar_type_node = make_signed_type (wchar_type_size);
5559 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5562 /* This is for wide string constants. */
5563 wchar_array_type_node
5564 = build_array_type (wchar_type_node, array_domain_type);
5566 /* Define 'char16_t'. */
5567 char16_type_node = get_identifier (CHAR16_TYPE);
5568 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5569 char16_type_size = TYPE_PRECISION (char16_type_node);
5570 if (c_dialect_cxx ())
5572 char16_type_node = make_unsigned_type (char16_type_size);
5574 if (cxx_dialect >= cxx11)
5575 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5578 /* This is for UTF-16 string constants. */
5579 char16_array_type_node
5580 = build_array_type (char16_type_node, array_domain_type);
5582 /* Define 'char32_t'. */
5583 char32_type_node = get_identifier (CHAR32_TYPE);
5584 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5585 char32_type_size = TYPE_PRECISION (char32_type_node);
5586 if (c_dialect_cxx ())
5588 char32_type_node = make_unsigned_type (char32_type_size);
5590 if (cxx_dialect >= cxx11)
5591 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5594 /* This is for UTF-32 string constants. */
5595 char32_array_type_node
5596 = build_array_type (char32_type_node, array_domain_type);
5598 wint_type_node =
5599 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5601 intmax_type_node =
5602 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5603 uintmax_type_node =
5604 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5606 if (SIG_ATOMIC_TYPE)
5607 sig_atomic_type_node =
5608 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5609 if (INT8_TYPE)
5610 int8_type_node =
5611 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5612 if (INT16_TYPE)
5613 int16_type_node =
5614 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5615 if (INT32_TYPE)
5616 int32_type_node =
5617 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5618 if (INT64_TYPE)
5619 int64_type_node =
5620 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5621 if (UINT8_TYPE)
5622 uint8_type_node =
5623 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5624 if (UINT16_TYPE)
5625 c_uint16_type_node = uint16_type_node =
5626 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5627 if (UINT32_TYPE)
5628 c_uint32_type_node = uint32_type_node =
5629 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5630 if (UINT64_TYPE)
5631 c_uint64_type_node = uint64_type_node =
5632 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5633 if (INT_LEAST8_TYPE)
5634 int_least8_type_node =
5635 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5636 if (INT_LEAST16_TYPE)
5637 int_least16_type_node =
5638 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5639 if (INT_LEAST32_TYPE)
5640 int_least32_type_node =
5641 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5642 if (INT_LEAST64_TYPE)
5643 int_least64_type_node =
5644 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5645 if (UINT_LEAST8_TYPE)
5646 uint_least8_type_node =
5647 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5648 if (UINT_LEAST16_TYPE)
5649 uint_least16_type_node =
5650 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5651 if (UINT_LEAST32_TYPE)
5652 uint_least32_type_node =
5653 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5654 if (UINT_LEAST64_TYPE)
5655 uint_least64_type_node =
5656 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5657 if (INT_FAST8_TYPE)
5658 int_fast8_type_node =
5659 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5660 if (INT_FAST16_TYPE)
5661 int_fast16_type_node =
5662 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5663 if (INT_FAST32_TYPE)
5664 int_fast32_type_node =
5665 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5666 if (INT_FAST64_TYPE)
5667 int_fast64_type_node =
5668 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5669 if (UINT_FAST8_TYPE)
5670 uint_fast8_type_node =
5671 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5672 if (UINT_FAST16_TYPE)
5673 uint_fast16_type_node =
5674 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5675 if (UINT_FAST32_TYPE)
5676 uint_fast32_type_node =
5677 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5678 if (UINT_FAST64_TYPE)
5679 uint_fast64_type_node =
5680 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5681 if (INTPTR_TYPE)
5682 intptr_type_node =
5683 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5684 if (UINTPTR_TYPE)
5685 uintptr_type_node =
5686 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5688 default_function_type
5689 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5690 ptrdiff_type_node
5691 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5692 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5694 lang_hooks.decls.pushdecl
5695 (build_decl (UNKNOWN_LOCATION,
5696 TYPE_DECL, get_identifier ("__builtin_va_list"),
5697 va_list_type_node));
5698 if (targetm.enum_va_list_p)
5700 int l;
5701 const char *pname;
5702 tree ptype;
5704 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5706 lang_hooks.decls.pushdecl
5707 (build_decl (UNKNOWN_LOCATION,
5708 TYPE_DECL, get_identifier (pname),
5709 ptype));
5714 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5716 va_list_arg_type_node = va_list_ref_type_node =
5717 build_pointer_type (TREE_TYPE (va_list_type_node));
5719 else
5721 va_list_arg_type_node = va_list_type_node;
5722 va_list_ref_type_node = build_reference_type (va_list_type_node);
5725 if (!flag_preprocess_only)
5726 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5728 main_identifier_node = get_identifier ("main");
5730 /* Create the built-in __null node. It is important that this is
5731 not shared. */
5732 null_node = make_node (INTEGER_CST);
5733 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5735 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5736 memset (builtin_types, 0, sizeof (builtin_types));
5739 /* The number of named compound-literals generated thus far. */
5740 static GTY(()) int compound_literal_number;
5742 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5744 void
5745 set_compound_literal_name (tree decl)
5747 char *name;
5748 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5749 compound_literal_number);
5750 compound_literal_number++;
5751 DECL_NAME (decl) = get_identifier (name);
5754 tree
5755 build_va_arg (location_t loc, tree expr, tree type)
5757 expr = build1 (VA_ARG_EXPR, type, expr);
5758 SET_EXPR_LOCATION (expr, loc);
5759 return expr;
5763 /* Linked list of disabled built-in functions. */
5765 typedef struct disabled_builtin
5767 const char *name;
5768 struct disabled_builtin *next;
5769 } disabled_builtin;
5770 static disabled_builtin *disabled_builtins = NULL;
5772 static bool builtin_function_disabled_p (const char *);
5774 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5775 begins with "__builtin_", give an error. */
5777 void
5778 disable_builtin_function (const char *name)
5780 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5781 error ("cannot disable built-in function %qs", name);
5782 else
5784 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5785 new_disabled_builtin->name = name;
5786 new_disabled_builtin->next = disabled_builtins;
5787 disabled_builtins = new_disabled_builtin;
5792 /* Return true if the built-in function NAME has been disabled, false
5793 otherwise. */
5795 static bool
5796 builtin_function_disabled_p (const char *name)
5798 disabled_builtin *p;
5799 for (p = disabled_builtins; p != NULL; p = p->next)
5801 if (strcmp (name, p->name) == 0)
5802 return true;
5804 return false;
5808 /* Worker for DEF_BUILTIN.
5809 Possibly define a builtin function with one or two names.
5810 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5811 nonansi_p and flag_no_nonansi_builtin. */
5813 static void
5814 def_builtin_1 (enum built_in_function fncode,
5815 const char *name,
5816 enum built_in_class fnclass,
5817 tree fntype, tree libtype,
5818 bool both_p, bool fallback_p, bool nonansi_p,
5819 tree fnattrs, bool implicit_p)
5821 tree decl;
5822 const char *libname;
5824 if (fntype == error_mark_node)
5825 return;
5827 gcc_assert ((!both_p && !fallback_p)
5828 || !strncmp (name, "__builtin_",
5829 strlen ("__builtin_")));
5831 libname = name + strlen ("__builtin_");
5832 decl = add_builtin_function (name, fntype, fncode, fnclass,
5833 (fallback_p ? libname : NULL),
5834 fnattrs);
5836 set_builtin_decl (fncode, decl, implicit_p);
5838 if (both_p
5839 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5840 && !(nonansi_p && flag_no_nonansi_builtin))
5841 add_builtin_function (libname, libtype, fncode, fnclass,
5842 NULL, fnattrs);
5845 /* Nonzero if the type T promotes to int. This is (nearly) the
5846 integral promotions defined in ISO C99 6.3.1.1/2. */
5848 bool
5849 c_promoting_integer_type_p (const_tree t)
5851 switch (TREE_CODE (t))
5853 case INTEGER_TYPE:
5854 return (TYPE_MAIN_VARIANT (t) == char_type_node
5855 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5856 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5857 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5858 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5859 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5861 case ENUMERAL_TYPE:
5862 /* ??? Technically all enumerations not larger than an int
5863 promote to an int. But this is used along code paths
5864 that only want to notice a size change. */
5865 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5867 case BOOLEAN_TYPE:
5868 return 1;
5870 default:
5871 return 0;
5875 /* Return 1 if PARMS specifies a fixed number of parameters
5876 and none of their types is affected by default promotions. */
5879 self_promoting_args_p (const_tree parms)
5881 const_tree t;
5882 for (t = parms; t; t = TREE_CHAIN (t))
5884 tree type = TREE_VALUE (t);
5886 if (type == error_mark_node)
5887 continue;
5889 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5890 return 0;
5892 if (type == 0)
5893 return 0;
5895 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5896 return 0;
5898 if (c_promoting_integer_type_p (type))
5899 return 0;
5901 return 1;
5904 /* Recursively remove any '*' or '&' operator from TYPE. */
5905 tree
5906 strip_pointer_operator (tree t)
5908 while (POINTER_TYPE_P (t))
5909 t = TREE_TYPE (t);
5910 return t;
5913 /* Recursively remove pointer or array type from TYPE. */
5914 tree
5915 strip_pointer_or_array_types (tree t)
5917 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5918 t = TREE_TYPE (t);
5919 return t;
5922 /* Used to compare case labels. K1 and K2 are actually tree nodes
5923 representing case labels, or NULL_TREE for a `default' label.
5924 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5925 K2, and 0 if K1 and K2 are equal. */
5928 case_compare (splay_tree_key k1, splay_tree_key k2)
5930 /* Consider a NULL key (such as arises with a `default' label) to be
5931 smaller than anything else. */
5932 if (!k1)
5933 return k2 ? -1 : 0;
5934 else if (!k2)
5935 return k1 ? 1 : 0;
5937 return tree_int_cst_compare ((tree) k1, (tree) k2);
5940 /* Process a case label, located at LOC, for the range LOW_VALUE
5941 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5942 then this case label is actually a `default' label. If only
5943 HIGH_VALUE is NULL_TREE, then case label was declared using the
5944 usual C/C++ syntax, rather than the GNU case range extension.
5945 CASES is a tree containing all the case ranges processed so far;
5946 COND is the condition for the switch-statement itself. Returns the
5947 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5948 is created. */
5950 tree
5951 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5952 tree low_value, tree high_value)
5954 tree type;
5955 tree label;
5956 tree case_label;
5957 splay_tree_node node;
5959 /* Create the LABEL_DECL itself. */
5960 label = create_artificial_label (loc);
5962 /* If there was an error processing the switch condition, bail now
5963 before we get more confused. */
5964 if (!cond || cond == error_mark_node)
5965 goto error_out;
5967 if ((low_value && TREE_TYPE (low_value)
5968 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5969 || (high_value && TREE_TYPE (high_value)
5970 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5972 error_at (loc, "pointers are not permitted as case values");
5973 goto error_out;
5976 /* Case ranges are a GNU extension. */
5977 if (high_value)
5978 pedwarn (loc, OPT_Wpedantic,
5979 "range expressions in switch statements are non-standard");
5981 type = TREE_TYPE (cond);
5982 if (low_value)
5984 low_value = check_case_value (low_value);
5985 low_value = convert_and_check (loc, type, low_value);
5986 if (low_value == error_mark_node)
5987 goto error_out;
5989 if (high_value)
5991 high_value = check_case_value (high_value);
5992 high_value = convert_and_check (loc, type, high_value);
5993 if (high_value == error_mark_node)
5994 goto error_out;
5997 if (low_value && high_value)
5999 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6000 really a case range, even though it was written that way.
6001 Remove the HIGH_VALUE to simplify later processing. */
6002 if (tree_int_cst_equal (low_value, high_value))
6003 high_value = NULL_TREE;
6004 else if (!tree_int_cst_lt (low_value, high_value))
6005 warning_at (loc, 0, "empty range specified");
6008 /* See if the case is in range of the type of the original testing
6009 expression. If both low_value and high_value are out of range,
6010 don't insert the case label and return NULL_TREE. */
6011 if (low_value
6012 && !check_case_bounds (type, orig_type,
6013 &low_value, high_value ? &high_value : NULL))
6014 return NULL_TREE;
6016 /* Look up the LOW_VALUE in the table of case labels we already
6017 have. */
6018 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6019 /* If there was not an exact match, check for overlapping ranges.
6020 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6021 that's a `default' label and the only overlap is an exact match. */
6022 if (!node && (low_value || high_value))
6024 splay_tree_node low_bound;
6025 splay_tree_node high_bound;
6027 /* Even though there wasn't an exact match, there might be an
6028 overlap between this case range and another case range.
6029 Since we've (inductively) not allowed any overlapping case
6030 ranges, we simply need to find the greatest low case label
6031 that is smaller that LOW_VALUE, and the smallest low case
6032 label that is greater than LOW_VALUE. If there is an overlap
6033 it will occur in one of these two ranges. */
6034 low_bound = splay_tree_predecessor (cases,
6035 (splay_tree_key) low_value);
6036 high_bound = splay_tree_successor (cases,
6037 (splay_tree_key) low_value);
6039 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6040 the LOW_VALUE, so there is no need to check unless the
6041 LOW_BOUND is in fact itself a case range. */
6042 if (low_bound
6043 && CASE_HIGH ((tree) low_bound->value)
6044 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6045 low_value) >= 0)
6046 node = low_bound;
6047 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6048 range is bigger than the low end of the current range, so we
6049 are only interested if the current range is a real range, and
6050 not an ordinary case label. */
6051 else if (high_bound
6052 && high_value
6053 && (tree_int_cst_compare ((tree) high_bound->key,
6054 high_value)
6055 <= 0))
6056 node = high_bound;
6058 /* If there was an overlap, issue an error. */
6059 if (node)
6061 tree duplicate = CASE_LABEL ((tree) node->value);
6063 if (high_value)
6065 error_at (loc, "duplicate (or overlapping) case value");
6066 error_at (DECL_SOURCE_LOCATION (duplicate),
6067 "this is the first entry overlapping that value");
6069 else if (low_value)
6071 error_at (loc, "duplicate case value") ;
6072 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6074 else
6076 error_at (loc, "multiple default labels in one switch");
6077 error_at (DECL_SOURCE_LOCATION (duplicate),
6078 "this is the first default label");
6080 goto error_out;
6083 /* Add a CASE_LABEL to the statement-tree. */
6084 case_label = add_stmt (build_case_label (low_value, high_value, label));
6085 /* Register this case label in the splay tree. */
6086 splay_tree_insert (cases,
6087 (splay_tree_key) low_value,
6088 (splay_tree_value) case_label);
6090 return case_label;
6092 error_out:
6093 /* Add a label so that the back-end doesn't think that the beginning of
6094 the switch is unreachable. Note that we do not add a case label, as
6095 that just leads to duplicates and thence to failure later on. */
6096 if (!cases->root)
6098 tree t = create_artificial_label (loc);
6099 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6101 return error_mark_node;
6104 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6105 Used to verify that case values match up with enumerator values. */
6107 static void
6108 match_case_to_enum_1 (tree key, tree type, tree label)
6110 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6112 /* ??? Not working too hard to print the double-word value.
6113 Should perhaps be done with %lwd in the diagnostic routines? */
6114 if (TREE_INT_CST_HIGH (key) == 0)
6115 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6116 TREE_INT_CST_LOW (key));
6117 else if (!TYPE_UNSIGNED (type)
6118 && TREE_INT_CST_HIGH (key) == -1
6119 && TREE_INT_CST_LOW (key) != 0)
6120 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6121 -TREE_INT_CST_LOW (key));
6122 else
6123 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6124 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6125 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6127 if (TYPE_NAME (type) == 0)
6128 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6129 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6130 "case value %qs not in enumerated type",
6131 buf);
6132 else
6133 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6134 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6135 "case value %qs not in enumerated type %qT",
6136 buf, type);
6139 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6140 Used to verify that case values match up with enumerator values. */
6142 static int
6143 match_case_to_enum (splay_tree_node node, void *data)
6145 tree label = (tree) node->value;
6146 tree type = (tree) data;
6148 /* Skip default case. */
6149 if (!CASE_LOW (label))
6150 return 0;
6152 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6153 when we did our enum->case scan. Reset our scratch bit after. */
6154 if (!CASE_LOW_SEEN (label))
6155 match_case_to_enum_1 (CASE_LOW (label), type, label);
6156 else
6157 CASE_LOW_SEEN (label) = 0;
6159 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6160 not set, that means that CASE_HIGH did not appear when we did our
6161 enum->case scan. Reset our scratch bit after. */
6162 if (CASE_HIGH (label))
6164 if (!CASE_HIGH_SEEN (label))
6165 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6166 else
6167 CASE_HIGH_SEEN (label) = 0;
6170 return 0;
6173 /* Handle -Wswitch*. Called from the front end after parsing the
6174 switch construct. */
6175 /* ??? Should probably be somewhere generic, since other languages
6176 besides C and C++ would want this. At the moment, however, C/C++
6177 are the only tree-ssa languages that support enumerations at all,
6178 so the point is moot. */
6180 void
6181 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6182 tree type, tree cond)
6184 splay_tree_node default_node;
6185 splay_tree_node node;
6186 tree chain;
6188 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6189 return;
6191 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6192 if (!default_node)
6193 warning_at (switch_location, OPT_Wswitch_default,
6194 "switch missing default case");
6196 /* From here on, we only care about about enumerated types. */
6197 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6198 return;
6200 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6201 if (!warn_switch_enum && !warn_switch)
6202 return;
6204 /* Check the cases. Warn about case values which are not members of
6205 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6206 there is no default case, check that exactly all enumeration
6207 literals are covered by the cases. */
6209 /* Clearing COND if it is not an integer constant simplifies
6210 the tests inside the loop below. */
6211 if (TREE_CODE (cond) != INTEGER_CST)
6212 cond = NULL_TREE;
6214 /* The time complexity here is O(N*lg(N)) worst case, but for the
6215 common case of monotonically increasing enumerators, it is
6216 O(N), since the nature of the splay tree will keep the next
6217 element adjacent to the root at all times. */
6219 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6221 tree value = TREE_VALUE (chain);
6222 if (TREE_CODE (value) == CONST_DECL)
6223 value = DECL_INITIAL (value);
6224 node = splay_tree_lookup (cases, (splay_tree_key) value);
6225 if (node)
6227 /* Mark the CASE_LOW part of the case entry as seen. */
6228 tree label = (tree) node->value;
6229 CASE_LOW_SEEN (label) = 1;
6230 continue;
6233 /* Even though there wasn't an exact match, there might be a
6234 case range which includes the enumerator's value. */
6235 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6236 if (node && CASE_HIGH ((tree) node->value))
6238 tree label = (tree) node->value;
6239 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6240 if (cmp >= 0)
6242 /* If we match the upper bound exactly, mark the CASE_HIGH
6243 part of the case entry as seen. */
6244 if (cmp == 0)
6245 CASE_HIGH_SEEN (label) = 1;
6246 continue;
6250 /* We've now determined that this enumerated literal isn't
6251 handled by the case labels of the switch statement. */
6253 /* If the switch expression is a constant, we only really care
6254 about whether that constant is handled by the switch. */
6255 if (cond && tree_int_cst_compare (cond, value))
6256 continue;
6258 /* If there is a default_node, the only relevant option is
6259 Wswitch-enum. Otherwise, if both are enabled then we prefer
6260 to warn using -Wswitch because -Wswitch is enabled by -Wall
6261 while -Wswitch-enum is explicit. */
6262 warning_at (switch_location,
6263 (default_node || !warn_switch
6264 ? OPT_Wswitch_enum
6265 : OPT_Wswitch),
6266 "enumeration value %qE not handled in switch",
6267 TREE_PURPOSE (chain));
6270 /* Warn if there are case expressions that don't correspond to
6271 enumerators. This can occur since C and C++ don't enforce
6272 type-checking of assignments to enumeration variables.
6274 The time complexity here is now always O(N) worst case, since
6275 we should have marked both the lower bound and upper bound of
6276 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6277 above. This scan also resets those fields. */
6279 splay_tree_foreach (cases, match_case_to_enum, type);
6282 /* Finish an expression taking the address of LABEL (an
6283 IDENTIFIER_NODE). Returns an expression for the address.
6285 LOC is the location for the expression returned. */
6287 tree
6288 finish_label_address_expr (tree label, location_t loc)
6290 tree result;
6292 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6294 if (label == error_mark_node)
6295 return error_mark_node;
6297 label = lookup_label (label);
6298 if (label == NULL_TREE)
6299 result = null_pointer_node;
6300 else
6302 TREE_USED (label) = 1;
6303 result = build1 (ADDR_EXPR, ptr_type_node, label);
6304 /* The current function is not necessarily uninlinable.
6305 Computed gotos are incompatible with inlining, but the value
6306 here could be used only in a diagnostic, for example. */
6307 protected_set_expr_location (result, loc);
6310 return result;
6314 /* Given a boolean expression ARG, return a tree representing an increment
6315 or decrement (as indicated by CODE) of ARG. The front end must check for
6316 invalid cases (e.g., decrement in C++). */
6317 tree
6318 boolean_increment (enum tree_code code, tree arg)
6320 tree val;
6321 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6323 arg = stabilize_reference (arg);
6324 switch (code)
6326 case PREINCREMENT_EXPR:
6327 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6328 break;
6329 case POSTINCREMENT_EXPR:
6330 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6331 arg = save_expr (arg);
6332 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6333 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6334 break;
6335 case PREDECREMENT_EXPR:
6336 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6337 invert_truthvalue_loc (input_location, arg));
6338 break;
6339 case POSTDECREMENT_EXPR:
6340 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6341 invert_truthvalue_loc (input_location, arg));
6342 arg = save_expr (arg);
6343 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6344 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6345 break;
6346 default:
6347 gcc_unreachable ();
6349 TREE_SIDE_EFFECTS (val) = 1;
6350 return val;
6353 /* Built-in macros for stddef.h and stdint.h, that require macros
6354 defined in this file. */
6355 void
6356 c_stddef_cpp_builtins(void)
6358 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6359 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6360 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6361 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6362 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6363 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6364 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6365 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6366 if (SIG_ATOMIC_TYPE)
6367 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6368 if (INT8_TYPE)
6369 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6370 if (INT16_TYPE)
6371 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6372 if (INT32_TYPE)
6373 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6374 if (INT64_TYPE)
6375 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6376 if (UINT8_TYPE)
6377 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6378 if (UINT16_TYPE)
6379 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6380 if (UINT32_TYPE)
6381 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6382 if (UINT64_TYPE)
6383 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6384 if (INT_LEAST8_TYPE)
6385 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6386 if (INT_LEAST16_TYPE)
6387 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6388 if (INT_LEAST32_TYPE)
6389 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6390 if (INT_LEAST64_TYPE)
6391 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6392 if (UINT_LEAST8_TYPE)
6393 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6394 if (UINT_LEAST16_TYPE)
6395 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6396 if (UINT_LEAST32_TYPE)
6397 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6398 if (UINT_LEAST64_TYPE)
6399 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6400 if (INT_FAST8_TYPE)
6401 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6402 if (INT_FAST16_TYPE)
6403 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6404 if (INT_FAST32_TYPE)
6405 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6406 if (INT_FAST64_TYPE)
6407 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6408 if (UINT_FAST8_TYPE)
6409 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6410 if (UINT_FAST16_TYPE)
6411 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6412 if (UINT_FAST32_TYPE)
6413 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6414 if (UINT_FAST64_TYPE)
6415 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6416 if (INTPTR_TYPE)
6417 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6418 if (UINTPTR_TYPE)
6419 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6422 static void
6423 c_init_attributes (void)
6425 /* Fill in the built_in_attributes array. */
6426 #define DEF_ATTR_NULL_TREE(ENUM) \
6427 built_in_attributes[(int) ENUM] = NULL_TREE;
6428 #define DEF_ATTR_INT(ENUM, VALUE) \
6429 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6430 #define DEF_ATTR_STRING(ENUM, VALUE) \
6431 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6432 #define DEF_ATTR_IDENT(ENUM, STRING) \
6433 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6434 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6435 built_in_attributes[(int) ENUM] \
6436 = tree_cons (built_in_attributes[(int) PURPOSE], \
6437 built_in_attributes[(int) VALUE], \
6438 built_in_attributes[(int) CHAIN]);
6439 #include "builtin-attrs.def"
6440 #undef DEF_ATTR_NULL_TREE
6441 #undef DEF_ATTR_INT
6442 #undef DEF_ATTR_IDENT
6443 #undef DEF_ATTR_TREE_LIST
6446 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6447 identifier as an argument, so the front end shouldn't look it up. */
6449 bool
6450 attribute_takes_identifier_p (const_tree attr_id)
6452 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6453 if (spec == NULL)
6454 /* Unknown attribute that we'll end up ignoring, return true so we
6455 don't complain about an identifier argument. */
6456 return true;
6457 else if (!strcmp ("mode", spec->name)
6458 || !strcmp ("format", spec->name)
6459 || !strcmp ("cleanup", spec->name))
6460 return true;
6461 else
6462 return targetm.attribute_takes_identifier_p (attr_id);
6465 /* Attribute handlers common to C front ends. */
6467 /* Handle a "packed" attribute; arguments as in
6468 struct attribute_spec.handler. */
6470 static tree
6471 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6472 int flags, bool *no_add_attrs)
6474 if (TYPE_P (*node))
6476 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6477 *node = build_variant_type_copy (*node);
6478 TYPE_PACKED (*node) = 1;
6480 else if (TREE_CODE (*node) == FIELD_DECL)
6482 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6483 /* Still pack bitfields. */
6484 && ! DECL_INITIAL (*node))
6485 warning (OPT_Wattributes,
6486 "%qE attribute ignored for field of type %qT",
6487 name, TREE_TYPE (*node));
6488 else
6489 DECL_PACKED (*node) = 1;
6491 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6492 used for DECL_REGISTER. It wouldn't mean anything anyway.
6493 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6494 that changes what the typedef is typing. */
6495 else
6497 warning (OPT_Wattributes, "%qE attribute ignored", name);
6498 *no_add_attrs = true;
6501 return NULL_TREE;
6504 /* Handle a "nocommon" attribute; arguments as in
6505 struct attribute_spec.handler. */
6507 static tree
6508 handle_nocommon_attribute (tree *node, tree name,
6509 tree ARG_UNUSED (args),
6510 int ARG_UNUSED (flags), bool *no_add_attrs)
6512 if (TREE_CODE (*node) == VAR_DECL)
6513 DECL_COMMON (*node) = 0;
6514 else
6516 warning (OPT_Wattributes, "%qE attribute ignored", name);
6517 *no_add_attrs = true;
6520 return NULL_TREE;
6523 /* Handle a "common" attribute; arguments as in
6524 struct attribute_spec.handler. */
6526 static tree
6527 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6528 int ARG_UNUSED (flags), bool *no_add_attrs)
6530 if (TREE_CODE (*node) == VAR_DECL)
6531 DECL_COMMON (*node) = 1;
6532 else
6534 warning (OPT_Wattributes, "%qE attribute ignored", name);
6535 *no_add_attrs = true;
6538 return NULL_TREE;
6541 /* Handle a "noreturn" attribute; arguments as in
6542 struct attribute_spec.handler. */
6544 static tree
6545 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6546 int ARG_UNUSED (flags), bool *no_add_attrs)
6548 tree type = TREE_TYPE (*node);
6550 /* See FIXME comment in c_common_attribute_table. */
6551 if (TREE_CODE (*node) == FUNCTION_DECL
6552 || objc_method_decl (TREE_CODE (*node)))
6553 TREE_THIS_VOLATILE (*node) = 1;
6554 else if (TREE_CODE (type) == POINTER_TYPE
6555 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6556 TREE_TYPE (*node)
6557 = build_pointer_type
6558 (build_type_variant (TREE_TYPE (type),
6559 TYPE_READONLY (TREE_TYPE (type)), 1));
6560 else
6562 warning (OPT_Wattributes, "%qE attribute ignored", name);
6563 *no_add_attrs = true;
6566 return NULL_TREE;
6569 /* Handle a "hot" and attribute; arguments as in
6570 struct attribute_spec.handler. */
6572 static tree
6573 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6574 int ARG_UNUSED (flags), bool *no_add_attrs)
6576 if (TREE_CODE (*node) == FUNCTION_DECL
6577 || TREE_CODE (*node) == LABEL_DECL)
6579 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6581 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6582 name, "cold");
6583 *no_add_attrs = true;
6585 /* Most of the rest of the hot processing is done later with
6586 lookup_attribute. */
6588 else
6590 warning (OPT_Wattributes, "%qE attribute ignored", name);
6591 *no_add_attrs = true;
6594 return NULL_TREE;
6597 /* Handle a "cold" and attribute; arguments as in
6598 struct attribute_spec.handler. */
6600 static tree
6601 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6602 int ARG_UNUSED (flags), bool *no_add_attrs)
6604 if (TREE_CODE (*node) == FUNCTION_DECL
6605 || TREE_CODE (*node) == LABEL_DECL)
6607 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6609 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6610 name, "hot");
6611 *no_add_attrs = true;
6613 /* Most of the rest of the cold processing is done later with
6614 lookup_attribute. */
6616 else
6618 warning (OPT_Wattributes, "%qE attribute ignored", name);
6619 *no_add_attrs = true;
6622 return NULL_TREE;
6625 /* Handle a "no_sanitize_address" attribute; arguments as in
6626 struct attribute_spec.handler. */
6628 static tree
6629 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6630 bool *no_add_attrs)
6632 if (TREE_CODE (*node) != FUNCTION_DECL)
6634 warning (OPT_Wattributes, "%qE attribute ignored", name);
6635 *no_add_attrs = true;
6638 return NULL_TREE;
6641 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6642 struct attribute_spec.handler. */
6644 static tree
6645 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6646 bool *no_add_attrs)
6648 if (TREE_CODE (*node) != FUNCTION_DECL)
6649 warning (OPT_Wattributes, "%qE attribute ignored", name);
6650 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6651 DECL_ATTRIBUTES (*node)
6652 = tree_cons (get_identifier ("no_sanitize_address"),
6653 NULL_TREE, DECL_ATTRIBUTES (*node));
6654 *no_add_attrs = true;
6655 return NULL_TREE;
6658 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6659 struct attribute_spec.handler. */
6661 static tree
6662 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6663 bool *no_add_attrs)
6665 if (TREE_CODE (*node) != FUNCTION_DECL)
6667 warning (OPT_Wattributes, "%qE attribute ignored", name);
6668 *no_add_attrs = true;
6671 return NULL_TREE;
6674 /* Handle a "noinline" attribute; arguments as in
6675 struct attribute_spec.handler. */
6677 static tree
6678 handle_noinline_attribute (tree *node, tree name,
6679 tree ARG_UNUSED (args),
6680 int ARG_UNUSED (flags), bool *no_add_attrs)
6682 if (TREE_CODE (*node) == FUNCTION_DECL)
6683 DECL_UNINLINABLE (*node) = 1;
6684 else
6686 warning (OPT_Wattributes, "%qE attribute ignored", name);
6687 *no_add_attrs = true;
6690 return NULL_TREE;
6693 /* Handle a "noclone" attribute; arguments as in
6694 struct attribute_spec.handler. */
6696 static tree
6697 handle_noclone_attribute (tree *node, tree name,
6698 tree ARG_UNUSED (args),
6699 int ARG_UNUSED (flags), bool *no_add_attrs)
6701 if (TREE_CODE (*node) != FUNCTION_DECL)
6703 warning (OPT_Wattributes, "%qE attribute ignored", name);
6704 *no_add_attrs = true;
6707 return NULL_TREE;
6710 /* Handle a "always_inline" attribute; arguments as in
6711 struct attribute_spec.handler. */
6713 static tree
6714 handle_always_inline_attribute (tree *node, tree name,
6715 tree ARG_UNUSED (args),
6716 int ARG_UNUSED (flags),
6717 bool *no_add_attrs)
6719 if (TREE_CODE (*node) == FUNCTION_DECL)
6721 /* Set the attribute and mark it for disregarding inline
6722 limits. */
6723 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6725 else
6727 warning (OPT_Wattributes, "%qE attribute ignored", name);
6728 *no_add_attrs = true;
6731 return NULL_TREE;
6734 /* Handle a "gnu_inline" attribute; arguments as in
6735 struct attribute_spec.handler. */
6737 static tree
6738 handle_gnu_inline_attribute (tree *node, tree name,
6739 tree ARG_UNUSED (args),
6740 int ARG_UNUSED (flags),
6741 bool *no_add_attrs)
6743 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6745 /* Do nothing else, just set the attribute. We'll get at
6746 it later with lookup_attribute. */
6748 else
6750 warning (OPT_Wattributes, "%qE attribute ignored", name);
6751 *no_add_attrs = true;
6754 return NULL_TREE;
6757 /* Handle a "leaf" attribute; arguments as in
6758 struct attribute_spec.handler. */
6760 static tree
6761 handle_leaf_attribute (tree *node, tree name,
6762 tree ARG_UNUSED (args),
6763 int ARG_UNUSED (flags), bool *no_add_attrs)
6765 if (TREE_CODE (*node) != FUNCTION_DECL)
6767 warning (OPT_Wattributes, "%qE attribute ignored", name);
6768 *no_add_attrs = true;
6770 if (!TREE_PUBLIC (*node))
6772 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6773 *no_add_attrs = true;
6776 return NULL_TREE;
6779 /* Handle an "artificial" attribute; arguments as in
6780 struct attribute_spec.handler. */
6782 static tree
6783 handle_artificial_attribute (tree *node, tree name,
6784 tree ARG_UNUSED (args),
6785 int ARG_UNUSED (flags),
6786 bool *no_add_attrs)
6788 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6790 /* Do nothing else, just set the attribute. We'll get at
6791 it later with lookup_attribute. */
6793 else
6795 warning (OPT_Wattributes, "%qE attribute ignored", name);
6796 *no_add_attrs = true;
6799 return NULL_TREE;
6802 /* Handle a "flatten" attribute; arguments as in
6803 struct attribute_spec.handler. */
6805 static tree
6806 handle_flatten_attribute (tree *node, tree name,
6807 tree args ATTRIBUTE_UNUSED,
6808 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6810 if (TREE_CODE (*node) == FUNCTION_DECL)
6811 /* Do nothing else, just set the attribute. We'll get at
6812 it later with lookup_attribute. */
6814 else
6816 warning (OPT_Wattributes, "%qE attribute ignored", name);
6817 *no_add_attrs = true;
6820 return NULL_TREE;
6823 /* Handle a "warning" or "error" attribute; arguments as in
6824 struct attribute_spec.handler. */
6826 static tree
6827 handle_error_attribute (tree *node, tree name, tree args,
6828 int ARG_UNUSED (flags), bool *no_add_attrs)
6830 if (TREE_CODE (*node) == FUNCTION_DECL
6831 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6832 /* Do nothing else, just set the attribute. We'll get at
6833 it later with lookup_attribute. */
6835 else
6837 warning (OPT_Wattributes, "%qE attribute ignored", name);
6838 *no_add_attrs = true;
6841 return NULL_TREE;
6844 /* Handle a "used" attribute; arguments as in
6845 struct attribute_spec.handler. */
6847 static tree
6848 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6849 int ARG_UNUSED (flags), bool *no_add_attrs)
6851 tree node = *pnode;
6853 if (TREE_CODE (node) == FUNCTION_DECL
6854 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6855 || (TREE_CODE (node) == TYPE_DECL))
6857 TREE_USED (node) = 1;
6858 DECL_PRESERVE_P (node) = 1;
6859 if (TREE_CODE (node) == VAR_DECL)
6860 DECL_READ_P (node) = 1;
6862 else
6864 warning (OPT_Wattributes, "%qE attribute ignored", name);
6865 *no_add_attrs = true;
6868 return NULL_TREE;
6871 /* Handle a "unused" attribute; arguments as in
6872 struct attribute_spec.handler. */
6874 static tree
6875 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6876 int flags, bool *no_add_attrs)
6878 if (DECL_P (*node))
6880 tree decl = *node;
6882 if (TREE_CODE (decl) == PARM_DECL
6883 || TREE_CODE (decl) == VAR_DECL
6884 || TREE_CODE (decl) == FIELD_DECL
6885 || TREE_CODE (decl) == FUNCTION_DECL
6886 || TREE_CODE (decl) == LABEL_DECL
6887 || TREE_CODE (decl) == TYPE_DECL)
6889 TREE_USED (decl) = 1;
6890 if (TREE_CODE (decl) == VAR_DECL
6891 || TREE_CODE (decl) == PARM_DECL)
6892 DECL_READ_P (decl) = 1;
6894 else
6896 warning (OPT_Wattributes, "%qE attribute ignored", name);
6897 *no_add_attrs = true;
6900 else
6902 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6903 *node = build_variant_type_copy (*node);
6904 TREE_USED (*node) = 1;
6907 return NULL_TREE;
6910 /* Handle a "externally_visible" attribute; arguments as in
6911 struct attribute_spec.handler. */
6913 static tree
6914 handle_externally_visible_attribute (tree *pnode, tree name,
6915 tree ARG_UNUSED (args),
6916 int ARG_UNUSED (flags),
6917 bool *no_add_attrs)
6919 tree node = *pnode;
6921 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6923 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6924 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6926 warning (OPT_Wattributes,
6927 "%qE attribute have effect only on public objects", name);
6928 *no_add_attrs = true;
6931 else
6933 warning (OPT_Wattributes, "%qE attribute ignored", name);
6934 *no_add_attrs = true;
6937 return NULL_TREE;
6940 /* Handle a "const" attribute; arguments as in
6941 struct attribute_spec.handler. */
6943 static tree
6944 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6945 int ARG_UNUSED (flags), bool *no_add_attrs)
6947 tree type = TREE_TYPE (*node);
6949 /* See FIXME comment on noreturn in c_common_attribute_table. */
6950 if (TREE_CODE (*node) == FUNCTION_DECL)
6951 TREE_READONLY (*node) = 1;
6952 else if (TREE_CODE (type) == POINTER_TYPE
6953 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6954 TREE_TYPE (*node)
6955 = build_pointer_type
6956 (build_type_variant (TREE_TYPE (type), 1,
6957 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6958 else
6960 warning (OPT_Wattributes, "%qE attribute ignored", name);
6961 *no_add_attrs = true;
6964 return NULL_TREE;
6967 /* Handle a "transparent_union" attribute; arguments as in
6968 struct attribute_spec.handler. */
6970 static tree
6971 handle_transparent_union_attribute (tree *node, tree name,
6972 tree ARG_UNUSED (args), int flags,
6973 bool *no_add_attrs)
6975 tree type;
6977 *no_add_attrs = true;
6980 if (TREE_CODE (*node) == TYPE_DECL
6981 && ! (flags & ATTR_FLAG_CXX11))
6982 node = &TREE_TYPE (*node);
6983 type = *node;
6985 if (TREE_CODE (type) == UNION_TYPE)
6987 /* Make sure that the first field will work for a transparent union.
6988 If the type isn't complete yet, leave the check to the code in
6989 finish_struct. */
6990 if (TYPE_SIZE (type))
6992 tree first = first_field (type);
6993 if (first == NULL_TREE
6994 || DECL_ARTIFICIAL (first)
6995 || TYPE_MODE (type) != DECL_MODE (first))
6996 goto ignored;
6999 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7001 /* If the type isn't complete yet, setting the flag
7002 on a variant wouldn't ever be checked. */
7003 if (!TYPE_SIZE (type))
7004 goto ignored;
7006 /* build_duplicate_type doesn't work for C++. */
7007 if (c_dialect_cxx ())
7008 goto ignored;
7010 /* A type variant isn't good enough, since we don't a cast
7011 to such a type removed as a no-op. */
7012 *node = type = build_duplicate_type (type);
7015 TYPE_TRANSPARENT_AGGR (type) = 1;
7016 return NULL_TREE;
7019 ignored:
7020 warning (OPT_Wattributes, "%qE attribute ignored", name);
7021 return NULL_TREE;
7024 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7025 get the requested priority for a constructor or destructor,
7026 possibly issuing diagnostics for invalid or reserved
7027 priorities. */
7029 static priority_type
7030 get_priority (tree args, bool is_destructor)
7032 HOST_WIDE_INT pri;
7033 tree arg;
7035 if (!args)
7036 return DEFAULT_INIT_PRIORITY;
7038 if (!SUPPORTS_INIT_PRIORITY)
7040 if (is_destructor)
7041 error ("destructor priorities are not supported");
7042 else
7043 error ("constructor priorities are not supported");
7044 return DEFAULT_INIT_PRIORITY;
7047 arg = TREE_VALUE (args);
7048 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7049 goto invalid;
7050 if (arg == error_mark_node)
7051 return DEFAULT_INIT_PRIORITY;
7052 arg = default_conversion (arg);
7053 if (!tree_fits_shwi_p (arg)
7054 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7055 goto invalid;
7057 pri = tree_to_shwi (arg);
7058 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7059 goto invalid;
7061 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7063 if (is_destructor)
7064 warning (0,
7065 "destructor priorities from 0 to %d are reserved "
7066 "for the implementation",
7067 MAX_RESERVED_INIT_PRIORITY);
7068 else
7069 warning (0,
7070 "constructor priorities from 0 to %d are reserved "
7071 "for the implementation",
7072 MAX_RESERVED_INIT_PRIORITY);
7074 return pri;
7076 invalid:
7077 if (is_destructor)
7078 error ("destructor priorities must be integers from 0 to %d inclusive",
7079 MAX_INIT_PRIORITY);
7080 else
7081 error ("constructor priorities must be integers from 0 to %d inclusive",
7082 MAX_INIT_PRIORITY);
7083 return DEFAULT_INIT_PRIORITY;
7086 /* Handle a "constructor" attribute; arguments as in
7087 struct attribute_spec.handler. */
7089 static tree
7090 handle_constructor_attribute (tree *node, tree name, tree args,
7091 int ARG_UNUSED (flags),
7092 bool *no_add_attrs)
7094 tree decl = *node;
7095 tree type = TREE_TYPE (decl);
7097 if (TREE_CODE (decl) == FUNCTION_DECL
7098 && TREE_CODE (type) == FUNCTION_TYPE
7099 && decl_function_context (decl) == 0)
7101 priority_type priority;
7102 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7103 priority = get_priority (args, /*is_destructor=*/false);
7104 SET_DECL_INIT_PRIORITY (decl, priority);
7105 TREE_USED (decl) = 1;
7107 else
7109 warning (OPT_Wattributes, "%qE attribute ignored", name);
7110 *no_add_attrs = true;
7113 return NULL_TREE;
7116 /* Handle a "destructor" attribute; arguments as in
7117 struct attribute_spec.handler. */
7119 static tree
7120 handle_destructor_attribute (tree *node, tree name, tree args,
7121 int ARG_UNUSED (flags),
7122 bool *no_add_attrs)
7124 tree decl = *node;
7125 tree type = TREE_TYPE (decl);
7127 if (TREE_CODE (decl) == FUNCTION_DECL
7128 && TREE_CODE (type) == FUNCTION_TYPE
7129 && decl_function_context (decl) == 0)
7131 priority_type priority;
7132 DECL_STATIC_DESTRUCTOR (decl) = 1;
7133 priority = get_priority (args, /*is_destructor=*/true);
7134 SET_DECL_FINI_PRIORITY (decl, priority);
7135 TREE_USED (decl) = 1;
7137 else
7139 warning (OPT_Wattributes, "%qE attribute ignored", name);
7140 *no_add_attrs = true;
7143 return NULL_TREE;
7146 /* Nonzero if the mode is a valid vector mode for this architecture.
7147 This returns nonzero even if there is no hardware support for the
7148 vector mode, but we can emulate with narrower modes. */
7150 static int
7151 vector_mode_valid_p (enum machine_mode mode)
7153 enum mode_class mclass = GET_MODE_CLASS (mode);
7154 enum machine_mode innermode;
7156 /* Doh! What's going on? */
7157 if (mclass != MODE_VECTOR_INT
7158 && mclass != MODE_VECTOR_FLOAT
7159 && mclass != MODE_VECTOR_FRACT
7160 && mclass != MODE_VECTOR_UFRACT
7161 && mclass != MODE_VECTOR_ACCUM
7162 && mclass != MODE_VECTOR_UACCUM)
7163 return 0;
7165 /* Hardware support. Woo hoo! */
7166 if (targetm.vector_mode_supported_p (mode))
7167 return 1;
7169 innermode = GET_MODE_INNER (mode);
7171 /* We should probably return 1 if requesting V4DI and we have no DI,
7172 but we have V2DI, but this is probably very unlikely. */
7174 /* If we have support for the inner mode, we can safely emulate it.
7175 We may not have V2DI, but me can emulate with a pair of DIs. */
7176 return targetm.scalar_mode_supported_p (innermode);
7180 /* Handle a "mode" attribute; arguments as in
7181 struct attribute_spec.handler. */
7183 static tree
7184 handle_mode_attribute (tree *node, tree name, tree args,
7185 int ARG_UNUSED (flags), bool *no_add_attrs)
7187 tree type = *node;
7188 tree ident = TREE_VALUE (args);
7190 *no_add_attrs = true;
7192 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7193 warning (OPT_Wattributes, "%qE attribute ignored", name);
7194 else
7196 int j;
7197 const char *p = IDENTIFIER_POINTER (ident);
7198 int len = strlen (p);
7199 enum machine_mode mode = VOIDmode;
7200 tree typefm;
7201 bool valid_mode;
7203 if (len > 4 && p[0] == '_' && p[1] == '_'
7204 && p[len - 1] == '_' && p[len - 2] == '_')
7206 char *newp = (char *) alloca (len - 1);
7208 strcpy (newp, &p[2]);
7209 newp[len - 4] = '\0';
7210 p = newp;
7213 /* Change this type to have a type with the specified mode.
7214 First check for the special modes. */
7215 if (!strcmp (p, "byte"))
7216 mode = byte_mode;
7217 else if (!strcmp (p, "word"))
7218 mode = word_mode;
7219 else if (!strcmp (p, "pointer"))
7220 mode = ptr_mode;
7221 else if (!strcmp (p, "libgcc_cmp_return"))
7222 mode = targetm.libgcc_cmp_return_mode ();
7223 else if (!strcmp (p, "libgcc_shift_count"))
7224 mode = targetm.libgcc_shift_count_mode ();
7225 else if (!strcmp (p, "unwind_word"))
7226 mode = targetm.unwind_word_mode ();
7227 else
7228 for (j = 0; j < NUM_MACHINE_MODES; j++)
7229 if (!strcmp (p, GET_MODE_NAME (j)))
7231 mode = (enum machine_mode) j;
7232 break;
7235 if (mode == VOIDmode)
7237 error ("unknown machine mode %qE", ident);
7238 return NULL_TREE;
7241 valid_mode = false;
7242 switch (GET_MODE_CLASS (mode))
7244 case MODE_INT:
7245 case MODE_PARTIAL_INT:
7246 case MODE_FLOAT:
7247 case MODE_DECIMAL_FLOAT:
7248 case MODE_FRACT:
7249 case MODE_UFRACT:
7250 case MODE_ACCUM:
7251 case MODE_UACCUM:
7252 valid_mode = targetm.scalar_mode_supported_p (mode);
7253 break;
7255 case MODE_COMPLEX_INT:
7256 case MODE_COMPLEX_FLOAT:
7257 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7258 break;
7260 case MODE_VECTOR_INT:
7261 case MODE_VECTOR_FLOAT:
7262 case MODE_VECTOR_FRACT:
7263 case MODE_VECTOR_UFRACT:
7264 case MODE_VECTOR_ACCUM:
7265 case MODE_VECTOR_UACCUM:
7266 warning (OPT_Wattributes, "specifying vector types with "
7267 "__attribute__ ((mode)) is deprecated");
7268 warning (OPT_Wattributes,
7269 "use __attribute__ ((vector_size)) instead");
7270 valid_mode = vector_mode_valid_p (mode);
7271 break;
7273 default:
7274 break;
7276 if (!valid_mode)
7278 error ("unable to emulate %qs", p);
7279 return NULL_TREE;
7282 if (POINTER_TYPE_P (type))
7284 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7285 tree (*fn)(tree, enum machine_mode, bool);
7287 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7289 error ("invalid pointer mode %qs", p);
7290 return NULL_TREE;
7293 if (TREE_CODE (type) == POINTER_TYPE)
7294 fn = build_pointer_type_for_mode;
7295 else
7296 fn = build_reference_type_for_mode;
7297 typefm = fn (TREE_TYPE (type), mode, false);
7299 else
7301 /* For fixed-point modes, we need to test if the signness of type
7302 and the machine mode are consistent. */
7303 if (ALL_FIXED_POINT_MODE_P (mode)
7304 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7306 error ("signedness of type and machine mode %qs don%'t match", p);
7307 return NULL_TREE;
7309 /* For fixed-point modes, we need to pass saturating info. */
7310 typefm = lang_hooks.types.type_for_mode (mode,
7311 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7312 : TYPE_UNSIGNED (type));
7315 if (typefm == NULL_TREE)
7317 error ("no data type for mode %qs", p);
7318 return NULL_TREE;
7320 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7322 /* For enumeral types, copy the precision from the integer
7323 type returned above. If not an INTEGER_TYPE, we can't use
7324 this mode for this type. */
7325 if (TREE_CODE (typefm) != INTEGER_TYPE)
7327 error ("cannot use mode %qs for enumeral types", p);
7328 return NULL_TREE;
7331 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7333 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7334 typefm = type;
7336 else
7338 /* We cannot build a type variant, as there's code that assumes
7339 that TYPE_MAIN_VARIANT has the same mode. This includes the
7340 debug generators. Instead, create a subrange type. This
7341 results in all of the enumeral values being emitted only once
7342 in the original, and the subtype gets them by reference. */
7343 if (TYPE_UNSIGNED (type))
7344 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7345 else
7346 typefm = make_signed_type (TYPE_PRECISION (typefm));
7347 TREE_TYPE (typefm) = type;
7350 else if (VECTOR_MODE_P (mode)
7351 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7352 : TREE_CODE (type) != TREE_CODE (typefm))
7354 error ("mode %qs applied to inappropriate type", p);
7355 return NULL_TREE;
7358 *node = typefm;
7361 return NULL_TREE;
7364 /* Handle a "section" attribute; arguments as in
7365 struct attribute_spec.handler. */
7367 static tree
7368 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7369 int ARG_UNUSED (flags), bool *no_add_attrs)
7371 tree decl = *node;
7373 if (targetm_common.have_named_sections)
7375 user_defined_section_attribute = true;
7377 if ((TREE_CODE (decl) == FUNCTION_DECL
7378 || TREE_CODE (decl) == VAR_DECL)
7379 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7381 if (TREE_CODE (decl) == VAR_DECL
7382 && current_function_decl != NULL_TREE
7383 && !TREE_STATIC (decl))
7385 error_at (DECL_SOURCE_LOCATION (decl),
7386 "section attribute cannot be specified for "
7387 "local variables");
7388 *no_add_attrs = true;
7391 /* The decl may have already been given a section attribute
7392 from a previous declaration. Ensure they match. */
7393 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7394 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7395 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7397 error ("section of %q+D conflicts with previous declaration",
7398 *node);
7399 *no_add_attrs = true;
7401 else if (TREE_CODE (decl) == VAR_DECL
7402 && !targetm.have_tls && targetm.emutls.tmpl_section
7403 && DECL_THREAD_LOCAL_P (decl))
7405 error ("section of %q+D cannot be overridden", *node);
7406 *no_add_attrs = true;
7408 else
7409 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7411 else
7413 error ("section attribute not allowed for %q+D", *node);
7414 *no_add_attrs = true;
7417 else
7419 error_at (DECL_SOURCE_LOCATION (*node),
7420 "section attributes are not supported for this target");
7421 *no_add_attrs = true;
7424 return NULL_TREE;
7427 /* Check whether ALIGN is a valid user-specified alignment. If so,
7428 return its base-2 log; if not, output an error and return -1. If
7429 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7430 no error. */
7432 check_user_alignment (const_tree align, bool allow_zero)
7434 int i;
7436 if (TREE_CODE (align) != INTEGER_CST
7437 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7439 error ("requested alignment is not an integer constant");
7440 return -1;
7442 else if (allow_zero && integer_zerop (align))
7443 return -1;
7444 else if (tree_int_cst_sgn (align) == -1
7445 || (i = tree_log2 (align)) == -1)
7447 error ("requested alignment is not a positive power of 2");
7448 return -1;
7450 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7452 error ("requested alignment is too large");
7453 return -1;
7455 return i;
7459 If in c++-11, check if the c++-11 alignment constraint with respect
7460 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7461 c++-11 mode, does nothing.
7463 [dcl.align]2/ says:
7465 [* if the constant expression evaluates to a fundamental alignment,
7466 the alignment requirement of the declared entity shall be the
7467 specified fundamental alignment.
7469 * if the constant expression evaluates to an extended alignment
7470 and the implementation supports that alignment in the context
7471 of the declaration, the alignment of the declared entity shall
7472 be that alignment
7474 * if the constant expression evaluates to an extended alignment
7475 and the implementation does not support that alignment in the
7476 context of the declaration, the program is ill-formed]. */
7478 static bool
7479 check_cxx_fundamental_alignment_constraints (tree node,
7480 unsigned align_log,
7481 int flags)
7483 bool alignment_too_large_p = false;
7484 unsigned requested_alignment = 1U << align_log;
7485 unsigned max_align = 0;
7487 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7488 || (node == NULL_TREE || node == error_mark_node))
7489 return true;
7491 if (cxx_fundamental_alignment_p (requested_alignment))
7492 return true;
7494 if (DECL_P (node))
7496 if (TREE_STATIC (node))
7498 /* For file scope variables and static members, the target
7499 supports alignments that are at most
7500 MAX_OFILE_ALIGNMENT. */
7501 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7502 alignment_too_large_p = true;
7504 else
7506 #ifdef BIGGEST_FIELD_ALIGNMENT
7507 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7508 #else
7509 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7510 #endif
7511 /* For non-static members, the target supports either
7512 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7513 if it is defined or BIGGEST_ALIGNMENT. */
7514 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7515 if (TREE_CODE (node) == FIELD_DECL
7516 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7517 alignment_too_large_p = true;
7518 #undef MAX_TARGET_FIELD_ALIGNMENT
7519 /* For stack variables, the target supports at most
7520 MAX_STACK_ALIGNMENT. */
7521 else if (decl_function_context (node) != NULL
7522 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7523 alignment_too_large_p = true;
7526 else if (TYPE_P (node))
7528 /* Let's be liberal for types. */
7529 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7530 alignment_too_large_p = true;
7533 if (alignment_too_large_p)
7534 pedwarn (input_location, OPT_Wattributes,
7535 "requested alignment %d is larger than %d",
7536 requested_alignment, max_align);
7538 return !alignment_too_large_p;
7541 /* Handle a "aligned" attribute; arguments as in
7542 struct attribute_spec.handler. */
7544 static tree
7545 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7546 int flags, bool *no_add_attrs)
7548 tree decl = NULL_TREE;
7549 tree *type = NULL;
7550 int is_type = 0;
7551 tree align_expr;
7552 int i;
7554 if (args)
7556 align_expr = TREE_VALUE (args);
7557 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE)
7558 align_expr = default_conversion (align_expr);
7560 else
7561 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7563 if (DECL_P (*node))
7565 decl = *node;
7566 type = &TREE_TYPE (decl);
7567 is_type = TREE_CODE (*node) == TYPE_DECL;
7569 else if (TYPE_P (*node))
7570 type = node, is_type = 1;
7572 if ((i = check_user_alignment (align_expr, false)) == -1
7573 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7574 *no_add_attrs = true;
7575 else if (is_type)
7577 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7578 /* OK, modify the type in place. */;
7579 /* If we have a TYPE_DECL, then copy the type, so that we
7580 don't accidentally modify a builtin type. See pushdecl. */
7581 else if (decl && TREE_TYPE (decl) != error_mark_node
7582 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7584 tree tt = TREE_TYPE (decl);
7585 *type = build_variant_type_copy (*type);
7586 DECL_ORIGINAL_TYPE (decl) = tt;
7587 TYPE_NAME (*type) = decl;
7588 TREE_USED (*type) = TREE_USED (decl);
7589 TREE_TYPE (decl) = *type;
7591 else
7592 *type = build_variant_type_copy (*type);
7594 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7595 TYPE_USER_ALIGN (*type) = 1;
7597 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7598 && TREE_CODE (decl) != FIELD_DECL)
7600 error ("alignment may not be specified for %q+D", decl);
7601 *no_add_attrs = true;
7603 else if (DECL_USER_ALIGN (decl)
7604 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7605 /* C++-11 [dcl.align/4]:
7607 When multiple alignment-specifiers are specified for an
7608 entity, the alignment requirement shall be set to the
7609 strictest specified alignment.
7611 This formally comes from the c++11 specification but we are
7612 doing it for the GNU attribute syntax as well. */
7613 *no_add_attrs = true;
7614 else if (TREE_CODE (decl) == FUNCTION_DECL
7615 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7617 if (DECL_USER_ALIGN (decl))
7618 error ("alignment for %q+D was previously specified as %d "
7619 "and may not be decreased", decl,
7620 DECL_ALIGN (decl) / BITS_PER_UNIT);
7621 else
7622 error ("alignment for %q+D must be at least %d", decl,
7623 DECL_ALIGN (decl) / BITS_PER_UNIT);
7624 *no_add_attrs = true;
7626 else
7628 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7629 DECL_USER_ALIGN (decl) = 1;
7632 return NULL_TREE;
7635 /* Handle a "weak" attribute; arguments as in
7636 struct attribute_spec.handler. */
7638 static tree
7639 handle_weak_attribute (tree *node, tree name,
7640 tree ARG_UNUSED (args),
7641 int ARG_UNUSED (flags),
7642 bool * ARG_UNUSED (no_add_attrs))
7644 if (TREE_CODE (*node) == FUNCTION_DECL
7645 && DECL_DECLARED_INLINE_P (*node))
7647 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7648 *no_add_attrs = true;
7650 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7652 error ("indirect function %q+D cannot be declared weak", *node);
7653 *no_add_attrs = true;
7654 return NULL_TREE;
7656 else if (TREE_CODE (*node) == FUNCTION_DECL
7657 || TREE_CODE (*node) == VAR_DECL)
7658 declare_weak (*node);
7659 else
7660 warning (OPT_Wattributes, "%qE attribute ignored", name);
7662 return NULL_TREE;
7665 /* Handle an "alias" or "ifunc" attribute; arguments as in
7666 struct attribute_spec.handler, except that IS_ALIAS tells us
7667 whether this is an alias as opposed to ifunc attribute. */
7669 static tree
7670 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7671 bool *no_add_attrs)
7673 tree decl = *node;
7675 if (TREE_CODE (decl) != FUNCTION_DECL
7676 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7678 warning (OPT_Wattributes, "%qE attribute ignored", name);
7679 *no_add_attrs = true;
7681 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7682 || (TREE_CODE (decl) != FUNCTION_DECL
7683 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7684 /* A static variable declaration is always a tentative definition,
7685 but the alias is a non-tentative definition which overrides. */
7686 || (TREE_CODE (decl) != FUNCTION_DECL
7687 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7689 error ("%q+D defined both normally and as %qE attribute", decl, name);
7690 *no_add_attrs = true;
7691 return NULL_TREE;
7693 else if (!is_alias
7694 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7695 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7697 error ("weak %q+D cannot be defined %qE", decl, name);
7698 *no_add_attrs = true;
7699 return NULL_TREE;
7702 /* Note that the very first time we process a nested declaration,
7703 decl_function_context will not be set. Indeed, *would* never
7704 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7705 we do below. After such frobbery, pushdecl would set the context.
7706 In any case, this is never what we want. */
7707 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7709 tree id;
7711 id = TREE_VALUE (args);
7712 if (TREE_CODE (id) != STRING_CST)
7714 error ("attribute %qE argument not a string", name);
7715 *no_add_attrs = true;
7716 return NULL_TREE;
7718 id = get_identifier (TREE_STRING_POINTER (id));
7719 /* This counts as a use of the object pointed to. */
7720 TREE_USED (id) = 1;
7722 if (TREE_CODE (decl) == FUNCTION_DECL)
7723 DECL_INITIAL (decl) = error_mark_node;
7724 else
7725 TREE_STATIC (decl) = 1;
7727 if (!is_alias)
7728 /* ifuncs are also aliases, so set that attribute too. */
7729 DECL_ATTRIBUTES (decl)
7730 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7732 else
7734 warning (OPT_Wattributes, "%qE attribute ignored", name);
7735 *no_add_attrs = true;
7738 return NULL_TREE;
7741 /* Handle an "alias" or "ifunc" attribute; arguments as in
7742 struct attribute_spec.handler. */
7744 static tree
7745 handle_ifunc_attribute (tree *node, tree name, tree args,
7746 int ARG_UNUSED (flags), bool *no_add_attrs)
7748 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7751 /* Handle an "alias" or "ifunc" attribute; arguments as in
7752 struct attribute_spec.handler. */
7754 static tree
7755 handle_alias_attribute (tree *node, tree name, tree args,
7756 int ARG_UNUSED (flags), bool *no_add_attrs)
7758 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7761 /* Handle a "weakref" attribute; arguments as in struct
7762 attribute_spec.handler. */
7764 static tree
7765 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7766 int flags, bool *no_add_attrs)
7768 tree attr = NULL_TREE;
7770 /* We must ignore the attribute when it is associated with
7771 local-scoped decls, since attribute alias is ignored and many
7772 such symbols do not even have a DECL_WEAK field. */
7773 if (decl_function_context (*node)
7774 || current_function_decl
7775 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7777 warning (OPT_Wattributes, "%qE attribute ignored", name);
7778 *no_add_attrs = true;
7779 return NULL_TREE;
7782 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7784 error ("indirect function %q+D cannot be declared weakref", *node);
7785 *no_add_attrs = true;
7786 return NULL_TREE;
7789 /* The idea here is that `weakref("name")' mutates into `weakref,
7790 alias("name")', and weakref without arguments, in turn,
7791 implicitly adds weak. */
7793 if (args)
7795 attr = tree_cons (get_identifier ("alias"), args, attr);
7796 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7798 *no_add_attrs = true;
7800 decl_attributes (node, attr, flags);
7802 else
7804 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7805 error_at (DECL_SOURCE_LOCATION (*node),
7806 "weakref attribute must appear before alias attribute");
7808 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7809 and that isn't supported; and because it wants to add it to
7810 the list of weak decls, which isn't helpful. */
7811 DECL_WEAK (*node) = 1;
7814 return NULL_TREE;
7817 /* Handle an "visibility" attribute; arguments as in
7818 struct attribute_spec.handler. */
7820 static tree
7821 handle_visibility_attribute (tree *node, tree name, tree args,
7822 int ARG_UNUSED (flags),
7823 bool *ARG_UNUSED (no_add_attrs))
7825 tree decl = *node;
7826 tree id = TREE_VALUE (args);
7827 enum symbol_visibility vis;
7829 if (TYPE_P (*node))
7831 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7832 /* OK */;
7833 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7835 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7836 name);
7837 return NULL_TREE;
7839 else if (TYPE_FIELDS (*node))
7841 error ("%qE attribute ignored because %qT is already defined",
7842 name, *node);
7843 return NULL_TREE;
7846 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7848 warning (OPT_Wattributes, "%qE attribute ignored", name);
7849 return NULL_TREE;
7852 if (TREE_CODE (id) != STRING_CST)
7854 error ("visibility argument not a string");
7855 return NULL_TREE;
7858 /* If this is a type, set the visibility on the type decl. */
7859 if (TYPE_P (decl))
7861 decl = TYPE_NAME (decl);
7862 if (!decl)
7863 return NULL_TREE;
7864 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7866 warning (OPT_Wattributes, "%qE attribute ignored on types",
7867 name);
7868 return NULL_TREE;
7872 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7873 vis = VISIBILITY_DEFAULT;
7874 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7875 vis = VISIBILITY_INTERNAL;
7876 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7877 vis = VISIBILITY_HIDDEN;
7878 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7879 vis = VISIBILITY_PROTECTED;
7880 else
7882 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7883 vis = VISIBILITY_DEFAULT;
7886 if (DECL_VISIBILITY_SPECIFIED (decl)
7887 && vis != DECL_VISIBILITY (decl))
7889 tree attributes = (TYPE_P (*node)
7890 ? TYPE_ATTRIBUTES (*node)
7891 : DECL_ATTRIBUTES (decl));
7892 if (lookup_attribute ("visibility", attributes))
7893 error ("%qD redeclared with different visibility", decl);
7894 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7895 && lookup_attribute ("dllimport", attributes))
7896 error ("%qD was declared %qs which implies default visibility",
7897 decl, "dllimport");
7898 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7899 && lookup_attribute ("dllexport", attributes))
7900 error ("%qD was declared %qs which implies default visibility",
7901 decl, "dllexport");
7904 DECL_VISIBILITY (decl) = vis;
7905 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7907 /* Go ahead and attach the attribute to the node as well. This is needed
7908 so we can determine whether we have VISIBILITY_DEFAULT because the
7909 visibility was not specified, or because it was explicitly overridden
7910 from the containing scope. */
7912 return NULL_TREE;
7915 /* Determine the ELF symbol visibility for DECL, which is either a
7916 variable or a function. It is an error to use this function if a
7917 definition of DECL is not available in this translation unit.
7918 Returns true if the final visibility has been determined by this
7919 function; false if the caller is free to make additional
7920 modifications. */
7922 bool
7923 c_determine_visibility (tree decl)
7925 gcc_assert (TREE_CODE (decl) == VAR_DECL
7926 || TREE_CODE (decl) == FUNCTION_DECL);
7928 /* If the user explicitly specified the visibility with an
7929 attribute, honor that. DECL_VISIBILITY will have been set during
7930 the processing of the attribute. We check for an explicit
7931 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7932 to distinguish the use of an attribute from the use of a "#pragma
7933 GCC visibility push(...)"; in the latter case we still want other
7934 considerations to be able to overrule the #pragma. */
7935 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7936 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7937 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7938 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7939 return true;
7941 /* Set default visibility to whatever the user supplied with
7942 visibility_specified depending on #pragma GCC visibility. */
7943 if (!DECL_VISIBILITY_SPECIFIED (decl))
7945 if (visibility_options.inpragma
7946 || DECL_VISIBILITY (decl) != default_visibility)
7948 DECL_VISIBILITY (decl) = default_visibility;
7949 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7950 /* If visibility changed and DECL already has DECL_RTL, ensure
7951 symbol flags are updated. */
7952 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7953 || TREE_CODE (decl) == FUNCTION_DECL)
7954 && DECL_RTL_SET_P (decl))
7955 make_decl_rtl (decl);
7958 return false;
7961 /* Handle an "tls_model" attribute; arguments as in
7962 struct attribute_spec.handler. */
7964 static tree
7965 handle_tls_model_attribute (tree *node, tree name, tree args,
7966 int ARG_UNUSED (flags), bool *no_add_attrs)
7968 tree id;
7969 tree decl = *node;
7970 enum tls_model kind;
7972 *no_add_attrs = true;
7974 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7976 warning (OPT_Wattributes, "%qE attribute ignored", name);
7977 return NULL_TREE;
7980 kind = DECL_TLS_MODEL (decl);
7981 id = TREE_VALUE (args);
7982 if (TREE_CODE (id) != STRING_CST)
7984 error ("tls_model argument not a string");
7985 return NULL_TREE;
7988 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7989 kind = TLS_MODEL_LOCAL_EXEC;
7990 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7991 kind = TLS_MODEL_INITIAL_EXEC;
7992 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7993 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7994 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7995 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7996 else
7997 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7999 DECL_TLS_MODEL (decl) = kind;
8000 return NULL_TREE;
8003 /* Handle a "no_instrument_function" attribute; arguments as in
8004 struct attribute_spec.handler. */
8006 static tree
8007 handle_no_instrument_function_attribute (tree *node, tree name,
8008 tree ARG_UNUSED (args),
8009 int ARG_UNUSED (flags),
8010 bool *no_add_attrs)
8012 tree decl = *node;
8014 if (TREE_CODE (decl) != FUNCTION_DECL)
8016 error_at (DECL_SOURCE_LOCATION (decl),
8017 "%qE attribute applies only to functions", name);
8018 *no_add_attrs = true;
8020 else
8021 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8023 return NULL_TREE;
8026 /* Handle a "malloc" attribute; arguments as in
8027 struct attribute_spec.handler. */
8029 static tree
8030 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8031 int ARG_UNUSED (flags), bool *no_add_attrs)
8033 if (TREE_CODE (*node) == FUNCTION_DECL
8034 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8035 DECL_IS_MALLOC (*node) = 1;
8036 else
8038 warning (OPT_Wattributes, "%qE attribute ignored", name);
8039 *no_add_attrs = true;
8042 return NULL_TREE;
8045 /* Handle a "alloc_size" attribute; arguments as in
8046 struct attribute_spec.handler. */
8048 static tree
8049 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8050 int ARG_UNUSED (flags), bool *no_add_attrs)
8052 unsigned arg_count = type_num_arguments (*node);
8053 for (; args; args = TREE_CHAIN (args))
8055 tree position = TREE_VALUE (args);
8056 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8057 && TREE_CODE (position) != FUNCTION_DECL)
8058 position = default_conversion (position);
8060 if (!tree_fits_uhwi_p (position)
8061 || !arg_count
8062 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8064 warning (OPT_Wattributes,
8065 "alloc_size parameter outside range");
8066 *no_add_attrs = true;
8067 return NULL_TREE;
8070 return NULL_TREE;
8073 /* Handle a "alloc_align" attribute; arguments as in
8074 struct attribute_spec.handler. */
8076 static tree
8077 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8078 bool *no_add_attrs)
8080 unsigned arg_count = type_num_arguments (*node);
8081 tree position = TREE_VALUE (args);
8082 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8083 position = default_conversion (position);
8085 if (!tree_fits_uhwi_p (position)
8086 || !arg_count
8087 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8089 warning (OPT_Wattributes,
8090 "alloc_align parameter outside range");
8091 *no_add_attrs = true;
8092 return NULL_TREE;
8094 return NULL_TREE;
8097 /* Handle a "assume_aligned" attribute; arguments as in
8098 struct attribute_spec.handler. */
8100 static tree
8101 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8102 bool *no_add_attrs)
8104 for (; args; args = TREE_CHAIN (args))
8106 tree position = TREE_VALUE (args);
8107 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8108 && TREE_CODE (position) != FUNCTION_DECL)
8109 position = default_conversion (position);
8111 if (TREE_CODE (position) != INTEGER_CST)
8113 warning (OPT_Wattributes,
8114 "assume_aligned parameter not integer constant");
8115 *no_add_attrs = true;
8116 return NULL_TREE;
8119 return NULL_TREE;
8122 /* Handle a "fn spec" attribute; arguments as in
8123 struct attribute_spec.handler. */
8125 static tree
8126 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8127 tree args, int ARG_UNUSED (flags),
8128 bool *no_add_attrs ATTRIBUTE_UNUSED)
8130 gcc_assert (args
8131 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8132 && !TREE_CHAIN (args));
8133 return NULL_TREE;
8136 /* Handle a "warn_unused" attribute; arguments as in
8137 struct attribute_spec.handler. */
8139 static tree
8140 handle_warn_unused_attribute (tree *node, tree name,
8141 tree args ATTRIBUTE_UNUSED,
8142 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8144 if (TYPE_P (*node))
8145 /* Do nothing else, just set the attribute. We'll get at
8146 it later with lookup_attribute. */
8148 else
8150 warning (OPT_Wattributes, "%qE attribute ignored", name);
8151 *no_add_attrs = true;
8154 return NULL_TREE;
8157 /* Handle an "omp declare simd" attribute; arguments as in
8158 struct attribute_spec.handler. */
8160 static tree
8161 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8163 return NULL_TREE;
8166 /* Handle an "omp declare target" attribute; arguments as in
8167 struct attribute_spec.handler. */
8169 static tree
8170 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8172 return NULL_TREE;
8175 /* Handle a "returns_twice" attribute; arguments as in
8176 struct attribute_spec.handler. */
8178 static tree
8179 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8180 int ARG_UNUSED (flags), bool *no_add_attrs)
8182 if (TREE_CODE (*node) == FUNCTION_DECL)
8183 DECL_IS_RETURNS_TWICE (*node) = 1;
8184 else
8186 warning (OPT_Wattributes, "%qE attribute ignored", name);
8187 *no_add_attrs = true;
8190 return NULL_TREE;
8193 /* Handle a "no_limit_stack" attribute; arguments as in
8194 struct attribute_spec.handler. */
8196 static tree
8197 handle_no_limit_stack_attribute (tree *node, tree name,
8198 tree ARG_UNUSED (args),
8199 int ARG_UNUSED (flags),
8200 bool *no_add_attrs)
8202 tree decl = *node;
8204 if (TREE_CODE (decl) != FUNCTION_DECL)
8206 error_at (DECL_SOURCE_LOCATION (decl),
8207 "%qE attribute applies only to functions", name);
8208 *no_add_attrs = true;
8210 else if (DECL_INITIAL (decl))
8212 error_at (DECL_SOURCE_LOCATION (decl),
8213 "can%'t set %qE attribute after definition", name);
8214 *no_add_attrs = true;
8216 else
8217 DECL_NO_LIMIT_STACK (decl) = 1;
8219 return NULL_TREE;
8222 /* Handle a "pure" attribute; arguments as in
8223 struct attribute_spec.handler. */
8225 static tree
8226 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8227 int ARG_UNUSED (flags), bool *no_add_attrs)
8229 if (TREE_CODE (*node) == FUNCTION_DECL)
8230 DECL_PURE_P (*node) = 1;
8231 /* ??? TODO: Support types. */
8232 else
8234 warning (OPT_Wattributes, "%qE attribute ignored", name);
8235 *no_add_attrs = true;
8238 return NULL_TREE;
8241 /* Digest an attribute list destined for a transactional memory statement.
8242 ALLOWED is the set of attributes that are allowed for this statement;
8243 return the attribute we parsed. Multiple attributes are never allowed. */
8246 parse_tm_stmt_attr (tree attrs, int allowed)
8248 tree a_seen = NULL;
8249 int m_seen = 0;
8251 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8253 tree a = TREE_PURPOSE (attrs);
8254 int m = 0;
8256 if (is_attribute_p ("outer", a))
8257 m = TM_STMT_ATTR_OUTER;
8259 if ((m & allowed) == 0)
8261 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8262 continue;
8265 if (m_seen == 0)
8267 a_seen = a;
8268 m_seen = m;
8270 else if (m_seen == m)
8271 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8272 else
8273 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8276 return m_seen;
8279 /* Transform a TM attribute name into a maskable integer and back.
8280 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8281 to how the lack of an attribute is treated. */
8284 tm_attr_to_mask (tree attr)
8286 if (attr == NULL)
8287 return 0;
8288 if (is_attribute_p ("transaction_safe", attr))
8289 return TM_ATTR_SAFE;
8290 if (is_attribute_p ("transaction_callable", attr))
8291 return TM_ATTR_CALLABLE;
8292 if (is_attribute_p ("transaction_pure", attr))
8293 return TM_ATTR_PURE;
8294 if (is_attribute_p ("transaction_unsafe", attr))
8295 return TM_ATTR_IRREVOCABLE;
8296 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8297 return TM_ATTR_MAY_CANCEL_OUTER;
8298 return 0;
8301 tree
8302 tm_mask_to_attr (int mask)
8304 const char *str;
8305 switch (mask)
8307 case TM_ATTR_SAFE:
8308 str = "transaction_safe";
8309 break;
8310 case TM_ATTR_CALLABLE:
8311 str = "transaction_callable";
8312 break;
8313 case TM_ATTR_PURE:
8314 str = "transaction_pure";
8315 break;
8316 case TM_ATTR_IRREVOCABLE:
8317 str = "transaction_unsafe";
8318 break;
8319 case TM_ATTR_MAY_CANCEL_OUTER:
8320 str = "transaction_may_cancel_outer";
8321 break;
8322 default:
8323 gcc_unreachable ();
8325 return get_identifier (str);
8328 /* Return the first TM attribute seen in LIST. */
8330 tree
8331 find_tm_attribute (tree list)
8333 for (; list ; list = TREE_CHAIN (list))
8335 tree name = TREE_PURPOSE (list);
8336 if (tm_attr_to_mask (name) != 0)
8337 return name;
8339 return NULL_TREE;
8342 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8343 Here we accept only function types, and verify that none of the other
8344 function TM attributes are also applied. */
8345 /* ??? We need to accept class types for C++, but not C. This greatly
8346 complicates this function, since we can no longer rely on the extra
8347 processing given by function_type_required. */
8349 static tree
8350 handle_tm_attribute (tree *node, tree name, tree args,
8351 int flags, bool *no_add_attrs)
8353 /* Only one path adds the attribute; others don't. */
8354 *no_add_attrs = true;
8356 switch (TREE_CODE (*node))
8358 case RECORD_TYPE:
8359 case UNION_TYPE:
8360 /* Only tm_callable and tm_safe apply to classes. */
8361 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8362 goto ignored;
8363 /* FALLTHRU */
8365 case FUNCTION_TYPE:
8366 case METHOD_TYPE:
8368 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8369 if (old_name == name)
8371 else if (old_name != NULL_TREE)
8372 error ("type was previously declared %qE", old_name);
8373 else
8374 *no_add_attrs = false;
8376 break;
8378 case POINTER_TYPE:
8380 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8381 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8383 tree fn_tmp = TREE_TYPE (*node);
8384 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8385 *node = build_pointer_type (fn_tmp);
8386 break;
8389 /* FALLTHRU */
8391 default:
8392 /* If a function is next, pass it on to be tried next. */
8393 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8394 return tree_cons (name, args, NULL);
8396 ignored:
8397 warning (OPT_Wattributes, "%qE attribute ignored", name);
8398 break;
8401 return NULL_TREE;
8404 /* Handle the TM_WRAP attribute; arguments as in
8405 struct attribute_spec.handler. */
8407 static tree
8408 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8409 int ARG_UNUSED (flags), bool *no_add_attrs)
8411 tree decl = *node;
8413 /* We don't need the attribute even on success, since we
8414 record the entry in an external table. */
8415 *no_add_attrs = true;
8417 if (TREE_CODE (decl) != FUNCTION_DECL)
8418 warning (OPT_Wattributes, "%qE attribute ignored", name);
8419 else
8421 tree wrap_decl = TREE_VALUE (args);
8422 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8423 && TREE_CODE (wrap_decl) != VAR_DECL
8424 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8425 error ("%qE argument not an identifier", name);
8426 else
8428 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8429 wrap_decl = lookup_name (wrap_decl);
8430 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8432 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8433 TREE_TYPE (wrap_decl)))
8434 record_tm_replacement (wrap_decl, decl);
8435 else
8436 error ("%qD is not compatible with %qD", wrap_decl, decl);
8438 else
8439 error ("transaction_wrap argument is not a function");
8443 return NULL_TREE;
8446 /* Ignore the given attribute. Used when this attribute may be usefully
8447 overridden by the target, but is not used generically. */
8449 static tree
8450 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8451 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8452 bool *no_add_attrs)
8454 *no_add_attrs = true;
8455 return NULL_TREE;
8458 /* Handle a "no vops" attribute; arguments as in
8459 struct attribute_spec.handler. */
8461 static tree
8462 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8463 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8464 bool *ARG_UNUSED (no_add_attrs))
8466 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8467 DECL_IS_NOVOPS (*node) = 1;
8468 return NULL_TREE;
8471 /* Handle a "deprecated" attribute; arguments as in
8472 struct attribute_spec.handler. */
8474 static tree
8475 handle_deprecated_attribute (tree *node, tree name,
8476 tree args, int flags,
8477 bool *no_add_attrs)
8479 tree type = NULL_TREE;
8480 int warn = 0;
8481 tree what = NULL_TREE;
8483 if (!args)
8484 *no_add_attrs = true;
8485 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8487 error ("deprecated message is not a string");
8488 *no_add_attrs = true;
8491 if (DECL_P (*node))
8493 tree decl = *node;
8494 type = TREE_TYPE (decl);
8496 if (TREE_CODE (decl) == TYPE_DECL
8497 || TREE_CODE (decl) == PARM_DECL
8498 || TREE_CODE (decl) == VAR_DECL
8499 || TREE_CODE (decl) == FUNCTION_DECL
8500 || TREE_CODE (decl) == FIELD_DECL
8501 || objc_method_decl (TREE_CODE (decl)))
8502 TREE_DEPRECATED (decl) = 1;
8503 else
8504 warn = 1;
8506 else if (TYPE_P (*node))
8508 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8509 *node = build_variant_type_copy (*node);
8510 TREE_DEPRECATED (*node) = 1;
8511 type = *node;
8513 else
8514 warn = 1;
8516 if (warn)
8518 *no_add_attrs = true;
8519 if (type && TYPE_NAME (type))
8521 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8522 what = TYPE_NAME (*node);
8523 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8524 && DECL_NAME (TYPE_NAME (type)))
8525 what = DECL_NAME (TYPE_NAME (type));
8527 if (what)
8528 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8529 else
8530 warning (OPT_Wattributes, "%qE attribute ignored", name);
8533 return NULL_TREE;
8536 /* Handle a "vector_size" attribute; arguments as in
8537 struct attribute_spec.handler. */
8539 static tree
8540 handle_vector_size_attribute (tree *node, tree name, tree args,
8541 int ARG_UNUSED (flags),
8542 bool *no_add_attrs)
8544 unsigned HOST_WIDE_INT vecsize, nunits;
8545 enum machine_mode orig_mode;
8546 tree type = *node, new_type, size;
8548 *no_add_attrs = true;
8550 size = TREE_VALUE (args);
8551 if (size && TREE_CODE (size) != IDENTIFIER_NODE)
8552 size = default_conversion (size);
8554 if (!tree_fits_uhwi_p (size))
8556 warning (OPT_Wattributes, "%qE attribute ignored", name);
8557 return NULL_TREE;
8560 /* Get the vector size (in bytes). */
8561 vecsize = tree_to_uhwi (size);
8563 /* We need to provide for vector pointers, vector arrays, and
8564 functions returning vectors. For example:
8566 __attribute__((vector_size(16))) short *foo;
8568 In this case, the mode is SI, but the type being modified is
8569 HI, so we need to look further. */
8571 while (POINTER_TYPE_P (type)
8572 || TREE_CODE (type) == FUNCTION_TYPE
8573 || TREE_CODE (type) == METHOD_TYPE
8574 || TREE_CODE (type) == ARRAY_TYPE
8575 || TREE_CODE (type) == OFFSET_TYPE)
8576 type = TREE_TYPE (type);
8578 /* Get the mode of the type being modified. */
8579 orig_mode = TYPE_MODE (type);
8581 if ((!INTEGRAL_TYPE_P (type)
8582 && !SCALAR_FLOAT_TYPE_P (type)
8583 && !FIXED_POINT_TYPE_P (type))
8584 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8585 && GET_MODE_CLASS (orig_mode) != MODE_INT
8586 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8587 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8588 || TREE_CODE (type) == BOOLEAN_TYPE)
8590 error ("invalid vector type for attribute %qE", name);
8591 return NULL_TREE;
8594 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8596 error ("vector size not an integral multiple of component size");
8597 return NULL;
8600 if (vecsize == 0)
8602 error ("zero vector size");
8603 return NULL;
8606 /* Calculate how many units fit in the vector. */
8607 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8608 if (nunits & (nunits - 1))
8610 error ("number of components of the vector not a power of two");
8611 return NULL_TREE;
8614 new_type = build_vector_type (type, nunits);
8616 /* Build back pointers if needed. */
8617 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8619 return NULL_TREE;
8622 /* Handle the "nonnull" attribute. */
8623 static tree
8624 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8625 tree args, int ARG_UNUSED (flags),
8626 bool *no_add_attrs)
8628 tree type = *node;
8629 unsigned HOST_WIDE_INT attr_arg_num;
8631 /* If no arguments are specified, all pointer arguments should be
8632 non-null. Verify a full prototype is given so that the arguments
8633 will have the correct types when we actually check them later. */
8634 if (!args)
8636 if (!prototype_p (type))
8638 error ("nonnull attribute without arguments on a non-prototype");
8639 *no_add_attrs = true;
8641 return NULL_TREE;
8644 /* Argument list specified. Verify that each argument number references
8645 a pointer argument. */
8646 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8648 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8650 tree arg = TREE_VALUE (args);
8651 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8652 && TREE_CODE (arg) != FUNCTION_DECL)
8653 arg = default_conversion (arg);
8655 if (!get_nonnull_operand (arg, &arg_num))
8657 error ("nonnull argument has invalid operand number (argument %lu)",
8658 (unsigned long) attr_arg_num);
8659 *no_add_attrs = true;
8660 return NULL_TREE;
8663 if (prototype_p (type))
8665 function_args_iterator iter;
8666 tree argument;
8668 function_args_iter_init (&iter, type);
8669 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8671 argument = function_args_iter_cond (&iter);
8672 if (argument == NULL_TREE || ck_num == arg_num)
8673 break;
8676 if (!argument
8677 || TREE_CODE (argument) == VOID_TYPE)
8679 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8680 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8681 *no_add_attrs = true;
8682 return NULL_TREE;
8685 if (TREE_CODE (argument) != POINTER_TYPE)
8687 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8688 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8689 *no_add_attrs = true;
8690 return NULL_TREE;
8695 return NULL_TREE;
8698 /* Handle a "always_patch_for_instrumentation" attribute; arguments as in
8699 struct attribute_spec.handler. */
8701 static tree
8702 handle_always_patch_for_instrumentation_attribute (tree *node, tree name,
8703 tree ARG_UNUSED (args),
8704 int ARG_UNUSED (flags),
8705 bool *no_add_attrs)
8707 if (TREE_CODE (*node) == FUNCTION_DECL)
8709 /* Disable inlining if forced instrumentation. */
8710 DECL_UNINLINABLE (*node) = 1;
8712 else
8714 warning (OPT_Wattributes, "%qE attribute ignored", name);
8715 *no_add_attrs = true;
8717 return NULL_TREE;
8721 /* Handle a "never_patch_for_instrumentation" attribute; arguments as in
8722 struct attribute_spec.handler. */
8724 static tree
8725 handle_never_patch_for_instrumentation_attribute (tree *node, tree name,
8726 tree ARG_UNUSED (args),
8727 int ARG_UNUSED (flags),
8728 bool *no_add_attrs)
8730 if (TREE_CODE (*node) != FUNCTION_DECL)
8732 warning (OPT_Wattributes, "%qE attribute ignored", name);
8733 *no_add_attrs = true;
8735 return NULL_TREE;
8739 /* Check the argument list of a function call for null in argument slots
8740 that are marked as requiring a non-null pointer argument. The NARGS
8741 arguments are passed in the array ARGARRAY.
8744 static void
8745 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8747 tree a;
8748 int i;
8750 attrs = lookup_attribute ("nonnull", attrs);
8751 if (attrs == NULL_TREE)
8752 return;
8754 a = attrs;
8755 /* See if any of the nonnull attributes has no arguments. If so,
8756 then every pointer argument is checked (in which case the check
8757 for pointer type is done in check_nonnull_arg). */
8758 if (TREE_VALUE (a) != NULL_TREE)
8760 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8761 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8763 if (a != NULL_TREE)
8764 for (i = 0; i < nargs; i++)
8765 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8766 i + 1);
8767 else
8769 /* Walk the argument list. If we encounter an argument number we
8770 should check for non-null, do it. */
8771 for (i = 0; i < nargs; i++)
8773 for (a = attrs; ; a = TREE_CHAIN (a))
8775 a = lookup_attribute ("nonnull", a);
8776 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8777 break;
8780 if (a != NULL_TREE)
8781 check_function_arguments_recurse (check_nonnull_arg, NULL,
8782 argarray[i], i + 1);
8787 /* Check that the Nth argument of a function call (counting backwards
8788 from the end) is a (pointer)0. The NARGS arguments are passed in the
8789 array ARGARRAY. */
8791 static void
8792 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8794 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8796 if (attr)
8798 int len = 0;
8799 int pos = 0;
8800 tree sentinel;
8801 function_args_iterator iter;
8802 tree t;
8804 /* Skip over the named arguments. */
8805 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8807 if (len == nargs)
8808 break;
8809 len++;
8812 if (TREE_VALUE (attr))
8814 tree p = TREE_VALUE (TREE_VALUE (attr));
8815 pos = TREE_INT_CST_LOW (p);
8818 /* The sentinel must be one of the varargs, i.e.
8819 in position >= the number of fixed arguments. */
8820 if ((nargs - 1 - pos) < len)
8822 warning (OPT_Wformat_,
8823 "not enough variable arguments to fit a sentinel");
8824 return;
8827 /* Validate the sentinel. */
8828 sentinel = argarray[nargs - 1 - pos];
8829 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8830 || !integer_zerop (sentinel))
8831 /* Although __null (in C++) is only an integer we allow it
8832 nevertheless, as we are guaranteed that it's exactly
8833 as wide as a pointer, and we don't want to force
8834 users to cast the NULL they have written there.
8835 We warn with -Wstrict-null-sentinel, though. */
8836 && (warn_strict_null_sentinel || null_node != sentinel))
8837 warning (OPT_Wformat_, "missing sentinel in function call");
8841 /* Helper for check_function_nonnull; given a list of operands which
8842 must be non-null in ARGS, determine if operand PARAM_NUM should be
8843 checked. */
8845 static bool
8846 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8848 unsigned HOST_WIDE_INT arg_num = 0;
8850 for (; args; args = TREE_CHAIN (args))
8852 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8854 gcc_assert (found);
8856 if (arg_num == param_num)
8857 return true;
8859 return false;
8862 /* Check that the function argument PARAM (which is operand number
8863 PARAM_NUM) is non-null. This is called by check_function_nonnull
8864 via check_function_arguments_recurse. */
8866 static void
8867 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8868 unsigned HOST_WIDE_INT param_num)
8870 /* Just skip checking the argument if it's not a pointer. This can
8871 happen if the "nonnull" attribute was given without an operand
8872 list (which means to check every pointer argument). */
8874 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8875 return;
8877 if (integer_zerop (param))
8878 warning (OPT_Wnonnull, "null argument where non-null required "
8879 "(argument %lu)", (unsigned long) param_num);
8882 /* Helper for nonnull attribute handling; fetch the operand number
8883 from the attribute argument list. */
8885 static bool
8886 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8888 /* Verify the arg number is a constant. */
8889 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8890 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8891 return false;
8893 *valp = TREE_INT_CST_LOW (arg_num_expr);
8894 return true;
8897 /* Handle a "nothrow" attribute; arguments as in
8898 struct attribute_spec.handler. */
8900 static tree
8901 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8902 int ARG_UNUSED (flags), bool *no_add_attrs)
8904 if (TREE_CODE (*node) == FUNCTION_DECL)
8905 TREE_NOTHROW (*node) = 1;
8906 /* ??? TODO: Support types. */
8907 else
8909 warning (OPT_Wattributes, "%qE attribute ignored", name);
8910 *no_add_attrs = true;
8913 return NULL_TREE;
8916 /* Handle a "cleanup" attribute; arguments as in
8917 struct attribute_spec.handler. */
8919 static tree
8920 handle_cleanup_attribute (tree *node, tree name, tree args,
8921 int ARG_UNUSED (flags), bool *no_add_attrs)
8923 tree decl = *node;
8924 tree cleanup_id, cleanup_decl;
8926 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8927 for global destructors in C++. This requires infrastructure that
8928 we don't have generically at the moment. It's also not a feature
8929 we'd be missing too much, since we do have attribute constructor. */
8930 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8932 warning (OPT_Wattributes, "%qE attribute ignored", name);
8933 *no_add_attrs = true;
8934 return NULL_TREE;
8937 /* Verify that the argument is a function in scope. */
8938 /* ??? We could support pointers to functions here as well, if
8939 that was considered desirable. */
8940 cleanup_id = TREE_VALUE (args);
8941 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8943 error ("cleanup argument not an identifier");
8944 *no_add_attrs = true;
8945 return NULL_TREE;
8947 cleanup_decl = lookup_name (cleanup_id);
8948 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8950 error ("cleanup argument not a function");
8951 *no_add_attrs = true;
8952 return NULL_TREE;
8955 /* That the function has proper type is checked with the
8956 eventual call to build_function_call. */
8958 return NULL_TREE;
8961 /* Handle a "warn_unused_result" attribute. No special handling. */
8963 static tree
8964 handle_warn_unused_result_attribute (tree *node, tree name,
8965 tree ARG_UNUSED (args),
8966 int ARG_UNUSED (flags), bool *no_add_attrs)
8968 /* Ignore the attribute for functions not returning any value. */
8969 if (VOID_TYPE_P (TREE_TYPE (*node)))
8971 warning (OPT_Wattributes, "%qE attribute ignored", name);
8972 *no_add_attrs = true;
8975 return NULL_TREE;
8978 /* Handle a "sentinel" attribute. */
8980 static tree
8981 handle_sentinel_attribute (tree *node, tree name, tree args,
8982 int ARG_UNUSED (flags), bool *no_add_attrs)
8984 if (!prototype_p (*node))
8986 warning (OPT_Wattributes,
8987 "%qE attribute requires prototypes with named arguments", name);
8988 *no_add_attrs = true;
8990 else
8992 if (!stdarg_p (*node))
8994 warning (OPT_Wattributes,
8995 "%qE attribute only applies to variadic functions", name);
8996 *no_add_attrs = true;
9000 if (args)
9002 tree position = TREE_VALUE (args);
9004 if (TREE_CODE (position) != INTEGER_CST)
9006 warning (OPT_Wattributes,
9007 "requested position is not an integer constant");
9008 *no_add_attrs = true;
9010 else
9012 if (tree_int_cst_lt (position, integer_zero_node))
9014 warning (OPT_Wattributes,
9015 "requested position is less than zero");
9016 *no_add_attrs = true;
9021 return NULL_TREE;
9024 /* Handle a "type_generic" attribute. */
9026 static tree
9027 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9028 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9029 bool * ARG_UNUSED (no_add_attrs))
9031 /* Ensure we have a function type. */
9032 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9034 /* Ensure we have a variadic function. */
9035 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9037 return NULL_TREE;
9040 /* Handle a "target" attribute. */
9042 static tree
9043 handle_target_attribute (tree *node, tree name, tree args, int flags,
9044 bool *no_add_attrs)
9046 /* Ensure we have a function type. */
9047 if (TREE_CODE (*node) != FUNCTION_DECL)
9049 warning (OPT_Wattributes, "%qE attribute ignored", name);
9050 *no_add_attrs = true;
9052 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9053 flags))
9054 *no_add_attrs = true;
9056 return NULL_TREE;
9059 /* Arguments being collected for optimization. */
9060 typedef const char *const_char_p; /* For DEF_VEC_P. */
9061 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9064 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9065 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9066 false for #pragma GCC optimize. */
9068 bool
9069 parse_optimize_options (tree args, bool attr_p)
9071 bool ret = true;
9072 unsigned opt_argc;
9073 unsigned i;
9074 int saved_flag_strict_aliasing;
9075 const char **opt_argv;
9076 struct cl_decoded_option *decoded_options;
9077 unsigned int decoded_options_count;
9078 tree ap;
9080 /* Build up argv vector. Just in case the string is stored away, use garbage
9081 collected strings. */
9082 vec_safe_truncate (optimize_args, 0);
9083 vec_safe_push (optimize_args, (const char *) NULL);
9085 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9087 tree value = TREE_VALUE (ap);
9089 if (TREE_CODE (value) == INTEGER_CST)
9091 char buffer[20];
9092 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9093 vec_safe_push (optimize_args, ggc_strdup (buffer));
9096 else if (TREE_CODE (value) == STRING_CST)
9098 /* Split string into multiple substrings. */
9099 size_t len = TREE_STRING_LENGTH (value);
9100 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9101 char *end = p + len;
9102 char *comma;
9103 char *next_p = p;
9105 while (next_p != NULL)
9107 size_t len2;
9108 char *q, *r;
9110 p = next_p;
9111 comma = strchr (p, ',');
9112 if (comma)
9114 len2 = comma - p;
9115 *comma = '\0';
9116 next_p = comma+1;
9118 else
9120 len2 = end - p;
9121 next_p = NULL;
9124 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9126 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9127 options. */
9128 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9130 ret = false;
9131 if (attr_p)
9132 warning (OPT_Wattributes,
9133 "bad option %s to optimize attribute", p);
9134 else
9135 warning (OPT_Wpragmas,
9136 "bad option %s to pragma attribute", p);
9137 continue;
9140 if (*p != '-')
9142 *r++ = '-';
9144 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9145 itself is -Os, and any other switch begins with a -f. */
9146 if ((*p >= '0' && *p <= '9')
9147 || (p[0] == 's' && p[1] == '\0'))
9148 *r++ = 'O';
9149 else if (*p != 'O')
9150 *r++ = 'f';
9153 memcpy (r, p, len2);
9154 r[len2] = '\0';
9155 vec_safe_push (optimize_args, (const char *) q);
9161 opt_argc = optimize_args->length ();
9162 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9164 for (i = 1; i < opt_argc; i++)
9165 opt_argv[i] = (*optimize_args)[i];
9167 saved_flag_strict_aliasing = flag_strict_aliasing;
9169 /* Now parse the options. */
9170 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9171 &decoded_options,
9172 &decoded_options_count);
9173 decode_options (&global_options, &global_options_set,
9174 decoded_options, decoded_options_count,
9175 input_location, global_dc);
9177 targetm.override_options_after_change();
9179 /* Don't allow changing -fstrict-aliasing. */
9180 flag_strict_aliasing = saved_flag_strict_aliasing;
9182 optimize_args->truncate (0);
9183 return ret;
9186 /* For handling "optimize" attribute. arguments as in
9187 struct attribute_spec.handler. */
9189 static tree
9190 handle_optimize_attribute (tree *node, tree name, tree args,
9191 int ARG_UNUSED (flags), bool *no_add_attrs)
9193 /* Ensure we have a function type. */
9194 if (TREE_CODE (*node) != FUNCTION_DECL)
9196 warning (OPT_Wattributes, "%qE attribute ignored", name);
9197 *no_add_attrs = true;
9199 else
9201 struct cl_optimization cur_opts;
9202 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9204 /* Save current options. */
9205 cl_optimization_save (&cur_opts, &global_options);
9207 /* If we previously had some optimization options, use them as the
9208 default. */
9209 if (old_opts)
9210 cl_optimization_restore (&global_options,
9211 TREE_OPTIMIZATION (old_opts));
9213 /* Parse options, and update the vector. */
9214 parse_optimize_options (args, true);
9215 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9216 = build_optimization_node (&global_options);
9218 /* Restore current options. */
9219 cl_optimization_restore (&global_options, &cur_opts);
9222 return NULL_TREE;
9225 /* Handle a "no_split_stack" attribute. */
9227 static tree
9228 handle_no_split_stack_attribute (tree *node, tree name,
9229 tree ARG_UNUSED (args),
9230 int ARG_UNUSED (flags),
9231 bool *no_add_attrs)
9233 tree decl = *node;
9235 if (TREE_CODE (decl) != FUNCTION_DECL)
9237 error_at (DECL_SOURCE_LOCATION (decl),
9238 "%qE attribute applies only to functions", name);
9239 *no_add_attrs = true;
9241 else if (DECL_INITIAL (decl))
9243 error_at (DECL_SOURCE_LOCATION (decl),
9244 "can%'t set %qE attribute after definition", name);
9245 *no_add_attrs = true;
9248 return NULL_TREE;
9251 /* Handle a "returns_nonnull" attribute; arguments as in
9252 struct attribute_spec.handler. */
9254 static tree
9255 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9256 bool *no_add_attrs)
9258 // Even without a prototype we still have a return type we can check.
9259 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9261 error ("returns_nonnull attribute on a function not returning a pointer");
9262 *no_add_attrs = true;
9264 return NULL_TREE;
9268 /* Check for valid arguments being passed to a function with FNTYPE.
9269 There are NARGS arguments in the array ARGARRAY. */
9270 void
9271 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9273 /* Check for null being passed in a pointer argument that must be
9274 non-null. We also need to do this if format checking is enabled. */
9276 if (warn_nonnull)
9277 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9279 /* Check for errors in format strings. */
9281 if (warn_format || warn_suggest_attribute_format)
9282 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9284 if (warn_format)
9285 check_function_sentinel (fntype, nargs, argarray);
9288 /* Generic argument checking recursion routine. PARAM is the argument to
9289 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9290 once the argument is resolved. CTX is context for the callback. */
9291 void
9292 check_function_arguments_recurse (void (*callback)
9293 (void *, tree, unsigned HOST_WIDE_INT),
9294 void *ctx, tree param,
9295 unsigned HOST_WIDE_INT param_num)
9297 if (CONVERT_EXPR_P (param)
9298 && (TYPE_PRECISION (TREE_TYPE (param))
9299 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9301 /* Strip coercion. */
9302 check_function_arguments_recurse (callback, ctx,
9303 TREE_OPERAND (param, 0), param_num);
9304 return;
9307 if (TREE_CODE (param) == CALL_EXPR)
9309 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9310 tree attrs;
9311 bool found_format_arg = false;
9313 /* See if this is a call to a known internationalization function
9314 that modifies a format arg. Such a function may have multiple
9315 format_arg attributes (for example, ngettext). */
9317 for (attrs = TYPE_ATTRIBUTES (type);
9318 attrs;
9319 attrs = TREE_CHAIN (attrs))
9320 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9322 tree inner_arg;
9323 tree format_num_expr;
9324 int format_num;
9325 int i;
9326 call_expr_arg_iterator iter;
9328 /* Extract the argument number, which was previously checked
9329 to be valid. */
9330 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9332 format_num = tree_to_uhwi (format_num_expr);
9334 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9335 inner_arg != 0;
9336 inner_arg = next_call_expr_arg (&iter), i++)
9337 if (i == format_num)
9339 check_function_arguments_recurse (callback, ctx,
9340 inner_arg, param_num);
9341 found_format_arg = true;
9342 break;
9346 /* If we found a format_arg attribute and did a recursive check,
9347 we are done with checking this argument. Otherwise, we continue
9348 and this will be considered a non-literal. */
9349 if (found_format_arg)
9350 return;
9353 if (TREE_CODE (param) == COND_EXPR)
9355 /* Check both halves of the conditional expression. */
9356 check_function_arguments_recurse (callback, ctx,
9357 TREE_OPERAND (param, 1), param_num);
9358 check_function_arguments_recurse (callback, ctx,
9359 TREE_OPERAND (param, 2), param_num);
9360 return;
9363 (*callback) (ctx, param, param_num);
9366 /* Checks for a builtin function FNDECL that the number of arguments
9367 NARGS against the required number REQUIRED and issues an error if
9368 there is a mismatch. Returns true if the number of arguments is
9369 correct, otherwise false. */
9371 static bool
9372 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9374 if (nargs < required)
9376 error_at (input_location,
9377 "not enough arguments to function %qE", fndecl);
9378 return false;
9380 else if (nargs > required)
9382 error_at (input_location,
9383 "too many arguments to function %qE", fndecl);
9384 return false;
9386 return true;
9389 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9390 Returns false if there was an error, otherwise true. */
9392 bool
9393 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9395 if (!DECL_BUILT_IN (fndecl)
9396 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9397 return true;
9399 switch (DECL_FUNCTION_CODE (fndecl))
9401 case BUILT_IN_CONSTANT_P:
9402 return builtin_function_validate_nargs (fndecl, nargs, 1);
9404 case BUILT_IN_ISFINITE:
9405 case BUILT_IN_ISINF:
9406 case BUILT_IN_ISINF_SIGN:
9407 case BUILT_IN_ISNAN:
9408 case BUILT_IN_ISNORMAL:
9409 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9411 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9413 error ("non-floating-point argument in call to "
9414 "function %qE", fndecl);
9415 return false;
9417 return true;
9419 return false;
9421 case BUILT_IN_ISGREATER:
9422 case BUILT_IN_ISGREATEREQUAL:
9423 case BUILT_IN_ISLESS:
9424 case BUILT_IN_ISLESSEQUAL:
9425 case BUILT_IN_ISLESSGREATER:
9426 case BUILT_IN_ISUNORDERED:
9427 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9429 enum tree_code code0, code1;
9430 code0 = TREE_CODE (TREE_TYPE (args[0]));
9431 code1 = TREE_CODE (TREE_TYPE (args[1]));
9432 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9433 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9434 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9436 error ("non-floating-point arguments in call to "
9437 "function %qE", fndecl);
9438 return false;
9440 return true;
9442 return false;
9444 case BUILT_IN_FPCLASSIFY:
9445 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9447 unsigned i;
9449 for (i=0; i<5; i++)
9450 if (TREE_CODE (args[i]) != INTEGER_CST)
9452 error ("non-const integer argument %u in call to function %qE",
9453 i+1, fndecl);
9454 return false;
9457 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9459 error ("non-floating-point argument in call to function %qE",
9460 fndecl);
9461 return false;
9463 return true;
9465 return false;
9467 case BUILT_IN_ASSUME_ALIGNED:
9468 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9470 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9472 error ("non-integer argument 3 in call to function %qE", fndecl);
9473 return false;
9475 return true;
9477 return false;
9479 default:
9480 return true;
9484 /* Function to help qsort sort FIELD_DECLs by name order. */
9487 field_decl_cmp (const void *x_p, const void *y_p)
9489 const tree *const x = (const tree *const) x_p;
9490 const tree *const y = (const tree *const) y_p;
9492 if (DECL_NAME (*x) == DECL_NAME (*y))
9493 /* A nontype is "greater" than a type. */
9494 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9495 if (DECL_NAME (*x) == NULL_TREE)
9496 return -1;
9497 if (DECL_NAME (*y) == NULL_TREE)
9498 return 1;
9499 if (DECL_NAME (*x) < DECL_NAME (*y))
9500 return -1;
9501 return 1;
9504 static struct {
9505 gt_pointer_operator new_value;
9506 void *cookie;
9507 } resort_data;
9509 /* This routine compares two fields like field_decl_cmp but using the
9510 pointer operator in resort_data. */
9512 static int
9513 resort_field_decl_cmp (const void *x_p, const void *y_p)
9515 const tree *const x = (const tree *const) x_p;
9516 const tree *const y = (const tree *const) y_p;
9518 if (DECL_NAME (*x) == DECL_NAME (*y))
9519 /* A nontype is "greater" than a type. */
9520 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9521 if (DECL_NAME (*x) == NULL_TREE)
9522 return -1;
9523 if (DECL_NAME (*y) == NULL_TREE)
9524 return 1;
9526 tree d1 = DECL_NAME (*x);
9527 tree d2 = DECL_NAME (*y);
9528 resort_data.new_value (&d1, resort_data.cookie);
9529 resort_data.new_value (&d2, resort_data.cookie);
9530 if (d1 < d2)
9531 return -1;
9533 return 1;
9536 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9538 void
9539 resort_sorted_fields (void *obj,
9540 void * ARG_UNUSED (orig_obj),
9541 gt_pointer_operator new_value,
9542 void *cookie)
9544 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9545 resort_data.new_value = new_value;
9546 resort_data.cookie = cookie;
9547 qsort (&sf->elts[0], sf->len, sizeof (tree),
9548 resort_field_decl_cmp);
9551 /* Subroutine of c_parse_error.
9552 Return the result of concatenating LHS and RHS. RHS is really
9553 a string literal, its first character is indicated by RHS_START and
9554 RHS_SIZE is its length (including the terminating NUL character).
9556 The caller is responsible for deleting the returned pointer. */
9558 static char *
9559 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9561 const int lhs_size = strlen (lhs);
9562 char *result = XNEWVEC (char, lhs_size + rhs_size);
9563 strncpy (result, lhs, lhs_size);
9564 strncpy (result + lhs_size, rhs_start, rhs_size);
9565 return result;
9568 /* Issue the error given by GMSGID, indicating that it occurred before
9569 TOKEN, which had the associated VALUE. */
9571 void
9572 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9573 tree value, unsigned char token_flags)
9575 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9577 char *message = NULL;
9579 if (token_type == CPP_EOF)
9580 message = catenate_messages (gmsgid, " at end of input");
9581 else if (token_type == CPP_CHAR
9582 || token_type == CPP_WCHAR
9583 || token_type == CPP_CHAR16
9584 || token_type == CPP_CHAR32)
9586 unsigned int val = TREE_INT_CST_LOW (value);
9587 const char *prefix;
9589 switch (token_type)
9591 default:
9592 prefix = "";
9593 break;
9594 case CPP_WCHAR:
9595 prefix = "L";
9596 break;
9597 case CPP_CHAR16:
9598 prefix = "u";
9599 break;
9600 case CPP_CHAR32:
9601 prefix = "U";
9602 break;
9605 if (val <= UCHAR_MAX && ISGRAPH (val))
9606 message = catenate_messages (gmsgid, " before %s'%c'");
9607 else
9608 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9610 error (message, prefix, val);
9611 free (message);
9612 message = NULL;
9614 else if (token_type == CPP_CHAR_USERDEF
9615 || token_type == CPP_WCHAR_USERDEF
9616 || token_type == CPP_CHAR16_USERDEF
9617 || token_type == CPP_CHAR32_USERDEF)
9618 message = catenate_messages (gmsgid,
9619 " before user-defined character literal");
9620 else if (token_type == CPP_STRING_USERDEF
9621 || token_type == CPP_WSTRING_USERDEF
9622 || token_type == CPP_STRING16_USERDEF
9623 || token_type == CPP_STRING32_USERDEF
9624 || token_type == CPP_UTF8STRING_USERDEF)
9625 message = catenate_messages (gmsgid, " before user-defined string literal");
9626 else if (token_type == CPP_STRING
9627 || token_type == CPP_WSTRING
9628 || token_type == CPP_STRING16
9629 || token_type == CPP_STRING32
9630 || token_type == CPP_UTF8STRING)
9631 message = catenate_messages (gmsgid, " before string constant");
9632 else if (token_type == CPP_NUMBER)
9633 message = catenate_messages (gmsgid, " before numeric constant");
9634 else if (token_type == CPP_NAME)
9636 message = catenate_messages (gmsgid, " before %qE");
9637 error (message, value);
9638 free (message);
9639 message = NULL;
9641 else if (token_type == CPP_PRAGMA)
9642 message = catenate_messages (gmsgid, " before %<#pragma%>");
9643 else if (token_type == CPP_PRAGMA_EOL)
9644 message = catenate_messages (gmsgid, " before end of line");
9645 else if (token_type == CPP_DECLTYPE)
9646 message = catenate_messages (gmsgid, " before %<decltype%>");
9647 else if (token_type < N_TTYPES)
9649 message = catenate_messages (gmsgid, " before %qs token");
9650 error (message, cpp_type2name (token_type, token_flags));
9651 free (message);
9652 message = NULL;
9654 else
9655 error (gmsgid);
9657 if (message)
9659 error (message);
9660 free (message);
9662 #undef catenate_messages
9665 /* Mapping for cpp message reasons to the options that enable them. */
9667 struct reason_option_codes_t
9669 const int reason; /* cpplib message reason. */
9670 const int option_code; /* gcc option that controls this message. */
9673 static const struct reason_option_codes_t option_codes[] = {
9674 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9675 {CPP_W_COMMENTS, OPT_Wcomment},
9676 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9677 {CPP_W_MULTICHAR, OPT_Wmultichar},
9678 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9679 {CPP_W_LONG_LONG, OPT_Wlong_long},
9680 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9681 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9682 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9683 {CPP_W_UNDEF, OPT_Wundef},
9684 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9685 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9686 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9687 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9688 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9689 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9690 {CPP_W_DATE_TIME, OPT_Wdate_time},
9691 {CPP_W_NONE, 0}
9694 /* Return the gcc option code associated with the reason for a cpp
9695 message, or 0 if none. */
9697 static int
9698 c_option_controlling_cpp_error (int reason)
9700 const struct reason_option_codes_t *entry;
9702 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9704 if (entry->reason == reason)
9705 return entry->option_code;
9707 return 0;
9710 /* Callback from cpp_error for PFILE to print diagnostics from the
9711 preprocessor. The diagnostic is of type LEVEL, with REASON set
9712 to the reason code if LEVEL is represents a warning, at location
9713 LOCATION unless this is after lexing and the compiler's location
9714 should be used instead, with column number possibly overridden by
9715 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9716 the arguments. Returns true if a diagnostic was emitted, false
9717 otherwise. */
9719 bool
9720 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9721 location_t location, unsigned int column_override,
9722 const char *msg, va_list *ap)
9724 diagnostic_info diagnostic;
9725 diagnostic_t dlevel;
9726 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9727 bool ret;
9729 switch (level)
9731 case CPP_DL_WARNING_SYSHDR:
9732 if (flag_no_output)
9733 return false;
9734 global_dc->dc_warn_system_headers = 1;
9735 /* Fall through. */
9736 case CPP_DL_WARNING:
9737 if (flag_no_output)
9738 return false;
9739 dlevel = DK_WARNING;
9740 break;
9741 case CPP_DL_PEDWARN:
9742 if (flag_no_output && !flag_pedantic_errors)
9743 return false;
9744 dlevel = DK_PEDWARN;
9745 break;
9746 case CPP_DL_ERROR:
9747 dlevel = DK_ERROR;
9748 break;
9749 case CPP_DL_ICE:
9750 dlevel = DK_ICE;
9751 break;
9752 case CPP_DL_NOTE:
9753 dlevel = DK_NOTE;
9754 break;
9755 case CPP_DL_FATAL:
9756 dlevel = DK_FATAL;
9757 break;
9758 default:
9759 gcc_unreachable ();
9761 if (done_lexing)
9762 location = input_location;
9763 diagnostic_set_info_translated (&diagnostic, msg, ap,
9764 location, dlevel);
9765 if (column_override)
9766 diagnostic_override_column (&diagnostic, column_override);
9767 diagnostic_override_option_index (&diagnostic,
9768 c_option_controlling_cpp_error (reason));
9769 ret = report_diagnostic (&diagnostic);
9770 if (level == CPP_DL_WARNING_SYSHDR)
9771 global_dc->dc_warn_system_headers = save_warn_system_headers;
9772 return ret;
9775 /* Convert a character from the host to the target execution character
9776 set. cpplib handles this, mostly. */
9778 HOST_WIDE_INT
9779 c_common_to_target_charset (HOST_WIDE_INT c)
9781 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9782 zero-extended under -fno-signed-char. cpplib insists that characters
9783 and character constants are always unsigned. Hence we must convert
9784 back and forth. */
9785 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9787 uc = cpp_host_to_exec_charset (parse_in, uc);
9789 if (flag_signed_char)
9790 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9791 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9792 else
9793 return uc;
9796 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9797 references with an INDIRECT_REF of a constant at the bottom; much like the
9798 traditional rendering of offsetof as a macro. Return the folded result. */
9800 tree
9801 fold_offsetof_1 (tree expr)
9803 tree base, off, t;
9805 switch (TREE_CODE (expr))
9807 case ERROR_MARK:
9808 return expr;
9810 case VAR_DECL:
9811 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9812 return error_mark_node;
9814 case CALL_EXPR:
9815 case TARGET_EXPR:
9816 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9817 return error_mark_node;
9819 case NOP_EXPR:
9820 case INDIRECT_REF:
9821 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9823 error ("cannot apply %<offsetof%> to a non constant address");
9824 return error_mark_node;
9826 return TREE_OPERAND (expr, 0);
9828 case COMPONENT_REF:
9829 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9830 if (base == error_mark_node)
9831 return base;
9833 t = TREE_OPERAND (expr, 1);
9834 if (DECL_C_BIT_FIELD (t))
9836 error ("attempt to take address of bit-field structure "
9837 "member %qD", t);
9838 return error_mark_node;
9840 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9841 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
9842 / BITS_PER_UNIT));
9843 break;
9845 case ARRAY_REF:
9846 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9847 if (base == error_mark_node)
9848 return base;
9850 t = TREE_OPERAND (expr, 1);
9852 /* Check if the offset goes beyond the upper bound of the array. */
9853 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9855 tree upbound = array_ref_up_bound (expr);
9856 if (upbound != NULL_TREE
9857 && TREE_CODE (upbound) == INTEGER_CST
9858 && !tree_int_cst_equal (upbound,
9859 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9861 upbound = size_binop (PLUS_EXPR, upbound,
9862 build_int_cst (TREE_TYPE (upbound), 1));
9863 if (tree_int_cst_lt (upbound, t))
9865 tree v;
9867 for (v = TREE_OPERAND (expr, 0);
9868 TREE_CODE (v) == COMPONENT_REF;
9869 v = TREE_OPERAND (v, 0))
9870 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9871 == RECORD_TYPE)
9873 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9874 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9875 if (TREE_CODE (fld_chain) == FIELD_DECL)
9876 break;
9878 if (fld_chain)
9879 break;
9881 /* Don't warn if the array might be considered a poor
9882 man's flexible array member with a very permissive
9883 definition thereof. */
9884 if (TREE_CODE (v) == ARRAY_REF
9885 || TREE_CODE (v) == COMPONENT_REF)
9886 warning (OPT_Warray_bounds,
9887 "index %E denotes an offset "
9888 "greater than size of %qT",
9889 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9894 t = convert (sizetype, t);
9895 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9896 break;
9898 case COMPOUND_EXPR:
9899 /* Handle static members of volatile structs. */
9900 t = TREE_OPERAND (expr, 1);
9901 gcc_assert (TREE_CODE (t) == VAR_DECL);
9902 return fold_offsetof_1 (t);
9904 default:
9905 gcc_unreachable ();
9908 return fold_build_pointer_plus (base, off);
9911 /* Likewise, but convert it to the return type of offsetof. */
9913 tree
9914 fold_offsetof (tree expr)
9916 return convert (size_type_node, fold_offsetof_1 (expr));
9919 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9920 expression, because B will always be true. */
9922 void
9923 warn_for_omitted_condop (location_t location, tree cond)
9925 if (truth_value_p (TREE_CODE (cond)))
9926 warning_at (location, OPT_Wparentheses,
9927 "the omitted middle operand in ?: will always be %<true%>, "
9928 "suggest explicit middle operand");
9931 /* Give an error for storing into ARG, which is 'const'. USE indicates
9932 how ARG was being used. */
9934 void
9935 readonly_error (location_t loc, tree arg, enum lvalue_use use)
9937 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9938 || use == lv_asm);
9939 /* Using this macro rather than (for example) arrays of messages
9940 ensures that all the format strings are checked at compile
9941 time. */
9942 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9943 : (use == lv_increment ? (I) \
9944 : (use == lv_decrement ? (D) : (AS))))
9945 if (TREE_CODE (arg) == COMPONENT_REF)
9947 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9948 error_at (loc, READONLY_MSG (G_("assignment of member "
9949 "%qD in read-only object"),
9950 G_("increment of member "
9951 "%qD in read-only object"),
9952 G_("decrement of member "
9953 "%qD in read-only object"),
9954 G_("member %qD in read-only object "
9955 "used as %<asm%> output")),
9956 TREE_OPERAND (arg, 1));
9957 else
9958 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
9959 G_("increment of read-only member %qD"),
9960 G_("decrement of read-only member %qD"),
9961 G_("read-only member %qD used as %<asm%> output")),
9962 TREE_OPERAND (arg, 1));
9964 else if (TREE_CODE (arg) == VAR_DECL)
9965 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
9966 G_("increment of read-only variable %qD"),
9967 G_("decrement of read-only variable %qD"),
9968 G_("read-only variable %qD used as %<asm%> output")),
9969 arg);
9970 else if (TREE_CODE (arg) == PARM_DECL)
9971 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
9972 G_("increment of read-only parameter %qD"),
9973 G_("decrement of read-only parameter %qD"),
9974 G_("read-only parameter %qD use as %<asm%> output")),
9975 arg);
9976 else if (TREE_CODE (arg) == RESULT_DECL)
9978 gcc_assert (c_dialect_cxx ());
9979 error_at (loc, READONLY_MSG (G_("assignment of "
9980 "read-only named return value %qD"),
9981 G_("increment of "
9982 "read-only named return value %qD"),
9983 G_("decrement of "
9984 "read-only named return value %qD"),
9985 G_("read-only named return value %qD "
9986 "used as %<asm%>output")),
9987 arg);
9989 else if (TREE_CODE (arg) == FUNCTION_DECL)
9990 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
9991 G_("increment of function %qD"),
9992 G_("decrement of function %qD"),
9993 G_("function %qD used as %<asm%> output")),
9994 arg);
9995 else
9996 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
9997 G_("increment of read-only location %qE"),
9998 G_("decrement of read-only location %qE"),
9999 G_("read-only location %qE used as %<asm%> output")),
10000 arg);
10003 /* Print an error message for an invalid lvalue. USE says
10004 how the lvalue is being used and so selects the error message. LOC
10005 is the location for the error. */
10007 void
10008 lvalue_error (location_t loc, enum lvalue_use use)
10010 switch (use)
10012 case lv_assign:
10013 error_at (loc, "lvalue required as left operand of assignment");
10014 break;
10015 case lv_increment:
10016 error_at (loc, "lvalue required as increment operand");
10017 break;
10018 case lv_decrement:
10019 error_at (loc, "lvalue required as decrement operand");
10020 break;
10021 case lv_addressof:
10022 error_at (loc, "lvalue required as unary %<&%> operand");
10023 break;
10024 case lv_asm:
10025 error_at (loc, "lvalue required in asm statement");
10026 break;
10027 default:
10028 gcc_unreachable ();
10032 /* Print an error message for an invalid indirection of type TYPE.
10033 ERRSTRING is the name of the operator for the indirection. */
10035 void
10036 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10038 switch (errstring)
10040 case RO_NULL:
10041 gcc_assert (c_dialect_cxx ());
10042 error_at (loc, "invalid type argument (have %qT)", type);
10043 break;
10044 case RO_ARRAY_INDEXING:
10045 error_at (loc,
10046 "invalid type argument of array indexing (have %qT)",
10047 type);
10048 break;
10049 case RO_UNARY_STAR:
10050 error_at (loc,
10051 "invalid type argument of unary %<*%> (have %qT)",
10052 type);
10053 break;
10054 case RO_ARROW:
10055 error_at (loc,
10056 "invalid type argument of %<->%> (have %qT)",
10057 type);
10058 break;
10059 case RO_ARROW_STAR:
10060 error_at (loc,
10061 "invalid type argument of %<->*%> (have %qT)",
10062 type);
10063 break;
10064 case RO_IMPLICIT_CONVERSION:
10065 error_at (loc,
10066 "invalid type argument of implicit conversion (have %qT)",
10067 type);
10068 break;
10069 default:
10070 gcc_unreachable ();
10074 /* *PTYPE is an incomplete array. Complete it with a domain based on
10075 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10076 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10077 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10080 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10082 tree maxindex, type, main_type, elt, unqual_elt;
10083 int failure = 0, quals;
10084 hashval_t hashcode = 0;
10085 bool overflow_p = false;
10087 maxindex = size_zero_node;
10088 if (initial_value)
10090 if (TREE_CODE (initial_value) == STRING_CST)
10092 int eltsize
10093 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10094 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10096 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10098 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10100 if (vec_safe_is_empty (v))
10102 if (pedantic)
10103 failure = 3;
10104 maxindex = ssize_int (-1);
10106 else
10108 tree curindex;
10109 unsigned HOST_WIDE_INT cnt;
10110 constructor_elt *ce;
10111 bool fold_p = false;
10113 if ((*v)[0].index)
10114 maxindex = (*v)[0].index, fold_p = true;
10116 curindex = maxindex;
10118 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10120 bool curfold_p = false;
10121 if (ce->index)
10122 curindex = ce->index, curfold_p = true;
10123 else
10125 if (fold_p)
10127 /* Since we treat size types now as ordinary
10128 unsigned types, we need an explicit overflow
10129 check. */
10130 tree orig = curindex;
10131 curindex = fold_convert (sizetype, curindex);
10132 overflow_p |= tree_int_cst_lt (curindex, orig);
10134 curindex = size_binop (PLUS_EXPR, curindex,
10135 size_one_node);
10137 if (tree_int_cst_lt (maxindex, curindex))
10138 maxindex = curindex, fold_p = curfold_p;
10140 if (fold_p)
10142 tree orig = maxindex;
10143 maxindex = fold_convert (sizetype, maxindex);
10144 overflow_p |= tree_int_cst_lt (maxindex, orig);
10148 else
10150 /* Make an error message unless that happened already. */
10151 if (initial_value != error_mark_node)
10152 failure = 1;
10155 else
10157 failure = 2;
10158 if (!do_default)
10159 return failure;
10162 type = *ptype;
10163 elt = TREE_TYPE (type);
10164 quals = TYPE_QUALS (strip_array_types (elt));
10165 if (quals == 0)
10166 unqual_elt = elt;
10167 else
10168 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10170 /* Using build_distinct_type_copy and modifying things afterward instead
10171 of using build_array_type to create a new type preserves all of the
10172 TYPE_LANG_FLAG_? bits that the front end may have set. */
10173 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10174 TREE_TYPE (main_type) = unqual_elt;
10175 TYPE_DOMAIN (main_type)
10176 = build_range_type (TREE_TYPE (maxindex),
10177 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10178 layout_type (main_type);
10180 /* Make sure we have the canonical MAIN_TYPE. */
10181 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10182 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10183 hashcode);
10184 main_type = type_hash_canon (hashcode, main_type);
10186 /* Fix the canonical type. */
10187 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10188 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10189 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10190 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10191 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10192 != TYPE_DOMAIN (main_type)))
10193 TYPE_CANONICAL (main_type)
10194 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10195 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10196 else
10197 TYPE_CANONICAL (main_type) = main_type;
10199 if (quals == 0)
10200 type = main_type;
10201 else
10202 type = c_build_qualified_type (main_type, quals);
10204 if (COMPLETE_TYPE_P (type)
10205 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10206 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10208 error ("size of array is too large");
10209 /* If we proceed with the array type as it is, we'll eventually
10210 crash in tree_to_[su]hwi(). */
10211 type = error_mark_node;
10214 *ptype = type;
10215 return failure;
10218 /* Like c_mark_addressable but don't check register qualifier. */
10219 void
10220 c_common_mark_addressable_vec (tree t)
10222 while (handled_component_p (t))
10223 t = TREE_OPERAND (t, 0);
10224 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10225 return;
10226 TREE_ADDRESSABLE (t) = 1;
10231 /* Used to help initialize the builtin-types.def table. When a type of
10232 the correct size doesn't exist, use error_mark_node instead of NULL.
10233 The later results in segfaults even when a decl using the type doesn't
10234 get invoked. */
10236 tree
10237 builtin_type_for_size (int size, bool unsignedp)
10239 tree type = c_common_type_for_size (size, unsignedp);
10240 return type ? type : error_mark_node;
10243 /* A helper function for resolve_overloaded_builtin in resolving the
10244 overloaded __sync_ builtins. Returns a positive power of 2 if the
10245 first operand of PARAMS is a pointer to a supported data type.
10246 Returns 0 if an error is encountered. */
10248 static int
10249 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10251 tree type;
10252 int size;
10254 if (!params)
10256 error ("too few arguments to function %qE", function);
10257 return 0;
10260 type = TREE_TYPE ((*params)[0]);
10261 if (TREE_CODE (type) != POINTER_TYPE)
10262 goto incompatible;
10264 type = TREE_TYPE (type);
10265 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10266 goto incompatible;
10268 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10269 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10270 return size;
10272 incompatible:
10273 error ("incompatible type for argument %d of %qE", 1, function);
10274 return 0;
10277 /* A helper function for resolve_overloaded_builtin. Adds casts to
10278 PARAMS to make arguments match up with those of FUNCTION. Drops
10279 the variadic arguments at the end. Returns false if some error
10280 was encountered; true on success. */
10282 static bool
10283 sync_resolve_params (location_t loc, tree orig_function, tree function,
10284 vec<tree, va_gc> *params, bool orig_format)
10286 function_args_iterator iter;
10287 tree ptype;
10288 unsigned int parmnum;
10290 function_args_iter_init (&iter, TREE_TYPE (function));
10291 /* We've declared the implementation functions to use "volatile void *"
10292 as the pointer parameter, so we shouldn't get any complaints from the
10293 call to check_function_arguments what ever type the user used. */
10294 function_args_iter_next (&iter);
10295 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10296 ptype = TYPE_MAIN_VARIANT (ptype);
10298 /* For the rest of the values, we need to cast these to FTYPE, so that we
10299 don't get warnings for passing pointer types, etc. */
10300 parmnum = 0;
10301 while (1)
10303 tree val, arg_type;
10305 arg_type = function_args_iter_cond (&iter);
10306 /* XXX void_type_node belies the abstraction. */
10307 if (arg_type == void_type_node)
10308 break;
10310 ++parmnum;
10311 if (params->length () <= parmnum)
10313 error_at (loc, "too few arguments to function %qE", orig_function);
10314 return false;
10317 /* Only convert parameters if arg_type is unsigned integer type with
10318 new format sync routines, i.e. don't attempt to convert pointer
10319 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10320 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10321 kinds). */
10322 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10324 /* Ideally for the first conversion we'd use convert_for_assignment
10325 so that we get warnings for anything that doesn't match the pointer
10326 type. This isn't portable across the C and C++ front ends atm. */
10327 val = (*params)[parmnum];
10328 val = convert (ptype, val);
10329 val = convert (arg_type, val);
10330 (*params)[parmnum] = val;
10333 function_args_iter_next (&iter);
10336 /* __atomic routines are not variadic. */
10337 if (!orig_format && params->length () != parmnum + 1)
10339 error_at (loc, "too many arguments to function %qE", orig_function);
10340 return false;
10343 /* The definition of these primitives is variadic, with the remaining
10344 being "an optional list of variables protected by the memory barrier".
10345 No clue what that's supposed to mean, precisely, but we consider all
10346 call-clobbered variables to be protected so we're safe. */
10347 params->truncate (parmnum + 1);
10349 return true;
10352 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10353 RESULT to make it match the type of the first pointer argument in
10354 PARAMS. */
10356 static tree
10357 sync_resolve_return (tree first_param, tree result, bool orig_format)
10359 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10360 tree rtype = TREE_TYPE (result);
10361 ptype = TYPE_MAIN_VARIANT (ptype);
10363 /* New format doesn't require casting unless the types are the same size. */
10364 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10365 return convert (ptype, result);
10366 else
10367 return result;
10370 /* This function verifies the PARAMS to generic atomic FUNCTION.
10371 It returns the size if all the parameters are the same size, otherwise
10372 0 is returned if the parameters are invalid. */
10374 static int
10375 get_atomic_generic_size (location_t loc, tree function,
10376 vec<tree, va_gc> *params)
10378 unsigned int n_param;
10379 unsigned int n_model;
10380 unsigned int x;
10381 int size_0;
10382 tree type_0;
10384 /* Determine the parameter makeup. */
10385 switch (DECL_FUNCTION_CODE (function))
10387 case BUILT_IN_ATOMIC_EXCHANGE:
10388 n_param = 4;
10389 n_model = 1;
10390 break;
10391 case BUILT_IN_ATOMIC_LOAD:
10392 case BUILT_IN_ATOMIC_STORE:
10393 n_param = 3;
10394 n_model = 1;
10395 break;
10396 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10397 n_param = 6;
10398 n_model = 2;
10399 break;
10400 default:
10401 gcc_unreachable ();
10404 if (vec_safe_length (params) != n_param)
10406 error_at (loc, "incorrect number of arguments to function %qE", function);
10407 return 0;
10410 /* Get type of first parameter, and determine its size. */
10411 type_0 = TREE_TYPE ((*params)[0]);
10412 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10414 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10415 function);
10416 return 0;
10419 /* Types must be compile time constant sizes. */
10420 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10422 error_at (loc,
10423 "argument 1 of %qE must be a pointer to a constant size type",
10424 function);
10425 return 0;
10428 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10430 /* Zero size objects are not allowed. */
10431 if (size_0 == 0)
10433 error_at (loc,
10434 "argument 1 of %qE must be a pointer to a nonzero size object",
10435 function);
10436 return 0;
10439 /* Check each other parameter is a pointer and the same size. */
10440 for (x = 0; x < n_param - n_model; x++)
10442 int size;
10443 tree type = TREE_TYPE ((*params)[x]);
10444 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10445 if (n_param == 6 && x == 3)
10446 continue;
10447 if (!POINTER_TYPE_P (type))
10449 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10450 function);
10451 return 0;
10453 size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10454 if (size != size_0)
10456 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10457 function);
10458 return 0;
10462 /* Check memory model parameters for validity. */
10463 for (x = n_param - n_model ; x < n_param; x++)
10465 tree p = (*params)[x];
10466 if (TREE_CODE (p) == INTEGER_CST)
10468 int i = tree_to_uhwi (p);
10469 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10471 warning_at (loc, OPT_Winvalid_memory_model,
10472 "invalid memory model argument %d of %qE", x + 1,
10473 function);
10476 else
10477 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10479 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10480 function);
10481 return 0;
10485 return size_0;
10489 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10490 at the beginning of the parameter list PARAMS representing the size of the
10491 objects. This is to match the library ABI requirement. LOC is the location
10492 of the function call.
10493 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10494 returned to allow the external call to be constructed. */
10496 static tree
10497 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10498 vec<tree, va_gc> *params)
10500 tree size_node;
10502 /* Insert a SIZE_T parameter as the first param. If there isn't
10503 enough space, allocate a new vector and recursively re-build with that. */
10504 if (!params->space (1))
10506 unsigned int z, len;
10507 vec<tree, va_gc> *v;
10508 tree f;
10510 len = params->length ();
10511 vec_alloc (v, len + 1);
10512 v->quick_push (build_int_cst (size_type_node, n));
10513 for (z = 0; z < len; z++)
10514 v->quick_push ((*params)[z]);
10515 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10516 vec_free (v);
10517 return f;
10520 /* Add the size parameter and leave as a function call for processing. */
10521 size_node = build_int_cst (size_type_node, n);
10522 params->quick_insert (0, size_node);
10523 return NULL_TREE;
10527 /* Return whether atomic operations for naturally aligned N-byte
10528 arguments are supported, whether inline or through libatomic. */
10529 static bool
10530 atomic_size_supported_p (int n)
10532 switch (n)
10534 case 1:
10535 case 2:
10536 case 4:
10537 case 8:
10538 return true;
10540 case 16:
10541 return targetm.scalar_mode_supported_p (TImode);
10543 default:
10544 return false;
10548 /* This will process an __atomic_exchange function call, determine whether it
10549 needs to be mapped to the _N variation, or turned into a library call.
10550 LOC is the location of the builtin call.
10551 FUNCTION is the DECL that has been invoked;
10552 PARAMS is the argument list for the call. The return value is non-null
10553 TRUE is returned if it is translated into the proper format for a call to the
10554 external library, and NEW_RETURN is set the tree for that function.
10555 FALSE is returned if processing for the _N variation is required, and
10556 NEW_RETURN is set to the the return value the result is copied into. */
10557 static bool
10558 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10559 vec<tree, va_gc> *params, tree *new_return)
10561 tree p0, p1, p2, p3;
10562 tree I_type, I_type_ptr;
10563 int n = get_atomic_generic_size (loc, function, params);
10565 /* Size of 0 is an error condition. */
10566 if (n == 0)
10568 *new_return = error_mark_node;
10569 return true;
10572 /* If not a lock-free size, change to the library generic format. */
10573 if (!atomic_size_supported_p (n))
10575 *new_return = add_atomic_size_parameter (n, loc, function, params);
10576 return true;
10579 /* Otherwise there is a lockfree match, transform the call from:
10580 void fn(T* mem, T* desired, T* return, model)
10581 into
10582 *return = (T) (fn (In* mem, (In) *desired, model)) */
10584 p0 = (*params)[0];
10585 p1 = (*params)[1];
10586 p2 = (*params)[2];
10587 p3 = (*params)[3];
10589 /* Create pointer to appropriate size. */
10590 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10591 I_type_ptr = build_pointer_type (I_type);
10593 /* Convert object pointer to required type. */
10594 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10595 (*params)[0] = p0;
10596 /* Convert new value to required type, and dereference it. */
10597 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10598 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10599 (*params)[1] = p1;
10601 /* Move memory model to the 3rd position, and end param list. */
10602 (*params)[2] = p3;
10603 params->truncate (3);
10605 /* Convert return pointer and dereference it for later assignment. */
10606 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10608 return false;
10612 /* This will process an __atomic_compare_exchange function call, determine
10613 whether it needs to be mapped to the _N variation, or turned into a lib call.
10614 LOC is the location of the builtin call.
10615 FUNCTION is the DECL that has been invoked;
10616 PARAMS is the argument list for the call. The return value is non-null
10617 TRUE is returned if it is translated into the proper format for a call to the
10618 external library, and NEW_RETURN is set the tree for that function.
10619 FALSE is returned if processing for the _N variation is required. */
10621 static bool
10622 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10623 vec<tree, va_gc> *params,
10624 tree *new_return)
10626 tree p0, p1, p2;
10627 tree I_type, I_type_ptr;
10628 int n = get_atomic_generic_size (loc, function, params);
10630 /* Size of 0 is an error condition. */
10631 if (n == 0)
10633 *new_return = error_mark_node;
10634 return true;
10637 /* If not a lock-free size, change to the library generic format. */
10638 if (!atomic_size_supported_p (n))
10640 /* The library generic format does not have the weak parameter, so
10641 remove it from the param list. Since a parameter has been removed,
10642 we can be sure that there is room for the SIZE_T parameter, meaning
10643 there will not be a recursive rebuilding of the parameter list, so
10644 there is no danger this will be done twice. */
10645 if (n > 0)
10647 (*params)[3] = (*params)[4];
10648 (*params)[4] = (*params)[5];
10649 params->truncate (5);
10651 *new_return = add_atomic_size_parameter (n, loc, function, params);
10652 return true;
10655 /* Otherwise, there is a match, so the call needs to be transformed from:
10656 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10657 into
10658 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10660 p0 = (*params)[0];
10661 p1 = (*params)[1];
10662 p2 = (*params)[2];
10664 /* Create pointer to appropriate size. */
10665 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10666 I_type_ptr = build_pointer_type (I_type);
10668 /* Convert object pointer to required type. */
10669 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10670 (*params)[0] = p0;
10672 /* Convert expected pointer to required type. */
10673 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10674 (*params)[1] = p1;
10676 /* Convert desired value to required type, and dereference it. */
10677 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10678 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10679 (*params)[2] = p2;
10681 /* The rest of the parameters are fine. NULL means no special return value
10682 processing.*/
10683 *new_return = NULL;
10684 return false;
10688 /* This will process an __atomic_load function call, determine whether it
10689 needs to be mapped to the _N variation, or turned into a library call.
10690 LOC is the location of the builtin call.
10691 FUNCTION is the DECL that has been invoked;
10692 PARAMS is the argument list for the call. The return value is non-null
10693 TRUE is returned if it is translated into the proper format for a call to the
10694 external library, and NEW_RETURN is set the tree for that function.
10695 FALSE is returned if processing for the _N variation is required, and
10696 NEW_RETURN is set to the the return value the result is copied into. */
10698 static bool
10699 resolve_overloaded_atomic_load (location_t loc, tree function,
10700 vec<tree, va_gc> *params, tree *new_return)
10702 tree p0, p1, p2;
10703 tree I_type, I_type_ptr;
10704 int n = get_atomic_generic_size (loc, function, params);
10706 /* Size of 0 is an error condition. */
10707 if (n == 0)
10709 *new_return = error_mark_node;
10710 return true;
10713 /* If not a lock-free size, change to the library generic format. */
10714 if (!atomic_size_supported_p (n))
10716 *new_return = add_atomic_size_parameter (n, loc, function, params);
10717 return true;
10720 /* Otherwise, there is a match, so the call needs to be transformed from:
10721 void fn(T* mem, T* return, model)
10722 into
10723 *return = (T) (fn ((In *) mem, model)) */
10725 p0 = (*params)[0];
10726 p1 = (*params)[1];
10727 p2 = (*params)[2];
10729 /* Create pointer to appropriate size. */
10730 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10731 I_type_ptr = build_pointer_type (I_type);
10733 /* Convert object pointer to required type. */
10734 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10735 (*params)[0] = p0;
10737 /* Move memory model to the 2nd position, and end param list. */
10738 (*params)[1] = p2;
10739 params->truncate (2);
10741 /* Convert return pointer and dereference it for later assignment. */
10742 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10744 return false;
10748 /* This will process an __atomic_store function call, determine whether it
10749 needs to be mapped to the _N variation, or turned into a library call.
10750 LOC is the location of the builtin call.
10751 FUNCTION is the DECL that has been invoked;
10752 PARAMS is the argument list for the call. The return value is non-null
10753 TRUE is returned if it is translated into the proper format for a call to the
10754 external library, and NEW_RETURN is set the tree for that function.
10755 FALSE is returned if processing for the _N variation is required, and
10756 NEW_RETURN is set to the the return value the result is copied into. */
10758 static bool
10759 resolve_overloaded_atomic_store (location_t loc, tree function,
10760 vec<tree, va_gc> *params, tree *new_return)
10762 tree p0, p1;
10763 tree I_type, I_type_ptr;
10764 int n = get_atomic_generic_size (loc, function, params);
10766 /* Size of 0 is an error condition. */
10767 if (n == 0)
10769 *new_return = error_mark_node;
10770 return true;
10773 /* If not a lock-free size, change to the library generic format. */
10774 if (!atomic_size_supported_p (n))
10776 *new_return = add_atomic_size_parameter (n, loc, function, params);
10777 return true;
10780 /* Otherwise, there is a match, so the call needs to be transformed from:
10781 void fn(T* mem, T* value, model)
10782 into
10783 fn ((In *) mem, (In) *value, model) */
10785 p0 = (*params)[0];
10786 p1 = (*params)[1];
10788 /* Create pointer to appropriate size. */
10789 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10790 I_type_ptr = build_pointer_type (I_type);
10792 /* Convert object pointer to required type. */
10793 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10794 (*params)[0] = p0;
10796 /* Convert new value to required type, and dereference it. */
10797 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10798 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10799 (*params)[1] = p1;
10801 /* The memory model is in the right spot already. Return is void. */
10802 *new_return = NULL_TREE;
10804 return false;
10808 /* Some builtin functions are placeholders for other expressions. This
10809 function should be called immediately after parsing the call expression
10810 before surrounding code has committed to the type of the expression.
10812 LOC is the location of the builtin call.
10814 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10815 PARAMS is the argument list for the call. The return value is non-null
10816 when expansion is complete, and null if normal processing should
10817 continue. */
10819 tree
10820 resolve_overloaded_builtin (location_t loc, tree function,
10821 vec<tree, va_gc> *params)
10823 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10824 bool orig_format = true;
10825 tree new_return = NULL_TREE;
10827 switch (DECL_BUILT_IN_CLASS (function))
10829 case BUILT_IN_NORMAL:
10830 break;
10831 case BUILT_IN_MD:
10832 if (targetm.resolve_overloaded_builtin)
10833 return targetm.resolve_overloaded_builtin (loc, function, params);
10834 else
10835 return NULL_TREE;
10836 default:
10837 return NULL_TREE;
10840 /* Handle BUILT_IN_NORMAL here. */
10841 switch (orig_code)
10843 case BUILT_IN_ATOMIC_EXCHANGE:
10844 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10845 case BUILT_IN_ATOMIC_LOAD:
10846 case BUILT_IN_ATOMIC_STORE:
10848 /* Handle these 4 together so that they can fall through to the next
10849 case if the call is transformed to an _N variant. */
10850 switch (orig_code)
10852 case BUILT_IN_ATOMIC_EXCHANGE:
10854 if (resolve_overloaded_atomic_exchange (loc, function, params,
10855 &new_return))
10856 return new_return;
10857 /* Change to the _N variant. */
10858 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10859 break;
10862 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10864 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10865 params,
10866 &new_return))
10867 return new_return;
10868 /* Change to the _N variant. */
10869 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10870 break;
10872 case BUILT_IN_ATOMIC_LOAD:
10874 if (resolve_overloaded_atomic_load (loc, function, params,
10875 &new_return))
10876 return new_return;
10877 /* Change to the _N variant. */
10878 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10879 break;
10881 case BUILT_IN_ATOMIC_STORE:
10883 if (resolve_overloaded_atomic_store (loc, function, params,
10884 &new_return))
10885 return new_return;
10886 /* Change to the _N variant. */
10887 orig_code = BUILT_IN_ATOMIC_STORE_N;
10888 break;
10890 default:
10891 gcc_unreachable ();
10893 /* Fallthrough to the normal processing. */
10895 case BUILT_IN_ATOMIC_EXCHANGE_N:
10896 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10897 case BUILT_IN_ATOMIC_LOAD_N:
10898 case BUILT_IN_ATOMIC_STORE_N:
10899 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10900 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10901 case BUILT_IN_ATOMIC_AND_FETCH_N:
10902 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10903 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10904 case BUILT_IN_ATOMIC_OR_FETCH_N:
10905 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10906 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10907 case BUILT_IN_ATOMIC_FETCH_AND_N:
10908 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10909 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10910 case BUILT_IN_ATOMIC_FETCH_OR_N:
10912 orig_format = false;
10913 /* Fallthru for parameter processing. */
10915 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10916 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10917 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10918 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10919 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10920 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10921 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10922 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10923 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10924 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10925 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10926 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10927 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10928 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10929 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10930 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10932 int n = sync_resolve_size (function, params);
10933 tree new_function, first_param, result;
10934 enum built_in_function fncode;
10936 if (n == 0)
10937 return error_mark_node;
10939 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10940 new_function = builtin_decl_explicit (fncode);
10941 if (!sync_resolve_params (loc, function, new_function, params,
10942 orig_format))
10943 return error_mark_node;
10945 first_param = (*params)[0];
10946 result = build_function_call_vec (loc, vNULL, new_function, params,
10947 NULL);
10948 if (result == error_mark_node)
10949 return result;
10950 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10951 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10952 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10953 result = sync_resolve_return (first_param, result, orig_format);
10955 /* If new_return is set, assign function to that expr and cast the
10956 result to void since the generic interface returned void. */
10957 if (new_return)
10959 /* Cast function result from I{1,2,4,8,16} to the required type. */
10960 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10961 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10962 result);
10963 TREE_SIDE_EFFECTS (result) = 1;
10964 protected_set_expr_location (result, loc);
10965 result = convert (void_type_node, result);
10967 return result;
10970 default:
10971 return NULL_TREE;
10975 /* vector_types_compatible_elements_p is used in type checks of vectors
10976 values used as operands of binary operators. Where it returns true, and
10977 the other checks of the caller succeed (being vector types in he first
10978 place, and matching number of elements), we can just treat the types
10979 as essentially the same.
10980 Contrast with vector_targets_convertible_p, which is used for vector
10981 pointer types, and vector_types_convertible_p, which will allow
10982 language-specific matches under the control of flag_lax_vector_conversions,
10983 and might still require a conversion. */
10984 /* True if vector types T1 and T2 can be inputs to the same binary
10985 operator without conversion.
10986 We don't check the overall vector size here because some of our callers
10987 want to give different error messages when the vectors are compatible
10988 except for the element count. */
10990 bool
10991 vector_types_compatible_elements_p (tree t1, tree t2)
10993 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10994 t1 = TREE_TYPE (t1);
10995 t2 = TREE_TYPE (t2);
10997 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10999 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11000 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11001 || c2 == FIXED_POINT_TYPE));
11003 t1 = c_common_signed_type (t1);
11004 t2 = c_common_signed_type (t2);
11005 /* Equality works here because c_common_signed_type uses
11006 TYPE_MAIN_VARIANT. */
11007 if (t1 == t2)
11008 return true;
11009 if (opaque && c1 == c2
11010 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11011 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11012 return true;
11013 return false;
11016 /* Check for missing format attributes on function pointers. LTYPE is
11017 the new type or left-hand side type. RTYPE is the old type or
11018 right-hand side type. Returns TRUE if LTYPE is missing the desired
11019 attribute. */
11021 bool
11022 check_missing_format_attribute (tree ltype, tree rtype)
11024 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11025 tree ra;
11027 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11028 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11029 break;
11030 if (ra)
11032 tree la;
11033 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11034 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11035 break;
11036 return !la;
11038 else
11039 return false;
11042 /* Subscripting with type char is likely to lose on a machine where
11043 chars are signed. So warn on any machine, but optionally. Don't
11044 warn for unsigned char since that type is safe. Don't warn for
11045 signed char because anyone who uses that must have done so
11046 deliberately. Furthermore, we reduce the false positive load by
11047 warning only for non-constant value of type char. */
11049 void
11050 warn_array_subscript_with_type_char (tree index)
11052 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11053 && TREE_CODE (index) != INTEGER_CST)
11054 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11057 /* Implement -Wparentheses for the unexpected C precedence rules, to
11058 cover cases like x + y << z which readers are likely to
11059 misinterpret. We have seen an expression in which CODE is a binary
11060 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11061 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11062 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11063 expression was not formed using a binary or unary operator, or it
11064 was enclosed in parentheses. */
11066 void
11067 warn_about_parentheses (location_t loc, enum tree_code code,
11068 enum tree_code code_left, tree arg_left,
11069 enum tree_code code_right, tree arg_right)
11071 if (!warn_parentheses)
11072 return;
11074 /* This macro tests that the expression ARG with original tree code
11075 CODE appears to be a boolean expression. or the result of folding a
11076 boolean expression. */
11077 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11078 (truth_value_p (TREE_CODE (ARG)) \
11079 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11080 /* Folding may create 0 or 1 integers from other expressions. */ \
11081 || ((CODE) != INTEGER_CST \
11082 && (integer_onep (ARG) || integer_zerop (ARG))))
11084 switch (code)
11086 case LSHIFT_EXPR:
11087 if (code_left == PLUS_EXPR)
11088 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11089 "suggest parentheses around %<+%> inside %<<<%>");
11090 else if (code_right == PLUS_EXPR)
11091 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11092 "suggest parentheses around %<+%> inside %<<<%>");
11093 else if (code_left == MINUS_EXPR)
11094 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11095 "suggest parentheses around %<-%> inside %<<<%>");
11096 else if (code_right == MINUS_EXPR)
11097 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11098 "suggest parentheses around %<-%> inside %<<<%>");
11099 return;
11101 case RSHIFT_EXPR:
11102 if (code_left == PLUS_EXPR)
11103 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11104 "suggest parentheses around %<+%> inside %<>>%>");
11105 else if (code_right == PLUS_EXPR)
11106 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11107 "suggest parentheses around %<+%> inside %<>>%>");
11108 else if (code_left == MINUS_EXPR)
11109 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11110 "suggest parentheses around %<-%> inside %<>>%>");
11111 else if (code_right == MINUS_EXPR)
11112 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11113 "suggest parentheses around %<-%> inside %<>>%>");
11114 return;
11116 case TRUTH_ORIF_EXPR:
11117 if (code_left == TRUTH_ANDIF_EXPR)
11118 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11119 "suggest parentheses around %<&&%> within %<||%>");
11120 else if (code_right == TRUTH_ANDIF_EXPR)
11121 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11122 "suggest parentheses around %<&&%> within %<||%>");
11123 return;
11125 case BIT_IOR_EXPR:
11126 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11127 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11128 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11129 "suggest parentheses around arithmetic in operand of %<|%>");
11130 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11131 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11132 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11133 "suggest parentheses around arithmetic in operand of %<|%>");
11134 /* Check cases like x|y==z */
11135 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11136 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11137 "suggest parentheses around comparison in operand of %<|%>");
11138 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11139 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11140 "suggest parentheses around comparison in operand of %<|%>");
11141 /* Check cases like !x | y */
11142 else if (code_left == TRUTH_NOT_EXPR
11143 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11144 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11145 "suggest parentheses around operand of "
11146 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11147 return;
11149 case BIT_XOR_EXPR:
11150 if (code_left == BIT_AND_EXPR
11151 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11152 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11153 "suggest parentheses around arithmetic in operand of %<^%>");
11154 else if (code_right == BIT_AND_EXPR
11155 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11156 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11157 "suggest parentheses around arithmetic in operand of %<^%>");
11158 /* Check cases like x^y==z */
11159 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11160 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11161 "suggest parentheses around comparison in operand of %<^%>");
11162 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11163 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11164 "suggest parentheses around comparison in operand of %<^%>");
11165 return;
11167 case BIT_AND_EXPR:
11168 if (code_left == PLUS_EXPR)
11169 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11170 "suggest parentheses around %<+%> in operand of %<&%>");
11171 else if (code_right == PLUS_EXPR)
11172 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11173 "suggest parentheses around %<+%> in operand of %<&%>");
11174 else if (code_left == MINUS_EXPR)
11175 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11176 "suggest parentheses around %<-%> in operand of %<&%>");
11177 else if (code_right == MINUS_EXPR)
11178 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11179 "suggest parentheses around %<-%> in operand of %<&%>");
11180 /* Check cases like x&y==z */
11181 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11182 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11183 "suggest parentheses around comparison in operand of %<&%>");
11184 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11185 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11186 "suggest parentheses around comparison in operand of %<&%>");
11187 /* Check cases like !x & y */
11188 else if (code_left == TRUTH_NOT_EXPR
11189 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11190 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11191 "suggest parentheses around operand of "
11192 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11193 return;
11195 case EQ_EXPR:
11196 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11197 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11198 "suggest parentheses around comparison in operand of %<==%>");
11199 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11200 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11201 "suggest parentheses around comparison in operand of %<==%>");
11202 return;
11203 case NE_EXPR:
11204 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11205 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11206 "suggest parentheses around comparison in operand of %<!=%>");
11207 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11208 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11209 "suggest parentheses around comparison in operand of %<!=%>");
11210 return;
11212 default:
11213 if (TREE_CODE_CLASS (code) == tcc_comparison)
11215 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11216 && code_left != NE_EXPR && code_left != EQ_EXPR
11217 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11218 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11219 "comparisons like %<X<=Y<=Z%> do not "
11220 "have their mathematical meaning");
11221 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11222 && code_right != NE_EXPR && code_right != EQ_EXPR
11223 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11224 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11225 "comparisons like %<X<=Y<=Z%> do not "
11226 "have their mathematical meaning");
11228 return;
11230 #undef NOT_A_BOOLEAN_EXPR_P
11233 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11235 void
11236 warn_for_unused_label (tree label)
11238 if (!TREE_USED (label))
11240 if (DECL_INITIAL (label))
11241 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11242 else
11243 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11247 /* Warn for division by zero according to the value of DIVISOR. LOC
11248 is the location of the division operator. */
11250 void
11251 warn_for_div_by_zero (location_t loc, tree divisor)
11253 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11254 about division by zero. Do not issue a warning if DIVISOR has a
11255 floating-point type, since we consider 0.0/0.0 a valid way of
11256 generating a NaN. */
11257 if (c_inhibit_evaluation_warnings == 0
11258 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11259 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11262 /* Subroutine of build_binary_op. Give warnings for comparisons
11263 between signed and unsigned quantities that may fail. Do the
11264 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11265 so that casts will be considered, but default promotions won't
11268 LOCATION is the location of the comparison operator.
11270 The arguments of this function map directly to local variables
11271 of build_binary_op. */
11273 void
11274 warn_for_sign_compare (location_t location,
11275 tree orig_op0, tree orig_op1,
11276 tree op0, tree op1,
11277 tree result_type, enum tree_code resultcode)
11279 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11280 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11281 int unsignedp0, unsignedp1;
11283 /* In C++, check for comparison of different enum types. */
11284 if (c_dialect_cxx()
11285 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11286 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11287 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11288 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11290 warning_at (location,
11291 OPT_Wsign_compare, "comparison between types %qT and %qT",
11292 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11295 /* Do not warn if the comparison is being done in a signed type,
11296 since the signed type will only be chosen if it can represent
11297 all the values of the unsigned type. */
11298 if (!TYPE_UNSIGNED (result_type))
11299 /* OK */;
11300 /* Do not warn if both operands are unsigned. */
11301 else if (op0_signed == op1_signed)
11302 /* OK */;
11303 else
11305 tree sop, uop, base_type;
11306 bool ovf;
11308 if (op0_signed)
11309 sop = orig_op0, uop = orig_op1;
11310 else
11311 sop = orig_op1, uop = orig_op0;
11313 STRIP_TYPE_NOPS (sop);
11314 STRIP_TYPE_NOPS (uop);
11315 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11316 ? TREE_TYPE (result_type) : result_type);
11318 /* Do not warn if the signed quantity is an unsuffixed integer
11319 literal (or some static constant expression involving such
11320 literals or a conditional expression involving such literals)
11321 and it is non-negative. */
11322 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11323 /* OK */;
11324 /* Do not warn if the comparison is an equality operation, the
11325 unsigned quantity is an integral constant, and it would fit
11326 in the result if the result were signed. */
11327 else if (TREE_CODE (uop) == INTEGER_CST
11328 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11329 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11330 /* OK */;
11331 /* In C, do not warn if the unsigned quantity is an enumeration
11332 constant and its maximum value would fit in the result if the
11333 result were signed. */
11334 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11335 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11336 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11337 c_common_signed_type (base_type)))
11338 /* OK */;
11339 else
11340 warning_at (location,
11341 OPT_Wsign_compare,
11342 "comparison between signed and unsigned integer expressions");
11345 /* Warn if two unsigned values are being compared in a size larger
11346 than their original size, and one (and only one) is the result of
11347 a `~' operator. This comparison will always fail.
11349 Also warn if one operand is a constant, and the constant does not
11350 have all bits set that are set in the ~ operand when it is
11351 extended. */
11353 op0 = c_common_get_narrower (op0, &unsignedp0);
11354 op1 = c_common_get_narrower (op1, &unsignedp1);
11356 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11357 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11359 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11360 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11361 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11362 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11364 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11366 tree primop;
11367 HOST_WIDE_INT constant, mask;
11368 int unsignedp;
11369 unsigned int bits;
11371 if (tree_fits_shwi_p (op0))
11373 primop = op1;
11374 unsignedp = unsignedp1;
11375 constant = tree_to_shwi (op0);
11377 else
11379 primop = op0;
11380 unsignedp = unsignedp0;
11381 constant = tree_to_shwi (op1);
11384 bits = TYPE_PRECISION (TREE_TYPE (primop));
11385 if (bits < TYPE_PRECISION (result_type)
11386 && bits < HOST_BITS_PER_LONG && unsignedp)
11388 mask = (~ (HOST_WIDE_INT) 0) << bits;
11389 if ((mask & constant) != mask)
11391 if (constant == 0)
11392 warning_at (location, OPT_Wsign_compare,
11393 "promoted ~unsigned is always non-zero");
11394 else
11395 warning_at (location, OPT_Wsign_compare,
11396 "comparison of promoted ~unsigned with constant");
11400 else if (unsignedp0 && unsignedp1
11401 && (TYPE_PRECISION (TREE_TYPE (op0))
11402 < TYPE_PRECISION (result_type))
11403 && (TYPE_PRECISION (TREE_TYPE (op1))
11404 < TYPE_PRECISION (result_type)))
11405 warning_at (location, OPT_Wsign_compare,
11406 "comparison of promoted ~unsigned with unsigned");
11410 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11411 type via c_common_type. If -Wdouble-promotion is in use, and the
11412 conditions for warning have been met, issue a warning. GMSGID is
11413 the warning message. It must have two %T specifiers for the type
11414 that was converted (generally "float") and the type to which it was
11415 converted (generally "double), respectively. LOC is the location
11416 to which the awrning should refer. */
11418 void
11419 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11420 const char *gmsgid, location_t loc)
11422 tree source_type;
11424 if (!warn_double_promotion)
11425 return;
11426 /* If the conversion will not occur at run-time, there is no need to
11427 warn about it. */
11428 if (c_inhibit_evaluation_warnings)
11429 return;
11430 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11431 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11432 return;
11433 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11434 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11435 source_type = type1;
11436 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11437 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11438 source_type = type2;
11439 else
11440 return;
11441 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11444 /* Setup a TYPE_DECL node as a typedef representation.
11446 X is a TYPE_DECL for a typedef statement. Create a brand new
11447 ..._TYPE node (which will be just a variant of the existing
11448 ..._TYPE node with identical properties) and then install X
11449 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11451 The whole point here is to end up with a situation where each
11452 and every ..._TYPE node the compiler creates will be uniquely
11453 associated with AT MOST one node representing a typedef name.
11454 This way, even though the compiler substitutes corresponding
11455 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11456 early on, later parts of the compiler can always do the reverse
11457 translation and get back the corresponding typedef name. For
11458 example, given:
11460 typedef struct S MY_TYPE;
11461 MY_TYPE object;
11463 Later parts of the compiler might only know that `object' was of
11464 type `struct S' if it were not for code just below. With this
11465 code however, later parts of the compiler see something like:
11467 struct S' == struct S
11468 typedef struct S' MY_TYPE;
11469 struct S' object;
11471 And they can then deduce (from the node for type struct S') that
11472 the original object declaration was:
11474 MY_TYPE object;
11476 Being able to do this is important for proper support of protoize,
11477 and also for generating precise symbolic debugging information
11478 which takes full account of the programmer's (typedef) vocabulary.
11480 Obviously, we don't want to generate a duplicate ..._TYPE node if
11481 the TYPE_DECL node that we are now processing really represents a
11482 standard built-in type. */
11484 void
11485 set_underlying_type (tree x)
11487 if (x == error_mark_node)
11488 return;
11489 if (DECL_IS_BUILTIN (x))
11491 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11492 TYPE_NAME (TREE_TYPE (x)) = x;
11494 else if (TREE_TYPE (x) != error_mark_node
11495 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11497 tree tt = TREE_TYPE (x);
11498 DECL_ORIGINAL_TYPE (x) = tt;
11499 tt = build_variant_type_copy (tt);
11500 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11501 TYPE_NAME (tt) = x;
11502 TREE_USED (tt) = TREE_USED (x);
11503 TREE_TYPE (x) = tt;
11507 /* Record the types used by the current global variable declaration
11508 being parsed, so that we can decide later to emit their debug info.
11509 Those types are in types_used_by_cur_var_decl, and we are going to
11510 store them in the types_used_by_vars_hash hash table.
11511 DECL is the declaration of the global variable that has been parsed. */
11513 void
11514 record_types_used_by_current_var_decl (tree decl)
11516 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11518 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11520 tree type = types_used_by_cur_var_decl->pop ();
11521 types_used_by_var_decl_insert (type, decl);
11525 /* If DECL is a typedef that is declared in the current function,
11526 record it for the purpose of -Wunused-local-typedefs. */
11528 void
11529 record_locally_defined_typedef (tree decl)
11531 struct c_language_function *l;
11533 if (!warn_unused_local_typedefs
11534 || cfun == NULL
11535 /* if this is not a locally defined typedef then we are not
11536 interested. */
11537 || !is_typedef_decl (decl)
11538 || !decl_function_context (decl))
11539 return;
11541 l = (struct c_language_function *) cfun->language;
11542 vec_safe_push (l->local_typedefs, decl);
11545 /* If T is a TYPE_DECL declared locally, mark it as used. */
11547 void
11548 maybe_record_typedef_use (tree t)
11550 if (!is_typedef_decl (t))
11551 return;
11553 TREE_USED (t) = true;
11556 /* Warn if there are some unused locally defined typedefs in the
11557 current function. */
11559 void
11560 maybe_warn_unused_local_typedefs (void)
11562 int i;
11563 tree decl;
11564 /* The number of times we have emitted -Wunused-local-typedefs
11565 warnings. If this is different from errorcount, that means some
11566 unrelated errors have been issued. In which case, we'll avoid
11567 emitting "unused-local-typedefs" warnings. */
11568 static int unused_local_typedefs_warn_count;
11569 struct c_language_function *l;
11571 if (cfun == NULL)
11572 return;
11574 if ((l = (struct c_language_function *) cfun->language) == NULL)
11575 return;
11577 if (warn_unused_local_typedefs
11578 && errorcount == unused_local_typedefs_warn_count)
11580 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11581 if (!TREE_USED (decl))
11582 warning_at (DECL_SOURCE_LOCATION (decl),
11583 OPT_Wunused_local_typedefs,
11584 "typedef %qD locally defined but not used", decl);
11585 unused_local_typedefs_warn_count = errorcount;
11588 vec_free (l->local_typedefs);
11591 /* The C and C++ parsers both use vectors to hold function arguments.
11592 For efficiency, we keep a cache of unused vectors. This is the
11593 cache. */
11595 typedef vec<tree, va_gc> *tree_gc_vec;
11596 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11598 /* Return a new vector from the cache. If the cache is empty,
11599 allocate a new vector. These vectors are GC'ed, so it is OK if the
11600 pointer is not released.. */
11602 vec<tree, va_gc> *
11603 make_tree_vector (void)
11605 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11606 return tree_vector_cache->pop ();
11607 else
11609 /* Passing 0 to vec::alloc returns NULL, and our callers require
11610 that we always return a non-NULL value. The vector code uses
11611 4 when growing a NULL vector, so we do too. */
11612 vec<tree, va_gc> *v;
11613 vec_alloc (v, 4);
11614 return v;
11618 /* Release a vector of trees back to the cache. */
11620 void
11621 release_tree_vector (vec<tree, va_gc> *vec)
11623 if (vec != NULL)
11625 vec->truncate (0);
11626 vec_safe_push (tree_vector_cache, vec);
11630 /* Get a new tree vector holding a single tree. */
11632 vec<tree, va_gc> *
11633 make_tree_vector_single (tree t)
11635 vec<tree, va_gc> *ret = make_tree_vector ();
11636 ret->quick_push (t);
11637 return ret;
11640 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11642 vec<tree, va_gc> *
11643 make_tree_vector_from_list (tree list)
11645 vec<tree, va_gc> *ret = make_tree_vector ();
11646 for (; list; list = TREE_CHAIN (list))
11647 vec_safe_push (ret, TREE_VALUE (list));
11648 return ret;
11651 /* Get a new tree vector which is a copy of an existing one. */
11653 vec<tree, va_gc> *
11654 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11656 vec<tree, va_gc> *ret;
11657 unsigned int ix;
11658 tree t;
11660 ret = make_tree_vector ();
11661 vec_safe_reserve (ret, vec_safe_length (orig));
11662 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11663 ret->quick_push (t);
11664 return ret;
11667 /* Return true if KEYWORD starts a type specifier. */
11669 bool
11670 keyword_begins_type_specifier (enum rid keyword)
11672 switch (keyword)
11674 case RID_AUTO_TYPE:
11675 case RID_INT:
11676 case RID_CHAR:
11677 case RID_FLOAT:
11678 case RID_DOUBLE:
11679 case RID_VOID:
11680 case RID_INT128:
11681 case RID_UNSIGNED:
11682 case RID_LONG:
11683 case RID_SHORT:
11684 case RID_SIGNED:
11685 case RID_DFLOAT32:
11686 case RID_DFLOAT64:
11687 case RID_DFLOAT128:
11688 case RID_FRACT:
11689 case RID_ACCUM:
11690 case RID_BOOL:
11691 case RID_WCHAR:
11692 case RID_CHAR16:
11693 case RID_CHAR32:
11694 case RID_SAT:
11695 case RID_COMPLEX:
11696 case RID_TYPEOF:
11697 case RID_STRUCT:
11698 case RID_CLASS:
11699 case RID_UNION:
11700 case RID_ENUM:
11701 return true;
11702 default:
11703 return false;
11707 /* Return true if KEYWORD names a type qualifier. */
11709 bool
11710 keyword_is_type_qualifier (enum rid keyword)
11712 switch (keyword)
11714 case RID_CONST:
11715 case RID_VOLATILE:
11716 case RID_RESTRICT:
11717 case RID_ATOMIC:
11718 return true;
11719 default:
11720 return false;
11724 /* Return true if KEYWORD names a storage class specifier.
11726 RID_TYPEDEF is not included in this list despite `typedef' being
11727 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11728 such for syntactic convenience only. */
11730 bool
11731 keyword_is_storage_class_specifier (enum rid keyword)
11733 switch (keyword)
11735 case RID_STATIC:
11736 case RID_EXTERN:
11737 case RID_REGISTER:
11738 case RID_AUTO:
11739 case RID_MUTABLE:
11740 case RID_THREAD:
11741 return true;
11742 default:
11743 return false;
11747 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11749 static bool
11750 keyword_is_function_specifier (enum rid keyword)
11752 switch (keyword)
11754 case RID_INLINE:
11755 case RID_NORETURN:
11756 case RID_VIRTUAL:
11757 case RID_EXPLICIT:
11758 return true;
11759 default:
11760 return false;
11764 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11765 declaration-specifier (C99 6.7). */
11767 bool
11768 keyword_is_decl_specifier (enum rid keyword)
11770 if (keyword_is_storage_class_specifier (keyword)
11771 || keyword_is_type_qualifier (keyword)
11772 || keyword_is_function_specifier (keyword))
11773 return true;
11775 switch (keyword)
11777 case RID_TYPEDEF:
11778 case RID_FRIEND:
11779 case RID_CONSTEXPR:
11780 return true;
11781 default:
11782 return false;
11786 /* Check for and warn about self-assignment or self-initialization.
11787 LHS and RHS are the tree nodes for the left-hand side and right-hand side
11788 of the assignment or initialization we are checking.
11789 LOCATION is the source location for RHS. */
11791 void
11792 check_for_self_assign (location_t location, tree lhs, tree rhs)
11794 if (lhs == NULL_TREE || rhs == NULL_TREE)
11795 return;
11797 /* Deal with TREE_LIST initializers (may be generated by class
11798 member initialization in C++). */
11799 if (TREE_CODE (rhs) == TREE_LIST)
11800 rhs = TREE_VALUE (rhs);
11802 /* Only emit a warning if RHS is not a folded expression so that we don't
11803 warn on something like x = x / 1. */
11804 if (!EXPR_FOLDED (rhs)
11805 && operand_equal_p (lhs, rhs,
11806 OEP_PURE_SAME | OEP_ALLOW_NULL | OEP_ALLOW_NO_TYPE))
11807 warning_at (location, OPT_Wself_assign, G_("%qE is assigned to itself"),
11808 lhs);
11811 /* Initialize language-specific-bits of tree_contains_struct. */
11813 void
11814 c_common_init_ts (void)
11816 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11817 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11818 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11821 /* Build a user-defined numeric literal out of an integer constant type VALUE
11822 with identifier SUFFIX. */
11824 tree
11825 build_userdef_literal (tree suffix_id, tree value,
11826 enum overflow_type overflow, tree num_string)
11828 tree literal = make_node (USERDEF_LITERAL);
11829 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11830 USERDEF_LITERAL_VALUE (literal) = value;
11831 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11832 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11833 return literal;
11836 /* For vector[index], convert the vector to a
11837 pointer of the underlying type. */
11838 void
11839 convert_vector_to_pointer_for_subscript (location_t loc,
11840 tree* vecp, tree index)
11842 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11844 tree type = TREE_TYPE (*vecp);
11845 tree type1;
11847 if (TREE_CODE (index) == INTEGER_CST)
11848 if (!tree_fits_uhwi_p (index)
11849 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
11850 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11852 c_common_mark_addressable_vec (*vecp);
11853 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11854 type1 = build_pointer_type (TREE_TYPE (*vecp));
11855 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
11856 if (!ref_all
11857 && !DECL_P (*vecp))
11859 /* If the original vector isn't declared may_alias and it
11860 isn't a bare vector look if the subscripting would
11861 alias the vector we subscript, and if not, force ref-all. */
11862 alias_set_type vecset = get_alias_set (*vecp);
11863 alias_set_type sset = get_alias_set (type);
11864 if (!alias_sets_must_conflict_p (sset, vecset)
11865 && !alias_set_subset_of (sset, vecset))
11866 ref_all = true;
11868 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
11869 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11870 *vecp = convert (type, *vecp);
11874 /* Determine which of the operands, if any, is a scalar that needs to be
11875 converted to a vector, for the range of operations. */
11876 enum stv_conv
11877 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11878 bool complain)
11880 tree type0 = TREE_TYPE (op0);
11881 tree type1 = TREE_TYPE (op1);
11882 bool integer_only_op = false;
11883 enum stv_conv ret = stv_firstarg;
11885 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11886 || TREE_CODE (type1) == VECTOR_TYPE);
11887 switch (code)
11889 /* Most GENERIC binary expressions require homogeneous arguments.
11890 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11891 argument that is a vector and a second one that is a scalar, so
11892 we never return stv_secondarg for them. */
11893 case RSHIFT_EXPR:
11894 case LSHIFT_EXPR:
11895 if (TREE_CODE (type0) == INTEGER_TYPE
11896 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11898 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11900 if (complain)
11901 error_at (loc, "conversion of scalar %qT to vector %qT "
11902 "involves truncation", type0, type1);
11903 return stv_error;
11905 else
11906 return stv_firstarg;
11908 break;
11910 case BIT_IOR_EXPR:
11911 case BIT_XOR_EXPR:
11912 case BIT_AND_EXPR:
11913 integer_only_op = true;
11914 /* ... fall through ... */
11916 case VEC_COND_EXPR:
11918 case PLUS_EXPR:
11919 case MINUS_EXPR:
11920 case MULT_EXPR:
11921 case TRUNC_DIV_EXPR:
11922 case CEIL_DIV_EXPR:
11923 case FLOOR_DIV_EXPR:
11924 case ROUND_DIV_EXPR:
11925 case EXACT_DIV_EXPR:
11926 case TRUNC_MOD_EXPR:
11927 case FLOOR_MOD_EXPR:
11928 case RDIV_EXPR:
11929 case EQ_EXPR:
11930 case NE_EXPR:
11931 case LE_EXPR:
11932 case GE_EXPR:
11933 case LT_EXPR:
11934 case GT_EXPR:
11935 /* What about UNLT_EXPR? */
11936 if (TREE_CODE (type0) == VECTOR_TYPE)
11938 tree tmp;
11939 ret = stv_secondarg;
11940 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11941 tmp = type0; type0 = type1; type1 = tmp;
11942 tmp = op0; op0 = op1; op1 = tmp;
11945 if (TREE_CODE (type0) == INTEGER_TYPE
11946 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11948 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11950 if (complain)
11951 error_at (loc, "conversion of scalar %qT to vector %qT "
11952 "involves truncation", type0, type1);
11953 return stv_error;
11955 return ret;
11957 else if (!integer_only_op
11958 /* Allow integer --> real conversion if safe. */
11959 && (TREE_CODE (type0) == REAL_TYPE
11960 || TREE_CODE (type0) == INTEGER_TYPE)
11961 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11963 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11965 if (complain)
11966 error_at (loc, "conversion of scalar %qT to vector %qT "
11967 "involves truncation", type0, type1);
11968 return stv_error;
11970 return ret;
11972 default:
11973 break;
11976 return stv_nothing;
11979 /* Return true iff ALIGN is an integral constant that is a fundamental
11980 alignment, as defined by [basic.align] in the c++-11
11981 specifications.
11983 That is:
11985 [A fundamental alignment is represented by an alignment less than or
11986 equal to the greatest alignment supported by the implementation
11987 in all contexts, which is equal to
11988 alignof(max_align_t)]. */
11990 bool
11991 cxx_fundamental_alignment_p (unsigned align)
11993 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11994 TYPE_ALIGN (long_double_type_node)));
11997 /* Return true if T is a pointer to a zero-sized aggregate. */
11999 bool
12000 pointer_to_zero_sized_aggr_p (tree t)
12002 if (!POINTER_TYPE_P (t))
12003 return false;
12004 t = TREE_TYPE (t);
12005 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12008 #include "gt-c-family-c-common.h"