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