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