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