OSDN Git Service

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