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