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