Merged trunk revision 217709 into the hsa branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob7e348d3da2b09f90c713132530bc392930daa19b
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "c-common.h"
24 #include "tm.h"
25 #include "intl.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "varasm.h"
33 #include "trans-mem.h"
34 #include "flags.h"
35 #include "c-pragma.h"
36 #include "c-objc.h"
37 #include "tm_p.h"
38 #include "obstack.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "diagnostic.h"
46 #include "tree-iterator.h"
47 #include "hashtab.h"
48 #include "opts.h"
49 #include "hash-map.h"
50 #include "is-a.h"
51 #include "plugin-api.h"
52 #include "vec.h"
53 #include "hash-set.h"
54 #include "machmode.h"
55 #include "hard-reg-set.h"
56 #include "input.h"
57 #include "function.h"
58 #include "ipa-ref.h"
59 #include "cgraph.h"
60 #include "target-def.h"
61 #include "gimplify.h"
62 #include "wide-int-print.h"
64 cpp_reader *parse_in; /* Declared in c-pragma.h. */
66 /* The following symbols are subsumed in the c_global_trees array, and
67 listed here individually for documentation purposes.
69 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
71 tree short_integer_type_node;
72 tree long_integer_type_node;
73 tree long_long_integer_type_node;
75 tree short_unsigned_type_node;
76 tree long_unsigned_type_node;
77 tree long_long_unsigned_type_node;
79 tree truthvalue_type_node;
80 tree truthvalue_false_node;
81 tree truthvalue_true_node;
83 tree ptrdiff_type_node;
85 tree unsigned_char_type_node;
86 tree signed_char_type_node;
87 tree wchar_type_node;
89 tree char16_type_node;
90 tree char32_type_node;
92 tree float_type_node;
93 tree double_type_node;
94 tree long_double_type_node;
96 tree complex_integer_type_node;
97 tree complex_float_type_node;
98 tree complex_double_type_node;
99 tree complex_long_double_type_node;
101 tree dfloat32_type_node;
102 tree dfloat64_type_node;
103 tree_dfloat128_type_node;
105 tree intQI_type_node;
106 tree intHI_type_node;
107 tree intSI_type_node;
108 tree intDI_type_node;
109 tree intTI_type_node;
111 tree unsigned_intQI_type_node;
112 tree unsigned_intHI_type_node;
113 tree unsigned_intSI_type_node;
114 tree unsigned_intDI_type_node;
115 tree unsigned_intTI_type_node;
117 tree widest_integer_literal_type_node;
118 tree widest_unsigned_literal_type_node;
120 Nodes for types `void *' and `const void *'.
122 tree ptr_type_node, const_ptr_type_node;
124 Nodes for types `char *' and `const char *'.
126 tree string_type_node, const_string_type_node;
128 Type `char[SOMENUMBER]'.
129 Used when an array of char is needed and the size is irrelevant.
131 tree char_array_type_node;
133 Type `wchar_t[SOMENUMBER]' or something like it.
134 Used when a wide string literal is created.
136 tree wchar_array_type_node;
138 Type `char16_t[SOMENUMBER]' or something like it.
139 Used when a UTF-16 string literal is created.
141 tree char16_array_type_node;
143 Type `char32_t[SOMENUMBER]' or something like it.
144 Used when a UTF-32 string literal is created.
146 tree char32_array_type_node;
148 Type `int ()' -- used for implicit declaration of functions.
150 tree default_function_type;
152 A VOID_TYPE node, packaged in a TREE_LIST.
154 tree void_list_node;
156 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
157 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
158 VAR_DECLS, but C++ does.)
160 tree function_name_decl_node;
161 tree pretty_function_name_decl_node;
162 tree c99_function_name_decl_node;
164 Stack of nested function name VAR_DECLs.
166 tree saved_function_name_decls;
170 tree c_global_trees[CTI_MAX];
172 /* Switches common to the C front ends. */
174 /* Nonzero means don't output line number information. */
176 char flag_no_line_commands;
178 /* Nonzero causes -E output not to be done, but directives such as
179 #define that have side effects are still obeyed. */
181 char flag_no_output;
183 /* Nonzero means dump macros in some fashion. */
185 char flag_dump_macros;
187 /* Nonzero means pass #include lines through to the output. */
189 char flag_dump_includes;
191 /* Nonzero means process PCH files while preprocessing. */
193 bool flag_pch_preprocess;
195 /* The file name to which we should write a precompiled header, or
196 NULL if no header will be written in this compile. */
198 const char *pch_file;
200 /* Nonzero if an ISO standard was selected. It rejects macros in the
201 user's namespace. */
202 int flag_iso;
204 /* C/ObjC language option variables. */
207 /* Nonzero means allow type mismatches in conditional expressions;
208 just make their values `void'. */
210 int flag_cond_mismatch;
212 /* Nonzero means enable C89 Amendment 1 features. */
214 int flag_isoc94;
216 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
218 int flag_isoc99;
220 /* Nonzero means use the ISO C11 dialect of C. */
222 int flag_isoc11;
224 /* Nonzero means that we have builtin functions, and main is an int. */
226 int flag_hosted = 1;
229 /* ObjC language option variables. */
232 /* Tells the compiler that this is a special run. Do not perform any
233 compiling, instead we are to test some platform dependent features
234 and output a C header file with appropriate definitions. */
236 int print_struct_values;
238 /* Tells the compiler what is the constant string class for ObjC. */
240 const char *constant_string_class_name;
243 /* C++ language option variables. */
246 /* Nonzero means generate separate instantiation control files and
247 juggle them at link time. */
249 int flag_use_repository;
251 /* The C++ dialect being used. C++98 is the default. */
253 enum cxx_dialect cxx_dialect = cxx98;
255 /* Maximum template instantiation depth. This limit exists to limit the
256 time it takes to notice excessively recursive template instantiations.
258 The default is lower than the 1024 recommended by the C++0x standard
259 because G++ runs out of stack before 1024 with highly recursive template
260 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
262 int max_tinst_depth = 900;
264 /* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
266 tree *ridpointers;
268 tree (*make_fname_decl) (location_t, tree, int);
270 /* Nonzero means don't warn about problems that occur when the code is
271 executed. */
272 int c_inhibit_evaluation_warnings;
274 /* Whether we are building a boolean conversion inside
275 convert_for_assignment, or some other late binary operation. If
276 build_binary_op is called for C (from code shared by C and C++) in
277 this case, then the operands have already been folded and the
278 result will not be folded again, so C_MAYBE_CONST_EXPR should not
279 be generated. */
280 bool in_late_binary_op;
282 /* Whether lexing has been completed, so subsequent preprocessor
283 errors should use the compiler's input_location. */
284 bool done_lexing = false;
286 /* Information about how a function name is generated. */
287 struct fname_var_t
289 tree *const decl; /* pointer to the VAR_DECL. */
290 const unsigned rid; /* RID number for the identifier. */
291 const int pretty; /* How pretty is it? */
294 /* The three ways of getting then name of the current function. */
296 const struct fname_var_t fname_vars[] =
298 /* C99 compliant __func__, must be first. */
299 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
300 /* GCC __FUNCTION__ compliant. */
301 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
302 /* GCC __PRETTY_FUNCTION__ compliant. */
303 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
304 {NULL, 0, 0},
307 /* Global visibility options. */
308 struct visibility_flags visibility_options;
310 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
311 static tree check_case_value (location_t, tree);
312 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
314 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
315 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
321 int, bool *);
322 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
323 int, bool *);
324 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
325 bool *);
326 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_hsa_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_always_inline_attribute (tree *, tree, tree, int,
331 bool *);
332 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
339 bool *);
340 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
341 bool *);
342 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_transparent_union_attribute (tree *, tree, tree,
344 int, bool *);
345 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
351 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
352 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
355 static tree handle_visibility_attribute (tree *, tree, tree, int,
356 bool *);
357 static tree handle_tls_model_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_no_instrument_function_attribute (tree *, tree,
360 tree, int, bool *);
361 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_deprecated_attribute (tree *, tree, tree, int,
370 bool *);
371 static tree handle_vector_size_attribute (tree *, tree, tree, int,
372 bool *);
373 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
385 static tree ignore_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
391 bool *);
392 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
393 bool *);
394 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
397 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
399 static void check_function_nonnull (tree, int, tree *);
400 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
401 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
402 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
403 static int resort_field_decl_cmp (const void *, const void *);
405 /* Reserved words. The third field is a mask: keywords are disabled
406 if they match the mask.
408 Masks for languages:
409 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
410 C --std=c99: D_CXXONLY | D_OBJC
411 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
412 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
413 C++ --std=c0x: D_CONLY | D_OBJC
414 ObjC++ is like C++ except that D_OBJC is not set
416 If -fno-asm is used, D_ASM is added to the mask. If
417 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
418 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
419 In C with -Wc++-compat, we warn if D_CXXWARN is set.
421 Note the complication of the D_CXX_OBJC keywords. These are
422 reserved words such as 'class'. In C++, 'class' is a reserved
423 word. In Objective-C++ it is too. In Objective-C, it is a
424 reserved word too, but only if it follows an '@' sign.
426 const struct c_common_resword c_common_reswords[] =
428 { "_Alignas", RID_ALIGNAS, D_CONLY },
429 { "_Alignof", RID_ALIGNOF, D_CONLY },
430 { "_Atomic", RID_ATOMIC, D_CONLY },
431 { "_Bool", RID_BOOL, D_CONLY },
432 { "_Complex", RID_COMPLEX, 0 },
433 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
434 { "_Cilk_sync", RID_CILK_SYNC, 0 },
435 { "_Cilk_for", RID_CILK_FOR, 0 },
436 { "_Imaginary", RID_IMAGINARY, D_CONLY },
437 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
438 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
439 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
440 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
441 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
442 { "_Sat", RID_SAT, D_CONLY | D_EXT },
443 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
444 { "_Noreturn", RID_NORETURN, D_CONLY },
445 { "_Generic", RID_GENERIC, D_CONLY },
446 { "_Thread_local", RID_THREAD, D_CONLY },
447 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
448 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
449 { "__alignof", RID_ALIGNOF, 0 },
450 { "__alignof__", RID_ALIGNOF, 0 },
451 { "__asm", RID_ASM, 0 },
452 { "__asm__", RID_ASM, 0 },
453 { "__attribute", RID_ATTRIBUTE, 0 },
454 { "__attribute__", RID_ATTRIBUTE, 0 },
455 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
456 { "__bases", RID_BASES, D_CXXONLY },
457 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
458 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
459 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
460 { "__builtin_offsetof", RID_OFFSETOF, 0 },
461 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
462 { "__builtin_va_arg", RID_VA_ARG, 0 },
463 { "__complex", RID_COMPLEX, 0 },
464 { "__complex__", RID_COMPLEX, 0 },
465 { "__const", RID_CONST, 0 },
466 { "__const__", RID_CONST, 0 },
467 { "__decltype", RID_DECLTYPE, D_CXXONLY },
468 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
469 { "__extension__", RID_EXTENSION, 0 },
470 { "__func__", RID_C99_FUNCTION_NAME, 0 },
471 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
472 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
473 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
474 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
475 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
476 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
477 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
478 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
479 { "__imag", RID_IMAGPART, 0 },
480 { "__imag__", RID_IMAGPART, 0 },
481 { "__inline", RID_INLINE, 0 },
482 { "__inline__", RID_INLINE, 0 },
483 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
484 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
485 { "__is_class", RID_IS_CLASS, D_CXXONLY },
486 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
487 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
488 { "__is_final", RID_IS_FINAL, D_CXXONLY },
489 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
490 { "__is_pod", RID_IS_POD, D_CXXONLY },
491 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
492 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
493 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
494 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
495 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
496 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
497 { "__is_union", RID_IS_UNION, D_CXXONLY },
498 { "__label__", RID_LABEL, 0 },
499 { "__null", RID_NULL, 0 },
500 { "__real", RID_REALPART, 0 },
501 { "__real__", RID_REALPART, 0 },
502 { "__restrict", RID_RESTRICT, 0 },
503 { "__restrict__", RID_RESTRICT, 0 },
504 { "__signed", RID_SIGNED, 0 },
505 { "__signed__", RID_SIGNED, 0 },
506 { "__thread", RID_THREAD, 0 },
507 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
508 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
509 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
510 { "__typeof", RID_TYPEOF, 0 },
511 { "__typeof__", RID_TYPEOF, 0 },
512 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
513 { "__volatile", RID_VOLATILE, 0 },
514 { "__volatile__", RID_VOLATILE, 0 },
515 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
516 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
517 { "asm", RID_ASM, D_ASM },
518 { "auto", RID_AUTO, 0 },
519 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
520 { "break", RID_BREAK, 0 },
521 { "case", RID_CASE, 0 },
522 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
523 { "char", RID_CHAR, 0 },
524 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
525 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
526 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
527 { "const", RID_CONST, 0 },
528 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
529 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
530 { "continue", RID_CONTINUE, 0 },
531 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
532 { "default", RID_DEFAULT, 0 },
533 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
534 { "do", RID_DO, 0 },
535 { "double", RID_DOUBLE, 0 },
536 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
537 { "else", RID_ELSE, 0 },
538 { "enum", RID_ENUM, 0 },
539 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
540 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
541 { "extern", RID_EXTERN, 0 },
542 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
543 { "float", RID_FLOAT, 0 },
544 { "for", RID_FOR, 0 },
545 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
546 { "goto", RID_GOTO, 0 },
547 { "if", RID_IF, 0 },
548 { "inline", RID_INLINE, D_EXT89 },
549 { "int", RID_INT, 0 },
550 { "long", RID_LONG, 0 },
551 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
552 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
553 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
554 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
555 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
556 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
557 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
558 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
559 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
560 { "register", RID_REGISTER, 0 },
561 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
562 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
563 { "return", RID_RETURN, 0 },
564 { "short", RID_SHORT, 0 },
565 { "signed", RID_SIGNED, 0 },
566 { "sizeof", RID_SIZEOF, 0 },
567 { "static", RID_STATIC, 0 },
568 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
569 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
570 { "struct", RID_STRUCT, 0 },
571 { "switch", RID_SWITCH, 0 },
572 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
573 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
574 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
575 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
576 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
577 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
578 { "typedef", RID_TYPEDEF, 0 },
579 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
580 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
581 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
582 { "union", RID_UNION, 0 },
583 { "unsigned", RID_UNSIGNED, 0 },
584 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
585 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
586 { "void", RID_VOID, 0 },
587 { "volatile", RID_VOLATILE, 0 },
588 { "wchar_t", RID_WCHAR, D_CXXONLY },
589 { "while", RID_WHILE, 0 },
590 /* These Objective-C keywords are recognized only immediately after
591 an '@'. */
592 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
593 { "defs", RID_AT_DEFS, D_OBJC },
594 { "encode", RID_AT_ENCODE, D_OBJC },
595 { "end", RID_AT_END, D_OBJC },
596 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
597 { "interface", RID_AT_INTERFACE, D_OBJC },
598 { "protocol", RID_AT_PROTOCOL, D_OBJC },
599 { "selector", RID_AT_SELECTOR, D_OBJC },
600 { "finally", RID_AT_FINALLY, D_OBJC },
601 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
602 { "optional", RID_AT_OPTIONAL, D_OBJC },
603 { "required", RID_AT_REQUIRED, D_OBJC },
604 { "property", RID_AT_PROPERTY, D_OBJC },
605 { "package", RID_AT_PACKAGE, D_OBJC },
606 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
607 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
608 /* These are recognized only in protocol-qualifier context
609 (see above) */
610 { "bycopy", RID_BYCOPY, D_OBJC },
611 { "byref", RID_BYREF, D_OBJC },
612 { "in", RID_IN, D_OBJC },
613 { "inout", RID_INOUT, D_OBJC },
614 { "oneway", RID_ONEWAY, D_OBJC },
615 { "out", RID_OUT, D_OBJC },
616 /* These are recognized inside a property attribute list */
617 { "assign", RID_ASSIGN, D_OBJC },
618 { "copy", RID_COPY, D_OBJC },
619 { "getter", RID_GETTER, D_OBJC },
620 { "nonatomic", RID_NONATOMIC, D_OBJC },
621 { "readonly", RID_READONLY, D_OBJC },
622 { "readwrite", RID_READWRITE, D_OBJC },
623 { "retain", RID_RETAIN, D_OBJC },
624 { "setter", RID_SETTER, D_OBJC },
627 const unsigned int num_c_common_reswords =
628 sizeof c_common_reswords / sizeof (struct c_common_resword);
630 /* Table of machine-independent attributes common to all C-like languages.
632 All attributes referencing arguments should be additionally processed
633 in chkp_copy_function_type_adding_bounds for correct instrumentation
634 by Pointer Bounds Checker.
635 Current list of processed common attributes: nonnull. */
636 const struct attribute_spec c_common_attribute_table[] =
638 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
639 affects_type_identity } */
640 { "packed", 0, 0, false, false, false,
641 handle_packed_attribute , false},
642 { "nocommon", 0, 0, true, false, false,
643 handle_nocommon_attribute, false},
644 { "common", 0, 0, true, false, false,
645 handle_common_attribute, false },
646 /* FIXME: logically, noreturn attributes should be listed as
647 "false, true, true" and apply to function types. But implementing this
648 would require all the places in the compiler that use TREE_THIS_VOLATILE
649 on a decl to identify non-returning functions to be located and fixed
650 to check the function type instead. */
651 { "noreturn", 0, 0, true, false, false,
652 handle_noreturn_attribute, false },
653 { "volatile", 0, 0, true, false, false,
654 handle_noreturn_attribute, false },
655 { "noinline", 0, 0, true, false, false,
656 handle_noinline_attribute, false },
657 { "noclone", 0, 0, true, false, false,
658 handle_noclone_attribute, false },
659 { "hsa", 0, 0, true, false, false,
660 handle_hsa_attribute, false },
661 { "hsakernel", 0, 0, true, false, false,
662 handle_hsa_attribute, false },
663 { "leaf", 0, 0, true, false, false,
664 handle_leaf_attribute, false },
665 { "always_inline", 0, 0, true, false, false,
666 handle_always_inline_attribute, false },
667 { "gnu_inline", 0, 0, true, false, false,
668 handle_gnu_inline_attribute, false },
669 { "artificial", 0, 0, true, false, false,
670 handle_artificial_attribute, false },
671 { "flatten", 0, 0, true, false, false,
672 handle_flatten_attribute, false },
673 { "used", 0, 0, true, false, false,
674 handle_used_attribute, false },
675 { "unused", 0, 0, false, false, false,
676 handle_unused_attribute, false },
677 { "externally_visible", 0, 0, true, false, false,
678 handle_externally_visible_attribute, false },
679 { "no_reorder", 0, 0, true, false, false,
680 handle_no_reorder_attribute, false },
681 /* The same comments as for noreturn attributes apply to const ones. */
682 { "const", 0, 0, true, false, false,
683 handle_const_attribute, false },
684 { "transparent_union", 0, 0, false, false, false,
685 handle_transparent_union_attribute, false },
686 { "constructor", 0, 1, true, false, false,
687 handle_constructor_attribute, false },
688 { "destructor", 0, 1, true, false, false,
689 handle_destructor_attribute, false },
690 { "mode", 1, 1, false, true, false,
691 handle_mode_attribute, false },
692 { "section", 1, 1, true, false, false,
693 handle_section_attribute, false },
694 { "aligned", 0, 1, false, false, false,
695 handle_aligned_attribute, false },
696 { "weak", 0, 0, true, false, false,
697 handle_weak_attribute, false },
698 { "ifunc", 1, 1, true, false, false,
699 handle_ifunc_attribute, false },
700 { "alias", 1, 1, true, false, false,
701 handle_alias_attribute, false },
702 { "weakref", 0, 1, true, false, false,
703 handle_weakref_attribute, false },
704 { "no_instrument_function", 0, 0, true, false, false,
705 handle_no_instrument_function_attribute,
706 false },
707 { "malloc", 0, 0, true, false, false,
708 handle_malloc_attribute, false },
709 { "returns_twice", 0, 0, true, false, false,
710 handle_returns_twice_attribute, false },
711 { "no_stack_limit", 0, 0, true, false, false,
712 handle_no_limit_stack_attribute, false },
713 { "pure", 0, 0, true, false, false,
714 handle_pure_attribute, false },
715 { "transaction_callable", 0, 0, false, true, false,
716 handle_tm_attribute, false },
717 { "transaction_unsafe", 0, 0, false, true, false,
718 handle_tm_attribute, false },
719 { "transaction_safe", 0, 0, false, true, false,
720 handle_tm_attribute, false },
721 { "transaction_may_cancel_outer", 0, 0, false, true, false,
722 handle_tm_attribute, false },
723 /* ??? These two attributes didn't make the transition from the
724 Intel language document to the multi-vendor language document. */
725 { "transaction_pure", 0, 0, false, true, false,
726 handle_tm_attribute, false },
727 { "transaction_wrap", 1, 1, true, false, false,
728 handle_tm_wrap_attribute, false },
729 /* For internal use (marking of builtins) only. The name contains space
730 to prevent its usage in source code. */
731 { "no vops", 0, 0, true, false, false,
732 handle_novops_attribute, false },
733 { "deprecated", 0, 1, false, false, false,
734 handle_deprecated_attribute, false },
735 { "vector_size", 1, 1, false, true, false,
736 handle_vector_size_attribute, false },
737 { "visibility", 1, 1, false, false, false,
738 handle_visibility_attribute, false },
739 { "tls_model", 1, 1, true, false, false,
740 handle_tls_model_attribute, false },
741 { "nonnull", 0, -1, false, true, true,
742 handle_nonnull_attribute, false },
743 { "nothrow", 0, 0, true, false, false,
744 handle_nothrow_attribute, false },
745 { "may_alias", 0, 0, false, true, false, NULL, false },
746 { "cleanup", 1, 1, true, false, false,
747 handle_cleanup_attribute, false },
748 { "warn_unused_result", 0, 0, false, true, true,
749 handle_warn_unused_result_attribute, false },
750 { "sentinel", 0, 1, false, true, true,
751 handle_sentinel_attribute, false },
752 /* For internal use (marking of builtins) only. The name contains space
753 to prevent its usage in source code. */
754 { "type generic", 0, 0, false, true, true,
755 handle_type_generic_attribute, false },
756 { "alloc_size", 1, 2, false, true, true,
757 handle_alloc_size_attribute, false },
758 { "cold", 0, 0, true, false, false,
759 handle_cold_attribute, false },
760 { "hot", 0, 0, true, false, false,
761 handle_hot_attribute, false },
762 { "no_address_safety_analysis",
763 0, 0, true, false, false,
764 handle_no_address_safety_analysis_attribute,
765 false },
766 { "no_sanitize_address", 0, 0, true, false, false,
767 handle_no_sanitize_address_attribute,
768 false },
769 { "no_sanitize_undefined", 0, 0, true, false, false,
770 handle_no_sanitize_undefined_attribute,
771 false },
772 { "warning", 1, 1, true, false, false,
773 handle_error_attribute, false },
774 { "error", 1, 1, true, false, false,
775 handle_error_attribute, false },
776 { "target", 1, -1, true, false, false,
777 handle_target_attribute, false },
778 { "optimize", 1, -1, true, false, false,
779 handle_optimize_attribute, false },
780 /* For internal use only. The leading '*' both prevents its usage in
781 source code and signals that it may be overridden by machine tables. */
782 { "*tm regparm", 0, 0, false, true, true,
783 ignore_attribute, false },
784 { "no_split_stack", 0, 0, true, false, false,
785 handle_no_split_stack_attribute, false },
786 /* For internal use (marking of builtins and runtime functions) only.
787 The name contains space to prevent its usage in source code. */
788 { "fn spec", 1, 1, false, true, true,
789 handle_fnspec_attribute, false },
790 { "warn_unused", 0, 0, false, false, false,
791 handle_warn_unused_attribute, false },
792 { "returns_nonnull", 0, 0, false, true, true,
793 handle_returns_nonnull_attribute, false },
794 { "omp declare simd", 0, -1, true, false, false,
795 handle_omp_declare_simd_attribute, false },
796 { "cilk simd function", 0, -1, true, false, false,
797 handle_omp_declare_simd_attribute, false },
798 { "omp declare target", 0, 0, true, false, false,
799 handle_omp_declare_target_attribute, false },
800 { "alloc_align", 1, 1, false, true, true,
801 handle_alloc_align_attribute, false },
802 { "assume_aligned", 1, 2, false, true, true,
803 handle_assume_aligned_attribute, false },
804 { "designated_init", 0, 0, false, true, false,
805 handle_designated_init_attribute, false },
806 { "bnd_variable_size", 0, 0, true, false, false,
807 handle_bnd_variable_size_attribute, false },
808 { "bnd_legacy", 0, 0, true, false, false,
809 handle_bnd_legacy, false },
810 { "bnd_instrument", 0, 0, true, false, false,
811 handle_bnd_instrument, false },
812 { NULL, 0, 0, false, false, false, NULL, false }
815 /* Give the specifications for the format attributes, used by C and all
816 descendants.
818 All attributes referencing arguments should be additionally processed
819 in chkp_copy_function_type_adding_bounds for correct instrumentation
820 by Pointer Bounds Checker.
821 Current list of processed format attributes: format, format_arg. */
822 const struct attribute_spec c_common_format_attribute_table[] =
824 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
825 affects_type_identity } */
826 { "format", 3, 3, false, true, true,
827 handle_format_attribute, false },
828 { "format_arg", 1, 1, false, true, true,
829 handle_format_arg_attribute, false },
830 { NULL, 0, 0, false, false, false, NULL, false }
833 /* Return identifier for address space AS. */
835 const char *
836 c_addr_space_name (addr_space_t as)
838 int rid = RID_FIRST_ADDR_SPACE + as;
839 gcc_assert (ridpointers [rid]);
840 return IDENTIFIER_POINTER (ridpointers [rid]);
843 /* Push current bindings for the function name VAR_DECLS. */
845 void
846 start_fname_decls (void)
848 unsigned ix;
849 tree saved = NULL_TREE;
851 for (ix = 0; fname_vars[ix].decl; ix++)
853 tree decl = *fname_vars[ix].decl;
855 if (decl)
857 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
858 saved);
859 *fname_vars[ix].decl = NULL_TREE;
862 if (saved || saved_function_name_decls)
863 /* Normally they'll have been NULL, so only push if we've got a
864 stack, or they are non-NULL. */
865 saved_function_name_decls = tree_cons (saved, NULL_TREE,
866 saved_function_name_decls);
869 /* Finish up the current bindings, adding them into the current function's
870 statement tree. This must be done _before_ finish_stmt_tree is called.
871 If there is no current function, we must be at file scope and no statements
872 are involved. Pop the previous bindings. */
874 void
875 finish_fname_decls (void)
877 unsigned ix;
878 tree stmts = NULL_TREE;
879 tree stack = saved_function_name_decls;
881 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
882 append_to_statement_list (TREE_VALUE (stack), &stmts);
884 if (stmts)
886 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
888 if (TREE_CODE (*bodyp) == BIND_EXPR)
889 bodyp = &BIND_EXPR_BODY (*bodyp);
891 append_to_statement_list_force (*bodyp, &stmts);
892 *bodyp = stmts;
895 for (ix = 0; fname_vars[ix].decl; ix++)
896 *fname_vars[ix].decl = NULL_TREE;
898 if (stack)
900 /* We had saved values, restore them. */
901 tree saved;
903 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
905 tree decl = TREE_PURPOSE (saved);
906 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
908 *fname_vars[ix].decl = decl;
910 stack = TREE_CHAIN (stack);
912 saved_function_name_decls = stack;
915 /* Return the text name of the current function, suitably prettified
916 by PRETTY_P. Return string must be freed by caller. */
918 const char *
919 fname_as_string (int pretty_p)
921 const char *name = "top level";
922 char *namep;
923 int vrb = 2, len;
924 cpp_string cstr = { 0, 0 }, strname;
926 if (!pretty_p)
928 name = "";
929 vrb = 0;
932 if (current_function_decl)
933 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
935 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
937 namep = XNEWVEC (char, len);
938 snprintf (namep, len, "\"%s\"", name);
939 strname.text = (unsigned char *) namep;
940 strname.len = len - 1;
942 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
944 XDELETEVEC (namep);
945 return (const char *) cstr.text;
948 return namep;
951 /* Return the VAR_DECL for a const char array naming the current
952 function. If the VAR_DECL has not yet been created, create it
953 now. RID indicates how it should be formatted and IDENTIFIER_NODE
954 ID is its name (unfortunately C and C++ hold the RID values of
955 keywords in different places, so we can't derive RID from ID in
956 this language independent code. LOC is the location of the
957 function. */
959 tree
960 fname_decl (location_t loc, unsigned int rid, tree id)
962 unsigned ix;
963 tree decl = NULL_TREE;
965 for (ix = 0; fname_vars[ix].decl; ix++)
966 if (fname_vars[ix].rid == rid)
967 break;
969 decl = *fname_vars[ix].decl;
970 if (!decl)
972 /* If a tree is built here, it would normally have the lineno of
973 the current statement. Later this tree will be moved to the
974 beginning of the function and this line number will be wrong.
975 To avoid this problem set the lineno to 0 here; that prevents
976 it from appearing in the RTL. */
977 tree stmts;
978 location_t saved_location = input_location;
979 input_location = UNKNOWN_LOCATION;
981 stmts = push_stmt_list ();
982 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
983 stmts = pop_stmt_list (stmts);
984 if (!IS_EMPTY_STMT (stmts))
985 saved_function_name_decls
986 = tree_cons (decl, stmts, saved_function_name_decls);
987 *fname_vars[ix].decl = decl;
988 input_location = saved_location;
990 if (!ix && !current_function_decl)
991 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
993 return decl;
996 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
998 tree
999 fix_string_type (tree value)
1001 int length = TREE_STRING_LENGTH (value);
1002 int nchars;
1003 tree e_type, i_type, a_type;
1005 /* Compute the number of elements, for the array type. */
1006 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1008 nchars = length;
1009 e_type = char_type_node;
1011 else if (TREE_TYPE (value) == char16_array_type_node)
1013 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1014 e_type = char16_type_node;
1016 else if (TREE_TYPE (value) == char32_array_type_node)
1018 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1019 e_type = char32_type_node;
1021 else
1023 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1024 e_type = wchar_type_node;
1027 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1028 limit in C++98 Annex B is very large (65536) and is not normative,
1029 so we do not diagnose it (warn_overlength_strings is forced off
1030 in c_common_post_options). */
1031 if (warn_overlength_strings)
1033 const int nchars_max = flag_isoc99 ? 4095 : 509;
1034 const int relevant_std = flag_isoc99 ? 99 : 90;
1035 if (nchars - 1 > nchars_max)
1036 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1037 separate the %d from the 'C'. 'ISO' should not be
1038 translated, but it may be moved after 'C%d' in languages
1039 where modifiers follow nouns. */
1040 pedwarn (input_location, OPT_Woverlength_strings,
1041 "string length %qd is greater than the length %qd "
1042 "ISO C%d compilers are required to support",
1043 nchars - 1, nchars_max, relevant_std);
1046 /* Create the array type for the string constant. The ISO C++
1047 standard says that a string literal has type `const char[N]' or
1048 `const wchar_t[N]'. We use the same logic when invoked as a C
1049 front-end with -Wwrite-strings.
1050 ??? We should change the type of an expression depending on the
1051 state of a warning flag. We should just be warning -- see how
1052 this is handled in the C++ front-end for the deprecated implicit
1053 conversion from string literals to `char*' or `wchar_t*'.
1055 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1056 array type being the unqualified version of that type.
1057 Therefore, if we are constructing an array of const char, we must
1058 construct the matching unqualified array type first. The C front
1059 end does not require this, but it does no harm, so we do it
1060 unconditionally. */
1061 i_type = build_index_type (size_int (nchars - 1));
1062 a_type = build_array_type (e_type, i_type);
1063 if (c_dialect_cxx() || warn_write_strings)
1064 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1066 TREE_TYPE (value) = a_type;
1067 TREE_CONSTANT (value) = 1;
1068 TREE_READONLY (value) = 1;
1069 TREE_STATIC (value) = 1;
1070 return value;
1073 /* If DISABLE is true, stop issuing warnings. This is used when
1074 parsing code that we know will not be executed. This function may
1075 be called multiple times, and works as a stack. */
1077 static void
1078 c_disable_warnings (bool disable)
1080 if (disable)
1082 ++c_inhibit_evaluation_warnings;
1083 fold_defer_overflow_warnings ();
1087 /* If ENABLE is true, reenable issuing warnings. */
1089 static void
1090 c_enable_warnings (bool enable)
1092 if (enable)
1094 --c_inhibit_evaluation_warnings;
1095 fold_undefer_and_ignore_overflow_warnings ();
1099 /* Fully fold EXPR, an expression that was not folded (beyond integer
1100 constant expressions and null pointer constants) when being built
1101 up. If IN_INIT, this is in a static initializer and certain
1102 changes are made to the folding done. Clear *MAYBE_CONST if
1103 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1104 expression because it contains an evaluated operator (in C99) or an
1105 operator outside of sizeof returning an integer constant (in C90)
1106 not permitted in constant expressions, or because it contains an
1107 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1108 set to true by callers before calling this function.) Return the
1109 folded expression. Function arguments have already been folded
1110 before calling this function, as have the contents of SAVE_EXPR,
1111 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1112 C_MAYBE_CONST_EXPR. */
1114 tree
1115 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1117 tree ret;
1118 tree eptype = NULL_TREE;
1119 bool dummy = true;
1120 bool maybe_const_itself = true;
1121 location_t loc = EXPR_LOCATION (expr);
1123 /* This function is not relevant to C++ because C++ folds while
1124 parsing, and may need changes to be correct for C++ when C++
1125 stops folding while parsing. */
1126 if (c_dialect_cxx ())
1127 gcc_unreachable ();
1129 if (!maybe_const)
1130 maybe_const = &dummy;
1131 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1133 eptype = TREE_TYPE (expr);
1134 expr = TREE_OPERAND (expr, 0);
1136 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1137 &maybe_const_itself);
1138 if (eptype)
1139 ret = fold_convert_loc (loc, eptype, ret);
1140 *maybe_const &= maybe_const_itself;
1141 return ret;
1144 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1145 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1146 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1147 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1148 both evaluated and unevaluated subexpressions while
1149 *MAYBE_CONST_ITSELF is carried from only evaluated
1150 subexpressions). */
1152 static tree
1153 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1154 bool *maybe_const_itself)
1156 tree ret = expr;
1157 enum tree_code code = TREE_CODE (expr);
1158 enum tree_code_class kind = TREE_CODE_CLASS (code);
1159 location_t loc = EXPR_LOCATION (expr);
1160 tree op0, op1, op2, op3;
1161 tree orig_op0, orig_op1, orig_op2;
1162 bool op0_const = true, op1_const = true, op2_const = true;
1163 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1164 bool nowarning = TREE_NO_WARNING (expr);
1165 bool unused_p;
1167 /* This function is not relevant to C++ because C++ folds while
1168 parsing, and may need changes to be correct for C++ when C++
1169 stops folding while parsing. */
1170 if (c_dialect_cxx ())
1171 gcc_unreachable ();
1173 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1174 anything else not counted as an expression cannot usefully be
1175 folded further at this point. */
1176 if (!IS_EXPR_CODE_CLASS (kind)
1177 || kind == tcc_statement
1178 || code == SAVE_EXPR)
1179 return expr;
1181 /* Operands of variable-length expressions (function calls) have
1182 already been folded, as have __builtin_* function calls, and such
1183 expressions cannot occur in constant expressions. */
1184 if (kind == tcc_vl_exp)
1186 *maybe_const_operands = false;
1187 ret = fold (expr);
1188 goto out;
1191 if (code == C_MAYBE_CONST_EXPR)
1193 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1194 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1195 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1196 *maybe_const_operands = false;
1197 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1198 *maybe_const_itself = false;
1199 if (pre && !in_init)
1200 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1201 else
1202 ret = inner;
1203 goto out;
1206 /* Assignment, increment, decrement, function call and comma
1207 operators, and statement expressions, cannot occur in constant
1208 expressions if evaluated / outside of sizeof. (Function calls
1209 were handled above, though VA_ARG_EXPR is treated like a function
1210 call here, and statement expressions are handled through
1211 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1212 switch (code)
1214 case MODIFY_EXPR:
1215 case PREDECREMENT_EXPR:
1216 case PREINCREMENT_EXPR:
1217 case POSTDECREMENT_EXPR:
1218 case POSTINCREMENT_EXPR:
1219 case COMPOUND_EXPR:
1220 *maybe_const_operands = false;
1221 break;
1223 case VA_ARG_EXPR:
1224 case TARGET_EXPR:
1225 case BIND_EXPR:
1226 case OBJ_TYPE_REF:
1227 *maybe_const_operands = false;
1228 ret = fold (expr);
1229 goto out;
1231 default:
1232 break;
1235 /* Fold individual tree codes as appropriate. */
1236 switch (code)
1238 case COMPOUND_LITERAL_EXPR:
1239 /* Any non-constancy will have been marked in a containing
1240 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1241 goto out;
1243 case COMPONENT_REF:
1244 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1245 op1 = TREE_OPERAND (expr, 1);
1246 op2 = TREE_OPERAND (expr, 2);
1247 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1248 maybe_const_itself);
1249 STRIP_TYPE_NOPS (op0);
1250 if (op0 != orig_op0)
1251 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1252 if (ret != expr)
1254 TREE_READONLY (ret) = TREE_READONLY (expr);
1255 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1257 goto out;
1259 case ARRAY_REF:
1260 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1261 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1262 op2 = TREE_OPERAND (expr, 2);
1263 op3 = TREE_OPERAND (expr, 3);
1264 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1265 maybe_const_itself);
1266 STRIP_TYPE_NOPS (op0);
1267 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1268 maybe_const_itself);
1269 STRIP_TYPE_NOPS (op1);
1270 op1 = decl_constant_value_for_optimization (op1);
1271 if (op0 != orig_op0 || op1 != orig_op1)
1272 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1273 if (ret != expr)
1275 TREE_READONLY (ret) = TREE_READONLY (expr);
1276 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1277 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1279 ret = fold (ret);
1280 goto out;
1282 case COMPOUND_EXPR:
1283 case MODIFY_EXPR:
1284 case PREDECREMENT_EXPR:
1285 case PREINCREMENT_EXPR:
1286 case POSTDECREMENT_EXPR:
1287 case POSTINCREMENT_EXPR:
1288 case PLUS_EXPR:
1289 case MINUS_EXPR:
1290 case MULT_EXPR:
1291 case POINTER_PLUS_EXPR:
1292 case TRUNC_DIV_EXPR:
1293 case CEIL_DIV_EXPR:
1294 case FLOOR_DIV_EXPR:
1295 case TRUNC_MOD_EXPR:
1296 case RDIV_EXPR:
1297 case EXACT_DIV_EXPR:
1298 case LSHIFT_EXPR:
1299 case RSHIFT_EXPR:
1300 case BIT_IOR_EXPR:
1301 case BIT_XOR_EXPR:
1302 case BIT_AND_EXPR:
1303 case LT_EXPR:
1304 case LE_EXPR:
1305 case GT_EXPR:
1306 case GE_EXPR:
1307 case EQ_EXPR:
1308 case NE_EXPR:
1309 case COMPLEX_EXPR:
1310 case TRUTH_AND_EXPR:
1311 case TRUTH_OR_EXPR:
1312 case TRUTH_XOR_EXPR:
1313 case UNORDERED_EXPR:
1314 case ORDERED_EXPR:
1315 case UNLT_EXPR:
1316 case UNLE_EXPR:
1317 case UNGT_EXPR:
1318 case UNGE_EXPR:
1319 case UNEQ_EXPR:
1320 /* Binary operations evaluating both arguments (increment and
1321 decrement are binary internally in GCC). */
1322 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1323 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1324 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1325 maybe_const_itself);
1326 STRIP_TYPE_NOPS (op0);
1327 if (code != MODIFY_EXPR
1328 && code != PREDECREMENT_EXPR
1329 && code != PREINCREMENT_EXPR
1330 && code != POSTDECREMENT_EXPR
1331 && code != POSTINCREMENT_EXPR)
1332 op0 = decl_constant_value_for_optimization (op0);
1333 /* The RHS of a MODIFY_EXPR was fully folded when building that
1334 expression for the sake of conversion warnings. */
1335 if (code != MODIFY_EXPR)
1336 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1337 maybe_const_itself);
1338 STRIP_TYPE_NOPS (op1);
1339 op1 = decl_constant_value_for_optimization (op1);
1340 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1341 ret = in_init
1342 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1343 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1344 else
1345 ret = fold (expr);
1346 if (TREE_OVERFLOW_P (ret)
1347 && !TREE_OVERFLOW_P (op0)
1348 && !TREE_OVERFLOW_P (op1))
1349 overflow_warning (EXPR_LOCATION (expr), ret);
1350 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1351 && TREE_CODE (orig_op1) != INTEGER_CST
1352 && TREE_CODE (op1) == INTEGER_CST
1353 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1354 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1355 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1356 && c_inhibit_evaluation_warnings == 0)
1358 if (tree_int_cst_sgn (op1) < 0)
1359 warning_at (loc, 0, (code == LSHIFT_EXPR
1360 ? G_("left shift count is negative")
1361 : G_("right shift count is negative")));
1362 else if (compare_tree_int (op1,
1363 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1364 >= 0)
1365 warning_at (loc, 0, (code == LSHIFT_EXPR
1366 ? G_("left shift count >= width of type")
1367 : G_("right shift count >= width of type")));
1369 goto out;
1371 case INDIRECT_REF:
1372 case FIX_TRUNC_EXPR:
1373 case FLOAT_EXPR:
1374 CASE_CONVERT:
1375 case ADDR_SPACE_CONVERT_EXPR:
1376 case VIEW_CONVERT_EXPR:
1377 case NON_LVALUE_EXPR:
1378 case NEGATE_EXPR:
1379 case BIT_NOT_EXPR:
1380 case TRUTH_NOT_EXPR:
1381 case ADDR_EXPR:
1382 case CONJ_EXPR:
1383 case REALPART_EXPR:
1384 case IMAGPART_EXPR:
1385 /* Unary operations. */
1386 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1387 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1388 maybe_const_itself);
1389 STRIP_TYPE_NOPS (op0);
1390 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1391 op0 = decl_constant_value_for_optimization (op0);
1392 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1393 not prepared to deal with them if they occur in initializers. */
1394 if (op0 != orig_op0
1395 && code == ADDR_EXPR
1396 && (op1 = get_base_address (op0)) != NULL_TREE
1397 && TREE_CODE (op1) == INDIRECT_REF
1398 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1399 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1400 else if (op0 != orig_op0 || in_init)
1401 ret = in_init
1402 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1403 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1404 else
1405 ret = fold (expr);
1406 if (code == INDIRECT_REF
1407 && ret != expr
1408 && TREE_CODE (ret) == INDIRECT_REF)
1410 TREE_READONLY (ret) = TREE_READONLY (expr);
1411 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1412 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1414 switch (code)
1416 case FIX_TRUNC_EXPR:
1417 case FLOAT_EXPR:
1418 CASE_CONVERT:
1419 /* Don't warn about explicit conversions. We will already
1420 have warned about suspect implicit conversions. */
1421 break;
1423 default:
1424 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1425 overflow_warning (EXPR_LOCATION (expr), ret);
1426 break;
1428 goto out;
1430 case TRUTH_ANDIF_EXPR:
1431 case TRUTH_ORIF_EXPR:
1432 /* Binary operations not necessarily evaluating both
1433 arguments. */
1434 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1435 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1436 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1437 STRIP_TYPE_NOPS (op0);
1439 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1440 ? truthvalue_false_node
1441 : truthvalue_true_node));
1442 c_disable_warnings (unused_p);
1443 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1444 STRIP_TYPE_NOPS (op1);
1445 c_enable_warnings (unused_p);
1447 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1448 ret = in_init
1449 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1450 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1451 else
1452 ret = fold (expr);
1453 *maybe_const_operands &= op0_const;
1454 *maybe_const_itself &= op0_const_self;
1455 if (!(flag_isoc99
1456 && op0_const
1457 && op0_const_self
1458 && (code == TRUTH_ANDIF_EXPR
1459 ? op0 == truthvalue_false_node
1460 : op0 == truthvalue_true_node)))
1461 *maybe_const_operands &= op1_const;
1462 if (!(op0_const
1463 && op0_const_self
1464 && (code == TRUTH_ANDIF_EXPR
1465 ? op0 == truthvalue_false_node
1466 : op0 == truthvalue_true_node)))
1467 *maybe_const_itself &= op1_const_self;
1468 goto out;
1470 case COND_EXPR:
1471 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1472 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1473 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1474 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1476 STRIP_TYPE_NOPS (op0);
1477 c_disable_warnings (op0 == truthvalue_false_node);
1478 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1479 STRIP_TYPE_NOPS (op1);
1480 c_enable_warnings (op0 == truthvalue_false_node);
1482 c_disable_warnings (op0 == truthvalue_true_node);
1483 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1484 STRIP_TYPE_NOPS (op2);
1485 c_enable_warnings (op0 == truthvalue_true_node);
1487 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1488 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1489 else
1490 ret = fold (expr);
1491 *maybe_const_operands &= op0_const;
1492 *maybe_const_itself &= op0_const_self;
1493 if (!(flag_isoc99
1494 && op0_const
1495 && op0_const_self
1496 && op0 == truthvalue_false_node))
1497 *maybe_const_operands &= op1_const;
1498 if (!(op0_const
1499 && op0_const_self
1500 && op0 == truthvalue_false_node))
1501 *maybe_const_itself &= op1_const_self;
1502 if (!(flag_isoc99
1503 && op0_const
1504 && op0_const_self
1505 && op0 == truthvalue_true_node))
1506 *maybe_const_operands &= op2_const;
1507 if (!(op0_const
1508 && op0_const_self
1509 && op0 == truthvalue_true_node))
1510 *maybe_const_itself &= op2_const_self;
1511 goto out;
1513 case EXCESS_PRECISION_EXPR:
1514 /* Each case where an operand with excess precision may be
1515 encountered must remove the EXCESS_PRECISION_EXPR around
1516 inner operands and possibly put one around the whole
1517 expression or possibly convert to the semantic type (which
1518 c_fully_fold does); we cannot tell at this stage which is
1519 appropriate in any particular case. */
1520 gcc_unreachable ();
1522 default:
1523 /* Various codes may appear through folding built-in functions
1524 and their arguments. */
1525 goto out;
1528 out:
1529 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1530 have been done by this point, so remove them again. */
1531 nowarning |= TREE_NO_WARNING (ret);
1532 STRIP_TYPE_NOPS (ret);
1533 if (nowarning && !TREE_NO_WARNING (ret))
1535 if (!CAN_HAVE_LOCATION_P (ret))
1536 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1537 TREE_NO_WARNING (ret) = 1;
1539 if (ret != expr)
1540 protected_set_expr_location (ret, loc);
1541 return ret;
1544 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1545 return EXP. Otherwise, return either EXP or its known constant
1546 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1547 Is the BLKmode test appropriate? */
1549 tree
1550 decl_constant_value_for_optimization (tree exp)
1552 tree ret;
1554 /* This function is only used by C, for c_fully_fold and other
1555 optimization, and may not be correct for C++. */
1556 if (c_dialect_cxx ())
1557 gcc_unreachable ();
1559 if (!optimize
1560 || TREE_CODE (exp) != VAR_DECL
1561 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1562 || DECL_MODE (exp) == BLKmode)
1563 return exp;
1565 ret = decl_constant_value (exp);
1566 /* Avoid unwanted tree sharing between the initializer and current
1567 function's body where the tree can be modified e.g. by the
1568 gimplifier. */
1569 if (ret != exp && TREE_STATIC (exp))
1570 ret = unshare_expr (ret);
1571 return ret;
1574 /* Print a warning if a constant expression had overflow in folding.
1575 Invoke this function on every expression that the language
1576 requires to be a constant expression.
1577 Note the ANSI C standard says it is erroneous for a
1578 constant expression to overflow. */
1580 void
1581 constant_expression_warning (tree value)
1583 if (warn_overflow && pedantic
1584 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1585 || TREE_CODE (value) == FIXED_CST
1586 || TREE_CODE (value) == VECTOR_CST
1587 || TREE_CODE (value) == COMPLEX_CST)
1588 && TREE_OVERFLOW (value))
1589 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1592 /* The same as above but print an unconditional error. */
1593 void
1594 constant_expression_error (tree value)
1596 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1597 || TREE_CODE (value) == FIXED_CST
1598 || TREE_CODE (value) == VECTOR_CST
1599 || TREE_CODE (value) == COMPLEX_CST)
1600 && TREE_OVERFLOW (value))
1601 error ("overflow in constant expression");
1604 /* Print a warning if an expression had overflow in folding and its
1605 operands hadn't.
1607 Invoke this function on every expression that
1608 (1) appears in the source code, and
1609 (2) is a constant expression that overflowed, and
1610 (3) is not already checked by convert_and_check;
1611 however, do not invoke this function on operands of explicit casts
1612 or when the expression is the result of an operator and any operand
1613 already overflowed. */
1615 void
1616 overflow_warning (location_t loc, tree value)
1618 if (c_inhibit_evaluation_warnings != 0)
1619 return;
1621 switch (TREE_CODE (value))
1623 case INTEGER_CST:
1624 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1625 break;
1627 case REAL_CST:
1628 warning_at (loc, OPT_Woverflow,
1629 "floating point overflow in expression");
1630 break;
1632 case FIXED_CST:
1633 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1634 break;
1636 case VECTOR_CST:
1637 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1638 break;
1640 case COMPLEX_CST:
1641 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1642 warning_at (loc, OPT_Woverflow,
1643 "complex integer overflow in expression");
1644 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1645 warning_at (loc, OPT_Woverflow,
1646 "complex floating point overflow in expression");
1647 break;
1649 default:
1650 break;
1654 /* Warn about uses of logical || / && operator in a context where it
1655 is likely that the bitwise equivalent was intended by the
1656 programmer. We have seen an expression in which CODE is a binary
1657 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1658 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1659 void
1660 warn_logical_operator (location_t location, enum tree_code code, tree type,
1661 enum tree_code code_left, tree op_left,
1662 enum tree_code ARG_UNUSED (code_right), tree op_right)
1664 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1665 int in0_p, in1_p, in_p;
1666 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1667 bool strict_overflow_p = false;
1669 if (code != TRUTH_ANDIF_EXPR
1670 && code != TRUTH_AND_EXPR
1671 && code != TRUTH_ORIF_EXPR
1672 && code != TRUTH_OR_EXPR)
1673 return;
1675 /* Warn if &&/|| are being used in a context where it is
1676 likely that the bitwise equivalent was intended by the
1677 programmer. That is, an expression such as op && MASK
1678 where op should not be any boolean expression, nor a
1679 constant, and mask seems to be a non-boolean integer constant. */
1680 if (!truth_value_p (code_left)
1681 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1682 && !CONSTANT_CLASS_P (op_left)
1683 && !TREE_NO_WARNING (op_left)
1684 && TREE_CODE (op_right) == INTEGER_CST
1685 && !integer_zerop (op_right)
1686 && !integer_onep (op_right))
1688 if (or_op)
1689 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1690 " applied to non-boolean constant");
1691 else
1692 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1693 " applied to non-boolean constant");
1694 TREE_NO_WARNING (op_left) = true;
1695 return;
1698 /* We do not warn for constants because they are typical of macro
1699 expansions that test for features. */
1700 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1701 return;
1703 /* This warning only makes sense with logical operands. */
1704 if (!(truth_value_p (TREE_CODE (op_left))
1705 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1706 || !(truth_value_p (TREE_CODE (op_right))
1707 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1708 return;
1710 /* The range computations only work with scalars. */
1711 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1712 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1713 return;
1715 /* We first test whether either side separately is trivially true
1716 (with OR) or trivially false (with AND). If so, do not warn.
1717 This is a common idiom for testing ranges of data types in
1718 portable code. */
1719 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1720 if (!lhs)
1721 return;
1722 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1723 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1725 /* If this is an OR operation, invert both sides; now, the result
1726 should be always false to get a warning. */
1727 if (or_op)
1728 in0_p = !in0_p;
1730 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1731 if (tem && integer_zerop (tem))
1732 return;
1734 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1735 if (!rhs)
1736 return;
1737 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1738 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1740 /* If this is an OR operation, invert both sides; now, the result
1741 should be always false to get a warning. */
1742 if (or_op)
1743 in1_p = !in1_p;
1745 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1746 if (tem && integer_zerop (tem))
1747 return;
1749 /* If both expressions have the same operand, if we can merge the
1750 ranges, and if the range test is always false, then warn. */
1751 if (operand_equal_p (lhs, rhs, 0)
1752 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1753 in1_p, low1, high1)
1754 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1755 type, lhs, in_p, low, high))
1756 && integer_zerop (tem))
1758 if (or_op)
1759 warning_at (location, OPT_Wlogical_op,
1760 "logical %<or%> "
1761 "of collectively exhaustive tests is always true");
1762 else
1763 warning_at (location, OPT_Wlogical_op,
1764 "logical %<and%> "
1765 "of mutually exclusive tests is always false");
1769 /* Warn about logical not used on the left hand side operand of a comparison.
1770 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1771 Do not warn if RHS is of a boolean type. */
1773 void
1774 warn_logical_not_parentheses (location_t location, enum tree_code code,
1775 tree rhs)
1777 if (TREE_CODE_CLASS (code) != tcc_comparison
1778 || TREE_TYPE (rhs) == NULL_TREE
1779 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1780 return;
1782 warning_at (location, OPT_Wlogical_not_parentheses,
1783 "logical not is only applied to the left hand side of "
1784 "comparison");
1787 /* Warn if EXP contains any computations whose results are not used.
1788 Return true if a warning is printed; false otherwise. LOCUS is the
1789 (potential) location of the expression. */
1791 bool
1792 warn_if_unused_value (const_tree exp, location_t locus)
1794 restart:
1795 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1796 return false;
1798 /* Don't warn about void constructs. This includes casting to void,
1799 void function calls, and statement expressions with a final cast
1800 to void. */
1801 if (VOID_TYPE_P (TREE_TYPE (exp)))
1802 return false;
1804 if (EXPR_HAS_LOCATION (exp))
1805 locus = EXPR_LOCATION (exp);
1807 switch (TREE_CODE (exp))
1809 case PREINCREMENT_EXPR:
1810 case POSTINCREMENT_EXPR:
1811 case PREDECREMENT_EXPR:
1812 case POSTDECREMENT_EXPR:
1813 case MODIFY_EXPR:
1814 case INIT_EXPR:
1815 case TARGET_EXPR:
1816 case CALL_EXPR:
1817 case TRY_CATCH_EXPR:
1818 case WITH_CLEANUP_EXPR:
1819 case EXIT_EXPR:
1820 case VA_ARG_EXPR:
1821 return false;
1823 case BIND_EXPR:
1824 /* For a binding, warn if no side effect within it. */
1825 exp = BIND_EXPR_BODY (exp);
1826 goto restart;
1828 case SAVE_EXPR:
1829 case NON_LVALUE_EXPR:
1830 case NOP_EXPR:
1831 exp = TREE_OPERAND (exp, 0);
1832 goto restart;
1834 case TRUTH_ORIF_EXPR:
1835 case TRUTH_ANDIF_EXPR:
1836 /* In && or ||, warn if 2nd operand has no side effect. */
1837 exp = TREE_OPERAND (exp, 1);
1838 goto restart;
1840 case COMPOUND_EXPR:
1841 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1842 return true;
1843 /* Let people do `(foo (), 0)' without a warning. */
1844 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1845 return false;
1846 exp = TREE_OPERAND (exp, 1);
1847 goto restart;
1849 case COND_EXPR:
1850 /* If this is an expression with side effects, don't warn; this
1851 case commonly appears in macro expansions. */
1852 if (TREE_SIDE_EFFECTS (exp))
1853 return false;
1854 goto warn;
1856 case INDIRECT_REF:
1857 /* Don't warn about automatic dereferencing of references, since
1858 the user cannot control it. */
1859 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1861 exp = TREE_OPERAND (exp, 0);
1862 goto restart;
1864 /* Fall through. */
1866 default:
1867 /* Referencing a volatile value is a side effect, so don't warn. */
1868 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1869 && TREE_THIS_VOLATILE (exp))
1870 return false;
1872 /* If this is an expression which has no operands, there is no value
1873 to be unused. There are no such language-independent codes,
1874 but front ends may define such. */
1875 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1876 return false;
1878 warn:
1879 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1884 /* Print a warning about casts that might indicate violation
1885 of strict aliasing rules if -Wstrict-aliasing is used and
1886 strict aliasing mode is in effect. OTYPE is the original
1887 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1889 bool
1890 strict_aliasing_warning (tree otype, tree type, tree expr)
1892 /* Strip pointer conversion chains and get to the correct original type. */
1893 STRIP_NOPS (expr);
1894 otype = TREE_TYPE (expr);
1896 if (!(flag_strict_aliasing
1897 && POINTER_TYPE_P (type)
1898 && POINTER_TYPE_P (otype)
1899 && !VOID_TYPE_P (TREE_TYPE (type)))
1900 /* If the type we are casting to is a ref-all pointer
1901 dereferencing it is always valid. */
1902 || TYPE_REF_CAN_ALIAS_ALL (type))
1903 return false;
1905 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1906 && (DECL_P (TREE_OPERAND (expr, 0))
1907 || handled_component_p (TREE_OPERAND (expr, 0))))
1909 /* Casting the address of an object to non void pointer. Warn
1910 if the cast breaks type based aliasing. */
1911 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1913 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1914 "might break strict-aliasing rules");
1915 return true;
1917 else
1919 /* warn_strict_aliasing >= 3. This includes the default (3).
1920 Only warn if the cast is dereferenced immediately. */
1921 alias_set_type set1 =
1922 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1923 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1925 if (set1 != set2 && set2 != 0
1926 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1928 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1929 "pointer will break strict-aliasing rules");
1930 return true;
1932 else if (warn_strict_aliasing == 2
1933 && !alias_sets_must_conflict_p (set1, set2))
1935 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1936 "pointer might break strict-aliasing rules");
1937 return true;
1941 else
1942 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1944 /* At this level, warn for any conversions, even if an address is
1945 not taken in the same statement. This will likely produce many
1946 false positives, but could be useful to pinpoint problems that
1947 are not revealed at higher levels. */
1948 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1949 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1950 if (!COMPLETE_TYPE_P (type)
1951 || !alias_sets_must_conflict_p (set1, set2))
1953 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1954 "pointer might break strict-aliasing rules");
1955 return true;
1959 return false;
1962 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1963 sizeof as last operand of certain builtins. */
1965 void
1966 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1967 vec<tree, va_gc> *params, tree *sizeof_arg,
1968 bool (*comp_types) (tree, tree))
1970 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1971 bool strop = false, cmp = false;
1972 unsigned int idx = ~0;
1973 location_t loc;
1975 if (TREE_CODE (callee) != FUNCTION_DECL
1976 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1977 || vec_safe_length (params) <= 1)
1978 return;
1980 switch (DECL_FUNCTION_CODE (callee))
1982 case BUILT_IN_STRNCMP:
1983 case BUILT_IN_STRNCASECMP:
1984 cmp = true;
1985 /* FALLTHRU */
1986 case BUILT_IN_STRNCPY:
1987 case BUILT_IN_STRNCPY_CHK:
1988 case BUILT_IN_STRNCAT:
1989 case BUILT_IN_STRNCAT_CHK:
1990 case BUILT_IN_STPNCPY:
1991 case BUILT_IN_STPNCPY_CHK:
1992 strop = true;
1993 /* FALLTHRU */
1994 case BUILT_IN_MEMCPY:
1995 case BUILT_IN_MEMCPY_CHK:
1996 case BUILT_IN_MEMMOVE:
1997 case BUILT_IN_MEMMOVE_CHK:
1998 if (params->length () < 3)
1999 return;
2000 src = (*params)[1];
2001 dest = (*params)[0];
2002 idx = 2;
2003 break;
2004 case BUILT_IN_BCOPY:
2005 if (params->length () < 3)
2006 return;
2007 src = (*params)[0];
2008 dest = (*params)[1];
2009 idx = 2;
2010 break;
2011 case BUILT_IN_MEMCMP:
2012 case BUILT_IN_BCMP:
2013 if (params->length () < 3)
2014 return;
2015 src = (*params)[1];
2016 dest = (*params)[0];
2017 idx = 2;
2018 cmp = true;
2019 break;
2020 case BUILT_IN_MEMSET:
2021 case BUILT_IN_MEMSET_CHK:
2022 if (params->length () < 3)
2023 return;
2024 dest = (*params)[0];
2025 idx = 2;
2026 break;
2027 case BUILT_IN_BZERO:
2028 dest = (*params)[0];
2029 idx = 1;
2030 break;
2031 case BUILT_IN_STRNDUP:
2032 src = (*params)[0];
2033 strop = true;
2034 idx = 1;
2035 break;
2036 case BUILT_IN_MEMCHR:
2037 if (params->length () < 3)
2038 return;
2039 src = (*params)[0];
2040 idx = 2;
2041 break;
2042 case BUILT_IN_SNPRINTF:
2043 case BUILT_IN_SNPRINTF_CHK:
2044 case BUILT_IN_VSNPRINTF:
2045 case BUILT_IN_VSNPRINTF_CHK:
2046 dest = (*params)[0];
2047 idx = 1;
2048 strop = true;
2049 break;
2050 default:
2051 break;
2054 if (idx >= 3)
2055 return;
2057 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2058 return;
2060 type = TYPE_P (sizeof_arg[idx])
2061 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2062 if (!POINTER_TYPE_P (type))
2063 return;
2065 if (dest
2066 && (tem = tree_strip_nop_conversions (dest))
2067 && POINTER_TYPE_P (TREE_TYPE (tem))
2068 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2069 return;
2071 if (src
2072 && (tem = tree_strip_nop_conversions (src))
2073 && POINTER_TYPE_P (TREE_TYPE (tem))
2074 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2075 return;
2077 loc = sizeof_arg_loc[idx];
2079 if (dest && !cmp)
2081 if (!TYPE_P (sizeof_arg[idx])
2082 && operand_equal_p (dest, sizeof_arg[idx], 0)
2083 && comp_types (TREE_TYPE (dest), type))
2085 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !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 "remove the addressof?", callee);
2090 else if ((TYPE_PRECISION (TREE_TYPE (type))
2091 == TYPE_PRECISION (char_type_node))
2092 || strop)
2093 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2094 "argument to %<sizeof%> in %qD call is the same "
2095 "expression as the destination; did you mean to "
2096 "provide an explicit length?", callee);
2097 else
2098 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2099 "argument to %<sizeof%> in %qD call is the same "
2100 "expression as the destination; did you mean to "
2101 "dereference it?", callee);
2102 return;
2105 if (POINTER_TYPE_P (TREE_TYPE (dest))
2106 && !strop
2107 && comp_types (TREE_TYPE (dest), type)
2108 && !VOID_TYPE_P (TREE_TYPE (type)))
2110 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2111 "argument to %<sizeof%> in %qD call is the same "
2112 "pointer type %qT as the destination; expected %qT "
2113 "or an explicit length", callee, TREE_TYPE (dest),
2114 TREE_TYPE (TREE_TYPE (dest)));
2115 return;
2119 if (src && !cmp)
2121 if (!TYPE_P (sizeof_arg[idx])
2122 && operand_equal_p (src, sizeof_arg[idx], 0)
2123 && comp_types (TREE_TYPE (src), type))
2125 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !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 "remove the addressof?", callee);
2130 else if ((TYPE_PRECISION (TREE_TYPE (type))
2131 == TYPE_PRECISION (char_type_node))
2132 || strop)
2133 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2134 "argument to %<sizeof%> in %qD call is the same "
2135 "expression as the source; did you mean to "
2136 "provide an explicit length?", callee);
2137 else
2138 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2139 "argument to %<sizeof%> in %qD call is the same "
2140 "expression as the source; did you mean to "
2141 "dereference it?", callee);
2142 return;
2145 if (POINTER_TYPE_P (TREE_TYPE (src))
2146 && !strop
2147 && comp_types (TREE_TYPE (src), type)
2148 && !VOID_TYPE_P (TREE_TYPE (type)))
2150 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2151 "argument to %<sizeof%> in %qD call is the same "
2152 "pointer type %qT as the source; expected %qT "
2153 "or an explicit length", callee, TREE_TYPE (src),
2154 TREE_TYPE (TREE_TYPE (src)));
2155 return;
2159 if (dest)
2161 if (!TYPE_P (sizeof_arg[idx])
2162 && operand_equal_p (dest, sizeof_arg[idx], 0)
2163 && comp_types (TREE_TYPE (dest), type))
2165 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !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 "remove the addressof?", callee);
2170 else if ((TYPE_PRECISION (TREE_TYPE (type))
2171 == TYPE_PRECISION (char_type_node))
2172 || strop)
2173 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2174 "argument to %<sizeof%> in %qD call is the same "
2175 "expression as the first source; did you mean to "
2176 "provide an explicit length?", callee);
2177 else
2178 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2179 "argument to %<sizeof%> in %qD call is the same "
2180 "expression as the first source; did you mean to "
2181 "dereference it?", callee);
2182 return;
2185 if (POINTER_TYPE_P (TREE_TYPE (dest))
2186 && !strop
2187 && comp_types (TREE_TYPE (dest), type)
2188 && !VOID_TYPE_P (TREE_TYPE (type)))
2190 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2191 "argument to %<sizeof%> in %qD call is the same "
2192 "pointer type %qT as the first source; expected %qT "
2193 "or an explicit length", callee, TREE_TYPE (dest),
2194 TREE_TYPE (TREE_TYPE (dest)));
2195 return;
2199 if (src)
2201 if (!TYPE_P (sizeof_arg[idx])
2202 && operand_equal_p (src, sizeof_arg[idx], 0)
2203 && comp_types (TREE_TYPE (src), type))
2205 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !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 "remove the addressof?", callee);
2210 else if ((TYPE_PRECISION (TREE_TYPE (type))
2211 == TYPE_PRECISION (char_type_node))
2212 || strop)
2213 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2214 "argument to %<sizeof%> in %qD call is the same "
2215 "expression as the second source; did you mean to "
2216 "provide an explicit length?", callee);
2217 else
2218 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2219 "argument to %<sizeof%> in %qD call is the same "
2220 "expression as the second source; did you mean to "
2221 "dereference it?", callee);
2222 return;
2225 if (POINTER_TYPE_P (TREE_TYPE (src))
2226 && !strop
2227 && comp_types (TREE_TYPE (src), type)
2228 && !VOID_TYPE_P (TREE_TYPE (type)))
2230 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2231 "argument to %<sizeof%> in %qD call is the same "
2232 "pointer type %qT as the second source; expected %qT "
2233 "or an explicit length", callee, TREE_TYPE (src),
2234 TREE_TYPE (TREE_TYPE (src)));
2235 return;
2241 /* Warn for unlikely, improbable, or stupid DECL declarations
2242 of `main'. */
2244 void
2245 check_main_parameter_types (tree decl)
2247 function_args_iterator iter;
2248 tree type;
2249 int argct = 0;
2251 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2253 /* XXX void_type_node belies the abstraction. */
2254 if (type == void_type_node || type == error_mark_node )
2255 break;
2257 tree t = type;
2258 if (TYPE_ATOMIC (t))
2259 pedwarn (input_location, OPT_Wmain,
2260 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2261 type, decl);
2262 while (POINTER_TYPE_P (t))
2264 t = TREE_TYPE (t);
2265 if (TYPE_ATOMIC (t))
2266 pedwarn (input_location, OPT_Wmain,
2267 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2268 type, decl);
2271 ++argct;
2272 switch (argct)
2274 case 1:
2275 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2276 pedwarn (input_location, OPT_Wmain,
2277 "first argument of %q+D should be %<int%>", decl);
2278 break;
2280 case 2:
2281 if (TREE_CODE (type) != POINTER_TYPE
2282 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2283 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2284 != char_type_node))
2285 pedwarn (input_location, OPT_Wmain,
2286 "second argument of %q+D should be %<char **%>", decl);
2287 break;
2289 case 3:
2290 if (TREE_CODE (type) != POINTER_TYPE
2291 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2292 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2293 != char_type_node))
2294 pedwarn (input_location, OPT_Wmain,
2295 "third argument of %q+D should probably be "
2296 "%<char **%>", decl);
2297 break;
2301 /* It is intentional that this message does not mention the third
2302 argument because it's only mentioned in an appendix of the
2303 standard. */
2304 if (argct > 0 && (argct < 2 || argct > 3))
2305 pedwarn (input_location, OPT_Wmain,
2306 "%q+D takes only zero or two arguments", decl);
2308 if (stdarg_p (TREE_TYPE (decl)))
2309 pedwarn (input_location, OPT_Wmain,
2310 "%q+D declared as variadic function", decl);
2313 /* vector_targets_convertible_p is used for vector pointer types. The
2314 callers perform various checks that the qualifiers are satisfactory,
2315 while OTOH vector_targets_convertible_p ignores the number of elements
2316 in the vectors. That's fine with vector pointers as we can consider,
2317 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2318 and that does not require and conversion of the pointer values.
2319 In contrast, vector_types_convertible_p and
2320 vector_types_compatible_elements_p are used for vector value types. */
2321 /* True if pointers to distinct types T1 and T2 can be converted to
2322 each other without an explicit cast. Only returns true for opaque
2323 vector types. */
2324 bool
2325 vector_targets_convertible_p (const_tree t1, const_tree t2)
2327 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2328 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2329 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2330 return true;
2332 return false;
2335 /* vector_types_convertible_p is used for vector value types.
2336 It could in principle call vector_targets_convertible_p as a subroutine,
2337 but then the check for vector type would be duplicated with its callers,
2338 and also the purpose of vector_targets_convertible_p would become
2339 muddled.
2340 Where vector_types_convertible_p returns true, a conversion might still be
2341 needed to make the types match.
2342 In contrast, vector_targets_convertible_p is used for vector pointer
2343 values, and vector_types_compatible_elements_p is used specifically
2344 in the context for binary operators, as a check if use is possible without
2345 conversion. */
2346 /* True if vector types T1 and T2 can be converted to each other
2347 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2348 can only be converted with -flax-vector-conversions yet that is not
2349 in effect, emit a note telling the user about that option if such
2350 a note has not previously been emitted. */
2351 bool
2352 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2354 static bool emitted_lax_note = false;
2355 bool convertible_lax;
2357 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2358 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2359 return true;
2361 convertible_lax =
2362 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2363 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2364 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2365 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2366 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2368 if (!convertible_lax || flag_lax_vector_conversions)
2369 return convertible_lax;
2371 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2372 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2373 return true;
2375 if (emit_lax_note && !emitted_lax_note)
2377 emitted_lax_note = true;
2378 inform (input_location, "use -flax-vector-conversions to permit "
2379 "conversions between vectors with differing "
2380 "element types or numbers of subparts");
2383 return false;
2386 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2387 and have vector types, V0 has the same type as V1, and the number of
2388 elements of V0, V1, MASK is the same.
2390 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2391 called with two arguments. In this case implementation passes the
2392 first argument twice in order to share the same tree code. This fact
2393 could enable the mask-values being twice the vector length. This is
2394 an implementation accident and this semantics is not guaranteed to
2395 the user. */
2396 tree
2397 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2398 bool complain)
2400 tree ret;
2401 bool wrap = true;
2402 bool maybe_const = false;
2403 bool two_arguments = false;
2405 if (v1 == NULL_TREE)
2407 two_arguments = true;
2408 v1 = v0;
2411 if (v0 == error_mark_node || v1 == error_mark_node
2412 || mask == error_mark_node)
2413 return error_mark_node;
2415 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2416 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2418 if (complain)
2419 error_at (loc, "__builtin_shuffle last argument must "
2420 "be an integer vector");
2421 return error_mark_node;
2424 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2425 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2427 if (complain)
2428 error_at (loc, "__builtin_shuffle arguments must be vectors");
2429 return error_mark_node;
2432 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2434 if (complain)
2435 error_at (loc, "__builtin_shuffle argument vectors must be of "
2436 "the same type");
2437 return error_mark_node;
2440 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2441 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2442 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2443 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2445 if (complain)
2446 error_at (loc, "__builtin_shuffle number of elements of the "
2447 "argument vector(s) and the mask vector should "
2448 "be the same");
2449 return error_mark_node;
2452 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2453 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2455 if (complain)
2456 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2457 "must have the same size as inner type of the mask");
2458 return error_mark_node;
2461 if (!c_dialect_cxx ())
2463 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2464 v0 = c_fully_fold (v0, false, &maybe_const);
2465 wrap &= maybe_const;
2467 if (two_arguments)
2468 v1 = v0 = save_expr (v0);
2469 else
2471 v1 = c_fully_fold (v1, false, &maybe_const);
2472 wrap &= maybe_const;
2475 mask = c_fully_fold (mask, false, &maybe_const);
2476 wrap &= maybe_const;
2478 else if (two_arguments)
2479 v1 = v0 = save_expr (v0);
2481 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2483 if (!c_dialect_cxx () && !wrap)
2484 ret = c_wrap_maybe_const (ret, true);
2486 return ret;
2489 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2490 to integral type. */
2492 static tree
2493 c_common_get_narrower (tree op, int *unsignedp_ptr)
2495 op = get_narrower (op, unsignedp_ptr);
2497 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2498 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2500 /* C++0x scoped enumerations don't implicitly convert to integral
2501 type; if we stripped an explicit conversion to a larger type we
2502 need to replace it so common_type will still work. */
2503 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2504 TYPE_UNSIGNED (TREE_TYPE (op)));
2505 op = fold_convert (type, op);
2507 return op;
2510 /* This is a helper function of build_binary_op.
2512 For certain operations if both args were extended from the same
2513 smaller type, do the arithmetic in that type and then extend.
2515 BITWISE indicates a bitwise operation.
2516 For them, this optimization is safe only if
2517 both args are zero-extended or both are sign-extended.
2518 Otherwise, we might change the result.
2519 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2520 but calculated in (unsigned short) it would be (unsigned short)-1.
2522 tree
2523 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2525 int unsigned0, unsigned1;
2526 tree arg0, arg1;
2527 int uns;
2528 tree type;
2530 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2531 excessive narrowing when we call get_narrower below. For
2532 example, suppose that OP0 is of unsigned int extended
2533 from signed char and that RESULT_TYPE is long long int.
2534 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2535 like
2537 (long long int) (unsigned int) signed_char
2539 which get_narrower would narrow down to
2541 (unsigned int) signed char
2543 If we do not cast OP0 first, get_narrower would return
2544 signed_char, which is inconsistent with the case of the
2545 explicit cast. */
2546 op0 = convert (result_type, op0);
2547 op1 = convert (result_type, op1);
2549 arg0 = c_common_get_narrower (op0, &unsigned0);
2550 arg1 = c_common_get_narrower (op1, &unsigned1);
2552 /* UNS is 1 if the operation to be done is an unsigned one. */
2553 uns = TYPE_UNSIGNED (result_type);
2555 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2556 but it *requires* conversion to FINAL_TYPE. */
2558 if ((TYPE_PRECISION (TREE_TYPE (op0))
2559 == TYPE_PRECISION (TREE_TYPE (arg0)))
2560 && TREE_TYPE (op0) != result_type)
2561 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2562 if ((TYPE_PRECISION (TREE_TYPE (op1))
2563 == TYPE_PRECISION (TREE_TYPE (arg1)))
2564 && TREE_TYPE (op1) != result_type)
2565 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2567 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2569 /* For bitwise operations, signedness of nominal type
2570 does not matter. Consider only how operands were extended. */
2571 if (bitwise)
2572 uns = unsigned0;
2574 /* Note that in all three cases below we refrain from optimizing
2575 an unsigned operation on sign-extended args.
2576 That would not be valid. */
2578 /* Both args variable: if both extended in same way
2579 from same width, do it in that width.
2580 Do it unsigned if args were zero-extended. */
2581 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2582 < TYPE_PRECISION (result_type))
2583 && (TYPE_PRECISION (TREE_TYPE (arg1))
2584 == TYPE_PRECISION (TREE_TYPE (arg0)))
2585 && unsigned0 == unsigned1
2586 && (unsigned0 || !uns))
2587 return c_common_signed_or_unsigned_type
2588 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2590 else if (TREE_CODE (arg0) == INTEGER_CST
2591 && (unsigned1 || !uns)
2592 && (TYPE_PRECISION (TREE_TYPE (arg1))
2593 < TYPE_PRECISION (result_type))
2594 && (type
2595 = c_common_signed_or_unsigned_type (unsigned1,
2596 TREE_TYPE (arg1)))
2597 && !POINTER_TYPE_P (type)
2598 && int_fits_type_p (arg0, type))
2599 return type;
2601 else if (TREE_CODE (arg1) == INTEGER_CST
2602 && (unsigned0 || !uns)
2603 && (TYPE_PRECISION (TREE_TYPE (arg0))
2604 < TYPE_PRECISION (result_type))
2605 && (type
2606 = c_common_signed_or_unsigned_type (unsigned0,
2607 TREE_TYPE (arg0)))
2608 && !POINTER_TYPE_P (type)
2609 && int_fits_type_p (arg1, type))
2610 return type;
2612 return result_type;
2615 /* Checks if expression EXPR of real/integer type cannot be converted
2616 to the real/integer type TYPE. Function returns non-zero when:
2617 * EXPR is a constant which cannot be exactly converted to TYPE.
2618 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2619 for EXPR type and TYPE being both integers or both real.
2620 * EXPR is not a constant of real type and TYPE is an integer.
2621 * EXPR is not a constant of integer type which cannot be
2622 exactly converted to real type.
2623 Function allows conversions between types of different signedness and
2624 can return SAFE_CONVERSION (zero) in that case. Function can produce
2625 signedness warnings if PRODUCE_WARNS is true. */
2627 enum conversion_safety
2628 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2630 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2631 tree expr_type = TREE_TYPE (expr);
2632 loc = expansion_point_location_if_in_system_header (loc);
2634 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2636 /* Warn for real constant that is not an exact integer converted
2637 to integer type. */
2638 if (TREE_CODE (expr_type) == REAL_TYPE
2639 && TREE_CODE (type) == INTEGER_TYPE)
2641 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2642 give_warning = UNSAFE_REAL;
2644 /* Warn for an integer constant that does not fit into integer type. */
2645 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2646 && TREE_CODE (type) == INTEGER_TYPE
2647 && !int_fits_type_p (expr, type))
2649 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2650 && tree_int_cst_sgn (expr) < 0)
2652 if (produce_warns)
2653 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2654 " implicitly converted to unsigned type");
2656 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2658 if (produce_warns)
2659 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2660 " constant value to negative integer");
2662 else
2663 give_warning = UNSAFE_OTHER;
2665 else if (TREE_CODE (type) == REAL_TYPE)
2667 /* Warn for an integer constant that does not fit into real type. */
2668 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2670 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2671 if (!exact_real_truncate (TYPE_MODE (type), &a))
2672 give_warning = UNSAFE_REAL;
2674 /* Warn for a real constant that does not fit into a smaller
2675 real type. */
2676 else if (TREE_CODE (expr_type) == REAL_TYPE
2677 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2679 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2680 if (!exact_real_truncate (TYPE_MODE (type), &a))
2681 give_warning = UNSAFE_REAL;
2685 else
2687 /* Warn for real types converted to integer types. */
2688 if (TREE_CODE (expr_type) == REAL_TYPE
2689 && TREE_CODE (type) == INTEGER_TYPE)
2690 give_warning = UNSAFE_REAL;
2692 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2693 && TREE_CODE (type) == INTEGER_TYPE)
2695 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2696 expr = get_unwidened (expr, 0);
2697 expr_type = TREE_TYPE (expr);
2699 /* Don't warn for short y; short x = ((int)y & 0xff); */
2700 if (TREE_CODE (expr) == BIT_AND_EXPR
2701 || TREE_CODE (expr) == BIT_IOR_EXPR
2702 || TREE_CODE (expr) == BIT_XOR_EXPR)
2704 /* If both args were extended from a shortest type,
2705 use that type if that is safe. */
2706 expr_type = shorten_binary_op (expr_type,
2707 TREE_OPERAND (expr, 0),
2708 TREE_OPERAND (expr, 1),
2709 /* bitwise */1);
2711 if (TREE_CODE (expr) == BIT_AND_EXPR)
2713 tree op0 = TREE_OPERAND (expr, 0);
2714 tree op1 = TREE_OPERAND (expr, 1);
2715 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2716 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2718 /* If one of the operands is a non-negative constant
2719 that fits in the target type, then the type of the
2720 other operand does not matter. */
2721 if ((TREE_CODE (op0) == INTEGER_CST
2722 && int_fits_type_p (op0, c_common_signed_type (type))
2723 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2724 || (TREE_CODE (op1) == INTEGER_CST
2725 && int_fits_type_p (op1, c_common_signed_type (type))
2726 && int_fits_type_p (op1,
2727 c_common_unsigned_type (type))))
2728 return SAFE_CONVERSION;
2729 /* If constant is unsigned and fits in the target
2730 type, then the result will also fit. */
2731 else if ((TREE_CODE (op0) == INTEGER_CST
2732 && unsigned0
2733 && int_fits_type_p (op0, type))
2734 || (TREE_CODE (op1) == INTEGER_CST
2735 && unsigned1
2736 && int_fits_type_p (op1, type)))
2737 return SAFE_CONVERSION;
2740 /* Warn for integer types converted to smaller integer types. */
2741 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2742 give_warning = UNSAFE_OTHER;
2744 /* When they are the same width but different signedness,
2745 then the value may change. */
2746 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2747 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2748 /* Even when converted to a bigger type, if the type is
2749 unsigned but expr is signed, then negative values
2750 will be changed. */
2751 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2752 && produce_warns)
2753 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2754 "may change the sign of the result",
2755 type, expr_type);
2758 /* Warn for integer types converted to real types if and only if
2759 all the range of values of the integer type cannot be
2760 represented by the real type. */
2761 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2762 && TREE_CODE (type) == REAL_TYPE)
2764 tree type_low_bound, type_high_bound;
2765 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2767 /* Don't warn about char y = 0xff; float x = (int) y; */
2768 expr = get_unwidened (expr, 0);
2769 expr_type = TREE_TYPE (expr);
2771 type_low_bound = TYPE_MIN_VALUE (expr_type);
2772 type_high_bound = TYPE_MAX_VALUE (expr_type);
2773 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2774 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2776 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2777 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2778 give_warning = UNSAFE_OTHER;
2781 /* Warn for real types converted to smaller real types. */
2782 else if (TREE_CODE (expr_type) == REAL_TYPE
2783 && TREE_CODE (type) == REAL_TYPE
2784 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2785 give_warning = UNSAFE_REAL;
2788 return give_warning;
2791 /* Warns if the conversion of EXPR to TYPE may alter a value.
2792 This is a helper function for warnings_for_convert_and_check. */
2794 static void
2795 conversion_warning (location_t loc, tree type, tree expr)
2797 tree expr_type = TREE_TYPE (expr);
2798 enum conversion_safety conversion_kind;
2800 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2801 return;
2803 /* This may happen, because for LHS op= RHS we preevaluate
2804 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2805 means we could no longer see the code of the EXPR. */
2806 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2807 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2808 if (TREE_CODE (expr) == SAVE_EXPR)
2809 expr = TREE_OPERAND (expr, 0);
2811 switch (TREE_CODE (expr))
2813 case EQ_EXPR:
2814 case NE_EXPR:
2815 case LE_EXPR:
2816 case GE_EXPR:
2817 case LT_EXPR:
2818 case GT_EXPR:
2819 case TRUTH_ANDIF_EXPR:
2820 case TRUTH_ORIF_EXPR:
2821 case TRUTH_AND_EXPR:
2822 case TRUTH_OR_EXPR:
2823 case TRUTH_XOR_EXPR:
2824 case TRUTH_NOT_EXPR:
2825 /* Conversion from boolean to a signed:1 bit-field (which only
2826 can hold the values 0 and -1) doesn't lose information - but
2827 it does change the value. */
2828 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2829 warning_at (loc, OPT_Wconversion,
2830 "conversion to %qT from boolean expression", type);
2831 return;
2833 case REAL_CST:
2834 case INTEGER_CST:
2835 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2836 if (conversion_kind == UNSAFE_REAL)
2837 warning_at (loc, OPT_Wfloat_conversion,
2838 "conversion to %qT alters %qT constant value",
2839 type, expr_type);
2840 else if (conversion_kind)
2841 warning_at (loc, OPT_Wconversion,
2842 "conversion to %qT alters %qT constant value",
2843 type, expr_type);
2844 return;
2846 case COND_EXPR:
2848 /* In case of COND_EXPR, we do not care about the type of
2849 COND_EXPR, only about the conversion of each operand. */
2850 tree op1 = TREE_OPERAND (expr, 1);
2851 tree op2 = TREE_OPERAND (expr, 2);
2853 conversion_warning (loc, type, op1);
2854 conversion_warning (loc, type, op2);
2855 return;
2858 default: /* 'expr' is not a constant. */
2859 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2860 if (conversion_kind == UNSAFE_REAL)
2861 warning_at (loc, OPT_Wfloat_conversion,
2862 "conversion to %qT from %qT may alter its value",
2863 type, expr_type);
2864 else if (conversion_kind)
2865 warning_at (loc, OPT_Wconversion,
2866 "conversion to %qT from %qT may alter its value",
2867 type, expr_type);
2871 /* Produce warnings after a conversion. RESULT is the result of
2872 converting EXPR to TYPE. This is a helper function for
2873 convert_and_check and cp_convert_and_check. */
2875 void
2876 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2877 tree result)
2879 loc = expansion_point_location_if_in_system_header (loc);
2881 if (TREE_CODE (expr) == INTEGER_CST
2882 && (TREE_CODE (type) == INTEGER_TYPE
2883 || TREE_CODE (type) == ENUMERAL_TYPE)
2884 && !int_fits_type_p (expr, type))
2886 /* Do not diagnose overflow in a constant expression merely
2887 because a conversion overflowed. */
2888 if (TREE_OVERFLOW (result))
2889 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2891 if (TYPE_UNSIGNED (type))
2893 /* This detects cases like converting -129 or 256 to
2894 unsigned char. */
2895 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2896 warning_at (loc, OPT_Woverflow,
2897 "large integer implicitly truncated to unsigned type");
2898 else
2899 conversion_warning (loc, type, expr);
2901 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2902 warning_at (loc, OPT_Woverflow,
2903 "overflow in implicit constant conversion");
2904 /* No warning for converting 0x80000000 to int. */
2905 else if (pedantic
2906 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2907 || TYPE_PRECISION (TREE_TYPE (expr))
2908 != TYPE_PRECISION (type)))
2909 warning_at (loc, OPT_Woverflow,
2910 "overflow in implicit constant conversion");
2912 else
2913 conversion_warning (loc, type, expr);
2915 else if ((TREE_CODE (result) == INTEGER_CST
2916 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2917 warning_at (loc, OPT_Woverflow,
2918 "overflow in implicit constant conversion");
2919 else
2920 conversion_warning (loc, type, expr);
2924 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2925 Invoke this function on every expression that is converted implicitly,
2926 i.e. because of language rules and not because of an explicit cast. */
2928 tree
2929 convert_and_check (location_t loc, tree type, tree expr)
2931 tree result;
2932 tree expr_for_warning;
2934 /* Convert from a value with possible excess precision rather than
2935 via the semantic type, but do not warn about values not fitting
2936 exactly in the semantic type. */
2937 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2939 tree orig_type = TREE_TYPE (expr);
2940 expr = TREE_OPERAND (expr, 0);
2941 expr_for_warning = convert (orig_type, expr);
2942 if (orig_type == type)
2943 return expr_for_warning;
2945 else
2946 expr_for_warning = expr;
2948 if (TREE_TYPE (expr) == type)
2949 return expr;
2951 result = convert (type, expr);
2953 if (c_inhibit_evaluation_warnings == 0
2954 && !TREE_OVERFLOW_P (expr)
2955 && result != error_mark_node)
2956 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2958 return result;
2961 /* A node in a list that describes references to variables (EXPR), which are
2962 either read accesses if WRITER is zero, or write accesses, in which case
2963 WRITER is the parent of EXPR. */
2964 struct tlist
2966 struct tlist *next;
2967 tree expr, writer;
2970 /* Used to implement a cache the results of a call to verify_tree. We only
2971 use this for SAVE_EXPRs. */
2972 struct tlist_cache
2974 struct tlist_cache *next;
2975 struct tlist *cache_before_sp;
2976 struct tlist *cache_after_sp;
2977 tree expr;
2980 /* Obstack to use when allocating tlist structures, and corresponding
2981 firstobj. */
2982 static struct obstack tlist_obstack;
2983 static char *tlist_firstobj = 0;
2985 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2986 warnings. */
2987 static struct tlist *warned_ids;
2988 /* SAVE_EXPRs need special treatment. We process them only once and then
2989 cache the results. */
2990 static struct tlist_cache *save_expr_cache;
2992 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2993 static void merge_tlist (struct tlist **, struct tlist *, int);
2994 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2995 static int warning_candidate_p (tree);
2996 static bool candidate_equal_p (const_tree, const_tree);
2997 static void warn_for_collisions (struct tlist *);
2998 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2999 static struct tlist *new_tlist (struct tlist *, tree, tree);
3001 /* Create a new struct tlist and fill in its fields. */
3002 static struct tlist *
3003 new_tlist (struct tlist *next, tree t, tree writer)
3005 struct tlist *l;
3006 l = XOBNEW (&tlist_obstack, struct tlist);
3007 l->next = next;
3008 l->expr = t;
3009 l->writer = writer;
3010 return l;
3013 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3014 is nonnull, we ignore any node we find which has a writer equal to it. */
3016 static void
3017 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3019 while (add)
3021 struct tlist *next = add->next;
3022 if (!copy)
3023 add->next = *to;
3024 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3025 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3026 add = next;
3030 /* Merge the nodes of ADD into TO. This merging process is done so that for
3031 each variable that already exists in TO, no new node is added; however if
3032 there is a write access recorded in ADD, and an occurrence on TO is only
3033 a read access, then the occurrence in TO will be modified to record the
3034 write. */
3036 static void
3037 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3039 struct tlist **end = to;
3041 while (*end)
3042 end = &(*end)->next;
3044 while (add)
3046 int found = 0;
3047 struct tlist *tmp2;
3048 struct tlist *next = add->next;
3050 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3051 if (candidate_equal_p (tmp2->expr, add->expr))
3053 found = 1;
3054 if (!tmp2->writer)
3055 tmp2->writer = add->writer;
3057 if (!found)
3059 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3060 end = &(*end)->next;
3061 *end = 0;
3063 add = next;
3067 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3068 references in list LIST conflict with it, excluding reads if ONLY writers
3069 is nonzero. */
3071 static void
3072 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3073 int only_writes)
3075 struct tlist *tmp;
3077 /* Avoid duplicate warnings. */
3078 for (tmp = warned_ids; tmp; tmp = tmp->next)
3079 if (candidate_equal_p (tmp->expr, written))
3080 return;
3082 while (list)
3084 if (candidate_equal_p (list->expr, written)
3085 && !candidate_equal_p (list->writer, writer)
3086 && (!only_writes || list->writer))
3088 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3089 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3090 OPT_Wsequence_point, "operation on %qE may be undefined",
3091 list->expr);
3093 list = list->next;
3097 /* Given a list LIST of references to variables, find whether any of these
3098 can cause conflicts due to missing sequence points. */
3100 static void
3101 warn_for_collisions (struct tlist *list)
3103 struct tlist *tmp;
3105 for (tmp = list; tmp; tmp = tmp->next)
3107 if (tmp->writer)
3108 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3112 /* Return nonzero if X is a tree that can be verified by the sequence point
3113 warnings. */
3114 static int
3115 warning_candidate_p (tree x)
3117 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3118 return 0;
3120 if (TREE_CODE (x) == BLOCK)
3121 return 0;
3123 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3124 (lvalue_p) crash on TRY/CATCH. */
3125 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3126 return 0;
3128 if (!lvalue_p (x))
3129 return 0;
3131 /* No point to track non-const calls, they will never satisfy
3132 operand_equal_p. */
3133 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3134 return 0;
3136 if (TREE_CODE (x) == STRING_CST)
3137 return 0;
3139 return 1;
3142 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3143 static bool
3144 candidate_equal_p (const_tree x, const_tree y)
3146 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3149 /* Walk the tree X, and record accesses to variables. If X is written by the
3150 parent tree, WRITER is the parent.
3151 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3152 expression or its only operand forces a sequence point, then everything up
3153 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3154 in PNO_SP.
3155 Once we return, we will have emitted warnings if any subexpression before
3156 such a sequence point could be undefined. On a higher level, however, the
3157 sequence point may not be relevant, and we'll merge the two lists.
3159 Example: (b++, a) + b;
3160 The call that processes the COMPOUND_EXPR will store the increment of B
3161 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3162 processes the PLUS_EXPR will need to merge the two lists so that
3163 eventually, all accesses end up on the same list (and we'll warn about the
3164 unordered subexpressions b++ and b.
3166 A note on merging. If we modify the former example so that our expression
3167 becomes
3168 (b++, b) + a
3169 care must be taken not simply to add all three expressions into the final
3170 PNO_SP list. The function merge_tlist takes care of that by merging the
3171 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3172 way, so that no more than one access to B is recorded. */
3174 static void
3175 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3176 tree writer)
3178 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3179 enum tree_code code;
3180 enum tree_code_class cl;
3182 /* X may be NULL if it is the operand of an empty statement expression
3183 ({ }). */
3184 if (x == NULL)
3185 return;
3187 restart:
3188 code = TREE_CODE (x);
3189 cl = TREE_CODE_CLASS (code);
3191 if (warning_candidate_p (x))
3192 *pno_sp = new_tlist (*pno_sp, x, writer);
3194 switch (code)
3196 case CONSTRUCTOR:
3197 case SIZEOF_EXPR:
3198 return;
3200 case COMPOUND_EXPR:
3201 case TRUTH_ANDIF_EXPR:
3202 case TRUTH_ORIF_EXPR:
3203 tmp_before = tmp_nosp = tmp_list3 = 0;
3204 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3205 warn_for_collisions (tmp_nosp);
3206 merge_tlist (pbefore_sp, tmp_before, 0);
3207 merge_tlist (pbefore_sp, tmp_nosp, 0);
3208 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3209 merge_tlist (pbefore_sp, tmp_list3, 0);
3210 return;
3212 case COND_EXPR:
3213 tmp_before = tmp_list2 = 0;
3214 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3215 warn_for_collisions (tmp_list2);
3216 merge_tlist (pbefore_sp, tmp_before, 0);
3217 merge_tlist (pbefore_sp, tmp_list2, 0);
3219 tmp_list3 = tmp_nosp = 0;
3220 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3221 warn_for_collisions (tmp_nosp);
3222 merge_tlist (pbefore_sp, tmp_list3, 0);
3224 tmp_list3 = tmp_list2 = 0;
3225 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3226 warn_for_collisions (tmp_list2);
3227 merge_tlist (pbefore_sp, tmp_list3, 0);
3228 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3229 two first, to avoid warning for (a ? b++ : b++). */
3230 merge_tlist (&tmp_nosp, tmp_list2, 0);
3231 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3232 return;
3234 case PREDECREMENT_EXPR:
3235 case PREINCREMENT_EXPR:
3236 case POSTDECREMENT_EXPR:
3237 case POSTINCREMENT_EXPR:
3238 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3239 return;
3241 case MODIFY_EXPR:
3242 tmp_before = tmp_nosp = tmp_list3 = 0;
3243 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3244 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3245 /* Expressions inside the LHS are not ordered wrt. the sequence points
3246 in the RHS. Example:
3247 *a = (a++, 2)
3248 Despite the fact that the modification of "a" is in the before_sp
3249 list (tmp_before), it conflicts with the use of "a" in the LHS.
3250 We can handle this by adding the contents of tmp_list3
3251 to those of tmp_before, and redoing the collision warnings for that
3252 list. */
3253 add_tlist (&tmp_before, tmp_list3, x, 1);
3254 warn_for_collisions (tmp_before);
3255 /* Exclude the LHS itself here; we first have to merge it into the
3256 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3257 didn't exclude the LHS, we'd get it twice, once as a read and once
3258 as a write. */
3259 add_tlist (pno_sp, tmp_list3, x, 0);
3260 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3262 merge_tlist (pbefore_sp, tmp_before, 0);
3263 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3264 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3265 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3266 return;
3268 case CALL_EXPR:
3269 /* We need to warn about conflicts among arguments and conflicts between
3270 args and the function address. Side effects of the function address,
3271 however, are not ordered by the sequence point of the call. */
3273 call_expr_arg_iterator iter;
3274 tree arg;
3275 tmp_before = tmp_nosp = 0;
3276 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3277 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3279 tmp_list2 = tmp_list3 = 0;
3280 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3281 merge_tlist (&tmp_list3, tmp_list2, 0);
3282 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3284 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3285 warn_for_collisions (tmp_before);
3286 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3287 return;
3290 case TREE_LIST:
3291 /* Scan all the list, e.g. indices of multi dimensional array. */
3292 while (x)
3294 tmp_before = tmp_nosp = 0;
3295 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3296 merge_tlist (&tmp_nosp, tmp_before, 0);
3297 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3298 x = TREE_CHAIN (x);
3300 return;
3302 case SAVE_EXPR:
3304 struct tlist_cache *t;
3305 for (t = save_expr_cache; t; t = t->next)
3306 if (candidate_equal_p (t->expr, x))
3307 break;
3309 if (!t)
3311 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3312 t->next = save_expr_cache;
3313 t->expr = x;
3314 save_expr_cache = t;
3316 tmp_before = tmp_nosp = 0;
3317 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3318 warn_for_collisions (tmp_nosp);
3320 tmp_list3 = 0;
3321 merge_tlist (&tmp_list3, tmp_nosp, 0);
3322 t->cache_before_sp = tmp_before;
3323 t->cache_after_sp = tmp_list3;
3325 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3326 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3327 return;
3330 case ADDR_EXPR:
3331 x = TREE_OPERAND (x, 0);
3332 if (DECL_P (x))
3333 return;
3334 writer = 0;
3335 goto restart;
3337 default:
3338 /* For other expressions, simply recurse on their operands.
3339 Manual tail recursion for unary expressions.
3340 Other non-expressions need not be processed. */
3341 if (cl == tcc_unary)
3343 x = TREE_OPERAND (x, 0);
3344 writer = 0;
3345 goto restart;
3347 else if (IS_EXPR_CODE_CLASS (cl))
3349 int lp;
3350 int max = TREE_OPERAND_LENGTH (x);
3351 for (lp = 0; lp < max; lp++)
3353 tmp_before = tmp_nosp = 0;
3354 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3355 merge_tlist (&tmp_nosp, tmp_before, 0);
3356 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3359 return;
3363 /* Try to warn for undefined behavior in EXPR due to missing sequence
3364 points. */
3366 DEBUG_FUNCTION void
3367 verify_sequence_points (tree expr)
3369 struct tlist *before_sp = 0, *after_sp = 0;
3371 warned_ids = 0;
3372 save_expr_cache = 0;
3373 if (tlist_firstobj == 0)
3375 gcc_obstack_init (&tlist_obstack);
3376 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3379 verify_tree (expr, &before_sp, &after_sp, 0);
3380 warn_for_collisions (after_sp);
3381 obstack_free (&tlist_obstack, tlist_firstobj);
3384 /* Validate the expression after `case' and apply default promotions. */
3386 static tree
3387 check_case_value (location_t loc, tree value)
3389 if (value == NULL_TREE)
3390 return value;
3392 if (TREE_CODE (value) == INTEGER_CST)
3393 /* Promote char or short to int. */
3394 value = perform_integral_promotions (value);
3395 else if (value != error_mark_node)
3397 error_at (loc, "case label does not reduce to an integer constant");
3398 value = error_mark_node;
3401 constant_expression_warning (value);
3403 return value;
3406 /* See if the case values LOW and HIGH are in the range of the original
3407 type (i.e. before the default conversion to int) of the switch testing
3408 expression.
3409 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3410 the type before promoting it. CASE_LOW_P is a pointer to the lower
3411 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3412 if the case is not a case range.
3413 The caller has to make sure that we are not called with NULL for
3414 CASE_LOW_P (i.e. the default case).
3415 Returns true if the case label is in range of ORIG_TYPE (saturated or
3416 untouched) or false if the label is out of range. */
3418 static bool
3419 check_case_bounds (location_t loc, tree type, tree orig_type,
3420 tree *case_low_p, tree *case_high_p)
3422 tree min_value, max_value;
3423 tree case_low = *case_low_p;
3424 tree case_high = case_high_p ? *case_high_p : case_low;
3426 /* If there was a problem with the original type, do nothing. */
3427 if (orig_type == error_mark_node)
3428 return true;
3430 min_value = TYPE_MIN_VALUE (orig_type);
3431 max_value = TYPE_MAX_VALUE (orig_type);
3433 /* Case label is less than minimum for type. */
3434 if (tree_int_cst_compare (case_low, min_value) < 0
3435 && tree_int_cst_compare (case_high, min_value) < 0)
3437 warning_at (loc, 0, "case label value is less than minimum value "
3438 "for type");
3439 return false;
3442 /* Case value is greater than maximum for type. */
3443 if (tree_int_cst_compare (case_low, max_value) > 0
3444 && tree_int_cst_compare (case_high, max_value) > 0)
3446 warning_at (loc, 0, "case label value exceeds maximum value for type");
3447 return false;
3450 /* Saturate lower case label value to minimum. */
3451 if (tree_int_cst_compare (case_high, min_value) >= 0
3452 && tree_int_cst_compare (case_low, min_value) < 0)
3454 warning_at (loc, 0, "lower value in case label range"
3455 " less than minimum value for type");
3456 case_low = min_value;
3459 /* Saturate upper case label value to maximum. */
3460 if (tree_int_cst_compare (case_low, max_value) <= 0
3461 && tree_int_cst_compare (case_high, max_value) > 0)
3463 warning_at (loc, 0, "upper value in case label range"
3464 " exceeds maximum value for type");
3465 case_high = max_value;
3468 if (*case_low_p != case_low)
3469 *case_low_p = convert (type, case_low);
3470 if (case_high_p && *case_high_p != case_high)
3471 *case_high_p = convert (type, case_high);
3473 return true;
3476 /* Return an integer type with BITS bits of precision,
3477 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3479 tree
3480 c_common_type_for_size (unsigned int bits, int unsignedp)
3482 int i;
3484 if (bits == TYPE_PRECISION (integer_type_node))
3485 return unsignedp ? unsigned_type_node : integer_type_node;
3487 if (bits == TYPE_PRECISION (signed_char_type_node))
3488 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3490 if (bits == TYPE_PRECISION (short_integer_type_node))
3491 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3493 if (bits == TYPE_PRECISION (long_integer_type_node))
3494 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3496 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3497 return (unsignedp ? long_long_unsigned_type_node
3498 : long_long_integer_type_node);
3500 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3501 if (int_n_enabled_p[i]
3502 && bits == int_n_data[i].bitsize)
3503 return (unsignedp ? int_n_trees[i].unsigned_type
3504 : int_n_trees[i].signed_type);
3506 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3507 return (unsignedp ? widest_unsigned_literal_type_node
3508 : widest_integer_literal_type_node);
3510 if (bits <= TYPE_PRECISION (intQI_type_node))
3511 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3513 if (bits <= TYPE_PRECISION (intHI_type_node))
3514 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3516 if (bits <= TYPE_PRECISION (intSI_type_node))
3517 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3519 if (bits <= TYPE_PRECISION (intDI_type_node))
3520 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3522 return 0;
3525 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3526 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3527 and saturating if SATP is nonzero, otherwise not saturating. */
3529 tree
3530 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3531 int unsignedp, int satp)
3533 machine_mode mode;
3534 if (ibit == 0)
3535 mode = unsignedp ? UQQmode : QQmode;
3536 else
3537 mode = unsignedp ? UHAmode : HAmode;
3539 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3540 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3541 break;
3543 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3545 sorry ("GCC cannot support operators with integer types and "
3546 "fixed-point types that have too many integral and "
3547 "fractional bits together");
3548 return 0;
3551 return c_common_type_for_mode (mode, satp);
3554 /* Used for communication between c_common_type_for_mode and
3555 c_register_builtin_type. */
3556 tree registered_builtin_types;
3558 /* Return a data type that has machine mode MODE.
3559 If the mode is an integer,
3560 then UNSIGNEDP selects between signed and unsigned types.
3561 If the mode is a fixed-point mode,
3562 then UNSIGNEDP selects between saturating and nonsaturating types. */
3564 tree
3565 c_common_type_for_mode (machine_mode mode, int unsignedp)
3567 tree t;
3568 int i;
3570 if (mode == TYPE_MODE (integer_type_node))
3571 return unsignedp ? unsigned_type_node : integer_type_node;
3573 if (mode == TYPE_MODE (signed_char_type_node))
3574 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3576 if (mode == TYPE_MODE (short_integer_type_node))
3577 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3579 if (mode == TYPE_MODE (long_integer_type_node))
3580 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3582 if (mode == TYPE_MODE (long_long_integer_type_node))
3583 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3585 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3586 if (int_n_enabled_p[i]
3587 && mode == int_n_data[i].m)
3588 return (unsignedp ? int_n_trees[i].unsigned_type
3589 : int_n_trees[i].signed_type);
3591 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3592 return unsignedp ? widest_unsigned_literal_type_node
3593 : widest_integer_literal_type_node;
3595 if (mode == QImode)
3596 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3598 if (mode == HImode)
3599 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3601 if (mode == SImode)
3602 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3604 if (mode == DImode)
3605 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3607 #if HOST_BITS_PER_WIDE_INT >= 64
3608 if (mode == TYPE_MODE (intTI_type_node))
3609 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3610 #endif
3612 if (mode == TYPE_MODE (float_type_node))
3613 return float_type_node;
3615 if (mode == TYPE_MODE (double_type_node))
3616 return double_type_node;
3618 if (mode == TYPE_MODE (long_double_type_node))
3619 return long_double_type_node;
3621 if (mode == TYPE_MODE (void_type_node))
3622 return void_type_node;
3624 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3625 return (unsignedp
3626 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3627 : make_signed_type (GET_MODE_PRECISION (mode)));
3629 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3630 return (unsignedp
3631 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3632 : make_signed_type (GET_MODE_PRECISION (mode)));
3634 if (COMPLEX_MODE_P (mode))
3636 machine_mode inner_mode;
3637 tree inner_type;
3639 if (mode == TYPE_MODE (complex_float_type_node))
3640 return complex_float_type_node;
3641 if (mode == TYPE_MODE (complex_double_type_node))
3642 return complex_double_type_node;
3643 if (mode == TYPE_MODE (complex_long_double_type_node))
3644 return complex_long_double_type_node;
3646 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3647 return complex_integer_type_node;
3649 inner_mode = GET_MODE_INNER (mode);
3650 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3651 if (inner_type != NULL_TREE)
3652 return build_complex_type (inner_type);
3654 else if (VECTOR_MODE_P (mode))
3656 machine_mode inner_mode = GET_MODE_INNER (mode);
3657 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3658 if (inner_type != NULL_TREE)
3659 return build_vector_type_for_mode (inner_type, mode);
3662 if (mode == TYPE_MODE (dfloat32_type_node))
3663 return dfloat32_type_node;
3664 if (mode == TYPE_MODE (dfloat64_type_node))
3665 return dfloat64_type_node;
3666 if (mode == TYPE_MODE (dfloat128_type_node))
3667 return dfloat128_type_node;
3669 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3671 if (mode == TYPE_MODE (short_fract_type_node))
3672 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3673 if (mode == TYPE_MODE (fract_type_node))
3674 return unsignedp ? sat_fract_type_node : fract_type_node;
3675 if (mode == TYPE_MODE (long_fract_type_node))
3676 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3677 if (mode == TYPE_MODE (long_long_fract_type_node))
3678 return unsignedp ? sat_long_long_fract_type_node
3679 : long_long_fract_type_node;
3681 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3682 return unsignedp ? sat_unsigned_short_fract_type_node
3683 : unsigned_short_fract_type_node;
3684 if (mode == TYPE_MODE (unsigned_fract_type_node))
3685 return unsignedp ? sat_unsigned_fract_type_node
3686 : unsigned_fract_type_node;
3687 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3688 return unsignedp ? sat_unsigned_long_fract_type_node
3689 : unsigned_long_fract_type_node;
3690 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3691 return unsignedp ? sat_unsigned_long_long_fract_type_node
3692 : unsigned_long_long_fract_type_node;
3694 if (mode == TYPE_MODE (short_accum_type_node))
3695 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3696 if (mode == TYPE_MODE (accum_type_node))
3697 return unsignedp ? sat_accum_type_node : accum_type_node;
3698 if (mode == TYPE_MODE (long_accum_type_node))
3699 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3700 if (mode == TYPE_MODE (long_long_accum_type_node))
3701 return unsignedp ? sat_long_long_accum_type_node
3702 : long_long_accum_type_node;
3704 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3705 return unsignedp ? sat_unsigned_short_accum_type_node
3706 : unsigned_short_accum_type_node;
3707 if (mode == TYPE_MODE (unsigned_accum_type_node))
3708 return unsignedp ? sat_unsigned_accum_type_node
3709 : unsigned_accum_type_node;
3710 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3711 return unsignedp ? sat_unsigned_long_accum_type_node
3712 : unsigned_long_accum_type_node;
3713 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3714 return unsignedp ? sat_unsigned_long_long_accum_type_node
3715 : unsigned_long_long_accum_type_node;
3717 if (mode == QQmode)
3718 return unsignedp ? sat_qq_type_node : qq_type_node;
3719 if (mode == HQmode)
3720 return unsignedp ? sat_hq_type_node : hq_type_node;
3721 if (mode == SQmode)
3722 return unsignedp ? sat_sq_type_node : sq_type_node;
3723 if (mode == DQmode)
3724 return unsignedp ? sat_dq_type_node : dq_type_node;
3725 if (mode == TQmode)
3726 return unsignedp ? sat_tq_type_node : tq_type_node;
3728 if (mode == UQQmode)
3729 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3730 if (mode == UHQmode)
3731 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3732 if (mode == USQmode)
3733 return unsignedp ? sat_usq_type_node : usq_type_node;
3734 if (mode == UDQmode)
3735 return unsignedp ? sat_udq_type_node : udq_type_node;
3736 if (mode == UTQmode)
3737 return unsignedp ? sat_utq_type_node : utq_type_node;
3739 if (mode == HAmode)
3740 return unsignedp ? sat_ha_type_node : ha_type_node;
3741 if (mode == SAmode)
3742 return unsignedp ? sat_sa_type_node : sa_type_node;
3743 if (mode == DAmode)
3744 return unsignedp ? sat_da_type_node : da_type_node;
3745 if (mode == TAmode)
3746 return unsignedp ? sat_ta_type_node : ta_type_node;
3748 if (mode == UHAmode)
3749 return unsignedp ? sat_uha_type_node : uha_type_node;
3750 if (mode == USAmode)
3751 return unsignedp ? sat_usa_type_node : usa_type_node;
3752 if (mode == UDAmode)
3753 return unsignedp ? sat_uda_type_node : uda_type_node;
3754 if (mode == UTAmode)
3755 return unsignedp ? sat_uta_type_node : uta_type_node;
3758 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3759 if (TYPE_MODE (TREE_VALUE (t)) == mode
3760 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3761 return TREE_VALUE (t);
3763 return 0;
3766 tree
3767 c_common_unsigned_type (tree type)
3769 return c_common_signed_or_unsigned_type (1, type);
3772 /* Return a signed type the same as TYPE in other respects. */
3774 tree
3775 c_common_signed_type (tree type)
3777 return c_common_signed_or_unsigned_type (0, type);
3780 /* Return a type the same as TYPE except unsigned or
3781 signed according to UNSIGNEDP. */
3783 tree
3784 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3786 tree type1;
3787 int i;
3789 /* This block of code emulates the behavior of the old
3790 c_common_unsigned_type. In particular, it returns
3791 long_unsigned_type_node if passed a long, even when a int would
3792 have the same size. This is necessary for warnings to work
3793 correctly in archs where sizeof(int) == sizeof(long) */
3795 type1 = TYPE_MAIN_VARIANT (type);
3796 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3797 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3798 if (type1 == integer_type_node || type1 == unsigned_type_node)
3799 return unsignedp ? unsigned_type_node : integer_type_node;
3800 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3801 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3802 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3803 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3804 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3805 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3807 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3808 if (int_n_enabled_p[i]
3809 && (type1 == int_n_trees[i].unsigned_type
3810 || type1 == int_n_trees[i].signed_type))
3811 return (unsignedp ? int_n_trees[i].unsigned_type
3812 : int_n_trees[i].signed_type);
3814 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3815 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3816 #if HOST_BITS_PER_WIDE_INT >= 64
3817 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3818 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3819 #endif
3820 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3821 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3822 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3823 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3824 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3825 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3826 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3827 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3829 #define C_COMMON_FIXED_TYPES(NAME) \
3830 if (type1 == short_ ## NAME ## _type_node \
3831 || type1 == unsigned_short_ ## NAME ## _type_node) \
3832 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3833 : short_ ## NAME ## _type_node; \
3834 if (type1 == NAME ## _type_node \
3835 || type1 == unsigned_ ## NAME ## _type_node) \
3836 return unsignedp ? unsigned_ ## NAME ## _type_node \
3837 : NAME ## _type_node; \
3838 if (type1 == long_ ## NAME ## _type_node \
3839 || type1 == unsigned_long_ ## NAME ## _type_node) \
3840 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3841 : long_ ## NAME ## _type_node; \
3842 if (type1 == long_long_ ## NAME ## _type_node \
3843 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3844 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3845 : long_long_ ## NAME ## _type_node;
3847 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3848 if (type1 == NAME ## _type_node \
3849 || type1 == u ## NAME ## _type_node) \
3850 return unsignedp ? u ## NAME ## _type_node \
3851 : NAME ## _type_node;
3853 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3854 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3855 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3856 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3857 : sat_ ## short_ ## NAME ## _type_node; \
3858 if (type1 == sat_ ## NAME ## _type_node \
3859 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3860 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3861 : sat_ ## NAME ## _type_node; \
3862 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3863 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3864 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3865 : sat_ ## long_ ## NAME ## _type_node; \
3866 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3867 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3868 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3869 : sat_ ## long_long_ ## NAME ## _type_node;
3871 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3872 if (type1 == sat_ ## NAME ## _type_node \
3873 || type1 == sat_ ## u ## NAME ## _type_node) \
3874 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3875 : sat_ ## NAME ## _type_node;
3877 C_COMMON_FIXED_TYPES (fract);
3878 C_COMMON_FIXED_TYPES_SAT (fract);
3879 C_COMMON_FIXED_TYPES (accum);
3880 C_COMMON_FIXED_TYPES_SAT (accum);
3882 C_COMMON_FIXED_MODE_TYPES (qq);
3883 C_COMMON_FIXED_MODE_TYPES (hq);
3884 C_COMMON_FIXED_MODE_TYPES (sq);
3885 C_COMMON_FIXED_MODE_TYPES (dq);
3886 C_COMMON_FIXED_MODE_TYPES (tq);
3887 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3888 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3889 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3890 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3891 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3892 C_COMMON_FIXED_MODE_TYPES (ha);
3893 C_COMMON_FIXED_MODE_TYPES (sa);
3894 C_COMMON_FIXED_MODE_TYPES (da);
3895 C_COMMON_FIXED_MODE_TYPES (ta);
3896 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3897 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3898 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3899 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3901 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3902 the precision; they have precision set to match their range, but
3903 may use a wider mode to match an ABI. If we change modes, we may
3904 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3905 the precision as well, so as to yield correct results for
3906 bit-field types. C++ does not have these separate bit-field
3907 types, and producing a signed or unsigned variant of an
3908 ENUMERAL_TYPE may cause other problems as well. */
3910 if (!INTEGRAL_TYPE_P (type)
3911 || TYPE_UNSIGNED (type) == unsignedp)
3912 return type;
3914 #define TYPE_OK(node) \
3915 (TYPE_MODE (type) == TYPE_MODE (node) \
3916 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3917 if (TYPE_OK (signed_char_type_node))
3918 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3919 if (TYPE_OK (integer_type_node))
3920 return unsignedp ? unsigned_type_node : integer_type_node;
3921 if (TYPE_OK (short_integer_type_node))
3922 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3923 if (TYPE_OK (long_integer_type_node))
3924 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3925 if (TYPE_OK (long_long_integer_type_node))
3926 return (unsignedp ? long_long_unsigned_type_node
3927 : long_long_integer_type_node);
3929 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3930 if (int_n_enabled_p[i]
3931 && TYPE_MODE (type) == int_n_data[i].m
3932 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3933 return (unsignedp ? int_n_trees[i].unsigned_type
3934 : int_n_trees[i].signed_type);
3936 if (TYPE_OK (widest_integer_literal_type_node))
3937 return (unsignedp ? widest_unsigned_literal_type_node
3938 : widest_integer_literal_type_node);
3940 #if HOST_BITS_PER_WIDE_INT >= 64
3941 if (TYPE_OK (intTI_type_node))
3942 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3943 #endif
3944 if (TYPE_OK (intDI_type_node))
3945 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3946 if (TYPE_OK (intSI_type_node))
3947 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3948 if (TYPE_OK (intHI_type_node))
3949 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3950 if (TYPE_OK (intQI_type_node))
3951 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3952 #undef TYPE_OK
3954 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3957 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3959 tree
3960 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3962 int i;
3964 /* Extended integer types of the same width as a standard type have
3965 lesser rank, so those of the same width as int promote to int or
3966 unsigned int and are valid for printf formats expecting int or
3967 unsigned int. To avoid such special cases, avoid creating
3968 extended integer types for bit-fields if a standard integer type
3969 is available. */
3970 if (width == TYPE_PRECISION (integer_type_node))
3971 return unsignedp ? unsigned_type_node : integer_type_node;
3972 if (width == TYPE_PRECISION (signed_char_type_node))
3973 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3974 if (width == TYPE_PRECISION (short_integer_type_node))
3975 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3976 if (width == TYPE_PRECISION (long_integer_type_node))
3977 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3978 if (width == TYPE_PRECISION (long_long_integer_type_node))
3979 return (unsignedp ? long_long_unsigned_type_node
3980 : long_long_integer_type_node);
3981 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3982 if (int_n_enabled_p[i]
3983 && width == int_n_data[i].bitsize)
3984 return (unsignedp ? int_n_trees[i].unsigned_type
3985 : int_n_trees[i].signed_type);
3986 return build_nonstandard_integer_type (width, unsignedp);
3989 /* The C version of the register_builtin_type langhook. */
3991 void
3992 c_register_builtin_type (tree type, const char* name)
3994 tree decl;
3996 decl = build_decl (UNKNOWN_LOCATION,
3997 TYPE_DECL, get_identifier (name), type);
3998 DECL_ARTIFICIAL (decl) = 1;
3999 if (!TYPE_NAME (type))
4000 TYPE_NAME (type) = decl;
4001 pushdecl (decl);
4003 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4006 /* Print an error message for invalid operands to arith operation
4007 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4008 LOCATION is the location of the message. */
4010 void
4011 binary_op_error (location_t location, enum tree_code code,
4012 tree type0, tree type1)
4014 const char *opname;
4016 switch (code)
4018 case PLUS_EXPR:
4019 opname = "+"; break;
4020 case MINUS_EXPR:
4021 opname = "-"; break;
4022 case MULT_EXPR:
4023 opname = "*"; break;
4024 case MAX_EXPR:
4025 opname = "max"; break;
4026 case MIN_EXPR:
4027 opname = "min"; break;
4028 case EQ_EXPR:
4029 opname = "=="; break;
4030 case NE_EXPR:
4031 opname = "!="; break;
4032 case LE_EXPR:
4033 opname = "<="; break;
4034 case GE_EXPR:
4035 opname = ">="; break;
4036 case LT_EXPR:
4037 opname = "<"; break;
4038 case GT_EXPR:
4039 opname = ">"; break;
4040 case LSHIFT_EXPR:
4041 opname = "<<"; break;
4042 case RSHIFT_EXPR:
4043 opname = ">>"; break;
4044 case TRUNC_MOD_EXPR:
4045 case FLOOR_MOD_EXPR:
4046 opname = "%"; break;
4047 case TRUNC_DIV_EXPR:
4048 case FLOOR_DIV_EXPR:
4049 opname = "/"; break;
4050 case BIT_AND_EXPR:
4051 opname = "&"; break;
4052 case BIT_IOR_EXPR:
4053 opname = "|"; break;
4054 case TRUTH_ANDIF_EXPR:
4055 opname = "&&"; break;
4056 case TRUTH_ORIF_EXPR:
4057 opname = "||"; break;
4058 case BIT_XOR_EXPR:
4059 opname = "^"; break;
4060 default:
4061 gcc_unreachable ();
4063 error_at (location,
4064 "invalid operands to binary %s (have %qT and %qT)", opname,
4065 type0, type1);
4068 /* Given an expression as a tree, return its original type. Do this
4069 by stripping any conversion that preserves the sign and precision. */
4070 static tree
4071 expr_original_type (tree expr)
4073 STRIP_SIGN_NOPS (expr);
4074 return TREE_TYPE (expr);
4077 /* Subroutine of build_binary_op, used for comparison operations.
4078 See if the operands have both been converted from subword integer types
4079 and, if so, perhaps change them both back to their original type.
4080 This function is also responsible for converting the two operands
4081 to the proper common type for comparison.
4083 The arguments of this function are all pointers to local variables
4084 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4085 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4087 LOC is the location of the comparison.
4089 If this function returns nonzero, it means that the comparison has
4090 a constant value. What this function returns is an expression for
4091 that value. */
4093 tree
4094 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4095 tree *restype_ptr, enum tree_code *rescode_ptr)
4097 tree type;
4098 tree op0 = *op0_ptr;
4099 tree op1 = *op1_ptr;
4100 int unsignedp0, unsignedp1;
4101 int real1, real2;
4102 tree primop0, primop1;
4103 enum tree_code code = *rescode_ptr;
4105 /* Throw away any conversions to wider types
4106 already present in the operands. */
4108 primop0 = c_common_get_narrower (op0, &unsignedp0);
4109 primop1 = c_common_get_narrower (op1, &unsignedp1);
4111 /* If primopN is first sign-extended from primopN's precision to opN's
4112 precision, then zero-extended from opN's precision to
4113 *restype_ptr precision, shortenings might be invalid. */
4114 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4115 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4116 && !unsignedp0
4117 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4118 primop0 = op0;
4119 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4120 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4121 && !unsignedp1
4122 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4123 primop1 = op1;
4125 /* Handle the case that OP0 does not *contain* a conversion
4126 but it *requires* conversion to FINAL_TYPE. */
4128 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4129 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4130 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4131 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4133 /* If one of the operands must be floated, we cannot optimize. */
4134 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4135 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4137 /* If first arg is constant, swap the args (changing operation
4138 so value is preserved), for canonicalization. Don't do this if
4139 the second arg is 0. */
4141 if (TREE_CONSTANT (primop0)
4142 && !integer_zerop (primop1) && !real_zerop (primop1)
4143 && !fixed_zerop (primop1))
4145 tree tem = primop0;
4146 int temi = unsignedp0;
4147 primop0 = primop1;
4148 primop1 = tem;
4149 tem = op0;
4150 op0 = op1;
4151 op1 = tem;
4152 *op0_ptr = op0;
4153 *op1_ptr = op1;
4154 unsignedp0 = unsignedp1;
4155 unsignedp1 = temi;
4156 temi = real1;
4157 real1 = real2;
4158 real2 = temi;
4160 switch (code)
4162 case LT_EXPR:
4163 code = GT_EXPR;
4164 break;
4165 case GT_EXPR:
4166 code = LT_EXPR;
4167 break;
4168 case LE_EXPR:
4169 code = GE_EXPR;
4170 break;
4171 case GE_EXPR:
4172 code = LE_EXPR;
4173 break;
4174 default:
4175 break;
4177 *rescode_ptr = code;
4180 /* If comparing an integer against a constant more bits wide,
4181 maybe we can deduce a value of 1 or 0 independent of the data.
4182 Or else truncate the constant now
4183 rather than extend the variable at run time.
4185 This is only interesting if the constant is the wider arg.
4186 Also, it is not safe if the constant is unsigned and the
4187 variable arg is signed, since in this case the variable
4188 would be sign-extended and then regarded as unsigned.
4189 Our technique fails in this case because the lowest/highest
4190 possible unsigned results don't follow naturally from the
4191 lowest/highest possible values of the variable operand.
4192 For just EQ_EXPR and NE_EXPR there is another technique that
4193 could be used: see if the constant can be faithfully represented
4194 in the other operand's type, by truncating it and reextending it
4195 and see if that preserves the constant's value. */
4197 if (!real1 && !real2
4198 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4199 && TREE_CODE (primop1) == INTEGER_CST
4200 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4202 int min_gt, max_gt, min_lt, max_lt;
4203 tree maxval, minval;
4204 /* 1 if comparison is nominally unsigned. */
4205 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4206 tree val;
4208 type = c_common_signed_or_unsigned_type (unsignedp0,
4209 TREE_TYPE (primop0));
4211 maxval = TYPE_MAX_VALUE (type);
4212 minval = TYPE_MIN_VALUE (type);
4214 if (unsignedp && !unsignedp0)
4215 *restype_ptr = c_common_signed_type (*restype_ptr);
4217 if (TREE_TYPE (primop1) != *restype_ptr)
4219 /* Convert primop1 to target type, but do not introduce
4220 additional overflow. We know primop1 is an int_cst. */
4221 primop1 = force_fit_type (*restype_ptr,
4222 wide_int::from
4223 (primop1,
4224 TYPE_PRECISION (*restype_ptr),
4225 TYPE_SIGN (TREE_TYPE (primop1))),
4226 0, TREE_OVERFLOW (primop1));
4228 if (type != *restype_ptr)
4230 minval = convert (*restype_ptr, minval);
4231 maxval = convert (*restype_ptr, maxval);
4234 min_gt = tree_int_cst_lt (primop1, minval);
4235 max_gt = tree_int_cst_lt (primop1, maxval);
4236 min_lt = tree_int_cst_lt (minval, primop1);
4237 max_lt = tree_int_cst_lt (maxval, primop1);
4239 val = 0;
4240 /* This used to be a switch, but Genix compiler can't handle that. */
4241 if (code == NE_EXPR)
4243 if (max_lt || min_gt)
4244 val = truthvalue_true_node;
4246 else if (code == EQ_EXPR)
4248 if (max_lt || min_gt)
4249 val = truthvalue_false_node;
4251 else if (code == LT_EXPR)
4253 if (max_lt)
4254 val = truthvalue_true_node;
4255 if (!min_lt)
4256 val = truthvalue_false_node;
4258 else if (code == GT_EXPR)
4260 if (min_gt)
4261 val = truthvalue_true_node;
4262 if (!max_gt)
4263 val = truthvalue_false_node;
4265 else if (code == LE_EXPR)
4267 if (!max_gt)
4268 val = truthvalue_true_node;
4269 if (min_gt)
4270 val = truthvalue_false_node;
4272 else if (code == GE_EXPR)
4274 if (!min_lt)
4275 val = truthvalue_true_node;
4276 if (max_lt)
4277 val = truthvalue_false_node;
4280 /* If primop0 was sign-extended and unsigned comparison specd,
4281 we did a signed comparison above using the signed type bounds.
4282 But the comparison we output must be unsigned.
4284 Also, for inequalities, VAL is no good; but if the signed
4285 comparison had *any* fixed result, it follows that the
4286 unsigned comparison just tests the sign in reverse
4287 (positive values are LE, negative ones GE).
4288 So we can generate an unsigned comparison
4289 against an extreme value of the signed type. */
4291 if (unsignedp && !unsignedp0)
4293 if (val != 0)
4294 switch (code)
4296 case LT_EXPR:
4297 case GE_EXPR:
4298 primop1 = TYPE_MIN_VALUE (type);
4299 val = 0;
4300 break;
4302 case LE_EXPR:
4303 case GT_EXPR:
4304 primop1 = TYPE_MAX_VALUE (type);
4305 val = 0;
4306 break;
4308 default:
4309 break;
4311 type = c_common_unsigned_type (type);
4314 if (TREE_CODE (primop0) != INTEGER_CST)
4316 if (val == truthvalue_false_node)
4317 warning_at (loc, OPT_Wtype_limits,
4318 "comparison is always false due to limited range of data type");
4319 if (val == truthvalue_true_node)
4320 warning_at (loc, OPT_Wtype_limits,
4321 "comparison is always true due to limited range of data type");
4324 if (val != 0)
4326 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4327 if (TREE_SIDE_EFFECTS (primop0))
4328 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4329 return val;
4332 /* Value is not predetermined, but do the comparison
4333 in the type of the operand that is not constant.
4334 TYPE is already properly set. */
4337 /* If either arg is decimal float and the other is float, find the
4338 proper common type to use for comparison. */
4339 else if (real1 && real2
4340 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4341 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4342 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4344 /* If either arg is decimal float and the other is float, fail. */
4345 else if (real1 && real2
4346 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4347 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4348 return 0;
4350 else if (real1 && real2
4351 && (TYPE_PRECISION (TREE_TYPE (primop0))
4352 == TYPE_PRECISION (TREE_TYPE (primop1))))
4353 type = TREE_TYPE (primop0);
4355 /* If args' natural types are both narrower than nominal type
4356 and both extend in the same manner, compare them
4357 in the type of the wider arg.
4358 Otherwise must actually extend both to the nominal
4359 common type lest different ways of extending
4360 alter the result.
4361 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4363 else if (unsignedp0 == unsignedp1 && real1 == real2
4364 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4365 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4367 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4368 type = c_common_signed_or_unsigned_type (unsignedp0
4369 || TYPE_UNSIGNED (*restype_ptr),
4370 type);
4371 /* Make sure shorter operand is extended the right way
4372 to match the longer operand. */
4373 primop0
4374 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4375 TREE_TYPE (primop0)),
4376 primop0);
4377 primop1
4378 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4379 TREE_TYPE (primop1)),
4380 primop1);
4382 else
4384 /* Here we must do the comparison on the nominal type
4385 using the args exactly as we received them. */
4386 type = *restype_ptr;
4387 primop0 = op0;
4388 primop1 = op1;
4390 if (!real1 && !real2 && integer_zerop (primop1)
4391 && TYPE_UNSIGNED (*restype_ptr))
4393 tree value = 0;
4394 /* All unsigned values are >= 0, so we warn. However,
4395 if OP0 is a constant that is >= 0, the signedness of
4396 the comparison isn't an issue, so suppress the
4397 warning. */
4398 bool warn =
4399 warn_type_limits && !in_system_header_at (loc)
4400 && !(TREE_CODE (primop0) == INTEGER_CST
4401 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4402 primop0)))
4403 /* Do not warn for enumeration types. */
4404 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4406 switch (code)
4408 case GE_EXPR:
4409 if (warn)
4410 warning_at (loc, OPT_Wtype_limits,
4411 "comparison of unsigned expression >= 0 is always true");
4412 value = truthvalue_true_node;
4413 break;
4415 case LT_EXPR:
4416 if (warn)
4417 warning_at (loc, OPT_Wtype_limits,
4418 "comparison of unsigned expression < 0 is always false");
4419 value = truthvalue_false_node;
4420 break;
4422 default:
4423 break;
4426 if (value != 0)
4428 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4429 if (TREE_SIDE_EFFECTS (primop0))
4430 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4431 primop0, value);
4432 return value;
4437 *op0_ptr = convert (type, primop0);
4438 *op1_ptr = convert (type, primop1);
4440 *restype_ptr = truthvalue_type_node;
4442 return 0;
4445 /* Return a tree for the sum or difference (RESULTCODE says which)
4446 of pointer PTROP and integer INTOP. */
4448 tree
4449 pointer_int_sum (location_t loc, enum tree_code resultcode,
4450 tree ptrop, tree intop, bool complain)
4452 tree size_exp, ret;
4454 /* The result is a pointer of the same type that is being added. */
4455 tree result_type = TREE_TYPE (ptrop);
4457 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4459 if (complain && warn_pointer_arith)
4460 pedwarn (loc, OPT_Wpointer_arith,
4461 "pointer of type %<void *%> used in arithmetic");
4462 else if (!complain)
4463 return error_mark_node;
4464 size_exp = integer_one_node;
4466 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4468 if (complain && warn_pointer_arith)
4469 pedwarn (loc, OPT_Wpointer_arith,
4470 "pointer to a function used in arithmetic");
4471 else if (!complain)
4472 return error_mark_node;
4473 size_exp = integer_one_node;
4475 else
4476 size_exp = size_in_bytes (TREE_TYPE (result_type));
4478 /* We are manipulating pointer values, so we don't need to warn
4479 about relying on undefined signed overflow. We disable the
4480 warning here because we use integer types so fold won't know that
4481 they are really pointers. */
4482 fold_defer_overflow_warnings ();
4484 /* If what we are about to multiply by the size of the elements
4485 contains a constant term, apply distributive law
4486 and multiply that constant term separately.
4487 This helps produce common subexpressions. */
4488 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4489 && !TREE_CONSTANT (intop)
4490 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4491 && TREE_CONSTANT (size_exp)
4492 /* If the constant comes from pointer subtraction,
4493 skip this optimization--it would cause an error. */
4494 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4495 /* If the constant is unsigned, and smaller than the pointer size,
4496 then we must skip this optimization. This is because it could cause
4497 an overflow error if the constant is negative but INTOP is not. */
4498 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4499 || (TYPE_PRECISION (TREE_TYPE (intop))
4500 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4502 enum tree_code subcode = resultcode;
4503 tree int_type = TREE_TYPE (intop);
4504 if (TREE_CODE (intop) == MINUS_EXPR)
4505 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4506 /* Convert both subexpression types to the type of intop,
4507 because weird cases involving pointer arithmetic
4508 can result in a sum or difference with different type args. */
4509 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4510 subcode, ptrop,
4511 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4512 intop = convert (int_type, TREE_OPERAND (intop, 0));
4515 /* Convert the integer argument to a type the same size as sizetype
4516 so the multiply won't overflow spuriously. */
4517 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4518 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4519 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4520 TYPE_UNSIGNED (sizetype)), intop);
4522 /* Replace the integer argument with a suitable product by the object size.
4523 Do this multiplication as signed, then convert to the appropriate type
4524 for the pointer operation and disregard an overflow that occurred only
4525 because of the sign-extension change in the latter conversion. */
4527 tree t = build_binary_op (loc,
4528 MULT_EXPR, intop,
4529 convert (TREE_TYPE (intop), size_exp), 1);
4530 intop = convert (sizetype, t);
4531 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4532 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4535 /* Create the sum or difference. */
4536 if (resultcode == MINUS_EXPR)
4537 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4539 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4541 fold_undefer_and_ignore_overflow_warnings ();
4543 return ret;
4546 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4547 and if NON_CONST is known not to be permitted in an evaluated part
4548 of a constant expression. */
4550 tree
4551 c_wrap_maybe_const (tree expr, bool non_const)
4553 bool nowarning = TREE_NO_WARNING (expr);
4554 location_t loc = EXPR_LOCATION (expr);
4556 /* This should never be called for C++. */
4557 if (c_dialect_cxx ())
4558 gcc_unreachable ();
4560 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4561 STRIP_TYPE_NOPS (expr);
4562 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4563 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4564 if (nowarning)
4565 TREE_NO_WARNING (expr) = 1;
4566 protected_set_expr_location (expr, loc);
4568 return expr;
4571 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4572 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4573 around the SAVE_EXPR if needed so that c_fully_fold does not need
4574 to look inside SAVE_EXPRs. */
4576 tree
4577 c_save_expr (tree expr)
4579 bool maybe_const = true;
4580 if (c_dialect_cxx ())
4581 return save_expr (expr);
4582 expr = c_fully_fold (expr, false, &maybe_const);
4583 expr = save_expr (expr);
4584 if (!maybe_const)
4585 expr = c_wrap_maybe_const (expr, true);
4586 return expr;
4589 /* Return whether EXPR is a declaration whose address can never be
4590 NULL. */
4592 bool
4593 decl_with_nonnull_addr_p (const_tree expr)
4595 return (DECL_P (expr)
4596 && (TREE_CODE (expr) == PARM_DECL
4597 || TREE_CODE (expr) == LABEL_DECL
4598 || !DECL_WEAK (expr)));
4601 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4602 or for an `if' or `while' statement or ?..: exp. It should already
4603 have been validated to be of suitable type; otherwise, a bad
4604 diagnostic may result.
4606 The EXPR is located at LOCATION.
4608 This preparation consists of taking the ordinary
4609 representation of an expression expr and producing a valid tree
4610 boolean expression describing whether expr is nonzero. We could
4611 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4612 but we optimize comparisons, &&, ||, and !.
4614 The resulting type should always be `truthvalue_type_node'. */
4616 tree
4617 c_common_truthvalue_conversion (location_t location, tree expr)
4619 switch (TREE_CODE (expr))
4621 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4622 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4623 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4624 case ORDERED_EXPR: case UNORDERED_EXPR:
4625 if (TREE_TYPE (expr) == truthvalue_type_node)
4626 return expr;
4627 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4628 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4629 goto ret;
4631 case TRUTH_ANDIF_EXPR:
4632 case TRUTH_ORIF_EXPR:
4633 case TRUTH_AND_EXPR:
4634 case TRUTH_OR_EXPR:
4635 case TRUTH_XOR_EXPR:
4636 if (TREE_TYPE (expr) == truthvalue_type_node)
4637 return expr;
4638 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4639 c_common_truthvalue_conversion (location,
4640 TREE_OPERAND (expr, 0)),
4641 c_common_truthvalue_conversion (location,
4642 TREE_OPERAND (expr, 1)));
4643 goto ret;
4645 case TRUTH_NOT_EXPR:
4646 if (TREE_TYPE (expr) == truthvalue_type_node)
4647 return expr;
4648 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4649 c_common_truthvalue_conversion (location,
4650 TREE_OPERAND (expr, 0)));
4651 goto ret;
4653 case ERROR_MARK:
4654 return expr;
4656 case INTEGER_CST:
4657 return integer_zerop (expr) ? truthvalue_false_node
4658 : truthvalue_true_node;
4660 case REAL_CST:
4661 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4662 ? truthvalue_true_node
4663 : truthvalue_false_node;
4665 case FIXED_CST:
4666 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4667 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4668 ? truthvalue_true_node
4669 : truthvalue_false_node;
4671 case FUNCTION_DECL:
4672 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4673 /* Fall through. */
4675 case ADDR_EXPR:
4677 tree inner = TREE_OPERAND (expr, 0);
4678 if (decl_with_nonnull_addr_p (inner))
4680 /* Common Ada/Pascal programmer's mistake. */
4681 warning_at (location,
4682 OPT_Waddress,
4683 "the address of %qD will always evaluate as %<true%>",
4684 inner);
4685 return truthvalue_true_node;
4687 break;
4690 case COMPLEX_EXPR:
4691 expr = build_binary_op (EXPR_LOCATION (expr),
4692 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4693 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4694 c_common_truthvalue_conversion (location,
4695 TREE_OPERAND (expr, 0)),
4696 c_common_truthvalue_conversion (location,
4697 TREE_OPERAND (expr, 1)),
4699 goto ret;
4701 case NEGATE_EXPR:
4702 case ABS_EXPR:
4703 case FLOAT_EXPR:
4704 case EXCESS_PRECISION_EXPR:
4705 /* These don't change whether an object is nonzero or zero. */
4706 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4708 case LROTATE_EXPR:
4709 case RROTATE_EXPR:
4710 /* These don't change whether an object is zero or nonzero, but
4711 we can't ignore them if their second arg has side-effects. */
4712 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4714 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4715 TREE_OPERAND (expr, 1),
4716 c_common_truthvalue_conversion
4717 (location, TREE_OPERAND (expr, 0)));
4718 goto ret;
4720 else
4721 return c_common_truthvalue_conversion (location,
4722 TREE_OPERAND (expr, 0));
4724 case COND_EXPR:
4725 /* Distribute the conversion into the arms of a COND_EXPR. */
4726 if (c_dialect_cxx ())
4728 tree op1 = TREE_OPERAND (expr, 1);
4729 tree op2 = TREE_OPERAND (expr, 2);
4730 /* In C++ one of the arms might have void type if it is throw. */
4731 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4732 op1 = c_common_truthvalue_conversion (location, op1);
4733 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4734 op2 = c_common_truthvalue_conversion (location, op2);
4735 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4736 TREE_OPERAND (expr, 0), op1, op2);
4737 goto ret;
4739 else
4741 /* Folding will happen later for C. */
4742 expr = build3 (COND_EXPR, truthvalue_type_node,
4743 TREE_OPERAND (expr, 0),
4744 c_common_truthvalue_conversion (location,
4745 TREE_OPERAND (expr, 1)),
4746 c_common_truthvalue_conversion (location,
4747 TREE_OPERAND (expr, 2)));
4748 goto ret;
4751 CASE_CONVERT:
4753 tree totype = TREE_TYPE (expr);
4754 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4756 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4757 since that affects how `default_conversion' will behave. */
4758 if (TREE_CODE (totype) == REFERENCE_TYPE
4759 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4760 break;
4761 /* Don't strip a conversion from C++0x scoped enum, since they
4762 don't implicitly convert to other types. */
4763 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4764 && ENUM_IS_SCOPED (fromtype))
4765 break;
4766 /* If this isn't narrowing the argument, we can ignore it. */
4767 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4768 return c_common_truthvalue_conversion (location,
4769 TREE_OPERAND (expr, 0));
4771 break;
4773 case MODIFY_EXPR:
4774 if (!TREE_NO_WARNING (expr)
4775 && warn_parentheses)
4777 warning (OPT_Wparentheses,
4778 "suggest parentheses around assignment used as truth value");
4779 TREE_NO_WARNING (expr) = 1;
4781 break;
4783 default:
4784 break;
4787 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4789 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4790 expr = (build_binary_op
4791 (EXPR_LOCATION (expr),
4792 (TREE_SIDE_EFFECTS (expr)
4793 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4794 c_common_truthvalue_conversion
4795 (location,
4796 build_unary_op (location, REALPART_EXPR, t, 0)),
4797 c_common_truthvalue_conversion
4798 (location,
4799 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4800 0));
4801 goto ret;
4804 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4806 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4807 FCONST0 (TYPE_MODE
4808 (TREE_TYPE (expr))));
4809 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4811 else
4812 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4814 ret:
4815 protected_set_expr_location (expr, location);
4816 return expr;
4819 static void def_builtin_1 (enum built_in_function fncode,
4820 const char *name,
4821 enum built_in_class fnclass,
4822 tree fntype, tree libtype,
4823 bool both_p, bool fallback_p, bool nonansi_p,
4824 tree fnattrs, bool implicit_p);
4827 /* Apply the TYPE_QUALS to the new DECL. */
4829 void
4830 c_apply_type_quals_to_decl (int type_quals, tree decl)
4832 tree type = TREE_TYPE (decl);
4834 if (type == error_mark_node)
4835 return;
4837 if ((type_quals & TYPE_QUAL_CONST)
4838 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4839 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4840 constructor can produce constant init, so rely on cp_finish_decl to
4841 clear TREE_READONLY if the variable has non-constant init. */
4842 TREE_READONLY (decl) = 1;
4843 if (type_quals & TYPE_QUAL_VOLATILE)
4845 TREE_SIDE_EFFECTS (decl) = 1;
4846 TREE_THIS_VOLATILE (decl) = 1;
4848 if (type_quals & TYPE_QUAL_RESTRICT)
4850 while (type && TREE_CODE (type) == ARRAY_TYPE)
4851 /* Allow 'restrict' on arrays of pointers.
4852 FIXME currently we just ignore it. */
4853 type = TREE_TYPE (type);
4854 if (!type
4855 || !POINTER_TYPE_P (type)
4856 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4857 error ("invalid use of %<restrict%>");
4861 struct c_type_hasher : ggc_hasher<tree>
4863 static hashval_t hash (tree);
4864 static bool equal (tree, tree);
4867 /* Hash function for the problem of multiple type definitions in
4868 different files. This must hash all types that will compare
4869 equal via comptypes to the same value. In practice it hashes
4870 on some of the simple stuff and leaves the details to comptypes. */
4872 hashval_t
4873 c_type_hasher::hash (tree t)
4875 int n_elements;
4876 int shift, size;
4877 tree t2;
4878 switch (TREE_CODE (t))
4880 /* For pointers, hash on pointee type plus some swizzling. */
4881 case POINTER_TYPE:
4882 return hash (TREE_TYPE (t)) ^ 0x3003003;
4883 /* Hash on number of elements and total size. */
4884 case ENUMERAL_TYPE:
4885 shift = 3;
4886 t2 = TYPE_VALUES (t);
4887 break;
4888 case RECORD_TYPE:
4889 shift = 0;
4890 t2 = TYPE_FIELDS (t);
4891 break;
4892 case QUAL_UNION_TYPE:
4893 shift = 1;
4894 t2 = TYPE_FIELDS (t);
4895 break;
4896 case UNION_TYPE:
4897 shift = 2;
4898 t2 = TYPE_FIELDS (t);
4899 break;
4900 default:
4901 gcc_unreachable ();
4903 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4904 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4905 n_elements = list_length (t2);
4906 /* We might have a VLA here. */
4907 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4908 size = 0;
4909 else
4910 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4911 return ((size << 24) | (n_elements << shift));
4914 bool
4915 c_type_hasher::equal (tree t1, tree t2)
4917 return lang_hooks.types_compatible_p (t1, t2);
4920 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4922 /* Return the typed-based alias set for T, which may be an expression
4923 or a type. Return -1 if we don't do anything special. */
4925 alias_set_type
4926 c_common_get_alias_set (tree t)
4928 tree u;
4930 /* For VLAs, use the alias set of the element type rather than the
4931 default of alias set 0 for types compared structurally. */
4932 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4934 if (TREE_CODE (t) == ARRAY_TYPE)
4935 return get_alias_set (TREE_TYPE (t));
4936 return -1;
4939 /* Permit type-punning when accessing a union, provided the access
4940 is directly through the union. For example, this code does not
4941 permit taking the address of a union member and then storing
4942 through it. Even the type-punning allowed here is a GCC
4943 extension, albeit a common and useful one; the C standard says
4944 that such accesses have implementation-defined behavior. */
4945 for (u = t;
4946 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4947 u = TREE_OPERAND (u, 0))
4948 if (TREE_CODE (u) == COMPONENT_REF
4949 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4950 return 0;
4952 /* That's all the expressions we handle specially. */
4953 if (!TYPE_P (t))
4954 return -1;
4956 /* The C standard guarantees that any object may be accessed via an
4957 lvalue that has character type. */
4958 if (t == char_type_node
4959 || t == signed_char_type_node
4960 || t == unsigned_char_type_node)
4961 return 0;
4963 /* The C standard specifically allows aliasing between signed and
4964 unsigned variants of the same type. We treat the signed
4965 variant as canonical. */
4966 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4968 tree t1 = c_common_signed_type (t);
4970 /* t1 == t can happen for boolean nodes which are always unsigned. */
4971 if (t1 != t)
4972 return get_alias_set (t1);
4975 /* Handle the case of multiple type nodes referring to "the same" type,
4976 which occurs with IMA. These share an alias set. FIXME: Currently only
4977 C90 is handled. (In C99 type compatibility is not transitive, which
4978 complicates things mightily. The alias set splay trees can theoretically
4979 represent this, but insertion is tricky when you consider all the
4980 different orders things might arrive in.) */
4982 if (c_language != clk_c || flag_isoc99)
4983 return -1;
4985 /* Save time if there's only one input file. */
4986 if (num_in_fnames == 1)
4987 return -1;
4989 /* Pointers need special handling if they point to any type that
4990 needs special handling (below). */
4991 if (TREE_CODE (t) == POINTER_TYPE)
4993 tree t2;
4994 /* Find bottom type under any nested POINTERs. */
4995 for (t2 = TREE_TYPE (t);
4996 TREE_CODE (t2) == POINTER_TYPE;
4997 t2 = TREE_TYPE (t2))
4999 if (TREE_CODE (t2) != RECORD_TYPE
5000 && TREE_CODE (t2) != ENUMERAL_TYPE
5001 && TREE_CODE (t2) != QUAL_UNION_TYPE
5002 && TREE_CODE (t2) != UNION_TYPE)
5003 return -1;
5004 if (TYPE_SIZE (t2) == 0)
5005 return -1;
5007 /* These are the only cases that need special handling. */
5008 if (TREE_CODE (t) != RECORD_TYPE
5009 && TREE_CODE (t) != ENUMERAL_TYPE
5010 && TREE_CODE (t) != QUAL_UNION_TYPE
5011 && TREE_CODE (t) != UNION_TYPE
5012 && TREE_CODE (t) != POINTER_TYPE)
5013 return -1;
5014 /* Undefined? */
5015 if (TYPE_SIZE (t) == 0)
5016 return -1;
5018 /* Look up t in hash table. Only one of the compatible types within each
5019 alias set is recorded in the table. */
5020 if (!type_hash_table)
5021 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5022 tree *slot = type_hash_table->find_slot (t, INSERT);
5023 if (*slot != NULL)
5025 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5026 return TYPE_ALIAS_SET ((tree)*slot);
5028 else
5029 /* Our caller will assign and record (in t) a new alias set; all we need
5030 to do is remember t in the hash table. */
5031 *slot = t;
5033 return -1;
5036 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5037 the IS_SIZEOF parameter indicates which operator is being applied.
5038 The COMPLAIN flag controls whether we should diagnose possibly
5039 ill-formed constructs or not. LOC is the location of the SIZEOF or
5040 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5041 a type in any context should be returned, rather than the normal
5042 alignment for that type. */
5044 tree
5045 c_sizeof_or_alignof_type (location_t loc,
5046 tree type, bool is_sizeof, bool min_alignof,
5047 int complain)
5049 const char *op_name;
5050 tree value = NULL;
5051 enum tree_code type_code = TREE_CODE (type);
5053 op_name = is_sizeof ? "sizeof" : "__alignof__";
5055 if (type_code == FUNCTION_TYPE)
5057 if (is_sizeof)
5059 if (complain && warn_pointer_arith)
5060 pedwarn (loc, OPT_Wpointer_arith,
5061 "invalid application of %<sizeof%> to a function type");
5062 else if (!complain)
5063 return error_mark_node;
5064 value = size_one_node;
5066 else
5068 if (complain)
5070 if (c_dialect_cxx ())
5071 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5072 "%<alignof%> applied to a function type");
5073 else
5074 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5075 "%<_Alignof%> applied to a function type");
5077 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5080 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5082 if (type_code == VOID_TYPE
5083 && complain && warn_pointer_arith)
5084 pedwarn (loc, OPT_Wpointer_arith,
5085 "invalid application of %qs to a void type", op_name);
5086 else if (!complain)
5087 return error_mark_node;
5088 value = size_one_node;
5090 else if (!COMPLETE_TYPE_P (type)
5091 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5093 if (complain)
5094 error_at (loc, "invalid application of %qs to incomplete type %qT",
5095 op_name, type);
5096 return error_mark_node;
5098 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5099 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5101 if (complain)
5102 error_at (loc, "invalid application of %qs to array type %qT of "
5103 "incomplete element type", op_name, type);
5104 return error_mark_node;
5106 else
5108 if (is_sizeof)
5109 /* Convert in case a char is more than one unit. */
5110 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5111 size_int (TYPE_PRECISION (char_type_node)
5112 / BITS_PER_UNIT));
5113 else if (min_alignof)
5114 value = size_int (min_align_of_type (type));
5115 else
5116 value = size_int (TYPE_ALIGN_UNIT (type));
5119 /* VALUE will have the middle-end integer type sizetype.
5120 However, we should really return a value of type `size_t',
5121 which is just a typedef for an ordinary integer type. */
5122 value = fold_convert_loc (loc, size_type_node, value);
5124 return value;
5127 /* Implement the __alignof keyword: Return the minimum required
5128 alignment of EXPR, measured in bytes. For VAR_DECLs,
5129 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5130 from an "aligned" __attribute__ specification). LOC is the
5131 location of the ALIGNOF operator. */
5133 tree
5134 c_alignof_expr (location_t loc, tree expr)
5136 tree t;
5138 if (VAR_OR_FUNCTION_DECL_P (expr))
5139 t = size_int (DECL_ALIGN_UNIT (expr));
5141 else if (TREE_CODE (expr) == COMPONENT_REF
5142 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5144 error_at (loc, "%<__alignof%> applied to a bit-field");
5145 t = size_one_node;
5147 else if (TREE_CODE (expr) == COMPONENT_REF
5148 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5149 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5151 else if (TREE_CODE (expr) == INDIRECT_REF)
5153 tree t = TREE_OPERAND (expr, 0);
5154 tree best = t;
5155 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5157 while (CONVERT_EXPR_P (t)
5158 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5160 int thisalign;
5162 t = TREE_OPERAND (t, 0);
5163 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5164 if (thisalign > bestalign)
5165 best = t, bestalign = thisalign;
5167 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5169 else
5170 return c_alignof (loc, TREE_TYPE (expr));
5172 return fold_convert_loc (loc, size_type_node, t);
5175 /* Handle C and C++ default attributes. */
5177 enum built_in_attribute
5179 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5180 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5181 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5182 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5183 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5184 #include "builtin-attrs.def"
5185 #undef DEF_ATTR_NULL_TREE
5186 #undef DEF_ATTR_INT
5187 #undef DEF_ATTR_STRING
5188 #undef DEF_ATTR_IDENT
5189 #undef DEF_ATTR_TREE_LIST
5190 ATTR_LAST
5193 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5195 static void c_init_attributes (void);
5197 enum c_builtin_type
5199 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5200 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5201 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5202 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5203 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5204 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5205 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5206 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5207 ARG6) NAME,
5208 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5209 ARG6, ARG7) NAME,
5210 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5211 ARG6, ARG7, ARG8) NAME,
5212 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5213 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5214 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5215 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5216 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5217 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5218 NAME,
5219 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5220 #include "builtin-types.def"
5221 #undef DEF_PRIMITIVE_TYPE
5222 #undef DEF_FUNCTION_TYPE_0
5223 #undef DEF_FUNCTION_TYPE_1
5224 #undef DEF_FUNCTION_TYPE_2
5225 #undef DEF_FUNCTION_TYPE_3
5226 #undef DEF_FUNCTION_TYPE_4
5227 #undef DEF_FUNCTION_TYPE_5
5228 #undef DEF_FUNCTION_TYPE_6
5229 #undef DEF_FUNCTION_TYPE_7
5230 #undef DEF_FUNCTION_TYPE_8
5231 #undef DEF_FUNCTION_TYPE_VAR_0
5232 #undef DEF_FUNCTION_TYPE_VAR_1
5233 #undef DEF_FUNCTION_TYPE_VAR_2
5234 #undef DEF_FUNCTION_TYPE_VAR_3
5235 #undef DEF_FUNCTION_TYPE_VAR_4
5236 #undef DEF_FUNCTION_TYPE_VAR_5
5237 #undef DEF_POINTER_TYPE
5238 BT_LAST
5241 typedef enum c_builtin_type builtin_type;
5243 /* A temporary array for c_common_nodes_and_builtins. Used in
5244 communication with def_fn_type. */
5245 static tree builtin_types[(int) BT_LAST + 1];
5247 /* A helper function for c_common_nodes_and_builtins. Build function type
5248 for DEF with return type RET and N arguments. If VAR is true, then the
5249 function should be variadic after those N arguments.
5251 Takes special care not to ICE if any of the types involved are
5252 error_mark_node, which indicates that said type is not in fact available
5253 (see builtin_type_for_size). In which case the function type as a whole
5254 should be error_mark_node. */
5256 static void
5257 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5259 tree t;
5260 tree *args = XALLOCAVEC (tree, n);
5261 va_list list;
5262 int i;
5264 va_start (list, n);
5265 for (i = 0; i < n; ++i)
5267 builtin_type a = (builtin_type) va_arg (list, int);
5268 t = builtin_types[a];
5269 if (t == error_mark_node)
5270 goto egress;
5271 args[i] = t;
5274 t = builtin_types[ret];
5275 if (t == error_mark_node)
5276 goto egress;
5277 if (var)
5278 t = build_varargs_function_type_array (t, n, args);
5279 else
5280 t = build_function_type_array (t, n, args);
5282 egress:
5283 builtin_types[def] = t;
5284 va_end (list);
5287 /* Build builtin functions common to both C and C++ language
5288 frontends. */
5290 static void
5291 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5293 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5294 builtin_types[ENUM] = VALUE;
5295 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5296 def_fn_type (ENUM, RETURN, 0, 0);
5297 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5298 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5299 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5300 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5301 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5302 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5303 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5304 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5305 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5306 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5307 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5308 ARG6) \
5309 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5310 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5311 ARG6, ARG7) \
5312 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5313 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5314 ARG6, ARG7, ARG8) \
5315 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5316 ARG7, ARG8);
5317 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5318 def_fn_type (ENUM, RETURN, 1, 0);
5319 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5320 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5321 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5322 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5323 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5324 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5325 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5326 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5327 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5328 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5329 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5330 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5332 #include "builtin-types.def"
5334 #undef DEF_PRIMITIVE_TYPE
5335 #undef DEF_FUNCTION_TYPE_0
5336 #undef DEF_FUNCTION_TYPE_1
5337 #undef DEF_FUNCTION_TYPE_2
5338 #undef DEF_FUNCTION_TYPE_3
5339 #undef DEF_FUNCTION_TYPE_4
5340 #undef DEF_FUNCTION_TYPE_5
5341 #undef DEF_FUNCTION_TYPE_6
5342 #undef DEF_FUNCTION_TYPE_7
5343 #undef DEF_FUNCTION_TYPE_8
5344 #undef DEF_FUNCTION_TYPE_VAR_0
5345 #undef DEF_FUNCTION_TYPE_VAR_1
5346 #undef DEF_FUNCTION_TYPE_VAR_2
5347 #undef DEF_FUNCTION_TYPE_VAR_3
5348 #undef DEF_FUNCTION_TYPE_VAR_4
5349 #undef DEF_FUNCTION_TYPE_VAR_5
5350 #undef DEF_POINTER_TYPE
5351 builtin_types[(int) BT_LAST] = NULL_TREE;
5353 c_init_attributes ();
5355 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5356 NONANSI_P, ATTRS, IMPLICIT, COND) \
5357 if (NAME && COND) \
5358 def_builtin_1 (ENUM, NAME, CLASS, \
5359 builtin_types[(int) TYPE], \
5360 builtin_types[(int) LIBTYPE], \
5361 BOTH_P, FALLBACK_P, NONANSI_P, \
5362 built_in_attributes[(int) ATTRS], IMPLICIT);
5363 #include "builtins.def"
5364 #undef DEF_BUILTIN
5366 targetm.init_builtins ();
5368 build_common_builtin_nodes ();
5370 if (flag_cilkplus)
5371 cilk_init_builtins ();
5374 /* Like get_identifier, but avoid warnings about null arguments when
5375 the argument may be NULL for targets where GCC lacks stdint.h type
5376 information. */
5378 static inline tree
5379 c_get_ident (const char *id)
5381 return get_identifier (id);
5384 /* Build tree nodes and builtin functions common to both C and C++ language
5385 frontends. */
5387 void
5388 c_common_nodes_and_builtins (void)
5390 int char16_type_size;
5391 int char32_type_size;
5392 int wchar_type_size;
5393 tree array_domain_type;
5394 tree va_list_ref_type_node;
5395 tree va_list_arg_type_node;
5396 int i;
5398 build_common_tree_nodes (flag_signed_char, flag_short_double);
5400 /* Define `int' and `char' first so that dbx will output them first. */
5401 record_builtin_type (RID_INT, NULL, integer_type_node);
5402 record_builtin_type (RID_CHAR, "char", char_type_node);
5404 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5405 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5406 but not C. Are the conditionals here needed? */
5407 if (c_dialect_cxx ())
5408 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5409 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5410 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5411 record_builtin_type (RID_MAX, "long unsigned int",
5412 long_unsigned_type_node);
5414 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5416 char name[25];
5418 sprintf (name, "__int%d", int_n_data[i].bitsize);
5419 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), xstrdup (name),
5420 int_n_trees[i].signed_type);
5421 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5422 record_builtin_type (RID_MAX, xstrdup (name),
5423 int_n_trees[i].unsigned_type);
5426 if (c_dialect_cxx ())
5427 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5428 record_builtin_type (RID_MAX, "long long int",
5429 long_long_integer_type_node);
5430 record_builtin_type (RID_MAX, "long long unsigned int",
5431 long_long_unsigned_type_node);
5432 if (c_dialect_cxx ())
5433 record_builtin_type (RID_MAX, "long long unsigned",
5434 long_long_unsigned_type_node);
5435 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5436 record_builtin_type (RID_MAX, "short unsigned int",
5437 short_unsigned_type_node);
5438 if (c_dialect_cxx ())
5439 record_builtin_type (RID_MAX, "unsigned short",
5440 short_unsigned_type_node);
5442 /* Define both `signed char' and `unsigned char'. */
5443 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5444 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5446 /* These are types that c_common_type_for_size and
5447 c_common_type_for_mode use. */
5448 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5449 TYPE_DECL, NULL_TREE,
5450 intQI_type_node));
5451 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5452 TYPE_DECL, NULL_TREE,
5453 intHI_type_node));
5454 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5455 TYPE_DECL, NULL_TREE,
5456 intSI_type_node));
5457 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5458 TYPE_DECL, NULL_TREE,
5459 intDI_type_node));
5460 #if HOST_BITS_PER_WIDE_INT >= 64
5461 /* Note that this is different than the __int128 type that's part of
5462 the generic __intN support. */
5463 if (targetm.scalar_mode_supported_p (TImode))
5464 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5465 TYPE_DECL,
5466 get_identifier ("__int128_t"),
5467 intTI_type_node));
5468 #endif
5469 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5470 TYPE_DECL, NULL_TREE,
5471 unsigned_intQI_type_node));
5472 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5473 TYPE_DECL, NULL_TREE,
5474 unsigned_intHI_type_node));
5475 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5476 TYPE_DECL, NULL_TREE,
5477 unsigned_intSI_type_node));
5478 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5479 TYPE_DECL, NULL_TREE,
5480 unsigned_intDI_type_node));
5481 #if HOST_BITS_PER_WIDE_INT >= 64
5482 if (targetm.scalar_mode_supported_p (TImode))
5483 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5484 TYPE_DECL,
5485 get_identifier ("__uint128_t"),
5486 unsigned_intTI_type_node));
5487 #endif
5489 /* Create the widest literal types. */
5490 widest_integer_literal_type_node
5491 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5492 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5493 TYPE_DECL, NULL_TREE,
5494 widest_integer_literal_type_node));
5496 widest_unsigned_literal_type_node
5497 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5498 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5499 TYPE_DECL, NULL_TREE,
5500 widest_unsigned_literal_type_node));
5502 signed_size_type_node = c_common_signed_type (size_type_node);
5504 pid_type_node =
5505 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5507 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5508 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5509 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5511 /* Only supported decimal floating point extension if the target
5512 actually supports underlying modes. */
5513 if (targetm.scalar_mode_supported_p (SDmode)
5514 && targetm.scalar_mode_supported_p (DDmode)
5515 && targetm.scalar_mode_supported_p (TDmode))
5517 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5518 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5519 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5522 if (targetm.fixed_point_supported_p ())
5524 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5525 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5526 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5527 record_builtin_type (RID_MAX, "long long _Fract",
5528 long_long_fract_type_node);
5529 record_builtin_type (RID_MAX, "unsigned short _Fract",
5530 unsigned_short_fract_type_node);
5531 record_builtin_type (RID_MAX, "unsigned _Fract",
5532 unsigned_fract_type_node);
5533 record_builtin_type (RID_MAX, "unsigned long _Fract",
5534 unsigned_long_fract_type_node);
5535 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5536 unsigned_long_long_fract_type_node);
5537 record_builtin_type (RID_MAX, "_Sat short _Fract",
5538 sat_short_fract_type_node);
5539 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5540 record_builtin_type (RID_MAX, "_Sat long _Fract",
5541 sat_long_fract_type_node);
5542 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5543 sat_long_long_fract_type_node);
5544 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5545 sat_unsigned_short_fract_type_node);
5546 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5547 sat_unsigned_fract_type_node);
5548 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5549 sat_unsigned_long_fract_type_node);
5550 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5551 sat_unsigned_long_long_fract_type_node);
5552 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5553 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5554 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5555 record_builtin_type (RID_MAX, "long long _Accum",
5556 long_long_accum_type_node);
5557 record_builtin_type (RID_MAX, "unsigned short _Accum",
5558 unsigned_short_accum_type_node);
5559 record_builtin_type (RID_MAX, "unsigned _Accum",
5560 unsigned_accum_type_node);
5561 record_builtin_type (RID_MAX, "unsigned long _Accum",
5562 unsigned_long_accum_type_node);
5563 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5564 unsigned_long_long_accum_type_node);
5565 record_builtin_type (RID_MAX, "_Sat short _Accum",
5566 sat_short_accum_type_node);
5567 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5568 record_builtin_type (RID_MAX, "_Sat long _Accum",
5569 sat_long_accum_type_node);
5570 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5571 sat_long_long_accum_type_node);
5572 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5573 sat_unsigned_short_accum_type_node);
5574 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5575 sat_unsigned_accum_type_node);
5576 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5577 sat_unsigned_long_accum_type_node);
5578 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5579 sat_unsigned_long_long_accum_type_node);
5583 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5584 TYPE_DECL,
5585 get_identifier ("complex int"),
5586 complex_integer_type_node));
5587 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5588 TYPE_DECL,
5589 get_identifier ("complex float"),
5590 complex_float_type_node));
5591 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5592 TYPE_DECL,
5593 get_identifier ("complex double"),
5594 complex_double_type_node));
5595 lang_hooks.decls.pushdecl
5596 (build_decl (UNKNOWN_LOCATION,
5597 TYPE_DECL, get_identifier ("complex long double"),
5598 complex_long_double_type_node));
5600 if (c_dialect_cxx ())
5601 /* For C++, make fileptr_type_node a distinct void * type until
5602 FILE type is defined. */
5603 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5605 record_builtin_type (RID_VOID, NULL, void_type_node);
5607 /* Set the TYPE_NAME for any variants that were built before
5608 record_builtin_type gave names to the built-in types. */
5610 tree void_name = TYPE_NAME (void_type_node);
5611 TYPE_NAME (void_type_node) = NULL_TREE;
5612 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5613 = void_name;
5614 TYPE_NAME (void_type_node) = void_name;
5617 void_list_node = build_void_list_node ();
5619 /* Make a type to be the domain of a few array types
5620 whose domains don't really matter.
5621 200 is small enough that it always fits in size_t
5622 and large enough that it can hold most function names for the
5623 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5624 array_domain_type = build_index_type (size_int (200));
5626 /* Make a type for arrays of characters.
5627 With luck nothing will ever really depend on the length of this
5628 array type. */
5629 char_array_type_node
5630 = build_array_type (char_type_node, array_domain_type);
5632 string_type_node = build_pointer_type (char_type_node);
5633 const_string_type_node
5634 = build_pointer_type (build_qualified_type
5635 (char_type_node, TYPE_QUAL_CONST));
5637 /* This is special for C++ so functions can be overloaded. */
5638 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5639 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5640 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5641 underlying_wchar_type_node = wchar_type_node;
5642 if (c_dialect_cxx ())
5644 if (TYPE_UNSIGNED (wchar_type_node))
5645 wchar_type_node = make_unsigned_type (wchar_type_size);
5646 else
5647 wchar_type_node = make_signed_type (wchar_type_size);
5648 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5651 /* This is for wide string constants. */
5652 wchar_array_type_node
5653 = build_array_type (wchar_type_node, array_domain_type);
5655 /* Define 'char16_t'. */
5656 char16_type_node = get_identifier (CHAR16_TYPE);
5657 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5658 char16_type_size = TYPE_PRECISION (char16_type_node);
5659 if (c_dialect_cxx ())
5661 char16_type_node = make_unsigned_type (char16_type_size);
5663 if (cxx_dialect >= cxx11)
5664 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5667 /* This is for UTF-16 string constants. */
5668 char16_array_type_node
5669 = build_array_type (char16_type_node, array_domain_type);
5671 /* Define 'char32_t'. */
5672 char32_type_node = get_identifier (CHAR32_TYPE);
5673 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5674 char32_type_size = TYPE_PRECISION (char32_type_node);
5675 if (c_dialect_cxx ())
5677 char32_type_node = make_unsigned_type (char32_type_size);
5679 if (cxx_dialect >= cxx11)
5680 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5683 /* This is for UTF-32 string constants. */
5684 char32_array_type_node
5685 = build_array_type (char32_type_node, array_domain_type);
5687 wint_type_node =
5688 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5690 intmax_type_node =
5691 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5692 uintmax_type_node =
5693 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5695 if (SIG_ATOMIC_TYPE)
5696 sig_atomic_type_node =
5697 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5698 if (INT8_TYPE)
5699 int8_type_node =
5700 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5701 if (INT16_TYPE)
5702 int16_type_node =
5703 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5704 if (INT32_TYPE)
5705 int32_type_node =
5706 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5707 if (INT64_TYPE)
5708 int64_type_node =
5709 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5710 if (UINT8_TYPE)
5711 uint8_type_node =
5712 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5713 if (UINT16_TYPE)
5714 c_uint16_type_node = uint16_type_node =
5715 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5716 if (UINT32_TYPE)
5717 c_uint32_type_node = uint32_type_node =
5718 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5719 if (UINT64_TYPE)
5720 c_uint64_type_node = uint64_type_node =
5721 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5722 if (INT_LEAST8_TYPE)
5723 int_least8_type_node =
5724 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5725 if (INT_LEAST16_TYPE)
5726 int_least16_type_node =
5727 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5728 if (INT_LEAST32_TYPE)
5729 int_least32_type_node =
5730 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5731 if (INT_LEAST64_TYPE)
5732 int_least64_type_node =
5733 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5734 if (UINT_LEAST8_TYPE)
5735 uint_least8_type_node =
5736 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5737 if (UINT_LEAST16_TYPE)
5738 uint_least16_type_node =
5739 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5740 if (UINT_LEAST32_TYPE)
5741 uint_least32_type_node =
5742 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5743 if (UINT_LEAST64_TYPE)
5744 uint_least64_type_node =
5745 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5746 if (INT_FAST8_TYPE)
5747 int_fast8_type_node =
5748 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5749 if (INT_FAST16_TYPE)
5750 int_fast16_type_node =
5751 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5752 if (INT_FAST32_TYPE)
5753 int_fast32_type_node =
5754 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5755 if (INT_FAST64_TYPE)
5756 int_fast64_type_node =
5757 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5758 if (UINT_FAST8_TYPE)
5759 uint_fast8_type_node =
5760 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5761 if (UINT_FAST16_TYPE)
5762 uint_fast16_type_node =
5763 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5764 if (UINT_FAST32_TYPE)
5765 uint_fast32_type_node =
5766 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5767 if (UINT_FAST64_TYPE)
5768 uint_fast64_type_node =
5769 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5770 if (INTPTR_TYPE)
5771 intptr_type_node =
5772 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5773 if (UINTPTR_TYPE)
5774 uintptr_type_node =
5775 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5777 default_function_type
5778 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5779 ptrdiff_type_node
5780 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5781 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5783 lang_hooks.decls.pushdecl
5784 (build_decl (UNKNOWN_LOCATION,
5785 TYPE_DECL, get_identifier ("__builtin_va_list"),
5786 va_list_type_node));
5787 if (targetm.enum_va_list_p)
5789 int l;
5790 const char *pname;
5791 tree ptype;
5793 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5795 lang_hooks.decls.pushdecl
5796 (build_decl (UNKNOWN_LOCATION,
5797 TYPE_DECL, get_identifier (pname),
5798 ptype));
5803 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5805 va_list_arg_type_node = va_list_ref_type_node =
5806 build_pointer_type (TREE_TYPE (va_list_type_node));
5808 else
5810 va_list_arg_type_node = va_list_type_node;
5811 va_list_ref_type_node = build_reference_type (va_list_type_node);
5814 if (!flag_preprocess_only)
5815 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5817 main_identifier_node = get_identifier ("main");
5819 /* Create the built-in __null node. It is important that this is
5820 not shared. */
5821 null_node = make_int_cst (1, 1);
5822 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5824 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5825 memset (builtin_types, 0, sizeof (builtin_types));
5828 /* The number of named compound-literals generated thus far. */
5829 static GTY(()) int compound_literal_number;
5831 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5833 void
5834 set_compound_literal_name (tree decl)
5836 char *name;
5837 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5838 compound_literal_number);
5839 compound_literal_number++;
5840 DECL_NAME (decl) = get_identifier (name);
5843 tree
5844 build_va_arg (location_t loc, tree expr, tree type)
5846 expr = build1 (VA_ARG_EXPR, type, expr);
5847 SET_EXPR_LOCATION (expr, loc);
5848 return expr;
5852 /* Linked list of disabled built-in functions. */
5854 typedef struct disabled_builtin
5856 const char *name;
5857 struct disabled_builtin *next;
5858 } disabled_builtin;
5859 static disabled_builtin *disabled_builtins = NULL;
5861 static bool builtin_function_disabled_p (const char *);
5863 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5864 begins with "__builtin_", give an error. */
5866 void
5867 disable_builtin_function (const char *name)
5869 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5870 error ("cannot disable built-in function %qs", name);
5871 else
5873 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5874 new_disabled_builtin->name = name;
5875 new_disabled_builtin->next = disabled_builtins;
5876 disabled_builtins = new_disabled_builtin;
5881 /* Return true if the built-in function NAME has been disabled, false
5882 otherwise. */
5884 static bool
5885 builtin_function_disabled_p (const char *name)
5887 disabled_builtin *p;
5888 for (p = disabled_builtins; p != NULL; p = p->next)
5890 if (strcmp (name, p->name) == 0)
5891 return true;
5893 return false;
5897 /* Worker for DEF_BUILTIN.
5898 Possibly define a builtin function with one or two names.
5899 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5900 nonansi_p and flag_no_nonansi_builtin. */
5902 static void
5903 def_builtin_1 (enum built_in_function fncode,
5904 const char *name,
5905 enum built_in_class fnclass,
5906 tree fntype, tree libtype,
5907 bool both_p, bool fallback_p, bool nonansi_p,
5908 tree fnattrs, bool implicit_p)
5910 tree decl;
5911 const char *libname;
5913 if (fntype == error_mark_node)
5914 return;
5916 gcc_assert ((!both_p && !fallback_p)
5917 || !strncmp (name, "__builtin_",
5918 strlen ("__builtin_")));
5920 libname = name + strlen ("__builtin_");
5921 decl = add_builtin_function (name, fntype, fncode, fnclass,
5922 (fallback_p ? libname : NULL),
5923 fnattrs);
5925 set_builtin_decl (fncode, decl, implicit_p);
5927 if (both_p
5928 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5929 && !(nonansi_p && flag_no_nonansi_builtin))
5930 add_builtin_function (libname, libtype, fncode, fnclass,
5931 NULL, fnattrs);
5934 /* Nonzero if the type T promotes to int. This is (nearly) the
5935 integral promotions defined in ISO C99 6.3.1.1/2. */
5937 bool
5938 c_promoting_integer_type_p (const_tree t)
5940 switch (TREE_CODE (t))
5942 case INTEGER_TYPE:
5943 return (TYPE_MAIN_VARIANT (t) == char_type_node
5944 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5945 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5946 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5947 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5948 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5950 case ENUMERAL_TYPE:
5951 /* ??? Technically all enumerations not larger than an int
5952 promote to an int. But this is used along code paths
5953 that only want to notice a size change. */
5954 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5956 case BOOLEAN_TYPE:
5957 return 1;
5959 default:
5960 return 0;
5964 /* Return 1 if PARMS specifies a fixed number of parameters
5965 and none of their types is affected by default promotions. */
5968 self_promoting_args_p (const_tree parms)
5970 const_tree t;
5971 for (t = parms; t; t = TREE_CHAIN (t))
5973 tree type = TREE_VALUE (t);
5975 if (type == error_mark_node)
5976 continue;
5978 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5979 return 0;
5981 if (type == 0)
5982 return 0;
5984 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5985 return 0;
5987 if (c_promoting_integer_type_p (type))
5988 return 0;
5990 return 1;
5993 /* Recursively remove any '*' or '&' operator from TYPE. */
5994 tree
5995 strip_pointer_operator (tree t)
5997 while (POINTER_TYPE_P (t))
5998 t = TREE_TYPE (t);
5999 return t;
6002 /* Recursively remove pointer or array type from TYPE. */
6003 tree
6004 strip_pointer_or_array_types (tree t)
6006 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6007 t = TREE_TYPE (t);
6008 return t;
6011 /* Used to compare case labels. K1 and K2 are actually tree nodes
6012 representing case labels, or NULL_TREE for a `default' label.
6013 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6014 K2, and 0 if K1 and K2 are equal. */
6017 case_compare (splay_tree_key k1, splay_tree_key k2)
6019 /* Consider a NULL key (such as arises with a `default' label) to be
6020 smaller than anything else. */
6021 if (!k1)
6022 return k2 ? -1 : 0;
6023 else if (!k2)
6024 return k1 ? 1 : 0;
6026 return tree_int_cst_compare ((tree) k1, (tree) k2);
6029 /* Process a case label, located at LOC, for the range LOW_VALUE
6030 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6031 then this case label is actually a `default' label. If only
6032 HIGH_VALUE is NULL_TREE, then case label was declared using the
6033 usual C/C++ syntax, rather than the GNU case range extension.
6034 CASES is a tree containing all the case ranges processed so far;
6035 COND is the condition for the switch-statement itself. Returns the
6036 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6037 is created. */
6039 tree
6040 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6041 tree low_value, tree high_value)
6043 tree type;
6044 tree label;
6045 tree case_label;
6046 splay_tree_node node;
6048 /* Create the LABEL_DECL itself. */
6049 label = create_artificial_label (loc);
6051 /* If there was an error processing the switch condition, bail now
6052 before we get more confused. */
6053 if (!cond || cond == error_mark_node)
6054 goto error_out;
6056 if ((low_value && TREE_TYPE (low_value)
6057 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6058 || (high_value && TREE_TYPE (high_value)
6059 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6061 error_at (loc, "pointers are not permitted as case values");
6062 goto error_out;
6065 /* Case ranges are a GNU extension. */
6066 if (high_value)
6067 pedwarn (loc, OPT_Wpedantic,
6068 "range expressions in switch statements are non-standard");
6070 type = TREE_TYPE (cond);
6071 if (low_value)
6073 low_value = check_case_value (loc, low_value);
6074 low_value = convert_and_check (loc, type, low_value);
6075 if (low_value == error_mark_node)
6076 goto error_out;
6078 if (high_value)
6080 high_value = check_case_value (loc, high_value);
6081 high_value = convert_and_check (loc, type, high_value);
6082 if (high_value == error_mark_node)
6083 goto error_out;
6086 if (low_value && high_value)
6088 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6089 really a case range, even though it was written that way.
6090 Remove the HIGH_VALUE to simplify later processing. */
6091 if (tree_int_cst_equal (low_value, high_value))
6092 high_value = NULL_TREE;
6093 else if (!tree_int_cst_lt (low_value, high_value))
6094 warning_at (loc, 0, "empty range specified");
6097 /* See if the case is in range of the type of the original testing
6098 expression. If both low_value and high_value are out of range,
6099 don't insert the case label and return NULL_TREE. */
6100 if (low_value
6101 && !check_case_bounds (loc, type, orig_type,
6102 &low_value, high_value ? &high_value : NULL))
6103 return NULL_TREE;
6105 /* Look up the LOW_VALUE in the table of case labels we already
6106 have. */
6107 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6108 /* If there was not an exact match, check for overlapping ranges.
6109 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6110 that's a `default' label and the only overlap is an exact match. */
6111 if (!node && (low_value || high_value))
6113 splay_tree_node low_bound;
6114 splay_tree_node high_bound;
6116 /* Even though there wasn't an exact match, there might be an
6117 overlap between this case range and another case range.
6118 Since we've (inductively) not allowed any overlapping case
6119 ranges, we simply need to find the greatest low case label
6120 that is smaller that LOW_VALUE, and the smallest low case
6121 label that is greater than LOW_VALUE. If there is an overlap
6122 it will occur in one of these two ranges. */
6123 low_bound = splay_tree_predecessor (cases,
6124 (splay_tree_key) low_value);
6125 high_bound = splay_tree_successor (cases,
6126 (splay_tree_key) low_value);
6128 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6129 the LOW_VALUE, so there is no need to check unless the
6130 LOW_BOUND is in fact itself a case range. */
6131 if (low_bound
6132 && CASE_HIGH ((tree) low_bound->value)
6133 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6134 low_value) >= 0)
6135 node = low_bound;
6136 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6137 range is bigger than the low end of the current range, so we
6138 are only interested if the current range is a real range, and
6139 not an ordinary case label. */
6140 else if (high_bound
6141 && high_value
6142 && (tree_int_cst_compare ((tree) high_bound->key,
6143 high_value)
6144 <= 0))
6145 node = high_bound;
6147 /* If there was an overlap, issue an error. */
6148 if (node)
6150 tree duplicate = CASE_LABEL ((tree) node->value);
6152 if (high_value)
6154 error_at (loc, "duplicate (or overlapping) case value");
6155 error_at (DECL_SOURCE_LOCATION (duplicate),
6156 "this is the first entry overlapping that value");
6158 else if (low_value)
6160 error_at (loc, "duplicate case value") ;
6161 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6163 else
6165 error_at (loc, "multiple default labels in one switch");
6166 error_at (DECL_SOURCE_LOCATION (duplicate),
6167 "this is the first default label");
6169 goto error_out;
6172 /* Add a CASE_LABEL to the statement-tree. */
6173 case_label = add_stmt (build_case_label (low_value, high_value, label));
6174 /* Register this case label in the splay tree. */
6175 splay_tree_insert (cases,
6176 (splay_tree_key) low_value,
6177 (splay_tree_value) case_label);
6179 return case_label;
6181 error_out:
6182 /* Add a label so that the back-end doesn't think that the beginning of
6183 the switch is unreachable. Note that we do not add a case label, as
6184 that just leads to duplicates and thence to failure later on. */
6185 if (!cases->root)
6187 tree t = create_artificial_label (loc);
6188 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6190 return error_mark_node;
6193 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6194 Used to verify that case values match up with enumerator values. */
6196 static void
6197 match_case_to_enum_1 (tree key, tree type, tree label)
6199 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6201 if (tree_fits_uhwi_p (key))
6202 print_dec (key, buf, UNSIGNED);
6203 else if (tree_fits_shwi_p (key))
6204 print_dec (key, buf, SIGNED);
6205 else
6206 print_hex (key, buf);
6208 if (TYPE_NAME (type) == 0)
6209 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6210 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6211 "case value %qs not in enumerated type",
6212 buf);
6213 else
6214 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6215 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6216 "case value %qs not in enumerated type %qT",
6217 buf, type);
6220 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6221 Used to verify that case values match up with enumerator values. */
6223 static int
6224 match_case_to_enum (splay_tree_node node, void *data)
6226 tree label = (tree) node->value;
6227 tree type = (tree) data;
6229 /* Skip default case. */
6230 if (!CASE_LOW (label))
6231 return 0;
6233 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6234 when we did our enum->case scan. Reset our scratch bit after. */
6235 if (!CASE_LOW_SEEN (label))
6236 match_case_to_enum_1 (CASE_LOW (label), type, label);
6237 else
6238 CASE_LOW_SEEN (label) = 0;
6240 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6241 not set, that means that CASE_HIGH did not appear when we did our
6242 enum->case scan. Reset our scratch bit after. */
6243 if (CASE_HIGH (label))
6245 if (!CASE_HIGH_SEEN (label))
6246 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6247 else
6248 CASE_HIGH_SEEN (label) = 0;
6251 return 0;
6254 /* Handle -Wswitch*. Called from the front end after parsing the
6255 switch construct. */
6256 /* ??? Should probably be somewhere generic, since other languages
6257 besides C and C++ would want this. At the moment, however, C/C++
6258 are the only tree-ssa languages that support enumerations at all,
6259 so the point is moot. */
6261 void
6262 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6263 tree type, tree cond)
6265 splay_tree_node default_node;
6266 splay_tree_node node;
6267 tree chain;
6269 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6270 return;
6272 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6273 if (!default_node)
6274 warning_at (switch_location, OPT_Wswitch_default,
6275 "switch missing default case");
6277 /* From here on, we only care about about enumerated types. */
6278 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6279 return;
6281 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6282 if (!warn_switch_enum && !warn_switch)
6283 return;
6285 /* Check the cases. Warn about case values which are not members of
6286 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6287 there is no default case, check that exactly all enumeration
6288 literals are covered by the cases. */
6290 /* Clearing COND if it is not an integer constant simplifies
6291 the tests inside the loop below. */
6292 if (TREE_CODE (cond) != INTEGER_CST)
6293 cond = NULL_TREE;
6295 /* The time complexity here is O(N*lg(N)) worst case, but for the
6296 common case of monotonically increasing enumerators, it is
6297 O(N), since the nature of the splay tree will keep the next
6298 element adjacent to the root at all times. */
6300 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6302 tree value = TREE_VALUE (chain);
6303 if (TREE_CODE (value) == CONST_DECL)
6304 value = DECL_INITIAL (value);
6305 node = splay_tree_lookup (cases, (splay_tree_key) value);
6306 if (node)
6308 /* Mark the CASE_LOW part of the case entry as seen. */
6309 tree label = (tree) node->value;
6310 CASE_LOW_SEEN (label) = 1;
6311 continue;
6314 /* Even though there wasn't an exact match, there might be a
6315 case range which includes the enumerator's value. */
6316 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6317 if (node && CASE_HIGH ((tree) node->value))
6319 tree label = (tree) node->value;
6320 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6321 if (cmp >= 0)
6323 /* If we match the upper bound exactly, mark the CASE_HIGH
6324 part of the case entry as seen. */
6325 if (cmp == 0)
6326 CASE_HIGH_SEEN (label) = 1;
6327 continue;
6331 /* We've now determined that this enumerated literal isn't
6332 handled by the case labels of the switch statement. */
6334 /* If the switch expression is a constant, we only really care
6335 about whether that constant is handled by the switch. */
6336 if (cond && tree_int_cst_compare (cond, value))
6337 continue;
6339 /* If there is a default_node, the only relevant option is
6340 Wswitch-enum. Otherwise, if both are enabled then we prefer
6341 to warn using -Wswitch because -Wswitch is enabled by -Wall
6342 while -Wswitch-enum is explicit. */
6343 warning_at (switch_location,
6344 (default_node || !warn_switch
6345 ? OPT_Wswitch_enum
6346 : OPT_Wswitch),
6347 "enumeration value %qE not handled in switch",
6348 TREE_PURPOSE (chain));
6351 /* Warn if there are case expressions that don't correspond to
6352 enumerators. This can occur since C and C++ don't enforce
6353 type-checking of assignments to enumeration variables.
6355 The time complexity here is now always O(N) worst case, since
6356 we should have marked both the lower bound and upper bound of
6357 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6358 above. This scan also resets those fields. */
6360 splay_tree_foreach (cases, match_case_to_enum, type);
6363 /* Finish an expression taking the address of LABEL (an
6364 IDENTIFIER_NODE). Returns an expression for the address.
6366 LOC is the location for the expression returned. */
6368 tree
6369 finish_label_address_expr (tree label, location_t loc)
6371 tree result;
6373 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6375 if (label == error_mark_node)
6376 return error_mark_node;
6378 label = lookup_label (label);
6379 if (label == NULL_TREE)
6380 result = null_pointer_node;
6381 else
6383 TREE_USED (label) = 1;
6384 result = build1 (ADDR_EXPR, ptr_type_node, label);
6385 /* The current function is not necessarily uninlinable.
6386 Computed gotos are incompatible with inlining, but the value
6387 here could be used only in a diagnostic, for example. */
6388 protected_set_expr_location (result, loc);
6391 return result;
6395 /* Given a boolean expression ARG, return a tree representing an increment
6396 or decrement (as indicated by CODE) of ARG. The front end must check for
6397 invalid cases (e.g., decrement in C++). */
6398 tree
6399 boolean_increment (enum tree_code code, tree arg)
6401 tree val;
6402 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6404 arg = stabilize_reference (arg);
6405 switch (code)
6407 case PREINCREMENT_EXPR:
6408 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6409 break;
6410 case POSTINCREMENT_EXPR:
6411 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6412 arg = save_expr (arg);
6413 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6414 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6415 break;
6416 case PREDECREMENT_EXPR:
6417 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6418 invert_truthvalue_loc (input_location, arg));
6419 break;
6420 case POSTDECREMENT_EXPR:
6421 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6422 invert_truthvalue_loc (input_location, arg));
6423 arg = save_expr (arg);
6424 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6425 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6426 break;
6427 default:
6428 gcc_unreachable ();
6430 TREE_SIDE_EFFECTS (val) = 1;
6431 return val;
6434 /* Built-in macros for stddef.h and stdint.h, that require macros
6435 defined in this file. */
6436 void
6437 c_stddef_cpp_builtins(void)
6439 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6440 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6441 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6442 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6443 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6444 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6445 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6446 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6447 if (SIG_ATOMIC_TYPE)
6448 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6449 if (INT8_TYPE)
6450 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6451 if (INT16_TYPE)
6452 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6453 if (INT32_TYPE)
6454 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6455 if (INT64_TYPE)
6456 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6457 if (UINT8_TYPE)
6458 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6459 if (UINT16_TYPE)
6460 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6461 if (UINT32_TYPE)
6462 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6463 if (UINT64_TYPE)
6464 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6465 if (INT_LEAST8_TYPE)
6466 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6467 if (INT_LEAST16_TYPE)
6468 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6469 if (INT_LEAST32_TYPE)
6470 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6471 if (INT_LEAST64_TYPE)
6472 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6473 if (UINT_LEAST8_TYPE)
6474 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6475 if (UINT_LEAST16_TYPE)
6476 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6477 if (UINT_LEAST32_TYPE)
6478 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6479 if (UINT_LEAST64_TYPE)
6480 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6481 if (INT_FAST8_TYPE)
6482 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6483 if (INT_FAST16_TYPE)
6484 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6485 if (INT_FAST32_TYPE)
6486 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6487 if (INT_FAST64_TYPE)
6488 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6489 if (UINT_FAST8_TYPE)
6490 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6491 if (UINT_FAST16_TYPE)
6492 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6493 if (UINT_FAST32_TYPE)
6494 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6495 if (UINT_FAST64_TYPE)
6496 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6497 if (INTPTR_TYPE)
6498 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6499 if (UINTPTR_TYPE)
6500 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6503 static void
6504 c_init_attributes (void)
6506 /* Fill in the built_in_attributes array. */
6507 #define DEF_ATTR_NULL_TREE(ENUM) \
6508 built_in_attributes[(int) ENUM] = NULL_TREE;
6509 #define DEF_ATTR_INT(ENUM, VALUE) \
6510 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6511 #define DEF_ATTR_STRING(ENUM, VALUE) \
6512 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6513 #define DEF_ATTR_IDENT(ENUM, STRING) \
6514 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6515 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6516 built_in_attributes[(int) ENUM] \
6517 = tree_cons (built_in_attributes[(int) PURPOSE], \
6518 built_in_attributes[(int) VALUE], \
6519 built_in_attributes[(int) CHAIN]);
6520 #include "builtin-attrs.def"
6521 #undef DEF_ATTR_NULL_TREE
6522 #undef DEF_ATTR_INT
6523 #undef DEF_ATTR_IDENT
6524 #undef DEF_ATTR_TREE_LIST
6527 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6528 identifier as an argument, so the front end shouldn't look it up. */
6530 bool
6531 attribute_takes_identifier_p (const_tree attr_id)
6533 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6534 if (spec == NULL)
6535 /* Unknown attribute that we'll end up ignoring, return true so we
6536 don't complain about an identifier argument. */
6537 return true;
6538 else if (!strcmp ("mode", spec->name)
6539 || !strcmp ("format", spec->name)
6540 || !strcmp ("cleanup", spec->name))
6541 return true;
6542 else
6543 return targetm.attribute_takes_identifier_p (attr_id);
6546 /* Attribute handlers common to C front ends. */
6548 /* Handle a "packed" attribute; arguments as in
6549 struct attribute_spec.handler. */
6551 static tree
6552 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6553 int flags, bool *no_add_attrs)
6555 if (TYPE_P (*node))
6557 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6558 *node = build_variant_type_copy (*node);
6559 TYPE_PACKED (*node) = 1;
6561 else if (TREE_CODE (*node) == FIELD_DECL)
6563 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6564 /* Still pack bitfields. */
6565 && ! DECL_INITIAL (*node))
6566 warning (OPT_Wattributes,
6567 "%qE attribute ignored for field of type %qT",
6568 name, TREE_TYPE (*node));
6569 else
6570 DECL_PACKED (*node) = 1;
6572 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6573 used for DECL_REGISTER. It wouldn't mean anything anyway.
6574 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6575 that changes what the typedef is typing. */
6576 else
6578 warning (OPT_Wattributes, "%qE attribute ignored", name);
6579 *no_add_attrs = true;
6582 return NULL_TREE;
6585 /* Handle a "nocommon" attribute; arguments as in
6586 struct attribute_spec.handler. */
6588 static tree
6589 handle_nocommon_attribute (tree *node, tree name,
6590 tree ARG_UNUSED (args),
6591 int ARG_UNUSED (flags), bool *no_add_attrs)
6593 if (TREE_CODE (*node) == VAR_DECL)
6594 DECL_COMMON (*node) = 0;
6595 else
6597 warning (OPT_Wattributes, "%qE attribute ignored", name);
6598 *no_add_attrs = true;
6601 return NULL_TREE;
6604 /* Handle a "common" attribute; arguments as in
6605 struct attribute_spec.handler. */
6607 static tree
6608 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6609 int ARG_UNUSED (flags), bool *no_add_attrs)
6611 if (TREE_CODE (*node) == VAR_DECL)
6612 DECL_COMMON (*node) = 1;
6613 else
6615 warning (OPT_Wattributes, "%qE attribute ignored", name);
6616 *no_add_attrs = true;
6619 return NULL_TREE;
6622 /* Handle a "noreturn" attribute; arguments as in
6623 struct attribute_spec.handler. */
6625 static tree
6626 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6627 int ARG_UNUSED (flags), bool *no_add_attrs)
6629 tree type = TREE_TYPE (*node);
6631 /* See FIXME comment in c_common_attribute_table. */
6632 if (TREE_CODE (*node) == FUNCTION_DECL
6633 || objc_method_decl (TREE_CODE (*node)))
6634 TREE_THIS_VOLATILE (*node) = 1;
6635 else if (TREE_CODE (type) == POINTER_TYPE
6636 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6637 TREE_TYPE (*node)
6638 = (build_qualified_type
6639 (build_pointer_type
6640 (build_type_variant (TREE_TYPE (type),
6641 TYPE_READONLY (TREE_TYPE (type)), 1)),
6642 TYPE_QUALS (type)));
6643 else
6645 warning (OPT_Wattributes, "%qE attribute ignored", name);
6646 *no_add_attrs = true;
6649 return NULL_TREE;
6652 /* Handle a "hot" and attribute; arguments as in
6653 struct attribute_spec.handler. */
6655 static tree
6656 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6657 int ARG_UNUSED (flags), bool *no_add_attrs)
6659 if (TREE_CODE (*node) == FUNCTION_DECL
6660 || TREE_CODE (*node) == LABEL_DECL)
6662 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6664 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6665 "with attribute %qs", name, "cold");
6666 *no_add_attrs = true;
6668 /* Most of the rest of the hot processing is done later with
6669 lookup_attribute. */
6671 else
6673 warning (OPT_Wattributes, "%qE attribute ignored", name);
6674 *no_add_attrs = true;
6677 return NULL_TREE;
6680 /* Handle a "cold" and attribute; arguments as in
6681 struct attribute_spec.handler. */
6683 static tree
6684 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6685 int ARG_UNUSED (flags), bool *no_add_attrs)
6687 if (TREE_CODE (*node) == FUNCTION_DECL
6688 || TREE_CODE (*node) == LABEL_DECL)
6690 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6692 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6693 "with attribute %qs", name, "hot");
6694 *no_add_attrs = true;
6696 /* Most of the rest of the cold processing is done later with
6697 lookup_attribute. */
6699 else
6701 warning (OPT_Wattributes, "%qE attribute ignored", name);
6702 *no_add_attrs = true;
6705 return NULL_TREE;
6708 /* Handle a "no_sanitize_address" attribute; arguments as in
6709 struct attribute_spec.handler. */
6711 static tree
6712 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6713 bool *no_add_attrs)
6715 if (TREE_CODE (*node) != FUNCTION_DECL)
6717 warning (OPT_Wattributes, "%qE attribute ignored", name);
6718 *no_add_attrs = true;
6721 return NULL_TREE;
6724 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6725 struct attribute_spec.handler. */
6727 static tree
6728 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6729 bool *no_add_attrs)
6731 if (TREE_CODE (*node) != FUNCTION_DECL)
6732 warning (OPT_Wattributes, "%qE attribute ignored", name);
6733 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6734 DECL_ATTRIBUTES (*node)
6735 = tree_cons (get_identifier ("no_sanitize_address"),
6736 NULL_TREE, DECL_ATTRIBUTES (*node));
6737 *no_add_attrs = true;
6738 return NULL_TREE;
6741 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6742 struct attribute_spec.handler. */
6744 static tree
6745 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6746 bool *no_add_attrs)
6748 if (TREE_CODE (*node) != FUNCTION_DECL)
6750 warning (OPT_Wattributes, "%qE attribute ignored", name);
6751 *no_add_attrs = true;
6754 return NULL_TREE;
6757 /* Handle a "noinline" attribute; arguments as in
6758 struct attribute_spec.handler. */
6760 static tree
6761 handle_noinline_attribute (tree *node, tree name,
6762 tree ARG_UNUSED (args),
6763 int ARG_UNUSED (flags), bool *no_add_attrs)
6765 if (TREE_CODE (*node) == FUNCTION_DECL)
6767 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6769 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6770 "with attribute %qs", name, "always_inline");
6771 *no_add_attrs = true;
6773 else
6774 DECL_UNINLINABLE (*node) = 1;
6776 else
6778 warning (OPT_Wattributes, "%qE attribute ignored", name);
6779 *no_add_attrs = true;
6782 return NULL_TREE;
6785 /* Handle a "noclone" attribute; arguments as in
6786 struct attribute_spec.handler. */
6788 static tree
6789 handle_noclone_attribute (tree *node, tree name,
6790 tree ARG_UNUSED (args),
6791 int ARG_UNUSED (flags), bool *no_add_attrs)
6793 if (TREE_CODE (*node) != FUNCTION_DECL)
6795 warning (OPT_Wattributes, "%qE attribute ignored", name);
6796 *no_add_attrs = true;
6799 return NULL_TREE;
6802 /* Handle a "hsa" attribute; arguments as in
6803 struct attribute_spec.handler. */
6805 static tree
6806 handle_hsa_attribute (tree *node, tree name,
6807 tree ARG_UNUSED (args),
6808 int ARG_UNUSED (flags), bool *no_add_attrs)
6810 if (TREE_CODE (*node) != FUNCTION_DECL)
6812 warning (OPT_Wattributes, "%qE attribute ignored", name);
6813 *no_add_attrs = true;
6814 return NULL_TREE;
6817 TREE_USED (*node) = 1;
6818 DECL_UNINLINABLE (*node) = 1;
6819 if (strcmp ("hsakernel", IDENTIFIER_POINTER (name)) == 0
6820 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (*node))))
6821 == void_type_node))
6822 warning (OPT_Wattributes, "%qE attribute on a function with fixed number "
6823 "of argument makes no sense", name);
6825 return NULL_TREE;
6828 /* Handle a "always_inline" attribute; arguments as in
6829 struct attribute_spec.handler. */
6831 static tree
6832 handle_always_inline_attribute (tree *node, tree name,
6833 tree ARG_UNUSED (args),
6834 int ARG_UNUSED (flags),
6835 bool *no_add_attrs)
6837 if (TREE_CODE (*node) == FUNCTION_DECL)
6839 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6841 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6842 "with %qs attribute", name, "noinline");
6843 *no_add_attrs = true;
6845 else
6846 /* Set the attribute and mark it for disregarding inline
6847 limits. */
6848 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6850 else
6852 warning (OPT_Wattributes, "%qE attribute ignored", name);
6853 *no_add_attrs = true;
6856 return NULL_TREE;
6859 /* Handle a "gnu_inline" attribute; arguments as in
6860 struct attribute_spec.handler. */
6862 static tree
6863 handle_gnu_inline_attribute (tree *node, tree name,
6864 tree ARG_UNUSED (args),
6865 int ARG_UNUSED (flags),
6866 bool *no_add_attrs)
6868 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6870 /* Do nothing else, just set the attribute. We'll get at
6871 it later with lookup_attribute. */
6873 else
6875 warning (OPT_Wattributes, "%qE attribute ignored", name);
6876 *no_add_attrs = true;
6879 return NULL_TREE;
6882 /* Handle a "leaf" attribute; arguments as in
6883 struct attribute_spec.handler. */
6885 static tree
6886 handle_leaf_attribute (tree *node, tree name,
6887 tree ARG_UNUSED (args),
6888 int ARG_UNUSED (flags), bool *no_add_attrs)
6890 if (TREE_CODE (*node) != FUNCTION_DECL)
6892 warning (OPT_Wattributes, "%qE attribute ignored", name);
6893 *no_add_attrs = true;
6895 if (!TREE_PUBLIC (*node))
6897 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6898 *no_add_attrs = true;
6901 return NULL_TREE;
6904 /* Handle an "artificial" attribute; arguments as in
6905 struct attribute_spec.handler. */
6907 static tree
6908 handle_artificial_attribute (tree *node, tree name,
6909 tree ARG_UNUSED (args),
6910 int ARG_UNUSED (flags),
6911 bool *no_add_attrs)
6913 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6915 /* Do nothing else, just set the attribute. We'll get at
6916 it later with lookup_attribute. */
6918 else
6920 warning (OPT_Wattributes, "%qE attribute ignored", name);
6921 *no_add_attrs = true;
6924 return NULL_TREE;
6927 /* Handle a "flatten" attribute; arguments as in
6928 struct attribute_spec.handler. */
6930 static tree
6931 handle_flatten_attribute (tree *node, tree name,
6932 tree args ATTRIBUTE_UNUSED,
6933 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6935 if (TREE_CODE (*node) == FUNCTION_DECL)
6936 /* Do nothing else, just set the attribute. We'll get at
6937 it later with lookup_attribute. */
6939 else
6941 warning (OPT_Wattributes, "%qE attribute ignored", name);
6942 *no_add_attrs = true;
6945 return NULL_TREE;
6948 /* Handle a "warning" or "error" attribute; arguments as in
6949 struct attribute_spec.handler. */
6951 static tree
6952 handle_error_attribute (tree *node, tree name, tree args,
6953 int ARG_UNUSED (flags), bool *no_add_attrs)
6955 if (TREE_CODE (*node) == FUNCTION_DECL
6956 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6957 /* Do nothing else, just set the attribute. We'll get at
6958 it later with lookup_attribute. */
6960 else
6962 warning (OPT_Wattributes, "%qE attribute ignored", name);
6963 *no_add_attrs = true;
6966 return NULL_TREE;
6969 /* Handle a "used" attribute; arguments as in
6970 struct attribute_spec.handler. */
6972 static tree
6973 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6974 int ARG_UNUSED (flags), bool *no_add_attrs)
6976 tree node = *pnode;
6978 if (TREE_CODE (node) == FUNCTION_DECL
6979 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6980 || (TREE_CODE (node) == TYPE_DECL))
6982 TREE_USED (node) = 1;
6983 DECL_PRESERVE_P (node) = 1;
6984 if (TREE_CODE (node) == VAR_DECL)
6985 DECL_READ_P (node) = 1;
6987 else
6989 warning (OPT_Wattributes, "%qE attribute ignored", name);
6990 *no_add_attrs = true;
6993 return NULL_TREE;
6996 /* Handle a "unused" attribute; arguments as in
6997 struct attribute_spec.handler. */
6999 static tree
7000 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7001 int flags, bool *no_add_attrs)
7003 if (DECL_P (*node))
7005 tree decl = *node;
7007 if (TREE_CODE (decl) == PARM_DECL
7008 || TREE_CODE (decl) == VAR_DECL
7009 || TREE_CODE (decl) == FUNCTION_DECL
7010 || TREE_CODE (decl) == LABEL_DECL
7011 || TREE_CODE (decl) == TYPE_DECL)
7013 TREE_USED (decl) = 1;
7014 if (TREE_CODE (decl) == VAR_DECL
7015 || TREE_CODE (decl) == PARM_DECL)
7016 DECL_READ_P (decl) = 1;
7018 else
7020 warning (OPT_Wattributes, "%qE attribute ignored", name);
7021 *no_add_attrs = true;
7024 else
7026 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7027 *node = build_variant_type_copy (*node);
7028 TREE_USED (*node) = 1;
7031 return NULL_TREE;
7034 /* Handle a "externally_visible" attribute; arguments as in
7035 struct attribute_spec.handler. */
7037 static tree
7038 handle_externally_visible_attribute (tree *pnode, tree name,
7039 tree ARG_UNUSED (args),
7040 int ARG_UNUSED (flags),
7041 bool *no_add_attrs)
7043 tree node = *pnode;
7045 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7047 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7048 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7050 warning (OPT_Wattributes,
7051 "%qE attribute have effect only on public objects", name);
7052 *no_add_attrs = true;
7055 else
7057 warning (OPT_Wattributes, "%qE attribute ignored", name);
7058 *no_add_attrs = true;
7061 return NULL_TREE;
7064 /* Handle the "no_reorder" attribute. Arguments as in
7065 struct attribute_spec.handler. */
7067 static tree
7068 handle_no_reorder_attribute (tree *pnode,
7069 tree name,
7070 tree,
7071 int,
7072 bool *no_add_attrs)
7074 tree node = *pnode;
7076 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7077 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7079 warning (OPT_Wattributes,
7080 "%qE attribute only affects top level objects",
7081 name);
7082 *no_add_attrs = true;
7085 return NULL_TREE;
7088 /* Handle a "const" attribute; arguments as in
7089 struct attribute_spec.handler. */
7091 static tree
7092 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7093 int ARG_UNUSED (flags), bool *no_add_attrs)
7095 tree type = TREE_TYPE (*node);
7097 /* See FIXME comment on noreturn in c_common_attribute_table. */
7098 if (TREE_CODE (*node) == FUNCTION_DECL)
7099 TREE_READONLY (*node) = 1;
7100 else if (TREE_CODE (type) == POINTER_TYPE
7101 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7102 TREE_TYPE (*node)
7103 = (build_qualified_type
7104 (build_pointer_type
7105 (build_type_variant (TREE_TYPE (type), 1,
7106 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7107 TYPE_QUALS (type)));
7108 else
7110 warning (OPT_Wattributes, "%qE attribute ignored", name);
7111 *no_add_attrs = true;
7114 return NULL_TREE;
7117 /* Handle a "transparent_union" attribute; arguments as in
7118 struct attribute_spec.handler. */
7120 static tree
7121 handle_transparent_union_attribute (tree *node, tree name,
7122 tree ARG_UNUSED (args), int flags,
7123 bool *no_add_attrs)
7125 tree type;
7127 *no_add_attrs = true;
7130 if (TREE_CODE (*node) == TYPE_DECL
7131 && ! (flags & ATTR_FLAG_CXX11))
7132 node = &TREE_TYPE (*node);
7133 type = *node;
7135 if (TREE_CODE (type) == UNION_TYPE)
7137 /* Make sure that the first field will work for a transparent union.
7138 If the type isn't complete yet, leave the check to the code in
7139 finish_struct. */
7140 if (TYPE_SIZE (type))
7142 tree first = first_field (type);
7143 if (first == NULL_TREE
7144 || DECL_ARTIFICIAL (first)
7145 || TYPE_MODE (type) != DECL_MODE (first))
7146 goto ignored;
7149 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7151 /* If the type isn't complete yet, setting the flag
7152 on a variant wouldn't ever be checked. */
7153 if (!TYPE_SIZE (type))
7154 goto ignored;
7156 /* build_duplicate_type doesn't work for C++. */
7157 if (c_dialect_cxx ())
7158 goto ignored;
7160 /* A type variant isn't good enough, since we don't a cast
7161 to such a type removed as a no-op. */
7162 *node = type = build_duplicate_type (type);
7165 TYPE_TRANSPARENT_AGGR (type) = 1;
7166 return NULL_TREE;
7169 ignored:
7170 warning (OPT_Wattributes, "%qE attribute ignored", name);
7171 return NULL_TREE;
7174 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7175 get the requested priority for a constructor or destructor,
7176 possibly issuing diagnostics for invalid or reserved
7177 priorities. */
7179 static priority_type
7180 get_priority (tree args, bool is_destructor)
7182 HOST_WIDE_INT pri;
7183 tree arg;
7185 if (!args)
7186 return DEFAULT_INIT_PRIORITY;
7188 if (!SUPPORTS_INIT_PRIORITY)
7190 if (is_destructor)
7191 error ("destructor priorities are not supported");
7192 else
7193 error ("constructor priorities are not supported");
7194 return DEFAULT_INIT_PRIORITY;
7197 arg = TREE_VALUE (args);
7198 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7199 goto invalid;
7200 if (arg == error_mark_node)
7201 return DEFAULT_INIT_PRIORITY;
7202 arg = default_conversion (arg);
7203 if (!tree_fits_shwi_p (arg)
7204 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7205 goto invalid;
7207 pri = tree_to_shwi (arg);
7208 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7209 goto invalid;
7211 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7213 if (is_destructor)
7214 warning (0,
7215 "destructor priorities from 0 to %d are reserved "
7216 "for the implementation",
7217 MAX_RESERVED_INIT_PRIORITY);
7218 else
7219 warning (0,
7220 "constructor priorities from 0 to %d are reserved "
7221 "for the implementation",
7222 MAX_RESERVED_INIT_PRIORITY);
7224 return pri;
7226 invalid:
7227 if (is_destructor)
7228 error ("destructor priorities must be integers from 0 to %d inclusive",
7229 MAX_INIT_PRIORITY);
7230 else
7231 error ("constructor priorities must be integers from 0 to %d inclusive",
7232 MAX_INIT_PRIORITY);
7233 return DEFAULT_INIT_PRIORITY;
7236 /* Handle a "constructor" attribute; arguments as in
7237 struct attribute_spec.handler. */
7239 static tree
7240 handle_constructor_attribute (tree *node, tree name, tree args,
7241 int ARG_UNUSED (flags),
7242 bool *no_add_attrs)
7244 tree decl = *node;
7245 tree type = TREE_TYPE (decl);
7247 if (TREE_CODE (decl) == FUNCTION_DECL
7248 && TREE_CODE (type) == FUNCTION_TYPE
7249 && decl_function_context (decl) == 0)
7251 priority_type priority;
7252 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7253 priority = get_priority (args, /*is_destructor=*/false);
7254 SET_DECL_INIT_PRIORITY (decl, priority);
7255 TREE_USED (decl) = 1;
7257 else
7259 warning (OPT_Wattributes, "%qE attribute ignored", name);
7260 *no_add_attrs = true;
7263 return NULL_TREE;
7266 /* Handle a "destructor" attribute; arguments as in
7267 struct attribute_spec.handler. */
7269 static tree
7270 handle_destructor_attribute (tree *node, tree name, tree args,
7271 int ARG_UNUSED (flags),
7272 bool *no_add_attrs)
7274 tree decl = *node;
7275 tree type = TREE_TYPE (decl);
7277 if (TREE_CODE (decl) == FUNCTION_DECL
7278 && TREE_CODE (type) == FUNCTION_TYPE
7279 && decl_function_context (decl) == 0)
7281 priority_type priority;
7282 DECL_STATIC_DESTRUCTOR (decl) = 1;
7283 priority = get_priority (args, /*is_destructor=*/true);
7284 SET_DECL_FINI_PRIORITY (decl, priority);
7285 TREE_USED (decl) = 1;
7287 else
7289 warning (OPT_Wattributes, "%qE attribute ignored", name);
7290 *no_add_attrs = true;
7293 return NULL_TREE;
7296 /* Nonzero if the mode is a valid vector mode for this architecture.
7297 This returns nonzero even if there is no hardware support for the
7298 vector mode, but we can emulate with narrower modes. */
7300 static int
7301 vector_mode_valid_p (machine_mode mode)
7303 enum mode_class mclass = GET_MODE_CLASS (mode);
7304 machine_mode innermode;
7306 /* Doh! What's going on? */
7307 if (mclass != MODE_VECTOR_INT
7308 && mclass != MODE_VECTOR_FLOAT
7309 && mclass != MODE_VECTOR_FRACT
7310 && mclass != MODE_VECTOR_UFRACT
7311 && mclass != MODE_VECTOR_ACCUM
7312 && mclass != MODE_VECTOR_UACCUM)
7313 return 0;
7315 /* Hardware support. Woo hoo! */
7316 if (targetm.vector_mode_supported_p (mode))
7317 return 1;
7319 innermode = GET_MODE_INNER (mode);
7321 /* We should probably return 1 if requesting V4DI and we have no DI,
7322 but we have V2DI, but this is probably very unlikely. */
7324 /* If we have support for the inner mode, we can safely emulate it.
7325 We may not have V2DI, but me can emulate with a pair of DIs. */
7326 return targetm.scalar_mode_supported_p (innermode);
7330 /* Handle a "mode" attribute; arguments as in
7331 struct attribute_spec.handler. */
7333 static tree
7334 handle_mode_attribute (tree *node, tree name, tree args,
7335 int ARG_UNUSED (flags), bool *no_add_attrs)
7337 tree type = *node;
7338 tree ident = TREE_VALUE (args);
7340 *no_add_attrs = true;
7342 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7343 warning (OPT_Wattributes, "%qE attribute ignored", name);
7344 else
7346 int j;
7347 const char *p = IDENTIFIER_POINTER (ident);
7348 int len = strlen (p);
7349 machine_mode mode = VOIDmode;
7350 tree typefm;
7351 bool valid_mode;
7353 if (len > 4 && p[0] == '_' && p[1] == '_'
7354 && p[len - 1] == '_' && p[len - 2] == '_')
7356 char *newp = (char *) alloca (len - 1);
7358 strcpy (newp, &p[2]);
7359 newp[len - 4] = '\0';
7360 p = newp;
7363 /* Change this type to have a type with the specified mode.
7364 First check for the special modes. */
7365 if (!strcmp (p, "byte"))
7366 mode = byte_mode;
7367 else if (!strcmp (p, "word"))
7368 mode = word_mode;
7369 else if (!strcmp (p, "pointer"))
7370 mode = ptr_mode;
7371 else if (!strcmp (p, "libgcc_cmp_return"))
7372 mode = targetm.libgcc_cmp_return_mode ();
7373 else if (!strcmp (p, "libgcc_shift_count"))
7374 mode = targetm.libgcc_shift_count_mode ();
7375 else if (!strcmp (p, "unwind_word"))
7376 mode = targetm.unwind_word_mode ();
7377 else
7378 for (j = 0; j < NUM_MACHINE_MODES; j++)
7379 if (!strcmp (p, GET_MODE_NAME (j)))
7381 mode = (machine_mode) j;
7382 break;
7385 if (mode == VOIDmode)
7387 error ("unknown machine mode %qE", ident);
7388 return NULL_TREE;
7391 valid_mode = false;
7392 switch (GET_MODE_CLASS (mode))
7394 case MODE_INT:
7395 case MODE_PARTIAL_INT:
7396 case MODE_FLOAT:
7397 case MODE_DECIMAL_FLOAT:
7398 case MODE_FRACT:
7399 case MODE_UFRACT:
7400 case MODE_ACCUM:
7401 case MODE_UACCUM:
7402 valid_mode = targetm.scalar_mode_supported_p (mode);
7403 break;
7405 case MODE_COMPLEX_INT:
7406 case MODE_COMPLEX_FLOAT:
7407 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7408 break;
7410 case MODE_VECTOR_INT:
7411 case MODE_VECTOR_FLOAT:
7412 case MODE_VECTOR_FRACT:
7413 case MODE_VECTOR_UFRACT:
7414 case MODE_VECTOR_ACCUM:
7415 case MODE_VECTOR_UACCUM:
7416 warning (OPT_Wattributes, "specifying vector types with "
7417 "__attribute__ ((mode)) is deprecated");
7418 warning (OPT_Wattributes,
7419 "use __attribute__ ((vector_size)) instead");
7420 valid_mode = vector_mode_valid_p (mode);
7421 break;
7423 default:
7424 break;
7426 if (!valid_mode)
7428 error ("unable to emulate %qs", p);
7429 return NULL_TREE;
7432 if (POINTER_TYPE_P (type))
7434 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7435 tree (*fn)(tree, machine_mode, bool);
7437 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7439 error ("invalid pointer mode %qs", p);
7440 return NULL_TREE;
7443 if (TREE_CODE (type) == POINTER_TYPE)
7444 fn = build_pointer_type_for_mode;
7445 else
7446 fn = build_reference_type_for_mode;
7447 typefm = fn (TREE_TYPE (type), mode, false);
7449 else
7451 /* For fixed-point modes, we need to test if the signness of type
7452 and the machine mode are consistent. */
7453 if (ALL_FIXED_POINT_MODE_P (mode)
7454 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7456 error ("signedness of type and machine mode %qs don%'t match", p);
7457 return NULL_TREE;
7459 /* For fixed-point modes, we need to pass saturating info. */
7460 typefm = lang_hooks.types.type_for_mode (mode,
7461 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7462 : TYPE_UNSIGNED (type));
7465 if (typefm == NULL_TREE)
7467 error ("no data type for mode %qs", p);
7468 return NULL_TREE;
7470 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7472 /* For enumeral types, copy the precision from the integer
7473 type returned above. If not an INTEGER_TYPE, we can't use
7474 this mode for this type. */
7475 if (TREE_CODE (typefm) != INTEGER_TYPE)
7477 error ("cannot use mode %qs for enumeral types", p);
7478 return NULL_TREE;
7481 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7483 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7484 typefm = type;
7486 else
7488 /* We cannot build a type variant, as there's code that assumes
7489 that TYPE_MAIN_VARIANT has the same mode. This includes the
7490 debug generators. Instead, create a subrange type. This
7491 results in all of the enumeral values being emitted only once
7492 in the original, and the subtype gets them by reference. */
7493 if (TYPE_UNSIGNED (type))
7494 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7495 else
7496 typefm = make_signed_type (TYPE_PRECISION (typefm));
7497 TREE_TYPE (typefm) = type;
7500 else if (VECTOR_MODE_P (mode)
7501 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7502 : TREE_CODE (type) != TREE_CODE (typefm))
7504 error ("mode %qs applied to inappropriate type", p);
7505 return NULL_TREE;
7508 *node = typefm;
7511 return NULL_TREE;
7514 /* Handle a "section" attribute; arguments as in
7515 struct attribute_spec.handler. */
7517 static tree
7518 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7519 int ARG_UNUSED (flags), bool *no_add_attrs)
7521 tree decl = *node;
7523 if (targetm_common.have_named_sections)
7525 user_defined_section_attribute = true;
7527 if ((TREE_CODE (decl) == FUNCTION_DECL
7528 || TREE_CODE (decl) == VAR_DECL)
7529 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7531 if (TREE_CODE (decl) == VAR_DECL
7532 && current_function_decl != NULL_TREE
7533 && !TREE_STATIC (decl))
7535 error_at (DECL_SOURCE_LOCATION (decl),
7536 "section attribute cannot be specified for "
7537 "local variables");
7538 *no_add_attrs = true;
7541 /* The decl may have already been given a section attribute
7542 from a previous declaration. Ensure they match. */
7543 else if (DECL_SECTION_NAME (decl) != NULL
7544 && strcmp (DECL_SECTION_NAME (decl),
7545 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7547 error ("section of %q+D conflicts with previous declaration",
7548 *node);
7549 *no_add_attrs = true;
7551 else if (TREE_CODE (decl) == VAR_DECL
7552 && !targetm.have_tls && targetm.emutls.tmpl_section
7553 && DECL_THREAD_LOCAL_P (decl))
7555 error ("section of %q+D cannot be overridden", *node);
7556 *no_add_attrs = true;
7558 else
7559 set_decl_section_name (decl,
7560 TREE_STRING_POINTER (TREE_VALUE (args)));
7562 else
7564 error ("section attribute not allowed for %q+D", *node);
7565 *no_add_attrs = true;
7568 else
7570 error_at (DECL_SOURCE_LOCATION (*node),
7571 "section attributes are not supported for this target");
7572 *no_add_attrs = true;
7575 return NULL_TREE;
7578 /* Check whether ALIGN is a valid user-specified alignment. If so,
7579 return its base-2 log; if not, output an error and return -1. If
7580 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7581 no error. */
7583 check_user_alignment (const_tree align, bool allow_zero)
7585 int i;
7587 if (error_operand_p (align))
7588 return -1;
7589 if (TREE_CODE (align) != INTEGER_CST
7590 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7592 error ("requested alignment is not an integer constant");
7593 return -1;
7595 else if (allow_zero && integer_zerop (align))
7596 return -1;
7597 else if (tree_int_cst_sgn (align) == -1
7598 || (i = tree_log2 (align)) == -1)
7600 error ("requested alignment is not a positive power of 2");
7601 return -1;
7603 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7605 error ("requested alignment is too large");
7606 return -1;
7608 return i;
7612 If in c++-11, check if the c++-11 alignment constraint with respect
7613 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7614 c++-11 mode, does nothing.
7616 [dcl.align]2/ says:
7618 [* if the constant expression evaluates to a fundamental alignment,
7619 the alignment requirement of the declared entity shall be the
7620 specified fundamental alignment.
7622 * if the constant expression evaluates to an extended alignment
7623 and the implementation supports that alignment in the context
7624 of the declaration, the alignment of the declared entity shall
7625 be that alignment
7627 * if the constant expression evaluates to an extended alignment
7628 and the implementation does not support that alignment in the
7629 context of the declaration, the program is ill-formed]. */
7631 static bool
7632 check_cxx_fundamental_alignment_constraints (tree node,
7633 unsigned align_log,
7634 int flags)
7636 bool alignment_too_large_p = false;
7637 unsigned requested_alignment = 1U << align_log;
7638 unsigned max_align = 0;
7640 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7641 || (node == NULL_TREE || node == error_mark_node))
7642 return true;
7644 if (cxx_fundamental_alignment_p (requested_alignment))
7645 return true;
7647 if (DECL_P (node))
7649 if (TREE_STATIC (node))
7651 /* For file scope variables and static members, the target
7652 supports alignments that are at most
7653 MAX_OFILE_ALIGNMENT. */
7654 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7655 alignment_too_large_p = true;
7657 else
7659 #ifdef BIGGEST_FIELD_ALIGNMENT
7660 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7661 #else
7662 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7663 #endif
7664 /* For non-static members, the target supports either
7665 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7666 if it is defined or BIGGEST_ALIGNMENT. */
7667 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7668 if (TREE_CODE (node) == FIELD_DECL
7669 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7670 alignment_too_large_p = true;
7671 #undef MAX_TARGET_FIELD_ALIGNMENT
7672 /* For stack variables, the target supports at most
7673 MAX_STACK_ALIGNMENT. */
7674 else if (decl_function_context (node) != NULL
7675 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7676 alignment_too_large_p = true;
7679 else if (TYPE_P (node))
7681 /* Let's be liberal for types. */
7682 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7683 alignment_too_large_p = true;
7686 if (alignment_too_large_p)
7687 pedwarn (input_location, OPT_Wattributes,
7688 "requested alignment %d is larger than %d",
7689 requested_alignment, max_align);
7691 return !alignment_too_large_p;
7694 /* Handle a "aligned" attribute; arguments as in
7695 struct attribute_spec.handler. */
7697 static tree
7698 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7699 int flags, bool *no_add_attrs)
7701 tree decl = NULL_TREE;
7702 tree *type = NULL;
7703 int is_type = 0;
7704 tree align_expr;
7705 int i;
7707 if (args)
7709 align_expr = TREE_VALUE (args);
7710 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7711 && TREE_CODE (align_expr) != FUNCTION_DECL)
7712 align_expr = default_conversion (align_expr);
7714 else
7715 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7717 if (DECL_P (*node))
7719 decl = *node;
7720 type = &TREE_TYPE (decl);
7721 is_type = TREE_CODE (*node) == TYPE_DECL;
7723 else if (TYPE_P (*node))
7724 type = node, is_type = 1;
7726 if ((i = check_user_alignment (align_expr, false)) == -1
7727 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7728 *no_add_attrs = true;
7729 else if (is_type)
7731 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7732 /* OK, modify the type in place. */;
7733 /* If we have a TYPE_DECL, then copy the type, so that we
7734 don't accidentally modify a builtin type. See pushdecl. */
7735 else if (decl && TREE_TYPE (decl) != error_mark_node
7736 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7738 tree tt = TREE_TYPE (decl);
7739 *type = build_variant_type_copy (*type);
7740 DECL_ORIGINAL_TYPE (decl) = tt;
7741 TYPE_NAME (*type) = decl;
7742 TREE_USED (*type) = TREE_USED (decl);
7743 TREE_TYPE (decl) = *type;
7745 else
7746 *type = build_variant_type_copy (*type);
7748 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7749 TYPE_USER_ALIGN (*type) = 1;
7751 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7752 && TREE_CODE (decl) != FIELD_DECL)
7754 error ("alignment may not be specified for %q+D", decl);
7755 *no_add_attrs = true;
7757 else if (DECL_USER_ALIGN (decl)
7758 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7759 /* C++-11 [dcl.align/4]:
7761 When multiple alignment-specifiers are specified for an
7762 entity, the alignment requirement shall be set to the
7763 strictest specified alignment.
7765 This formally comes from the c++11 specification but we are
7766 doing it for the GNU attribute syntax as well. */
7767 *no_add_attrs = true;
7768 else if (TREE_CODE (decl) == FUNCTION_DECL
7769 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7771 if (DECL_USER_ALIGN (decl))
7772 error ("alignment for %q+D was previously specified as %d "
7773 "and may not be decreased", decl,
7774 DECL_ALIGN (decl) / BITS_PER_UNIT);
7775 else
7776 error ("alignment for %q+D must be at least %d", decl,
7777 DECL_ALIGN (decl) / BITS_PER_UNIT);
7778 *no_add_attrs = true;
7780 else
7782 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7783 DECL_USER_ALIGN (decl) = 1;
7786 return NULL_TREE;
7789 /* Handle a "weak" attribute; arguments as in
7790 struct attribute_spec.handler. */
7792 static tree
7793 handle_weak_attribute (tree *node, tree name,
7794 tree ARG_UNUSED (args),
7795 int ARG_UNUSED (flags),
7796 bool * ARG_UNUSED (no_add_attrs))
7798 if (TREE_CODE (*node) == FUNCTION_DECL
7799 && DECL_DECLARED_INLINE_P (*node))
7801 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7802 *no_add_attrs = true;
7804 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7806 error ("indirect function %q+D cannot be declared weak", *node);
7807 *no_add_attrs = true;
7808 return NULL_TREE;
7810 else if (TREE_CODE (*node) == FUNCTION_DECL
7811 || TREE_CODE (*node) == VAR_DECL)
7812 declare_weak (*node);
7813 else
7814 warning (OPT_Wattributes, "%qE attribute ignored", name);
7816 return NULL_TREE;
7819 /* Handle an "alias" or "ifunc" attribute; arguments as in
7820 struct attribute_spec.handler, except that IS_ALIAS tells us
7821 whether this is an alias as opposed to ifunc attribute. */
7823 static tree
7824 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7825 bool *no_add_attrs)
7827 tree decl = *node;
7829 if (TREE_CODE (decl) != FUNCTION_DECL
7830 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7832 warning (OPT_Wattributes, "%qE attribute ignored", name);
7833 *no_add_attrs = true;
7835 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7836 || (TREE_CODE (decl) != FUNCTION_DECL
7837 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7838 /* A static variable declaration is always a tentative definition,
7839 but the alias is a non-tentative definition which overrides. */
7840 || (TREE_CODE (decl) != FUNCTION_DECL
7841 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7843 error ("%q+D defined both normally and as %qE attribute", decl, name);
7844 *no_add_attrs = true;
7845 return NULL_TREE;
7847 else if (!is_alias
7848 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7849 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7851 error ("weak %q+D cannot be defined %qE", decl, name);
7852 *no_add_attrs = true;
7853 return NULL_TREE;
7856 /* Note that the very first time we process a nested declaration,
7857 decl_function_context will not be set. Indeed, *would* never
7858 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7859 we do below. After such frobbery, pushdecl would set the context.
7860 In any case, this is never what we want. */
7861 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7863 tree id;
7865 id = TREE_VALUE (args);
7866 if (TREE_CODE (id) != STRING_CST)
7868 error ("attribute %qE argument not a string", name);
7869 *no_add_attrs = true;
7870 return NULL_TREE;
7872 id = get_identifier (TREE_STRING_POINTER (id));
7873 /* This counts as a use of the object pointed to. */
7874 TREE_USED (id) = 1;
7876 if (TREE_CODE (decl) == FUNCTION_DECL)
7877 DECL_INITIAL (decl) = error_mark_node;
7878 else
7879 TREE_STATIC (decl) = 1;
7881 if (!is_alias)
7882 /* ifuncs are also aliases, so set that attribute too. */
7883 DECL_ATTRIBUTES (decl)
7884 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7886 else
7888 warning (OPT_Wattributes, "%qE attribute ignored", name);
7889 *no_add_attrs = true;
7892 if (decl_in_symtab_p (*node))
7894 struct symtab_node *n = symtab_node::get (decl);
7895 if (n && n->refuse_visibility_changes)
7897 if (is_alias)
7898 error ("%+D declared alias after being used", decl);
7899 else
7900 error ("%+D declared ifunc after being used", decl);
7905 return NULL_TREE;
7908 /* Handle an "alias" or "ifunc" attribute; arguments as in
7909 struct attribute_spec.handler. */
7911 static tree
7912 handle_ifunc_attribute (tree *node, tree name, tree args,
7913 int ARG_UNUSED (flags), bool *no_add_attrs)
7915 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7918 /* Handle an "alias" or "ifunc" attribute; arguments as in
7919 struct attribute_spec.handler. */
7921 static tree
7922 handle_alias_attribute (tree *node, tree name, tree args,
7923 int ARG_UNUSED (flags), bool *no_add_attrs)
7925 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7928 /* Handle a "weakref" attribute; arguments as in struct
7929 attribute_spec.handler. */
7931 static tree
7932 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7933 int flags, bool *no_add_attrs)
7935 tree attr = NULL_TREE;
7937 /* We must ignore the attribute when it is associated with
7938 local-scoped decls, since attribute alias is ignored and many
7939 such symbols do not even have a DECL_WEAK field. */
7940 if (decl_function_context (*node)
7941 || current_function_decl
7942 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7944 warning (OPT_Wattributes, "%qE attribute ignored", name);
7945 *no_add_attrs = true;
7946 return NULL_TREE;
7949 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7951 error ("indirect function %q+D cannot be declared weakref", *node);
7952 *no_add_attrs = true;
7953 return NULL_TREE;
7956 /* The idea here is that `weakref("name")' mutates into `weakref,
7957 alias("name")', and weakref without arguments, in turn,
7958 implicitly adds weak. */
7960 if (args)
7962 attr = tree_cons (get_identifier ("alias"), args, attr);
7963 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7965 *no_add_attrs = true;
7967 decl_attributes (node, attr, flags);
7969 else
7971 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7972 error_at (DECL_SOURCE_LOCATION (*node),
7973 "weakref attribute must appear before alias attribute");
7975 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7976 and that isn't supported; and because it wants to add it to
7977 the list of weak decls, which isn't helpful. */
7978 DECL_WEAK (*node) = 1;
7981 if (decl_in_symtab_p (*node))
7983 struct symtab_node *n = symtab_node::get (*node);
7984 if (n && n->refuse_visibility_changes)
7985 error ("%+D declared weakref after being used", *node);
7988 return NULL_TREE;
7991 /* Handle an "visibility" attribute; arguments as in
7992 struct attribute_spec.handler. */
7994 static tree
7995 handle_visibility_attribute (tree *node, tree name, tree args,
7996 int ARG_UNUSED (flags),
7997 bool *ARG_UNUSED (no_add_attrs))
7999 tree decl = *node;
8000 tree id = TREE_VALUE (args);
8001 enum symbol_visibility vis;
8003 if (TYPE_P (*node))
8005 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8006 /* OK */;
8007 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8009 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8010 name);
8011 return NULL_TREE;
8013 else if (TYPE_FIELDS (*node))
8015 error ("%qE attribute ignored because %qT is already defined",
8016 name, *node);
8017 return NULL_TREE;
8020 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8022 warning (OPT_Wattributes, "%qE attribute ignored", name);
8023 return NULL_TREE;
8026 if (TREE_CODE (id) != STRING_CST)
8028 error ("visibility argument not a string");
8029 return NULL_TREE;
8032 /* If this is a type, set the visibility on the type decl. */
8033 if (TYPE_P (decl))
8035 decl = TYPE_NAME (decl);
8036 if (!decl)
8037 return NULL_TREE;
8038 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8040 warning (OPT_Wattributes, "%qE attribute ignored on types",
8041 name);
8042 return NULL_TREE;
8046 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8047 vis = VISIBILITY_DEFAULT;
8048 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8049 vis = VISIBILITY_INTERNAL;
8050 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8051 vis = VISIBILITY_HIDDEN;
8052 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8053 vis = VISIBILITY_PROTECTED;
8054 else
8056 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8057 vis = VISIBILITY_DEFAULT;
8060 if (DECL_VISIBILITY_SPECIFIED (decl)
8061 && vis != DECL_VISIBILITY (decl))
8063 tree attributes = (TYPE_P (*node)
8064 ? TYPE_ATTRIBUTES (*node)
8065 : DECL_ATTRIBUTES (decl));
8066 if (lookup_attribute ("visibility", attributes))
8067 error ("%qD redeclared with different visibility", decl);
8068 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8069 && lookup_attribute ("dllimport", attributes))
8070 error ("%qD was declared %qs which implies default visibility",
8071 decl, "dllimport");
8072 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8073 && lookup_attribute ("dllexport", attributes))
8074 error ("%qD was declared %qs which implies default visibility",
8075 decl, "dllexport");
8078 DECL_VISIBILITY (decl) = vis;
8079 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8081 /* Go ahead and attach the attribute to the node as well. This is needed
8082 so we can determine whether we have VISIBILITY_DEFAULT because the
8083 visibility was not specified, or because it was explicitly overridden
8084 from the containing scope. */
8086 return NULL_TREE;
8089 /* Determine the ELF symbol visibility for DECL, which is either a
8090 variable or a function. It is an error to use this function if a
8091 definition of DECL is not available in this translation unit.
8092 Returns true if the final visibility has been determined by this
8093 function; false if the caller is free to make additional
8094 modifications. */
8096 bool
8097 c_determine_visibility (tree decl)
8099 gcc_assert (TREE_CODE (decl) == VAR_DECL
8100 || TREE_CODE (decl) == FUNCTION_DECL);
8102 /* If the user explicitly specified the visibility with an
8103 attribute, honor that. DECL_VISIBILITY will have been set during
8104 the processing of the attribute. We check for an explicit
8105 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8106 to distinguish the use of an attribute from the use of a "#pragma
8107 GCC visibility push(...)"; in the latter case we still want other
8108 considerations to be able to overrule the #pragma. */
8109 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8110 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8111 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8112 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8113 return true;
8115 /* Set default visibility to whatever the user supplied with
8116 visibility_specified depending on #pragma GCC visibility. */
8117 if (!DECL_VISIBILITY_SPECIFIED (decl))
8119 if (visibility_options.inpragma
8120 || DECL_VISIBILITY (decl) != default_visibility)
8122 DECL_VISIBILITY (decl) = default_visibility;
8123 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8124 /* If visibility changed and DECL already has DECL_RTL, ensure
8125 symbol flags are updated. */
8126 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8127 || TREE_CODE (decl) == FUNCTION_DECL)
8128 && DECL_RTL_SET_P (decl))
8129 make_decl_rtl (decl);
8132 return false;
8135 /* Handle an "tls_model" attribute; arguments as in
8136 struct attribute_spec.handler. */
8138 static tree
8139 handle_tls_model_attribute (tree *node, tree name, tree args,
8140 int ARG_UNUSED (flags), bool *no_add_attrs)
8142 tree id;
8143 tree decl = *node;
8144 enum tls_model kind;
8146 *no_add_attrs = true;
8148 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8150 warning (OPT_Wattributes, "%qE attribute ignored", name);
8151 return NULL_TREE;
8154 kind = DECL_TLS_MODEL (decl);
8155 id = TREE_VALUE (args);
8156 if (TREE_CODE (id) != STRING_CST)
8158 error ("tls_model argument not a string");
8159 return NULL_TREE;
8162 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8163 kind = TLS_MODEL_LOCAL_EXEC;
8164 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8165 kind = TLS_MODEL_INITIAL_EXEC;
8166 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8167 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8168 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8169 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8170 else
8171 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8173 set_decl_tls_model (decl, kind);
8174 return NULL_TREE;
8177 /* Handle a "no_instrument_function" attribute; arguments as in
8178 struct attribute_spec.handler. */
8180 static tree
8181 handle_no_instrument_function_attribute (tree *node, tree name,
8182 tree ARG_UNUSED (args),
8183 int ARG_UNUSED (flags),
8184 bool *no_add_attrs)
8186 tree decl = *node;
8188 if (TREE_CODE (decl) != FUNCTION_DECL)
8190 error_at (DECL_SOURCE_LOCATION (decl),
8191 "%qE attribute applies only to functions", name);
8192 *no_add_attrs = true;
8194 else
8195 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8197 return NULL_TREE;
8200 /* Handle a "malloc" attribute; arguments as in
8201 struct attribute_spec.handler. */
8203 static tree
8204 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8205 int ARG_UNUSED (flags), bool *no_add_attrs)
8207 if (TREE_CODE (*node) == FUNCTION_DECL
8208 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8209 DECL_IS_MALLOC (*node) = 1;
8210 else
8212 warning (OPT_Wattributes, "%qE attribute ignored", name);
8213 *no_add_attrs = true;
8216 return NULL_TREE;
8219 /* Handle a "alloc_size" attribute; arguments as in
8220 struct attribute_spec.handler. */
8222 static tree
8223 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8224 int ARG_UNUSED (flags), bool *no_add_attrs)
8226 unsigned arg_count = type_num_arguments (*node);
8227 for (; args; args = TREE_CHAIN (args))
8229 tree position = TREE_VALUE (args);
8230 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8231 && TREE_CODE (position) != FUNCTION_DECL)
8232 position = default_conversion (position);
8234 if (!tree_fits_uhwi_p (position)
8235 || !arg_count
8236 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8238 warning (OPT_Wattributes,
8239 "alloc_size parameter outside range");
8240 *no_add_attrs = true;
8241 return NULL_TREE;
8244 return NULL_TREE;
8247 /* Handle a "alloc_align" attribute; arguments as in
8248 struct attribute_spec.handler. */
8250 static tree
8251 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8252 bool *no_add_attrs)
8254 unsigned arg_count = type_num_arguments (*node);
8255 tree position = TREE_VALUE (args);
8256 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8257 position = default_conversion (position);
8259 if (!tree_fits_uhwi_p (position)
8260 || !arg_count
8261 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8263 warning (OPT_Wattributes,
8264 "alloc_align parameter outside range");
8265 *no_add_attrs = true;
8266 return NULL_TREE;
8268 return NULL_TREE;
8271 /* Handle a "assume_aligned" attribute; arguments as in
8272 struct attribute_spec.handler. */
8274 static tree
8275 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8276 bool *no_add_attrs)
8278 for (; args; args = TREE_CHAIN (args))
8280 tree position = TREE_VALUE (args);
8281 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8282 && TREE_CODE (position) != FUNCTION_DECL)
8283 position = default_conversion (position);
8285 if (TREE_CODE (position) != INTEGER_CST)
8287 warning (OPT_Wattributes,
8288 "assume_aligned parameter not integer constant");
8289 *no_add_attrs = true;
8290 return NULL_TREE;
8293 return NULL_TREE;
8296 /* Handle a "fn spec" attribute; arguments as in
8297 struct attribute_spec.handler. */
8299 static tree
8300 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8301 tree args, int ARG_UNUSED (flags),
8302 bool *no_add_attrs ATTRIBUTE_UNUSED)
8304 gcc_assert (args
8305 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8306 && !TREE_CHAIN (args));
8307 return NULL_TREE;
8310 /* Handle a "bnd_variable_size" attribute; arguments as in
8311 struct attribute_spec.handler. */
8313 static tree
8314 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8315 int ARG_UNUSED (flags), bool *no_add_attrs)
8317 if (TREE_CODE (*node) != FIELD_DECL)
8319 warning (OPT_Wattributes, "%qE attribute ignored", name);
8320 *no_add_attrs = true;
8323 return NULL_TREE;
8326 /* Handle a "bnd_legacy" attribute; arguments as in
8327 struct attribute_spec.handler. */
8329 static tree
8330 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8331 int ARG_UNUSED (flags), bool *no_add_attrs)
8333 if (TREE_CODE (*node) != FUNCTION_DECL)
8335 warning (OPT_Wattributes, "%qE attribute ignored", name);
8336 *no_add_attrs = true;
8339 return NULL_TREE;
8342 /* Handle a "bnd_instrument" attribute; arguments as in
8343 struct attribute_spec.handler. */
8345 static tree
8346 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8347 int ARG_UNUSED (flags), bool *no_add_attrs)
8349 if (TREE_CODE (*node) != FUNCTION_DECL)
8351 warning (OPT_Wattributes, "%qE attribute ignored", name);
8352 *no_add_attrs = true;
8355 return NULL_TREE;
8358 /* Handle a "warn_unused" attribute; arguments as in
8359 struct attribute_spec.handler. */
8361 static tree
8362 handle_warn_unused_attribute (tree *node, tree name,
8363 tree args ATTRIBUTE_UNUSED,
8364 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8366 if (TYPE_P (*node))
8367 /* Do nothing else, just set the attribute. We'll get at
8368 it later with lookup_attribute. */
8370 else
8372 warning (OPT_Wattributes, "%qE attribute ignored", name);
8373 *no_add_attrs = true;
8376 return NULL_TREE;
8379 /* Handle an "omp declare simd" attribute; arguments as in
8380 struct attribute_spec.handler. */
8382 static tree
8383 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8385 return NULL_TREE;
8388 /* Handle an "omp declare target" attribute; arguments as in
8389 struct attribute_spec.handler. */
8391 static tree
8392 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8394 return NULL_TREE;
8397 /* Handle a "returns_twice" attribute; arguments as in
8398 struct attribute_spec.handler. */
8400 static tree
8401 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8402 int ARG_UNUSED (flags), bool *no_add_attrs)
8404 if (TREE_CODE (*node) == FUNCTION_DECL)
8405 DECL_IS_RETURNS_TWICE (*node) = 1;
8406 else
8408 warning (OPT_Wattributes, "%qE attribute ignored", name);
8409 *no_add_attrs = true;
8412 return NULL_TREE;
8415 /* Handle a "no_limit_stack" attribute; arguments as in
8416 struct attribute_spec.handler. */
8418 static tree
8419 handle_no_limit_stack_attribute (tree *node, tree name,
8420 tree ARG_UNUSED (args),
8421 int ARG_UNUSED (flags),
8422 bool *no_add_attrs)
8424 tree decl = *node;
8426 if (TREE_CODE (decl) != FUNCTION_DECL)
8428 error_at (DECL_SOURCE_LOCATION (decl),
8429 "%qE attribute applies only to functions", name);
8430 *no_add_attrs = true;
8432 else if (DECL_INITIAL (decl))
8434 error_at (DECL_SOURCE_LOCATION (decl),
8435 "can%'t set %qE attribute after definition", name);
8436 *no_add_attrs = true;
8438 else
8439 DECL_NO_LIMIT_STACK (decl) = 1;
8441 return NULL_TREE;
8444 /* Handle a "pure" attribute; arguments as in
8445 struct attribute_spec.handler. */
8447 static tree
8448 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8449 int ARG_UNUSED (flags), bool *no_add_attrs)
8451 if (TREE_CODE (*node) == FUNCTION_DECL)
8452 DECL_PURE_P (*node) = 1;
8453 /* ??? TODO: Support types. */
8454 else
8456 warning (OPT_Wattributes, "%qE attribute ignored", name);
8457 *no_add_attrs = true;
8460 return NULL_TREE;
8463 /* Digest an attribute list destined for a transactional memory statement.
8464 ALLOWED is the set of attributes that are allowed for this statement;
8465 return the attribute we parsed. Multiple attributes are never allowed. */
8468 parse_tm_stmt_attr (tree attrs, int allowed)
8470 tree a_seen = NULL;
8471 int m_seen = 0;
8473 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8475 tree a = TREE_PURPOSE (attrs);
8476 int m = 0;
8478 if (is_attribute_p ("outer", a))
8479 m = TM_STMT_ATTR_OUTER;
8481 if ((m & allowed) == 0)
8483 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8484 continue;
8487 if (m_seen == 0)
8489 a_seen = a;
8490 m_seen = m;
8492 else if (m_seen == m)
8493 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8494 else
8495 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8498 return m_seen;
8501 /* Transform a TM attribute name into a maskable integer and back.
8502 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8503 to how the lack of an attribute is treated. */
8506 tm_attr_to_mask (tree attr)
8508 if (attr == NULL)
8509 return 0;
8510 if (is_attribute_p ("transaction_safe", attr))
8511 return TM_ATTR_SAFE;
8512 if (is_attribute_p ("transaction_callable", attr))
8513 return TM_ATTR_CALLABLE;
8514 if (is_attribute_p ("transaction_pure", attr))
8515 return TM_ATTR_PURE;
8516 if (is_attribute_p ("transaction_unsafe", attr))
8517 return TM_ATTR_IRREVOCABLE;
8518 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8519 return TM_ATTR_MAY_CANCEL_OUTER;
8520 return 0;
8523 tree
8524 tm_mask_to_attr (int mask)
8526 const char *str;
8527 switch (mask)
8529 case TM_ATTR_SAFE:
8530 str = "transaction_safe";
8531 break;
8532 case TM_ATTR_CALLABLE:
8533 str = "transaction_callable";
8534 break;
8535 case TM_ATTR_PURE:
8536 str = "transaction_pure";
8537 break;
8538 case TM_ATTR_IRREVOCABLE:
8539 str = "transaction_unsafe";
8540 break;
8541 case TM_ATTR_MAY_CANCEL_OUTER:
8542 str = "transaction_may_cancel_outer";
8543 break;
8544 default:
8545 gcc_unreachable ();
8547 return get_identifier (str);
8550 /* Return the first TM attribute seen in LIST. */
8552 tree
8553 find_tm_attribute (tree list)
8555 for (; list ; list = TREE_CHAIN (list))
8557 tree name = TREE_PURPOSE (list);
8558 if (tm_attr_to_mask (name) != 0)
8559 return name;
8561 return NULL_TREE;
8564 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8565 Here we accept only function types, and verify that none of the other
8566 function TM attributes are also applied. */
8567 /* ??? We need to accept class types for C++, but not C. This greatly
8568 complicates this function, since we can no longer rely on the extra
8569 processing given by function_type_required. */
8571 static tree
8572 handle_tm_attribute (tree *node, tree name, tree args,
8573 int flags, bool *no_add_attrs)
8575 /* Only one path adds the attribute; others don't. */
8576 *no_add_attrs = true;
8578 switch (TREE_CODE (*node))
8580 case RECORD_TYPE:
8581 case UNION_TYPE:
8582 /* Only tm_callable and tm_safe apply to classes. */
8583 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8584 goto ignored;
8585 /* FALLTHRU */
8587 case FUNCTION_TYPE:
8588 case METHOD_TYPE:
8590 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8591 if (old_name == name)
8593 else if (old_name != NULL_TREE)
8594 error ("type was previously declared %qE", old_name);
8595 else
8596 *no_add_attrs = false;
8598 break;
8600 case POINTER_TYPE:
8602 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8603 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8605 tree fn_tmp = TREE_TYPE (*node);
8606 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8607 *node = build_pointer_type (fn_tmp);
8608 break;
8611 /* FALLTHRU */
8613 default:
8614 /* If a function is next, pass it on to be tried next. */
8615 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8616 return tree_cons (name, args, NULL);
8618 ignored:
8619 warning (OPT_Wattributes, "%qE attribute ignored", name);
8620 break;
8623 return NULL_TREE;
8626 /* Handle the TM_WRAP attribute; arguments as in
8627 struct attribute_spec.handler. */
8629 static tree
8630 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8631 int ARG_UNUSED (flags), bool *no_add_attrs)
8633 tree decl = *node;
8635 /* We don't need the attribute even on success, since we
8636 record the entry in an external table. */
8637 *no_add_attrs = true;
8639 if (TREE_CODE (decl) != FUNCTION_DECL)
8640 warning (OPT_Wattributes, "%qE attribute ignored", name);
8641 else
8643 tree wrap_decl = TREE_VALUE (args);
8644 if (error_operand_p (wrap_decl))
8646 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8647 && TREE_CODE (wrap_decl) != VAR_DECL
8648 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8649 error ("%qE argument not an identifier", name);
8650 else
8652 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8653 wrap_decl = lookup_name (wrap_decl);
8654 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8656 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8657 TREE_TYPE (wrap_decl)))
8658 record_tm_replacement (wrap_decl, decl);
8659 else
8660 error ("%qD is not compatible with %qD", wrap_decl, decl);
8662 else
8663 error ("%qE argument is not a function", name);
8667 return NULL_TREE;
8670 /* Ignore the given attribute. Used when this attribute may be usefully
8671 overridden by the target, but is not used generically. */
8673 static tree
8674 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8675 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8676 bool *no_add_attrs)
8678 *no_add_attrs = true;
8679 return NULL_TREE;
8682 /* Handle a "no vops" attribute; arguments as in
8683 struct attribute_spec.handler. */
8685 static tree
8686 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8687 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8688 bool *ARG_UNUSED (no_add_attrs))
8690 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8691 DECL_IS_NOVOPS (*node) = 1;
8692 return NULL_TREE;
8695 /* Handle a "deprecated" attribute; arguments as in
8696 struct attribute_spec.handler. */
8698 static tree
8699 handle_deprecated_attribute (tree *node, tree name,
8700 tree args, int flags,
8701 bool *no_add_attrs)
8703 tree type = NULL_TREE;
8704 int warn = 0;
8705 tree what = NULL_TREE;
8707 if (!args)
8708 *no_add_attrs = true;
8709 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8711 error ("deprecated message is not a string");
8712 *no_add_attrs = true;
8715 if (DECL_P (*node))
8717 tree decl = *node;
8718 type = TREE_TYPE (decl);
8720 if (TREE_CODE (decl) == TYPE_DECL
8721 || TREE_CODE (decl) == PARM_DECL
8722 || TREE_CODE (decl) == VAR_DECL
8723 || TREE_CODE (decl) == FUNCTION_DECL
8724 || TREE_CODE (decl) == FIELD_DECL
8725 || objc_method_decl (TREE_CODE (decl)))
8726 TREE_DEPRECATED (decl) = 1;
8727 else
8728 warn = 1;
8730 else if (TYPE_P (*node))
8732 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8733 *node = build_variant_type_copy (*node);
8734 TREE_DEPRECATED (*node) = 1;
8735 type = *node;
8737 else
8738 warn = 1;
8740 if (warn)
8742 *no_add_attrs = true;
8743 if (type && TYPE_NAME (type))
8745 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8746 what = TYPE_NAME (*node);
8747 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8748 && DECL_NAME (TYPE_NAME (type)))
8749 what = DECL_NAME (TYPE_NAME (type));
8751 if (what)
8752 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8753 else
8754 warning (OPT_Wattributes, "%qE attribute ignored", name);
8757 return NULL_TREE;
8760 /* Handle a "vector_size" attribute; arguments as in
8761 struct attribute_spec.handler. */
8763 static tree
8764 handle_vector_size_attribute (tree *node, tree name, tree args,
8765 int ARG_UNUSED (flags),
8766 bool *no_add_attrs)
8768 unsigned HOST_WIDE_INT vecsize, nunits;
8769 machine_mode orig_mode;
8770 tree type = *node, new_type, size;
8772 *no_add_attrs = true;
8774 size = TREE_VALUE (args);
8775 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8776 && TREE_CODE (size) != FUNCTION_DECL)
8777 size = default_conversion (size);
8779 if (!tree_fits_uhwi_p (size))
8781 warning (OPT_Wattributes, "%qE attribute ignored", name);
8782 return NULL_TREE;
8785 /* Get the vector size (in bytes). */
8786 vecsize = tree_to_uhwi (size);
8788 /* We need to provide for vector pointers, vector arrays, and
8789 functions returning vectors. For example:
8791 __attribute__((vector_size(16))) short *foo;
8793 In this case, the mode is SI, but the type being modified is
8794 HI, so we need to look further. */
8796 while (POINTER_TYPE_P (type)
8797 || TREE_CODE (type) == FUNCTION_TYPE
8798 || TREE_CODE (type) == METHOD_TYPE
8799 || TREE_CODE (type) == ARRAY_TYPE
8800 || TREE_CODE (type) == OFFSET_TYPE)
8801 type = TREE_TYPE (type);
8803 /* Get the mode of the type being modified. */
8804 orig_mode = TYPE_MODE (type);
8806 if ((!INTEGRAL_TYPE_P (type)
8807 && !SCALAR_FLOAT_TYPE_P (type)
8808 && !FIXED_POINT_TYPE_P (type))
8809 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8810 && GET_MODE_CLASS (orig_mode) != MODE_INT
8811 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8812 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8813 || TREE_CODE (type) == BOOLEAN_TYPE)
8815 error ("invalid vector type for attribute %qE", name);
8816 return NULL_TREE;
8819 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8821 error ("vector size not an integral multiple of component size");
8822 return NULL;
8825 if (vecsize == 0)
8827 error ("zero vector size");
8828 return NULL;
8831 /* Calculate how many units fit in the vector. */
8832 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8833 if (nunits & (nunits - 1))
8835 error ("number of components of the vector not a power of two");
8836 return NULL_TREE;
8839 new_type = build_vector_type (type, nunits);
8841 /* Build back pointers if needed. */
8842 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8844 return NULL_TREE;
8847 /* Handle the "nonnull" attribute. */
8848 static tree
8849 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8850 tree args, int ARG_UNUSED (flags),
8851 bool *no_add_attrs)
8853 tree type = *node;
8854 unsigned HOST_WIDE_INT attr_arg_num;
8856 /* If no arguments are specified, all pointer arguments should be
8857 non-null. Verify a full prototype is given so that the arguments
8858 will have the correct types when we actually check them later. */
8859 if (!args)
8861 if (!prototype_p (type))
8863 error ("nonnull attribute without arguments on a non-prototype");
8864 *no_add_attrs = true;
8866 return NULL_TREE;
8869 /* Argument list specified. Verify that each argument number references
8870 a pointer argument. */
8871 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8873 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8875 tree arg = TREE_VALUE (args);
8876 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8877 && TREE_CODE (arg) != FUNCTION_DECL)
8878 arg = default_conversion (arg);
8880 if (!get_nonnull_operand (arg, &arg_num))
8882 error ("nonnull argument has invalid operand number (argument %lu)",
8883 (unsigned long) attr_arg_num);
8884 *no_add_attrs = true;
8885 return NULL_TREE;
8888 if (prototype_p (type))
8890 function_args_iterator iter;
8891 tree argument;
8893 function_args_iter_init (&iter, type);
8894 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8896 argument = function_args_iter_cond (&iter);
8897 if (argument == NULL_TREE || ck_num == arg_num)
8898 break;
8901 if (!argument
8902 || TREE_CODE (argument) == VOID_TYPE)
8904 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8905 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8906 *no_add_attrs = true;
8907 return NULL_TREE;
8910 if (TREE_CODE (argument) != POINTER_TYPE)
8912 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8913 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8914 *no_add_attrs = true;
8915 return NULL_TREE;
8920 return NULL_TREE;
8923 /* Check the argument list of a function call for null in argument slots
8924 that are marked as requiring a non-null pointer argument. The NARGS
8925 arguments are passed in the array ARGARRAY.
8928 static void
8929 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8931 tree a;
8932 int i;
8934 attrs = lookup_attribute ("nonnull", attrs);
8935 if (attrs == NULL_TREE)
8936 return;
8938 a = attrs;
8939 /* See if any of the nonnull attributes has no arguments. If so,
8940 then every pointer argument is checked (in which case the check
8941 for pointer type is done in check_nonnull_arg). */
8942 if (TREE_VALUE (a) != NULL_TREE)
8944 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8945 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8947 if (a != NULL_TREE)
8948 for (i = 0; i < nargs; i++)
8949 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8950 i + 1);
8951 else
8953 /* Walk the argument list. If we encounter an argument number we
8954 should check for non-null, do it. */
8955 for (i = 0; i < nargs; i++)
8957 for (a = attrs; ; a = TREE_CHAIN (a))
8959 a = lookup_attribute ("nonnull", a);
8960 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8961 break;
8964 if (a != NULL_TREE)
8965 check_function_arguments_recurse (check_nonnull_arg, NULL,
8966 argarray[i], i + 1);
8971 /* Check that the Nth argument of a function call (counting backwards
8972 from the end) is a (pointer)0. The NARGS arguments are passed in the
8973 array ARGARRAY. */
8975 static void
8976 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8978 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8980 if (attr)
8982 int len = 0;
8983 int pos = 0;
8984 tree sentinel;
8985 function_args_iterator iter;
8986 tree t;
8988 /* Skip over the named arguments. */
8989 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8991 if (len == nargs)
8992 break;
8993 len++;
8996 if (TREE_VALUE (attr))
8998 tree p = TREE_VALUE (TREE_VALUE (attr));
8999 pos = TREE_INT_CST_LOW (p);
9002 /* The sentinel must be one of the varargs, i.e.
9003 in position >= the number of fixed arguments. */
9004 if ((nargs - 1 - pos) < len)
9006 warning (OPT_Wformat_,
9007 "not enough variable arguments to fit a sentinel");
9008 return;
9011 /* Validate the sentinel. */
9012 sentinel = argarray[nargs - 1 - pos];
9013 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9014 || !integer_zerop (sentinel))
9015 /* Although __null (in C++) is only an integer we allow it
9016 nevertheless, as we are guaranteed that it's exactly
9017 as wide as a pointer, and we don't want to force
9018 users to cast the NULL they have written there.
9019 We warn with -Wstrict-null-sentinel, though. */
9020 && (warn_strict_null_sentinel || null_node != sentinel))
9021 warning (OPT_Wformat_, "missing sentinel in function call");
9025 /* Helper for check_function_nonnull; given a list of operands which
9026 must be non-null in ARGS, determine if operand PARAM_NUM should be
9027 checked. */
9029 static bool
9030 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9032 unsigned HOST_WIDE_INT arg_num = 0;
9034 for (; args; args = TREE_CHAIN (args))
9036 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9038 gcc_assert (found);
9040 if (arg_num == param_num)
9041 return true;
9043 return false;
9046 /* Check that the function argument PARAM (which is operand number
9047 PARAM_NUM) is non-null. This is called by check_function_nonnull
9048 via check_function_arguments_recurse. */
9050 static void
9051 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9052 unsigned HOST_WIDE_INT param_num)
9054 /* Just skip checking the argument if it's not a pointer. This can
9055 happen if the "nonnull" attribute was given without an operand
9056 list (which means to check every pointer argument). */
9058 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9059 return;
9061 if (integer_zerop (param))
9062 warning (OPT_Wnonnull, "null argument where non-null required "
9063 "(argument %lu)", (unsigned long) param_num);
9066 /* Helper for nonnull attribute handling; fetch the operand number
9067 from the attribute argument list. */
9069 static bool
9070 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9072 /* Verify the arg number is a small constant. */
9073 if (tree_fits_uhwi_p (arg_num_expr))
9075 *valp = TREE_INT_CST_LOW (arg_num_expr);
9076 return true;
9078 else
9079 return false;
9082 /* Handle a "nothrow" attribute; arguments as in
9083 struct attribute_spec.handler. */
9085 static tree
9086 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9087 int ARG_UNUSED (flags), bool *no_add_attrs)
9089 if (TREE_CODE (*node) == FUNCTION_DECL)
9090 TREE_NOTHROW (*node) = 1;
9091 /* ??? TODO: Support types. */
9092 else
9094 warning (OPT_Wattributes, "%qE attribute ignored", name);
9095 *no_add_attrs = true;
9098 return NULL_TREE;
9101 /* Handle a "cleanup" attribute; arguments as in
9102 struct attribute_spec.handler. */
9104 static tree
9105 handle_cleanup_attribute (tree *node, tree name, tree args,
9106 int ARG_UNUSED (flags), bool *no_add_attrs)
9108 tree decl = *node;
9109 tree cleanup_id, cleanup_decl;
9111 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9112 for global destructors in C++. This requires infrastructure that
9113 we don't have generically at the moment. It's also not a feature
9114 we'd be missing too much, since we do have attribute constructor. */
9115 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9117 warning (OPT_Wattributes, "%qE attribute ignored", name);
9118 *no_add_attrs = true;
9119 return NULL_TREE;
9122 /* Verify that the argument is a function in scope. */
9123 /* ??? We could support pointers to functions here as well, if
9124 that was considered desirable. */
9125 cleanup_id = TREE_VALUE (args);
9126 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9128 error ("cleanup argument not an identifier");
9129 *no_add_attrs = true;
9130 return NULL_TREE;
9132 cleanup_decl = lookup_name (cleanup_id);
9133 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9135 error ("cleanup argument not a function");
9136 *no_add_attrs = true;
9137 return NULL_TREE;
9140 /* That the function has proper type is checked with the
9141 eventual call to build_function_call. */
9143 return NULL_TREE;
9146 /* Handle a "warn_unused_result" attribute. No special handling. */
9148 static tree
9149 handle_warn_unused_result_attribute (tree *node, tree name,
9150 tree ARG_UNUSED (args),
9151 int ARG_UNUSED (flags), bool *no_add_attrs)
9153 /* Ignore the attribute for functions not returning any value. */
9154 if (VOID_TYPE_P (TREE_TYPE (*node)))
9156 warning (OPT_Wattributes, "%qE attribute ignored", name);
9157 *no_add_attrs = true;
9160 return NULL_TREE;
9163 /* Handle a "sentinel" attribute. */
9165 static tree
9166 handle_sentinel_attribute (tree *node, tree name, tree args,
9167 int ARG_UNUSED (flags), bool *no_add_attrs)
9169 if (!prototype_p (*node))
9171 warning (OPT_Wattributes,
9172 "%qE attribute requires prototypes with named arguments", name);
9173 *no_add_attrs = true;
9175 else
9177 if (!stdarg_p (*node))
9179 warning (OPT_Wattributes,
9180 "%qE attribute only applies to variadic functions", name);
9181 *no_add_attrs = true;
9185 if (args)
9187 tree position = TREE_VALUE (args);
9188 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9189 && TREE_CODE (position) != FUNCTION_DECL)
9190 position = default_conversion (position);
9192 if (TREE_CODE (position) != INTEGER_CST
9193 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9195 warning (OPT_Wattributes,
9196 "requested position is not an integer constant");
9197 *no_add_attrs = true;
9199 else
9201 if (tree_int_cst_lt (position, integer_zero_node))
9203 warning (OPT_Wattributes,
9204 "requested position is less than zero");
9205 *no_add_attrs = true;
9210 return NULL_TREE;
9213 /* Handle a "type_generic" attribute. */
9215 static tree
9216 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9217 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9218 bool * ARG_UNUSED (no_add_attrs))
9220 /* Ensure we have a function type. */
9221 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9223 /* Ensure we have a variadic function. */
9224 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9226 return NULL_TREE;
9229 /* Handle a "target" attribute. */
9231 static tree
9232 handle_target_attribute (tree *node, tree name, tree args, int flags,
9233 bool *no_add_attrs)
9235 /* Ensure we have a function type. */
9236 if (TREE_CODE (*node) != FUNCTION_DECL)
9238 warning (OPT_Wattributes, "%qE attribute ignored", name);
9239 *no_add_attrs = true;
9241 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9242 flags))
9243 *no_add_attrs = true;
9245 return NULL_TREE;
9248 /* Arguments being collected for optimization. */
9249 typedef const char *const_char_p; /* For DEF_VEC_P. */
9250 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9253 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9254 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9255 false for #pragma GCC optimize. */
9257 bool
9258 parse_optimize_options (tree args, bool attr_p)
9260 bool ret = true;
9261 unsigned opt_argc;
9262 unsigned i;
9263 int saved_flag_strict_aliasing;
9264 const char **opt_argv;
9265 struct cl_decoded_option *decoded_options;
9266 unsigned int decoded_options_count;
9267 tree ap;
9269 /* Build up argv vector. Just in case the string is stored away, use garbage
9270 collected strings. */
9271 vec_safe_truncate (optimize_args, 0);
9272 vec_safe_push (optimize_args, (const char *) NULL);
9274 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9276 tree value = TREE_VALUE (ap);
9278 if (TREE_CODE (value) == INTEGER_CST)
9280 char buffer[20];
9281 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9282 vec_safe_push (optimize_args, ggc_strdup (buffer));
9285 else if (TREE_CODE (value) == STRING_CST)
9287 /* Split string into multiple substrings. */
9288 size_t len = TREE_STRING_LENGTH (value);
9289 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9290 char *end = p + len;
9291 char *comma;
9292 char *next_p = p;
9294 while (next_p != NULL)
9296 size_t len2;
9297 char *q, *r;
9299 p = next_p;
9300 comma = strchr (p, ',');
9301 if (comma)
9303 len2 = comma - p;
9304 *comma = '\0';
9305 next_p = comma+1;
9307 else
9309 len2 = end - p;
9310 next_p = NULL;
9313 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9315 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9316 options. */
9317 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9319 ret = false;
9320 if (attr_p)
9321 warning (OPT_Wattributes,
9322 "bad option %s to optimize attribute", p);
9323 else
9324 warning (OPT_Wpragmas,
9325 "bad option %s to pragma attribute", p);
9326 continue;
9329 if (*p != '-')
9331 *r++ = '-';
9333 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9334 itself is -Os, and any other switch begins with a -f. */
9335 if ((*p >= '0' && *p <= '9')
9336 || (p[0] == 's' && p[1] == '\0'))
9337 *r++ = 'O';
9338 else if (*p != 'O')
9339 *r++ = 'f';
9342 memcpy (r, p, len2);
9343 r[len2] = '\0';
9344 vec_safe_push (optimize_args, (const char *) q);
9350 opt_argc = optimize_args->length ();
9351 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9353 for (i = 1; i < opt_argc; i++)
9354 opt_argv[i] = (*optimize_args)[i];
9356 saved_flag_strict_aliasing = flag_strict_aliasing;
9358 /* Now parse the options. */
9359 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9360 &decoded_options,
9361 &decoded_options_count);
9362 decode_options (&global_options, &global_options_set,
9363 decoded_options, decoded_options_count,
9364 input_location, global_dc);
9366 targetm.override_options_after_change();
9368 /* Don't allow changing -fstrict-aliasing. */
9369 flag_strict_aliasing = saved_flag_strict_aliasing;
9371 optimize_args->truncate (0);
9372 return ret;
9375 /* For handling "optimize" attribute. arguments as in
9376 struct attribute_spec.handler. */
9378 static tree
9379 handle_optimize_attribute (tree *node, tree name, tree args,
9380 int ARG_UNUSED (flags), bool *no_add_attrs)
9382 /* Ensure we have a function type. */
9383 if (TREE_CODE (*node) != FUNCTION_DECL)
9385 warning (OPT_Wattributes, "%qE attribute ignored", name);
9386 *no_add_attrs = true;
9388 else
9390 struct cl_optimization cur_opts;
9391 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9393 /* Save current options. */
9394 cl_optimization_save (&cur_opts, &global_options);
9396 /* If we previously had some optimization options, use them as the
9397 default. */
9398 if (old_opts)
9399 cl_optimization_restore (&global_options,
9400 TREE_OPTIMIZATION (old_opts));
9402 /* Parse options, and update the vector. */
9403 parse_optimize_options (args, true);
9404 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9405 = build_optimization_node (&global_options);
9407 /* Restore current options. */
9408 cl_optimization_restore (&global_options, &cur_opts);
9411 return NULL_TREE;
9414 /* Handle a "no_split_stack" attribute. */
9416 static tree
9417 handle_no_split_stack_attribute (tree *node, tree name,
9418 tree ARG_UNUSED (args),
9419 int ARG_UNUSED (flags),
9420 bool *no_add_attrs)
9422 tree decl = *node;
9424 if (TREE_CODE (decl) != FUNCTION_DECL)
9426 error_at (DECL_SOURCE_LOCATION (decl),
9427 "%qE attribute applies only to functions", name);
9428 *no_add_attrs = true;
9430 else if (DECL_INITIAL (decl))
9432 error_at (DECL_SOURCE_LOCATION (decl),
9433 "can%'t set %qE attribute after definition", name);
9434 *no_add_attrs = true;
9437 return NULL_TREE;
9440 /* Handle a "returns_nonnull" attribute; arguments as in
9441 struct attribute_spec.handler. */
9443 static tree
9444 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9445 bool *no_add_attrs)
9447 // Even without a prototype we still have a return type we can check.
9448 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9450 error ("returns_nonnull attribute on a function not returning a pointer");
9451 *no_add_attrs = true;
9453 return NULL_TREE;
9456 /* Handle a "designated_init" attribute; arguments as in
9457 struct attribute_spec.handler. */
9459 static tree
9460 handle_designated_init_attribute (tree *node, tree name, tree, int,
9461 bool *no_add_attrs)
9463 if (TREE_CODE (*node) != RECORD_TYPE)
9465 error ("%qE attribute is only valid on %<struct%> type", name);
9466 *no_add_attrs = true;
9468 return NULL_TREE;
9472 /* Check for valid arguments being passed to a function with FNTYPE.
9473 There are NARGS arguments in the array ARGARRAY. */
9474 void
9475 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9477 /* Check for null being passed in a pointer argument that must be
9478 non-null. We also need to do this if format checking is enabled. */
9480 if (warn_nonnull)
9481 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9483 /* Check for errors in format strings. */
9485 if (warn_format || warn_suggest_attribute_format)
9486 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9488 if (warn_format)
9489 check_function_sentinel (fntype, nargs, argarray);
9492 /* Generic argument checking recursion routine. PARAM is the argument to
9493 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9494 once the argument is resolved. CTX is context for the callback. */
9495 void
9496 check_function_arguments_recurse (void (*callback)
9497 (void *, tree, unsigned HOST_WIDE_INT),
9498 void *ctx, tree param,
9499 unsigned HOST_WIDE_INT param_num)
9501 if (CONVERT_EXPR_P (param)
9502 && (TYPE_PRECISION (TREE_TYPE (param))
9503 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9505 /* Strip coercion. */
9506 check_function_arguments_recurse (callback, ctx,
9507 TREE_OPERAND (param, 0), param_num);
9508 return;
9511 if (TREE_CODE (param) == CALL_EXPR)
9513 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9514 tree attrs;
9515 bool found_format_arg = false;
9517 /* See if this is a call to a known internationalization function
9518 that modifies a format arg. Such a function may have multiple
9519 format_arg attributes (for example, ngettext). */
9521 for (attrs = TYPE_ATTRIBUTES (type);
9522 attrs;
9523 attrs = TREE_CHAIN (attrs))
9524 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9526 tree inner_arg;
9527 tree format_num_expr;
9528 int format_num;
9529 int i;
9530 call_expr_arg_iterator iter;
9532 /* Extract the argument number, which was previously checked
9533 to be valid. */
9534 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9536 format_num = tree_to_uhwi (format_num_expr);
9538 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9539 inner_arg != 0;
9540 inner_arg = next_call_expr_arg (&iter), i++)
9541 if (i == format_num)
9543 check_function_arguments_recurse (callback, ctx,
9544 inner_arg, param_num);
9545 found_format_arg = true;
9546 break;
9550 /* If we found a format_arg attribute and did a recursive check,
9551 we are done with checking this argument. Otherwise, we continue
9552 and this will be considered a non-literal. */
9553 if (found_format_arg)
9554 return;
9557 if (TREE_CODE (param) == COND_EXPR)
9559 /* Check both halves of the conditional expression. */
9560 check_function_arguments_recurse (callback, ctx,
9561 TREE_OPERAND (param, 1), param_num);
9562 check_function_arguments_recurse (callback, ctx,
9563 TREE_OPERAND (param, 2), param_num);
9564 return;
9567 (*callback) (ctx, param, param_num);
9570 /* Checks for a builtin function FNDECL that the number of arguments
9571 NARGS against the required number REQUIRED and issues an error if
9572 there is a mismatch. Returns true if the number of arguments is
9573 correct, otherwise false. */
9575 static bool
9576 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9578 if (nargs < required)
9580 error_at (input_location,
9581 "not enough arguments to function %qE", fndecl);
9582 return false;
9584 else if (nargs > required)
9586 error_at (input_location,
9587 "too many arguments to function %qE", fndecl);
9588 return false;
9590 return true;
9593 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9594 Returns false if there was an error, otherwise true. */
9596 bool
9597 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9599 if (!DECL_BUILT_IN (fndecl)
9600 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9601 return true;
9603 switch (DECL_FUNCTION_CODE (fndecl))
9605 case BUILT_IN_CONSTANT_P:
9606 return builtin_function_validate_nargs (fndecl, nargs, 1);
9608 case BUILT_IN_ISFINITE:
9609 case BUILT_IN_ISINF:
9610 case BUILT_IN_ISINF_SIGN:
9611 case BUILT_IN_ISNAN:
9612 case BUILT_IN_ISNORMAL:
9613 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9615 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9617 error ("non-floating-point argument in call to "
9618 "function %qE", fndecl);
9619 return false;
9621 return true;
9623 return false;
9625 case BUILT_IN_ISGREATER:
9626 case BUILT_IN_ISGREATEREQUAL:
9627 case BUILT_IN_ISLESS:
9628 case BUILT_IN_ISLESSEQUAL:
9629 case BUILT_IN_ISLESSGREATER:
9630 case BUILT_IN_ISUNORDERED:
9631 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9633 enum tree_code code0, code1;
9634 code0 = TREE_CODE (TREE_TYPE (args[0]));
9635 code1 = TREE_CODE (TREE_TYPE (args[1]));
9636 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9637 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9638 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9640 error ("non-floating-point arguments in call to "
9641 "function %qE", fndecl);
9642 return false;
9644 return true;
9646 return false;
9648 case BUILT_IN_FPCLASSIFY:
9649 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9651 unsigned i;
9653 for (i=0; i<5; i++)
9654 if (TREE_CODE (args[i]) != INTEGER_CST)
9656 error ("non-const integer argument %u in call to function %qE",
9657 i+1, fndecl);
9658 return false;
9661 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9663 error ("non-floating-point argument in call to function %qE",
9664 fndecl);
9665 return false;
9667 return true;
9669 return false;
9671 case BUILT_IN_ASSUME_ALIGNED:
9672 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9674 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9676 error ("non-integer argument 3 in call to function %qE", fndecl);
9677 return false;
9679 return true;
9681 return false;
9683 case BUILT_IN_ADD_OVERFLOW:
9684 case BUILT_IN_SUB_OVERFLOW:
9685 case BUILT_IN_MUL_OVERFLOW:
9686 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9688 unsigned i;
9689 for (i = 0; i < 2; i++)
9690 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9692 error ("argument %u in call to function %qE does not have "
9693 "integral type", i + 1, fndecl);
9694 return false;
9696 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9697 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9699 error ("argument 3 in call to function %qE does not have "
9700 "pointer to integer type", fndecl);
9701 return false;
9703 return true;
9705 return false;
9707 default:
9708 return true;
9712 /* Function to help qsort sort FIELD_DECLs by name order. */
9715 field_decl_cmp (const void *x_p, const void *y_p)
9717 const tree *const x = (const tree *const) x_p;
9718 const tree *const y = (const tree *const) y_p;
9720 if (DECL_NAME (*x) == DECL_NAME (*y))
9721 /* A nontype is "greater" than a type. */
9722 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9723 if (DECL_NAME (*x) == NULL_TREE)
9724 return -1;
9725 if (DECL_NAME (*y) == NULL_TREE)
9726 return 1;
9727 if (DECL_NAME (*x) < DECL_NAME (*y))
9728 return -1;
9729 return 1;
9732 static struct {
9733 gt_pointer_operator new_value;
9734 void *cookie;
9735 } resort_data;
9737 /* This routine compares two fields like field_decl_cmp but using the
9738 pointer operator in resort_data. */
9740 static int
9741 resort_field_decl_cmp (const void *x_p, const void *y_p)
9743 const tree *const x = (const tree *const) x_p;
9744 const tree *const y = (const tree *const) y_p;
9746 if (DECL_NAME (*x) == DECL_NAME (*y))
9747 /* A nontype is "greater" than a type. */
9748 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9749 if (DECL_NAME (*x) == NULL_TREE)
9750 return -1;
9751 if (DECL_NAME (*y) == NULL_TREE)
9752 return 1;
9754 tree d1 = DECL_NAME (*x);
9755 tree d2 = DECL_NAME (*y);
9756 resort_data.new_value (&d1, resort_data.cookie);
9757 resort_data.new_value (&d2, resort_data.cookie);
9758 if (d1 < d2)
9759 return -1;
9761 return 1;
9764 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9766 void
9767 resort_sorted_fields (void *obj,
9768 void * ARG_UNUSED (orig_obj),
9769 gt_pointer_operator new_value,
9770 void *cookie)
9772 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9773 resort_data.new_value = new_value;
9774 resort_data.cookie = cookie;
9775 qsort (&sf->elts[0], sf->len, sizeof (tree),
9776 resort_field_decl_cmp);
9779 /* Subroutine of c_parse_error.
9780 Return the result of concatenating LHS and RHS. RHS is really
9781 a string literal, its first character is indicated by RHS_START and
9782 RHS_SIZE is its length (including the terminating NUL character).
9784 The caller is responsible for deleting the returned pointer. */
9786 static char *
9787 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9789 const int lhs_size = strlen (lhs);
9790 char *result = XNEWVEC (char, lhs_size + rhs_size);
9791 strncpy (result, lhs, lhs_size);
9792 strncpy (result + lhs_size, rhs_start, rhs_size);
9793 return result;
9796 /* Issue the error given by GMSGID, indicating that it occurred before
9797 TOKEN, which had the associated VALUE. */
9799 void
9800 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9801 tree value, unsigned char token_flags)
9803 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9805 char *message = NULL;
9807 if (token_type == CPP_EOF)
9808 message = catenate_messages (gmsgid, " at end of input");
9809 else if (token_type == CPP_CHAR
9810 || token_type == CPP_WCHAR
9811 || token_type == CPP_CHAR16
9812 || token_type == CPP_CHAR32)
9814 unsigned int val = TREE_INT_CST_LOW (value);
9815 const char *prefix;
9817 switch (token_type)
9819 default:
9820 prefix = "";
9821 break;
9822 case CPP_WCHAR:
9823 prefix = "L";
9824 break;
9825 case CPP_CHAR16:
9826 prefix = "u";
9827 break;
9828 case CPP_CHAR32:
9829 prefix = "U";
9830 break;
9833 if (val <= UCHAR_MAX && ISGRAPH (val))
9834 message = catenate_messages (gmsgid, " before %s'%c'");
9835 else
9836 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9838 error (message, prefix, val);
9839 free (message);
9840 message = NULL;
9842 else if (token_type == CPP_CHAR_USERDEF
9843 || token_type == CPP_WCHAR_USERDEF
9844 || token_type == CPP_CHAR16_USERDEF
9845 || token_type == CPP_CHAR32_USERDEF)
9846 message = catenate_messages (gmsgid,
9847 " before user-defined character literal");
9848 else if (token_type == CPP_STRING_USERDEF
9849 || token_type == CPP_WSTRING_USERDEF
9850 || token_type == CPP_STRING16_USERDEF
9851 || token_type == CPP_STRING32_USERDEF
9852 || token_type == CPP_UTF8STRING_USERDEF)
9853 message = catenate_messages (gmsgid, " before user-defined string literal");
9854 else if (token_type == CPP_STRING
9855 || token_type == CPP_WSTRING
9856 || token_type == CPP_STRING16
9857 || token_type == CPP_STRING32
9858 || token_type == CPP_UTF8STRING)
9859 message = catenate_messages (gmsgid, " before string constant");
9860 else if (token_type == CPP_NUMBER)
9861 message = catenate_messages (gmsgid, " before numeric constant");
9862 else if (token_type == CPP_NAME)
9864 message = catenate_messages (gmsgid, " before %qE");
9865 error (message, value);
9866 free (message);
9867 message = NULL;
9869 else if (token_type == CPP_PRAGMA)
9870 message = catenate_messages (gmsgid, " before %<#pragma%>");
9871 else if (token_type == CPP_PRAGMA_EOL)
9872 message = catenate_messages (gmsgid, " before end of line");
9873 else if (token_type == CPP_DECLTYPE)
9874 message = catenate_messages (gmsgid, " before %<decltype%>");
9875 else if (token_type < N_TTYPES)
9877 message = catenate_messages (gmsgid, " before %qs token");
9878 error (message, cpp_type2name (token_type, token_flags));
9879 free (message);
9880 message = NULL;
9882 else
9883 error (gmsgid);
9885 if (message)
9887 error (message);
9888 free (message);
9890 #undef catenate_messages
9893 /* Return the gcc option code associated with the reason for a cpp
9894 message, or 0 if none. */
9896 static int
9897 c_option_controlling_cpp_error (int reason)
9899 const struct cpp_reason_option_codes_t *entry;
9901 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
9903 if (entry->reason == reason)
9904 return entry->option_code;
9906 return 0;
9909 /* Callback from cpp_error for PFILE to print diagnostics from the
9910 preprocessor. The diagnostic is of type LEVEL, with REASON set
9911 to the reason code if LEVEL is represents a warning, at location
9912 LOCATION unless this is after lexing and the compiler's location
9913 should be used instead, with column number possibly overridden by
9914 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9915 the arguments. Returns true if a diagnostic was emitted, false
9916 otherwise. */
9918 bool
9919 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9920 location_t location, unsigned int column_override,
9921 const char *msg, va_list *ap)
9923 diagnostic_info diagnostic;
9924 diagnostic_t dlevel;
9925 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9926 bool ret;
9928 switch (level)
9930 case CPP_DL_WARNING_SYSHDR:
9931 if (flag_no_output)
9932 return false;
9933 global_dc->dc_warn_system_headers = 1;
9934 /* Fall through. */
9935 case CPP_DL_WARNING:
9936 if (flag_no_output)
9937 return false;
9938 dlevel = DK_WARNING;
9939 break;
9940 case CPP_DL_PEDWARN:
9941 if (flag_no_output && !flag_pedantic_errors)
9942 return false;
9943 dlevel = DK_PEDWARN;
9944 break;
9945 case CPP_DL_ERROR:
9946 dlevel = DK_ERROR;
9947 break;
9948 case CPP_DL_ICE:
9949 dlevel = DK_ICE;
9950 break;
9951 case CPP_DL_NOTE:
9952 dlevel = DK_NOTE;
9953 break;
9954 case CPP_DL_FATAL:
9955 dlevel = DK_FATAL;
9956 break;
9957 default:
9958 gcc_unreachable ();
9960 if (done_lexing)
9961 location = input_location;
9962 diagnostic_set_info_translated (&diagnostic, msg, ap,
9963 location, dlevel);
9964 if (column_override)
9965 diagnostic_override_column (&diagnostic, column_override);
9966 diagnostic_override_option_index (&diagnostic,
9967 c_option_controlling_cpp_error (reason));
9968 ret = report_diagnostic (&diagnostic);
9969 if (level == CPP_DL_WARNING_SYSHDR)
9970 global_dc->dc_warn_system_headers = save_warn_system_headers;
9971 return ret;
9974 /* Convert a character from the host to the target execution character
9975 set. cpplib handles this, mostly. */
9977 HOST_WIDE_INT
9978 c_common_to_target_charset (HOST_WIDE_INT c)
9980 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9981 zero-extended under -fno-signed-char. cpplib insists that characters
9982 and character constants are always unsigned. Hence we must convert
9983 back and forth. */
9984 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9986 uc = cpp_host_to_exec_charset (parse_in, uc);
9988 if (flag_signed_char)
9989 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9990 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9991 else
9992 return uc;
9995 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9996 references with an INDIRECT_REF of a constant at the bottom; much like the
9997 traditional rendering of offsetof as a macro. Return the folded result. */
9999 tree
10000 fold_offsetof_1 (tree expr)
10002 tree base, off, t;
10004 switch (TREE_CODE (expr))
10006 case ERROR_MARK:
10007 return expr;
10009 case VAR_DECL:
10010 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10011 return error_mark_node;
10013 case CALL_EXPR:
10014 case TARGET_EXPR:
10015 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10016 return error_mark_node;
10018 case NOP_EXPR:
10019 case INDIRECT_REF:
10020 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10022 error ("cannot apply %<offsetof%> to a non constant address");
10023 return error_mark_node;
10025 return TREE_OPERAND (expr, 0);
10027 case COMPONENT_REF:
10028 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10029 if (base == error_mark_node)
10030 return base;
10032 t = TREE_OPERAND (expr, 1);
10033 if (DECL_C_BIT_FIELD (t))
10035 error ("attempt to take address of bit-field structure "
10036 "member %qD", t);
10037 return error_mark_node;
10039 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10040 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10041 / BITS_PER_UNIT));
10042 break;
10044 case ARRAY_REF:
10045 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10046 if (base == error_mark_node)
10047 return base;
10049 t = TREE_OPERAND (expr, 1);
10051 /* Check if the offset goes beyond the upper bound of the array. */
10052 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10054 tree upbound = array_ref_up_bound (expr);
10055 if (upbound != NULL_TREE
10056 && TREE_CODE (upbound) == INTEGER_CST
10057 && !tree_int_cst_equal (upbound,
10058 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10060 upbound = size_binop (PLUS_EXPR, upbound,
10061 build_int_cst (TREE_TYPE (upbound), 1));
10062 if (tree_int_cst_lt (upbound, t))
10064 tree v;
10066 for (v = TREE_OPERAND (expr, 0);
10067 TREE_CODE (v) == COMPONENT_REF;
10068 v = TREE_OPERAND (v, 0))
10069 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10070 == RECORD_TYPE)
10072 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10073 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10074 if (TREE_CODE (fld_chain) == FIELD_DECL)
10075 break;
10077 if (fld_chain)
10078 break;
10080 /* Don't warn if the array might be considered a poor
10081 man's flexible array member with a very permissive
10082 definition thereof. */
10083 if (TREE_CODE (v) == ARRAY_REF
10084 || TREE_CODE (v) == COMPONENT_REF)
10085 warning (OPT_Warray_bounds,
10086 "index %E denotes an offset "
10087 "greater than size of %qT",
10088 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10093 t = convert (sizetype, t);
10094 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10095 break;
10097 case COMPOUND_EXPR:
10098 /* Handle static members of volatile structs. */
10099 t = TREE_OPERAND (expr, 1);
10100 gcc_assert (TREE_CODE (t) == VAR_DECL);
10101 return fold_offsetof_1 (t);
10103 default:
10104 gcc_unreachable ();
10107 return fold_build_pointer_plus (base, off);
10110 /* Likewise, but convert it to the return type of offsetof. */
10112 tree
10113 fold_offsetof (tree expr)
10115 return convert (size_type_node, fold_offsetof_1 (expr));
10118 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10119 expression, because B will always be true. */
10121 void
10122 warn_for_omitted_condop (location_t location, tree cond)
10124 if (truth_value_p (TREE_CODE (cond)))
10125 warning_at (location, OPT_Wparentheses,
10126 "the omitted middle operand in ?: will always be %<true%>, "
10127 "suggest explicit middle operand");
10130 /* Give an error for storing into ARG, which is 'const'. USE indicates
10131 how ARG was being used. */
10133 void
10134 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10136 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10137 || use == lv_asm);
10138 /* Using this macro rather than (for example) arrays of messages
10139 ensures that all the format strings are checked at compile
10140 time. */
10141 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10142 : (use == lv_increment ? (I) \
10143 : (use == lv_decrement ? (D) : (AS))))
10144 if (TREE_CODE (arg) == COMPONENT_REF)
10146 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10147 error_at (loc, READONLY_MSG (G_("assignment of member "
10148 "%qD in read-only object"),
10149 G_("increment of member "
10150 "%qD in read-only object"),
10151 G_("decrement of member "
10152 "%qD in read-only object"),
10153 G_("member %qD in read-only object "
10154 "used as %<asm%> output")),
10155 TREE_OPERAND (arg, 1));
10156 else
10157 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10158 G_("increment of read-only member %qD"),
10159 G_("decrement of read-only member %qD"),
10160 G_("read-only member %qD used as %<asm%> output")),
10161 TREE_OPERAND (arg, 1));
10163 else if (TREE_CODE (arg) == VAR_DECL)
10164 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10165 G_("increment of read-only variable %qD"),
10166 G_("decrement of read-only variable %qD"),
10167 G_("read-only variable %qD used as %<asm%> output")),
10168 arg);
10169 else if (TREE_CODE (arg) == PARM_DECL)
10170 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10171 G_("increment of read-only parameter %qD"),
10172 G_("decrement of read-only parameter %qD"),
10173 G_("read-only parameter %qD use as %<asm%> output")),
10174 arg);
10175 else if (TREE_CODE (arg) == RESULT_DECL)
10177 gcc_assert (c_dialect_cxx ());
10178 error_at (loc, READONLY_MSG (G_("assignment of "
10179 "read-only named return value %qD"),
10180 G_("increment of "
10181 "read-only named return value %qD"),
10182 G_("decrement of "
10183 "read-only named return value %qD"),
10184 G_("read-only named return value %qD "
10185 "used as %<asm%>output")),
10186 arg);
10188 else if (TREE_CODE (arg) == FUNCTION_DECL)
10189 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10190 G_("increment of function %qD"),
10191 G_("decrement of function %qD"),
10192 G_("function %qD used as %<asm%> output")),
10193 arg);
10194 else
10195 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10196 G_("increment of read-only location %qE"),
10197 G_("decrement of read-only location %qE"),
10198 G_("read-only location %qE used as %<asm%> output")),
10199 arg);
10202 /* Print an error message for an invalid lvalue. USE says
10203 how the lvalue is being used and so selects the error message. LOC
10204 is the location for the error. */
10206 void
10207 lvalue_error (location_t loc, enum lvalue_use use)
10209 switch (use)
10211 case lv_assign:
10212 error_at (loc, "lvalue required as left operand of assignment");
10213 break;
10214 case lv_increment:
10215 error_at (loc, "lvalue required as increment operand");
10216 break;
10217 case lv_decrement:
10218 error_at (loc, "lvalue required as decrement operand");
10219 break;
10220 case lv_addressof:
10221 error_at (loc, "lvalue required as unary %<&%> operand");
10222 break;
10223 case lv_asm:
10224 error_at (loc, "lvalue required in asm statement");
10225 break;
10226 default:
10227 gcc_unreachable ();
10231 /* Print an error message for an invalid indirection of type TYPE.
10232 ERRSTRING is the name of the operator for the indirection. */
10234 void
10235 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10237 switch (errstring)
10239 case RO_NULL:
10240 gcc_assert (c_dialect_cxx ());
10241 error_at (loc, "invalid type argument (have %qT)", type);
10242 break;
10243 case RO_ARRAY_INDEXING:
10244 error_at (loc,
10245 "invalid type argument of array indexing (have %qT)",
10246 type);
10247 break;
10248 case RO_UNARY_STAR:
10249 error_at (loc,
10250 "invalid type argument of unary %<*%> (have %qT)",
10251 type);
10252 break;
10253 case RO_ARROW:
10254 error_at (loc,
10255 "invalid type argument of %<->%> (have %qT)",
10256 type);
10257 break;
10258 case RO_ARROW_STAR:
10259 error_at (loc,
10260 "invalid type argument of %<->*%> (have %qT)",
10261 type);
10262 break;
10263 case RO_IMPLICIT_CONVERSION:
10264 error_at (loc,
10265 "invalid type argument of implicit conversion (have %qT)",
10266 type);
10267 break;
10268 default:
10269 gcc_unreachable ();
10273 /* *PTYPE is an incomplete array. Complete it with a domain based on
10274 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10275 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10276 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10279 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10281 tree maxindex, type, main_type, elt, unqual_elt;
10282 int failure = 0, quals;
10283 hashval_t hashcode = 0;
10284 bool overflow_p = false;
10286 maxindex = size_zero_node;
10287 if (initial_value)
10289 if (TREE_CODE (initial_value) == STRING_CST)
10291 int eltsize
10292 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10293 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10295 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10297 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10299 if (vec_safe_is_empty (v))
10301 if (pedantic)
10302 failure = 3;
10303 maxindex = ssize_int (-1);
10305 else
10307 tree curindex;
10308 unsigned HOST_WIDE_INT cnt;
10309 constructor_elt *ce;
10310 bool fold_p = false;
10312 if ((*v)[0].index)
10313 maxindex = (*v)[0].index, fold_p = true;
10315 curindex = maxindex;
10317 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10319 bool curfold_p = false;
10320 if (ce->index)
10321 curindex = ce->index, curfold_p = true;
10322 else
10324 if (fold_p)
10326 /* Since we treat size types now as ordinary
10327 unsigned types, we need an explicit overflow
10328 check. */
10329 tree orig = curindex;
10330 curindex = fold_convert (sizetype, curindex);
10331 overflow_p |= tree_int_cst_lt (curindex, orig);
10333 curindex = size_binop (PLUS_EXPR, curindex,
10334 size_one_node);
10336 if (tree_int_cst_lt (maxindex, curindex))
10337 maxindex = curindex, fold_p = curfold_p;
10339 if (fold_p)
10341 tree orig = maxindex;
10342 maxindex = fold_convert (sizetype, maxindex);
10343 overflow_p |= tree_int_cst_lt (maxindex, orig);
10347 else
10349 /* Make an error message unless that happened already. */
10350 if (initial_value != error_mark_node)
10351 failure = 1;
10354 else
10356 failure = 2;
10357 if (!do_default)
10358 return failure;
10361 type = *ptype;
10362 elt = TREE_TYPE (type);
10363 quals = TYPE_QUALS (strip_array_types (elt));
10364 if (quals == 0)
10365 unqual_elt = elt;
10366 else
10367 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10369 /* Using build_distinct_type_copy and modifying things afterward instead
10370 of using build_array_type to create a new type preserves all of the
10371 TYPE_LANG_FLAG_? bits that the front end may have set. */
10372 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10373 TREE_TYPE (main_type) = unqual_elt;
10374 TYPE_DOMAIN (main_type)
10375 = build_range_type (TREE_TYPE (maxindex),
10376 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10377 layout_type (main_type);
10379 /* Make sure we have the canonical MAIN_TYPE. */
10380 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10381 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10382 hashcode);
10383 main_type = type_hash_canon (hashcode, main_type);
10385 /* Fix the canonical type. */
10386 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10387 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10388 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10389 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10390 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10391 != TYPE_DOMAIN (main_type)))
10392 TYPE_CANONICAL (main_type)
10393 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10394 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10395 else
10396 TYPE_CANONICAL (main_type) = main_type;
10398 if (quals == 0)
10399 type = main_type;
10400 else
10401 type = c_build_qualified_type (main_type, quals);
10403 if (COMPLETE_TYPE_P (type)
10404 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10405 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10407 error ("size of array is too large");
10408 /* If we proceed with the array type as it is, we'll eventually
10409 crash in tree_to_[su]hwi(). */
10410 type = error_mark_node;
10413 *ptype = type;
10414 return failure;
10417 /* Like c_mark_addressable but don't check register qualifier. */
10418 void
10419 c_common_mark_addressable_vec (tree t)
10421 while (handled_component_p (t))
10422 t = TREE_OPERAND (t, 0);
10423 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10424 return;
10425 TREE_ADDRESSABLE (t) = 1;
10430 /* Used to help initialize the builtin-types.def table. When a type of
10431 the correct size doesn't exist, use error_mark_node instead of NULL.
10432 The later results in segfaults even when a decl using the type doesn't
10433 get invoked. */
10435 tree
10436 builtin_type_for_size (int size, bool unsignedp)
10438 tree type = c_common_type_for_size (size, unsignedp);
10439 return type ? type : error_mark_node;
10442 /* A helper function for resolve_overloaded_builtin in resolving the
10443 overloaded __sync_ builtins. Returns a positive power of 2 if the
10444 first operand of PARAMS is a pointer to a supported data type.
10445 Returns 0 if an error is encountered. */
10447 static int
10448 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10450 tree type;
10451 int size;
10453 if (!params)
10455 error ("too few arguments to function %qE", function);
10456 return 0;
10459 type = TREE_TYPE ((*params)[0]);
10460 if (TREE_CODE (type) == ARRAY_TYPE)
10462 /* Force array-to-pointer decay for C++. */
10463 gcc_assert (c_dialect_cxx());
10464 (*params)[0] = default_conversion ((*params)[0]);
10465 type = TREE_TYPE ((*params)[0]);
10467 if (TREE_CODE (type) != POINTER_TYPE)
10468 goto incompatible;
10470 type = TREE_TYPE (type);
10471 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10472 goto incompatible;
10474 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10475 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10476 return size;
10478 incompatible:
10479 error ("incompatible type for argument %d of %qE", 1, function);
10480 return 0;
10483 /* A helper function for resolve_overloaded_builtin. Adds casts to
10484 PARAMS to make arguments match up with those of FUNCTION. Drops
10485 the variadic arguments at the end. Returns false if some error
10486 was encountered; true on success. */
10488 static bool
10489 sync_resolve_params (location_t loc, tree orig_function, tree function,
10490 vec<tree, va_gc> *params, bool orig_format)
10492 function_args_iterator iter;
10493 tree ptype;
10494 unsigned int parmnum;
10496 function_args_iter_init (&iter, TREE_TYPE (function));
10497 /* We've declared the implementation functions to use "volatile void *"
10498 as the pointer parameter, so we shouldn't get any complaints from the
10499 call to check_function_arguments what ever type the user used. */
10500 function_args_iter_next (&iter);
10501 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10502 ptype = TYPE_MAIN_VARIANT (ptype);
10504 /* For the rest of the values, we need to cast these to FTYPE, so that we
10505 don't get warnings for passing pointer types, etc. */
10506 parmnum = 0;
10507 while (1)
10509 tree val, arg_type;
10511 arg_type = function_args_iter_cond (&iter);
10512 /* XXX void_type_node belies the abstraction. */
10513 if (arg_type == void_type_node)
10514 break;
10516 ++parmnum;
10517 if (params->length () <= parmnum)
10519 error_at (loc, "too few arguments to function %qE", orig_function);
10520 return false;
10523 /* Only convert parameters if arg_type is unsigned integer type with
10524 new format sync routines, i.e. don't attempt to convert pointer
10525 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10526 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10527 kinds). */
10528 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10530 /* Ideally for the first conversion we'd use convert_for_assignment
10531 so that we get warnings for anything that doesn't match the pointer
10532 type. This isn't portable across the C and C++ front ends atm. */
10533 val = (*params)[parmnum];
10534 val = convert (ptype, val);
10535 val = convert (arg_type, val);
10536 (*params)[parmnum] = val;
10539 function_args_iter_next (&iter);
10542 /* __atomic routines are not variadic. */
10543 if (!orig_format && params->length () != parmnum + 1)
10545 error_at (loc, "too many arguments to function %qE", orig_function);
10546 return false;
10549 /* The definition of these primitives is variadic, with the remaining
10550 being "an optional list of variables protected by the memory barrier".
10551 No clue what that's supposed to mean, precisely, but we consider all
10552 call-clobbered variables to be protected so we're safe. */
10553 params->truncate (parmnum + 1);
10555 return true;
10558 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10559 RESULT to make it match the type of the first pointer argument in
10560 PARAMS. */
10562 static tree
10563 sync_resolve_return (tree first_param, tree result, bool orig_format)
10565 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10566 tree rtype = TREE_TYPE (result);
10567 ptype = TYPE_MAIN_VARIANT (ptype);
10569 /* New format doesn't require casting unless the types are the same size. */
10570 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10571 return convert (ptype, result);
10572 else
10573 return result;
10576 /* This function verifies the PARAMS to generic atomic FUNCTION.
10577 It returns the size if all the parameters are the same size, otherwise
10578 0 is returned if the parameters are invalid. */
10580 static int
10581 get_atomic_generic_size (location_t loc, tree function,
10582 vec<tree, va_gc> *params)
10584 unsigned int n_param;
10585 unsigned int n_model;
10586 unsigned int x;
10587 int size_0;
10588 tree type_0;
10590 /* Determine the parameter makeup. */
10591 switch (DECL_FUNCTION_CODE (function))
10593 case BUILT_IN_ATOMIC_EXCHANGE:
10594 n_param = 4;
10595 n_model = 1;
10596 break;
10597 case BUILT_IN_ATOMIC_LOAD:
10598 case BUILT_IN_ATOMIC_STORE:
10599 n_param = 3;
10600 n_model = 1;
10601 break;
10602 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10603 n_param = 6;
10604 n_model = 2;
10605 break;
10606 default:
10607 gcc_unreachable ();
10610 if (vec_safe_length (params) != n_param)
10612 error_at (loc, "incorrect number of arguments to function %qE", function);
10613 return 0;
10616 /* Get type of first parameter, and determine its size. */
10617 type_0 = TREE_TYPE ((*params)[0]);
10618 if (TREE_CODE (type_0) == ARRAY_TYPE)
10620 /* Force array-to-pointer decay for C++. */
10621 gcc_assert (c_dialect_cxx());
10622 (*params)[0] = default_conversion ((*params)[0]);
10623 type_0 = TREE_TYPE ((*params)[0]);
10625 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10627 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10628 function);
10629 return 0;
10632 /* Types must be compile time constant sizes. */
10633 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10635 error_at (loc,
10636 "argument 1 of %qE must be a pointer to a constant size type",
10637 function);
10638 return 0;
10641 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10643 /* Zero size objects are not allowed. */
10644 if (size_0 == 0)
10646 error_at (loc,
10647 "argument 1 of %qE must be a pointer to a nonzero size object",
10648 function);
10649 return 0;
10652 /* Check each other parameter is a pointer and the same size. */
10653 for (x = 0; x < n_param - n_model; x++)
10655 int size;
10656 tree type = TREE_TYPE ((*params)[x]);
10657 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10658 if (n_param == 6 && x == 3)
10659 continue;
10660 if (!POINTER_TYPE_P (type))
10662 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10663 function);
10664 return 0;
10666 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10667 size = type_size ? tree_to_uhwi (type_size) : 0;
10668 if (size != size_0)
10670 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10671 function);
10672 return 0;
10676 /* Check memory model parameters for validity. */
10677 for (x = n_param - n_model ; x < n_param; x++)
10679 tree p = (*params)[x];
10680 if (TREE_CODE (p) == INTEGER_CST)
10682 int i = tree_to_uhwi (p);
10683 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10685 warning_at (loc, OPT_Winvalid_memory_model,
10686 "invalid memory model argument %d of %qE", x + 1,
10687 function);
10690 else
10691 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10693 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10694 function);
10695 return 0;
10699 return size_0;
10703 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10704 at the beginning of the parameter list PARAMS representing the size of the
10705 objects. This is to match the library ABI requirement. LOC is the location
10706 of the function call.
10707 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10708 returned to allow the external call to be constructed. */
10710 static tree
10711 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10712 vec<tree, va_gc> *params)
10714 tree size_node;
10716 /* Insert a SIZE_T parameter as the first param. If there isn't
10717 enough space, allocate a new vector and recursively re-build with that. */
10718 if (!params->space (1))
10720 unsigned int z, len;
10721 vec<tree, va_gc> *v;
10722 tree f;
10724 len = params->length ();
10725 vec_alloc (v, len + 1);
10726 v->quick_push (build_int_cst (size_type_node, n));
10727 for (z = 0; z < len; z++)
10728 v->quick_push ((*params)[z]);
10729 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10730 vec_free (v);
10731 return f;
10734 /* Add the size parameter and leave as a function call for processing. */
10735 size_node = build_int_cst (size_type_node, n);
10736 params->quick_insert (0, size_node);
10737 return NULL_TREE;
10741 /* Return whether atomic operations for naturally aligned N-byte
10742 arguments are supported, whether inline or through libatomic. */
10743 static bool
10744 atomic_size_supported_p (int n)
10746 switch (n)
10748 case 1:
10749 case 2:
10750 case 4:
10751 case 8:
10752 return true;
10754 case 16:
10755 return targetm.scalar_mode_supported_p (TImode);
10757 default:
10758 return false;
10762 /* This will process an __atomic_exchange function call, determine whether it
10763 needs to be mapped to the _N variation, or turned into a library call.
10764 LOC is the location of the builtin call.
10765 FUNCTION is the DECL that has been invoked;
10766 PARAMS is the argument list for the call. The return value is non-null
10767 TRUE is returned if it is translated into the proper format for a call to the
10768 external library, and NEW_RETURN is set the tree for that function.
10769 FALSE is returned if processing for the _N variation is required, and
10770 NEW_RETURN is set to the the return value the result is copied into. */
10771 static bool
10772 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10773 vec<tree, va_gc> *params, tree *new_return)
10775 tree p0, p1, p2, p3;
10776 tree I_type, I_type_ptr;
10777 int n = get_atomic_generic_size (loc, function, params);
10779 /* Size of 0 is an error condition. */
10780 if (n == 0)
10782 *new_return = error_mark_node;
10783 return true;
10786 /* If not a lock-free size, change to the library generic format. */
10787 if (!atomic_size_supported_p (n))
10789 *new_return = add_atomic_size_parameter (n, loc, function, params);
10790 return true;
10793 /* Otherwise there is a lockfree match, transform the call from:
10794 void fn(T* mem, T* desired, T* return, model)
10795 into
10796 *return = (T) (fn (In* mem, (In) *desired, model)) */
10798 p0 = (*params)[0];
10799 p1 = (*params)[1];
10800 p2 = (*params)[2];
10801 p3 = (*params)[3];
10803 /* Create pointer to appropriate size. */
10804 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10805 I_type_ptr = build_pointer_type (I_type);
10807 /* Convert object pointer to required type. */
10808 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10809 (*params)[0] = p0;
10810 /* Convert new value to required type, and dereference it. */
10811 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10812 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10813 (*params)[1] = p1;
10815 /* Move memory model to the 3rd position, and end param list. */
10816 (*params)[2] = p3;
10817 params->truncate (3);
10819 /* Convert return pointer and dereference it for later assignment. */
10820 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10822 return false;
10826 /* This will process an __atomic_compare_exchange function call, determine
10827 whether it needs to be mapped to the _N variation, or turned into a lib 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. */
10835 static bool
10836 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10837 vec<tree, va_gc> *params,
10838 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 /* The library generic format does not have the weak parameter, so
10855 remove it from the param list. Since a parameter has been removed,
10856 we can be sure that there is room for the SIZE_T parameter, meaning
10857 there will not be a recursive rebuilding of the parameter list, so
10858 there is no danger this will be done twice. */
10859 if (n > 0)
10861 (*params)[3] = (*params)[4];
10862 (*params)[4] = (*params)[5];
10863 params->truncate (5);
10865 *new_return = add_atomic_size_parameter (n, loc, function, params);
10866 return true;
10869 /* Otherwise, there is a match, so the call needs to be transformed from:
10870 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10871 into
10872 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10874 p0 = (*params)[0];
10875 p1 = (*params)[1];
10876 p2 = (*params)[2];
10878 /* Create pointer to appropriate size. */
10879 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10880 I_type_ptr = build_pointer_type (I_type);
10882 /* Convert object pointer to required type. */
10883 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10884 (*params)[0] = p0;
10886 /* Convert expected pointer to required type. */
10887 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10888 (*params)[1] = p1;
10890 /* Convert desired value to required type, and dereference it. */
10891 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10892 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10893 (*params)[2] = p2;
10895 /* The rest of the parameters are fine. NULL means no special return value
10896 processing.*/
10897 *new_return = NULL;
10898 return false;
10902 /* This will process an __atomic_load function call, determine whether it
10903 needs to be mapped to the _N variation, or turned into a library call.
10904 LOC is the location of the builtin call.
10905 FUNCTION is the DECL that has been invoked;
10906 PARAMS is the argument list for the call. The return value is non-null
10907 TRUE is returned if it is translated into the proper format for a call to the
10908 external library, and NEW_RETURN is set the tree for that function.
10909 FALSE is returned if processing for the _N variation is required, and
10910 NEW_RETURN is set to the the return value the result is copied into. */
10912 static bool
10913 resolve_overloaded_atomic_load (location_t loc, tree function,
10914 vec<tree, va_gc> *params, tree *new_return)
10916 tree p0, p1, p2;
10917 tree I_type, I_type_ptr;
10918 int n = get_atomic_generic_size (loc, function, params);
10920 /* Size of 0 is an error condition. */
10921 if (n == 0)
10923 *new_return = error_mark_node;
10924 return true;
10927 /* If not a lock-free size, change to the library generic format. */
10928 if (!atomic_size_supported_p (n))
10930 *new_return = add_atomic_size_parameter (n, loc, function, params);
10931 return true;
10934 /* Otherwise, there is a match, so the call needs to be transformed from:
10935 void fn(T* mem, T* return, model)
10936 into
10937 *return = (T) (fn ((In *) mem, model)) */
10939 p0 = (*params)[0];
10940 p1 = (*params)[1];
10941 p2 = (*params)[2];
10943 /* Create pointer to appropriate size. */
10944 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10945 I_type_ptr = build_pointer_type (I_type);
10947 /* Convert object pointer to required type. */
10948 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10949 (*params)[0] = p0;
10951 /* Move memory model to the 2nd position, and end param list. */
10952 (*params)[1] = p2;
10953 params->truncate (2);
10955 /* Convert return pointer and dereference it for later assignment. */
10956 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10958 return false;
10962 /* This will process an __atomic_store function call, determine whether it
10963 needs to be mapped to the _N variation, or turned into a library call.
10964 LOC is the location of the builtin call.
10965 FUNCTION is the DECL that has been invoked;
10966 PARAMS is the argument list for the call. The return value is non-null
10967 TRUE is returned if it is translated into the proper format for a call to the
10968 external library, and NEW_RETURN is set the tree for that function.
10969 FALSE is returned if processing for the _N variation is required, and
10970 NEW_RETURN is set to the the return value the result is copied into. */
10972 static bool
10973 resolve_overloaded_atomic_store (location_t loc, tree function,
10974 vec<tree, va_gc> *params, tree *new_return)
10976 tree p0, p1;
10977 tree I_type, I_type_ptr;
10978 int n = get_atomic_generic_size (loc, function, params);
10980 /* Size of 0 is an error condition. */
10981 if (n == 0)
10983 *new_return = error_mark_node;
10984 return true;
10987 /* If not a lock-free size, change to the library generic format. */
10988 if (!atomic_size_supported_p (n))
10990 *new_return = add_atomic_size_parameter (n, loc, function, params);
10991 return true;
10994 /* Otherwise, there is a match, so the call needs to be transformed from:
10995 void fn(T* mem, T* value, model)
10996 into
10997 fn ((In *) mem, (In) *value, model) */
10999 p0 = (*params)[0];
11000 p1 = (*params)[1];
11002 /* Create pointer to appropriate size. */
11003 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11004 I_type_ptr = build_pointer_type (I_type);
11006 /* Convert object pointer to required type. */
11007 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11008 (*params)[0] = p0;
11010 /* Convert new value to required type, and dereference it. */
11011 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11012 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11013 (*params)[1] = p1;
11015 /* The memory model is in the right spot already. Return is void. */
11016 *new_return = NULL_TREE;
11018 return false;
11022 /* Some builtin functions are placeholders for other expressions. This
11023 function should be called immediately after parsing the call expression
11024 before surrounding code has committed to the type of the expression.
11026 LOC is the location of the builtin call.
11028 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11029 PARAMS is the argument list for the call. The return value is non-null
11030 when expansion is complete, and null if normal processing should
11031 continue. */
11033 tree
11034 resolve_overloaded_builtin (location_t loc, tree function,
11035 vec<tree, va_gc> *params)
11037 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11038 bool orig_format = true;
11039 tree new_return = NULL_TREE;
11041 switch (DECL_BUILT_IN_CLASS (function))
11043 case BUILT_IN_NORMAL:
11044 break;
11045 case BUILT_IN_MD:
11046 if (targetm.resolve_overloaded_builtin)
11047 return targetm.resolve_overloaded_builtin (loc, function, params);
11048 else
11049 return NULL_TREE;
11050 default:
11051 return NULL_TREE;
11054 /* Handle BUILT_IN_NORMAL here. */
11055 switch (orig_code)
11057 case BUILT_IN_ATOMIC_EXCHANGE:
11058 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11059 case BUILT_IN_ATOMIC_LOAD:
11060 case BUILT_IN_ATOMIC_STORE:
11062 /* Handle these 4 together so that they can fall through to the next
11063 case if the call is transformed to an _N variant. */
11064 switch (orig_code)
11066 case BUILT_IN_ATOMIC_EXCHANGE:
11068 if (resolve_overloaded_atomic_exchange (loc, function, params,
11069 &new_return))
11070 return new_return;
11071 /* Change to the _N variant. */
11072 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11073 break;
11076 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11078 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11079 params,
11080 &new_return))
11081 return new_return;
11082 /* Change to the _N variant. */
11083 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11084 break;
11086 case BUILT_IN_ATOMIC_LOAD:
11088 if (resolve_overloaded_atomic_load (loc, function, params,
11089 &new_return))
11090 return new_return;
11091 /* Change to the _N variant. */
11092 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11093 break;
11095 case BUILT_IN_ATOMIC_STORE:
11097 if (resolve_overloaded_atomic_store (loc, function, params,
11098 &new_return))
11099 return new_return;
11100 /* Change to the _N variant. */
11101 orig_code = BUILT_IN_ATOMIC_STORE_N;
11102 break;
11104 default:
11105 gcc_unreachable ();
11107 /* Fallthrough to the normal processing. */
11109 case BUILT_IN_ATOMIC_EXCHANGE_N:
11110 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11111 case BUILT_IN_ATOMIC_LOAD_N:
11112 case BUILT_IN_ATOMIC_STORE_N:
11113 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11114 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11115 case BUILT_IN_ATOMIC_AND_FETCH_N:
11116 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11117 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11118 case BUILT_IN_ATOMIC_OR_FETCH_N:
11119 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11120 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11121 case BUILT_IN_ATOMIC_FETCH_AND_N:
11122 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11123 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11124 case BUILT_IN_ATOMIC_FETCH_OR_N:
11126 orig_format = false;
11127 /* Fallthru for parameter processing. */
11129 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11130 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11131 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11132 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11133 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11134 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11135 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11136 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11137 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11138 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11139 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11140 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11141 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11142 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11143 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11144 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11146 int n = sync_resolve_size (function, params);
11147 tree new_function, first_param, result;
11148 enum built_in_function fncode;
11150 if (n == 0)
11151 return error_mark_node;
11153 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11154 new_function = builtin_decl_explicit (fncode);
11155 if (!sync_resolve_params (loc, function, new_function, params,
11156 orig_format))
11157 return error_mark_node;
11159 first_param = (*params)[0];
11160 result = build_function_call_vec (loc, vNULL, new_function, params,
11161 NULL);
11162 if (result == error_mark_node)
11163 return result;
11164 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11165 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11166 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11167 result = sync_resolve_return (first_param, result, orig_format);
11169 /* If new_return is set, assign function to that expr and cast the
11170 result to void since the generic interface returned void. */
11171 if (new_return)
11173 /* Cast function result from I{1,2,4,8,16} to the required type. */
11174 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11175 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11176 result);
11177 TREE_SIDE_EFFECTS (result) = 1;
11178 protected_set_expr_location (result, loc);
11179 result = convert (void_type_node, result);
11181 return result;
11184 default:
11185 return NULL_TREE;
11189 /* vector_types_compatible_elements_p is used in type checks of vectors
11190 values used as operands of binary operators. Where it returns true, and
11191 the other checks of the caller succeed (being vector types in he first
11192 place, and matching number of elements), we can just treat the types
11193 as essentially the same.
11194 Contrast with vector_targets_convertible_p, which is used for vector
11195 pointer types, and vector_types_convertible_p, which will allow
11196 language-specific matches under the control of flag_lax_vector_conversions,
11197 and might still require a conversion. */
11198 /* True if vector types T1 and T2 can be inputs to the same binary
11199 operator without conversion.
11200 We don't check the overall vector size here because some of our callers
11201 want to give different error messages when the vectors are compatible
11202 except for the element count. */
11204 bool
11205 vector_types_compatible_elements_p (tree t1, tree t2)
11207 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11208 t1 = TREE_TYPE (t1);
11209 t2 = TREE_TYPE (t2);
11211 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11213 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11214 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11215 || c2 == FIXED_POINT_TYPE));
11217 t1 = c_common_signed_type (t1);
11218 t2 = c_common_signed_type (t2);
11219 /* Equality works here because c_common_signed_type uses
11220 TYPE_MAIN_VARIANT. */
11221 if (t1 == t2)
11222 return true;
11223 if (opaque && c1 == c2
11224 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11225 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11226 return true;
11227 return false;
11230 /* Check for missing format attributes on function pointers. LTYPE is
11231 the new type or left-hand side type. RTYPE is the old type or
11232 right-hand side type. Returns TRUE if LTYPE is missing the desired
11233 attribute. */
11235 bool
11236 check_missing_format_attribute (tree ltype, tree rtype)
11238 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11239 tree ra;
11241 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11242 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11243 break;
11244 if (ra)
11246 tree la;
11247 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11248 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11249 break;
11250 return !la;
11252 else
11253 return false;
11256 /* Subscripting with type char is likely to lose on a machine where
11257 chars are signed. So warn on any machine, but optionally. Don't
11258 warn for unsigned char since that type is safe. Don't warn for
11259 signed char because anyone who uses that must have done so
11260 deliberately. Furthermore, we reduce the false positive load by
11261 warning only for non-constant value of type char. */
11263 void
11264 warn_array_subscript_with_type_char (tree index)
11266 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11267 && TREE_CODE (index) != INTEGER_CST)
11268 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11271 /* Implement -Wparentheses for the unexpected C precedence rules, to
11272 cover cases like x + y << z which readers are likely to
11273 misinterpret. We have seen an expression in which CODE is a binary
11274 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11275 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11276 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11277 expression was not formed using a binary or unary operator, or it
11278 was enclosed in parentheses. */
11280 void
11281 warn_about_parentheses (location_t loc, enum tree_code code,
11282 enum tree_code code_left, tree arg_left,
11283 enum tree_code code_right, tree arg_right)
11285 if (!warn_parentheses)
11286 return;
11288 /* This macro tests that the expression ARG with original tree code
11289 CODE appears to be a boolean expression. or the result of folding a
11290 boolean expression. */
11291 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11292 (truth_value_p (TREE_CODE (ARG)) \
11293 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11294 /* Folding may create 0 or 1 integers from other expressions. */ \
11295 || ((CODE) != INTEGER_CST \
11296 && (integer_onep (ARG) || integer_zerop (ARG))))
11298 switch (code)
11300 case LSHIFT_EXPR:
11301 if (code_left == PLUS_EXPR)
11302 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11303 "suggest parentheses around %<+%> inside %<<<%>");
11304 else if (code_right == PLUS_EXPR)
11305 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11306 "suggest parentheses around %<+%> inside %<<<%>");
11307 else if (code_left == MINUS_EXPR)
11308 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11309 "suggest parentheses around %<-%> inside %<<<%>");
11310 else if (code_right == MINUS_EXPR)
11311 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11312 "suggest parentheses around %<-%> inside %<<<%>");
11313 return;
11315 case RSHIFT_EXPR:
11316 if (code_left == PLUS_EXPR)
11317 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11318 "suggest parentheses around %<+%> inside %<>>%>");
11319 else if (code_right == PLUS_EXPR)
11320 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11321 "suggest parentheses around %<+%> inside %<>>%>");
11322 else if (code_left == MINUS_EXPR)
11323 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11324 "suggest parentheses around %<-%> inside %<>>%>");
11325 else if (code_right == MINUS_EXPR)
11326 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11327 "suggest parentheses around %<-%> inside %<>>%>");
11328 return;
11330 case TRUTH_ORIF_EXPR:
11331 if (code_left == TRUTH_ANDIF_EXPR)
11332 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11333 "suggest parentheses around %<&&%> within %<||%>");
11334 else if (code_right == TRUTH_ANDIF_EXPR)
11335 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11336 "suggest parentheses around %<&&%> within %<||%>");
11337 return;
11339 case BIT_IOR_EXPR:
11340 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11341 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11342 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11343 "suggest parentheses around arithmetic in operand of %<|%>");
11344 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11345 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11346 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11347 "suggest parentheses around arithmetic in operand of %<|%>");
11348 /* Check cases like x|y==z */
11349 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11350 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11351 "suggest parentheses around comparison in operand of %<|%>");
11352 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11353 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11354 "suggest parentheses around comparison in operand of %<|%>");
11355 /* Check cases like !x | y */
11356 else if (code_left == TRUTH_NOT_EXPR
11357 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11358 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11359 "suggest parentheses around operand of "
11360 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11361 return;
11363 case BIT_XOR_EXPR:
11364 if (code_left == BIT_AND_EXPR
11365 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11366 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11367 "suggest parentheses around arithmetic in operand of %<^%>");
11368 else if (code_right == BIT_AND_EXPR
11369 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11370 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11371 "suggest parentheses around arithmetic in operand of %<^%>");
11372 /* Check cases like x^y==z */
11373 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11374 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11375 "suggest parentheses around comparison in operand of %<^%>");
11376 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11377 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11378 "suggest parentheses around comparison in operand of %<^%>");
11379 return;
11381 case BIT_AND_EXPR:
11382 if (code_left == PLUS_EXPR)
11383 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11384 "suggest parentheses around %<+%> in operand of %<&%>");
11385 else if (code_right == PLUS_EXPR)
11386 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11387 "suggest parentheses around %<+%> in operand of %<&%>");
11388 else if (code_left == MINUS_EXPR)
11389 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11390 "suggest parentheses around %<-%> in operand of %<&%>");
11391 else if (code_right == MINUS_EXPR)
11392 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11393 "suggest parentheses around %<-%> in operand of %<&%>");
11394 /* Check cases like x&y==z */
11395 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11396 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11397 "suggest parentheses around comparison in operand of %<&%>");
11398 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11399 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11400 "suggest parentheses around comparison in operand of %<&%>");
11401 /* Check cases like !x & y */
11402 else if (code_left == TRUTH_NOT_EXPR
11403 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11404 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11405 "suggest parentheses around operand of "
11406 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11407 return;
11409 case EQ_EXPR:
11410 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11411 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11412 "suggest parentheses around comparison in operand of %<==%>");
11413 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11414 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11415 "suggest parentheses around comparison in operand of %<==%>");
11416 return;
11417 case NE_EXPR:
11418 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11419 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11420 "suggest parentheses around comparison in operand of %<!=%>");
11421 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11422 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11423 "suggest parentheses around comparison in operand of %<!=%>");
11424 return;
11426 default:
11427 if (TREE_CODE_CLASS (code) == tcc_comparison)
11429 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11430 && code_left != NE_EXPR && code_left != EQ_EXPR
11431 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11432 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11433 "comparisons like %<X<=Y<=Z%> do not "
11434 "have their mathematical meaning");
11435 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11436 && code_right != NE_EXPR && code_right != EQ_EXPR
11437 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11438 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11439 "comparisons like %<X<=Y<=Z%> do not "
11440 "have their mathematical meaning");
11442 return;
11444 #undef NOT_A_BOOLEAN_EXPR_P
11447 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11449 void
11450 warn_for_unused_label (tree label)
11452 if (!TREE_USED (label))
11454 if (DECL_INITIAL (label))
11455 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11456 else
11457 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11461 /* Warn for division by zero according to the value of DIVISOR. LOC
11462 is the location of the division operator. */
11464 void
11465 warn_for_div_by_zero (location_t loc, tree divisor)
11467 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11468 about division by zero. Do not issue a warning if DIVISOR has a
11469 floating-point type, since we consider 0.0/0.0 a valid way of
11470 generating a NaN. */
11471 if (c_inhibit_evaluation_warnings == 0
11472 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11473 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11476 /* Subroutine of build_binary_op. Give warnings for comparisons
11477 between signed and unsigned quantities that may fail. Do the
11478 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11479 so that casts will be considered, but default promotions won't
11482 LOCATION is the location of the comparison operator.
11484 The arguments of this function map directly to local variables
11485 of build_binary_op. */
11487 void
11488 warn_for_sign_compare (location_t location,
11489 tree orig_op0, tree orig_op1,
11490 tree op0, tree op1,
11491 tree result_type, enum tree_code resultcode)
11493 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11494 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11495 int unsignedp0, unsignedp1;
11497 /* In C++, check for comparison of different enum types. */
11498 if (c_dialect_cxx()
11499 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11500 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11501 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11502 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11504 warning_at (location,
11505 OPT_Wsign_compare, "comparison between types %qT and %qT",
11506 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11509 /* Do not warn if the comparison is being done in a signed type,
11510 since the signed type will only be chosen if it can represent
11511 all the values of the unsigned type. */
11512 if (!TYPE_UNSIGNED (result_type))
11513 /* OK */;
11514 /* Do not warn if both operands are unsigned. */
11515 else if (op0_signed == op1_signed)
11516 /* OK */;
11517 else
11519 tree sop, uop, base_type;
11520 bool ovf;
11522 if (op0_signed)
11523 sop = orig_op0, uop = orig_op1;
11524 else
11525 sop = orig_op1, uop = orig_op0;
11527 STRIP_TYPE_NOPS (sop);
11528 STRIP_TYPE_NOPS (uop);
11529 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11530 ? TREE_TYPE (result_type) : result_type);
11532 /* Do not warn if the signed quantity is an unsuffixed integer
11533 literal (or some static constant expression involving such
11534 literals or a conditional expression involving such literals)
11535 and it is non-negative. */
11536 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11537 /* OK */;
11538 /* Do not warn if the comparison is an equality operation, the
11539 unsigned quantity is an integral constant, and it would fit
11540 in the result if the result were signed. */
11541 else if (TREE_CODE (uop) == INTEGER_CST
11542 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11543 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11544 /* OK */;
11545 /* In C, do not warn if the unsigned quantity is an enumeration
11546 constant and its maximum value would fit in the result if the
11547 result were signed. */
11548 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11549 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11550 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11551 c_common_signed_type (base_type)))
11552 /* OK */;
11553 else
11554 warning_at (location,
11555 OPT_Wsign_compare,
11556 "comparison between signed and unsigned integer expressions");
11559 /* Warn if two unsigned values are being compared in a size larger
11560 than their original size, and one (and only one) is the result of
11561 a `~' operator. This comparison will always fail.
11563 Also warn if one operand is a constant, and the constant does not
11564 have all bits set that are set in the ~ operand when it is
11565 extended. */
11567 op0 = c_common_get_narrower (op0, &unsignedp0);
11568 op1 = c_common_get_narrower (op1, &unsignedp1);
11570 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11571 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11573 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11574 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11575 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11576 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11578 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11580 tree primop;
11581 HOST_WIDE_INT constant, mask;
11582 int unsignedp;
11583 unsigned int bits;
11585 if (tree_fits_shwi_p (op0))
11587 primop = op1;
11588 unsignedp = unsignedp1;
11589 constant = tree_to_shwi (op0);
11591 else
11593 primop = op0;
11594 unsignedp = unsignedp0;
11595 constant = tree_to_shwi (op1);
11598 bits = TYPE_PRECISION (TREE_TYPE (primop));
11599 if (bits < TYPE_PRECISION (result_type)
11600 && bits < HOST_BITS_PER_LONG && unsignedp)
11602 mask = (~ (HOST_WIDE_INT) 0) << bits;
11603 if ((mask & constant) != mask)
11605 if (constant == 0)
11606 warning_at (location, OPT_Wsign_compare,
11607 "promoted ~unsigned is always non-zero");
11608 else
11609 warning_at (location, OPT_Wsign_compare,
11610 "comparison of promoted ~unsigned with constant");
11614 else if (unsignedp0 && unsignedp1
11615 && (TYPE_PRECISION (TREE_TYPE (op0))
11616 < TYPE_PRECISION (result_type))
11617 && (TYPE_PRECISION (TREE_TYPE (op1))
11618 < TYPE_PRECISION (result_type)))
11619 warning_at (location, OPT_Wsign_compare,
11620 "comparison of promoted ~unsigned with unsigned");
11624 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11625 type via c_common_type. If -Wdouble-promotion is in use, and the
11626 conditions for warning have been met, issue a warning. GMSGID is
11627 the warning message. It must have two %T specifiers for the type
11628 that was converted (generally "float") and the type to which it was
11629 converted (generally "double), respectively. LOC is the location
11630 to which the awrning should refer. */
11632 void
11633 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11634 const char *gmsgid, location_t loc)
11636 tree source_type;
11638 if (!warn_double_promotion)
11639 return;
11640 /* If the conversion will not occur at run-time, there is no need to
11641 warn about it. */
11642 if (c_inhibit_evaluation_warnings)
11643 return;
11644 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11645 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11646 return;
11647 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11648 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11649 source_type = type1;
11650 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11651 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11652 source_type = type2;
11653 else
11654 return;
11655 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11658 /* Setup a TYPE_DECL node as a typedef representation.
11660 X is a TYPE_DECL for a typedef statement. Create a brand new
11661 ..._TYPE node (which will be just a variant of the existing
11662 ..._TYPE node with identical properties) and then install X
11663 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11665 The whole point here is to end up with a situation where each
11666 and every ..._TYPE node the compiler creates will be uniquely
11667 associated with AT MOST one node representing a typedef name.
11668 This way, even though the compiler substitutes corresponding
11669 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11670 early on, later parts of the compiler can always do the reverse
11671 translation and get back the corresponding typedef name. For
11672 example, given:
11674 typedef struct S MY_TYPE;
11675 MY_TYPE object;
11677 Later parts of the compiler might only know that `object' was of
11678 type `struct S' if it were not for code just below. With this
11679 code however, later parts of the compiler see something like:
11681 struct S' == struct S
11682 typedef struct S' MY_TYPE;
11683 struct S' object;
11685 And they can then deduce (from the node for type struct S') that
11686 the original object declaration was:
11688 MY_TYPE object;
11690 Being able to do this is important for proper support of protoize,
11691 and also for generating precise symbolic debugging information
11692 which takes full account of the programmer's (typedef) vocabulary.
11694 Obviously, we don't want to generate a duplicate ..._TYPE node if
11695 the TYPE_DECL node that we are now processing really represents a
11696 standard built-in type. */
11698 void
11699 set_underlying_type (tree x)
11701 if (x == error_mark_node)
11702 return;
11703 if (DECL_IS_BUILTIN (x))
11705 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11706 TYPE_NAME (TREE_TYPE (x)) = x;
11708 else if (TREE_TYPE (x) != error_mark_node
11709 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11711 tree tt = TREE_TYPE (x);
11712 DECL_ORIGINAL_TYPE (x) = tt;
11713 tt = build_variant_type_copy (tt);
11714 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11715 TYPE_NAME (tt) = x;
11716 TREE_USED (tt) = TREE_USED (x);
11717 TREE_TYPE (x) = tt;
11721 /* Record the types used by the current global variable declaration
11722 being parsed, so that we can decide later to emit their debug info.
11723 Those types are in types_used_by_cur_var_decl, and we are going to
11724 store them in the types_used_by_vars_hash hash table.
11725 DECL is the declaration of the global variable that has been parsed. */
11727 void
11728 record_types_used_by_current_var_decl (tree decl)
11730 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11732 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11734 tree type = types_used_by_cur_var_decl->pop ();
11735 types_used_by_var_decl_insert (type, decl);
11739 /* If DECL is a typedef that is declared in the current function,
11740 record it for the purpose of -Wunused-local-typedefs. */
11742 void
11743 record_locally_defined_typedef (tree decl)
11745 struct c_language_function *l;
11747 if (!warn_unused_local_typedefs
11748 || cfun == NULL
11749 /* if this is not a locally defined typedef then we are not
11750 interested. */
11751 || !is_typedef_decl (decl)
11752 || !decl_function_context (decl))
11753 return;
11755 l = (struct c_language_function *) cfun->language;
11756 vec_safe_push (l->local_typedefs, decl);
11759 /* If T is a TYPE_DECL declared locally, mark it as used. */
11761 void
11762 maybe_record_typedef_use (tree t)
11764 if (!is_typedef_decl (t))
11765 return;
11767 TREE_USED (t) = true;
11770 /* Warn if there are some unused locally defined typedefs in the
11771 current function. */
11773 void
11774 maybe_warn_unused_local_typedefs (void)
11776 int i;
11777 tree decl;
11778 /* The number of times we have emitted -Wunused-local-typedefs
11779 warnings. If this is different from errorcount, that means some
11780 unrelated errors have been issued. In which case, we'll avoid
11781 emitting "unused-local-typedefs" warnings. */
11782 static int unused_local_typedefs_warn_count;
11783 struct c_language_function *l;
11785 if (cfun == NULL)
11786 return;
11788 if ((l = (struct c_language_function *) cfun->language) == NULL)
11789 return;
11791 if (warn_unused_local_typedefs
11792 && errorcount == unused_local_typedefs_warn_count)
11794 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11795 if (!TREE_USED (decl))
11796 warning_at (DECL_SOURCE_LOCATION (decl),
11797 OPT_Wunused_local_typedefs,
11798 "typedef %qD locally defined but not used", decl);
11799 unused_local_typedefs_warn_count = errorcount;
11802 vec_free (l->local_typedefs);
11805 /* Warn about boolean expression compared with an integer value different
11806 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11807 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11808 are the operands of the comparison. The caller must ensure that
11809 either operand is a boolean expression. */
11811 void
11812 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11813 tree op1)
11815 if (TREE_CODE_CLASS (code) != tcc_comparison)
11816 return;
11818 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11819 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11820 if (!cst)
11821 return;
11823 if (!integer_zerop (cst) && !integer_onep (cst))
11825 int sign = (TREE_CODE (op0) == INTEGER_CST)
11826 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11827 if (code == EQ_EXPR
11828 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11829 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11830 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11831 "with boolean expression is always false", cst);
11832 else
11833 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11834 "with boolean expression is always true", cst);
11838 /* The C and C++ parsers both use vectors to hold function arguments.
11839 For efficiency, we keep a cache of unused vectors. This is the
11840 cache. */
11842 typedef vec<tree, va_gc> *tree_gc_vec;
11843 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11845 /* Return a new vector from the cache. If the cache is empty,
11846 allocate a new vector. These vectors are GC'ed, so it is OK if the
11847 pointer is not released.. */
11849 vec<tree, va_gc> *
11850 make_tree_vector (void)
11852 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11853 return tree_vector_cache->pop ();
11854 else
11856 /* Passing 0 to vec::alloc returns NULL, and our callers require
11857 that we always return a non-NULL value. The vector code uses
11858 4 when growing a NULL vector, so we do too. */
11859 vec<tree, va_gc> *v;
11860 vec_alloc (v, 4);
11861 return v;
11865 /* Release a vector of trees back to the cache. */
11867 void
11868 release_tree_vector (vec<tree, va_gc> *vec)
11870 if (vec != NULL)
11872 vec->truncate (0);
11873 vec_safe_push (tree_vector_cache, vec);
11877 /* Get a new tree vector holding a single tree. */
11879 vec<tree, va_gc> *
11880 make_tree_vector_single (tree t)
11882 vec<tree, va_gc> *ret = make_tree_vector ();
11883 ret->quick_push (t);
11884 return ret;
11887 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11889 vec<tree, va_gc> *
11890 make_tree_vector_from_list (tree list)
11892 vec<tree, va_gc> *ret = make_tree_vector ();
11893 for (; list; list = TREE_CHAIN (list))
11894 vec_safe_push (ret, TREE_VALUE (list));
11895 return ret;
11898 /* Get a new tree vector which is a copy of an existing one. */
11900 vec<tree, va_gc> *
11901 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11903 vec<tree, va_gc> *ret;
11904 unsigned int ix;
11905 tree t;
11907 ret = make_tree_vector ();
11908 vec_safe_reserve (ret, vec_safe_length (orig));
11909 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11910 ret->quick_push (t);
11911 return ret;
11914 /* Return true if KEYWORD starts a type specifier. */
11916 bool
11917 keyword_begins_type_specifier (enum rid keyword)
11919 switch (keyword)
11921 case RID_AUTO_TYPE:
11922 case RID_INT:
11923 case RID_CHAR:
11924 case RID_FLOAT:
11925 case RID_DOUBLE:
11926 case RID_VOID:
11927 case RID_UNSIGNED:
11928 case RID_LONG:
11929 case RID_SHORT:
11930 case RID_SIGNED:
11931 case RID_DFLOAT32:
11932 case RID_DFLOAT64:
11933 case RID_DFLOAT128:
11934 case RID_FRACT:
11935 case RID_ACCUM:
11936 case RID_BOOL:
11937 case RID_WCHAR:
11938 case RID_CHAR16:
11939 case RID_CHAR32:
11940 case RID_SAT:
11941 case RID_COMPLEX:
11942 case RID_TYPEOF:
11943 case RID_STRUCT:
11944 case RID_CLASS:
11945 case RID_UNION:
11946 case RID_ENUM:
11947 return true;
11948 default:
11949 if (keyword >= RID_FIRST_INT_N
11950 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
11951 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
11952 return true;
11953 return false;
11957 /* Return true if KEYWORD names a type qualifier. */
11959 bool
11960 keyword_is_type_qualifier (enum rid keyword)
11962 switch (keyword)
11964 case RID_CONST:
11965 case RID_VOLATILE:
11966 case RID_RESTRICT:
11967 case RID_ATOMIC:
11968 return true;
11969 default:
11970 return false;
11974 /* Return true if KEYWORD names a storage class specifier.
11976 RID_TYPEDEF is not included in this list despite `typedef' being
11977 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11978 such for syntactic convenience only. */
11980 bool
11981 keyword_is_storage_class_specifier (enum rid keyword)
11983 switch (keyword)
11985 case RID_STATIC:
11986 case RID_EXTERN:
11987 case RID_REGISTER:
11988 case RID_AUTO:
11989 case RID_MUTABLE:
11990 case RID_THREAD:
11991 return true;
11992 default:
11993 return false;
11997 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11999 static bool
12000 keyword_is_function_specifier (enum rid keyword)
12002 switch (keyword)
12004 case RID_INLINE:
12005 case RID_NORETURN:
12006 case RID_VIRTUAL:
12007 case RID_EXPLICIT:
12008 return true;
12009 default:
12010 return false;
12014 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12015 declaration-specifier (C99 6.7). */
12017 bool
12018 keyword_is_decl_specifier (enum rid keyword)
12020 if (keyword_is_storage_class_specifier (keyword)
12021 || keyword_is_type_qualifier (keyword)
12022 || keyword_is_function_specifier (keyword))
12023 return true;
12025 switch (keyword)
12027 case RID_TYPEDEF:
12028 case RID_FRIEND:
12029 case RID_CONSTEXPR:
12030 return true;
12031 default:
12032 return false;
12036 /* Initialize language-specific-bits of tree_contains_struct. */
12038 void
12039 c_common_init_ts (void)
12041 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12042 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12043 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12046 /* Build a user-defined numeric literal out of an integer constant type VALUE
12047 with identifier SUFFIX. */
12049 tree
12050 build_userdef_literal (tree suffix_id, tree value,
12051 enum overflow_type overflow, tree num_string)
12053 tree literal = make_node (USERDEF_LITERAL);
12054 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12055 USERDEF_LITERAL_VALUE (literal) = value;
12056 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12057 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12058 return literal;
12061 /* For vector[index], convert the vector to a
12062 pointer of the underlying type. */
12063 void
12064 convert_vector_to_pointer_for_subscript (location_t loc,
12065 tree* vecp, tree index)
12067 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12069 tree type = TREE_TYPE (*vecp);
12070 tree type1;
12072 if (TREE_CODE (index) == INTEGER_CST)
12073 if (!tree_fits_uhwi_p (index)
12074 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12075 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12077 c_common_mark_addressable_vec (*vecp);
12078 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12079 type1 = build_pointer_type (TREE_TYPE (*vecp));
12080 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12081 if (!ref_all
12082 && !DECL_P (*vecp))
12084 /* If the original vector isn't declared may_alias and it
12085 isn't a bare vector look if the subscripting would
12086 alias the vector we subscript, and if not, force ref-all. */
12087 alias_set_type vecset = get_alias_set (*vecp);
12088 alias_set_type sset = get_alias_set (type);
12089 if (!alias_sets_must_conflict_p (sset, vecset)
12090 && !alias_set_subset_of (sset, vecset))
12091 ref_all = true;
12093 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12094 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12095 *vecp = convert (type, *vecp);
12099 /* Determine which of the operands, if any, is a scalar that needs to be
12100 converted to a vector, for the range of operations. */
12101 enum stv_conv
12102 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12103 bool complain)
12105 tree type0 = TREE_TYPE (op0);
12106 tree type1 = TREE_TYPE (op1);
12107 bool integer_only_op = false;
12108 enum stv_conv ret = stv_firstarg;
12110 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12111 || TREE_CODE (type1) == VECTOR_TYPE);
12112 switch (code)
12114 /* Most GENERIC binary expressions require homogeneous arguments.
12115 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12116 argument that is a vector and a second one that is a scalar, so
12117 we never return stv_secondarg for them. */
12118 case RSHIFT_EXPR:
12119 case LSHIFT_EXPR:
12120 if (TREE_CODE (type0) == INTEGER_TYPE
12121 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12123 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12125 if (complain)
12126 error_at (loc, "conversion of scalar %qT to vector %qT "
12127 "involves truncation", type0, type1);
12128 return stv_error;
12130 else
12131 return stv_firstarg;
12133 break;
12135 case BIT_IOR_EXPR:
12136 case BIT_XOR_EXPR:
12137 case BIT_AND_EXPR:
12138 integer_only_op = true;
12139 /* ... fall through ... */
12141 case VEC_COND_EXPR:
12143 case PLUS_EXPR:
12144 case MINUS_EXPR:
12145 case MULT_EXPR:
12146 case TRUNC_DIV_EXPR:
12147 case CEIL_DIV_EXPR:
12148 case FLOOR_DIV_EXPR:
12149 case ROUND_DIV_EXPR:
12150 case EXACT_DIV_EXPR:
12151 case TRUNC_MOD_EXPR:
12152 case FLOOR_MOD_EXPR:
12153 case RDIV_EXPR:
12154 case EQ_EXPR:
12155 case NE_EXPR:
12156 case LE_EXPR:
12157 case GE_EXPR:
12158 case LT_EXPR:
12159 case GT_EXPR:
12160 /* What about UNLT_EXPR? */
12161 if (TREE_CODE (type0) == VECTOR_TYPE)
12163 tree tmp;
12164 ret = stv_secondarg;
12165 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12166 tmp = type0; type0 = type1; type1 = tmp;
12167 tmp = op0; op0 = op1; op1 = tmp;
12170 if (TREE_CODE (type0) == INTEGER_TYPE
12171 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12173 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12175 if (complain)
12176 error_at (loc, "conversion of scalar %qT to vector %qT "
12177 "involves truncation", type0, type1);
12178 return stv_error;
12180 return ret;
12182 else if (!integer_only_op
12183 /* Allow integer --> real conversion if safe. */
12184 && (TREE_CODE (type0) == REAL_TYPE
12185 || TREE_CODE (type0) == INTEGER_TYPE)
12186 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12188 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12190 if (complain)
12191 error_at (loc, "conversion of scalar %qT to vector %qT "
12192 "involves truncation", type0, type1);
12193 return stv_error;
12195 return ret;
12197 default:
12198 break;
12201 return stv_nothing;
12204 /* Return true iff ALIGN is an integral constant that is a fundamental
12205 alignment, as defined by [basic.align] in the c++-11
12206 specifications.
12208 That is:
12210 [A fundamental alignment is represented by an alignment less than or
12211 equal to the greatest alignment supported by the implementation
12212 in all contexts, which is equal to
12213 alignof(max_align_t)]. */
12215 bool
12216 cxx_fundamental_alignment_p (unsigned align)
12218 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12219 TYPE_ALIGN (long_double_type_node)));
12222 /* Return true if T is a pointer to a zero-sized aggregate. */
12224 bool
12225 pointer_to_zero_sized_aggr_p (tree t)
12227 if (!POINTER_TYPE_P (t))
12228 return false;
12229 t = TREE_TYPE (t);
12230 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12233 #include "gt-c-family-c-common.h"