OSDN Git Service

9d20d80981c3778cc9db7106ed5628d0a56e8d4f
[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   int i;
2125   const int expr_num_operands = TREE_OPERAND_LENGTH (expr);
2126   tree expr_type = TREE_TYPE (expr);
2127   location_t loc = EXPR_LOC_OR_HERE (expr);
2128
2129   if (!warn_conversion && !warn_sign_conversion)
2130     return;
2131
2132   /* If any operand is artificial, then this expression was generated
2133      by the compiler and we do not warn.  */
2134   for (i = 0; i < expr_num_operands; i++)
2135     {
2136       tree op = TREE_OPERAND (expr, i);
2137       if (op && DECL_P (op) && DECL_ARTIFICIAL (op))
2138         return;
2139     }
2140
2141   switch (TREE_CODE (expr))
2142     {
2143     case EQ_EXPR:
2144     case NE_EXPR:
2145     case LE_EXPR:
2146     case GE_EXPR:
2147     case LT_EXPR:
2148     case GT_EXPR:
2149     case TRUTH_ANDIF_EXPR:
2150     case TRUTH_ORIF_EXPR:
2151     case TRUTH_AND_EXPR:
2152     case TRUTH_OR_EXPR:
2153     case TRUTH_XOR_EXPR:
2154     case TRUTH_NOT_EXPR:
2155       /* Conversion from boolean to a signed:1 bit-field (which only
2156          can hold the values 0 and -1) doesn't lose information - but
2157          it does change the value.  */
2158       if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2159         warning_at (loc, OPT_Wconversion,
2160                     "conversion to %qT from boolean expression", type);
2161       return;
2162
2163     case REAL_CST:
2164     case INTEGER_CST:
2165       if (unsafe_conversion_p (type, expr, true))
2166         warning_at (loc, OPT_Wconversion,
2167                     "conversion to %qT alters %qT constant value",
2168                     type, expr_type);
2169       return;
2170
2171     case COND_EXPR:
2172       {
2173         /* In case of COND_EXPR, if both operands are constants or
2174            COND_EXPR, then we do not care about the type of COND_EXPR,
2175            only about the conversion of each operand.  */
2176         tree op1 = TREE_OPERAND (expr, 1);
2177         tree op2 = TREE_OPERAND (expr, 2);
2178
2179         if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2180              || TREE_CODE (op1) == COND_EXPR)
2181             && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2182                 || TREE_CODE (op2) == COND_EXPR))
2183           {
2184             conversion_warning (type, op1);
2185             conversion_warning (type, op2);
2186             return;
2187           }
2188         /* Fall through.  */
2189       }
2190
2191     default: /* 'expr' is not a constant.  */
2192       if (unsafe_conversion_p (type, expr, true))
2193         warning_at (loc, OPT_Wconversion,
2194                     "conversion to %qT from %qT may alter its value",
2195                     type, expr_type);
2196     }
2197 }
2198
2199 /* Produce warnings after a conversion. RESULT is the result of
2200    converting EXPR to TYPE.  This is a helper function for
2201    convert_and_check and cp_convert_and_check.  */
2202
2203 void
2204 warnings_for_convert_and_check (tree type, tree expr, tree result)
2205 {
2206   if (TREE_CODE (expr) == INTEGER_CST
2207       && (TREE_CODE (type) == INTEGER_TYPE
2208           || TREE_CODE (type) == ENUMERAL_TYPE)
2209       && !int_fits_type_p (expr, type))
2210     {
2211       /* Do not diagnose overflow in a constant expression merely
2212          because a conversion overflowed.  */
2213       if (TREE_OVERFLOW (result))
2214         TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2215
2216       if (TYPE_UNSIGNED (type))
2217         {
2218           /* This detects cases like converting -129 or 256 to
2219              unsigned char.  */
2220           if (!int_fits_type_p (expr, c_common_signed_type (type)))
2221             warning (OPT_Woverflow,
2222                      "large integer implicitly truncated to unsigned type");
2223           else
2224             conversion_warning (type, expr);
2225         }
2226       else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2227         warning (OPT_Woverflow,
2228                  "overflow in implicit constant conversion");
2229       /* No warning for converting 0x80000000 to int.  */
2230       else if (pedantic
2231                && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2232                    || TYPE_PRECISION (TREE_TYPE (expr))
2233                    != TYPE_PRECISION (type)))
2234         warning (OPT_Woverflow,
2235                  "overflow in implicit constant conversion");
2236
2237       else
2238         conversion_warning (type, expr);
2239     }
2240   else if ((TREE_CODE (result) == INTEGER_CST
2241             || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2242     warning (OPT_Woverflow,
2243              "overflow in implicit constant conversion");
2244   else
2245     conversion_warning (type, expr);
2246 }
2247
2248
2249 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2250    Invoke this function on every expression that is converted implicitly,
2251    i.e. because of language rules and not because of an explicit cast.  */
2252
2253 tree
2254 convert_and_check (tree type, tree expr)
2255 {
2256   tree result;
2257   tree expr_for_warning;
2258
2259   /* Convert from a value with possible excess precision rather than
2260      via the semantic type, but do not warn about values not fitting
2261      exactly in the semantic type.  */
2262   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2263     {
2264       tree orig_type = TREE_TYPE (expr);
2265       expr = TREE_OPERAND (expr, 0);
2266       expr_for_warning = convert (orig_type, expr);
2267       if (orig_type == type)
2268         return expr_for_warning;
2269     }
2270   else
2271     expr_for_warning = expr;
2272
2273   if (TREE_TYPE (expr) == type)
2274     return expr;
2275
2276   result = convert (type, expr);
2277
2278   if (c_inhibit_evaluation_warnings == 0
2279       && !TREE_OVERFLOW_P (expr)
2280       && result != error_mark_node)
2281     warnings_for_convert_and_check (type, expr_for_warning, result);
2282
2283   return result;
2284 }
2285 \f
2286 /* A node in a list that describes references to variables (EXPR), which are
2287    either read accesses if WRITER is zero, or write accesses, in which case
2288    WRITER is the parent of EXPR.  */
2289 struct tlist
2290 {
2291   struct tlist *next;
2292   tree expr, writer;
2293 };
2294
2295 /* Used to implement a cache the results of a call to verify_tree.  We only
2296    use this for SAVE_EXPRs.  */
2297 struct tlist_cache
2298 {
2299   struct tlist_cache *next;
2300   struct tlist *cache_before_sp;
2301   struct tlist *cache_after_sp;
2302   tree expr;
2303 };
2304
2305 /* Obstack to use when allocating tlist structures, and corresponding
2306    firstobj.  */
2307 static struct obstack tlist_obstack;
2308 static char *tlist_firstobj = 0;
2309
2310 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2311    warnings.  */
2312 static struct tlist *warned_ids;
2313 /* SAVE_EXPRs need special treatment.  We process them only once and then
2314    cache the results.  */
2315 static struct tlist_cache *save_expr_cache;
2316
2317 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2318 static void merge_tlist (struct tlist **, struct tlist *, int);
2319 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2320 static int warning_candidate_p (tree);
2321 static bool candidate_equal_p (const_tree, const_tree);
2322 static void warn_for_collisions (struct tlist *);
2323 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2324 static struct tlist *new_tlist (struct tlist *, tree, tree);
2325
2326 /* Create a new struct tlist and fill in its fields.  */
2327 static struct tlist *
2328 new_tlist (struct tlist *next, tree t, tree writer)
2329 {
2330   struct tlist *l;
2331   l = XOBNEW (&tlist_obstack, struct tlist);
2332   l->next = next;
2333   l->expr = t;
2334   l->writer = writer;
2335   return l;
2336 }
2337
2338 /* Add duplicates of the nodes found in ADD to the list *TO.  If EXCLUDE_WRITER
2339    is nonnull, we ignore any node we find which has a writer equal to it.  */
2340
2341 static void
2342 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2343 {
2344   while (add)
2345     {
2346       struct tlist *next = add->next;
2347       if (!copy)
2348         add->next = *to;
2349       if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2350         *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2351       add = next;
2352     }
2353 }
2354
2355 /* Merge the nodes of ADD into TO.  This merging process is done so that for
2356    each variable that already exists in TO, no new node is added; however if
2357    there is a write access recorded in ADD, and an occurrence on TO is only
2358    a read access, then the occurrence in TO will be modified to record the
2359    write.  */
2360
2361 static void
2362 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2363 {
2364   struct tlist **end = to;
2365
2366   while (*end)
2367     end = &(*end)->next;
2368
2369   while (add)
2370     {
2371       int found = 0;
2372       struct tlist *tmp2;
2373       struct tlist *next = add->next;
2374
2375       for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2376         if (candidate_equal_p (tmp2->expr, add->expr))
2377           {
2378             found = 1;
2379             if (!tmp2->writer)
2380               tmp2->writer = add->writer;
2381           }
2382       if (!found)
2383         {
2384           *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2385           end = &(*end)->next;
2386           *end = 0;
2387         }
2388       add = next;
2389     }
2390 }
2391
2392 /* WRITTEN is a variable, WRITER is its parent.  Warn if any of the variable
2393    references in list LIST conflict with it, excluding reads if ONLY writers
2394    is nonzero.  */
2395
2396 static void
2397 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2398                        int only_writes)
2399 {
2400   struct tlist *tmp;
2401
2402   /* Avoid duplicate warnings.  */
2403   for (tmp = warned_ids; tmp; tmp = tmp->next)
2404     if (candidate_equal_p (tmp->expr, written))
2405       return;
2406
2407   while (list)
2408     {
2409       if (candidate_equal_p (list->expr, written)
2410           && !candidate_equal_p (list->writer, writer)
2411           && (!only_writes || list->writer))
2412         {
2413           warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2414           warning_at (EXPR_LOC_OR_HERE (writer),
2415                       OPT_Wsequence_point, "operation on %qE may be undefined",
2416                       list->expr);
2417         }
2418       list = list->next;
2419     }
2420 }
2421
2422 /* Given a list LIST of references to variables, find whether any of these
2423    can cause conflicts due to missing sequence points.  */
2424
2425 static void
2426 warn_for_collisions (struct tlist *list)
2427 {
2428   struct tlist *tmp;
2429
2430   for (tmp = list; tmp; tmp = tmp->next)
2431     {
2432       if (tmp->writer)
2433         warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2434     }
2435 }
2436
2437 /* Return nonzero if X is a tree that can be verified by the sequence point
2438    warnings.  */
2439 static int
2440 warning_candidate_p (tree x)
2441 {
2442   if (DECL_P (x) && DECL_ARTIFICIAL (x))
2443     return 0;
2444
2445   if (TREE_CODE (x) == BLOCK)
2446     return 0;
2447
2448   /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2449      (lvalue_p) crash on TRY/CATCH. */
2450   if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2451     return 0;
2452
2453   if (!lvalue_p (x))
2454     return 0;
2455
2456   /* No point to track non-const calls, they will never satisfy
2457      operand_equal_p.  */
2458   if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2459     return 0;
2460
2461   if (TREE_CODE (x) == STRING_CST)
2462     return 0;
2463
2464   return 1;
2465 }
2466
2467 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2468 static bool
2469 candidate_equal_p (const_tree x, const_tree y)
2470 {
2471   return (x == y) || (x && y && operand_equal_p (x, y, 0));
2472 }
2473
2474 /* Walk the tree X, and record accesses to variables.  If X is written by the
2475    parent tree, WRITER is the parent.
2476    We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP.  If this
2477    expression or its only operand forces a sequence point, then everything up
2478    to the sequence point is stored in PBEFORE_SP.  Everything else gets stored
2479    in PNO_SP.
2480    Once we return, we will have emitted warnings if any subexpression before
2481    such a sequence point could be undefined.  On a higher level, however, the
2482    sequence point may not be relevant, and we'll merge the two lists.
2483
2484    Example: (b++, a) + b;
2485    The call that processes the COMPOUND_EXPR will store the increment of B
2486    in PBEFORE_SP, and the use of A in PNO_SP.  The higher-level call that
2487    processes the PLUS_EXPR will need to merge the two lists so that
2488    eventually, all accesses end up on the same list (and we'll warn about the
2489    unordered subexpressions b++ and b.
2490
2491    A note on merging.  If we modify the former example so that our expression
2492    becomes
2493      (b++, b) + a
2494    care must be taken not simply to add all three expressions into the final
2495    PNO_SP list.  The function merge_tlist takes care of that by merging the
2496    before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2497    way, so that no more than one access to B is recorded.  */
2498
2499 static void
2500 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2501              tree writer)
2502 {
2503   struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2504   enum tree_code code;
2505   enum tree_code_class cl;
2506
2507   /* X may be NULL if it is the operand of an empty statement expression
2508      ({ }).  */
2509   if (x == NULL)
2510     return;
2511
2512  restart:
2513   code = TREE_CODE (x);
2514   cl = TREE_CODE_CLASS (code);
2515
2516   if (warning_candidate_p (x))
2517     *pno_sp = new_tlist (*pno_sp, x, writer);
2518
2519   switch (code)
2520     {
2521     case CONSTRUCTOR:
2522       return;
2523
2524     case COMPOUND_EXPR:
2525     case TRUTH_ANDIF_EXPR:
2526     case TRUTH_ORIF_EXPR:
2527       tmp_before = tmp_nosp = tmp_list3 = 0;
2528       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2529       warn_for_collisions (tmp_nosp);
2530       merge_tlist (pbefore_sp, tmp_before, 0);
2531       merge_tlist (pbefore_sp, tmp_nosp, 0);
2532       verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2533       merge_tlist (pbefore_sp, tmp_list3, 0);
2534       return;
2535
2536     case COND_EXPR:
2537       tmp_before = tmp_list2 = 0;
2538       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2539       warn_for_collisions (tmp_list2);
2540       merge_tlist (pbefore_sp, tmp_before, 0);
2541       merge_tlist (pbefore_sp, tmp_list2, 1);
2542
2543       tmp_list3 = tmp_nosp = 0;
2544       verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2545       warn_for_collisions (tmp_nosp);
2546       merge_tlist (pbefore_sp, tmp_list3, 0);
2547
2548       tmp_list3 = tmp_list2 = 0;
2549       verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2550       warn_for_collisions (tmp_list2);
2551       merge_tlist (pbefore_sp, tmp_list3, 0);
2552       /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2553          two first, to avoid warning for (a ? b++ : b++).  */
2554       merge_tlist (&tmp_nosp, tmp_list2, 0);
2555       add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2556       return;
2557
2558     case PREDECREMENT_EXPR:
2559     case PREINCREMENT_EXPR:
2560     case POSTDECREMENT_EXPR:
2561     case POSTINCREMENT_EXPR:
2562       verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2563       return;
2564
2565     case MODIFY_EXPR:
2566       tmp_before = tmp_nosp = tmp_list3 = 0;
2567       verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2568       verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2569       /* Expressions inside the LHS are not ordered wrt. the sequence points
2570          in the RHS.  Example:
2571            *a = (a++, 2)
2572          Despite the fact that the modification of "a" is in the before_sp
2573          list (tmp_before), it conflicts with the use of "a" in the LHS.
2574          We can handle this by adding the contents of tmp_list3
2575          to those of tmp_before, and redoing the collision warnings for that
2576          list.  */
2577       add_tlist (&tmp_before, tmp_list3, x, 1);
2578       warn_for_collisions (tmp_before);
2579       /* Exclude the LHS itself here; we first have to merge it into the
2580          tmp_nosp list.  This is done to avoid warning for "a = a"; if we
2581          didn't exclude the LHS, we'd get it twice, once as a read and once
2582          as a write.  */
2583       add_tlist (pno_sp, tmp_list3, x, 0);
2584       warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2585
2586       merge_tlist (pbefore_sp, tmp_before, 0);
2587       if (warning_candidate_p (TREE_OPERAND (x, 0)))
2588         merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2589       add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2590       return;
2591
2592     case CALL_EXPR:
2593       /* We need to warn about conflicts among arguments and conflicts between
2594          args and the function address.  Side effects of the function address,
2595          however, are not ordered by the sequence point of the call.  */
2596       {
2597         call_expr_arg_iterator iter;
2598         tree arg;
2599         tmp_before = tmp_nosp = 0;
2600         verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2601         FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2602           {
2603             tmp_list2 = tmp_list3 = 0;
2604             verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2605             merge_tlist (&tmp_list3, tmp_list2, 0);
2606             add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2607           }
2608         add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2609         warn_for_collisions (tmp_before);
2610         add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2611         return;
2612       }
2613
2614     case TREE_LIST:
2615       /* Scan all the list, e.g. indices of multi dimensional array.  */
2616       while (x)
2617         {
2618           tmp_before = tmp_nosp = 0;
2619           verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2620           merge_tlist (&tmp_nosp, tmp_before, 0);
2621           add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2622           x = TREE_CHAIN (x);
2623         }
2624       return;
2625
2626     case SAVE_EXPR:
2627       {
2628         struct tlist_cache *t;
2629         for (t = save_expr_cache; t; t = t->next)
2630           if (candidate_equal_p (t->expr, x))
2631             break;
2632
2633         if (!t)
2634           {
2635             t = XOBNEW (&tlist_obstack, struct tlist_cache);
2636             t->next = save_expr_cache;
2637             t->expr = x;
2638             save_expr_cache = t;
2639
2640             tmp_before = tmp_nosp = 0;
2641             verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2642             warn_for_collisions (tmp_nosp);
2643
2644             tmp_list3 = 0;
2645             while (tmp_nosp)
2646               {
2647                 struct tlist *t = tmp_nosp;
2648                 tmp_nosp = t->next;
2649                 merge_tlist (&tmp_list3, t, 0);
2650               }
2651             t->cache_before_sp = tmp_before;
2652             t->cache_after_sp = tmp_list3;
2653           }
2654         merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2655         add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2656         return;
2657       }
2658
2659     case ADDR_EXPR:
2660       x = TREE_OPERAND (x, 0);
2661       if (DECL_P (x))
2662         return;
2663       writer = 0;
2664       goto restart;
2665
2666     default:
2667       /* For other expressions, simply recurse on their operands.
2668          Manual tail recursion for unary expressions.
2669          Other non-expressions need not be processed.  */
2670       if (cl == tcc_unary)
2671         {
2672           x = TREE_OPERAND (x, 0);
2673           writer = 0;
2674           goto restart;
2675         }
2676       else if (IS_EXPR_CODE_CLASS (cl))
2677         {
2678           int lp;
2679           int max = TREE_OPERAND_LENGTH (x);
2680           for (lp = 0; lp < max; lp++)
2681             {
2682               tmp_before = tmp_nosp = 0;
2683               verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2684               merge_tlist (&tmp_nosp, tmp_before, 0);
2685               add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2686             }
2687         }
2688       return;
2689     }
2690 }
2691
2692 /* Try to warn for undefined behavior in EXPR due to missing sequence
2693    points.  */
2694
2695 DEBUG_FUNCTION void
2696 verify_sequence_points (tree expr)
2697 {
2698   struct tlist *before_sp = 0, *after_sp = 0;
2699
2700   warned_ids = 0;
2701   save_expr_cache = 0;
2702   if (tlist_firstobj == 0)
2703     {
2704       gcc_obstack_init (&tlist_obstack);
2705       tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2706     }
2707
2708   verify_tree (expr, &before_sp, &after_sp, 0);
2709   warn_for_collisions (after_sp);
2710   obstack_free (&tlist_obstack, tlist_firstobj);
2711 }
2712 \f
2713 /* Validate the expression after `case' and apply default promotions.  */
2714
2715 static tree
2716 check_case_value (tree value)
2717 {
2718   if (value == NULL_TREE)
2719     return value;
2720
2721   if (TREE_CODE (value) == INTEGER_CST)
2722     /* Promote char or short to int.  */
2723     value = perform_integral_promotions (value);
2724   else if (value != error_mark_node)
2725     {
2726       error ("case label does not reduce to an integer constant");
2727       value = error_mark_node;
2728     }
2729
2730   constant_expression_warning (value);
2731
2732   return value;
2733 }
2734 \f
2735 /* See if the case values LOW and HIGH are in the range of the original
2736    type (i.e. before the default conversion to int) of the switch testing
2737    expression.
2738    TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2739    the type before promoting it.  CASE_LOW_P is a pointer to the lower
2740    bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2741    if the case is not a case range.
2742    The caller has to make sure that we are not called with NULL for
2743    CASE_LOW_P (i.e. the default case).
2744    Returns true if the case label is in range of ORIG_TYPE (saturated or
2745    untouched) or false if the label is out of range.  */
2746
2747 static bool
2748 check_case_bounds (tree type, tree orig_type,
2749                    tree *case_low_p, tree *case_high_p)
2750 {
2751   tree min_value, max_value;
2752   tree case_low = *case_low_p;
2753   tree case_high = case_high_p ? *case_high_p : case_low;
2754
2755   /* If there was a problem with the original type, do nothing.  */
2756   if (orig_type == error_mark_node)
2757     return true;
2758
2759   min_value = TYPE_MIN_VALUE (orig_type);
2760   max_value = TYPE_MAX_VALUE (orig_type);
2761
2762   /* Case label is less than minimum for type.  */
2763   if (tree_int_cst_compare (case_low, min_value) < 0
2764       && tree_int_cst_compare (case_high, min_value) < 0)
2765     {
2766       warning (0, "case label value is less than minimum value for type");
2767       return false;
2768     }
2769
2770   /* Case value is greater than maximum for type.  */
2771   if (tree_int_cst_compare (case_low, max_value) > 0
2772       && tree_int_cst_compare (case_high, max_value) > 0)
2773     {
2774       warning (0, "case label value exceeds maximum value for type");
2775       return false;
2776     }
2777
2778   /* Saturate lower case label value to minimum.  */
2779   if (tree_int_cst_compare (case_high, min_value) >= 0
2780       && tree_int_cst_compare (case_low, min_value) < 0)
2781     {
2782       warning (0, "lower value in case label range"
2783                " less than minimum value for type");
2784       case_low = min_value;
2785     }
2786
2787   /* Saturate upper case label value to maximum.  */
2788   if (tree_int_cst_compare (case_low, max_value) <= 0
2789       && tree_int_cst_compare (case_high, max_value) > 0)
2790     {
2791       warning (0, "upper value in case label range"
2792                " exceeds maximum value for type");
2793       case_high = max_value;
2794     }
2795
2796   if (*case_low_p != case_low)
2797     *case_low_p = convert (type, case_low);
2798   if (case_high_p && *case_high_p != case_high)
2799     *case_high_p = convert (type, case_high);
2800
2801   return true;
2802 }
2803 \f
2804 /* Return an integer type with BITS bits of precision,
2805    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
2806
2807 tree
2808 c_common_type_for_size (unsigned int bits, int unsignedp)
2809 {
2810   if (bits == TYPE_PRECISION (integer_type_node))
2811     return unsignedp ? unsigned_type_node : integer_type_node;
2812
2813   if (bits == TYPE_PRECISION (signed_char_type_node))
2814     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2815
2816   if (bits == TYPE_PRECISION (short_integer_type_node))
2817     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2818
2819   if (bits == TYPE_PRECISION (long_integer_type_node))
2820     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2821
2822   if (bits == TYPE_PRECISION (long_long_integer_type_node))
2823     return (unsignedp ? long_long_unsigned_type_node
2824             : long_long_integer_type_node);
2825
2826   if (int128_integer_type_node
2827       && bits == TYPE_PRECISION (int128_integer_type_node))
2828     return (unsignedp ? int128_unsigned_type_node
2829             : int128_integer_type_node);
2830
2831   if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2832     return (unsignedp ? widest_unsigned_literal_type_node
2833             : widest_integer_literal_type_node);
2834
2835   if (bits <= TYPE_PRECISION (intQI_type_node))
2836     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2837
2838   if (bits <= TYPE_PRECISION (intHI_type_node))
2839     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2840
2841   if (bits <= TYPE_PRECISION (intSI_type_node))
2842     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2843
2844   if (bits <= TYPE_PRECISION (intDI_type_node))
2845     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2846
2847   return 0;
2848 }
2849
2850 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2851    that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2852    and saturating if SATP is nonzero, otherwise not saturating.  */
2853
2854 tree
2855 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2856                                     int unsignedp, int satp)
2857 {
2858   enum machine_mode mode;
2859   if (ibit == 0)
2860     mode = unsignedp ? UQQmode : QQmode;
2861   else
2862     mode = unsignedp ? UHAmode : HAmode;
2863
2864   for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2865     if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2866       break;
2867
2868   if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2869     {
2870       sorry ("GCC cannot support operators with integer types and "
2871              "fixed-point types that have too many integral and "
2872              "fractional bits together");
2873       return 0;
2874     }
2875
2876   return c_common_type_for_mode (mode, satp);
2877 }
2878
2879 /* Used for communication between c_common_type_for_mode and
2880    c_register_builtin_type.  */
2881 static GTY(()) tree registered_builtin_types;
2882
2883 /* Return a data type that has machine mode MODE.
2884    If the mode is an integer,
2885    then UNSIGNEDP selects between signed and unsigned types.
2886    If the mode is a fixed-point mode,
2887    then UNSIGNEDP selects between saturating and nonsaturating types.  */
2888
2889 tree
2890 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
2891 {
2892   tree t;
2893
2894   if (mode == TYPE_MODE (integer_type_node))
2895     return unsignedp ? unsigned_type_node : integer_type_node;
2896
2897   if (mode == TYPE_MODE (signed_char_type_node))
2898     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2899
2900   if (mode == TYPE_MODE (short_integer_type_node))
2901     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2902
2903   if (mode == TYPE_MODE (long_integer_type_node))
2904     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2905
2906   if (mode == TYPE_MODE (long_long_integer_type_node))
2907     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2908
2909   if (int128_integer_type_node
2910       && mode == TYPE_MODE (int128_integer_type_node))
2911     return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
2912
2913   if (mode == TYPE_MODE (widest_integer_literal_type_node))
2914     return unsignedp ? widest_unsigned_literal_type_node
2915                      : widest_integer_literal_type_node;
2916
2917   if (mode == QImode)
2918     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2919
2920   if (mode == HImode)
2921     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2922
2923   if (mode == SImode)
2924     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2925
2926   if (mode == DImode)
2927     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2928
2929 #if HOST_BITS_PER_WIDE_INT >= 64
2930   if (mode == TYPE_MODE (intTI_type_node))
2931     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2932 #endif
2933
2934   if (mode == TYPE_MODE (float_type_node))
2935     return float_type_node;
2936
2937   if (mode == TYPE_MODE (double_type_node))
2938     return double_type_node;
2939
2940   if (mode == TYPE_MODE (long_double_type_node))
2941     return long_double_type_node;
2942
2943   if (mode == TYPE_MODE (void_type_node))
2944     return void_type_node;
2945
2946   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2947     return (unsignedp
2948             ? make_unsigned_type (GET_MODE_PRECISION (mode))
2949             : make_signed_type (GET_MODE_PRECISION (mode)));
2950
2951   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2952     return (unsignedp
2953             ? make_unsigned_type (GET_MODE_PRECISION (mode))
2954             : make_signed_type (GET_MODE_PRECISION (mode)));
2955
2956   if (COMPLEX_MODE_P (mode))
2957     {
2958       enum machine_mode inner_mode;
2959       tree inner_type;
2960
2961       if (mode == TYPE_MODE (complex_float_type_node))
2962         return complex_float_type_node;
2963       if (mode == TYPE_MODE (complex_double_type_node))
2964         return complex_double_type_node;
2965       if (mode == TYPE_MODE (complex_long_double_type_node))
2966         return complex_long_double_type_node;
2967
2968       if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2969         return complex_integer_type_node;
2970
2971       inner_mode = GET_MODE_INNER (mode);
2972       inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2973       if (inner_type != NULL_TREE)
2974         return build_complex_type (inner_type);
2975     }
2976   else if (VECTOR_MODE_P (mode))
2977     {
2978       enum machine_mode inner_mode = GET_MODE_INNER (mode);
2979       tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2980       if (inner_type != NULL_TREE)
2981         return build_vector_type_for_mode (inner_type, mode);
2982     }
2983
2984   if (mode == TYPE_MODE (dfloat32_type_node))
2985     return dfloat32_type_node;
2986   if (mode == TYPE_MODE (dfloat64_type_node))
2987     return dfloat64_type_node;
2988   if (mode == TYPE_MODE (dfloat128_type_node))
2989     return dfloat128_type_node;
2990
2991   if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2992     {
2993       if (mode == TYPE_MODE (short_fract_type_node))
2994         return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2995       if (mode == TYPE_MODE (fract_type_node))
2996         return unsignedp ? sat_fract_type_node : fract_type_node;
2997       if (mode == TYPE_MODE (long_fract_type_node))
2998         return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2999       if (mode == TYPE_MODE (long_long_fract_type_node))
3000         return unsignedp ? sat_long_long_fract_type_node
3001                          : long_long_fract_type_node;
3002
3003       if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3004         return unsignedp ? sat_unsigned_short_fract_type_node
3005                          : unsigned_short_fract_type_node;
3006       if (mode == TYPE_MODE (unsigned_fract_type_node))
3007         return unsignedp ? sat_unsigned_fract_type_node
3008                          : unsigned_fract_type_node;
3009       if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3010         return unsignedp ? sat_unsigned_long_fract_type_node
3011                          : unsigned_long_fract_type_node;
3012       if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3013         return unsignedp ? sat_unsigned_long_long_fract_type_node
3014                          : unsigned_long_long_fract_type_node;
3015
3016       if (mode == TYPE_MODE (short_accum_type_node))
3017         return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3018       if (mode == TYPE_MODE (accum_type_node))
3019         return unsignedp ? sat_accum_type_node : accum_type_node;
3020       if (mode == TYPE_MODE (long_accum_type_node))
3021         return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3022       if (mode == TYPE_MODE (long_long_accum_type_node))
3023         return unsignedp ? sat_long_long_accum_type_node
3024                          : long_long_accum_type_node;
3025
3026       if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3027         return unsignedp ? sat_unsigned_short_accum_type_node
3028                          : unsigned_short_accum_type_node;
3029       if (mode == TYPE_MODE (unsigned_accum_type_node))
3030         return unsignedp ? sat_unsigned_accum_type_node
3031                          : unsigned_accum_type_node;
3032       if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3033         return unsignedp ? sat_unsigned_long_accum_type_node
3034                          : unsigned_long_accum_type_node;
3035       if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3036         return unsignedp ? sat_unsigned_long_long_accum_type_node
3037                          : unsigned_long_long_accum_type_node;
3038
3039       if (mode == QQmode)
3040         return unsignedp ? sat_qq_type_node : qq_type_node;
3041       if (mode == HQmode)
3042         return unsignedp ? sat_hq_type_node : hq_type_node;
3043       if (mode == SQmode)
3044         return unsignedp ? sat_sq_type_node : sq_type_node;
3045       if (mode == DQmode)
3046         return unsignedp ? sat_dq_type_node : dq_type_node;
3047       if (mode == TQmode)
3048         return unsignedp ? sat_tq_type_node : tq_type_node;
3049
3050       if (mode == UQQmode)
3051         return unsignedp ? sat_uqq_type_node : uqq_type_node;
3052       if (mode == UHQmode)
3053         return unsignedp ? sat_uhq_type_node : uhq_type_node;
3054       if (mode == USQmode)
3055         return unsignedp ? sat_usq_type_node : usq_type_node;
3056       if (mode == UDQmode)
3057         return unsignedp ? sat_udq_type_node : udq_type_node;
3058       if (mode == UTQmode)
3059         return unsignedp ? sat_utq_type_node : utq_type_node;
3060
3061       if (mode == HAmode)
3062         return unsignedp ? sat_ha_type_node : ha_type_node;
3063       if (mode == SAmode)
3064         return unsignedp ? sat_sa_type_node : sa_type_node;
3065       if (mode == DAmode)
3066         return unsignedp ? sat_da_type_node : da_type_node;
3067       if (mode == TAmode)
3068         return unsignedp ? sat_ta_type_node : ta_type_node;
3069
3070       if (mode == UHAmode)
3071         return unsignedp ? sat_uha_type_node : uha_type_node;
3072       if (mode == USAmode)
3073         return unsignedp ? sat_usa_type_node : usa_type_node;
3074       if (mode == UDAmode)
3075         return unsignedp ? sat_uda_type_node : uda_type_node;
3076       if (mode == UTAmode)
3077         return unsignedp ? sat_uta_type_node : uta_type_node;
3078     }
3079
3080   for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3081     if (TYPE_MODE (TREE_VALUE (t)) == mode)
3082       return TREE_VALUE (t);
3083
3084   return 0;
3085 }
3086
3087 tree
3088 c_common_unsigned_type (tree type)
3089 {
3090   return c_common_signed_or_unsigned_type (1, type);
3091 }
3092
3093 /* Return a signed type the same as TYPE in other respects.  */
3094
3095 tree
3096 c_common_signed_type (tree type)
3097 {
3098   return c_common_signed_or_unsigned_type (0, type);
3099 }
3100
3101 /* Return a type the same as TYPE except unsigned or
3102    signed according to UNSIGNEDP.  */
3103
3104 tree
3105 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3106 {
3107   tree type1;
3108
3109   /* This block of code emulates the behavior of the old
3110      c_common_unsigned_type. In particular, it returns
3111      long_unsigned_type_node if passed a long, even when a int would
3112      have the same size. This is necessary for warnings to work
3113      correctly in archs where sizeof(int) == sizeof(long) */
3114
3115   type1 = TYPE_MAIN_VARIANT (type);
3116   if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3117     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3118   if (type1 == integer_type_node || type1 == unsigned_type_node)
3119     return unsignedp ? unsigned_type_node : integer_type_node;
3120   if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3121     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3122   if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3123     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3124   if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3125     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3126   if (int128_integer_type_node
3127       && (type1 == int128_integer_type_node
3128           || type1 == int128_unsigned_type_node))
3129     return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3130   if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3131     return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3132 #if HOST_BITS_PER_WIDE_INT >= 64
3133   if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3134     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3135 #endif
3136   if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3137     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3138   if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3139     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3140   if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3141     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3142   if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3143     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3144
3145 #define C_COMMON_FIXED_TYPES(NAME)          \
3146   if (type1 == short_ ## NAME ## _type_node \
3147       || type1 == unsigned_short_ ## NAME ## _type_node) \
3148     return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3149                      : short_ ## NAME ## _type_node; \
3150   if (type1 == NAME ## _type_node \
3151       || type1 == unsigned_ ## NAME ## _type_node) \
3152     return unsignedp ? unsigned_ ## NAME ## _type_node \
3153                      : NAME ## _type_node; \
3154   if (type1 == long_ ## NAME ## _type_node \
3155       || type1 == unsigned_long_ ## NAME ## _type_node) \
3156     return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3157                      : long_ ## NAME ## _type_node; \
3158   if (type1 == long_long_ ## NAME ## _type_node \
3159       || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3160     return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3161                      : long_long_ ## NAME ## _type_node;
3162
3163 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3164   if (type1 == NAME ## _type_node \
3165       || type1 == u ## NAME ## _type_node) \
3166     return unsignedp ? u ## NAME ## _type_node \
3167                      : NAME ## _type_node;
3168
3169 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3170   if (type1 == sat_ ## short_ ## NAME ## _type_node \
3171       || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3172     return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3173                      : sat_ ## short_ ## NAME ## _type_node; \
3174   if (type1 == sat_ ## NAME ## _type_node \
3175       || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3176     return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3177                      : sat_ ## NAME ## _type_node; \
3178   if (type1 == sat_ ## long_ ## NAME ## _type_node \
3179       || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3180     return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3181                      : sat_ ## long_ ## NAME ## _type_node; \
3182   if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3183       || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3184     return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3185                      : sat_ ## long_long_ ## NAME ## _type_node;
3186
3187 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME)     \
3188   if (type1 == sat_ ## NAME ## _type_node \
3189       || type1 == sat_ ## u ## NAME ## _type_node) \
3190     return unsignedp ? sat_ ## u ## NAME ## _type_node \
3191                      : sat_ ## NAME ## _type_node;
3192
3193   C_COMMON_FIXED_TYPES (fract);
3194   C_COMMON_FIXED_TYPES_SAT (fract);
3195   C_COMMON_FIXED_TYPES (accum);
3196   C_COMMON_FIXED_TYPES_SAT (accum);
3197
3198   C_COMMON_FIXED_MODE_TYPES (qq);
3199   C_COMMON_FIXED_MODE_TYPES (hq);
3200   C_COMMON_FIXED_MODE_TYPES (sq);
3201   C_COMMON_FIXED_MODE_TYPES (dq);
3202   C_COMMON_FIXED_MODE_TYPES (tq);
3203   C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3204   C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3205   C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3206   C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3207   C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3208   C_COMMON_FIXED_MODE_TYPES (ha);
3209   C_COMMON_FIXED_MODE_TYPES (sa);
3210   C_COMMON_FIXED_MODE_TYPES (da);
3211   C_COMMON_FIXED_MODE_TYPES (ta);
3212   C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3213   C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3214   C_COMMON_FIXED_MODE_TYPES_SAT (da);
3215   C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3216
3217   /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3218      the precision; they have precision set to match their range, but
3219      may use a wider mode to match an ABI.  If we change modes, we may
3220      wind up with bad conversions.  For INTEGER_TYPEs in C, must check
3221      the precision as well, so as to yield correct results for
3222      bit-field types.  C++ does not have these separate bit-field
3223      types, and producing a signed or unsigned variant of an
3224      ENUMERAL_TYPE may cause other problems as well.  */
3225
3226   if (!INTEGRAL_TYPE_P (type)
3227       || TYPE_UNSIGNED (type) == unsignedp)
3228     return type;
3229
3230 #define TYPE_OK(node)                                                       \
3231   (TYPE_MODE (type) == TYPE_MODE (node)                                     \
3232    && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3233   if (TYPE_OK (signed_char_type_node))
3234     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3235   if (TYPE_OK (integer_type_node))
3236     return unsignedp ? unsigned_type_node : integer_type_node;
3237   if (TYPE_OK (short_integer_type_node))
3238     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3239   if (TYPE_OK (long_integer_type_node))
3240     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3241   if (TYPE_OK (long_long_integer_type_node))
3242     return (unsignedp ? long_long_unsigned_type_node
3243             : long_long_integer_type_node);
3244   if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3245     return (unsignedp ? int128_unsigned_type_node
3246             : int128_integer_type_node);
3247   if (TYPE_OK (widest_integer_literal_type_node))
3248     return (unsignedp ? widest_unsigned_literal_type_node
3249             : widest_integer_literal_type_node);
3250
3251 #if HOST_BITS_PER_WIDE_INT >= 64
3252   if (TYPE_OK (intTI_type_node))
3253     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3254 #endif
3255   if (TYPE_OK (intDI_type_node))
3256     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3257   if (TYPE_OK (intSI_type_node))
3258     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3259   if (TYPE_OK (intHI_type_node))
3260     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3261   if (TYPE_OK (intQI_type_node))
3262     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3263 #undef TYPE_OK
3264
3265   return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3266 }
3267
3268 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP.  */
3269
3270 tree
3271 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3272 {
3273   /* Extended integer types of the same width as a standard type have
3274      lesser rank, so those of the same width as int promote to int or
3275      unsigned int and are valid for printf formats expecting int or
3276      unsigned int.  To avoid such special cases, avoid creating
3277      extended integer types for bit-fields if a standard integer type
3278      is available.  */
3279   if (width == TYPE_PRECISION (integer_type_node))
3280     return unsignedp ? unsigned_type_node : integer_type_node;
3281   if (width == TYPE_PRECISION (signed_char_type_node))
3282     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3283   if (width == TYPE_PRECISION (short_integer_type_node))
3284     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3285   if (width == TYPE_PRECISION (long_integer_type_node))
3286     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3287   if (width == TYPE_PRECISION (long_long_integer_type_node))
3288     return (unsignedp ? long_long_unsigned_type_node
3289             : long_long_integer_type_node);
3290   if (int128_integer_type_node
3291       && width == TYPE_PRECISION (int128_integer_type_node))
3292     return (unsignedp ? int128_unsigned_type_node
3293             : int128_integer_type_node);
3294   return build_nonstandard_integer_type (width, unsignedp);
3295 }
3296
3297 /* The C version of the register_builtin_type langhook.  */
3298
3299 void
3300 c_register_builtin_type (tree type, const char* name)
3301 {
3302   tree decl;
3303
3304   decl = build_decl (UNKNOWN_LOCATION,
3305                      TYPE_DECL, get_identifier (name), type);
3306   DECL_ARTIFICIAL (decl) = 1;
3307   if (!TYPE_NAME (type))
3308     TYPE_NAME (type) = decl;
3309   pushdecl (decl);
3310
3311   registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3312 }
3313 \f
3314 /* Print an error message for invalid operands to arith operation
3315    CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3316    LOCATION is the location of the message.  */
3317
3318 void
3319 binary_op_error (location_t location, enum tree_code code,
3320                  tree type0, tree type1)
3321 {
3322   const char *opname;
3323
3324   switch (code)
3325     {
3326     case PLUS_EXPR:
3327       opname = "+"; break;
3328     case MINUS_EXPR:
3329       opname = "-"; break;
3330     case MULT_EXPR:
3331       opname = "*"; break;
3332     case MAX_EXPR:
3333       opname = "max"; break;
3334     case MIN_EXPR:
3335       opname = "min"; break;
3336     case EQ_EXPR:
3337       opname = "=="; break;
3338     case NE_EXPR:
3339       opname = "!="; break;
3340     case LE_EXPR:
3341       opname = "<="; break;
3342     case GE_EXPR:
3343       opname = ">="; break;
3344     case LT_EXPR:
3345       opname = "<"; break;
3346     case GT_EXPR:
3347       opname = ">"; break;
3348     case LSHIFT_EXPR:
3349       opname = "<<"; break;
3350     case RSHIFT_EXPR:
3351       opname = ">>"; break;
3352     case TRUNC_MOD_EXPR:
3353     case FLOOR_MOD_EXPR:
3354       opname = "%"; break;
3355     case TRUNC_DIV_EXPR:
3356     case FLOOR_DIV_EXPR:
3357       opname = "/"; break;
3358     case BIT_AND_EXPR:
3359       opname = "&"; break;
3360     case BIT_IOR_EXPR:
3361       opname = "|"; break;
3362     case TRUTH_ANDIF_EXPR:
3363       opname = "&&"; break;
3364     case TRUTH_ORIF_EXPR:
3365       opname = "||"; break;
3366     case BIT_XOR_EXPR:
3367       opname = "^"; break;
3368     default:
3369       gcc_unreachable ();
3370     }
3371   error_at (location,
3372             "invalid operands to binary %s (have %qT and %qT)", opname,
3373             type0, type1);
3374 }
3375 \f
3376 /* Subroutine of build_binary_op, used for comparison operations.
3377    See if the operands have both been converted from subword integer types
3378    and, if so, perhaps change them both back to their original type.
3379    This function is also responsible for converting the two operands
3380    to the proper common type for comparison.
3381
3382    The arguments of this function are all pointers to local variables
3383    of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3384    RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3385
3386    If this function returns nonzero, it means that the comparison has
3387    a constant value.  What this function returns is an expression for
3388    that value.  */
3389
3390 tree
3391 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3392                  enum tree_code *rescode_ptr)
3393 {
3394   tree type;
3395   tree op0 = *op0_ptr;
3396   tree op1 = *op1_ptr;
3397   int unsignedp0, unsignedp1;
3398   int real1, real2;
3399   tree primop0, primop1;
3400   enum tree_code code = *rescode_ptr;
3401
3402   /* Throw away any conversions to wider types
3403      already present in the operands.  */
3404
3405   primop0 = c_common_get_narrower (op0, &unsignedp0);
3406   primop1 = c_common_get_narrower (op1, &unsignedp1);
3407
3408   /* If primopN is first sign-extended from primopN's precision to opN's
3409      precision, then zero-extended from opN's precision to
3410      *restype_ptr precision, shortenings might be invalid.  */
3411   if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3412       && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3413       && !unsignedp0
3414       && TYPE_UNSIGNED (TREE_TYPE (op0)))
3415     primop0 = op0;
3416   if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3417       && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3418       && !unsignedp1
3419       && TYPE_UNSIGNED (TREE_TYPE (op1)))
3420     primop1 = op1;
3421
3422   /* Handle the case that OP0 does not *contain* a conversion
3423      but it *requires* conversion to FINAL_TYPE.  */
3424
3425   if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3426     unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3427   if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3428     unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3429
3430   /* If one of the operands must be floated, we cannot optimize.  */
3431   real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3432   real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3433
3434   /* If first arg is constant, swap the args (changing operation
3435      so value is preserved), for canonicalization.  Don't do this if
3436      the second arg is 0.  */
3437
3438   if (TREE_CONSTANT (primop0)
3439       && !integer_zerop (primop1) && !real_zerop (primop1)
3440       && !fixed_zerop (primop1))
3441     {
3442       tree tem = primop0;
3443       int temi = unsignedp0;
3444       primop0 = primop1;
3445       primop1 = tem;
3446       tem = op0;
3447       op0 = op1;
3448       op1 = tem;
3449       *op0_ptr = op0;
3450       *op1_ptr = op1;
3451       unsignedp0 = unsignedp1;
3452       unsignedp1 = temi;
3453       temi = real1;
3454       real1 = real2;
3455       real2 = temi;
3456
3457       switch (code)
3458         {
3459         case LT_EXPR:
3460           code = GT_EXPR;
3461           break;
3462         case GT_EXPR:
3463           code = LT_EXPR;
3464           break;
3465         case LE_EXPR:
3466           code = GE_EXPR;
3467           break;
3468         case GE_EXPR:
3469           code = LE_EXPR;
3470           break;
3471         default:
3472           break;
3473         }
3474       *rescode_ptr = code;
3475     }
3476
3477   /* If comparing an integer against a constant more bits wide,
3478      maybe we can deduce a value of 1 or 0 independent of the data.
3479      Or else truncate the constant now
3480      rather than extend the variable at run time.
3481
3482      This is only interesting if the constant is the wider arg.
3483      Also, it is not safe if the constant is unsigned and the
3484      variable arg is signed, since in this case the variable
3485      would be sign-extended and then regarded as unsigned.
3486      Our technique fails in this case because the lowest/highest
3487      possible unsigned results don't follow naturally from the
3488      lowest/highest possible values of the variable operand.
3489      For just EQ_EXPR and NE_EXPR there is another technique that
3490      could be used: see if the constant can be faithfully represented
3491      in the other operand's type, by truncating it and reextending it
3492      and see if that preserves the constant's value.  */
3493
3494   if (!real1 && !real2
3495       && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3496       && TREE_CODE (primop1) == INTEGER_CST
3497       && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3498     {
3499       int min_gt, max_gt, min_lt, max_lt;
3500       tree maxval, minval;
3501       /* 1 if comparison is nominally unsigned.  */
3502       int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3503       tree val;
3504
3505       type = c_common_signed_or_unsigned_type (unsignedp0,
3506                                                TREE_TYPE (primop0));
3507
3508       maxval = TYPE_MAX_VALUE (type);
3509       minval = TYPE_MIN_VALUE (type);
3510
3511       if (unsignedp && !unsignedp0)
3512         *restype_ptr = c_common_signed_type (*restype_ptr);
3513
3514       if (TREE_TYPE (primop1) != *restype_ptr)
3515         {
3516           /* Convert primop1 to target type, but do not introduce
3517              additional overflow.  We know primop1 is an int_cst.  */
3518           primop1 = force_fit_type_double (*restype_ptr,
3519                                            tree_to_double_int (primop1),
3520                                            0, TREE_OVERFLOW (primop1));
3521         }
3522       if (type != *restype_ptr)
3523         {
3524           minval = convert (*restype_ptr, minval);
3525           maxval = convert (*restype_ptr, maxval);
3526         }
3527
3528       if (unsignedp && unsignedp0)
3529         {
3530           min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3531           max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3532           min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3533           max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3534         }
3535       else
3536         {
3537           min_gt = INT_CST_LT (primop1, minval);
3538           max_gt = INT_CST_LT (primop1, maxval);
3539           min_lt = INT_CST_LT (minval, primop1);
3540           max_lt = INT_CST_LT (maxval, primop1);
3541         }
3542
3543       val = 0;
3544       /* This used to be a switch, but Genix compiler can't handle that.  */
3545       if (code == NE_EXPR)
3546         {
3547           if (max_lt || min_gt)
3548             val = truthvalue_true_node;
3549         }
3550       else if (code == EQ_EXPR)
3551         {
3552           if (max_lt || min_gt)
3553             val = truthvalue_false_node;
3554         }
3555       else if (code == LT_EXPR)
3556         {
3557           if (max_lt)
3558             val = truthvalue_true_node;
3559           if (!min_lt)
3560             val = truthvalue_false_node;
3561         }
3562       else if (code == GT_EXPR)
3563         {
3564           if (min_gt)
3565             val = truthvalue_true_node;
3566           if (!max_gt)
3567             val = truthvalue_false_node;
3568         }
3569       else if (code == LE_EXPR)
3570         {
3571           if (!max_gt)
3572             val = truthvalue_true_node;
3573           if (min_gt)
3574             val = truthvalue_false_node;
3575         }
3576       else if (code == GE_EXPR)
3577         {
3578           if (!min_lt)
3579             val = truthvalue_true_node;
3580           if (max_lt)
3581             val = truthvalue_false_node;
3582         }
3583
3584       /* If primop0 was sign-extended and unsigned comparison specd,
3585          we did a signed comparison above using the signed type bounds.
3586          But the comparison we output must be unsigned.
3587
3588          Also, for inequalities, VAL is no good; but if the signed
3589          comparison had *any* fixed result, it follows that the
3590          unsigned comparison just tests the sign in reverse
3591          (positive values are LE, negative ones GE).
3592          So we can generate an unsigned comparison
3593          against an extreme value of the signed type.  */
3594
3595       if (unsignedp && !unsignedp0)
3596         {
3597           if (val != 0)
3598             switch (code)
3599               {
3600               case LT_EXPR:
3601               case GE_EXPR:
3602                 primop1 = TYPE_MIN_VALUE (type);
3603                 val = 0;
3604                 break;
3605
3606               case LE_EXPR:
3607               case GT_EXPR:
3608                 primop1 = TYPE_MAX_VALUE (type);
3609                 val = 0;
3610                 break;
3611
3612               default:
3613                 break;
3614               }
3615           type = c_common_unsigned_type (type);
3616         }
3617
3618       if (TREE_CODE (primop0) != INTEGER_CST)
3619         {
3620           if (val == truthvalue_false_node)
3621             warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type");
3622           if (val == truthvalue_true_node)
3623             warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type");
3624         }
3625
3626       if (val != 0)
3627         {
3628           /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
3629           if (TREE_SIDE_EFFECTS (primop0))
3630             return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3631           return val;
3632         }
3633
3634       /* Value is not predetermined, but do the comparison
3635          in the type of the operand that is not constant.
3636          TYPE is already properly set.  */
3637     }
3638
3639   /* If either arg is decimal float and the other is float, find the
3640      proper common type to use for comparison.  */
3641   else if (real1 && real2
3642            && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3643                || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3644     type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3645
3646   else if (real1 && real2
3647            && (TYPE_PRECISION (TREE_TYPE (primop0))
3648                == TYPE_PRECISION (TREE_TYPE (primop1))))
3649     type = TREE_TYPE (primop0);
3650
3651   /* If args' natural types are both narrower than nominal type
3652      and both extend in the same manner, compare them
3653      in the type of the wider arg.
3654      Otherwise must actually extend both to the nominal
3655      common type lest different ways of extending
3656      alter the result.
3657      (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
3658
3659   else if (unsignedp0 == unsignedp1 && real1 == real2
3660            && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3661            && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3662     {
3663       type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3664       type = c_common_signed_or_unsigned_type (unsignedp0
3665                                                || TYPE_UNSIGNED (*restype_ptr),
3666                                                type);
3667       /* Make sure shorter operand is extended the right way
3668          to match the longer operand.  */
3669       primop0
3670         = convert (c_common_signed_or_unsigned_type (unsignedp0,
3671                                                      TREE_TYPE (primop0)),
3672                    primop0);
3673       primop1
3674         = convert (c_common_signed_or_unsigned_type (unsignedp1,
3675                                                      TREE_TYPE (primop1)),
3676                    primop1);
3677     }
3678   else
3679     {
3680       /* Here we must do the comparison on the nominal type
3681          using the args exactly as we received them.  */
3682       type = *restype_ptr;
3683       primop0 = op0;
3684       primop1 = op1;
3685
3686       if (!real1 && !real2 && integer_zerop (primop1)
3687           && TYPE_UNSIGNED (*restype_ptr))
3688         {
3689           tree value = 0;
3690           switch (code)
3691             {
3692             case GE_EXPR:
3693               /* All unsigned values are >= 0, so we warn.  However,
3694                  if OP0 is a constant that is >= 0, the signedness of
3695                  the comparison isn't an issue, so suppress the
3696                  warning.  */
3697               if (warn_type_limits && !in_system_header
3698                   && !(TREE_CODE (primop0) == INTEGER_CST
3699                        && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3700                                                    primop0))))
3701                 warning (OPT_Wtype_limits,
3702                          "comparison of unsigned expression >= 0 is always true");
3703               value = truthvalue_true_node;
3704               break;
3705
3706             case LT_EXPR:
3707               if (warn_type_limits && !in_system_header
3708                   && !(TREE_CODE (primop0) == INTEGER_CST
3709                        && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3710                                                    primop0))))
3711                 warning (OPT_Wtype_limits,
3712                          "comparison of unsigned expression < 0 is always false");
3713               value = truthvalue_false_node;
3714               break;
3715
3716             default:
3717               break;
3718             }
3719
3720           if (value != 0)
3721             {
3722               /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
3723               if (TREE_SIDE_EFFECTS (primop0))
3724                 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3725                                primop0, value);
3726               return value;
3727             }
3728         }
3729     }
3730
3731   *op0_ptr = convert (type, primop0);
3732   *op1_ptr = convert (type, primop1);
3733
3734   *restype_ptr = truthvalue_type_node;
3735
3736   return 0;
3737 }
3738 \f
3739 /* Return a tree for the sum or difference (RESULTCODE says which)
3740    of pointer PTROP and integer INTOP.  */
3741
3742 tree
3743 pointer_int_sum (location_t loc, enum tree_code resultcode,
3744                  tree ptrop, tree intop)
3745 {
3746   tree size_exp, ret;
3747
3748   /* The result is a pointer of the same type that is being added.  */
3749   tree result_type = TREE_TYPE (ptrop);
3750
3751   if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3752     {
3753       pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3754                "pointer of type %<void *%> used in arithmetic");
3755       size_exp = integer_one_node;
3756     }
3757   else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3758     {
3759       pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3760                "pointer to a function used in arithmetic");
3761       size_exp = integer_one_node;
3762     }
3763   else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3764     {
3765       pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3766                "pointer to member function used in arithmetic");
3767       size_exp = integer_one_node;
3768     }
3769   else
3770     size_exp = size_in_bytes (TREE_TYPE (result_type));
3771
3772   /* We are manipulating pointer values, so we don't need to warn
3773      about relying on undefined signed overflow.  We disable the
3774      warning here because we use integer types so fold won't know that
3775      they are really pointers.  */
3776   fold_defer_overflow_warnings ();
3777
3778   /* If what we are about to multiply by the size of the elements
3779      contains a constant term, apply distributive law
3780      and multiply that constant term separately.
3781      This helps produce common subexpressions.  */
3782   if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3783       && !TREE_CONSTANT (intop)
3784       && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3785       && TREE_CONSTANT (size_exp)
3786       /* If the constant comes from pointer subtraction,
3787          skip this optimization--it would cause an error.  */
3788       && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3789       /* If the constant is unsigned, and smaller than the pointer size,
3790          then we must skip this optimization.  This is because it could cause
3791          an overflow error if the constant is negative but INTOP is not.  */
3792       && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3793           || (TYPE_PRECISION (TREE_TYPE (intop))
3794               == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3795     {
3796       enum tree_code subcode = resultcode;
3797       tree int_type = TREE_TYPE (intop);
3798       if (TREE_CODE (intop) == MINUS_EXPR)
3799         subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3800       /* Convert both subexpression types to the type of intop,
3801          because weird cases involving pointer arithmetic
3802          can result in a sum or difference with different type args.  */
3803       ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3804                                subcode, ptrop,
3805                                convert (int_type, TREE_OPERAND (intop, 1)), 1);
3806       intop = convert (int_type, TREE_OPERAND (intop, 0));
3807     }
3808
3809   /* Convert the integer argument to a type the same size as sizetype
3810      so the multiply won't overflow spuriously.  */
3811   if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3812       || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3813     intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3814                                              TYPE_UNSIGNED (sizetype)), intop);
3815
3816   /* Replace the integer argument with a suitable product by the object size.
3817      Do this multiplication as signed, then convert to the appropriate type
3818      for the pointer operation and disregard an overflow that occured only
3819      because of the sign-extension change in the latter conversion.  */
3820   {
3821     tree t = build_binary_op (loc,
3822                               MULT_EXPR, intop,
3823                               convert (TREE_TYPE (intop), size_exp), 1);
3824     intop = convert (sizetype, t);
3825     if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3826       intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
3827                                   TREE_INT_CST_HIGH (intop));
3828   }
3829
3830   /* Create the sum or difference.  */
3831   if (resultcode == MINUS_EXPR)
3832     intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3833
3834   ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3835
3836   fold_undefer_and_ignore_overflow_warnings ();
3837
3838   return ret;
3839 }
3840 \f
3841 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3842    and if NON_CONST is known not to be permitted in an evaluated part
3843    of a constant expression.  */
3844
3845 tree
3846 c_wrap_maybe_const (tree expr, bool non_const)
3847 {
3848   bool nowarning = TREE_NO_WARNING (expr);
3849   location_t loc = EXPR_LOCATION (expr);
3850
3851   /* This should never be called for C++.  */
3852   if (c_dialect_cxx ())
3853     gcc_unreachable ();
3854
3855   /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING.  */
3856   STRIP_TYPE_NOPS (expr);
3857   expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3858   C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3859   if (nowarning)
3860     TREE_NO_WARNING (expr) = 1;
3861   protected_set_expr_location (expr, loc);
3862
3863   return expr;
3864 }
3865
3866 /* Wrap a SAVE_EXPR around EXPR, if appropriate.  Like save_expr, but
3867    for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3868    around the SAVE_EXPR if needed so that c_fully_fold does not need
3869    to look inside SAVE_EXPRs.  */
3870
3871 tree
3872 c_save_expr (tree expr)
3873 {
3874   bool maybe_const = true;
3875   if (c_dialect_cxx ())
3876     return save_expr (expr);
3877   expr = c_fully_fold (expr, false, &maybe_const);
3878   expr = save_expr (expr);
3879   if (!maybe_const)
3880     expr = c_wrap_maybe_const (expr, true);
3881   return expr;
3882 }
3883
3884 /* Return whether EXPR is a declaration whose address can never be
3885    NULL.  */
3886
3887 bool
3888 decl_with_nonnull_addr_p (const_tree expr)
3889 {
3890   return (DECL_P (expr)
3891           && (TREE_CODE (expr) == PARM_DECL
3892               || TREE_CODE (expr) == LABEL_DECL
3893               || !DECL_WEAK (expr)));
3894 }
3895
3896 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3897    or for an `if' or `while' statement or ?..: exp.  It should already
3898    have been validated to be of suitable type; otherwise, a bad
3899    diagnostic may result.
3900
3901    The EXPR is located at LOCATION.
3902
3903    This preparation consists of taking the ordinary
3904    representation of an expression expr and producing a valid tree
3905    boolean expression describing whether expr is nonzero.  We could
3906    simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3907    but we optimize comparisons, &&, ||, and !.
3908
3909    The resulting type should always be `truthvalue_type_node'.  */
3910
3911 tree
3912 c_common_truthvalue_conversion (location_t location, tree expr)
3913 {
3914   switch (TREE_CODE (expr))
3915     {
3916     case EQ_EXPR:   case NE_EXPR:   case UNEQ_EXPR: case LTGT_EXPR:
3917     case LE_EXPR:   case GE_EXPR:   case LT_EXPR:   case GT_EXPR:
3918     case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3919     case ORDERED_EXPR: case UNORDERED_EXPR:
3920       if (TREE_TYPE (expr) == truthvalue_type_node)
3921         return expr;
3922       expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3923                      TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3924       goto ret;
3925
3926     case TRUTH_ANDIF_EXPR:
3927     case TRUTH_ORIF_EXPR:
3928     case TRUTH_AND_EXPR:
3929     case TRUTH_OR_EXPR:
3930     case TRUTH_XOR_EXPR:
3931       if (TREE_TYPE (expr) == truthvalue_type_node)
3932         return expr;
3933       expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3934                      c_common_truthvalue_conversion (location,
3935                                                      TREE_OPERAND (expr, 0)),
3936                      c_common_truthvalue_conversion (location,
3937                                                      TREE_OPERAND (expr, 1)));
3938       goto ret;
3939
3940     case TRUTH_NOT_EXPR:
3941       if (TREE_TYPE (expr) == truthvalue_type_node)
3942         return expr;
3943       expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3944                      c_common_truthvalue_conversion (location,
3945                                                      TREE_OPERAND (expr, 0)));
3946       goto ret;
3947
3948     case ERROR_MARK:
3949       return expr;
3950
3951     case INTEGER_CST:
3952       return integer_zerop (expr) ? truthvalue_false_node
3953                                   : truthvalue_true_node;
3954
3955     case REAL_CST:
3956       return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3957              ? truthvalue_true_node
3958              : truthvalue_false_node;
3959
3960     case FIXED_CST:
3961       return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3962                             &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3963              ? truthvalue_true_node
3964              : truthvalue_false_node;
3965
3966     case FUNCTION_DECL:
3967       expr = build_unary_op (location, ADDR_EXPR, expr, 0);
3968       /* Fall through.  */
3969
3970     case ADDR_EXPR:
3971       {
3972         tree inner = TREE_OPERAND (expr, 0);
3973         if (decl_with_nonnull_addr_p (inner))
3974           {
3975             /* Common Ada/Pascal programmer's mistake.  */
3976             warning_at (location,
3977                         OPT_Waddress,
3978                         "the address of %qD will always evaluate as %<true%>",
3979                         inner);
3980             return truthvalue_true_node;
3981           }
3982         break;
3983       }
3984
3985     case COMPLEX_EXPR:
3986       expr = build_binary_op (EXPR_LOCATION (expr),
3987                               (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3988                                ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3989                 c_common_truthvalue_conversion (location,
3990                                                 TREE_OPERAND (expr, 0)),
3991                 c_common_truthvalue_conversion (location,
3992                                                 TREE_OPERAND (expr, 1)),
3993                               0);
3994       goto ret;
3995
3996     case NEGATE_EXPR:
3997     case ABS_EXPR:
3998     case FLOAT_EXPR:
3999     case EXCESS_PRECISION_EXPR:
4000       /* These don't change whether an object is nonzero or zero.  */
4001       return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4002
4003     case LROTATE_EXPR:
4004     case RROTATE_EXPR:
4005       /* These don't change whether an object is zero or nonzero, but
4006          we can't ignore them if their second arg has side-effects.  */
4007       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4008         {
4009           expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4010                          TREE_OPERAND (expr, 1),
4011                          c_common_truthvalue_conversion
4012                          (location, TREE_OPERAND (expr, 0)));
4013           goto ret;
4014         }
4015       else
4016         return c_common_truthvalue_conversion (location,
4017                                                TREE_OPERAND (expr, 0));
4018
4019     case COND_EXPR:
4020       /* Distribute the conversion into the arms of a COND_EXPR.  */
4021       if (c_dialect_cxx ())
4022         {
4023           tree op1 = TREE_OPERAND (expr, 1);
4024           tree op2 = TREE_OPERAND (expr, 2);
4025           /* In C++ one of the arms might have void type if it is throw.  */
4026           if (!VOID_TYPE_P (TREE_TYPE (op1)))
4027             op1 = c_common_truthvalue_conversion (location, op1);
4028           if (!VOID_TYPE_P (TREE_TYPE (op2)))
4029             op2 = c_common_truthvalue_conversion (location, op2);
4030           expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4031                                   TREE_OPERAND (expr, 0), op1, op2);
4032           goto ret;
4033         }
4034       else
4035         {
4036           /* Folding will happen later for C.  */
4037           expr = build3 (COND_EXPR, truthvalue_type_node,
4038                          TREE_OPERAND (expr, 0),
4039                          c_common_truthvalue_conversion (location,
4040                                                          TREE_OPERAND (expr, 1)),
4041                          c_common_truthvalue_conversion (location,
4042                                                          TREE_OPERAND (expr, 2)));
4043           goto ret;
4044         }
4045
4046     CASE_CONVERT:
4047       {
4048         tree totype = TREE_TYPE (expr);
4049         tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4050
4051         /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4052            since that affects how `default_conversion' will behave.  */
4053         if (TREE_CODE (totype) == REFERENCE_TYPE
4054             || TREE_CODE (fromtype) == REFERENCE_TYPE)
4055           break;
4056         /* Don't strip a conversion from C++0x scoped enum, since they
4057            don't implicitly convert to other types.  */
4058         if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4059             && ENUM_IS_SCOPED (fromtype))
4060           break;
4061         /* If this isn't narrowing the argument, we can ignore it.  */
4062         if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4063           return c_common_truthvalue_conversion (location,
4064                                                  TREE_OPERAND (expr, 0));
4065       }
4066       break;
4067
4068     case MODIFY_EXPR:
4069       if (!TREE_NO_WARNING (expr)
4070           && warn_parentheses)
4071         {
4072           warning (OPT_Wparentheses,
4073                    "suggest parentheses around assignment used as truth value");
4074           TREE_NO_WARNING (expr) = 1;
4075         }
4076       break;
4077
4078     default:
4079       break;
4080     }
4081
4082   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4083     {
4084       tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4085       expr = (build_binary_op
4086               (EXPR_LOCATION (expr),
4087                (TREE_SIDE_EFFECTS (expr)
4088                 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4089         c_common_truthvalue_conversion
4090                (location,
4091                 build_unary_op (location, REALPART_EXPR, t, 0)),
4092         c_common_truthvalue_conversion
4093                (location,
4094                 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4095                0));
4096       goto ret;
4097     }
4098
4099   if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4100     {
4101       tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4102                                           FCONST0 (TYPE_MODE
4103                                                    (TREE_TYPE (expr))));
4104       return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4105     }
4106   else
4107     return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4108
4109  ret:
4110   protected_set_expr_location (expr, location);
4111   return expr;
4112 }
4113 \f
4114 static void def_builtin_1  (enum built_in_function fncode,
4115                             const char *name,
4116                             enum built_in_class fnclass,
4117                             tree fntype, tree libtype,
4118                             bool both_p, bool fallback_p, bool nonansi_p,
4119                             tree fnattrs, bool implicit_p);
4120
4121
4122 /* Apply the TYPE_QUALS to the new DECL.  */
4123
4124 void
4125 c_apply_type_quals_to_decl (int type_quals, tree decl)
4126 {
4127   tree type = TREE_TYPE (decl);
4128
4129   if (type == error_mark_node)
4130     return;
4131
4132   if ((type_quals & TYPE_QUAL_CONST)
4133       || (type && TREE_CODE (type) == REFERENCE_TYPE))
4134     /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4135        constructor can produce constant init, so rely on cp_finish_decl to
4136        clear TREE_READONLY if the variable has non-constant init.  */
4137     TREE_READONLY (decl) = 1;
4138   if (type_quals & TYPE_QUAL_VOLATILE)
4139     {
4140       TREE_SIDE_EFFECTS (decl) = 1;
4141       TREE_THIS_VOLATILE (decl) = 1;
4142     }
4143   if (type_quals & TYPE_QUAL_RESTRICT)
4144     {
4145       while (type && TREE_CODE (type) == ARRAY_TYPE)
4146         /* Allow 'restrict' on arrays of pointers.
4147            FIXME currently we just ignore it.  */
4148         type = TREE_TYPE (type);
4149       if (!type
4150           || !POINTER_TYPE_P (type)
4151           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4152         error ("invalid use of %<restrict%>");
4153     }
4154 }
4155
4156 /* Hash function for the problem of multiple type definitions in
4157    different files.  This must hash all types that will compare
4158    equal via comptypes to the same value.  In practice it hashes
4159    on some of the simple stuff and leaves the details to comptypes.  */
4160
4161 static hashval_t
4162 c_type_hash (const void *p)
4163 {
4164   int n_elements;
4165   int shift, size;
4166   const_tree const t = (const_tree) p;
4167   tree t2;
4168   switch (TREE_CODE (t))
4169     {
4170     /* For pointers, hash on pointee type plus some swizzling.  */
4171     case POINTER_TYPE:
4172       return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4173     /* Hash on number of elements and total size.  */
4174     case ENUMERAL_TYPE:
4175       shift = 3;
4176       t2 = TYPE_VALUES (t);
4177       break;
4178     case RECORD_TYPE:
4179       shift = 0;
4180       t2 = TYPE_FIELDS (t);
4181       break;
4182     case QUAL_UNION_TYPE:
4183       shift = 1;
4184       t2 = TYPE_FIELDS (t);
4185       break;
4186     case UNION_TYPE:
4187       shift = 2;
4188       t2 = TYPE_FIELDS (t);
4189       break;
4190     default:
4191       gcc_unreachable ();
4192     }
4193   /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4194      TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST.  */
4195   n_elements = list_length (t2);
4196   /* We might have a VLA here.  */
4197   if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4198     size = 0;
4199   else
4200     size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4201   return ((size << 24) | (n_elements << shift));
4202 }
4203
4204 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4205
4206 /* Return the typed-based alias set for T, which may be an expression
4207    or a type.  Return -1 if we don't do anything special.  */
4208
4209 alias_set_type
4210 c_common_get_alias_set (tree t)
4211 {
4212   tree u;
4213   PTR *slot;
4214
4215   /* For VLAs, use the alias set of the element type rather than the
4216      default of alias set 0 for types compared structurally.  */
4217   if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4218     {
4219       if (TREE_CODE (t) == ARRAY_TYPE)
4220         return get_alias_set (TREE_TYPE (t));
4221       return -1;
4222     }
4223
4224   /* Permit type-punning when accessing a union, provided the access
4225      is directly through the union.  For example, this code does not
4226      permit taking the address of a union member and then storing
4227      through it.  Even the type-punning allowed here is a GCC
4228      extension, albeit a common and useful one; the C standard says
4229      that such accesses have implementation-defined behavior.  */
4230   for (u = t;
4231        TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4232        u = TREE_OPERAND (u, 0))
4233     if (TREE_CODE (u) == COMPONENT_REF
4234         && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4235       return 0;
4236
4237   /* That's all the expressions we handle specially.  */
4238   if (!TYPE_P (t))
4239     return -1;
4240
4241   /* The C standard guarantees that any object may be accessed via an
4242      lvalue that has character type.  */
4243   if (t == char_type_node
4244       || t == signed_char_type_node
4245       || t == unsigned_char_type_node)
4246     return 0;
4247
4248   /* The C standard specifically allows aliasing between signed and
4249      unsigned variants of the same type.  We treat the signed
4250      variant as canonical.  */
4251   if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4252     {
4253       tree t1 = c_common_signed_type (t);
4254
4255       /* t1 == t can happen for boolean nodes which are always unsigned.  */
4256       if (t1 != t)
4257         return get_alias_set (t1);
4258     }
4259
4260   /* Handle the case of multiple type nodes referring to "the same" type,
4261      which occurs with IMA.  These share an alias set.  FIXME:  Currently only
4262      C90 is handled.  (In C99 type compatibility is not transitive, which
4263      complicates things mightily. The alias set splay trees can theoretically
4264      represent this, but insertion is tricky when you consider all the
4265      different orders things might arrive in.) */
4266
4267   if (c_language != clk_c || flag_isoc99)
4268     return -1;
4269
4270   /* Save time if there's only one input file.  */
4271   if (num_in_fnames == 1)
4272     return -1;
4273
4274   /* Pointers need special handling if they point to any type that
4275      needs special handling (below).  */
4276   if (TREE_CODE (t) == POINTER_TYPE)
4277     {
4278       tree t2;
4279       /* Find bottom type under any nested POINTERs.  */
4280       for (t2 = TREE_TYPE (t);
4281            TREE_CODE (t2) == POINTER_TYPE;
4282            t2 = TREE_TYPE (t2))
4283         ;
4284       if (TREE_CODE (t2) != RECORD_TYPE
4285           && TREE_CODE (t2) != ENUMERAL_TYPE
4286           && TREE_CODE (t2) != QUAL_UNION_TYPE
4287           && TREE_CODE (t2) != UNION_TYPE)
4288         return -1;
4289       if (TYPE_SIZE (t2) == 0)
4290         return -1;
4291     }
4292   /* These are the only cases that need special handling.  */
4293   if (TREE_CODE (t) != RECORD_TYPE
4294       && TREE_CODE (t) != ENUMERAL_TYPE
4295       && TREE_CODE (t) != QUAL_UNION_TYPE
4296       && TREE_CODE (t) != UNION_TYPE
4297       && TREE_CODE (t) != POINTER_TYPE)
4298     return -1;
4299   /* Undefined? */
4300   if (TYPE_SIZE (t) == 0)
4301     return -1;
4302
4303   /* Look up t in hash table.  Only one of the compatible types within each
4304      alias set is recorded in the table.  */
4305   if (!type_hash_table)
4306     type_hash_table = htab_create_ggc (1021, c_type_hash,
4307             (htab_eq) lang_hooks.types_compatible_p,
4308             NULL);
4309   slot = htab_find_slot (type_hash_table, t, INSERT);
4310   if (*slot != NULL)
4311     {
4312       TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4313       return TYPE_ALIAS_SET ((tree)*slot);
4314     }
4315   else
4316     /* Our caller will assign and record (in t) a new alias set; all we need
4317        to do is remember t in the hash table.  */
4318     *slot = t;
4319
4320   return -1;
4321 }
4322 \f
4323 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4324    the second parameter indicates which OPERATOR is being applied.
4325    The COMPLAIN flag controls whether we should diagnose possibly
4326    ill-formed constructs or not.  LOC is the location of the SIZEOF or
4327    TYPEOF operator.  */
4328
4329 tree
4330 c_sizeof_or_alignof_type (location_t loc,
4331                           tree type, bool is_sizeof, int complain)
4332 {
4333   const char *op_name;
4334   tree value = NULL;
4335   enum tree_code type_code = TREE_CODE (type);
4336
4337   op_name = is_sizeof ? "sizeof" : "__alignof__";
4338
4339   if (type_code == FUNCTION_TYPE)
4340     {
4341       if (is_sizeof)
4342         {
4343           if (complain && (pedantic || warn_pointer_arith))
4344             pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4345                      "invalid application of %<sizeof%> to a function type");
4346           else if (!complain)
4347             return error_mark_node;
4348           value = size_one_node;
4349         }
4350       else
4351         value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4352     }
4353   else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4354     {
4355       if (type_code == VOID_TYPE
4356           && complain && (pedantic || warn_pointer_arith))
4357         pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4358                  "invalid application of %qs to a void type", op_name);
4359       else if (!complain)
4360         return error_mark_node;
4361       value = size_one_node;
4362     }
4363   else if (!COMPLETE_TYPE_P (type))
4364     {
4365       if (complain)
4366         error_at (loc, "invalid application of %qs to incomplete type %qT ",
4367                   op_name, type);
4368       return error_mark_node;
4369     }
4370   else
4371     {
4372       if (is_sizeof)
4373         /* Convert in case a char is more than one unit.  */
4374         value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4375                                 size_int (TYPE_PRECISION (char_type_node)
4376                                           / BITS_PER_UNIT));
4377       else
4378         value = size_int (TYPE_ALIGN_UNIT (type));
4379     }
4380
4381   /* VALUE will have an integer type with TYPE_IS_SIZETYPE set.
4382      TYPE_IS_SIZETYPE means that certain things (like overflow) will
4383      never happen.  However, this node should really have type
4384      `size_t', which is just a typedef for an ordinary integer type.  */
4385   value = fold_convert_loc (loc, size_type_node, value);
4386   gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
4387
4388   return value;
4389 }
4390
4391 /* Implement the __alignof keyword: Return the minimum required
4392    alignment of EXPR, measured in bytes.  For VAR_DECLs,
4393    FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4394    from an "aligned" __attribute__ specification).  LOC is the
4395    location of the ALIGNOF operator.  */
4396
4397 tree
4398 c_alignof_expr (location_t loc, tree expr)
4399 {
4400   tree t;
4401
4402   if (VAR_OR_FUNCTION_DECL_P (expr))
4403     t = size_int (DECL_ALIGN_UNIT (expr));
4404
4405   else if (TREE_CODE (expr) == COMPONENT_REF
4406            && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4407     {
4408       error_at (loc, "%<__alignof%> applied to a bit-field");
4409       t = size_one_node;
4410     }
4411   else if (TREE_CODE (expr) == COMPONENT_REF
4412            && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4413     t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4414
4415   else if (TREE_CODE (expr) == INDIRECT_REF)
4416     {
4417       tree t = TREE_OPERAND (expr, 0);
4418       tree best = t;
4419       int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4420
4421       while (CONVERT_EXPR_P (t)
4422              && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4423         {
4424           int thisalign;
4425
4426           t = TREE_OPERAND (t, 0);
4427           thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4428           if (thisalign > bestalign)
4429             best = t, bestalign = thisalign;
4430         }
4431       return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4432     }
4433   else
4434     return c_alignof (loc, TREE_TYPE (expr));
4435
4436   return fold_convert_loc (loc, size_type_node, t);
4437 }
4438 \f
4439 /* Handle C and C++ default attributes.  */
4440
4441 enum built_in_attribute
4442 {
4443 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4444 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4445 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4446 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4447 #include "builtin-attrs.def"
4448 #undef DEF_ATTR_NULL_TREE
4449 #undef DEF_ATTR_INT
4450 #undef DEF_ATTR_IDENT
4451 #undef DEF_ATTR_TREE_LIST
4452   ATTR_LAST
4453 };
4454
4455 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4456
4457 static void c_init_attributes (void);
4458
4459 enum c_builtin_type
4460 {
4461 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4462 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4463 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4464 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4465 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4466 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4467 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4468 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4469 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4470 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4471 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4472 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4473 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4474 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4475 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4476   NAME,
4477 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4478 #include "builtin-types.def"
4479 #undef DEF_PRIMITIVE_TYPE
4480 #undef DEF_FUNCTION_TYPE_0
4481 #undef DEF_FUNCTION_TYPE_1
4482 #undef DEF_FUNCTION_TYPE_2
4483 #undef DEF_FUNCTION_TYPE_3
4484 #undef DEF_FUNCTION_TYPE_4
4485 #undef DEF_FUNCTION_TYPE_5
4486 #undef DEF_FUNCTION_TYPE_6
4487 #undef DEF_FUNCTION_TYPE_7
4488 #undef DEF_FUNCTION_TYPE_VAR_0
4489 #undef DEF_FUNCTION_TYPE_VAR_1
4490 #undef DEF_FUNCTION_TYPE_VAR_2
4491 #undef DEF_FUNCTION_TYPE_VAR_3
4492 #undef DEF_FUNCTION_TYPE_VAR_4
4493 #undef DEF_FUNCTION_TYPE_VAR_5
4494 #undef DEF_POINTER_TYPE
4495   BT_LAST
4496 };
4497
4498 typedef enum c_builtin_type builtin_type;
4499
4500 /* A temporary array for c_common_nodes_and_builtins.  Used in
4501    communication with def_fn_type.  */
4502 static tree builtin_types[(int) BT_LAST + 1];
4503
4504 /* A helper function for c_common_nodes_and_builtins.  Build function type
4505    for DEF with return type RET and N arguments.  If VAR is true, then the
4506    function should be variadic after those N arguments.
4507
4508    Takes special care not to ICE if any of the types involved are
4509    error_mark_node, which indicates that said type is not in fact available
4510    (see builtin_type_for_size).  In which case the function type as a whole
4511    should be error_mark_node.  */
4512
4513 static void
4514 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4515 {
4516   tree t;
4517   tree *args = XALLOCAVEC (tree, n);
4518   va_list list;
4519   int i;
4520
4521   va_start (list, n);
4522   for (i = 0; i < n; ++i)
4523     {
4524       builtin_type a = (builtin_type) va_arg (list, int);
4525       t = builtin_types[a];
4526       if (t == error_mark_node)
4527         goto egress;
4528       args[i] = t;
4529     }
4530
4531   t = builtin_types[ret];
4532   if (t == error_mark_node)
4533     goto egress;
4534   if (var)
4535     t = build_varargs_function_type_array (t, n, args);
4536   else
4537     t = build_function_type_array (t, n, args);
4538
4539  egress:
4540   builtin_types[def] = t;
4541   va_end (list);
4542 }
4543
4544 /* Build builtin functions common to both C and C++ language
4545    frontends.  */
4546
4547 static void
4548 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4549 {
4550 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4551   builtin_types[ENUM] = VALUE;
4552 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4553   def_fn_type (ENUM, RETURN, 0, 0);
4554 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4555   def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4556 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4557   def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4558 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4559   def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4560 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4561   def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4562 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4563   def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4564 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4565                             ARG6)                                       \
4566   def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4567 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4568                             ARG6, ARG7)                                 \
4569   def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4570 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4571   def_fn_type (ENUM, RETURN, 1, 0);
4572 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4573   def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4574 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4575   def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4576 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4577   def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4578 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4579   def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4580 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4581   def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4582 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4583   builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4584
4585 #include "builtin-types.def"
4586
4587 #undef DEF_PRIMITIVE_TYPE
4588 #undef DEF_FUNCTION_TYPE_1
4589 #undef DEF_FUNCTION_TYPE_2
4590 #undef DEF_FUNCTION_TYPE_3
4591 #undef DEF_FUNCTION_TYPE_4
4592 #undef DEF_FUNCTION_TYPE_5
4593 #undef DEF_FUNCTION_TYPE_6
4594 #undef DEF_FUNCTION_TYPE_VAR_0
4595 #undef DEF_FUNCTION_TYPE_VAR_1
4596 #undef DEF_FUNCTION_TYPE_VAR_2
4597 #undef DEF_FUNCTION_TYPE_VAR_3
4598 #undef DEF_FUNCTION_TYPE_VAR_4
4599 #undef DEF_FUNCTION_TYPE_VAR_5
4600 #undef DEF_POINTER_TYPE
4601   builtin_types[(int) BT_LAST] = NULL_TREE;
4602
4603   c_init_attributes ();
4604
4605 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4606                     NONANSI_P, ATTRS, IMPLICIT, COND)                   \
4607   if (NAME && COND)                                                     \
4608     def_builtin_1 (ENUM, NAME, CLASS,                                   \
4609                    builtin_types[(int) TYPE],                           \
4610                    builtin_types[(int) LIBTYPE],                        \
4611                    BOTH_P, FALLBACK_P, NONANSI_P,                       \
4612                    built_in_attributes[(int) ATTRS], IMPLICIT);
4613 #include "builtins.def"
4614 #undef DEF_BUILTIN
4615
4616   targetm.init_builtins ();
4617
4618   build_common_builtin_nodes ();
4619
4620   if (flag_mudflap)
4621     mudflap_init ();
4622 }
4623
4624 /* Like get_identifier, but avoid warnings about null arguments when
4625    the argument may be NULL for targets where GCC lacks stdint.h type
4626    information.  */
4627
4628 static inline tree
4629 c_get_ident (const char *id)
4630 {
4631   return get_identifier (id);
4632 }
4633
4634 /* Build tree nodes and builtin functions common to both C and C++ language
4635    frontends.  */
4636
4637 void
4638 c_common_nodes_and_builtins (void)
4639 {
4640   int char16_type_size;
4641   int char32_type_size;
4642   int wchar_type_size;
4643   tree array_domain_type;
4644   tree va_list_ref_type_node;
4645   tree va_list_arg_type_node;
4646
4647   build_common_tree_nodes (flag_signed_char, flag_short_double);
4648
4649   /* Define `int' and `char' first so that dbx will output them first.  */
4650   record_builtin_type (RID_INT, NULL, integer_type_node);
4651   record_builtin_type (RID_CHAR, "char", char_type_node);
4652
4653   /* `signed' is the same as `int'.  FIXME: the declarations of "signed",
4654      "unsigned long", "long long unsigned" and "unsigned short" were in C++
4655      but not C.  Are the conditionals here needed?  */
4656   if (c_dialect_cxx ())
4657     record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4658   record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4659   record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4660   record_builtin_type (RID_MAX, "long unsigned int",
4661                        long_unsigned_type_node);
4662   if (int128_integer_type_node != NULL_TREE)
4663     {
4664       record_builtin_type (RID_INT128, "__int128",
4665                            int128_integer_type_node);
4666       record_builtin_type (RID_MAX, "__int128 unsigned",
4667                            int128_unsigned_type_node);
4668     }
4669   if (c_dialect_cxx ())
4670     record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4671   record_builtin_type (RID_MAX, "long long int",
4672                        long_long_integer_type_node);
4673   record_builtin_type (RID_MAX, "long long unsigned int",
4674                        long_long_unsigned_type_node);
4675   if (c_dialect_cxx ())
4676     record_builtin_type (RID_MAX, "long long unsigned",
4677                          long_long_unsigned_type_node);
4678   record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4679   record_builtin_type (RID_MAX, "short unsigned int",
4680                        short_unsigned_type_node);
4681   if (c_dialect_cxx ())
4682     record_builtin_type (RID_MAX, "unsigned short",
4683                          short_unsigned_type_node);
4684
4685   /* Define both `signed char' and `unsigned char'.  */
4686   record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4687   record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4688
4689   /* These are types that c_common_type_for_size and
4690      c_common_type_for_mode use.  */
4691   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4692                                          TYPE_DECL, NULL_TREE,
4693                                          intQI_type_node));
4694   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4695                                          TYPE_DECL, NULL_TREE,
4696                                          intHI_type_node));
4697   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4698                                          TYPE_DECL, NULL_TREE,
4699                                          intSI_type_node));
4700   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4701                                          TYPE_DECL, NULL_TREE,
4702                                          intDI_type_node));
4703 #if HOST_BITS_PER_WIDE_INT >= 64
4704   if (targetm.scalar_mode_supported_p (TImode))
4705     lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4706                                            TYPE_DECL,
4707                                            get_identifier ("__int128_t"),
4708                                            intTI_type_node));
4709 #endif
4710   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4711                                          TYPE_DECL, NULL_TREE,
4712                                          unsigned_intQI_type_node));
4713   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4714                                          TYPE_DECL, NULL_TREE,
4715                                          unsigned_intHI_type_node));
4716   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4717                                          TYPE_DECL, NULL_TREE,
4718                                          unsigned_intSI_type_node));
4719   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4720                                          TYPE_DECL, NULL_TREE,
4721                                          unsigned_intDI_type_node));
4722 #if HOST_BITS_PER_WIDE_INT >= 64
4723   if (targetm.scalar_mode_supported_p (TImode))
4724     lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4725                                            TYPE_DECL,
4726                                            get_identifier ("__uint128_t"),
4727                                            unsigned_intTI_type_node));
4728 #endif
4729
4730   /* Create the widest literal types.  */
4731   widest_integer_literal_type_node
4732     = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
4733   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4734                                          TYPE_DECL, NULL_TREE,
4735                                          widest_integer_literal_type_node));
4736
4737   widest_unsigned_literal_type_node
4738     = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
4739   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4740                                          TYPE_DECL, NULL_TREE,
4741                                          widest_unsigned_literal_type_node));
4742
4743   signed_size_type_node = c_common_signed_type (size_type_node);
4744
4745   pid_type_node =
4746     TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4747
4748   record_builtin_type (RID_FLOAT, NULL, float_type_node);
4749   record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4750   record_builtin_type (RID_MAX, "long double", long_double_type_node);
4751
4752   /* Only supported decimal floating point extension if the target
4753      actually supports underlying modes. */
4754   if (targetm.scalar_mode_supported_p (SDmode)
4755       && targetm.scalar_mode_supported_p (DDmode)
4756       && targetm.scalar_mode_supported_p (TDmode))
4757     {
4758       record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4759       record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4760       record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4761     }
4762
4763   if (targetm.fixed_point_supported_p ())
4764     {
4765       record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4766       record_builtin_type (RID_FRACT, NULL, fract_type_node);
4767       record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4768       record_builtin_type (RID_MAX, "long long _Fract",
4769                            long_long_fract_type_node);
4770       record_builtin_type (RID_MAX, "unsigned short _Fract",
4771                            unsigned_short_fract_type_node);
4772       record_builtin_type (RID_MAX, "unsigned _Fract",
4773                            unsigned_fract_type_node);
4774       record_builtin_type (RID_MAX, "unsigned long _Fract",
4775                            unsigned_long_fract_type_node);
4776       record_builtin_type (RID_MAX, "unsigned long long _Fract",
4777                            unsigned_long_long_fract_type_node);
4778       record_builtin_type (RID_MAX, "_Sat short _Fract",
4779                            sat_short_fract_type_node);
4780       record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4781       record_builtin_type (RID_MAX, "_Sat long _Fract",
4782                            sat_long_fract_type_node);
4783       record_builtin_type (RID_MAX, "_Sat long long _Fract",
4784                            sat_long_long_fract_type_node);
4785       record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4786                            sat_unsigned_short_fract_type_node);
4787       record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4788                            sat_unsigned_fract_type_node);
4789       record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4790                            sat_unsigned_long_fract_type_node);
4791       record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4792                            sat_unsigned_long_long_fract_type_node);
4793       record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4794       record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4795       record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4796       record_builtin_type (RID_MAX, "long long _Accum",
4797                            long_long_accum_type_node);
4798       record_builtin_type (RID_MAX, "unsigned short _Accum",
4799                            unsigned_short_accum_type_node);
4800       record_builtin_type (RID_MAX, "unsigned _Accum",
4801                            unsigned_accum_type_node);
4802       record_builtin_type (RID_MAX, "unsigned long _Accum",
4803                            unsigned_long_accum_type_node);
4804       record_builtin_type (RID_MAX, "unsigned long long _Accum",
4805                            unsigned_long_long_accum_type_node);
4806       record_builtin_type (RID_MAX, "_Sat short _Accum",
4807                            sat_short_accum_type_node);
4808       record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4809       record_builtin_type (RID_MAX, "_Sat long _Accum",
4810                            sat_long_accum_type_node);
4811       record_builtin_type (RID_MAX, "_Sat long long _Accum",
4812                           sat_long_long_accum_type_node);
4813       record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4814                            sat_unsigned_short_accum_type_node);
4815       record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4816                            sat_unsigned_accum_type_node);
4817       record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4818                            sat_unsigned_long_accum_type_node);
4819       record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4820                            sat_unsigned_long_long_accum_type_node);
4821
4822     }
4823
4824   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4825                                          TYPE_DECL,
4826                                          get_identifier ("complex int"),
4827                                          complex_integer_type_node));
4828   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4829                                          TYPE_DECL,
4830                                          get_identifier ("complex float"),
4831                                          complex_float_type_node));
4832   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4833                                          TYPE_DECL,
4834                                          get_identifier ("complex double"),
4835                                          complex_double_type_node));
4836   lang_hooks.decls.pushdecl
4837     (build_decl (UNKNOWN_LOCATION,
4838                  TYPE_DECL, get_identifier ("complex long double"),
4839                  complex_long_double_type_node));
4840
4841   if (c_dialect_cxx ())
4842     /* For C++, make fileptr_type_node a distinct void * type until
4843        FILE type is defined.  */
4844     fileptr_type_node = build_variant_type_copy (ptr_type_node);
4845
4846   record_builtin_type (RID_VOID, NULL, void_type_node);
4847
4848   /* Set the TYPE_NAME for any variants that were built before
4849      record_builtin_type gave names to the built-in types. */
4850   {
4851     tree void_name = TYPE_NAME (void_type_node);
4852     TYPE_NAME (void_type_node) = NULL_TREE;
4853     TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4854       = void_name;
4855     TYPE_NAME (void_type_node) = void_name;
4856   }
4857
4858   /* This node must not be shared.  */
4859   void_zero_node = make_node (INTEGER_CST);
4860   TREE_TYPE (void_zero_node) = void_type_node;
4861
4862   void_list_node = build_void_list_node ();
4863
4864   /* Make a type to be the domain of a few array types
4865      whose domains don't really matter.
4866      200 is small enough that it always fits in size_t
4867      and large enough that it can hold most function names for the
4868      initializations of __FUNCTION__ and __PRETTY_FUNCTION__.  */
4869   array_domain_type = build_index_type (size_int (200));
4870
4871   /* Make a type for arrays of characters.
4872      With luck nothing will ever really depend on the length of this
4873      array type.  */
4874   char_array_type_node
4875     = build_array_type (char_type_node, array_domain_type);
4876
4877   /* Likewise for arrays of ints.  */
4878   int_array_type_node
4879     = build_array_type (integer_type_node, array_domain_type);
4880
4881   string_type_node = build_pointer_type (char_type_node);
4882   const_string_type_node
4883     = build_pointer_type (build_qualified_type
4884                           (char_type_node, TYPE_QUAL_CONST));
4885
4886   /* This is special for C++ so functions can be overloaded.  */
4887   wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4888   wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4889   wchar_type_size = TYPE_PRECISION (wchar_type_node);
4890   underlying_wchar_type_node = wchar_type_node;
4891   if (c_dialect_cxx ())
4892     {
4893       if (TYPE_UNSIGNED (wchar_type_node))
4894         wchar_type_node = make_unsigned_type (wchar_type_size);
4895       else
4896         wchar_type_node = make_signed_type (wchar_type_size);
4897       record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4898     }
4899
4900   /* This is for wide string constants.  */
4901   wchar_array_type_node
4902     = build_array_type (wchar_type_node, array_domain_type);
4903
4904   /* Define 'char16_t'.  */
4905   char16_type_node = get_identifier (CHAR16_TYPE);
4906   char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4907   char16_type_size = TYPE_PRECISION (char16_type_node);
4908   if (c_dialect_cxx ())
4909     {
4910       char16_type_node = make_unsigned_type (char16_type_size);
4911
4912       if (cxx_dialect == cxx0x)
4913         record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4914     }
4915
4916   /* This is for UTF-16 string constants.  */
4917   char16_array_type_node
4918     = build_array_type (char16_type_node, array_domain_type);
4919
4920   /* Define 'char32_t'.  */
4921   char32_type_node = get_identifier (CHAR32_TYPE);
4922   char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4923   char32_type_size = TYPE_PRECISION (char32_type_node);
4924   if (c_dialect_cxx ())
4925     {
4926       char32_type_node = make_unsigned_type (char32_type_size);
4927
4928       if (cxx_dialect == cxx0x)
4929         record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4930     }
4931
4932   /* This is for UTF-32 string constants.  */
4933   char32_array_type_node
4934     = build_array_type (char32_type_node, array_domain_type);
4935
4936   wint_type_node =
4937     TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4938
4939   intmax_type_node =
4940     TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4941   uintmax_type_node =
4942     TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4943
4944   if (SIG_ATOMIC_TYPE)
4945     sig_atomic_type_node =
4946       TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4947   if (INT8_TYPE)
4948     int8_type_node =
4949       TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4950   if (INT16_TYPE)
4951     int16_type_node =
4952       TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4953   if (INT32_TYPE)
4954     int32_type_node =
4955       TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4956   if (INT64_TYPE)
4957     int64_type_node =
4958       TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4959   if (UINT8_TYPE)
4960     uint8_type_node =
4961       TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4962   if (UINT16_TYPE)
4963     uint16_type_node =
4964       TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4965   if (UINT32_TYPE)
4966     c_uint32_type_node =
4967       TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4968   if (UINT64_TYPE)
4969     c_uint64_type_node =
4970       TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4971   if (INT_LEAST8_TYPE)
4972     int_least8_type_node =
4973       TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4974   if (INT_LEAST16_TYPE)
4975     int_least16_type_node =
4976       TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4977   if (INT_LEAST32_TYPE)
4978     int_least32_type_node =
4979       TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4980   if (INT_LEAST64_TYPE)
4981     int_least64_type_node =
4982       TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4983   if (UINT_LEAST8_TYPE)
4984     uint_least8_type_node =
4985       TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4986   if (UINT_LEAST16_TYPE)
4987     uint_least16_type_node =
4988       TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4989   if (UINT_LEAST32_TYPE)
4990     uint_least32_type_node =
4991       TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4992   if (UINT_LEAST64_TYPE)
4993     uint_least64_type_node =
4994       TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4995   if (INT_FAST8_TYPE)
4996     int_fast8_type_node =
4997       TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4998   if (INT_FAST16_TYPE)
4999     int_fast16_type_node =
5000       TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5001   if (INT_FAST32_TYPE)
5002     int_fast32_type_node =
5003       TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5004   if (INT_FAST64_TYPE)
5005     int_fast64_type_node =
5006       TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5007   if (UINT_FAST8_TYPE)
5008     uint_fast8_type_node =
5009       TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5010   if (UINT_FAST16_TYPE)
5011     uint_fast16_type_node =
5012       TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5013   if (UINT_FAST32_TYPE)
5014     uint_fast32_type_node =
5015       TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5016   if (UINT_FAST64_TYPE)
5017     uint_fast64_type_node =
5018       TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5019   if (INTPTR_TYPE)
5020     intptr_type_node =
5021       TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5022   if (UINTPTR_TYPE)
5023     uintptr_type_node =
5024       TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5025
5026   default_function_type
5027     = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5028   ptrdiff_type_node
5029     = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5030   unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5031
5032   lang_hooks.decls.pushdecl
5033     (build_decl (UNKNOWN_LOCATION,
5034                  TYPE_DECL, get_identifier ("__builtin_va_list"),
5035                  va_list_type_node));
5036   if (targetm.enum_va_list_p)
5037     {
5038       int l;
5039       const char *pname;
5040       tree ptype;
5041
5042       for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5043         {
5044           lang_hooks.decls.pushdecl
5045             (build_decl (UNKNOWN_LOCATION,
5046                          TYPE_DECL, get_identifier (pname),
5047                          ptype));
5048
5049         }
5050     }
5051
5052   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5053     {
5054       va_list_arg_type_node = va_list_ref_type_node =
5055         build_pointer_type (TREE_TYPE (va_list_type_node));
5056     }
5057   else
5058     {
5059       va_list_arg_type_node = va_list_type_node;
5060       va_list_ref_type_node = build_reference_type (va_list_type_node);
5061     }
5062
5063   if (!flag_preprocess_only)
5064     c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5065
5066   main_identifier_node = get_identifier ("main");
5067
5068   /* Create the built-in __null node.  It is important that this is
5069      not shared.  */
5070   null_node = make_node (INTEGER_CST);
5071   TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5072
5073   /* Since builtin_types isn't gc'ed, don't export these nodes.  */
5074   memset (builtin_types, 0, sizeof (builtin_types));
5075 }
5076
5077 /* The number of named compound-literals generated thus far.  */
5078 static GTY(()) int compound_literal_number;
5079
5080 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal.  */
5081
5082 void
5083 set_compound_literal_name (tree decl)
5084 {
5085   char *name;
5086   ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5087                            compound_literal_number);
5088   compound_literal_number++;
5089   DECL_NAME (decl) = get_identifier (name);
5090 }
5091
5092 tree
5093 build_va_arg (location_t loc, tree expr, tree type)
5094 {
5095   expr = build1 (VA_ARG_EXPR, type, expr);
5096   SET_EXPR_LOCATION (expr, loc);
5097   return expr;
5098 }
5099
5100
5101 /* Linked list of disabled built-in functions.  */
5102
5103 typedef struct disabled_builtin
5104 {
5105   const char *name;
5106   struct disabled_builtin *next;
5107 } disabled_builtin;
5108 static disabled_builtin *disabled_builtins = NULL;
5109
5110 static bool builtin_function_disabled_p (const char *);
5111
5112 /* Disable a built-in function specified by -fno-builtin-NAME.  If NAME
5113    begins with "__builtin_", give an error.  */
5114
5115 void
5116 disable_builtin_function (const char *name)
5117 {
5118   if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5119     error ("cannot disable built-in function %qs", name);
5120   else
5121     {
5122       disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5123       new_disabled_builtin->name = name;
5124       new_disabled_builtin->next = disabled_builtins;
5125       disabled_builtins = new_disabled_builtin;
5126     }
5127 }
5128
5129
5130 /* Return true if the built-in function NAME has been disabled, false
5131    otherwise.  */
5132
5133 static bool
5134 builtin_function_disabled_p (const char *name)
5135 {
5136   disabled_builtin *p;
5137   for (p = disabled_builtins; p != NULL; p = p->next)
5138     {
5139       if (strcmp (name, p->name) == 0)
5140         return true;
5141     }
5142   return false;
5143 }
5144
5145
5146 /* Worker for DEF_BUILTIN.
5147    Possibly define a builtin function with one or two names.
5148    Does not declare a non-__builtin_ function if flag_no_builtin, or if
5149    nonansi_p and flag_no_nonansi_builtin.  */
5150
5151 static void
5152 def_builtin_1 (enum built_in_function fncode,
5153                const char *name,
5154                enum built_in_class fnclass,
5155                tree fntype, tree libtype,
5156                bool both_p, bool fallback_p, bool nonansi_p,
5157                tree fnattrs, bool implicit_p)
5158 {
5159   tree decl;
5160   const char *libname;
5161
5162   if (fntype == error_mark_node)
5163     return;
5164
5165   gcc_assert ((!both_p && !fallback_p)
5166               || !strncmp (name, "__builtin_",
5167                            strlen ("__builtin_")));
5168
5169   libname = name + strlen ("__builtin_");
5170   decl = add_builtin_function (name, fntype, fncode, fnclass,
5171                                (fallback_p ? libname : NULL),
5172                                fnattrs);
5173
5174   set_builtin_decl (fncode, decl, implicit_p);
5175
5176   if (both_p
5177       && !flag_no_builtin && !builtin_function_disabled_p (libname)
5178       && !(nonansi_p && flag_no_nonansi_builtin))
5179     add_builtin_function (libname, libtype, fncode, fnclass,
5180                           NULL, fnattrs);
5181 }
5182 \f
5183 /* Nonzero if the type T promotes to int.  This is (nearly) the
5184    integral promotions defined in ISO C99 6.3.1.1/2.  */
5185
5186 bool
5187 c_promoting_integer_type_p (const_tree t)
5188 {
5189   switch (TREE_CODE (t))
5190     {
5191     case INTEGER_TYPE:
5192       return (TYPE_MAIN_VARIANT (t) == char_type_node
5193               || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5194               || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5195               || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5196               || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5197               || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5198
5199     case ENUMERAL_TYPE:
5200       /* ??? Technically all enumerations not larger than an int
5201          promote to an int.  But this is used along code paths
5202          that only want to notice a size change.  */
5203       return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5204
5205     case BOOLEAN_TYPE:
5206       return 1;
5207
5208     default:
5209       return 0;
5210     }
5211 }
5212
5213 /* Return 1 if PARMS specifies a fixed number of parameters
5214    and none of their types is affected by default promotions.  */
5215
5216 int
5217 self_promoting_args_p (const_tree parms)
5218 {
5219   const_tree t;
5220   for (t = parms; t; t = TREE_CHAIN (t))
5221     {
5222       tree type = TREE_VALUE (t);
5223
5224       if (type == error_mark_node)
5225         continue;
5226
5227       if (TREE_CHAIN (t) == 0 && type != void_type_node)
5228         return 0;
5229
5230       if (type == 0)
5231         return 0;
5232
5233       if (TYPE_MAIN_VARIANT (type) == float_type_node)
5234         return 0;
5235
5236       if (c_promoting_integer_type_p (type))
5237         return 0;
5238     }
5239   return 1;
5240 }
5241
5242 /* Recursively remove any '*' or '&' operator from TYPE.  */
5243 tree
5244 strip_pointer_operator (tree t)
5245 {
5246   while (POINTER_TYPE_P (t))
5247     t = TREE_TYPE (t);
5248   return t;
5249 }
5250
5251 /* Recursively remove pointer or array type from TYPE. */
5252 tree
5253 strip_pointer_or_array_types (tree t)
5254 {
5255   while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5256     t = TREE_TYPE (t);
5257   return t;
5258 }
5259
5260 /* Used to compare case labels.  K1 and K2 are actually tree nodes
5261    representing case labels, or NULL_TREE for a `default' label.
5262    Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5263    K2, and 0 if K1 and K2 are equal.  */
5264
5265 int
5266 case_compare (splay_tree_key k1, splay_tree_key k2)
5267 {
5268   /* Consider a NULL key (such as arises with a `default' label) to be
5269      smaller than anything else.  */
5270   if (!k1)
5271     return k2 ? -1 : 0;
5272   else if (!k2)
5273     return k1 ? 1 : 0;
5274
5275   return tree_int_cst_compare ((tree) k1, (tree) k2);
5276 }
5277
5278 /* Process a case label, located at LOC, for the range LOW_VALUE
5279    ... HIGH_VALUE.  If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5280    then this case label is actually a `default' label.  If only
5281    HIGH_VALUE is NULL_TREE, then case label was declared using the
5282    usual C/C++ syntax, rather than the GNU case range extension.
5283    CASES is a tree containing all the case ranges processed so far;
5284    COND is the condition for the switch-statement itself.  Returns the
5285    CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5286    is created.  */
5287
5288 tree
5289 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5290                   tree low_value, tree high_value)
5291 {
5292   tree type;
5293   tree label;
5294   tree case_label;
5295   splay_tree_node node;
5296
5297   /* Create the LABEL_DECL itself.  */
5298   label = create_artificial_label (loc);
5299
5300   /* If there was an error processing the switch condition, bail now
5301      before we get more confused.  */
5302   if (!cond || cond == error_mark_node)
5303     goto error_out;
5304
5305   if ((low_value && TREE_TYPE (low_value)
5306        && POINTER_TYPE_P (TREE_TYPE (low_value)))
5307       || (high_value && TREE_TYPE (high_value)
5308           && POINTER_TYPE_P (TREE_TYPE (high_value))))
5309     {
5310       error_at (loc, "pointers are not permitted as case values");
5311       goto error_out;
5312     }
5313
5314   /* Case ranges are a GNU extension.  */
5315   if (high_value)
5316     pedwarn (loc, OPT_pedantic,
5317              "range expressions in switch statements are non-standard");
5318
5319   type = TREE_TYPE (cond);
5320   if (low_value)
5321     {
5322       low_value = check_case_value (low_value);
5323       low_value = convert_and_check (type, low_value);
5324       if (low_value == error_mark_node)
5325         goto error_out;
5326     }
5327   if (high_value)
5328     {
5329       high_value = check_case_value (high_value);
5330       high_value = convert_and_check (type, high_value);
5331       if (high_value == error_mark_node)
5332         goto error_out;
5333     }
5334
5335   if (low_value && high_value)
5336     {
5337       /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5338          really a case range, even though it was written that way.
5339          Remove the HIGH_VALUE to simplify later processing.  */
5340       if (tree_int_cst_equal (low_value, high_value))
5341         high_value = NULL_TREE;
5342       else if (!tree_int_cst_lt (low_value, high_value))
5343         warning_at (loc, 0, "empty range specified");
5344     }
5345
5346   /* See if the case is in range of the type of the original testing
5347      expression.  If both low_value and high_value are out of range,
5348      don't insert the case label and return NULL_TREE.  */
5349   if (low_value
5350       && !check_case_bounds (type, orig_type,
5351                              &low_value, high_value ? &high_value : NULL))
5352     return NULL_TREE;
5353
5354   /* Look up the LOW_VALUE in the table of case labels we already
5355      have.  */
5356   node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5357   /* If there was not an exact match, check for overlapping ranges.
5358      There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5359      that's a `default' label and the only overlap is an exact match.  */
5360   if (!node && (low_value || high_value))
5361     {
5362       splay_tree_node low_bound;
5363       splay_tree_node high_bound;
5364
5365       /* Even though there wasn't an exact match, there might be an
5366          overlap between this case range and another case range.
5367          Since we've (inductively) not allowed any overlapping case
5368          ranges, we simply need to find the greatest low case label
5369          that is smaller that LOW_VALUE, and the smallest low case
5370          label that is greater than LOW_VALUE.  If there is an overlap
5371          it will occur in one of these two ranges.  */
5372       low_bound = splay_tree_predecessor (cases,
5373                                           (splay_tree_key) low_value);
5374       high_bound = splay_tree_successor (cases,
5375                                          (splay_tree_key) low_value);
5376
5377       /* Check to see if the LOW_BOUND overlaps.  It is smaller than
5378          the LOW_VALUE, so there is no need to check unless the
5379          LOW_BOUND is in fact itself a case range.  */
5380       if (low_bound
5381           && CASE_HIGH ((tree) low_bound->value)
5382           && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5383                                     low_value) >= 0)
5384         node = low_bound;
5385       /* Check to see if the HIGH_BOUND overlaps.  The low end of that
5386          range is bigger than the low end of the current range, so we
5387          are only interested if the current range is a real range, and
5388          not an ordinary case label.  */
5389       else if (high_bound
5390                && high_value
5391                && (tree_int_cst_compare ((tree) high_bound->key,
5392                                          high_value)
5393                    <= 0))
5394         node = high_bound;
5395     }
5396   /* If there was an overlap, issue an error.  */
5397   if (node)
5398     {
5399       tree duplicate = CASE_LABEL ((tree) node->value);
5400
5401       if (high_value)
5402         {
5403           error_at (loc, "duplicate (or overlapping) case value");
5404           error_at (DECL_SOURCE_LOCATION (duplicate),
5405                     "this is the first entry overlapping that value");
5406         }
5407       else if (low_value)
5408         {
5409           error_at (loc, "duplicate case value") ;
5410           error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5411         }
5412       else
5413         {
5414           error_at (loc, "multiple default labels in one switch");
5415           error_at (DECL_SOURCE_LOCATION (duplicate),
5416                     "this is the first default label");
5417         }
5418       goto error_out;
5419     }
5420
5421   /* Add a CASE_LABEL to the statement-tree.  */
5422   case_label = add_stmt (build_case_label (low_value, high_value, label));
5423   /* Register this case label in the splay tree.  */
5424   splay_tree_insert (cases,
5425                      (splay_tree_key) low_value,
5426                      (splay_tree_value) case_label);
5427
5428   return case_label;
5429
5430  error_out:
5431   /* Add a label so that the back-end doesn't think that the beginning of
5432      the switch is unreachable.  Note that we do not add a case label, as
5433      that just leads to duplicates and thence to failure later on.  */
5434   if (!cases->root)
5435     {
5436       tree t = create_artificial_label (loc);
5437       add_stmt (build_stmt (loc, LABEL_EXPR, t));
5438     }
5439   return error_mark_node;
5440 }
5441
5442 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5443    Used to verify that case values match up with enumerator values.  */
5444
5445 static void
5446 match_case_to_enum_1 (tree key, tree type, tree label)
5447 {
5448   char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5449
5450   /* ??? Not working too hard to print the double-word value.
5451      Should perhaps be done with %lwd in the diagnostic routines?  */
5452   if (TREE_INT_CST_HIGH (key) == 0)
5453     snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5454               TREE_INT_CST_LOW (key));
5455   else if (!TYPE_UNSIGNED (type)
5456            && TREE_INT_CST_HIGH (key) == -1
5457            && TREE_INT_CST_LOW (key) != 0)
5458     snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5459               -TREE_INT_CST_LOW (key));
5460   else
5461     snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5462               (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5463               (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5464
5465   if (TYPE_NAME (type) == 0)
5466     warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5467                 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5468                 "case value %qs not in enumerated type",
5469                 buf);
5470   else
5471     warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5472                 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5473                 "case value %qs not in enumerated type %qT",
5474                 buf, type);
5475 }
5476
5477 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5478    Used to verify that case values match up with enumerator values.  */
5479
5480 static int
5481 match_case_to_enum (splay_tree_node node, void *data)
5482 {
5483   tree label = (tree) node->value;
5484   tree type = (tree) data;
5485
5486   /* Skip default case.  */
5487   if (!CASE_LOW (label))
5488     return 0;
5489
5490   /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5491      when we did our enum->case scan.  Reset our scratch bit after.  */
5492   if (!CASE_LOW_SEEN (label))
5493     match_case_to_enum_1 (CASE_LOW (label), type, label);
5494   else
5495     CASE_LOW_SEEN (label) = 0;
5496
5497   /* If CASE_HIGH is non-null, we have a range.  If CASE_HIGH_SEEN is
5498      not set, that means that CASE_HIGH did not appear when we did our
5499      enum->case scan.  Reset our scratch bit after.  */
5500   if (CASE_HIGH (label))
5501     {
5502       if (!CASE_HIGH_SEEN (label))
5503         match_case_to_enum_1 (CASE_HIGH (label), type, label);
5504       else
5505         CASE_HIGH_SEEN (label) = 0;
5506     }
5507
5508   return 0;
5509 }
5510
5511 /* Handle -Wswitch*.  Called from the front end after parsing the
5512    switch construct.  */
5513 /* ??? Should probably be somewhere generic, since other languages
5514    besides C and C++ would want this.  At the moment, however, C/C++
5515    are the only tree-ssa languages that support enumerations at all,
5516    so the point is moot.  */
5517
5518 void
5519 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5520                       tree type, tree cond)
5521 {
5522   splay_tree_node default_node;
5523   splay_tree_node node;
5524   tree chain;
5525
5526   if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5527     return;
5528
5529   default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5530   if (!default_node)
5531     warning_at (switch_location, OPT_Wswitch_default,
5532                 "switch missing default case");
5533
5534   /* From here on, we only care about about enumerated types.  */
5535   if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5536     return;
5537
5538   /* From here on, we only care about -Wswitch and -Wswitch-enum.  */
5539   if (!warn_switch_enum && !warn_switch)
5540     return;
5541
5542   /* Check the cases.  Warn about case values which are not members of
5543      the enumerated type.  For -Wswitch-enum, or for -Wswitch when
5544      there is no default case, check that exactly all enumeration
5545      literals are covered by the cases.  */
5546
5547   /* Clearing COND if it is not an integer constant simplifies
5548      the tests inside the loop below.  */
5549   if (TREE_CODE (cond) != INTEGER_CST)
5550     cond = NULL_TREE;
5551
5552   /* The time complexity here is O(N*lg(N)) worst case, but for the
5553       common case of monotonically increasing enumerators, it is
5554       O(N), since the nature of the splay tree will keep the next
5555       element adjacent to the root at all times.  */
5556
5557   for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5558     {
5559       tree value = TREE_VALUE (chain);
5560       if (TREE_CODE (value) == CONST_DECL)
5561         value = DECL_INITIAL (value);
5562       node = splay_tree_lookup (cases, (splay_tree_key) value);
5563       if (node)
5564         {
5565           /* Mark the CASE_LOW part of the case entry as seen.  */
5566           tree label = (tree) node->value;
5567           CASE_LOW_SEEN (label) = 1;
5568           continue;
5569         }
5570
5571       /* Even though there wasn't an exact match, there might be a
5572          case range which includes the enumerator's value.  */
5573       node = splay_tree_predecessor (cases, (splay_tree_key) value);
5574       if (node && CASE_HIGH ((tree) node->value))
5575         {
5576           tree label = (tree) node->value;
5577           int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
5578           if (cmp >= 0)
5579             {
5580               /* If we match the upper bound exactly, mark the CASE_HIGH
5581                  part of the case entry as seen.  */
5582               if (cmp == 0)
5583                 CASE_HIGH_SEEN (label) = 1;
5584               continue;
5585             }
5586         }
5587
5588       /* We've now determined that this enumerated literal isn't
5589          handled by the case labels of the switch statement.  */
5590
5591       /* If the switch expression is a constant, we only really care
5592          about whether that constant is handled by the switch.  */
5593       if (cond && tree_int_cst_compare (cond, value))
5594         continue;
5595
5596       /* If there is a default_node, the only relevant option is
5597          Wswitch-enum.  Otherwise, if both are enabled then we prefer
5598          to warn using -Wswitch because -Wswitch is enabled by -Wall
5599          while -Wswitch-enum is explicit.  */
5600       warning_at (switch_location,
5601                   (default_node || !warn_switch
5602                    ? OPT_Wswitch_enum
5603                    : OPT_Wswitch),
5604                   "enumeration value %qE not handled in switch",
5605                   TREE_PURPOSE (chain));
5606     }
5607
5608   /* Warn if there are case expressions that don't correspond to
5609      enumerators.  This can occur since C and C++ don't enforce
5610      type-checking of assignments to enumeration variables.
5611
5612      The time complexity here is now always O(N) worst case, since
5613      we should have marked both the lower bound and upper bound of
5614      every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
5615      above.  This scan also resets those fields.  */
5616
5617   splay_tree_foreach (cases, match_case_to_enum, type);
5618 }
5619
5620 /* Finish an expression taking the address of LABEL (an
5621    IDENTIFIER_NODE).  Returns an expression for the address.
5622
5623    LOC is the location for the expression returned.  */
5624
5625 tree
5626 finish_label_address_expr (tree label, location_t loc)
5627 {
5628   tree result;
5629
5630   pedwarn (input_location, OPT_pedantic, "taking the address of a label is non-standard");
5631
5632   if (label == error_mark_node)
5633     return error_mark_node;
5634
5635   label = lookup_label (label);
5636   if (label == NULL_TREE)
5637     result = null_pointer_node;
5638   else
5639     {
5640       TREE_USED (label) = 1;
5641       result = build1 (ADDR_EXPR, ptr_type_node, label);
5642       /* The current function is not necessarily uninlinable.
5643          Computed gotos are incompatible with inlining, but the value
5644          here could be used only in a diagnostic, for example.  */
5645       protected_set_expr_location (result, loc);
5646     }
5647
5648   return result;
5649 }
5650 \f
5651
5652 /* Given a boolean expression ARG, return a tree representing an increment
5653    or decrement (as indicated by CODE) of ARG.  The front end must check for
5654    invalid cases (e.g., decrement in C++).  */
5655 tree
5656 boolean_increment (enum tree_code code, tree arg)
5657 {
5658   tree val;
5659   tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5660
5661   arg = stabilize_reference (arg);
5662   switch (code)
5663     {
5664     case PREINCREMENT_EXPR:
5665       val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5666       break;
5667     case POSTINCREMENT_EXPR:
5668       val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
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     case PREDECREMENT_EXPR:
5674       val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5675                     invert_truthvalue_loc (input_location, arg));
5676       break;
5677     case POSTDECREMENT_EXPR:
5678       val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5679                     invert_truthvalue_loc (input_location, arg));
5680       arg = save_expr (arg);
5681       val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5682       val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5683       break;
5684     default:
5685       gcc_unreachable ();
5686     }
5687   TREE_SIDE_EFFECTS (val) = 1;
5688   return val;
5689 }
5690 \f
5691 /* Built-in macros for stddef.h and stdint.h, that require macros
5692    defined in this file.  */
5693 void
5694 c_stddef_cpp_builtins(void)
5695 {
5696   builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5697   builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5698   builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5699   builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5700   builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5701   builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5702   builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5703   builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5704   if (SIG_ATOMIC_TYPE)
5705     builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5706   if (INT8_TYPE)
5707     builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5708   if (INT16_TYPE)
5709     builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5710   if (INT32_TYPE)
5711     builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5712   if (INT64_TYPE)
5713     builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5714   if (UINT8_TYPE)
5715     builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5716   if (UINT16_TYPE)
5717     builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5718   if (UINT32_TYPE)
5719     builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5720   if (UINT64_TYPE)
5721     builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5722   if (INT_LEAST8_TYPE)
5723     builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5724   if (INT_LEAST16_TYPE)
5725     builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5726   if (INT_LEAST32_TYPE)
5727     builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5728   if (INT_LEAST64_TYPE)
5729     builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5730   if (UINT_LEAST8_TYPE)
5731     builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5732   if (UINT_LEAST16_TYPE)
5733     builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5734   if (UINT_LEAST32_TYPE)
5735     builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5736   if (UINT_LEAST64_TYPE)
5737     builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5738   if (INT_FAST8_TYPE)
5739     builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5740   if (INT_FAST16_TYPE)
5741     builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5742   if (INT_FAST32_TYPE)
5743     builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5744   if (INT_FAST64_TYPE)
5745     builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5746   if (UINT_FAST8_TYPE)
5747     builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5748   if (UINT_FAST16_TYPE)
5749     builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5750   if (UINT_FAST32_TYPE)
5751     builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5752   if (UINT_FAST64_TYPE)
5753     builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5754   if (INTPTR_TYPE)
5755     builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5756   if (UINTPTR_TYPE)
5757     builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5758 }
5759
5760 static void
5761 c_init_attributes (void)
5762 {
5763   /* Fill in the built_in_attributes array.  */
5764 #define DEF_ATTR_NULL_TREE(ENUM)                                \
5765   built_in_attributes[(int) ENUM] = NULL_TREE;
5766 #define DEF_ATTR_INT(ENUM, VALUE)                               \
5767   built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5768 #define DEF_ATTR_IDENT(ENUM, STRING)                            \
5769   built_in_attributes[(int) ENUM] = get_identifier (STRING);
5770 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5771   built_in_attributes[(int) ENUM]                       \
5772     = tree_cons (built_in_attributes[(int) PURPOSE],    \
5773                  built_in_attributes[(int) VALUE],      \
5774                  built_in_attributes[(int) CHAIN]);
5775 #include "builtin-attrs.def"
5776 #undef DEF_ATTR_NULL_TREE
5777 #undef DEF_ATTR_INT
5778 #undef DEF_ATTR_IDENT
5779 #undef DEF_ATTR_TREE_LIST
5780 }
5781
5782 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
5783    identifier as an argument, so the front end shouldn't look it up.  */
5784
5785 bool
5786 attribute_takes_identifier_p (const_tree attr_id)
5787 {
5788   const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
5789   if (spec == NULL)
5790     /* Unknown attribute that we'll end up ignoring, return true so we
5791        don't complain about an identifier argument.  */
5792     return true;
5793   else if (!strcmp ("mode", spec->name)
5794            || !strcmp ("format", spec->name)
5795            || !strcmp ("cleanup", spec->name))
5796     return true;
5797   else
5798     return targetm.attribute_takes_identifier_p (attr_id);
5799 }
5800
5801 /* Attribute handlers common to C front ends.  */
5802
5803 /* Handle a "packed" attribute; arguments as in
5804    struct attribute_spec.handler.  */
5805
5806 static tree
5807 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5808                          int flags, bool *no_add_attrs)
5809 {
5810   if (TYPE_P (*node))
5811     {
5812       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5813         *node = build_variant_type_copy (*node);
5814       TYPE_PACKED (*node) = 1;
5815     }
5816   else if (TREE_CODE (*node) == FIELD_DECL)
5817     {
5818       if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
5819           /* Still pack bitfields.  */
5820           && ! DECL_INITIAL (*node))
5821         warning (OPT_Wattributes,
5822                  "%qE attribute ignored for field of type %qT",
5823                  name, TREE_TYPE (*node));
5824       else
5825         DECL_PACKED (*node) = 1;
5826     }
5827   /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
5828      used for DECL_REGISTER.  It wouldn't mean anything anyway.
5829      We can't set DECL_PACKED on the type of a TYPE_DECL, because
5830      that changes what the typedef is typing.  */
5831   else
5832     {
5833       warning (OPT_Wattributes, "%qE attribute ignored", name);
5834       *no_add_attrs = true;
5835     }
5836
5837   return NULL_TREE;
5838 }
5839
5840 /* Handle a "nocommon" attribute; arguments as in
5841    struct attribute_spec.handler.  */
5842
5843 static tree
5844 handle_nocommon_attribute (tree *node, tree name,
5845                            tree ARG_UNUSED (args),
5846                            int ARG_UNUSED (flags), bool *no_add_attrs)
5847 {
5848   if (TREE_CODE (*node) == VAR_DECL)
5849     DECL_COMMON (*node) = 0;
5850   else
5851     {
5852       warning (OPT_Wattributes, "%qE attribute ignored", name);
5853       *no_add_attrs = true;
5854     }
5855
5856   return NULL_TREE;
5857 }
5858
5859 /* Handle a "common" attribute; arguments as in
5860    struct attribute_spec.handler.  */
5861
5862 static tree
5863 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5864                          int ARG_UNUSED (flags), bool *no_add_attrs)
5865 {
5866   if (TREE_CODE (*node) == VAR_DECL)
5867     DECL_COMMON (*node) = 1;
5868   else
5869     {
5870       warning (OPT_Wattributes, "%qE attribute ignored", name);
5871       *no_add_attrs = true;
5872     }
5873
5874   return NULL_TREE;
5875 }
5876
5877 /* Handle a "noreturn" attribute; arguments as in
5878    struct attribute_spec.handler.  */
5879
5880 static tree
5881 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5882                            int ARG_UNUSED (flags), bool *no_add_attrs)
5883 {
5884   tree type = TREE_TYPE (*node);
5885
5886   /* See FIXME comment in c_common_attribute_table.  */
5887   if (TREE_CODE (*node) == FUNCTION_DECL
5888       || objc_method_decl (TREE_CODE (*node)))
5889     TREE_THIS_VOLATILE (*node) = 1;
5890   else if (TREE_CODE (type) == POINTER_TYPE
5891            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5892     TREE_TYPE (*node)
5893       = build_pointer_type
5894         (build_type_variant (TREE_TYPE (type),
5895                              TYPE_READONLY (TREE_TYPE (type)), 1));
5896   else
5897     {
5898       warning (OPT_Wattributes, "%qE attribute ignored", name);
5899       *no_add_attrs = true;
5900     }
5901
5902   return NULL_TREE;
5903 }
5904
5905 /* Handle a "hot" and attribute; arguments as in
5906    struct attribute_spec.handler.  */
5907
5908 static tree
5909 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5910                       int ARG_UNUSED (flags), bool *no_add_attrs)
5911 {
5912   if (TREE_CODE (*node) == FUNCTION_DECL)
5913     {
5914       if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
5915         {
5916           warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5917                    name, "cold");
5918           *no_add_attrs = true;
5919         }
5920       /* Most of the rest of the hot processing is done later with
5921          lookup_attribute.  */
5922     }
5923   else
5924     {
5925       warning (OPT_Wattributes, "%qE attribute ignored", name);
5926       *no_add_attrs = true;
5927     }
5928
5929   return NULL_TREE;
5930 }
5931 /* Handle a "cold" and attribute; arguments as in
5932    struct attribute_spec.handler.  */
5933
5934 static tree
5935 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5936                        int ARG_UNUSED (flags), bool *no_add_attrs)
5937 {
5938   if (TREE_CODE (*node) == FUNCTION_DECL)
5939     {
5940       if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
5941         {
5942           warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5943                    name, "hot");
5944           *no_add_attrs = true;
5945         }
5946       /* Most of the rest of the cold processing is done later with
5947          lookup_attribute.  */
5948     }
5949   else
5950     {
5951       warning (OPT_Wattributes, "%qE attribute ignored", name);
5952       *no_add_attrs = true;
5953     }
5954
5955   return NULL_TREE;
5956 }
5957
5958 /* Handle a "noinline" attribute; arguments as in
5959    struct attribute_spec.handler.  */
5960
5961 static tree
5962 handle_noinline_attribute (tree *node, tree name,
5963                            tree ARG_UNUSED (args),
5964                            int ARG_UNUSED (flags), bool *no_add_attrs)
5965 {
5966   if (TREE_CODE (*node) == FUNCTION_DECL)
5967     DECL_UNINLINABLE (*node) = 1;
5968   else
5969     {
5970       warning (OPT_Wattributes, "%qE attribute ignored", name);
5971       *no_add_attrs = true;
5972     }
5973
5974   return NULL_TREE;
5975 }
5976
5977 /* Handle a "noclone" attribute; arguments as in
5978    struct attribute_spec.handler.  */
5979
5980 static tree
5981 handle_noclone_attribute (tree *node, tree name,
5982                           tree ARG_UNUSED (args),
5983                           int ARG_UNUSED (flags), bool *no_add_attrs)
5984 {
5985   if (TREE_CODE (*node) != FUNCTION_DECL)
5986     {
5987       warning (OPT_Wattributes, "%qE attribute ignored", name);
5988       *no_add_attrs = true;
5989     }
5990
5991   return NULL_TREE;
5992 }
5993
5994 /* Handle a "always_inline" attribute; arguments as in
5995    struct attribute_spec.handler.  */
5996
5997 static tree
5998 handle_always_inline_attribute (tree *node, tree name,
5999                                 tree ARG_UNUSED (args),
6000                                 int ARG_UNUSED (flags),
6001                                 bool *no_add_attrs)
6002 {
6003   if (TREE_CODE (*node) == FUNCTION_DECL)
6004     {
6005       /* Set the attribute and mark it for disregarding inline
6006          limits.  */
6007       DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6008     }
6009   else
6010     {
6011       warning (OPT_Wattributes, "%qE attribute ignored", name);
6012       *no_add_attrs = true;
6013     }
6014
6015   return NULL_TREE;
6016 }
6017
6018 /* Handle a "gnu_inline" attribute; arguments as in
6019    struct attribute_spec.handler.  */
6020
6021 static tree
6022 handle_gnu_inline_attribute (tree *node, tree name,
6023                              tree ARG_UNUSED (args),
6024                              int ARG_UNUSED (flags),
6025                              bool *no_add_attrs)
6026 {
6027   if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6028     {
6029       /* Do nothing else, just set the attribute.  We'll get at
6030          it later with lookup_attribute.  */
6031     }
6032   else
6033     {
6034       warning (OPT_Wattributes, "%qE attribute ignored", name);
6035       *no_add_attrs = true;
6036     }
6037
6038   return NULL_TREE;
6039 }
6040
6041 /* Handle a "leaf" attribute; arguments as in
6042    struct attribute_spec.handler.  */
6043
6044 static tree
6045 handle_leaf_attribute (tree *node, tree name,
6046                        tree ARG_UNUSED (args),
6047                        int ARG_UNUSED (flags), bool *no_add_attrs)
6048 {
6049   if (TREE_CODE (*node) != FUNCTION_DECL)
6050     {
6051       warning (OPT_Wattributes, "%qE attribute ignored", name);
6052       *no_add_attrs = true;
6053     }
6054   if (!TREE_PUBLIC (*node))
6055     {
6056       warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6057       *no_add_attrs = true;
6058     }
6059
6060   return NULL_TREE;
6061 }
6062
6063 /* Handle an "artificial" attribute; arguments as in
6064    struct attribute_spec.handler.  */
6065
6066 static tree
6067 handle_artificial_attribute (tree *node, tree name,
6068                              tree ARG_UNUSED (args),
6069                              int ARG_UNUSED (flags),
6070                              bool *no_add_attrs)
6071 {
6072   if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6073     {
6074       /* Do nothing else, just set the attribute.  We'll get at
6075          it later with lookup_attribute.  */
6076     }
6077   else
6078     {
6079       warning (OPT_Wattributes, "%qE attribute ignored", name);
6080       *no_add_attrs = true;
6081     }
6082
6083   return NULL_TREE;
6084 }
6085
6086 /* Handle a "flatten" attribute; arguments as in
6087    struct attribute_spec.handler.  */
6088
6089 static tree
6090 handle_flatten_attribute (tree *node, tree name,
6091                           tree args ATTRIBUTE_UNUSED,
6092                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6093 {
6094   if (TREE_CODE (*node) == FUNCTION_DECL)
6095     /* Do nothing else, just set the attribute.  We'll get at
6096        it later with lookup_attribute.  */
6097     ;
6098   else
6099     {
6100       warning (OPT_Wattributes, "%qE attribute ignored", name);
6101       *no_add_attrs = true;
6102     }
6103
6104   return NULL_TREE;
6105 }
6106
6107 /* Handle a "warning" or "error" attribute; arguments as in
6108    struct attribute_spec.handler.  */
6109
6110 static tree
6111 handle_error_attribute (tree *node, tree name, tree args,
6112                         int ARG_UNUSED (flags), bool *no_add_attrs)
6113 {
6114   if (TREE_CODE (*node) == FUNCTION_DECL
6115       || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6116     /* Do nothing else, just set the attribute.  We'll get at
6117        it later with lookup_attribute.  */
6118     ;
6119   else
6120     {
6121       warning (OPT_Wattributes, "%qE attribute ignored", name);
6122       *no_add_attrs = true;
6123     }
6124
6125   return NULL_TREE;
6126 }
6127
6128 /* Handle a "used" attribute; arguments as in
6129    struct attribute_spec.handler.  */
6130
6131 static tree
6132 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6133                        int ARG_UNUSED (flags), bool *no_add_attrs)
6134 {
6135   tree node = *pnode;
6136
6137   if (TREE_CODE (node) == FUNCTION_DECL
6138       || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6139       || (TREE_CODE (node) == TYPE_DECL))
6140     {
6141       TREE_USED (node) = 1;
6142       DECL_PRESERVE_P (node) = 1;
6143       if (TREE_CODE (node) == VAR_DECL)
6144         DECL_READ_P (node) = 1;
6145     }
6146   else
6147     {
6148       warning (OPT_Wattributes, "%qE attribute ignored", name);
6149       *no_add_attrs = true;
6150     }
6151
6152   return NULL_TREE;
6153 }
6154
6155 /* Handle a "unused" attribute; arguments as in
6156    struct attribute_spec.handler.  */
6157
6158 static tree
6159 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6160                          int flags, bool *no_add_attrs)
6161 {
6162   if (DECL_P (*node))
6163     {
6164       tree decl = *node;
6165
6166       if (TREE_CODE (decl) == PARM_DECL
6167           || TREE_CODE (decl) == VAR_DECL
6168           || TREE_CODE (decl) == FUNCTION_DECL
6169           || TREE_CODE (decl) == LABEL_DECL
6170           || TREE_CODE (decl) == TYPE_DECL)
6171         {
6172           TREE_USED (decl) = 1;
6173           if (TREE_CODE (decl) == VAR_DECL
6174               || TREE_CODE (decl) == PARM_DECL)
6175             DECL_READ_P (decl) = 1;
6176         }
6177       else
6178         {
6179           warning (OPT_Wattributes, "%qE attribute ignored", name);
6180           *no_add_attrs = true;
6181         }
6182     }
6183   else
6184     {
6185       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6186         *node = build_variant_type_copy (*node);
6187       TREE_USED (*node) = 1;
6188     }
6189
6190   return NULL_TREE;
6191 }
6192
6193 /* Handle a "externally_visible" attribute; arguments as in
6194    struct attribute_spec.handler.  */
6195
6196 static tree
6197 handle_externally_visible_attribute (tree *pnode, tree name,
6198                                      tree ARG_UNUSED (args),
6199                                      int ARG_UNUSED (flags),
6200                                      bool *no_add_attrs)
6201 {
6202   tree node = *pnode;
6203
6204   if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6205     {
6206       if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6207            && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6208         {
6209           warning (OPT_Wattributes,
6210                    "%qE attribute have effect only on public objects", name);
6211           *no_add_attrs = true;
6212         }
6213     }
6214   else
6215     {
6216       warning (OPT_Wattributes, "%qE attribute ignored", name);
6217       *no_add_attrs = true;
6218     }
6219
6220   return NULL_TREE;
6221 }
6222
6223 /* Handle a "const" attribute; arguments as in
6224    struct attribute_spec.handler.  */
6225
6226 static tree
6227 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6228                         int ARG_UNUSED (flags), bool *no_add_attrs)
6229 {
6230   tree type = TREE_TYPE (*node);
6231
6232   /* See FIXME comment on noreturn in c_common_attribute_table.  */
6233   if (TREE_CODE (*node) == FUNCTION_DECL)
6234     TREE_READONLY (*node) = 1;
6235   else if (TREE_CODE (type) == POINTER_TYPE
6236            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6237     TREE_TYPE (*node)
6238       = build_pointer_type
6239         (build_type_variant (TREE_TYPE (type), 1,
6240                              TREE_THIS_VOLATILE (TREE_TYPE (type))));
6241   else
6242     {
6243       warning (OPT_Wattributes, "%qE attribute ignored", name);
6244       *no_add_attrs = true;
6245     }
6246
6247   return NULL_TREE;
6248 }
6249
6250 /* Handle a "transparent_union" attribute; arguments as in
6251    struct attribute_spec.handler.  */
6252
6253 static tree
6254 handle_transparent_union_attribute (tree *node, tree name,
6255                                     tree ARG_UNUSED (args), int flags,
6256                                     bool *no_add_attrs)
6257 {
6258   tree type;
6259
6260   *no_add_attrs = true;
6261
6262   if (TREE_CODE (*node) == TYPE_DECL)
6263     node = &TREE_TYPE (*node);
6264   type = *node;
6265
6266   if (TREE_CODE (type) == UNION_TYPE)
6267     {
6268       /* When IN_PLACE is set, leave the check for FIELDS and MODE to
6269          the code in finish_struct.  */
6270       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6271         {
6272           if (TYPE_FIELDS (type) == NULL_TREE
6273               || c_dialect_cxx ()
6274               || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type)))
6275             goto ignored;
6276
6277           /* A type variant isn't good enough, since we don't a cast
6278              to such a type removed as a no-op.  */
6279           *node = type = build_duplicate_type (type);
6280         }
6281
6282       TYPE_TRANSPARENT_AGGR (type) = 1;
6283       return NULL_TREE;
6284     }
6285
6286  ignored:
6287   warning (OPT_Wattributes, "%qE attribute ignored", name);
6288   return NULL_TREE;
6289 }
6290
6291 /* Subroutine of handle_{con,de}structor_attribute.  Evaluate ARGS to
6292    get the requested priority for a constructor or destructor,
6293    possibly issuing diagnostics for invalid or reserved
6294    priorities.  */
6295
6296 static priority_type
6297 get_priority (tree args, bool is_destructor)
6298 {
6299   HOST_WIDE_INT pri;
6300   tree arg;
6301
6302   if (!args)
6303     return DEFAULT_INIT_PRIORITY;
6304
6305   if (!SUPPORTS_INIT_PRIORITY)
6306     {
6307       if (is_destructor)
6308         error ("destructor priorities are not supported");
6309       else
6310         error ("constructor priorities are not supported");
6311       return DEFAULT_INIT_PRIORITY;
6312     }
6313
6314   arg = TREE_VALUE (args);
6315   if (!host_integerp (arg, /*pos=*/0)
6316       || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6317     goto invalid;
6318
6319   pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0);
6320   if (pri < 0 || pri > MAX_INIT_PRIORITY)
6321     goto invalid;
6322
6323   if (pri <= MAX_RESERVED_INIT_PRIORITY)
6324     {
6325       if (is_destructor)
6326         warning (0,
6327                  "destructor priorities from 0 to %d are reserved "
6328                  "for the implementation",
6329                  MAX_RESERVED_INIT_PRIORITY);
6330       else
6331         warning (0,
6332                  "constructor priorities from 0 to %d are reserved "
6333                  "for the implementation",
6334                  MAX_RESERVED_INIT_PRIORITY);
6335     }
6336   return pri;
6337
6338  invalid:
6339   if (is_destructor)
6340     error ("destructor priorities must be integers from 0 to %d inclusive",
6341            MAX_INIT_PRIORITY);
6342   else
6343     error ("constructor priorities must be integers from 0 to %d inclusive",
6344            MAX_INIT_PRIORITY);
6345   return DEFAULT_INIT_PRIORITY;
6346 }
6347
6348 /* Handle a "constructor" attribute; arguments as in
6349    struct attribute_spec.handler.  */
6350
6351 static tree
6352 handle_constructor_attribute (tree *node, tree name, tree args,
6353                               int ARG_UNUSED (flags),
6354                               bool *no_add_attrs)
6355 {
6356   tree decl = *node;
6357   tree type = TREE_TYPE (decl);
6358
6359   if (TREE_CODE (decl) == FUNCTION_DECL
6360       && TREE_CODE (type) == FUNCTION_TYPE
6361       && decl_function_context (decl) == 0)
6362     {
6363       priority_type priority;
6364       DECL_STATIC_CONSTRUCTOR (decl) = 1;
6365       priority = get_priority (args, /*is_destructor=*/false);
6366       SET_DECL_INIT_PRIORITY (decl, priority);
6367       TREE_USED (decl) = 1;
6368     }
6369   else
6370     {
6371       warning (OPT_Wattributes, "%qE attribute ignored", name);
6372       *no_add_attrs = true;
6373     }
6374
6375   return NULL_TREE;
6376 }
6377
6378 /* Handle a "destructor" attribute; arguments as in
6379    struct attribute_spec.handler.  */
6380
6381 static tree
6382 handle_destructor_attribute (tree *node, tree name, tree args,
6383                              int ARG_UNUSED (flags),
6384                              bool *no_add_attrs)
6385 {
6386   tree decl = *node;
6387   tree type = TREE_TYPE (decl);
6388
6389   if (TREE_CODE (decl) == FUNCTION_DECL
6390       && TREE_CODE (type) == FUNCTION_TYPE
6391       && decl_function_context (decl) == 0)
6392     {
6393       priority_type priority;
6394       DECL_STATIC_DESTRUCTOR (decl) = 1;
6395       priority = get_priority (args, /*is_destructor=*/true);
6396       SET_DECL_FINI_PRIORITY (decl, priority);
6397       TREE_USED (decl) = 1;
6398     }
6399   else
6400     {
6401       warning (OPT_Wattributes, "%qE attribute ignored", name);
6402       *no_add_attrs = true;
6403     }
6404
6405   return NULL_TREE;
6406 }
6407
6408 /* Nonzero if the mode is a valid vector mode for this architecture.
6409    This returns nonzero even if there is no hardware support for the
6410    vector mode, but we can emulate with narrower modes.  */
6411
6412 static int
6413 vector_mode_valid_p (enum machine_mode mode)
6414 {
6415   enum mode_class mclass = GET_MODE_CLASS (mode);
6416   enum machine_mode innermode;
6417
6418   /* Doh!  What's going on?  */
6419   if (mclass != MODE_VECTOR_INT
6420       && mclass != MODE_VECTOR_FLOAT
6421       && mclass != MODE_VECTOR_FRACT
6422       && mclass != MODE_VECTOR_UFRACT
6423       && mclass != MODE_VECTOR_ACCUM
6424       && mclass != MODE_VECTOR_UACCUM)
6425     return 0;
6426
6427   /* Hardware support.  Woo hoo!  */
6428   if (targetm.vector_mode_supported_p (mode))
6429     return 1;
6430
6431   innermode = GET_MODE_INNER (mode);
6432
6433   /* We should probably return 1 if requesting V4DI and we have no DI,
6434      but we have V2DI, but this is probably very unlikely.  */
6435
6436   /* If we have support for the inner mode, we can safely emulate it.
6437      We may not have V2DI, but me can emulate with a pair of DIs.  */
6438   return targetm.scalar_mode_supported_p (innermode);
6439 }
6440
6441
6442 /* Handle a "mode" attribute; arguments as in
6443    struct attribute_spec.handler.  */
6444
6445 static tree
6446 handle_mode_attribute (tree *node, tree name, tree args,
6447                        int ARG_UNUSED (flags), bool *no_add_attrs)
6448 {
6449   tree type = *node;
6450   tree ident = TREE_VALUE (args);
6451
6452   *no_add_attrs = true;
6453
6454   if (TREE_CODE (ident) != IDENTIFIER_NODE)
6455     warning (OPT_Wattributes, "%qE attribute ignored", name);
6456   else
6457     {
6458       int j;
6459       const char *p = IDENTIFIER_POINTER (ident);
6460       int len = strlen (p);
6461       enum machine_mode mode = VOIDmode;
6462       tree typefm;
6463       bool valid_mode;
6464
6465       if (len > 4 && p[0] == '_' && p[1] == '_'
6466           && p[len - 1] == '_' && p[len - 2] == '_')
6467         {
6468           char *newp = (char *) alloca (len - 1);
6469
6470           strcpy (newp, &p[2]);
6471           newp[len - 4] = '\0';
6472           p = newp;
6473         }
6474
6475       /* Change this type to have a type with the specified mode.
6476          First check for the special modes.  */
6477       if (!strcmp (p, "byte"))
6478         mode = byte_mode;
6479       else if (!strcmp (p, "word"))
6480         mode = word_mode;
6481       else if (!strcmp (p, "pointer"))
6482         mode = ptr_mode;
6483       else if (!strcmp (p, "libgcc_cmp_return"))
6484         mode = targetm.libgcc_cmp_return_mode ();
6485       else if (!strcmp (p, "libgcc_shift_count"))
6486         mode = targetm.libgcc_shift_count_mode ();
6487       else if (!strcmp (p, "unwind_word"))
6488         mode = targetm.unwind_word_mode ();
6489       else
6490         for (j = 0; j < NUM_MACHINE_MODES; j++)
6491           if (!strcmp (p, GET_MODE_NAME (j)))
6492             {
6493               mode = (enum machine_mode) j;
6494               break;
6495             }
6496
6497       if (mode == VOIDmode)
6498         {
6499           error ("unknown machine mode %qE", ident);
6500           return NULL_TREE;
6501         }
6502
6503       valid_mode = false;
6504       switch (GET_MODE_CLASS (mode))
6505         {
6506         case MODE_INT:
6507         case MODE_PARTIAL_INT:
6508         case MODE_FLOAT:
6509         case MODE_DECIMAL_FLOAT:
6510         case MODE_FRACT:
6511         case MODE_UFRACT:
6512         case MODE_ACCUM:
6513         case MODE_UACCUM:
6514           valid_mode = targetm.scalar_mode_supported_p (mode);
6515           break;
6516
6517         case MODE_COMPLEX_INT:
6518         case MODE_COMPLEX_FLOAT:
6519           valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
6520           break;
6521
6522         case MODE_VECTOR_INT:
6523         case MODE_VECTOR_FLOAT:
6524         case MODE_VECTOR_FRACT:
6525         case MODE_VECTOR_UFRACT:
6526         case MODE_VECTOR_ACCUM:
6527         case MODE_VECTOR_UACCUM:
6528           warning (OPT_Wattributes, "specifying vector types with "
6529                    "__attribute__ ((mode)) is deprecated");
6530           warning (OPT_Wattributes,
6531                    "use __attribute__ ((vector_size)) instead");
6532           valid_mode = vector_mode_valid_p (mode);
6533           break;
6534
6535         default:
6536           break;
6537         }
6538       if (!valid_mode)
6539         {
6540           error ("unable to emulate %qs", p);
6541           return NULL_TREE;
6542         }
6543
6544       if (POINTER_TYPE_P (type))
6545         {
6546           addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
6547           tree (*fn)(tree, enum machine_mode, bool);
6548
6549           if (!targetm.addr_space.valid_pointer_mode (mode, as))
6550             {
6551               error ("invalid pointer mode %qs", p);
6552               return NULL_TREE;
6553             }
6554
6555           if (TREE_CODE (type) == POINTER_TYPE)
6556             fn = build_pointer_type_for_mode;
6557           else
6558             fn = build_reference_type_for_mode;
6559           typefm = fn (TREE_TYPE (type), mode, false);
6560         }
6561       else
6562         {
6563           /* For fixed-point modes, we need to test if the signness of type
6564              and the machine mode are consistent.  */
6565           if (ALL_FIXED_POINT_MODE_P (mode)
6566               && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
6567             {
6568               error ("signedness of type and machine mode %qs don%'t match", p);
6569               return NULL_TREE;
6570             }
6571           /* For fixed-point modes, we need to pass saturating info.  */
6572           typefm = lang_hooks.types.type_for_mode (mode,
6573                         ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
6574                                                       : TYPE_UNSIGNED (type));
6575         }
6576
6577       if (typefm == NULL_TREE)
6578         {
6579           error ("no data type for mode %qs", p);
6580           return NULL_TREE;
6581         }
6582       else if (TREE_CODE (type) == ENUMERAL_TYPE)
6583         {
6584           /* For enumeral types, copy the precision from the integer
6585              type returned above.  If not an INTEGER_TYPE, we can't use
6586              this mode for this type.  */
6587           if (TREE_CODE (typefm) != INTEGER_TYPE)
6588             {
6589               error ("cannot use mode %qs for enumeral types", p);
6590               return NULL_TREE;
6591             }
6592
6593           if (flags & ATTR_FLAG_TYPE_IN_PLACE)
6594             {
6595               TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
6596               typefm = type;
6597             }
6598           else
6599             {
6600               /* We cannot build a type variant, as there's code that assumes
6601                  that TYPE_MAIN_VARIANT has the same mode.  This includes the
6602                  debug generators.  Instead, create a subrange type.  This
6603                  results in all of the enumeral values being emitted only once
6604                  in the original, and the subtype gets them by reference.  */
6605               if (TYPE_UNSIGNED (type))
6606                 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
6607               else
6608                 typefm = make_signed_type (TYPE_PRECISION (typefm));
6609               TREE_TYPE (typefm) = type;
6610             }
6611         }
6612       else if (VECTOR_MODE_P (mode)
6613                ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
6614                : TREE_CODE (type) != TREE_CODE (typefm))
6615         {
6616           error ("mode %qs applied to inappropriate type", p);
6617           return NULL_TREE;
6618         }
6619
6620       *node = typefm;
6621     }
6622
6623   return NULL_TREE;
6624 }
6625
6626 /* Handle a "section" attribute; arguments as in
6627    struct attribute_spec.handler.  */
6628
6629 static tree
6630 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6631                           int ARG_UNUSED (flags), bool *no_add_attrs)
6632 {
6633   tree decl = *node;
6634
6635   if (targetm_common.have_named_sections)
6636     {
6637       user_defined_section_attribute = true;
6638
6639       if ((TREE_CODE (decl) == FUNCTION_DECL
6640            || TREE_CODE (decl) == VAR_DECL)
6641           && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6642         {
6643           if (TREE_CODE (decl) == VAR_DECL
6644               && current_function_decl != NULL_TREE
6645               && !TREE_STATIC (decl))
6646             {
6647               error_at (DECL_SOURCE_LOCATION (decl),
6648                         "section attribute cannot be specified for "
6649                         "local variables");
6650               *no_add_attrs = true;
6651             }
6652
6653           /* The decl may have already been given a section attribute
6654              from a previous declaration.  Ensure they match.  */
6655           else if (DECL_SECTION_NAME (decl) != NULL_TREE
6656                    && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
6657                               TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
6658             {
6659               error ("section of %q+D conflicts with previous declaration",
6660                      *node);
6661               *no_add_attrs = true;
6662             }
6663           else if (TREE_CODE (decl) == VAR_DECL
6664                    && !targetm.have_tls && targetm.emutls.tmpl_section
6665                    && DECL_THREAD_LOCAL_P (decl))
6666             {
6667               error ("section of %q+D cannot be overridden", *node);
6668               *no_add_attrs = true;
6669             }
6670           else
6671             DECL_SECTION_NAME (decl) = TREE_VALUE (args);
6672         }
6673       else
6674         {
6675           error ("section attribute not allowed for %q+D", *node);
6676           *no_add_attrs = true;
6677         }
6678     }
6679   else
6680     {
6681       error_at (DECL_SOURCE_LOCATION (*node),
6682                 "section attributes are not supported for this target");
6683       *no_add_attrs = true;
6684     }
6685
6686   return NULL_TREE;
6687 }
6688
6689 /* Handle a "aligned" attribute; arguments as in
6690    struct attribute_spec.handler.  */
6691
6692 static tree
6693 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6694                           int flags, bool *no_add_attrs)
6695 {
6696   tree decl = NULL_TREE;
6697   tree *type = NULL;
6698   int is_type = 0;
6699   tree align_expr = (args ? TREE_VALUE (args)
6700                      : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
6701   int i;
6702
6703   if (DECL_P (*node))
6704     {
6705       decl = *node;
6706       type = &TREE_TYPE (decl);
6707       is_type = TREE_CODE (*node) == TYPE_DECL;
6708     }
6709   else if (TYPE_P (*node))
6710     type = node, is_type = 1;
6711
6712   if (TREE_CODE (align_expr) != INTEGER_CST)
6713     {
6714       error ("requested alignment is not a constant");
6715       *no_add_attrs = true;
6716     }
6717   else if ((i = tree_log2 (align_expr)) == -1)
6718     {
6719       error ("requested alignment is not a power of 2");
6720       *no_add_attrs = true;
6721     }
6722   else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
6723     {
6724       error ("requested alignment is too large");
6725       *no_add_attrs = true;
6726     }
6727   else if (is_type)
6728     {
6729       if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6730         /* OK, modify the type in place.  */;
6731       /* If we have a TYPE_DECL, then copy the type, so that we
6732          don't accidentally modify a builtin type.  See pushdecl.  */
6733       else if (decl && TREE_TYPE (decl) != error_mark_node
6734                && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
6735         {
6736           tree tt = TREE_TYPE (decl);
6737           *type = build_variant_type_copy (*type);
6738           DECL_ORIGINAL_TYPE (decl) = tt;
6739           TYPE_NAME (*type) = decl;
6740           TREE_USED (*type) = TREE_USED (decl);
6741           TREE_TYPE (decl) = *type;
6742         }
6743       else
6744         *type = build_variant_type_copy (*type);
6745
6746       TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
6747       TYPE_USER_ALIGN (*type) = 1;
6748     }
6749   else if (! VAR_OR_FUNCTION_DECL_P (decl)
6750            && TREE_CODE (decl) != FIELD_DECL)
6751     {
6752       error ("alignment may not be specified for %q+D", decl);
6753       *no_add_attrs = true;
6754     }
6755   else if (TREE_CODE (decl) == FUNCTION_DECL
6756            && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
6757     {
6758       if (DECL_USER_ALIGN (decl))
6759         error ("alignment for %q+D was previously specified as %d "
6760                "and may not be decreased", decl,
6761                DECL_ALIGN (decl) / BITS_PER_UNIT);
6762       else
6763         error ("alignment for %q+D must be at least %d", decl,
6764                DECL_ALIGN (decl) / BITS_PER_UNIT);
6765       *no_add_attrs = true;
6766     }
6767   else
6768     {
6769       DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
6770       DECL_USER_ALIGN (decl) = 1;
6771     }
6772
6773   return NULL_TREE;
6774 }
6775
6776 /* Handle a "weak" attribute; arguments as in
6777    struct attribute_spec.handler.  */
6778
6779 static tree
6780 handle_weak_attribute (tree *node, tree name,
6781                        tree ARG_UNUSED (args),
6782                        int ARG_UNUSED (flags),
6783                        bool * ARG_UNUSED (no_add_attrs))
6784 {
6785   if (TREE_CODE (*node) == FUNCTION_DECL
6786       && DECL_DECLARED_INLINE_P (*node))
6787     {
6788       warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6789       *no_add_attrs = true;
6790     }
6791   else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6792     {
6793       error ("indirect function %q+D cannot be declared weak", *node);
6794       *no_add_attrs = true;
6795       return NULL_TREE;
6796     }
6797   else if (TREE_CODE (*node) == FUNCTION_DECL
6798            || TREE_CODE (*node) == VAR_DECL)
6799     declare_weak (*node);
6800   else
6801     warning (OPT_Wattributes, "%qE attribute ignored", name);
6802
6803   return NULL_TREE;
6804 }
6805
6806 /* Handle an "alias" or "ifunc" attribute; arguments as in
6807    struct attribute_spec.handler, except that IS_ALIAS tells us
6808    whether this is an alias as opposed to ifunc attribute.  */
6809
6810 static tree
6811 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
6812                               bool *no_add_attrs)
6813 {
6814   tree decl = *node;
6815
6816   if (TREE_CODE (decl) != FUNCTION_DECL
6817       && (!is_alias || TREE_CODE (decl) != VAR_DECL))
6818     {
6819       warning (OPT_Wattributes, "%qE attribute ignored", name);
6820       *no_add_attrs = true;
6821     }
6822   else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
6823       || (TREE_CODE (decl) != FUNCTION_DECL
6824           && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
6825       /* A static variable declaration is always a tentative definition,
6826          but the alias is a non-tentative definition which overrides.  */
6827       || (TREE_CODE (decl) != FUNCTION_DECL
6828           && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
6829     {
6830       error ("%q+D defined both normally and as %qE attribute", decl, name);
6831       *no_add_attrs = true;
6832       return NULL_TREE;
6833     }
6834   else if (!is_alias
6835            && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl)) 
6836                || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
6837     {
6838       error ("weak %q+D cannot be defined %qE", decl, name);
6839       *no_add_attrs = true;
6840       return NULL_TREE;
6841     }                    
6842
6843   /* Note that the very first time we process a nested declaration,
6844      decl_function_context will not be set.  Indeed, *would* never
6845      be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
6846      we do below.  After such frobbery, pushdecl would set the context.
6847      In any case, this is never what we want.  */
6848   else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
6849     {
6850       tree id;
6851
6852       id = TREE_VALUE (args);
6853       if (TREE_CODE (id) != STRING_CST)
6854         {
6855           error ("attribute %qE argument not a string", name);
6856           *no_add_attrs = true;
6857           return NULL_TREE;
6858         }
6859       id = get_identifier (TREE_STRING_POINTER (id));
6860       /* This counts as a use of the object pointed to.  */
6861       TREE_USED (id) = 1;
6862
6863       if (TREE_CODE (decl) == FUNCTION_DECL)
6864         DECL_INITIAL (decl) = error_mark_node;
6865       else
6866         {
6867           if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6868             DECL_EXTERNAL (decl) = 1;
6869           else
6870             DECL_EXTERNAL (decl) = 0;
6871           TREE_STATIC (decl) = 1;
6872         }
6873
6874       if (!is_alias)
6875         /* ifuncs are also aliases, so set that attribute too. */
6876         DECL_ATTRIBUTES (decl)
6877           = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
6878     }
6879   else
6880     {
6881       warning (OPT_Wattributes, "%qE attribute ignored", name);
6882       *no_add_attrs = true;
6883     }
6884
6885   return NULL_TREE;
6886 }
6887
6888 /* Handle an "alias" or "ifunc" attribute; arguments as in
6889    struct attribute_spec.handler.  */
6890
6891 static tree
6892 handle_ifunc_attribute (tree *node, tree name, tree args,
6893                         int ARG_UNUSED (flags), bool *no_add_attrs)
6894 {
6895   return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
6896 }
6897
6898 /* Handle an "alias" or "ifunc" attribute; arguments as in
6899    struct attribute_spec.handler.  */
6900
6901 static tree
6902 handle_alias_attribute (tree *node, tree name, tree args,
6903                         int ARG_UNUSED (flags), bool *no_add_attrs)
6904 {
6905   return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
6906 }
6907
6908 /* Handle a "weakref" attribute; arguments as in struct
6909    attribute_spec.handler.  */
6910
6911 static tree
6912 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6913                           int flags, bool *no_add_attrs)
6914 {
6915   tree attr = NULL_TREE;
6916
6917   /* We must ignore the attribute when it is associated with
6918      local-scoped decls, since attribute alias is ignored and many
6919      such symbols do not even have a DECL_WEAK field.  */
6920   if (decl_function_context (*node)
6921       || current_function_decl
6922       || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
6923     {
6924       warning (OPT_Wattributes, "%qE attribute ignored", name);
6925       *no_add_attrs = true;
6926       return NULL_TREE;
6927     }
6928
6929   if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6930     {
6931       error ("indirect function %q+D cannot be declared weakref", *node);
6932       *no_add_attrs = true;
6933       return NULL_TREE;
6934     }
6935
6936   /* The idea here is that `weakref("name")' mutates into `weakref,
6937      alias("name")', and weakref without arguments, in turn,
6938      implicitly adds weak. */
6939
6940   if (args)
6941     {
6942       attr = tree_cons (get_identifier ("alias"), args, attr);
6943       attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
6944
6945       *no_add_attrs = true;
6946
6947       decl_attributes (node, attr, flags);
6948     }
6949   else
6950     {
6951       if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
6952         error_at (DECL_SOURCE_LOCATION (*node),
6953                   "weakref attribute must appear before alias attribute");
6954
6955       /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
6956          and that isn't supported; and because it wants to add it to
6957          the list of weak decls, which isn't helpful.  */
6958       DECL_WEAK (*node) = 1;
6959     }
6960
6961   return NULL_TREE;
6962 }
6963
6964 /* Handle an "visibility" attribute; arguments as in
6965    struct attribute_spec.handler.  */
6966
6967 static tree
6968 handle_visibility_attribute (tree *node, tree name, tree args,
6969                              int ARG_UNUSED (flags),
6970                              bool *ARG_UNUSED (no_add_attrs))
6971 {
6972   tree decl = *node;
6973   tree id = TREE_VALUE (args);
6974   enum symbol_visibility vis;
6975
6976   if (TYPE_P (*node))
6977     {
6978       if (TREE_CODE (*node) == ENUMERAL_TYPE)
6979         /* OK */;
6980       else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
6981         {
6982           warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
6983                    name);
6984           return NULL_TREE;
6985         }
6986       else if (TYPE_FIELDS (*node))
6987         {
6988           error ("%qE attribute ignored because %qT is already defined",
6989                  name, *node);
6990           return NULL_TREE;
6991         }
6992     }
6993   else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
6994     {
6995       warning (OPT_Wattributes, "%qE attribute ignored", name);
6996       return NULL_TREE;
6997     }
6998
6999   if (TREE_CODE (id) != STRING_CST)
7000     {
7001       error ("visibility argument not a string");
7002       return NULL_TREE;
7003     }
7004
7005   /*  If this is a type, set the visibility on the type decl.  */
7006   if (TYPE_P (decl))
7007     {
7008       decl = TYPE_NAME (decl);
7009       if (!decl)
7010         return NULL_TREE;
7011       if (TREE_CODE (decl) == IDENTIFIER_NODE)
7012         {
7013            warning (OPT_Wattributes, "%qE attribute ignored on types",
7014                     name);
7015            return NULL_TREE;
7016         }
7017     }
7018
7019   if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7020     vis = VISIBILITY_DEFAULT;
7021   else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7022     vis = VISIBILITY_INTERNAL;
7023   else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7024     vis = VISIBILITY_HIDDEN;
7025   else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7026     vis = VISIBILITY_PROTECTED;
7027   else
7028     {
7029       error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7030       vis = VISIBILITY_DEFAULT;
7031     }
7032
7033   if (DECL_VISIBILITY_SPECIFIED (decl)
7034       && vis != DECL_VISIBILITY (decl))
7035     {
7036       tree attributes = (TYPE_P (*node)
7037                          ? TYPE_ATTRIBUTES (*node)
7038                          : DECL_ATTRIBUTES (decl));
7039       if (lookup_attribute ("visibility", attributes))
7040         error ("%qD redeclared with different visibility", decl);
7041       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7042                && lookup_attribute ("dllimport", attributes))
7043         error ("%qD was declared %qs which implies default visibility",
7044                decl, "dllimport");
7045       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7046                && lookup_attribute ("dllexport", attributes))
7047         error ("%qD was declared %qs which implies default visibility",
7048                decl, "dllexport");
7049     }
7050
7051   DECL_VISIBILITY (decl) = vis;
7052   DECL_VISIBILITY_SPECIFIED (decl) = 1;
7053
7054   /* Go ahead and attach the attribute to the node as well.  This is needed
7055      so we can determine whether we have VISIBILITY_DEFAULT because the
7056      visibility was not specified, or because it was explicitly overridden
7057      from the containing scope.  */
7058
7059   return NULL_TREE;
7060 }
7061
7062 /* Determine the ELF symbol visibility for DECL, which is either a
7063    variable or a function.  It is an error to use this function if a
7064    definition of DECL is not available in this translation unit.
7065    Returns true if the final visibility has been determined by this
7066    function; false if the caller is free to make additional
7067    modifications.  */
7068
7069 bool
7070 c_determine_visibility (tree decl)
7071 {
7072   gcc_assert (TREE_CODE (decl) == VAR_DECL
7073               || TREE_CODE (decl) == FUNCTION_DECL);
7074
7075   /* If the user explicitly specified the visibility with an
7076      attribute, honor that.  DECL_VISIBILITY will have been set during
7077      the processing of the attribute.  We check for an explicit
7078      attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7079      to distinguish the use of an attribute from the use of a "#pragma
7080      GCC visibility push(...)"; in the latter case we still want other
7081      considerations to be able to overrule the #pragma.  */
7082   if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7083       || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7084           && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7085               || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7086     return true;
7087
7088   /* Set default visibility to whatever the user supplied with
7089      visibility_specified depending on #pragma GCC visibility.  */
7090   if (!DECL_VISIBILITY_SPECIFIED (decl))
7091     {
7092       if (visibility_options.inpragma
7093           || DECL_VISIBILITY (decl) != default_visibility)
7094         {
7095           DECL_VISIBILITY (decl) = default_visibility;
7096           DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7097           /* If visibility changed and DECL already has DECL_RTL, ensure
7098              symbol flags are updated.  */
7099           if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7100                || TREE_CODE (decl) == FUNCTION_DECL)
7101               && DECL_RTL_SET_P (decl))
7102             make_decl_rtl (decl);
7103         }
7104     }
7105   return false;
7106 }
7107
7108 /* Handle an "tls_model" attribute; arguments as in
7109    struct attribute_spec.handler.  */
7110
7111 static tree
7112 handle_tls_model_attribute (tree *node, tree name, tree args,
7113                             int ARG_UNUSED (flags), bool *no_add_attrs)
7114 {
7115   tree id;
7116   tree decl = *node;
7117   enum tls_model kind;
7118
7119   *no_add_attrs = true;
7120
7121   if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7122     {
7123       warning (OPT_Wattributes, "%qE attribute ignored", name);
7124       return NULL_TREE;
7125     }
7126
7127   kind = DECL_TLS_MODEL (decl);
7128   id = TREE_VALUE (args);
7129   if (TREE_CODE (id) != STRING_CST)
7130     {
7131       error ("tls_model argument not a string");
7132       return NULL_TREE;
7133     }
7134
7135   if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7136     kind = TLS_MODEL_LOCAL_EXEC;
7137   else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7138     kind = TLS_MODEL_INITIAL_EXEC;
7139   else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7140     kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7141   else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7142     kind = TLS_MODEL_GLOBAL_DYNAMIC;
7143   else
7144     error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7145
7146   DECL_TLS_MODEL (decl) = kind;
7147   return NULL_TREE;
7148 }
7149
7150 /* Handle a "no_instrument_function" attribute; arguments as in
7151    struct attribute_spec.handler.  */
7152
7153 static tree
7154 handle_no_instrument_function_attribute (tree *node, tree name,
7155                                          tree ARG_UNUSED (args),
7156                                          int ARG_UNUSED (flags),
7157                                          bool *no_add_attrs)
7158 {
7159   tree decl = *node;
7160
7161   if (TREE_CODE (decl) != FUNCTION_DECL)
7162     {
7163       error_at (DECL_SOURCE_LOCATION (decl),
7164                 "%qE attribute applies only to functions", name);
7165       *no_add_attrs = true;
7166     }
7167   else if (DECL_INITIAL (decl))
7168     {
7169       error_at (DECL_SOURCE_LOCATION (decl),
7170                 "can%'t set %qE attribute after definition", name);
7171       *no_add_attrs = true;
7172     }
7173   else
7174     DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7175
7176   return NULL_TREE;
7177 }
7178
7179 /* Handle a "malloc" attribute; arguments as in
7180    struct attribute_spec.handler.  */
7181
7182 static tree
7183 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7184                          int ARG_UNUSED (flags), bool *no_add_attrs)
7185 {
7186   if (TREE_CODE (*node) == FUNCTION_DECL
7187       && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7188     DECL_IS_MALLOC (*node) = 1;
7189   else
7190     {
7191       warning (OPT_Wattributes, "%qE attribute ignored", name);
7192       *no_add_attrs = true;
7193     }
7194
7195   return NULL_TREE;
7196 }
7197
7198 /* Handle a "alloc_size" attribute; arguments as in
7199    struct attribute_spec.handler.  */
7200
7201 static tree
7202 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7203                              int ARG_UNUSED (flags), bool *no_add_attrs)
7204 {
7205   unsigned arg_count = type_num_arguments (*node);
7206   for (; args; args = TREE_CHAIN (args))
7207     {
7208       tree position = TREE_VALUE (args);
7209
7210       if (TREE_CODE (position) != INTEGER_CST
7211           || TREE_INT_CST_HIGH (position)
7212           || TREE_INT_CST_LOW (position) < 1
7213           || TREE_INT_CST_LOW (position) > arg_count )
7214         {
7215           warning (OPT_Wattributes,
7216                    "alloc_size parameter outside range");
7217           *no_add_attrs = true;
7218           return NULL_TREE;
7219         }
7220     }
7221   return NULL_TREE;
7222 }
7223
7224 /* Handle a "fn spec" attribute; arguments as in
7225    struct attribute_spec.handler.  */
7226
7227 static tree
7228 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7229                          tree args, int ARG_UNUSED (flags),
7230                          bool *no_add_attrs ATTRIBUTE_UNUSED)
7231 {
7232   gcc_assert (args
7233               && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7234               && !TREE_CHAIN (args));
7235   return NULL_TREE;
7236 }
7237
7238 /* Handle a "returns_twice" attribute; arguments as in
7239    struct attribute_spec.handler.  */
7240
7241 static tree
7242 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7243                          int ARG_UNUSED (flags), bool *no_add_attrs)
7244 {
7245   if (TREE_CODE (*node) == FUNCTION_DECL)
7246     DECL_IS_RETURNS_TWICE (*node) = 1;
7247   else
7248     {
7249       warning (OPT_Wattributes, "%qE attribute ignored", name);
7250       *no_add_attrs = true;
7251     }
7252
7253   return NULL_TREE;
7254 }
7255
7256 /* Handle a "no_limit_stack" attribute; arguments as in
7257    struct attribute_spec.handler.  */
7258
7259 static tree
7260 handle_no_limit_stack_attribute (tree *node, tree name,
7261                                  tree ARG_UNUSED (args),
7262                                  int ARG_UNUSED (flags),
7263                                  bool *no_add_attrs)
7264 {
7265   tree decl = *node;
7266
7267   if (TREE_CODE (decl) != FUNCTION_DECL)
7268     {
7269       error_at (DECL_SOURCE_LOCATION (decl),
7270              "%qE attribute applies only to functions", name);
7271       *no_add_attrs = true;
7272     }
7273   else if (DECL_INITIAL (decl))
7274     {
7275       error_at (DECL_SOURCE_LOCATION (decl),
7276                 "can%'t set %qE attribute after definition", name);
7277       *no_add_attrs = true;
7278     }
7279   else
7280     DECL_NO_LIMIT_STACK (decl) = 1;
7281
7282   return NULL_TREE;
7283 }
7284
7285 /* Handle a "pure" attribute; arguments as in
7286    struct attribute_spec.handler.  */
7287
7288 static tree
7289 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7290                        int ARG_UNUSED (flags), bool *no_add_attrs)
7291 {
7292   if (TREE_CODE (*node) == FUNCTION_DECL)
7293     DECL_PURE_P (*node) = 1;
7294   /* ??? TODO: Support types.  */
7295   else
7296     {
7297       warning (OPT_Wattributes, "%qE attribute ignored", name);
7298       *no_add_attrs = true;
7299     }
7300
7301   return NULL_TREE;
7302 }
7303
7304 /* Handle a "no vops" attribute; arguments as in
7305    struct attribute_spec.handler.  */
7306
7307 static tree
7308 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
7309                          tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7310                          bool *ARG_UNUSED (no_add_attrs))
7311 {
7312   gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
7313   DECL_IS_NOVOPS (*node) = 1;
7314   return NULL_TREE;
7315 }
7316
7317 /* Handle a "deprecated" attribute; arguments as in
7318    struct attribute_spec.handler.  */
7319
7320 static tree
7321 handle_deprecated_attribute (tree *node, tree name,
7322                              tree args, int flags,
7323                              bool *no_add_attrs)
7324 {
7325   tree type = NULL_TREE;
7326   int warn = 0;
7327   tree what = NULL_TREE;
7328
7329   if (!args)
7330     *no_add_attrs = true;
7331   else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7332     {
7333       error ("deprecated message is not a string");
7334       *no_add_attrs = true;
7335     }
7336
7337   if (DECL_P (*node))
7338     {
7339       tree decl = *node;
7340       type = TREE_TYPE (decl);
7341
7342       if (TREE_CODE (decl) == TYPE_DECL
7343           || TREE_CODE (decl) == PARM_DECL
7344           || TREE_CODE (decl) == VAR_DECL
7345           || TREE_CODE (decl) == FUNCTION_DECL
7346           || TREE_CODE (decl) == FIELD_DECL
7347           || objc_method_decl (TREE_CODE (decl)))
7348         TREE_DEPRECATED (decl) = 1;
7349       else
7350         warn = 1;
7351     }
7352   else if (TYPE_P (*node))
7353     {
7354       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7355         *node = build_variant_type_copy (*node);
7356       TREE_DEPRECATED (*node) = 1;
7357       type = *node;
7358     }
7359   else
7360     warn = 1;
7361
7362   if (warn)
7363     {
7364       *no_add_attrs = true;
7365       if (type && TYPE_NAME (type))
7366         {
7367           if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
7368             what = TYPE_NAME (*node);
7369           else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7370                    && DECL_NAME (TYPE_NAME (type)))
7371             what = DECL_NAME (TYPE_NAME (type));
7372         }
7373       if (what)
7374         warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
7375       else
7376         warning (OPT_Wattributes, "%qE attribute ignored", name);
7377     }
7378
7379   return NULL_TREE;
7380 }
7381
7382 /* Handle a "vector_size" attribute; arguments as in
7383    struct attribute_spec.handler.  */
7384
7385 static tree
7386 handle_vector_size_attribute (tree *node, tree name, tree args,
7387                               int ARG_UNUSED (flags),
7388                               bool *no_add_attrs)
7389 {
7390   unsigned HOST_WIDE_INT vecsize, nunits;
7391   enum machine_mode orig_mode;
7392   tree type = *node, new_type, size;
7393
7394   *no_add_attrs = true;
7395
7396   size = TREE_VALUE (args);
7397
7398   if (!host_integerp (size, 1))
7399     {
7400       warning (OPT_Wattributes, "%qE attribute ignored", name);
7401       return NULL_TREE;
7402     }
7403
7404   /* Get the vector size (in bytes).  */
7405   vecsize = tree_low_cst (size, 1);
7406
7407   /* We need to provide for vector pointers, vector arrays, and
7408      functions returning vectors.  For example:
7409
7410        __attribute__((vector_size(16))) short *foo;
7411
7412      In this case, the mode is SI, but the type being modified is
7413      HI, so we need to look further.  */
7414
7415   while (POINTER_TYPE_P (type)
7416          || TREE_CODE (type) == FUNCTION_TYPE
7417          || TREE_CODE (type) == METHOD_TYPE
7418          || TREE_CODE (type) == ARRAY_TYPE
7419          || TREE_CODE (type) == OFFSET_TYPE)
7420     type = TREE_TYPE (type);
7421
7422   /* Get the mode of the type being modified.  */
7423   orig_mode = TYPE_MODE (type);
7424
7425   if ((!INTEGRAL_TYPE_P (type)
7426        && !SCALAR_FLOAT_TYPE_P (type)
7427        && !FIXED_POINT_TYPE_P (type))
7428       || (!SCALAR_FLOAT_MODE_P (orig_mode)
7429           && GET_MODE_CLASS (orig_mode) != MODE_INT
7430           && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
7431       || !host_integerp (TYPE_SIZE_UNIT (type), 1)
7432       || TREE_CODE (type) == BOOLEAN_TYPE)
7433     {
7434       error ("invalid vector type for attribute %qE", name);
7435       return NULL_TREE;
7436     }
7437
7438   if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
7439     {
7440       error ("vector size not an integral multiple of component size");
7441       return NULL;
7442     }
7443
7444   if (vecsize == 0)
7445     {
7446       error ("zero vector size");
7447       return NULL;
7448     }
7449
7450   /* Calculate how many units fit in the vector.  */
7451   nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
7452   if (nunits & (nunits - 1))
7453     {
7454       error ("number of components of the vector not a power of two");
7455       return NULL_TREE;
7456     }
7457
7458   new_type = build_vector_type (type, nunits);
7459
7460   /* Build back pointers if needed.  */
7461   *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
7462
7463   return NULL_TREE;
7464 }
7465
7466 /* Handle the "nonnull" attribute.  */
7467 static tree
7468 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
7469                           tree args, int ARG_UNUSED (flags),
7470                           bool *no_add_attrs)
7471 {
7472   tree type = *node;
7473   unsigned HOST_WIDE_INT attr_arg_num;
7474
7475   /* If no arguments are specified, all pointer arguments should be
7476      non-null.  Verify a full prototype is given so that the arguments
7477      will have the correct types when we actually check them later.  */
7478   if (!args)
7479     {
7480       if (!prototype_p (type))
7481         {
7482           error ("nonnull attribute without arguments on a non-prototype");
7483           *no_add_attrs = true;
7484         }
7485       return NULL_TREE;
7486     }
7487
7488   /* Argument list specified.  Verify that each argument number references
7489      a pointer argument.  */
7490   for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
7491     {
7492       unsigned HOST_WIDE_INT arg_num = 0, ck_num;
7493
7494       if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
7495         {
7496           error ("nonnull argument has invalid operand number (argument %lu)",
7497                  (unsigned long) attr_arg_num);
7498           *no_add_attrs = true;
7499           return NULL_TREE;
7500         }
7501
7502       if (prototype_p (type))
7503         {
7504           function_args_iterator iter;
7505           tree argument;
7506
7507           function_args_iter_init (&iter, type);
7508           for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
7509             {
7510               argument = function_args_iter_cond (&iter);
7511               if (argument == NULL_TREE || ck_num == arg_num)
7512                 break;
7513             }
7514
7515           if (!argument
7516               || TREE_CODE (argument) == VOID_TYPE)
7517             {
7518               error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
7519                      (unsigned long) attr_arg_num, (unsigned long) arg_num);
7520               *no_add_attrs = true;
7521               return NULL_TREE;
7522             }
7523
7524           if (TREE_CODE (argument) != POINTER_TYPE)
7525             {
7526               error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
7527                    (unsigned long) attr_arg_num, (unsigned long) arg_num);
7528               *no_add_attrs = true;
7529               return NULL_TREE;
7530             }
7531         }
7532     }
7533
7534   return NULL_TREE;
7535 }
7536
7537 /* Check the argument list of a function call for null in argument slots
7538    that are marked as requiring a non-null pointer argument.  The NARGS
7539    arguments are passed in the array ARGARRAY.
7540 */
7541
7542 static void
7543 check_function_nonnull (tree attrs, int nargs, tree *argarray)
7544 {
7545   tree a, args;
7546   int i;
7547
7548   for (a = attrs; a; a = TREE_CHAIN (a))
7549     {
7550       if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
7551         {
7552           args = TREE_VALUE (a);
7553
7554           /* Walk the argument list.  If we encounter an argument number we
7555              should check for non-null, do it.  If the attribute has no args,
7556              then every pointer argument is checked (in which case the check
7557              for pointer type is done in check_nonnull_arg).  */
7558           for (i = 0; i < nargs; i++)
7559             {
7560               if (!args || nonnull_check_p (args, i + 1))
7561                 check_function_arguments_recurse (check_nonnull_arg, NULL,
7562                                                   argarray[i],
7563                                                   i + 1);
7564             }
7565         }
7566     }
7567 }
7568
7569 /* Check that the Nth argument of a function call (counting backwards
7570    from the end) is a (pointer)0.  The NARGS arguments are passed in the
7571    array ARGARRAY.  */
7572
7573 static void
7574 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
7575 {
7576   tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
7577
7578   if (attr)
7579     {
7580       int len = 0;
7581       int pos = 0;
7582       tree sentinel;
7583       function_args_iterator iter;
7584       tree t;
7585
7586       /* Skip over the named arguments.  */
7587       FOREACH_FUNCTION_ARGS (fntype, t, iter)
7588         {
7589           if (len == nargs)
7590             break;
7591           len++;
7592         }
7593
7594       if (TREE_VALUE (attr))
7595         {
7596           tree p = TREE_VALUE (TREE_VALUE (attr));
7597           pos = TREE_INT_CST_LOW (p);
7598         }
7599
7600       /* The sentinel must be one of the varargs, i.e.
7601          in position >= the number of fixed arguments.  */
7602       if ((nargs - 1 - pos) < len)
7603         {
7604           warning (OPT_Wformat,
7605                    "not enough variable arguments to fit a sentinel");
7606           return;
7607         }
7608
7609       /* Validate the sentinel.  */
7610       sentinel = argarray[nargs - 1 - pos];
7611       if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
7612            || !integer_zerop (sentinel))
7613           /* Although __null (in C++) is only an integer we allow it
7614              nevertheless, as we are guaranteed that it's exactly
7615              as wide as a pointer, and we don't want to force
7616              users to cast the NULL they have written there.
7617              We warn with -Wstrict-null-sentinel, though.  */
7618           && (warn_strict_null_sentinel || null_node != sentinel))
7619         warning (OPT_Wformat, "missing sentinel in function call");
7620     }
7621 }
7622
7623 /* Helper for check_function_nonnull; given a list of operands which
7624    must be non-null in ARGS, determine if operand PARAM_NUM should be
7625    checked.  */
7626
7627 static bool
7628 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
7629 {
7630   unsigned HOST_WIDE_INT arg_num = 0;
7631
7632   for (; args; args = TREE_CHAIN (args))
7633     {
7634       bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
7635
7636       gcc_assert (found);
7637
7638       if (arg_num == param_num)
7639         return true;
7640     }
7641   return false;
7642 }
7643
7644 /* Check that the function argument PARAM (which is operand number
7645    PARAM_NUM) is non-null.  This is called by check_function_nonnull
7646    via check_function_arguments_recurse.  */
7647
7648 static void
7649 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
7650                    unsigned HOST_WIDE_INT param_num)
7651 {
7652   /* Just skip checking the argument if it's not a pointer.  This can
7653      happen if the "nonnull" attribute was given without an operand
7654      list (which means to check every pointer argument).  */
7655
7656   if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
7657     return;
7658
7659   if (integer_zerop (param))
7660     warning (OPT_Wnonnull, "null argument where non-null required "
7661              "(argument %lu)", (unsigned long) param_num);
7662 }
7663
7664 /* Helper for nonnull attribute handling; fetch the operand number
7665    from the attribute argument list.  */
7666
7667 static bool
7668 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
7669 {
7670   /* Verify the arg number is a constant.  */
7671   if (TREE_CODE (arg_num_expr) != INTEGER_CST
7672       || TREE_INT_CST_HIGH (arg_num_expr) != 0)
7673     return false;
7674
7675   *valp = TREE_INT_CST_LOW (arg_num_expr);
7676   return true;
7677 }
7678
7679 /* Handle a "nothrow" attribute; arguments as in
7680    struct attribute_spec.handler.  */
7681
7682 static tree
7683 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7684                           int ARG_UNUSED (flags), bool *no_add_attrs)
7685 {
7686   if (TREE_CODE (*node) == FUNCTION_DECL)
7687     TREE_NOTHROW (*node) = 1;
7688   /* ??? TODO: Support types.  */
7689   else
7690     {
7691       warning (OPT_Wattributes, "%qE attribute ignored", name);
7692       *no_add_attrs = true;
7693     }
7694
7695   return NULL_TREE;
7696 }
7697
7698 /* Handle a "cleanup" attribute; arguments as in
7699    struct attribute_spec.handler.  */
7700
7701 static tree
7702 handle_cleanup_attribute (tree *node, tree name, tree args,
7703                           int ARG_UNUSED (flags), bool *no_add_attrs)
7704 {
7705   tree decl = *node;
7706   tree cleanup_id, cleanup_decl;
7707
7708   /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
7709      for global destructors in C++.  This requires infrastructure that
7710      we don't have generically at the moment.  It's also not a feature
7711      we'd be missing too much, since we do have attribute constructor.  */
7712   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
7713     {
7714       warning (OPT_Wattributes, "%qE attribute ignored", name);
7715       *no_add_attrs = true;
7716       return NULL_TREE;
7717     }
7718
7719   /* Verify that the argument is a function in scope.  */
7720   /* ??? We could support pointers to functions here as well, if
7721      that was considered desirable.  */
7722   cleanup_id = TREE_VALUE (args);
7723   if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
7724     {
7725       error ("cleanup argument not an identifier");
7726       *no_add_attrs = true;
7727       return NULL_TREE;
7728     }
7729   cleanup_decl = lookup_name (cleanup_id);
7730   if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
7731     {
7732       error ("cleanup argument not a function");
7733       *no_add_attrs = true;
7734       return NULL_TREE;
7735     }
7736
7737   /* That the function has proper type is checked with the
7738      eventual call to build_function_call.  */
7739
7740   return NULL_TREE;
7741 }
7742
7743 /* Handle a "warn_unused_result" attribute.  No special handling.  */
7744
7745 static tree
7746 handle_warn_unused_result_attribute (tree *node, tree name,
7747                                tree ARG_UNUSED (args),
7748                                int ARG_UNUSED (flags), bool *no_add_attrs)
7749 {
7750   /* Ignore the attribute for functions not returning any value.  */
7751   if (VOID_TYPE_P (TREE_TYPE (*node)))
7752     {
7753       warning (OPT_Wattributes, "%qE attribute ignored", name);
7754       *no_add_attrs = true;
7755     }
7756
7757   return NULL_TREE;
7758 }
7759
7760 /* Handle a "sentinel" attribute.  */
7761
7762 static tree
7763 handle_sentinel_attribute (tree *node, tree name, tree args,
7764                            int ARG_UNUSED (flags), bool *no_add_attrs)
7765 {
7766   if (!prototype_p (*node))
7767     {
7768       warning (OPT_Wattributes,
7769                "%qE attribute requires prototypes with named arguments", name);
7770       *no_add_attrs = true;
7771     }
7772   else
7773     {
7774       if (!stdarg_p (*node))
7775         {
7776           warning (OPT_Wattributes,
7777                    "%qE attribute only applies to variadic functions", name);
7778           *no_add_attrs = true;
7779         }
7780     }
7781
7782   if (args)
7783     {
7784       tree position = TREE_VALUE (args);
7785
7786       if (TREE_CODE (position) != INTEGER_CST)
7787         {
7788           warning (OPT_Wattributes,
7789                    "requested position is not an integer constant");
7790           *no_add_attrs = true;
7791         }
7792       else
7793         {
7794           if (tree_int_cst_lt (position, integer_zero_node))
7795             {
7796               warning (OPT_Wattributes,
7797                        "requested position is less than zero");
7798               *no_add_attrs = true;
7799             }
7800         }
7801     }
7802
7803   return NULL_TREE;
7804 }
7805
7806 /* Handle a "type_generic" attribute.  */
7807
7808 static tree
7809 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
7810                                tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7811                                bool * ARG_UNUSED (no_add_attrs))
7812 {
7813   /* Ensure we have a function type.  */
7814   gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
7815
7816   /* Ensure we have a variadic function.  */
7817   gcc_assert (!prototype_p (*node) || stdarg_p (*node));
7818
7819   return NULL_TREE;
7820 }
7821
7822 /* Handle a "target" attribute.  */
7823
7824 static tree
7825 handle_target_attribute (tree *node, tree name, tree args, int flags,
7826                          bool *no_add_attrs)
7827 {
7828   /* Ensure we have a function type.  */
7829   if (TREE_CODE (*node) != FUNCTION_DECL)
7830     {
7831       warning (OPT_Wattributes, "%qE attribute ignored", name);
7832       *no_add_attrs = true;
7833     }
7834   else if (! targetm.target_option.valid_attribute_p (*node, name, args,
7835                                                       flags))
7836     *no_add_attrs = true;
7837
7838   return NULL_TREE;
7839 }
7840
7841 /* Arguments being collected for optimization.  */
7842 typedef const char *const_char_p;               /* For DEF_VEC_P.  */
7843 DEF_VEC_P(const_char_p);
7844 DEF_VEC_ALLOC_P(const_char_p, gc);
7845 static GTY(()) VEC(const_char_p, gc) *optimize_args;
7846
7847
7848 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
7849    options in ARGS.  ATTR_P is true if this is for attribute(optimize), and
7850    false for #pragma GCC optimize.  */
7851
7852 bool
7853 parse_optimize_options (tree args, bool attr_p)
7854 {
7855   bool ret = true;
7856   unsigned opt_argc;
7857   unsigned i;
7858   int saved_flag_strict_aliasing;
7859   const char **opt_argv;
7860   struct cl_decoded_option *decoded_options;
7861   unsigned int decoded_options_count;
7862   tree ap;
7863
7864   /* Build up argv vector.  Just in case the string is stored away, use garbage
7865      collected strings.  */
7866   VEC_truncate (const_char_p, optimize_args, 0);
7867   VEC_safe_push (const_char_p, gc, optimize_args, NULL);
7868
7869   for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
7870     {
7871       tree value = TREE_VALUE (ap);
7872
7873       if (TREE_CODE (value) == INTEGER_CST)
7874         {
7875           char buffer[20];
7876           sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
7877           VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
7878         }
7879
7880       else if (TREE_CODE (value) == STRING_CST)
7881         {
7882           /* Split string into multiple substrings.  */
7883           size_t len = TREE_STRING_LENGTH (value);
7884           char *p = ASTRDUP (TREE_STRING_POINTER (value));
7885           char *end = p + len;
7886           char *comma;
7887           char *next_p = p;
7888
7889           while (next_p != NULL)
7890             {
7891               size_t len2;
7892               char *q, *r;
7893
7894               p = next_p;
7895               comma = strchr (p, ',');
7896               if (comma)
7897                 {
7898                   len2 = comma - p;
7899                   *comma = '\0';
7900                   next_p = comma+1;
7901                 }
7902               else
7903                 {
7904                   len2 = end - p;
7905                   next_p = NULL;
7906                 }
7907
7908               r = q = (char *) ggc_alloc_atomic (len2 + 3);
7909
7910               /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
7911                  options.  */
7912               if (*p == '-' && p[1] != 'O' && p[1] != 'f')
7913                 {
7914                   ret = false;
7915                   if (attr_p)
7916                     warning (OPT_Wattributes,
7917                              "bad option %s to optimize attribute", p);
7918                   else
7919                     warning (OPT_Wpragmas,
7920                              "bad option %s to pragma attribute", p);
7921                   continue;
7922                 }
7923
7924               if (*p != '-')
7925                 {
7926                   *r++ = '-';
7927
7928                   /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
7929                      itself is -Os, and any other switch begins with a -f.  */
7930                   if ((*p >= '0' && *p <= '9')
7931                       || (p[0] == 's' && p[1] == '\0'))
7932                     *r++ = 'O';
7933                   else if (*p != 'O')
7934                     *r++ = 'f';
7935                 }
7936
7937               memcpy (r, p, len2);
7938               r[len2] = '\0';
7939               VEC_safe_push (const_char_p, gc, optimize_args, q);
7940             }
7941
7942         }
7943     }
7944
7945   opt_argc = VEC_length (const_char_p, optimize_args);
7946   opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
7947
7948   for (i = 1; i < opt_argc; i++)
7949     opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
7950
7951   saved_flag_strict_aliasing = flag_strict_aliasing;
7952
7953   /* Now parse the options.  */
7954   decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
7955                                                 &decoded_options,
7956                                                 &decoded_options_count);
7957   decode_options (&global_options, &global_options_set,
7958                   decoded_options, decoded_options_count,
7959                   input_location, global_dc);
7960
7961   targetm.override_options_after_change();
7962
7963   /* Don't allow changing -fstrict-aliasing.  */
7964   flag_strict_aliasing = saved_flag_strict_aliasing;
7965
7966   VEC_truncate (const_char_p, optimize_args, 0);
7967   return ret;
7968 }
7969
7970 /* For handling "optimize" attribute. arguments as in
7971    struct attribute_spec.handler.  */
7972
7973 static tree
7974 handle_optimize_attribute (tree *node, tree name, tree args,
7975                            int ARG_UNUSED (flags), bool *no_add_attrs)
7976 {
7977   /* Ensure we have a function type.  */
7978   if (TREE_CODE (*node) != FUNCTION_DECL)
7979     {
7980       warning (OPT_Wattributes, "%qE attribute ignored", name);
7981       *no_add_attrs = true;
7982     }
7983   else
7984     {
7985       struct cl_optimization cur_opts;
7986       tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
7987
7988       /* Save current options.  */
7989       cl_optimization_save (&cur_opts, &global_options);
7990
7991       /* If we previously had some optimization options, use them as the
7992          default.  */
7993       if (old_opts)
7994         cl_optimization_restore (&global_options,
7995                                  TREE_OPTIMIZATION (old_opts));
7996
7997       /* Parse options, and update the vector.  */
7998       parse_optimize_options (args, true);
7999       DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8000         = build_optimization_node ();
8001
8002       /* Restore current options.  */
8003       cl_optimization_restore (&global_options, &cur_opts);
8004     }
8005
8006   return NULL_TREE;
8007 }
8008
8009 /* Handle a "no_split_stack" attribute.  */
8010
8011 static tree
8012 handle_no_split_stack_attribute (tree *node, tree name,
8013                                  tree ARG_UNUSED (args),
8014                                  int ARG_UNUSED (flags),
8015                                  bool *no_add_attrs)
8016 {
8017   tree decl = *node;
8018
8019   if (TREE_CODE (decl) != FUNCTION_DECL)
8020     {
8021       error_at (DECL_SOURCE_LOCATION (decl),
8022                 "%qE attribute applies only to functions", name);
8023       *no_add_attrs = true;
8024     }
8025   else if (DECL_INITIAL (decl))
8026     {
8027       error_at (DECL_SOURCE_LOCATION (decl),
8028                 "can%'t set %qE attribute after definition", name);
8029       *no_add_attrs = true;
8030     }
8031
8032   return NULL_TREE;
8033 }
8034 \f
8035 /* Check for valid arguments being passed to a function with FNTYPE.
8036    There are NARGS arguments in the array ARGARRAY.  */
8037 void
8038 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
8039 {
8040   /* Check for null being passed in a pointer argument that must be
8041      non-null.  We also need to do this if format checking is enabled.  */
8042
8043   if (warn_nonnull)
8044     check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8045
8046   /* Check for errors in format strings.  */
8047
8048   if (warn_format || warn_missing_format_attribute)
8049     check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8050
8051   if (warn_format)
8052     check_function_sentinel (fntype, nargs, argarray);
8053 }
8054
8055 /* Generic argument checking recursion routine.  PARAM is the argument to
8056    be checked.  PARAM_NUM is the number of the argument.  CALLBACK is invoked
8057    once the argument is resolved.  CTX is context for the callback.  */
8058 void
8059 check_function_arguments_recurse (void (*callback)
8060                                   (void *, tree, unsigned HOST_WIDE_INT),
8061                                   void *ctx, tree param,
8062                                   unsigned HOST_WIDE_INT param_num)
8063 {
8064   if (CONVERT_EXPR_P (param)
8065       && (TYPE_PRECISION (TREE_TYPE (param))
8066           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
8067     {
8068       /* Strip coercion.  */
8069       check_function_arguments_recurse (callback, ctx,
8070                                         TREE_OPERAND (param, 0), param_num);
8071       return;
8072     }
8073
8074   if (TREE_CODE (param) == CALL_EXPR)
8075     {
8076       tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
8077       tree attrs;
8078       bool found_format_arg = false;
8079
8080       /* See if this is a call to a known internationalization function
8081          that modifies a format arg.  Such a function may have multiple
8082          format_arg attributes (for example, ngettext).  */
8083
8084       for (attrs = TYPE_ATTRIBUTES (type);
8085            attrs;
8086            attrs = TREE_CHAIN (attrs))
8087         if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
8088           {
8089             tree inner_arg;
8090             tree format_num_expr;
8091             int format_num;
8092             int i;
8093             call_expr_arg_iterator iter;
8094
8095             /* Extract the argument number, which was previously checked
8096                to be valid.  */
8097             format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
8098
8099             gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
8100                         && !TREE_INT_CST_HIGH (format_num_expr));
8101
8102             format_num = TREE_INT_CST_LOW (format_num_expr);
8103
8104             for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
8105                  inner_arg != 0;
8106                  inner_arg = next_call_expr_arg (&iter), i++)
8107               if (i == format_num)
8108                 {
8109                   check_function_arguments_recurse (callback, ctx,
8110                                                     inner_arg, param_num);
8111                   found_format_arg = true;
8112                   break;
8113                 }
8114           }
8115
8116       /* If we found a format_arg attribute and did a recursive check,
8117          we are done with checking this argument.  Otherwise, we continue
8118          and this will be considered a non-literal.  */
8119       if (found_format_arg)
8120         return;
8121     }
8122
8123   if (TREE_CODE (param) == COND_EXPR)
8124     {
8125       /* Check both halves of the conditional expression.  */
8126       check_function_arguments_recurse (callback, ctx,
8127                                         TREE_OPERAND (param, 1), param_num);
8128       check_function_arguments_recurse (callback, ctx,
8129                                         TREE_OPERAND (param, 2), param_num);
8130       return;
8131     }
8132
8133   (*callback) (ctx, param, param_num);
8134 }
8135
8136 /* Checks for a builtin function FNDECL that the number of arguments
8137    NARGS against the required number REQUIRED and issues an error if
8138    there is a mismatch.  Returns true if the number of arguments is
8139    correct, otherwise false.  */
8140
8141 static bool
8142 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
8143 {
8144   if (nargs < required)
8145     {
8146       error_at (input_location,
8147                 "not enough arguments to function %qE", fndecl);
8148       return false;
8149     }
8150   else if (nargs > required)
8151     {
8152       error_at (input_location,
8153                 "too many arguments to function %qE", fndecl);
8154       return false;
8155     }
8156   return true;
8157 }
8158
8159 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
8160    Returns false if there was an error, otherwise true.  */
8161
8162 bool
8163 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
8164 {
8165   if (!DECL_BUILT_IN (fndecl)
8166       || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
8167     return true;
8168
8169   switch (DECL_FUNCTION_CODE (fndecl))
8170     {
8171     case BUILT_IN_CONSTANT_P:
8172       return builtin_function_validate_nargs (fndecl, nargs, 1);
8173
8174     case BUILT_IN_ISFINITE:
8175     case BUILT_IN_ISINF:
8176     case BUILT_IN_ISINF_SIGN:
8177     case BUILT_IN_ISNAN:
8178     case BUILT_IN_ISNORMAL:
8179       if (builtin_function_validate_nargs (fndecl, nargs, 1))
8180         {
8181           if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
8182             {
8183               error ("non-floating-point argument in call to "
8184                      "function %qE", fndecl);
8185               return false;
8186             }
8187           return true;
8188         }
8189       return false;
8190
8191     case BUILT_IN_ISGREATER:
8192     case BUILT_IN_ISGREATEREQUAL:
8193     case BUILT_IN_ISLESS:
8194     case BUILT_IN_ISLESSEQUAL:
8195     case BUILT_IN_ISLESSGREATER:
8196     case BUILT_IN_ISUNORDERED:
8197       if (builtin_function_validate_nargs (fndecl, nargs, 2))
8198         {
8199           enum tree_code code0, code1;
8200           code0 = TREE_CODE (TREE_TYPE (args[0]));
8201           code1 = TREE_CODE (TREE_TYPE (args[1]));
8202           if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
8203                 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8204                 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
8205             {
8206               error ("non-floating-point arguments in call to "
8207                      "function %qE", fndecl);
8208               return false;
8209             }
8210           return true;
8211         }
8212       return false;
8213
8214     case BUILT_IN_FPCLASSIFY:
8215       if (builtin_function_validate_nargs (fndecl, nargs, 6))
8216         {
8217           unsigned i;
8218
8219           for (i=0; i<5; i++)
8220             if (TREE_CODE (args[i]) != INTEGER_CST)
8221               {
8222                 error ("non-const integer argument %u in call to function %qE",
8223                        i+1, fndecl);
8224                 return false;
8225               }
8226
8227           if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
8228             {
8229               error ("non-floating-point argument in call to function %qE",
8230                      fndecl);
8231               return false;
8232             }
8233           return true;
8234         }
8235       return false;
8236
8237     case BUILT_IN_ASSUME_ALIGNED:
8238       if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
8239         {
8240           if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
8241             {
8242               error ("non-integer argument 3 in call to function %qE", fndecl);
8243               return false;
8244             }
8245           return true;
8246         }
8247       return false;
8248
8249     default:
8250       return true;
8251     }
8252 }
8253
8254 /* Function to help qsort sort FIELD_DECLs by name order.  */
8255
8256 int
8257 field_decl_cmp (const void *x_p, const void *y_p)
8258 {
8259   const tree *const x = (const tree *const) x_p;
8260   const tree *const y = (const tree *const) y_p;
8261
8262   if (DECL_NAME (*x) == DECL_NAME (*y))
8263     /* A nontype is "greater" than a type.  */
8264     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8265   if (DECL_NAME (*x) == NULL_TREE)
8266     return -1;
8267   if (DECL_NAME (*y) == NULL_TREE)
8268     return 1;
8269   if (DECL_NAME (*x) < DECL_NAME (*y))
8270     return -1;
8271   return 1;
8272 }
8273
8274 static struct {
8275   gt_pointer_operator new_value;
8276   void *cookie;
8277 } resort_data;
8278
8279 /* This routine compares two fields like field_decl_cmp but using the
8280 pointer operator in resort_data.  */
8281
8282 static int
8283 resort_field_decl_cmp (const void *x_p, const void *y_p)
8284 {
8285   const tree *const x = (const tree *const) x_p;
8286   const tree *const y = (const tree *const) y_p;
8287
8288   if (DECL_NAME (*x) == DECL_NAME (*y))
8289     /* A nontype is "greater" than a type.  */
8290     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8291   if (DECL_NAME (*x) == NULL_TREE)
8292     return -1;
8293   if (DECL_NAME (*y) == NULL_TREE)
8294     return 1;
8295   {
8296     tree d1 = DECL_NAME (*x);
8297     tree d2 = DECL_NAME (*y);
8298     resort_data.new_value (&d1, resort_data.cookie);
8299     resort_data.new_value (&d2, resort_data.cookie);
8300     if (d1 < d2)
8301       return -1;
8302   }
8303   return 1;
8304 }
8305
8306 /* Resort DECL_SORTED_FIELDS because pointers have been reordered.  */
8307
8308 void
8309 resort_sorted_fields (void *obj,
8310                       void * ARG_UNUSED (orig_obj),
8311                       gt_pointer_operator new_value,
8312                       void *cookie)
8313 {
8314   struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8315   resort_data.new_value = new_value;
8316   resort_data.cookie = cookie;
8317   qsort (&sf->elts[0], sf->len, sizeof (tree),
8318          resort_field_decl_cmp);
8319 }
8320
8321 /* Subroutine of c_parse_error.
8322    Return the result of concatenating LHS and RHS. RHS is really
8323    a string literal, its first character is indicated by RHS_START and
8324    RHS_SIZE is its length (including the terminating NUL character).
8325
8326    The caller is responsible for deleting the returned pointer.  */
8327
8328 static char *
8329 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
8330 {
8331   const int lhs_size = strlen (lhs);
8332   char *result = XNEWVEC (char, lhs_size + rhs_size);
8333   strncpy (result, lhs, lhs_size);
8334   strncpy (result + lhs_size, rhs_start, rhs_size);
8335   return result;
8336 }
8337
8338 /* Issue the error given by GMSGID, indicating that it occurred before
8339    TOKEN, which had the associated VALUE.  */
8340
8341 void
8342 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
8343                tree value, unsigned char token_flags)
8344 {
8345 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
8346
8347   char *message = NULL;
8348
8349   if (token_type == CPP_EOF)
8350     message = catenate_messages (gmsgid, " at end of input");
8351   else if (token_type == CPP_CHAR
8352            || token_type == CPP_WCHAR
8353            || token_type == CPP_CHAR16
8354            || token_type == CPP_CHAR32)
8355     {
8356       unsigned int val = TREE_INT_CST_LOW (value);
8357       const char *prefix;
8358
8359       switch (token_type)
8360         {
8361         default:
8362           prefix = "";
8363           break;
8364         case CPP_WCHAR:
8365           prefix = "L";
8366           break;
8367         case CPP_CHAR16:
8368           prefix = "u";
8369           break;
8370         case CPP_CHAR32:
8371           prefix = "U";
8372           break;
8373         }
8374
8375       if (val <= UCHAR_MAX && ISGRAPH (val))
8376         message = catenate_messages (gmsgid, " before %s'%c'");
8377       else
8378         message = catenate_messages (gmsgid, " before %s'\\x%x'");
8379
8380       error (message, prefix, val);
8381       free (message);
8382       message = NULL;
8383     }
8384   else if (token_type == CPP_STRING
8385            || token_type == CPP_WSTRING
8386            || token_type == CPP_STRING16
8387            || token_type == CPP_STRING32
8388            || token_type == CPP_UTF8STRING)
8389     message = catenate_messages (gmsgid, " before string constant");
8390   else if (token_type == CPP_NUMBER)
8391     message = catenate_messages (gmsgid, " before numeric constant");
8392   else if (token_type == CPP_NAME)
8393     {
8394       message = catenate_messages (gmsgid, " before %qE");
8395       error (message, value);
8396       free (message);
8397       message = NULL;
8398     }
8399   else if (token_type == CPP_PRAGMA)
8400     message = catenate_messages (gmsgid, " before %<#pragma%>");
8401   else if (token_type == CPP_PRAGMA_EOL)
8402     message = catenate_messages (gmsgid, " before end of line");
8403   else if (token_type == CPP_DECLTYPE)
8404     message = catenate_messages (gmsgid, " before %<decltype%>");
8405   else if (token_type < N_TTYPES)
8406     {
8407       message = catenate_messages (gmsgid, " before %qs token");
8408       error (message, cpp_type2name (token_type, token_flags));
8409       free (message);
8410       message = NULL;
8411     }
8412   else
8413     error (gmsgid);
8414
8415   if (message)
8416     {
8417       error (message);
8418       free (message);
8419     }
8420 #undef catenate_messages
8421 }
8422
8423 /* Mapping for cpp message reasons to the options that enable them.  */
8424
8425 struct reason_option_codes_t
8426 {
8427   const int reason;             /* cpplib message reason.  */
8428   const int option_code;        /* gcc option that controls this message.  */
8429 };
8430
8431 static const struct reason_option_codes_t option_codes[] = {
8432   {CPP_W_DEPRECATED,                    OPT_Wdeprecated},
8433   {CPP_W_COMMENTS,                      OPT_Wcomment},
8434   {CPP_W_TRIGRAPHS,                     OPT_Wtrigraphs},
8435   {CPP_W_MULTICHAR,                     OPT_Wmultichar},
8436   {CPP_W_TRADITIONAL,                   OPT_Wtraditional},
8437   {CPP_W_LONG_LONG,                     OPT_Wlong_long},
8438   {CPP_W_ENDIF_LABELS,                  OPT_Wendif_labels},
8439   {CPP_W_VARIADIC_MACROS,               OPT_Wvariadic_macros},
8440   {CPP_W_BUILTIN_MACRO_REDEFINED,       OPT_Wbuiltin_macro_redefined},
8441   {CPP_W_UNDEF,                         OPT_Wundef},
8442   {CPP_W_UNUSED_MACROS,                 OPT_Wunused_macros},
8443   {CPP_W_CXX_OPERATOR_NAMES,            OPT_Wc___compat},
8444   {CPP_W_NORMALIZE,                     OPT_Wnormalized_},
8445   {CPP_W_INVALID_PCH,                   OPT_Winvalid_pch},
8446   {CPP_W_WARNING_DIRECTIVE,             OPT_Wcpp},
8447   {CPP_W_NONE,                          0}
8448 };
8449
8450 /* Return the gcc option code associated with the reason for a cpp
8451    message, or 0 if none.  */
8452
8453 static int
8454 c_option_controlling_cpp_error (int reason)
8455 {
8456   const struct reason_option_codes_t *entry;
8457
8458   for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
8459     {
8460       if (entry->reason == reason)
8461         return entry->option_code;
8462     }
8463   return 0;
8464 }
8465
8466 /* Callback from cpp_error for PFILE to print diagnostics from the
8467    preprocessor.  The diagnostic is of type LEVEL, with REASON set
8468    to the reason code if LEVEL is represents a warning, at location
8469    LOCATION unless this is after lexing and the compiler's location
8470    should be used instead, with column number possibly overridden by
8471    COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
8472    the arguments.  Returns true if a diagnostic was emitted, false
8473    otherwise.  */
8474
8475 bool
8476 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8477              location_t location, unsigned int column_override,
8478              const char *msg, va_list *ap)
8479 {
8480   diagnostic_info diagnostic;
8481   diagnostic_t dlevel;
8482   bool save_warn_system_headers = global_dc->dc_warn_system_headers;
8483   bool ret;
8484
8485   switch (level)
8486     {
8487     case CPP_DL_WARNING_SYSHDR:
8488       if (flag_no_output)
8489         return false;
8490       global_dc->dc_warn_system_headers = 1;
8491       /* Fall through.  */
8492     case CPP_DL_WARNING:
8493       if (flag_no_output)
8494         return false;
8495       dlevel = DK_WARNING;
8496       break;
8497     case CPP_DL_PEDWARN:
8498       if (flag_no_output && !flag_pedantic_errors)
8499         return false;
8500       dlevel = DK_PEDWARN;
8501       break;
8502     case CPP_DL_ERROR:
8503       dlevel = DK_ERROR;
8504       break;
8505     case CPP_DL_ICE:
8506       dlevel = DK_ICE;
8507       break;
8508     case CPP_DL_NOTE:
8509       dlevel = DK_NOTE;
8510       break;
8511     case CPP_DL_FATAL:
8512       dlevel = DK_FATAL;
8513       break;
8514     default:
8515       gcc_unreachable ();
8516     }
8517   if (done_lexing)
8518     location = input_location;
8519   diagnostic_set_info_translated (&diagnostic, msg, ap,
8520                                   location, dlevel);
8521   if (column_override)
8522     diagnostic_override_column (&diagnostic, column_override);
8523   diagnostic_override_option_index (&diagnostic,
8524                                     c_option_controlling_cpp_error (reason));
8525   ret = report_diagnostic (&diagnostic);
8526   if (level == CPP_DL_WARNING_SYSHDR)
8527     global_dc->dc_warn_system_headers = save_warn_system_headers;
8528   return ret;
8529 }
8530
8531 /* Convert a character from the host to the target execution character
8532    set.  cpplib handles this, mostly.  */
8533
8534 HOST_WIDE_INT
8535 c_common_to_target_charset (HOST_WIDE_INT c)
8536 {
8537   /* Character constants in GCC proper are sign-extended under -fsigned-char,
8538      zero-extended under -fno-signed-char.  cpplib insists that characters
8539      and character constants are always unsigned.  Hence we must convert
8540      back and forth.  */
8541   cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
8542
8543   uc = cpp_host_to_exec_charset (parse_in, uc);
8544
8545   if (flag_signed_char)
8546     return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
8547                                >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
8548   else
8549     return uc;
8550 }
8551
8552 /* Build the result of __builtin_offsetof.  EXPR is a nested sequence of
8553    component references, with STOP_REF, or alternatively an INDIRECT_REF of
8554    NULL, at the bottom; much like the traditional rendering of offsetof as a
8555    macro.  Returns the folded and properly cast result.  */
8556
8557 static tree
8558 fold_offsetof_1 (tree expr, tree stop_ref)
8559 {
8560   enum tree_code code = PLUS_EXPR;
8561   tree base, off, t;
8562
8563   if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK)
8564     return size_zero_node;
8565
8566   switch (TREE_CODE (expr))
8567     {
8568     case ERROR_MARK:
8569       return expr;
8570
8571     case VAR_DECL:
8572       error ("cannot apply %<offsetof%> to static data member %qD", expr);
8573       return error_mark_node;
8574
8575     case CALL_EXPR:
8576     case TARGET_EXPR:
8577       error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
8578       return error_mark_node;
8579
8580     case NOP_EXPR:
8581     case INDIRECT_REF:
8582       if (!integer_zerop (TREE_OPERAND (expr, 0)))
8583         {
8584           error ("cannot apply %<offsetof%> to a non constant address");
8585           return error_mark_node;
8586         }
8587       return size_zero_node;
8588
8589     case COMPONENT_REF:
8590       base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
8591       if (base == error_mark_node)
8592         return base;
8593
8594       t = TREE_OPERAND (expr, 1);
8595       if (DECL_C_BIT_FIELD (t))
8596         {
8597           error ("attempt to take address of bit-field structure "
8598                  "member %qD", t);
8599           return error_mark_node;
8600         }
8601       off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
8602                             size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
8603                                                     1)
8604                                       / BITS_PER_UNIT));
8605       break;
8606
8607     case ARRAY_REF:
8608       base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
8609       if (base == error_mark_node)
8610         return base;
8611
8612       t = TREE_OPERAND (expr, 1);
8613       if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0)
8614         {
8615           code = MINUS_EXPR;
8616           t = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (t), t);
8617         }
8618       t = convert (sizetype, t);
8619       off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
8620
8621       /* Check if the offset goes beyond the upper bound of the array.  */
8622       if (code == PLUS_EXPR && TREE_CODE (t) == INTEGER_CST)
8623         {
8624           tree upbound = array_ref_up_bound (expr);
8625           if (upbound != NULL_TREE
8626               && TREE_CODE (upbound) == INTEGER_CST
8627               && !tree_int_cst_equal (upbound,
8628                                       TYPE_MAX_VALUE (TREE_TYPE (upbound))))
8629             {
8630               upbound = size_binop (PLUS_EXPR, upbound,
8631                                     build_int_cst (TREE_TYPE (upbound), 1));
8632               if (tree_int_cst_lt (upbound, t))
8633                 {
8634                   tree v;
8635
8636                   for (v = TREE_OPERAND (expr, 0);
8637                        TREE_CODE (v) == COMPONENT_REF;
8638                        v = TREE_OPERAND (v, 0))
8639                     if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
8640                         == RECORD_TYPE)
8641                       {
8642                         tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
8643                         for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
8644                           if (TREE_CODE (fld_chain) == FIELD_DECL)
8645                             break;
8646
8647                         if (fld_chain)
8648                           break;
8649                       }
8650                   /* Don't warn if the array might be considered a poor
8651                      man's flexible array member with a very permissive
8652                      definition thereof.  */
8653                   if (TREE_CODE (v) == ARRAY_REF
8654                       || TREE_CODE (v) == COMPONENT_REF)
8655                     warning (OPT_Warray_bounds,
8656                              "index %E denotes an offset "
8657                              "greater than size of %qT",
8658                              t, TREE_TYPE (TREE_OPERAND (expr, 0)));
8659                 }
8660             }
8661         }
8662       break;
8663
8664     case COMPOUND_EXPR:
8665       /* Handle static members of volatile structs.  */
8666       t = TREE_OPERAND (expr, 1);
8667       gcc_assert (TREE_CODE (t) == VAR_DECL);
8668       return fold_offsetof_1 (t, stop_ref);
8669
8670     default:
8671       gcc_unreachable ();
8672     }
8673
8674   return size_binop (code, base, off);
8675 }
8676
8677 tree
8678 fold_offsetof (tree expr, tree stop_ref)
8679 {
8680   /* Convert back from the internal sizetype to size_t.  */
8681   return convert (size_type_node, fold_offsetof_1 (expr, stop_ref));
8682 }
8683
8684 /* Warn for A ?: C expressions (with B omitted) where A is a boolean 
8685    expression, because B will always be true. */
8686
8687 void
8688 warn_for_omitted_condop (location_t location, tree cond) 
8689
8690   if (truth_value_p (TREE_CODE (cond))) 
8691       warning_at (location, OPT_Wparentheses, 
8692                 "the omitted middle operand in ?: will always be %<true%>, "
8693                 "suggest explicit middle operand");
8694
8695
8696 /* Give an error for storing into ARG, which is 'const'.  USE indicates
8697    how ARG was being used.  */
8698
8699 void
8700 readonly_error (tree arg, enum lvalue_use use)
8701 {
8702   gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
8703               || use == lv_asm);
8704   /* Using this macro rather than (for example) arrays of messages
8705      ensures that all the format strings are checked at compile
8706      time.  */
8707 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A)               \
8708                                    : (use == lv_increment ? (I)         \
8709                                    : (use == lv_decrement ? (D) : (AS))))
8710   if (TREE_CODE (arg) == COMPONENT_REF)
8711     {
8712       if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
8713         error (READONLY_MSG (G_("assignment of member "
8714                                 "%qD in read-only object"),
8715                              G_("increment of member "
8716                                 "%qD in read-only object"),
8717                              G_("decrement of member "
8718                                 "%qD in read-only object"),
8719                              G_("member %qD in read-only object "
8720                                 "used as %<asm%> output")),
8721                TREE_OPERAND (arg, 1));
8722       else
8723         error (READONLY_MSG (G_("assignment of read-only member %qD"),
8724                              G_("increment of read-only member %qD"),
8725                              G_("decrement of read-only member %qD"),
8726                              G_("read-only member %qD used as %<asm%> output")),
8727                TREE_OPERAND (arg, 1));
8728     }
8729   else if (TREE_CODE (arg) == VAR_DECL)
8730     error (READONLY_MSG (G_("assignment of read-only variable %qD"),
8731                          G_("increment of read-only variable %qD"),
8732                          G_("decrement of read-only variable %qD"),
8733                          G_("read-only variable %qD used as %<asm%> output")),
8734            arg);
8735   else if (TREE_CODE (arg) == PARM_DECL)
8736     error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
8737                          G_("increment of read-only parameter %qD"),
8738                          G_("decrement of read-only parameter %qD"),
8739                          G_("read-only parameter %qD use as %<asm%> output")),
8740            arg);  
8741   else if (TREE_CODE (arg) == RESULT_DECL)
8742     {
8743       gcc_assert (c_dialect_cxx ());
8744       error (READONLY_MSG (G_("assignment of "
8745                               "read-only named return value %qD"),
8746                            G_("increment of "
8747                               "read-only named return value %qD"),
8748                            G_("decrement of "
8749                               "read-only named return value %qD"),
8750                            G_("read-only named return value %qD "
8751                               "used as %<asm%>output")),
8752              arg);
8753     }
8754   else if (TREE_CODE (arg) == FUNCTION_DECL)
8755     error (READONLY_MSG (G_("assignment of function %qD"),
8756                          G_("increment of function %qD"),
8757                          G_("decrement of function %qD"),
8758                          G_("function %qD used as %<asm%> output")),
8759            arg);
8760   else
8761     error (READONLY_MSG (G_("assignment of read-only location %qE"),
8762                          G_("increment of read-only location %qE"),
8763                          G_("decrement of read-only location %qE"),
8764                          G_("read-only location %qE used as %<asm%> output")),
8765            arg);
8766 }
8767
8768 /* Print an error message for an invalid lvalue.  USE says
8769    how the lvalue is being used and so selects the error message.  LOC
8770    is the location for the error.  */
8771
8772 void
8773 lvalue_error (location_t loc, enum lvalue_use use)
8774 {
8775   switch (use)
8776     {
8777     case lv_assign:
8778       error_at (loc, "lvalue required as left operand of assignment");
8779       break;
8780     case lv_increment:
8781       error_at (loc, "lvalue required as increment operand");
8782       break;
8783     case lv_decrement:
8784       error_at (loc, "lvalue required as decrement operand");
8785       break;
8786     case lv_addressof:
8787       error_at (loc, "lvalue required as unary %<&%> operand");
8788       break;
8789     case lv_asm:
8790       error_at (loc, "lvalue required in asm statement");
8791       break;
8792     default:
8793       gcc_unreachable ();
8794     }
8795 }
8796
8797 /* Print an error message for an invalid indirection of type TYPE.
8798    ERRSTRING is the name of the operator for the indirection.  */
8799
8800 void
8801 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
8802 {
8803   switch (errstring)
8804     {
8805     case RO_NULL:
8806       gcc_assert (c_dialect_cxx ());
8807       error_at (loc, "invalid type argument (have %qT)", type);
8808       break;
8809     case RO_ARRAY_INDEXING:
8810       error_at (loc,
8811                 "invalid type argument of array indexing (have %qT)",
8812                 type);
8813       break;
8814     case RO_UNARY_STAR:
8815       error_at (loc,
8816                 "invalid type argument of unary %<*%> (have %qT)",
8817                 type);
8818       break;
8819     case RO_ARROW:
8820       error_at (loc,
8821                 "invalid type argument of %<->%> (have %qT)",
8822                 type);
8823       break;
8824     case RO_IMPLICIT_CONVERSION:
8825       error_at (loc,
8826                 "invalid type argument of implicit conversion (have %qT)",
8827                 type);
8828       break;
8829     default:
8830       gcc_unreachable ();
8831     }
8832 }
8833 \f
8834 /* *PTYPE is an incomplete array.  Complete it with a domain based on
8835    INITIAL_VALUE.  If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
8836    is true.  Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8837    2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty.  */
8838
8839 int
8840 complete_array_type (tree *ptype, tree initial_value, bool do_default)
8841 {
8842   tree maxindex, type, main_type, elt, unqual_elt;
8843   int failure = 0, quals;
8844   hashval_t hashcode = 0;
8845
8846   maxindex = size_zero_node;
8847   if (initial_value)
8848     {
8849       if (TREE_CODE (initial_value) == STRING_CST)
8850         {
8851           int eltsize
8852             = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8853           maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
8854         }
8855       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8856         {
8857           VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
8858
8859           if (VEC_empty (constructor_elt, v))
8860             {
8861               if (pedantic)
8862                 failure = 3;
8863               maxindex = ssize_int (-1);
8864             }
8865           else
8866             {
8867               tree curindex;
8868               unsigned HOST_WIDE_INT cnt;
8869               constructor_elt *ce;
8870               bool fold_p = false;
8871
8872               if (VEC_index (constructor_elt, v, 0)->index)
8873                 maxindex = fold_convert_loc (input_location, sizetype,
8874                                              VEC_index (constructor_elt,
8875                                                         v, 0)->index);
8876               curindex = maxindex;
8877
8878               for (cnt = 1;
8879                    VEC_iterate (constructor_elt, v, cnt, ce);
8880                    cnt++)
8881                 {
8882                   bool curfold_p = false;
8883                   if (ce->index)
8884                     curindex = ce->index, curfold_p = true;
8885                   else
8886                     {
8887                       if (fold_p)
8888                         curindex = fold_convert (sizetype, curindex);
8889                       curindex = size_binop (PLUS_EXPR, curindex,
8890                                              size_one_node);
8891                     }
8892                   if (tree_int_cst_lt (maxindex, curindex))
8893                     maxindex = curindex, fold_p = curfold_p;
8894                 }
8895                if (fold_p)
8896                  maxindex = fold_convert (sizetype, maxindex);
8897             }
8898         }
8899       else
8900         {
8901           /* Make an error message unless that happened already.  */
8902           if (initial_value != error_mark_node)
8903             failure = 1;
8904         }
8905     }
8906   else
8907     {
8908       failure = 2;
8909       if (!do_default)
8910         return failure;
8911     }
8912
8913   type = *ptype;
8914   elt = TREE_TYPE (type);
8915   quals = TYPE_QUALS (strip_array_types (elt));
8916   if (quals == 0)
8917     unqual_elt = elt;
8918   else
8919     unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
8920
8921   /* Using build_distinct_type_copy and modifying things afterward instead
8922      of using build_array_type to create a new type preserves all of the
8923      TYPE_LANG_FLAG_? bits that the front end may have set.  */
8924   main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8925   TREE_TYPE (main_type) = unqual_elt;
8926   TYPE_DOMAIN (main_type)
8927     = build_range_type (TREE_TYPE (maxindex),
8928                         build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
8929   layout_type (main_type);
8930
8931   /* Make sure we have the canonical MAIN_TYPE. */
8932   hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
8933   hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
8934                                     hashcode);
8935   main_type = type_hash_canon (hashcode, main_type);
8936
8937   /* Fix the canonical type.  */
8938   if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
8939       || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
8940     SET_TYPE_STRUCTURAL_EQUALITY (main_type);
8941   else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
8942            || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
8943                != TYPE_DOMAIN (main_type)))
8944     TYPE_CANONICAL (main_type)
8945       = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
8946                           TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
8947   else
8948     TYPE_CANONICAL (main_type) = main_type;
8949
8950   if (quals == 0)
8951     type = main_type;
8952   else
8953     type = c_build_qualified_type (main_type, quals);
8954
8955   if (COMPLETE_TYPE_P (type)
8956       && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8957       && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
8958     {
8959       error ("size of array is too large");
8960       /* If we proceed with the array type as it is, we'll eventually
8961          crash in tree_low_cst().  */
8962       type = error_mark_node;
8963     }
8964
8965   *ptype = type;
8966   return failure;
8967 }
8968
8969 /* Like c_mark_addressable but don't check register qualifier.  */
8970 void 
8971 c_common_mark_addressable_vec (tree t)
8972 {   
8973   while (handled_component_p (t))
8974     t = TREE_OPERAND (t, 0);
8975   if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8976     return;
8977   TREE_ADDRESSABLE (t) = 1;
8978 }
8979
8980
8981 \f
8982 /* Used to help initialize the builtin-types.def table.  When a type of
8983    the correct size doesn't exist, use error_mark_node instead of NULL.
8984    The later results in segfaults even when a decl using the type doesn't
8985    get invoked.  */
8986
8987 tree
8988 builtin_type_for_size (int size, bool unsignedp)
8989 {
8990   tree type = lang_hooks.types.type_for_size (size, unsignedp);
8991   return type ? type : error_mark_node;
8992 }
8993
8994 /* A helper function for resolve_overloaded_builtin in resolving the
8995    overloaded __sync_ builtins.  Returns a positive power of 2 if the
8996    first operand of PARAMS is a pointer to a supported data type.
8997    Returns 0 if an error is encountered.  */
8998
8999 static int
9000 sync_resolve_size (tree function, VEC(tree,gc) *params)
9001 {
9002   tree type;
9003   int size;
9004
9005   if (VEC_empty (tree, params))
9006     {
9007       error ("too few arguments to function %qE", function);
9008       return 0;
9009     }
9010
9011   type = TREE_TYPE (VEC_index (tree, params, 0));
9012   if (TREE_CODE (type) != POINTER_TYPE)
9013     goto incompatible;
9014
9015   type = TREE_TYPE (type);
9016   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9017     goto incompatible;
9018
9019   size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
9020   if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
9021     return size;
9022
9023  incompatible:
9024   error ("incompatible type for argument %d of %qE", 1, function);
9025   return 0;
9026 }
9027
9028 /* A helper function for resolve_overloaded_builtin.  Adds casts to
9029    PARAMS to make arguments match up with those of FUNCTION.  Drops
9030    the variadic arguments at the end.  Returns false if some error
9031    was encountered; true on success.  */
9032
9033 static bool
9034 sync_resolve_params (tree orig_function, tree function, VEC(tree, gc) *params)
9035 {
9036   function_args_iterator iter;
9037   tree ptype;
9038   unsigned int parmnum;
9039
9040   function_args_iter_init (&iter, TREE_TYPE (function));
9041   /* We've declared the implementation functions to use "volatile void *"
9042      as the pointer parameter, so we shouldn't get any complaints from the
9043      call to check_function_arguments what ever type the user used.  */
9044   function_args_iter_next (&iter);
9045   ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
9046
9047   /* For the rest of the values, we need to cast these to FTYPE, so that we
9048      don't get warnings for passing pointer types, etc.  */
9049   parmnum = 0;
9050   while (1)
9051     {
9052       tree val, arg_type;
9053
9054       arg_type = function_args_iter_cond (&iter);
9055       /* XXX void_type_node belies the abstraction.  */
9056       if (arg_type == void_type_node)
9057         break;
9058
9059       ++parmnum;
9060       if (VEC_length (tree, params) <= parmnum)
9061         {
9062           error ("too few arguments to function %qE", orig_function);
9063           return false;
9064         }
9065
9066       /* ??? Ideally for the first conversion we'd use convert_for_assignment
9067          so that we get warnings for anything that doesn't match the pointer
9068          type.  This isn't portable across the C and C++ front ends atm.  */
9069       val = VEC_index (tree, params, parmnum);
9070       val = convert (ptype, val);
9071       val = convert (arg_type, val);
9072       VEC_replace (tree, params, parmnum, val);
9073
9074       function_args_iter_next (&iter);
9075     }
9076
9077   /* The definition of these primitives is variadic, with the remaining
9078      being "an optional list of variables protected by the memory barrier".
9079      No clue what that's supposed to mean, precisely, but we consider all
9080      call-clobbered variables to be protected so we're safe.  */
9081   VEC_truncate (tree, params, parmnum + 1);
9082
9083   return true;
9084 }
9085
9086 /* A helper function for resolve_overloaded_builtin.  Adds a cast to
9087    RESULT to make it match the type of the first pointer argument in
9088    PARAMS.  */
9089
9090 static tree
9091 sync_resolve_return (tree first_param, tree result)
9092 {
9093   tree ptype = TREE_TYPE (TREE_TYPE (first_param));
9094   ptype = TYPE_MAIN_VARIANT (ptype);
9095   return convert (ptype, result);
9096 }
9097
9098 /* Some builtin functions are placeholders for other expressions.  This
9099    function should be called immediately after parsing the call expression
9100    before surrounding code has committed to the type of the expression.
9101
9102    LOC is the location of the builtin call.
9103
9104    FUNCTION is the DECL that has been invoked; it is known to be a builtin.
9105    PARAMS is the argument list for the call.  The return value is non-null
9106    when expansion is complete, and null if normal processing should
9107    continue.  */
9108
9109 tree
9110 resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params)
9111 {
9112   enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
9113   switch (DECL_BUILT_IN_CLASS (function))
9114     {
9115     case BUILT_IN_NORMAL:
9116       break;
9117     case BUILT_IN_MD:
9118       if (targetm.resolve_overloaded_builtin)
9119         return targetm.resolve_overloaded_builtin (loc, function, params);
9120       else
9121         return NULL_TREE;
9122     default:
9123       return NULL_TREE;
9124     }
9125
9126   /* Handle BUILT_IN_NORMAL here.  */
9127   switch (orig_code)
9128     {
9129     case BUILT_IN_SYNC_FETCH_AND_ADD_N:
9130     case BUILT_IN_SYNC_FETCH_AND_SUB_N:
9131     case BUILT_IN_SYNC_FETCH_AND_OR_N:
9132     case BUILT_IN_SYNC_FETCH_AND_AND_N:
9133     case BUILT_IN_SYNC_FETCH_AND_XOR_N:
9134     case BUILT_IN_SYNC_FETCH_AND_NAND_N:
9135     case BUILT_IN_SYNC_ADD_AND_FETCH_N:
9136     case BUILT_IN_SYNC_SUB_AND_FETCH_N:
9137     case BUILT_IN_SYNC_OR_AND_FETCH_N:
9138     case BUILT_IN_SYNC_AND_AND_FETCH_N:
9139     case BUILT_IN_SYNC_XOR_AND_FETCH_N:
9140     case BUILT_IN_SYNC_NAND_AND_FETCH_N:
9141     case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
9142     case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
9143     case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
9144     case BUILT_IN_SYNC_LOCK_RELEASE_N:
9145       {
9146         int n = sync_resolve_size (function, params);
9147         tree new_function, first_param, result;
9148         enum built_in_function fncode;
9149
9150         if (n == 0)
9151           return error_mark_node;
9152
9153         fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
9154         new_function = builtin_decl_explicit (fncode);
9155         if (!sync_resolve_params (function, new_function, params))
9156           return error_mark_node;
9157
9158         first_param = VEC_index (tree, params, 0);
9159         result = build_function_call_vec (loc, new_function, params, NULL);
9160         if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
9161             && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N)
9162           result = sync_resolve_return (first_param, result);
9163
9164         return result;
9165       }
9166
9167     default:
9168       return NULL_TREE;
9169     }
9170 }
9171
9172 /* Ignoring their sign, return true if two scalar types are the same.  */
9173 bool
9174 same_scalar_type_ignoring_signedness (tree t1, tree t2)
9175 {
9176   enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
9177
9178   gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
9179               && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
9180                   || c2 == FIXED_POINT_TYPE));
9181
9182   /* Equality works here because c_common_signed_type uses
9183      TYPE_MAIN_VARIANT.  */
9184   return c_common_signed_type (t1)
9185     == c_common_signed_type (t2);
9186 }
9187
9188 /* Check for missing format attributes on function pointers.  LTYPE is
9189    the new type or left-hand side type.  RTYPE is the old type or
9190    right-hand side type.  Returns TRUE if LTYPE is missing the desired
9191    attribute.  */
9192
9193 bool
9194 check_missing_format_attribute (tree ltype, tree rtype)
9195 {
9196   tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
9197   tree ra;
9198
9199   for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
9200     if (is_attribute_p ("format", TREE_PURPOSE (ra)))
9201       break;
9202   if (ra)
9203     {
9204       tree la;
9205       for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
9206         if (is_attribute_p ("format", TREE_PURPOSE (la)))
9207           break;
9208       return !la;
9209     }
9210   else
9211     return false;
9212 }
9213
9214 /* Subscripting with type char is likely to lose on a machine where
9215    chars are signed.  So warn on any machine, but optionally.  Don't
9216    warn for unsigned char since that type is safe.  Don't warn for
9217    signed char because anyone who uses that must have done so
9218    deliberately. Furthermore, we reduce the false positive load by
9219    warning only for non-constant value of type char.  */
9220
9221 void
9222 warn_array_subscript_with_type_char (tree index)
9223 {
9224   if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
9225       && TREE_CODE (index) != INTEGER_CST)
9226     warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
9227 }
9228
9229 /* Implement -Wparentheses for the unexpected C precedence rules, to
9230    cover cases like x + y << z which readers are likely to
9231    misinterpret.  We have seen an expression in which CODE is a binary
9232    operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
9233    before folding had CODE_LEFT and CODE_RIGHT.  CODE_LEFT and
9234    CODE_RIGHT may be ERROR_MARK, which means that that side of the
9235    expression was not formed using a binary or unary operator, or it
9236    was enclosed in parentheses.  */
9237
9238 void
9239 warn_about_parentheses (enum tree_code code,
9240                         enum tree_code code_left, tree arg_left,
9241                         enum tree_code code_right, tree arg_right)
9242 {
9243   if (!warn_parentheses)
9244     return;
9245
9246   /* This macro tests that the expression ARG with original tree code
9247      CODE appears to be a boolean expression. or the result of folding a
9248      boolean expression.  */
9249 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG)                             \
9250         (truth_value_p (TREE_CODE (ARG))                                    \
9251          || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE                     \
9252          /* Folding may create 0 or 1 integers from other expressions.  */  \
9253          || ((CODE) != INTEGER_CST                                          \
9254              && (integer_onep (ARG) || integer_zerop (ARG))))
9255
9256   switch (code)
9257     {
9258     case LSHIFT_EXPR:
9259       if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9260         warning (OPT_Wparentheses,
9261                  "suggest parentheses around %<+%> inside %<<<%>");
9262       else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9263         warning (OPT_Wparentheses,
9264                  "suggest parentheses around %<-%> inside %<<<%>");
9265       return;
9266
9267     case RSHIFT_EXPR:
9268       if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9269         warning (OPT_Wparentheses,
9270                  "suggest parentheses around %<+%> inside %<>>%>");
9271       else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9272         warning (OPT_Wparentheses,
9273                  "suggest parentheses around %<-%> inside %<>>%>");
9274       return;
9275
9276     case TRUTH_ORIF_EXPR:
9277       if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
9278         warning (OPT_Wparentheses,
9279                  "suggest parentheses around %<&&%> within %<||%>");
9280       return;
9281
9282     case BIT_IOR_EXPR:
9283       if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
9284           || code_left == PLUS_EXPR || code_left == MINUS_EXPR
9285           || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
9286           || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
9287         warning (OPT_Wparentheses,
9288                  "suggest parentheses around arithmetic in operand of %<|%>");
9289       /* Check cases like x|y==z */
9290       else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9291                || TREE_CODE_CLASS (code_right) == tcc_comparison)
9292         warning (OPT_Wparentheses,
9293                  "suggest parentheses around comparison in operand of %<|%>");
9294       /* Check cases like !x | y */
9295       else if (code_left == TRUTH_NOT_EXPR
9296                && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
9297         warning (OPT_Wparentheses, "suggest parentheses around operand of "
9298                  "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
9299       return;
9300
9301     case BIT_XOR_EXPR:
9302       if (code_left == BIT_AND_EXPR
9303           || code_left == PLUS_EXPR || code_left == MINUS_EXPR
9304           || code_right == BIT_AND_EXPR
9305           || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
9306         warning (OPT_Wparentheses,
9307                  "suggest parentheses around arithmetic in operand of %<^%>");
9308       /* Check cases like x^y==z */
9309       else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9310                || TREE_CODE_CLASS (code_right) == tcc_comparison)
9311         warning (OPT_Wparentheses,
9312                  "suggest parentheses around comparison in operand of %<^%>");
9313       return;
9314
9315     case BIT_AND_EXPR:
9316       if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9317         warning (OPT_Wparentheses,
9318                  "suggest parentheses around %<+%> in operand of %<&%>");
9319       else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9320         warning (OPT_Wparentheses,
9321                  "suggest parentheses around %<-%> in operand of %<&%>");
9322       /* Check cases like x&y==z */
9323       else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9324                || TREE_CODE_CLASS (code_right) == tcc_comparison)
9325         warning (OPT_Wparentheses,
9326                  "suggest parentheses around comparison in operand of %<&%>");
9327       /* Check cases like !x & y */
9328       else if (code_left == TRUTH_NOT_EXPR
9329                && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
9330         warning (OPT_Wparentheses, "suggest parentheses around operand of "
9331                  "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
9332       return;
9333
9334     case EQ_EXPR:
9335       if (TREE_CODE_CLASS (code_left) == tcc_comparison
9336           || TREE_CODE_CLASS (code_right) == tcc_comparison)
9337         warning (OPT_Wparentheses,
9338                  "suggest parentheses around comparison in operand of %<==%>");
9339       return;
9340     case NE_EXPR:
9341       if (TREE_CODE_CLASS (code_left) == tcc_comparison
9342           || TREE_CODE_CLASS (code_right) == tcc_comparison)
9343         warning (OPT_Wparentheses,
9344                  "suggest parentheses around comparison in operand of %<!=%>");
9345       return;
9346
9347     default:
9348       if (TREE_CODE_CLASS (code) == tcc_comparison
9349            && ((TREE_CODE_CLASS (code_left) == tcc_comparison
9350                 && code_left != NE_EXPR && code_left != EQ_EXPR
9351                 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
9352                || (TREE_CODE_CLASS (code_right) == tcc_comparison
9353                    && code_right != NE_EXPR && code_right != EQ_EXPR
9354                    && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
9355         warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
9356                  "have their mathematical meaning");
9357       return;
9358     }
9359 #undef NOT_A_BOOLEAN_EXPR_P
9360 }
9361
9362 /* If LABEL (a LABEL_DECL) has not been used, issue a warning.  */
9363
9364 void
9365 warn_for_unused_label (tree label)
9366 {
9367   if (!TREE_USED (label))
9368     {
9369       if (DECL_INITIAL (label))
9370         warning (OPT_Wunused_label, "label %q+D defined but not used", label);
9371       else
9372         warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
9373     }
9374 }
9375
9376 /* Warn for division by zero according to the value of DIVISOR.  LOC
9377    is the location of the division operator.  */
9378
9379 void
9380 warn_for_div_by_zero (location_t loc, tree divisor)
9381 {
9382   /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
9383      about division by zero.  Do not issue a warning if DIVISOR has a
9384      floating-point type, since we consider 0.0/0.0 a valid way of
9385      generating a NaN.  */
9386   if (c_inhibit_evaluation_warnings == 0
9387       && (integer_zerop (divisor) || fixed_zerop (divisor)))
9388     warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
9389 }
9390
9391 /* Subroutine of build_binary_op. Give warnings for comparisons
9392    between signed and unsigned quantities that may fail. Do the
9393    checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
9394    so that casts will be considered, but default promotions won't
9395    be.
9396
9397    LOCATION is the location of the comparison operator.
9398
9399    The arguments of this function map directly to local variables
9400    of build_binary_op.  */
9401
9402 void
9403 warn_for_sign_compare (location_t location,
9404                        tree orig_op0, tree orig_op1,
9405                        tree op0, tree op1,
9406                        tree result_type, enum tree_code resultcode)
9407 {
9408   int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
9409   int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
9410   int unsignedp0, unsignedp1;
9411
9412   /* In C++, check for comparison of different enum types.  */
9413   if (c_dialect_cxx()
9414       && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
9415       && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
9416       && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
9417          != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
9418     {
9419       warning_at (location,
9420                   OPT_Wsign_compare, "comparison between types %qT and %qT",
9421                   TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
9422     }
9423
9424   /* Do not warn if the comparison is being done in a signed type,
9425      since the signed type will only be chosen if it can represent
9426      all the values of the unsigned type.  */
9427   if (!TYPE_UNSIGNED (result_type))
9428     /* OK */;
9429   /* Do not warn if both operands are unsigned.  */
9430   else if (op0_signed == op1_signed)
9431     /* OK */;
9432   else
9433     {
9434       tree sop, uop, base_type;
9435       bool ovf;
9436
9437       if (op0_signed)
9438         sop = orig_op0, uop = orig_op1;
9439       else
9440         sop = orig_op1, uop = orig_op0;
9441
9442       STRIP_TYPE_NOPS (sop);
9443       STRIP_TYPE_NOPS (uop);
9444       base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
9445                    ? TREE_TYPE (result_type) : result_type);
9446
9447       /* Do not warn if the signed quantity is an unsuffixed integer
9448          literal (or some static constant expression involving such
9449          literals or a conditional expression involving such literals)
9450          and it is non-negative.  */
9451       if (tree_expr_nonnegative_warnv_p (sop, &ovf))
9452         /* OK */;
9453       /* Do not warn if the comparison is an equality operation, the
9454          unsigned quantity is an integral constant, and it would fit
9455          in the result if the result were signed.  */
9456       else if (TREE_CODE (uop) == INTEGER_CST
9457                && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
9458                && int_fits_type_p (uop, c_common_signed_type (base_type)))
9459         /* OK */;
9460       /* In C, do not warn if the unsigned quantity is an enumeration
9461          constant and its maximum value would fit in the result if the
9462          result were signed.  */
9463       else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
9464                && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
9465                && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
9466                                    c_common_signed_type (base_type)))
9467         /* OK */;
9468       else
9469         warning_at (location,
9470                     OPT_Wsign_compare,
9471                     "comparison between signed and unsigned integer expressions");
9472     }
9473
9474   /* Warn if two unsigned values are being compared in a size larger
9475      than their original size, and one (and only one) is the result of
9476      a `~' operator.  This comparison will always fail.
9477
9478      Also warn if one operand is a constant, and the constant does not
9479      have all bits set that are set in the ~ operand when it is
9480      extended.  */
9481
9482   op0 = c_common_get_narrower (op0, &unsignedp0);
9483   op1 = c_common_get_narrower (op1, &unsignedp1);
9484
9485   if ((TREE_CODE (op0) == BIT_NOT_EXPR)
9486       ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
9487     {
9488       if (TREE_CODE (op0) == BIT_NOT_EXPR)
9489         op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
9490       if (TREE_CODE (op1) == BIT_NOT_EXPR)
9491         op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
9492
9493       if (host_integerp (op0, 0) || host_integerp (op1, 0))
9494         {
9495           tree primop;
9496           HOST_WIDE_INT constant, mask;
9497           int unsignedp;
9498           unsigned int bits;
9499
9500           if (host_integerp (op0, 0))
9501             {
9502               primop = op1;
9503               unsignedp = unsignedp1;
9504               constant = tree_low_cst (op0, 0);
9505             }
9506           else
9507             {
9508               primop = op0;
9509               unsignedp = unsignedp0;
9510               constant = tree_low_cst (op1, 0);
9511             }
9512
9513           bits = TYPE_PRECISION (TREE_TYPE (primop));
9514           if (bits < TYPE_PRECISION (result_type)
9515               && bits < HOST_BITS_PER_LONG && unsignedp)
9516             {
9517               mask = (~ (HOST_WIDE_INT) 0) << bits;
9518               if ((mask & constant) != mask)
9519                 {
9520                   if (constant == 0)
9521                     warning (OPT_Wsign_compare,
9522                              "promoted ~unsigned is always non-zero");
9523                   else
9524                     warning_at (location, OPT_Wsign_compare,
9525                                 "comparison of promoted ~unsigned with constant");
9526                 }
9527             }
9528         }
9529       else if (unsignedp0 && unsignedp1
9530                && (TYPE_PRECISION (TREE_TYPE (op0))
9531                    < TYPE_PRECISION (result_type))
9532                && (TYPE_PRECISION (TREE_TYPE (op1))
9533                    < TYPE_PRECISION (result_type)))
9534         warning_at (location, OPT_Wsign_compare,
9535                  "comparison of promoted ~unsigned with unsigned");
9536     }
9537 }
9538
9539 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
9540    type via c_common_type.  If -Wdouble-promotion is in use, and the
9541    conditions for warning have been met, issue a warning.  GMSGID is
9542    the warning message.  It must have two %T specifiers for the type
9543    that was converted (generally "float") and the type to which it was
9544    converted (generally "double), respectively.  LOC is the location
9545    to which the awrning should refer.  */
9546
9547 void
9548 do_warn_double_promotion (tree result_type, tree type1, tree type2,
9549                          const char *gmsgid, location_t loc)
9550 {
9551   tree source_type;
9552
9553   if (!warn_double_promotion)
9554     return;
9555   /* If the conversion will not occur at run-time, there is no need to
9556      warn about it.  */
9557   if (c_inhibit_evaluation_warnings)
9558     return;
9559   if (TYPE_MAIN_VARIANT (result_type) != double_type_node
9560       && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
9561     return;
9562   if (TYPE_MAIN_VARIANT (type1) == float_type_node
9563       || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
9564     source_type = type1;
9565   else if (TYPE_MAIN_VARIANT (type2) == float_type_node
9566            || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
9567     source_type = type2;
9568   else
9569     return;
9570   warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
9571 }
9572
9573 /* Setup a TYPE_DECL node as a typedef representation.
9574
9575    X is a TYPE_DECL for a typedef statement.  Create a brand new
9576    ..._TYPE node (which will be just a variant of the existing
9577    ..._TYPE node with identical properties) and then install X
9578    as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
9579
9580    The whole point here is to end up with a situation where each
9581    and every ..._TYPE node the compiler creates will be uniquely
9582    associated with AT MOST one node representing a typedef name.
9583    This way, even though the compiler substitutes corresponding
9584    ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
9585    early on, later parts of the compiler can always do the reverse
9586    translation and get back the corresponding typedef name.  For
9587    example, given:
9588
9589         typedef struct S MY_TYPE;
9590         MY_TYPE object;
9591
9592    Later parts of the compiler might only know that `object' was of
9593    type `struct S' if it were not for code just below.  With this
9594    code however, later parts of the compiler see something like:
9595
9596         struct S' == struct S
9597         typedef struct S' MY_TYPE;
9598         struct S' object;
9599
9600     And they can then deduce (from the node for type struct S') that
9601     the original object declaration was:
9602
9603                 MY_TYPE object;
9604
9605     Being able to do this is important for proper support of protoize,
9606     and also for generating precise symbolic debugging information
9607     which takes full account of the programmer's (typedef) vocabulary.
9608
9609     Obviously, we don't want to generate a duplicate ..._TYPE node if
9610     the TYPE_DECL node that we are now processing really represents a
9611     standard built-in type.  */
9612
9613 void
9614 set_underlying_type (tree x)
9615 {
9616   if (x == error_mark_node)
9617     return;
9618   if (DECL_IS_BUILTIN (x))
9619     {
9620       if (TYPE_NAME (TREE_TYPE (x)) == 0)
9621         TYPE_NAME (TREE_TYPE (x)) = x;
9622     }
9623   else if (TREE_TYPE (x) != error_mark_node
9624            && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
9625     {
9626       tree tt = TREE_TYPE (x);
9627       DECL_ORIGINAL_TYPE (x) = tt;
9628       tt = build_variant_type_copy (tt);
9629       TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
9630       TYPE_NAME (tt) = x;
9631       TREE_USED (tt) = TREE_USED (x);
9632       TREE_TYPE (x) = tt;
9633     }
9634 }
9635
9636 /* Record the types used by the current global variable declaration
9637    being parsed, so that we can decide later to emit their debug info.
9638    Those types are in types_used_by_cur_var_decl, and we are going to
9639    store them in the types_used_by_vars_hash hash table.
9640    DECL is the declaration of the global variable that has been parsed.  */
9641
9642 void
9643 record_types_used_by_current_var_decl (tree decl)
9644 {
9645   gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
9646
9647   while (!VEC_empty (tree, types_used_by_cur_var_decl))
9648     {
9649       tree type = VEC_pop (tree, types_used_by_cur_var_decl);
9650       types_used_by_var_decl_insert (type, decl);
9651     }
9652 }
9653
9654 /* If DECL is a typedef that is declared in the current function,
9655    record it for the purpose of -Wunused-local-typedefs.  */
9656
9657 void
9658 record_locally_defined_typedef (tree decl)
9659 {
9660   struct c_language_function *l;
9661
9662   if (!warn_unused_local_typedefs
9663       || cfun == NULL
9664       /* if this is not a locally defined typedef then we are not
9665          interested.  */
9666       || !is_typedef_decl (decl)
9667       || !decl_function_context (decl))
9668     return;
9669
9670   l = (struct c_language_function *) cfun->language;
9671   VEC_safe_push (tree, gc, l->local_typedefs, decl);
9672 }
9673
9674 /* If T is a TYPE_DECL declared locally, mark it as used.  */
9675
9676 void
9677 maybe_record_typedef_use (tree t)
9678 {
9679   if (!is_typedef_decl (t))
9680     return;
9681
9682   TREE_USED (t) = true;
9683 }
9684
9685 /* Warn if there are some unused locally defined typedefs in the
9686    current function. */
9687
9688 void
9689 maybe_warn_unused_local_typedefs (void)
9690 {
9691   int i;
9692   tree decl;
9693   /* The number of times we have emitted -Wunused-local-typedefs
9694      warnings.  If this is different from errorcount, that means some
9695      unrelated errors have been issued.  In which case, we'll avoid
9696      emitting "unused-local-typedefs" warnings.  */
9697   static int unused_local_typedefs_warn_count;
9698   struct c_language_function *l;
9699
9700   if (cfun == NULL)
9701     return;
9702
9703   if ((l = (struct c_language_function *) cfun->language) == NULL)
9704     return;
9705
9706   if (warn_unused_local_typedefs
9707       && errorcount == unused_local_typedefs_warn_count)
9708     {
9709       FOR_EACH_VEC_ELT (tree, l->local_typedefs, i, decl)
9710         if (!TREE_USED (decl))
9711           warning_at (DECL_SOURCE_LOCATION (decl),
9712                       OPT_Wunused_local_typedefs,
9713                       "typedef %qD locally defined but not used", decl);
9714       unused_local_typedefs_warn_count = errorcount;
9715     }
9716
9717   if (l->local_typedefs)
9718     {
9719       VEC_free (tree, gc, l->local_typedefs);
9720       l->local_typedefs = NULL;
9721     }
9722 }
9723
9724 /* The C and C++ parsers both use vectors to hold function arguments.
9725    For efficiency, we keep a cache of unused vectors.  This is the
9726    cache.  */
9727
9728 typedef VEC(tree,gc)* tree_gc_vec;
9729 DEF_VEC_P(tree_gc_vec);
9730 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
9731 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
9732
9733 /* Return a new vector from the cache.  If the cache is empty,
9734    allocate a new vector.  These vectors are GC'ed, so it is OK if the
9735    pointer is not released..  */
9736
9737 VEC(tree,gc) *
9738 make_tree_vector (void)
9739 {
9740   if (!VEC_empty (tree_gc_vec, tree_vector_cache))
9741     return VEC_pop (tree_gc_vec, tree_vector_cache);
9742   else
9743     {
9744       /* Passing 0 to VEC_alloc returns NULL, and our callers require
9745          that we always return a non-NULL value.  The vector code uses
9746          4 when growing a NULL vector, so we do too.  */
9747       return VEC_alloc (tree, gc, 4);
9748     }
9749 }
9750
9751 /* Release a vector of trees back to the cache.  */
9752
9753 void
9754 release_tree_vector (VEC(tree,gc) *vec)
9755 {
9756   if (vec != NULL)
9757     {
9758       VEC_truncate (tree, vec, 0);
9759       VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
9760     }
9761 }
9762
9763 /* Get a new tree vector holding a single tree.  */
9764
9765 VEC(tree,gc) *
9766 make_tree_vector_single (tree t)
9767 {
9768   VEC(tree,gc) *ret = make_tree_vector ();
9769   VEC_quick_push (tree, ret, t);
9770   return ret;
9771 }
9772
9773 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain.  */
9774
9775 VEC(tree,gc) *
9776 make_tree_vector_from_list (tree list)
9777 {
9778   VEC(tree,gc) *ret = make_tree_vector ();
9779   for (; list; list = TREE_CHAIN (list))
9780     VEC_safe_push (tree, gc, ret, TREE_VALUE (list));
9781   return ret;
9782 }
9783
9784 /* Get a new tree vector which is a copy of an existing one.  */
9785
9786 VEC(tree,gc) *
9787 make_tree_vector_copy (const VEC(tree,gc) *orig)
9788 {
9789   VEC(tree,gc) *ret;
9790   unsigned int ix;
9791   tree t;
9792
9793   ret = make_tree_vector ();
9794   VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
9795   FOR_EACH_VEC_ELT (tree, orig, ix, t)
9796     VEC_quick_push (tree, ret, t);
9797   return ret;
9798 }
9799
9800 /* Return true if KEYWORD starts a type specifier.  */
9801
9802 bool
9803 keyword_begins_type_specifier (enum rid keyword)
9804 {
9805   switch (keyword)
9806     {
9807     case RID_INT:
9808     case RID_CHAR:
9809     case RID_FLOAT:
9810     case RID_DOUBLE:
9811     case RID_VOID:
9812     case RID_INT128:
9813     case RID_UNSIGNED:
9814     case RID_LONG:
9815     case RID_SHORT:
9816     case RID_SIGNED:
9817     case RID_DFLOAT32:
9818     case RID_DFLOAT64:
9819     case RID_DFLOAT128:
9820     case RID_FRACT:
9821     case RID_ACCUM:
9822     case RID_BOOL:
9823     case RID_WCHAR:
9824     case RID_CHAR16:
9825     case RID_CHAR32:
9826     case RID_SAT:
9827     case RID_COMPLEX:
9828     case RID_TYPEOF:
9829     case RID_STRUCT:
9830     case RID_CLASS:
9831     case RID_UNION:
9832     case RID_ENUM:
9833       return true;
9834     default:
9835       return false;
9836     }
9837 }
9838
9839 /* Return true if KEYWORD names a type qualifier.  */
9840
9841 bool
9842 keyword_is_type_qualifier (enum rid keyword)
9843 {
9844   switch (keyword)
9845     {
9846     case RID_CONST:
9847     case RID_VOLATILE:
9848     case RID_RESTRICT:
9849       return true;
9850     default:
9851       return false;
9852     }
9853 }
9854
9855 /* Return true if KEYWORD names a storage class specifier.
9856
9857    RID_TYPEDEF is not included in this list despite `typedef' being
9858    listed in C99 6.7.1.1.  6.7.1.3 indicates that `typedef' is listed as
9859    such for syntactic convenience only.  */
9860
9861 bool
9862 keyword_is_storage_class_specifier (enum rid keyword)
9863 {
9864   switch (keyword)
9865     {
9866     case RID_STATIC:
9867     case RID_EXTERN:
9868     case RID_REGISTER:
9869     case RID_AUTO:
9870     case RID_MUTABLE:
9871     case RID_THREAD:
9872       return true;
9873     default:
9874       return false;
9875     }
9876 }
9877
9878 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec].  */
9879
9880 static bool
9881 keyword_is_function_specifier (enum rid keyword)
9882 {
9883   switch (keyword)
9884     {
9885     case RID_INLINE:
9886     case RID_NORETURN:
9887     case RID_VIRTUAL:
9888     case RID_EXPLICIT:
9889       return true;
9890     default:
9891       return false;
9892     }
9893 }
9894
9895 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
9896    declaration-specifier (C99 6.7).  */
9897
9898 bool
9899 keyword_is_decl_specifier (enum rid keyword)
9900 {
9901   if (keyword_is_storage_class_specifier (keyword)
9902       || keyword_is_type_qualifier (keyword)
9903       || keyword_is_function_specifier (keyword))
9904     return true;
9905
9906   switch (keyword)
9907     {
9908     case RID_TYPEDEF:
9909     case RID_FRIEND:
9910     case RID_CONSTEXPR:
9911       return true;
9912     default:
9913       return false;
9914     }
9915 }
9916
9917 /* Initialize language-specific-bits of tree_contains_struct.  */
9918
9919 void
9920 c_common_init_ts (void)
9921 {
9922   MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
9923   MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
9924 }
9925
9926 #include "gt-c-family-c-common.h"