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