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