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