xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/c-family/c-common.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /* Subroutines shared by all languages that are variants of C.
2    Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #define GCC_C_COMMON_C
21 
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "target.h"
26 #include "function.h"
27 #include "tree.h"
28 #include "memmodel.h"
29 #include "c-common.h"
30 #include "gimple-expr.h"
31 #include "tm_p.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "diagnostic.h"
35 #include "intl.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "trans-mem.h"
41 #include "c-objc.h"
42 #include "common/common-target.h"
43 #include "langhooks.h"
44 #include "tree-inline.h"
45 #include "toplev.h"
46 #include "tree-iterator.h"
47 #include "opts.h"
48 #include "gimplify.h"
49 #include "substring-locations.h"
50 #include "spellcheck.h"
51 
52 cpp_reader *parse_in;		/* Declared in c-pragma.h.  */
53 
54 /* Mode used to build pointers (VOIDmode means ptr_mode).  */
55 
56 machine_mode c_default_pointer_mode = VOIDmode;
57 
58 /* The following symbols are subsumed in the c_global_trees array, and
59    listed here individually for documentation purposes.
60 
61    INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
62 
63 	tree short_integer_type_node;
64 	tree long_integer_type_node;
65 	tree long_long_integer_type_node;
66 
67 	tree short_unsigned_type_node;
68 	tree long_unsigned_type_node;
69 	tree long_long_unsigned_type_node;
70 
71 	tree truthvalue_type_node;
72 	tree truthvalue_false_node;
73 	tree truthvalue_true_node;
74 
75 	tree ptrdiff_type_node;
76 
77 	tree unsigned_char_type_node;
78 	tree signed_char_type_node;
79 	tree wchar_type_node;
80 
81 	tree char16_type_node;
82 	tree char32_type_node;
83 
84 	tree float_type_node;
85 	tree double_type_node;
86 	tree long_double_type_node;
87 
88 	tree complex_integer_type_node;
89 	tree complex_float_type_node;
90 	tree complex_double_type_node;
91 	tree complex_long_double_type_node;
92 
93 	tree dfloat32_type_node;
94 	tree dfloat64_type_node;
95 	tree_dfloat128_type_node;
96 
97 	tree intQI_type_node;
98 	tree intHI_type_node;
99 	tree intSI_type_node;
100 	tree intDI_type_node;
101 	tree intTI_type_node;
102 
103 	tree unsigned_intQI_type_node;
104 	tree unsigned_intHI_type_node;
105 	tree unsigned_intSI_type_node;
106 	tree unsigned_intDI_type_node;
107 	tree unsigned_intTI_type_node;
108 
109 	tree widest_integer_literal_type_node;
110 	tree widest_unsigned_literal_type_node;
111 
112    Nodes for types `void *' and `const void *'.
113 
114 	tree ptr_type_node, const_ptr_type_node;
115 
116    Nodes for types `char *' and `const char *'.
117 
118 	tree string_type_node, const_string_type_node;
119 
120    Type `char[SOMENUMBER]'.
121    Used when an array of char is needed and the size is irrelevant.
122 
123 	tree char_array_type_node;
124 
125    Type `wchar_t[SOMENUMBER]' or something like it.
126    Used when a wide string literal is created.
127 
128 	tree wchar_array_type_node;
129 
130    Type `char16_t[SOMENUMBER]' or something like it.
131    Used when a UTF-16 string literal is created.
132 
133 	tree char16_array_type_node;
134 
135    Type `char32_t[SOMENUMBER]' or something like it.
136    Used when a UTF-32 string literal is created.
137 
138 	tree char32_array_type_node;
139 
140    Type `int ()' -- used for implicit declaration of functions.
141 
142 	tree default_function_type;
143 
144    A VOID_TYPE node, packaged in a TREE_LIST.
145 
146 	tree void_list_node;
147 
148   The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
149   and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
150   VAR_DECLS, but C++ does.)
151 
152 	tree function_name_decl_node;
153 	tree pretty_function_name_decl_node;
154 	tree c99_function_name_decl_node;
155 
156   Stack of nested function name VAR_DECLs.
157 
158 	tree saved_function_name_decls;
159 
160 */
161 
162 tree c_global_trees[CTI_MAX];
163 
164 /* Switches common to the C front ends.  */
165 
166 /* Nonzero means don't output line number information.  */
167 
168 char flag_no_line_commands;
169 
170 /* Nonzero causes -E output not to be done, but directives such as
171    #define that have side effects are still obeyed.  */
172 
173 char flag_no_output;
174 
175 /* Nonzero means dump macros in some fashion.  */
176 
177 char flag_dump_macros;
178 
179 /* Nonzero means pass #include lines through to the output.  */
180 
181 char flag_dump_includes;
182 
183 /* Nonzero means process PCH files while preprocessing.  */
184 
185 bool flag_pch_preprocess;
186 
187 /* The file name to which we should write a precompiled header, or
188    NULL if no header will be written in this compile.  */
189 
190 const char *pch_file;
191 
192 /* Nonzero if an ISO standard was selected.  It rejects macros in the
193    user's namespace.  */
194 int flag_iso;
195 
196 /* C/ObjC language option variables.  */
197 
198 
199 /* Nonzero means allow type mismatches in conditional expressions;
200    just make their values `void'.  */
201 
202 int flag_cond_mismatch;
203 
204 /* Nonzero means enable C89 Amendment 1 features.  */
205 
206 int flag_isoc94;
207 
208 /* Nonzero means use the ISO C99 (or C11) dialect of C.  */
209 
210 int flag_isoc99;
211 
212 /* Nonzero means use the ISO C11 dialect of C.  */
213 
214 int flag_isoc11;
215 
216 /* Nonzero means that we have builtin functions, and main is an int.  */
217 
218 int flag_hosted = 1;
219 
220 
221 /* ObjC language option variables.  */
222 
223 
224 /* Tells the compiler that this is a special run.  Do not perform any
225    compiling, instead we are to test some platform dependent features
226    and output a C header file with appropriate definitions.  */
227 
228 int print_struct_values;
229 
230 /* Tells the compiler what is the constant string class for ObjC.  */
231 
232 const char *constant_string_class_name;
233 
234 
235 /* C++ language option variables.  */
236 
237 /* The reference version of the ABI for -Wabi.  */
238 
239 int warn_abi_version = -1;
240 
241 /* Nonzero means generate separate instantiation control files and
242    juggle them at link time.  */
243 
244 int flag_use_repository;
245 
246 /* The C++ dialect being used.  Default set in c_common_post_options.  */
247 
248 enum cxx_dialect cxx_dialect = cxx_unset;
249 
250 /* Maximum template instantiation depth.  This limit exists to limit the
251    time it takes to notice excessively recursive template instantiations.
252 
253    The default is lower than the 1024 recommended by the C++0x standard
254    because G++ runs out of stack before 1024 with highly recursive template
255    argument deduction substitution (g++.dg/cpp0x/enum11.C).  */
256 
257 int max_tinst_depth = 900;
258 
259 /* The elements of `ridpointers' are identifier nodes for the reserved
260    type names and storage classes.  It is indexed by a RID_... value.  */
261 tree *ridpointers;
262 
263 tree (*make_fname_decl) (location_t, tree, int);
264 
265 /* Nonzero means don't warn about problems that occur when the code is
266    executed.  */
267 int c_inhibit_evaluation_warnings;
268 
269 /* Whether we are building a boolean conversion inside
270    convert_for_assignment, or some other late binary operation.  If
271    build_binary_op is called for C (from code shared by C and C++) in
272    this case, then the operands have already been folded and the
273    result will not be folded again, so C_MAYBE_CONST_EXPR should not
274    be generated.  */
275 bool in_late_binary_op;
276 
277 /* Whether lexing has been completed, so subsequent preprocessor
278    errors should use the compiler's input_location.  */
279 bool done_lexing = false;
280 
281 /* Information about how a function name is generated.  */
282 struct fname_var_t
283 {
284   tree *const decl;	/* pointer to the VAR_DECL.  */
285   const unsigned rid;	/* RID number for the identifier.  */
286   const int pretty;	/* How pretty is it? */
287 };
288 
289 /* The three ways of getting then name of the current function.  */
290 
291 const struct fname_var_t fname_vars[] =
292 {
293   /* C99 compliant __func__, must be first.  */
294   {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
295   /* GCC __FUNCTION__ compliant.  */
296   {&function_name_decl_node, RID_FUNCTION_NAME, 0},
297   /* GCC __PRETTY_FUNCTION__ compliant.  */
298   {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
299   {NULL, 0, 0},
300 };
301 
302 /* Global visibility options.  */
303 struct visibility_flags visibility_options;
304 
305 static tree check_case_value (location_t, tree);
306 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
307 			       bool *);
308 
309 
310 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
311 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
312 static int resort_field_decl_cmp (const void *, const void *);
313 
314 /* Reserved words.  The third field is a mask: keywords are disabled
315    if they match the mask.
316 
317    Masks for languages:
318    C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
319    C --std=c99: D_CXXONLY | D_OBJC
320    ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
321    C++ --std=c++98: D_CONLY | D_CXX11 | D_OBJC
322    C++ --std=c++11: D_CONLY | D_OBJC
323    ObjC++ is like C++ except that D_OBJC is not set
324 
325    If -fno-asm is used, D_ASM is added to the mask.  If
326    -fno-gnu-keywords is used, D_EXT is added.  If -fno-asm and C in
327    C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
328    In C with -Wc++-compat, we warn if D_CXXWARN is set.
329 
330    Note the complication of the D_CXX_OBJC keywords.  These are
331    reserved words such as 'class'.  In C++, 'class' is a reserved
332    word.  In Objective-C++ it is too.  In Objective-C, it is a
333    reserved word too, but only if it follows an '@' sign.
334 */
335 const struct c_common_resword c_common_reswords[] =
336 {
337   { "_Alignas",		RID_ALIGNAS,   D_CONLY },
338   { "_Alignof",		RID_ALIGNOF,   D_CONLY },
339   { "_Atomic",		RID_ATOMIC,    D_CONLY },
340   { "_Bool",		RID_BOOL,      D_CONLY },
341   { "_Complex",		RID_COMPLEX,	0 },
342   { "_Cilk_spawn",      RID_CILK_SPAWN, 0 },
343   { "_Cilk_sync",       RID_CILK_SYNC,  0 },
344   { "_Cilk_for",        RID_CILK_FOR,   0 },
345   { "_Imaginary",	RID_IMAGINARY, D_CONLY },
346   { "_Float16",         RID_FLOAT16,   D_CONLY },
347   { "_Float32",         RID_FLOAT32,   D_CONLY },
348   { "_Float64",         RID_FLOAT64,   D_CONLY },
349   { "_Float128",        RID_FLOAT128,  D_CONLY },
350   { "_Float32x",        RID_FLOAT32X,  D_CONLY },
351   { "_Float64x",        RID_FLOAT64X,  D_CONLY },
352   { "_Float128x",       RID_FLOAT128X, D_CONLY },
353   { "_Decimal32",       RID_DFLOAT32,  D_CONLY | D_EXT },
354   { "_Decimal64",       RID_DFLOAT64,  D_CONLY | D_EXT },
355   { "_Decimal128",      RID_DFLOAT128, D_CONLY | D_EXT },
356   { "_Fract",           RID_FRACT,     D_CONLY | D_EXT },
357   { "_Accum",           RID_ACCUM,     D_CONLY | D_EXT },
358   { "_Sat",             RID_SAT,       D_CONLY | D_EXT },
359   { "_Static_assert",   RID_STATIC_ASSERT, D_CONLY },
360   { "_Noreturn",        RID_NORETURN,  D_CONLY },
361   { "_Generic",         RID_GENERIC,   D_CONLY },
362   { "_Thread_local",    RID_THREAD,    D_CONLY },
363   { "__FUNCTION__",	RID_FUNCTION_NAME, 0 },
364   { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
365   { "__alignof",	RID_ALIGNOF,	0 },
366   { "__alignof__",	RID_ALIGNOF,	0 },
367   { "__asm",		RID_ASM,	0 },
368   { "__asm__",		RID_ASM,	0 },
369   { "__attribute",	RID_ATTRIBUTE,	0 },
370   { "__attribute__",	RID_ATTRIBUTE,	0 },
371   { "__auto_type",	RID_AUTO_TYPE,	D_CONLY },
372   { "__bases",          RID_BASES, D_CXXONLY },
373   { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
374   { "__builtin_call_with_static_chain",
375     RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
376   { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
377   { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
378   { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
379   { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
380   { "__builtin_offsetof", RID_OFFSETOF, 0 },
381   { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
382   { "__builtin_va_arg",	RID_VA_ARG,	0 },
383   { "__complex",	RID_COMPLEX,	0 },
384   { "__complex__",	RID_COMPLEX,	0 },
385   { "__const",		RID_CONST,	0 },
386   { "__const__",	RID_CONST,	0 },
387   { "__decltype",       RID_DECLTYPE,   D_CXXONLY },
388   { "__direct_bases",   RID_DIRECT_BASES, D_CXXONLY },
389   { "__extension__",	RID_EXTENSION,	0 },
390   { "__func__",		RID_C99_FUNCTION_NAME, 0 },
391   { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
392   { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
393   { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
394   { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
395   { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
396   { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
397   { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
398   { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
399 					D_CXXONLY },
400   { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
401   { "__imag",		RID_IMAGPART,	0 },
402   { "__imag__",		RID_IMAGPART,	0 },
403   { "__inline",		RID_INLINE,	0 },
404   { "__inline__",	RID_INLINE,	0 },
405   { "__is_abstract",	RID_IS_ABSTRACT, D_CXXONLY },
406   { "__is_aggregate",	RID_IS_AGGREGATE, D_CXXONLY },
407   { "__is_base_of",	RID_IS_BASE_OF, D_CXXONLY },
408   { "__is_class",	RID_IS_CLASS,	D_CXXONLY },
409   { "__is_empty",	RID_IS_EMPTY,	D_CXXONLY },
410   { "__is_enum",	RID_IS_ENUM,	D_CXXONLY },
411   { "__is_final",	RID_IS_FINAL,	D_CXXONLY },
412   { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
413   { "__is_pod",		RID_IS_POD,	D_CXXONLY },
414   { "__is_polymorphic",	RID_IS_POLYMORPHIC, D_CXXONLY },
415   { "__is_same_as",     RID_IS_SAME_AS, D_CXXONLY },
416   { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
417   { "__is_trivial",     RID_IS_TRIVIAL, D_CXXONLY },
418   { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
419   { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
420   { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
421   { "__is_union",	RID_IS_UNION,	D_CXXONLY },
422   { "__label__",	RID_LABEL,	0 },
423   { "__null",		RID_NULL,	0 },
424   { "__real",		RID_REALPART,	0 },
425   { "__real__",		RID_REALPART,	0 },
426   { "__restrict",	RID_RESTRICT,	0 },
427   { "__restrict__",	RID_RESTRICT,	0 },
428   { "__signed",		RID_SIGNED,	0 },
429   { "__signed__",	RID_SIGNED,	0 },
430   { "__thread",		RID_THREAD,	0 },
431   { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
432   { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
433   { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
434   { "__typeof",		RID_TYPEOF,	0 },
435   { "__typeof__",	RID_TYPEOF,	0 },
436   { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
437   { "__volatile",	RID_VOLATILE,	0 },
438   { "__volatile__",	RID_VOLATILE,	0 },
439   { "__GIMPLE",		RID_GIMPLE,	D_CONLY },
440   { "__PHI",		RID_PHI,	D_CONLY },
441   { "__RTL",		RID_RTL,	D_CONLY },
442   { "alignas",		RID_ALIGNAS,	D_CXXONLY | D_CXX11 | D_CXXWARN },
443   { "alignof",		RID_ALIGNOF,	D_CXXONLY | D_CXX11 | D_CXXWARN },
444   { "asm",		RID_ASM,	D_ASM },
445   { "auto",		RID_AUTO,	0 },
446   { "bool",		RID_BOOL,	D_CXXONLY | D_CXXWARN },
447   { "break",		RID_BREAK,	0 },
448   { "case",		RID_CASE,	0 },
449   { "catch",		RID_CATCH,	D_CXX_OBJC | D_CXXWARN },
450   { "char",		RID_CHAR,	0 },
451   { "char16_t",		RID_CHAR16,	D_CXXONLY | D_CXX11 | D_CXXWARN },
452   { "char32_t",		RID_CHAR32,	D_CXXONLY | D_CXX11 | D_CXXWARN },
453   { "class",		RID_CLASS,	D_CXX_OBJC | D_CXXWARN },
454   { "const",		RID_CONST,	0 },
455   { "constexpr",	RID_CONSTEXPR,	D_CXXONLY | D_CXX11 | D_CXXWARN },
456   { "const_cast",	RID_CONSTCAST,	D_CXXONLY | D_CXXWARN },
457   { "continue",		RID_CONTINUE,	0 },
458   { "decltype",         RID_DECLTYPE,   D_CXXONLY | D_CXX11 | D_CXXWARN },
459   { "default",		RID_DEFAULT,	0 },
460   { "delete",		RID_DELETE,	D_CXXONLY | D_CXXWARN },
461   { "do",		RID_DO,		0 },
462   { "double",		RID_DOUBLE,	0 },
463   { "dynamic_cast",	RID_DYNCAST,	D_CXXONLY | D_CXXWARN },
464   { "else",		RID_ELSE,	0 },
465   { "enum",		RID_ENUM,	0 },
466   { "explicit",		RID_EXPLICIT,	D_CXXONLY | D_CXXWARN },
467   { "export",		RID_EXPORT,	D_CXXONLY | D_CXXWARN },
468   { "extern",		RID_EXTERN,	0 },
469   { "false",		RID_FALSE,	D_CXXONLY | D_CXXWARN },
470   { "float",		RID_FLOAT,	0 },
471   { "for",		RID_FOR,	0 },
472   { "friend",		RID_FRIEND,	D_CXXONLY | D_CXXWARN },
473   { "goto",		RID_GOTO,	0 },
474   { "if",		RID_IF,		0 },
475   { "inline",		RID_INLINE,	D_EXT89 },
476   { "int",		RID_INT,	0 },
477   { "long",		RID_LONG,	0 },
478   { "mutable",		RID_MUTABLE,	D_CXXONLY | D_CXXWARN },
479   { "namespace",	RID_NAMESPACE,	D_CXXONLY | D_CXXWARN },
480   { "new",		RID_NEW,	D_CXXONLY | D_CXXWARN },
481   { "noexcept",		RID_NOEXCEPT,	D_CXXONLY | D_CXX11 | D_CXXWARN },
482   { "nullptr",		RID_NULLPTR,	D_CXXONLY | D_CXX11 | D_CXXWARN },
483   { "operator",		RID_OPERATOR,	D_CXXONLY | D_CXXWARN },
484   { "private",		RID_PRIVATE,	D_CXX_OBJC | D_CXXWARN },
485   { "protected",	RID_PROTECTED,	D_CXX_OBJC | D_CXXWARN },
486   { "public",		RID_PUBLIC,	D_CXX_OBJC | D_CXXWARN },
487   { "register",		RID_REGISTER,	0 },
488   { "reinterpret_cast",	RID_REINTCAST,	D_CXXONLY | D_CXXWARN },
489   { "restrict",		RID_RESTRICT,	D_CONLY | D_C99 },
490   { "return",		RID_RETURN,	0 },
491   { "short",		RID_SHORT,	0 },
492   { "signed",		RID_SIGNED,	0 },
493   { "sizeof",		RID_SIZEOF,	0 },
494   { "static",		RID_STATIC,	0 },
495   { "static_assert",    RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
496   { "static_cast",	RID_STATCAST,	D_CXXONLY | D_CXXWARN },
497   { "struct",		RID_STRUCT,	0 },
498   { "switch",		RID_SWITCH,	0 },
499   { "template",		RID_TEMPLATE,	D_CXXONLY | D_CXXWARN },
500   { "this",		RID_THIS,	D_CXXONLY | D_CXXWARN },
501   { "thread_local",	RID_THREAD,	D_CXXONLY | D_CXX11 | D_CXXWARN },
502   { "throw",		RID_THROW,	D_CXX_OBJC | D_CXXWARN },
503   { "true",		RID_TRUE,	D_CXXONLY | D_CXXWARN },
504   { "try",		RID_TRY,	D_CXX_OBJC | D_CXXWARN },
505   { "typedef",		RID_TYPEDEF,	0 },
506   { "typename",		RID_TYPENAME,	D_CXXONLY | D_CXXWARN },
507   { "typeid",		RID_TYPEID,	D_CXXONLY | D_CXXWARN },
508   { "typeof",		RID_TYPEOF,	D_ASM | D_EXT },
509   { "union",		RID_UNION,	0 },
510   { "unsigned",		RID_UNSIGNED,	0 },
511   { "using",		RID_USING,	D_CXXONLY | D_CXXWARN },
512   { "virtual",		RID_VIRTUAL,	D_CXXONLY | D_CXXWARN },
513   { "void",		RID_VOID,	0 },
514   { "volatile",		RID_VOLATILE,	0 },
515   { "wchar_t",		RID_WCHAR,	D_CXXONLY },
516   { "while",		RID_WHILE,	0 },
517 
518   /* C++ transactional memory.  */
519   { "synchronized",	RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
520   { "atomic_noexcept",	RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
521   { "atomic_cancel",	RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
522   { "atomic_commit",	RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
523 
524   /* Concepts-related keywords */
525   { "concept",		RID_CONCEPT,	D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
526   { "requires", 	RID_REQUIRES,	D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
527 
528   /* These Objective-C keywords are recognized only immediately after
529      an '@'.  */
530   { "compatibility_alias", RID_AT_ALIAS,	D_OBJC },
531   { "defs",		RID_AT_DEFS,		D_OBJC },
532   { "encode",		RID_AT_ENCODE,		D_OBJC },
533   { "end",		RID_AT_END,		D_OBJC },
534   { "implementation",	RID_AT_IMPLEMENTATION,	D_OBJC },
535   { "interface",	RID_AT_INTERFACE,	D_OBJC },
536   { "protocol",		RID_AT_PROTOCOL,	D_OBJC },
537   { "selector",		RID_AT_SELECTOR,	D_OBJC },
538   { "finally",		RID_AT_FINALLY,		D_OBJC },
539   { "optional",		RID_AT_OPTIONAL,	D_OBJC },
540   { "required",		RID_AT_REQUIRED,	D_OBJC },
541   { "property",		RID_AT_PROPERTY,	D_OBJC },
542   { "package",		RID_AT_PACKAGE,		D_OBJC },
543   { "synthesize",	RID_AT_SYNTHESIZE,	D_OBJC },
544   { "dynamic",		RID_AT_DYNAMIC,		D_OBJC },
545   /* These are recognized only in protocol-qualifier context
546      (see above) */
547   { "bycopy",		RID_BYCOPY,		D_OBJC },
548   { "byref",		RID_BYREF,		D_OBJC },
549   { "in",		RID_IN,			D_OBJC },
550   { "inout",		RID_INOUT,		D_OBJC },
551   { "oneway",		RID_ONEWAY,		D_OBJC },
552   { "out",		RID_OUT,		D_OBJC },
553   /* These are recognized inside a property attribute list */
554   { "assign",	        RID_ASSIGN,		D_OBJC },
555   { "copy",	        RID_COPY,		D_OBJC },
556   { "getter",		RID_GETTER,		D_OBJC },
557   { "nonatomic",	RID_NONATOMIC,		D_OBJC },
558   { "readonly",		RID_READONLY,		D_OBJC },
559   { "readwrite",	RID_READWRITE,		D_OBJC },
560   { "retain",	        RID_RETAIN,		D_OBJC },
561   { "setter",		RID_SETTER,		D_OBJC },
562 };
563 
564 const unsigned int num_c_common_reswords =
565   sizeof c_common_reswords / sizeof (struct c_common_resword);
566 
567 /* Return identifier for address space AS.  */
568 
569 const char *
570 c_addr_space_name (addr_space_t as)
571 {
572   int rid = RID_FIRST_ADDR_SPACE + as;
573   gcc_assert (ridpointers [rid]);
574   return IDENTIFIER_POINTER (ridpointers [rid]);
575 }
576 
577 /* Push current bindings for the function name VAR_DECLS.  */
578 
579 void
580 start_fname_decls (void)
581 {
582   unsigned ix;
583   tree saved = NULL_TREE;
584 
585   for (ix = 0; fname_vars[ix].decl; ix++)
586     {
587       tree decl = *fname_vars[ix].decl;
588 
589       if (decl)
590 	{
591 	  saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
592 			     saved);
593 	  *fname_vars[ix].decl = NULL_TREE;
594 	}
595     }
596   if (saved || saved_function_name_decls)
597     /* Normally they'll have been NULL, so only push if we've got a
598        stack, or they are non-NULL.  */
599     saved_function_name_decls = tree_cons (saved, NULL_TREE,
600 					   saved_function_name_decls);
601 }
602 
603 /* Finish up the current bindings, adding them into the current function's
604    statement tree.  This must be done _before_ finish_stmt_tree is called.
605    If there is no current function, we must be at file scope and no statements
606    are involved. Pop the previous bindings.  */
607 
608 void
609 finish_fname_decls (void)
610 {
611   unsigned ix;
612   tree stmts = NULL_TREE;
613   tree stack = saved_function_name_decls;
614 
615   for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
616     append_to_statement_list (TREE_VALUE (stack), &stmts);
617 
618   if (stmts)
619     {
620       tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
621 
622       if (TREE_CODE (*bodyp) == BIND_EXPR)
623 	bodyp = &BIND_EXPR_BODY (*bodyp);
624 
625       append_to_statement_list_force (*bodyp, &stmts);
626       *bodyp = stmts;
627     }
628 
629   for (ix = 0; fname_vars[ix].decl; ix++)
630     *fname_vars[ix].decl = NULL_TREE;
631 
632   if (stack)
633     {
634       /* We had saved values, restore them.  */
635       tree saved;
636 
637       for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
638 	{
639 	  tree decl = TREE_PURPOSE (saved);
640 	  unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
641 
642 	  *fname_vars[ix].decl = decl;
643 	}
644       stack = TREE_CHAIN (stack);
645     }
646   saved_function_name_decls = stack;
647 }
648 
649 /* Return the text name of the current function, suitably prettified
650    by PRETTY_P.  Return string must be freed by caller.  */
651 
652 const char *
653 fname_as_string (int pretty_p)
654 {
655   const char *name = "top level";
656   char *namep;
657   int vrb = 2, len;
658   cpp_string cstr = { 0, 0 }, strname;
659 
660   if (!pretty_p)
661     {
662       name = "";
663       vrb = 0;
664     }
665 
666   if (current_function_decl)
667     name = lang_hooks.decl_printable_name (current_function_decl, vrb);
668 
669   len = strlen (name) + 3; /* Two for '"'s.  One for NULL.  */
670 
671   namep = XNEWVEC (char, len);
672   snprintf (namep, len, "\"%s\"", name);
673   strname.text = (unsigned char *) namep;
674   strname.len = len - 1;
675 
676   if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
677     {
678       XDELETEVEC (namep);
679       return (const char *) cstr.text;
680     }
681 
682   return namep;
683 }
684 
685 /* Return the VAR_DECL for a const char array naming the current
686    function. If the VAR_DECL has not yet been created, create it
687    now. RID indicates how it should be formatted and IDENTIFIER_NODE
688    ID is its name (unfortunately C and C++ hold the RID values of
689    keywords in different places, so we can't derive RID from ID in
690    this language independent code. LOC is the location of the
691    function.  */
692 
693 tree
694 fname_decl (location_t loc, unsigned int rid, tree id)
695 {
696   unsigned ix;
697   tree decl = NULL_TREE;
698 
699   for (ix = 0; fname_vars[ix].decl; ix++)
700     if (fname_vars[ix].rid == rid)
701       break;
702 
703   decl = *fname_vars[ix].decl;
704   if (!decl)
705     {
706       /* If a tree is built here, it would normally have the lineno of
707 	 the current statement.  Later this tree will be moved to the
708 	 beginning of the function and this line number will be wrong.
709 	 To avoid this problem set the lineno to 0 here; that prevents
710 	 it from appearing in the RTL.  */
711       tree stmts;
712       location_t saved_location = input_location;
713       input_location = UNKNOWN_LOCATION;
714 
715       stmts = push_stmt_list ();
716       decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
717       stmts = pop_stmt_list (stmts);
718       if (!IS_EMPTY_STMT (stmts))
719 	saved_function_name_decls
720 	  = tree_cons (decl, stmts, saved_function_name_decls);
721       *fname_vars[ix].decl = decl;
722       input_location = saved_location;
723     }
724   if (!ix && !current_function_decl)
725     pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
726 
727   return decl;
728 }
729 
730 /* Given a STRING_CST, give it a suitable array-of-chars data type.  */
731 
732 tree
733 fix_string_type (tree value)
734 {
735   int length = TREE_STRING_LENGTH (value);
736   int nchars;
737   tree e_type, i_type, a_type;
738 
739   /* Compute the number of elements, for the array type.  */
740   if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
741     {
742       nchars = length;
743       e_type = char_type_node;
744     }
745   else if (TREE_TYPE (value) == char16_array_type_node)
746     {
747       nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
748       e_type = char16_type_node;
749     }
750   else if (TREE_TYPE (value) == char32_array_type_node)
751     {
752       nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
753       e_type = char32_type_node;
754     }
755   else
756     {
757       nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
758       e_type = wchar_type_node;
759     }
760 
761   /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits).  The analogous
762      limit in C++98 Annex B is very large (65536) and is not normative,
763      so we do not diagnose it (warn_overlength_strings is forced off
764      in c_common_post_options).  */
765   if (warn_overlength_strings)
766     {
767       const int nchars_max = flag_isoc99 ? 4095 : 509;
768       const int relevant_std = flag_isoc99 ? 99 : 90;
769       if (nchars - 1 > nchars_max)
770 	/* Translators: The %d after 'ISO C' will be 90 or 99.  Do not
771 	   separate the %d from the 'C'.  'ISO' should not be
772 	   translated, but it may be moved after 'C%d' in languages
773 	   where modifiers follow nouns.  */
774 	pedwarn (input_location, OPT_Woverlength_strings,
775 		 "string length %qd is greater than the length %qd "
776 		 "ISO C%d compilers are required to support",
777 		 nchars - 1, nchars_max, relevant_std);
778     }
779 
780   /* Create the array type for the string constant.  The ISO C++
781      standard says that a string literal has type `const char[N]' or
782      `const wchar_t[N]'.  We use the same logic when invoked as a C
783      front-end with -Wwrite-strings.
784      ??? We should change the type of an expression depending on the
785      state of a warning flag.  We should just be warning -- see how
786      this is handled in the C++ front-end for the deprecated implicit
787      conversion from string literals to `char*' or `wchar_t*'.
788 
789      The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
790      array type being the unqualified version of that type.
791      Therefore, if we are constructing an array of const char, we must
792      construct the matching unqualified array type first.  The C front
793      end does not require this, but it does no harm, so we do it
794      unconditionally.  */
795   i_type = build_index_type (size_int (nchars - 1));
796   a_type = build_array_type (e_type, i_type);
797   if (c_dialect_cxx() || warn_write_strings)
798     a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
799 
800   TREE_TYPE (value) = a_type;
801   TREE_CONSTANT (value) = 1;
802   TREE_READONLY (value) = 1;
803   TREE_STATIC (value) = 1;
804   return value;
805 }
806 
807 /* Given a string of type STRING_TYPE, determine what kind of string
808    token would give an equivalent execution encoding: CPP_STRING,
809    CPP_STRING16, or CPP_STRING32.  Return CPP_OTHER in case of error.
810    This may not be exactly the string token type that initially created
811    the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
812    string type at this point.
813 
814    This effectively reverses part of the logic in lex_string and
815    fix_string_type.  */
816 
817 static enum cpp_ttype
818 get_cpp_ttype_from_string_type (tree string_type)
819 {
820   gcc_assert (string_type);
821   if (TREE_CODE (string_type) == POINTER_TYPE)
822     string_type = TREE_TYPE (string_type);
823 
824   if (TREE_CODE (string_type) != ARRAY_TYPE)
825     return CPP_OTHER;
826 
827   tree element_type = TREE_TYPE (string_type);
828   if (TREE_CODE (element_type) != INTEGER_TYPE)
829     return CPP_OTHER;
830 
831   int bits_per_character = TYPE_PRECISION (element_type);
832   switch (bits_per_character)
833     {
834     case 8:
835       return CPP_STRING;  /* It could have also been CPP_UTF8STRING.  */
836     case 16:
837       return CPP_STRING16;
838     case 32:
839       return CPP_STRING32;
840     }
841 
842   return CPP_OTHER;
843 }
844 
845 /* The global record of string concatentations, for use in
846    extracting locations within string literals.  */
847 
848 GTY(()) string_concat_db *g_string_concat_db;
849 
850 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION.  */
851 
852 const char *
853 c_get_substring_location (const substring_loc &substr_loc,
854 			  location_t *out_loc)
855 {
856   enum cpp_ttype tok_type
857     = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
858   if (tok_type == CPP_OTHER)
859     return "unrecognized string type";
860 
861   return get_source_location_for_substring (parse_in, g_string_concat_db,
862 					    substr_loc.get_fmt_string_loc (),
863 					    tok_type,
864 					    substr_loc.get_caret_idx (),
865 					    substr_loc.get_start_idx (),
866 					    substr_loc.get_end_idx (),
867 					    out_loc);
868 }
869 
870 
871 /* Fold X for consideration by one of the warning functions when checking
872    whether an expression has a constant value.  */
873 
874 tree
875 fold_for_warn (tree x)
876 {
877   if (c_dialect_cxx ())
878     return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
879   else
880     /* The C front-end has already folded X appropriately.  */
881     return x;
882 }
883 
884 /* Return true iff T is a boolean promoted to int.  */
885 
886 bool
887 bool_promoted_to_int_p (tree t)
888 {
889   return (CONVERT_EXPR_P (t)
890 	  && TREE_TYPE (t) == integer_type_node
891 	  && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
892 }
893 
894 /* vector_targets_convertible_p is used for vector pointer types.  The
895    callers perform various checks that the qualifiers are satisfactory,
896    while OTOH vector_targets_convertible_p ignores the number of elements
897    in the vectors.  That's fine with vector pointers as we can consider,
898    say, a vector of 8 elements as two consecutive vectors of 4 elements,
899    and that does not require and conversion of the pointer values.
900    In contrast, vector_types_convertible_p and
901    vector_types_compatible_elements_p are used for vector value types.  */
902 /* True if pointers to distinct types T1 and T2 can be converted to
903    each other without an explicit cast.  Only returns true for opaque
904    vector types.  */
905 bool
906 vector_targets_convertible_p (const_tree t1, const_tree t2)
907 {
908   if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
909       && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
910       && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
911     return true;
912 
913   return false;
914 }
915 
916 /* vector_types_convertible_p is used for vector value types.
917    It could in principle call vector_targets_convertible_p as a subroutine,
918    but then the check for vector type would be duplicated with its callers,
919    and also the purpose of vector_targets_convertible_p would become
920    muddled.
921    Where vector_types_convertible_p returns true, a conversion might still be
922    needed to make the types match.
923    In contrast, vector_targets_convertible_p is used for vector pointer
924    values, and vector_types_compatible_elements_p is used specifically
925    in the context for binary operators, as a check if use is possible without
926    conversion.  */
927 /* True if vector types T1 and T2 can be converted to each other
928    without an explicit cast.  If EMIT_LAX_NOTE is true, and T1 and T2
929    can only be converted with -flax-vector-conversions yet that is not
930    in effect, emit a note telling the user about that option if such
931    a note has not previously been emitted.  */
932 bool
933 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
934 {
935   static bool emitted_lax_note = false;
936   bool convertible_lax;
937 
938   if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
939       && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
940     return true;
941 
942   convertible_lax =
943     (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
944      && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
945 	 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
946      && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
947 	 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
948 
949   if (!convertible_lax || flag_lax_vector_conversions)
950     return convertible_lax;
951 
952   if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
953       && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
954     return true;
955 
956   if (emit_lax_note && !emitted_lax_note)
957     {
958       emitted_lax_note = true;
959       inform (input_location, "use -flax-vector-conversions to permit "
960               "conversions between vectors with differing "
961               "element types or numbers of subparts");
962     }
963 
964   return false;
965 }
966 
967 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
968    and have vector types, V0 has the same type as V1, and the number of
969    elements of V0, V1, MASK is the same.
970 
971    In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
972    called with two arguments.  In this case implementation passes the
973    first argument twice in order to share the same tree code.  This fact
974    could enable the mask-values being twice the vector length.  This is
975    an implementation accident and this semantics is not guaranteed to
976    the user.  */
977 tree
978 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
979 		       bool complain)
980 {
981   tree ret;
982   bool wrap = true;
983   bool maybe_const = false;
984   bool two_arguments = false;
985 
986   if (v1 == NULL_TREE)
987     {
988       two_arguments = true;
989       v1 = v0;
990     }
991 
992   if (v0 == error_mark_node || v1 == error_mark_node
993       || mask == error_mark_node)
994     return error_mark_node;
995 
996   if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
997     {
998       if (complain)
999 	error_at (loc, "__builtin_shuffle last argument must "
1000 		       "be an integer vector");
1001       return error_mark_node;
1002     }
1003 
1004   if (!VECTOR_TYPE_P (TREE_TYPE (v0))
1005       || !VECTOR_TYPE_P (TREE_TYPE (v1)))
1006     {
1007       if (complain)
1008 	error_at (loc, "__builtin_shuffle arguments must be vectors");
1009       return error_mark_node;
1010     }
1011 
1012   if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1013     {
1014       if (complain)
1015 	error_at (loc, "__builtin_shuffle argument vectors must be of "
1016 		       "the same type");
1017       return error_mark_node;
1018     }
1019 
1020   if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
1021       != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
1022       && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
1023 	 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1024     {
1025       if (complain)
1026 	error_at (loc, "__builtin_shuffle number of elements of the "
1027 		       "argument vector(s) and the mask vector should "
1028 		       "be the same");
1029       return error_mark_node;
1030     }
1031 
1032   if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1033       != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1034     {
1035       if (complain)
1036 	error_at (loc, "__builtin_shuffle argument vector(s) inner type "
1037 		       "must have the same size as inner type of the mask");
1038       return error_mark_node;
1039     }
1040 
1041   if (!c_dialect_cxx ())
1042     {
1043       /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR.  */
1044       v0 = c_fully_fold (v0, false, &maybe_const);
1045       wrap &= maybe_const;
1046 
1047       if (two_arguments)
1048         v1 = v0 = save_expr (v0);
1049       else
1050         {
1051           v1 = c_fully_fold (v1, false, &maybe_const);
1052           wrap &= maybe_const;
1053         }
1054 
1055       mask = c_fully_fold (mask, false, &maybe_const);
1056       wrap &= maybe_const;
1057     }
1058   else if (two_arguments)
1059     v1 = v0 = save_expr (v0);
1060 
1061   ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1062 
1063   if (!c_dialect_cxx () && !wrap)
1064     ret = c_wrap_maybe_const (ret, true);
1065 
1066   return ret;
1067 }
1068 
1069 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1070    to integral type.  */
1071 
1072 tree
1073 c_common_get_narrower (tree op, int *unsignedp_ptr)
1074 {
1075   op = get_narrower (op, unsignedp_ptr);
1076 
1077   if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1078       && ENUM_IS_SCOPED (TREE_TYPE (op)))
1079     {
1080       /* C++0x scoped enumerations don't implicitly convert to integral
1081 	 type; if we stripped an explicit conversion to a larger type we
1082 	 need to replace it so common_type will still work.  */
1083       tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1084 					  TYPE_UNSIGNED (TREE_TYPE (op)));
1085       op = fold_convert (type, op);
1086     }
1087   return op;
1088 }
1089 
1090 /* This is a helper function of build_binary_op.
1091 
1092    For certain operations if both args were extended from the same
1093    smaller type, do the arithmetic in that type and then extend.
1094 
1095    BITWISE indicates a bitwise operation.
1096    For them, this optimization is safe only if
1097    both args are zero-extended or both are sign-extended.
1098    Otherwise, we might change the result.
1099    Eg, (short)-1 | (unsigned short)-1 is (int)-1
1100    but calculated in (unsigned short) it would be (unsigned short)-1.
1101 */
1102 tree
1103 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1104 {
1105   int unsigned0, unsigned1;
1106   tree arg0, arg1;
1107   int uns;
1108   tree type;
1109 
1110   /* Cast OP0 and OP1 to RESULT_TYPE.  Doing so prevents
1111      excessive narrowing when we call get_narrower below.  For
1112      example, suppose that OP0 is of unsigned int extended
1113      from signed char and that RESULT_TYPE is long long int.
1114      If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1115      like
1116 
1117      (long long int) (unsigned int) signed_char
1118 
1119      which get_narrower would narrow down to
1120 
1121      (unsigned int) signed char
1122 
1123      If we do not cast OP0 first, get_narrower would return
1124      signed_char, which is inconsistent with the case of the
1125      explicit cast.  */
1126   op0 = convert (result_type, op0);
1127   op1 = convert (result_type, op1);
1128 
1129   arg0 = c_common_get_narrower (op0, &unsigned0);
1130   arg1 = c_common_get_narrower (op1, &unsigned1);
1131 
1132   /* UNS is 1 if the operation to be done is an unsigned one.  */
1133   uns = TYPE_UNSIGNED (result_type);
1134 
1135   /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1136      but it *requires* conversion to FINAL_TYPE.  */
1137 
1138   if ((TYPE_PRECISION (TREE_TYPE (op0))
1139        == TYPE_PRECISION (TREE_TYPE (arg0)))
1140       && TREE_TYPE (op0) != result_type)
1141     unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1142   if ((TYPE_PRECISION (TREE_TYPE (op1))
1143        == TYPE_PRECISION (TREE_TYPE (arg1)))
1144       && TREE_TYPE (op1) != result_type)
1145     unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1146 
1147   /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE.  */
1148 
1149   /* For bitwise operations, signedness of nominal type
1150      does not matter.  Consider only how operands were extended.  */
1151   if (bitwise)
1152     uns = unsigned0;
1153 
1154   /* Note that in all three cases below we refrain from optimizing
1155      an unsigned operation on sign-extended args.
1156      That would not be valid.  */
1157 
1158   /* Both args variable: if both extended in same way
1159      from same width, do it in that width.
1160      Do it unsigned if args were zero-extended.  */
1161   if ((TYPE_PRECISION (TREE_TYPE (arg0))
1162        < TYPE_PRECISION (result_type))
1163       && (TYPE_PRECISION (TREE_TYPE (arg1))
1164 	  == TYPE_PRECISION (TREE_TYPE (arg0)))
1165       && unsigned0 == unsigned1
1166       && (unsigned0 || !uns))
1167     return c_common_signed_or_unsigned_type
1168       (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1169 
1170   else if (TREE_CODE (arg0) == INTEGER_CST
1171 	   && (unsigned1 || !uns)
1172 	   && (TYPE_PRECISION (TREE_TYPE (arg1))
1173 	       < TYPE_PRECISION (result_type))
1174 	   && (type
1175 	       = c_common_signed_or_unsigned_type (unsigned1,
1176 						   TREE_TYPE (arg1)))
1177 	   && !POINTER_TYPE_P (type)
1178 	   && int_fits_type_p (arg0, type))
1179     return type;
1180 
1181   else if (TREE_CODE (arg1) == INTEGER_CST
1182 	   && (unsigned0 || !uns)
1183 	   && (TYPE_PRECISION (TREE_TYPE (arg0))
1184 	       < TYPE_PRECISION (result_type))
1185 	   && (type
1186 	       = c_common_signed_or_unsigned_type (unsigned0,
1187 						   TREE_TYPE (arg0)))
1188 	   && !POINTER_TYPE_P (type)
1189 	   && int_fits_type_p (arg1, type))
1190     return type;
1191 
1192   return result_type;
1193 }
1194 
1195 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1196    real of type TO_TYPE.  This is a helper function for unsafe_conversion_p.  */
1197 
1198 static bool
1199 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1200 {
1201   tree type_low_bound = TYPE_MIN_VALUE (from_type);
1202   tree type_high_bound = TYPE_MAX_VALUE (from_type);
1203   REAL_VALUE_TYPE real_low_bound =
1204 	  real_value_from_int_cst (0, type_low_bound);
1205   REAL_VALUE_TYPE real_high_bound =
1206 	  real_value_from_int_cst (0, type_high_bound);
1207 
1208   return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1209 	 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1210 }
1211 
1212 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1213    to the complex/real/integer type TYPE.  Function returns non-zero when:
1214 	* EXPR is a constant which cannot be exactly converted to TYPE.
1215 	* EXPR is not a constant and size of EXPR's type > than size of TYPE,
1216 	  for EXPR type and TYPE being both integers or both real, or both
1217 	  complex.
1218 	* EXPR is not a constant of complex type and TYPE is a real or
1219 	  an integer.
1220 	* EXPR is not a constant of real type and TYPE is an integer.
1221 	* EXPR is not a constant of integer type which cannot be
1222 	  exactly converted to real type.
1223 
1224    Function allows conversions between types of different signedness and
1225    can return SAFE_CONVERSION (zero) in that case.  Function can produce
1226    signedness warnings if PRODUCE_WARNS is true.
1227 
1228    Function allows conversions from complex constants to non-complex types,
1229    provided that imaginary part is zero and real part can be safely converted
1230    to TYPE.  */
1231 
1232 enum conversion_safety
1233 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
1234 {
1235   enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1236   tree expr_type = TREE_TYPE (expr);
1237   loc = expansion_point_location_if_in_system_header (loc);
1238 
1239   if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1240     {
1241       /* If type is complex, we are interested in compatibility with
1242 	 underlying type.  */
1243       if (TREE_CODE (type) == COMPLEX_TYPE)
1244 	  type = TREE_TYPE (type);
1245 
1246       /* Warn for real constant that is not an exact integer converted
1247 	 to integer type.  */
1248       if (TREE_CODE (expr_type) == REAL_TYPE
1249 	  && TREE_CODE (type) == INTEGER_TYPE)
1250 	{
1251 	  if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1252 	    give_warning = UNSAFE_REAL;
1253 	}
1254       /* Warn for an integer constant that does not fit into integer type.  */
1255       else if (TREE_CODE (expr_type) == INTEGER_TYPE
1256 	       && TREE_CODE (type) == INTEGER_TYPE
1257 	       && !int_fits_type_p (expr, type))
1258 	{
1259 	  if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1260 	      && tree_int_cst_sgn (expr) < 0)
1261 	    {
1262 	      if (produce_warns)
1263 		warning_at (loc, OPT_Wsign_conversion, "negative integer"
1264 			    " implicitly converted to unsigned type");
1265 	    }
1266 	  else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1267 	    {
1268 	      if (produce_warns)
1269 		warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
1270 			    " constant value to negative integer");
1271 	    }
1272 	  else
1273 	    give_warning = UNSAFE_OTHER;
1274 	}
1275       else if (TREE_CODE (type) == REAL_TYPE)
1276 	{
1277 	  /* Warn for an integer constant that does not fit into real type.  */
1278 	  if (TREE_CODE (expr_type) == INTEGER_TYPE)
1279 	    {
1280 	      REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1281 	      if (!exact_real_truncate (TYPE_MODE (type), &a))
1282 		give_warning = UNSAFE_REAL;
1283 	    }
1284 	  /* Warn for a real constant that does not fit into a smaller
1285 	     real type.  */
1286 	  else if (TREE_CODE (expr_type) == REAL_TYPE
1287 		   && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1288 	    {
1289 	      REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1290 	      if (!exact_real_truncate (TYPE_MODE (type), &a))
1291 		give_warning = UNSAFE_REAL;
1292 	    }
1293 	}
1294     }
1295 
1296   else if (TREE_CODE (expr) == COMPLEX_CST)
1297     {
1298       tree imag_part = TREE_IMAGPART (expr);
1299       /* Conversion from complex constant with zero imaginary part,
1300 	 perform check for conversion of real part.  */
1301       if ((TREE_CODE (imag_part) == REAL_CST
1302 	   && real_zerop (imag_part))
1303 	  || (TREE_CODE (imag_part) == INTEGER_CST
1304 	      && integer_zerop (imag_part)))
1305 	/* Note: in this branch we use recursive call to unsafe_conversion_p
1306 	   with different type of EXPR, but it is still safe, because when EXPR
1307 	   is a constant, it's type is not used in text of generated warnings
1308 	   (otherwise they could sound misleading).  */
1309 	return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
1310 				    produce_warns);
1311       /* Conversion from complex constant with non-zero imaginary part.  */
1312       else
1313 	{
1314 	  /* Conversion to complex type.
1315 	     Perform checks for both real and imaginary parts.  */
1316 	  if (TREE_CODE (type) == COMPLEX_TYPE)
1317 	    {
1318 	      /* Unfortunately, produce_warns must be false in two subsequent
1319 		 calls of unsafe_conversion_p, because otherwise we could
1320 		 produce strange "double" warnings, if both real and imaginary
1321 		 parts have conversion problems related to signedness.
1322 
1323 		 For example:
1324 		 int32_t _Complex a = 0x80000000 + 0x80000000i;
1325 
1326 		 Possible solution: add a separate function for checking
1327 		 constants and combine result of two calls appropriately.  */
1328 	      enum conversion_safety re_safety =
1329 		  unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
1330 	      enum conversion_safety im_safety =
1331 		  unsafe_conversion_p (loc, type, imag_part, false);
1332 
1333 	      /* Merge the results into appropriate single warning.  */
1334 
1335 	      /* Note: this case includes SAFE_CONVERSION, i.e. success.  */
1336 	      if (re_safety == im_safety)
1337 		give_warning = re_safety;
1338 	      else if (!re_safety && im_safety)
1339 		give_warning = im_safety;
1340 	      else if (re_safety && !im_safety)
1341 		give_warning = re_safety;
1342 	      else
1343 		give_warning = UNSAFE_OTHER;
1344 	    }
1345 	  /* Warn about conversion from complex to real or integer type.  */
1346 	  else
1347 	    give_warning = UNSAFE_IMAGINARY;
1348 	}
1349     }
1350 
1351   /* Checks for remaining case: EXPR is not constant.  */
1352   else
1353     {
1354       /* Warn for real types converted to integer types.  */
1355       if (TREE_CODE (expr_type) == REAL_TYPE
1356 	  && TREE_CODE (type) == INTEGER_TYPE)
1357 	give_warning = UNSAFE_REAL;
1358 
1359       else if (TREE_CODE (expr_type) == INTEGER_TYPE
1360 	       && TREE_CODE (type) == INTEGER_TYPE)
1361 	{
1362 	  /* Don't warn about unsigned char y = 0xff, x = (int) y;  */
1363 	  expr = get_unwidened (expr, 0);
1364 	  expr_type = TREE_TYPE (expr);
1365 
1366 	  /* Don't warn for short y; short x = ((int)y & 0xff);  */
1367 	  if (TREE_CODE (expr) == BIT_AND_EXPR
1368 	      || TREE_CODE (expr) == BIT_IOR_EXPR
1369 	      || TREE_CODE (expr) == BIT_XOR_EXPR)
1370 	    {
1371 	      /* If both args were extended from a shortest type,
1372 		 use that type if that is safe.  */
1373 	      expr_type = shorten_binary_op (expr_type,
1374 					     TREE_OPERAND (expr, 0),
1375 					     TREE_OPERAND (expr, 1),
1376 					     /* bitwise */1);
1377 
1378 	      if (TREE_CODE (expr) == BIT_AND_EXPR)
1379 		{
1380 		  tree op0 = TREE_OPERAND (expr, 0);
1381 		  tree op1 = TREE_OPERAND (expr, 1);
1382 		  bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1383 		  bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1384 
1385 		  /* If one of the operands is a non-negative constant
1386 		     that fits in the target type, then the type of the
1387 		     other operand does not matter. */
1388 		  if ((TREE_CODE (op0) == INTEGER_CST
1389 		       && int_fits_type_p (op0, c_common_signed_type (type))
1390 		       && int_fits_type_p (op0, c_common_unsigned_type (type)))
1391 		      || (TREE_CODE (op1) == INTEGER_CST
1392 			  && int_fits_type_p (op1, c_common_signed_type (type))
1393 			  && int_fits_type_p (op1,
1394 					      c_common_unsigned_type (type))))
1395 		    return SAFE_CONVERSION;
1396 		  /* If constant is unsigned and fits in the target
1397 		     type, then the result will also fit.  */
1398 		  else if ((TREE_CODE (op0) == INTEGER_CST
1399 			    && unsigned0
1400 			    && int_fits_type_p (op0, type))
1401 			   || (TREE_CODE (op1) == INTEGER_CST
1402 			       && unsigned1
1403 			       && int_fits_type_p (op1, type)))
1404 		    return SAFE_CONVERSION;
1405 		}
1406 	    }
1407 	  /* Warn for integer types converted to smaller integer types.  */
1408 	  if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1409 	    give_warning = UNSAFE_OTHER;
1410 
1411 	  /* When they are the same width but different signedness,
1412 	     then the value may change.  */
1413 	  else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1414 		    && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1415 		   /* Even when converted to a bigger type, if the type is
1416 		      unsigned but expr is signed, then negative values
1417 		      will be changed.  */
1418 		    || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1419 		   && produce_warns)
1420 	    warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
1421 			"may change the sign of the result",
1422 			type, expr_type);
1423 	}
1424 
1425       /* Warn for integer types converted to real types if and only if
1426 	 all the range of values of the integer type cannot be
1427 	 represented by the real type.  */
1428       else if (TREE_CODE (expr_type) == INTEGER_TYPE
1429 	       && TREE_CODE (type) == REAL_TYPE)
1430 	{
1431 	  /* Don't warn about char y = 0xff; float x = (int) y;  */
1432 	  expr = get_unwidened (expr, 0);
1433 	  expr_type = TREE_TYPE (expr);
1434 
1435 	  if (!int_safely_convertible_to_real_p (expr_type, type))
1436 	    give_warning = UNSAFE_OTHER;
1437 	}
1438 
1439       /* Warn for real types converted to smaller real types.  */
1440       else if (TREE_CODE (expr_type) == REAL_TYPE
1441 	       && TREE_CODE (type) == REAL_TYPE
1442 	       && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1443 	give_warning = UNSAFE_REAL;
1444 
1445       /* Check conversion between two complex types.  */
1446       else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1447 	       && TREE_CODE (type) == COMPLEX_TYPE)
1448 	{
1449 	  /* Extract underlying types (i.e., type of real and imaginary
1450 	     parts) of expr_type and type.  */
1451 	  tree from_type = TREE_TYPE (expr_type);
1452 	  tree to_type = TREE_TYPE (type);
1453 
1454 	  /* Warn for real types converted to integer types.  */
1455 	  if (TREE_CODE (from_type) == REAL_TYPE
1456 	      && TREE_CODE (to_type) == INTEGER_TYPE)
1457 	    give_warning = UNSAFE_REAL;
1458 
1459 	  /* Warn for real types converted to smaller real types.  */
1460 	  else if (TREE_CODE (from_type) == REAL_TYPE
1461 		   && TREE_CODE (to_type) == REAL_TYPE
1462 		   && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1463 	    give_warning = UNSAFE_REAL;
1464 
1465 	  /* Check conversion for complex integer types.  Here implementation
1466 	     is simpler than for real-domain integers because it does not
1467 	     involve sophisticated cases, such as bitmasks, casts, etc.  */
1468 	  else if (TREE_CODE (from_type) == INTEGER_TYPE
1469 		   && TREE_CODE (to_type) == INTEGER_TYPE)
1470 	    {
1471 	      /* Warn for integer types converted to smaller integer types.  */
1472 	      if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1473 		give_warning = UNSAFE_OTHER;
1474 
1475 	      /* Check for different signedness, see case for real-domain
1476 		 integers (above) for a more detailed comment.  */
1477 	      else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1478 		    && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1479 		    || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1480 		    && produce_warns)
1481 		warning_at (loc, OPT_Wsign_conversion,
1482 			"conversion to %qT from %qT "
1483 			"may change the sign of the result",
1484 			type, expr_type);
1485 	    }
1486 	  else if (TREE_CODE (from_type) == INTEGER_TYPE
1487 		   && TREE_CODE (to_type) == REAL_TYPE
1488 		   && !int_safely_convertible_to_real_p (from_type, to_type))
1489 	    give_warning = UNSAFE_OTHER;
1490 	}
1491 
1492       /* Warn for complex types converted to real or integer types.  */
1493       else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1494 	       && TREE_CODE (type) != COMPLEX_TYPE)
1495 	give_warning = UNSAFE_IMAGINARY;
1496     }
1497 
1498   return give_warning;
1499 }
1500 
1501 
1502 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1503    Invoke this function on every expression that is converted implicitly,
1504    i.e. because of language rules and not because of an explicit cast.  */
1505 
1506 tree
1507 convert_and_check (location_t loc, tree type, tree expr)
1508 {
1509   tree result;
1510   tree expr_for_warning;
1511 
1512   /* Convert from a value with possible excess precision rather than
1513      via the semantic type, but do not warn about values not fitting
1514      exactly in the semantic type.  */
1515   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1516     {
1517       tree orig_type = TREE_TYPE (expr);
1518       expr = TREE_OPERAND (expr, 0);
1519       expr_for_warning = convert (orig_type, expr);
1520       if (orig_type == type)
1521 	return expr_for_warning;
1522     }
1523   else
1524     expr_for_warning = expr;
1525 
1526   if (TREE_TYPE (expr) == type)
1527     return expr;
1528 
1529   result = convert (type, expr);
1530 
1531   if (c_inhibit_evaluation_warnings == 0
1532       && !TREE_OVERFLOW_P (expr)
1533       && result != error_mark_node)
1534     warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1535 
1536   return result;
1537 }
1538 
1539 /* A node in a list that describes references to variables (EXPR), which are
1540    either read accesses if WRITER is zero, or write accesses, in which case
1541    WRITER is the parent of EXPR.  */
1542 struct tlist
1543 {
1544   struct tlist *next;
1545   tree expr, writer;
1546 };
1547 
1548 /* Used to implement a cache the results of a call to verify_tree.  We only
1549    use this for SAVE_EXPRs.  */
1550 struct tlist_cache
1551 {
1552   struct tlist_cache *next;
1553   struct tlist *cache_before_sp;
1554   struct tlist *cache_after_sp;
1555   tree expr;
1556 };
1557 
1558 /* Obstack to use when allocating tlist structures, and corresponding
1559    firstobj.  */
1560 static struct obstack tlist_obstack;
1561 static char *tlist_firstobj = 0;
1562 
1563 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1564    warnings.  */
1565 static struct tlist *warned_ids;
1566 /* SAVE_EXPRs need special treatment.  We process them only once and then
1567    cache the results.  */
1568 static struct tlist_cache *save_expr_cache;
1569 
1570 static void add_tlist (struct tlist **, struct tlist *, tree, int);
1571 static void merge_tlist (struct tlist **, struct tlist *, int);
1572 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1573 static bool warning_candidate_p (tree);
1574 static bool candidate_equal_p (const_tree, const_tree);
1575 static void warn_for_collisions (struct tlist *);
1576 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1577 static struct tlist *new_tlist (struct tlist *, tree, tree);
1578 
1579 /* Create a new struct tlist and fill in its fields.  */
1580 static struct tlist *
1581 new_tlist (struct tlist *next, tree t, tree writer)
1582 {
1583   struct tlist *l;
1584   l = XOBNEW (&tlist_obstack, struct tlist);
1585   l->next = next;
1586   l->expr = t;
1587   l->writer = writer;
1588   return l;
1589 }
1590 
1591 /* Add duplicates of the nodes found in ADD to the list *TO.  If EXCLUDE_WRITER
1592    is nonnull, we ignore any node we find which has a writer equal to it.  */
1593 
1594 static void
1595 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1596 {
1597   while (add)
1598     {
1599       struct tlist *next = add->next;
1600       if (!copy)
1601 	add->next = *to;
1602       if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1603 	*to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1604       add = next;
1605     }
1606 }
1607 
1608 /* Merge the nodes of ADD into TO.  This merging process is done so that for
1609    each variable that already exists in TO, no new node is added; however if
1610    there is a write access recorded in ADD, and an occurrence on TO is only
1611    a read access, then the occurrence in TO will be modified to record the
1612    write.  */
1613 
1614 static void
1615 merge_tlist (struct tlist **to, struct tlist *add, int copy)
1616 {
1617   struct tlist **end = to;
1618 
1619   while (*end)
1620     end = &(*end)->next;
1621 
1622   while (add)
1623     {
1624       int found = 0;
1625       struct tlist *tmp2;
1626       struct tlist *next = add->next;
1627 
1628       for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1629 	if (candidate_equal_p (tmp2->expr, add->expr))
1630 	  {
1631 	    found = 1;
1632 	    if (!tmp2->writer)
1633 	      tmp2->writer = add->writer;
1634 	  }
1635       if (!found)
1636 	{
1637 	  *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1638 	  end = &(*end)->next;
1639 	  *end = 0;
1640 	}
1641       add = next;
1642     }
1643 }
1644 
1645 /* WRITTEN is a variable, WRITER is its parent.  Warn if any of the variable
1646    references in list LIST conflict with it, excluding reads if ONLY writers
1647    is nonzero.  */
1648 
1649 static void
1650 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1651 		       int only_writes)
1652 {
1653   struct tlist *tmp;
1654 
1655   /* Avoid duplicate warnings.  */
1656   for (tmp = warned_ids; tmp; tmp = tmp->next)
1657     if (candidate_equal_p (tmp->expr, written))
1658       return;
1659 
1660   while (list)
1661     {
1662       if (candidate_equal_p (list->expr, written)
1663 	  && !candidate_equal_p (list->writer, writer)
1664 	  && (!only_writes || list->writer))
1665 	{
1666 	  warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1667 	  warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1668 		      OPT_Wsequence_point, "operation on %qE may be undefined",
1669 		      list->expr);
1670 	}
1671       list = list->next;
1672     }
1673 }
1674 
1675 /* Given a list LIST of references to variables, find whether any of these
1676    can cause conflicts due to missing sequence points.  */
1677 
1678 static void
1679 warn_for_collisions (struct tlist *list)
1680 {
1681   struct tlist *tmp;
1682 
1683   for (tmp = list; tmp; tmp = tmp->next)
1684     {
1685       if (tmp->writer)
1686 	warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1687     }
1688 }
1689 
1690 /* Return nonzero if X is a tree that can be verified by the sequence point
1691    warnings.  */
1692 
1693 static bool
1694 warning_candidate_p (tree x)
1695 {
1696   if (DECL_P (x) && DECL_ARTIFICIAL (x))
1697     return false;
1698 
1699   if (TREE_CODE (x) == BLOCK)
1700     return false;
1701 
1702   /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1703      (lvalue_p) crash on TRY/CATCH. */
1704   if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
1705     return false;
1706 
1707   if (!lvalue_p (x))
1708     return false;
1709 
1710   /* No point to track non-const calls, they will never satisfy
1711      operand_equal_p.  */
1712   if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
1713     return false;
1714 
1715   if (TREE_CODE (x) == STRING_CST)
1716     return false;
1717 
1718   return true;
1719 }
1720 
1721 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1722 static bool
1723 candidate_equal_p (const_tree x, const_tree y)
1724 {
1725   return (x == y) || (x && y && operand_equal_p (x, y, 0));
1726 }
1727 
1728 /* Walk the tree X, and record accesses to variables.  If X is written by the
1729    parent tree, WRITER is the parent.
1730    We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP.  If this
1731    expression or its only operand forces a sequence point, then everything up
1732    to the sequence point is stored in PBEFORE_SP.  Everything else gets stored
1733    in PNO_SP.
1734    Once we return, we will have emitted warnings if any subexpression before
1735    such a sequence point could be undefined.  On a higher level, however, the
1736    sequence point may not be relevant, and we'll merge the two lists.
1737 
1738    Example: (b++, a) + b;
1739    The call that processes the COMPOUND_EXPR will store the increment of B
1740    in PBEFORE_SP, and the use of A in PNO_SP.  The higher-level call that
1741    processes the PLUS_EXPR will need to merge the two lists so that
1742    eventually, all accesses end up on the same list (and we'll warn about the
1743    unordered subexpressions b++ and b.
1744 
1745    A note on merging.  If we modify the former example so that our expression
1746    becomes
1747      (b++, b) + a
1748    care must be taken not simply to add all three expressions into the final
1749    PNO_SP list.  The function merge_tlist takes care of that by merging the
1750    before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1751    way, so that no more than one access to B is recorded.  */
1752 
1753 static void
1754 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1755 	     tree writer)
1756 {
1757   struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1758   enum tree_code code;
1759   enum tree_code_class cl;
1760 
1761   /* X may be NULL if it is the operand of an empty statement expression
1762      ({ }).  */
1763   if (x == NULL)
1764     return;
1765 
1766  restart:
1767   code = TREE_CODE (x);
1768   cl = TREE_CODE_CLASS (code);
1769 
1770   if (warning_candidate_p (x))
1771     *pno_sp = new_tlist (*pno_sp, x, writer);
1772 
1773   switch (code)
1774     {
1775     case CONSTRUCTOR:
1776     case SIZEOF_EXPR:
1777       return;
1778 
1779     case COMPOUND_EXPR:
1780     case TRUTH_ANDIF_EXPR:
1781     case TRUTH_ORIF_EXPR:
1782       tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1783       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1784       warn_for_collisions (tmp_nosp);
1785       merge_tlist (pbefore_sp, tmp_before, 0);
1786       merge_tlist (pbefore_sp, tmp_nosp, 0);
1787       verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
1788       warn_for_collisions (tmp_list2);
1789       merge_tlist (pbefore_sp, tmp_list3, 0);
1790       merge_tlist (pno_sp, tmp_list2, 0);
1791       return;
1792 
1793     case COND_EXPR:
1794       tmp_before = tmp_list2 = 0;
1795       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1796       warn_for_collisions (tmp_list2);
1797       merge_tlist (pbefore_sp, tmp_before, 0);
1798       merge_tlist (pbefore_sp, tmp_list2, 0);
1799 
1800       tmp_list3 = tmp_nosp = 0;
1801       verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1802       warn_for_collisions (tmp_nosp);
1803       merge_tlist (pbefore_sp, tmp_list3, 0);
1804 
1805       tmp_list3 = tmp_list2 = 0;
1806       verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1807       warn_for_collisions (tmp_list2);
1808       merge_tlist (pbefore_sp, tmp_list3, 0);
1809       /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1810 	 two first, to avoid warning for (a ? b++ : b++).  */
1811       merge_tlist (&tmp_nosp, tmp_list2, 0);
1812       add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1813       return;
1814 
1815     case PREDECREMENT_EXPR:
1816     case PREINCREMENT_EXPR:
1817     case POSTDECREMENT_EXPR:
1818     case POSTINCREMENT_EXPR:
1819       verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1820       return;
1821 
1822     case MODIFY_EXPR:
1823       tmp_before = tmp_nosp = tmp_list3 = 0;
1824       verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1825       verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1826       /* Expressions inside the LHS are not ordered wrt. the sequence points
1827 	 in the RHS.  Example:
1828 	   *a = (a++, 2)
1829 	 Despite the fact that the modification of "a" is in the before_sp
1830 	 list (tmp_before), it conflicts with the use of "a" in the LHS.
1831 	 We can handle this by adding the contents of tmp_list3
1832 	 to those of tmp_before, and redoing the collision warnings for that
1833 	 list.  */
1834       add_tlist (&tmp_before, tmp_list3, x, 1);
1835       warn_for_collisions (tmp_before);
1836       /* Exclude the LHS itself here; we first have to merge it into the
1837 	 tmp_nosp list.  This is done to avoid warning for "a = a"; if we
1838 	 didn't exclude the LHS, we'd get it twice, once as a read and once
1839 	 as a write.  */
1840       add_tlist (pno_sp, tmp_list3, x, 0);
1841       warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1842 
1843       merge_tlist (pbefore_sp, tmp_before, 0);
1844       if (warning_candidate_p (TREE_OPERAND (x, 0)))
1845 	merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1846       add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1847       return;
1848 
1849     case CALL_EXPR:
1850       /* We need to warn about conflicts among arguments and conflicts between
1851 	 args and the function address.  Side effects of the function address,
1852 	 however, are not ordered by the sequence point of the call.  */
1853       {
1854 	call_expr_arg_iterator iter;
1855 	tree arg;
1856 	tmp_before = tmp_nosp = 0;
1857 	verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1858 	FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1859 	  {
1860 	    tmp_list2 = tmp_list3 = 0;
1861 	    verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1862 	    merge_tlist (&tmp_list3, tmp_list2, 0);
1863 	    add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1864 	  }
1865 	add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1866 	warn_for_collisions (tmp_before);
1867 	add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1868 	return;
1869       }
1870 
1871     case TREE_LIST:
1872       /* Scan all the list, e.g. indices of multi dimensional array.  */
1873       while (x)
1874 	{
1875 	  tmp_before = tmp_nosp = 0;
1876 	  verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1877 	  merge_tlist (&tmp_nosp, tmp_before, 0);
1878 	  add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1879 	  x = TREE_CHAIN (x);
1880 	}
1881       return;
1882 
1883     case SAVE_EXPR:
1884       {
1885 	struct tlist_cache *t;
1886 	for (t = save_expr_cache; t; t = t->next)
1887 	  if (candidate_equal_p (t->expr, x))
1888 	    break;
1889 
1890 	if (!t)
1891 	  {
1892 	    t = XOBNEW (&tlist_obstack, struct tlist_cache);
1893 	    t->next = save_expr_cache;
1894 	    t->expr = x;
1895 	    save_expr_cache = t;
1896 
1897 	    tmp_before = tmp_nosp = 0;
1898 	    verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1899 	    warn_for_collisions (tmp_nosp);
1900 
1901 	    tmp_list3 = 0;
1902 	    merge_tlist (&tmp_list3, tmp_nosp, 0);
1903 	    t->cache_before_sp = tmp_before;
1904 	    t->cache_after_sp = tmp_list3;
1905 	  }
1906 	merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1907 	add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1908 	return;
1909       }
1910 
1911     case ADDR_EXPR:
1912       x = TREE_OPERAND (x, 0);
1913       if (DECL_P (x))
1914 	return;
1915       writer = 0;
1916       goto restart;
1917 
1918     default:
1919       /* For other expressions, simply recurse on their operands.
1920 	 Manual tail recursion for unary expressions.
1921 	 Other non-expressions need not be processed.  */
1922       if (cl == tcc_unary)
1923 	{
1924 	  x = TREE_OPERAND (x, 0);
1925 	  writer = 0;
1926 	  goto restart;
1927 	}
1928       else if (IS_EXPR_CODE_CLASS (cl))
1929 	{
1930 	  int lp;
1931 	  int max = TREE_OPERAND_LENGTH (x);
1932 	  for (lp = 0; lp < max; lp++)
1933 	    {
1934 	      tmp_before = tmp_nosp = 0;
1935 	      verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
1936 	      merge_tlist (&tmp_nosp, tmp_before, 0);
1937 	      add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1938 	    }
1939 	}
1940       return;
1941     }
1942 }
1943 
1944 /* Try to warn for undefined behavior in EXPR due to missing sequence
1945    points.  */
1946 
1947 DEBUG_FUNCTION void
1948 verify_sequence_points (tree expr)
1949 {
1950   struct tlist *before_sp = 0, *after_sp = 0;
1951 
1952   warned_ids = 0;
1953   save_expr_cache = 0;
1954   if (tlist_firstobj == 0)
1955     {
1956       gcc_obstack_init (&tlist_obstack);
1957       tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
1958     }
1959 
1960   verify_tree (expr, &before_sp, &after_sp, 0);
1961   warn_for_collisions (after_sp);
1962   obstack_free (&tlist_obstack, tlist_firstobj);
1963 }
1964 
1965 /* Validate the expression after `case' and apply default promotions.  */
1966 
1967 static tree
1968 check_case_value (location_t loc, tree value)
1969 {
1970   if (value == NULL_TREE)
1971     return value;
1972 
1973   if (TREE_CODE (value) == INTEGER_CST)
1974     /* Promote char or short to int.  */
1975     value = perform_integral_promotions (value);
1976   else if (value != error_mark_node)
1977     {
1978       error_at (loc, "case label does not reduce to an integer constant");
1979       value = error_mark_node;
1980     }
1981 
1982   constant_expression_warning (value);
1983 
1984   return value;
1985 }
1986 
1987 /* See if the case values LOW and HIGH are in the range of the original
1988    type (i.e. before the default conversion to int) of the switch testing
1989    expression.
1990    TYPE is the promoted type of the testing expression, and ORIG_TYPE is
1991    the type before promoting it.  CASE_LOW_P is a pointer to the lower
1992    bound of the case label, and CASE_HIGH_P is the upper bound or NULL
1993    if the case is not a case range.
1994    The caller has to make sure that we are not called with NULL for
1995    CASE_LOW_P (i.e. the default case).  OUTSIDE_RANGE_P says whether there
1996    was a case value that doesn't fit into the range of the ORIG_TYPE.
1997    Returns true if the case label is in range of ORIG_TYPE (saturated or
1998    untouched) or false if the label is out of range.  */
1999 
2000 static bool
2001 check_case_bounds (location_t loc, tree type, tree orig_type,
2002 		   tree *case_low_p, tree *case_high_p,
2003 		   bool *outside_range_p)
2004 {
2005   tree min_value, max_value;
2006   tree case_low = *case_low_p;
2007   tree case_high = case_high_p ? *case_high_p : case_low;
2008 
2009   /* If there was a problem with the original type, do nothing.  */
2010   if (orig_type == error_mark_node)
2011     return true;
2012 
2013   min_value = TYPE_MIN_VALUE (orig_type);
2014   max_value = TYPE_MAX_VALUE (orig_type);
2015 
2016   /* We'll really need integer constants here.  */
2017   case_low = fold (case_low);
2018   case_high = fold (case_high);
2019 
2020   /* Case label is less than minimum for type.  */
2021   if (tree_int_cst_compare (case_low, min_value) < 0
2022       && tree_int_cst_compare (case_high, min_value) < 0)
2023     {
2024       warning_at (loc, 0, "case label value is less than minimum value "
2025 		  "for type");
2026       *outside_range_p = true;
2027       return false;
2028     }
2029 
2030   /* Case value is greater than maximum for type.  */
2031   if (tree_int_cst_compare (case_low, max_value) > 0
2032       && tree_int_cst_compare (case_high, max_value) > 0)
2033     {
2034       warning_at (loc, 0, "case label value exceeds maximum value for type");
2035       *outside_range_p = true;
2036       return false;
2037     }
2038 
2039   /* Saturate lower case label value to minimum.  */
2040   if (tree_int_cst_compare (case_high, min_value) >= 0
2041       && tree_int_cst_compare (case_low, min_value) < 0)
2042     {
2043       warning_at (loc, 0, "lower value in case label range"
2044 		  " less than minimum value for type");
2045       *outside_range_p = true;
2046       case_low = min_value;
2047     }
2048 
2049   /* Saturate upper case label value to maximum.  */
2050   if (tree_int_cst_compare (case_low, max_value) <= 0
2051       && tree_int_cst_compare (case_high, max_value) > 0)
2052     {
2053       warning_at (loc, 0, "upper value in case label range"
2054 		  " exceeds maximum value for type");
2055       *outside_range_p = true;
2056       case_high = max_value;
2057     }
2058 
2059   if (*case_low_p != case_low)
2060     *case_low_p = convert (type, case_low);
2061   if (case_high_p && *case_high_p != case_high)
2062     *case_high_p = convert (type, case_high);
2063 
2064   return true;
2065 }
2066 
2067 /* Return an integer type with BITS bits of precision,
2068    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
2069 
2070 tree
2071 c_common_type_for_size (unsigned int bits, int unsignedp)
2072 {
2073   int i;
2074 
2075   if (bits == TYPE_PRECISION (integer_type_node))
2076     return unsignedp ? unsigned_type_node : integer_type_node;
2077 
2078   if (bits == TYPE_PRECISION (signed_char_type_node))
2079     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2080 
2081   if (bits == TYPE_PRECISION (short_integer_type_node))
2082     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2083 
2084   if (bits == TYPE_PRECISION (long_integer_type_node))
2085     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2086 
2087   if (bits == TYPE_PRECISION (long_long_integer_type_node))
2088     return (unsignedp ? long_long_unsigned_type_node
2089 	    : long_long_integer_type_node);
2090 
2091   for (i = 0; i < NUM_INT_N_ENTS; i ++)
2092     if (int_n_enabled_p[i]
2093 	&& bits == int_n_data[i].bitsize)
2094       return (unsignedp ? int_n_trees[i].unsigned_type
2095 	      : int_n_trees[i].signed_type);
2096 
2097   if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2098     return (unsignedp ? widest_unsigned_literal_type_node
2099 	    : widest_integer_literal_type_node);
2100 
2101   if (bits <= TYPE_PRECISION (intQI_type_node))
2102     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2103 
2104   if (bits <= TYPE_PRECISION (intHI_type_node))
2105     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2106 
2107   if (bits <= TYPE_PRECISION (intSI_type_node))
2108     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2109 
2110   if (bits <= TYPE_PRECISION (intDI_type_node))
2111     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2112 
2113   return 0;
2114 }
2115 
2116 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2117    that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2118    and saturating if SATP is nonzero, otherwise not saturating.  */
2119 
2120 tree
2121 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2122 				    int unsignedp, int satp)
2123 {
2124   machine_mode mode;
2125   if (ibit == 0)
2126     mode = unsignedp ? UQQmode : QQmode;
2127   else
2128     mode = unsignedp ? UHAmode : HAmode;
2129 
2130   for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2131     if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2132       break;
2133 
2134   if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2135     {
2136       sorry ("GCC cannot support operators with integer types and "
2137 	     "fixed-point types that have too many integral and "
2138 	     "fractional bits together");
2139       return 0;
2140     }
2141 
2142   return c_common_type_for_mode (mode, satp);
2143 }
2144 
2145 /* Used for communication between c_common_type_for_mode and
2146    c_register_builtin_type.  */
2147 tree registered_builtin_types;
2148 
2149 /* Return a data type that has machine mode MODE.
2150    If the mode is an integer,
2151    then UNSIGNEDP selects between signed and unsigned types.
2152    If the mode is a fixed-point mode,
2153    then UNSIGNEDP selects between saturating and nonsaturating types.  */
2154 
2155 tree
2156 c_common_type_for_mode (machine_mode mode, int unsignedp)
2157 {
2158   tree t;
2159   int i;
2160 
2161   if (mode == TYPE_MODE (integer_type_node))
2162     return unsignedp ? unsigned_type_node : integer_type_node;
2163 
2164   if (mode == TYPE_MODE (signed_char_type_node))
2165     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2166 
2167   if (mode == TYPE_MODE (short_integer_type_node))
2168     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2169 
2170   if (mode == TYPE_MODE (long_integer_type_node))
2171     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2172 
2173   if (mode == TYPE_MODE (long_long_integer_type_node))
2174     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2175 
2176   for (i = 0; i < NUM_INT_N_ENTS; i ++)
2177     if (int_n_enabled_p[i]
2178 	&& mode == int_n_data[i].m)
2179       return (unsignedp ? int_n_trees[i].unsigned_type
2180 	      : int_n_trees[i].signed_type);
2181 
2182   if (mode == QImode)
2183     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2184 
2185   if (mode == HImode)
2186     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2187 
2188   if (mode == SImode)
2189     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2190 
2191   if (mode == DImode)
2192     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2193 
2194 #if HOST_BITS_PER_WIDE_INT >= 64
2195   if (mode == TYPE_MODE (intTI_type_node))
2196     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2197 #endif
2198 
2199   if (mode == TYPE_MODE (float_type_node))
2200     return float_type_node;
2201 
2202   if (mode == TYPE_MODE (double_type_node))
2203     return double_type_node;
2204 
2205   if (mode == TYPE_MODE (long_double_type_node))
2206     return long_double_type_node;
2207 
2208   for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2209     if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2210 	&& mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2211       return FLOATN_NX_TYPE_NODE (i);
2212 
2213   if (mode == TYPE_MODE (void_type_node))
2214     return void_type_node;
2215 
2216   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2217     return (unsignedp
2218 	    ? make_unsigned_type (GET_MODE_PRECISION (mode))
2219 	    : make_signed_type (GET_MODE_PRECISION (mode)));
2220 
2221   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2222     return (unsignedp
2223 	    ? make_unsigned_type (GET_MODE_PRECISION (mode))
2224 	    : make_signed_type (GET_MODE_PRECISION (mode)));
2225 
2226   if (COMPLEX_MODE_P (mode))
2227     {
2228       machine_mode inner_mode;
2229       tree inner_type;
2230 
2231       if (mode == TYPE_MODE (complex_float_type_node))
2232 	return complex_float_type_node;
2233       if (mode == TYPE_MODE (complex_double_type_node))
2234 	return complex_double_type_node;
2235       if (mode == TYPE_MODE (complex_long_double_type_node))
2236 	return complex_long_double_type_node;
2237 
2238       for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2239 	if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2240 	    && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2241 	  return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2242 
2243       if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2244 	return complex_integer_type_node;
2245 
2246       inner_mode = GET_MODE_INNER (mode);
2247       inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2248       if (inner_type != NULL_TREE)
2249 	return build_complex_type (inner_type);
2250     }
2251   else if (VECTOR_MODE_P (mode))
2252     {
2253       machine_mode inner_mode = GET_MODE_INNER (mode);
2254       tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2255       if (inner_type != NULL_TREE)
2256 	return build_vector_type_for_mode (inner_type, mode);
2257     }
2258 
2259   if (mode == TYPE_MODE (dfloat32_type_node))
2260     return dfloat32_type_node;
2261   if (mode == TYPE_MODE (dfloat64_type_node))
2262     return dfloat64_type_node;
2263   if (mode == TYPE_MODE (dfloat128_type_node))
2264     return dfloat128_type_node;
2265 
2266   if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2267     {
2268       if (mode == TYPE_MODE (short_fract_type_node))
2269 	return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2270       if (mode == TYPE_MODE (fract_type_node))
2271 	return unsignedp ? sat_fract_type_node : fract_type_node;
2272       if (mode == TYPE_MODE (long_fract_type_node))
2273 	return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2274       if (mode == TYPE_MODE (long_long_fract_type_node))
2275 	return unsignedp ? sat_long_long_fract_type_node
2276 			 : long_long_fract_type_node;
2277 
2278       if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2279 	return unsignedp ? sat_unsigned_short_fract_type_node
2280 			 : unsigned_short_fract_type_node;
2281       if (mode == TYPE_MODE (unsigned_fract_type_node))
2282 	return unsignedp ? sat_unsigned_fract_type_node
2283 			 : unsigned_fract_type_node;
2284       if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2285 	return unsignedp ? sat_unsigned_long_fract_type_node
2286 			 : unsigned_long_fract_type_node;
2287       if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2288 	return unsignedp ? sat_unsigned_long_long_fract_type_node
2289 			 : unsigned_long_long_fract_type_node;
2290 
2291       if (mode == TYPE_MODE (short_accum_type_node))
2292 	return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2293       if (mode == TYPE_MODE (accum_type_node))
2294 	return unsignedp ? sat_accum_type_node : accum_type_node;
2295       if (mode == TYPE_MODE (long_accum_type_node))
2296 	return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2297       if (mode == TYPE_MODE (long_long_accum_type_node))
2298 	return unsignedp ? sat_long_long_accum_type_node
2299 			 : long_long_accum_type_node;
2300 
2301       if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2302 	return unsignedp ? sat_unsigned_short_accum_type_node
2303 			 : unsigned_short_accum_type_node;
2304       if (mode == TYPE_MODE (unsigned_accum_type_node))
2305 	return unsignedp ? sat_unsigned_accum_type_node
2306 			 : unsigned_accum_type_node;
2307       if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2308 	return unsignedp ? sat_unsigned_long_accum_type_node
2309 			 : unsigned_long_accum_type_node;
2310       if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2311 	return unsignedp ? sat_unsigned_long_long_accum_type_node
2312 			 : unsigned_long_long_accum_type_node;
2313 
2314       if (mode == QQmode)
2315 	return unsignedp ? sat_qq_type_node : qq_type_node;
2316       if (mode == HQmode)
2317 	return unsignedp ? sat_hq_type_node : hq_type_node;
2318       if (mode == SQmode)
2319 	return unsignedp ? sat_sq_type_node : sq_type_node;
2320       if (mode == DQmode)
2321 	return unsignedp ? sat_dq_type_node : dq_type_node;
2322       if (mode == TQmode)
2323 	return unsignedp ? sat_tq_type_node : tq_type_node;
2324 
2325       if (mode == UQQmode)
2326 	return unsignedp ? sat_uqq_type_node : uqq_type_node;
2327       if (mode == UHQmode)
2328 	return unsignedp ? sat_uhq_type_node : uhq_type_node;
2329       if (mode == USQmode)
2330 	return unsignedp ? sat_usq_type_node : usq_type_node;
2331       if (mode == UDQmode)
2332 	return unsignedp ? sat_udq_type_node : udq_type_node;
2333       if (mode == UTQmode)
2334 	return unsignedp ? sat_utq_type_node : utq_type_node;
2335 
2336       if (mode == HAmode)
2337 	return unsignedp ? sat_ha_type_node : ha_type_node;
2338       if (mode == SAmode)
2339 	return unsignedp ? sat_sa_type_node : sa_type_node;
2340       if (mode == DAmode)
2341 	return unsignedp ? sat_da_type_node : da_type_node;
2342       if (mode == TAmode)
2343 	return unsignedp ? sat_ta_type_node : ta_type_node;
2344 
2345       if (mode == UHAmode)
2346 	return unsignedp ? sat_uha_type_node : uha_type_node;
2347       if (mode == USAmode)
2348 	return unsignedp ? sat_usa_type_node : usa_type_node;
2349       if (mode == UDAmode)
2350 	return unsignedp ? sat_uda_type_node : uda_type_node;
2351       if (mode == UTAmode)
2352 	return unsignedp ? sat_uta_type_node : uta_type_node;
2353     }
2354 
2355   for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2356     if (TYPE_MODE (TREE_VALUE (t)) == mode
2357 	&& !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
2358       return TREE_VALUE (t);
2359 
2360   return 0;
2361 }
2362 
2363 tree
2364 c_common_unsigned_type (tree type)
2365 {
2366   return c_common_signed_or_unsigned_type (1, type);
2367 }
2368 
2369 /* Return a signed type the same as TYPE in other respects.  */
2370 
2371 tree
2372 c_common_signed_type (tree type)
2373 {
2374   return c_common_signed_or_unsigned_type (0, type);
2375 }
2376 
2377 /* Return a type the same as TYPE except unsigned or
2378    signed according to UNSIGNEDP.  */
2379 
2380 tree
2381 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2382 {
2383   tree type1;
2384   int i;
2385 
2386   /* This block of code emulates the behavior of the old
2387      c_common_unsigned_type. In particular, it returns
2388      long_unsigned_type_node if passed a long, even when a int would
2389      have the same size. This is necessary for warnings to work
2390      correctly in archs where sizeof(int) == sizeof(long) */
2391 
2392   type1 = TYPE_MAIN_VARIANT (type);
2393   if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2394     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2395   if (type1 == integer_type_node || type1 == unsigned_type_node)
2396     return unsignedp ? unsigned_type_node : integer_type_node;
2397   if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2398     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2399   if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2400     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2401   if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2402     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2403 
2404   for (i = 0; i < NUM_INT_N_ENTS; i ++)
2405     if (int_n_enabled_p[i]
2406 	&& (type1 == int_n_trees[i].unsigned_type
2407 	    || type1 == int_n_trees[i].signed_type))
2408       return (unsignedp ? int_n_trees[i].unsigned_type
2409 	      : int_n_trees[i].signed_type);
2410 
2411 #if HOST_BITS_PER_WIDE_INT >= 64
2412   if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2413     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2414 #endif
2415   if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2416     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2417   if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2418     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2419   if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2420     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2421   if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2422     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2423 
2424 #define C_COMMON_FIXED_TYPES(NAME)	    \
2425   if (type1 == short_ ## NAME ## _type_node \
2426       || type1 == unsigned_short_ ## NAME ## _type_node) \
2427     return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2428 		     : short_ ## NAME ## _type_node; \
2429   if (type1 == NAME ## _type_node \
2430       || type1 == unsigned_ ## NAME ## _type_node) \
2431     return unsignedp ? unsigned_ ## NAME ## _type_node \
2432 		     : NAME ## _type_node; \
2433   if (type1 == long_ ## NAME ## _type_node \
2434       || type1 == unsigned_long_ ## NAME ## _type_node) \
2435     return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2436 		     : long_ ## NAME ## _type_node; \
2437   if (type1 == long_long_ ## NAME ## _type_node \
2438       || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2439     return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2440 		     : long_long_ ## NAME ## _type_node;
2441 
2442 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2443   if (type1 == NAME ## _type_node \
2444       || type1 == u ## NAME ## _type_node) \
2445     return unsignedp ? u ## NAME ## _type_node \
2446 		     : NAME ## _type_node;
2447 
2448 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2449   if (type1 == sat_ ## short_ ## NAME ## _type_node \
2450       || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2451     return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2452 		     : sat_ ## short_ ## NAME ## _type_node; \
2453   if (type1 == sat_ ## NAME ## _type_node \
2454       || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2455     return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2456 		     : sat_ ## NAME ## _type_node; \
2457   if (type1 == sat_ ## long_ ## NAME ## _type_node \
2458       || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2459     return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2460 		     : sat_ ## long_ ## NAME ## _type_node; \
2461   if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2462       || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2463     return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2464 		     : sat_ ## long_long_ ## NAME ## _type_node;
2465 
2466 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME)	\
2467   if (type1 == sat_ ## NAME ## _type_node \
2468       || type1 == sat_ ## u ## NAME ## _type_node) \
2469     return unsignedp ? sat_ ## u ## NAME ## _type_node \
2470 		     : sat_ ## NAME ## _type_node;
2471 
2472   C_COMMON_FIXED_TYPES (fract);
2473   C_COMMON_FIXED_TYPES_SAT (fract);
2474   C_COMMON_FIXED_TYPES (accum);
2475   C_COMMON_FIXED_TYPES_SAT (accum);
2476 
2477   C_COMMON_FIXED_MODE_TYPES (qq);
2478   C_COMMON_FIXED_MODE_TYPES (hq);
2479   C_COMMON_FIXED_MODE_TYPES (sq);
2480   C_COMMON_FIXED_MODE_TYPES (dq);
2481   C_COMMON_FIXED_MODE_TYPES (tq);
2482   C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2483   C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2484   C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2485   C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2486   C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2487   C_COMMON_FIXED_MODE_TYPES (ha);
2488   C_COMMON_FIXED_MODE_TYPES (sa);
2489   C_COMMON_FIXED_MODE_TYPES (da);
2490   C_COMMON_FIXED_MODE_TYPES (ta);
2491   C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2492   C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2493   C_COMMON_FIXED_MODE_TYPES_SAT (da);
2494   C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2495 
2496   /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2497      the precision; they have precision set to match their range, but
2498      may use a wider mode to match an ABI.  If we change modes, we may
2499      wind up with bad conversions.  For INTEGER_TYPEs in C, must check
2500      the precision as well, so as to yield correct results for
2501      bit-field types.  C++ does not have these separate bit-field
2502      types, and producing a signed or unsigned variant of an
2503      ENUMERAL_TYPE may cause other problems as well.  */
2504 
2505   if (!INTEGRAL_TYPE_P (type)
2506       || TYPE_UNSIGNED (type) == unsignedp)
2507     return type;
2508 
2509 #define TYPE_OK(node)							    \
2510   (TYPE_MODE (type) == TYPE_MODE (node)					    \
2511    && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2512   if (TYPE_OK (signed_char_type_node))
2513     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2514   if (TYPE_OK (integer_type_node))
2515     return unsignedp ? unsigned_type_node : integer_type_node;
2516   if (TYPE_OK (short_integer_type_node))
2517     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2518   if (TYPE_OK (long_integer_type_node))
2519     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2520   if (TYPE_OK (long_long_integer_type_node))
2521     return (unsignedp ? long_long_unsigned_type_node
2522 	    : long_long_integer_type_node);
2523 
2524   for (i = 0; i < NUM_INT_N_ENTS; i ++)
2525     if (int_n_enabled_p[i]
2526 	&& TYPE_MODE (type) == int_n_data[i].m
2527 	&& TYPE_PRECISION (type) == int_n_data[i].bitsize)
2528       return (unsignedp ? int_n_trees[i].unsigned_type
2529 	      : int_n_trees[i].signed_type);
2530 
2531 #if HOST_BITS_PER_WIDE_INT >= 64
2532   if (TYPE_OK (intTI_type_node))
2533     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2534 #endif
2535   if (TYPE_OK (intDI_type_node))
2536     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2537   if (TYPE_OK (intSI_type_node))
2538     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2539   if (TYPE_OK (intHI_type_node))
2540     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2541   if (TYPE_OK (intQI_type_node))
2542     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2543 #undef TYPE_OK
2544 
2545   return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2546 }
2547 
2548 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP.  */
2549 
2550 tree
2551 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2552 {
2553   int i;
2554 
2555   /* Extended integer types of the same width as a standard type have
2556      lesser rank, so those of the same width as int promote to int or
2557      unsigned int and are valid for printf formats expecting int or
2558      unsigned int.  To avoid such special cases, avoid creating
2559      extended integer types for bit-fields if a standard integer type
2560      is available.  */
2561   if (width == TYPE_PRECISION (integer_type_node))
2562     return unsignedp ? unsigned_type_node : integer_type_node;
2563   if (width == TYPE_PRECISION (signed_char_type_node))
2564     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2565   if (width == TYPE_PRECISION (short_integer_type_node))
2566     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2567   if (width == TYPE_PRECISION (long_integer_type_node))
2568     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2569   if (width == TYPE_PRECISION (long_long_integer_type_node))
2570     return (unsignedp ? long_long_unsigned_type_node
2571 	    : long_long_integer_type_node);
2572   for (i = 0; i < NUM_INT_N_ENTS; i ++)
2573     if (int_n_enabled_p[i]
2574 	&& width == int_n_data[i].bitsize)
2575       return (unsignedp ? int_n_trees[i].unsigned_type
2576 	      : int_n_trees[i].signed_type);
2577   return build_nonstandard_integer_type (width, unsignedp);
2578 }
2579 
2580 /* The C version of the register_builtin_type langhook.  */
2581 
2582 void
2583 c_register_builtin_type (tree type, const char* name)
2584 {
2585   tree decl;
2586 
2587   decl = build_decl (UNKNOWN_LOCATION,
2588 		     TYPE_DECL, get_identifier (name), type);
2589   DECL_ARTIFICIAL (decl) = 1;
2590   if (!TYPE_NAME (type))
2591     TYPE_NAME (type) = decl;
2592   pushdecl (decl);
2593 
2594   registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2595 }
2596 
2597 /* Print an error message for invalid operands to arith operation
2598    CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2599    RICHLOC is a rich location for the message, containing either
2600    three separate locations for each of the operator and operands
2601 
2602       lhs op rhs
2603       ~~~ ^~ ~~~
2604 
2605    (C FE), or one location ranging over all over them
2606 
2607       lhs op rhs
2608       ~~~~^~~~~~
2609 
2610    (C++ FE).  */
2611 
2612 void
2613 binary_op_error (rich_location *richloc, enum tree_code code,
2614 		 tree type0, tree type1)
2615 {
2616   const char *opname;
2617 
2618   switch (code)
2619     {
2620     case PLUS_EXPR:
2621       opname = "+"; break;
2622     case MINUS_EXPR:
2623       opname = "-"; break;
2624     case MULT_EXPR:
2625       opname = "*"; break;
2626     case MAX_EXPR:
2627       opname = "max"; break;
2628     case MIN_EXPR:
2629       opname = "min"; break;
2630     case EQ_EXPR:
2631       opname = "=="; break;
2632     case NE_EXPR:
2633       opname = "!="; break;
2634     case LE_EXPR:
2635       opname = "<="; break;
2636     case GE_EXPR:
2637       opname = ">="; break;
2638     case LT_EXPR:
2639       opname = "<"; break;
2640     case GT_EXPR:
2641       opname = ">"; break;
2642     case LSHIFT_EXPR:
2643       opname = "<<"; break;
2644     case RSHIFT_EXPR:
2645       opname = ">>"; break;
2646     case TRUNC_MOD_EXPR:
2647     case FLOOR_MOD_EXPR:
2648       opname = "%"; break;
2649     case TRUNC_DIV_EXPR:
2650     case FLOOR_DIV_EXPR:
2651       opname = "/"; break;
2652     case BIT_AND_EXPR:
2653       opname = "&"; break;
2654     case BIT_IOR_EXPR:
2655       opname = "|"; break;
2656     case TRUTH_ANDIF_EXPR:
2657       opname = "&&"; break;
2658     case TRUTH_ORIF_EXPR:
2659       opname = "||"; break;
2660     case BIT_XOR_EXPR:
2661       opname = "^"; break;
2662     default:
2663       gcc_unreachable ();
2664     }
2665   error_at_rich_loc (richloc,
2666 		     "invalid operands to binary %s (have %qT and %qT)",
2667 		     opname, type0, type1);
2668 }
2669 
2670 /* Given an expression as a tree, return its original type.  Do this
2671    by stripping any conversion that preserves the sign and precision.  */
2672 static tree
2673 expr_original_type (tree expr)
2674 {
2675   STRIP_SIGN_NOPS (expr);
2676   return TREE_TYPE (expr);
2677 }
2678 
2679 /* Subroutine of build_binary_op, used for comparison operations.
2680    See if the operands have both been converted from subword integer types
2681    and, if so, perhaps change them both back to their original type.
2682    This function is also responsible for converting the two operands
2683    to the proper common type for comparison.
2684 
2685    The arguments of this function are all pointers to local variables
2686    of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2687    RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2688 
2689    LOC is the location of the comparison.
2690 
2691    If this function returns nonzero, it means that the comparison has
2692    a constant value.  What this function returns is an expression for
2693    that value.  */
2694 
2695 tree
2696 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
2697 		 tree *restype_ptr, enum tree_code *rescode_ptr)
2698 {
2699   tree type;
2700   tree op0 = *op0_ptr;
2701   tree op1 = *op1_ptr;
2702   int unsignedp0, unsignedp1;
2703   int real1, real2;
2704   tree primop0, primop1;
2705   enum tree_code code = *rescode_ptr;
2706 
2707   /* Throw away any conversions to wider types
2708      already present in the operands.  */
2709 
2710   primop0 = c_common_get_narrower (op0, &unsignedp0);
2711   primop1 = c_common_get_narrower (op1, &unsignedp1);
2712 
2713   /* If primopN is first sign-extended from primopN's precision to opN's
2714      precision, then zero-extended from opN's precision to
2715      *restype_ptr precision, shortenings might be invalid.  */
2716   if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
2717       && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
2718       && !unsignedp0
2719       && TYPE_UNSIGNED (TREE_TYPE (op0)))
2720     primop0 = op0;
2721   if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
2722       && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
2723       && !unsignedp1
2724       && TYPE_UNSIGNED (TREE_TYPE (op1)))
2725     primop1 = op1;
2726 
2727   /* Handle the case that OP0 does not *contain* a conversion
2728      but it *requires* conversion to FINAL_TYPE.  */
2729 
2730   if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2731     unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2732   if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2733     unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2734 
2735   /* If one of the operands must be floated, we cannot optimize.  */
2736   real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2737   real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2738 
2739   /* If first arg is constant, swap the args (changing operation
2740      so value is preserved), for canonicalization.  Don't do this if
2741      the second arg is 0.  */
2742 
2743   if (TREE_CONSTANT (primop0)
2744       && !integer_zerop (primop1) && !real_zerop (primop1)
2745       && !fixed_zerop (primop1))
2746     {
2747       std::swap (primop0, primop1);
2748       std::swap (op0, op1);
2749       *op0_ptr = op0;
2750       *op1_ptr = op1;
2751       std::swap (unsignedp0, unsignedp1);
2752       std::swap (real1, real2);
2753 
2754       switch (code)
2755 	{
2756 	case LT_EXPR:
2757 	  code = GT_EXPR;
2758 	  break;
2759 	case GT_EXPR:
2760 	  code = LT_EXPR;
2761 	  break;
2762 	case LE_EXPR:
2763 	  code = GE_EXPR;
2764 	  break;
2765 	case GE_EXPR:
2766 	  code = LE_EXPR;
2767 	  break;
2768 	default:
2769 	  break;
2770 	}
2771       *rescode_ptr = code;
2772     }
2773 
2774   /* If comparing an integer against a constant more bits wide,
2775      maybe we can deduce a value of 1 or 0 independent of the data.
2776      Or else truncate the constant now
2777      rather than extend the variable at run time.
2778 
2779      This is only interesting if the constant is the wider arg.
2780      Also, it is not safe if the constant is unsigned and the
2781      variable arg is signed, since in this case the variable
2782      would be sign-extended and then regarded as unsigned.
2783      Our technique fails in this case because the lowest/highest
2784      possible unsigned results don't follow naturally from the
2785      lowest/highest possible values of the variable operand.
2786      For just EQ_EXPR and NE_EXPR there is another technique that
2787      could be used: see if the constant can be faithfully represented
2788      in the other operand's type, by truncating it and reextending it
2789      and see if that preserves the constant's value.  */
2790 
2791   if (!real1 && !real2
2792       && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
2793       && TREE_CODE (primop1) == INTEGER_CST
2794       && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2795     {
2796       int min_gt, max_gt, min_lt, max_lt;
2797       tree maxval, minval;
2798       /* 1 if comparison is nominally unsigned.  */
2799       int unsignedp = TYPE_UNSIGNED (*restype_ptr);
2800       tree val;
2801 
2802       type = c_common_signed_or_unsigned_type (unsignedp0,
2803 					       TREE_TYPE (primop0));
2804 
2805       maxval = TYPE_MAX_VALUE (type);
2806       minval = TYPE_MIN_VALUE (type);
2807 
2808       if (unsignedp && !unsignedp0)
2809 	*restype_ptr = c_common_signed_type (*restype_ptr);
2810 
2811       if (TREE_TYPE (primop1) != *restype_ptr)
2812 	{
2813 	  /* Convert primop1 to target type, but do not introduce
2814 	     additional overflow.  We know primop1 is an int_cst.  */
2815 	  primop1 = force_fit_type (*restype_ptr,
2816 				    wi::to_wide
2817 				     (primop1,
2818 				      TYPE_PRECISION (*restype_ptr)),
2819 				    0, TREE_OVERFLOW (primop1));
2820 	}
2821       if (type != *restype_ptr)
2822 	{
2823 	  minval = convert (*restype_ptr, minval);
2824 	  maxval = convert (*restype_ptr, maxval);
2825 	}
2826 
2827       min_gt = tree_int_cst_lt (primop1, minval);
2828       max_gt = tree_int_cst_lt (primop1, maxval);
2829       min_lt = tree_int_cst_lt (minval, primop1);
2830       max_lt = tree_int_cst_lt (maxval, primop1);
2831 
2832       val = 0;
2833       /* This used to be a switch, but Genix compiler can't handle that.  */
2834       if (code == NE_EXPR)
2835 	{
2836 	  if (max_lt || min_gt)
2837 	    val = truthvalue_true_node;
2838 	}
2839       else if (code == EQ_EXPR)
2840 	{
2841 	  if (max_lt || min_gt)
2842 	    val = truthvalue_false_node;
2843 	}
2844       else if (code == LT_EXPR)
2845 	{
2846 	  if (max_lt)
2847 	    val = truthvalue_true_node;
2848 	  if (!min_lt)
2849 	    val = truthvalue_false_node;
2850 	}
2851       else if (code == GT_EXPR)
2852 	{
2853 	  if (min_gt)
2854 	    val = truthvalue_true_node;
2855 	  if (!max_gt)
2856 	    val = truthvalue_false_node;
2857 	}
2858       else if (code == LE_EXPR)
2859 	{
2860 	  if (!max_gt)
2861 	    val = truthvalue_true_node;
2862 	  if (min_gt)
2863 	    val = truthvalue_false_node;
2864 	}
2865       else if (code == GE_EXPR)
2866 	{
2867 	  if (!min_lt)
2868 	    val = truthvalue_true_node;
2869 	  if (max_lt)
2870 	    val = truthvalue_false_node;
2871 	}
2872 
2873       /* If primop0 was sign-extended and unsigned comparison specd,
2874 	 we did a signed comparison above using the signed type bounds.
2875 	 But the comparison we output must be unsigned.
2876 
2877 	 Also, for inequalities, VAL is no good; but if the signed
2878 	 comparison had *any* fixed result, it follows that the
2879 	 unsigned comparison just tests the sign in reverse
2880 	 (positive values are LE, negative ones GE).
2881 	 So we can generate an unsigned comparison
2882 	 against an extreme value of the signed type.  */
2883 
2884       if (unsignedp && !unsignedp0)
2885 	{
2886 	  if (val != 0)
2887 	    switch (code)
2888 	      {
2889 	      case LT_EXPR:
2890 	      case GE_EXPR:
2891 		primop1 = TYPE_MIN_VALUE (type);
2892 		val = 0;
2893 		break;
2894 
2895 	      case LE_EXPR:
2896 	      case GT_EXPR:
2897 		primop1 = TYPE_MAX_VALUE (type);
2898 		val = 0;
2899 		break;
2900 
2901 	      default:
2902 		break;
2903 	      }
2904 	  type = c_common_unsigned_type (type);
2905 	}
2906 
2907       if (TREE_CODE (primop0) != INTEGER_CST
2908 	  /* Don't warn if it's from a (non-system) macro.  */
2909 	  && !(from_macro_expansion_at
2910 	       (expansion_point_location_if_in_system_header
2911 		(EXPR_LOCATION (primop0)))))
2912 	{
2913 	  if (val == truthvalue_false_node)
2914 	    warning_at (loc, OPT_Wtype_limits,
2915 			"comparison is always false due to limited range of data type");
2916 	  if (val == truthvalue_true_node)
2917 	    warning_at (loc, OPT_Wtype_limits,
2918 			"comparison is always true due to limited range of data type");
2919 	}
2920 
2921       if (val != 0)
2922 	{
2923 	  /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
2924 	  if (TREE_SIDE_EFFECTS (primop0))
2925 	    return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2926 	  return val;
2927 	}
2928 
2929       /* Value is not predetermined, but do the comparison
2930 	 in the type of the operand that is not constant.
2931 	 TYPE is already properly set.  */
2932     }
2933 
2934   /* If either arg is decimal float and the other is float, find the
2935      proper common type to use for comparison.  */
2936   else if (real1 && real2
2937 	   && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2938 	   && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
2939     type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2940 
2941   /* If either arg is decimal float and the other is float, fail.  */
2942   else if (real1 && real2
2943 	   && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2944 	       || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
2945     return 0;
2946 
2947   else if (real1 && real2
2948 	   && (TYPE_PRECISION (TREE_TYPE (primop0))
2949 	       == TYPE_PRECISION (TREE_TYPE (primop1))))
2950     type = TREE_TYPE (primop0);
2951 
2952   /* If args' natural types are both narrower than nominal type
2953      and both extend in the same manner, compare them
2954      in the type of the wider arg.
2955      Otherwise must actually extend both to the nominal
2956      common type lest different ways of extending
2957      alter the result.
2958      (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
2959 
2960   else if (unsignedp0 == unsignedp1 && real1 == real2
2961 	   && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2962 	   && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2963     {
2964       type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2965       type = c_common_signed_or_unsigned_type (unsignedp0
2966 					       || TYPE_UNSIGNED (*restype_ptr),
2967 					       type);
2968       /* Make sure shorter operand is extended the right way
2969 	 to match the longer operand.  */
2970       primop0
2971 	= convert (c_common_signed_or_unsigned_type (unsignedp0,
2972 						     TREE_TYPE (primop0)),
2973 		   primop0);
2974       primop1
2975 	= convert (c_common_signed_or_unsigned_type (unsignedp1,
2976 						     TREE_TYPE (primop1)),
2977 		   primop1);
2978     }
2979   else
2980     {
2981       /* Here we must do the comparison on the nominal type
2982 	 using the args exactly as we received them.  */
2983       type = *restype_ptr;
2984       primop0 = op0;
2985       primop1 = op1;
2986 
2987       if (!real1 && !real2 && integer_zerop (primop1)
2988 	  && TYPE_UNSIGNED (*restype_ptr))
2989 	{
2990 	  tree value = 0;
2991 	  /* All unsigned values are >= 0, so we warn.  However,
2992 	     if OP0 is a constant that is >= 0, the signedness of
2993 	     the comparison isn't an issue, so suppress the
2994 	     warning.  */
2995 	  bool warn =
2996 	    warn_type_limits && !in_system_header_at (loc)
2997 	    && !(TREE_CODE (primop0) == INTEGER_CST
2998 		 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
2999 					     primop0)))
3000 	    /* Do not warn for enumeration types.  */
3001 	    && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
3002 
3003 	  switch (code)
3004 	    {
3005 	    case GE_EXPR:
3006 	      if (warn)
3007 		warning_at (loc, OPT_Wtype_limits,
3008 			    "comparison of unsigned expression >= 0 is always true");
3009 	      value = truthvalue_true_node;
3010 	      break;
3011 
3012 	    case LT_EXPR:
3013 	      if (warn)
3014 		warning_at (loc, OPT_Wtype_limits,
3015 			    "comparison of unsigned expression < 0 is always false");
3016 	      value = truthvalue_false_node;
3017 	      break;
3018 
3019 	    default:
3020 	      break;
3021 	    }
3022 
3023 	  if (value != 0)
3024 	    {
3025 	      /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
3026 	      if (TREE_SIDE_EFFECTS (primop0))
3027 		return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3028 			       primop0, value);
3029 	      return value;
3030 	    }
3031 	}
3032     }
3033 
3034   *op0_ptr = convert (type, primop0);
3035   *op1_ptr = convert (type, primop1);
3036 
3037   *restype_ptr = truthvalue_type_node;
3038 
3039   return 0;
3040 }
3041 
3042 /* Return a tree for the sum or difference (RESULTCODE says which)
3043    of pointer PTROP and integer INTOP.  */
3044 
3045 tree
3046 pointer_int_sum (location_t loc, enum tree_code resultcode,
3047 		 tree ptrop, tree intop, bool complain)
3048 {
3049   tree size_exp, ret;
3050 
3051   /* The result is a pointer of the same type that is being added.  */
3052   tree result_type = TREE_TYPE (ptrop);
3053 
3054   if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3055     {
3056       if (complain && warn_pointer_arith)
3057 	pedwarn (loc, OPT_Wpointer_arith,
3058 		 "pointer of type %<void *%> used in arithmetic");
3059       else if (!complain)
3060 	return error_mark_node;
3061       size_exp = integer_one_node;
3062     }
3063   else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3064     {
3065       if (complain && warn_pointer_arith)
3066 	pedwarn (loc, OPT_Wpointer_arith,
3067 		 "pointer to a function used in arithmetic");
3068       else if (!complain)
3069 	return error_mark_node;
3070       size_exp = integer_one_node;
3071     }
3072   else
3073     size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3074 
3075   /* We are manipulating pointer values, so we don't need to warn
3076      about relying on undefined signed overflow.  We disable the
3077      warning here because we use integer types so fold won't know that
3078      they are really pointers.  */
3079   fold_defer_overflow_warnings ();
3080 
3081   /* If what we are about to multiply by the size of the elements
3082      contains a constant term, apply distributive law
3083      and multiply that constant term separately.
3084      This helps produce common subexpressions.  */
3085   if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3086       && !TREE_CONSTANT (intop)
3087       && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3088       && TREE_CONSTANT (size_exp)
3089       /* If the constant comes from pointer subtraction,
3090 	 skip this optimization--it would cause an error.  */
3091       && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3092       /* If the constant is unsigned, and smaller than the pointer size,
3093 	 then we must skip this optimization.  This is because it could cause
3094 	 an overflow error if the constant is negative but INTOP is not.  */
3095       && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3096 	  || (TYPE_PRECISION (TREE_TYPE (intop))
3097 	      == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3098     {
3099       enum tree_code subcode = resultcode;
3100       tree int_type = TREE_TYPE (intop);
3101       if (TREE_CODE (intop) == MINUS_EXPR)
3102 	subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3103       /* Convert both subexpression types to the type of intop,
3104 	 because weird cases involving pointer arithmetic
3105 	 can result in a sum or difference with different type args.  */
3106       ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3107 			       subcode, ptrop,
3108 			       convert (int_type, TREE_OPERAND (intop, 1)), 1);
3109       intop = convert (int_type, TREE_OPERAND (intop, 0));
3110     }
3111 
3112   /* Convert the integer argument to a type the same size as sizetype
3113      so the multiply won't overflow spuriously.  */
3114   if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3115       || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3116     intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3117 					     TYPE_UNSIGNED (sizetype)), intop);
3118 
3119   /* Replace the integer argument with a suitable product by the object size.
3120      Do this multiplication as signed, then convert to the appropriate type
3121      for the pointer operation and disregard an overflow that occurred only
3122      because of the sign-extension change in the latter conversion.  */
3123   {
3124     tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3125 			      convert (TREE_TYPE (intop), size_exp));
3126     intop = convert (sizetype, t);
3127     if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3128       intop = wide_int_to_tree (TREE_TYPE (intop), intop);
3129   }
3130 
3131   /* Create the sum or difference.  */
3132   if (resultcode == MINUS_EXPR)
3133     intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3134 
3135   ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3136 
3137   fold_undefer_and_ignore_overflow_warnings ();
3138 
3139   return ret;
3140 }
3141 
3142 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3143    and if NON_CONST is known not to be permitted in an evaluated part
3144    of a constant expression.  */
3145 
3146 tree
3147 c_wrap_maybe_const (tree expr, bool non_const)
3148 {
3149   bool nowarning = TREE_NO_WARNING (expr);
3150   location_t loc = EXPR_LOCATION (expr);
3151 
3152   /* This should never be called for C++.  */
3153   if (c_dialect_cxx ())
3154     gcc_unreachable ();
3155 
3156   /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING.  */
3157   STRIP_TYPE_NOPS (expr);
3158   expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3159   C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3160   if (nowarning)
3161     TREE_NO_WARNING (expr) = 1;
3162   protected_set_expr_location (expr, loc);
3163 
3164   return expr;
3165 }
3166 
3167 /* Wrap a SAVE_EXPR around EXPR, if appropriate.  Like save_expr, but
3168    for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3169    around the SAVE_EXPR if needed so that c_fully_fold does not need
3170    to look inside SAVE_EXPRs.  */
3171 
3172 tree
3173 c_save_expr (tree expr)
3174 {
3175   bool maybe_const = true;
3176   if (c_dialect_cxx ())
3177     return save_expr (expr);
3178   expr = c_fully_fold (expr, false, &maybe_const);
3179   expr = save_expr (expr);
3180   if (!maybe_const)
3181     expr = c_wrap_maybe_const (expr, true);
3182   return expr;
3183 }
3184 
3185 /* Return whether EXPR is a declaration whose address can never be
3186    NULL.  */
3187 
3188 bool
3189 decl_with_nonnull_addr_p (const_tree expr)
3190 {
3191   return (DECL_P (expr)
3192 	  && (TREE_CODE (expr) == PARM_DECL
3193 	      || TREE_CODE (expr) == LABEL_DECL
3194 	      || !DECL_WEAK (expr)));
3195 }
3196 
3197 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3198    or for an `if' or `while' statement or ?..: exp.  It should already
3199    have been validated to be of suitable type; otherwise, a bad
3200    diagnostic may result.
3201 
3202    The EXPR is located at LOCATION.
3203 
3204    This preparation consists of taking the ordinary
3205    representation of an expression expr and producing a valid tree
3206    boolean expression describing whether expr is nonzero.  We could
3207    simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3208    but we optimize comparisons, &&, ||, and !.
3209 
3210    The resulting type should always be `truthvalue_type_node'.  */
3211 
3212 tree
3213 c_common_truthvalue_conversion (location_t location, tree expr)
3214 {
3215   switch (TREE_CODE (expr))
3216     {
3217     case EQ_EXPR:   case NE_EXPR:   case UNEQ_EXPR: case LTGT_EXPR:
3218     case LE_EXPR:   case GE_EXPR:   case LT_EXPR:   case GT_EXPR:
3219     case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3220     case ORDERED_EXPR: case UNORDERED_EXPR:
3221       if (TREE_TYPE (expr) == truthvalue_type_node)
3222 	return expr;
3223       expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3224 		     TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3225       goto ret;
3226 
3227     case TRUTH_ANDIF_EXPR:
3228     case TRUTH_ORIF_EXPR:
3229     case TRUTH_AND_EXPR:
3230     case TRUTH_OR_EXPR:
3231     case TRUTH_XOR_EXPR:
3232       if (TREE_TYPE (expr) == truthvalue_type_node)
3233 	return expr;
3234       expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3235 		     c_common_truthvalue_conversion (location,
3236 						     TREE_OPERAND (expr, 0)),
3237 		     c_common_truthvalue_conversion (location,
3238 						     TREE_OPERAND (expr, 1)));
3239       goto ret;
3240 
3241     case TRUTH_NOT_EXPR:
3242       if (TREE_TYPE (expr) == truthvalue_type_node)
3243 	return expr;
3244       expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3245 		     c_common_truthvalue_conversion (location,
3246 						     TREE_OPERAND (expr, 0)));
3247       goto ret;
3248 
3249     case ERROR_MARK:
3250       return expr;
3251 
3252     case INTEGER_CST:
3253       if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3254 	  && !integer_zerop (expr)
3255 	  && !integer_onep (expr))
3256 	warning_at (location, OPT_Wint_in_bool_context,
3257 		    "enum constant in boolean context");
3258       return integer_zerop (expr) ? truthvalue_false_node
3259 				  : truthvalue_true_node;
3260 
3261     case REAL_CST:
3262       return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3263 	     ? truthvalue_true_node
3264 	     : truthvalue_false_node;
3265 
3266     case FIXED_CST:
3267       return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3268 			    &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3269 	     ? truthvalue_true_node
3270 	     : truthvalue_false_node;
3271 
3272     case FUNCTION_DECL:
3273       expr = build_unary_op (location, ADDR_EXPR, expr, false);
3274       /* Fall through.  */
3275 
3276     case ADDR_EXPR:
3277       {
3278  	tree inner = TREE_OPERAND (expr, 0);
3279 	if (decl_with_nonnull_addr_p (inner))
3280 	  {
3281 	    /* Common Ada/Pascal programmer's mistake.  */
3282 	    warning_at (location,
3283 			OPT_Waddress,
3284 			"the address of %qD will always evaluate as %<true%>",
3285 			inner);
3286 	    return truthvalue_true_node;
3287 	  }
3288 	break;
3289       }
3290 
3291     case COMPLEX_EXPR:
3292       expr = build_binary_op (EXPR_LOCATION (expr),
3293 			      (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3294 			       ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3295 		c_common_truthvalue_conversion (location,
3296 						TREE_OPERAND (expr, 0)),
3297 		c_common_truthvalue_conversion (location,
3298 						TREE_OPERAND (expr, 1)),
3299 			      0);
3300       goto ret;
3301 
3302     case NEGATE_EXPR:
3303     case ABS_EXPR:
3304     case FLOAT_EXPR:
3305     case EXCESS_PRECISION_EXPR:
3306       /* These don't change whether an object is nonzero or zero.  */
3307       return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3308 
3309     case LROTATE_EXPR:
3310     case RROTATE_EXPR:
3311       /* These don't change whether an object is zero or nonzero, but
3312 	 we can't ignore them if their second arg has side-effects.  */
3313       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3314 	{
3315 	  expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3316 			 TREE_OPERAND (expr, 1),
3317 			 c_common_truthvalue_conversion
3318 			 (location, TREE_OPERAND (expr, 0)));
3319 	  goto ret;
3320 	}
3321       else
3322 	return c_common_truthvalue_conversion (location,
3323 					       TREE_OPERAND (expr, 0));
3324 
3325     case MULT_EXPR:
3326       warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3327 		  "%<*%> in boolean context, suggest %<&&%> instead");
3328       break;
3329 
3330     case LSHIFT_EXPR:
3331       /* We will only warn on signed shifts here, because the majority of
3332 	 false positive warnings happen in code where unsigned arithmetic
3333 	 was used in anticipation of a possible overflow.
3334 	 Furthermore, if we see an unsigned type here we know that the
3335 	 result of the shift is not subject to integer promotion rules.  */
3336       if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3337 	  && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3338 	warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3339 		    "%<<<%> in boolean context, did you mean %<<%> ?");
3340       break;
3341 
3342     case COND_EXPR:
3343       if (warn_int_in_bool_context
3344 	  && !from_macro_definition_at (EXPR_LOCATION (expr)))
3345 	{
3346 	  tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3347 	  tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3348 	  if (TREE_CODE (val1) == INTEGER_CST
3349 	      && TREE_CODE (val2) == INTEGER_CST
3350 	      && !integer_zerop (val1)
3351 	      && !integer_zerop (val2)
3352 	      && (!integer_onep (val1)
3353 		  || !integer_onep (val2)))
3354 	    warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3355 			"?: using integer constants in boolean context, "
3356 			"the expression will always evaluate to %<true%>");
3357 	  else if ((TREE_CODE (val1) == INTEGER_CST
3358 		    && !integer_zerop (val1)
3359 		    && !integer_onep (val1))
3360 		   || (TREE_CODE (val2) == INTEGER_CST
3361 		       && !integer_zerop (val2)
3362 		       && !integer_onep (val2)))
3363 	    warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3364 			"?: using integer constants in boolean context");
3365 	}
3366       /* Distribute the conversion into the arms of a COND_EXPR.  */
3367       if (c_dialect_cxx ())
3368 	/* Avoid premature folding.  */
3369 	break;
3370       else
3371 	{
3372 	  int w = warn_int_in_bool_context;
3373 	  warn_int_in_bool_context = 0;
3374 	  /* Folding will happen later for C.  */
3375 	  expr = build3 (COND_EXPR, truthvalue_type_node,
3376 			 TREE_OPERAND (expr, 0),
3377 			 c_common_truthvalue_conversion (location,
3378 							 TREE_OPERAND (expr, 1)),
3379 			 c_common_truthvalue_conversion (location,
3380 							 TREE_OPERAND (expr, 2)));
3381 	  warn_int_in_bool_context = w;
3382 	  goto ret;
3383 	}
3384 
3385     CASE_CONVERT:
3386       {
3387 	tree totype = TREE_TYPE (expr);
3388 	tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3389 
3390 	if (POINTER_TYPE_P (totype)
3391 	    && !c_inhibit_evaluation_warnings
3392 	    && TREE_CODE (fromtype) == REFERENCE_TYPE)
3393 	  {
3394 	    tree inner = expr;
3395 	    STRIP_NOPS (inner);
3396 
3397 	    if (DECL_P (inner))
3398 	      warning_at (location,
3399 			  OPT_Waddress,
3400 			  "the compiler can assume that the address of "
3401 			  "%qD will always evaluate to %<true%>",
3402 			  inner);
3403 	  }
3404 
3405 	/* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3406 	   since that affects how `default_conversion' will behave.  */
3407 	if (TREE_CODE (totype) == REFERENCE_TYPE
3408 	    || TREE_CODE (fromtype) == REFERENCE_TYPE)
3409 	  break;
3410 	/* Don't strip a conversion from C++0x scoped enum, since they
3411 	   don't implicitly convert to other types.  */
3412 	if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3413 	    && ENUM_IS_SCOPED (fromtype))
3414 	  break;
3415 	/* If this isn't narrowing the argument, we can ignore it.  */
3416 	if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3417 	  return c_common_truthvalue_conversion (location,
3418 						 TREE_OPERAND (expr, 0));
3419       }
3420       break;
3421 
3422     case MODIFY_EXPR:
3423       if (!TREE_NO_WARNING (expr)
3424 	  && warn_parentheses)
3425 	{
3426 	  warning_at (location, OPT_Wparentheses,
3427 		      "suggest parentheses around assignment used as "
3428 		      "truth value");
3429 	  TREE_NO_WARNING (expr) = 1;
3430 	}
3431       break;
3432 
3433     default:
3434       break;
3435     }
3436 
3437   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3438     {
3439       tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
3440       expr = (build_binary_op
3441 	      (EXPR_LOCATION (expr),
3442 	       (TREE_SIDE_EFFECTS (expr)
3443 		? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3444 	c_common_truthvalue_conversion
3445 	       (location,
3446 		build_unary_op (location, REALPART_EXPR, t, false)),
3447 	c_common_truthvalue_conversion
3448 	       (location,
3449 		build_unary_op (location, IMAGPART_EXPR, t, false)),
3450 	       0));
3451       goto ret;
3452     }
3453 
3454   if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3455     {
3456       tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3457 					  FCONST0 (TYPE_MODE
3458 						   (TREE_TYPE (expr))));
3459       return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
3460     }
3461   else
3462     return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
3463 
3464  ret:
3465   protected_set_expr_location (expr, location);
3466   return expr;
3467 }
3468 
3469 static void def_builtin_1  (enum built_in_function fncode,
3470 			    const char *name,
3471 			    enum built_in_class fnclass,
3472 			    tree fntype, tree libtype,
3473 			    bool both_p, bool fallback_p, bool nonansi_p,
3474 			    tree fnattrs, bool implicit_p);
3475 
3476 
3477 /* Apply the TYPE_QUALS to the new DECL.  */
3478 
3479 void
3480 c_apply_type_quals_to_decl (int type_quals, tree decl)
3481 {
3482   tree type = TREE_TYPE (decl);
3483 
3484   if (type == error_mark_node)
3485     return;
3486 
3487   if ((type_quals & TYPE_QUAL_CONST)
3488       || (type && TREE_CODE (type) == REFERENCE_TYPE))
3489     /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3490        constructor can produce constant init, so rely on cp_finish_decl to
3491        clear TREE_READONLY if the variable has non-constant init.  */
3492     TREE_READONLY (decl) = 1;
3493   if (type_quals & TYPE_QUAL_VOLATILE)
3494     {
3495       TREE_SIDE_EFFECTS (decl) = 1;
3496       TREE_THIS_VOLATILE (decl) = 1;
3497     }
3498   if (type_quals & TYPE_QUAL_RESTRICT)
3499     {
3500       while (type && TREE_CODE (type) == ARRAY_TYPE)
3501 	/* Allow 'restrict' on arrays of pointers.
3502 	   FIXME currently we just ignore it.  */
3503 	type = TREE_TYPE (type);
3504       if (!type
3505 	  || !POINTER_TYPE_P (type)
3506 	  || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3507 	error ("invalid use of %<restrict%>");
3508     }
3509 }
3510 
3511 struct c_type_hasher : ggc_ptr_hash<tree_node>
3512 {
3513   static hashval_t hash (tree);
3514   static bool equal (tree, tree);
3515 };
3516 
3517 /* Hash function for the problem of multiple type definitions in
3518    different files.  This must hash all types that will compare
3519    equal via comptypes to the same value.  In practice it hashes
3520    on some of the simple stuff and leaves the details to comptypes.  */
3521 
3522 hashval_t
3523 c_type_hasher::hash (tree t)
3524 {
3525   int n_elements;
3526   int shift, size;
3527   tree t2;
3528   switch (TREE_CODE (t))
3529     {
3530     /* For pointers, hash on pointee type plus some swizzling.  */
3531     case POINTER_TYPE:
3532       return hash (TREE_TYPE (t)) ^ 0x3003003;
3533     /* Hash on number of elements and total size.  */
3534     case ENUMERAL_TYPE:
3535       shift = 3;
3536       t2 = TYPE_VALUES (t);
3537       break;
3538     case RECORD_TYPE:
3539       shift = 0;
3540       t2 = TYPE_FIELDS (t);
3541       break;
3542     case QUAL_UNION_TYPE:
3543       shift = 1;
3544       t2 = TYPE_FIELDS (t);
3545       break;
3546     case UNION_TYPE:
3547       shift = 2;
3548       t2 = TYPE_FIELDS (t);
3549       break;
3550     default:
3551       gcc_unreachable ();
3552     }
3553   /* FIXME: We want to use a DECL_CHAIN iteration method here, but
3554      TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST.  */
3555   n_elements = list_length (t2);
3556   /* We might have a VLA here.  */
3557   if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
3558     size = 0;
3559   else
3560     size = TREE_INT_CST_LOW (TYPE_SIZE (t));
3561   return ((size << 24) | (n_elements << shift));
3562 }
3563 
3564 bool
3565 c_type_hasher::equal (tree t1, tree t2)
3566 {
3567   return lang_hooks.types_compatible_p (t1, t2);
3568 }
3569 
3570 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
3571 
3572 /* Return the typed-based alias set for T, which may be an expression
3573    or a type.  Return -1 if we don't do anything special.  */
3574 
3575 alias_set_type
3576 c_common_get_alias_set (tree t)
3577 {
3578   /* For VLAs, use the alias set of the element type rather than the
3579      default of alias set 0 for types compared structurally.  */
3580   if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3581     {
3582       if (TREE_CODE (t) == ARRAY_TYPE)
3583 	return get_alias_set (TREE_TYPE (t));
3584       return -1;
3585     }
3586 
3587   /* That's all the expressions we handle specially.  */
3588   if (!TYPE_P (t))
3589     return -1;
3590 
3591   /* The C standard guarantees that any object may be accessed via an
3592      lvalue that has character type.  */
3593   if (t == char_type_node
3594       || t == signed_char_type_node
3595       || t == unsigned_char_type_node)
3596     return 0;
3597 
3598   /* The C standard specifically allows aliasing between signed and
3599      unsigned variants of the same type.  We treat the signed
3600      variant as canonical.  */
3601   if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
3602     {
3603       tree t1 = c_common_signed_type (t);
3604 
3605       /* t1 == t can happen for boolean nodes which are always unsigned.  */
3606       if (t1 != t)
3607 	return get_alias_set (t1);
3608     }
3609 
3610   /* Handle the case of multiple type nodes referring to "the same" type,
3611      which occurs with IMA.  These share an alias set.  FIXME:  Currently only
3612      C90 is handled.  (In C99 type compatibility is not transitive, which
3613      complicates things mightily. The alias set splay trees can theoretically
3614      represent this, but insertion is tricky when you consider all the
3615      different orders things might arrive in.) */
3616 
3617   if (c_language != clk_c || flag_isoc99)
3618     return -1;
3619 
3620   /* Save time if there's only one input file.  */
3621   if (num_in_fnames == 1)
3622     return -1;
3623 
3624   /* Pointers need special handling if they point to any type that
3625      needs special handling (below).  */
3626   if (TREE_CODE (t) == POINTER_TYPE)
3627     {
3628       tree t2;
3629       /* Find bottom type under any nested POINTERs.  */
3630       for (t2 = TREE_TYPE (t);
3631 	   TREE_CODE (t2) == POINTER_TYPE;
3632 	   t2 = TREE_TYPE (t2))
3633 	;
3634       if (!RECORD_OR_UNION_TYPE_P (t2)
3635 	  && TREE_CODE (t2) != ENUMERAL_TYPE)
3636 	return -1;
3637       if (TYPE_SIZE (t2) == 0)
3638 	return -1;
3639     }
3640   /* These are the only cases that need special handling.  */
3641   if (!RECORD_OR_UNION_TYPE_P (t)
3642       && TREE_CODE (t) != ENUMERAL_TYPE
3643       && TREE_CODE (t) != POINTER_TYPE)
3644     return -1;
3645   /* Undefined? */
3646   if (TYPE_SIZE (t) == 0)
3647     return -1;
3648 
3649   /* Look up t in hash table.  Only one of the compatible types within each
3650      alias set is recorded in the table.  */
3651   if (!type_hash_table)
3652     type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
3653   tree *slot = type_hash_table->find_slot (t, INSERT);
3654   if (*slot != NULL)
3655     {
3656       TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
3657       return TYPE_ALIAS_SET ((tree)*slot);
3658     }
3659   else
3660     /* Our caller will assign and record (in t) a new alias set; all we need
3661        to do is remember t in the hash table.  */
3662     *slot = t;
3663 
3664   return -1;
3665 }
3666 
3667 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3668    the IS_SIZEOF parameter indicates which operator is being applied.
3669    The COMPLAIN flag controls whether we should diagnose possibly
3670    ill-formed constructs or not.  LOC is the location of the SIZEOF or
3671    TYPEOF operator.  If MIN_ALIGNOF, the least alignment required for
3672    a type in any context should be returned, rather than the normal
3673    alignment for that type.  */
3674 
3675 tree
3676 c_sizeof_or_alignof_type (location_t loc,
3677 			  tree type, bool is_sizeof, bool min_alignof,
3678 			  int complain)
3679 {
3680   const char *op_name;
3681   tree value = NULL;
3682   enum tree_code type_code = TREE_CODE (type);
3683 
3684   op_name = is_sizeof ? "sizeof" : "__alignof__";
3685 
3686   if (type_code == FUNCTION_TYPE)
3687     {
3688       if (is_sizeof)
3689 	{
3690 	  if (complain && warn_pointer_arith)
3691 	    pedwarn (loc, OPT_Wpointer_arith,
3692 		     "invalid application of %<sizeof%> to a function type");
3693           else if (!complain)
3694             return error_mark_node;
3695 	  value = size_one_node;
3696 	}
3697       else
3698 	{
3699 	  if (complain)
3700 	    {
3701 	      if (c_dialect_cxx ())
3702 		pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3703 			 "%<alignof%> applied to a function type");
3704 	      else
3705 		pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3706 			 "%<_Alignof%> applied to a function type");
3707 	    }
3708 	  value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3709 	}
3710     }
3711   else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3712     {
3713       if (type_code == VOID_TYPE
3714 	  && complain && warn_pointer_arith)
3715 	pedwarn (loc, OPT_Wpointer_arith,
3716 		 "invalid application of %qs to a void type", op_name);
3717       else if (!complain)
3718         return error_mark_node;
3719       value = size_one_node;
3720     }
3721   else if (!COMPLETE_TYPE_P (type)
3722 	   && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
3723     {
3724       if (complain)
3725 	error_at (loc, "invalid application of %qs to incomplete type %qT",
3726 		  op_name, type);
3727       return error_mark_node;
3728     }
3729   else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3730 	   && !COMPLETE_TYPE_P (TREE_TYPE (type)))
3731     {
3732       if (complain)
3733 	error_at (loc, "invalid application of %qs to array type %qT of "
3734 		  "incomplete element type", op_name, type);
3735       return error_mark_node;
3736     }
3737   else
3738     {
3739       if (is_sizeof)
3740 	/* Convert in case a char is more than one unit.  */
3741 	value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3742 				size_int (TYPE_PRECISION (char_type_node)
3743 					  / BITS_PER_UNIT));
3744       else if (min_alignof)
3745 	value = size_int (min_align_of_type (type));
3746       else
3747 	value = size_int (TYPE_ALIGN_UNIT (type));
3748     }
3749 
3750   /* VALUE will have the middle-end integer type sizetype.
3751      However, we should really return a value of type `size_t',
3752      which is just a typedef for an ordinary integer type.  */
3753   value = fold_convert_loc (loc, size_type_node, value);
3754 
3755   return value;
3756 }
3757 
3758 /* Implement the __alignof keyword: Return the minimum required
3759    alignment of EXPR, measured in bytes.  For VAR_DECLs,
3760    FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3761    from an "aligned" __attribute__ specification).  LOC is the
3762    location of the ALIGNOF operator.  */
3763 
3764 tree
3765 c_alignof_expr (location_t loc, tree expr)
3766 {
3767   tree t;
3768 
3769   if (VAR_OR_FUNCTION_DECL_P (expr))
3770     t = size_int (DECL_ALIGN_UNIT (expr));
3771 
3772   else if (TREE_CODE (expr) == COMPONENT_REF
3773 	   && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3774     {
3775       error_at (loc, "%<__alignof%> applied to a bit-field");
3776       t = size_one_node;
3777     }
3778   else if (TREE_CODE (expr) == COMPONENT_REF
3779 	   && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
3780     t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
3781 
3782   else if (INDIRECT_REF_P (expr))
3783     {
3784       tree t = TREE_OPERAND (expr, 0);
3785       tree best = t;
3786       int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3787 
3788       while (CONVERT_EXPR_P (t)
3789 	     && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
3790 	{
3791 	  int thisalign;
3792 
3793 	  t = TREE_OPERAND (t, 0);
3794 	  thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3795 	  if (thisalign > bestalign)
3796 	    best = t, bestalign = thisalign;
3797 	}
3798       return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
3799     }
3800   else
3801     return c_alignof (loc, TREE_TYPE (expr));
3802 
3803   return fold_convert_loc (loc, size_type_node, t);
3804 }
3805 
3806 /* Handle C and C++ default attributes.  */
3807 
3808 enum built_in_attribute
3809 {
3810 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3811 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3812 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3813 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3814 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3815 #include "builtin-attrs.def"
3816 #undef DEF_ATTR_NULL_TREE
3817 #undef DEF_ATTR_INT
3818 #undef DEF_ATTR_STRING
3819 #undef DEF_ATTR_IDENT
3820 #undef DEF_ATTR_TREE_LIST
3821   ATTR_LAST
3822 };
3823 
3824 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3825 
3826 static void c_init_attributes (void);
3827 
3828 enum c_builtin_type
3829 {
3830 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3831 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3832 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3833 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3834 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3835 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3836 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3837 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3838 			    ARG6) NAME,
3839 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3840 			    ARG6, ARG7) NAME,
3841 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3842 			    ARG6, ARG7, ARG8) NAME,
3843 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3844 			    ARG6, ARG7, ARG8, ARG9) NAME,
3845 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3846 			     ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3847 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3848 			     ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3849 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3850 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3851 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3852 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3853 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3854 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3855 				NAME,
3856 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3857 				ARG6) NAME,
3858 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3859 				ARG6, ARG7) NAME,
3860 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3861 #include "builtin-types.def"
3862 #undef DEF_PRIMITIVE_TYPE
3863 #undef DEF_FUNCTION_TYPE_0
3864 #undef DEF_FUNCTION_TYPE_1
3865 #undef DEF_FUNCTION_TYPE_2
3866 #undef DEF_FUNCTION_TYPE_3
3867 #undef DEF_FUNCTION_TYPE_4
3868 #undef DEF_FUNCTION_TYPE_5
3869 #undef DEF_FUNCTION_TYPE_6
3870 #undef DEF_FUNCTION_TYPE_7
3871 #undef DEF_FUNCTION_TYPE_8
3872 #undef DEF_FUNCTION_TYPE_9
3873 #undef DEF_FUNCTION_TYPE_10
3874 #undef DEF_FUNCTION_TYPE_11
3875 #undef DEF_FUNCTION_TYPE_VAR_0
3876 #undef DEF_FUNCTION_TYPE_VAR_1
3877 #undef DEF_FUNCTION_TYPE_VAR_2
3878 #undef DEF_FUNCTION_TYPE_VAR_3
3879 #undef DEF_FUNCTION_TYPE_VAR_4
3880 #undef DEF_FUNCTION_TYPE_VAR_5
3881 #undef DEF_FUNCTION_TYPE_VAR_6
3882 #undef DEF_FUNCTION_TYPE_VAR_7
3883 #undef DEF_POINTER_TYPE
3884   BT_LAST
3885 };
3886 
3887 typedef enum c_builtin_type builtin_type;
3888 
3889 /* A temporary array for c_common_nodes_and_builtins.  Used in
3890    communication with def_fn_type.  */
3891 static tree builtin_types[(int) BT_LAST + 1];
3892 
3893 /* A helper function for c_common_nodes_and_builtins.  Build function type
3894    for DEF with return type RET and N arguments.  If VAR is true, then the
3895    function should be variadic after those N arguments.
3896 
3897    Takes special care not to ICE if any of the types involved are
3898    error_mark_node, which indicates that said type is not in fact available
3899    (see builtin_type_for_size).  In which case the function type as a whole
3900    should be error_mark_node.  */
3901 
3902 static void
3903 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3904 {
3905   tree t;
3906   tree *args = XALLOCAVEC (tree, n);
3907   va_list list;
3908   int i;
3909 
3910   va_start (list, n);
3911   for (i = 0; i < n; ++i)
3912     {
3913       builtin_type a = (builtin_type) va_arg (list, int);
3914       t = builtin_types[a];
3915       if (t == error_mark_node)
3916 	goto egress;
3917       args[i] = t;
3918     }
3919 
3920   t = builtin_types[ret];
3921   if (t == error_mark_node)
3922     goto egress;
3923   if (var)
3924     t = build_varargs_function_type_array (t, n, args);
3925   else
3926     t = build_function_type_array (t, n, args);
3927 
3928  egress:
3929   builtin_types[def] = t;
3930   va_end (list);
3931 }
3932 
3933 /* Build builtin functions common to both C and C++ language
3934    frontends.  */
3935 
3936 static void
3937 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3938 {
3939 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3940   builtin_types[ENUM] = VALUE;
3941 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3942   def_fn_type (ENUM, RETURN, 0, 0);
3943 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3944   def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3945 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3946   def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3947 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3948   def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3949 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3950   def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3951 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5)	\
3952   def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3953 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3954 			    ARG6)					\
3955   def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3956 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3957 			    ARG6, ARG7)					\
3958   def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3959 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3960 			    ARG6, ARG7, ARG8)				\
3961   def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	\
3962 	       ARG7, ARG8);
3963 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3964 			    ARG6, ARG7, ARG8, ARG9)			\
3965   def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	\
3966 	       ARG7, ARG8, ARG9);
3967 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3968 			     ARG6, ARG7, ARG8, ARG9, ARG10)		 \
3969   def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	 \
3970 	       ARG7, ARG8, ARG9, ARG10);
3971 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3972 			     ARG6, ARG7, ARG8, ARG9, ARG10, ARG11)	 \
3973   def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	 \
3974 	       ARG7, ARG8, ARG9, ARG10, ARG11);
3975 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3976   def_fn_type (ENUM, RETURN, 1, 0);
3977 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3978   def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3979 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3980   def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3981 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3982   def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3983 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3984   def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3985 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3986   def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3987 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3988 				ARG6) \
3989   def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3990 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3991 				ARG6, ARG7)				\
3992   def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3993 #define DEF_POINTER_TYPE(ENUM, TYPE) \
3994   builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
3995 
3996 #include "builtin-types.def"
3997 
3998 #undef DEF_PRIMITIVE_TYPE
3999 #undef DEF_FUNCTION_TYPE_0
4000 #undef DEF_FUNCTION_TYPE_1
4001 #undef DEF_FUNCTION_TYPE_2
4002 #undef DEF_FUNCTION_TYPE_3
4003 #undef DEF_FUNCTION_TYPE_4
4004 #undef DEF_FUNCTION_TYPE_5
4005 #undef DEF_FUNCTION_TYPE_6
4006 #undef DEF_FUNCTION_TYPE_7
4007 #undef DEF_FUNCTION_TYPE_8
4008 #undef DEF_FUNCTION_TYPE_9
4009 #undef DEF_FUNCTION_TYPE_10
4010 #undef DEF_FUNCTION_TYPE_11
4011 #undef DEF_FUNCTION_TYPE_VAR_0
4012 #undef DEF_FUNCTION_TYPE_VAR_1
4013 #undef DEF_FUNCTION_TYPE_VAR_2
4014 #undef DEF_FUNCTION_TYPE_VAR_3
4015 #undef DEF_FUNCTION_TYPE_VAR_4
4016 #undef DEF_FUNCTION_TYPE_VAR_5
4017 #undef DEF_FUNCTION_TYPE_VAR_6
4018 #undef DEF_FUNCTION_TYPE_VAR_7
4019 #undef DEF_POINTER_TYPE
4020   builtin_types[(int) BT_LAST] = NULL_TREE;
4021 
4022   c_init_attributes ();
4023 
4024 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4025 		    NONANSI_P, ATTRS, IMPLICIT, COND)			\
4026   if (NAME && COND)							\
4027     def_builtin_1 (ENUM, NAME, CLASS,                                   \
4028 		   builtin_types[(int) TYPE],                           \
4029 		   builtin_types[(int) LIBTYPE],                        \
4030 		   BOTH_P, FALLBACK_P, NONANSI_P,                       \
4031 		   built_in_attributes[(int) ATTRS], IMPLICIT);
4032 #include "builtins.def"
4033 
4034   targetm.init_builtins ();
4035 
4036   build_common_builtin_nodes ();
4037 
4038   if (flag_cilkplus)
4039     cilk_init_builtins ();
4040 }
4041 
4042 /* Like get_identifier, but avoid warnings about null arguments when
4043    the argument may be NULL for targets where GCC lacks stdint.h type
4044    information.  */
4045 
4046 static inline tree
4047 c_get_ident (const char *id)
4048 {
4049   return get_identifier (id);
4050 }
4051 
4052 /* Build tree nodes and builtin functions common to both C and C++ language
4053    frontends.  */
4054 
4055 void
4056 c_common_nodes_and_builtins (void)
4057 {
4058   int char16_type_size;
4059   int char32_type_size;
4060   int wchar_type_size;
4061   tree array_domain_type;
4062   tree va_list_ref_type_node;
4063   tree va_list_arg_type_node;
4064   int i;
4065 
4066   build_common_tree_nodes (flag_signed_char);
4067 
4068   /* Define `int' and `char' first so that dbx will output them first.  */
4069   record_builtin_type (RID_INT, NULL, integer_type_node);
4070   record_builtin_type (RID_CHAR, "char", char_type_node);
4071 
4072   /* `signed' is the same as `int'.  FIXME: the declarations of "signed",
4073      "unsigned long", "long long unsigned" and "unsigned short" were in C++
4074      but not C.  Are the conditionals here needed?  */
4075   if (c_dialect_cxx ())
4076     record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4077   record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4078   record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4079   record_builtin_type (RID_MAX, "long unsigned int",
4080 		       long_unsigned_type_node);
4081 
4082   for (i = 0; i < NUM_INT_N_ENTS; i ++)
4083     {
4084       char name[25];
4085 
4086       sprintf (name, "__int%d", int_n_data[i].bitsize);
4087       record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4088 			   int_n_trees[i].signed_type);
4089       sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
4090       record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4091     }
4092 
4093   if (c_dialect_cxx ())
4094     record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4095   record_builtin_type (RID_MAX, "long long int",
4096 		       long_long_integer_type_node);
4097   record_builtin_type (RID_MAX, "long long unsigned int",
4098 		       long_long_unsigned_type_node);
4099   if (c_dialect_cxx ())
4100     record_builtin_type (RID_MAX, "long long unsigned",
4101 			 long_long_unsigned_type_node);
4102   record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4103   record_builtin_type (RID_MAX, "short unsigned int",
4104 		       short_unsigned_type_node);
4105   if (c_dialect_cxx ())
4106     record_builtin_type (RID_MAX, "unsigned short",
4107 			 short_unsigned_type_node);
4108 
4109   /* Define both `signed char' and `unsigned char'.  */
4110   record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4111   record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4112 
4113   /* These are types that c_common_type_for_size and
4114      c_common_type_for_mode use.  */
4115   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4116 					 TYPE_DECL, NULL_TREE,
4117 					 intQI_type_node));
4118   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4119 					 TYPE_DECL, NULL_TREE,
4120 					 intHI_type_node));
4121   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4122 					 TYPE_DECL, NULL_TREE,
4123 					 intSI_type_node));
4124   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4125 					 TYPE_DECL, NULL_TREE,
4126 					 intDI_type_node));
4127 #if HOST_BITS_PER_WIDE_INT >= 64
4128   /* Note that this is different than the __int128 type that's part of
4129      the generic __intN support.  */
4130   if (targetm.scalar_mode_supported_p (TImode))
4131     lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4132 					   TYPE_DECL,
4133 					   get_identifier ("__int128_t"),
4134 					   intTI_type_node));
4135 #endif
4136   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4137 					 TYPE_DECL, NULL_TREE,
4138 					 unsigned_intQI_type_node));
4139   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4140 					 TYPE_DECL, NULL_TREE,
4141 					 unsigned_intHI_type_node));
4142   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4143 					 TYPE_DECL, NULL_TREE,
4144 					 unsigned_intSI_type_node));
4145   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4146 					 TYPE_DECL, NULL_TREE,
4147 					 unsigned_intDI_type_node));
4148 #if HOST_BITS_PER_WIDE_INT >= 64
4149   if (targetm.scalar_mode_supported_p (TImode))
4150     lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4151 					   TYPE_DECL,
4152 					   get_identifier ("__uint128_t"),
4153 					   unsigned_intTI_type_node));
4154 #endif
4155 
4156   /* Create the widest literal types.  */
4157   if (targetm.scalar_mode_supported_p (TImode))
4158     {
4159       widest_integer_literal_type_node = intTI_type_node;
4160       widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4161     }
4162   else
4163     {
4164       widest_integer_literal_type_node = intDI_type_node;
4165       widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4166     }
4167 
4168   signed_size_type_node = c_common_signed_type (size_type_node);
4169 
4170   pid_type_node =
4171     TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4172 
4173   record_builtin_type (RID_FLOAT, NULL, float_type_node);
4174   record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4175   record_builtin_type (RID_MAX, "long double", long_double_type_node);
4176 
4177   if (!c_dialect_cxx ())
4178     for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4179       if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4180 	record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4181 			     FLOATN_NX_TYPE_NODE (i));
4182 
4183   /* Only supported decimal floating point extension if the target
4184      actually supports underlying modes. */
4185   if (targetm.scalar_mode_supported_p (SDmode)
4186       && targetm.scalar_mode_supported_p (DDmode)
4187       && targetm.scalar_mode_supported_p (TDmode))
4188     {
4189       record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4190       record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4191       record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4192     }
4193 
4194   if (targetm.fixed_point_supported_p ())
4195     {
4196       record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4197       record_builtin_type (RID_FRACT, NULL, fract_type_node);
4198       record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4199       record_builtin_type (RID_MAX, "long long _Fract",
4200 			   long_long_fract_type_node);
4201       record_builtin_type (RID_MAX, "unsigned short _Fract",
4202 			   unsigned_short_fract_type_node);
4203       record_builtin_type (RID_MAX, "unsigned _Fract",
4204 			   unsigned_fract_type_node);
4205       record_builtin_type (RID_MAX, "unsigned long _Fract",
4206 			   unsigned_long_fract_type_node);
4207       record_builtin_type (RID_MAX, "unsigned long long _Fract",
4208 			   unsigned_long_long_fract_type_node);
4209       record_builtin_type (RID_MAX, "_Sat short _Fract",
4210 			   sat_short_fract_type_node);
4211       record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4212       record_builtin_type (RID_MAX, "_Sat long _Fract",
4213 			   sat_long_fract_type_node);
4214       record_builtin_type (RID_MAX, "_Sat long long _Fract",
4215 			   sat_long_long_fract_type_node);
4216       record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4217 			   sat_unsigned_short_fract_type_node);
4218       record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4219 			   sat_unsigned_fract_type_node);
4220       record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4221 			   sat_unsigned_long_fract_type_node);
4222       record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4223 			   sat_unsigned_long_long_fract_type_node);
4224       record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4225       record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4226       record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4227       record_builtin_type (RID_MAX, "long long _Accum",
4228 			   long_long_accum_type_node);
4229       record_builtin_type (RID_MAX, "unsigned short _Accum",
4230 			   unsigned_short_accum_type_node);
4231       record_builtin_type (RID_MAX, "unsigned _Accum",
4232 			   unsigned_accum_type_node);
4233       record_builtin_type (RID_MAX, "unsigned long _Accum",
4234 			   unsigned_long_accum_type_node);
4235       record_builtin_type (RID_MAX, "unsigned long long _Accum",
4236 			   unsigned_long_long_accum_type_node);
4237       record_builtin_type (RID_MAX, "_Sat short _Accum",
4238 			   sat_short_accum_type_node);
4239       record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4240       record_builtin_type (RID_MAX, "_Sat long _Accum",
4241 			   sat_long_accum_type_node);
4242       record_builtin_type (RID_MAX, "_Sat long long _Accum",
4243 			  sat_long_long_accum_type_node);
4244       record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4245 			   sat_unsigned_short_accum_type_node);
4246       record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4247 			   sat_unsigned_accum_type_node);
4248       record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4249 			   sat_unsigned_long_accum_type_node);
4250       record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4251 			   sat_unsigned_long_long_accum_type_node);
4252 
4253     }
4254 
4255   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4256 					 TYPE_DECL,
4257 					 get_identifier ("complex int"),
4258 					 complex_integer_type_node));
4259   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4260 					 TYPE_DECL,
4261 					 get_identifier ("complex float"),
4262 					 complex_float_type_node));
4263   lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4264 					 TYPE_DECL,
4265 					 get_identifier ("complex double"),
4266 					 complex_double_type_node));
4267   lang_hooks.decls.pushdecl
4268     (build_decl (UNKNOWN_LOCATION,
4269 		 TYPE_DECL, get_identifier ("complex long double"),
4270 		 complex_long_double_type_node));
4271 
4272   if (!c_dialect_cxx ())
4273     for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4274       if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4275 	{
4276 	  char buf[30];
4277 	  sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4278 		   floatn_nx_types[i].extended ? "x" : "");
4279 	  lang_hooks.decls.pushdecl
4280 	    (build_decl (UNKNOWN_LOCATION,
4281 			 TYPE_DECL,
4282 			 get_identifier (buf),
4283 			 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4284 	}
4285 
4286   if (c_dialect_cxx ())
4287     {
4288       /* For C++, make fileptr_type_node a distinct void * type until
4289 	 FILE type is defined.  */
4290       fileptr_type_node = build_variant_type_copy (ptr_type_node);
4291       /* Likewise for const struct tm*.  */
4292       const_tm_ptr_type_node = build_variant_type_copy (const_ptr_type_node);
4293     }
4294 
4295   record_builtin_type (RID_VOID, NULL, void_type_node);
4296 
4297   /* Set the TYPE_NAME for any variants that were built before
4298      record_builtin_type gave names to the built-in types. */
4299   {
4300     tree void_name = TYPE_NAME (void_type_node);
4301     TYPE_NAME (void_type_node) = NULL_TREE;
4302     TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4303       = void_name;
4304     TYPE_NAME (void_type_node) = void_name;
4305   }
4306 
4307   void_list_node = build_void_list_node ();
4308 
4309   /* Make a type to be the domain of a few array types
4310      whose domains don't really matter.
4311      200 is small enough that it always fits in size_t
4312      and large enough that it can hold most function names for the
4313      initializations of __FUNCTION__ and __PRETTY_FUNCTION__.  */
4314   array_domain_type = build_index_type (size_int (200));
4315 
4316   /* Make a type for arrays of characters.
4317      With luck nothing will ever really depend on the length of this
4318      array type.  */
4319   char_array_type_node
4320     = build_array_type (char_type_node, array_domain_type);
4321 
4322   string_type_node = build_pointer_type (char_type_node);
4323   const_string_type_node
4324     = build_pointer_type (build_qualified_type
4325 			  (char_type_node, TYPE_QUAL_CONST));
4326 
4327   /* This is special for C++ so functions can be overloaded.  */
4328   wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4329   wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4330   wchar_type_size = TYPE_PRECISION (wchar_type_node);
4331   underlying_wchar_type_node = wchar_type_node;
4332   if (c_dialect_cxx ())
4333     {
4334       if (TYPE_UNSIGNED (wchar_type_node))
4335 	wchar_type_node = make_unsigned_type (wchar_type_size);
4336       else
4337 	wchar_type_node = make_signed_type (wchar_type_size);
4338       record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4339     }
4340 
4341   /* This is for wide string constants.  */
4342   wchar_array_type_node
4343     = build_array_type (wchar_type_node, array_domain_type);
4344 
4345   /* Define 'char16_t'.  */
4346   char16_type_node = get_identifier (CHAR16_TYPE);
4347   char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4348   char16_type_size = TYPE_PRECISION (char16_type_node);
4349   if (c_dialect_cxx ())
4350     {
4351       char16_type_node = make_unsigned_type (char16_type_size);
4352 
4353       if (cxx_dialect >= cxx11)
4354 	record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4355     }
4356 
4357   /* This is for UTF-16 string constants.  */
4358   char16_array_type_node
4359     = build_array_type (char16_type_node, array_domain_type);
4360 
4361   /* Define 'char32_t'.  */
4362   char32_type_node = get_identifier (CHAR32_TYPE);
4363   char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4364   char32_type_size = TYPE_PRECISION (char32_type_node);
4365   if (c_dialect_cxx ())
4366     {
4367       char32_type_node = make_unsigned_type (char32_type_size);
4368 
4369       if (cxx_dialect >= cxx11)
4370 	record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4371     }
4372 
4373   /* This is for UTF-32 string constants.  */
4374   char32_array_type_node
4375     = build_array_type (char32_type_node, array_domain_type);
4376 
4377   wint_type_node =
4378     TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4379 
4380   intmax_type_node =
4381     TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4382   uintmax_type_node =
4383     TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4384 
4385   if (SIG_ATOMIC_TYPE)
4386     sig_atomic_type_node =
4387       TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4388   if (INT8_TYPE)
4389     int8_type_node =
4390       TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4391   if (INT16_TYPE)
4392     int16_type_node =
4393       TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4394   if (INT32_TYPE)
4395     int32_type_node =
4396       TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4397   if (INT64_TYPE)
4398     int64_type_node =
4399       TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4400   if (UINT8_TYPE)
4401     uint8_type_node =
4402       TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4403   if (UINT16_TYPE)
4404     c_uint16_type_node = uint16_type_node =
4405       TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4406   if (UINT32_TYPE)
4407     c_uint32_type_node = uint32_type_node =
4408       TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4409   if (UINT64_TYPE)
4410     c_uint64_type_node = uint64_type_node =
4411       TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4412   if (INT_LEAST8_TYPE)
4413     int_least8_type_node =
4414       TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4415   if (INT_LEAST16_TYPE)
4416     int_least16_type_node =
4417       TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4418   if (INT_LEAST32_TYPE)
4419     int_least32_type_node =
4420       TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4421   if (INT_LEAST64_TYPE)
4422     int_least64_type_node =
4423       TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4424   if (UINT_LEAST8_TYPE)
4425     uint_least8_type_node =
4426       TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4427   if (UINT_LEAST16_TYPE)
4428     uint_least16_type_node =
4429       TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4430   if (UINT_LEAST32_TYPE)
4431     uint_least32_type_node =
4432       TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4433   if (UINT_LEAST64_TYPE)
4434     uint_least64_type_node =
4435       TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4436   if (INT_FAST8_TYPE)
4437     int_fast8_type_node =
4438       TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4439   if (INT_FAST16_TYPE)
4440     int_fast16_type_node =
4441       TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4442   if (INT_FAST32_TYPE)
4443     int_fast32_type_node =
4444       TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4445   if (INT_FAST64_TYPE)
4446     int_fast64_type_node =
4447       TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4448   if (UINT_FAST8_TYPE)
4449     uint_fast8_type_node =
4450       TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4451   if (UINT_FAST16_TYPE)
4452     uint_fast16_type_node =
4453       TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4454   if (UINT_FAST32_TYPE)
4455     uint_fast32_type_node =
4456       TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4457   if (UINT_FAST64_TYPE)
4458     uint_fast64_type_node =
4459       TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4460   if (INTPTR_TYPE)
4461     intptr_type_node =
4462       TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4463   if (UINTPTR_TYPE)
4464     uintptr_type_node =
4465       TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4466 
4467   default_function_type
4468     = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4469   unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4470 
4471   lang_hooks.decls.pushdecl
4472     (build_decl (UNKNOWN_LOCATION,
4473 		 TYPE_DECL, get_identifier ("__builtin_va_list"),
4474 		 va_list_type_node));
4475   if (targetm.enum_va_list_p)
4476     {
4477       int l;
4478       const char *pname;
4479       tree ptype;
4480 
4481       for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4482 	{
4483 	  lang_hooks.decls.pushdecl
4484 	    (build_decl (UNKNOWN_LOCATION,
4485 		         TYPE_DECL, get_identifier (pname),
4486 	  	         ptype));
4487 
4488 	}
4489     }
4490 
4491   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4492     {
4493       va_list_arg_type_node = va_list_ref_type_node =
4494 	build_pointer_type (TREE_TYPE (va_list_type_node));
4495     }
4496   else
4497     {
4498       va_list_arg_type_node = va_list_type_node;
4499       va_list_ref_type_node = build_reference_type (va_list_type_node);
4500     }
4501 
4502   if (!flag_preprocess_only)
4503     c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4504 
4505   main_identifier_node = get_identifier ("main");
4506 
4507   /* Create the built-in __null node.  It is important that this is
4508      not shared.  */
4509   null_node = make_int_cst (1, 1);
4510   TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4511 
4512   /* Since builtin_types isn't gc'ed, don't export these nodes.  */
4513   memset (builtin_types, 0, sizeof (builtin_types));
4514 }
4515 
4516 /* The number of named compound-literals generated thus far.  */
4517 static GTY(()) int compound_literal_number;
4518 
4519 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal.  */
4520 
4521 void
4522 set_compound_literal_name (tree decl)
4523 {
4524   char *name;
4525   ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4526 			   compound_literal_number);
4527   compound_literal_number++;
4528   DECL_NAME (decl) = get_identifier (name);
4529 }
4530 
4531 /* build_va_arg helper function.  Return a VA_ARG_EXPR with location LOC, type
4532    TYPE and operand OP.  */
4533 
4534 static tree
4535 build_va_arg_1 (location_t loc, tree type, tree op)
4536 {
4537   tree expr = build1 (VA_ARG_EXPR, type, op);
4538   SET_EXPR_LOCATION (expr, loc);
4539   return expr;
4540 }
4541 
4542 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4543    va_arg (EXPR, TYPE) at source location LOC.  */
4544 
4545 tree
4546 build_va_arg (location_t loc, tree expr, tree type)
4547 {
4548   tree va_type = TREE_TYPE (expr);
4549   tree canon_va_type = (va_type == error_mark_node
4550 			? error_mark_node
4551 			: targetm.canonical_va_list_type (va_type));
4552 
4553   if (va_type == error_mark_node
4554       || canon_va_type == NULL_TREE)
4555     {
4556       if (canon_va_type == NULL_TREE)
4557 	error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4558 
4559       /* Let's handle things neutrallly, if expr:
4560 	 - has undeclared type, or
4561 	 - is not an va_list type.  */
4562       return build_va_arg_1 (loc, type, error_mark_node);
4563     }
4564 
4565   if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4566     {
4567       /* Case 1: Not an array type.  */
4568 
4569       /* Take the address, to get '&ap'.  Note that &ap is not a va_list
4570 	 type.  */
4571       mark_addressable (expr);
4572       expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4573 
4574       return build_va_arg_1 (loc, type, expr);
4575     }
4576 
4577   /* Case 2: Array type.
4578 
4579      Background:
4580 
4581      For contrast, let's start with the simple case (case 1).  If
4582      canon_va_type is not an array type, but say a char *, then when
4583      passing-by-value a va_list, the type of the va_list param decl is
4584      the same as for another va_list decl (all ap's are char *):
4585 
4586      f2_1 (char * ap)
4587        D.1815 = VA_ARG (&ap, 0B, 1);
4588        return D.1815;
4589 
4590      f2 (int i)
4591        char * ap.0;
4592        char * ap;
4593        __builtin_va_start (&ap, 0);
4594        ap.0 = ap;
4595        res = f2_1 (ap.0);
4596        __builtin_va_end (&ap);
4597        D.1812 = res;
4598        return D.1812;
4599 
4600      However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4601      va_list the type of the va_list param decl (case 2b, struct * ap) is not
4602      the same as for another va_list decl (case 2a, struct ap[1]).
4603 
4604      f2_1 (struct  * ap)
4605        D.1844 = VA_ARG (ap, 0B, 0);
4606        return D.1844;
4607 
4608      f2 (int i)
4609        struct  ap[1];
4610        __builtin_va_start (&ap, 0);
4611        res = f2_1 (&ap);
4612        __builtin_va_end (&ap);
4613        D.1841 = res;
4614        return D.1841;
4615 
4616      Case 2b is different because:
4617      - on the callee side, the parm decl has declared type va_list, but
4618        grokdeclarator changes the type of the parm decl to a pointer to the
4619        array elem type.
4620      - on the caller side, the pass-by-value uses &ap.
4621 
4622      We unify these two cases (case 2a: va_list is array type,
4623      case 2b: va_list is pointer to array elem type), by adding '&' for the
4624      array type case, such that we have a pointer to array elem in both
4625      cases.  */
4626 
4627   if (TREE_CODE (va_type) == ARRAY_TYPE)
4628     {
4629       /* Case 2a: va_list is array type.  */
4630 
4631       /* Take the address, to get '&ap'.  Make sure it's a pointer to array
4632 	 elem type.  */
4633       mark_addressable (expr);
4634       expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
4635 		     expr);
4636 
4637       /* Verify that &ap is still recognized as having va_list type.  */
4638       tree canon_expr_type
4639 	= targetm.canonical_va_list_type (TREE_TYPE (expr));
4640       gcc_assert (canon_expr_type != NULL_TREE);
4641     }
4642   else
4643     {
4644       /* Case 2b: va_list is pointer to array elem type.  */
4645       gcc_assert (POINTER_TYPE_P (va_type));
4646 
4647       /* Comparison as in std_canonical_va_list_type.  */
4648       gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
4649 		  == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
4650 
4651       /* Don't take the address.  We've already got '&ap'.  */
4652       ;
4653     }
4654 
4655   return build_va_arg_1 (loc, type, expr);
4656 }
4657 
4658 
4659 /* Linked list of disabled built-in functions.  */
4660 
4661 struct disabled_builtin
4662 {
4663   const char *name;
4664   struct disabled_builtin *next;
4665 };
4666 static disabled_builtin *disabled_builtins = NULL;
4667 
4668 static bool builtin_function_disabled_p (const char *);
4669 
4670 /* Disable a built-in function specified by -fno-builtin-NAME.  If NAME
4671    begins with "__builtin_", give an error.  */
4672 
4673 void
4674 disable_builtin_function (const char *name)
4675 {
4676   if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
4677     error ("cannot disable built-in function %qs", name);
4678   else
4679     {
4680       disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4681       new_disabled_builtin->name = name;
4682       new_disabled_builtin->next = disabled_builtins;
4683       disabled_builtins = new_disabled_builtin;
4684     }
4685 }
4686 
4687 
4688 /* Return true if the built-in function NAME has been disabled, false
4689    otherwise.  */
4690 
4691 static bool
4692 builtin_function_disabled_p (const char *name)
4693 {
4694   disabled_builtin *p;
4695   for (p = disabled_builtins; p != NULL; p = p->next)
4696     {
4697       if (strcmp (name, p->name) == 0)
4698 	return true;
4699     }
4700   return false;
4701 }
4702 
4703 
4704 /* Worker for DEF_BUILTIN.
4705    Possibly define a builtin function with one or two names.
4706    Does not declare a non-__builtin_ function if flag_no_builtin, or if
4707    nonansi_p and flag_no_nonansi_builtin.  */
4708 
4709 static void
4710 def_builtin_1 (enum built_in_function fncode,
4711 	       const char *name,
4712 	       enum built_in_class fnclass,
4713 	       tree fntype, tree libtype,
4714 	       bool both_p, bool fallback_p, bool nonansi_p,
4715 	       tree fnattrs, bool implicit_p)
4716 {
4717   tree decl;
4718   const char *libname;
4719 
4720   if (fntype == error_mark_node)
4721     return;
4722 
4723   gcc_assert ((!both_p && !fallback_p)
4724 	      || !strncmp (name, "__builtin_",
4725 			   strlen ("__builtin_")));
4726 
4727   libname = name + strlen ("__builtin_");
4728   decl = add_builtin_function (name, fntype, fncode, fnclass,
4729 			       (fallback_p ? libname : NULL),
4730 			       fnattrs);
4731 
4732   set_builtin_decl (fncode, decl, implicit_p);
4733 
4734   if (both_p
4735       && !flag_no_builtin && !builtin_function_disabled_p (libname)
4736       && !(nonansi_p && flag_no_nonansi_builtin))
4737     add_builtin_function (libname, libtype, fncode, fnclass,
4738 			  NULL, fnattrs);
4739 }
4740 
4741 /* Nonzero if the type T promotes to int.  This is (nearly) the
4742    integral promotions defined in ISO C99 6.3.1.1/2.  */
4743 
4744 bool
4745 c_promoting_integer_type_p (const_tree t)
4746 {
4747   switch (TREE_CODE (t))
4748     {
4749     case INTEGER_TYPE:
4750       return (TYPE_MAIN_VARIANT (t) == char_type_node
4751 	      || TYPE_MAIN_VARIANT (t) == signed_char_type_node
4752 	      || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
4753 	      || TYPE_MAIN_VARIANT (t) == short_integer_type_node
4754 	      || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
4755 	      || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
4756 
4757     case ENUMERAL_TYPE:
4758       /* ??? Technically all enumerations not larger than an int
4759 	 promote to an int.  But this is used along code paths
4760 	 that only want to notice a size change.  */
4761       return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
4762 
4763     case BOOLEAN_TYPE:
4764       return 1;
4765 
4766     default:
4767       return 0;
4768     }
4769 }
4770 
4771 /* Return 1 if PARMS specifies a fixed number of parameters
4772    and none of their types is affected by default promotions.  */
4773 
4774 int
4775 self_promoting_args_p (const_tree parms)
4776 {
4777   const_tree t;
4778   for (t = parms; t; t = TREE_CHAIN (t))
4779     {
4780       tree type = TREE_VALUE (t);
4781 
4782       if (type == error_mark_node)
4783 	continue;
4784 
4785       if (TREE_CHAIN (t) == 0 && type != void_type_node)
4786 	return 0;
4787 
4788       if (type == 0)
4789 	return 0;
4790 
4791       if (TYPE_MAIN_VARIANT (type) == float_type_node)
4792 	return 0;
4793 
4794       if (c_promoting_integer_type_p (type))
4795 	return 0;
4796     }
4797   return 1;
4798 }
4799 
4800 /* Recursively remove any '*' or '&' operator from TYPE.  */
4801 tree
4802 strip_pointer_operator (tree t)
4803 {
4804   while (POINTER_TYPE_P (t))
4805     t = TREE_TYPE (t);
4806   return t;
4807 }
4808 
4809 /* Recursively remove pointer or array type from TYPE. */
4810 tree
4811 strip_pointer_or_array_types (tree t)
4812 {
4813   while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
4814     t = TREE_TYPE (t);
4815   return t;
4816 }
4817 
4818 /* Used to compare case labels.  K1 and K2 are actually tree nodes
4819    representing case labels, or NULL_TREE for a `default' label.
4820    Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4821    K2, and 0 if K1 and K2 are equal.  */
4822 
4823 int
4824 case_compare (splay_tree_key k1, splay_tree_key k2)
4825 {
4826   /* Consider a NULL key (such as arises with a `default' label) to be
4827      smaller than anything else.  */
4828   if (!k1)
4829     return k2 ? -1 : 0;
4830   else if (!k2)
4831     return k1 ? 1 : 0;
4832 
4833   return tree_int_cst_compare ((tree) k1, (tree) k2);
4834 }
4835 
4836 /* Process a case label, located at LOC, for the range LOW_VALUE
4837    ... HIGH_VALUE.  If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4838    then this case label is actually a `default' label.  If only
4839    HIGH_VALUE is NULL_TREE, then case label was declared using the
4840    usual C/C++ syntax, rather than the GNU case range extension.
4841    CASES is a tree containing all the case ranges processed so far;
4842    COND is the condition for the switch-statement itself.
4843    OUTSIDE_RANGE_P says whether there was a case value that doesn't
4844    fit into the range of the ORIG_TYPE.  Returns the CASE_LABEL_EXPR
4845    created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created.  */
4846 
4847 tree
4848 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
4849 		  tree low_value, tree high_value, bool *outside_range_p)
4850 {
4851   tree type;
4852   tree label;
4853   tree case_label;
4854   splay_tree_node node;
4855 
4856   /* Create the LABEL_DECL itself.  */
4857   label = create_artificial_label (loc);
4858 
4859   /* If there was an error processing the switch condition, bail now
4860      before we get more confused.  */
4861   if (!cond || cond == error_mark_node)
4862     goto error_out;
4863 
4864   if ((low_value && TREE_TYPE (low_value)
4865        && POINTER_TYPE_P (TREE_TYPE (low_value)))
4866       || (high_value && TREE_TYPE (high_value)
4867 	  && POINTER_TYPE_P (TREE_TYPE (high_value))))
4868     {
4869       error_at (loc, "pointers are not permitted as case values");
4870       goto error_out;
4871     }
4872 
4873   /* Case ranges are a GNU extension.  */
4874   if (high_value)
4875     pedwarn (loc, OPT_Wpedantic,
4876 	     "range expressions in switch statements are non-standard");
4877 
4878   type = TREE_TYPE (cond);
4879   if (low_value)
4880     {
4881       low_value = check_case_value (loc, low_value);
4882       low_value = convert_and_check (loc, type, low_value);
4883       if (low_value == error_mark_node)
4884 	goto error_out;
4885     }
4886   if (high_value)
4887     {
4888       high_value = check_case_value (loc, high_value);
4889       high_value = convert_and_check (loc, type, high_value);
4890       if (high_value == error_mark_node)
4891 	goto error_out;
4892     }
4893 
4894   if (low_value && high_value)
4895     {
4896       /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4897 	 really a case range, even though it was written that way.
4898 	 Remove the HIGH_VALUE to simplify later processing.  */
4899       if (tree_int_cst_equal (low_value, high_value))
4900 	high_value = NULL_TREE;
4901       else if (!tree_int_cst_lt (low_value, high_value))
4902 	warning_at (loc, 0, "empty range specified");
4903     }
4904 
4905   /* See if the case is in range of the type of the original testing
4906      expression.  If both low_value and high_value are out of range,
4907      don't insert the case label and return NULL_TREE.  */
4908   if (low_value
4909       && !check_case_bounds (loc, type, orig_type,
4910 			     &low_value, high_value ? &high_value : NULL,
4911 			     outside_range_p))
4912     return NULL_TREE;
4913 
4914   /* Look up the LOW_VALUE in the table of case labels we already
4915      have.  */
4916   node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4917   /* If there was not an exact match, check for overlapping ranges.
4918      There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4919      that's a `default' label and the only overlap is an exact match.  */
4920   if (!node && (low_value || high_value))
4921     {
4922       splay_tree_node low_bound;
4923       splay_tree_node high_bound;
4924 
4925       /* Even though there wasn't an exact match, there might be an
4926 	 overlap between this case range and another case range.
4927 	 Since we've (inductively) not allowed any overlapping case
4928 	 ranges, we simply need to find the greatest low case label
4929 	 that is smaller that LOW_VALUE, and the smallest low case
4930 	 label that is greater than LOW_VALUE.  If there is an overlap
4931 	 it will occur in one of these two ranges.  */
4932       low_bound = splay_tree_predecessor (cases,
4933 					  (splay_tree_key) low_value);
4934       high_bound = splay_tree_successor (cases,
4935 					 (splay_tree_key) low_value);
4936 
4937       /* Check to see if the LOW_BOUND overlaps.  It is smaller than
4938 	 the LOW_VALUE, so there is no need to check unless the
4939 	 LOW_BOUND is in fact itself a case range.  */
4940       if (low_bound
4941 	  && CASE_HIGH ((tree) low_bound->value)
4942 	  && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4943 				    low_value) >= 0)
4944 	node = low_bound;
4945       /* Check to see if the HIGH_BOUND overlaps.  The low end of that
4946 	 range is bigger than the low end of the current range, so we
4947 	 are only interested if the current range is a real range, and
4948 	 not an ordinary case label.  */
4949       else if (high_bound
4950 	       && high_value
4951 	       && (tree_int_cst_compare ((tree) high_bound->key,
4952 					 high_value)
4953 		   <= 0))
4954 	node = high_bound;
4955     }
4956   /* If there was an overlap, issue an error.  */
4957   if (node)
4958     {
4959       tree duplicate = CASE_LABEL ((tree) node->value);
4960 
4961       if (high_value)
4962 	{
4963 	  error_at (loc, "duplicate (or overlapping) case value");
4964 	  inform (DECL_SOURCE_LOCATION (duplicate),
4965 		  "this is the first entry overlapping that value");
4966 	}
4967       else if (low_value)
4968 	{
4969 	  error_at (loc, "duplicate case value") ;
4970 	  inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
4971 	}
4972       else
4973 	{
4974 	  error_at (loc, "multiple default labels in one switch");
4975 	  inform (DECL_SOURCE_LOCATION (duplicate),
4976 		  "this is the first default label");
4977 	}
4978       goto error_out;
4979     }
4980 
4981   /* Add a CASE_LABEL to the statement-tree.  */
4982   case_label = add_stmt (build_case_label (low_value, high_value, label));
4983   /* Register this case label in the splay tree.  */
4984   splay_tree_insert (cases,
4985 		     (splay_tree_key) low_value,
4986 		     (splay_tree_value) case_label);
4987 
4988   return case_label;
4989 
4990  error_out:
4991   /* Add a label so that the back-end doesn't think that the beginning of
4992      the switch is unreachable.  Note that we do not add a case label, as
4993      that just leads to duplicates and thence to failure later on.  */
4994   if (!cases->root)
4995     {
4996       tree t = create_artificial_label (loc);
4997       add_stmt (build_stmt (loc, LABEL_EXPR, t));
4998     }
4999   return error_mark_node;
5000 }
5001 
5002 /* Finish an expression taking the address of LABEL (an
5003    IDENTIFIER_NODE).  Returns an expression for the address.
5004 
5005    LOC is the location for the expression returned.  */
5006 
5007 tree
5008 finish_label_address_expr (tree label, location_t loc)
5009 {
5010   tree result;
5011 
5012   pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
5013 
5014   if (label == error_mark_node)
5015     return error_mark_node;
5016 
5017   label = lookup_label (label);
5018   if (label == NULL_TREE)
5019     result = null_pointer_node;
5020   else
5021     {
5022       TREE_USED (label) = 1;
5023       result = build1 (ADDR_EXPR, ptr_type_node, label);
5024       /* The current function is not necessarily uninlinable.
5025 	 Computed gotos are incompatible with inlining, but the value
5026 	 here could be used only in a diagnostic, for example.  */
5027       protected_set_expr_location (result, loc);
5028     }
5029 
5030   return result;
5031 }
5032 
5033 
5034 /* Given a boolean expression ARG, return a tree representing an increment
5035    or decrement (as indicated by CODE) of ARG.  The front end must check for
5036    invalid cases (e.g., decrement in C++).  */
5037 tree
5038 boolean_increment (enum tree_code code, tree arg)
5039 {
5040   tree val;
5041   tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5042 
5043   arg = stabilize_reference (arg);
5044   switch (code)
5045     {
5046     case PREINCREMENT_EXPR:
5047       val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5048       break;
5049     case POSTINCREMENT_EXPR:
5050       val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5051       arg = save_expr (arg);
5052       val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5053       val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5054       break;
5055     case PREDECREMENT_EXPR:
5056       val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5057 		    invert_truthvalue_loc (input_location, arg));
5058       break;
5059     case POSTDECREMENT_EXPR:
5060       val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5061 		    invert_truthvalue_loc (input_location, arg));
5062       arg = save_expr (arg);
5063       val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5064       val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5065       break;
5066     default:
5067       gcc_unreachable ();
5068     }
5069   TREE_SIDE_EFFECTS (val) = 1;
5070   return val;
5071 }
5072 
5073 /* Built-in macros for stddef.h and stdint.h, that require macros
5074    defined in this file.  */
5075 void
5076 c_stddef_cpp_builtins(void)
5077 {
5078   builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5079   builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5080   builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5081   builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5082   builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5083   builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5084   builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5085   builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5086   if (SIG_ATOMIC_TYPE)
5087     builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5088   if (INT8_TYPE)
5089     builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5090   if (INT16_TYPE)
5091     builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5092   if (INT32_TYPE)
5093     builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5094   if (INT64_TYPE)
5095     builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5096   if (UINT8_TYPE)
5097     builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5098   if (UINT16_TYPE)
5099     builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5100   if (UINT32_TYPE)
5101     builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5102   if (UINT64_TYPE)
5103     builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5104   if (INT_LEAST8_TYPE)
5105     builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5106   if (INT_LEAST16_TYPE)
5107     builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5108   if (INT_LEAST32_TYPE)
5109     builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5110   if (INT_LEAST64_TYPE)
5111     builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5112   if (UINT_LEAST8_TYPE)
5113     builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5114   if (UINT_LEAST16_TYPE)
5115     builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5116   if (UINT_LEAST32_TYPE)
5117     builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5118   if (UINT_LEAST64_TYPE)
5119     builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5120   if (INT_FAST8_TYPE)
5121     builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5122   if (INT_FAST16_TYPE)
5123     builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5124   if (INT_FAST32_TYPE)
5125     builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5126   if (INT_FAST64_TYPE)
5127     builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5128   if (UINT_FAST8_TYPE)
5129     builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5130   if (UINT_FAST16_TYPE)
5131     builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5132   if (UINT_FAST32_TYPE)
5133     builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5134   if (UINT_FAST64_TYPE)
5135     builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5136   if (INTPTR_TYPE)
5137     builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5138   if (UINTPTR_TYPE)
5139     builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5140 }
5141 
5142 static void
5143 c_init_attributes (void)
5144 {
5145   /* Fill in the built_in_attributes array.  */
5146 #define DEF_ATTR_NULL_TREE(ENUM)				\
5147   built_in_attributes[(int) ENUM] = NULL_TREE;
5148 #define DEF_ATTR_INT(ENUM, VALUE)				\
5149   built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5150 #define DEF_ATTR_STRING(ENUM, VALUE)				\
5151   built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5152 #define DEF_ATTR_IDENT(ENUM, STRING)				\
5153   built_in_attributes[(int) ENUM] = get_identifier (STRING);
5154 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN)	\
5155   built_in_attributes[(int) ENUM]			\
5156     = tree_cons (built_in_attributes[(int) PURPOSE],	\
5157 		 built_in_attributes[(int) VALUE],	\
5158 		 built_in_attributes[(int) CHAIN]);
5159 #include "builtin-attrs.def"
5160 #undef DEF_ATTR_NULL_TREE
5161 #undef DEF_ATTR_INT
5162 #undef DEF_ATTR_IDENT
5163 #undef DEF_ATTR_TREE_LIST
5164 }
5165 
5166 /* Check whether ALIGN is a valid user-specified alignment.  If so,
5167    return its base-2 log; if not, output an error and return -1.  If
5168    ALLOW_ZERO then 0 is valid and should result in a return of -1 with
5169    no error.  */
5170 int
5171 check_user_alignment (const_tree align, bool allow_zero)
5172 {
5173   int i;
5174 
5175   if (error_operand_p (align))
5176     return -1;
5177   if (TREE_CODE (align) != INTEGER_CST
5178       || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5179     {
5180       error ("requested alignment is not an integer constant");
5181       return -1;
5182     }
5183   else if (allow_zero && integer_zerop (align))
5184     return -1;
5185   else if (tree_int_cst_sgn (align) == -1
5186            || (i = tree_log2 (align)) == -1)
5187     {
5188       error ("requested alignment is not a positive power of 2");
5189       return -1;
5190     }
5191   else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5192     {
5193       error ("requested alignment is too large");
5194       return -1;
5195     }
5196   return i;
5197 }
5198 
5199 /* Determine the ELF symbol visibility for DECL, which is either a
5200    variable or a function.  It is an error to use this function if a
5201    definition of DECL is not available in this translation unit.
5202    Returns true if the final visibility has been determined by this
5203    function; false if the caller is free to make additional
5204    modifications.  */
5205 
5206 bool
5207 c_determine_visibility (tree decl)
5208 {
5209   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5210 
5211   /* If the user explicitly specified the visibility with an
5212      attribute, honor that.  DECL_VISIBILITY will have been set during
5213      the processing of the attribute.  We check for an explicit
5214      attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5215      to distinguish the use of an attribute from the use of a "#pragma
5216      GCC visibility push(...)"; in the latter case we still want other
5217      considerations to be able to overrule the #pragma.  */
5218   if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5219       || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5220 	  && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5221 	      || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5222     return true;
5223 
5224   /* Set default visibility to whatever the user supplied with
5225      visibility_specified depending on #pragma GCC visibility.  */
5226   if (!DECL_VISIBILITY_SPECIFIED (decl))
5227     {
5228       if (visibility_options.inpragma
5229 	  || DECL_VISIBILITY (decl) != default_visibility)
5230 	{
5231 	  DECL_VISIBILITY (decl) = default_visibility;
5232 	  DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5233 	  /* If visibility changed and DECL already has DECL_RTL, ensure
5234 	     symbol flags are updated.  */
5235 	  if (((VAR_P (decl) && TREE_STATIC (decl))
5236 	       || TREE_CODE (decl) == FUNCTION_DECL)
5237 	      && DECL_RTL_SET_P (decl))
5238 	    make_decl_rtl (decl);
5239 	}
5240     }
5241   return false;
5242 }
5243 
5244 /* Data to communicate through check_function_arguments_recurse between
5245    check_function_nonnull and check_nonnull_arg.  */
5246 
5247 struct nonnull_arg_ctx
5248 {
5249   location_t loc;
5250   bool warned_p;
5251 };
5252 
5253 /* Check the argument list of a function call for null in argument slots
5254    that are marked as requiring a non-null pointer argument.  The NARGS
5255    arguments are passed in the array ARGARRAY.  Return true if we have
5256    warned.  */
5257 
5258 static bool
5259 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
5260 {
5261   tree a;
5262   int i;
5263 
5264   attrs = lookup_attribute ("nonnull", attrs);
5265   if (attrs == NULL_TREE)
5266     return false;
5267 
5268   a = attrs;
5269   /* See if any of the nonnull attributes has no arguments.  If so,
5270      then every pointer argument is checked (in which case the check
5271      for pointer type is done in check_nonnull_arg).  */
5272   if (TREE_VALUE (a) != NULL_TREE)
5273     do
5274       a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5275     while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
5276 
5277   struct nonnull_arg_ctx ctx = { loc, false };
5278   if (a != NULL_TREE)
5279     for (i = 0; i < nargs; i++)
5280       check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5281 					i + 1);
5282   else
5283     {
5284       /* Walk the argument list.  If we encounter an argument number we
5285 	 should check for non-null, do it.  */
5286       for (i = 0; i < nargs; i++)
5287 	{
5288 	  for (a = attrs; ; a = TREE_CHAIN (a))
5289 	    {
5290 	      a = lookup_attribute ("nonnull", a);
5291 	      if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5292 		break;
5293 	    }
5294 
5295 	  if (a != NULL_TREE)
5296 	    check_function_arguments_recurse (check_nonnull_arg, &ctx,
5297 					      argarray[i], i + 1);
5298 	}
5299     }
5300   return ctx.warned_p;
5301 }
5302 
5303 /* Check that the Nth argument of a function call (counting backwards
5304    from the end) is a (pointer)0.  The NARGS arguments are passed in the
5305    array ARGARRAY.  */
5306 
5307 static void
5308 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5309 {
5310   tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5311 
5312   if (attr)
5313     {
5314       int len = 0;
5315       int pos = 0;
5316       tree sentinel;
5317       function_args_iterator iter;
5318       tree t;
5319 
5320       /* Skip over the named arguments.  */
5321       FOREACH_FUNCTION_ARGS (fntype, t, iter)
5322 	{
5323 	  if (len == nargs)
5324 	    break;
5325 	  len++;
5326 	}
5327 
5328       if (TREE_VALUE (attr))
5329 	{
5330 	  tree p = TREE_VALUE (TREE_VALUE (attr));
5331 	  pos = TREE_INT_CST_LOW (p);
5332 	}
5333 
5334       /* The sentinel must be one of the varargs, i.e.
5335 	 in position >= the number of fixed arguments.  */
5336       if ((nargs - 1 - pos) < len)
5337 	{
5338 	  warning (OPT_Wformat_,
5339 		   "not enough variable arguments to fit a sentinel");
5340 	  return;
5341 	}
5342 
5343       /* Validate the sentinel.  */
5344       sentinel = argarray[nargs - 1 - pos];
5345       if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5346 	   || !integer_zerop (sentinel))
5347 	  /* Although __null (in C++) is only an integer we allow it
5348 	     nevertheless, as we are guaranteed that it's exactly
5349 	     as wide as a pointer, and we don't want to force
5350 	     users to cast the NULL they have written there.
5351 	     We warn with -Wstrict-null-sentinel, though.  */
5352 	  && (warn_strict_null_sentinel || null_node != sentinel))
5353 	warning (OPT_Wformat_, "missing sentinel in function call");
5354     }
5355 }
5356 
5357 /* Check that the same argument isn't passed to restrict arguments
5358    and other arguments.  */
5359 
5360 static void
5361 check_function_restrict (const_tree fndecl, const_tree fntype,
5362 			 int nargs, tree *argarray)
5363 {
5364   int i;
5365   tree parms;
5366 
5367   if (fndecl
5368       && TREE_CODE (fndecl) == FUNCTION_DECL
5369       && DECL_ARGUMENTS (fndecl))
5370     parms = DECL_ARGUMENTS (fndecl);
5371   else
5372     parms = TYPE_ARG_TYPES (fntype);
5373 
5374   for (i = 0; i < nargs; i++)
5375     TREE_VISITED (argarray[i]) = 0;
5376 
5377   for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5378     {
5379       tree type;
5380       if (TREE_CODE (parms) == PARM_DECL)
5381 	{
5382 	  type = TREE_TYPE (parms);
5383 	  parms = DECL_CHAIN (parms);
5384 	}
5385       else
5386 	{
5387 	  type = TREE_VALUE (parms);
5388 	  parms = TREE_CHAIN (parms);
5389 	}
5390       if (POINTER_TYPE_P (type)
5391 	  && TYPE_RESTRICT (type)
5392 	  && !TYPE_READONLY (TREE_TYPE (type)))
5393 	warn_for_restrict (i, argarray, nargs);
5394     }
5395 
5396   for (i = 0; i < nargs; i++)
5397     TREE_VISITED (argarray[i]) = 0;
5398 }
5399 
5400 /* Helper for check_function_nonnull; given a list of operands which
5401    must be non-null in ARGS, determine if operand PARAM_NUM should be
5402    checked.  */
5403 
5404 static bool
5405 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5406 {
5407   unsigned HOST_WIDE_INT arg_num = 0;
5408 
5409   for (; args; args = TREE_CHAIN (args))
5410     {
5411       bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
5412 
5413       gcc_assert (found);
5414 
5415       if (arg_num == param_num)
5416 	return true;
5417     }
5418   return false;
5419 }
5420 
5421 /* Check that the function argument PARAM (which is operand number
5422    PARAM_NUM) is non-null.  This is called by check_function_nonnull
5423    via check_function_arguments_recurse.  */
5424 
5425 static void
5426 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
5427 {
5428   struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
5429 
5430   /* Just skip checking the argument if it's not a pointer.  This can
5431      happen if the "nonnull" attribute was given without an operand
5432      list (which means to check every pointer argument).  */
5433 
5434   if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5435     return;
5436 
5437   /* Diagnose the simple cases of null arguments.  */
5438   if (integer_zerop (fold_for_warn (param)))
5439     {
5440       warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5441 		  "required (argument %lu)", (unsigned long) param_num);
5442       pctx->warned_p = true;
5443     }
5444 }
5445 
5446 /* Helper for nonnull attribute handling; fetch the operand number
5447    from the attribute argument list.  */
5448 
5449 bool
5450 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5451 {
5452   /* Verify the arg number is a small constant.  */
5453   if (tree_fits_uhwi_p (arg_num_expr))
5454     {
5455       *valp = TREE_INT_CST_LOW (arg_num_expr);
5456       return true;
5457     }
5458   else
5459     return false;
5460 }
5461 
5462 /* Arguments being collected for optimization.  */
5463 typedef const char *const_char_p;		/* For DEF_VEC_P.  */
5464 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
5465 
5466 
5467 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5468    options in ARGS.  ATTR_P is true if this is for attribute(optimize), and
5469    false for #pragma GCC optimize.  */
5470 
5471 bool
5472 parse_optimize_options (tree args, bool attr_p)
5473 {
5474   bool ret = true;
5475   unsigned opt_argc;
5476   unsigned i;
5477   const char **opt_argv;
5478   struct cl_decoded_option *decoded_options;
5479   unsigned int decoded_options_count;
5480   tree ap;
5481 
5482   /* Build up argv vector.  Just in case the string is stored away, use garbage
5483      collected strings.  */
5484   vec_safe_truncate (optimize_args, 0);
5485   vec_safe_push (optimize_args, (const char *) NULL);
5486 
5487   for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5488     {
5489       tree value = TREE_VALUE (ap);
5490 
5491       if (TREE_CODE (value) == INTEGER_CST)
5492 	{
5493 	  char buffer[20];
5494 	  sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
5495 	  vec_safe_push (optimize_args, ggc_strdup (buffer));
5496 	}
5497 
5498       else if (TREE_CODE (value) == STRING_CST)
5499 	{
5500 	  /* Split string into multiple substrings.  */
5501 	  size_t len = TREE_STRING_LENGTH (value);
5502 	  char *p = ASTRDUP (TREE_STRING_POINTER (value));
5503 	  char *end = p + len;
5504 	  char *comma;
5505 	  char *next_p = p;
5506 
5507 	  while (next_p != NULL)
5508 	    {
5509 	      size_t len2;
5510 	      char *q, *r;
5511 
5512 	      p = next_p;
5513 	      comma = strchr (p, ',');
5514 	      if (comma)
5515 		{
5516 		  len2 = comma - p;
5517 		  *comma = '\0';
5518 		  next_p = comma+1;
5519 		}
5520 	      else
5521 		{
5522 		  len2 = end - p;
5523 		  next_p = NULL;
5524 		}
5525 
5526 	      r = q = (char *) ggc_alloc_atomic (len2 + 3);
5527 
5528 	      /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5529 		 options.  */
5530 	      if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5531 		{
5532 		  ret = false;
5533 		  if (attr_p)
5534 		    warning (OPT_Wattributes,
5535 			     "bad option %qs to attribute %<optimize%>", p);
5536 		  else
5537 		    warning (OPT_Wpragmas,
5538 			     "bad option %qs to pragma %<optimize%>", p);
5539 		  continue;
5540 		}
5541 
5542 	      if (*p != '-')
5543 		{
5544 		  *r++ = '-';
5545 
5546 		  /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5547 		     itself is -Os, and any other switch begins with a -f.  */
5548 		  if ((*p >= '0' && *p <= '9')
5549 		      || (p[0] == 's' && p[1] == '\0'))
5550 		    *r++ = 'O';
5551 		  else if (*p != 'O')
5552 		    *r++ = 'f';
5553 		}
5554 
5555 	      memcpy (r, p, len2);
5556 	      r[len2] = '\0';
5557 	      vec_safe_push (optimize_args, (const char *) q);
5558 	    }
5559 
5560 	}
5561     }
5562 
5563   opt_argc = optimize_args->length ();
5564   opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5565 
5566   for (i = 1; i < opt_argc; i++)
5567     opt_argv[i] = (*optimize_args)[i];
5568 
5569   /* Now parse the options.  */
5570   decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5571 						&decoded_options,
5572 						&decoded_options_count);
5573   /* Drop non-Optimization options.  */
5574   unsigned j = 1;
5575   for (i = 1; i < decoded_options_count; ++i)
5576     {
5577       if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5578 	{
5579 	  ret = false;
5580 	  if (attr_p)
5581 	    warning (OPT_Wattributes,
5582 		     "bad option %qs to attribute %<optimize%>",
5583 		     decoded_options[i].orig_option_with_args_text);
5584 	  else
5585 	    warning (OPT_Wpragmas,
5586 		     "bad option %qs to pragma %<optimize%>",
5587 		     decoded_options[i].orig_option_with_args_text);
5588 	  continue;
5589 	}
5590       if (i != j)
5591 	decoded_options[j] = decoded_options[i];
5592       j++;
5593     }
5594   decoded_options_count = j;
5595   /* And apply them.  */
5596   decode_options (&global_options, &global_options_set,
5597 		  decoded_options, decoded_options_count,
5598 		  input_location, global_dc, NULL);
5599 
5600   targetm.override_options_after_change();
5601 
5602   optimize_args->truncate (0);
5603   return ret;
5604 }
5605 
5606 /* Check whether ATTR is a valid attribute fallthrough.  */
5607 
5608 bool
5609 attribute_fallthrough_p (tree attr)
5610 {
5611   if (attr == error_mark_node)
5612    return false;
5613   tree t = lookup_attribute ("fallthrough", attr);
5614   if (t == NULL_TREE)
5615     return false;
5616   /* This attribute shall appear at most once in each attribute-list.  */
5617   if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5618     warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5619 	     "times");
5620   /* No attribute-argument-clause shall be present.  */
5621   else if (TREE_VALUE (t) != NULL_TREE)
5622     warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5623 	     "a parameter");
5624   /* Warn if other attributes are found.  */
5625   for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5626     {
5627       tree name = get_attribute_name (t);
5628       if (!is_attribute_p ("fallthrough", name))
5629 	warning (OPT_Wattributes, "%qE attribute ignored", name);
5630     }
5631   return true;
5632 }
5633 
5634 
5635 /* Check for valid arguments being passed to a function with FNTYPE.
5636    There are NARGS arguments in the array ARGARRAY.  LOC should be used for
5637    diagnostics.  Return true if -Wnonnull warning has been diagnosed.  */
5638 bool
5639 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
5640 			  int nargs, tree *argarray)
5641 {
5642   bool warned_p = false;
5643 
5644   /* Check for null being passed in a pointer argument that must be
5645      non-null.  We also need to do this if format checking is enabled.  */
5646 
5647   if (warn_nonnull)
5648     warned_p = check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype),
5649 				       nargs, argarray);
5650 
5651   /* Check for errors in format strings.  */
5652 
5653   if (warn_format || warn_suggest_attribute_format)
5654     check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
5655 
5656   if (warn_format)
5657     check_function_sentinel (fntype, nargs, argarray);
5658 
5659   if (warn_restrict)
5660     check_function_restrict (fndecl, fntype, nargs, argarray);
5661   return warned_p;
5662 }
5663 
5664 /* Generic argument checking recursion routine.  PARAM is the argument to
5665    be checked.  PARAM_NUM is the number of the argument.  CALLBACK is invoked
5666    once the argument is resolved.  CTX is context for the callback.  */
5667 void
5668 check_function_arguments_recurse (void (*callback)
5669 				  (void *, tree, unsigned HOST_WIDE_INT),
5670 				  void *ctx, tree param,
5671 				  unsigned HOST_WIDE_INT param_num)
5672 {
5673   if (CONVERT_EXPR_P (param)
5674       && (TYPE_PRECISION (TREE_TYPE (param))
5675 	  == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
5676     {
5677       /* Strip coercion.  */
5678       check_function_arguments_recurse (callback, ctx,
5679 					TREE_OPERAND (param, 0), param_num);
5680       return;
5681     }
5682 
5683   if (TREE_CODE (param) == CALL_EXPR)
5684     {
5685       tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
5686       tree attrs;
5687       bool found_format_arg = false;
5688 
5689       /* See if this is a call to a known internationalization function
5690 	 that modifies a format arg.  Such a function may have multiple
5691 	 format_arg attributes (for example, ngettext).  */
5692 
5693       for (attrs = TYPE_ATTRIBUTES (type);
5694 	   attrs;
5695 	   attrs = TREE_CHAIN (attrs))
5696 	if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5697 	  {
5698 	    tree inner_arg;
5699 	    tree format_num_expr;
5700 	    int format_num;
5701 	    int i;
5702 	    call_expr_arg_iterator iter;
5703 
5704 	    /* Extract the argument number, which was previously checked
5705 	       to be valid.  */
5706 	    format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5707 
5708 	    format_num = tree_to_uhwi (format_num_expr);
5709 
5710 	    for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
5711 		 inner_arg != 0;
5712 		 inner_arg = next_call_expr_arg (&iter), i++)
5713 	      if (i == format_num)
5714 		{
5715 		  check_function_arguments_recurse (callback, ctx,
5716 						    inner_arg, param_num);
5717 		  found_format_arg = true;
5718 		  break;
5719 		}
5720 	  }
5721 
5722       /* If we found a format_arg attribute and did a recursive check,
5723 	 we are done with checking this argument.  Otherwise, we continue
5724 	 and this will be considered a non-literal.  */
5725       if (found_format_arg)
5726 	return;
5727     }
5728 
5729   if (TREE_CODE (param) == COND_EXPR)
5730     {
5731       /* Simplify to avoid warning for an impossible case.  */
5732       param = fold_for_warn (param);
5733       if (TREE_CODE (param) == COND_EXPR)
5734 	{
5735 	  /* Check both halves of the conditional expression.  */
5736 	  check_function_arguments_recurse (callback, ctx,
5737 					    TREE_OPERAND (param, 1),
5738 					    param_num);
5739 	  check_function_arguments_recurse (callback, ctx,
5740 					    TREE_OPERAND (param, 2),
5741 					    param_num);
5742 	  return;
5743 	}
5744     }
5745 
5746   (*callback) (ctx, param, param_num);
5747 }
5748 
5749 /* Checks for a builtin function FNDECL that the number of arguments
5750    NARGS against the required number REQUIRED and issues an error if
5751    there is a mismatch.  Returns true if the number of arguments is
5752    correct, otherwise false.  LOC is the location of FNDECL.  */
5753 
5754 static bool
5755 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5756 				 int required)
5757 {
5758   if (nargs < required)
5759     {
5760       error_at (loc, "too few arguments to function %qE", fndecl);
5761       return false;
5762     }
5763   else if (nargs > required)
5764     {
5765       error_at (loc, "too many arguments to function %qE", fndecl);
5766       return false;
5767     }
5768   return true;
5769 }
5770 
5771 /* Helper macro for check_builtin_function_arguments.  */
5772 #define ARG_LOCATION(N)					\
5773   (arg_loc.is_empty ()					\
5774    ? EXPR_LOC_OR_LOC (args[(N)], input_location)	\
5775    : expansion_point_location (arg_loc[(N)]))
5776 
5777 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5778    Returns false if there was an error, otherwise true.  LOC is the
5779    location of the function; ARG_LOC is a vector of locations of the
5780    arguments.  */
5781 
5782 bool
5783 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5784 				  tree fndecl, int nargs, tree *args)
5785 {
5786   if (!DECL_BUILT_IN (fndecl)
5787       || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
5788     return true;
5789 
5790   switch (DECL_FUNCTION_CODE (fndecl))
5791     {
5792     case BUILT_IN_ALLOCA_WITH_ALIGN:
5793       {
5794 	/* Get the requested alignment (in bits) if it's a constant
5795 	   integer expression.  */
5796 	unsigned HOST_WIDE_INT align
5797 	  = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5798 
5799 	/* Determine if the requested alignment is a power of 2.  */
5800 	if ((align & (align - 1)))
5801 	  align = 0;
5802 
5803 	/* The maximum alignment in bits corresponding to the same
5804 	   maximum in bytes enforced in check_user_alignment().  */
5805 	unsigned maxalign = (UINT_MAX >> 1) + 1;
5806 
5807 	/* Reject invalid alignments.  */
5808 	if (align < BITS_PER_UNIT || maxalign < align)
5809 	  {
5810 	    error_at (ARG_LOCATION (1),
5811 		      "second argument to function %qE must be a constant "
5812 		      "integer power of 2 between %qi and %qu bits",
5813 		      fndecl, BITS_PER_UNIT, maxalign);
5814 	    return false;
5815 	  }
5816 	return true;
5817       }
5818 
5819     case BUILT_IN_CONSTANT_P:
5820       return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
5821 
5822     case BUILT_IN_ISFINITE:
5823     case BUILT_IN_ISINF:
5824     case BUILT_IN_ISINF_SIGN:
5825     case BUILT_IN_ISNAN:
5826     case BUILT_IN_ISNORMAL:
5827     case BUILT_IN_SIGNBIT:
5828       if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
5829 	{
5830 	  if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5831 	    {
5832 	      error_at (ARG_LOCATION (0), "non-floating-point argument in "
5833 			"call to function %qE", fndecl);
5834 	      return false;
5835 	    }
5836 	  return true;
5837 	}
5838       return false;
5839 
5840     case BUILT_IN_ISGREATER:
5841     case BUILT_IN_ISGREATEREQUAL:
5842     case BUILT_IN_ISLESS:
5843     case BUILT_IN_ISLESSEQUAL:
5844     case BUILT_IN_ISLESSGREATER:
5845     case BUILT_IN_ISUNORDERED:
5846       if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
5847 	{
5848 	  enum tree_code code0, code1;
5849 	  code0 = TREE_CODE (TREE_TYPE (args[0]));
5850 	  code1 = TREE_CODE (TREE_TYPE (args[1]));
5851 	  if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
5852 		|| (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
5853 		|| (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
5854 	    {
5855 	      error_at (loc, "non-floating-point arguments in call to "
5856 			"function %qE", fndecl);
5857 	      return false;
5858 	    }
5859 	  return true;
5860 	}
5861       return false;
5862 
5863     case BUILT_IN_FPCLASSIFY:
5864       if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
5865 	{
5866 	  for (unsigned int i = 0; i < 5; i++)
5867 	    if (TREE_CODE (args[i]) != INTEGER_CST)
5868 	      {
5869 		error_at (ARG_LOCATION (i), "non-const integer argument %u in "
5870 			  "call to function %qE", i + 1, fndecl);
5871 		return false;
5872 	      }
5873 
5874 	  if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
5875 	    {
5876 	      error_at (ARG_LOCATION (5), "non-floating-point argument in "
5877 			"call to function %qE", fndecl);
5878 	      return false;
5879 	    }
5880 	  return true;
5881 	}
5882       return false;
5883 
5884     case BUILT_IN_ASSUME_ALIGNED:
5885       if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
5886 	{
5887 	  if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
5888 	    {
5889 	      error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5890 			"function %qE", fndecl);
5891 	      return false;
5892 	    }
5893 	  return true;
5894 	}
5895       return false;
5896 
5897     case BUILT_IN_ADD_OVERFLOW:
5898     case BUILT_IN_SUB_OVERFLOW:
5899     case BUILT_IN_MUL_OVERFLOW:
5900       if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5901 	{
5902 	  unsigned i;
5903 	  for (i = 0; i < 2; i++)
5904 	    if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5905 	      {
5906 		error_at (ARG_LOCATION (i), "argument %u in call to function "
5907 			  "%qE does not have integral type", i + 1, fndecl);
5908 		return false;
5909 	      }
5910 	  if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
5911 	      || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
5912 	    {
5913 	      error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5914 			"does not have pointer to integral type", fndecl);
5915 	      return false;
5916 	    }
5917 	  else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
5918 	    {
5919 	      error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5920 			"has pointer to enumerated type", fndecl);
5921 	      return false;
5922 	    }
5923 	  else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
5924 	    {
5925 	      error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5926 			"has pointer to boolean type", fndecl);
5927 	      return false;
5928 	    }
5929 	  return true;
5930 	}
5931       return false;
5932 
5933     case BUILT_IN_ADD_OVERFLOW_P:
5934     case BUILT_IN_SUB_OVERFLOW_P:
5935     case BUILT_IN_MUL_OVERFLOW_P:
5936       if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5937 	{
5938 	  unsigned i;
5939 	  for (i = 0; i < 3; i++)
5940 	    if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5941 	      {
5942 		error_at (ARG_LOCATION (i), "argument %u in call to function "
5943 			  "%qE does not have integral type", i + 1, fndecl);
5944 		return false;
5945 	      }
5946 	  if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
5947 	    {
5948 	      error_at (ARG_LOCATION (2), "argument 3 in call to function "
5949 			"%qE has enumerated type", fndecl);
5950 	      return false;
5951 	    }
5952 	  else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
5953 	    {
5954 	      error_at (ARG_LOCATION (2), "argument 3 in call to function "
5955 			"%qE has boolean type", fndecl);
5956 	      return false;
5957 	    }
5958 	  return true;
5959 	}
5960       return false;
5961 
5962     default:
5963       return true;
5964     }
5965 }
5966 
5967 /* Function to help qsort sort FIELD_DECLs by name order.  */
5968 
5969 int
5970 field_decl_cmp (const void *x_p, const void *y_p)
5971 {
5972   const tree *const x = (const tree *const) x_p;
5973   const tree *const y = (const tree *const) y_p;
5974 
5975   if (DECL_NAME (*x) == DECL_NAME (*y))
5976     /* A nontype is "greater" than a type.  */
5977     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5978   if (DECL_NAME (*x) == NULL_TREE)
5979     return -1;
5980   if (DECL_NAME (*y) == NULL_TREE)
5981     return 1;
5982   if (DECL_NAME (*x) < DECL_NAME (*y))
5983     return -1;
5984   return 1;
5985 }
5986 
5987 static struct {
5988   gt_pointer_operator new_value;
5989   void *cookie;
5990 } resort_data;
5991 
5992 /* This routine compares two fields like field_decl_cmp but using the
5993 pointer operator in resort_data.  */
5994 
5995 static int
5996 resort_field_decl_cmp (const void *x_p, const void *y_p)
5997 {
5998   const tree *const x = (const tree *const) x_p;
5999   const tree *const y = (const tree *const) y_p;
6000 
6001   if (DECL_NAME (*x) == DECL_NAME (*y))
6002     /* A nontype is "greater" than a type.  */
6003     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
6004   if (DECL_NAME (*x) == NULL_TREE)
6005     return -1;
6006   if (DECL_NAME (*y) == NULL_TREE)
6007     return 1;
6008   {
6009     tree d1 = DECL_NAME (*x);
6010     tree d2 = DECL_NAME (*y);
6011     resort_data.new_value (&d1, resort_data.cookie);
6012     resort_data.new_value (&d2, resort_data.cookie);
6013     if (d1 < d2)
6014       return -1;
6015   }
6016   return 1;
6017 }
6018 
6019 /* Resort DECL_SORTED_FIELDS because pointers have been reordered.  */
6020 
6021 void
6022 resort_sorted_fields (void *obj,
6023 		      void * ARG_UNUSED (orig_obj),
6024 		      gt_pointer_operator new_value,
6025 		      void *cookie)
6026 {
6027   struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
6028   resort_data.new_value = new_value;
6029   resort_data.cookie = cookie;
6030   qsort (&sf->elts[0], sf->len, sizeof (tree),
6031 	 resort_field_decl_cmp);
6032 }
6033 
6034 /* Subroutine of c_parse_error.
6035    Return the result of concatenating LHS and RHS. RHS is really
6036    a string literal, its first character is indicated by RHS_START and
6037    RHS_SIZE is its length (including the terminating NUL character).
6038 
6039    The caller is responsible for deleting the returned pointer.  */
6040 
6041 static char *
6042 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
6043 {
6044   const int lhs_size = strlen (lhs);
6045   char *result = XNEWVEC (char, lhs_size + rhs_size);
6046   strncpy (result, lhs, lhs_size);
6047   strncpy (result + lhs_size, rhs_start, rhs_size);
6048   return result;
6049 }
6050 
6051 /* Issue the error given by GMSGID, indicating that it occurred before
6052    TOKEN, which had the associated VALUE.  */
6053 
6054 void
6055 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
6056 	       tree value, unsigned char token_flags)
6057 {
6058 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6059 
6060   char *message = NULL;
6061 
6062   if (token_type == CPP_EOF)
6063     message = catenate_messages (gmsgid, " at end of input");
6064   else if (token_type == CPP_CHAR
6065 	   || token_type == CPP_WCHAR
6066 	   || token_type == CPP_CHAR16
6067 	   || token_type == CPP_CHAR32
6068 	   || token_type == CPP_UTF8CHAR)
6069     {
6070       unsigned int val = TREE_INT_CST_LOW (value);
6071       const char *prefix;
6072 
6073       switch (token_type)
6074 	{
6075 	default:
6076 	  prefix = "";
6077 	  break;
6078 	case CPP_WCHAR:
6079 	  prefix = "L";
6080 	  break;
6081 	case CPP_CHAR16:
6082 	  prefix = "u";
6083 	  break;
6084 	case CPP_CHAR32:
6085 	  prefix = "U";
6086 	  break;
6087 	case CPP_UTF8CHAR:
6088 	  prefix = "u8";
6089 	  break;
6090         }
6091 
6092       if (val <= UCHAR_MAX && ISGRAPH (val))
6093 	message = catenate_messages (gmsgid, " before %s'%c'");
6094       else
6095 	message = catenate_messages (gmsgid, " before %s'\\x%x'");
6096 
6097       error (message, prefix, val);
6098       free (message);
6099       message = NULL;
6100     }
6101   else if (token_type == CPP_CHAR_USERDEF
6102 	   || token_type == CPP_WCHAR_USERDEF
6103 	   || token_type == CPP_CHAR16_USERDEF
6104 	   || token_type == CPP_CHAR32_USERDEF
6105 	   || token_type == CPP_UTF8CHAR_USERDEF)
6106     message = catenate_messages (gmsgid,
6107 				 " before user-defined character literal");
6108   else if (token_type == CPP_STRING_USERDEF
6109 	   || token_type == CPP_WSTRING_USERDEF
6110 	   || token_type == CPP_STRING16_USERDEF
6111 	   || token_type == CPP_STRING32_USERDEF
6112 	   || token_type == CPP_UTF8STRING_USERDEF)
6113     message = catenate_messages (gmsgid, " before user-defined string literal");
6114   else if (token_type == CPP_STRING
6115 	   || token_type == CPP_WSTRING
6116 	   || token_type == CPP_STRING16
6117 	   || token_type == CPP_STRING32
6118 	   || token_type == CPP_UTF8STRING)
6119     message = catenate_messages (gmsgid, " before string constant");
6120   else if (token_type == CPP_NUMBER)
6121     message = catenate_messages (gmsgid, " before numeric constant");
6122   else if (token_type == CPP_NAME)
6123     {
6124       message = catenate_messages (gmsgid, " before %qE");
6125       error (message, value);
6126       free (message);
6127       message = NULL;
6128     }
6129   else if (token_type == CPP_PRAGMA)
6130     message = catenate_messages (gmsgid, " before %<#pragma%>");
6131   else if (token_type == CPP_PRAGMA_EOL)
6132     message = catenate_messages (gmsgid, " before end of line");
6133   else if (token_type == CPP_DECLTYPE)
6134     message = catenate_messages (gmsgid, " before %<decltype%>");
6135   else if (token_type < N_TTYPES)
6136     {
6137       message = catenate_messages (gmsgid, " before %qs token");
6138       error (message, cpp_type2name (token_type, token_flags));
6139       free (message);
6140       message = NULL;
6141     }
6142   else
6143     error (gmsgid);
6144 
6145   if (message)
6146     {
6147       error (message);
6148       free (message);
6149     }
6150 #undef catenate_messages
6151 }
6152 
6153 /* Return the gcc option code associated with the reason for a cpp
6154    message, or 0 if none.  */
6155 
6156 static int
6157 c_option_controlling_cpp_error (int reason)
6158 {
6159   const struct cpp_reason_option_codes_t *entry;
6160 
6161   for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6162     {
6163       if (entry->reason == reason)
6164 	return entry->option_code;
6165     }
6166   return 0;
6167 }
6168 
6169 /* Callback from cpp_error for PFILE to print diagnostics from the
6170    preprocessor.  The diagnostic is of type LEVEL, with REASON set
6171    to the reason code if LEVEL is represents a warning, at location
6172    RICHLOC unless this is after lexing and the compiler's location
6173    should be used instead; MSG is the translated message and AP
6174    the arguments.  Returns true if a diagnostic was emitted, false
6175    otherwise.  */
6176 
6177 bool
6178 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
6179 	     rich_location *richloc,
6180 	     const char *msg, va_list *ap)
6181 {
6182   diagnostic_info diagnostic;
6183   diagnostic_t dlevel;
6184   bool save_warn_system_headers = global_dc->dc_warn_system_headers;
6185   bool ret;
6186 
6187   switch (level)
6188     {
6189     case CPP_DL_WARNING_SYSHDR:
6190       if (flag_no_output)
6191 	return false;
6192       global_dc->dc_warn_system_headers = 1;
6193       /* Fall through.  */
6194     case CPP_DL_WARNING:
6195       if (flag_no_output)
6196 	return false;
6197       dlevel = DK_WARNING;
6198       break;
6199     case CPP_DL_PEDWARN:
6200       if (flag_no_output && !flag_pedantic_errors)
6201 	return false;
6202       dlevel = DK_PEDWARN;
6203       break;
6204     case CPP_DL_ERROR:
6205       dlevel = DK_ERROR;
6206       break;
6207     case CPP_DL_ICE:
6208       dlevel = DK_ICE;
6209       break;
6210     case CPP_DL_NOTE:
6211       dlevel = DK_NOTE;
6212       break;
6213     case CPP_DL_FATAL:
6214       dlevel = DK_FATAL;
6215       break;
6216     default:
6217       gcc_unreachable ();
6218     }
6219   if (done_lexing)
6220     richloc->set_range (line_table, 0, input_location, true);
6221   diagnostic_set_info_translated (&diagnostic, msg, ap,
6222 				  richloc, dlevel);
6223   diagnostic_override_option_index (&diagnostic,
6224                                     c_option_controlling_cpp_error (reason));
6225   ret = report_diagnostic (&diagnostic);
6226   if (level == CPP_DL_WARNING_SYSHDR)
6227     global_dc->dc_warn_system_headers = save_warn_system_headers;
6228   return ret;
6229 }
6230 
6231 /* Convert a character from the host to the target execution character
6232    set.  cpplib handles this, mostly.  */
6233 
6234 HOST_WIDE_INT
6235 c_common_to_target_charset (HOST_WIDE_INT c)
6236 {
6237   /* Character constants in GCC proper are sign-extended under -fsigned-char,
6238      zero-extended under -fno-signed-char.  cpplib insists that characters
6239      and character constants are always unsigned.  Hence we must convert
6240      back and forth.  */
6241   cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6242 
6243   uc = cpp_host_to_exec_charset (parse_in, uc);
6244 
6245   if (flag_signed_char)
6246     return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6247 			       >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6248   else
6249     return uc;
6250 }
6251 
6252 /* Fold an offsetof-like expression.  EXPR is a nested sequence of component
6253    references with an INDIRECT_REF of a constant at the bottom; much like the
6254    traditional rendering of offsetof as a macro.  TYPE is the desired type of
6255    the whole expression.  Return the folded result.  */
6256 
6257 tree
6258 fold_offsetof (tree expr, tree type, enum tree_code ctx)
6259 {
6260   tree base, off, t;
6261   tree_code code = TREE_CODE (expr);
6262   switch (code)
6263     {
6264     case ERROR_MARK:
6265       return expr;
6266 
6267     case VAR_DECL:
6268       error ("cannot apply %<offsetof%> to static data member %qD", expr);
6269       return error_mark_node;
6270 
6271     case CALL_EXPR:
6272     case TARGET_EXPR:
6273       error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6274       return error_mark_node;
6275 
6276     case NOP_EXPR:
6277     case INDIRECT_REF:
6278       if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
6279 	{
6280 	  error ("cannot apply %<offsetof%> to a non constant address");
6281 	  return error_mark_node;
6282 	}
6283       return convert (type, TREE_OPERAND (expr, 0));
6284 
6285     case COMPONENT_REF:
6286       base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
6287       if (base == error_mark_node)
6288 	return base;
6289 
6290       t = TREE_OPERAND (expr, 1);
6291       if (DECL_C_BIT_FIELD (t))
6292 	{
6293 	  error ("attempt to take address of bit-field structure "
6294 		 "member %qD", t);
6295 	  return error_mark_node;
6296 	}
6297       off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
6298 			    size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
6299 				      / BITS_PER_UNIT));
6300       break;
6301 
6302     case ARRAY_REF:
6303       base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
6304       if (base == error_mark_node)
6305 	return base;
6306 
6307       t = TREE_OPERAND (expr, 1);
6308 
6309       /* Check if the offset goes beyond the upper bound of the array.  */
6310       if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
6311 	{
6312 	  tree upbound = array_ref_up_bound (expr);
6313 	  if (upbound != NULL_TREE
6314 	      && TREE_CODE (upbound) == INTEGER_CST
6315 	      && !tree_int_cst_equal (upbound,
6316 				      TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6317 	    {
6318 	      if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6319 	        upbound = size_binop (PLUS_EXPR, upbound,
6320 				      build_int_cst (TREE_TYPE (upbound), 1));
6321 	      if (tree_int_cst_lt (upbound, t))
6322 		{
6323 		  tree v;
6324 
6325 		  for (v = TREE_OPERAND (expr, 0);
6326 		       TREE_CODE (v) == COMPONENT_REF;
6327 		       v = TREE_OPERAND (v, 0))
6328 		    if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6329 			== RECORD_TYPE)
6330 		      {
6331 			tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6332 			for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
6333 			  if (TREE_CODE (fld_chain) == FIELD_DECL)
6334 			    break;
6335 
6336 			if (fld_chain)
6337 			  break;
6338 		      }
6339 		  /* Don't warn if the array might be considered a poor
6340 		     man's flexible array member with a very permissive
6341 		     definition thereof.  */
6342 		  if (TREE_CODE (v) == ARRAY_REF
6343 		      || TREE_CODE (v) == COMPONENT_REF)
6344 		    warning (OPT_Warray_bounds,
6345 			     "index %E denotes an offset "
6346 			     "greater than size of %qT",
6347 			     t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6348 		}
6349 	    }
6350 	}
6351 
6352       t = convert (sizetype, t);
6353       off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
6354       break;
6355 
6356     case COMPOUND_EXPR:
6357       /* Handle static members of volatile structs.  */
6358       t = TREE_OPERAND (expr, 1);
6359       gcc_assert (VAR_P (t));
6360       return fold_offsetof (t, type);
6361 
6362     default:
6363       gcc_unreachable ();
6364     }
6365 
6366   if (!POINTER_TYPE_P (type))
6367     return size_binop (PLUS_EXPR, base, convert (type, off));
6368   return fold_build_pointer_plus (base, off);
6369 }
6370 
6371 /* *PTYPE is an incomplete array.  Complete it with a domain based on
6372    INITIAL_VALUE.  If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6373    is true.  Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6374    2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty.  */
6375 
6376 int
6377 complete_array_type (tree *ptype, tree initial_value, bool do_default)
6378 {
6379   tree maxindex, type, main_type, elt, unqual_elt;
6380   int failure = 0, quals;
6381   bool overflow_p = false;
6382 
6383   maxindex = size_zero_node;
6384   if (initial_value)
6385     {
6386       if (TREE_CODE (initial_value) == STRING_CST)
6387 	{
6388 	  int eltsize
6389 	    = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6390 	  maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6391 	}
6392       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6393 	{
6394 	  vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
6395 
6396 	  if (vec_safe_is_empty (v))
6397 	    {
6398 	      if (pedantic)
6399 		failure = 3;
6400 	      maxindex = ssize_int (-1);
6401 	    }
6402 	  else
6403 	    {
6404 	      tree curindex;
6405 	      unsigned HOST_WIDE_INT cnt;
6406 	      constructor_elt *ce;
6407 	      bool fold_p = false;
6408 
6409 	      if ((*v)[0].index)
6410 		maxindex = (*v)[0].index, fold_p = true;
6411 
6412 	      curindex = maxindex;
6413 
6414 	      for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
6415 		{
6416 		  bool curfold_p = false;
6417 		  if (ce->index)
6418 		    curindex = ce->index, curfold_p = true;
6419 		  else
6420 		    {
6421 		      if (fold_p)
6422 			{
6423 			  /* Since we treat size types now as ordinary
6424 			     unsigned types, we need an explicit overflow
6425 			     check.  */
6426 			  tree orig = curindex;
6427 		          curindex = fold_convert (sizetype, curindex);
6428 			  overflow_p |= tree_int_cst_lt (curindex, orig);
6429 			}
6430 		      curindex = size_binop (PLUS_EXPR, curindex,
6431 					     size_one_node);
6432 		    }
6433 		  if (tree_int_cst_lt (maxindex, curindex))
6434 		    maxindex = curindex, fold_p = curfold_p;
6435 		}
6436 	      if (fold_p)
6437 		{
6438 		  tree orig = maxindex;
6439 	          maxindex = fold_convert (sizetype, maxindex);
6440 		  overflow_p |= tree_int_cst_lt (maxindex, orig);
6441 		}
6442 	    }
6443 	}
6444       else
6445 	{
6446 	  /* Make an error message unless that happened already.  */
6447 	  if (initial_value != error_mark_node)
6448 	    failure = 1;
6449 	}
6450     }
6451   else
6452     {
6453       failure = 2;
6454       if (!do_default)
6455 	return failure;
6456     }
6457 
6458   type = *ptype;
6459   elt = TREE_TYPE (type);
6460   quals = TYPE_QUALS (strip_array_types (elt));
6461   if (quals == 0)
6462     unqual_elt = elt;
6463   else
6464     unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
6465 
6466   /* Using build_distinct_type_copy and modifying things afterward instead
6467      of using build_array_type to create a new type preserves all of the
6468      TYPE_LANG_FLAG_? bits that the front end may have set.  */
6469   main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6470   TREE_TYPE (main_type) = unqual_elt;
6471   TYPE_DOMAIN (main_type)
6472     = build_range_type (TREE_TYPE (maxindex),
6473 			build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
6474   TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
6475   layout_type (main_type);
6476 
6477   /* Make sure we have the canonical MAIN_TYPE. */
6478   inchash::hash hstate;
6479   hstate.add_object (TYPE_HASH (unqual_elt));
6480   hstate.add_object (TYPE_HASH (TYPE_DOMAIN (main_type)));
6481   if (!AGGREGATE_TYPE_P (unqual_elt))
6482     hstate.add_flag (TYPE_TYPELESS_STORAGE (main_type));
6483   main_type = type_hash_canon (hstate.end (), main_type);
6484 
6485   /* Fix the canonical type.  */
6486   if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6487       || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6488     SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6489   else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6490 	   || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6491 	       != TYPE_DOMAIN (main_type)))
6492     TYPE_CANONICAL (main_type)
6493       = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
6494 			  TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
6495 			  TYPE_TYPELESS_STORAGE (main_type));
6496   else
6497     TYPE_CANONICAL (main_type) = main_type;
6498 
6499   if (quals == 0)
6500     type = main_type;
6501   else
6502     type = c_build_qualified_type (main_type, quals);
6503 
6504   if (COMPLETE_TYPE_P (type)
6505       && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6506       && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
6507     {
6508       error ("size of array is too large");
6509       /* If we proceed with the array type as it is, we'll eventually
6510 	 crash in tree_to_[su]hwi().  */
6511       type = error_mark_node;
6512     }
6513 
6514   *ptype = type;
6515   return failure;
6516 }
6517 
6518 /* Like c_mark_addressable but don't check register qualifier.  */
6519 void
6520 c_common_mark_addressable_vec (tree t)
6521 {
6522   if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
6523     t = C_MAYBE_CONST_EXPR_EXPR (t);
6524   while (handled_component_p (t))
6525     t = TREE_OPERAND (t, 0);
6526   if (!VAR_P (t)
6527       && TREE_CODE (t) != PARM_DECL
6528       && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
6529     return;
6530   if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
6531     TREE_ADDRESSABLE (t) = 1;
6532 }
6533 
6534 
6535 
6536 /* Used to help initialize the builtin-types.def table.  When a type of
6537    the correct size doesn't exist, use error_mark_node instead of NULL.
6538    The later results in segfaults even when a decl using the type doesn't
6539    get invoked.  */
6540 
6541 tree
6542 builtin_type_for_size (int size, bool unsignedp)
6543 {
6544   tree type = c_common_type_for_size (size, unsignedp);
6545   return type ? type : error_mark_node;
6546 }
6547 
6548 /* A helper function for resolve_overloaded_builtin in resolving the
6549    overloaded __sync_ builtins.  Returns a positive power of 2 if the
6550    first operand of PARAMS is a pointer to a supported data type.
6551    Returns 0 if an error is encountered.
6552    FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6553    built-ins.  */
6554 
6555 static int
6556 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
6557 {
6558   /* Type of the argument.  */
6559   tree argtype;
6560   /* Type the argument points to.  */
6561   tree type;
6562   int size;
6563 
6564   if (vec_safe_is_empty (params))
6565     {
6566       error ("too few arguments to function %qE", function);
6567       return 0;
6568     }
6569 
6570   argtype = type = TREE_TYPE ((*params)[0]);
6571   if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6572     {
6573       /* Force array-to-pointer decay for C++.  */
6574       (*params)[0] = default_conversion ((*params)[0]);
6575       type = TREE_TYPE ((*params)[0]);
6576     }
6577   if (TREE_CODE (type) != POINTER_TYPE)
6578     goto incompatible;
6579 
6580   type = TREE_TYPE (type);
6581   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6582     goto incompatible;
6583 
6584   if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6585     goto incompatible;
6586 
6587   size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6588   if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6589     return size;
6590 
6591  incompatible:
6592   /* Issue the diagnostic only if the argument is valid, otherwise
6593      it would be redundant at best and could be misleading.  */
6594   if (argtype != error_mark_node)
6595     error ("operand type %qT is incompatible with argument %d of %qE",
6596 	   argtype, 1, function);
6597   return 0;
6598 }
6599 
6600 /* A helper function for resolve_overloaded_builtin.  Adds casts to
6601    PARAMS to make arguments match up with those of FUNCTION.  Drops
6602    the variadic arguments at the end.  Returns false if some error
6603    was encountered; true on success.  */
6604 
6605 static bool
6606 sync_resolve_params (location_t loc, tree orig_function, tree function,
6607 		     vec<tree, va_gc> *params, bool orig_format)
6608 {
6609   function_args_iterator iter;
6610   tree ptype;
6611   unsigned int parmnum;
6612 
6613   function_args_iter_init (&iter, TREE_TYPE (function));
6614   /* We've declared the implementation functions to use "volatile void *"
6615      as the pointer parameter, so we shouldn't get any complaints from the
6616      call to check_function_arguments what ever type the user used.  */
6617   function_args_iter_next (&iter);
6618   ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
6619   ptype = TYPE_MAIN_VARIANT (ptype);
6620 
6621   /* For the rest of the values, we need to cast these to FTYPE, so that we
6622      don't get warnings for passing pointer types, etc.  */
6623   parmnum = 0;
6624   while (1)
6625     {
6626       tree val, arg_type;
6627 
6628       arg_type = function_args_iter_cond (&iter);
6629       /* XXX void_type_node belies the abstraction.  */
6630       if (arg_type == void_type_node)
6631 	break;
6632 
6633       ++parmnum;
6634       if (params->length () <= parmnum)
6635 	{
6636 	  error_at (loc, "too few arguments to function %qE", orig_function);
6637 	  return false;
6638 	}
6639 
6640       /* Only convert parameters if arg_type is unsigned integer type with
6641 	 new format sync routines, i.e. don't attempt to convert pointer
6642 	 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6643 	 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6644 	 kinds).  */
6645       if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
6646 	{
6647 	  /* Ideally for the first conversion we'd use convert_for_assignment
6648 	     so that we get warnings for anything that doesn't match the pointer
6649 	     type.  This isn't portable across the C and C++ front ends atm.  */
6650 	  val = (*params)[parmnum];
6651 	  val = convert (ptype, val);
6652 	  val = convert (arg_type, val);
6653 	  (*params)[parmnum] = val;
6654 	}
6655 
6656       function_args_iter_next (&iter);
6657     }
6658 
6659   /* __atomic routines are not variadic.  */
6660   if (!orig_format && params->length () != parmnum + 1)
6661     {
6662       error_at (loc, "too many arguments to function %qE", orig_function);
6663       return false;
6664     }
6665 
6666   /* The definition of these primitives is variadic, with the remaining
6667      being "an optional list of variables protected by the memory barrier".
6668      No clue what that's supposed to mean, precisely, but we consider all
6669      call-clobbered variables to be protected so we're safe.  */
6670   params->truncate (parmnum + 1);
6671 
6672   return true;
6673 }
6674 
6675 /* A helper function for resolve_overloaded_builtin.  Adds a cast to
6676    RESULT to make it match the type of the first pointer argument in
6677    PARAMS.  */
6678 
6679 static tree
6680 sync_resolve_return (tree first_param, tree result, bool orig_format)
6681 {
6682   tree ptype = TREE_TYPE (TREE_TYPE (first_param));
6683   tree rtype = TREE_TYPE (result);
6684   ptype = TYPE_MAIN_VARIANT (ptype);
6685 
6686   /* New format doesn't require casting unless the types are the same size.  */
6687   if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6688     return convert (ptype, result);
6689   else
6690     return result;
6691 }
6692 
6693 /* This function verifies the PARAMS to generic atomic FUNCTION.
6694    It returns the size if all the parameters are the same size, otherwise
6695    0 is returned if the parameters are invalid.  */
6696 
6697 static int
6698 get_atomic_generic_size (location_t loc, tree function,
6699 			 vec<tree, va_gc> *params)
6700 {
6701   unsigned int n_param;
6702   unsigned int n_model;
6703   unsigned int x;
6704   int size_0;
6705   tree type_0;
6706 
6707   /* Determine the parameter makeup.  */
6708   switch (DECL_FUNCTION_CODE (function))
6709     {
6710     case BUILT_IN_ATOMIC_EXCHANGE:
6711       n_param = 4;
6712       n_model = 1;
6713       break;
6714     case BUILT_IN_ATOMIC_LOAD:
6715     case BUILT_IN_ATOMIC_STORE:
6716       n_param = 3;
6717       n_model = 1;
6718       break;
6719     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6720       n_param = 6;
6721       n_model = 2;
6722       break;
6723     default:
6724       gcc_unreachable ();
6725     }
6726 
6727   if (vec_safe_length (params) != n_param)
6728     {
6729       error_at (loc, "incorrect number of arguments to function %qE", function);
6730       return 0;
6731     }
6732 
6733   /* Get type of first parameter, and determine its size.  */
6734   type_0 = TREE_TYPE ((*params)[0]);
6735   if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
6736     {
6737       /* Force array-to-pointer decay for C++.  */
6738       (*params)[0] = default_conversion ((*params)[0]);
6739       type_0 = TREE_TYPE ((*params)[0]);
6740     }
6741   if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6742     {
6743       error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6744 		function);
6745       return 0;
6746     }
6747 
6748   /* Types must be compile time constant sizes. */
6749   if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
6750     {
6751       error_at (loc,
6752 		"argument 1 of %qE must be a pointer to a constant size type",
6753 		function);
6754       return 0;
6755     }
6756 
6757   size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
6758 
6759   /* Zero size objects are not allowed.  */
6760   if (size_0 == 0)
6761     {
6762       error_at (loc,
6763 		"argument 1 of %qE must be a pointer to a nonzero size object",
6764 		function);
6765       return 0;
6766     }
6767 
6768   /* Check each other parameter is a pointer and the same size.  */
6769   for (x = 0; x < n_param - n_model; x++)
6770     {
6771       int size;
6772       tree type = TREE_TYPE ((*params)[x]);
6773       /* __atomic_compare_exchange has a bool in the 4th position, skip it.  */
6774       if (n_param == 6 && x == 3)
6775         continue;
6776       if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6777 	{
6778 	  /* Force array-to-pointer decay for C++.  */
6779 	  (*params)[x] = default_conversion ((*params)[x]);
6780 	  type = TREE_TYPE ((*params)[x]);
6781 	}
6782       if (!POINTER_TYPE_P (type))
6783 	{
6784 	  error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
6785 		    function);
6786 	  return 0;
6787 	}
6788       else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
6789 	       && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
6790 		  != INTEGER_CST)
6791 	{
6792 	  error_at (loc, "argument %d of %qE must be a pointer to a constant "
6793 		    "size type", x + 1, function);
6794 	  return 0;
6795 	}
6796       else if (FUNCTION_POINTER_TYPE_P (type))
6797 	{
6798 	  error_at (loc, "argument %d of %qE must not be a pointer to a "
6799 		    "function", x + 1, function);
6800 	  return 0;
6801 	}
6802       tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
6803       size = type_size ? tree_to_uhwi (type_size) : 0;
6804       if (size != size_0)
6805 	{
6806 	  error_at (loc, "size mismatch in argument %d of %qE", x + 1,
6807 		    function);
6808 	  return 0;
6809 	}
6810     }
6811 
6812   /* Check memory model parameters for validity.  */
6813   for (x = n_param - n_model ; x < n_param; x++)
6814     {
6815       tree p = (*params)[x];
6816       if (TREE_CODE (p) == INTEGER_CST)
6817         {
6818 	  /* memmodel_base masks the low 16 bits, thus ignore any bits above
6819 	     it by using TREE_INT_CST_LOW instead of tree_to_*hwi.  Those high
6820 	     bits will be checked later during expansion in target specific
6821 	     way.  */
6822 	  if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
6823 	    warning_at (loc, OPT_Winvalid_memory_model,
6824 			"invalid memory model argument %d of %qE", x + 1,
6825 			function);
6826 	}
6827       else
6828 	if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6829 	  {
6830 	    error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6831 		   function);
6832 	    return 0;
6833 	  }
6834       }
6835 
6836   return size_0;
6837 }
6838 
6839 
6840 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6841    at the beginning of the parameter list PARAMS representing the size of the
6842    objects.  This is to match the library ABI requirement.  LOC is the location
6843    of the function call.
6844    The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6845    returned to allow the external call to be constructed.  */
6846 
6847 static tree
6848 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
6849 			   vec<tree, va_gc> *params)
6850 {
6851   tree size_node;
6852 
6853   /* Insert a SIZE_T parameter as the first param.  If there isn't
6854      enough space, allocate a new vector and recursively re-build with that.  */
6855   if (!params->space (1))
6856     {
6857       unsigned int z, len;
6858       vec<tree, va_gc> *v;
6859       tree f;
6860 
6861       len = params->length ();
6862       vec_alloc (v, len + 1);
6863       v->quick_push (build_int_cst (size_type_node, n));
6864       for (z = 0; z < len; z++)
6865 	v->quick_push ((*params)[z]);
6866       f = build_function_call_vec (loc, vNULL, function, v, NULL);
6867       vec_free (v);
6868       return f;
6869     }
6870 
6871   /* Add the size parameter and leave as a function call for processing.  */
6872   size_node = build_int_cst (size_type_node, n);
6873   params->quick_insert (0, size_node);
6874   return NULL_TREE;
6875 }
6876 
6877 
6878 /* Return whether atomic operations for naturally aligned N-byte
6879    arguments are supported, whether inline or through libatomic.  */
6880 static bool
6881 atomic_size_supported_p (int n)
6882 {
6883   switch (n)
6884     {
6885     case 1:
6886     case 2:
6887     case 4:
6888     case 8:
6889       return true;
6890 
6891     case 16:
6892       return targetm.scalar_mode_supported_p (TImode);
6893 
6894     default:
6895       return false;
6896     }
6897 }
6898 
6899 /* This will process an __atomic_exchange function call, determine whether it
6900    needs to be mapped to the _N variation, or turned into a library call.
6901    LOC is the location of the builtin call.
6902    FUNCTION is the DECL that has been invoked;
6903    PARAMS is the argument list for the call.  The return value is non-null
6904    TRUE is returned if it is translated into the proper format for a call to the
6905    external library, and NEW_RETURN is set the tree for that function.
6906    FALSE is returned if processing for the _N variation is required, and
6907    NEW_RETURN is set to the return value the result is copied into.  */
6908 static bool
6909 resolve_overloaded_atomic_exchange (location_t loc, tree function,
6910 				    vec<tree, va_gc> *params, tree *new_return)
6911 {
6912   tree p0, p1, p2, p3;
6913   tree I_type, I_type_ptr;
6914   int n = get_atomic_generic_size (loc, function, params);
6915 
6916   /* Size of 0 is an error condition.  */
6917   if (n == 0)
6918     {
6919       *new_return = error_mark_node;
6920       return true;
6921     }
6922 
6923   /* If not a lock-free size, change to the library generic format.  */
6924   if (!atomic_size_supported_p (n))
6925     {
6926       *new_return = add_atomic_size_parameter (n, loc, function, params);
6927       return true;
6928     }
6929 
6930   /* Otherwise there is a lockfree match, transform the call from:
6931        void fn(T* mem, T* desired, T* return, model)
6932      into
6933        *return = (T) (fn (In* mem, (In) *desired, model))  */
6934 
6935   p0 = (*params)[0];
6936   p1 = (*params)[1];
6937   p2 = (*params)[2];
6938   p3 = (*params)[3];
6939 
6940   /* Create pointer to appropriate size.  */
6941   I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6942   I_type_ptr = build_pointer_type (I_type);
6943 
6944   /* Convert object pointer to required type.  */
6945   p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6946   (*params)[0] = p0;
6947   /* Convert new value to required type, and dereference it.  */
6948   p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
6949   p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
6950   (*params)[1] = p1;
6951 
6952   /* Move memory model to the 3rd position, and end param list.  */
6953   (*params)[2] = p3;
6954   params->truncate (3);
6955 
6956   /* Convert return pointer and dereference it for later assignment.  */
6957   *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6958 
6959   return false;
6960 }
6961 
6962 
6963 /* This will process an __atomic_compare_exchange function call, determine
6964    whether it needs to be mapped to the _N variation, or turned into a lib call.
6965    LOC is the location of the builtin call.
6966    FUNCTION is the DECL that has been invoked;
6967    PARAMS is the argument list for the call.  The return value is non-null
6968    TRUE is returned if it is translated into the proper format for a call to the
6969    external library, and NEW_RETURN is set the tree for that function.
6970    FALSE is returned if processing for the _N variation is required.  */
6971 
6972 static bool
6973 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
6974 					    vec<tree, va_gc> *params,
6975 					    tree *new_return)
6976 {
6977   tree p0, p1, p2;
6978   tree I_type, I_type_ptr;
6979   int n = get_atomic_generic_size (loc, function, params);
6980 
6981   /* Size of 0 is an error condition.  */
6982   if (n == 0)
6983     {
6984       *new_return = error_mark_node;
6985       return true;
6986     }
6987 
6988   /* If not a lock-free size, change to the library generic format.  */
6989   if (!atomic_size_supported_p (n))
6990     {
6991       /* The library generic format does not have the weak parameter, so
6992 	 remove it from the param list.  Since a parameter has been removed,
6993 	 we can be sure that there is room for the SIZE_T parameter, meaning
6994 	 there will not be a recursive rebuilding of the parameter list, so
6995 	 there is no danger this will be done twice.  */
6996       if (n > 0)
6997         {
6998 	  (*params)[3] = (*params)[4];
6999 	  (*params)[4] = (*params)[5];
7000 	  params->truncate (5);
7001 	}
7002       *new_return = add_atomic_size_parameter (n, loc, function, params);
7003       return true;
7004     }
7005 
7006   /* Otherwise, there is a match, so the call needs to be transformed from:
7007        bool fn(T* mem, T* desired, T* return, weak, success, failure)
7008      into
7009        bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail)  */
7010 
7011   p0 = (*params)[0];
7012   p1 = (*params)[1];
7013   p2 = (*params)[2];
7014 
7015   /* Create pointer to appropriate size.  */
7016   I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7017   I_type_ptr = build_pointer_type (I_type);
7018 
7019   /* Convert object pointer to required type.  */
7020   p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7021   (*params)[0] = p0;
7022 
7023   /* Convert expected pointer to required type.  */
7024   p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
7025   (*params)[1] = p1;
7026 
7027   /* Convert desired value to required type, and dereference it.  */
7028   p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7029   p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
7030   (*params)[2] = p2;
7031 
7032   /* The rest of the parameters are fine. NULL means no special return value
7033      processing.*/
7034   *new_return = NULL;
7035   return false;
7036 }
7037 
7038 
7039 /* This will process an __atomic_load function call, determine whether it
7040    needs to be mapped to the _N variation, or turned into a library call.
7041    LOC is the location of the builtin call.
7042    FUNCTION is the DECL that has been invoked;
7043    PARAMS is the argument list for the call.  The return value is non-null
7044    TRUE is returned if it is translated into the proper format for a call to the
7045    external library, and NEW_RETURN is set the tree for that function.
7046    FALSE is returned if processing for the _N variation is required, and
7047    NEW_RETURN is set to the return value the result is copied into.  */
7048 
7049 static bool
7050 resolve_overloaded_atomic_load (location_t loc, tree function,
7051 				vec<tree, va_gc> *params, tree *new_return)
7052 {
7053   tree p0, p1, p2;
7054   tree I_type, I_type_ptr;
7055   int n = get_atomic_generic_size (loc, function, params);
7056 
7057   /* Size of 0 is an error condition.  */
7058   if (n == 0)
7059     {
7060       *new_return = error_mark_node;
7061       return true;
7062     }
7063 
7064   /* If not a lock-free size, change to the library generic format.  */
7065   if (!atomic_size_supported_p (n))
7066     {
7067       *new_return = add_atomic_size_parameter (n, loc, function, params);
7068       return true;
7069     }
7070 
7071   /* Otherwise, there is a match, so the call needs to be transformed from:
7072        void fn(T* mem, T* return, model)
7073      into
7074        *return = (T) (fn ((In *) mem, model))  */
7075 
7076   p0 = (*params)[0];
7077   p1 = (*params)[1];
7078   p2 = (*params)[2];
7079 
7080   /* Create pointer to appropriate size.  */
7081   I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7082   I_type_ptr = build_pointer_type (I_type);
7083 
7084   /* Convert object pointer to required type.  */
7085   p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7086   (*params)[0] = p0;
7087 
7088   /* Move memory model to the 2nd position, and end param list.  */
7089   (*params)[1] = p2;
7090   params->truncate (2);
7091 
7092   /* Convert return pointer and dereference it for later assignment.  */
7093   *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7094 
7095   return false;
7096 }
7097 
7098 
7099 /* This will process an __atomic_store function call, determine whether it
7100    needs to be mapped to the _N variation, or turned into a library call.
7101    LOC is the location of the builtin call.
7102    FUNCTION is the DECL that has been invoked;
7103    PARAMS is the argument list for the call.  The return value is non-null
7104    TRUE is returned if it is translated into the proper format for a call to the
7105    external library, and NEW_RETURN is set the tree for that function.
7106    FALSE is returned if processing for the _N variation is required, and
7107    NEW_RETURN is set to the return value the result is copied into.  */
7108 
7109 static bool
7110 resolve_overloaded_atomic_store (location_t loc, tree function,
7111 				 vec<tree, va_gc> *params, tree *new_return)
7112 {
7113   tree p0, p1;
7114   tree I_type, I_type_ptr;
7115   int n = get_atomic_generic_size (loc, function, params);
7116 
7117   /* Size of 0 is an error condition.  */
7118   if (n == 0)
7119     {
7120       *new_return = error_mark_node;
7121       return true;
7122     }
7123 
7124   /* If not a lock-free size, change to the library generic format.  */
7125   if (!atomic_size_supported_p (n))
7126     {
7127       *new_return = add_atomic_size_parameter (n, loc, function, params);
7128       return true;
7129     }
7130 
7131   /* Otherwise, there is a match, so the call needs to be transformed from:
7132        void fn(T* mem, T* value, model)
7133      into
7134        fn ((In *) mem, (In) *value, model)  */
7135 
7136   p0 = (*params)[0];
7137   p1 = (*params)[1];
7138 
7139   /* Create pointer to appropriate size.  */
7140   I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7141   I_type_ptr = build_pointer_type (I_type);
7142 
7143   /* Convert object pointer to required type.  */
7144   p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7145   (*params)[0] = p0;
7146 
7147   /* Convert new value to required type, and dereference it.  */
7148   p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7149   p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7150   (*params)[1] = p1;
7151 
7152   /* The memory model is in the right spot already. Return is void.  */
7153   *new_return = NULL_TREE;
7154 
7155   return false;
7156 }
7157 
7158 
7159 /* Some builtin functions are placeholders for other expressions.  This
7160    function should be called immediately after parsing the call expression
7161    before surrounding code has committed to the type of the expression.
7162 
7163    LOC is the location of the builtin call.
7164 
7165    FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7166    PARAMS is the argument list for the call.  The return value is non-null
7167    when expansion is complete, and null if normal processing should
7168    continue.  */
7169 
7170 tree
7171 resolve_overloaded_builtin (location_t loc, tree function,
7172 			    vec<tree, va_gc> *params)
7173 {
7174   enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7175 
7176   /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7177      Those are not valid to call with a pointer to _Bool (or C++ bool)
7178      and so must be rejected.  */
7179   bool fetch_op = true;
7180   bool orig_format = true;
7181   tree new_return = NULL_TREE;
7182 
7183   switch (DECL_BUILT_IN_CLASS (function))
7184     {
7185     case BUILT_IN_NORMAL:
7186       break;
7187     case BUILT_IN_MD:
7188       if (targetm.resolve_overloaded_builtin)
7189 	return targetm.resolve_overloaded_builtin (loc, function, params);
7190       else
7191 	return NULL_TREE;
7192     default:
7193       return NULL_TREE;
7194     }
7195 
7196   /* Handle BUILT_IN_NORMAL here.  */
7197   switch (orig_code)
7198     {
7199     case BUILT_IN_ATOMIC_EXCHANGE:
7200     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7201     case BUILT_IN_ATOMIC_LOAD:
7202     case BUILT_IN_ATOMIC_STORE:
7203       {
7204 	/* Handle these 4 together so that they can fall through to the next
7205 	   case if the call is transformed to an _N variant.  */
7206         switch (orig_code)
7207 	  {
7208 	  case BUILT_IN_ATOMIC_EXCHANGE:
7209 	    {
7210 	      if (resolve_overloaded_atomic_exchange (loc, function, params,
7211 						      &new_return))
7212 		return new_return;
7213 	      /* Change to the _N variant.  */
7214 	      orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7215 	      break;
7216 	    }
7217 
7218 	  case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7219 	    {
7220 	      if (resolve_overloaded_atomic_compare_exchange (loc, function,
7221 							      params,
7222 							      &new_return))
7223 		return new_return;
7224 	      /* Change to the _N variant.  */
7225 	      orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7226 	      break;
7227 	    }
7228 	  case BUILT_IN_ATOMIC_LOAD:
7229 	    {
7230 	      if (resolve_overloaded_atomic_load (loc, function, params,
7231 						  &new_return))
7232 		return new_return;
7233 	      /* Change to the _N variant.  */
7234 	      orig_code = BUILT_IN_ATOMIC_LOAD_N;
7235 	      break;
7236 	    }
7237 	  case BUILT_IN_ATOMIC_STORE:
7238 	    {
7239 	      if (resolve_overloaded_atomic_store (loc, function, params,
7240 						   &new_return))
7241 		return new_return;
7242 	      /* Change to the _N variant.  */
7243 	      orig_code = BUILT_IN_ATOMIC_STORE_N;
7244 	      break;
7245 	    }
7246 	  default:
7247 	    gcc_unreachable ();
7248 	  }
7249       }
7250       /* FALLTHRU */
7251     case BUILT_IN_ATOMIC_EXCHANGE_N:
7252     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7253     case BUILT_IN_ATOMIC_LOAD_N:
7254     case BUILT_IN_ATOMIC_STORE_N:
7255       fetch_op = false;
7256       /* FALLTHRU */
7257     case BUILT_IN_ATOMIC_ADD_FETCH_N:
7258     case BUILT_IN_ATOMIC_SUB_FETCH_N:
7259     case BUILT_IN_ATOMIC_AND_FETCH_N:
7260     case BUILT_IN_ATOMIC_NAND_FETCH_N:
7261     case BUILT_IN_ATOMIC_XOR_FETCH_N:
7262     case BUILT_IN_ATOMIC_OR_FETCH_N:
7263     case BUILT_IN_ATOMIC_FETCH_ADD_N:
7264     case BUILT_IN_ATOMIC_FETCH_SUB_N:
7265     case BUILT_IN_ATOMIC_FETCH_AND_N:
7266     case BUILT_IN_ATOMIC_FETCH_NAND_N:
7267     case BUILT_IN_ATOMIC_FETCH_XOR_N:
7268     case BUILT_IN_ATOMIC_FETCH_OR_N:
7269       orig_format = false;
7270       /* FALLTHRU */
7271     case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7272     case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7273     case BUILT_IN_SYNC_FETCH_AND_OR_N:
7274     case BUILT_IN_SYNC_FETCH_AND_AND_N:
7275     case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7276     case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7277     case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7278     case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7279     case BUILT_IN_SYNC_OR_AND_FETCH_N:
7280     case BUILT_IN_SYNC_AND_AND_FETCH_N:
7281     case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7282     case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7283     case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7284     case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7285     case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7286     case BUILT_IN_SYNC_LOCK_RELEASE_N:
7287       {
7288 	/* The following are not _FETCH_OPs and must be accepted with
7289 	   pointers to _Bool (or C++ bool).  */
7290 	if (fetch_op)
7291 	  fetch_op =
7292 	    (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7293 	     && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7294 	     && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7295 	     && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7296 
7297 	int n = sync_resolve_size (function, params, fetch_op);
7298 	tree new_function, first_param, result;
7299 	enum built_in_function fncode;
7300 
7301 	if (n == 0)
7302 	  return error_mark_node;
7303 
7304 	fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7305 	new_function = builtin_decl_explicit (fncode);
7306 	if (!sync_resolve_params (loc, function, new_function, params,
7307 				  orig_format))
7308 	  return error_mark_node;
7309 
7310 	first_param = (*params)[0];
7311 	result = build_function_call_vec (loc, vNULL, new_function, params,
7312 					  NULL);
7313 	if (result == error_mark_node)
7314 	  return result;
7315 	if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7316 	    && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
7317 	    && orig_code != BUILT_IN_ATOMIC_STORE_N
7318 	    && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
7319 	  result = sync_resolve_return (first_param, result, orig_format);
7320 
7321 	if (fetch_op)
7322 	  /* Prevent -Wunused-value warning.  */
7323 	  TREE_USED (result) = true;
7324 
7325 	/* If new_return is set, assign function to that expr and cast the
7326 	   result to void since the generic interface returned void.  */
7327 	if (new_return)
7328 	  {
7329 	    /* Cast function result from I{1,2,4,8,16} to the required type.  */
7330 	    result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7331 	    result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7332 			     result);
7333 	    TREE_SIDE_EFFECTS (result) = 1;
7334 	    protected_set_expr_location (result, loc);
7335 	    result = convert (void_type_node, result);
7336 	  }
7337 	return result;
7338       }
7339 
7340     default:
7341       return NULL_TREE;
7342     }
7343 }
7344 
7345 /* vector_types_compatible_elements_p is used in type checks of vectors
7346    values used as operands of binary operators.  Where it returns true, and
7347    the other checks of the caller succeed (being vector types in he first
7348    place, and matching number of elements), we can just treat the types
7349    as essentially the same.
7350    Contrast with vector_targets_convertible_p, which is used for vector
7351    pointer types,  and vector_types_convertible_p, which will allow
7352    language-specific matches under the control of flag_lax_vector_conversions,
7353    and might still require a conversion.  */
7354 /* True if vector types T1 and T2 can be inputs to the same binary
7355    operator without conversion.
7356    We don't check the overall vector size here because some of our callers
7357    want to give different error messages when the vectors are compatible
7358    except for the element count.  */
7359 
7360 bool
7361 vector_types_compatible_elements_p (tree t1, tree t2)
7362 {
7363   bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7364   t1 = TREE_TYPE (t1);
7365   t2 = TREE_TYPE (t2);
7366 
7367   enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7368 
7369   gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
7370 	      && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
7371 		  || c2 == FIXED_POINT_TYPE));
7372 
7373   t1 = c_common_signed_type (t1);
7374   t2 = c_common_signed_type (t2);
7375   /* Equality works here because c_common_signed_type uses
7376      TYPE_MAIN_VARIANT.  */
7377   if (t1 == t2)
7378     return true;
7379   if (opaque && c1 == c2
7380       && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
7381       && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7382     return true;
7383   return false;
7384 }
7385 
7386 /* Check for missing format attributes on function pointers.  LTYPE is
7387    the new type or left-hand side type.  RTYPE is the old type or
7388    right-hand side type.  Returns TRUE if LTYPE is missing the desired
7389    attribute.  */
7390 
7391 bool
7392 check_missing_format_attribute (tree ltype, tree rtype)
7393 {
7394   tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7395   tree ra;
7396 
7397   for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7398     if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7399       break;
7400   if (ra)
7401     {
7402       tree la;
7403       for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7404 	if (is_attribute_p ("format", TREE_PURPOSE (la)))
7405 	  break;
7406       return !la;
7407     }
7408   else
7409     return false;
7410 }
7411 
7412 /* Setup a TYPE_DECL node as a typedef representation.
7413 
7414    X is a TYPE_DECL for a typedef statement.  Create a brand new
7415    ..._TYPE node (which will be just a variant of the existing
7416    ..._TYPE node with identical properties) and then install X
7417    as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7418 
7419    The whole point here is to end up with a situation where each
7420    and every ..._TYPE node the compiler creates will be uniquely
7421    associated with AT MOST one node representing a typedef name.
7422    This way, even though the compiler substitutes corresponding
7423    ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7424    early on, later parts of the compiler can always do the reverse
7425    translation and get back the corresponding typedef name.  For
7426    example, given:
7427 
7428 	typedef struct S MY_TYPE;
7429 	MY_TYPE object;
7430 
7431    Later parts of the compiler might only know that `object' was of
7432    type `struct S' if it were not for code just below.  With this
7433    code however, later parts of the compiler see something like:
7434 
7435 	struct S' == struct S
7436 	typedef struct S' MY_TYPE;
7437 	struct S' object;
7438 
7439     And they can then deduce (from the node for type struct S') that
7440     the original object declaration was:
7441 
7442 		MY_TYPE object;
7443 
7444     Being able to do this is important for proper support of protoize,
7445     and also for generating precise symbolic debugging information
7446     which takes full account of the programmer's (typedef) vocabulary.
7447 
7448     Obviously, we don't want to generate a duplicate ..._TYPE node if
7449     the TYPE_DECL node that we are now processing really represents a
7450     standard built-in type.  */
7451 
7452 void
7453 set_underlying_type (tree x)
7454 {
7455   if (x == error_mark_node)
7456     return;
7457   if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
7458     {
7459       if (TYPE_NAME (TREE_TYPE (x)) == 0)
7460 	TYPE_NAME (TREE_TYPE (x)) = x;
7461     }
7462   else if (TREE_TYPE (x) != error_mark_node
7463 	   && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
7464     {
7465       tree tt = TREE_TYPE (x);
7466       DECL_ORIGINAL_TYPE (x) = tt;
7467       tt = build_variant_type_copy (tt);
7468       TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7469       TYPE_NAME (tt) = x;
7470 
7471       /* Mark the type as used only when its type decl is decorated
7472 	 with attribute unused.  */
7473       if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
7474 	TREE_USED (tt) = 1;
7475 
7476       TREE_TYPE (x) = tt;
7477     }
7478 }
7479 
7480 /* Record the types used by the current global variable declaration
7481    being parsed, so that we can decide later to emit their debug info.
7482    Those types are in types_used_by_cur_var_decl, and we are going to
7483    store them in the types_used_by_vars_hash hash table.
7484    DECL is the declaration of the global variable that has been parsed.  */
7485 
7486 void
7487 record_types_used_by_current_var_decl (tree decl)
7488 {
7489   gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7490 
7491   while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
7492     {
7493       tree type = types_used_by_cur_var_decl->pop ();
7494       types_used_by_var_decl_insert (type, decl);
7495     }
7496 }
7497 
7498 /* The C and C++ parsers both use vectors to hold function arguments.
7499    For efficiency, we keep a cache of unused vectors.  This is the
7500    cache.  */
7501 
7502 typedef vec<tree, va_gc> *tree_gc_vec;
7503 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
7504 
7505 /* Return a new vector from the cache.  If the cache is empty,
7506    allocate a new vector.  These vectors are GC'ed, so it is OK if the
7507    pointer is not released..  */
7508 
7509 vec<tree, va_gc> *
7510 make_tree_vector (void)
7511 {
7512   if (tree_vector_cache && !tree_vector_cache->is_empty ())
7513     return tree_vector_cache->pop ();
7514   else
7515     {
7516       /* Passing 0 to vec::alloc returns NULL, and our callers require
7517 	 that we always return a non-NULL value.  The vector code uses
7518 	 4 when growing a NULL vector, so we do too.  */
7519       vec<tree, va_gc> *v;
7520       vec_alloc (v, 4);
7521       return v;
7522     }
7523 }
7524 
7525 /* Release a vector of trees back to the cache.  */
7526 
7527 void
7528 release_tree_vector (vec<tree, va_gc> *vec)
7529 {
7530   if (vec != NULL)
7531     {
7532       vec->truncate (0);
7533       vec_safe_push (tree_vector_cache, vec);
7534     }
7535 }
7536 
7537 /* Get a new tree vector holding a single tree.  */
7538 
7539 vec<tree, va_gc> *
7540 make_tree_vector_single (tree t)
7541 {
7542   vec<tree, va_gc> *ret = make_tree_vector ();
7543   ret->quick_push (t);
7544   return ret;
7545 }
7546 
7547 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain.  */
7548 
7549 vec<tree, va_gc> *
7550 make_tree_vector_from_list (tree list)
7551 {
7552   vec<tree, va_gc> *ret = make_tree_vector ();
7553   for (; list; list = TREE_CHAIN (list))
7554     vec_safe_push (ret, TREE_VALUE (list));
7555   return ret;
7556 }
7557 
7558 /* Get a new tree vector of the values of a CONSTRUCTOR.  */
7559 
7560 vec<tree, va_gc> *
7561 make_tree_vector_from_ctor (tree ctor)
7562 {
7563   vec<tree,va_gc> *ret = make_tree_vector ();
7564   vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7565   for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7566     ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7567   return ret;
7568 }
7569 
7570 /* Get a new tree vector which is a copy of an existing one.  */
7571 
7572 vec<tree, va_gc> *
7573 make_tree_vector_copy (const vec<tree, va_gc> *orig)
7574 {
7575   vec<tree, va_gc> *ret;
7576   unsigned int ix;
7577   tree t;
7578 
7579   ret = make_tree_vector ();
7580   vec_safe_reserve (ret, vec_safe_length (orig));
7581   FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7582     ret->quick_push (t);
7583   return ret;
7584 }
7585 
7586 /* Return true if KEYWORD starts a type specifier.  */
7587 
7588 bool
7589 keyword_begins_type_specifier (enum rid keyword)
7590 {
7591   switch (keyword)
7592     {
7593     case RID_AUTO_TYPE:
7594     case RID_INT:
7595     case RID_CHAR:
7596     case RID_FLOAT:
7597     case RID_DOUBLE:
7598     case RID_VOID:
7599     case RID_UNSIGNED:
7600     case RID_LONG:
7601     case RID_SHORT:
7602     case RID_SIGNED:
7603     CASE_RID_FLOATN_NX:
7604     case RID_DFLOAT32:
7605     case RID_DFLOAT64:
7606     case RID_DFLOAT128:
7607     case RID_FRACT:
7608     case RID_ACCUM:
7609     case RID_BOOL:
7610     case RID_WCHAR:
7611     case RID_CHAR16:
7612     case RID_CHAR32:
7613     case RID_SAT:
7614     case RID_COMPLEX:
7615     case RID_TYPEOF:
7616     case RID_STRUCT:
7617     case RID_CLASS:
7618     case RID_UNION:
7619     case RID_ENUM:
7620       return true;
7621     default:
7622       if (keyword >= RID_FIRST_INT_N
7623 	  && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7624 	  && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7625 	return true;
7626       return false;
7627     }
7628 }
7629 
7630 /* Return true if KEYWORD names a type qualifier.  */
7631 
7632 bool
7633 keyword_is_type_qualifier (enum rid keyword)
7634 {
7635   switch (keyword)
7636     {
7637     case RID_CONST:
7638     case RID_VOLATILE:
7639     case RID_RESTRICT:
7640     case RID_ATOMIC:
7641       return true;
7642     default:
7643       return false;
7644     }
7645 }
7646 
7647 /* Return true if KEYWORD names a storage class specifier.
7648 
7649    RID_TYPEDEF is not included in this list despite `typedef' being
7650    listed in C99 6.7.1.1.  6.7.1.3 indicates that `typedef' is listed as
7651    such for syntactic convenience only.  */
7652 
7653 bool
7654 keyword_is_storage_class_specifier (enum rid keyword)
7655 {
7656   switch (keyword)
7657     {
7658     case RID_STATIC:
7659     case RID_EXTERN:
7660     case RID_REGISTER:
7661     case RID_AUTO:
7662     case RID_MUTABLE:
7663     case RID_THREAD:
7664       return true;
7665     default:
7666       return false;
7667     }
7668 }
7669 
7670 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec].  */
7671 
7672 static bool
7673 keyword_is_function_specifier (enum rid keyword)
7674 {
7675   switch (keyword)
7676     {
7677     case RID_INLINE:
7678     case RID_NORETURN:
7679     case RID_VIRTUAL:
7680     case RID_EXPLICIT:
7681       return true;
7682     default:
7683       return false;
7684     }
7685 }
7686 
7687 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7688    declaration-specifier (C99 6.7).  */
7689 
7690 bool
7691 keyword_is_decl_specifier (enum rid keyword)
7692 {
7693   if (keyword_is_storage_class_specifier (keyword)
7694       || keyword_is_type_qualifier (keyword)
7695       || keyword_is_function_specifier (keyword))
7696     return true;
7697 
7698   switch (keyword)
7699     {
7700     case RID_TYPEDEF:
7701     case RID_FRIEND:
7702     case RID_CONSTEXPR:
7703       return true;
7704     default:
7705       return false;
7706     }
7707 }
7708 
7709 /* Initialize language-specific-bits of tree_contains_struct.  */
7710 
7711 void
7712 c_common_init_ts (void)
7713 {
7714   MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7715   MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7716   MARK_TS_TYPED (ARRAY_NOTATION_REF);
7717 }
7718 
7719 /* Build a user-defined numeric literal out of an integer constant type VALUE
7720    with identifier SUFFIX.  */
7721 
7722 tree
7723 build_userdef_literal (tree suffix_id, tree value,
7724 		       enum overflow_type overflow, tree num_string)
7725 {
7726   tree literal = make_node (USERDEF_LITERAL);
7727   USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
7728   USERDEF_LITERAL_VALUE (literal) = value;
7729   USERDEF_LITERAL_OVERFLOW (literal) = overflow;
7730   USERDEF_LITERAL_NUM_STRING (literal) = num_string;
7731   return literal;
7732 }
7733 
7734 /* For vector[index], convert the vector to an array of the underlying type.
7735    Return true if the resulting ARRAY_REF should not be an lvalue.  */
7736 
7737 bool
7738 convert_vector_to_array_for_subscript (location_t loc,
7739 				       tree *vecp, tree index)
7740 {
7741   bool ret = false;
7742   if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7743     {
7744       tree type = TREE_TYPE (*vecp);
7745 
7746       ret = !lvalue_p (*vecp);
7747 
7748       if (TREE_CODE (index) == INTEGER_CST)
7749         if (!tree_fits_uhwi_p (index)
7750             || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7751           warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7752 
7753       /* We are building an ARRAY_REF so mark the vector as addressable
7754          to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7755 	 for function parameters.  */
7756       c_common_mark_addressable_vec (*vecp);
7757 
7758       *vecp = build1 (VIEW_CONVERT_EXPR,
7759 		      build_array_type_nelts (TREE_TYPE (type),
7760 					      TYPE_VECTOR_SUBPARTS (type)),
7761 		      *vecp);
7762     }
7763   return ret;
7764 }
7765 
7766 /* Determine which of the operands, if any, is a scalar that needs to be
7767    converted to a vector, for the range of operations.  */
7768 enum stv_conv
7769 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
7770 		  bool complain)
7771 {
7772   tree type0 = TREE_TYPE (op0);
7773   tree type1 = TREE_TYPE (op1);
7774   bool integer_only_op = false;
7775   enum stv_conv ret = stv_firstarg;
7776 
7777   gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
7778   switch (code)
7779     {
7780       /* Most GENERIC binary expressions require homogeneous arguments.
7781 	 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7782 	 argument that is a vector and a second one that is a scalar, so
7783 	 we never return stv_secondarg for them.  */
7784       case RSHIFT_EXPR:
7785       case LSHIFT_EXPR:
7786 	if (TREE_CODE (type0) == INTEGER_TYPE
7787 	    && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7788 	  {
7789 	    if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7790 	      {
7791 		if (complain)
7792 		  error_at (loc, "conversion of scalar %qT to vector %qT "
7793 			    "involves truncation", type0, type1);
7794 		return stv_error;
7795 	      }
7796 	    else
7797 	      return stv_firstarg;
7798 	  }
7799 	break;
7800 
7801       case BIT_IOR_EXPR:
7802       case BIT_XOR_EXPR:
7803       case BIT_AND_EXPR:
7804 	integer_only_op = true;
7805 	/* fall through */
7806 
7807       case VEC_COND_EXPR:
7808 
7809       case PLUS_EXPR:
7810       case MINUS_EXPR:
7811       case MULT_EXPR:
7812       case TRUNC_DIV_EXPR:
7813       case CEIL_DIV_EXPR:
7814       case FLOOR_DIV_EXPR:
7815       case ROUND_DIV_EXPR:
7816       case EXACT_DIV_EXPR:
7817       case TRUNC_MOD_EXPR:
7818       case FLOOR_MOD_EXPR:
7819       case RDIV_EXPR:
7820       case EQ_EXPR:
7821       case NE_EXPR:
7822       case LE_EXPR:
7823       case GE_EXPR:
7824       case LT_EXPR:
7825       case GT_EXPR:
7826       /* What about UNLT_EXPR?  */
7827 	if (VECTOR_TYPE_P (type0))
7828 	  {
7829 	    ret = stv_secondarg;
7830 	    std::swap (type0, type1);
7831 	    std::swap (op0, op1);
7832 	  }
7833 
7834 	if (TREE_CODE (type0) == INTEGER_TYPE
7835 	    && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7836 	  {
7837 	    if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7838 	      {
7839 		if (complain)
7840 		  error_at (loc, "conversion of scalar %qT to vector %qT "
7841 			    "involves truncation", type0, type1);
7842 		return stv_error;
7843 	      }
7844 	    return ret;
7845 	  }
7846 	else if (!integer_only_op
7847 		    /* Allow integer --> real conversion if safe.  */
7848 		 && (TREE_CODE (type0) == REAL_TYPE
7849 		     || TREE_CODE (type0) == INTEGER_TYPE)
7850 		 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
7851 	  {
7852 	    if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7853 	      {
7854 		if (complain)
7855 		  error_at (loc, "conversion of scalar %qT to vector %qT "
7856 			    "involves truncation", type0, type1);
7857 		return stv_error;
7858 	      }
7859 	    return ret;
7860 	  }
7861       default:
7862 	break;
7863     }
7864 
7865   return stv_nothing;
7866 }
7867 
7868 /* Return the alignment of std::max_align_t.
7869 
7870    [support.types.layout] The type max_align_t is a POD type whose alignment
7871    requirement is at least as great as that of every scalar type, and whose
7872    alignment requirement is supported in every context.  */
7873 
7874 unsigned
7875 max_align_t_align ()
7876 {
7877   unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
7878 				TYPE_ALIGN (long_double_type_node));
7879   if (float128_type_node != NULL_TREE)
7880     max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
7881   return max_align;
7882 }
7883 
7884 /* Return true iff ALIGN is an integral constant that is a fundamental
7885    alignment, as defined by [basic.align] in the c++-11
7886    specifications.
7887 
7888    That is:
7889 
7890        [A fundamental alignment is represented by an alignment less than or
7891         equal to the greatest alignment supported by the implementation
7892         in all contexts, which is equal to alignof(max_align_t)].  */
7893 
7894 bool
7895 cxx_fundamental_alignment_p (unsigned align)
7896 {
7897   return (align <= max_align_t_align ());
7898 }
7899 
7900 /* Return true if T is a pointer to a zero-sized aggregate.  */
7901 
7902 bool
7903 pointer_to_zero_sized_aggr_p (tree t)
7904 {
7905   if (!POINTER_TYPE_P (t))
7906     return false;
7907   t = TREE_TYPE (t);
7908   return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
7909 }
7910 
7911 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
7912    with no library fallback or for an ADDR_EXPR whose operand is such type
7913    issues an error pointing to the location LOC.
7914    Returns true when the expression has been diagnosed and false
7915    otherwise.  */
7916 
7917 bool
7918 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
7919 {
7920   if (TREE_CODE (expr) == ADDR_EXPR)
7921     expr = TREE_OPERAND (expr, 0);
7922 
7923   if (TREE_TYPE (expr)
7924       && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
7925       && TREE_CODE (expr) == FUNCTION_DECL
7926       /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
7927 	 false positives for user-declared built-ins such as abs or
7928 	 strlen, and for C++ operators new and delete.
7929 	 The c_decl_implicit() test avoids false positives for implicitly
7930 	 declared built-ins with library fallbacks (such as abs).  */
7931       && DECL_BUILT_IN (expr)
7932       && DECL_IS_BUILTIN (expr)
7933       && !c_decl_implicit (expr)
7934       && !DECL_ASSEMBLER_NAME_SET_P (expr))
7935     {
7936       if (loc == UNKNOWN_LOCATION)
7937 	loc = EXPR_LOC_OR_LOC (expr, input_location);
7938 
7939       /* Reject arguments that are built-in functions with
7940 	 no library fallback.  */
7941       error_at (loc, "built-in function %qE must be directly called", expr);
7942 
7943       return true;
7944     }
7945 
7946   return false;
7947 }
7948 
7949 /* Check if array size calculations overflow or if the array covers more
7950    than half of the address space.  Return true if the size of the array
7951    is valid, false otherwise.  TYPE is the type of the array and NAME is
7952    the name of the array, or NULL_TREE for unnamed arrays.  */
7953 
7954 bool
7955 valid_array_size_p (location_t loc, tree type, tree name)
7956 {
7957   if (type != error_mark_node
7958       && COMPLETE_TYPE_P (type)
7959       && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7960       && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
7961     {
7962       if (name)
7963 	error_at (loc, "size of array %qE is too large", name);
7964       else
7965 	error_at (loc, "size of unnamed array is too large");
7966       return false;
7967     }
7968   return true;
7969 }
7970 
7971 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
7972    timestamp to replace embedded current dates to get reproducible
7973    results.  Returns -1 if SOURCE_DATE_EPOCH is not defined.  */
7974 
7975 time_t
7976 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
7977 {
7978   char *source_date_epoch;
7979   int64_t epoch;
7980   char *endptr;
7981 
7982   source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
7983   if (!source_date_epoch)
7984     return (time_t) -1;
7985 
7986   errno = 0;
7987 #if defined(INT64_T_IS_LONG)
7988   epoch = strtol (source_date_epoch, &endptr, 10);
7989 #else
7990   epoch = strtoll (source_date_epoch, &endptr, 10);
7991 #endif
7992   if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
7993       || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
7994     {
7995       error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
7996 	        "expand to a non-negative integer less than or equal to %wd",
7997 		MAX_SOURCE_DATE_EPOCH);
7998       return (time_t) -1;
7999     }
8000 
8001   return (time_t) epoch;
8002 }
8003 
8004 /* Callback for libcpp for offering spelling suggestions for misspelled
8005    directives.  GOAL is an unrecognized string; CANDIDATES is a
8006    NULL-terminated array of candidate strings.  Return the closest
8007    match to GOAL within CANDIDATES, or NULL if none are good
8008    suggestions.  */
8009 
8010 const char *
8011 cb_get_suggestion (cpp_reader *, const char *goal,
8012 		   const char *const *candidates)
8013 {
8014   best_match<const char *, const char *> bm (goal);
8015   while (*candidates)
8016     bm.consider (*candidates++);
8017   return bm.get_best_meaningful_candidate ();
8018 }
8019 
8020 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
8021    modes X, Y.  This isn't just  >, as the FLT_EVAL_METHOD values added
8022    by C TS 18661-3 for interchange  types that are computed in their
8023    native precision are larger than the C11 values for evaluating in the
8024    precision of float/double/long double.  If either mode is
8025    FLT_EVAL_METHOD_UNPREDICTABLE, return that.  */
8026 
8027 enum flt_eval_method
8028 excess_precision_mode_join (enum flt_eval_method x,
8029 			    enum flt_eval_method y)
8030 {
8031   if (x == FLT_EVAL_METHOD_UNPREDICTABLE
8032       || y == FLT_EVAL_METHOD_UNPREDICTABLE)
8033     return FLT_EVAL_METHOD_UNPREDICTABLE;
8034 
8035   /* GCC only supports one interchange type right now, _Float16.  If
8036      we're evaluating _Float16 in 16-bit precision, then flt_eval_method
8037      will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16.  */
8038   if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8039     return y;
8040   if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8041     return x;
8042 
8043   /* Other values for flt_eval_method are directly comparable, and we want
8044      the maximum.  */
8045   return MAX (x, y);
8046 }
8047 
8048 /* Return the value that should be set for FLT_EVAL_METHOD in the
8049    context of ISO/IEC TS 18861-3.
8050 
8051    This relates to the effective excess precision seen by the user,
8052    which is the join point of the precision the target requests for
8053    -fexcess-precision={standard,fast} and the implicit excess precision
8054    the target uses.  */
8055 
8056 static enum flt_eval_method
8057 c_ts18661_flt_eval_method (void)
8058 {
8059   enum flt_eval_method implicit
8060     = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
8061 
8062   enum excess_precision_type flag_type
8063     = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
8064        ? EXCESS_PRECISION_TYPE_STANDARD
8065        : EXCESS_PRECISION_TYPE_FAST);
8066 
8067   enum flt_eval_method requested
8068     = targetm.c.excess_precision (flag_type);
8069 
8070   return excess_precision_mode_join (implicit, requested);
8071 }
8072 
8073 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8074    those that were permitted by C11.  That is to say, eliminates
8075    FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16.  */
8076 
8077 static enum flt_eval_method
8078 c_c11_flt_eval_method (void)
8079 {
8080   return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8081 				     FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
8082 }
8083 
8084 /* Return the value that should be set for FLT_EVAL_METHOD.
8085    MAYBE_C11_ONLY_P is TRUE if we should check
8086    FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8087    values we can return to those from C99/C11, and FALSE otherwise.
8088    See the comments on c_ts18661_flt_eval_method for what value we choose
8089    to set here.  */
8090 
8091 int
8092 c_flt_eval_method (bool maybe_c11_only_p)
8093 {
8094   if (maybe_c11_only_p
8095       && flag_permitted_flt_eval_methods
8096 	  == PERMITTED_FLT_EVAL_METHODS_C11)
8097     return c_c11_flt_eval_method ();
8098   else
8099     return c_ts18661_flt_eval_method ();
8100 }
8101 
8102 #include "gt-c-family-c-common.h"
8103