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