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