Merge trunk version 215014 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob2dd8308b5fa10254a915da24fc2f59103c317b9a
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 "tree-upc.h"
27 #include "fold-const.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "varasm.h"
33 #include "trans-mem.h"
34 #include "flags.h"
35 #include "c-pragma.h"
36 #include "c-common.h"
37 #include "c-objc.h"
38 #include "tm_p.h"
39 #include "obstack.h"
40 #include "cpplib.h"
41 #include "target.h"
42 #include "common/common-target.h"
43 #include "langhooks.h"
44 #include "tree-inline.h"
45 #include "toplev.h"
46 #include "diagnostic.h"
47 #include "tree-iterator.h"
48 #include "hashtab.h"
49 #include "opts.h"
50 #include "cgraph.h"
51 #include "target-def.h"
52 #include "gimplify.h"
53 #include "wide-int-print.h"
55 cpp_reader *parse_in; /* Declared in c-pragma.h. */
57 /* The following symbols are subsumed in the c_global_trees array, and
58 listed here individually for documentation purposes.
60 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
62 tree short_integer_type_node;
63 tree long_integer_type_node;
64 tree long_long_integer_type_node;
65 tree int128_integer_type_node;
67 tree short_unsigned_type_node;
68 tree long_unsigned_type_node;
69 tree long_long_unsigned_type_node;
70 tree int128_unsigned_type_node;
72 tree truthvalue_type_node;
73 tree truthvalue_false_node;
74 tree truthvalue_true_node;
76 tree ptrdiff_type_node;
78 tree unsigned_char_type_node;
79 tree signed_char_type_node;
80 tree wchar_type_node;
82 tree char16_type_node;
83 tree char32_type_node;
85 tree float_type_node;
86 tree double_type_node;
87 tree long_double_type_node;
89 tree complex_integer_type_node;
90 tree complex_float_type_node;
91 tree complex_double_type_node;
92 tree complex_long_double_type_node;
94 tree dfloat32_type_node;
95 tree dfloat64_type_node;
96 tree_dfloat128_type_node;
98 tree intQI_type_node;
99 tree intHI_type_node;
100 tree intSI_type_node;
101 tree intDI_type_node;
102 tree intTI_type_node;
104 tree unsigned_intQI_type_node;
105 tree unsigned_intHI_type_node;
106 tree unsigned_intSI_type_node;
107 tree unsigned_intDI_type_node;
108 tree unsigned_intTI_type_node;
110 tree widest_integer_literal_type_node;
111 tree widest_unsigned_literal_type_node;
113 Nodes for types `void *' and `const void *'.
115 tree ptr_type_node, const_ptr_type_node;
117 Nodes for types `char *' and `const char *'.
119 tree string_type_node, const_string_type_node;
121 Type `char[SOMENUMBER]'.
122 Used when an array of char is needed and the size is irrelevant.
124 tree char_array_type_node;
126 Type `wchar_t[SOMENUMBER]' or something like it.
127 Used when a wide string literal is created.
129 tree wchar_array_type_node;
131 Type `char16_t[SOMENUMBER]' or something like it.
132 Used when a UTF-16 string literal is created.
134 tree char16_array_type_node;
136 Type `char32_t[SOMENUMBER]' or something like it.
137 Used when a UTF-32 string literal is created.
139 tree char32_array_type_node;
141 Type `int ()' -- used for implicit declaration of functions.
143 tree default_function_type;
145 A VOID_TYPE node, packaged in a TREE_LIST.
147 tree void_list_node;
149 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
150 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
151 VAR_DECLS, but C++ does.)
153 tree function_name_decl_node;
154 tree pretty_function_name_decl_node;
155 tree c99_function_name_decl_node;
157 Stack of nested function name VAR_DECLs.
159 tree saved_function_name_decls;
163 tree c_global_trees[CTI_MAX];
165 /* Switches common to the C front ends. */
167 /* Nonzero means don't output line number information. */
169 char flag_no_line_commands;
171 /* Nonzero causes -E output not to be done, but directives such as
172 #define that have side effects are still obeyed. */
174 char flag_no_output;
176 /* Nonzero means dump macros in some fashion. */
178 char flag_dump_macros;
180 /* Nonzero means pass #include lines through to the output. */
182 char flag_dump_includes;
184 /* Nonzero means process PCH files while preprocessing. */
186 bool flag_pch_preprocess;
188 /* The file name to which we should write a precompiled header, or
189 NULL if no header will be written in this compile. */
191 const char *pch_file;
193 /* Nonzero if an ISO standard was selected. It rejects macros in the
194 user's namespace. */
195 int flag_iso;
198 /* C/ObjC language option variables. */
201 /* Nonzero means allow type mismatches in conditional expressions;
202 just make their values `void'. */
204 int flag_cond_mismatch;
206 /* Nonzero means enable C89 Amendment 1 features. */
208 int flag_isoc94;
210 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
212 int flag_isoc99;
214 /* Nonzero means use the ISO C11 dialect of C. */
216 int flag_isoc11;
218 /* Nonzero means that we have builtin functions, and main is an int. */
220 int flag_hosted = 1;
223 /* ObjC language option variables. */
226 /* Tells the compiler that this is a special run. Do not perform any
227 compiling, instead we are to test some platform dependent features
228 and output a C header file with appropriate definitions. */
230 int print_struct_values;
232 /* Tells the compiler what is the constant string class for ObjC. */
234 const char *constant_string_class_name;
236 /* UPC language option variables. */
238 /* Nonzero whenever UPC -fupc-threads-N is asserted.
239 The value N gives the number of UPC threads to be
240 defined at compile-time. */
241 int flag_upc_threads;
243 /* Nonzero whenever UPC -fupc-pthreads-model-* is asserted. */
244 int flag_upc_pthreads;
246 /* The implementation model for UPC threads that
247 are mapped to POSIX threads, specified at compilation
248 time by the -fupc-pthreads-model-* switch. */
249 upc_pthreads_model_kind upc_pthreads_model;
251 /* C++ language option variables. */
254 /* Nonzero means generate separate instantiation control files and
255 juggle them at link time. */
257 int flag_use_repository;
259 /* The C++ dialect being used. C++98 is the default. */
261 enum cxx_dialect cxx_dialect = cxx98;
263 /* Maximum template instantiation depth. This limit exists to limit the
264 time it takes to notice excessively recursive template instantiations.
266 The default is lower than the 1024 recommended by the C++0x standard
267 because G++ runs out of stack before 1024 with highly recursive template
268 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
270 int max_tinst_depth = 900;
272 /* The elements of `ridpointers' are identifier nodes for the reserved
273 type names and storage classes. It is indexed by a RID_... value. */
274 tree *ridpointers;
276 tree (*make_fname_decl) (location_t, tree, int);
278 /* Nonzero means don't warn about problems that occur when the code is
279 executed. */
280 int c_inhibit_evaluation_warnings;
282 /* Whether we are building a boolean conversion inside
283 convert_for_assignment, or some other late binary operation. If
284 build_binary_op is called for C (from code shared by C and C++) in
285 this case, then the operands have already been folded and the
286 result will not be folded again, so C_MAYBE_CONST_EXPR should not
287 be generated. */
288 bool in_late_binary_op;
290 /* Whether lexing has been completed, so subsequent preprocessor
291 errors should use the compiler's input_location. */
292 bool done_lexing = false;
294 /* Information about how a function name is generated. */
295 struct fname_var_t
297 tree *const decl; /* pointer to the VAR_DECL. */
298 const unsigned rid; /* RID number for the identifier. */
299 const int pretty; /* How pretty is it? */
302 /* The three ways of getting then name of the current function. */
304 const struct fname_var_t fname_vars[] =
306 /* C99 compliant __func__, must be first. */
307 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
308 /* GCC __FUNCTION__ compliant. */
309 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
310 /* GCC __PRETTY_FUNCTION__ compliant. */
311 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
312 {NULL, 0, 0},
315 /* Global visibility options. */
316 struct visibility_flags visibility_options;
318 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
319 static tree check_case_value (location_t, tree);
320 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
322 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
329 int, bool *);
330 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
331 int, bool *);
332 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
333 bool *);
334 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_always_inline_attribute (tree *, tree, tree, int,
338 bool *);
339 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
346 bool *);
347 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_transparent_union_attribute (tree *, tree, tree,
349 int, bool *);
350 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
356 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
357 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
360 static tree handle_visibility_attribute (tree *, tree, tree, int,
361 bool *);
362 static tree handle_tls_model_attribute (tree *, tree, tree, int,
363 bool *);
364 static tree handle_no_instrument_function_attribute (tree *, tree,
365 tree, int, bool *);
366 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_deprecated_attribute (tree *, tree, tree, int,
375 bool *);
376 static tree handle_vector_size_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
382 bool *);
383 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
390 static tree ignore_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
394 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
396 bool *);
397 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
398 bool *);
399 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
401 static void check_function_nonnull (tree, int, tree *);
402 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
403 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
404 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
405 static int resort_field_decl_cmp (const void *, const void *);
407 /* Reserved words. The third field is a mask: keywords are disabled
408 if they match the mask.
410 Masks for languages:
411 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
412 C --std=c99: D_CXXONLY | D_OBJC
413 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
414 UPC is like C except that D_UPC is not set
415 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | D_UPC
416 C++ --std=c0x: D_CONLY | D_OBJC | D_UPC
417 ObjC++ is like C++ except that D_OBJC is not set
419 If -fno-asm is used, D_ASM is added to the mask. If
420 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
421 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
422 In C with -Wc++-compat, we warn if D_CXXWARN is set.
424 Note the complication of the D_CXX_OBJC keywords. These are
425 reserved words such as 'class'. In C++, 'class' is a reserved
426 word. In Objective-C++ it is too. In Objective-C, it is a
427 reserved word too, but only if it follows an '@' sign.
429 const struct c_common_resword c_common_reswords[] =
431 { "_Alignas", RID_ALIGNAS, D_CONLY },
432 { "_Alignof", RID_ALIGNOF, D_CONLY },
433 { "_Atomic", RID_ATOMIC, D_CONLY },
434 { "_Bool", RID_BOOL, D_CONLY },
435 { "_Complex", RID_COMPLEX, 0 },
436 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
437 { "_Cilk_sync", RID_CILK_SYNC, 0 },
438 { "_Cilk_for", RID_CILK_FOR, 0 },
439 { "_Imaginary", RID_IMAGINARY, D_CONLY },
440 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
441 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
442 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
443 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
444 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
445 { "_Sat", RID_SAT, D_CONLY | D_EXT },
446 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
447 { "_Noreturn", RID_NORETURN, D_CONLY },
448 { "_Generic", RID_GENERIC, D_CONLY },
449 { "_Thread_local", RID_THREAD, D_CONLY },
450 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
451 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
452 { "__alignof", RID_ALIGNOF, 0 },
453 { "__alignof__", RID_ALIGNOF, 0 },
454 { "__asm", RID_ASM, 0 },
455 { "__asm__", RID_ASM, 0 },
456 { "__attribute", RID_ATTRIBUTE, 0 },
457 { "__attribute__", RID_ATTRIBUTE, 0 },
458 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
459 { "__bases", RID_BASES, D_CXXONLY },
460 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
461 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
462 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
463 { "__builtin_offsetof", RID_OFFSETOF, 0 },
464 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
465 { "__builtin_va_arg", RID_VA_ARG, 0 },
466 { "__complex", RID_COMPLEX, 0 },
467 { "__complex__", RID_COMPLEX, 0 },
468 { "__const", RID_CONST, 0 },
469 { "__const__", RID_CONST, 0 },
470 { "__decltype", RID_DECLTYPE, D_CXXONLY },
471 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
472 { "__extension__", RID_EXTENSION, 0 },
473 { "__func__", RID_C99_FUNCTION_NAME, 0 },
474 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
475 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
476 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
477 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
478 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
479 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
480 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
481 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
482 { "__imag", RID_IMAGPART, 0 },
483 { "__imag__", RID_IMAGPART, 0 },
484 { "__inline", RID_INLINE, 0 },
485 { "__inline__", RID_INLINE, 0 },
486 { "__int128", RID_INT128, 0 },
487 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
488 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
489 { "__is_class", RID_IS_CLASS, D_CXXONLY },
490 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
491 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
492 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
493 { "__is_final", RID_IS_FINAL, D_CXXONLY },
494 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
495 { "__is_pod", RID_IS_POD, D_CXXONLY },
496 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
497 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
498 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
499 { "__is_union", RID_IS_UNION, D_CXXONLY },
500 { "__label__", RID_LABEL, 0 },
501 { "__null", RID_NULL, 0 },
502 { "__real", RID_REALPART, 0 },
503 { "__real__", RID_REALPART, 0 },
504 { "__restrict", RID_RESTRICT, 0 },
505 { "__restrict__", RID_RESTRICT, 0 },
506 { "__signed", RID_SIGNED, 0 },
507 { "__signed__", RID_SIGNED, 0 },
508 { "__thread", RID_THREAD, 0 },
509 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
510 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
511 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
512 { "__typeof", RID_TYPEOF, 0 },
513 { "__typeof__", RID_TYPEOF, 0 },
514 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
515 { "__volatile", RID_VOLATILE, 0 },
516 { "__volatile__", RID_VOLATILE, 0 },
517 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
518 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
519 { "asm", RID_ASM, D_ASM },
520 { "auto", RID_AUTO, 0 },
521 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
522 { "break", RID_BREAK, 0 },
523 { "case", RID_CASE, 0 },
524 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
525 { "char", RID_CHAR, 0 },
526 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
527 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
529 { "const", RID_CONST, 0 },
530 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
531 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
532 { "continue", RID_CONTINUE, 0 },
533 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
534 { "default", RID_DEFAULT, 0 },
535 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
536 { "do", RID_DO, 0 },
537 { "double", RID_DOUBLE, 0 },
538 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
539 { "else", RID_ELSE, 0 },
540 { "enum", RID_ENUM, 0 },
541 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
542 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
543 { "extern", RID_EXTERN, 0 },
544 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
545 { "float", RID_FLOAT, 0 },
546 { "for", RID_FOR, 0 },
547 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
548 { "goto", RID_GOTO, 0 },
549 { "if", RID_IF, 0 },
550 { "inline", RID_INLINE, D_EXT89 },
551 { "int", RID_INT, 0 },
552 { "long", RID_LONG, 0 },
553 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
554 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
555 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
556 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
557 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
558 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
559 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
560 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
561 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
562 { "register", RID_REGISTER, 0 },
563 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
564 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
565 { "return", RID_RETURN, 0 },
566 { "short", RID_SHORT, 0 },
567 { "signed", RID_SIGNED, 0 },
568 { "sizeof", RID_SIZEOF, 0 },
569 { "static", RID_STATIC, 0 },
570 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
571 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
572 { "struct", RID_STRUCT, 0 },
573 { "switch", RID_SWITCH, 0 },
574 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
575 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
576 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
577 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
578 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
579 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
580 { "typedef", RID_TYPEDEF, 0 },
581 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
582 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
583 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
584 { "union", RID_UNION, 0 },
585 { "unsigned", RID_UNSIGNED, 0 },
586 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
587 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
588 { "void", RID_VOID, 0 },
589 { "volatile", RID_VOLATILE, 0 },
590 { "wchar_t", RID_WCHAR, D_CXXONLY },
591 { "while", RID_WHILE, 0 },
592 /* These Objective-C keywords are recognized only immediately after
593 an '@'. */
594 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
595 { "defs", RID_AT_DEFS, D_OBJC },
596 { "encode", RID_AT_ENCODE, D_OBJC },
597 { "end", RID_AT_END, D_OBJC },
598 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
599 { "interface", RID_AT_INTERFACE, D_OBJC },
600 { "protocol", RID_AT_PROTOCOL, D_OBJC },
601 { "selector", RID_AT_SELECTOR, D_OBJC },
602 { "finally", RID_AT_FINALLY, D_OBJC },
603 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
604 { "optional", RID_AT_OPTIONAL, D_OBJC },
605 { "required", RID_AT_REQUIRED, D_OBJC },
606 { "property", RID_AT_PROPERTY, D_OBJC },
607 { "package", RID_AT_PACKAGE, D_OBJC },
608 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
609 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
610 /* These are recognized only in protocol-qualifier context
611 (see above) */
612 { "bycopy", RID_BYCOPY, D_OBJC },
613 { "byref", RID_BYREF, D_OBJC },
614 { "in", RID_IN, D_OBJC },
615 { "inout", RID_INOUT, D_OBJC },
616 { "oneway", RID_ONEWAY, D_OBJC },
617 { "out", RID_OUT, D_OBJC },
619 /* UPC keywords */
620 { "shared", RID_SHARED, D_UPC },
621 { "relaxed", RID_RELAXED, D_UPC },
622 { "strict", RID_STRICT, D_UPC },
623 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
624 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
625 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
626 { "upc_forall", RID_UPC_FORALL, D_UPC },
627 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
628 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
629 { "upc_wait", RID_UPC_WAIT, D_UPC },
631 /* These are recognized inside a property attribute list */
632 { "assign", RID_ASSIGN, D_OBJC },
633 { "copy", RID_COPY, D_OBJC },
634 { "getter", RID_GETTER, D_OBJC },
635 { "nonatomic", RID_NONATOMIC, D_OBJC },
636 { "readonly", RID_READONLY, D_OBJC },
637 { "readwrite", RID_READWRITE, D_OBJC },
638 { "retain", RID_RETAIN, D_OBJC },
639 { "setter", RID_SETTER, D_OBJC },
642 const unsigned int num_c_common_reswords =
643 sizeof c_common_reswords / sizeof (struct c_common_resword);
645 /* Table of machine-independent attributes common to all C-like languages. */
646 const struct attribute_spec c_common_attribute_table[] =
648 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
649 affects_type_identity } */
650 { "packed", 0, 0, false, false, false,
651 handle_packed_attribute , false},
652 { "nocommon", 0, 0, true, false, false,
653 handle_nocommon_attribute, false},
654 { "common", 0, 0, true, false, false,
655 handle_common_attribute, false },
656 /* FIXME: logically, noreturn attributes should be listed as
657 "false, true, true" and apply to function types. But implementing this
658 would require all the places in the compiler that use TREE_THIS_VOLATILE
659 on a decl to identify non-returning functions to be located and fixed
660 to check the function type instead. */
661 { "noreturn", 0, 0, true, false, false,
662 handle_noreturn_attribute, false },
663 { "volatile", 0, 0, true, false, false,
664 handle_noreturn_attribute, false },
665 { "noinline", 0, 0, true, false, false,
666 handle_noinline_attribute, false },
667 { "noclone", 0, 0, true, false, false,
668 handle_noclone_attribute, false },
669 { "leaf", 0, 0, true, false, false,
670 handle_leaf_attribute, false },
671 { "always_inline", 0, 0, true, false, false,
672 handle_always_inline_attribute, false },
673 { "gnu_inline", 0, 0, true, false, false,
674 handle_gnu_inline_attribute, false },
675 { "artificial", 0, 0, true, false, false,
676 handle_artificial_attribute, false },
677 { "flatten", 0, 0, true, false, false,
678 handle_flatten_attribute, false },
679 { "used", 0, 0, true, false, false,
680 handle_used_attribute, false },
681 { "unused", 0, 0, false, false, false,
682 handle_unused_attribute, false },
683 { "externally_visible", 0, 0, true, false, false,
684 handle_externally_visible_attribute, false },
685 /* The same comments as for noreturn attributes apply to const ones. */
686 { "const", 0, 0, true, false, false,
687 handle_const_attribute, false },
688 { "transparent_union", 0, 0, false, false, false,
689 handle_transparent_union_attribute, false },
690 { "constructor", 0, 1, true, false, false,
691 handle_constructor_attribute, false },
692 { "destructor", 0, 1, true, false, false,
693 handle_destructor_attribute, false },
694 { "mode", 1, 1, false, true, false,
695 handle_mode_attribute, false },
696 { "section", 1, 1, true, false, false,
697 handle_section_attribute, false },
698 { "aligned", 0, 1, false, false, false,
699 handle_aligned_attribute, false },
700 { "weak", 0, 0, true, false, false,
701 handle_weak_attribute, false },
702 { "ifunc", 1, 1, true, false, false,
703 handle_ifunc_attribute, false },
704 { "alias", 1, 1, true, false, false,
705 handle_alias_attribute, false },
706 { "weakref", 0, 1, true, false, false,
707 handle_weakref_attribute, false },
708 { "no_instrument_function", 0, 0, true, false, false,
709 handle_no_instrument_function_attribute,
710 false },
711 { "malloc", 0, 0, true, false, false,
712 handle_malloc_attribute, false },
713 { "returns_twice", 0, 0, true, false, false,
714 handle_returns_twice_attribute, false },
715 { "no_stack_limit", 0, 0, true, false, false,
716 handle_no_limit_stack_attribute, false },
717 { "pure", 0, 0, true, false, false,
718 handle_pure_attribute, false },
719 { "transaction_callable", 0, 0, false, true, false,
720 handle_tm_attribute, false },
721 { "transaction_unsafe", 0, 0, false, true, false,
722 handle_tm_attribute, false },
723 { "transaction_safe", 0, 0, false, true, false,
724 handle_tm_attribute, false },
725 { "transaction_may_cancel_outer", 0, 0, false, true, false,
726 handle_tm_attribute, false },
727 /* ??? These two attributes didn't make the transition from the
728 Intel language document to the multi-vendor language document. */
729 { "transaction_pure", 0, 0, false, true, false,
730 handle_tm_attribute, false },
731 { "transaction_wrap", 1, 1, true, false, false,
732 handle_tm_wrap_attribute, false },
733 /* For internal use (marking of builtins) only. The name contains space
734 to prevent its usage in source code. */
735 { "no vops", 0, 0, true, false, false,
736 handle_novops_attribute, false },
737 { "deprecated", 0, 1, false, false, false,
738 handle_deprecated_attribute, false },
739 { "vector_size", 1, 1, false, true, false,
740 handle_vector_size_attribute, false },
741 { "visibility", 1, 1, false, false, false,
742 handle_visibility_attribute, false },
743 { "tls_model", 1, 1, true, false, false,
744 handle_tls_model_attribute, false },
745 { "nonnull", 0, -1, false, true, true,
746 handle_nonnull_attribute, false },
747 { "nothrow", 0, 0, true, false, false,
748 handle_nothrow_attribute, false },
749 { "may_alias", 0, 0, false, true, false, NULL, false },
750 { "cleanup", 1, 1, true, false, false,
751 handle_cleanup_attribute, false },
752 { "warn_unused_result", 0, 0, false, true, true,
753 handle_warn_unused_result_attribute, false },
754 { "sentinel", 0, 1, false, true, true,
755 handle_sentinel_attribute, false },
756 /* For internal use (marking of builtins) only. The name contains space
757 to prevent its usage in source code. */
758 { "type generic", 0, 0, false, true, true,
759 handle_type_generic_attribute, false },
760 { "alloc_size", 1, 2, false, true, true,
761 handle_alloc_size_attribute, false },
762 { "cold", 0, 0, true, false, false,
763 handle_cold_attribute, false },
764 { "hot", 0, 0, true, false, false,
765 handle_hot_attribute, false },
766 { "no_address_safety_analysis",
767 0, 0, true, false, false,
768 handle_no_address_safety_analysis_attribute,
769 false },
770 { "no_sanitize_address", 0, 0, true, false, false,
771 handle_no_sanitize_address_attribute,
772 false },
773 { "no_sanitize_undefined", 0, 0, true, false, false,
774 handle_no_sanitize_undefined_attribute,
775 false },
776 { "warning", 1, 1, true, false, false,
777 handle_error_attribute, false },
778 { "error", 1, 1, true, false, false,
779 handle_error_attribute, false },
780 { "target", 1, -1, true, false, false,
781 handle_target_attribute, false },
782 { "optimize", 1, -1, true, false, false,
783 handle_optimize_attribute, false },
784 /* For internal use only. The leading '*' both prevents its usage in
785 source code and signals that it may be overridden by machine tables. */
786 { "*tm regparm", 0, 0, false, true, true,
787 ignore_attribute, false },
788 { "no_split_stack", 0, 0, true, false, false,
789 handle_no_split_stack_attribute, false },
790 /* For internal use (marking of builtins and runtime functions) only.
791 The name contains space to prevent its usage in source code. */
792 { "fn spec", 1, 1, false, true, true,
793 handle_fnspec_attribute, false },
794 { "warn_unused", 0, 0, false, false, false,
795 handle_warn_unused_attribute, false },
796 { "returns_nonnull", 0, 0, false, true, true,
797 handle_returns_nonnull_attribute, false },
798 { "omp declare simd", 0, -1, true, false, false,
799 handle_omp_declare_simd_attribute, false },
800 { "cilk simd function", 0, -1, true, false, false,
801 handle_omp_declare_simd_attribute, false },
802 { "omp declare target", 0, 0, true, false, false,
803 handle_omp_declare_target_attribute, false },
804 { "alloc_align", 1, 1, false, true, true,
805 handle_alloc_align_attribute, false },
806 { "assume_aligned", 1, 2, false, true, true,
807 handle_assume_aligned_attribute, false },
808 { "designated_init", 0, 0, false, true, false,
809 handle_designated_init_attribute, false },
810 { NULL, 0, 0, false, false, false, NULL, false }
813 /* Give the specifications for the format attributes, used by C and all
814 descendants. */
816 const struct attribute_spec c_common_format_attribute_table[] =
818 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
819 affects_type_identity } */
820 { "format", 3, 3, false, true, true,
821 handle_format_attribute, false },
822 { "format_arg", 1, 1, false, true, true,
823 handle_format_arg_attribute, false },
824 { NULL, 0, 0, false, false, false, NULL, false }
827 /* Return identifier for address space AS. */
829 const char *
830 c_addr_space_name (addr_space_t as)
832 int rid = RID_FIRST_ADDR_SPACE + as;
833 gcc_assert (ridpointers [rid]);
834 return IDENTIFIER_POINTER (ridpointers [rid]);
837 /* Push current bindings for the function name VAR_DECLS. */
839 void
840 start_fname_decls (void)
842 unsigned ix;
843 tree saved = NULL_TREE;
845 for (ix = 0; fname_vars[ix].decl; ix++)
847 tree decl = *fname_vars[ix].decl;
849 if (decl)
851 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
852 saved);
853 *fname_vars[ix].decl = NULL_TREE;
856 if (saved || saved_function_name_decls)
857 /* Normally they'll have been NULL, so only push if we've got a
858 stack, or they are non-NULL. */
859 saved_function_name_decls = tree_cons (saved, NULL_TREE,
860 saved_function_name_decls);
863 /* Finish up the current bindings, adding them into the current function's
864 statement tree. This must be done _before_ finish_stmt_tree is called.
865 If there is no current function, we must be at file scope and no statements
866 are involved. Pop the previous bindings. */
868 void
869 finish_fname_decls (void)
871 unsigned ix;
872 tree stmts = NULL_TREE;
873 tree stack = saved_function_name_decls;
875 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
876 append_to_statement_list (TREE_VALUE (stack), &stmts);
878 if (stmts)
880 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
882 if (TREE_CODE (*bodyp) == BIND_EXPR)
883 bodyp = &BIND_EXPR_BODY (*bodyp);
885 append_to_statement_list_force (*bodyp, &stmts);
886 *bodyp = stmts;
889 for (ix = 0; fname_vars[ix].decl; ix++)
890 *fname_vars[ix].decl = NULL_TREE;
892 if (stack)
894 /* We had saved values, restore them. */
895 tree saved;
897 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
899 tree decl = TREE_PURPOSE (saved);
900 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
902 *fname_vars[ix].decl = decl;
904 stack = TREE_CHAIN (stack);
906 saved_function_name_decls = stack;
909 /* Return the text name of the current function, suitably prettified
910 by PRETTY_P. Return string must be freed by caller. */
912 const char *
913 fname_as_string (int pretty_p)
915 const char *name = "top level";
916 char *namep;
917 int vrb = 2, len;
918 cpp_string cstr = { 0, 0 }, strname;
920 if (!pretty_p)
922 name = "";
923 vrb = 0;
926 if (current_function_decl)
927 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
929 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
931 namep = XNEWVEC (char, len);
932 snprintf (namep, len, "\"%s\"", name);
933 strname.text = (unsigned char *) namep;
934 strname.len = len - 1;
936 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
938 XDELETEVEC (namep);
939 return (const char *) cstr.text;
942 return namep;
945 /* Return the VAR_DECL for a const char array naming the current
946 function. If the VAR_DECL has not yet been created, create it
947 now. RID indicates how it should be formatted and IDENTIFIER_NODE
948 ID is its name (unfortunately C and C++ hold the RID values of
949 keywords in different places, so we can't derive RID from ID in
950 this language independent code. LOC is the location of the
951 function. */
953 tree
954 fname_decl (location_t loc, unsigned int rid, tree id)
956 unsigned ix;
957 tree decl = NULL_TREE;
959 for (ix = 0; fname_vars[ix].decl; ix++)
960 if (fname_vars[ix].rid == rid)
961 break;
963 decl = *fname_vars[ix].decl;
964 if (!decl)
966 /* If a tree is built here, it would normally have the lineno of
967 the current statement. Later this tree will be moved to the
968 beginning of the function and this line number will be wrong.
969 To avoid this problem set the lineno to 0 here; that prevents
970 it from appearing in the RTL. */
971 tree stmts;
972 location_t saved_location = input_location;
973 input_location = UNKNOWN_LOCATION;
975 stmts = push_stmt_list ();
976 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
977 stmts = pop_stmt_list (stmts);
978 if (!IS_EMPTY_STMT (stmts))
979 saved_function_name_decls
980 = tree_cons (decl, stmts, saved_function_name_decls);
981 *fname_vars[ix].decl = decl;
982 input_location = saved_location;
984 if (!ix && !current_function_decl)
985 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
987 return decl;
990 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
992 tree
993 fix_string_type (tree value)
995 int length = TREE_STRING_LENGTH (value);
996 int nchars;
997 tree e_type, i_type, a_type;
999 /* Compute the number of elements, for the array type. */
1000 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1002 nchars = length;
1003 e_type = char_type_node;
1005 else if (TREE_TYPE (value) == char16_array_type_node)
1007 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1008 e_type = char16_type_node;
1010 else if (TREE_TYPE (value) == char32_array_type_node)
1012 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1013 e_type = char32_type_node;
1015 else
1017 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1018 e_type = wchar_type_node;
1021 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1022 limit in C++98 Annex B is very large (65536) and is not normative,
1023 so we do not diagnose it (warn_overlength_strings is forced off
1024 in c_common_post_options). */
1025 if (warn_overlength_strings)
1027 const int nchars_max = flag_isoc99 ? 4095 : 509;
1028 const int relevant_std = flag_isoc99 ? 99 : 90;
1029 if (nchars - 1 > nchars_max)
1030 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1031 separate the %d from the 'C'. 'ISO' should not be
1032 translated, but it may be moved after 'C%d' in languages
1033 where modifiers follow nouns. */
1034 pedwarn (input_location, OPT_Woverlength_strings,
1035 "string length %qd is greater than the length %qd "
1036 "ISO C%d compilers are required to support",
1037 nchars - 1, nchars_max, relevant_std);
1040 /* Create the array type for the string constant. The ISO C++
1041 standard says that a string literal has type `const char[N]' or
1042 `const wchar_t[N]'. We use the same logic when invoked as a C
1043 front-end with -Wwrite-strings.
1044 ??? We should change the type of an expression depending on the
1045 state of a warning flag. We should just be warning -- see how
1046 this is handled in the C++ front-end for the deprecated implicit
1047 conversion from string literals to `char*' or `wchar_t*'.
1049 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1050 array type being the unqualified version of that type.
1051 Therefore, if we are constructing an array of const char, we must
1052 construct the matching unqualified array type first. The C front
1053 end does not require this, but it does no harm, so we do it
1054 unconditionally. */
1055 i_type = build_index_type (size_int (nchars - 1));
1056 a_type = build_array_type (e_type, i_type);
1057 if (c_dialect_cxx() || warn_write_strings)
1058 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1060 TREE_TYPE (value) = a_type;
1061 TREE_CONSTANT (value) = 1;
1062 TREE_READONLY (value) = 1;
1063 TREE_STATIC (value) = 1;
1064 return value;
1067 /* If DISABLE is true, stop issuing warnings. This is used when
1068 parsing code that we know will not be executed. This function may
1069 be called multiple times, and works as a stack. */
1071 static void
1072 c_disable_warnings (bool disable)
1074 if (disable)
1076 ++c_inhibit_evaluation_warnings;
1077 fold_defer_overflow_warnings ();
1081 /* If ENABLE is true, reenable issuing warnings. */
1083 static void
1084 c_enable_warnings (bool enable)
1086 if (enable)
1088 --c_inhibit_evaluation_warnings;
1089 fold_undefer_and_ignore_overflow_warnings ();
1093 /* Fully fold EXPR, an expression that was not folded (beyond integer
1094 constant expressions and null pointer constants) when being built
1095 up. If IN_INIT, this is in a static initializer and certain
1096 changes are made to the folding done. Clear *MAYBE_CONST if
1097 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1098 expression because it contains an evaluated operator (in C99) or an
1099 operator outside of sizeof returning an integer constant (in C90)
1100 not permitted in constant expressions, or because it contains an
1101 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1102 set to true by callers before calling this function.) Return the
1103 folded expression. Function arguments have already been folded
1104 before calling this function, as have the contents of SAVE_EXPR,
1105 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1106 C_MAYBE_CONST_EXPR. */
1108 tree
1109 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1111 tree ret;
1112 tree eptype = NULL_TREE;
1113 bool dummy = true;
1114 bool maybe_const_itself = true;
1115 location_t loc = EXPR_LOCATION (expr);
1117 /* This function is not relevant to C++ because C++ folds while
1118 parsing, and may need changes to be correct for C++ when C++
1119 stops folding while parsing. */
1120 if (c_dialect_cxx ())
1121 gcc_unreachable ();
1123 if (!maybe_const)
1124 maybe_const = &dummy;
1125 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1127 eptype = TREE_TYPE (expr);
1128 expr = TREE_OPERAND (expr, 0);
1130 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1131 &maybe_const_itself);
1132 if (eptype)
1133 ret = fold_convert_loc (loc, eptype, ret);
1134 *maybe_const &= maybe_const_itself;
1135 return ret;
1138 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1139 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1140 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1141 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1142 both evaluated and unevaluated subexpressions while
1143 *MAYBE_CONST_ITSELF is carried from only evaluated
1144 subexpressions). */
1146 static tree
1147 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1148 bool *maybe_const_itself)
1150 tree ret = expr;
1151 enum tree_code code = TREE_CODE (expr);
1152 enum tree_code_class kind = TREE_CODE_CLASS (code);
1153 location_t loc = EXPR_LOCATION (expr);
1154 tree op0, op1, op2, op3;
1155 tree orig_op0, orig_op1, orig_op2;
1156 bool op0_const = true, op1_const = true, op2_const = true;
1157 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1158 bool nowarning = TREE_NO_WARNING (expr);
1159 bool unused_p;
1161 /* This function is not relevant to C++ because C++ folds while
1162 parsing, and may need changes to be correct for C++ when C++
1163 stops folding while parsing. */
1164 if (c_dialect_cxx ())
1165 gcc_unreachable ();
1167 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1168 anything else not counted as an expression cannot usefully be
1169 folded further at this point. */
1170 if (!IS_EXPR_CODE_CLASS (kind)
1171 || kind == tcc_statement
1172 || code == SAVE_EXPR)
1173 return expr;
1175 /* Operands of variable-length expressions (function calls) have
1176 already been folded, as have __builtin_* function calls, and such
1177 expressions cannot occur in constant expressions. */
1178 if (kind == tcc_vl_exp)
1180 *maybe_const_operands = false;
1181 ret = fold (expr);
1182 goto out;
1185 if (code == C_MAYBE_CONST_EXPR)
1187 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1188 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1189 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1190 *maybe_const_operands = false;
1191 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1192 *maybe_const_itself = false;
1193 if (pre && !in_init)
1194 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1195 else
1196 ret = inner;
1197 goto out;
1200 /* Assignment, increment, decrement, function call and comma
1201 operators, and statement expressions, cannot occur in constant
1202 expressions if evaluated / outside of sizeof. (Function calls
1203 were handled above, though VA_ARG_EXPR is treated like a function
1204 call here, and statement expressions are handled through
1205 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1206 switch (code)
1208 case MODIFY_EXPR:
1209 case PREDECREMENT_EXPR:
1210 case PREINCREMENT_EXPR:
1211 case POSTDECREMENT_EXPR:
1212 case POSTINCREMENT_EXPR:
1213 case COMPOUND_EXPR:
1214 *maybe_const_operands = false;
1215 break;
1217 case VA_ARG_EXPR:
1218 case TARGET_EXPR:
1219 case BIND_EXPR:
1220 case OBJ_TYPE_REF:
1221 *maybe_const_operands = false;
1222 ret = fold (expr);
1223 goto out;
1225 default:
1226 break;
1229 /* Fold individual tree codes as appropriate. */
1230 switch (code)
1232 case COMPOUND_LITERAL_EXPR:
1233 /* Any non-constancy will have been marked in a containing
1234 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1235 goto out;
1237 case COMPONENT_REF:
1238 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1239 op1 = TREE_OPERAND (expr, 1);
1240 op2 = TREE_OPERAND (expr, 2);
1241 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1242 maybe_const_itself);
1243 STRIP_TYPE_NOPS (op0);
1244 if (op0 != orig_op0)
1245 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1246 if (ret != expr)
1248 TREE_READONLY (ret) = TREE_READONLY (expr);
1249 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1251 goto out;
1253 case ARRAY_REF:
1254 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1255 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1256 op2 = TREE_OPERAND (expr, 2);
1257 op3 = TREE_OPERAND (expr, 3);
1258 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1259 maybe_const_itself);
1260 STRIP_TYPE_NOPS (op0);
1261 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1262 maybe_const_itself);
1263 STRIP_TYPE_NOPS (op1);
1264 op1 = decl_constant_value_for_optimization (op1);
1265 if (op0 != orig_op0 || op1 != orig_op1)
1266 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1267 if (ret != expr)
1269 TREE_READONLY (ret) = TREE_READONLY (expr);
1270 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1271 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1273 ret = fold (ret);
1274 goto out;
1276 case COMPOUND_EXPR:
1277 case MODIFY_EXPR:
1278 case PREDECREMENT_EXPR:
1279 case PREINCREMENT_EXPR:
1280 case POSTDECREMENT_EXPR:
1281 case POSTINCREMENT_EXPR:
1282 case PLUS_EXPR:
1283 case MINUS_EXPR:
1284 case MULT_EXPR:
1285 case POINTER_PLUS_EXPR:
1286 case TRUNC_DIV_EXPR:
1287 case CEIL_DIV_EXPR:
1288 case FLOOR_DIV_EXPR:
1289 case TRUNC_MOD_EXPR:
1290 case RDIV_EXPR:
1291 case EXACT_DIV_EXPR:
1292 case LSHIFT_EXPR:
1293 case RSHIFT_EXPR:
1294 case BIT_IOR_EXPR:
1295 case BIT_XOR_EXPR:
1296 case BIT_AND_EXPR:
1297 case LT_EXPR:
1298 case LE_EXPR:
1299 case GT_EXPR:
1300 case GE_EXPR:
1301 case EQ_EXPR:
1302 case NE_EXPR:
1303 case COMPLEX_EXPR:
1304 case TRUTH_AND_EXPR:
1305 case TRUTH_OR_EXPR:
1306 case TRUTH_XOR_EXPR:
1307 case UNORDERED_EXPR:
1308 case ORDERED_EXPR:
1309 case UNLT_EXPR:
1310 case UNLE_EXPR:
1311 case UNGT_EXPR:
1312 case UNGE_EXPR:
1313 case UNEQ_EXPR:
1314 /* Binary operations evaluating both arguments (increment and
1315 decrement are binary internally in GCC). */
1316 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1317 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1318 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1319 maybe_const_itself);
1320 STRIP_TYPE_NOPS (op0);
1321 if (code != MODIFY_EXPR
1322 && code != PREDECREMENT_EXPR
1323 && code != PREINCREMENT_EXPR
1324 && code != POSTDECREMENT_EXPR
1325 && code != POSTINCREMENT_EXPR)
1326 op0 = decl_constant_value_for_optimization (op0);
1327 /* The RHS of a MODIFY_EXPR was fully folded when building that
1328 expression for the sake of conversion warnings. */
1329 if (code != MODIFY_EXPR)
1330 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1331 maybe_const_itself);
1332 STRIP_TYPE_NOPS (op1);
1333 op1 = decl_constant_value_for_optimization (op1);
1334 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1335 ret = in_init
1336 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1337 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1338 else
1339 ret = fold (expr);
1340 if (TREE_OVERFLOW_P (ret)
1341 && !TREE_OVERFLOW_P (op0)
1342 && !TREE_OVERFLOW_P (op1))
1343 overflow_warning (EXPR_LOCATION (expr), ret);
1344 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1345 && TREE_CODE (orig_op1) != INTEGER_CST
1346 && TREE_CODE (op1) == INTEGER_CST
1347 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1348 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1349 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1350 && c_inhibit_evaluation_warnings == 0)
1352 if (tree_int_cst_sgn (op1) < 0)
1353 warning_at (loc, 0, (code == LSHIFT_EXPR
1354 ? G_("left shift count is negative")
1355 : G_("right shift count is negative")));
1356 else if (compare_tree_int (op1,
1357 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1358 >= 0)
1359 warning_at (loc, 0, (code == LSHIFT_EXPR
1360 ? G_("left shift count >= width of type")
1361 : G_("right shift count >= width of type")));
1363 goto out;
1365 case INDIRECT_REF:
1366 case FIX_TRUNC_EXPR:
1367 case FLOAT_EXPR:
1368 CASE_CONVERT:
1369 case ADDR_SPACE_CONVERT_EXPR:
1370 case VIEW_CONVERT_EXPR:
1371 case NON_LVALUE_EXPR:
1372 case NEGATE_EXPR:
1373 case BIT_NOT_EXPR:
1374 case TRUTH_NOT_EXPR:
1375 case ADDR_EXPR:
1376 case CONJ_EXPR:
1377 case REALPART_EXPR:
1378 case IMAGPART_EXPR:
1379 /* Unary operations. */
1380 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1381 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1382 maybe_const_itself);
1383 STRIP_TYPE_NOPS (op0);
1384 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1385 op0 = decl_constant_value_for_optimization (op0);
1386 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1387 not prepared to deal with them if they occur in initializers.
1388 Avoid attempts to fold references to UPC shared components
1389 due to the complexities of UPC pointer-to-shared arithmetic. */
1390 if (op0 != orig_op0
1391 && code == ADDR_EXPR
1392 && (op1 = get_base_address (op0)) != NULL_TREE
1393 && TREE_CODE (op1) == INDIRECT_REF
1394 && !upc_shared_type_p (TREE_TYPE (op1))
1395 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1396 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1397 else if (op0 != orig_op0 || in_init)
1398 ret = in_init
1399 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1400 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1401 else
1402 ret = fold (expr);
1403 if (code == INDIRECT_REF
1404 && ret != expr
1405 && TREE_CODE (ret) == INDIRECT_REF)
1407 TREE_READONLY (ret) = TREE_READONLY (expr);
1408 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1409 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1411 switch (code)
1413 case FIX_TRUNC_EXPR:
1414 case FLOAT_EXPR:
1415 CASE_CONVERT:
1416 /* Don't warn about explicit conversions. We will already
1417 have warned about suspect implicit conversions. */
1418 break;
1420 default:
1421 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1422 overflow_warning (EXPR_LOCATION (expr), ret);
1423 break;
1425 goto out;
1427 case TRUTH_ANDIF_EXPR:
1428 case TRUTH_ORIF_EXPR:
1429 /* Binary operations not necessarily evaluating both
1430 arguments. */
1431 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1432 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1433 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1434 STRIP_TYPE_NOPS (op0);
1436 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1437 ? truthvalue_false_node
1438 : truthvalue_true_node));
1439 c_disable_warnings (unused_p);
1440 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1441 STRIP_TYPE_NOPS (op1);
1442 c_enable_warnings (unused_p);
1444 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1445 ret = in_init
1446 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1447 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1448 else
1449 ret = fold (expr);
1450 *maybe_const_operands &= op0_const;
1451 *maybe_const_itself &= op0_const_self;
1452 if (!(flag_isoc99
1453 && op0_const
1454 && op0_const_self
1455 && (code == TRUTH_ANDIF_EXPR
1456 ? op0 == truthvalue_false_node
1457 : op0 == truthvalue_true_node)))
1458 *maybe_const_operands &= op1_const;
1459 if (!(op0_const
1460 && op0_const_self
1461 && (code == TRUTH_ANDIF_EXPR
1462 ? op0 == truthvalue_false_node
1463 : op0 == truthvalue_true_node)))
1464 *maybe_const_itself &= op1_const_self;
1465 goto out;
1467 case COND_EXPR:
1468 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1469 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1470 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1471 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1473 STRIP_TYPE_NOPS (op0);
1474 c_disable_warnings (op0 == truthvalue_false_node);
1475 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1476 STRIP_TYPE_NOPS (op1);
1477 c_enable_warnings (op0 == truthvalue_false_node);
1479 c_disable_warnings (op0 == truthvalue_true_node);
1480 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1481 STRIP_TYPE_NOPS (op2);
1482 c_enable_warnings (op0 == truthvalue_true_node);
1484 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1485 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1486 else
1487 ret = fold (expr);
1488 *maybe_const_operands &= op0_const;
1489 *maybe_const_itself &= op0_const_self;
1490 if (!(flag_isoc99
1491 && op0_const
1492 && op0_const_self
1493 && op0 == truthvalue_false_node))
1494 *maybe_const_operands &= op1_const;
1495 if (!(op0_const
1496 && op0_const_self
1497 && op0 == truthvalue_false_node))
1498 *maybe_const_itself &= op1_const_self;
1499 if (!(flag_isoc99
1500 && op0_const
1501 && op0_const_self
1502 && op0 == truthvalue_true_node))
1503 *maybe_const_operands &= op2_const;
1504 if (!(op0_const
1505 && op0_const_self
1506 && op0 == truthvalue_true_node))
1507 *maybe_const_itself &= op2_const_self;
1508 goto out;
1510 case EXCESS_PRECISION_EXPR:
1511 /* Each case where an operand with excess precision may be
1512 encountered must remove the EXCESS_PRECISION_EXPR around
1513 inner operands and possibly put one around the whole
1514 expression or possibly convert to the semantic type (which
1515 c_fully_fold does); we cannot tell at this stage which is
1516 appropriate in any particular case. */
1517 gcc_unreachable ();
1519 default:
1520 /* Various codes may appear through folding built-in functions
1521 and their arguments. */
1522 goto out;
1525 out:
1526 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1527 have been done by this point, so remove them again. */
1528 nowarning |= TREE_NO_WARNING (ret);
1529 STRIP_TYPE_NOPS (ret);
1530 if (nowarning && !TREE_NO_WARNING (ret))
1532 if (!CAN_HAVE_LOCATION_P (ret))
1533 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1534 TREE_NO_WARNING (ret) = 1;
1536 if (ret != expr)
1537 protected_set_expr_location (ret, loc);
1538 return ret;
1541 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1542 return EXP. Otherwise, return either EXP or its known constant
1543 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1544 Is the BLKmode test appropriate? */
1546 tree
1547 decl_constant_value_for_optimization (tree exp)
1549 tree ret;
1551 /* This function is only used by C, for c_fully_fold and other
1552 optimization, and may not be correct for C++. */
1553 if (c_dialect_cxx ())
1554 gcc_unreachable ();
1556 if (!optimize
1557 || TREE_CODE (exp) != VAR_DECL
1558 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1559 || DECL_MODE (exp) == BLKmode)
1560 return exp;
1562 ret = decl_constant_value (exp);
1563 /* Avoid unwanted tree sharing between the initializer and current
1564 function's body where the tree can be modified e.g. by the
1565 gimplifier. */
1566 if (ret != exp && TREE_STATIC (exp))
1567 ret = unshare_expr (ret);
1568 return ret;
1571 /* Print a warning if a constant expression had overflow in folding.
1572 Invoke this function on every expression that the language
1573 requires to be a constant expression.
1574 Note the ANSI C standard says it is erroneous for a
1575 constant expression to overflow. */
1577 void
1578 constant_expression_warning (tree value)
1580 if (warn_overflow && pedantic
1581 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1582 || TREE_CODE (value) == FIXED_CST
1583 || TREE_CODE (value) == VECTOR_CST
1584 || TREE_CODE (value) == COMPLEX_CST)
1585 && TREE_OVERFLOW (value))
1586 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1589 /* The same as above but print an unconditional error. */
1590 void
1591 constant_expression_error (tree value)
1593 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1594 || TREE_CODE (value) == FIXED_CST
1595 || TREE_CODE (value) == VECTOR_CST
1596 || TREE_CODE (value) == COMPLEX_CST)
1597 && TREE_OVERFLOW (value))
1598 error ("overflow in constant expression");
1601 /* Print a warning if an expression had overflow in folding and its
1602 operands hadn't.
1604 Invoke this function on every expression that
1605 (1) appears in the source code, and
1606 (2) is a constant expression that overflowed, and
1607 (3) is not already checked by convert_and_check;
1608 however, do not invoke this function on operands of explicit casts
1609 or when the expression is the result of an operator and any operand
1610 already overflowed. */
1612 void
1613 overflow_warning (location_t loc, tree value)
1615 if (c_inhibit_evaluation_warnings != 0)
1616 return;
1618 switch (TREE_CODE (value))
1620 case INTEGER_CST:
1621 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1622 break;
1624 case REAL_CST:
1625 warning_at (loc, OPT_Woverflow,
1626 "floating point overflow in expression");
1627 break;
1629 case FIXED_CST:
1630 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1631 break;
1633 case VECTOR_CST:
1634 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1635 break;
1637 case COMPLEX_CST:
1638 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1639 warning_at (loc, OPT_Woverflow,
1640 "complex integer overflow in expression");
1641 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1642 warning_at (loc, OPT_Woverflow,
1643 "complex floating point overflow in expression");
1644 break;
1646 default:
1647 break;
1651 /* Warn about uses of logical || / && operator in a context where it
1652 is likely that the bitwise equivalent was intended by the
1653 programmer. We have seen an expression in which CODE is a binary
1654 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1655 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1656 void
1657 warn_logical_operator (location_t location, enum tree_code code, tree type,
1658 enum tree_code code_left, tree op_left,
1659 enum tree_code ARG_UNUSED (code_right), tree op_right)
1661 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1662 int in0_p, in1_p, in_p;
1663 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1664 bool strict_overflow_p = false;
1666 if (code != TRUTH_ANDIF_EXPR
1667 && code != TRUTH_AND_EXPR
1668 && code != TRUTH_ORIF_EXPR
1669 && code != TRUTH_OR_EXPR)
1670 return;
1672 /* Warn if &&/|| are being used in a context where it is
1673 likely that the bitwise equivalent was intended by the
1674 programmer. That is, an expression such as op && MASK
1675 where op should not be any boolean expression, nor a
1676 constant, and mask seems to be a non-boolean integer constant. */
1677 if (!truth_value_p (code_left)
1678 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1679 && !CONSTANT_CLASS_P (op_left)
1680 && !TREE_NO_WARNING (op_left)
1681 && TREE_CODE (op_right) == INTEGER_CST
1682 && !integer_zerop (op_right)
1683 && !integer_onep (op_right))
1685 if (or_op)
1686 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1687 " applied to non-boolean constant");
1688 else
1689 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1690 " applied to non-boolean constant");
1691 TREE_NO_WARNING (op_left) = true;
1692 return;
1695 /* We do not warn for constants because they are typical of macro
1696 expansions that test for features. */
1697 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1698 return;
1700 /* This warning only makes sense with logical operands. */
1701 if (!(truth_value_p (TREE_CODE (op_left))
1702 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1703 || !(truth_value_p (TREE_CODE (op_right))
1704 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1705 return;
1708 /* We first test whether either side separately is trivially true
1709 (with OR) or trivially false (with AND). If so, do not warn.
1710 This is a common idiom for testing ranges of data types in
1711 portable code. */
1712 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1713 if (!lhs)
1714 return;
1715 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1716 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1718 /* If this is an OR operation, invert both sides; now, the result
1719 should be always false to get a warning. */
1720 if (or_op)
1721 in0_p = !in0_p;
1723 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1724 if (tem && integer_zerop (tem))
1725 return;
1727 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1728 if (!rhs)
1729 return;
1730 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1731 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1733 /* If this is an OR operation, invert both sides; now, the result
1734 should be always false to get a warning. */
1735 if (or_op)
1736 in1_p = !in1_p;
1738 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1739 if (tem && integer_zerop (tem))
1740 return;
1742 /* If both expressions have the same operand, if we can merge the
1743 ranges, and if the range test is always false, then warn. */
1744 if (operand_equal_p (lhs, rhs, 0)
1745 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1746 in1_p, low1, high1)
1747 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1748 type, lhs, in_p, low, high))
1749 && integer_zerop (tem))
1751 if (or_op)
1752 warning_at (location, OPT_Wlogical_op,
1753 "logical %<or%> "
1754 "of collectively exhaustive tests is always true");
1755 else
1756 warning_at (location, OPT_Wlogical_op,
1757 "logical %<and%> "
1758 "of mutually exclusive tests is always false");
1762 /* Warn about logical not used on the left hand side operand of a comparison.
1763 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1764 Do not warn if RHS is of a boolean type. */
1766 void
1767 warn_logical_not_parentheses (location_t location, enum tree_code code,
1768 tree rhs)
1770 if (TREE_CODE_CLASS (code) != tcc_comparison
1771 || TREE_TYPE (rhs) == NULL_TREE
1772 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1773 return;
1775 warning_at (location, OPT_Wlogical_not_parentheses,
1776 "logical not is only applied to the left hand side of "
1777 "comparison");
1780 /* Warn if EXP contains any computations whose results are not used.
1781 Return true if a warning is printed; false otherwise. LOCUS is the
1782 (potential) location of the expression. */
1784 bool
1785 warn_if_unused_value (const_tree exp, location_t locus)
1787 restart:
1788 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1789 return false;
1791 /* Don't warn about void constructs. This includes casting to void,
1792 void function calls, and statement expressions with a final cast
1793 to void. */
1794 if (VOID_TYPE_P (TREE_TYPE (exp)))
1795 return false;
1797 if (EXPR_HAS_LOCATION (exp))
1798 locus = EXPR_LOCATION (exp);
1800 switch (TREE_CODE (exp))
1802 case PREINCREMENT_EXPR:
1803 case POSTINCREMENT_EXPR:
1804 case PREDECREMENT_EXPR:
1805 case POSTDECREMENT_EXPR:
1806 case MODIFY_EXPR:
1807 case INIT_EXPR:
1808 case TARGET_EXPR:
1809 case CALL_EXPR:
1810 case TRY_CATCH_EXPR:
1811 case WITH_CLEANUP_EXPR:
1812 case EXIT_EXPR:
1813 case VA_ARG_EXPR:
1814 return false;
1816 case BIND_EXPR:
1817 /* For a binding, warn if no side effect within it. */
1818 exp = BIND_EXPR_BODY (exp);
1819 goto restart;
1821 case SAVE_EXPR:
1822 case NON_LVALUE_EXPR:
1823 case NOP_EXPR:
1824 exp = TREE_OPERAND (exp, 0);
1825 goto restart;
1827 case TRUTH_ORIF_EXPR:
1828 case TRUTH_ANDIF_EXPR:
1829 /* In && or ||, warn if 2nd operand has no side effect. */
1830 exp = TREE_OPERAND (exp, 1);
1831 goto restart;
1833 case COMPOUND_EXPR:
1834 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1835 return true;
1836 /* Let people do `(foo (), 0)' without a warning. */
1837 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1838 return false;
1839 exp = TREE_OPERAND (exp, 1);
1840 goto restart;
1842 case COND_EXPR:
1843 /* If this is an expression with side effects, don't warn; this
1844 case commonly appears in macro expansions. */
1845 if (TREE_SIDE_EFFECTS (exp))
1846 return false;
1847 goto warn;
1849 case INDIRECT_REF:
1850 /* Don't warn about automatic dereferencing of references, since
1851 the user cannot control it. */
1852 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1854 exp = TREE_OPERAND (exp, 0);
1855 goto restart;
1857 /* Fall through. */
1859 default:
1860 /* Referencing a volatile value is a side effect, so don't warn. */
1861 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1862 && TREE_THIS_VOLATILE (exp))
1863 return false;
1865 /* If this is an expression which has no operands, there is no value
1866 to be unused. There are no such language-independent codes,
1867 but front ends may define such. */
1868 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1869 return false;
1871 warn:
1872 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1877 /* Print a warning about casts that might indicate violation
1878 of strict aliasing rules if -Wstrict-aliasing is used and
1879 strict aliasing mode is in effect. OTYPE is the original
1880 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1882 bool
1883 strict_aliasing_warning (tree otype, tree type, tree expr)
1885 /* Strip pointer conversion chains and get to the correct original type. */
1886 STRIP_NOPS (expr);
1887 otype = TREE_TYPE (expr);
1889 if (!(flag_strict_aliasing
1890 && POINTER_TYPE_P (type)
1891 && POINTER_TYPE_P (otype)
1892 && !VOID_TYPE_P (TREE_TYPE (type)))
1893 /* If the type we are casting to is a ref-all pointer
1894 dereferencing it is always valid. */
1895 || TYPE_REF_CAN_ALIAS_ALL (type))
1896 return false;
1898 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1899 && (DECL_P (TREE_OPERAND (expr, 0))
1900 || handled_component_p (TREE_OPERAND (expr, 0))))
1902 /* Casting the address of an object to non void pointer. Warn
1903 if the cast breaks type based aliasing. */
1904 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1906 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1907 "might break strict-aliasing rules");
1908 return true;
1910 else
1912 /* warn_strict_aliasing >= 3. This includes the default (3).
1913 Only warn if the cast is dereferenced immediately. */
1914 alias_set_type set1 =
1915 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1916 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1918 if (set1 != set2 && set2 != 0
1919 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1921 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1922 "pointer will break strict-aliasing rules");
1923 return true;
1925 else if (warn_strict_aliasing == 2
1926 && !alias_sets_must_conflict_p (set1, set2))
1928 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1929 "pointer might break strict-aliasing rules");
1930 return true;
1934 else
1935 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1937 /* At this level, warn for any conversions, even if an address is
1938 not taken in the same statement. This will likely produce many
1939 false positives, but could be useful to pinpoint problems that
1940 are not revealed at higher levels. */
1941 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1942 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1943 if (!COMPLETE_TYPE_P (type)
1944 || !alias_sets_must_conflict_p (set1, set2))
1946 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1947 "pointer might break strict-aliasing rules");
1948 return true;
1952 return false;
1955 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1956 sizeof as last operand of certain builtins. */
1958 void
1959 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1960 vec<tree, va_gc> *params, tree *sizeof_arg,
1961 bool (*comp_types) (tree, tree))
1963 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1964 bool strop = false, cmp = false;
1965 unsigned int idx = ~0;
1966 location_t loc;
1968 if (TREE_CODE (callee) != FUNCTION_DECL
1969 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1970 || vec_safe_length (params) <= 1)
1971 return;
1973 switch (DECL_FUNCTION_CODE (callee))
1975 case BUILT_IN_STRNCMP:
1976 case BUILT_IN_STRNCASECMP:
1977 cmp = true;
1978 /* FALLTHRU */
1979 case BUILT_IN_STRNCPY:
1980 case BUILT_IN_STRNCPY_CHK:
1981 case BUILT_IN_STRNCAT:
1982 case BUILT_IN_STRNCAT_CHK:
1983 case BUILT_IN_STPNCPY:
1984 case BUILT_IN_STPNCPY_CHK:
1985 strop = true;
1986 /* FALLTHRU */
1987 case BUILT_IN_MEMCPY:
1988 case BUILT_IN_MEMCPY_CHK:
1989 case BUILT_IN_MEMMOVE:
1990 case BUILT_IN_MEMMOVE_CHK:
1991 if (params->length () < 3)
1992 return;
1993 src = (*params)[1];
1994 dest = (*params)[0];
1995 idx = 2;
1996 break;
1997 case BUILT_IN_BCOPY:
1998 if (params->length () < 3)
1999 return;
2000 src = (*params)[0];
2001 dest = (*params)[1];
2002 idx = 2;
2003 break;
2004 case BUILT_IN_MEMCMP:
2005 case BUILT_IN_BCMP:
2006 if (params->length () < 3)
2007 return;
2008 src = (*params)[1];
2009 dest = (*params)[0];
2010 idx = 2;
2011 cmp = true;
2012 break;
2013 case BUILT_IN_MEMSET:
2014 case BUILT_IN_MEMSET_CHK:
2015 if (params->length () < 3)
2016 return;
2017 dest = (*params)[0];
2018 idx = 2;
2019 break;
2020 case BUILT_IN_BZERO:
2021 dest = (*params)[0];
2022 idx = 1;
2023 break;
2024 case BUILT_IN_STRNDUP:
2025 src = (*params)[0];
2026 strop = true;
2027 idx = 1;
2028 break;
2029 case BUILT_IN_MEMCHR:
2030 if (params->length () < 3)
2031 return;
2032 src = (*params)[0];
2033 idx = 2;
2034 break;
2035 case BUILT_IN_SNPRINTF:
2036 case BUILT_IN_SNPRINTF_CHK:
2037 case BUILT_IN_VSNPRINTF:
2038 case BUILT_IN_VSNPRINTF_CHK:
2039 dest = (*params)[0];
2040 idx = 1;
2041 strop = true;
2042 break;
2043 default:
2044 break;
2047 if (idx >= 3)
2048 return;
2050 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2051 return;
2053 type = TYPE_P (sizeof_arg[idx])
2054 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2055 if (!POINTER_TYPE_P (type))
2056 return;
2058 if (dest
2059 && (tem = tree_strip_nop_conversions (dest))
2060 && POINTER_TYPE_P (TREE_TYPE (tem))
2061 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2062 return;
2064 if (src
2065 && (tem = tree_strip_nop_conversions (src))
2066 && POINTER_TYPE_P (TREE_TYPE (tem))
2067 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2068 return;
2070 loc = sizeof_arg_loc[idx];
2072 if (dest && !cmp)
2074 if (!TYPE_P (sizeof_arg[idx])
2075 && operand_equal_p (dest, sizeof_arg[idx], 0)
2076 && comp_types (TREE_TYPE (dest), type))
2078 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2079 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2080 "argument to %<sizeof%> in %qD call is the same "
2081 "expression as the destination; did you mean to "
2082 "remove the addressof?", callee);
2083 else if ((TYPE_PRECISION (TREE_TYPE (type))
2084 == TYPE_PRECISION (char_type_node))
2085 || strop)
2086 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2087 "argument to %<sizeof%> in %qD call is the same "
2088 "expression as the destination; did you mean to "
2089 "provide an explicit length?", callee);
2090 else
2091 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2092 "argument to %<sizeof%> in %qD call is the same "
2093 "expression as the destination; did you mean to "
2094 "dereference it?", callee);
2095 return;
2098 if (POINTER_TYPE_P (TREE_TYPE (dest))
2099 && !strop
2100 && comp_types (TREE_TYPE (dest), type)
2101 && !VOID_TYPE_P (TREE_TYPE (type)))
2103 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2104 "argument to %<sizeof%> in %qD call is the same "
2105 "pointer type %qT as the destination; expected %qT "
2106 "or an explicit length", callee, TREE_TYPE (dest),
2107 TREE_TYPE (TREE_TYPE (dest)));
2108 return;
2112 if (src && !cmp)
2114 if (!TYPE_P (sizeof_arg[idx])
2115 && operand_equal_p (src, sizeof_arg[idx], 0)
2116 && comp_types (TREE_TYPE (src), type))
2118 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2119 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2120 "argument to %<sizeof%> in %qD call is the same "
2121 "expression as the source; did you mean to "
2122 "remove the addressof?", callee);
2123 else if ((TYPE_PRECISION (TREE_TYPE (type))
2124 == TYPE_PRECISION (char_type_node))
2125 || strop)
2126 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2127 "argument to %<sizeof%> in %qD call is the same "
2128 "expression as the source; did you mean to "
2129 "provide an explicit length?", callee);
2130 else
2131 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2132 "argument to %<sizeof%> in %qD call is the same "
2133 "expression as the source; did you mean to "
2134 "dereference it?", callee);
2135 return;
2138 if (POINTER_TYPE_P (TREE_TYPE (src))
2139 && !strop
2140 && comp_types (TREE_TYPE (src), type)
2141 && !VOID_TYPE_P (TREE_TYPE (type)))
2143 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2144 "argument to %<sizeof%> in %qD call is the same "
2145 "pointer type %qT as the source; expected %qT "
2146 "or an explicit length", callee, TREE_TYPE (src),
2147 TREE_TYPE (TREE_TYPE (src)));
2148 return;
2152 if (dest)
2154 if (!TYPE_P (sizeof_arg[idx])
2155 && operand_equal_p (dest, sizeof_arg[idx], 0)
2156 && comp_types (TREE_TYPE (dest), type))
2158 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2159 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2160 "argument to %<sizeof%> in %qD call is the same "
2161 "expression as the first source; did you mean to "
2162 "remove the addressof?", callee);
2163 else if ((TYPE_PRECISION (TREE_TYPE (type))
2164 == TYPE_PRECISION (char_type_node))
2165 || strop)
2166 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2167 "argument to %<sizeof%> in %qD call is the same "
2168 "expression as the first source; did you mean to "
2169 "provide an explicit length?", callee);
2170 else
2171 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2172 "argument to %<sizeof%> in %qD call is the same "
2173 "expression as the first source; did you mean to "
2174 "dereference it?", callee);
2175 return;
2178 if (POINTER_TYPE_P (TREE_TYPE (dest))
2179 && !strop
2180 && comp_types (TREE_TYPE (dest), type)
2181 && !VOID_TYPE_P (TREE_TYPE (type)))
2183 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2184 "argument to %<sizeof%> in %qD call is the same "
2185 "pointer type %qT as the first source; expected %qT "
2186 "or an explicit length", callee, TREE_TYPE (dest),
2187 TREE_TYPE (TREE_TYPE (dest)));
2188 return;
2192 if (src)
2194 if (!TYPE_P (sizeof_arg[idx])
2195 && operand_equal_p (src, sizeof_arg[idx], 0)
2196 && comp_types (TREE_TYPE (src), type))
2198 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2199 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2200 "argument to %<sizeof%> in %qD call is the same "
2201 "expression as the second source; did you mean to "
2202 "remove the addressof?", callee);
2203 else if ((TYPE_PRECISION (TREE_TYPE (type))
2204 == TYPE_PRECISION (char_type_node))
2205 || strop)
2206 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2207 "argument to %<sizeof%> in %qD call is the same "
2208 "expression as the second source; did you mean to "
2209 "provide an explicit length?", callee);
2210 else
2211 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2212 "argument to %<sizeof%> in %qD call is the same "
2213 "expression as the second source; did you mean to "
2214 "dereference it?", callee);
2215 return;
2218 if (POINTER_TYPE_P (TREE_TYPE (src))
2219 && !strop
2220 && comp_types (TREE_TYPE (src), type)
2221 && !VOID_TYPE_P (TREE_TYPE (type)))
2223 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2224 "argument to %<sizeof%> in %qD call is the same "
2225 "pointer type %qT as the second source; expected %qT "
2226 "or an explicit length", callee, TREE_TYPE (src),
2227 TREE_TYPE (TREE_TYPE (src)));
2228 return;
2234 /* Warn for unlikely, improbable, or stupid DECL declarations
2235 of `main'. */
2237 void
2238 check_main_parameter_types (tree decl)
2240 function_args_iterator iter;
2241 tree type;
2242 int argct = 0;
2244 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2246 /* XXX void_type_node belies the abstraction. */
2247 if (type == void_type_node || type == error_mark_node )
2248 break;
2250 tree t = type;
2251 if (TYPE_ATOMIC (t))
2252 pedwarn (input_location, OPT_Wmain,
2253 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2254 type, decl);
2255 while (POINTER_TYPE_P (t))
2257 t = TREE_TYPE (t);
2258 if (TYPE_ATOMIC (t))
2259 pedwarn (input_location, OPT_Wmain,
2260 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2261 type, decl);
2264 ++argct;
2265 switch (argct)
2267 case 1:
2268 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2269 pedwarn (input_location, OPT_Wmain,
2270 "first argument of %q+D should be %<int%>", decl);
2271 break;
2273 case 2:
2274 if (TREE_CODE (type) != POINTER_TYPE
2275 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2276 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2277 != char_type_node))
2278 pedwarn (input_location, OPT_Wmain,
2279 "second argument of %q+D should be %<char **%>", decl);
2280 break;
2282 case 3:
2283 if (TREE_CODE (type) != POINTER_TYPE
2284 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2285 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2286 != char_type_node))
2287 pedwarn (input_location, OPT_Wmain,
2288 "third argument of %q+D should probably be "
2289 "%<char **%>", decl);
2290 break;
2294 /* It is intentional that this message does not mention the third
2295 argument because it's only mentioned in an appendix of the
2296 standard. */
2297 if (argct > 0 && (argct < 2 || argct > 3))
2298 pedwarn (input_location, OPT_Wmain,
2299 "%q+D takes only zero or two arguments", decl);
2301 if (stdarg_p (TREE_TYPE (decl)))
2302 pedwarn (input_location, OPT_Wmain,
2303 "%q+D declared as variadic function", decl);
2306 /* vector_targets_convertible_p is used for vector pointer types. The
2307 callers perform various checks that the qualifiers are satisfactory,
2308 while OTOH vector_targets_convertible_p ignores the number of elements
2309 in the vectors. That's fine with vector pointers as we can consider,
2310 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2311 and that does not require and conversion of the pointer values.
2312 In contrast, vector_types_convertible_p and
2313 vector_types_compatible_elements_p are used for vector value types. */
2314 /* True if pointers to distinct types T1 and T2 can be converted to
2315 each other without an explicit cast. Only returns true for opaque
2316 vector types. */
2317 bool
2318 vector_targets_convertible_p (const_tree t1, const_tree t2)
2320 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2321 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2322 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2323 return true;
2325 return false;
2328 /* vector_types_convertible_p is used for vector value types.
2329 It could in principle call vector_targets_convertible_p as a subroutine,
2330 but then the check for vector type would be duplicated with its callers,
2331 and also the purpose of vector_targets_convertible_p would become
2332 muddled.
2333 Where vector_types_convertible_p returns true, a conversion might still be
2334 needed to make the types match.
2335 In contrast, vector_targets_convertible_p is used for vector pointer
2336 values, and vector_types_compatible_elements_p is used specifically
2337 in the context for binary operators, as a check if use is possible without
2338 conversion. */
2339 /* True if vector types T1 and T2 can be converted to each other
2340 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2341 can only be converted with -flax-vector-conversions yet that is not
2342 in effect, emit a note telling the user about that option if such
2343 a note has not previously been emitted. */
2344 bool
2345 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2347 static bool emitted_lax_note = false;
2348 bool convertible_lax;
2350 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2351 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2352 return true;
2354 convertible_lax =
2355 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2356 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2357 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2358 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2359 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2361 if (!convertible_lax || flag_lax_vector_conversions)
2362 return convertible_lax;
2364 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2365 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2366 return true;
2368 if (emit_lax_note && !emitted_lax_note)
2370 emitted_lax_note = true;
2371 inform (input_location, "use -flax-vector-conversions to permit "
2372 "conversions between vectors with differing "
2373 "element types or numbers of subparts");
2376 return false;
2379 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2380 and have vector types, V0 has the same type as V1, and the number of
2381 elements of V0, V1, MASK is the same.
2383 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2384 called with two arguments. In this case implementation passes the
2385 first argument twice in order to share the same tree code. This fact
2386 could enable the mask-values being twice the vector length. This is
2387 an implementation accident and this semantics is not guaranteed to
2388 the user. */
2389 tree
2390 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2391 bool complain)
2393 tree ret;
2394 bool wrap = true;
2395 bool maybe_const = false;
2396 bool two_arguments = false;
2398 if (v1 == NULL_TREE)
2400 two_arguments = true;
2401 v1 = v0;
2404 if (v0 == error_mark_node || v1 == error_mark_node
2405 || mask == error_mark_node)
2406 return error_mark_node;
2408 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2409 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2411 if (complain)
2412 error_at (loc, "__builtin_shuffle last argument must "
2413 "be an integer vector");
2414 return error_mark_node;
2417 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2418 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2420 if (complain)
2421 error_at (loc, "__builtin_shuffle arguments must be vectors");
2422 return error_mark_node;
2425 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2427 if (complain)
2428 error_at (loc, "__builtin_shuffle argument vectors must be of "
2429 "the same type");
2430 return error_mark_node;
2433 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2434 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2435 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2436 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2438 if (complain)
2439 error_at (loc, "__builtin_shuffle number of elements of the "
2440 "argument vector(s) and the mask vector should "
2441 "be the same");
2442 return error_mark_node;
2445 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2446 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2448 if (complain)
2449 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2450 "must have the same size as inner type of the mask");
2451 return error_mark_node;
2454 if (!c_dialect_cxx ())
2456 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2457 v0 = c_fully_fold (v0, false, &maybe_const);
2458 wrap &= maybe_const;
2460 if (two_arguments)
2461 v1 = v0 = save_expr (v0);
2462 else
2464 v1 = c_fully_fold (v1, false, &maybe_const);
2465 wrap &= maybe_const;
2468 mask = c_fully_fold (mask, false, &maybe_const);
2469 wrap &= maybe_const;
2471 else if (two_arguments)
2472 v1 = v0 = save_expr (v0);
2474 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2476 if (!c_dialect_cxx () && !wrap)
2477 ret = c_wrap_maybe_const (ret, true);
2479 return ret;
2482 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2483 to integral type. */
2485 static tree
2486 c_common_get_narrower (tree op, int *unsignedp_ptr)
2488 op = get_narrower (op, unsignedp_ptr);
2490 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2491 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2493 /* C++0x scoped enumerations don't implicitly convert to integral
2494 type; if we stripped an explicit conversion to a larger type we
2495 need to replace it so common_type will still work. */
2496 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2497 TYPE_UNSIGNED (TREE_TYPE (op)));
2498 op = fold_convert (type, op);
2500 return op;
2503 /* This is a helper function of build_binary_op.
2505 For certain operations if both args were extended from the same
2506 smaller type, do the arithmetic in that type and then extend.
2508 BITWISE indicates a bitwise operation.
2509 For them, this optimization is safe only if
2510 both args are zero-extended or both are sign-extended.
2511 Otherwise, we might change the result.
2512 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2513 but calculated in (unsigned short) it would be (unsigned short)-1.
2515 tree
2516 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2518 int unsigned0, unsigned1;
2519 tree arg0, arg1;
2520 int uns;
2521 tree type;
2523 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2524 excessive narrowing when we call get_narrower below. For
2525 example, suppose that OP0 is of unsigned int extended
2526 from signed char and that RESULT_TYPE is long long int.
2527 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2528 like
2530 (long long int) (unsigned int) signed_char
2532 which get_narrower would narrow down to
2534 (unsigned int) signed char
2536 If we do not cast OP0 first, get_narrower would return
2537 signed_char, which is inconsistent with the case of the
2538 explicit cast. */
2539 op0 = convert (result_type, op0);
2540 op1 = convert (result_type, op1);
2542 arg0 = c_common_get_narrower (op0, &unsigned0);
2543 arg1 = c_common_get_narrower (op1, &unsigned1);
2545 /* UNS is 1 if the operation to be done is an unsigned one. */
2546 uns = TYPE_UNSIGNED (result_type);
2548 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2549 but it *requires* conversion to FINAL_TYPE. */
2551 if ((TYPE_PRECISION (TREE_TYPE (op0))
2552 == TYPE_PRECISION (TREE_TYPE (arg0)))
2553 && TREE_TYPE (op0) != result_type)
2554 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2555 if ((TYPE_PRECISION (TREE_TYPE (op1))
2556 == TYPE_PRECISION (TREE_TYPE (arg1)))
2557 && TREE_TYPE (op1) != result_type)
2558 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2560 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2562 /* For bitwise operations, signedness of nominal type
2563 does not matter. Consider only how operands were extended. */
2564 if (bitwise)
2565 uns = unsigned0;
2567 /* Note that in all three cases below we refrain from optimizing
2568 an unsigned operation on sign-extended args.
2569 That would not be valid. */
2571 /* Both args variable: if both extended in same way
2572 from same width, do it in that width.
2573 Do it unsigned if args were zero-extended. */
2574 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2575 < TYPE_PRECISION (result_type))
2576 && (TYPE_PRECISION (TREE_TYPE (arg1))
2577 == TYPE_PRECISION (TREE_TYPE (arg0)))
2578 && unsigned0 == unsigned1
2579 && (unsigned0 || !uns))
2580 return c_common_signed_or_unsigned_type
2581 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2583 else if (TREE_CODE (arg0) == INTEGER_CST
2584 && (unsigned1 || !uns)
2585 && (TYPE_PRECISION (TREE_TYPE (arg1))
2586 < TYPE_PRECISION (result_type))
2587 && (type
2588 = c_common_signed_or_unsigned_type (unsigned1,
2589 TREE_TYPE (arg1)))
2590 && !POINTER_TYPE_P (type)
2591 && int_fits_type_p (arg0, type))
2592 return type;
2594 else if (TREE_CODE (arg1) == INTEGER_CST
2595 && (unsigned0 || !uns)
2596 && (TYPE_PRECISION (TREE_TYPE (arg0))
2597 < TYPE_PRECISION (result_type))
2598 && (type
2599 = c_common_signed_or_unsigned_type (unsigned0,
2600 TREE_TYPE (arg0)))
2601 && !POINTER_TYPE_P (type)
2602 && int_fits_type_p (arg1, type))
2603 return type;
2605 return result_type;
2608 /* Checks if expression EXPR of real/integer type cannot be converted
2609 to the real/integer type TYPE. Function returns non-zero when:
2610 * EXPR is a constant which cannot be exactly converted to TYPE.
2611 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2612 for EXPR type and TYPE being both integers or both real.
2613 * EXPR is not a constant of real type and TYPE is an integer.
2614 * EXPR is not a constant of integer type which cannot be
2615 exactly converted to real type.
2616 Function allows conversions between types of different signedness and
2617 can return SAFE_CONVERSION (zero) in that case. Function can produce
2618 signedness warnings if PRODUCE_WARNS is true. */
2620 enum conversion_safety
2621 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2623 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2624 tree expr_type = TREE_TYPE (expr);
2625 loc = expansion_point_location_if_in_system_header (loc);
2627 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2629 /* Warn for real constant that is not an exact integer converted
2630 to integer type. */
2631 if (TREE_CODE (expr_type) == REAL_TYPE
2632 && TREE_CODE (type) == INTEGER_TYPE)
2634 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2635 give_warning = UNSAFE_REAL;
2637 /* Warn for an integer constant that does not fit into integer type. */
2638 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2639 && TREE_CODE (type) == INTEGER_TYPE
2640 && !int_fits_type_p (expr, type))
2642 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2643 && tree_int_cst_sgn (expr) < 0)
2645 if (produce_warns)
2646 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2647 " implicitly converted to unsigned type");
2649 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2651 if (produce_warns)
2652 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2653 " constant value to negative integer");
2655 else
2656 give_warning = UNSAFE_OTHER;
2658 else if (TREE_CODE (type) == REAL_TYPE)
2660 /* Warn for an integer constant that does not fit into real type. */
2661 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2663 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2664 if (!exact_real_truncate (TYPE_MODE (type), &a))
2665 give_warning = UNSAFE_REAL;
2667 /* Warn for a real constant that does not fit into a smaller
2668 real type. */
2669 else if (TREE_CODE (expr_type) == REAL_TYPE
2670 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2672 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2673 if (!exact_real_truncate (TYPE_MODE (type), &a))
2674 give_warning = UNSAFE_REAL;
2678 else
2680 /* Warn for real types converted to integer types. */
2681 if (TREE_CODE (expr_type) == REAL_TYPE
2682 && TREE_CODE (type) == INTEGER_TYPE)
2683 give_warning = UNSAFE_REAL;
2685 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2686 && TREE_CODE (type) == INTEGER_TYPE)
2688 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2689 expr = get_unwidened (expr, 0);
2690 expr_type = TREE_TYPE (expr);
2692 /* Don't warn for short y; short x = ((int)y & 0xff); */
2693 if (TREE_CODE (expr) == BIT_AND_EXPR
2694 || TREE_CODE (expr) == BIT_IOR_EXPR
2695 || TREE_CODE (expr) == BIT_XOR_EXPR)
2697 /* If both args were extended from a shortest type,
2698 use that type if that is safe. */
2699 expr_type = shorten_binary_op (expr_type,
2700 TREE_OPERAND (expr, 0),
2701 TREE_OPERAND (expr, 1),
2702 /* bitwise */1);
2704 if (TREE_CODE (expr) == BIT_AND_EXPR)
2706 tree op0 = TREE_OPERAND (expr, 0);
2707 tree op1 = TREE_OPERAND (expr, 1);
2708 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2709 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2711 /* If one of the operands is a non-negative constant
2712 that fits in the target type, then the type of the
2713 other operand does not matter. */
2714 if ((TREE_CODE (op0) == INTEGER_CST
2715 && int_fits_type_p (op0, c_common_signed_type (type))
2716 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2717 || (TREE_CODE (op1) == INTEGER_CST
2718 && int_fits_type_p (op1, c_common_signed_type (type))
2719 && int_fits_type_p (op1,
2720 c_common_unsigned_type (type))))
2721 return SAFE_CONVERSION;
2722 /* If constant is unsigned and fits in the target
2723 type, then the result will also fit. */
2724 else if ((TREE_CODE (op0) == INTEGER_CST
2725 && unsigned0
2726 && int_fits_type_p (op0, type))
2727 || (TREE_CODE (op1) == INTEGER_CST
2728 && unsigned1
2729 && int_fits_type_p (op1, type)))
2730 return SAFE_CONVERSION;
2733 /* Warn for integer types converted to smaller integer types. */
2734 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2735 give_warning = UNSAFE_OTHER;
2737 /* When they are the same width but different signedness,
2738 then the value may change. */
2739 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2740 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2741 /* Even when converted to a bigger type, if the type is
2742 unsigned but expr is signed, then negative values
2743 will be changed. */
2744 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2745 && produce_warns)
2746 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2747 "may change the sign of the result",
2748 type, expr_type);
2751 /* Warn for integer types converted to real types if and only if
2752 all the range of values of the integer type cannot be
2753 represented by the real type. */
2754 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2755 && TREE_CODE (type) == REAL_TYPE)
2757 tree type_low_bound, type_high_bound;
2758 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2760 /* Don't warn about char y = 0xff; float x = (int) y; */
2761 expr = get_unwidened (expr, 0);
2762 expr_type = TREE_TYPE (expr);
2764 type_low_bound = TYPE_MIN_VALUE (expr_type);
2765 type_high_bound = TYPE_MAX_VALUE (expr_type);
2766 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2767 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2769 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2770 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2771 give_warning = UNSAFE_OTHER;
2774 /* Warn for real types converted to smaller real types. */
2775 else if (TREE_CODE (expr_type) == REAL_TYPE
2776 && TREE_CODE (type) == REAL_TYPE
2777 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2778 give_warning = UNSAFE_REAL;
2781 return give_warning;
2784 /* Warns if the conversion of EXPR to TYPE may alter a value.
2785 This is a helper function for warnings_for_convert_and_check. */
2787 static void
2788 conversion_warning (location_t loc, tree type, tree expr)
2790 tree expr_type = TREE_TYPE (expr);
2791 enum conversion_safety conversion_kind;
2793 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2794 return;
2796 /* This may happen, because for LHS op= RHS we preevaluate
2797 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2798 means we could no longer see the code of the EXPR. */
2799 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2800 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2801 if (TREE_CODE (expr) == SAVE_EXPR)
2802 expr = TREE_OPERAND (expr, 0);
2804 switch (TREE_CODE (expr))
2806 case EQ_EXPR:
2807 case NE_EXPR:
2808 case LE_EXPR:
2809 case GE_EXPR:
2810 case LT_EXPR:
2811 case GT_EXPR:
2812 case TRUTH_ANDIF_EXPR:
2813 case TRUTH_ORIF_EXPR:
2814 case TRUTH_AND_EXPR:
2815 case TRUTH_OR_EXPR:
2816 case TRUTH_XOR_EXPR:
2817 case TRUTH_NOT_EXPR:
2818 /* Conversion from boolean to a signed:1 bit-field (which only
2819 can hold the values 0 and -1) doesn't lose information - but
2820 it does change the value. */
2821 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2822 warning_at (loc, OPT_Wconversion,
2823 "conversion to %qT from boolean expression", type);
2824 return;
2826 case REAL_CST:
2827 case INTEGER_CST:
2828 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2829 if (conversion_kind == UNSAFE_REAL)
2830 warning_at (loc, OPT_Wfloat_conversion,
2831 "conversion to %qT alters %qT constant value",
2832 type, expr_type);
2833 else if (conversion_kind)
2834 warning_at (loc, OPT_Wconversion,
2835 "conversion to %qT alters %qT constant value",
2836 type, expr_type);
2837 return;
2839 case COND_EXPR:
2841 /* In case of COND_EXPR, we do not care about the type of
2842 COND_EXPR, only about the conversion of each operand. */
2843 tree op1 = TREE_OPERAND (expr, 1);
2844 tree op2 = TREE_OPERAND (expr, 2);
2846 conversion_warning (loc, type, op1);
2847 conversion_warning (loc, type, op2);
2848 return;
2851 default: /* 'expr' is not a constant. */
2852 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2853 if (conversion_kind == UNSAFE_REAL)
2854 warning_at (loc, OPT_Wfloat_conversion,
2855 "conversion to %qT from %qT may alter its value",
2856 type, expr_type);
2857 else if (conversion_kind)
2858 warning_at (loc, OPT_Wconversion,
2859 "conversion to %qT from %qT may alter its value",
2860 type, expr_type);
2864 /* Produce warnings after a conversion. RESULT is the result of
2865 converting EXPR to TYPE. This is a helper function for
2866 convert_and_check and cp_convert_and_check. */
2868 void
2869 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2870 tree result)
2872 loc = expansion_point_location_if_in_system_header (loc);
2874 if (TREE_CODE (expr) == INTEGER_CST
2875 && (TREE_CODE (type) == INTEGER_TYPE
2876 || TREE_CODE (type) == ENUMERAL_TYPE)
2877 && !int_fits_type_p (expr, type))
2879 /* Do not diagnose overflow in a constant expression merely
2880 because a conversion overflowed. */
2881 if (TREE_OVERFLOW (result))
2882 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2884 if (TYPE_UNSIGNED (type))
2886 /* This detects cases like converting -129 or 256 to
2887 unsigned char. */
2888 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2889 warning_at (loc, OPT_Woverflow,
2890 "large integer implicitly truncated to unsigned type");
2891 else
2892 conversion_warning (loc, type, expr);
2894 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2895 warning_at (loc, OPT_Woverflow,
2896 "overflow in implicit constant conversion");
2897 /* No warning for converting 0x80000000 to int. */
2898 else if (pedantic
2899 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2900 || TYPE_PRECISION (TREE_TYPE (expr))
2901 != TYPE_PRECISION (type)))
2902 warning_at (loc, OPT_Woverflow,
2903 "overflow in implicit constant conversion");
2905 else
2906 conversion_warning (loc, type, expr);
2908 else if ((TREE_CODE (result) == INTEGER_CST
2909 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2910 warning_at (loc, OPT_Woverflow,
2911 "overflow in implicit constant conversion");
2912 else
2913 conversion_warning (loc, type, expr);
2917 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2918 Invoke this function on every expression that is converted implicitly,
2919 i.e. because of language rules and not because of an explicit cast. */
2921 tree
2922 convert_and_check (location_t loc, tree type, tree expr)
2924 tree result;
2925 tree expr_for_warning;
2927 /* Convert from a value with possible excess precision rather than
2928 via the semantic type, but do not warn about values not fitting
2929 exactly in the semantic type. */
2930 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2932 tree orig_type = TREE_TYPE (expr);
2933 expr = TREE_OPERAND (expr, 0);
2934 expr_for_warning = convert (orig_type, expr);
2935 if (orig_type == type)
2936 return expr_for_warning;
2938 else
2939 expr_for_warning = expr;
2941 if (TREE_TYPE (expr) == type)
2942 return expr;
2944 result = convert (type, expr);
2946 if (c_inhibit_evaluation_warnings == 0
2947 && !TREE_OVERFLOW_P (expr)
2948 && result != error_mark_node)
2949 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2951 return result;
2954 /* A node in a list that describes references to variables (EXPR), which are
2955 either read accesses if WRITER is zero, or write accesses, in which case
2956 WRITER is the parent of EXPR. */
2957 struct tlist
2959 struct tlist *next;
2960 tree expr, writer;
2963 /* Used to implement a cache the results of a call to verify_tree. We only
2964 use this for SAVE_EXPRs. */
2965 struct tlist_cache
2967 struct tlist_cache *next;
2968 struct tlist *cache_before_sp;
2969 struct tlist *cache_after_sp;
2970 tree expr;
2973 /* Obstack to use when allocating tlist structures, and corresponding
2974 firstobj. */
2975 static struct obstack tlist_obstack;
2976 static char *tlist_firstobj = 0;
2978 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2979 warnings. */
2980 static struct tlist *warned_ids;
2981 /* SAVE_EXPRs need special treatment. We process them only once and then
2982 cache the results. */
2983 static struct tlist_cache *save_expr_cache;
2985 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2986 static void merge_tlist (struct tlist **, struct tlist *, int);
2987 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2988 static int warning_candidate_p (tree);
2989 static bool candidate_equal_p (const_tree, const_tree);
2990 static void warn_for_collisions (struct tlist *);
2991 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2992 static struct tlist *new_tlist (struct tlist *, tree, tree);
2994 /* Create a new struct tlist and fill in its fields. */
2995 static struct tlist *
2996 new_tlist (struct tlist *next, tree t, tree writer)
2998 struct tlist *l;
2999 l = XOBNEW (&tlist_obstack, struct tlist);
3000 l->next = next;
3001 l->expr = t;
3002 l->writer = writer;
3003 return l;
3006 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3007 is nonnull, we ignore any node we find which has a writer equal to it. */
3009 static void
3010 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3012 while (add)
3014 struct tlist *next = add->next;
3015 if (!copy)
3016 add->next = *to;
3017 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3018 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3019 add = next;
3023 /* Merge the nodes of ADD into TO. This merging process is done so that for
3024 each variable that already exists in TO, no new node is added; however if
3025 there is a write access recorded in ADD, and an occurrence on TO is only
3026 a read access, then the occurrence in TO will be modified to record the
3027 write. */
3029 static void
3030 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3032 struct tlist **end = to;
3034 while (*end)
3035 end = &(*end)->next;
3037 while (add)
3039 int found = 0;
3040 struct tlist *tmp2;
3041 struct tlist *next = add->next;
3043 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3044 if (candidate_equal_p (tmp2->expr, add->expr))
3046 found = 1;
3047 if (!tmp2->writer)
3048 tmp2->writer = add->writer;
3050 if (!found)
3052 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3053 end = &(*end)->next;
3054 *end = 0;
3056 add = next;
3060 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3061 references in list LIST conflict with it, excluding reads if ONLY writers
3062 is nonzero. */
3064 static void
3065 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3066 int only_writes)
3068 struct tlist *tmp;
3070 /* Avoid duplicate warnings. */
3071 for (tmp = warned_ids; tmp; tmp = tmp->next)
3072 if (candidate_equal_p (tmp->expr, written))
3073 return;
3075 while (list)
3077 if (candidate_equal_p (list->expr, written)
3078 && !candidate_equal_p (list->writer, writer)
3079 && (!only_writes || list->writer))
3081 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3082 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3083 OPT_Wsequence_point, "operation on %qE may be undefined",
3084 list->expr);
3086 list = list->next;
3090 /* Given a list LIST of references to variables, find whether any of these
3091 can cause conflicts due to missing sequence points. */
3093 static void
3094 warn_for_collisions (struct tlist *list)
3096 struct tlist *tmp;
3098 for (tmp = list; tmp; tmp = tmp->next)
3100 if (tmp->writer)
3101 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3105 /* Return nonzero if X is a tree that can be verified by the sequence point
3106 warnings. */
3107 static int
3108 warning_candidate_p (tree x)
3110 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3111 return 0;
3113 if (TREE_CODE (x) == BLOCK)
3114 return 0;
3116 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3117 (lvalue_p) crash on TRY/CATCH. */
3118 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3119 return 0;
3121 if (!lvalue_p (x))
3122 return 0;
3124 /* No point to track non-const calls, they will never satisfy
3125 operand_equal_p. */
3126 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3127 return 0;
3129 if (TREE_CODE (x) == STRING_CST)
3130 return 0;
3132 return 1;
3135 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3136 static bool
3137 candidate_equal_p (const_tree x, const_tree y)
3139 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3142 /* Walk the tree X, and record accesses to variables. If X is written by the
3143 parent tree, WRITER is the parent.
3144 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3145 expression or its only operand forces a sequence point, then everything up
3146 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3147 in PNO_SP.
3148 Once we return, we will have emitted warnings if any subexpression before
3149 such a sequence point could be undefined. On a higher level, however, the
3150 sequence point may not be relevant, and we'll merge the two lists.
3152 Example: (b++, a) + b;
3153 The call that processes the COMPOUND_EXPR will store the increment of B
3154 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3155 processes the PLUS_EXPR will need to merge the two lists so that
3156 eventually, all accesses end up on the same list (and we'll warn about the
3157 unordered subexpressions b++ and b.
3159 A note on merging. If we modify the former example so that our expression
3160 becomes
3161 (b++, b) + a
3162 care must be taken not simply to add all three expressions into the final
3163 PNO_SP list. The function merge_tlist takes care of that by merging the
3164 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3165 way, so that no more than one access to B is recorded. */
3167 static void
3168 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3169 tree writer)
3171 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3172 enum tree_code code;
3173 enum tree_code_class cl;
3175 /* X may be NULL if it is the operand of an empty statement expression
3176 ({ }). */
3177 if (x == NULL)
3178 return;
3180 restart:
3181 code = TREE_CODE (x);
3182 cl = TREE_CODE_CLASS (code);
3184 if (warning_candidate_p (x))
3185 *pno_sp = new_tlist (*pno_sp, x, writer);
3187 switch (code)
3189 case CONSTRUCTOR:
3190 case SIZEOF_EXPR:
3191 return;
3193 case COMPOUND_EXPR:
3194 case TRUTH_ANDIF_EXPR:
3195 case TRUTH_ORIF_EXPR:
3196 tmp_before = tmp_nosp = tmp_list3 = 0;
3197 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3198 warn_for_collisions (tmp_nosp);
3199 merge_tlist (pbefore_sp, tmp_before, 0);
3200 merge_tlist (pbefore_sp, tmp_nosp, 0);
3201 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3202 merge_tlist (pbefore_sp, tmp_list3, 0);
3203 return;
3205 case COND_EXPR:
3206 tmp_before = tmp_list2 = 0;
3207 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3208 warn_for_collisions (tmp_list2);
3209 merge_tlist (pbefore_sp, tmp_before, 0);
3210 merge_tlist (pbefore_sp, tmp_list2, 0);
3212 tmp_list3 = tmp_nosp = 0;
3213 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3214 warn_for_collisions (tmp_nosp);
3215 merge_tlist (pbefore_sp, tmp_list3, 0);
3217 tmp_list3 = tmp_list2 = 0;
3218 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3219 warn_for_collisions (tmp_list2);
3220 merge_tlist (pbefore_sp, tmp_list3, 0);
3221 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3222 two first, to avoid warning for (a ? b++ : b++). */
3223 merge_tlist (&tmp_nosp, tmp_list2, 0);
3224 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3225 return;
3227 case PREDECREMENT_EXPR:
3228 case PREINCREMENT_EXPR:
3229 case POSTDECREMENT_EXPR:
3230 case POSTINCREMENT_EXPR:
3231 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3232 return;
3234 case MODIFY_EXPR:
3235 tmp_before = tmp_nosp = tmp_list3 = 0;
3236 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3237 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3238 /* Expressions inside the LHS are not ordered wrt. the sequence points
3239 in the RHS. Example:
3240 *a = (a++, 2)
3241 Despite the fact that the modification of "a" is in the before_sp
3242 list (tmp_before), it conflicts with the use of "a" in the LHS.
3243 We can handle this by adding the contents of tmp_list3
3244 to those of tmp_before, and redoing the collision warnings for that
3245 list. */
3246 add_tlist (&tmp_before, tmp_list3, x, 1);
3247 warn_for_collisions (tmp_before);
3248 /* Exclude the LHS itself here; we first have to merge it into the
3249 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3250 didn't exclude the LHS, we'd get it twice, once as a read and once
3251 as a write. */
3252 add_tlist (pno_sp, tmp_list3, x, 0);
3253 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3255 merge_tlist (pbefore_sp, tmp_before, 0);
3256 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3257 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3258 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3259 return;
3261 case CALL_EXPR:
3262 /* We need to warn about conflicts among arguments and conflicts between
3263 args and the function address. Side effects of the function address,
3264 however, are not ordered by the sequence point of the call. */
3266 call_expr_arg_iterator iter;
3267 tree arg;
3268 tmp_before = tmp_nosp = 0;
3269 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3270 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3272 tmp_list2 = tmp_list3 = 0;
3273 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3274 merge_tlist (&tmp_list3, tmp_list2, 0);
3275 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3277 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3278 warn_for_collisions (tmp_before);
3279 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3280 return;
3283 case TREE_LIST:
3284 /* Scan all the list, e.g. indices of multi dimensional array. */
3285 while (x)
3287 tmp_before = tmp_nosp = 0;
3288 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3289 merge_tlist (&tmp_nosp, tmp_before, 0);
3290 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3291 x = TREE_CHAIN (x);
3293 return;
3295 case SAVE_EXPR:
3297 struct tlist_cache *t;
3298 for (t = save_expr_cache; t; t = t->next)
3299 if (candidate_equal_p (t->expr, x))
3300 break;
3302 if (!t)
3304 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3305 t->next = save_expr_cache;
3306 t->expr = x;
3307 save_expr_cache = t;
3309 tmp_before = tmp_nosp = 0;
3310 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3311 warn_for_collisions (tmp_nosp);
3313 tmp_list3 = 0;
3314 merge_tlist (&tmp_list3, tmp_nosp, 0);
3315 t->cache_before_sp = tmp_before;
3316 t->cache_after_sp = tmp_list3;
3318 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3319 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3320 return;
3323 case ADDR_EXPR:
3324 x = TREE_OPERAND (x, 0);
3325 if (DECL_P (x))
3326 return;
3327 writer = 0;
3328 goto restart;
3330 default:
3331 /* For other expressions, simply recurse on their operands.
3332 Manual tail recursion for unary expressions.
3333 Other non-expressions need not be processed. */
3334 if (cl == tcc_unary)
3336 x = TREE_OPERAND (x, 0);
3337 writer = 0;
3338 goto restart;
3340 else if (IS_EXPR_CODE_CLASS (cl))
3342 int lp;
3343 int max = TREE_OPERAND_LENGTH (x);
3344 for (lp = 0; lp < max; lp++)
3346 tmp_before = tmp_nosp = 0;
3347 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3348 merge_tlist (&tmp_nosp, tmp_before, 0);
3349 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3352 return;
3356 /* Try to warn for undefined behavior in EXPR due to missing sequence
3357 points. */
3359 DEBUG_FUNCTION void
3360 verify_sequence_points (tree expr)
3362 struct tlist *before_sp = 0, *after_sp = 0;
3364 warned_ids = 0;
3365 save_expr_cache = 0;
3366 if (tlist_firstobj == 0)
3368 gcc_obstack_init (&tlist_obstack);
3369 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3372 verify_tree (expr, &before_sp, &after_sp, 0);
3373 warn_for_collisions (after_sp);
3374 obstack_free (&tlist_obstack, tlist_firstobj);
3377 /* Validate the expression after `case' and apply default promotions. */
3379 static tree
3380 check_case_value (location_t loc, tree value)
3382 if (value == NULL_TREE)
3383 return value;
3385 if (TREE_CODE (value) == INTEGER_CST)
3386 /* Promote char or short to int. */
3387 value = perform_integral_promotions (value);
3388 else if (value != error_mark_node)
3390 error_at (loc, "case label does not reduce to an integer constant");
3391 value = error_mark_node;
3394 constant_expression_warning (value);
3396 return value;
3399 /* See if the case values LOW and HIGH are in the range of the original
3400 type (i.e. before the default conversion to int) of the switch testing
3401 expression.
3402 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3403 the type before promoting it. CASE_LOW_P is a pointer to the lower
3404 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3405 if the case is not a case range.
3406 The caller has to make sure that we are not called with NULL for
3407 CASE_LOW_P (i.e. the default case).
3408 Returns true if the case label is in range of ORIG_TYPE (saturated or
3409 untouched) or false if the label is out of range. */
3411 static bool
3412 check_case_bounds (location_t loc, tree type, tree orig_type,
3413 tree *case_low_p, tree *case_high_p)
3415 tree min_value, max_value;
3416 tree case_low = *case_low_p;
3417 tree case_high = case_high_p ? *case_high_p : case_low;
3419 /* If there was a problem with the original type, do nothing. */
3420 if (orig_type == error_mark_node)
3421 return true;
3423 min_value = TYPE_MIN_VALUE (orig_type);
3424 max_value = TYPE_MAX_VALUE (orig_type);
3426 /* Case label is less than minimum for type. */
3427 if (tree_int_cst_compare (case_low, min_value) < 0
3428 && tree_int_cst_compare (case_high, min_value) < 0)
3430 warning_at (loc, 0, "case label value is less than minimum value "
3431 "for type");
3432 return false;
3435 /* Case value is greater than maximum for type. */
3436 if (tree_int_cst_compare (case_low, max_value) > 0
3437 && tree_int_cst_compare (case_high, max_value) > 0)
3439 warning_at (loc, 0, "case label value exceeds maximum value for type");
3440 return false;
3443 /* Saturate lower case label value to minimum. */
3444 if (tree_int_cst_compare (case_high, min_value) >= 0
3445 && tree_int_cst_compare (case_low, min_value) < 0)
3447 warning_at (loc, 0, "lower value in case label range"
3448 " less than minimum value for type");
3449 case_low = min_value;
3452 /* Saturate upper case label value to maximum. */
3453 if (tree_int_cst_compare (case_low, max_value) <= 0
3454 && tree_int_cst_compare (case_high, max_value) > 0)
3456 warning_at (loc, 0, "upper value in case label range"
3457 " exceeds maximum value for type");
3458 case_high = max_value;
3461 if (*case_low_p != case_low)
3462 *case_low_p = convert (type, case_low);
3463 if (case_high_p && *case_high_p != case_high)
3464 *case_high_p = convert (type, case_high);
3466 return true;
3469 /* Return an integer type with BITS bits of precision,
3470 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3472 tree
3473 c_common_type_for_size (unsigned int bits, int unsignedp)
3475 if (bits == TYPE_PRECISION (integer_type_node))
3476 return unsignedp ? unsigned_type_node : integer_type_node;
3478 if (bits == TYPE_PRECISION (signed_char_type_node))
3479 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3481 if (bits == TYPE_PRECISION (short_integer_type_node))
3482 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3484 if (bits == TYPE_PRECISION (long_integer_type_node))
3485 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3487 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3488 return (unsignedp ? long_long_unsigned_type_node
3489 : long_long_integer_type_node);
3491 if (int128_integer_type_node
3492 && bits == TYPE_PRECISION (int128_integer_type_node))
3493 return (unsignedp ? int128_unsigned_type_node
3494 : int128_integer_type_node);
3496 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3497 return (unsignedp ? widest_unsigned_literal_type_node
3498 : widest_integer_literal_type_node);
3500 if (bits <= TYPE_PRECISION (intQI_type_node))
3501 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3503 if (bits <= TYPE_PRECISION (intHI_type_node))
3504 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3506 if (bits <= TYPE_PRECISION (intSI_type_node))
3507 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3509 if (bits <= TYPE_PRECISION (intDI_type_node))
3510 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3512 return 0;
3515 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3516 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3517 and saturating if SATP is nonzero, otherwise not saturating. */
3519 tree
3520 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3521 int unsignedp, int satp)
3523 enum machine_mode mode;
3524 if (ibit == 0)
3525 mode = unsignedp ? UQQmode : QQmode;
3526 else
3527 mode = unsignedp ? UHAmode : HAmode;
3529 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3530 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3531 break;
3533 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3535 sorry ("GCC cannot support operators with integer types and "
3536 "fixed-point types that have too many integral and "
3537 "fractional bits together");
3538 return 0;
3541 return c_common_type_for_mode (mode, satp);
3544 /* Used for communication between c_common_type_for_mode and
3545 c_register_builtin_type. */
3546 tree registered_builtin_types;
3548 /* Return a data type that has machine mode MODE.
3549 If the mode is an integer,
3550 then UNSIGNEDP selects between signed and unsigned types.
3551 If the mode is a fixed-point mode,
3552 then UNSIGNEDP selects between saturating and nonsaturating types. */
3554 tree
3555 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3557 tree t;
3559 if (mode == TYPE_MODE (integer_type_node))
3560 return unsignedp ? unsigned_type_node : integer_type_node;
3562 if (mode == TYPE_MODE (signed_char_type_node))
3563 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3565 if (mode == TYPE_MODE (short_integer_type_node))
3566 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3568 if (mode == TYPE_MODE (long_integer_type_node))
3569 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3571 if (mode == TYPE_MODE (long_long_integer_type_node))
3572 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3574 if (int128_integer_type_node
3575 && mode == TYPE_MODE (int128_integer_type_node))
3576 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3578 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3579 return unsignedp ? widest_unsigned_literal_type_node
3580 : widest_integer_literal_type_node;
3582 if (mode == QImode)
3583 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3585 if (mode == HImode)
3586 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3588 if (mode == SImode)
3589 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3591 if (mode == DImode)
3592 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3594 #if HOST_BITS_PER_WIDE_INT >= 64
3595 if (mode == TYPE_MODE (intTI_type_node))
3596 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3597 #endif
3599 if (mode == TYPE_MODE (float_type_node))
3600 return float_type_node;
3602 if (mode == TYPE_MODE (double_type_node))
3603 return double_type_node;
3605 if (mode == TYPE_MODE (long_double_type_node))
3606 return long_double_type_node;
3608 if (mode == TYPE_MODE (void_type_node))
3609 return void_type_node;
3611 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3612 return (unsignedp
3613 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3614 : make_signed_type (GET_MODE_PRECISION (mode)));
3616 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3617 return (unsignedp
3618 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3619 : make_signed_type (GET_MODE_PRECISION (mode)));
3621 if (COMPLEX_MODE_P (mode))
3623 enum machine_mode inner_mode;
3624 tree inner_type;
3626 if (mode == TYPE_MODE (complex_float_type_node))
3627 return complex_float_type_node;
3628 if (mode == TYPE_MODE (complex_double_type_node))
3629 return complex_double_type_node;
3630 if (mode == TYPE_MODE (complex_long_double_type_node))
3631 return complex_long_double_type_node;
3633 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3634 return complex_integer_type_node;
3636 inner_mode = GET_MODE_INNER (mode);
3637 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3638 if (inner_type != NULL_TREE)
3639 return build_complex_type (inner_type);
3641 else if (VECTOR_MODE_P (mode))
3643 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3644 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3645 if (inner_type != NULL_TREE)
3646 return build_vector_type_for_mode (inner_type, mode);
3649 if (mode == TYPE_MODE (dfloat32_type_node))
3650 return dfloat32_type_node;
3651 if (mode == TYPE_MODE (dfloat64_type_node))
3652 return dfloat64_type_node;
3653 if (mode == TYPE_MODE (dfloat128_type_node))
3654 return dfloat128_type_node;
3656 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3658 if (mode == TYPE_MODE (short_fract_type_node))
3659 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3660 if (mode == TYPE_MODE (fract_type_node))
3661 return unsignedp ? sat_fract_type_node : fract_type_node;
3662 if (mode == TYPE_MODE (long_fract_type_node))
3663 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3664 if (mode == TYPE_MODE (long_long_fract_type_node))
3665 return unsignedp ? sat_long_long_fract_type_node
3666 : long_long_fract_type_node;
3668 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3669 return unsignedp ? sat_unsigned_short_fract_type_node
3670 : unsigned_short_fract_type_node;
3671 if (mode == TYPE_MODE (unsigned_fract_type_node))
3672 return unsignedp ? sat_unsigned_fract_type_node
3673 : unsigned_fract_type_node;
3674 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3675 return unsignedp ? sat_unsigned_long_fract_type_node
3676 : unsigned_long_fract_type_node;
3677 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3678 return unsignedp ? sat_unsigned_long_long_fract_type_node
3679 : unsigned_long_long_fract_type_node;
3681 if (mode == TYPE_MODE (short_accum_type_node))
3682 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3683 if (mode == TYPE_MODE (accum_type_node))
3684 return unsignedp ? sat_accum_type_node : accum_type_node;
3685 if (mode == TYPE_MODE (long_accum_type_node))
3686 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3687 if (mode == TYPE_MODE (long_long_accum_type_node))
3688 return unsignedp ? sat_long_long_accum_type_node
3689 : long_long_accum_type_node;
3691 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3692 return unsignedp ? sat_unsigned_short_accum_type_node
3693 : unsigned_short_accum_type_node;
3694 if (mode == TYPE_MODE (unsigned_accum_type_node))
3695 return unsignedp ? sat_unsigned_accum_type_node
3696 : unsigned_accum_type_node;
3697 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3698 return unsignedp ? sat_unsigned_long_accum_type_node
3699 : unsigned_long_accum_type_node;
3700 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3701 return unsignedp ? sat_unsigned_long_long_accum_type_node
3702 : unsigned_long_long_accum_type_node;
3704 if (mode == QQmode)
3705 return unsignedp ? sat_qq_type_node : qq_type_node;
3706 if (mode == HQmode)
3707 return unsignedp ? sat_hq_type_node : hq_type_node;
3708 if (mode == SQmode)
3709 return unsignedp ? sat_sq_type_node : sq_type_node;
3710 if (mode == DQmode)
3711 return unsignedp ? sat_dq_type_node : dq_type_node;
3712 if (mode == TQmode)
3713 return unsignedp ? sat_tq_type_node : tq_type_node;
3715 if (mode == UQQmode)
3716 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3717 if (mode == UHQmode)
3718 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3719 if (mode == USQmode)
3720 return unsignedp ? sat_usq_type_node : usq_type_node;
3721 if (mode == UDQmode)
3722 return unsignedp ? sat_udq_type_node : udq_type_node;
3723 if (mode == UTQmode)
3724 return unsignedp ? sat_utq_type_node : utq_type_node;
3726 if (mode == HAmode)
3727 return unsignedp ? sat_ha_type_node : ha_type_node;
3728 if (mode == SAmode)
3729 return unsignedp ? sat_sa_type_node : sa_type_node;
3730 if (mode == DAmode)
3731 return unsignedp ? sat_da_type_node : da_type_node;
3732 if (mode == TAmode)
3733 return unsignedp ? sat_ta_type_node : ta_type_node;
3735 if (mode == UHAmode)
3736 return unsignedp ? sat_uha_type_node : uha_type_node;
3737 if (mode == USAmode)
3738 return unsignedp ? sat_usa_type_node : usa_type_node;
3739 if (mode == UDAmode)
3740 return unsignedp ? sat_uda_type_node : uda_type_node;
3741 if (mode == UTAmode)
3742 return unsignedp ? sat_uta_type_node : uta_type_node;
3745 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3746 if (TYPE_MODE (TREE_VALUE (t)) == mode
3747 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3748 return TREE_VALUE (t);
3750 return 0;
3753 tree
3754 c_common_unsigned_type (tree type)
3756 return c_common_signed_or_unsigned_type (1, type);
3759 /* Return a signed type the same as TYPE in other respects. */
3761 tree
3762 c_common_signed_type (tree type)
3764 return c_common_signed_or_unsigned_type (0, type);
3767 /* Return a type the same as TYPE except unsigned or
3768 signed according to UNSIGNEDP. */
3770 tree
3771 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3773 tree type1;
3775 /* This block of code emulates the behavior of the old
3776 c_common_unsigned_type. In particular, it returns
3777 long_unsigned_type_node if passed a long, even when a int would
3778 have the same size. This is necessary for warnings to work
3779 correctly in archs where sizeof(int) == sizeof(long) */
3781 type1 = TYPE_MAIN_VARIANT (type);
3782 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3783 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3784 if (type1 == integer_type_node || type1 == unsigned_type_node)
3785 return unsignedp ? unsigned_type_node : integer_type_node;
3786 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3787 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3788 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3789 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3790 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3791 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3792 if (int128_integer_type_node
3793 && (type1 == int128_integer_type_node
3794 || type1 == int128_unsigned_type_node))
3795 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3796 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3797 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3798 #if HOST_BITS_PER_WIDE_INT >= 64
3799 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3800 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3801 #endif
3802 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3803 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3804 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3805 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3806 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3807 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3808 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3809 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3811 #define C_COMMON_FIXED_TYPES(NAME) \
3812 if (type1 == short_ ## NAME ## _type_node \
3813 || type1 == unsigned_short_ ## NAME ## _type_node) \
3814 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3815 : short_ ## NAME ## _type_node; \
3816 if (type1 == NAME ## _type_node \
3817 || type1 == unsigned_ ## NAME ## _type_node) \
3818 return unsignedp ? unsigned_ ## NAME ## _type_node \
3819 : NAME ## _type_node; \
3820 if (type1 == long_ ## NAME ## _type_node \
3821 || type1 == unsigned_long_ ## NAME ## _type_node) \
3822 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3823 : long_ ## NAME ## _type_node; \
3824 if (type1 == long_long_ ## NAME ## _type_node \
3825 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3826 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3827 : long_long_ ## NAME ## _type_node;
3829 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3830 if (type1 == NAME ## _type_node \
3831 || type1 == u ## NAME ## _type_node) \
3832 return unsignedp ? u ## NAME ## _type_node \
3833 : NAME ## _type_node;
3835 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3836 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3837 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3838 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3839 : sat_ ## short_ ## NAME ## _type_node; \
3840 if (type1 == sat_ ## NAME ## _type_node \
3841 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3842 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3843 : sat_ ## NAME ## _type_node; \
3844 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3845 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3846 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3847 : sat_ ## long_ ## NAME ## _type_node; \
3848 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3849 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3850 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3851 : sat_ ## long_long_ ## NAME ## _type_node;
3853 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3854 if (type1 == sat_ ## NAME ## _type_node \
3855 || type1 == sat_ ## u ## NAME ## _type_node) \
3856 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3857 : sat_ ## NAME ## _type_node;
3859 C_COMMON_FIXED_TYPES (fract);
3860 C_COMMON_FIXED_TYPES_SAT (fract);
3861 C_COMMON_FIXED_TYPES (accum);
3862 C_COMMON_FIXED_TYPES_SAT (accum);
3864 C_COMMON_FIXED_MODE_TYPES (qq);
3865 C_COMMON_FIXED_MODE_TYPES (hq);
3866 C_COMMON_FIXED_MODE_TYPES (sq);
3867 C_COMMON_FIXED_MODE_TYPES (dq);
3868 C_COMMON_FIXED_MODE_TYPES (tq);
3869 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3870 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3871 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3872 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3873 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3874 C_COMMON_FIXED_MODE_TYPES (ha);
3875 C_COMMON_FIXED_MODE_TYPES (sa);
3876 C_COMMON_FIXED_MODE_TYPES (da);
3877 C_COMMON_FIXED_MODE_TYPES (ta);
3878 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3879 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3880 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3881 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3883 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3884 the precision; they have precision set to match their range, but
3885 may use a wider mode to match an ABI. If we change modes, we may
3886 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3887 the precision as well, so as to yield correct results for
3888 bit-field types. C++ does not have these separate bit-field
3889 types, and producing a signed or unsigned variant of an
3890 ENUMERAL_TYPE may cause other problems as well. */
3892 if (!INTEGRAL_TYPE_P (type)
3893 || TYPE_UNSIGNED (type) == unsignedp)
3894 return type;
3896 #define TYPE_OK(node) \
3897 (TYPE_MODE (type) == TYPE_MODE (node) \
3898 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3899 if (TYPE_OK (signed_char_type_node))
3900 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3901 if (TYPE_OK (integer_type_node))
3902 return unsignedp ? unsigned_type_node : integer_type_node;
3903 if (TYPE_OK (short_integer_type_node))
3904 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3905 if (TYPE_OK (long_integer_type_node))
3906 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3907 if (TYPE_OK (long_long_integer_type_node))
3908 return (unsignedp ? long_long_unsigned_type_node
3909 : long_long_integer_type_node);
3910 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3911 return (unsignedp ? int128_unsigned_type_node
3912 : int128_integer_type_node);
3913 if (TYPE_OK (widest_integer_literal_type_node))
3914 return (unsignedp ? widest_unsigned_literal_type_node
3915 : widest_integer_literal_type_node);
3917 #if HOST_BITS_PER_WIDE_INT >= 64
3918 if (TYPE_OK (intTI_type_node))
3919 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3920 #endif
3921 if (TYPE_OK (intDI_type_node))
3922 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3923 if (TYPE_OK (intSI_type_node))
3924 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3925 if (TYPE_OK (intHI_type_node))
3926 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3927 if (TYPE_OK (intQI_type_node))
3928 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3929 #undef TYPE_OK
3931 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3934 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3936 tree
3937 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3939 /* Extended integer types of the same width as a standard type have
3940 lesser rank, so those of the same width as int promote to int or
3941 unsigned int and are valid for printf formats expecting int or
3942 unsigned int. To avoid such special cases, avoid creating
3943 extended integer types for bit-fields if a standard integer type
3944 is available. */
3945 if (width == TYPE_PRECISION (integer_type_node))
3946 return unsignedp ? unsigned_type_node : integer_type_node;
3947 if (width == TYPE_PRECISION (signed_char_type_node))
3948 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3949 if (width == TYPE_PRECISION (short_integer_type_node))
3950 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3951 if (width == TYPE_PRECISION (long_integer_type_node))
3952 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3953 if (width == TYPE_PRECISION (long_long_integer_type_node))
3954 return (unsignedp ? long_long_unsigned_type_node
3955 : long_long_integer_type_node);
3956 if (int128_integer_type_node
3957 && width == TYPE_PRECISION (int128_integer_type_node))
3958 return (unsignedp ? int128_unsigned_type_node
3959 : int128_integer_type_node);
3960 return build_nonstandard_integer_type (width, unsignedp);
3963 /* The C version of the register_builtin_type langhook. */
3965 void
3966 c_register_builtin_type (tree type, const char* name)
3968 tree decl;
3970 decl = build_decl (UNKNOWN_LOCATION,
3971 TYPE_DECL, get_identifier (name), type);
3972 DECL_ARTIFICIAL (decl) = 1;
3973 if (!TYPE_NAME (type))
3974 TYPE_NAME (type) = decl;
3975 pushdecl (decl);
3977 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3980 /* Print an error message for invalid operands to arith operation
3981 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3982 LOCATION is the location of the message. */
3984 void
3985 binary_op_error (location_t location, enum tree_code code,
3986 tree type0, tree type1)
3988 const char *opname;
3990 switch (code)
3992 case PLUS_EXPR:
3993 opname = "+"; break;
3994 case MINUS_EXPR:
3995 opname = "-"; break;
3996 case MULT_EXPR:
3997 opname = "*"; break;
3998 case MAX_EXPR:
3999 opname = "max"; break;
4000 case MIN_EXPR:
4001 opname = "min"; break;
4002 case EQ_EXPR:
4003 opname = "=="; break;
4004 case NE_EXPR:
4005 opname = "!="; break;
4006 case LE_EXPR:
4007 opname = "<="; break;
4008 case GE_EXPR:
4009 opname = ">="; break;
4010 case LT_EXPR:
4011 opname = "<"; break;
4012 case GT_EXPR:
4013 opname = ">"; break;
4014 case LSHIFT_EXPR:
4015 opname = "<<"; break;
4016 case RSHIFT_EXPR:
4017 opname = ">>"; break;
4018 case TRUNC_MOD_EXPR:
4019 case FLOOR_MOD_EXPR:
4020 opname = "%"; break;
4021 case TRUNC_DIV_EXPR:
4022 case FLOOR_DIV_EXPR:
4023 opname = "/"; break;
4024 case BIT_AND_EXPR:
4025 opname = "&"; break;
4026 case BIT_IOR_EXPR:
4027 opname = "|"; break;
4028 case TRUTH_ANDIF_EXPR:
4029 opname = "&&"; break;
4030 case TRUTH_ORIF_EXPR:
4031 opname = "||"; break;
4032 case BIT_XOR_EXPR:
4033 opname = "^"; break;
4034 default:
4035 gcc_unreachable ();
4037 error_at (location,
4038 "invalid operands to binary %s (have %qT and %qT)", opname,
4039 type0, type1);
4042 /* Given an expression as a tree, return its original type. Do this
4043 by stripping any conversion that preserves the sign and precision. */
4044 static tree
4045 expr_original_type (tree expr)
4047 STRIP_SIGN_NOPS (expr);
4048 return TREE_TYPE (expr);
4051 /* Subroutine of build_binary_op, used for comparison operations.
4052 See if the operands have both been converted from subword integer types
4053 and, if so, perhaps change them both back to their original type.
4054 This function is also responsible for converting the two operands
4055 to the proper common type for comparison.
4057 The arguments of this function are all pointers to local variables
4058 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4059 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4061 LOC is the location of the comparison.
4063 If this function returns nonzero, it means that the comparison has
4064 a constant value. What this function returns is an expression for
4065 that value. */
4067 tree
4068 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4069 tree *restype_ptr, enum tree_code *rescode_ptr)
4071 tree type;
4072 tree op0 = *op0_ptr;
4073 tree op1 = *op1_ptr;
4074 int unsignedp0, unsignedp1;
4075 int real1, real2;
4076 tree primop0, primop1;
4077 enum tree_code code = *rescode_ptr;
4079 /* Throw away any conversions to wider types
4080 already present in the operands. */
4082 primop0 = c_common_get_narrower (op0, &unsignedp0);
4083 primop1 = c_common_get_narrower (op1, &unsignedp1);
4085 /* If primopN is first sign-extended from primopN's precision to opN's
4086 precision, then zero-extended from opN's precision to
4087 *restype_ptr precision, shortenings might be invalid. */
4088 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4089 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4090 && !unsignedp0
4091 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4092 primop0 = op0;
4093 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4094 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4095 && !unsignedp1
4096 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4097 primop1 = op1;
4099 /* Handle the case that OP0 does not *contain* a conversion
4100 but it *requires* conversion to FINAL_TYPE. */
4102 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4103 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4104 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4105 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4107 /* If one of the operands must be floated, we cannot optimize. */
4108 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4109 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4111 /* If first arg is constant, swap the args (changing operation
4112 so value is preserved), for canonicalization. Don't do this if
4113 the second arg is 0. */
4115 if (TREE_CONSTANT (primop0)
4116 && !integer_zerop (primop1) && !real_zerop (primop1)
4117 && !fixed_zerop (primop1))
4119 tree tem = primop0;
4120 int temi = unsignedp0;
4121 primop0 = primop1;
4122 primop1 = tem;
4123 tem = op0;
4124 op0 = op1;
4125 op1 = tem;
4126 *op0_ptr = op0;
4127 *op1_ptr = op1;
4128 unsignedp0 = unsignedp1;
4129 unsignedp1 = temi;
4130 temi = real1;
4131 real1 = real2;
4132 real2 = temi;
4134 switch (code)
4136 case LT_EXPR:
4137 code = GT_EXPR;
4138 break;
4139 case GT_EXPR:
4140 code = LT_EXPR;
4141 break;
4142 case LE_EXPR:
4143 code = GE_EXPR;
4144 break;
4145 case GE_EXPR:
4146 code = LE_EXPR;
4147 break;
4148 default:
4149 break;
4151 *rescode_ptr = code;
4154 /* If comparing an integer against a constant more bits wide,
4155 maybe we can deduce a value of 1 or 0 independent of the data.
4156 Or else truncate the constant now
4157 rather than extend the variable at run time.
4159 This is only interesting if the constant is the wider arg.
4160 Also, it is not safe if the constant is unsigned and the
4161 variable arg is signed, since in this case the variable
4162 would be sign-extended and then regarded as unsigned.
4163 Our technique fails in this case because the lowest/highest
4164 possible unsigned results don't follow naturally from the
4165 lowest/highest possible values of the variable operand.
4166 For just EQ_EXPR and NE_EXPR there is another technique that
4167 could be used: see if the constant can be faithfully represented
4168 in the other operand's type, by truncating it and reextending it
4169 and see if that preserves the constant's value. */
4171 if (!real1 && !real2
4172 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4173 && TREE_CODE (primop1) == INTEGER_CST
4174 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4176 int min_gt, max_gt, min_lt, max_lt;
4177 tree maxval, minval;
4178 /* 1 if comparison is nominally unsigned. */
4179 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4180 tree val;
4182 type = c_common_signed_or_unsigned_type (unsignedp0,
4183 TREE_TYPE (primop0));
4185 maxval = TYPE_MAX_VALUE (type);
4186 minval = TYPE_MIN_VALUE (type);
4188 if (unsignedp && !unsignedp0)
4189 *restype_ptr = c_common_signed_type (*restype_ptr);
4191 if (TREE_TYPE (primop1) != *restype_ptr)
4193 /* Convert primop1 to target type, but do not introduce
4194 additional overflow. We know primop1 is an int_cst. */
4195 primop1 = force_fit_type (*restype_ptr,
4196 wide_int::from
4197 (primop1,
4198 TYPE_PRECISION (*restype_ptr),
4199 TYPE_SIGN (TREE_TYPE (primop1))),
4200 0, TREE_OVERFLOW (primop1));
4202 if (type != *restype_ptr)
4204 minval = convert (*restype_ptr, minval);
4205 maxval = convert (*restype_ptr, maxval);
4208 min_gt = tree_int_cst_lt (primop1, minval);
4209 max_gt = tree_int_cst_lt (primop1, maxval);
4210 min_lt = tree_int_cst_lt (minval, primop1);
4211 max_lt = tree_int_cst_lt (maxval, primop1);
4213 val = 0;
4214 /* This used to be a switch, but Genix compiler can't handle that. */
4215 if (code == NE_EXPR)
4217 if (max_lt || min_gt)
4218 val = truthvalue_true_node;
4220 else if (code == EQ_EXPR)
4222 if (max_lt || min_gt)
4223 val = truthvalue_false_node;
4225 else if (code == LT_EXPR)
4227 if (max_lt)
4228 val = truthvalue_true_node;
4229 if (!min_lt)
4230 val = truthvalue_false_node;
4232 else if (code == GT_EXPR)
4234 if (min_gt)
4235 val = truthvalue_true_node;
4236 if (!max_gt)
4237 val = truthvalue_false_node;
4239 else if (code == LE_EXPR)
4241 if (!max_gt)
4242 val = truthvalue_true_node;
4243 if (min_gt)
4244 val = truthvalue_false_node;
4246 else if (code == GE_EXPR)
4248 if (!min_lt)
4249 val = truthvalue_true_node;
4250 if (max_lt)
4251 val = truthvalue_false_node;
4254 /* If primop0 was sign-extended and unsigned comparison specd,
4255 we did a signed comparison above using the signed type bounds.
4256 But the comparison we output must be unsigned.
4258 Also, for inequalities, VAL is no good; but if the signed
4259 comparison had *any* fixed result, it follows that the
4260 unsigned comparison just tests the sign in reverse
4261 (positive values are LE, negative ones GE).
4262 So we can generate an unsigned comparison
4263 against an extreme value of the signed type. */
4265 if (unsignedp && !unsignedp0)
4267 if (val != 0)
4268 switch (code)
4270 case LT_EXPR:
4271 case GE_EXPR:
4272 primop1 = TYPE_MIN_VALUE (type);
4273 val = 0;
4274 break;
4276 case LE_EXPR:
4277 case GT_EXPR:
4278 primop1 = TYPE_MAX_VALUE (type);
4279 val = 0;
4280 break;
4282 default:
4283 break;
4285 type = c_common_unsigned_type (type);
4288 if (TREE_CODE (primop0) != INTEGER_CST)
4290 if (val == truthvalue_false_node)
4291 warning_at (loc, OPT_Wtype_limits,
4292 "comparison is always false due to limited range of data type");
4293 if (val == truthvalue_true_node)
4294 warning_at (loc, OPT_Wtype_limits,
4295 "comparison is always true due to limited range of data type");
4298 if (val != 0)
4300 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4301 if (TREE_SIDE_EFFECTS (primop0))
4302 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4303 return val;
4306 /* Value is not predetermined, but do the comparison
4307 in the type of the operand that is not constant.
4308 TYPE is already properly set. */
4311 /* If either arg is decimal float and the other is float, find the
4312 proper common type to use for comparison. */
4313 else if (real1 && real2
4314 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4315 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4316 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4318 else if (real1 && real2
4319 && (TYPE_PRECISION (TREE_TYPE (primop0))
4320 == TYPE_PRECISION (TREE_TYPE (primop1))))
4321 type = TREE_TYPE (primop0);
4323 /* If args' natural types are both narrower than nominal type
4324 and both extend in the same manner, compare them
4325 in the type of the wider arg.
4326 Otherwise must actually extend both to the nominal
4327 common type lest different ways of extending
4328 alter the result.
4329 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4331 else if (unsignedp0 == unsignedp1 && real1 == real2
4332 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4333 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4335 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4336 type = c_common_signed_or_unsigned_type (unsignedp0
4337 || TYPE_UNSIGNED (*restype_ptr),
4338 type);
4339 /* Make sure shorter operand is extended the right way
4340 to match the longer operand. */
4341 primop0
4342 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4343 TREE_TYPE (primop0)),
4344 primop0);
4345 primop1
4346 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4347 TREE_TYPE (primop1)),
4348 primop1);
4350 else
4352 /* Here we must do the comparison on the nominal type
4353 using the args exactly as we received them. */
4354 type = *restype_ptr;
4355 primop0 = op0;
4356 primop1 = op1;
4358 if (!real1 && !real2 && integer_zerop (primop1)
4359 && TYPE_UNSIGNED (*restype_ptr))
4361 tree value = 0;
4362 /* All unsigned values are >= 0, so we warn. However,
4363 if OP0 is a constant that is >= 0, the signedness of
4364 the comparison isn't an issue, so suppress the
4365 warning. */
4366 bool warn =
4367 warn_type_limits && !in_system_header_at (loc)
4368 && !(TREE_CODE (primop0) == INTEGER_CST
4369 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4370 primop0)))
4371 /* Do not warn for enumeration types. */
4372 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4374 switch (code)
4376 case GE_EXPR:
4377 if (warn)
4378 warning_at (loc, OPT_Wtype_limits,
4379 "comparison of unsigned expression >= 0 is always true");
4380 value = truthvalue_true_node;
4381 break;
4383 case LT_EXPR:
4384 if (warn)
4385 warning_at (loc, OPT_Wtype_limits,
4386 "comparison of unsigned expression < 0 is always false");
4387 value = truthvalue_false_node;
4388 break;
4390 default:
4391 break;
4394 if (value != 0)
4396 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4397 if (TREE_SIDE_EFFECTS (primop0))
4398 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4399 primop0, value);
4400 return value;
4405 *op0_ptr = convert (type, primop0);
4406 *op1_ptr = convert (type, primop1);
4408 *restype_ptr = truthvalue_type_node;
4410 return 0;
4413 /* Return a tree for the sum or difference (RESULTCODE says which)
4414 of pointer PTROP and integer INTOP. */
4416 tree
4417 pointer_int_sum (location_t loc, enum tree_code resultcode,
4418 tree ptrop, tree intop, bool complain)
4420 tree size_exp, ret;
4422 /* The result is a pointer of the same type that is being added. */
4423 tree result_type = TREE_TYPE (ptrop);
4425 /* If the pointer lives in UPC shared memory, then
4426 drop the 'shared' qualifier. */
4427 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4428 result_type = build_upc_unshared_type (result_type);
4430 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4432 if (complain && warn_pointer_arith)
4433 pedwarn (loc, OPT_Wpointer_arith,
4434 "pointer of type %<void *%> used in arithmetic");
4435 else if (!complain)
4436 return error_mark_node;
4437 size_exp = integer_one_node;
4439 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4441 if (complain && warn_pointer_arith)
4442 pedwarn (loc, OPT_Wpointer_arith,
4443 "pointer to a function used in arithmetic");
4444 else if (!complain)
4445 return error_mark_node;
4446 size_exp = integer_one_node;
4448 else
4449 size_exp = size_in_bytes (TREE_TYPE (result_type));
4451 /* We are manipulating pointer values, so we don't need to warn
4452 about relying on undefined signed overflow. We disable the
4453 warning here because we use integer types so fold won't know that
4454 they are really pointers. */
4455 fold_defer_overflow_warnings ();
4457 /* If what we are about to multiply by the size of the elements
4458 contains a constant term, apply distributive law
4459 and multiply that constant term separately.
4460 This helps produce common subexpressions. */
4461 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4462 && !TREE_CONSTANT (intop)
4463 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4464 && TREE_CONSTANT (size_exp)
4465 /* If the constant comes from pointer subtraction,
4466 skip this optimization--it would cause an error. */
4467 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4468 /* If the constant is unsigned, and smaller than the pointer size,
4469 then we must skip this optimization. This is because it could cause
4470 an overflow error if the constant is negative but INTOP is not. */
4471 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4472 || (TYPE_PRECISION (TREE_TYPE (intop))
4473 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4475 enum tree_code subcode = resultcode;
4476 tree int_type = TREE_TYPE (intop);
4477 if (TREE_CODE (intop) == MINUS_EXPR)
4478 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4479 /* Convert both subexpression types to the type of intop,
4480 because weird cases involving pointer arithmetic
4481 can result in a sum or difference with different type args. */
4482 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4483 subcode, ptrop,
4484 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4485 intop = convert (int_type, TREE_OPERAND (intop, 0));
4488 /* Convert the integer argument to a type the same size as sizetype
4489 so the multiply won't overflow spuriously. */
4490 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4491 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4492 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4493 TYPE_UNSIGNED (sizetype)), intop);
4495 /* Replace the integer argument with a suitable product by the object size.
4496 Do this multiplication as signed, then convert to the appropriate type
4497 for the pointer operation and disregard an overflow that occurred only
4498 because of the sign-extension change in the latter conversion. */
4500 tree t = build_binary_op (loc,
4501 MULT_EXPR, intop,
4502 convert (TREE_TYPE (intop), size_exp), 1);
4503 intop = convert (sizetype, t);
4504 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4505 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4508 /* Create the sum or difference. */
4509 if (resultcode == MINUS_EXPR)
4510 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4512 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4514 fold_undefer_and_ignore_overflow_warnings ();
4516 return ret;
4519 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4520 and if NON_CONST is known not to be permitted in an evaluated part
4521 of a constant expression. */
4523 tree
4524 c_wrap_maybe_const (tree expr, bool non_const)
4526 bool nowarning = TREE_NO_WARNING (expr);
4527 location_t loc = EXPR_LOCATION (expr);
4529 /* This should never be called for C++. */
4530 if (c_dialect_cxx ())
4531 gcc_unreachable ();
4533 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4534 STRIP_TYPE_NOPS (expr);
4535 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4536 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4537 if (nowarning)
4538 TREE_NO_WARNING (expr) = 1;
4539 protected_set_expr_location (expr, loc);
4541 return expr;
4544 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4545 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4546 around the SAVE_EXPR if needed so that c_fully_fold does not need
4547 to look inside SAVE_EXPRs. */
4549 tree
4550 c_save_expr (tree expr)
4552 bool maybe_const = true;
4553 if (c_dialect_cxx ())
4554 return save_expr (expr);
4555 expr = c_fully_fold (expr, false, &maybe_const);
4556 expr = save_expr (expr);
4557 if (!maybe_const)
4558 expr = c_wrap_maybe_const (expr, true);
4559 return expr;
4562 /* Return whether EXPR is a declaration whose address can never be
4563 NULL. */
4565 bool
4566 decl_with_nonnull_addr_p (const_tree expr)
4568 return (DECL_P (expr)
4569 && (TREE_CODE (expr) == PARM_DECL
4570 || TREE_CODE (expr) == LABEL_DECL
4571 || !DECL_WEAK (expr)));
4574 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4575 or for an `if' or `while' statement or ?..: exp. It should already
4576 have been validated to be of suitable type; otherwise, a bad
4577 diagnostic may result.
4579 The EXPR is located at LOCATION.
4581 This preparation consists of taking the ordinary
4582 representation of an expression expr and producing a valid tree
4583 boolean expression describing whether expr is nonzero. We could
4584 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4585 but we optimize comparisons, &&, ||, and !.
4587 The resulting type should always be `truthvalue_type_node'. */
4589 tree
4590 c_common_truthvalue_conversion (location_t location, tree expr)
4592 switch (TREE_CODE (expr))
4594 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4595 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4596 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4597 case ORDERED_EXPR: case UNORDERED_EXPR:
4598 if (TREE_TYPE (expr) == truthvalue_type_node)
4599 return expr;
4600 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4601 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4602 goto ret;
4604 case TRUTH_ANDIF_EXPR:
4605 case TRUTH_ORIF_EXPR:
4606 case TRUTH_AND_EXPR:
4607 case TRUTH_OR_EXPR:
4608 case TRUTH_XOR_EXPR:
4609 if (TREE_TYPE (expr) == truthvalue_type_node)
4610 return expr;
4611 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4612 c_common_truthvalue_conversion (location,
4613 TREE_OPERAND (expr, 0)),
4614 c_common_truthvalue_conversion (location,
4615 TREE_OPERAND (expr, 1)));
4616 goto ret;
4618 case TRUTH_NOT_EXPR:
4619 if (TREE_TYPE (expr) == truthvalue_type_node)
4620 return expr;
4621 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4622 c_common_truthvalue_conversion (location,
4623 TREE_OPERAND (expr, 0)));
4624 goto ret;
4626 case ERROR_MARK:
4627 return expr;
4629 case INTEGER_CST:
4630 return integer_zerop (expr) ? truthvalue_false_node
4631 : truthvalue_true_node;
4633 case REAL_CST:
4634 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4635 ? truthvalue_true_node
4636 : truthvalue_false_node;
4638 case FIXED_CST:
4639 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4640 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4641 ? truthvalue_true_node
4642 : truthvalue_false_node;
4644 case FUNCTION_DECL:
4645 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4646 /* Fall through. */
4648 case ADDR_EXPR:
4650 tree inner = TREE_OPERAND (expr, 0);
4651 if (decl_with_nonnull_addr_p (inner))
4653 /* Common Ada/Pascal programmer's mistake. */
4654 warning_at (location,
4655 OPT_Waddress,
4656 "the address of %qD will always evaluate as %<true%>",
4657 inner);
4658 return truthvalue_true_node;
4660 break;
4663 case COMPLEX_EXPR:
4664 expr = build_binary_op (EXPR_LOCATION (expr),
4665 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4666 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4667 c_common_truthvalue_conversion (location,
4668 TREE_OPERAND (expr, 0)),
4669 c_common_truthvalue_conversion (location,
4670 TREE_OPERAND (expr, 1)),
4672 goto ret;
4674 case NEGATE_EXPR:
4675 case ABS_EXPR:
4676 case FLOAT_EXPR:
4677 case EXCESS_PRECISION_EXPR:
4678 /* These don't change whether an object is nonzero or zero. */
4679 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4681 case LROTATE_EXPR:
4682 case RROTATE_EXPR:
4683 /* These don't change whether an object is zero or nonzero, but
4684 we can't ignore them if their second arg has side-effects. */
4685 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4687 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4688 TREE_OPERAND (expr, 1),
4689 c_common_truthvalue_conversion
4690 (location, TREE_OPERAND (expr, 0)));
4691 goto ret;
4693 else
4694 return c_common_truthvalue_conversion (location,
4695 TREE_OPERAND (expr, 0));
4697 case COND_EXPR:
4698 /* Distribute the conversion into the arms of a COND_EXPR. */
4699 if (c_dialect_cxx ())
4701 tree op1 = TREE_OPERAND (expr, 1);
4702 tree op2 = TREE_OPERAND (expr, 2);
4703 /* In C++ one of the arms might have void type if it is throw. */
4704 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4705 op1 = c_common_truthvalue_conversion (location, op1);
4706 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4707 op2 = c_common_truthvalue_conversion (location, op2);
4708 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4709 TREE_OPERAND (expr, 0), op1, op2);
4710 goto ret;
4712 else
4714 /* Folding will happen later for C. */
4715 expr = build3 (COND_EXPR, truthvalue_type_node,
4716 TREE_OPERAND (expr, 0),
4717 c_common_truthvalue_conversion (location,
4718 TREE_OPERAND (expr, 1)),
4719 c_common_truthvalue_conversion (location,
4720 TREE_OPERAND (expr, 2)));
4721 goto ret;
4724 CASE_CONVERT:
4726 tree totype = TREE_TYPE (expr);
4727 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4729 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4730 since that affects how `default_conversion' will behave. */
4731 if (TREE_CODE (totype) == REFERENCE_TYPE
4732 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4733 break;
4734 /* Don't strip a conversion from C++0x scoped enum, since they
4735 don't implicitly convert to other types. */
4736 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4737 && ENUM_IS_SCOPED (fromtype))
4738 break;
4739 /* If this isn't narrowing the argument, we can ignore it. */
4740 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4741 return c_common_truthvalue_conversion (location,
4742 TREE_OPERAND (expr, 0));
4744 break;
4746 case MODIFY_EXPR:
4747 if (!TREE_NO_WARNING (expr)
4748 && warn_parentheses)
4750 warning (OPT_Wparentheses,
4751 "suggest parentheses around assignment used as truth value");
4752 TREE_NO_WARNING (expr) = 1;
4754 break;
4756 default:
4757 break;
4760 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4762 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4763 expr = (build_binary_op
4764 (EXPR_LOCATION (expr),
4765 (TREE_SIDE_EFFECTS (expr)
4766 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4767 c_common_truthvalue_conversion
4768 (location,
4769 build_unary_op (location, REALPART_EXPR, t, 0)),
4770 c_common_truthvalue_conversion
4771 (location,
4772 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4773 0));
4774 goto ret;
4777 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4779 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4780 FCONST0 (TYPE_MODE
4781 (TREE_TYPE (expr))));
4782 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4784 else
4785 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4787 ret:
4788 protected_set_expr_location (expr, location);
4789 return expr;
4792 static void def_builtin_1 (enum built_in_function fncode,
4793 const char *name,
4794 enum built_in_class fnclass,
4795 tree fntype, tree libtype,
4796 bool both_p, bool fallback_p, bool nonansi_p,
4797 tree fnattrs, bool implicit_p);
4800 /* Apply the TYPE_QUALS to the new DECL. */
4802 void
4803 c_apply_type_quals_to_decl (int type_quals, tree decl)
4805 tree type = TREE_TYPE (decl);
4807 if (type == error_mark_node)
4808 return;
4810 if ((type_quals & TYPE_QUAL_CONST)
4811 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4812 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4813 constructor can produce constant init, so rely on cp_finish_decl to
4814 clear TREE_READONLY if the variable has non-constant init. */
4815 TREE_READONLY (decl) = 1;
4816 if (type_quals & TYPE_QUAL_VOLATILE)
4818 TREE_SIDE_EFFECTS (decl) = 1;
4819 TREE_THIS_VOLATILE (decl) = 1;
4821 if (type_quals & TYPE_QUAL_RESTRICT)
4823 while (type && TREE_CODE (type) == ARRAY_TYPE)
4824 /* Allow 'restrict' on arrays of pointers.
4825 FIXME currently we just ignore it. */
4826 type = TREE_TYPE (type);
4827 if (!type
4828 || !POINTER_TYPE_P (type)
4829 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4830 error ("invalid use of %<restrict%>");
4832 if (type_quals & TYPE_QUAL_UPC_SHARED)
4834 TREE_SHARED (decl) = 1;
4835 if (type_quals & TYPE_QUAL_UPC_STRICT)
4836 TREE_STRICT(decl) = 1;
4837 else if (type_quals & TYPE_QUAL_UPC_RELAXED)
4838 TREE_RELAXED(decl) = 1;
4839 /* The declaration's type should have been previously defined
4840 as a UPC shared type. */
4841 gcc_assert (upc_shared_type_p (type));
4845 /* Hash function for the problem of multiple type definitions in
4846 different files. This must hash all types that will compare
4847 equal via comptypes to the same value. In practice it hashes
4848 on some of the simple stuff and leaves the details to comptypes. */
4850 static hashval_t
4851 c_type_hash (const void *p)
4853 int n_elements;
4854 int shift, size;
4855 const_tree const t = (const_tree) p;
4856 tree t2;
4857 switch (TREE_CODE (t))
4859 /* For pointers, hash on pointee type plus some swizzling. */
4860 case POINTER_TYPE:
4861 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4862 /* Hash on number of elements and total size. */
4863 case ENUMERAL_TYPE:
4864 shift = 3;
4865 t2 = TYPE_VALUES (t);
4866 break;
4867 case RECORD_TYPE:
4868 shift = 0;
4869 t2 = TYPE_FIELDS (t);
4870 break;
4871 case QUAL_UNION_TYPE:
4872 shift = 1;
4873 t2 = TYPE_FIELDS (t);
4874 break;
4875 case UNION_TYPE:
4876 shift = 2;
4877 t2 = TYPE_FIELDS (t);
4878 break;
4879 default:
4880 gcc_unreachable ();
4882 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4883 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4884 n_elements = list_length (t2);
4885 /* We might have a VLA here. */
4886 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4887 size = 0;
4888 else
4889 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4890 return ((size << 24) | (n_elements << shift));
4893 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4895 /* Return the typed-based alias set for T, which may be an expression
4896 or a type. Return -1 if we don't do anything special. */
4898 alias_set_type
4899 c_common_get_alias_set (tree t)
4901 tree u;
4902 PTR *slot;
4904 /* For VLAs, use the alias set of the element type rather than the
4905 default of alias set 0 for types compared structurally. */
4906 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4908 if (TREE_CODE (t) == ARRAY_TYPE)
4909 return get_alias_set (TREE_TYPE (t));
4910 return -1;
4913 /* Permit type-punning when accessing a union, provided the access
4914 is directly through the union. For example, this code does not
4915 permit taking the address of a union member and then storing
4916 through it. Even the type-punning allowed here is a GCC
4917 extension, albeit a common and useful one; the C standard says
4918 that such accesses have implementation-defined behavior. */
4919 for (u = t;
4920 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4921 u = TREE_OPERAND (u, 0))
4922 if (TREE_CODE (u) == COMPONENT_REF
4923 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4924 return 0;
4926 /* That's all the expressions we handle specially. */
4927 if (!TYPE_P (t))
4928 return -1;
4930 /* The C standard guarantees that any object may be accessed via an
4931 lvalue that has character type. */
4932 if (t == char_type_node
4933 || t == signed_char_type_node
4934 || t == unsigned_char_type_node)
4935 return 0;
4937 /* The C standard specifically allows aliasing between signed and
4938 unsigned variants of the same type. We treat the signed
4939 variant as canonical. */
4940 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4942 tree t1 = c_common_signed_type (t);
4944 /* t1 == t can happen for boolean nodes which are always unsigned. */
4945 if (t1 != t)
4946 return get_alias_set (t1);
4949 /* For the time being, make UPC pointers-to-shared conflict
4950 with everything else. Ideally, UPC pointers-to-shared should
4951 only conflict with the internal type used to represent
4952 the UPC pointer-to-shared (i.e., upc_pts_rep_type_node). */
4954 if (TYPE_P (t) ? (TREE_CODE (t) == POINTER_TYPE
4955 && upc_shared_type_p (TREE_TYPE (t)))
4956 : (TREE_TYPE(t)
4957 && TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
4958 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (t)))))
4959 return 0;
4961 /* Handle the case of multiple type nodes referring to "the same" type,
4962 which occurs with IMA. These share an alias set. FIXME: Currently only
4963 C90 is handled. (In C99 type compatibility is not transitive, which
4964 complicates things mightily. The alias set splay trees can theoretically
4965 represent this, but insertion is tricky when you consider all the
4966 different orders things might arrive in.) */
4968 if (c_language != clk_c || flag_isoc99)
4969 return -1;
4971 /* Save time if there's only one input file. */
4972 if (num_in_fnames == 1)
4973 return -1;
4975 /* Pointers need special handling if they point to any type that
4976 needs special handling (below). */
4977 if (TREE_CODE (t) == POINTER_TYPE)
4979 tree t2;
4980 /* Find bottom type under any nested POINTERs. */
4981 for (t2 = TREE_TYPE (t);
4982 TREE_CODE (t2) == POINTER_TYPE;
4983 t2 = TREE_TYPE (t2))
4985 if (TREE_CODE (t2) != RECORD_TYPE
4986 && TREE_CODE (t2) != ENUMERAL_TYPE
4987 && TREE_CODE (t2) != QUAL_UNION_TYPE
4988 && TREE_CODE (t2) != UNION_TYPE)
4989 return -1;
4990 if (TYPE_SIZE (t2) == 0)
4991 return -1;
4993 /* These are the only cases that need special handling. */
4994 if (TREE_CODE (t) != RECORD_TYPE
4995 && TREE_CODE (t) != ENUMERAL_TYPE
4996 && TREE_CODE (t) != QUAL_UNION_TYPE
4997 && TREE_CODE (t) != UNION_TYPE
4998 && TREE_CODE (t) != POINTER_TYPE)
4999 return -1;
5000 /* Undefined? */
5001 if (TYPE_SIZE (t) == 0)
5002 return -1;
5004 /* Look up t in hash table. Only one of the compatible types within each
5005 alias set is recorded in the table. */
5006 if (!type_hash_table)
5007 type_hash_table = htab_create_ggc (1021, c_type_hash,
5008 (htab_eq) lang_hooks.types_compatible_p,
5009 NULL);
5010 slot = htab_find_slot (type_hash_table, t, INSERT);
5011 if (*slot != NULL)
5013 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5014 return TYPE_ALIAS_SET ((tree)*slot);
5016 else
5017 /* Our caller will assign and record (in t) a new alias set; all we need
5018 to do is remember t in the hash table. */
5019 *slot = t;
5021 return -1;
5024 /* Return the value of THREADS.
5026 UPC defines a reserved variable, THREADS, which returns the
5027 number of threads that will be created when the UPC program
5028 executes. The value of threads can be specified at runtime via
5029 the -fupc-threads=N switch, where N is an integer specifying
5030 the number of threads. When the value of THREADS is specified
5031 at compile-time, this is called the "static threads compilation
5032 environment".
5034 In the static threads compilation environment, THREADS is a
5035 pre-defined preprocessor macro with the value, N.
5037 If no value for threads is given at compile-time, then the value
5038 must be specified when the application program is executed.
5039 This method of establishing the value of THREADS is called
5040 the "dynamic threads compilation environment". */
5042 tree
5043 upc_num_threads (void)
5045 tree n;
5046 gcc_assert (flag_upc);
5047 n = flag_upc_threads ? ssize_int (flag_upc_threads)
5048 : lookup_name (get_identifier ("THREADS"));
5049 if (!n)
5051 error ("the UPC-required THREADS variable is undefined; "
5052 "when compiling pre-processed source, "
5053 "all -fupc-* switches must be passed on the command line, "
5054 "asserting the same values as supplied when the "
5055 "original source file was preprocessed");
5056 abort ();
5059 return n;
5062 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5063 the IS_SIZEOF parameter indicates which operator is being applied.
5064 The COMPLAIN flag controls whether we should diagnose possibly
5065 ill-formed constructs or not. LOC is the location of the SIZEOF or
5066 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5067 a type in any context should be returned, rather than the normal
5068 alignment for that type. */
5070 tree
5071 c_sizeof_or_alignof_type (location_t loc,
5072 tree type, bool is_sizeof, bool min_alignof,
5073 int complain)
5075 const char *op_name;
5076 tree value = NULL;
5077 enum tree_code type_code = TREE_CODE (type);
5079 op_name = is_sizeof ? "sizeof" : "__alignof__";
5081 if (type_code == FUNCTION_TYPE)
5083 if (is_sizeof)
5085 if (complain && warn_pointer_arith)
5086 pedwarn (loc, OPT_Wpointer_arith,
5087 "invalid application of %<sizeof%> to a function type");
5088 else if (!complain)
5089 return error_mark_node;
5090 value = size_one_node;
5092 else
5094 if (complain)
5096 if (c_dialect_cxx ())
5097 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5098 "%<alignof%> applied to a function type");
5099 else
5100 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5101 "%<_Alignof%> applied to a function type");
5103 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5106 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5108 if (complain)
5110 if (type_code == VOID_TYPE && upc_shared_type_p (type))
5111 error_at (loc, "invalid application of %qs"
5112 " to %<shared void%> type", op_name);
5113 else if (type_code == VOID_TYPE && warn_pointer_arith)
5114 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
5115 "invalid application of %qs to a void type", op_name);
5117 else
5118 return error_mark_node;
5119 value = size_one_node;
5121 else if (!COMPLETE_TYPE_P (type)
5122 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5124 if (complain)
5125 error_at (loc, "invalid application of %qs to incomplete type %qT",
5126 op_name, type);
5127 return error_mark_node;
5129 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5130 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5132 if (complain)
5133 error_at (loc, "invalid application of %qs to array type %qT of "
5134 "incomplete element type", op_name, type);
5135 return error_mark_node;
5137 else
5139 if (is_sizeof)
5140 /* Convert in case a char is more than one unit. */
5141 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5142 size_int (TYPE_PRECISION (char_type_node)
5143 / BITS_PER_UNIT));
5144 else if (min_alignof)
5145 value = size_int (min_align_of_type (type));
5146 else
5147 value = size_int (TYPE_ALIGN_UNIT (type));
5150 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
5151 && upc_shared_type_p (type)
5152 && TYPE_HAS_THREADS_FACTOR (type))
5154 const tree n_threads = convert (sizetype, upc_num_threads ());
5155 value = size_binop (MULT_EXPR, value, n_threads);
5158 /* VALUE will have the middle-end integer type sizetype.
5159 However, we should really return a value of type `size_t',
5160 which is just a typedef for an ordinary integer type. */
5161 value = fold_convert_loc (loc, size_type_node, value);
5163 return value;
5166 /* Implement the __alignof keyword: Return the minimum required
5167 alignment of EXPR, measured in bytes. For VAR_DECLs,
5168 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5169 from an "aligned" __attribute__ specification). LOC is the
5170 location of the ALIGNOF operator. */
5172 tree
5173 c_alignof_expr (location_t loc, tree expr)
5175 tree t;
5177 if (VAR_OR_FUNCTION_DECL_P (expr))
5178 t = size_int (DECL_ALIGN_UNIT (expr));
5180 else if (TREE_CODE (expr) == COMPONENT_REF
5181 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5183 error_at (loc, "%<__alignof%> applied to a bit-field");
5184 t = size_one_node;
5186 else if (TREE_CODE (expr) == COMPONENT_REF
5187 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5188 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5190 else if (TREE_CODE (expr) == INDIRECT_REF)
5192 tree t = TREE_OPERAND (expr, 0);
5193 tree best = t;
5194 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5196 while (CONVERT_EXPR_P (t)
5197 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5199 int thisalign;
5201 t = TREE_OPERAND (t, 0);
5202 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5203 if (thisalign > bestalign)
5204 best = t, bestalign = thisalign;
5206 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5208 else
5209 return c_alignof (loc, TREE_TYPE (expr));
5211 return fold_convert_loc (loc, size_type_node, t);
5214 /* Handle C and C++ default attributes. */
5216 enum built_in_attribute
5218 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5219 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5220 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5221 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5222 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5223 #include "builtin-attrs.def"
5224 #undef DEF_ATTR_NULL_TREE
5225 #undef DEF_ATTR_INT
5226 #undef DEF_ATTR_STRING
5227 #undef DEF_ATTR_IDENT
5228 #undef DEF_ATTR_TREE_LIST
5229 ATTR_LAST
5232 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5234 static void c_init_attributes (void);
5236 enum c_builtin_type
5238 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5239 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5240 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5241 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5242 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5243 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5244 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5245 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5246 ARG6) NAME,
5247 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5248 ARG6, ARG7) NAME,
5249 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5250 ARG6, ARG7, ARG8) NAME,
5251 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5252 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5253 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5254 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5255 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5256 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5257 NAME,
5258 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5259 #include "builtin-types.def"
5260 #undef DEF_PRIMITIVE_TYPE
5261 #undef DEF_FUNCTION_TYPE_0
5262 #undef DEF_FUNCTION_TYPE_1
5263 #undef DEF_FUNCTION_TYPE_2
5264 #undef DEF_FUNCTION_TYPE_3
5265 #undef DEF_FUNCTION_TYPE_4
5266 #undef DEF_FUNCTION_TYPE_5
5267 #undef DEF_FUNCTION_TYPE_6
5268 #undef DEF_FUNCTION_TYPE_7
5269 #undef DEF_FUNCTION_TYPE_8
5270 #undef DEF_FUNCTION_TYPE_VAR_0
5271 #undef DEF_FUNCTION_TYPE_VAR_1
5272 #undef DEF_FUNCTION_TYPE_VAR_2
5273 #undef DEF_FUNCTION_TYPE_VAR_3
5274 #undef DEF_FUNCTION_TYPE_VAR_4
5275 #undef DEF_FUNCTION_TYPE_VAR_5
5276 #undef DEF_POINTER_TYPE
5277 BT_LAST
5280 typedef enum c_builtin_type builtin_type;
5282 /* A temporary array for c_common_nodes_and_builtins. Used in
5283 communication with def_fn_type. */
5284 static tree builtin_types[(int) BT_LAST + 1];
5286 /* A helper function for c_common_nodes_and_builtins. Build function type
5287 for DEF with return type RET and N arguments. If VAR is true, then the
5288 function should be variadic after those N arguments.
5290 Takes special care not to ICE if any of the types involved are
5291 error_mark_node, which indicates that said type is not in fact available
5292 (see builtin_type_for_size). In which case the function type as a whole
5293 should be error_mark_node. */
5295 static void
5296 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5298 tree t;
5299 tree *args = XALLOCAVEC (tree, n);
5300 va_list list;
5301 int i;
5303 va_start (list, n);
5304 for (i = 0; i < n; ++i)
5306 builtin_type a = (builtin_type) va_arg (list, int);
5307 t = builtin_types[a];
5308 if (t == error_mark_node)
5309 goto egress;
5310 args[i] = t;
5313 t = builtin_types[ret];
5314 if (t == error_mark_node)
5315 goto egress;
5316 if (var)
5317 t = build_varargs_function_type_array (t, n, args);
5318 else
5319 t = build_function_type_array (t, n, args);
5321 egress:
5322 builtin_types[def] = t;
5323 va_end (list);
5326 /* Build builtin functions common to both C and C++ language
5327 frontends. */
5329 static void
5330 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5332 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5333 builtin_types[ENUM] = VALUE;
5334 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5335 def_fn_type (ENUM, RETURN, 0, 0);
5336 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5337 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5338 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5339 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5340 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5341 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5342 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5343 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5344 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5345 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5346 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5347 ARG6) \
5348 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5349 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5350 ARG6, ARG7) \
5351 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5352 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5353 ARG6, ARG7, ARG8) \
5354 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5355 ARG7, ARG8);
5356 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5357 def_fn_type (ENUM, RETURN, 1, 0);
5358 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5359 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5360 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5361 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5362 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5363 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5364 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5365 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5366 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5367 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5368 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5369 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5371 #include "builtin-types.def"
5373 #undef DEF_PRIMITIVE_TYPE
5374 #undef DEF_FUNCTION_TYPE_0
5375 #undef DEF_FUNCTION_TYPE_1
5376 #undef DEF_FUNCTION_TYPE_2
5377 #undef DEF_FUNCTION_TYPE_3
5378 #undef DEF_FUNCTION_TYPE_4
5379 #undef DEF_FUNCTION_TYPE_5
5380 #undef DEF_FUNCTION_TYPE_6
5381 #undef DEF_FUNCTION_TYPE_7
5382 #undef DEF_FUNCTION_TYPE_8
5383 #undef DEF_FUNCTION_TYPE_VAR_0
5384 #undef DEF_FUNCTION_TYPE_VAR_1
5385 #undef DEF_FUNCTION_TYPE_VAR_2
5386 #undef DEF_FUNCTION_TYPE_VAR_3
5387 #undef DEF_FUNCTION_TYPE_VAR_4
5388 #undef DEF_FUNCTION_TYPE_VAR_5
5389 #undef DEF_POINTER_TYPE
5390 builtin_types[(int) BT_LAST] = NULL_TREE;
5392 c_init_attributes ();
5394 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5395 NONANSI_P, ATTRS, IMPLICIT, COND) \
5396 if (NAME && COND) \
5397 def_builtin_1 (ENUM, NAME, CLASS, \
5398 builtin_types[(int) TYPE], \
5399 builtin_types[(int) LIBTYPE], \
5400 BOTH_P, FALLBACK_P, NONANSI_P, \
5401 built_in_attributes[(int) ATTRS], IMPLICIT);
5402 #include "builtins.def"
5403 #undef DEF_BUILTIN
5405 targetm.init_builtins ();
5407 build_common_builtin_nodes ();
5409 if (flag_cilkplus)
5410 cilk_init_builtins ();
5413 /* Like get_identifier, but avoid warnings about null arguments when
5414 the argument may be NULL for targets where GCC lacks stdint.h type
5415 information. */
5417 static inline tree
5418 c_get_ident (const char *id)
5420 return get_identifier (id);
5423 /* Build tree nodes and builtin functions common to both C and C++ language
5424 frontends. */
5426 void
5427 c_common_nodes_and_builtins (void)
5429 int char16_type_size;
5430 int char32_type_size;
5431 int wchar_type_size;
5432 tree array_domain_type;
5433 tree va_list_ref_type_node;
5434 tree va_list_arg_type_node;
5436 build_common_tree_nodes (flag_signed_char, flag_short_double);
5438 /* Define `int' and `char' first so that dbx will output them first. */
5439 record_builtin_type (RID_INT, NULL, integer_type_node);
5440 record_builtin_type (RID_CHAR, "char", char_type_node);
5442 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5443 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5444 but not C. Are the conditionals here needed? */
5445 if (c_dialect_cxx ())
5446 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5447 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5448 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5449 record_builtin_type (RID_MAX, "long unsigned int",
5450 long_unsigned_type_node);
5451 if (int128_integer_type_node != NULL_TREE)
5453 record_builtin_type (RID_INT128, "__int128",
5454 int128_integer_type_node);
5455 record_builtin_type (RID_MAX, "__int128 unsigned",
5456 int128_unsigned_type_node);
5458 if (c_dialect_cxx ())
5459 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5460 record_builtin_type (RID_MAX, "long long int",
5461 long_long_integer_type_node);
5462 record_builtin_type (RID_MAX, "long long unsigned int",
5463 long_long_unsigned_type_node);
5464 if (c_dialect_cxx ())
5465 record_builtin_type (RID_MAX, "long long unsigned",
5466 long_long_unsigned_type_node);
5467 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5468 record_builtin_type (RID_MAX, "short unsigned int",
5469 short_unsigned_type_node);
5470 if (c_dialect_cxx ())
5471 record_builtin_type (RID_MAX, "unsigned short",
5472 short_unsigned_type_node);
5474 /* Define both `signed char' and `unsigned char'. */
5475 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5476 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5478 /* These are types that c_common_type_for_size and
5479 c_common_type_for_mode use. */
5480 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5481 TYPE_DECL, NULL_TREE,
5482 intQI_type_node));
5483 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5484 TYPE_DECL, NULL_TREE,
5485 intHI_type_node));
5486 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5487 TYPE_DECL, NULL_TREE,
5488 intSI_type_node));
5489 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5490 TYPE_DECL, NULL_TREE,
5491 intDI_type_node));
5492 #if HOST_BITS_PER_WIDE_INT >= 64
5493 if (targetm.scalar_mode_supported_p (TImode))
5494 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5495 TYPE_DECL,
5496 get_identifier ("__int128_t"),
5497 intTI_type_node));
5498 #endif
5499 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5500 TYPE_DECL, NULL_TREE,
5501 unsigned_intQI_type_node));
5502 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5503 TYPE_DECL, NULL_TREE,
5504 unsigned_intHI_type_node));
5505 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5506 TYPE_DECL, NULL_TREE,
5507 unsigned_intSI_type_node));
5508 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5509 TYPE_DECL, NULL_TREE,
5510 unsigned_intDI_type_node));
5511 #if HOST_BITS_PER_WIDE_INT >= 64
5512 if (targetm.scalar_mode_supported_p (TImode))
5513 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5514 TYPE_DECL,
5515 get_identifier ("__uint128_t"),
5516 unsigned_intTI_type_node));
5517 #endif
5519 /* Create the widest literal types. */
5520 widest_integer_literal_type_node
5521 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5522 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5523 TYPE_DECL, NULL_TREE,
5524 widest_integer_literal_type_node));
5526 widest_unsigned_literal_type_node
5527 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5528 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5529 TYPE_DECL, NULL_TREE,
5530 widest_unsigned_literal_type_node));
5532 signed_size_type_node = c_common_signed_type (size_type_node);
5534 pid_type_node =
5535 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5537 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5538 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5539 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5541 /* Only supported decimal floating point extension if the target
5542 actually supports underlying modes. */
5543 if (targetm.scalar_mode_supported_p (SDmode)
5544 && targetm.scalar_mode_supported_p (DDmode)
5545 && targetm.scalar_mode_supported_p (TDmode))
5547 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5548 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5549 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5552 if (targetm.fixed_point_supported_p ())
5554 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5555 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5556 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5557 record_builtin_type (RID_MAX, "long long _Fract",
5558 long_long_fract_type_node);
5559 record_builtin_type (RID_MAX, "unsigned short _Fract",
5560 unsigned_short_fract_type_node);
5561 record_builtin_type (RID_MAX, "unsigned _Fract",
5562 unsigned_fract_type_node);
5563 record_builtin_type (RID_MAX, "unsigned long _Fract",
5564 unsigned_long_fract_type_node);
5565 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5566 unsigned_long_long_fract_type_node);
5567 record_builtin_type (RID_MAX, "_Sat short _Fract",
5568 sat_short_fract_type_node);
5569 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5570 record_builtin_type (RID_MAX, "_Sat long _Fract",
5571 sat_long_fract_type_node);
5572 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5573 sat_long_long_fract_type_node);
5574 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5575 sat_unsigned_short_fract_type_node);
5576 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5577 sat_unsigned_fract_type_node);
5578 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5579 sat_unsigned_long_fract_type_node);
5580 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5581 sat_unsigned_long_long_fract_type_node);
5582 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5583 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5584 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5585 record_builtin_type (RID_MAX, "long long _Accum",
5586 long_long_accum_type_node);
5587 record_builtin_type (RID_MAX, "unsigned short _Accum",
5588 unsigned_short_accum_type_node);
5589 record_builtin_type (RID_MAX, "unsigned _Accum",
5590 unsigned_accum_type_node);
5591 record_builtin_type (RID_MAX, "unsigned long _Accum",
5592 unsigned_long_accum_type_node);
5593 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5594 unsigned_long_long_accum_type_node);
5595 record_builtin_type (RID_MAX, "_Sat short _Accum",
5596 sat_short_accum_type_node);
5597 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5598 record_builtin_type (RID_MAX, "_Sat long _Accum",
5599 sat_long_accum_type_node);
5600 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5601 sat_long_long_accum_type_node);
5602 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5603 sat_unsigned_short_accum_type_node);
5604 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5605 sat_unsigned_accum_type_node);
5606 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5607 sat_unsigned_long_accum_type_node);
5608 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5609 sat_unsigned_long_long_accum_type_node);
5613 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5614 TYPE_DECL,
5615 get_identifier ("complex int"),
5616 complex_integer_type_node));
5617 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5618 TYPE_DECL,
5619 get_identifier ("complex float"),
5620 complex_float_type_node));
5621 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5622 TYPE_DECL,
5623 get_identifier ("complex double"),
5624 complex_double_type_node));
5625 lang_hooks.decls.pushdecl
5626 (build_decl (UNKNOWN_LOCATION,
5627 TYPE_DECL, get_identifier ("complex long double"),
5628 complex_long_double_type_node));
5630 if (c_dialect_cxx ())
5631 /* For C++, make fileptr_type_node a distinct void * type until
5632 FILE type is defined. */
5633 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5635 record_builtin_type (RID_VOID, NULL, void_type_node);
5637 /* Set the TYPE_NAME for any variants that were built before
5638 record_builtin_type gave names to the built-in types. */
5640 tree void_name = TYPE_NAME (void_type_node);
5641 TYPE_NAME (void_type_node) = NULL_TREE;
5642 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5643 = void_name;
5644 TYPE_NAME (void_type_node) = void_name;
5647 void_list_node = build_void_list_node ();
5649 /* Make a type to be the domain of a few array types
5650 whose domains don't really matter.
5651 200 is small enough that it always fits in size_t
5652 and large enough that it can hold most function names for the
5653 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5654 array_domain_type = build_index_type (size_int (200));
5656 /* Make a type for arrays of characters.
5657 With luck nothing will ever really depend on the length of this
5658 array type. */
5659 char_array_type_node
5660 = build_array_type (char_type_node, array_domain_type);
5662 string_type_node = build_pointer_type (char_type_node);
5663 const_string_type_node
5664 = build_pointer_type (build_qualified_type
5665 (char_type_node, TYPE_QUAL_CONST));
5667 /* This is special for C++ so functions can be overloaded. */
5668 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5669 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5670 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5671 underlying_wchar_type_node = wchar_type_node;
5672 if (c_dialect_cxx ())
5674 if (TYPE_UNSIGNED (wchar_type_node))
5675 wchar_type_node = make_unsigned_type (wchar_type_size);
5676 else
5677 wchar_type_node = make_signed_type (wchar_type_size);
5678 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5681 /* This is for wide string constants. */
5682 wchar_array_type_node
5683 = build_array_type (wchar_type_node, array_domain_type);
5685 /* Define 'char16_t'. */
5686 char16_type_node = get_identifier (CHAR16_TYPE);
5687 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5688 char16_type_size = TYPE_PRECISION (char16_type_node);
5689 if (c_dialect_cxx ())
5691 char16_type_node = make_unsigned_type (char16_type_size);
5693 if (cxx_dialect >= cxx11)
5694 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5697 /* This is for UTF-16 string constants. */
5698 char16_array_type_node
5699 = build_array_type (char16_type_node, array_domain_type);
5701 /* Define 'char32_t'. */
5702 char32_type_node = get_identifier (CHAR32_TYPE);
5703 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5704 char32_type_size = TYPE_PRECISION (char32_type_node);
5705 if (c_dialect_cxx ())
5707 char32_type_node = make_unsigned_type (char32_type_size);
5709 if (cxx_dialect >= cxx11)
5710 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5713 /* This is for UTF-32 string constants. */
5714 char32_array_type_node
5715 = build_array_type (char32_type_node, array_domain_type);
5717 wint_type_node =
5718 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5720 intmax_type_node =
5721 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5722 uintmax_type_node =
5723 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5725 if (SIG_ATOMIC_TYPE)
5726 sig_atomic_type_node =
5727 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5728 if (INT8_TYPE)
5729 int8_type_node =
5730 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5731 if (INT16_TYPE)
5732 int16_type_node =
5733 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5734 if (INT32_TYPE)
5735 int32_type_node =
5736 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5737 if (INT64_TYPE)
5738 int64_type_node =
5739 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5740 if (UINT8_TYPE)
5741 uint8_type_node =
5742 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5743 if (UINT16_TYPE)
5744 c_uint16_type_node = uint16_type_node =
5745 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5746 if (UINT32_TYPE)
5747 c_uint32_type_node = uint32_type_node =
5748 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5749 if (UINT64_TYPE)
5750 c_uint64_type_node = uint64_type_node =
5751 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5752 if (INT_LEAST8_TYPE)
5753 int_least8_type_node =
5754 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5755 if (INT_LEAST16_TYPE)
5756 int_least16_type_node =
5757 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5758 if (INT_LEAST32_TYPE)
5759 int_least32_type_node =
5760 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5761 if (INT_LEAST64_TYPE)
5762 int_least64_type_node =
5763 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5764 if (UINT_LEAST8_TYPE)
5765 uint_least8_type_node =
5766 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5767 if (UINT_LEAST16_TYPE)
5768 uint_least16_type_node =
5769 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5770 if (UINT_LEAST32_TYPE)
5771 uint_least32_type_node =
5772 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5773 if (UINT_LEAST64_TYPE)
5774 uint_least64_type_node =
5775 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5776 if (INT_FAST8_TYPE)
5777 int_fast8_type_node =
5778 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5779 if (INT_FAST16_TYPE)
5780 int_fast16_type_node =
5781 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5782 if (INT_FAST32_TYPE)
5783 int_fast32_type_node =
5784 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5785 if (INT_FAST64_TYPE)
5786 int_fast64_type_node =
5787 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5788 if (UINT_FAST8_TYPE)
5789 uint_fast8_type_node =
5790 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5791 if (UINT_FAST16_TYPE)
5792 uint_fast16_type_node =
5793 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5794 if (UINT_FAST32_TYPE)
5795 uint_fast32_type_node =
5796 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5797 if (UINT_FAST64_TYPE)
5798 uint_fast64_type_node =
5799 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5800 if (INTPTR_TYPE)
5801 intptr_type_node =
5802 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5803 if (UINTPTR_TYPE)
5804 uintptr_type_node =
5805 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5807 default_function_type
5808 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5809 ptrdiff_type_node
5810 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5811 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5813 lang_hooks.decls.pushdecl
5814 (build_decl (UNKNOWN_LOCATION,
5815 TYPE_DECL, get_identifier ("__builtin_va_list"),
5816 va_list_type_node));
5817 if (targetm.enum_va_list_p)
5819 int l;
5820 const char *pname;
5821 tree ptype;
5823 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5825 lang_hooks.decls.pushdecl
5826 (build_decl (UNKNOWN_LOCATION,
5827 TYPE_DECL, get_identifier (pname),
5828 ptype));
5833 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5835 va_list_arg_type_node = va_list_ref_type_node =
5836 build_pointer_type (TREE_TYPE (va_list_type_node));
5838 else
5840 va_list_arg_type_node = va_list_type_node;
5841 va_list_ref_type_node = build_reference_type (va_list_type_node);
5844 if (!flag_preprocess_only)
5845 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5847 main_identifier_node = get_identifier ("main");
5849 /* Create the built-in __null node. It is important that this is
5850 not shared. */
5851 null_node = make_int_cst (1, 1);
5852 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5854 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5855 memset (builtin_types, 0, sizeof (builtin_types));
5858 /* The number of named compound-literals generated thus far. */
5859 static GTY(()) int compound_literal_number;
5861 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5863 void
5864 set_compound_literal_name (tree decl)
5866 char *name;
5867 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5868 compound_literal_number);
5869 compound_literal_number++;
5870 DECL_NAME (decl) = get_identifier (name);
5873 tree
5874 build_va_arg (location_t loc, tree expr, tree type)
5876 expr = build1 (VA_ARG_EXPR, type, expr);
5877 SET_EXPR_LOCATION (expr, loc);
5878 return expr;
5882 /* Linked list of disabled built-in functions. */
5884 typedef struct disabled_builtin
5886 const char *name;
5887 struct disabled_builtin *next;
5888 } disabled_builtin;
5889 static disabled_builtin *disabled_builtins = NULL;
5891 static bool builtin_function_disabled_p (const char *);
5893 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5894 begins with "__builtin_", give an error. */
5896 void
5897 disable_builtin_function (const char *name)
5899 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5900 error ("cannot disable built-in function %qs", name);
5901 else
5903 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5904 new_disabled_builtin->name = name;
5905 new_disabled_builtin->next = disabled_builtins;
5906 disabled_builtins = new_disabled_builtin;
5911 /* Return true if the built-in function NAME has been disabled, false
5912 otherwise. */
5914 static bool
5915 builtin_function_disabled_p (const char *name)
5917 disabled_builtin *p;
5918 for (p = disabled_builtins; p != NULL; p = p->next)
5920 if (strcmp (name, p->name) == 0)
5921 return true;
5923 return false;
5927 /* Worker for DEF_BUILTIN.
5928 Possibly define a builtin function with one or two names.
5929 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5930 nonansi_p and flag_no_nonansi_builtin. */
5932 static void
5933 def_builtin_1 (enum built_in_function fncode,
5934 const char *name,
5935 enum built_in_class fnclass,
5936 tree fntype, tree libtype,
5937 bool both_p, bool fallback_p, bool nonansi_p,
5938 tree fnattrs, bool implicit_p)
5940 tree decl;
5941 const char *libname;
5943 if (fntype == error_mark_node)
5944 return;
5946 gcc_assert ((!both_p && !fallback_p)
5947 || !strncmp (name, "__builtin_",
5948 strlen ("__builtin_")));
5950 libname = name + strlen ("__builtin_");
5951 decl = add_builtin_function (name, fntype, fncode, fnclass,
5952 (fallback_p ? libname : NULL),
5953 fnattrs);
5955 set_builtin_decl (fncode, decl, implicit_p);
5957 if (both_p
5958 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5959 && !(nonansi_p && flag_no_nonansi_builtin))
5960 add_builtin_function (libname, libtype, fncode, fnclass,
5961 NULL, fnattrs);
5964 /* Nonzero if the type T promotes to int. This is (nearly) the
5965 integral promotions defined in ISO C99 6.3.1.1/2. */
5967 bool
5968 c_promoting_integer_type_p (const_tree t)
5970 switch (TREE_CODE (t))
5972 case INTEGER_TYPE:
5973 return (TYPE_MAIN_VARIANT (t) == char_type_node
5974 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5975 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5976 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5977 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5978 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5980 case ENUMERAL_TYPE:
5981 /* ??? Technically all enumerations not larger than an int
5982 promote to an int. But this is used along code paths
5983 that only want to notice a size change. */
5984 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5986 case BOOLEAN_TYPE:
5987 return 1;
5989 default:
5990 return 0;
5994 /* Return 1 if PARMS specifies a fixed number of parameters
5995 and none of their types is affected by default promotions. */
5998 self_promoting_args_p (const_tree parms)
6000 const_tree t;
6001 for (t = parms; t; t = TREE_CHAIN (t))
6003 tree type = TREE_VALUE (t);
6005 if (type == error_mark_node)
6006 continue;
6008 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6009 return 0;
6011 if (type == 0)
6012 return 0;
6014 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6015 return 0;
6017 if (c_promoting_integer_type_p (type))
6018 return 0;
6020 return 1;
6023 /* Recursively remove any '*' or '&' operator from TYPE. */
6024 tree
6025 strip_pointer_operator (tree t)
6027 while (POINTER_TYPE_P (t))
6028 t = TREE_TYPE (t);
6029 return t;
6032 /* Recursively remove pointer or array type from TYPE. */
6033 tree
6034 strip_pointer_or_array_types (tree t)
6036 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6037 t = TREE_TYPE (t);
6038 return t;
6041 /* Used to compare case labels. K1 and K2 are actually tree nodes
6042 representing case labels, or NULL_TREE for a `default' label.
6043 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6044 K2, and 0 if K1 and K2 are equal. */
6047 case_compare (splay_tree_key k1, splay_tree_key k2)
6049 /* Consider a NULL key (such as arises with a `default' label) to be
6050 smaller than anything else. */
6051 if (!k1)
6052 return k2 ? -1 : 0;
6053 else if (!k2)
6054 return k1 ? 1 : 0;
6056 return tree_int_cst_compare ((tree) k1, (tree) k2);
6059 /* Process a case label, located at LOC, for the range LOW_VALUE
6060 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6061 then this case label is actually a `default' label. If only
6062 HIGH_VALUE is NULL_TREE, then case label was declared using the
6063 usual C/C++ syntax, rather than the GNU case range extension.
6064 CASES is a tree containing all the case ranges processed so far;
6065 COND is the condition for the switch-statement itself. Returns the
6066 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6067 is created. */
6069 tree
6070 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6071 tree low_value, tree high_value)
6073 tree type;
6074 tree label;
6075 tree case_label;
6076 splay_tree_node node;
6078 /* Create the LABEL_DECL itself. */
6079 label = create_artificial_label (loc);
6081 /* If there was an error processing the switch condition, bail now
6082 before we get more confused. */
6083 if (!cond || cond == error_mark_node)
6084 goto error_out;
6086 if ((low_value && TREE_TYPE (low_value)
6087 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6088 || (high_value && TREE_TYPE (high_value)
6089 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6091 error_at (loc, "pointers are not permitted as case values");
6092 goto error_out;
6095 /* Case ranges are a GNU extension. */
6096 if (high_value)
6097 pedwarn (loc, OPT_Wpedantic,
6098 "range expressions in switch statements are non-standard");
6100 type = TREE_TYPE (cond);
6101 if (low_value)
6103 low_value = check_case_value (loc, low_value);
6104 low_value = convert_and_check (loc, type, low_value);
6105 if (low_value == error_mark_node)
6106 goto error_out;
6108 if (high_value)
6110 high_value = check_case_value (loc, high_value);
6111 high_value = convert_and_check (loc, type, high_value);
6112 if (high_value == error_mark_node)
6113 goto error_out;
6116 if (low_value && high_value)
6118 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6119 really a case range, even though it was written that way.
6120 Remove the HIGH_VALUE to simplify later processing. */
6121 if (tree_int_cst_equal (low_value, high_value))
6122 high_value = NULL_TREE;
6123 else if (!tree_int_cst_lt (low_value, high_value))
6124 warning_at (loc, 0, "empty range specified");
6127 /* See if the case is in range of the type of the original testing
6128 expression. If both low_value and high_value are out of range,
6129 don't insert the case label and return NULL_TREE. */
6130 if (low_value
6131 && !check_case_bounds (loc, type, orig_type,
6132 &low_value, high_value ? &high_value : NULL))
6133 return NULL_TREE;
6135 /* Look up the LOW_VALUE in the table of case labels we already
6136 have. */
6137 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6138 /* If there was not an exact match, check for overlapping ranges.
6139 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6140 that's a `default' label and the only overlap is an exact match. */
6141 if (!node && (low_value || high_value))
6143 splay_tree_node low_bound;
6144 splay_tree_node high_bound;
6146 /* Even though there wasn't an exact match, there might be an
6147 overlap between this case range and another case range.
6148 Since we've (inductively) not allowed any overlapping case
6149 ranges, we simply need to find the greatest low case label
6150 that is smaller that LOW_VALUE, and the smallest low case
6151 label that is greater than LOW_VALUE. If there is an overlap
6152 it will occur in one of these two ranges. */
6153 low_bound = splay_tree_predecessor (cases,
6154 (splay_tree_key) low_value);
6155 high_bound = splay_tree_successor (cases,
6156 (splay_tree_key) low_value);
6158 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6159 the LOW_VALUE, so there is no need to check unless the
6160 LOW_BOUND is in fact itself a case range. */
6161 if (low_bound
6162 && CASE_HIGH ((tree) low_bound->value)
6163 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6164 low_value) >= 0)
6165 node = low_bound;
6166 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6167 range is bigger than the low end of the current range, so we
6168 are only interested if the current range is a real range, and
6169 not an ordinary case label. */
6170 else if (high_bound
6171 && high_value
6172 && (tree_int_cst_compare ((tree) high_bound->key,
6173 high_value)
6174 <= 0))
6175 node = high_bound;
6177 /* If there was an overlap, issue an error. */
6178 if (node)
6180 tree duplicate = CASE_LABEL ((tree) node->value);
6182 if (high_value)
6184 error_at (loc, "duplicate (or overlapping) case value");
6185 error_at (DECL_SOURCE_LOCATION (duplicate),
6186 "this is the first entry overlapping that value");
6188 else if (low_value)
6190 error_at (loc, "duplicate case value") ;
6191 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6193 else
6195 error_at (loc, "multiple default labels in one switch");
6196 error_at (DECL_SOURCE_LOCATION (duplicate),
6197 "this is the first default label");
6199 goto error_out;
6202 /* Add a CASE_LABEL to the statement-tree. */
6203 case_label = add_stmt (build_case_label (low_value, high_value, label));
6204 /* Register this case label in the splay tree. */
6205 splay_tree_insert (cases,
6206 (splay_tree_key) low_value,
6207 (splay_tree_value) case_label);
6209 return case_label;
6211 error_out:
6212 /* Add a label so that the back-end doesn't think that the beginning of
6213 the switch is unreachable. Note that we do not add a case label, as
6214 that just leads to duplicates and thence to failure later on. */
6215 if (!cases->root)
6217 tree t = create_artificial_label (loc);
6218 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6220 return error_mark_node;
6223 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6224 Used to verify that case values match up with enumerator values. */
6226 static void
6227 match_case_to_enum_1 (tree key, tree type, tree label)
6229 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6231 if (tree_fits_uhwi_p (key))
6232 print_dec (key, buf, UNSIGNED);
6233 else if (tree_fits_shwi_p (key))
6234 print_dec (key, buf, SIGNED);
6235 else
6236 print_hex (key, buf);
6238 if (TYPE_NAME (type) == 0)
6239 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6240 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6241 "case value %qs not in enumerated type",
6242 buf);
6243 else
6244 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6245 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6246 "case value %qs not in enumerated type %qT",
6247 buf, type);
6250 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6251 Used to verify that case values match up with enumerator values. */
6253 static int
6254 match_case_to_enum (splay_tree_node node, void *data)
6256 tree label = (tree) node->value;
6257 tree type = (tree) data;
6259 /* Skip default case. */
6260 if (!CASE_LOW (label))
6261 return 0;
6263 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6264 when we did our enum->case scan. Reset our scratch bit after. */
6265 if (!CASE_LOW_SEEN (label))
6266 match_case_to_enum_1 (CASE_LOW (label), type, label);
6267 else
6268 CASE_LOW_SEEN (label) = 0;
6270 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6271 not set, that means that CASE_HIGH did not appear when we did our
6272 enum->case scan. Reset our scratch bit after. */
6273 if (CASE_HIGH (label))
6275 if (!CASE_HIGH_SEEN (label))
6276 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6277 else
6278 CASE_HIGH_SEEN (label) = 0;
6281 return 0;
6284 /* Handle -Wswitch*. Called from the front end after parsing the
6285 switch construct. */
6286 /* ??? Should probably be somewhere generic, since other languages
6287 besides C and C++ would want this. At the moment, however, C/C++
6288 are the only tree-ssa languages that support enumerations at all,
6289 so the point is moot. */
6291 void
6292 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6293 tree type, tree cond)
6295 splay_tree_node default_node;
6296 splay_tree_node node;
6297 tree chain;
6299 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6300 return;
6302 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6303 if (!default_node)
6304 warning_at (switch_location, OPT_Wswitch_default,
6305 "switch missing default case");
6307 /* From here on, we only care about about enumerated types. */
6308 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6309 return;
6311 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6312 if (!warn_switch_enum && !warn_switch)
6313 return;
6315 /* Check the cases. Warn about case values which are not members of
6316 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6317 there is no default case, check that exactly all enumeration
6318 literals are covered by the cases. */
6320 /* Clearing COND if it is not an integer constant simplifies
6321 the tests inside the loop below. */
6322 if (TREE_CODE (cond) != INTEGER_CST)
6323 cond = NULL_TREE;
6325 /* The time complexity here is O(N*lg(N)) worst case, but for the
6326 common case of monotonically increasing enumerators, it is
6327 O(N), since the nature of the splay tree will keep the next
6328 element adjacent to the root at all times. */
6330 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6332 tree value = TREE_VALUE (chain);
6333 if (TREE_CODE (value) == CONST_DECL)
6334 value = DECL_INITIAL (value);
6335 node = splay_tree_lookup (cases, (splay_tree_key) value);
6336 if (node)
6338 /* Mark the CASE_LOW part of the case entry as seen. */
6339 tree label = (tree) node->value;
6340 CASE_LOW_SEEN (label) = 1;
6341 continue;
6344 /* Even though there wasn't an exact match, there might be a
6345 case range which includes the enumerator's value. */
6346 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6347 if (node && CASE_HIGH ((tree) node->value))
6349 tree label = (tree) node->value;
6350 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6351 if (cmp >= 0)
6353 /* If we match the upper bound exactly, mark the CASE_HIGH
6354 part of the case entry as seen. */
6355 if (cmp == 0)
6356 CASE_HIGH_SEEN (label) = 1;
6357 continue;
6361 /* We've now determined that this enumerated literal isn't
6362 handled by the case labels of the switch statement. */
6364 /* If the switch expression is a constant, we only really care
6365 about whether that constant is handled by the switch. */
6366 if (cond && tree_int_cst_compare (cond, value))
6367 continue;
6369 /* If there is a default_node, the only relevant option is
6370 Wswitch-enum. Otherwise, if both are enabled then we prefer
6371 to warn using -Wswitch because -Wswitch is enabled by -Wall
6372 while -Wswitch-enum is explicit. */
6373 warning_at (switch_location,
6374 (default_node || !warn_switch
6375 ? OPT_Wswitch_enum
6376 : OPT_Wswitch),
6377 "enumeration value %qE not handled in switch",
6378 TREE_PURPOSE (chain));
6381 /* Warn if there are case expressions that don't correspond to
6382 enumerators. This can occur since C and C++ don't enforce
6383 type-checking of assignments to enumeration variables.
6385 The time complexity here is now always O(N) worst case, since
6386 we should have marked both the lower bound and upper bound of
6387 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6388 above. This scan also resets those fields. */
6390 splay_tree_foreach (cases, match_case_to_enum, type);
6393 /* Finish an expression taking the address of LABEL (an
6394 IDENTIFIER_NODE). Returns an expression for the address.
6396 LOC is the location for the expression returned. */
6398 tree
6399 finish_label_address_expr (tree label, location_t loc)
6401 tree result;
6403 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6405 if (label == error_mark_node)
6406 return error_mark_node;
6408 label = lookup_label (label);
6409 if (label == NULL_TREE)
6410 result = null_pointer_node;
6411 else
6413 TREE_USED (label) = 1;
6414 result = build1 (ADDR_EXPR, ptr_type_node, label);
6415 /* The current function is not necessarily uninlinable.
6416 Computed gotos are incompatible with inlining, but the value
6417 here could be used only in a diagnostic, for example. */
6418 protected_set_expr_location (result, loc);
6421 return result;
6425 /* Given a boolean expression ARG, return a tree representing an increment
6426 or decrement (as indicated by CODE) of ARG. The front end must check for
6427 invalid cases (e.g., decrement in C++). */
6428 tree
6429 boolean_increment (enum tree_code code, tree arg)
6431 tree val;
6432 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6434 arg = stabilize_reference (arg);
6435 switch (code)
6437 case PREINCREMENT_EXPR:
6438 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6439 break;
6440 case POSTINCREMENT_EXPR:
6441 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6442 arg = save_expr (arg);
6443 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6444 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6445 break;
6446 case PREDECREMENT_EXPR:
6447 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6448 invert_truthvalue_loc (input_location, arg));
6449 break;
6450 case POSTDECREMENT_EXPR:
6451 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6452 invert_truthvalue_loc (input_location, arg));
6453 arg = save_expr (arg);
6454 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6455 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6456 break;
6457 default:
6458 gcc_unreachable ();
6460 TREE_SIDE_EFFECTS (val) = 1;
6461 return val;
6464 /* Built-in macros for stddef.h and stdint.h, that require macros
6465 defined in this file. */
6466 void
6467 c_stddef_cpp_builtins(void)
6469 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6470 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6471 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6472 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6473 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6474 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6475 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6476 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6477 if (SIG_ATOMIC_TYPE)
6478 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6479 if (INT8_TYPE)
6480 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6481 if (INT16_TYPE)
6482 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6483 if (INT32_TYPE)
6484 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6485 if (INT64_TYPE)
6486 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6487 if (UINT8_TYPE)
6488 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6489 if (UINT16_TYPE)
6490 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6491 if (UINT32_TYPE)
6492 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6493 if (UINT64_TYPE)
6494 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6495 if (INT_LEAST8_TYPE)
6496 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6497 if (INT_LEAST16_TYPE)
6498 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6499 if (INT_LEAST32_TYPE)
6500 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6501 if (INT_LEAST64_TYPE)
6502 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6503 if (UINT_LEAST8_TYPE)
6504 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6505 if (UINT_LEAST16_TYPE)
6506 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6507 if (UINT_LEAST32_TYPE)
6508 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6509 if (UINT_LEAST64_TYPE)
6510 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6511 if (INT_FAST8_TYPE)
6512 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6513 if (INT_FAST16_TYPE)
6514 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6515 if (INT_FAST32_TYPE)
6516 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6517 if (INT_FAST64_TYPE)
6518 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6519 if (UINT_FAST8_TYPE)
6520 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6521 if (UINT_FAST16_TYPE)
6522 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6523 if (UINT_FAST32_TYPE)
6524 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6525 if (UINT_FAST64_TYPE)
6526 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6527 if (INTPTR_TYPE)
6528 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6529 if (UINTPTR_TYPE)
6530 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6533 static void
6534 c_init_attributes (void)
6536 /* Fill in the built_in_attributes array. */
6537 #define DEF_ATTR_NULL_TREE(ENUM) \
6538 built_in_attributes[(int) ENUM] = NULL_TREE;
6539 #define DEF_ATTR_INT(ENUM, VALUE) \
6540 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6541 #define DEF_ATTR_STRING(ENUM, VALUE) \
6542 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6543 #define DEF_ATTR_IDENT(ENUM, STRING) \
6544 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6545 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6546 built_in_attributes[(int) ENUM] \
6547 = tree_cons (built_in_attributes[(int) PURPOSE], \
6548 built_in_attributes[(int) VALUE], \
6549 built_in_attributes[(int) CHAIN]);
6550 #include "builtin-attrs.def"
6551 #undef DEF_ATTR_NULL_TREE
6552 #undef DEF_ATTR_INT
6553 #undef DEF_ATTR_IDENT
6554 #undef DEF_ATTR_TREE_LIST
6557 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6558 identifier as an argument, so the front end shouldn't look it up. */
6560 bool
6561 attribute_takes_identifier_p (const_tree attr_id)
6563 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6564 if (spec == NULL)
6565 /* Unknown attribute that we'll end up ignoring, return true so we
6566 don't complain about an identifier argument. */
6567 return true;
6568 else if (!strcmp ("mode", spec->name)
6569 || !strcmp ("format", spec->name)
6570 || !strcmp ("cleanup", spec->name))
6571 return true;
6572 else
6573 return targetm.attribute_takes_identifier_p (attr_id);
6576 /* Attribute handlers common to C front ends. */
6578 /* Handle a "packed" attribute; arguments as in
6579 struct attribute_spec.handler. */
6581 static tree
6582 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6583 int flags, bool *no_add_attrs)
6585 if (TYPE_P (*node))
6587 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6588 *node = build_variant_type_copy (*node);
6589 TYPE_PACKED (*node) = 1;
6591 else if (TREE_CODE (*node) == FIELD_DECL)
6593 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6594 /* Still pack bitfields. */
6595 && ! DECL_INITIAL (*node))
6596 warning (OPT_Wattributes,
6597 "%qE attribute ignored for field of type %qT",
6598 name, TREE_TYPE (*node));
6599 else
6600 DECL_PACKED (*node) = 1;
6602 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6603 used for DECL_REGISTER. It wouldn't mean anything anyway.
6604 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6605 that changes what the typedef is typing. */
6606 else
6608 warning (OPT_Wattributes, "%qE attribute ignored", name);
6609 *no_add_attrs = true;
6612 return NULL_TREE;
6615 /* Handle a "nocommon" attribute; arguments as in
6616 struct attribute_spec.handler. */
6618 static tree
6619 handle_nocommon_attribute (tree *node, tree name,
6620 tree ARG_UNUSED (args),
6621 int ARG_UNUSED (flags), bool *no_add_attrs)
6623 if (TREE_CODE (*node) == VAR_DECL)
6624 DECL_COMMON (*node) = 0;
6625 else
6627 warning (OPT_Wattributes, "%qE attribute ignored", name);
6628 *no_add_attrs = true;
6631 return NULL_TREE;
6634 /* Handle a "common" attribute; arguments as in
6635 struct attribute_spec.handler. */
6637 static tree
6638 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6639 int ARG_UNUSED (flags), bool *no_add_attrs)
6641 if (TREE_CODE (*node) == VAR_DECL)
6642 DECL_COMMON (*node) = 1;
6643 else
6645 warning (OPT_Wattributes, "%qE attribute ignored", name);
6646 *no_add_attrs = true;
6649 return NULL_TREE;
6652 /* Handle a "noreturn" attribute; arguments as in
6653 struct attribute_spec.handler. */
6655 static tree
6656 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6657 int ARG_UNUSED (flags), bool *no_add_attrs)
6659 tree type = TREE_TYPE (*node);
6661 /* See FIXME comment in c_common_attribute_table. */
6662 if (TREE_CODE (*node) == FUNCTION_DECL
6663 || objc_method_decl (TREE_CODE (*node)))
6664 TREE_THIS_VOLATILE (*node) = 1;
6665 else if (TREE_CODE (type) == POINTER_TYPE
6666 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6667 TREE_TYPE (*node)
6668 = (build_qualified_type
6669 (build_pointer_type
6670 (build_type_variant (TREE_TYPE (type),
6671 TYPE_READONLY (TREE_TYPE (type)), 1)),
6672 TYPE_QUALS (type)));
6673 else
6675 warning (OPT_Wattributes, "%qE attribute ignored", name);
6676 *no_add_attrs = true;
6679 return NULL_TREE;
6682 /* Handle a "hot" and attribute; arguments as in
6683 struct attribute_spec.handler. */
6685 static tree
6686 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6687 int ARG_UNUSED (flags), bool *no_add_attrs)
6689 if (TREE_CODE (*node) == FUNCTION_DECL
6690 || TREE_CODE (*node) == LABEL_DECL)
6692 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6694 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6695 "with attribute %qs", name, "cold");
6696 *no_add_attrs = true;
6698 /* Most of the rest of the hot processing is done later with
6699 lookup_attribute. */
6701 else
6703 warning (OPT_Wattributes, "%qE attribute ignored", name);
6704 *no_add_attrs = true;
6707 return NULL_TREE;
6710 /* Handle a "cold" and attribute; arguments as in
6711 struct attribute_spec.handler. */
6713 static tree
6714 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6715 int ARG_UNUSED (flags), bool *no_add_attrs)
6717 if (TREE_CODE (*node) == FUNCTION_DECL
6718 || TREE_CODE (*node) == LABEL_DECL)
6720 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6722 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6723 "with attribute %qs", name, "hot");
6724 *no_add_attrs = true;
6726 /* Most of the rest of the cold processing is done later with
6727 lookup_attribute. */
6729 else
6731 warning (OPT_Wattributes, "%qE attribute ignored", name);
6732 *no_add_attrs = true;
6735 return NULL_TREE;
6738 /* Handle a "no_sanitize_address" attribute; arguments as in
6739 struct attribute_spec.handler. */
6741 static tree
6742 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6743 bool *no_add_attrs)
6745 if (TREE_CODE (*node) != FUNCTION_DECL)
6747 warning (OPT_Wattributes, "%qE attribute ignored", name);
6748 *no_add_attrs = true;
6751 return NULL_TREE;
6754 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6755 struct attribute_spec.handler. */
6757 static tree
6758 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6759 bool *no_add_attrs)
6761 if (TREE_CODE (*node) != FUNCTION_DECL)
6762 warning (OPT_Wattributes, "%qE attribute ignored", name);
6763 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6764 DECL_ATTRIBUTES (*node)
6765 = tree_cons (get_identifier ("no_sanitize_address"),
6766 NULL_TREE, DECL_ATTRIBUTES (*node));
6767 *no_add_attrs = true;
6768 return NULL_TREE;
6771 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6772 struct attribute_spec.handler. */
6774 static tree
6775 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6776 bool *no_add_attrs)
6778 if (TREE_CODE (*node) != FUNCTION_DECL)
6780 warning (OPT_Wattributes, "%qE attribute ignored", name);
6781 *no_add_attrs = true;
6784 return NULL_TREE;
6787 /* Handle a "noinline" attribute; arguments as in
6788 struct attribute_spec.handler. */
6790 static tree
6791 handle_noinline_attribute (tree *node, tree name,
6792 tree ARG_UNUSED (args),
6793 int ARG_UNUSED (flags), bool *no_add_attrs)
6795 if (TREE_CODE (*node) == FUNCTION_DECL)
6797 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6799 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6800 "with attribute %qs", name, "always_inline");
6801 *no_add_attrs = true;
6803 else
6804 DECL_UNINLINABLE (*node) = 1;
6806 else
6808 warning (OPT_Wattributes, "%qE attribute ignored", name);
6809 *no_add_attrs = true;
6812 return NULL_TREE;
6815 /* Handle a "noclone" attribute; arguments as in
6816 struct attribute_spec.handler. */
6818 static tree
6819 handle_noclone_attribute (tree *node, tree name,
6820 tree ARG_UNUSED (args),
6821 int ARG_UNUSED (flags), bool *no_add_attrs)
6823 if (TREE_CODE (*node) != FUNCTION_DECL)
6825 warning (OPT_Wattributes, "%qE attribute ignored", name);
6826 *no_add_attrs = true;
6829 return NULL_TREE;
6832 /* Handle a "always_inline" attribute; arguments as in
6833 struct attribute_spec.handler. */
6835 static tree
6836 handle_always_inline_attribute (tree *node, tree name,
6837 tree ARG_UNUSED (args),
6838 int ARG_UNUSED (flags),
6839 bool *no_add_attrs)
6841 if (TREE_CODE (*node) == FUNCTION_DECL)
6843 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6845 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6846 "with %qs attribute", name, "noinline");
6847 *no_add_attrs = true;
6849 else
6850 /* Set the attribute and mark it for disregarding inline
6851 limits. */
6852 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6854 else
6856 warning (OPT_Wattributes, "%qE attribute ignored", name);
6857 *no_add_attrs = true;
6860 return NULL_TREE;
6863 /* Handle a "gnu_inline" attribute; arguments as in
6864 struct attribute_spec.handler. */
6866 static tree
6867 handle_gnu_inline_attribute (tree *node, tree name,
6868 tree ARG_UNUSED (args),
6869 int ARG_UNUSED (flags),
6870 bool *no_add_attrs)
6872 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6874 /* Do nothing else, just set the attribute. We'll get at
6875 it later with lookup_attribute. */
6877 else
6879 warning (OPT_Wattributes, "%qE attribute ignored", name);
6880 *no_add_attrs = true;
6883 return NULL_TREE;
6886 /* Handle a "leaf" attribute; arguments as in
6887 struct attribute_spec.handler. */
6889 static tree
6890 handle_leaf_attribute (tree *node, tree name,
6891 tree ARG_UNUSED (args),
6892 int ARG_UNUSED (flags), bool *no_add_attrs)
6894 if (TREE_CODE (*node) != FUNCTION_DECL)
6896 warning (OPT_Wattributes, "%qE attribute ignored", name);
6897 *no_add_attrs = true;
6899 if (!TREE_PUBLIC (*node))
6901 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6902 *no_add_attrs = true;
6905 return NULL_TREE;
6908 /* Handle an "artificial" attribute; arguments as in
6909 struct attribute_spec.handler. */
6911 static tree
6912 handle_artificial_attribute (tree *node, tree name,
6913 tree ARG_UNUSED (args),
6914 int ARG_UNUSED (flags),
6915 bool *no_add_attrs)
6917 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6919 /* Do nothing else, just set the attribute. We'll get at
6920 it later with lookup_attribute. */
6922 else
6924 warning (OPT_Wattributes, "%qE attribute ignored", name);
6925 *no_add_attrs = true;
6928 return NULL_TREE;
6931 /* Handle a "flatten" attribute; arguments as in
6932 struct attribute_spec.handler. */
6934 static tree
6935 handle_flatten_attribute (tree *node, tree name,
6936 tree args ATTRIBUTE_UNUSED,
6937 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6939 if (TREE_CODE (*node) == FUNCTION_DECL)
6940 /* Do nothing else, just set the attribute. We'll get at
6941 it later with lookup_attribute. */
6943 else
6945 warning (OPT_Wattributes, "%qE attribute ignored", name);
6946 *no_add_attrs = true;
6949 return NULL_TREE;
6952 /* Handle a "warning" or "error" attribute; arguments as in
6953 struct attribute_spec.handler. */
6955 static tree
6956 handle_error_attribute (tree *node, tree name, tree args,
6957 int ARG_UNUSED (flags), bool *no_add_attrs)
6959 if (TREE_CODE (*node) == FUNCTION_DECL
6960 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6961 /* Do nothing else, just set the attribute. We'll get at
6962 it later with lookup_attribute. */
6964 else
6966 warning (OPT_Wattributes, "%qE attribute ignored", name);
6967 *no_add_attrs = true;
6970 return NULL_TREE;
6973 /* Handle a "used" attribute; arguments as in
6974 struct attribute_spec.handler. */
6976 static tree
6977 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6978 int ARG_UNUSED (flags), bool *no_add_attrs)
6980 tree node = *pnode;
6982 if (TREE_CODE (node) == FUNCTION_DECL
6983 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6984 || (TREE_CODE (node) == TYPE_DECL))
6986 TREE_USED (node) = 1;
6987 DECL_PRESERVE_P (node) = 1;
6988 if (TREE_CODE (node) == VAR_DECL)
6989 DECL_READ_P (node) = 1;
6991 else
6993 warning (OPT_Wattributes, "%qE attribute ignored", name);
6994 *no_add_attrs = true;
6997 return NULL_TREE;
7000 /* Handle a "unused" attribute; arguments as in
7001 struct attribute_spec.handler. */
7003 static tree
7004 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7005 int flags, bool *no_add_attrs)
7007 if (DECL_P (*node))
7009 tree decl = *node;
7011 if (TREE_CODE (decl) == PARM_DECL
7012 || TREE_CODE (decl) == VAR_DECL
7013 || TREE_CODE (decl) == FUNCTION_DECL
7014 || TREE_CODE (decl) == LABEL_DECL
7015 || TREE_CODE (decl) == TYPE_DECL)
7017 TREE_USED (decl) = 1;
7018 if (TREE_CODE (decl) == VAR_DECL
7019 || TREE_CODE (decl) == PARM_DECL)
7020 DECL_READ_P (decl) = 1;
7022 else
7024 warning (OPT_Wattributes, "%qE attribute ignored", name);
7025 *no_add_attrs = true;
7028 else
7030 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7031 *node = build_variant_type_copy (*node);
7032 TREE_USED (*node) = 1;
7035 return NULL_TREE;
7038 /* Handle a "externally_visible" attribute; arguments as in
7039 struct attribute_spec.handler. */
7041 static tree
7042 handle_externally_visible_attribute (tree *pnode, tree name,
7043 tree ARG_UNUSED (args),
7044 int ARG_UNUSED (flags),
7045 bool *no_add_attrs)
7047 tree node = *pnode;
7049 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7051 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7052 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7054 warning (OPT_Wattributes,
7055 "%qE attribute have effect only on public objects", name);
7056 *no_add_attrs = true;
7059 else
7061 warning (OPT_Wattributes, "%qE attribute ignored", name);
7062 *no_add_attrs = true;
7065 return NULL_TREE;
7068 /* Handle a "const" attribute; arguments as in
7069 struct attribute_spec.handler. */
7071 static tree
7072 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7073 int ARG_UNUSED (flags), bool *no_add_attrs)
7075 tree type = TREE_TYPE (*node);
7077 /* See FIXME comment on noreturn in c_common_attribute_table. */
7078 if (TREE_CODE (*node) == FUNCTION_DECL)
7079 TREE_READONLY (*node) = 1;
7080 else if (TREE_CODE (type) == POINTER_TYPE
7081 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7082 TREE_TYPE (*node)
7083 = (build_qualified_type
7084 (build_pointer_type
7085 (build_type_variant (TREE_TYPE (type), 1,
7086 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7087 TYPE_QUALS (type)));
7088 else
7090 warning (OPT_Wattributes, "%qE attribute ignored", name);
7091 *no_add_attrs = true;
7094 return NULL_TREE;
7097 /* Handle a "transparent_union" attribute; arguments as in
7098 struct attribute_spec.handler. */
7100 static tree
7101 handle_transparent_union_attribute (tree *node, tree name,
7102 tree ARG_UNUSED (args), int flags,
7103 bool *no_add_attrs)
7105 tree type;
7107 *no_add_attrs = true;
7110 if (TREE_CODE (*node) == TYPE_DECL
7111 && ! (flags & ATTR_FLAG_CXX11))
7112 node = &TREE_TYPE (*node);
7113 type = *node;
7115 if (TREE_CODE (type) == UNION_TYPE)
7117 /* Make sure that the first field will work for a transparent union.
7118 If the type isn't complete yet, leave the check to the code in
7119 finish_struct. */
7120 if (TYPE_SIZE (type))
7122 tree first = first_field (type);
7123 if (first == NULL_TREE
7124 || DECL_ARTIFICIAL (first)
7125 || TYPE_MODE (type) != DECL_MODE (first))
7126 goto ignored;
7129 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7131 /* If the type isn't complete yet, setting the flag
7132 on a variant wouldn't ever be checked. */
7133 if (!TYPE_SIZE (type))
7134 goto ignored;
7136 /* build_duplicate_type doesn't work for C++. */
7137 if (c_dialect_cxx ())
7138 goto ignored;
7140 /* A type variant isn't good enough, since we don't a cast
7141 to such a type removed as a no-op. */
7142 *node = type = build_duplicate_type (type);
7145 TYPE_TRANSPARENT_AGGR (type) = 1;
7146 return NULL_TREE;
7149 ignored:
7150 warning (OPT_Wattributes, "%qE attribute ignored", name);
7151 return NULL_TREE;
7154 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7155 get the requested priority for a constructor or destructor,
7156 possibly issuing diagnostics for invalid or reserved
7157 priorities. */
7159 static priority_type
7160 get_priority (tree args, bool is_destructor)
7162 HOST_WIDE_INT pri;
7163 tree arg;
7165 if (!args)
7166 return DEFAULT_INIT_PRIORITY;
7168 if (!SUPPORTS_INIT_PRIORITY)
7170 if (is_destructor)
7171 error ("destructor priorities are not supported");
7172 else
7173 error ("constructor priorities are not supported");
7174 return DEFAULT_INIT_PRIORITY;
7177 arg = TREE_VALUE (args);
7178 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7179 goto invalid;
7180 if (arg == error_mark_node)
7181 return DEFAULT_INIT_PRIORITY;
7182 arg = default_conversion (arg);
7183 if (!tree_fits_shwi_p (arg)
7184 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7185 goto invalid;
7187 pri = tree_to_shwi (arg);
7188 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7189 goto invalid;
7191 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7193 if (is_destructor)
7194 warning (0,
7195 "destructor priorities from 0 to %d are reserved "
7196 "for the implementation",
7197 MAX_RESERVED_INIT_PRIORITY);
7198 else
7199 warning (0,
7200 "constructor priorities from 0 to %d are reserved "
7201 "for the implementation",
7202 MAX_RESERVED_INIT_PRIORITY);
7204 return pri;
7206 invalid:
7207 if (is_destructor)
7208 error ("destructor priorities must be integers from 0 to %d inclusive",
7209 MAX_INIT_PRIORITY);
7210 else
7211 error ("constructor priorities must be integers from 0 to %d inclusive",
7212 MAX_INIT_PRIORITY);
7213 return DEFAULT_INIT_PRIORITY;
7216 /* Handle a "constructor" attribute; arguments as in
7217 struct attribute_spec.handler. */
7219 static tree
7220 handle_constructor_attribute (tree *node, tree name, tree args,
7221 int ARG_UNUSED (flags),
7222 bool *no_add_attrs)
7224 tree decl = *node;
7225 tree type = TREE_TYPE (decl);
7227 if (TREE_CODE (decl) == FUNCTION_DECL
7228 && TREE_CODE (type) == FUNCTION_TYPE
7229 && decl_function_context (decl) == 0)
7231 priority_type priority;
7232 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7233 priority = get_priority (args, /*is_destructor=*/false);
7234 SET_DECL_INIT_PRIORITY (decl, priority);
7235 TREE_USED (decl) = 1;
7237 else
7239 warning (OPT_Wattributes, "%qE attribute ignored", name);
7240 *no_add_attrs = true;
7243 return NULL_TREE;
7246 /* Handle a "destructor" attribute; arguments as in
7247 struct attribute_spec.handler. */
7249 static tree
7250 handle_destructor_attribute (tree *node, tree name, tree args,
7251 int ARG_UNUSED (flags),
7252 bool *no_add_attrs)
7254 tree decl = *node;
7255 tree type = TREE_TYPE (decl);
7257 if (TREE_CODE (decl) == FUNCTION_DECL
7258 && TREE_CODE (type) == FUNCTION_TYPE
7259 && decl_function_context (decl) == 0)
7261 priority_type priority;
7262 DECL_STATIC_DESTRUCTOR (decl) = 1;
7263 priority = get_priority (args, /*is_destructor=*/true);
7264 SET_DECL_FINI_PRIORITY (decl, priority);
7265 TREE_USED (decl) = 1;
7267 else
7269 warning (OPT_Wattributes, "%qE attribute ignored", name);
7270 *no_add_attrs = true;
7273 return NULL_TREE;
7276 /* Nonzero if the mode is a valid vector mode for this architecture.
7277 This returns nonzero even if there is no hardware support for the
7278 vector mode, but we can emulate with narrower modes. */
7280 static int
7281 vector_mode_valid_p (enum machine_mode mode)
7283 enum mode_class mclass = GET_MODE_CLASS (mode);
7284 enum machine_mode innermode;
7286 /* Doh! What's going on? */
7287 if (mclass != MODE_VECTOR_INT
7288 && mclass != MODE_VECTOR_FLOAT
7289 && mclass != MODE_VECTOR_FRACT
7290 && mclass != MODE_VECTOR_UFRACT
7291 && mclass != MODE_VECTOR_ACCUM
7292 && mclass != MODE_VECTOR_UACCUM)
7293 return 0;
7295 /* Hardware support. Woo hoo! */
7296 if (targetm.vector_mode_supported_p (mode))
7297 return 1;
7299 innermode = GET_MODE_INNER (mode);
7301 /* We should probably return 1 if requesting V4DI and we have no DI,
7302 but we have V2DI, but this is probably very unlikely. */
7304 /* If we have support for the inner mode, we can safely emulate it.
7305 We may not have V2DI, but me can emulate with a pair of DIs. */
7306 return targetm.scalar_mode_supported_p (innermode);
7310 /* Handle a "mode" attribute; arguments as in
7311 struct attribute_spec.handler. */
7313 static tree
7314 handle_mode_attribute (tree *node, tree name, tree args,
7315 int ARG_UNUSED (flags), bool *no_add_attrs)
7317 tree type = *node;
7318 tree ident = TREE_VALUE (args);
7320 *no_add_attrs = true;
7322 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7323 warning (OPT_Wattributes, "%qE attribute ignored", name);
7324 else
7326 int j;
7327 const char *p = IDENTIFIER_POINTER (ident);
7328 int len = strlen (p);
7329 enum machine_mode mode = VOIDmode;
7330 tree typefm;
7331 bool valid_mode;
7333 if (len > 4 && p[0] == '_' && p[1] == '_'
7334 && p[len - 1] == '_' && p[len - 2] == '_')
7336 char *newp = (char *) alloca (len - 1);
7338 strcpy (newp, &p[2]);
7339 newp[len - 4] = '\0';
7340 p = newp;
7343 /* Change this type to have a type with the specified mode.
7344 First check for the special modes. */
7345 if (!strcmp (p, "byte"))
7346 mode = byte_mode;
7347 else if (!strcmp (p, "word"))
7348 mode = word_mode;
7349 else if (!strcmp (p, "pointer"))
7350 mode = ptr_mode;
7351 else if (!strcmp (p, "libgcc_cmp_return"))
7352 mode = targetm.libgcc_cmp_return_mode ();
7353 else if (!strcmp (p, "libgcc_shift_count"))
7354 mode = targetm.libgcc_shift_count_mode ();
7355 else if (!strcmp (p, "unwind_word"))
7356 mode = targetm.unwind_word_mode ();
7357 else
7358 for (j = 0; j < NUM_MACHINE_MODES; j++)
7359 if (!strcmp (p, GET_MODE_NAME (j)))
7361 mode = (enum machine_mode) j;
7362 break;
7365 if (mode == VOIDmode)
7367 error ("unknown machine mode %qE", ident);
7368 return NULL_TREE;
7371 valid_mode = false;
7372 switch (GET_MODE_CLASS (mode))
7374 case MODE_INT:
7375 case MODE_PARTIAL_INT:
7376 case MODE_FLOAT:
7377 case MODE_DECIMAL_FLOAT:
7378 case MODE_FRACT:
7379 case MODE_UFRACT:
7380 case MODE_ACCUM:
7381 case MODE_UACCUM:
7382 valid_mode = targetm.scalar_mode_supported_p (mode);
7383 break;
7385 case MODE_COMPLEX_INT:
7386 case MODE_COMPLEX_FLOAT:
7387 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7388 break;
7390 case MODE_VECTOR_INT:
7391 case MODE_VECTOR_FLOAT:
7392 case MODE_VECTOR_FRACT:
7393 case MODE_VECTOR_UFRACT:
7394 case MODE_VECTOR_ACCUM:
7395 case MODE_VECTOR_UACCUM:
7396 warning (OPT_Wattributes, "specifying vector types with "
7397 "__attribute__ ((mode)) is deprecated");
7398 warning (OPT_Wattributes,
7399 "use __attribute__ ((vector_size)) instead");
7400 valid_mode = vector_mode_valid_p (mode);
7401 break;
7403 default:
7404 break;
7406 if (!valid_mode)
7408 error ("unable to emulate %qs", p);
7409 return NULL_TREE;
7412 if (POINTER_TYPE_P (type))
7414 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7415 tree (*fn)(tree, enum machine_mode, bool);
7417 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7419 error ("invalid pointer mode %qs", p);
7420 return NULL_TREE;
7423 if (TREE_CODE (type) == POINTER_TYPE)
7424 fn = build_pointer_type_for_mode;
7425 else
7426 fn = build_reference_type_for_mode;
7427 typefm = fn (TREE_TYPE (type), mode, false);
7429 else
7431 /* For fixed-point modes, we need to test if the signness of type
7432 and the machine mode are consistent. */
7433 if (ALL_FIXED_POINT_MODE_P (mode)
7434 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7436 error ("signedness of type and machine mode %qs don%'t match", p);
7437 return NULL_TREE;
7439 /* For fixed-point modes, we need to pass saturating info. */
7440 typefm = lang_hooks.types.type_for_mode (mode,
7441 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7442 : TYPE_UNSIGNED (type));
7445 if (typefm == NULL_TREE)
7447 error ("no data type for mode %qs", p);
7448 return NULL_TREE;
7450 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7452 /* For enumeral types, copy the precision from the integer
7453 type returned above. If not an INTEGER_TYPE, we can't use
7454 this mode for this type. */
7455 if (TREE_CODE (typefm) != INTEGER_TYPE)
7457 error ("cannot use mode %qs for enumeral types", p);
7458 return NULL_TREE;
7461 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7463 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7464 typefm = type;
7466 else
7468 /* We cannot build a type variant, as there's code that assumes
7469 that TYPE_MAIN_VARIANT has the same mode. This includes the
7470 debug generators. Instead, create a subrange type. This
7471 results in all of the enumeral values being emitted only once
7472 in the original, and the subtype gets them by reference. */
7473 if (TYPE_UNSIGNED (type))
7474 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7475 else
7476 typefm = make_signed_type (TYPE_PRECISION (typefm));
7477 TREE_TYPE (typefm) = type;
7480 else if (VECTOR_MODE_P (mode)
7481 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7482 : TREE_CODE (type) != TREE_CODE (typefm))
7484 error ("mode %qs applied to inappropriate type", p);
7485 return NULL_TREE;
7488 *node = typefm;
7491 return NULL_TREE;
7494 /* Handle a "section" attribute; arguments as in
7495 struct attribute_spec.handler. */
7497 static tree
7498 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7499 int ARG_UNUSED (flags), bool *no_add_attrs)
7501 tree decl = *node;
7503 if (targetm_common.have_named_sections)
7505 user_defined_section_attribute = true;
7507 if ((TREE_CODE (decl) == FUNCTION_DECL
7508 || TREE_CODE (decl) == VAR_DECL)
7509 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7511 if (TREE_CODE (decl) == VAR_DECL
7512 && current_function_decl != NULL_TREE
7513 && !TREE_STATIC (decl))
7515 error_at (DECL_SOURCE_LOCATION (decl),
7516 "section attribute cannot be specified for "
7517 "local variables");
7518 *no_add_attrs = true;
7521 /* The decl may have already been given a section attribute
7522 from a previous declaration. Ensure they match. */
7523 else if (DECL_SECTION_NAME (decl) != NULL
7524 && strcmp (DECL_SECTION_NAME (decl),
7525 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7527 error ("section of %q+D conflicts with previous declaration",
7528 *node);
7529 *no_add_attrs = true;
7531 else if (TREE_CODE (decl) == VAR_DECL
7532 && !targetm.have_tls && targetm.emutls.tmpl_section
7533 && DECL_THREAD_LOCAL_P (decl))
7535 error ("section of %q+D cannot be overridden", *node);
7536 *no_add_attrs = true;
7538 else
7539 set_decl_section_name (decl,
7540 TREE_STRING_POINTER (TREE_VALUE (args)));
7542 else
7544 error ("section attribute not allowed for %q+D", *node);
7545 *no_add_attrs = true;
7548 else
7550 error_at (DECL_SOURCE_LOCATION (*node),
7551 "section attributes are not supported for this target");
7552 *no_add_attrs = true;
7555 return NULL_TREE;
7558 /* Check whether ALIGN is a valid user-specified alignment. If so,
7559 return its base-2 log; if not, output an error and return -1. If
7560 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7561 no error. */
7563 check_user_alignment (const_tree align, bool allow_zero)
7565 int i;
7567 if (error_operand_p (align))
7568 return -1;
7569 if (TREE_CODE (align) != INTEGER_CST
7570 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7572 error ("requested alignment is not an integer constant");
7573 return -1;
7575 else if (allow_zero && integer_zerop (align))
7576 return -1;
7577 else if (tree_int_cst_sgn (align) == -1
7578 || (i = tree_log2 (align)) == -1)
7580 error ("requested alignment is not a positive power of 2");
7581 return -1;
7583 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7585 error ("requested alignment is too large");
7586 return -1;
7588 return i;
7592 If in c++-11, check if the c++-11 alignment constraint with respect
7593 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7594 c++-11 mode, does nothing.
7596 [dcl.align]2/ says:
7598 [* if the constant expression evaluates to a fundamental alignment,
7599 the alignment requirement of the declared entity shall be the
7600 specified fundamental alignment.
7602 * if the constant expression evaluates to an extended alignment
7603 and the implementation supports that alignment in the context
7604 of the declaration, the alignment of the declared entity shall
7605 be that alignment
7607 * if the constant expression evaluates to an extended alignment
7608 and the implementation does not support that alignment in the
7609 context of the declaration, the program is ill-formed]. */
7611 static bool
7612 check_cxx_fundamental_alignment_constraints (tree node,
7613 unsigned align_log,
7614 int flags)
7616 bool alignment_too_large_p = false;
7617 unsigned requested_alignment = 1U << align_log;
7618 unsigned max_align = 0;
7620 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7621 || (node == NULL_TREE || node == error_mark_node))
7622 return true;
7624 if (cxx_fundamental_alignment_p (requested_alignment))
7625 return true;
7627 if (DECL_P (node))
7629 if (TREE_STATIC (node))
7631 /* For file scope variables and static members, the target
7632 supports alignments that are at most
7633 MAX_OFILE_ALIGNMENT. */
7634 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7635 alignment_too_large_p = true;
7637 else
7639 #ifdef BIGGEST_FIELD_ALIGNMENT
7640 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7641 #else
7642 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7643 #endif
7644 /* For non-static members, the target supports either
7645 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7646 if it is defined or BIGGEST_ALIGNMENT. */
7647 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7648 if (TREE_CODE (node) == FIELD_DECL
7649 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7650 alignment_too_large_p = true;
7651 #undef MAX_TARGET_FIELD_ALIGNMENT
7652 /* For stack variables, the target supports at most
7653 MAX_STACK_ALIGNMENT. */
7654 else if (decl_function_context (node) != NULL
7655 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7656 alignment_too_large_p = true;
7659 else if (TYPE_P (node))
7661 /* Let's be liberal for types. */
7662 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7663 alignment_too_large_p = true;
7666 if (alignment_too_large_p)
7667 pedwarn (input_location, OPT_Wattributes,
7668 "requested alignment %d is larger than %d",
7669 requested_alignment, max_align);
7671 return !alignment_too_large_p;
7674 /* Handle a "aligned" attribute; arguments as in
7675 struct attribute_spec.handler. */
7677 static tree
7678 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7679 int flags, bool *no_add_attrs)
7681 tree decl = NULL_TREE;
7682 tree *type = NULL;
7683 int is_type = 0;
7684 tree align_expr;
7685 int i;
7687 if (args)
7689 align_expr = TREE_VALUE (args);
7690 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7691 && TREE_CODE (align_expr) != FUNCTION_DECL)
7692 align_expr = default_conversion (align_expr);
7694 else
7695 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7697 if (DECL_P (*node))
7699 decl = *node;
7700 type = &TREE_TYPE (decl);
7701 is_type = TREE_CODE (*node) == TYPE_DECL;
7703 else if (TYPE_P (*node))
7704 type = node, is_type = 1;
7706 if ((i = check_user_alignment (align_expr, false)) == -1
7707 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7708 *no_add_attrs = true;
7709 else if (is_type)
7711 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7712 /* OK, modify the type in place. */;
7713 /* If we have a TYPE_DECL, then copy the type, so that we
7714 don't accidentally modify a builtin type. See pushdecl. */
7715 else if (decl && TREE_TYPE (decl) != error_mark_node
7716 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7718 tree tt = TREE_TYPE (decl);
7719 *type = build_variant_type_copy (*type);
7720 DECL_ORIGINAL_TYPE (decl) = tt;
7721 TYPE_NAME (*type) = decl;
7722 TREE_USED (*type) = TREE_USED (decl);
7723 TREE_TYPE (decl) = *type;
7725 else
7726 *type = build_variant_type_copy (*type);
7728 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7729 TYPE_USER_ALIGN (*type) = 1;
7731 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7732 && TREE_CODE (decl) != FIELD_DECL)
7734 error ("alignment may not be specified for %q+D", decl);
7735 *no_add_attrs = true;
7737 else if (DECL_USER_ALIGN (decl)
7738 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7739 /* C++-11 [dcl.align/4]:
7741 When multiple alignment-specifiers are specified for an
7742 entity, the alignment requirement shall be set to the
7743 strictest specified alignment.
7745 This formally comes from the c++11 specification but we are
7746 doing it for the GNU attribute syntax as well. */
7747 *no_add_attrs = true;
7748 else if (TREE_CODE (decl) == FUNCTION_DECL
7749 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7751 if (DECL_USER_ALIGN (decl))
7752 error ("alignment for %q+D was previously specified as %d "
7753 "and may not be decreased", decl,
7754 DECL_ALIGN (decl) / BITS_PER_UNIT);
7755 else
7756 error ("alignment for %q+D must be at least %d", decl,
7757 DECL_ALIGN (decl) / BITS_PER_UNIT);
7758 *no_add_attrs = true;
7760 else
7762 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7763 DECL_USER_ALIGN (decl) = 1;
7766 return NULL_TREE;
7769 /* Handle a "weak" attribute; arguments as in
7770 struct attribute_spec.handler. */
7772 static tree
7773 handle_weak_attribute (tree *node, tree name,
7774 tree ARG_UNUSED (args),
7775 int ARG_UNUSED (flags),
7776 bool * ARG_UNUSED (no_add_attrs))
7778 if (TREE_CODE (*node) == FUNCTION_DECL
7779 && DECL_DECLARED_INLINE_P (*node))
7781 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7782 *no_add_attrs = true;
7784 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7786 error ("indirect function %q+D cannot be declared weak", *node);
7787 *no_add_attrs = true;
7788 return NULL_TREE;
7790 else if (TREE_CODE (*node) == FUNCTION_DECL
7791 || TREE_CODE (*node) == VAR_DECL)
7792 declare_weak (*node);
7793 else
7794 warning (OPT_Wattributes, "%qE attribute ignored", name);
7796 return NULL_TREE;
7799 /* Handle an "alias" or "ifunc" attribute; arguments as in
7800 struct attribute_spec.handler, except that IS_ALIAS tells us
7801 whether this is an alias as opposed to ifunc attribute. */
7803 static tree
7804 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7805 bool *no_add_attrs)
7807 tree decl = *node;
7809 if (TREE_CODE (decl) != FUNCTION_DECL
7810 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7812 warning (OPT_Wattributes, "%qE attribute ignored", name);
7813 *no_add_attrs = true;
7815 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7816 || (TREE_CODE (decl) != FUNCTION_DECL
7817 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7818 /* A static variable declaration is always a tentative definition,
7819 but the alias is a non-tentative definition which overrides. */
7820 || (TREE_CODE (decl) != FUNCTION_DECL
7821 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7823 error ("%q+D defined both normally and as %qE attribute", decl, name);
7824 *no_add_attrs = true;
7825 return NULL_TREE;
7827 else if (!is_alias
7828 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7829 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7831 error ("weak %q+D cannot be defined %qE", decl, name);
7832 *no_add_attrs = true;
7833 return NULL_TREE;
7836 /* Note that the very first time we process a nested declaration,
7837 decl_function_context will not be set. Indeed, *would* never
7838 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7839 we do below. After such frobbery, pushdecl would set the context.
7840 In any case, this is never what we want. */
7841 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7843 tree id;
7845 id = TREE_VALUE (args);
7846 if (TREE_CODE (id) != STRING_CST)
7848 error ("attribute %qE argument not a string", name);
7849 *no_add_attrs = true;
7850 return NULL_TREE;
7852 id = get_identifier (TREE_STRING_POINTER (id));
7853 /* This counts as a use of the object pointed to. */
7854 TREE_USED (id) = 1;
7856 if (TREE_CODE (decl) == FUNCTION_DECL)
7857 DECL_INITIAL (decl) = error_mark_node;
7858 else
7859 TREE_STATIC (decl) = 1;
7861 if (!is_alias)
7862 /* ifuncs are also aliases, so set that attribute too. */
7863 DECL_ATTRIBUTES (decl)
7864 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7866 else
7868 warning (OPT_Wattributes, "%qE attribute ignored", name);
7869 *no_add_attrs = true;
7872 return NULL_TREE;
7875 /* Handle an "alias" or "ifunc" attribute; arguments as in
7876 struct attribute_spec.handler. */
7878 static tree
7879 handle_ifunc_attribute (tree *node, tree name, tree args,
7880 int ARG_UNUSED (flags), bool *no_add_attrs)
7882 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7885 /* Handle an "alias" or "ifunc" attribute; arguments as in
7886 struct attribute_spec.handler. */
7888 static tree
7889 handle_alias_attribute (tree *node, tree name, tree args,
7890 int ARG_UNUSED (flags), bool *no_add_attrs)
7892 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7895 /* Handle a "weakref" attribute; arguments as in struct
7896 attribute_spec.handler. */
7898 static tree
7899 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7900 int flags, bool *no_add_attrs)
7902 tree attr = NULL_TREE;
7904 /* We must ignore the attribute when it is associated with
7905 local-scoped decls, since attribute alias is ignored and many
7906 such symbols do not even have a DECL_WEAK field. */
7907 if (decl_function_context (*node)
7908 || current_function_decl
7909 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7911 warning (OPT_Wattributes, "%qE attribute ignored", name);
7912 *no_add_attrs = true;
7913 return NULL_TREE;
7916 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7918 error ("indirect function %q+D cannot be declared weakref", *node);
7919 *no_add_attrs = true;
7920 return NULL_TREE;
7923 /* The idea here is that `weakref("name")' mutates into `weakref,
7924 alias("name")', and weakref without arguments, in turn,
7925 implicitly adds weak. */
7927 if (args)
7929 attr = tree_cons (get_identifier ("alias"), args, attr);
7930 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7932 *no_add_attrs = true;
7934 decl_attributes (node, attr, flags);
7936 else
7938 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7939 error_at (DECL_SOURCE_LOCATION (*node),
7940 "weakref attribute must appear before alias attribute");
7942 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7943 and that isn't supported; and because it wants to add it to
7944 the list of weak decls, which isn't helpful. */
7945 DECL_WEAK (*node) = 1;
7948 return NULL_TREE;
7951 /* Handle an "visibility" attribute; arguments as in
7952 struct attribute_spec.handler. */
7954 static tree
7955 handle_visibility_attribute (tree *node, tree name, tree args,
7956 int ARG_UNUSED (flags),
7957 bool *ARG_UNUSED (no_add_attrs))
7959 tree decl = *node;
7960 tree id = TREE_VALUE (args);
7961 enum symbol_visibility vis;
7963 if (TYPE_P (*node))
7965 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7966 /* OK */;
7967 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7969 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7970 name);
7971 return NULL_TREE;
7973 else if (TYPE_FIELDS (*node))
7975 error ("%qE attribute ignored because %qT is already defined",
7976 name, *node);
7977 return NULL_TREE;
7980 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7982 warning (OPT_Wattributes, "%qE attribute ignored", name);
7983 return NULL_TREE;
7986 if (TREE_CODE (id) != STRING_CST)
7988 error ("visibility argument not a string");
7989 return NULL_TREE;
7992 /* If this is a type, set the visibility on the type decl. */
7993 if (TYPE_P (decl))
7995 decl = TYPE_NAME (decl);
7996 if (!decl)
7997 return NULL_TREE;
7998 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8000 warning (OPT_Wattributes, "%qE attribute ignored on types",
8001 name);
8002 return NULL_TREE;
8006 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8007 vis = VISIBILITY_DEFAULT;
8008 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8009 vis = VISIBILITY_INTERNAL;
8010 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8011 vis = VISIBILITY_HIDDEN;
8012 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8013 vis = VISIBILITY_PROTECTED;
8014 else
8016 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8017 vis = VISIBILITY_DEFAULT;
8020 if (DECL_VISIBILITY_SPECIFIED (decl)
8021 && vis != DECL_VISIBILITY (decl))
8023 tree attributes = (TYPE_P (*node)
8024 ? TYPE_ATTRIBUTES (*node)
8025 : DECL_ATTRIBUTES (decl));
8026 if (lookup_attribute ("visibility", attributes))
8027 error ("%qD redeclared with different visibility", decl);
8028 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8029 && lookup_attribute ("dllimport", attributes))
8030 error ("%qD was declared %qs which implies default visibility",
8031 decl, "dllimport");
8032 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8033 && lookup_attribute ("dllexport", attributes))
8034 error ("%qD was declared %qs which implies default visibility",
8035 decl, "dllexport");
8038 DECL_VISIBILITY (decl) = vis;
8039 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8041 /* Go ahead and attach the attribute to the node as well. This is needed
8042 so we can determine whether we have VISIBILITY_DEFAULT because the
8043 visibility was not specified, or because it was explicitly overridden
8044 from the containing scope. */
8046 return NULL_TREE;
8049 /* Determine the ELF symbol visibility for DECL, which is either a
8050 variable or a function. It is an error to use this function if a
8051 definition of DECL is not available in this translation unit.
8052 Returns true if the final visibility has been determined by this
8053 function; false if the caller is free to make additional
8054 modifications. */
8056 bool
8057 c_determine_visibility (tree decl)
8059 gcc_assert (TREE_CODE (decl) == VAR_DECL
8060 || TREE_CODE (decl) == FUNCTION_DECL);
8062 /* If the user explicitly specified the visibility with an
8063 attribute, honor that. DECL_VISIBILITY will have been set during
8064 the processing of the attribute. We check for an explicit
8065 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8066 to distinguish the use of an attribute from the use of a "#pragma
8067 GCC visibility push(...)"; in the latter case we still want other
8068 considerations to be able to overrule the #pragma. */
8069 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8070 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8071 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8072 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8073 return true;
8075 /* Set default visibility to whatever the user supplied with
8076 visibility_specified depending on #pragma GCC visibility. */
8077 if (!DECL_VISIBILITY_SPECIFIED (decl))
8079 if (visibility_options.inpragma
8080 || DECL_VISIBILITY (decl) != default_visibility)
8082 DECL_VISIBILITY (decl) = default_visibility;
8083 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8084 /* If visibility changed and DECL already has DECL_RTL, ensure
8085 symbol flags are updated. */
8086 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8087 || TREE_CODE (decl) == FUNCTION_DECL)
8088 && DECL_RTL_SET_P (decl))
8089 make_decl_rtl (decl);
8092 return false;
8095 /* Handle an "tls_model" attribute; arguments as in
8096 struct attribute_spec.handler. */
8098 static tree
8099 handle_tls_model_attribute (tree *node, tree name, tree args,
8100 int ARG_UNUSED (flags), bool *no_add_attrs)
8102 tree id;
8103 tree decl = *node;
8104 enum tls_model kind;
8106 *no_add_attrs = true;
8108 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8110 warning (OPT_Wattributes, "%qE attribute ignored", name);
8111 return NULL_TREE;
8114 kind = DECL_TLS_MODEL (decl);
8115 id = TREE_VALUE (args);
8116 if (TREE_CODE (id) != STRING_CST)
8118 error ("tls_model argument not a string");
8119 return NULL_TREE;
8122 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8123 kind = TLS_MODEL_LOCAL_EXEC;
8124 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8125 kind = TLS_MODEL_INITIAL_EXEC;
8126 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8127 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8128 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8129 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8130 else
8131 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8133 set_decl_tls_model (decl, kind);
8134 return NULL_TREE;
8137 /* Handle a "no_instrument_function" attribute; arguments as in
8138 struct attribute_spec.handler. */
8140 static tree
8141 handle_no_instrument_function_attribute (tree *node, tree name,
8142 tree ARG_UNUSED (args),
8143 int ARG_UNUSED (flags),
8144 bool *no_add_attrs)
8146 tree decl = *node;
8148 if (TREE_CODE (decl) != FUNCTION_DECL)
8150 error_at (DECL_SOURCE_LOCATION (decl),
8151 "%qE attribute applies only to functions", name);
8152 *no_add_attrs = true;
8154 else
8155 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8157 return NULL_TREE;
8160 /* Handle a "malloc" attribute; arguments as in
8161 struct attribute_spec.handler. */
8163 static tree
8164 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8165 int ARG_UNUSED (flags), bool *no_add_attrs)
8167 if (TREE_CODE (*node) == FUNCTION_DECL
8168 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8169 DECL_IS_MALLOC (*node) = 1;
8170 else
8172 warning (OPT_Wattributes, "%qE attribute ignored", name);
8173 *no_add_attrs = true;
8176 return NULL_TREE;
8179 /* Handle a "alloc_size" attribute; arguments as in
8180 struct attribute_spec.handler. */
8182 static tree
8183 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8184 int ARG_UNUSED (flags), bool *no_add_attrs)
8186 unsigned arg_count = type_num_arguments (*node);
8187 for (; args; args = TREE_CHAIN (args))
8189 tree position = TREE_VALUE (args);
8190 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8191 && TREE_CODE (position) != FUNCTION_DECL)
8192 position = default_conversion (position);
8194 if (!tree_fits_uhwi_p (position)
8195 || !arg_count
8196 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8198 warning (OPT_Wattributes,
8199 "alloc_size parameter outside range");
8200 *no_add_attrs = true;
8201 return NULL_TREE;
8204 return NULL_TREE;
8207 /* Handle a "alloc_align" attribute; arguments as in
8208 struct attribute_spec.handler. */
8210 static tree
8211 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8212 bool *no_add_attrs)
8214 unsigned arg_count = type_num_arguments (*node);
8215 tree position = TREE_VALUE (args);
8216 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8217 position = default_conversion (position);
8219 if (!tree_fits_uhwi_p (position)
8220 || !arg_count
8221 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8223 warning (OPT_Wattributes,
8224 "alloc_align parameter outside range");
8225 *no_add_attrs = true;
8226 return NULL_TREE;
8228 return NULL_TREE;
8231 /* Handle a "assume_aligned" attribute; arguments as in
8232 struct attribute_spec.handler. */
8234 static tree
8235 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8236 bool *no_add_attrs)
8238 for (; args; args = TREE_CHAIN (args))
8240 tree position = TREE_VALUE (args);
8241 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8242 && TREE_CODE (position) != FUNCTION_DECL)
8243 position = default_conversion (position);
8245 if (TREE_CODE (position) != INTEGER_CST)
8247 warning (OPT_Wattributes,
8248 "assume_aligned parameter not integer constant");
8249 *no_add_attrs = true;
8250 return NULL_TREE;
8253 return NULL_TREE;
8256 /* Handle a "fn spec" attribute; arguments as in
8257 struct attribute_spec.handler. */
8259 static tree
8260 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8261 tree args, int ARG_UNUSED (flags),
8262 bool *no_add_attrs ATTRIBUTE_UNUSED)
8264 gcc_assert (args
8265 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8266 && !TREE_CHAIN (args));
8267 return NULL_TREE;
8270 /* Handle a "warn_unused" attribute; arguments as in
8271 struct attribute_spec.handler. */
8273 static tree
8274 handle_warn_unused_attribute (tree *node, tree name,
8275 tree args ATTRIBUTE_UNUSED,
8276 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8278 if (TYPE_P (*node))
8279 /* Do nothing else, just set the attribute. We'll get at
8280 it later with lookup_attribute. */
8282 else
8284 warning (OPT_Wattributes, "%qE attribute ignored", name);
8285 *no_add_attrs = true;
8288 return NULL_TREE;
8291 /* Handle an "omp declare simd" attribute; arguments as in
8292 struct attribute_spec.handler. */
8294 static tree
8295 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8297 return NULL_TREE;
8300 /* Handle an "omp declare target" attribute; arguments as in
8301 struct attribute_spec.handler. */
8303 static tree
8304 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8306 return NULL_TREE;
8309 /* Handle a "returns_twice" attribute; arguments as in
8310 struct attribute_spec.handler. */
8312 static tree
8313 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8314 int ARG_UNUSED (flags), bool *no_add_attrs)
8316 if (TREE_CODE (*node) == FUNCTION_DECL)
8317 DECL_IS_RETURNS_TWICE (*node) = 1;
8318 else
8320 warning (OPT_Wattributes, "%qE attribute ignored", name);
8321 *no_add_attrs = true;
8324 return NULL_TREE;
8327 /* Handle a "no_limit_stack" attribute; arguments as in
8328 struct attribute_spec.handler. */
8330 static tree
8331 handle_no_limit_stack_attribute (tree *node, tree name,
8332 tree ARG_UNUSED (args),
8333 int ARG_UNUSED (flags),
8334 bool *no_add_attrs)
8336 tree decl = *node;
8338 if (TREE_CODE (decl) != FUNCTION_DECL)
8340 error_at (DECL_SOURCE_LOCATION (decl),
8341 "%qE attribute applies only to functions", name);
8342 *no_add_attrs = true;
8344 else if (DECL_INITIAL (decl))
8346 error_at (DECL_SOURCE_LOCATION (decl),
8347 "can%'t set %qE attribute after definition", name);
8348 *no_add_attrs = true;
8350 else
8351 DECL_NO_LIMIT_STACK (decl) = 1;
8353 return NULL_TREE;
8356 /* Handle a "pure" attribute; arguments as in
8357 struct attribute_spec.handler. */
8359 static tree
8360 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8361 int ARG_UNUSED (flags), bool *no_add_attrs)
8363 if (TREE_CODE (*node) == FUNCTION_DECL)
8364 DECL_PURE_P (*node) = 1;
8365 /* ??? TODO: Support types. */
8366 else
8368 warning (OPT_Wattributes, "%qE attribute ignored", name);
8369 *no_add_attrs = true;
8372 return NULL_TREE;
8375 /* Digest an attribute list destined for a transactional memory statement.
8376 ALLOWED is the set of attributes that are allowed for this statement;
8377 return the attribute we parsed. Multiple attributes are never allowed. */
8380 parse_tm_stmt_attr (tree attrs, int allowed)
8382 tree a_seen = NULL;
8383 int m_seen = 0;
8385 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8387 tree a = TREE_PURPOSE (attrs);
8388 int m = 0;
8390 if (is_attribute_p ("outer", a))
8391 m = TM_STMT_ATTR_OUTER;
8393 if ((m & allowed) == 0)
8395 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8396 continue;
8399 if (m_seen == 0)
8401 a_seen = a;
8402 m_seen = m;
8404 else if (m_seen == m)
8405 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8406 else
8407 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8410 return m_seen;
8413 /* Transform a TM attribute name into a maskable integer and back.
8414 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8415 to how the lack of an attribute is treated. */
8418 tm_attr_to_mask (tree attr)
8420 if (attr == NULL)
8421 return 0;
8422 if (is_attribute_p ("transaction_safe", attr))
8423 return TM_ATTR_SAFE;
8424 if (is_attribute_p ("transaction_callable", attr))
8425 return TM_ATTR_CALLABLE;
8426 if (is_attribute_p ("transaction_pure", attr))
8427 return TM_ATTR_PURE;
8428 if (is_attribute_p ("transaction_unsafe", attr))
8429 return TM_ATTR_IRREVOCABLE;
8430 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8431 return TM_ATTR_MAY_CANCEL_OUTER;
8432 return 0;
8435 tree
8436 tm_mask_to_attr (int mask)
8438 const char *str;
8439 switch (mask)
8441 case TM_ATTR_SAFE:
8442 str = "transaction_safe";
8443 break;
8444 case TM_ATTR_CALLABLE:
8445 str = "transaction_callable";
8446 break;
8447 case TM_ATTR_PURE:
8448 str = "transaction_pure";
8449 break;
8450 case TM_ATTR_IRREVOCABLE:
8451 str = "transaction_unsafe";
8452 break;
8453 case TM_ATTR_MAY_CANCEL_OUTER:
8454 str = "transaction_may_cancel_outer";
8455 break;
8456 default:
8457 gcc_unreachable ();
8459 return get_identifier (str);
8462 /* Return the first TM attribute seen in LIST. */
8464 tree
8465 find_tm_attribute (tree list)
8467 for (; list ; list = TREE_CHAIN (list))
8469 tree name = TREE_PURPOSE (list);
8470 if (tm_attr_to_mask (name) != 0)
8471 return name;
8473 return NULL_TREE;
8476 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8477 Here we accept only function types, and verify that none of the other
8478 function TM attributes are also applied. */
8479 /* ??? We need to accept class types for C++, but not C. This greatly
8480 complicates this function, since we can no longer rely on the extra
8481 processing given by function_type_required. */
8483 static tree
8484 handle_tm_attribute (tree *node, tree name, tree args,
8485 int flags, bool *no_add_attrs)
8487 /* Only one path adds the attribute; others don't. */
8488 *no_add_attrs = true;
8490 switch (TREE_CODE (*node))
8492 case RECORD_TYPE:
8493 case UNION_TYPE:
8494 /* Only tm_callable and tm_safe apply to classes. */
8495 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8496 goto ignored;
8497 /* FALLTHRU */
8499 case FUNCTION_TYPE:
8500 case METHOD_TYPE:
8502 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8503 if (old_name == name)
8505 else if (old_name != NULL_TREE)
8506 error ("type was previously declared %qE", old_name);
8507 else
8508 *no_add_attrs = false;
8510 break;
8512 case POINTER_TYPE:
8514 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8515 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8517 tree fn_tmp = TREE_TYPE (*node);
8518 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8519 *node = build_pointer_type (fn_tmp);
8520 break;
8523 /* FALLTHRU */
8525 default:
8526 /* If a function is next, pass it on to be tried next. */
8527 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8528 return tree_cons (name, args, NULL);
8530 ignored:
8531 warning (OPT_Wattributes, "%qE attribute ignored", name);
8532 break;
8535 return NULL_TREE;
8538 /* Handle the TM_WRAP attribute; arguments as in
8539 struct attribute_spec.handler. */
8541 static tree
8542 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8543 int ARG_UNUSED (flags), bool *no_add_attrs)
8545 tree decl = *node;
8547 /* We don't need the attribute even on success, since we
8548 record the entry in an external table. */
8549 *no_add_attrs = true;
8551 if (TREE_CODE (decl) != FUNCTION_DECL)
8552 warning (OPT_Wattributes, "%qE attribute ignored", name);
8553 else
8555 tree wrap_decl = TREE_VALUE (args);
8556 if (error_operand_p (wrap_decl))
8558 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8559 && TREE_CODE (wrap_decl) != VAR_DECL
8560 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8561 error ("%qE argument not an identifier", name);
8562 else
8564 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8565 wrap_decl = lookup_name (wrap_decl);
8566 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8568 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8569 TREE_TYPE (wrap_decl)))
8570 record_tm_replacement (wrap_decl, decl);
8571 else
8572 error ("%qD is not compatible with %qD", wrap_decl, decl);
8574 else
8575 error ("%qE argument is not a function", name);
8579 return NULL_TREE;
8582 /* Ignore the given attribute. Used when this attribute may be usefully
8583 overridden by the target, but is not used generically. */
8585 static tree
8586 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8587 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8588 bool *no_add_attrs)
8590 *no_add_attrs = true;
8591 return NULL_TREE;
8594 /* Handle a "no vops" attribute; arguments as in
8595 struct attribute_spec.handler. */
8597 static tree
8598 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8599 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8600 bool *ARG_UNUSED (no_add_attrs))
8602 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8603 DECL_IS_NOVOPS (*node) = 1;
8604 return NULL_TREE;
8607 /* Handle a "deprecated" attribute; arguments as in
8608 struct attribute_spec.handler. */
8610 static tree
8611 handle_deprecated_attribute (tree *node, tree name,
8612 tree args, int flags,
8613 bool *no_add_attrs)
8615 tree type = NULL_TREE;
8616 int warn = 0;
8617 tree what = NULL_TREE;
8619 if (!args)
8620 *no_add_attrs = true;
8621 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8623 error ("deprecated message is not a string");
8624 *no_add_attrs = true;
8627 if (DECL_P (*node))
8629 tree decl = *node;
8630 type = TREE_TYPE (decl);
8632 if (TREE_CODE (decl) == TYPE_DECL
8633 || TREE_CODE (decl) == PARM_DECL
8634 || TREE_CODE (decl) == VAR_DECL
8635 || TREE_CODE (decl) == FUNCTION_DECL
8636 || TREE_CODE (decl) == FIELD_DECL
8637 || objc_method_decl (TREE_CODE (decl)))
8638 TREE_DEPRECATED (decl) = 1;
8639 else
8640 warn = 1;
8642 else if (TYPE_P (*node))
8644 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8645 *node = build_variant_type_copy (*node);
8646 TREE_DEPRECATED (*node) = 1;
8647 type = *node;
8649 else
8650 warn = 1;
8652 if (warn)
8654 *no_add_attrs = true;
8655 if (type && TYPE_NAME (type))
8657 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8658 what = TYPE_NAME (*node);
8659 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8660 && DECL_NAME (TYPE_NAME (type)))
8661 what = DECL_NAME (TYPE_NAME (type));
8663 if (what)
8664 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8665 else
8666 warning (OPT_Wattributes, "%qE attribute ignored", name);
8669 return NULL_TREE;
8672 /* Handle a "vector_size" attribute; arguments as in
8673 struct attribute_spec.handler. */
8675 static tree
8676 handle_vector_size_attribute (tree *node, tree name, tree args,
8677 int ARG_UNUSED (flags),
8678 bool *no_add_attrs)
8680 unsigned HOST_WIDE_INT vecsize, nunits;
8681 enum machine_mode orig_mode;
8682 tree type = *node, new_type, size;
8684 *no_add_attrs = true;
8686 size = TREE_VALUE (args);
8687 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8688 && TREE_CODE (size) != FUNCTION_DECL)
8689 size = default_conversion (size);
8691 if (!tree_fits_uhwi_p (size))
8693 warning (OPT_Wattributes, "%qE attribute ignored", name);
8694 return NULL_TREE;
8697 /* Get the vector size (in bytes). */
8698 vecsize = tree_to_uhwi (size);
8700 /* We need to provide for vector pointers, vector arrays, and
8701 functions returning vectors. For example:
8703 __attribute__((vector_size(16))) short *foo;
8705 In this case, the mode is SI, but the type being modified is
8706 HI, so we need to look further. */
8708 while (POINTER_TYPE_P (type)
8709 || TREE_CODE (type) == FUNCTION_TYPE
8710 || TREE_CODE (type) == METHOD_TYPE
8711 || TREE_CODE (type) == ARRAY_TYPE
8712 || TREE_CODE (type) == OFFSET_TYPE)
8713 type = TREE_TYPE (type);
8715 /* Get the mode of the type being modified. */
8716 orig_mode = TYPE_MODE (type);
8718 if ((!INTEGRAL_TYPE_P (type)
8719 && !SCALAR_FLOAT_TYPE_P (type)
8720 && !FIXED_POINT_TYPE_P (type))
8721 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8722 && GET_MODE_CLASS (orig_mode) != MODE_INT
8723 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8724 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8725 || TREE_CODE (type) == BOOLEAN_TYPE)
8727 error ("invalid vector type for attribute %qE", name);
8728 return NULL_TREE;
8731 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8733 error ("vector size not an integral multiple of component size");
8734 return NULL;
8737 if (vecsize == 0)
8739 error ("zero vector size");
8740 return NULL;
8743 /* Calculate how many units fit in the vector. */
8744 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8745 if (nunits & (nunits - 1))
8747 error ("number of components of the vector not a power of two");
8748 return NULL_TREE;
8751 new_type = build_vector_type (type, nunits);
8753 /* Build back pointers if needed. */
8754 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8756 return NULL_TREE;
8759 /* Handle the "nonnull" attribute. */
8760 static tree
8761 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8762 tree args, int ARG_UNUSED (flags),
8763 bool *no_add_attrs)
8765 tree type = *node;
8766 unsigned HOST_WIDE_INT attr_arg_num;
8768 /* If no arguments are specified, all pointer arguments should be
8769 non-null. Verify a full prototype is given so that the arguments
8770 will have the correct types when we actually check them later. */
8771 if (!args)
8773 if (!prototype_p (type))
8775 error ("nonnull attribute without arguments on a non-prototype");
8776 *no_add_attrs = true;
8778 return NULL_TREE;
8781 /* Argument list specified. Verify that each argument number references
8782 a pointer argument. */
8783 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8785 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8787 tree arg = TREE_VALUE (args);
8788 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8789 && TREE_CODE (arg) != FUNCTION_DECL)
8790 arg = default_conversion (arg);
8792 if (!get_nonnull_operand (arg, &arg_num))
8794 error ("nonnull argument has invalid operand number (argument %lu)",
8795 (unsigned long) attr_arg_num);
8796 *no_add_attrs = true;
8797 return NULL_TREE;
8800 if (prototype_p (type))
8802 function_args_iterator iter;
8803 tree argument;
8805 function_args_iter_init (&iter, type);
8806 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8808 argument = function_args_iter_cond (&iter);
8809 if (argument == NULL_TREE || ck_num == arg_num)
8810 break;
8813 if (!argument
8814 || TREE_CODE (argument) == VOID_TYPE)
8816 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8817 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8818 *no_add_attrs = true;
8819 return NULL_TREE;
8822 if (TREE_CODE (argument) != POINTER_TYPE)
8824 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8825 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8826 *no_add_attrs = true;
8827 return NULL_TREE;
8832 return NULL_TREE;
8835 /* Check the argument list of a function call for null in argument slots
8836 that are marked as requiring a non-null pointer argument. The NARGS
8837 arguments are passed in the array ARGARRAY.
8840 static void
8841 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8843 tree a;
8844 int i;
8846 attrs = lookup_attribute ("nonnull", attrs);
8847 if (attrs == NULL_TREE)
8848 return;
8850 a = attrs;
8851 /* See if any of the nonnull attributes has no arguments. If so,
8852 then every pointer argument is checked (in which case the check
8853 for pointer type is done in check_nonnull_arg). */
8854 if (TREE_VALUE (a) != NULL_TREE)
8856 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8857 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8859 if (a != NULL_TREE)
8860 for (i = 0; i < nargs; i++)
8861 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8862 i + 1);
8863 else
8865 /* Walk the argument list. If we encounter an argument number we
8866 should check for non-null, do it. */
8867 for (i = 0; i < nargs; i++)
8869 for (a = attrs; ; a = TREE_CHAIN (a))
8871 a = lookup_attribute ("nonnull", a);
8872 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8873 break;
8876 if (a != NULL_TREE)
8877 check_function_arguments_recurse (check_nonnull_arg, NULL,
8878 argarray[i], i + 1);
8883 /* Check that the Nth argument of a function call (counting backwards
8884 from the end) is a (pointer)0. The NARGS arguments are passed in the
8885 array ARGARRAY. */
8887 static void
8888 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8890 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8892 if (attr)
8894 int len = 0;
8895 int pos = 0;
8896 tree sentinel;
8897 function_args_iterator iter;
8898 tree t;
8900 /* Skip over the named arguments. */
8901 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8903 if (len == nargs)
8904 break;
8905 len++;
8908 if (TREE_VALUE (attr))
8910 tree p = TREE_VALUE (TREE_VALUE (attr));
8911 pos = TREE_INT_CST_LOW (p);
8914 /* The sentinel must be one of the varargs, i.e.
8915 in position >= the number of fixed arguments. */
8916 if ((nargs - 1 - pos) < len)
8918 warning (OPT_Wformat_,
8919 "not enough variable arguments to fit a sentinel");
8920 return;
8923 /* Validate the sentinel. */
8924 sentinel = argarray[nargs - 1 - pos];
8925 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8926 || !integer_zerop (sentinel))
8927 /* Although __null (in C++) is only an integer we allow it
8928 nevertheless, as we are guaranteed that it's exactly
8929 as wide as a pointer, and we don't want to force
8930 users to cast the NULL they have written there.
8931 We warn with -Wstrict-null-sentinel, though. */
8932 && (warn_strict_null_sentinel || null_node != sentinel))
8933 warning (OPT_Wformat_, "missing sentinel in function call");
8937 /* Helper for check_function_nonnull; given a list of operands which
8938 must be non-null in ARGS, determine if operand PARAM_NUM should be
8939 checked. */
8941 static bool
8942 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8944 unsigned HOST_WIDE_INT arg_num = 0;
8946 for (; args; args = TREE_CHAIN (args))
8948 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8950 gcc_assert (found);
8952 if (arg_num == param_num)
8953 return true;
8955 return false;
8958 /* Check that the function argument PARAM (which is operand number
8959 PARAM_NUM) is non-null. This is called by check_function_nonnull
8960 via check_function_arguments_recurse. */
8962 static void
8963 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8964 unsigned HOST_WIDE_INT param_num)
8966 /* Just skip checking the argument if it's not a pointer. This can
8967 happen if the "nonnull" attribute was given without an operand
8968 list (which means to check every pointer argument). */
8970 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8971 return;
8973 if (integer_zerop (param))
8974 warning (OPT_Wnonnull, "null argument where non-null required "
8975 "(argument %lu)", (unsigned long) param_num);
8978 /* Helper for nonnull attribute handling; fetch the operand number
8979 from the attribute argument list. */
8981 static bool
8982 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8984 /* Verify the arg number is a small constant. */
8985 if (tree_fits_uhwi_p (arg_num_expr))
8987 *valp = TREE_INT_CST_LOW (arg_num_expr);
8988 return true;
8990 else
8991 return false;
8994 /* Handle a "nothrow" attribute; arguments as in
8995 struct attribute_spec.handler. */
8997 static tree
8998 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8999 int ARG_UNUSED (flags), bool *no_add_attrs)
9001 if (TREE_CODE (*node) == FUNCTION_DECL)
9002 TREE_NOTHROW (*node) = 1;
9003 /* ??? TODO: Support types. */
9004 else
9006 warning (OPT_Wattributes, "%qE attribute ignored", name);
9007 *no_add_attrs = true;
9010 return NULL_TREE;
9013 /* Handle a "cleanup" attribute; arguments as in
9014 struct attribute_spec.handler. */
9016 static tree
9017 handle_cleanup_attribute (tree *node, tree name, tree args,
9018 int ARG_UNUSED (flags), bool *no_add_attrs)
9020 tree decl = *node;
9021 tree cleanup_id, cleanup_decl;
9023 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9024 for global destructors in C++. This requires infrastructure that
9025 we don't have generically at the moment. It's also not a feature
9026 we'd be missing too much, since we do have attribute constructor. */
9027 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9029 warning (OPT_Wattributes, "%qE attribute ignored", name);
9030 *no_add_attrs = true;
9031 return NULL_TREE;
9034 /* Verify that the argument is a function in scope. */
9035 /* ??? We could support pointers to functions here as well, if
9036 that was considered desirable. */
9037 cleanup_id = TREE_VALUE (args);
9038 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9040 error ("cleanup argument not an identifier");
9041 *no_add_attrs = true;
9042 return NULL_TREE;
9044 cleanup_decl = lookup_name (cleanup_id);
9045 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9047 error ("cleanup argument not a function");
9048 *no_add_attrs = true;
9049 return NULL_TREE;
9052 /* That the function has proper type is checked with the
9053 eventual call to build_function_call. */
9055 return NULL_TREE;
9058 /* Handle a "warn_unused_result" attribute. No special handling. */
9060 static tree
9061 handle_warn_unused_result_attribute (tree *node, tree name,
9062 tree ARG_UNUSED (args),
9063 int ARG_UNUSED (flags), bool *no_add_attrs)
9065 /* Ignore the attribute for functions not returning any value. */
9066 if (VOID_TYPE_P (TREE_TYPE (*node)))
9068 warning (OPT_Wattributes, "%qE attribute ignored", name);
9069 *no_add_attrs = true;
9072 return NULL_TREE;
9075 /* Handle a "sentinel" attribute. */
9077 static tree
9078 handle_sentinel_attribute (tree *node, tree name, tree args,
9079 int ARG_UNUSED (flags), bool *no_add_attrs)
9081 if (!prototype_p (*node))
9083 warning (OPT_Wattributes,
9084 "%qE attribute requires prototypes with named arguments", name);
9085 *no_add_attrs = true;
9087 else
9089 if (!stdarg_p (*node))
9091 warning (OPT_Wattributes,
9092 "%qE attribute only applies to variadic functions", name);
9093 *no_add_attrs = true;
9097 if (args)
9099 tree position = TREE_VALUE (args);
9100 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9101 && TREE_CODE (position) != FUNCTION_DECL)
9102 position = default_conversion (position);
9104 if (TREE_CODE (position) != INTEGER_CST
9105 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9107 warning (OPT_Wattributes,
9108 "requested position is not an integer constant");
9109 *no_add_attrs = true;
9111 else
9113 if (tree_int_cst_lt (position, integer_zero_node))
9115 warning (OPT_Wattributes,
9116 "requested position is less than zero");
9117 *no_add_attrs = true;
9122 return NULL_TREE;
9125 /* Handle a "type_generic" attribute. */
9127 static tree
9128 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9129 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9130 bool * ARG_UNUSED (no_add_attrs))
9132 /* Ensure we have a function type. */
9133 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9135 /* Ensure we have a variadic function. */
9136 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9138 return NULL_TREE;
9141 /* Handle a "target" attribute. */
9143 static tree
9144 handle_target_attribute (tree *node, tree name, tree args, int flags,
9145 bool *no_add_attrs)
9147 /* Ensure we have a function type. */
9148 if (TREE_CODE (*node) != FUNCTION_DECL)
9150 warning (OPT_Wattributes, "%qE attribute ignored", name);
9151 *no_add_attrs = true;
9153 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9154 flags))
9155 *no_add_attrs = true;
9157 return NULL_TREE;
9160 /* Arguments being collected for optimization. */
9161 typedef const char *const_char_p; /* For DEF_VEC_P. */
9162 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9165 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9166 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9167 false for #pragma GCC optimize. */
9169 bool
9170 parse_optimize_options (tree args, bool attr_p)
9172 bool ret = true;
9173 unsigned opt_argc;
9174 unsigned i;
9175 int saved_flag_strict_aliasing;
9176 const char **opt_argv;
9177 struct cl_decoded_option *decoded_options;
9178 unsigned int decoded_options_count;
9179 tree ap;
9181 /* Build up argv vector. Just in case the string is stored away, use garbage
9182 collected strings. */
9183 vec_safe_truncate (optimize_args, 0);
9184 vec_safe_push (optimize_args, (const char *) NULL);
9186 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9188 tree value = TREE_VALUE (ap);
9190 if (TREE_CODE (value) == INTEGER_CST)
9192 char buffer[20];
9193 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9194 vec_safe_push (optimize_args, ggc_strdup (buffer));
9197 else if (TREE_CODE (value) == STRING_CST)
9199 /* Split string into multiple substrings. */
9200 size_t len = TREE_STRING_LENGTH (value);
9201 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9202 char *end = p + len;
9203 char *comma;
9204 char *next_p = p;
9206 while (next_p != NULL)
9208 size_t len2;
9209 char *q, *r;
9211 p = next_p;
9212 comma = strchr (p, ',');
9213 if (comma)
9215 len2 = comma - p;
9216 *comma = '\0';
9217 next_p = comma+1;
9219 else
9221 len2 = end - p;
9222 next_p = NULL;
9225 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9227 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9228 options. */
9229 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9231 ret = false;
9232 if (attr_p)
9233 warning (OPT_Wattributes,
9234 "bad option %s to optimize attribute", p);
9235 else
9236 warning (OPT_Wpragmas,
9237 "bad option %s to pragma attribute", p);
9238 continue;
9241 if (*p != '-')
9243 *r++ = '-';
9245 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9246 itself is -Os, and any other switch begins with a -f. */
9247 if ((*p >= '0' && *p <= '9')
9248 || (p[0] == 's' && p[1] == '\0'))
9249 *r++ = 'O';
9250 else if (*p != 'O')
9251 *r++ = 'f';
9254 memcpy (r, p, len2);
9255 r[len2] = '\0';
9256 vec_safe_push (optimize_args, (const char *) q);
9262 opt_argc = optimize_args->length ();
9263 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9265 for (i = 1; i < opt_argc; i++)
9266 opt_argv[i] = (*optimize_args)[i];
9268 saved_flag_strict_aliasing = flag_strict_aliasing;
9270 /* Now parse the options. */
9271 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9272 &decoded_options,
9273 &decoded_options_count);
9274 decode_options (&global_options, &global_options_set,
9275 decoded_options, decoded_options_count,
9276 input_location, global_dc);
9278 targetm.override_options_after_change();
9280 /* Don't allow changing -fstrict-aliasing. */
9281 flag_strict_aliasing = saved_flag_strict_aliasing;
9283 optimize_args->truncate (0);
9284 return ret;
9287 /* For handling "optimize" attribute. arguments as in
9288 struct attribute_spec.handler. */
9290 static tree
9291 handle_optimize_attribute (tree *node, tree name, tree args,
9292 int ARG_UNUSED (flags), bool *no_add_attrs)
9294 /* Ensure we have a function type. */
9295 if (TREE_CODE (*node) != FUNCTION_DECL)
9297 warning (OPT_Wattributes, "%qE attribute ignored", name);
9298 *no_add_attrs = true;
9300 else
9302 struct cl_optimization cur_opts;
9303 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9305 /* Save current options. */
9306 cl_optimization_save (&cur_opts, &global_options);
9308 /* If we previously had some optimization options, use them as the
9309 default. */
9310 if (old_opts)
9311 cl_optimization_restore (&global_options,
9312 TREE_OPTIMIZATION (old_opts));
9314 /* Parse options, and update the vector. */
9315 parse_optimize_options (args, true);
9316 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9317 = build_optimization_node (&global_options);
9319 /* Restore current options. */
9320 cl_optimization_restore (&global_options, &cur_opts);
9323 return NULL_TREE;
9326 /* Handle a "no_split_stack" attribute. */
9328 static tree
9329 handle_no_split_stack_attribute (tree *node, tree name,
9330 tree ARG_UNUSED (args),
9331 int ARG_UNUSED (flags),
9332 bool *no_add_attrs)
9334 tree decl = *node;
9336 if (TREE_CODE (decl) != FUNCTION_DECL)
9338 error_at (DECL_SOURCE_LOCATION (decl),
9339 "%qE attribute applies only to functions", name);
9340 *no_add_attrs = true;
9342 else if (DECL_INITIAL (decl))
9344 error_at (DECL_SOURCE_LOCATION (decl),
9345 "can%'t set %qE attribute after definition", name);
9346 *no_add_attrs = true;
9349 return NULL_TREE;
9352 /* Handle a "returns_nonnull" attribute; arguments as in
9353 struct attribute_spec.handler. */
9355 static tree
9356 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9357 bool *no_add_attrs)
9359 // Even without a prototype we still have a return type we can check.
9360 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9362 error ("returns_nonnull attribute on a function not returning a pointer");
9363 *no_add_attrs = true;
9365 return NULL_TREE;
9368 /* Handle a "designated_init" attribute; arguments as in
9369 struct attribute_spec.handler. */
9371 static tree
9372 handle_designated_init_attribute (tree *node, tree name, tree, int,
9373 bool *no_add_attrs)
9375 if (TREE_CODE (*node) != RECORD_TYPE)
9377 error ("%qE attribute is only valid on %<struct%> type", name);
9378 *no_add_attrs = true;
9380 return NULL_TREE;
9384 /* Check for valid arguments being passed to a function with FNTYPE.
9385 There are NARGS arguments in the array ARGARRAY. */
9386 void
9387 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9389 /* Check for null being passed in a pointer argument that must be
9390 non-null. We also need to do this if format checking is enabled. */
9392 if (warn_nonnull)
9393 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9395 /* Check for errors in format strings. */
9397 if (warn_format || warn_suggest_attribute_format)
9398 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9400 if (warn_format)
9401 check_function_sentinel (fntype, nargs, argarray);
9404 /* Generic argument checking recursion routine. PARAM is the argument to
9405 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9406 once the argument is resolved. CTX is context for the callback. */
9407 void
9408 check_function_arguments_recurse (void (*callback)
9409 (void *, tree, unsigned HOST_WIDE_INT),
9410 void *ctx, tree param,
9411 unsigned HOST_WIDE_INT param_num)
9413 if (CONVERT_EXPR_P (param)
9414 && (TYPE_PRECISION (TREE_TYPE (param))
9415 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9417 /* Strip coercion. */
9418 check_function_arguments_recurse (callback, ctx,
9419 TREE_OPERAND (param, 0), param_num);
9420 return;
9423 if (TREE_CODE (param) == CALL_EXPR)
9425 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9426 tree attrs;
9427 bool found_format_arg = false;
9429 /* See if this is a call to a known internationalization function
9430 that modifies a format arg. Such a function may have multiple
9431 format_arg attributes (for example, ngettext). */
9433 for (attrs = TYPE_ATTRIBUTES (type);
9434 attrs;
9435 attrs = TREE_CHAIN (attrs))
9436 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9438 tree inner_arg;
9439 tree format_num_expr;
9440 int format_num;
9441 int i;
9442 call_expr_arg_iterator iter;
9444 /* Extract the argument number, which was previously checked
9445 to be valid. */
9446 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9448 format_num = tree_to_uhwi (format_num_expr);
9450 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9451 inner_arg != 0;
9452 inner_arg = next_call_expr_arg (&iter), i++)
9453 if (i == format_num)
9455 check_function_arguments_recurse (callback, ctx,
9456 inner_arg, param_num);
9457 found_format_arg = true;
9458 break;
9462 /* If we found a format_arg attribute and did a recursive check,
9463 we are done with checking this argument. Otherwise, we continue
9464 and this will be considered a non-literal. */
9465 if (found_format_arg)
9466 return;
9469 if (TREE_CODE (param) == COND_EXPR)
9471 /* Check both halves of the conditional expression. */
9472 check_function_arguments_recurse (callback, ctx,
9473 TREE_OPERAND (param, 1), param_num);
9474 check_function_arguments_recurse (callback, ctx,
9475 TREE_OPERAND (param, 2), param_num);
9476 return;
9479 (*callback) (ctx, param, param_num);
9482 /* Checks for a builtin function FNDECL that the number of arguments
9483 NARGS against the required number REQUIRED and issues an error if
9484 there is a mismatch. Returns true if the number of arguments is
9485 correct, otherwise false. */
9487 static bool
9488 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9490 if (nargs < required)
9492 error_at (input_location,
9493 "not enough arguments to function %qE", fndecl);
9494 return false;
9496 else if (nargs > required)
9498 error_at (input_location,
9499 "too many arguments to function %qE", fndecl);
9500 return false;
9502 return true;
9505 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9506 Returns false if there was an error, otherwise true. */
9508 bool
9509 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9511 if (!DECL_BUILT_IN (fndecl)
9512 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9513 return true;
9515 switch (DECL_FUNCTION_CODE (fndecl))
9517 case BUILT_IN_CONSTANT_P:
9518 return builtin_function_validate_nargs (fndecl, nargs, 1);
9520 case BUILT_IN_ISFINITE:
9521 case BUILT_IN_ISINF:
9522 case BUILT_IN_ISINF_SIGN:
9523 case BUILT_IN_ISNAN:
9524 case BUILT_IN_ISNORMAL:
9525 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9527 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9529 error ("non-floating-point argument in call to "
9530 "function %qE", fndecl);
9531 return false;
9533 return true;
9535 return false;
9537 case BUILT_IN_ISGREATER:
9538 case BUILT_IN_ISGREATEREQUAL:
9539 case BUILT_IN_ISLESS:
9540 case BUILT_IN_ISLESSEQUAL:
9541 case BUILT_IN_ISLESSGREATER:
9542 case BUILT_IN_ISUNORDERED:
9543 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9545 enum tree_code code0, code1;
9546 code0 = TREE_CODE (TREE_TYPE (args[0]));
9547 code1 = TREE_CODE (TREE_TYPE (args[1]));
9548 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9549 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9550 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9552 error ("non-floating-point arguments in call to "
9553 "function %qE", fndecl);
9554 return false;
9556 return true;
9558 return false;
9560 case BUILT_IN_FPCLASSIFY:
9561 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9563 unsigned i;
9565 for (i=0; i<5; i++)
9566 if (TREE_CODE (args[i]) != INTEGER_CST)
9568 error ("non-const integer argument %u in call to function %qE",
9569 i+1, fndecl);
9570 return false;
9573 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9575 error ("non-floating-point argument in call to function %qE",
9576 fndecl);
9577 return false;
9579 return true;
9581 return false;
9583 case BUILT_IN_ASSUME_ALIGNED:
9584 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9586 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9588 error ("non-integer argument 3 in call to function %qE", fndecl);
9589 return false;
9591 return true;
9593 return false;
9595 default:
9596 return true;
9600 /* Function to help qsort sort FIELD_DECLs by name order. */
9603 field_decl_cmp (const void *x_p, const void *y_p)
9605 const tree *const x = (const tree *const) x_p;
9606 const tree *const y = (const tree *const) y_p;
9608 if (DECL_NAME (*x) == DECL_NAME (*y))
9609 /* A nontype is "greater" than a type. */
9610 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9611 if (DECL_NAME (*x) == NULL_TREE)
9612 return -1;
9613 if (DECL_NAME (*y) == NULL_TREE)
9614 return 1;
9615 if (DECL_NAME (*x) < DECL_NAME (*y))
9616 return -1;
9617 return 1;
9620 static struct {
9621 gt_pointer_operator new_value;
9622 void *cookie;
9623 } resort_data;
9625 /* This routine compares two fields like field_decl_cmp but using the
9626 pointer operator in resort_data. */
9628 static int
9629 resort_field_decl_cmp (const void *x_p, const void *y_p)
9631 const tree *const x = (const tree *const) x_p;
9632 const tree *const y = (const tree *const) y_p;
9634 if (DECL_NAME (*x) == DECL_NAME (*y))
9635 /* A nontype is "greater" than a type. */
9636 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9637 if (DECL_NAME (*x) == NULL_TREE)
9638 return -1;
9639 if (DECL_NAME (*y) == NULL_TREE)
9640 return 1;
9642 tree d1 = DECL_NAME (*x);
9643 tree d2 = DECL_NAME (*y);
9644 resort_data.new_value (&d1, resort_data.cookie);
9645 resort_data.new_value (&d2, resort_data.cookie);
9646 if (d1 < d2)
9647 return -1;
9649 return 1;
9652 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9654 void
9655 resort_sorted_fields (void *obj,
9656 void * ARG_UNUSED (orig_obj),
9657 gt_pointer_operator new_value,
9658 void *cookie)
9660 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9661 resort_data.new_value = new_value;
9662 resort_data.cookie = cookie;
9663 qsort (&sf->elts[0], sf->len, sizeof (tree),
9664 resort_field_decl_cmp);
9667 /* Subroutine of c_parse_error.
9668 Return the result of concatenating LHS and RHS. RHS is really
9669 a string literal, its first character is indicated by RHS_START and
9670 RHS_SIZE is its length (including the terminating NUL character).
9672 The caller is responsible for deleting the returned pointer. */
9674 static char *
9675 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9677 const int lhs_size = strlen (lhs);
9678 char *result = XNEWVEC (char, lhs_size + rhs_size);
9679 strncpy (result, lhs, lhs_size);
9680 strncpy (result + lhs_size, rhs_start, rhs_size);
9681 return result;
9684 /* Issue the error given by GMSGID, indicating that it occurred before
9685 TOKEN, which had the associated VALUE. */
9687 void
9688 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9689 tree value, unsigned char token_flags)
9691 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9693 char *message = NULL;
9695 if (token_type == CPP_EOF)
9696 message = catenate_messages (gmsgid, " at end of input");
9697 else if (token_type == CPP_CHAR
9698 || token_type == CPP_WCHAR
9699 || token_type == CPP_CHAR16
9700 || token_type == CPP_CHAR32)
9702 unsigned int val = TREE_INT_CST_LOW (value);
9703 const char *prefix;
9705 switch (token_type)
9707 default:
9708 prefix = "";
9709 break;
9710 case CPP_WCHAR:
9711 prefix = "L";
9712 break;
9713 case CPP_CHAR16:
9714 prefix = "u";
9715 break;
9716 case CPP_CHAR32:
9717 prefix = "U";
9718 break;
9721 if (val <= UCHAR_MAX && ISGRAPH (val))
9722 message = catenate_messages (gmsgid, " before %s'%c'");
9723 else
9724 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9726 error (message, prefix, val);
9727 free (message);
9728 message = NULL;
9730 else if (token_type == CPP_CHAR_USERDEF
9731 || token_type == CPP_WCHAR_USERDEF
9732 || token_type == CPP_CHAR16_USERDEF
9733 || token_type == CPP_CHAR32_USERDEF)
9734 message = catenate_messages (gmsgid,
9735 " before user-defined character literal");
9736 else if (token_type == CPP_STRING_USERDEF
9737 || token_type == CPP_WSTRING_USERDEF
9738 || token_type == CPP_STRING16_USERDEF
9739 || token_type == CPP_STRING32_USERDEF
9740 || token_type == CPP_UTF8STRING_USERDEF)
9741 message = catenate_messages (gmsgid, " before user-defined string literal");
9742 else if (token_type == CPP_STRING
9743 || token_type == CPP_WSTRING
9744 || token_type == CPP_STRING16
9745 || token_type == CPP_STRING32
9746 || token_type == CPP_UTF8STRING)
9747 message = catenate_messages (gmsgid, " before string constant");
9748 else if (token_type == CPP_NUMBER)
9749 message = catenate_messages (gmsgid, " before numeric constant");
9750 else if (token_type == CPP_NAME)
9752 message = catenate_messages (gmsgid, " before %qE");
9753 error (message, value);
9754 free (message);
9755 message = NULL;
9757 else if (token_type == CPP_PRAGMA)
9758 message = catenate_messages (gmsgid, " before %<#pragma%>");
9759 else if (token_type == CPP_PRAGMA_EOL)
9760 message = catenate_messages (gmsgid, " before end of line");
9761 else if (token_type == CPP_DECLTYPE)
9762 message = catenate_messages (gmsgid, " before %<decltype%>");
9763 else if (token_type < N_TTYPES)
9765 message = catenate_messages (gmsgid, " before %qs token");
9766 error (message, cpp_type2name (token_type, token_flags));
9767 free (message);
9768 message = NULL;
9770 else
9771 error (gmsgid);
9773 if (message)
9775 error (message);
9776 free (message);
9778 #undef catenate_messages
9781 /* Mapping for cpp message reasons to the options that enable them. */
9783 struct reason_option_codes_t
9785 const int reason; /* cpplib message reason. */
9786 const int option_code; /* gcc option that controls this message. */
9789 static const struct reason_option_codes_t option_codes[] = {
9790 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9791 {CPP_W_C90_C99_COMPAT, OPT_Wc90_c99_compat},
9792 {CPP_W_COMMENTS, OPT_Wcomment},
9793 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9794 {CPP_W_DATE_TIME, OPT_Wdate_time},
9795 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9796 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9797 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9798 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9799 {CPP_W_LONG_LONG, OPT_Wlong_long},
9800 {CPP_W_MISSING_INCLUDE_DIRS, OPT_Wmissing_include_dirs},
9801 {CPP_W_MULTICHAR, OPT_Wmultichar},
9802 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9803 {CPP_W_PEDANTIC, OPT_Wpedantic},
9804 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9805 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9806 {CPP_W_UNDEF, OPT_Wundef},
9807 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9808 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9809 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9810 {CPP_W_NONE, 0}
9813 /* Return the gcc option code associated with the reason for a cpp
9814 message, or 0 if none. */
9816 static int
9817 c_option_controlling_cpp_error (int reason)
9819 const struct reason_option_codes_t *entry;
9821 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9823 if (entry->reason == reason)
9824 return entry->option_code;
9826 return 0;
9829 /* Callback from cpp_error for PFILE to print diagnostics from the
9830 preprocessor. The diagnostic is of type LEVEL, with REASON set
9831 to the reason code if LEVEL is represents a warning, at location
9832 LOCATION unless this is after lexing and the compiler's location
9833 should be used instead, with column number possibly overridden by
9834 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9835 the arguments. Returns true if a diagnostic was emitted, false
9836 otherwise. */
9838 bool
9839 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9840 location_t location, unsigned int column_override,
9841 const char *msg, va_list *ap)
9843 diagnostic_info diagnostic;
9844 diagnostic_t dlevel;
9845 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9846 bool ret;
9848 switch (level)
9850 case CPP_DL_WARNING_SYSHDR:
9851 if (flag_no_output)
9852 return false;
9853 global_dc->dc_warn_system_headers = 1;
9854 /* Fall through. */
9855 case CPP_DL_WARNING:
9856 if (flag_no_output)
9857 return false;
9858 dlevel = DK_WARNING;
9859 break;
9860 case CPP_DL_PEDWARN:
9861 if (flag_no_output && !flag_pedantic_errors)
9862 return false;
9863 dlevel = DK_PEDWARN;
9864 break;
9865 case CPP_DL_ERROR:
9866 dlevel = DK_ERROR;
9867 break;
9868 case CPP_DL_ICE:
9869 dlevel = DK_ICE;
9870 break;
9871 case CPP_DL_NOTE:
9872 dlevel = DK_NOTE;
9873 break;
9874 case CPP_DL_FATAL:
9875 dlevel = DK_FATAL;
9876 break;
9877 default:
9878 gcc_unreachable ();
9880 if (done_lexing)
9881 location = input_location;
9882 diagnostic_set_info_translated (&diagnostic, msg, ap,
9883 location, dlevel);
9884 if (column_override)
9885 diagnostic_override_column (&diagnostic, column_override);
9886 diagnostic_override_option_index (&diagnostic,
9887 c_option_controlling_cpp_error (reason));
9888 ret = report_diagnostic (&diagnostic);
9889 if (level == CPP_DL_WARNING_SYSHDR)
9890 global_dc->dc_warn_system_headers = save_warn_system_headers;
9891 return ret;
9894 /* Convert a character from the host to the target execution character
9895 set. cpplib handles this, mostly. */
9897 HOST_WIDE_INT
9898 c_common_to_target_charset (HOST_WIDE_INT c)
9900 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9901 zero-extended under -fno-signed-char. cpplib insists that characters
9902 and character constants are always unsigned. Hence we must convert
9903 back and forth. */
9904 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9906 uc = cpp_host_to_exec_charset (parse_in, uc);
9908 if (flag_signed_char)
9909 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9910 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9911 else
9912 return uc;
9915 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9916 references with an INDIRECT_REF of a constant at the bottom; much like the
9917 traditional rendering of offsetof as a macro. Return the folded result. */
9919 tree
9920 fold_offsetof_1 (tree expr)
9922 tree base, off, t;
9924 switch (TREE_CODE (expr))
9926 case ERROR_MARK:
9927 return expr;
9929 case VAR_DECL:
9930 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9931 return error_mark_node;
9933 case CALL_EXPR:
9934 case TARGET_EXPR:
9935 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9936 return error_mark_node;
9938 case NOP_EXPR:
9939 case INDIRECT_REF:
9940 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9942 error ("cannot apply %<offsetof%> to a non constant address");
9943 return error_mark_node;
9945 return TREE_OPERAND (expr, 0);
9947 case COMPONENT_REF:
9948 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9949 if (base == error_mark_node)
9950 return base;
9952 t = TREE_OPERAND (expr, 1);
9953 if (DECL_C_BIT_FIELD (t))
9955 error ("attempt to take address of bit-field structure "
9956 "member %qD", t);
9957 return error_mark_node;
9959 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9960 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
9961 / BITS_PER_UNIT));
9962 break;
9964 case ARRAY_REF:
9965 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9966 if (base == error_mark_node)
9967 return base;
9969 t = TREE_OPERAND (expr, 1);
9971 /* Check if the offset goes beyond the upper bound of the array. */
9972 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9974 tree upbound = array_ref_up_bound (expr);
9975 if (upbound != NULL_TREE
9976 && TREE_CODE (upbound) == INTEGER_CST
9977 && !tree_int_cst_equal (upbound,
9978 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9980 upbound = size_binop (PLUS_EXPR, upbound,
9981 build_int_cst (TREE_TYPE (upbound), 1));
9982 if (tree_int_cst_lt (upbound, t))
9984 tree v;
9986 for (v = TREE_OPERAND (expr, 0);
9987 TREE_CODE (v) == COMPONENT_REF;
9988 v = TREE_OPERAND (v, 0))
9989 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9990 == RECORD_TYPE)
9992 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9993 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9994 if (TREE_CODE (fld_chain) == FIELD_DECL)
9995 break;
9997 if (fld_chain)
9998 break;
10000 /* Don't warn if the array might be considered a poor
10001 man's flexible array member with a very permissive
10002 definition thereof. */
10003 if (TREE_CODE (v) == ARRAY_REF
10004 || TREE_CODE (v) == COMPONENT_REF)
10005 warning (OPT_Warray_bounds,
10006 "index %E denotes an offset "
10007 "greater than size of %qT",
10008 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10013 t = convert (sizetype, t);
10014 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10015 break;
10017 case COMPOUND_EXPR:
10018 /* Handle static members of volatile structs. */
10019 t = TREE_OPERAND (expr, 1);
10020 gcc_assert (TREE_CODE (t) == VAR_DECL);
10021 return fold_offsetof_1 (t);
10023 default:
10024 gcc_unreachable ();
10027 return fold_build_pointer_plus (base, off);
10030 /* Likewise, but convert it to the return type of offsetof. */
10032 tree
10033 fold_offsetof (tree expr)
10035 return convert (size_type_node, fold_offsetof_1 (expr));
10038 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10039 expression, because B will always be true. */
10041 void
10042 warn_for_omitted_condop (location_t location, tree cond)
10044 if (truth_value_p (TREE_CODE (cond)))
10045 warning_at (location, OPT_Wparentheses,
10046 "the omitted middle operand in ?: will always be %<true%>, "
10047 "suggest explicit middle operand");
10050 /* Give an error for storing into ARG, which is 'const'. USE indicates
10051 how ARG was being used. */
10053 void
10054 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10056 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10057 || use == lv_asm);
10058 /* Using this macro rather than (for example) arrays of messages
10059 ensures that all the format strings are checked at compile
10060 time. */
10061 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10062 : (use == lv_increment ? (I) \
10063 : (use == lv_decrement ? (D) : (AS))))
10064 if (TREE_CODE (arg) == COMPONENT_REF)
10066 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10067 error_at (loc, READONLY_MSG (G_("assignment of member "
10068 "%qD in read-only object"),
10069 G_("increment of member "
10070 "%qD in read-only object"),
10071 G_("decrement of member "
10072 "%qD in read-only object"),
10073 G_("member %qD in read-only object "
10074 "used as %<asm%> output")),
10075 TREE_OPERAND (arg, 1));
10076 else
10077 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10078 G_("increment of read-only member %qD"),
10079 G_("decrement of read-only member %qD"),
10080 G_("read-only member %qD used as %<asm%> output")),
10081 TREE_OPERAND (arg, 1));
10083 else if (TREE_CODE (arg) == VAR_DECL)
10084 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10085 G_("increment of read-only variable %qD"),
10086 G_("decrement of read-only variable %qD"),
10087 G_("read-only variable %qD used as %<asm%> output")),
10088 arg);
10089 else if (TREE_CODE (arg) == PARM_DECL)
10090 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10091 G_("increment of read-only parameter %qD"),
10092 G_("decrement of read-only parameter %qD"),
10093 G_("read-only parameter %qD use as %<asm%> output")),
10094 arg);
10095 else if (TREE_CODE (arg) == RESULT_DECL)
10097 gcc_assert (c_dialect_cxx ());
10098 error_at (loc, READONLY_MSG (G_("assignment of "
10099 "read-only named return value %qD"),
10100 G_("increment of "
10101 "read-only named return value %qD"),
10102 G_("decrement of "
10103 "read-only named return value %qD"),
10104 G_("read-only named return value %qD "
10105 "used as %<asm%>output")),
10106 arg);
10108 else if (TREE_CODE (arg) == FUNCTION_DECL)
10109 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10110 G_("increment of function %qD"),
10111 G_("decrement of function %qD"),
10112 G_("function %qD used as %<asm%> output")),
10113 arg);
10114 else
10115 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10116 G_("increment of read-only location %qE"),
10117 G_("decrement of read-only location %qE"),
10118 G_("read-only location %qE used as %<asm%> output")),
10119 arg);
10122 /* Print an error message for an invalid lvalue. USE says
10123 how the lvalue is being used and so selects the error message. LOC
10124 is the location for the error. */
10126 void
10127 lvalue_error (location_t loc, enum lvalue_use use)
10129 switch (use)
10131 case lv_assign:
10132 error_at (loc, "lvalue required as left operand of assignment");
10133 break;
10134 case lv_increment:
10135 error_at (loc, "lvalue required as increment operand");
10136 break;
10137 case lv_decrement:
10138 error_at (loc, "lvalue required as decrement operand");
10139 break;
10140 case lv_addressof:
10141 error_at (loc, "lvalue required as unary %<&%> operand");
10142 break;
10143 case lv_asm:
10144 error_at (loc, "lvalue required in asm statement");
10145 break;
10146 default:
10147 gcc_unreachable ();
10151 /* Print an error message for an invalid indirection of type TYPE.
10152 ERRSTRING is the name of the operator for the indirection. */
10154 void
10155 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10157 switch (errstring)
10159 case RO_NULL:
10160 gcc_assert (c_dialect_cxx ());
10161 error_at (loc, "invalid type argument (have %qT)", type);
10162 break;
10163 case RO_ARRAY_INDEXING:
10164 error_at (loc,
10165 "invalid type argument of array indexing (have %qT)",
10166 type);
10167 break;
10168 case RO_UNARY_STAR:
10169 error_at (loc,
10170 "invalid type argument of unary %<*%> (have %qT)",
10171 type);
10172 break;
10173 case RO_ARROW:
10174 error_at (loc,
10175 "invalid type argument of %<->%> (have %qT)",
10176 type);
10177 break;
10178 case RO_ARROW_STAR:
10179 error_at (loc,
10180 "invalid type argument of %<->*%> (have %qT)",
10181 type);
10182 break;
10183 case RO_IMPLICIT_CONVERSION:
10184 error_at (loc,
10185 "invalid type argument of implicit conversion (have %qT)",
10186 type);
10187 break;
10188 default:
10189 gcc_unreachable ();
10193 /* *PTYPE is an incomplete array. Complete it with a domain based on
10194 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10195 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10196 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10199 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10201 tree maxindex, type, main_type, elt, unqual_elt;
10202 int failure = 0, quals;
10203 hashval_t hashcode = 0;
10204 bool overflow_p = false;
10206 maxindex = size_zero_node;
10207 if (initial_value)
10209 if (TREE_CODE (initial_value) == STRING_CST)
10211 int eltsize
10212 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10213 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10215 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10217 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10219 if (vec_safe_is_empty (v))
10221 if (pedantic)
10222 failure = 3;
10223 maxindex = ssize_int (-1);
10225 else
10227 tree curindex;
10228 unsigned HOST_WIDE_INT cnt;
10229 constructor_elt *ce;
10230 bool fold_p = false;
10232 if ((*v)[0].index)
10233 maxindex = (*v)[0].index, fold_p = true;
10235 curindex = maxindex;
10237 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10239 bool curfold_p = false;
10240 if (ce->index)
10241 curindex = ce->index, curfold_p = true;
10242 else
10244 if (fold_p)
10246 /* Since we treat size types now as ordinary
10247 unsigned types, we need an explicit overflow
10248 check. */
10249 tree orig = curindex;
10250 curindex = fold_convert (sizetype, curindex);
10251 overflow_p |= tree_int_cst_lt (curindex, orig);
10253 curindex = size_binop (PLUS_EXPR, curindex,
10254 size_one_node);
10256 if (tree_int_cst_lt (maxindex, curindex))
10257 maxindex = curindex, fold_p = curfold_p;
10259 if (fold_p)
10261 tree orig = maxindex;
10262 maxindex = fold_convert (sizetype, maxindex);
10263 overflow_p |= tree_int_cst_lt (maxindex, orig);
10267 else
10269 /* Make an error message unless that happened already. */
10270 if (initial_value != error_mark_node)
10271 failure = 1;
10274 else
10276 failure = 2;
10277 if (!do_default)
10278 return failure;
10281 type = *ptype;
10282 /* Force an indefinite layout factor. */
10283 if (upc_shared_type_p (type))
10284 type = c_build_qualified_type_1 (type, TYPE_QUAL_UPC_SHARED,
10285 size_zero_node);
10286 elt = TREE_TYPE (type);
10287 quals = TYPE_QUALS (strip_array_types (elt));
10288 if (quals == 0)
10289 unqual_elt = elt;
10290 else
10291 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10293 /* Using build_distinct_type_copy and modifying things afterward instead
10294 of using build_array_type to create a new type preserves all of the
10295 TYPE_LANG_FLAG_? bits that the front end may have set. */
10296 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10297 TREE_TYPE (main_type) = unqual_elt;
10298 TYPE_DOMAIN (main_type)
10299 = build_range_type (TREE_TYPE (maxindex),
10300 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10301 layout_type (main_type);
10303 /* Make sure we have the canonical MAIN_TYPE. */
10304 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10305 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10306 hashcode);
10307 main_type = type_hash_canon (hashcode, main_type);
10309 /* Fix the canonical type. */
10310 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10311 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10312 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10313 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10314 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10315 != TYPE_DOMAIN (main_type)))
10316 TYPE_CANONICAL (main_type)
10317 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10318 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10319 else
10320 TYPE_CANONICAL (main_type) = main_type;
10322 if (quals == 0)
10323 type = main_type;
10324 else
10325 type = c_build_qualified_type (main_type, quals);
10327 if (COMPLETE_TYPE_P (type)
10328 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10329 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10331 error ("size of array is too large");
10332 /* If we proceed with the array type as it is, we'll eventually
10333 crash in tree_to_[su]hwi(). */
10334 type = error_mark_node;
10337 *ptype = type;
10338 return failure;
10341 /* Like c_mark_addressable but don't check register qualifier. */
10342 void
10343 c_common_mark_addressable_vec (tree t)
10345 while (handled_component_p (t))
10346 t = TREE_OPERAND (t, 0);
10347 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10348 return;
10349 TREE_ADDRESSABLE (t) = 1;
10354 /* Used to help initialize the builtin-types.def table. When a type of
10355 the correct size doesn't exist, use error_mark_node instead of NULL.
10356 The later results in segfaults even when a decl using the type doesn't
10357 get invoked. */
10359 tree
10360 builtin_type_for_size (int size, bool unsignedp)
10362 tree type = c_common_type_for_size (size, unsignedp);
10363 return type ? type : error_mark_node;
10366 /* A helper function for resolve_overloaded_builtin in resolving the
10367 overloaded __sync_ builtins. Returns a positive power of 2 if the
10368 first operand of PARAMS is a pointer to a supported data type.
10369 Returns 0 if an error is encountered. */
10371 static int
10372 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10374 tree type;
10375 int size;
10377 if (!params)
10379 error ("too few arguments to function %qE", function);
10380 return 0;
10383 type = TREE_TYPE ((*params)[0]);
10384 if (TREE_CODE (type) == ARRAY_TYPE)
10386 /* Force array-to-pointer decay for C++. */
10387 gcc_assert (c_dialect_cxx());
10388 (*params)[0] = default_conversion ((*params)[0]);
10389 type = TREE_TYPE ((*params)[0]);
10391 if (TREE_CODE (type) != POINTER_TYPE)
10392 goto incompatible;
10394 type = TREE_TYPE (type);
10395 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10396 goto incompatible;
10398 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10399 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10400 return size;
10402 incompatible:
10403 error ("incompatible type for argument %d of %qE", 1, function);
10404 return 0;
10407 /* A helper function for resolve_overloaded_builtin. Adds casts to
10408 PARAMS to make arguments match up with those of FUNCTION. Drops
10409 the variadic arguments at the end. Returns false if some error
10410 was encountered; true on success. */
10412 static bool
10413 sync_resolve_params (location_t loc, tree orig_function, tree function,
10414 vec<tree, va_gc> *params, bool orig_format)
10416 function_args_iterator iter;
10417 tree ptype;
10418 unsigned int parmnum;
10420 function_args_iter_init (&iter, TREE_TYPE (function));
10421 /* We've declared the implementation functions to use "volatile void *"
10422 as the pointer parameter, so we shouldn't get any complaints from the
10423 call to check_function_arguments what ever type the user used. */
10424 function_args_iter_next (&iter);
10425 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10426 ptype = TYPE_MAIN_VARIANT (ptype);
10428 /* For the rest of the values, we need to cast these to FTYPE, so that we
10429 don't get warnings for passing pointer types, etc. */
10430 parmnum = 0;
10431 while (1)
10433 tree val, arg_type;
10435 arg_type = function_args_iter_cond (&iter);
10436 /* XXX void_type_node belies the abstraction. */
10437 if (arg_type == void_type_node)
10438 break;
10440 ++parmnum;
10441 if (params->length () <= parmnum)
10443 error_at (loc, "too few arguments to function %qE", orig_function);
10444 return false;
10447 /* Only convert parameters if arg_type is unsigned integer type with
10448 new format sync routines, i.e. don't attempt to convert pointer
10449 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10450 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10451 kinds). */
10452 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10454 /* Ideally for the first conversion we'd use convert_for_assignment
10455 so that we get warnings for anything that doesn't match the pointer
10456 type. This isn't portable across the C and C++ front ends atm. */
10457 val = (*params)[parmnum];
10458 val = convert (ptype, val);
10459 val = convert (arg_type, val);
10460 (*params)[parmnum] = val;
10463 function_args_iter_next (&iter);
10466 /* __atomic routines are not variadic. */
10467 if (!orig_format && params->length () != parmnum + 1)
10469 error_at (loc, "too many arguments to function %qE", orig_function);
10470 return false;
10473 /* The definition of these primitives is variadic, with the remaining
10474 being "an optional list of variables protected by the memory barrier".
10475 No clue what that's supposed to mean, precisely, but we consider all
10476 call-clobbered variables to be protected so we're safe. */
10477 params->truncate (parmnum + 1);
10479 return true;
10482 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10483 RESULT to make it match the type of the first pointer argument in
10484 PARAMS. */
10486 static tree
10487 sync_resolve_return (tree first_param, tree result, bool orig_format)
10489 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10490 tree rtype = TREE_TYPE (result);
10491 ptype = TYPE_MAIN_VARIANT (ptype);
10493 /* New format doesn't require casting unless the types are the same size. */
10494 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10495 return convert (ptype, result);
10496 else
10497 return result;
10500 /* This function verifies the PARAMS to generic atomic FUNCTION.
10501 It returns the size if all the parameters are the same size, otherwise
10502 0 is returned if the parameters are invalid. */
10504 static int
10505 get_atomic_generic_size (location_t loc, tree function,
10506 vec<tree, va_gc> *params)
10508 unsigned int n_param;
10509 unsigned int n_model;
10510 unsigned int x;
10511 int size_0;
10512 tree type_0;
10514 /* Determine the parameter makeup. */
10515 switch (DECL_FUNCTION_CODE (function))
10517 case BUILT_IN_ATOMIC_EXCHANGE:
10518 n_param = 4;
10519 n_model = 1;
10520 break;
10521 case BUILT_IN_ATOMIC_LOAD:
10522 case BUILT_IN_ATOMIC_STORE:
10523 n_param = 3;
10524 n_model = 1;
10525 break;
10526 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10527 n_param = 6;
10528 n_model = 2;
10529 break;
10530 default:
10531 gcc_unreachable ();
10534 if (vec_safe_length (params) != n_param)
10536 error_at (loc, "incorrect number of arguments to function %qE", function);
10537 return 0;
10540 /* Get type of first parameter, and determine its size. */
10541 type_0 = TREE_TYPE ((*params)[0]);
10542 if (TREE_CODE (type_0) == ARRAY_TYPE)
10544 /* Force array-to-pointer decay for C++. */
10545 gcc_assert (c_dialect_cxx());
10546 (*params)[0] = default_conversion ((*params)[0]);
10547 type_0 = TREE_TYPE ((*params)[0]);
10549 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10551 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10552 function);
10553 return 0;
10556 /* Types must be compile time constant sizes. */
10557 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10559 error_at (loc,
10560 "argument 1 of %qE must be a pointer to a constant size type",
10561 function);
10562 return 0;
10565 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10567 /* Zero size objects are not allowed. */
10568 if (size_0 == 0)
10570 error_at (loc,
10571 "argument 1 of %qE must be a pointer to a nonzero size object",
10572 function);
10573 return 0;
10576 /* Check each other parameter is a pointer and the same size. */
10577 for (x = 0; x < n_param - n_model; x++)
10579 int size;
10580 tree type = TREE_TYPE ((*params)[x]);
10581 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10582 if (n_param == 6 && x == 3)
10583 continue;
10584 if (!POINTER_TYPE_P (type))
10586 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10587 function);
10588 return 0;
10590 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10591 size = type_size ? tree_to_uhwi (type_size) : 0;
10592 if (size != size_0)
10594 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10595 function);
10596 return 0;
10600 /* Check memory model parameters for validity. */
10601 for (x = n_param - n_model ; x < n_param; x++)
10603 tree p = (*params)[x];
10604 if (TREE_CODE (p) == INTEGER_CST)
10606 int i = tree_to_uhwi (p);
10607 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10609 warning_at (loc, OPT_Winvalid_memory_model,
10610 "invalid memory model argument %d of %qE", x + 1,
10611 function);
10614 else
10615 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10617 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10618 function);
10619 return 0;
10623 return size_0;
10627 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10628 at the beginning of the parameter list PARAMS representing the size of the
10629 objects. This is to match the library ABI requirement. LOC is the location
10630 of the function call.
10631 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10632 returned to allow the external call to be constructed. */
10634 static tree
10635 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10636 vec<tree, va_gc> *params)
10638 tree size_node;
10640 /* Insert a SIZE_T parameter as the first param. If there isn't
10641 enough space, allocate a new vector and recursively re-build with that. */
10642 if (!params->space (1))
10644 unsigned int z, len;
10645 vec<tree, va_gc> *v;
10646 tree f;
10648 len = params->length ();
10649 vec_alloc (v, len + 1);
10650 v->quick_push (build_int_cst (size_type_node, n));
10651 for (z = 0; z < len; z++)
10652 v->quick_push ((*params)[z]);
10653 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10654 vec_free (v);
10655 return f;
10658 /* Add the size parameter and leave as a function call for processing. */
10659 size_node = build_int_cst (size_type_node, n);
10660 params->quick_insert (0, size_node);
10661 return NULL_TREE;
10665 /* Return whether atomic operations for naturally aligned N-byte
10666 arguments are supported, whether inline or through libatomic. */
10667 static bool
10668 atomic_size_supported_p (int n)
10670 switch (n)
10672 case 1:
10673 case 2:
10674 case 4:
10675 case 8:
10676 return true;
10678 case 16:
10679 return targetm.scalar_mode_supported_p (TImode);
10681 default:
10682 return false;
10686 /* This will process an __atomic_exchange function call, determine whether it
10687 needs to be mapped to the _N variation, or turned into a library call.
10688 LOC is the location of the builtin call.
10689 FUNCTION is the DECL that has been invoked;
10690 PARAMS is the argument list for the call. The return value is non-null
10691 TRUE is returned if it is translated into the proper format for a call to the
10692 external library, and NEW_RETURN is set the tree for that function.
10693 FALSE is returned if processing for the _N variation is required, and
10694 NEW_RETURN is set to the the return value the result is copied into. */
10695 static bool
10696 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10697 vec<tree, va_gc> *params, tree *new_return)
10699 tree p0, p1, p2, p3;
10700 tree I_type, I_type_ptr;
10701 int n = get_atomic_generic_size (loc, function, params);
10703 /* Size of 0 is an error condition. */
10704 if (n == 0)
10706 *new_return = error_mark_node;
10707 return true;
10710 /* If not a lock-free size, change to the library generic format. */
10711 if (!atomic_size_supported_p (n))
10713 *new_return = add_atomic_size_parameter (n, loc, function, params);
10714 return true;
10717 /* Otherwise there is a lockfree match, transform the call from:
10718 void fn(T* mem, T* desired, T* return, model)
10719 into
10720 *return = (T) (fn (In* mem, (In) *desired, model)) */
10722 p0 = (*params)[0];
10723 p1 = (*params)[1];
10724 p2 = (*params)[2];
10725 p3 = (*params)[3];
10727 /* Create pointer to appropriate size. */
10728 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10729 I_type_ptr = build_pointer_type (I_type);
10731 /* Convert object pointer to required type. */
10732 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10733 (*params)[0] = p0;
10734 /* Convert new value to required type, and dereference it. */
10735 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10736 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10737 (*params)[1] = p1;
10739 /* Move memory model to the 3rd position, and end param list. */
10740 (*params)[2] = p3;
10741 params->truncate (3);
10743 /* Convert return pointer and dereference it for later assignment. */
10744 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10746 return false;
10750 /* This will process an __atomic_compare_exchange function call, determine
10751 whether it needs to be mapped to the _N variation, or turned into a lib call.
10752 LOC is the location of the builtin call.
10753 FUNCTION is the DECL that has been invoked;
10754 PARAMS is the argument list for the call. The return value is non-null
10755 TRUE is returned if it is translated into the proper format for a call to the
10756 external library, and NEW_RETURN is set the tree for that function.
10757 FALSE is returned if processing for the _N variation is required. */
10759 static bool
10760 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10761 vec<tree, va_gc> *params,
10762 tree *new_return)
10764 tree p0, p1, p2;
10765 tree I_type, I_type_ptr;
10766 int n = get_atomic_generic_size (loc, function, params);
10768 /* Size of 0 is an error condition. */
10769 if (n == 0)
10771 *new_return = error_mark_node;
10772 return true;
10775 /* If not a lock-free size, change to the library generic format. */
10776 if (!atomic_size_supported_p (n))
10778 /* The library generic format does not have the weak parameter, so
10779 remove it from the param list. Since a parameter has been removed,
10780 we can be sure that there is room for the SIZE_T parameter, meaning
10781 there will not be a recursive rebuilding of the parameter list, so
10782 there is no danger this will be done twice. */
10783 if (n > 0)
10785 (*params)[3] = (*params)[4];
10786 (*params)[4] = (*params)[5];
10787 params->truncate (5);
10789 *new_return = add_atomic_size_parameter (n, loc, function, params);
10790 return true;
10793 /* Otherwise, there is a match, so the call needs to be transformed from:
10794 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10795 into
10796 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10798 p0 = (*params)[0];
10799 p1 = (*params)[1];
10800 p2 = (*params)[2];
10802 /* Create pointer to appropriate size. */
10803 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10804 I_type_ptr = build_pointer_type (I_type);
10806 /* Convert object pointer to required type. */
10807 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10808 (*params)[0] = p0;
10810 /* Convert expected pointer to required type. */
10811 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10812 (*params)[1] = p1;
10814 /* Convert desired value to required type, and dereference it. */
10815 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10816 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10817 (*params)[2] = p2;
10819 /* The rest of the parameters are fine. NULL means no special return value
10820 processing.*/
10821 *new_return = NULL;
10822 return false;
10826 /* This will process an __atomic_load function call, determine whether it
10827 needs to be mapped to the _N variation, or turned into a library call.
10828 LOC is the location of the builtin call.
10829 FUNCTION is the DECL that has been invoked;
10830 PARAMS is the argument list for the call. The return value is non-null
10831 TRUE is returned if it is translated into the proper format for a call to the
10832 external library, and NEW_RETURN is set the tree for that function.
10833 FALSE is returned if processing for the _N variation is required, and
10834 NEW_RETURN is set to the the return value the result is copied into. */
10836 static bool
10837 resolve_overloaded_atomic_load (location_t loc, tree function,
10838 vec<tree, va_gc> *params, tree *new_return)
10840 tree p0, p1, p2;
10841 tree I_type, I_type_ptr;
10842 int n = get_atomic_generic_size (loc, function, params);
10844 /* Size of 0 is an error condition. */
10845 if (n == 0)
10847 *new_return = error_mark_node;
10848 return true;
10851 /* If not a lock-free size, change to the library generic format. */
10852 if (!atomic_size_supported_p (n))
10854 *new_return = add_atomic_size_parameter (n, loc, function, params);
10855 return true;
10858 /* Otherwise, there is a match, so the call needs to be transformed from:
10859 void fn(T* mem, T* return, model)
10860 into
10861 *return = (T) (fn ((In *) mem, model)) */
10863 p0 = (*params)[0];
10864 p1 = (*params)[1];
10865 p2 = (*params)[2];
10867 /* Create pointer to appropriate size. */
10868 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10869 I_type_ptr = build_pointer_type (I_type);
10871 /* Convert object pointer to required type. */
10872 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10873 (*params)[0] = p0;
10875 /* Move memory model to the 2nd position, and end param list. */
10876 (*params)[1] = p2;
10877 params->truncate (2);
10879 /* Convert return pointer and dereference it for later assignment. */
10880 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10882 return false;
10886 /* This will process an __atomic_store function call, determine whether it
10887 needs to be mapped to the _N variation, or turned into a library call.
10888 LOC is the location of the builtin call.
10889 FUNCTION is the DECL that has been invoked;
10890 PARAMS is the argument list for the call. The return value is non-null
10891 TRUE is returned if it is translated into the proper format for a call to the
10892 external library, and NEW_RETURN is set the tree for that function.
10893 FALSE is returned if processing for the _N variation is required, and
10894 NEW_RETURN is set to the the return value the result is copied into. */
10896 static bool
10897 resolve_overloaded_atomic_store (location_t loc, tree function,
10898 vec<tree, va_gc> *params, tree *new_return)
10900 tree p0, p1;
10901 tree I_type, I_type_ptr;
10902 int n = get_atomic_generic_size (loc, function, params);
10904 /* Size of 0 is an error condition. */
10905 if (n == 0)
10907 *new_return = error_mark_node;
10908 return true;
10911 /* If not a lock-free size, change to the library generic format. */
10912 if (!atomic_size_supported_p (n))
10914 *new_return = add_atomic_size_parameter (n, loc, function, params);
10915 return true;
10918 /* Otherwise, there is a match, so the call needs to be transformed from:
10919 void fn(T* mem, T* value, model)
10920 into
10921 fn ((In *) mem, (In) *value, model) */
10923 p0 = (*params)[0];
10924 p1 = (*params)[1];
10926 /* Create pointer to appropriate size. */
10927 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10928 I_type_ptr = build_pointer_type (I_type);
10930 /* Convert object pointer to required type. */
10931 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10932 (*params)[0] = p0;
10934 /* Convert new value to required type, and dereference it. */
10935 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10936 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10937 (*params)[1] = p1;
10939 /* The memory model is in the right spot already. Return is void. */
10940 *new_return = NULL_TREE;
10942 return false;
10946 /* Some builtin functions are placeholders for other expressions. This
10947 function should be called immediately after parsing the call expression
10948 before surrounding code has committed to the type of the expression.
10950 LOC is the location of the builtin call.
10952 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10953 PARAMS is the argument list for the call. The return value is non-null
10954 when expansion is complete, and null if normal processing should
10955 continue. */
10957 tree
10958 resolve_overloaded_builtin (location_t loc, tree function,
10959 vec<tree, va_gc> *params)
10961 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10962 bool orig_format = true;
10963 tree new_return = NULL_TREE;
10965 switch (DECL_BUILT_IN_CLASS (function))
10967 case BUILT_IN_NORMAL:
10968 break;
10969 case BUILT_IN_MD:
10970 if (targetm.resolve_overloaded_builtin)
10971 return targetm.resolve_overloaded_builtin (loc, function, params);
10972 else
10973 return NULL_TREE;
10974 default:
10975 return NULL_TREE;
10978 /* Handle BUILT_IN_NORMAL here. */
10979 switch (orig_code)
10981 case BUILT_IN_ATOMIC_EXCHANGE:
10982 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10983 case BUILT_IN_ATOMIC_LOAD:
10984 case BUILT_IN_ATOMIC_STORE:
10986 /* Handle these 4 together so that they can fall through to the next
10987 case if the call is transformed to an _N variant. */
10988 switch (orig_code)
10990 case BUILT_IN_ATOMIC_EXCHANGE:
10992 if (resolve_overloaded_atomic_exchange (loc, function, params,
10993 &new_return))
10994 return new_return;
10995 /* Change to the _N variant. */
10996 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10997 break;
11000 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11002 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11003 params,
11004 &new_return))
11005 return new_return;
11006 /* Change to the _N variant. */
11007 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11008 break;
11010 case BUILT_IN_ATOMIC_LOAD:
11012 if (resolve_overloaded_atomic_load (loc, function, params,
11013 &new_return))
11014 return new_return;
11015 /* Change to the _N variant. */
11016 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11017 break;
11019 case BUILT_IN_ATOMIC_STORE:
11021 if (resolve_overloaded_atomic_store (loc, function, params,
11022 &new_return))
11023 return new_return;
11024 /* Change to the _N variant. */
11025 orig_code = BUILT_IN_ATOMIC_STORE_N;
11026 break;
11028 default:
11029 gcc_unreachable ();
11031 /* Fallthrough to the normal processing. */
11033 case BUILT_IN_ATOMIC_EXCHANGE_N:
11034 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11035 case BUILT_IN_ATOMIC_LOAD_N:
11036 case BUILT_IN_ATOMIC_STORE_N:
11037 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11038 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11039 case BUILT_IN_ATOMIC_AND_FETCH_N:
11040 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11041 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11042 case BUILT_IN_ATOMIC_OR_FETCH_N:
11043 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11044 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11045 case BUILT_IN_ATOMIC_FETCH_AND_N:
11046 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11047 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11048 case BUILT_IN_ATOMIC_FETCH_OR_N:
11050 orig_format = false;
11051 /* Fallthru for parameter processing. */
11053 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11054 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11055 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11056 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11057 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11058 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11059 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11060 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11061 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11062 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11063 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11064 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11065 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11066 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11067 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11068 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11070 int n = sync_resolve_size (function, params);
11071 tree new_function, first_param, result;
11072 enum built_in_function fncode;
11074 if (n == 0)
11075 return error_mark_node;
11077 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11078 new_function = builtin_decl_explicit (fncode);
11079 if (!sync_resolve_params (loc, function, new_function, params,
11080 orig_format))
11081 return error_mark_node;
11083 first_param = (*params)[0];
11084 result = build_function_call_vec (loc, vNULL, new_function, params,
11085 NULL);
11086 if (result == error_mark_node)
11087 return result;
11088 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11089 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11090 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11091 result = sync_resolve_return (first_param, result, orig_format);
11093 /* If new_return is set, assign function to that expr and cast the
11094 result to void since the generic interface returned void. */
11095 if (new_return)
11097 /* Cast function result from I{1,2,4,8,16} to the required type. */
11098 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11099 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11100 result);
11101 TREE_SIDE_EFFECTS (result) = 1;
11102 protected_set_expr_location (result, loc);
11103 result = convert (void_type_node, result);
11105 return result;
11108 default:
11109 return NULL_TREE;
11113 /* vector_types_compatible_elements_p is used in type checks of vectors
11114 values used as operands of binary operators. Where it returns true, and
11115 the other checks of the caller succeed (being vector types in he first
11116 place, and matching number of elements), we can just treat the types
11117 as essentially the same.
11118 Contrast with vector_targets_convertible_p, which is used for vector
11119 pointer types, and vector_types_convertible_p, which will allow
11120 language-specific matches under the control of flag_lax_vector_conversions,
11121 and might still require a conversion. */
11122 /* True if vector types T1 and T2 can be inputs to the same binary
11123 operator without conversion.
11124 We don't check the overall vector size here because some of our callers
11125 want to give different error messages when the vectors are compatible
11126 except for the element count. */
11128 bool
11129 vector_types_compatible_elements_p (tree t1, tree t2)
11131 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11132 t1 = TREE_TYPE (t1);
11133 t2 = TREE_TYPE (t2);
11135 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11137 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11138 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11139 || c2 == FIXED_POINT_TYPE));
11141 t1 = c_common_signed_type (t1);
11142 t2 = c_common_signed_type (t2);
11143 /* Equality works here because c_common_signed_type uses
11144 TYPE_MAIN_VARIANT. */
11145 if (t1 == t2)
11146 return true;
11147 if (opaque && c1 == c2
11148 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11149 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11150 return true;
11151 return false;
11154 /* Check for missing format attributes on function pointers. LTYPE is
11155 the new type or left-hand side type. RTYPE is the old type or
11156 right-hand side type. Returns TRUE if LTYPE is missing the desired
11157 attribute. */
11159 bool
11160 check_missing_format_attribute (tree ltype, tree rtype)
11162 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11163 tree ra;
11165 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11166 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11167 break;
11168 if (ra)
11170 tree la;
11171 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11172 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11173 break;
11174 return !la;
11176 else
11177 return false;
11180 /* Subscripting with type char is likely to lose on a machine where
11181 chars are signed. So warn on any machine, but optionally. Don't
11182 warn for unsigned char since that type is safe. Don't warn for
11183 signed char because anyone who uses that must have done so
11184 deliberately. Furthermore, we reduce the false positive load by
11185 warning only for non-constant value of type char. */
11187 void
11188 warn_array_subscript_with_type_char (tree index)
11190 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11191 && TREE_CODE (index) != INTEGER_CST)
11192 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11195 /* Implement -Wparentheses for the unexpected C precedence rules, to
11196 cover cases like x + y << z which readers are likely to
11197 misinterpret. We have seen an expression in which CODE is a binary
11198 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11199 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11200 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11201 expression was not formed using a binary or unary operator, or it
11202 was enclosed in parentheses. */
11204 void
11205 warn_about_parentheses (location_t loc, enum tree_code code,
11206 enum tree_code code_left, tree arg_left,
11207 enum tree_code code_right, tree arg_right)
11209 if (!warn_parentheses)
11210 return;
11212 /* This macro tests that the expression ARG with original tree code
11213 CODE appears to be a boolean expression. or the result of folding a
11214 boolean expression. */
11215 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11216 (truth_value_p (TREE_CODE (ARG)) \
11217 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11218 /* Folding may create 0 or 1 integers from other expressions. */ \
11219 || ((CODE) != INTEGER_CST \
11220 && (integer_onep (ARG) || integer_zerop (ARG))))
11222 switch (code)
11224 case LSHIFT_EXPR:
11225 if (code_left == PLUS_EXPR)
11226 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11227 "suggest parentheses around %<+%> inside %<<<%>");
11228 else if (code_right == PLUS_EXPR)
11229 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11230 "suggest parentheses around %<+%> inside %<<<%>");
11231 else if (code_left == MINUS_EXPR)
11232 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11233 "suggest parentheses around %<-%> inside %<<<%>");
11234 else if (code_right == MINUS_EXPR)
11235 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11236 "suggest parentheses around %<-%> inside %<<<%>");
11237 return;
11239 case RSHIFT_EXPR:
11240 if (code_left == PLUS_EXPR)
11241 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11242 "suggest parentheses around %<+%> inside %<>>%>");
11243 else if (code_right == PLUS_EXPR)
11244 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11245 "suggest parentheses around %<+%> inside %<>>%>");
11246 else if (code_left == MINUS_EXPR)
11247 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11248 "suggest parentheses around %<-%> inside %<>>%>");
11249 else if (code_right == MINUS_EXPR)
11250 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11251 "suggest parentheses around %<-%> inside %<>>%>");
11252 return;
11254 case TRUTH_ORIF_EXPR:
11255 if (code_left == TRUTH_ANDIF_EXPR)
11256 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11257 "suggest parentheses around %<&&%> within %<||%>");
11258 else if (code_right == TRUTH_ANDIF_EXPR)
11259 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11260 "suggest parentheses around %<&&%> within %<||%>");
11261 return;
11263 case BIT_IOR_EXPR:
11264 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11265 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11266 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11267 "suggest parentheses around arithmetic in operand of %<|%>");
11268 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11269 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11270 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11271 "suggest parentheses around arithmetic in operand of %<|%>");
11272 /* Check cases like x|y==z */
11273 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11274 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11275 "suggest parentheses around comparison in operand of %<|%>");
11276 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11277 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11278 "suggest parentheses around comparison in operand of %<|%>");
11279 /* Check cases like !x | y */
11280 else if (code_left == TRUTH_NOT_EXPR
11281 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11282 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11283 "suggest parentheses around operand of "
11284 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11285 return;
11287 case BIT_XOR_EXPR:
11288 if (code_left == BIT_AND_EXPR
11289 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11290 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11291 "suggest parentheses around arithmetic in operand of %<^%>");
11292 else if (code_right == BIT_AND_EXPR
11293 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11294 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11295 "suggest parentheses around arithmetic in operand of %<^%>");
11296 /* Check cases like x^y==z */
11297 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11298 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11299 "suggest parentheses around comparison in operand of %<^%>");
11300 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11301 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11302 "suggest parentheses around comparison in operand of %<^%>");
11303 return;
11305 case BIT_AND_EXPR:
11306 if (code_left == PLUS_EXPR)
11307 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11308 "suggest parentheses around %<+%> in operand of %<&%>");
11309 else if (code_right == PLUS_EXPR)
11310 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11311 "suggest parentheses around %<+%> in operand of %<&%>");
11312 else if (code_left == MINUS_EXPR)
11313 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11314 "suggest parentheses around %<-%> in operand of %<&%>");
11315 else if (code_right == MINUS_EXPR)
11316 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11317 "suggest parentheses around %<-%> in operand of %<&%>");
11318 /* Check cases like x&y==z */
11319 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11320 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11321 "suggest parentheses around comparison in operand of %<&%>");
11322 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11323 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11324 "suggest parentheses around comparison in operand of %<&%>");
11325 /* Check cases like !x & y */
11326 else if (code_left == TRUTH_NOT_EXPR
11327 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11328 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11329 "suggest parentheses around operand of "
11330 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11331 return;
11333 case EQ_EXPR:
11334 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11335 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11336 "suggest parentheses around comparison in operand of %<==%>");
11337 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11338 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11339 "suggest parentheses around comparison in operand of %<==%>");
11340 return;
11341 case NE_EXPR:
11342 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11343 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11344 "suggest parentheses around comparison in operand of %<!=%>");
11345 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11346 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11347 "suggest parentheses around comparison in operand of %<!=%>");
11348 return;
11350 default:
11351 if (TREE_CODE_CLASS (code) == tcc_comparison)
11353 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11354 && code_left != NE_EXPR && code_left != EQ_EXPR
11355 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11356 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11357 "comparisons like %<X<=Y<=Z%> do not "
11358 "have their mathematical meaning");
11359 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11360 && code_right != NE_EXPR && code_right != EQ_EXPR
11361 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11362 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11363 "comparisons like %<X<=Y<=Z%> do not "
11364 "have their mathematical meaning");
11366 return;
11368 #undef NOT_A_BOOLEAN_EXPR_P
11371 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11373 void
11374 warn_for_unused_label (tree label)
11376 if (!TREE_USED (label))
11378 if (DECL_INITIAL (label))
11379 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11380 else
11381 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11385 /* Warn for division by zero according to the value of DIVISOR. LOC
11386 is the location of the division operator. */
11388 void
11389 warn_for_div_by_zero (location_t loc, tree divisor)
11391 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11392 about division by zero. Do not issue a warning if DIVISOR has a
11393 floating-point type, since we consider 0.0/0.0 a valid way of
11394 generating a NaN. */
11395 if (c_inhibit_evaluation_warnings == 0
11396 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11397 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11400 /* Subroutine of build_binary_op. Give warnings for comparisons
11401 between signed and unsigned quantities that may fail. Do the
11402 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11403 so that casts will be considered, but default promotions won't
11406 LOCATION is the location of the comparison operator.
11408 The arguments of this function map directly to local variables
11409 of build_binary_op. */
11411 void
11412 warn_for_sign_compare (location_t location,
11413 tree orig_op0, tree orig_op1,
11414 tree op0, tree op1,
11415 tree result_type, enum tree_code resultcode)
11417 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11418 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11419 int unsignedp0, unsignedp1;
11421 /* In C++, check for comparison of different enum types. */
11422 if (c_dialect_cxx()
11423 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11424 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11425 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11426 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11428 warning_at (location,
11429 OPT_Wsign_compare, "comparison between types %qT and %qT",
11430 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11433 /* Do not warn if the comparison is being done in a signed type,
11434 since the signed type will only be chosen if it can represent
11435 all the values of the unsigned type. */
11436 if (!TYPE_UNSIGNED (result_type))
11437 /* OK */;
11438 /* Do not warn if both operands are unsigned. */
11439 else if (op0_signed == op1_signed)
11440 /* OK */;
11441 else
11443 tree sop, uop, base_type;
11444 bool ovf;
11446 if (op0_signed)
11447 sop = orig_op0, uop = orig_op1;
11448 else
11449 sop = orig_op1, uop = orig_op0;
11451 STRIP_TYPE_NOPS (sop);
11452 STRIP_TYPE_NOPS (uop);
11453 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11454 ? TREE_TYPE (result_type) : result_type);
11456 /* Do not warn if the signed quantity is an unsuffixed integer
11457 literal (or some static constant expression involving such
11458 literals or a conditional expression involving such literals)
11459 and it is non-negative. */
11460 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11461 /* OK */;
11462 /* Do not warn if the comparison is an equality operation, the
11463 unsigned quantity is an integral constant, and it would fit
11464 in the result if the result were signed. */
11465 else if (TREE_CODE (uop) == INTEGER_CST
11466 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11467 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11468 /* OK */;
11469 /* In C, do not warn if the unsigned quantity is an enumeration
11470 constant and its maximum value would fit in the result if the
11471 result were signed. */
11472 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11473 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11474 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11475 c_common_signed_type (base_type)))
11476 /* OK */;
11477 else
11478 warning_at (location,
11479 OPT_Wsign_compare,
11480 "comparison between signed and unsigned integer expressions");
11483 /* Warn if two unsigned values are being compared in a size larger
11484 than their original size, and one (and only one) is the result of
11485 a `~' operator. This comparison will always fail.
11487 Also warn if one operand is a constant, and the constant does not
11488 have all bits set that are set in the ~ operand when it is
11489 extended. */
11491 op0 = c_common_get_narrower (op0, &unsignedp0);
11492 op1 = c_common_get_narrower (op1, &unsignedp1);
11494 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11495 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11497 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11498 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11499 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11500 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11502 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11504 tree primop;
11505 HOST_WIDE_INT constant, mask;
11506 int unsignedp;
11507 unsigned int bits;
11509 if (tree_fits_shwi_p (op0))
11511 primop = op1;
11512 unsignedp = unsignedp1;
11513 constant = tree_to_shwi (op0);
11515 else
11517 primop = op0;
11518 unsignedp = unsignedp0;
11519 constant = tree_to_shwi (op1);
11522 bits = TYPE_PRECISION (TREE_TYPE (primop));
11523 if (bits < TYPE_PRECISION (result_type)
11524 && bits < HOST_BITS_PER_LONG && unsignedp)
11526 mask = (~ (HOST_WIDE_INT) 0) << bits;
11527 if ((mask & constant) != mask)
11529 if (constant == 0)
11530 warning_at (location, OPT_Wsign_compare,
11531 "promoted ~unsigned is always non-zero");
11532 else
11533 warning_at (location, OPT_Wsign_compare,
11534 "comparison of promoted ~unsigned with constant");
11538 else if (unsignedp0 && unsignedp1
11539 && (TYPE_PRECISION (TREE_TYPE (op0))
11540 < TYPE_PRECISION (result_type))
11541 && (TYPE_PRECISION (TREE_TYPE (op1))
11542 < TYPE_PRECISION (result_type)))
11543 warning_at (location, OPT_Wsign_compare,
11544 "comparison of promoted ~unsigned with unsigned");
11548 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11549 type via c_common_type. If -Wdouble-promotion is in use, and the
11550 conditions for warning have been met, issue a warning. GMSGID is
11551 the warning message. It must have two %T specifiers for the type
11552 that was converted (generally "float") and the type to which it was
11553 converted (generally "double), respectively. LOC is the location
11554 to which the awrning should refer. */
11556 void
11557 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11558 const char *gmsgid, location_t loc)
11560 tree source_type;
11562 if (!warn_double_promotion)
11563 return;
11564 /* If the conversion will not occur at run-time, there is no need to
11565 warn about it. */
11566 if (c_inhibit_evaluation_warnings)
11567 return;
11568 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11569 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11570 return;
11571 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11572 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11573 source_type = type1;
11574 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11575 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11576 source_type = type2;
11577 else
11578 return;
11579 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11582 /* Setup a TYPE_DECL node as a typedef representation.
11584 X is a TYPE_DECL for a typedef statement. Create a brand new
11585 ..._TYPE node (which will be just a variant of the existing
11586 ..._TYPE node with identical properties) and then install X
11587 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11589 The whole point here is to end up with a situation where each
11590 and every ..._TYPE node the compiler creates will be uniquely
11591 associated with AT MOST one node representing a typedef name.
11592 This way, even though the compiler substitutes corresponding
11593 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11594 early on, later parts of the compiler can always do the reverse
11595 translation and get back the corresponding typedef name. For
11596 example, given:
11598 typedef struct S MY_TYPE;
11599 MY_TYPE object;
11601 Later parts of the compiler might only know that `object' was of
11602 type `struct S' if it were not for code just below. With this
11603 code however, later parts of the compiler see something like:
11605 struct S' == struct S
11606 typedef struct S' MY_TYPE;
11607 struct S' object;
11609 And they can then deduce (from the node for type struct S') that
11610 the original object declaration was:
11612 MY_TYPE object;
11614 Being able to do this is important for proper support of protoize,
11615 and also for generating precise symbolic debugging information
11616 which takes full account of the programmer's (typedef) vocabulary.
11618 Obviously, we don't want to generate a duplicate ..._TYPE node if
11619 the TYPE_DECL node that we are now processing really represents a
11620 standard built-in type. */
11622 void
11623 set_underlying_type (tree x)
11625 if (x == error_mark_node)
11626 return;
11627 if (DECL_IS_BUILTIN (x))
11629 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11630 TYPE_NAME (TREE_TYPE (x)) = x;
11632 else if (TREE_TYPE (x) != error_mark_node
11633 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11635 tree tt = TREE_TYPE (x);
11636 DECL_ORIGINAL_TYPE (x) = tt;
11637 tt = build_variant_type_copy (tt);
11638 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11639 TYPE_NAME (tt) = x;
11640 TREE_USED (tt) = TREE_USED (x);
11641 TREE_TYPE (x) = tt;
11645 /* Record the types used by the current global variable declaration
11646 being parsed, so that we can decide later to emit their debug info.
11647 Those types are in types_used_by_cur_var_decl, and we are going to
11648 store them in the types_used_by_vars_hash hash table.
11649 DECL is the declaration of the global variable that has been parsed. */
11651 void
11652 record_types_used_by_current_var_decl (tree decl)
11654 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11656 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11658 tree type = types_used_by_cur_var_decl->pop ();
11659 types_used_by_var_decl_insert (type, decl);
11663 /* If DECL is a typedef that is declared in the current function,
11664 record it for the purpose of -Wunused-local-typedefs. */
11666 void
11667 record_locally_defined_typedef (tree decl)
11669 struct c_language_function *l;
11671 if (!warn_unused_local_typedefs
11672 || cfun == NULL
11673 /* if this is not a locally defined typedef then we are not
11674 interested. */
11675 || !is_typedef_decl (decl)
11676 || !decl_function_context (decl))
11677 return;
11679 l = (struct c_language_function *) cfun->language;
11680 vec_safe_push (l->local_typedefs, decl);
11683 /* If T is a TYPE_DECL declared locally, mark it as used. */
11685 void
11686 maybe_record_typedef_use (tree t)
11688 if (!is_typedef_decl (t))
11689 return;
11691 TREE_USED (t) = true;
11694 /* Warn if there are some unused locally defined typedefs in the
11695 current function. */
11697 void
11698 maybe_warn_unused_local_typedefs (void)
11700 int i;
11701 tree decl;
11702 /* The number of times we have emitted -Wunused-local-typedefs
11703 warnings. If this is different from errorcount, that means some
11704 unrelated errors have been issued. In which case, we'll avoid
11705 emitting "unused-local-typedefs" warnings. */
11706 static int unused_local_typedefs_warn_count;
11707 struct c_language_function *l;
11709 if (cfun == NULL)
11710 return;
11712 if ((l = (struct c_language_function *) cfun->language) == NULL)
11713 return;
11715 if (warn_unused_local_typedefs
11716 && errorcount == unused_local_typedefs_warn_count)
11718 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11719 if (!TREE_USED (decl))
11720 warning_at (DECL_SOURCE_LOCATION (decl),
11721 OPT_Wunused_local_typedefs,
11722 "typedef %qD locally defined but not used", decl);
11723 unused_local_typedefs_warn_count = errorcount;
11726 vec_free (l->local_typedefs);
11729 /* Warn about boolean expression compared with an integer value different
11730 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11731 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11732 are the operands of the comparison. The caller must ensure that
11733 either operand is a boolean expression. */
11735 void
11736 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11737 tree op1)
11739 if (TREE_CODE_CLASS (code) != tcc_comparison)
11740 return;
11742 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11743 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11744 if (!cst)
11745 return;
11747 if (!integer_zerop (cst) && !integer_onep (cst))
11749 int sign = (TREE_CODE (op0) == INTEGER_CST)
11750 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11751 if (code == EQ_EXPR
11752 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11753 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11754 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11755 "with boolean expression is always false", cst);
11756 else
11757 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11758 "with boolean expression is always true", cst);
11762 /* The C and C++ parsers both use vectors to hold function arguments.
11763 For efficiency, we keep a cache of unused vectors. This is the
11764 cache. */
11766 typedef vec<tree, va_gc> *tree_gc_vec;
11767 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11769 /* Return a new vector from the cache. If the cache is empty,
11770 allocate a new vector. These vectors are GC'ed, so it is OK if the
11771 pointer is not released.. */
11773 vec<tree, va_gc> *
11774 make_tree_vector (void)
11776 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11777 return tree_vector_cache->pop ();
11778 else
11780 /* Passing 0 to vec::alloc returns NULL, and our callers require
11781 that we always return a non-NULL value. The vector code uses
11782 4 when growing a NULL vector, so we do too. */
11783 vec<tree, va_gc> *v;
11784 vec_alloc (v, 4);
11785 return v;
11789 /* Release a vector of trees back to the cache. */
11791 void
11792 release_tree_vector (vec<tree, va_gc> *vec)
11794 if (vec != NULL)
11796 vec->truncate (0);
11797 vec_safe_push (tree_vector_cache, vec);
11801 /* Get a new tree vector holding a single tree. */
11803 vec<tree, va_gc> *
11804 make_tree_vector_single (tree t)
11806 vec<tree, va_gc> *ret = make_tree_vector ();
11807 ret->quick_push (t);
11808 return ret;
11811 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11813 vec<tree, va_gc> *
11814 make_tree_vector_from_list (tree list)
11816 vec<tree, va_gc> *ret = make_tree_vector ();
11817 for (; list; list = TREE_CHAIN (list))
11818 vec_safe_push (ret, TREE_VALUE (list));
11819 return ret;
11822 /* Get a new tree vector which is a copy of an existing one. */
11824 vec<tree, va_gc> *
11825 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11827 vec<tree, va_gc> *ret;
11828 unsigned int ix;
11829 tree t;
11831 ret = make_tree_vector ();
11832 vec_safe_reserve (ret, vec_safe_length (orig));
11833 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11834 ret->quick_push (t);
11835 return ret;
11838 /* Return true if KEYWORD starts a type specifier. */
11840 bool
11841 keyword_begins_type_specifier (enum rid keyword)
11843 switch (keyword)
11845 case RID_AUTO_TYPE:
11846 case RID_INT:
11847 case RID_CHAR:
11848 case RID_FLOAT:
11849 case RID_DOUBLE:
11850 case RID_VOID:
11851 case RID_INT128:
11852 case RID_UNSIGNED:
11853 case RID_LONG:
11854 case RID_SHORT:
11855 case RID_SIGNED:
11856 case RID_DFLOAT32:
11857 case RID_DFLOAT64:
11858 case RID_DFLOAT128:
11859 case RID_FRACT:
11860 case RID_ACCUM:
11861 case RID_BOOL:
11862 case RID_WCHAR:
11863 case RID_CHAR16:
11864 case RID_CHAR32:
11865 case RID_SAT:
11866 case RID_COMPLEX:
11867 case RID_TYPEOF:
11868 case RID_STRUCT:
11869 case RID_CLASS:
11870 case RID_UNION:
11871 case RID_ENUM:
11872 return true;
11873 default:
11874 return false;
11878 /* Return true if KEYWORD names a type qualifier. */
11880 bool
11881 keyword_is_type_qualifier (enum rid keyword)
11883 switch (keyword)
11885 case RID_CONST:
11886 case RID_VOLATILE:
11887 case RID_RESTRICT:
11888 case RID_ATOMIC:
11889 return true;
11890 default:
11891 return false;
11895 /* Return true if KEYWORD names a storage class specifier.
11897 RID_TYPEDEF is not included in this list despite `typedef' being
11898 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11899 such for syntactic convenience only. */
11901 bool
11902 keyword_is_storage_class_specifier (enum rid keyword)
11904 switch (keyword)
11906 case RID_STATIC:
11907 case RID_EXTERN:
11908 case RID_REGISTER:
11909 case RID_AUTO:
11910 case RID_MUTABLE:
11911 case RID_THREAD:
11912 return true;
11913 default:
11914 return false;
11918 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11920 static bool
11921 keyword_is_function_specifier (enum rid keyword)
11923 switch (keyword)
11925 case RID_INLINE:
11926 case RID_NORETURN:
11927 case RID_VIRTUAL:
11928 case RID_EXPLICIT:
11929 return true;
11930 default:
11931 return false;
11935 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11936 declaration-specifier (C99 6.7). */
11938 bool
11939 keyword_is_decl_specifier (enum rid keyword)
11941 if (keyword_is_storage_class_specifier (keyword)
11942 || keyword_is_type_qualifier (keyword)
11943 || keyword_is_function_specifier (keyword))
11944 return true;
11946 switch (keyword)
11948 case RID_TYPEDEF:
11949 case RID_FRIEND:
11950 case RID_CONSTEXPR:
11951 return true;
11952 default:
11953 return false;
11957 /* Initialize language-specific-bits of tree_contains_struct. */
11959 void
11960 c_common_init_ts (void)
11962 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11963 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11964 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11965 MARK_TS_COMMON (UPC_FORALL_STMT);
11966 MARK_TS_COMMON (UPC_SYNC_STMT);
11969 /* Build a user-defined numeric literal out of an integer constant type VALUE
11970 with identifier SUFFIX. */
11972 tree
11973 build_userdef_literal (tree suffix_id, tree value,
11974 enum overflow_type overflow, tree num_string)
11976 tree literal = make_node (USERDEF_LITERAL);
11977 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11978 USERDEF_LITERAL_VALUE (literal) = value;
11979 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11980 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11981 return literal;
11984 /* For vector[index], convert the vector to a
11985 pointer of the underlying type. */
11986 void
11987 convert_vector_to_pointer_for_subscript (location_t loc,
11988 tree* vecp, tree index)
11990 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11992 tree type = TREE_TYPE (*vecp);
11993 tree type1;
11995 if (TREE_CODE (index) == INTEGER_CST)
11996 if (!tree_fits_uhwi_p (index)
11997 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
11998 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12000 c_common_mark_addressable_vec (*vecp);
12001 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12002 type1 = build_pointer_type (TREE_TYPE (*vecp));
12003 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12004 if (!ref_all
12005 && !DECL_P (*vecp))
12007 /* If the original vector isn't declared may_alias and it
12008 isn't a bare vector look if the subscripting would
12009 alias the vector we subscript, and if not, force ref-all. */
12010 alias_set_type vecset = get_alias_set (*vecp);
12011 alias_set_type sset = get_alias_set (type);
12012 if (!alias_sets_must_conflict_p (sset, vecset)
12013 && !alias_set_subset_of (sset, vecset))
12014 ref_all = true;
12016 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12017 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12018 *vecp = convert (type, *vecp);
12022 /* Determine which of the operands, if any, is a scalar that needs to be
12023 converted to a vector, for the range of operations. */
12024 enum stv_conv
12025 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12026 bool complain)
12028 tree type0 = TREE_TYPE (op0);
12029 tree type1 = TREE_TYPE (op1);
12030 bool integer_only_op = false;
12031 enum stv_conv ret = stv_firstarg;
12033 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12034 || TREE_CODE (type1) == VECTOR_TYPE);
12035 switch (code)
12037 /* Most GENERIC binary expressions require homogeneous arguments.
12038 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12039 argument that is a vector and a second one that is a scalar, so
12040 we never return stv_secondarg for them. */
12041 case RSHIFT_EXPR:
12042 case LSHIFT_EXPR:
12043 if (TREE_CODE (type0) == INTEGER_TYPE
12044 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12046 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12048 if (complain)
12049 error_at (loc, "conversion of scalar %qT to vector %qT "
12050 "involves truncation", type0, type1);
12051 return stv_error;
12053 else
12054 return stv_firstarg;
12056 break;
12058 case BIT_IOR_EXPR:
12059 case BIT_XOR_EXPR:
12060 case BIT_AND_EXPR:
12061 integer_only_op = true;
12062 /* ... fall through ... */
12064 case VEC_COND_EXPR:
12066 case PLUS_EXPR:
12067 case MINUS_EXPR:
12068 case MULT_EXPR:
12069 case TRUNC_DIV_EXPR:
12070 case CEIL_DIV_EXPR:
12071 case FLOOR_DIV_EXPR:
12072 case ROUND_DIV_EXPR:
12073 case EXACT_DIV_EXPR:
12074 case TRUNC_MOD_EXPR:
12075 case FLOOR_MOD_EXPR:
12076 case RDIV_EXPR:
12077 case EQ_EXPR:
12078 case NE_EXPR:
12079 case LE_EXPR:
12080 case GE_EXPR:
12081 case LT_EXPR:
12082 case GT_EXPR:
12083 /* What about UNLT_EXPR? */
12084 if (TREE_CODE (type0) == VECTOR_TYPE)
12086 tree tmp;
12087 ret = stv_secondarg;
12088 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12089 tmp = type0; type0 = type1; type1 = tmp;
12090 tmp = op0; op0 = op1; op1 = tmp;
12093 if (TREE_CODE (type0) == INTEGER_TYPE
12094 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12096 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12098 if (complain)
12099 error_at (loc, "conversion of scalar %qT to vector %qT "
12100 "involves truncation", type0, type1);
12101 return stv_error;
12103 return ret;
12105 else if (!integer_only_op
12106 /* Allow integer --> real conversion if safe. */
12107 && (TREE_CODE (type0) == REAL_TYPE
12108 || TREE_CODE (type0) == INTEGER_TYPE)
12109 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12111 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12113 if (complain)
12114 error_at (loc, "conversion of scalar %qT to vector %qT "
12115 "involves truncation", type0, type1);
12116 return stv_error;
12118 return ret;
12120 default:
12121 break;
12124 return stv_nothing;
12127 /* Return true iff ALIGN is an integral constant that is a fundamental
12128 alignment, as defined by [basic.align] in the c++-11
12129 specifications.
12131 That is:
12133 [A fundamental alignment is represented by an alignment less than or
12134 equal to the greatest alignment supported by the implementation
12135 in all contexts, which is equal to
12136 alignof(max_align_t)]. */
12138 bool
12139 cxx_fundamental_alignment_p (unsigned align)
12141 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12142 TYPE_ALIGN (long_double_type_node)));
12145 /* Return true if T is a pointer to a zero-sized aggregate. */
12147 bool
12148 pointer_to_zero_sized_aggr_p (tree t)
12150 if (!POINTER_TYPE_P (t))
12151 return false;
12152 t = TREE_TYPE (t);
12153 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12156 #include "gt-c-family-c-common.h"