OSDN Git Service

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