]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
re PR target/36780 (Wrong reload generated for subreg address on SH)
[thirdparty/gcc.git] / gcc / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
1574ef13 2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
6fc3c3c0 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
b30f223b 4
1322177d 5This file is part of GCC.
b30f223b 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
1322177d 10version.
b30f223b 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
b30f223b
RS
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
b30f223b
RS
20
21#include "config.h"
670ee920 22#include "system.h"
4977bab6
ZW
23#include "coretypes.h"
24#include "tm.h"
d9b2742a 25#include "intl.h"
b30f223b 26#include "tree.h"
b30f223b 27#include "flags.h"
d6f4ec51 28#include "output.h"
e2af664c 29#include "c-pragma.h"
3932261a 30#include "rtl.h"
1526a060 31#include "ggc.h"
17211ab5 32#include "varray.h"
c6991660 33#include "expr.h"
8f17b5c5 34#include "c-common.h"
22703ccc 35#include "diagnostic.h"
7bdb32b9 36#include "tm_p.h"
235cfbc4 37#include "obstack.h"
c8724862 38#include "cpplib.h"
12a68f1f 39#include "target.h"
7afff7cf 40#include "langhooks.h"
d57a4b98 41#include "tree-inline.h"
0bfa5f65 42#include "c-tree.h"
5f1989e6 43#include "toplev.h"
6de9cd9a
DN
44#include "tree-iterator.h"
45#include "hashtab.h"
c31b8e1b 46#include "tree-mudflap.h"
d974312d 47#include "opts.h"
010c4d9c 48#include "real.h"
ce91e74c 49#include "cgraph.h"
1f1d5130 50#include "target-def.h"
ab22c1fa 51#include "fixed-value.h"
cb60f38d 52
81a75f0f 53cpp_reader *parse_in; /* Declared in c-pragma.h. */
c8724862 54
eaa7c03f
JM
55/* We let tm.h override the types used here, to handle trivial differences
56 such as the choice of unsigned int or long unsigned int for size_t.
57 When machines start needing nontrivial differences in the size type,
58 it would be best to do something here to figure out automatically
59 from other information what type to use. */
60
61#ifndef SIZE_TYPE
62#define SIZE_TYPE "long unsigned int"
63#endif
64
d1c38823
ZD
65#ifndef PID_TYPE
66#define PID_TYPE "int"
67#endif
68
b6baa67d
KVH
69#ifndef CHAR16_TYPE
70#define CHAR16_TYPE "short unsigned int"
71#endif
72
73#ifndef CHAR32_TYPE
74#define CHAR32_TYPE "unsigned int"
75#endif
76
eaa7c03f
JM
77#ifndef WCHAR_TYPE
78#define WCHAR_TYPE "int"
79#endif
80
a11eba95
ZW
81/* WCHAR_TYPE gets overridden by -fshort-wchar. */
82#define MODIFIED_WCHAR_TYPE \
83 (flag_short_wchar ? "short unsigned int" : WCHAR_TYPE)
84
0884b60c
BS
85#ifndef PTRDIFF_TYPE
86#define PTRDIFF_TYPE "long int"
87#endif
88
5fd8e536
JM
89#ifndef WINT_TYPE
90#define WINT_TYPE "unsigned int"
91#endif
92
93#ifndef INTMAX_TYPE
94#define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
95 ? "int" \
96 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
97 ? "long int" \
98 : "long long int"))
99#endif
100
101#ifndef UINTMAX_TYPE
102#define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
103 ? "unsigned int" \
104 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
105 ? "long unsigned int" \
106 : "long long unsigned int"))
107#endif
108
7f4edbcb 109/* The following symbols are subsumed in the c_global_trees array, and
d125d268 110 listed here individually for documentation purposes.
7f4edbcb
BS
111
112 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
113
114 tree short_integer_type_node;
115 tree long_integer_type_node;
116 tree long_long_integer_type_node;
117
118 tree short_unsigned_type_node;
119 tree long_unsigned_type_node;
120 tree long_long_unsigned_type_node;
121
de7df9eb
JM
122 tree truthvalue_type_node;
123 tree truthvalue_false_node;
124 tree truthvalue_true_node;
7f4edbcb
BS
125
126 tree ptrdiff_type_node;
127
128 tree unsigned_char_type_node;
129 tree signed_char_type_node;
130 tree wchar_type_node;
131 tree signed_wchar_type_node;
132 tree unsigned_wchar_type_node;
133
b6baa67d
KVH
134 tree char16_type_node;
135 tree char32_type_node;
136
7f4edbcb
BS
137 tree float_type_node;
138 tree double_type_node;
139 tree long_double_type_node;
140
141 tree complex_integer_type_node;
142 tree complex_float_type_node;
143 tree complex_double_type_node;
144 tree complex_long_double_type_node;
145
9a8ce21f
JG
146 tree dfloat32_type_node;
147 tree dfloat64_type_node;
148 tree_dfloat128_type_node;
149
7f4edbcb
BS
150 tree intQI_type_node;
151 tree intHI_type_node;
152 tree intSI_type_node;
153 tree intDI_type_node;
154 tree intTI_type_node;
155
156 tree unsigned_intQI_type_node;
157 tree unsigned_intHI_type_node;
158 tree unsigned_intSI_type_node;
159 tree unsigned_intDI_type_node;
160 tree unsigned_intTI_type_node;
161
162 tree widest_integer_literal_type_node;
163 tree widest_unsigned_literal_type_node;
164
165 Nodes for types `void *' and `const void *'.
166
167 tree ptr_type_node, const_ptr_type_node;
168
169 Nodes for types `char *' and `const char *'.
170
171 tree string_type_node, const_string_type_node;
172
173 Type `char[SOMENUMBER]'.
174 Used when an array of char is needed and the size is irrelevant.
175
176 tree char_array_type_node;
177
178 Type `int[SOMENUMBER]' or something like it.
179 Used when an array of int needed and the size is irrelevant.
180
181 tree int_array_type_node;
182
183 Type `wchar_t[SOMENUMBER]' or something like it.
184 Used when a wide string literal is created.
185
186 tree wchar_array_type_node;
187
b6baa67d
KVH
188 Type `char16_t[SOMENUMBER]' or something like it.
189 Used when a UTF-16 string literal is created.
190
191 tree char16_array_type_node;
192
193 Type `char32_t[SOMENUMBER]' or something like it.
194 Used when a UTF-32 string literal is created.
195
196 tree char32_array_type_node;
197
7f4edbcb
BS
198 Type `int ()' -- used for implicit declaration of functions.
199
200 tree default_function_type;
201
7f4edbcb
BS
202 A VOID_TYPE node, packaged in a TREE_LIST.
203
204 tree void_list_node;
205
684d9f3b 206 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
0ba8a114
NS
207 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
208 VAR_DECLS, but C++ does.)
63ad61ed 209
0ba8a114 210 tree function_name_decl_node;
684d9f3b 211 tree pretty_function_name_decl_node;
0ba8a114
NS
212 tree c99_function_name_decl_node;
213
214 Stack of nested function name VAR_DECLs.
35b1a6fa 215
0ba8a114 216 tree saved_function_name_decls;
63ad61ed 217
7f4edbcb
BS
218*/
219
220tree c_global_trees[CTI_MAX];
17211ab5 221\f
4078b403
NB
222/* Switches common to the C front ends. */
223
fa10beec 224/* Nonzero if preprocessing only. */
63973df3 225
aaf93206
NB
226int flag_preprocess_only;
227
63973df3
NB
228/* Nonzero means don't output line number information. */
229
230char flag_no_line_commands;
231
232/* Nonzero causes -E output not to be done, but directives such as
233 #define that have side effects are still obeyed. */
234
235char flag_no_output;
236
237/* Nonzero means dump macros in some fashion. */
238
239char flag_dump_macros;
240
241/* Nonzero means pass #include lines through to the output. */
242
243char flag_dump_includes;
244
c0d578e6
GK
245/* Nonzero means process PCH files while preprocessing. */
246
247bool flag_pch_preprocess;
248
17211ab5
GK
249/* The file name to which we should write a precompiled header, or
250 NULL if no header will be written in this compile. */
251
252const char *pch_file;
253
3df89291
NB
254/* Nonzero if an ISO standard was selected. It rejects macros in the
255 user's namespace. */
256int flag_iso;
257
258/* Nonzero if -undef was given. It suppresses target built-in macros
259 and assertions. */
260int flag_undef;
261
6bcedb4e
MM
262/* Nonzero means don't recognize the non-ANSI builtin functions. */
263
264int flag_no_builtin;
265
266/* Nonzero means don't recognize the non-ANSI builtin functions.
267 -ansi sets this. */
268
269int flag_no_nonansi_builtin;
270
eaa7c03f
JM
271/* Nonzero means give `double' the same size as `float'. */
272
273int flag_short_double;
274
275/* Nonzero means give `wchar_t' the same size as `short'. */
276
277int flag_short_wchar;
278
00c8e9f6
MS
279/* Nonzero means allow implicit conversions between vectors with
280 differing numbers of subparts and/or differing element types. */
281int flag_lax_vector_conversions;
282
750491fc
RH
283/* Nonzero means allow Microsoft extensions without warnings or errors. */
284int flag_ms_extensions;
285
4078b403
NB
286/* Nonzero means don't recognize the keyword `asm'. */
287
288int flag_no_asm;
289
4078b403
NB
290/* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
291
292int flag_signed_bitfields = 1;
4078b403 293
35b1a6fa 294/* Warn about #pragma directives that are not recognized. */
4078b403 295
35b1a6fa 296int warn_unknown_pragmas; /* Tri state variable. */
4078b403 297
4078b403
NB
298/* Warn about format/argument anomalies in calls to formatted I/O functions
299 (*printf, *scanf, strftime, strfmon, etc.). */
300
301int warn_format;
302
b2f97e4a
MM
303/* Warn about using __null (as NULL in C++) as sentinel. For code compiled
304 with GCC this doesn't matter as __null is guaranteed to have the right
305 size. */
306
307int warn_strict_null_sentinel;
308
264fa2db
ZL
309/* Zero means that faster, ...NonNil variants of objc_msgSend...
310 calls will be used in ObjC; passing nil receivers to such calls
311 will most likely result in crashes. */
312int flag_nil_receivers = 1;
313
264fa2db
ZL
314/* Nonzero means that code generation will be altered to support
315 "zero-link" execution. This currently affects ObjC only, but may
316 affect other languages in the future. */
317int flag_zero_link = 0;
318
319/* Nonzero means emit an '__OBJC, __image_info' for the current translation
320 unit. It will inform the ObjC runtime that class definition(s) herein
321 contained are to replace one(s) previously loaded. */
322int flag_replace_objc_classes = 0;
9f63daea 323
4078b403
NB
324/* C/ObjC language option variables. */
325
326
4078b403
NB
327/* Nonzero means allow type mismatches in conditional expressions;
328 just make their values `void'. */
329
330int flag_cond_mismatch;
331
332/* Nonzero means enable C89 Amendment 1 features. */
333
334int flag_isoc94;
335
336/* Nonzero means use the ISO C99 dialect of C. */
337
338int flag_isoc99;
339
6614fd40 340/* Nonzero means that we have builtin functions, and main is an int. */
4078b403
NB
341
342int flag_hosted = 1;
343
4078b403
NB
344/* Warn if main is suspicious. */
345
346int warn_main;
347
4078b403
NB
348
349/* ObjC language option variables. */
350
351
352/* Open and close the file for outputting class declarations, if
353 requested (ObjC). */
354
355int flag_gen_declaration;
356
4078b403
NB
357/* Tells the compiler that this is a special run. Do not perform any
358 compiling, instead we are to test some platform dependent features
359 and output a C header file with appropriate definitions. */
360
361int print_struct_values;
362
fa10beec 363/* Tells the compiler what is the constant string class for ObjC. */
4078b403
NB
364
365const char *constant_string_class_name;
366
4078b403
NB
367
368/* C++ language option variables. */
369
370
371/* Nonzero means don't recognize any extension keywords. */
372
373int flag_no_gnu_keywords;
374
375/* Nonzero means do emit exported implementations of functions even if
376 they can be inlined. */
377
378int flag_implement_inlines = 1;
379
4078b403
NB
380/* Nonzero means that implicit instantiations will be emitted if needed. */
381
382int flag_implicit_templates = 1;
383
384/* Nonzero means that implicit instantiations of inline templates will be
385 emitted if needed, even if instantiations of non-inline templates
386 aren't. */
387
388int flag_implicit_inline_templates = 1;
389
390/* Nonzero means generate separate instantiation control files and
391 juggle them at link time. */
392
393int flag_use_repository;
394
395/* Nonzero if we want to issue diagnostics that the standard says are not
396 required. */
397
398int flag_optional_diags = 1;
399
400/* Nonzero means we should attempt to elide constructors when possible. */
401
402int flag_elide_constructors = 1;
403
404/* Nonzero means that member functions defined in class scope are
405 inline by default. */
406
407int flag_default_inline = 1;
408
409/* Controls whether compiler generates 'type descriptor' that give
410 run-time type information. */
411
412int flag_rtti = 1;
413
414/* Nonzero if we want to conserve space in the .o files. We do this
415 by putting uninitialized data and runtime initialized data into
416 .common instead of .data at the expense of not flagging multiple
417 definitions. */
418
419int flag_conserve_space;
420
421/* Nonzero if we want to obey access control semantics. */
422
423int flag_access_control = 1;
424
425/* Nonzero if we want to check the return value of new and avoid calling
426 constructors if it is a null pointer. */
427
428int flag_check_new;
429
c1ae8be5 430/* The C++ dialect being used. C++98 is the default. */
966541e3 431
c1ae8be5 432enum cxx_dialect cxx_dialect = cxx98;
966541e3 433
4078b403
NB
434/* Nonzero if we want the new ISO rules for pushing a new scope for `for'
435 initialization variables.
436 0: Old rules, set by -fno-for-scope.
437 2: New ISO rules, set by -ffor-scope.
438 1: Try to implement new ISO rules, but with backup compatibility
439 (and warnings). This is the default, for now. */
440
441int flag_new_for_scope = 1;
442
443/* Nonzero if we want to emit defined symbols with common-like linkage as
444 weak symbols where possible, in order to conform to C++ semantics.
445 Otherwise, emit them as local symbols. */
446
447int flag_weak = 1;
448
b20d9f0c
AO
449/* 0 means we want the preprocessor to not emit line directives for
450 the current working directory. 1 means we want it to do it. -1
451 means we should decide depending on whether debugging information
452 is being emitted or not. */
453
454int flag_working_directory = -1;
455
4078b403 456/* Nonzero to use __cxa_atexit, rather than atexit, to register
c7b5e395
GK
457 destructors for local statics and global objects. '2' means it has been
458 set nonzero as a default, not by a command-line flag. */
4078b403
NB
459
460int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT;
461
c7b5e395
GK
462/* Nonzero to use __cxa_get_exception_ptr in C++ exception-handling
463 code. '2' means it has not been set explicitly on the command line. */
464
465int flag_use_cxa_get_exception_ptr = 2;
466
4078b403
NB
467/* Nonzero means to implement standard semantics for exception
468 specifications, calling unexpected if an exception is thrown that
469 doesn't match the specification. Zero means to treat them as
470 assertions and optimize accordingly, but not check them. */
471
472int flag_enforce_eh_specs = 1;
473
40aac948
JM
474/* Nonzero means to generate thread-safe code for initializing local
475 statics. */
476
477int flag_threadsafe_statics = 1;
478
4078b403
NB
479/* Nonzero means warn about implicit declarations. */
480
481int warn_implicit = 1;
482
4078b403
NB
483/* Maximum template instantiation depth. This limit is rather
484 arbitrary, but it exists to limit the time it takes to notice
485 infinite template instantiations. */
486
487int max_tinst_depth = 500;
488
489
490
f09f1de5
MM
491/* The elements of `ridpointers' are identifier nodes for the reserved
492 type names and storage classes. It is indexed by a RID_... value. */
493tree *ridpointers;
494
35b1a6fa 495tree (*make_fname_decl) (tree, int);
2ce07e2d 496
e78a3b42
RK
497/* Nonzero means the expression being parsed will never be evaluated.
498 This is a count, since unevaluated expressions can nest. */
499int skip_evaluation;
500
ec5c56db 501/* Information about how a function name is generated. */
0ba8a114
NS
502struct fname_var_t
503{
8b60264b
KG
504 tree *const decl; /* pointer to the VAR_DECL. */
505 const unsigned rid; /* RID number for the identifier. */
506 const int pretty; /* How pretty is it? */
0ba8a114
NS
507};
508
ec5c56db 509/* The three ways of getting then name of the current function. */
0ba8a114
NS
510
511const struct fname_var_t fname_vars[] =
512{
ec5c56db 513 /* C99 compliant __func__, must be first. */
0ba8a114 514 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
ec5c56db 515 /* GCC __FUNCTION__ compliant. */
0ba8a114 516 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
ec5c56db 517 /* GCC __PRETTY_FUNCTION__ compliant. */
0ba8a114
NS
518 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
519 {NULL, 0, 0},
520};
521
a6c0a76c
SB
522static tree check_case_value (tree);
523static bool check_case_bounds (tree, tree, tree *, tree *);
4724b3de 524
35b1a6fa
AJ
525static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
526static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
527static tree handle_common_attribute (tree *, tree, tree, int, bool *);
528static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52bf96d2
JH
529static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
530static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
531static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
532static tree handle_always_inline_attribute (tree *, tree, tree, int,
533 bool *);
d752cfdb
JJ
534static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
535static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0691d1d4 536static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
d2af6a68 537static tree handle_error_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
538static tree handle_used_attribute (tree *, tree, tree, int, bool *);
539static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
ce91e74c
JH
540static tree handle_externally_visible_attribute (tree *, tree, tree, int,
541 bool *);
35b1a6fa
AJ
542static tree handle_const_attribute (tree *, tree, tree, int, bool *);
543static tree handle_transparent_union_attribute (tree *, tree, tree,
544 int, bool *);
545static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
546static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
547static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
548static tree handle_section_attribute (tree *, tree, tree, int, bool *);
549static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
550static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
551static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
a0203ca7 552static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
35b1a6fa
AJ
553static tree handle_visibility_attribute (tree *, tree, tree, int,
554 bool *);
555static tree handle_tls_model_attribute (tree *, tree, tree, int,
556 bool *);
557static tree handle_no_instrument_function_attribute (tree *, tree,
558 tree, int, bool *);
559static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
6e9a3221 560static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
561static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
562 bool *);
563static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
dcd6de6d 564static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
565static tree handle_deprecated_attribute (tree *, tree, tree, int,
566 bool *);
567static tree handle_vector_size_attribute (tree *, tree, tree, int,
568 bool *);
569static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
570static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
571static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
72954a4f
JM
572static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
573 bool *);
3d091dac 574static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5d32c25 575static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
51bc54a6 576static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 577
94a0dd7b 578static void check_function_nonnull (tree, int, tree *);
35b1a6fa
AJ
579static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
580static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
581static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
d07605f5 582static int resort_field_decl_cmp (const void *, const void *);
b34c7881 583
eea1139b
ILT
584/* Reserved words. The third field is a mask: keywords are disabled
585 if they match the mask.
586
587 Masks for languages:
588 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
589 C --std=c99: D_CXXONLY | D_OBJC
590 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
591 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
592 C++ --std=c0x: D_CONLY | D_OBJC
593 ObjC++ is like C++ except that D_OBJC is not set
594
595 If -fno-asm is used, D_ASM is added to the mask. If
596 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
597 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
598 In C with -Wcxx-compat, we warn if D_CXXWARN is set. */
599
600const struct c_common_resword c_common_reswords[] =
601{
602 { "_Bool", RID_BOOL, D_CONLY },
603 { "_Complex", RID_COMPLEX, 0 },
604 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
605 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
606 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
607 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
608 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
609 { "_Sat", RID_SAT, D_CONLY | D_EXT },
610 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
611 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
612 { "__alignof", RID_ALIGNOF, 0 },
613 { "__alignof__", RID_ALIGNOF, 0 },
614 { "__asm", RID_ASM, 0 },
615 { "__asm__", RID_ASM, 0 },
616 { "__attribute", RID_ATTRIBUTE, 0 },
617 { "__attribute__", RID_ATTRIBUTE, 0 },
618 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
619 { "__builtin_offsetof", RID_OFFSETOF, 0 },
620 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
621 { "__builtin_va_arg", RID_VA_ARG, 0 },
622 { "__complex", RID_COMPLEX, 0 },
623 { "__complex__", RID_COMPLEX, 0 },
624 { "__const", RID_CONST, 0 },
625 { "__const__", RID_CONST, 0 },
626 { "__decltype", RID_DECLTYPE, D_CXXONLY },
627 { "__extension__", RID_EXTENSION, 0 },
628 { "__func__", RID_C99_FUNCTION_NAME, 0 },
629 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
630 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
631 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
632 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
633 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
634 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
635 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
636 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
637 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
638 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
639 { "__is_class", RID_IS_CLASS, D_CXXONLY },
640 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
641 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
642 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
643 { "__is_pod", RID_IS_POD, D_CXXONLY },
644 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
645 { "__is_union", RID_IS_UNION, D_CXXONLY },
646 { "__imag", RID_IMAGPART, 0 },
647 { "__imag__", RID_IMAGPART, 0 },
648 { "__inline", RID_INLINE, 0 },
649 { "__inline__", RID_INLINE, 0 },
650 { "__label__", RID_LABEL, 0 },
651 { "__null", RID_NULL, 0 },
652 { "__real", RID_REALPART, 0 },
653 { "__real__", RID_REALPART, 0 },
654 { "__restrict", RID_RESTRICT, 0 },
655 { "__restrict__", RID_RESTRICT, 0 },
656 { "__signed", RID_SIGNED, 0 },
657 { "__signed__", RID_SIGNED, 0 },
658 { "__thread", RID_THREAD, 0 },
659 { "__typeof", RID_TYPEOF, 0 },
660 { "__typeof__", RID_TYPEOF, 0 },
661 { "__volatile", RID_VOLATILE, 0 },
662 { "__volatile__", RID_VOLATILE, 0 },
663 { "asm", RID_ASM, D_ASM },
664 { "auto", RID_AUTO, 0 },
665 { "bool", RID_BOOL, D_CXXONLY },
666 { "break", RID_BREAK, 0 },
667 { "case", RID_CASE, 0 },
668 { "catch", RID_CATCH, D_CXX_OBJC },
669 { "char", RID_CHAR, 0 },
670 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X },
671 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X },
672 { "class", RID_CLASS, D_CXX_OBJC },
673 { "const", RID_CONST, 0 },
674 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
675 { "continue", RID_CONTINUE, 0 },
676 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X },
677 { "default", RID_DEFAULT, 0 },
678 { "delete", RID_DELETE, D_CXXONLY },
679 { "do", RID_DO, 0 },
680 { "double", RID_DOUBLE, 0 },
681 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
682 { "else", RID_ELSE, 0 },
683 { "enum", RID_ENUM, 0 },
684 { "explicit", RID_EXPLICIT, D_CXXONLY },
685 { "export", RID_EXPORT, D_CXXONLY },
686 { "extern", RID_EXTERN, 0 },
687 { "false", RID_FALSE, D_CXXONLY },
688 { "float", RID_FLOAT, 0 },
689 { "for", RID_FOR, 0 },
690 { "friend", RID_FRIEND, D_CXXONLY },
691 { "goto", RID_GOTO, 0 },
692 { "if", RID_IF, 0 },
693 { "inline", RID_INLINE, D_EXT89 },
694 { "int", RID_INT, 0 },
695 { "long", RID_LONG, 0 },
696 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
697 { "namespace", RID_NAMESPACE, D_CXXONLY },
698 { "new", RID_NEW, D_CXXONLY },
699 { "operator", RID_OPERATOR, D_CXXONLY },
700 { "private", RID_PRIVATE, D_CXX_OBJC },
701 { "protected", RID_PROTECTED, D_CXX_OBJC },
702 { "public", RID_PUBLIC, D_CXX_OBJC },
703 { "register", RID_REGISTER, 0 },
704 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
705 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
706 { "return", RID_RETURN, 0 },
707 { "short", RID_SHORT, 0 },
708 { "signed", RID_SIGNED, 0 },
709 { "sizeof", RID_SIZEOF, 0 },
710 { "static", RID_STATIC, 0 },
711 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
712 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
713 { "struct", RID_STRUCT, 0 },
714 { "switch", RID_SWITCH, 0 },
715 { "template", RID_TEMPLATE, D_CXXONLY },
716 { "this", RID_THIS, D_CXXONLY },
717 { "throw", RID_THROW, D_CXX_OBJC },
718 { "true", RID_TRUE, D_CXXONLY },
719 { "try", RID_TRY, D_CXX_OBJC },
720 { "typedef", RID_TYPEDEF, 0 },
721 { "typename", RID_TYPENAME, D_CXXONLY },
722 { "typeid", RID_TYPEID, D_CXXONLY },
723 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
724 { "union", RID_UNION, 0 },
725 { "unsigned", RID_UNSIGNED, 0 },
726 { "using", RID_USING, D_CXXONLY },
727 { "virtual", RID_VIRTUAL, D_CXXONLY },
728 { "void", RID_VOID, 0 },
729 { "volatile", RID_VOLATILE, 0 },
730 { "wchar_t", RID_WCHAR, D_CXXONLY },
731 { "while", RID_WHILE, 0 },
732 /* These Objective-C keywords are recognized only immediately after
733 an '@'. */
734 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
735 { "defs", RID_AT_DEFS, D_OBJC },
736 { "encode", RID_AT_ENCODE, D_OBJC },
737 { "end", RID_AT_END, D_OBJC },
738 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
739 { "interface", RID_AT_INTERFACE, D_OBJC },
740 { "protocol", RID_AT_PROTOCOL, D_OBJC },
741 { "selector", RID_AT_SELECTOR, D_OBJC },
742 { "finally", RID_AT_FINALLY, D_OBJC },
743 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
744 /* These are recognized only in protocol-qualifier context
745 (see above) */
746 { "bycopy", RID_BYCOPY, D_OBJC },
747 { "byref", RID_BYREF, D_OBJC },
748 { "in", RID_IN, D_OBJC },
749 { "inout", RID_INOUT, D_OBJC },
750 { "oneway", RID_ONEWAY, D_OBJC },
751 { "out", RID_OUT, D_OBJC },
752};
753
754const unsigned int num_c_common_reswords =
755 sizeof c_common_reswords / sizeof (struct c_common_resword);
756
349ae713
NB
757/* Table of machine-independent attributes common to all C-like languages. */
758const struct attribute_spec c_common_attribute_table[] =
759{
760 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
761 { "packed", 0, 0, false, false, false,
35b1a6fa 762 handle_packed_attribute },
349ae713
NB
763 { "nocommon", 0, 0, true, false, false,
764 handle_nocommon_attribute },
765 { "common", 0, 0, true, false, false,
766 handle_common_attribute },
767 /* FIXME: logically, noreturn attributes should be listed as
768 "false, true, true" and apply to function types. But implementing this
769 would require all the places in the compiler that use TREE_THIS_VOLATILE
770 on a decl to identify non-returning functions to be located and fixed
771 to check the function type instead. */
772 { "noreturn", 0, 0, true, false, false,
773 handle_noreturn_attribute },
774 { "volatile", 0, 0, true, false, false,
775 handle_noreturn_attribute },
776 { "noinline", 0, 0, true, false, false,
777 handle_noinline_attribute },
778 { "always_inline", 0, 0, true, false, false,
779 handle_always_inline_attribute },
4eb7fd83
JJ
780 { "gnu_inline", 0, 0, true, false, false,
781 handle_gnu_inline_attribute },
d752cfdb
JJ
782 { "artificial", 0, 0, true, false, false,
783 handle_artificial_attribute },
0691d1d4 784 { "flatten", 0, 0, true, false, false,
c22cacf3 785 handle_flatten_attribute },
349ae713
NB
786 { "used", 0, 0, true, false, false,
787 handle_used_attribute },
788 { "unused", 0, 0, false, false, false,
789 handle_unused_attribute },
ce91e74c
JH
790 { "externally_visible", 0, 0, true, false, false,
791 handle_externally_visible_attribute },
349ae713
NB
792 /* The same comments as for noreturn attributes apply to const ones. */
793 { "const", 0, 0, true, false, false,
794 handle_const_attribute },
795 { "transparent_union", 0, 0, false, false, false,
796 handle_transparent_union_attribute },
fc8600f9 797 { "constructor", 0, 1, true, false, false,
349ae713 798 handle_constructor_attribute },
fc8600f9 799 { "destructor", 0, 1, true, false, false,
349ae713
NB
800 handle_destructor_attribute },
801 { "mode", 1, 1, false, true, false,
802 handle_mode_attribute },
803 { "section", 1, 1, true, false, false,
804 handle_section_attribute },
805 { "aligned", 0, 1, false, false, false,
806 handle_aligned_attribute },
807 { "weak", 0, 0, true, false, false,
808 handle_weak_attribute },
809 { "alias", 1, 1, true, false, false,
810 handle_alias_attribute },
a0203ca7
AO
811 { "weakref", 0, 1, true, false, false,
812 handle_weakref_attribute },
349ae713
NB
813 { "no_instrument_function", 0, 0, true, false, false,
814 handle_no_instrument_function_attribute },
815 { "malloc", 0, 0, true, false, false,
816 handle_malloc_attribute },
6e9a3221
AN
817 { "returns_twice", 0, 0, true, false, false,
818 handle_returns_twice_attribute },
349ae713
NB
819 { "no_stack_limit", 0, 0, true, false, false,
820 handle_no_limit_stack_attribute },
821 { "pure", 0, 0, true, false, false,
822 handle_pure_attribute },
dcd6de6d
ZD
823 /* For internal use (marking of builtins) only. The name contains space
824 to prevent its usage in source code. */
825 { "no vops", 0, 0, true, false, false,
826 handle_novops_attribute },
349ae713
NB
827 { "deprecated", 0, 0, false, false, false,
828 handle_deprecated_attribute },
829 { "vector_size", 1, 1, false, true, false,
830 handle_vector_size_attribute },
d7afec4b 831 { "visibility", 1, 1, false, false, false,
349ae713 832 handle_visibility_attribute },
dce81a1a
JJ
833 { "tls_model", 1, 1, true, false, false,
834 handle_tls_model_attribute },
b34c7881
JT
835 { "nonnull", 0, -1, false, true, true,
836 handle_nonnull_attribute },
39f2f3c8
RS
837 { "nothrow", 0, 0, true, false, false,
838 handle_nothrow_attribute },
d18b1ed8 839 { "may_alias", 0, 0, false, true, false, NULL },
0bfa5f65
RH
840 { "cleanup", 1, 1, true, false, false,
841 handle_cleanup_attribute },
72954a4f
JM
842 { "warn_unused_result", 0, 0, false, true, true,
843 handle_warn_unused_result_attribute },
254986c7 844 { "sentinel", 0, 1, false, true, true,
3d091dac 845 handle_sentinel_attribute },
b5d32c25
KG
846 /* For internal use (marking of builtins) only. The name contains space
847 to prevent its usage in source code. */
848 { "type generic", 0, 0, false, true, true,
849 handle_type_generic_attribute },
51bc54a6
DM
850 { "alloc_size", 1, 2, false, true, true,
851 handle_alloc_size_attribute },
52bf96d2
JH
852 { "cold", 0, 0, true, false, false,
853 handle_cold_attribute },
854 { "hot", 0, 0, true, false, false,
855 handle_hot_attribute },
d2af6a68
JJ
856 { "warning", 1, 1, true, false, false,
857 handle_error_attribute },
858 { "error", 1, 1, true, false, false,
859 handle_error_attribute },
349ae713
NB
860 { NULL, 0, 0, false, false, false, NULL }
861};
862
863/* Give the specifications for the format attributes, used by C and all
95bd1dd7 864 descendants. */
349ae713
NB
865
866const struct attribute_spec c_common_format_attribute_table[] =
867{
868 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
869 { "format", 3, 3, false, true, true,
870 handle_format_attribute },
871 { "format_arg", 1, 1, false, true, true,
872 handle_format_arg_attribute },
873 { NULL, 0, 0, false, false, false, NULL }
874};
875
ec5c56db 876/* Push current bindings for the function name VAR_DECLS. */
7da551a2
RS
877
878void
35b1a6fa 879start_fname_decls (void)
7da551a2 880{
0ba8a114
NS
881 unsigned ix;
882 tree saved = NULL_TREE;
35b1a6fa 883
0ba8a114
NS
884 for (ix = 0; fname_vars[ix].decl; ix++)
885 {
886 tree decl = *fname_vars[ix].decl;
7da551a2 887
0ba8a114
NS
888 if (decl)
889 {
7d60be94 890 saved = tree_cons (decl, build_int_cst (NULL_TREE, ix), saved);
0ba8a114
NS
891 *fname_vars[ix].decl = NULL_TREE;
892 }
893 }
894 if (saved || saved_function_name_decls)
895 /* Normally they'll have been NULL, so only push if we've got a
896 stack, or they are non-NULL. */
897 saved_function_name_decls = tree_cons (saved, NULL_TREE,
898 saved_function_name_decls);
899}
900
325c3691
RH
901/* Finish up the current bindings, adding them into the current function's
902 statement tree. This must be done _before_ finish_stmt_tree is called.
903 If there is no current function, we must be at file scope and no statements
904 are involved. Pop the previous bindings. */
0ba8a114
NS
905
906void
35b1a6fa 907finish_fname_decls (void)
0ba8a114
NS
908{
909 unsigned ix;
325c3691 910 tree stmts = NULL_TREE;
0ba8a114
NS
911 tree stack = saved_function_name_decls;
912
913 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
325c3691 914 append_to_statement_list (TREE_VALUE (stack), &stmts);
35b1a6fa 915
325c3691 916 if (stmts)
0ba8a114 917 {
325c3691 918 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
bfaba7a9 919
325c3691
RH
920 if (TREE_CODE (*bodyp) == BIND_EXPR)
921 bodyp = &BIND_EXPR_BODY (*bodyp);
6cce57b0 922
86ad3aa9 923 append_to_statement_list_force (*bodyp, &stmts);
325c3691 924 *bodyp = stmts;
0ba8a114 925 }
35b1a6fa 926
0ba8a114
NS
927 for (ix = 0; fname_vars[ix].decl; ix++)
928 *fname_vars[ix].decl = NULL_TREE;
35b1a6fa 929
0ba8a114 930 if (stack)
7da551a2 931 {
ec5c56db 932 /* We had saved values, restore them. */
0ba8a114
NS
933 tree saved;
934
935 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
936 {
937 tree decl = TREE_PURPOSE (saved);
938 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
35b1a6fa 939
0ba8a114
NS
940 *fname_vars[ix].decl = decl;
941 }
942 stack = TREE_CHAIN (stack);
7da551a2 943 }
0ba8a114
NS
944 saved_function_name_decls = stack;
945}
946
6cce57b0 947/* Return the text name of the current function, suitably prettified
0d0bc036 948 by PRETTY_P. Return string must be freed by caller. */
0ba8a114
NS
949
950const char *
35b1a6fa 951fname_as_string (int pretty_p)
0ba8a114 952{
47ab33b2 953 const char *name = "top level";
0d0bc036 954 char *namep;
46c2514e
TT
955 int vrb = 2, len;
956 cpp_string cstr = { 0, 0 }, strname;
47ab33b2 957
3f75a254 958 if (!pretty_p)
47ab33b2
MA
959 {
960 name = "";
961 vrb = 0;
962 }
963
964 if (current_function_decl)
ae2bcd98 965 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
47ab33b2 966
46c2514e 967 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
0d0bc036 968
46c2514e
TT
969 namep = XNEWVEC (char, len);
970 snprintf (namep, len, "\"%s\"", name);
971 strname.text = (unsigned char *) namep;
972 strname.len = len - 1;
0d0bc036 973
b6baa67d 974 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
46c2514e
TT
975 {
976 XDELETEVEC (namep);
977 return (const char *) cstr.text;
0d0bc036 978 }
0d0bc036
AH
979
980 return namep;
0ba8a114
NS
981}
982
3c79fa86
AP
983/* Expand DECL if it declares an entity not handled by the
984 common code. */
985
986int
987c_expand_decl (tree decl)
988{
989 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
990 {
991 /* Let the back-end know about this variable. */
992 if (!anon_aggr_type_p (TREE_TYPE (decl)))
c22cacf3 993 emit_local_var (decl);
3c79fa86 994 else
c22cacf3
MS
995 expand_anon_union_decl (decl, NULL_TREE,
996 DECL_ANON_UNION_ELEMS (decl));
3c79fa86 997 }
3c79fa86
AP
998 else
999 return 0;
1000
1001 return 1;
1002}
1003
1004
0ba8a114
NS
1005/* Return the VAR_DECL for a const char array naming the current
1006 function. If the VAR_DECL has not yet been created, create it
1007 now. RID indicates how it should be formatted and IDENTIFIER_NODE
1008 ID is its name (unfortunately C and C++ hold the RID values of
1009 keywords in different places, so we can't derive RID from ID in
f63d1bf7 1010 this language independent code. */
0ba8a114
NS
1011
1012tree
35b1a6fa 1013fname_decl (unsigned int rid, tree id)
0ba8a114
NS
1014{
1015 unsigned ix;
1016 tree decl = NULL_TREE;
1017
1018 for (ix = 0; fname_vars[ix].decl; ix++)
1019 if (fname_vars[ix].rid == rid)
1020 break;
1021
1022 decl = *fname_vars[ix].decl;
1023 if (!decl)
7da551a2 1024 {
8d3e27d1
DJ
1025 /* If a tree is built here, it would normally have the lineno of
1026 the current statement. Later this tree will be moved to the
1027 beginning of the function and this line number will be wrong.
1028 To avoid this problem set the lineno to 0 here; that prevents
4b7e68e7 1029 it from appearing in the RTL. */
325c3691 1030 tree stmts;
3c20847b 1031 location_t saved_location = input_location;
3c20847b 1032 input_location = UNKNOWN_LOCATION;
35b1a6fa 1033
325c3691 1034 stmts = push_stmt_list ();
0ba8a114 1035 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
325c3691
RH
1036 stmts = pop_stmt_list (stmts);
1037 if (!IS_EMPTY_STMT (stmts))
1038 saved_function_name_decls
1039 = tree_cons (decl, stmts, saved_function_name_decls);
0ba8a114 1040 *fname_vars[ix].decl = decl;
3c20847b 1041 input_location = saved_location;
7da551a2 1042 }
0ba8a114 1043 if (!ix && !current_function_decl)
8b17a9d6 1044 pedwarn ("%qD is not defined outside of function scope", decl);
6cce57b0 1045
0ba8a114 1046 return decl;
7da551a2
RS
1047}
1048
b84a3874 1049/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b30f223b
RS
1050
1051tree
35b1a6fa 1052fix_string_type (tree value)
b30f223b 1053{
b84a3874
RH
1054 int length = TREE_STRING_LENGTH (value);
1055 int nchars;
c162c75e
MA
1056 tree e_type, i_type, a_type;
1057
b57062ca 1058 /* Compute the number of elements, for the array type. */
b6baa67d
KVH
1059 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1060 {
1061 nchars = length;
1062 e_type = char_type_node;
1063 }
1064 else if (TREE_TYPE (value) == char16_array_type_node)
1065 {
1066 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1067 e_type = char16_type_node;
1068 }
1069 else if (TREE_TYPE (value) == char32_array_type_node)
1070 {
1071 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1072 e_type = char32_type_node;
1073 }
1074 else
1075 {
1076 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1077 e_type = wchar_type_node;
1078 }
b30f223b 1079
89a42ac8
ZW
1080 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1081 limit in C++98 Annex B is very large (65536) and is not normative,
1082 so we do not diagnose it (warn_overlength_strings is forced off
1083 in c_common_post_options). */
1084 if (warn_overlength_strings)
1085 {
1086 const int nchars_max = flag_isoc99 ? 4095 : 509;
1087 const int relevant_std = flag_isoc99 ? 99 : 90;
1088 if (nchars - 1 > nchars_max)
1089 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1090 separate the %d from the 'C'. 'ISO' should not be
1091 translated, but it may be moved after 'C%d' in languages
1092 where modifiers follow nouns. */
1093 pedwarn ("string length %qd is greater than the length %qd "
1094 "ISO C%d compilers are required to support",
1095 nchars - 1, nchars_max, relevant_std);
1096 }
1326a48b 1097
cfb10bd3
GDR
1098 /* Create the array type for the string constant. The ISO C++
1099 standard says that a string literal has type `const char[N]' or
1100 `const wchar_t[N]'. We use the same logic when invoked as a C
1101 front-end with -Wwrite-strings.
1102 ??? We should change the type of an expression depending on the
1103 state of a warning flag. We should just be warning -- see how
1104 this is handled in the C++ front-end for the deprecated implicit
1105 conversion from string literals to `char*' or `wchar_t*'.
c162c75e
MA
1106
1107 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1108 array type being the unqualified version of that type.
1109 Therefore, if we are constructing an array of const char, we must
1110 construct the matching unqualified array type first. The C front
1111 end does not require this, but it does no harm, so we do it
1112 unconditionally. */
7d60be94 1113 i_type = build_index_type (build_int_cst (NULL_TREE, nchars - 1));
c162c75e 1114 a_type = build_array_type (e_type, i_type);
cfb10bd3 1115 if (c_dialect_cxx() || warn_write_strings)
46df2823 1116 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
d9cf7c82 1117
c162c75e 1118 TREE_TYPE (value) = a_type;
ccd4c832 1119 TREE_CONSTANT (value) = 1;
3521b33c 1120 TREE_READONLY (value) = 1;
b30f223b
RS
1121 TREE_STATIC (value) = 1;
1122 return value;
1123}
1124\f
d74154d5
RS
1125/* Print a warning if a constant expression had overflow in folding.
1126 Invoke this function on every expression that the language
1127 requires to be a constant expression.
1128 Note the ANSI C standard says it is erroneous for a
1129 constant expression to overflow. */
96571883
BK
1130
1131void
35b1a6fa 1132constant_expression_warning (tree value)
393eda6a
MLI
1133{
1134 if (warn_overflow && pedantic
1135 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1136 || TREE_CODE (value) == FIXED_CST
1137 || TREE_CODE (value) == VECTOR_CST
1138 || TREE_CODE (value) == COMPLEX_CST)
1139 && TREE_OVERFLOW (value))
1140 pedwarn ("overflow in constant expression");
1141}
1142
1143/* The same as above but print an unconditional error. */
1144void
1145constant_expression_error (tree value)
96571883 1146{
c05f751c 1147 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
ab22c1fa 1148 || TREE_CODE (value) == FIXED_CST
69ef87e2 1149 || TREE_CODE (value) == VECTOR_CST
c05f751c 1150 || TREE_CODE (value) == COMPLEX_CST)
393eda6a
MLI
1151 && TREE_OVERFLOW (value))
1152 error ("overflow in constant expression");
d74154d5
RS
1153}
1154
59c0753d
MLI
1155/* Print a warning if an expression had overflow in folding and its
1156 operands hadn't.
1157
d74154d5
RS
1158 Invoke this function on every expression that
1159 (1) appears in the source code, and
59c0753d 1160 (2) is a constant expression that overflowed, and
d74154d5 1161 (3) is not already checked by convert_and_check;
59c0753d
MLI
1162 however, do not invoke this function on operands of explicit casts
1163 or when the expression is the result of an operator and any operand
1164 already overflowed. */
d74154d5
RS
1165
1166void
35b1a6fa 1167overflow_warning (tree value)
d74154d5 1168{
59c0753d
MLI
1169 if (skip_evaluation) return;
1170
1171 switch (TREE_CODE (value))
69ef87e2 1172 {
59c0753d
MLI
1173 case INTEGER_CST:
1174 warning (OPT_Woverflow, "integer overflow in expression");
1175 break;
1176
1177 case REAL_CST:
1178 warning (OPT_Woverflow, "floating point overflow in expression");
1179 break;
1180
ab22c1fa
CF
1181 case FIXED_CST:
1182 warning (OPT_Woverflow, "fixed-point overflow in expression");
1183 break;
1184
59c0753d
MLI
1185 case VECTOR_CST:
1186 warning (OPT_Woverflow, "vector overflow in expression");
1187 break;
1188
1189 case COMPLEX_CST:
1190 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1191 warning (OPT_Woverflow, "complex integer overflow in expression");
1192 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1193 warning (OPT_Woverflow, "complex floating point overflow in expression");
1194 break;
1195
1196 default:
1197 break;
69ef87e2 1198 }
d74154d5
RS
1199}
1200
63a08740
DM
1201
1202/* Warn about use of a logical || / && operator being used in a
1203 context where it is likely that the bitwise equivalent was intended
1204 by the programmer. CODE is the TREE_CODE of the operator, ARG1
1205 and ARG2 the arguments. */
1206
1207void
1208warn_logical_operator (enum tree_code code, tree arg1, tree
1209 arg2)
1210{
1211 switch (code)
1212 {
1213 case TRUTH_ANDIF_EXPR:
1214 case TRUTH_ORIF_EXPR:
1215 case TRUTH_OR_EXPR:
1216 case TRUTH_AND_EXPR:
89132ebc
DM
1217 if (!TREE_NO_WARNING (arg1)
1218 && INTEGRAL_TYPE_P (TREE_TYPE (arg1))
1219 && !CONSTANT_CLASS_P (arg1)
1220 && TREE_CODE (arg2) == INTEGER_CST
1221 && !integer_zerop (arg2))
1222 {
1223 warning (OPT_Wlogical_op,
1224 "logical %<%s%> with non-zero constant "
1225 "will always evaluate as true",
1226 ((code == TRUTH_ANDIF_EXPR)
1227 || (code == TRUTH_AND_EXPR)) ? "&&" : "||");
1228 TREE_NO_WARNING (arg1) = true;
1229 }
1230 break;
63a08740 1231 default:
89132ebc 1232 break;
63a08740
DM
1233 }
1234}
1235
1236
de9c56a4
RG
1237/* Print a warning about casts that might indicate violation
1238 of strict aliasing rules if -Wstrict-aliasing is used and
3f0a2a47
DM
1239 strict aliasing mode is in effect. OTYPE is the original
1240 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
de9c56a4 1241
79bedddc 1242bool
3f0a2a47 1243strict_aliasing_warning (tree otype, tree type, tree expr)
de9c56a4 1244{
79bedddc
SR
1245 if (!(flag_strict_aliasing && POINTER_TYPE_P (type)
1246 && POINTER_TYPE_P (otype) && !VOID_TYPE_P (TREE_TYPE (type))))
1247 return false;
1248
1249 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
de9c56a4 1250 && (DECL_P (TREE_OPERAND (expr, 0))
79bedddc 1251 || handled_component_p (TREE_OPERAND (expr, 0))))
de9c56a4
RG
1252 {
1253 /* Casting the address of an object to non void pointer. Warn
1254 if the cast breaks type based aliasing. */
79bedddc
SR
1255 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1256 {
1257 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1258 "might break strict-aliasing rules");
1259 return true;
1260 }
de9c56a4
RG
1261 else
1262 {
79bedddc
SR
1263 /* warn_strict_aliasing >= 3. This includes the default (3).
1264 Only warn if the cast is dereferenced immediately. */
4862826d 1265 alias_set_type set1 =
79bedddc 1266 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
4862826d 1267 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
de9c56a4 1268
4653cae5
RG
1269 if (set1 != set2 && set2 != 0
1270 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
79bedddc
SR
1271 {
1272 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1273 "pointer will break strict-aliasing rules");
1274 return true;
1275 }
1276 else if (warn_strict_aliasing == 2
836f7794 1277 && !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1278 {
1279 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1280 "pointer might break strict-aliasing rules");
1281 return true;
1282 }
de9c56a4
RG
1283 }
1284 }
79bedddc
SR
1285 else
1286 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1287 {
1288 /* At this level, warn for any conversions, even if an address is
1289 not taken in the same statement. This will likely produce many
1290 false positives, but could be useful to pinpoint problems that
1291 are not revealed at higher levels. */
4862826d
ILT
1292 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1293 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1294 if (!COMPLETE_TYPE_P (type)
836f7794 1295 || !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1296 {
1297 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1298 "pointer might break strict-aliasing rules");
1299 return true;
1300 }
1301 }
1302
1303 return false;
de9c56a4
RG
1304}
1305
74ac79fa
DM
1306/* Print a warning about if (); or if () .. else; constructs
1307 via the special empty statement node that we create. INNER_THEN
1308 and INNER_ELSE are the statement lists of the if and the else
1309 block. */
1310
1311void
62e00e94 1312empty_if_body_warning (tree inner_then, tree inner_else)
74ac79fa 1313{
62e00e94
DM
1314 if (TREE_CODE (inner_then) == STATEMENT_LIST
1315 && STATEMENT_LIST_TAIL (inner_then))
1316 inner_then = STATEMENT_LIST_TAIL (inner_then)->stmt;
1317
1318 if (inner_else && TREE_CODE (inner_else) == STATEMENT_LIST
1319 && STATEMENT_LIST_TAIL (inner_else))
1320 inner_else = STATEMENT_LIST_TAIL (inner_else)->stmt;
1321
1322 if (IS_EMPTY_STMT (inner_then) && !inner_else)
1323 warning (OPT_Wempty_body, "%Hsuggest braces around empty body "
1324 "in an %<if%> statement", EXPR_LOCUS (inner_then));
1325
1326 else if (inner_else && IS_EMPTY_STMT (inner_else))
1327 warning (OPT_Wempty_body, "%Hsuggest braces around empty body "
1328 "in an %<else%> statement", EXPR_LOCUS (inner_else));
74ac79fa
DM
1329}
1330
a1e45ff0
DM
1331/* Warn for unlikely, improbable, or stupid DECL declarations
1332 of `main'. */
1333
1334void
1335check_main_parameter_types (tree decl)
1336{
1337 tree args;
1338 int argct = 0;
1339
1340 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl)); args;
1341 args = TREE_CHAIN (args))
1342 {
1343 tree type = args ? TREE_VALUE (args) : 0;
1344
d8e1d619 1345 if (type == void_type_node || type == error_mark_node )
a1e45ff0
DM
1346 break;
1347
1348 ++argct;
1349 switch (argct)
1350 {
1351 case 1:
1352 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1353 pedwarn ("first argument of %q+D should be %<int%>", decl);
1354 break;
1355
1356 case 2:
1357 if (TREE_CODE (type) != POINTER_TYPE
1358 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1359 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1360 != char_type_node))
1361 pedwarn ("second argument of %q+D should be %<char **%>",
1362 decl);
1363 break;
1364
1365 case 3:
1366 if (TREE_CODE (type) != POINTER_TYPE
1367 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1368 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1369 != char_type_node))
1370 pedwarn ("third argument of %q+D should probably be "
1371 "%<char **%>", decl);
1372 break;
1373 }
1374 }
1375
1376 /* It is intentional that this message does not mention the third
1377 argument because it's only mentioned in an appendix of the
1378 standard. */
1379 if (argct > 0 && (argct < 2 || argct > 3))
1380 pedwarn ("%q+D takes only zero or two arguments", decl);
1381}
1382
f83c7f63
DJ
1383/* True if pointers to distinct types T1 and T2 can be converted to
1384 each other without an explicit cast. Only returns true for opaque
1385 vector types. */
1386bool
1387vector_targets_convertible_p (const_tree t1, const_tree t2)
1388{
1389 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
1390 && (targetm.vector_opaque_p (t1) || targetm.vector_opaque_p (t2))
1391 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1392 return true;
1393
1394 return false;
1395}
1396
00c8e9f6
MS
1397/* True if vector types T1 and T2 can be converted to each other
1398 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1399 can only be converted with -flax-vector-conversions yet that is not
1400 in effect, emit a note telling the user about that option if such
1401 a note has not previously been emitted. */
1402bool
58f9752a 1403vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
cc27e657 1404{
00c8e9f6 1405 static bool emitted_lax_note = false;
14e765da
JM
1406 bool convertible_lax;
1407
1408 if ((targetm.vector_opaque_p (t1) || targetm.vector_opaque_p (t2))
1409 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1410 return true;
1411
1412 convertible_lax =
1413 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1414 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
1415 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1416 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1417 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
00c8e9f6
MS
1418
1419 if (!convertible_lax || flag_lax_vector_conversions)
1420 return convertible_lax;
1421
1422 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
cf7bc668 1423 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
00c8e9f6
MS
1424 return true;
1425
1426 if (emit_lax_note && !emitted_lax_note)
1427 {
1428 emitted_lax_note = true;
1429 inform ("use -flax-vector-conversions to permit "
1430 "conversions between vectors with differing "
1431 "element types or numbers of subparts");
1432 }
1433
1434 return false;
cc27e657
PB
1435}
1436
422c3a54 1437/* Warns if the conversion of EXPR to TYPE may alter a value.
07231d4f 1438 This is a helper function for warnings_for_convert_and_check. */
422c3a54
MLI
1439
1440static void
1441conversion_warning (tree type, tree expr)
1442{
1443 bool give_warning = false;
1444
1445 unsigned int formal_prec = TYPE_PRECISION (type);
1446
7060db96
MLI
1447 if (!warn_conversion && !warn_sign_conversion)
1448 return;
1449
422c3a54
MLI
1450 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1451 {
1452 /* Warn for real constant that is not an exact integer converted
1453 to integer type. */
1454 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
1455 && TREE_CODE (type) == INTEGER_TYPE)
1456 {
1457 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (TREE_TYPE (expr))))
1458 give_warning = true;
1459 }
91c41804 1460 /* Warn for an integer constant that does not fit into integer type. */
422c3a54
MLI
1461 else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1462 && TREE_CODE (type) == INTEGER_TYPE
91c41804
RS
1463 && !int_fits_type_p (expr, type))
1464 {
1465 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (TREE_TYPE (expr)))
7060db96
MLI
1466 warning (OPT_Wsign_conversion,
1467 "negative integer implicitly converted to unsigned type");
1468 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (TREE_TYPE (expr)))
1469 warning (OPT_Wsign_conversion,
1470 "conversion of unsigned constant value to negative integer");
1471 else
1472 give_warning = true;
91c41804 1473 }
422c3a54
MLI
1474 else if (TREE_CODE (type) == REAL_TYPE)
1475 {
1476 /* Warn for an integer constant that does not fit into real type. */
1477 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE)
1478 {
1479 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1480 if (!exact_real_truncate (TYPE_MODE (type), &a))
1481 give_warning = true;
1482 }
1483 /* Warn for a real constant that does not fit into a smaller
1484 real type. */
1485 else if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
1486 && formal_prec < TYPE_PRECISION (TREE_TYPE (expr)))
1487 {
1488 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1489 if (!exact_real_truncate (TYPE_MODE (type), &a))
1490 give_warning = true;
1491 }
1492 }
1493
1494 if (give_warning)
1495 warning (OPT_Wconversion,
1496 "conversion to %qT alters %qT constant value",
1497 type, TREE_TYPE (expr));
1498 }
1499 else /* 'expr' is not a constant. */
1500 {
1501 /* Warn for real types converted to integer types. */
1502 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
1503 && TREE_CODE (type) == INTEGER_TYPE)
1504 give_warning = true;
1505
1506 else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1507 && TREE_CODE (type) == INTEGER_TYPE)
1508 {
cfdaefec 1509 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
c00e8b06 1510 expr = get_unwidened (expr, 0);
cfdaefec 1511
422c3a54 1512 /* Warn for integer types converted to smaller integer types. */
7060db96
MLI
1513 if (formal_prec < TYPE_PRECISION (TREE_TYPE (expr)))
1514 give_warning = true;
1515
1516 /* When they are the same width but different signedness,
1517 then the value may change. */
1518 else if ((formal_prec == TYPE_PRECISION (TREE_TYPE (expr))
c00e8b06 1519 && TYPE_UNSIGNED (TREE_TYPE (expr)) != TYPE_UNSIGNED (type))
7060db96
MLI
1520 /* Even when converted to a bigger type, if the type is
1521 unsigned but expr is signed, then negative values
1522 will be changed. */
c00e8b06
MLI
1523 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (TREE_TYPE (expr))))
1524 warning (OPT_Wsign_conversion,
1525 "conversion to %qT from %qT may change the sign of the result",
1526 type, TREE_TYPE (expr));
422c3a54
MLI
1527 }
1528
1529 /* Warn for integer types converted to real types if and only if
1530 all the range of values of the integer type cannot be
1531 represented by the real type. */
1532 else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1533 && TREE_CODE (type) == REAL_TYPE)
1534 {
1535 tree type_low_bound = TYPE_MIN_VALUE (TREE_TYPE (expr));
1536 tree type_high_bound = TYPE_MAX_VALUE (TREE_TYPE (expr));
1537 REAL_VALUE_TYPE real_low_bound = real_value_from_int_cst (0, type_low_bound);
1538 REAL_VALUE_TYPE real_high_bound = real_value_from_int_cst (0, type_high_bound);
1539
1540 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
1541 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
1542 give_warning = true;
1543 }
1544
1545 /* Warn for real types converted to smaller real types. */
1546 else if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
1547 && TREE_CODE (type) == REAL_TYPE
1548 && formal_prec < TYPE_PRECISION (TREE_TYPE (expr)))
1549 give_warning = true;
1550
1551
1552 if (give_warning)
1553 warning (OPT_Wconversion,
1554 "conversion to %qT from %qT may alter its value",
1555 type, TREE_TYPE (expr));
1556 }
1557}
1558
07231d4f
MLI
1559/* Produce warnings after a conversion. RESULT is the result of
1560 converting EXPR to TYPE. This is a helper function for
1561 convert_and_check and cp_convert_and_check. */
d74154d5 1562
07231d4f
MLI
1563void
1564warnings_for_convert_and_check (tree type, tree expr, tree result)
d74154d5 1565{
91c41804
RS
1566 if (TREE_CODE (expr) == INTEGER_CST
1567 && (TREE_CODE (type) == INTEGER_TYPE
1568 || TREE_CODE (type) == ENUMERAL_TYPE)
1569 && !int_fits_type_p (expr, type))
1570 {
422c3a54
MLI
1571 /* Do not diagnose overflow in a constant expression merely
1572 because a conversion overflowed. */
91c41804 1573 if (TREE_OVERFLOW (result))
d95787e6
RS
1574 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
1575
91c41804 1576 if (TYPE_UNSIGNED (type))
422c3a54 1577 {
91c41804
RS
1578 /* This detects cases like converting -129 or 256 to
1579 unsigned char. */
1580 if (!int_fits_type_p (expr, c_common_signed_type (type)))
1581 warning (OPT_Woverflow,
1582 "large integer implicitly truncated to unsigned type");
7060db96 1583 else
91c41804
RS
1584 conversion_warning (type, expr);
1585 }
12753674 1586 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
f73fe417
MLI
1587 warning (OPT_Woverflow,
1588 "overflow in implicit constant conversion");
1589 /* No warning for converting 0x80000000 to int. */
1590 else if (pedantic
1591 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
1592 || TYPE_PRECISION (TREE_TYPE (expr))
1593 != TYPE_PRECISION (type)))
1594 warning (OPT_Woverflow,
1595 "overflow in implicit constant conversion");
1596
7060db96 1597 else
f73fe417 1598 conversion_warning (type, expr);
d74154d5 1599 }
ab22c1fa
CF
1600 else if ((TREE_CODE (result) == INTEGER_CST
1601 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
91c41804
RS
1602 warning (OPT_Woverflow,
1603 "overflow in implicit constant conversion");
7060db96 1604 else
91c41804 1605 conversion_warning (type, expr);
07231d4f
MLI
1606}
1607
1608
1609/* Convert EXPR to TYPE, warning about conversion problems with constants.
1610 Invoke this function on every expression that is converted implicitly,
1611 i.e. because of language rules and not because of an explicit cast. */
1612
1613tree
1614convert_and_check (tree type, tree expr)
1615{
1616 tree result;
1617
1618 if (TREE_TYPE (expr) == type)
1619 return expr;
91c41804 1620
07231d4f
MLI
1621 result = convert (type, expr);
1622
7060db96 1623 if (!skip_evaluation && !TREE_OVERFLOW_P (expr) && result != error_mark_node)
07231d4f
MLI
1624 warnings_for_convert_and_check (type, expr, result);
1625
91c41804 1626 return result;
96571883
BK
1627}
1628\f
235cfbc4
BS
1629/* A node in a list that describes references to variables (EXPR), which are
1630 either read accesses if WRITER is zero, or write accesses, in which case
1631 WRITER is the parent of EXPR. */
1632struct tlist
1633{
1634 struct tlist *next;
1635 tree expr, writer;
1636};
1637
1638/* Used to implement a cache the results of a call to verify_tree. We only
1639 use this for SAVE_EXPRs. */
1640struct tlist_cache
1641{
1642 struct tlist_cache *next;
1643 struct tlist *cache_before_sp;
1644 struct tlist *cache_after_sp;
1645 tree expr;
2683ed8d
BS
1646};
1647
235cfbc4
BS
1648/* Obstack to use when allocating tlist structures, and corresponding
1649 firstobj. */
1650static struct obstack tlist_obstack;
1651static char *tlist_firstobj = 0;
1652
1653/* Keep track of the identifiers we've warned about, so we can avoid duplicate
1654 warnings. */
1655static struct tlist *warned_ids;
1656/* SAVE_EXPRs need special treatment. We process them only once and then
1657 cache the results. */
1658static struct tlist_cache *save_expr_cache;
1659
35b1a6fa
AJ
1660static void add_tlist (struct tlist **, struct tlist *, tree, int);
1661static void merge_tlist (struct tlist **, struct tlist *, int);
1662static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1663static int warning_candidate_p (tree);
1664static void warn_for_collisions (struct tlist *);
1665static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1666static struct tlist *new_tlist (struct tlist *, tree, tree);
2683ed8d 1667
235cfbc4
BS
1668/* Create a new struct tlist and fill in its fields. */
1669static struct tlist *
35b1a6fa 1670new_tlist (struct tlist *next, tree t, tree writer)
235cfbc4
BS
1671{
1672 struct tlist *l;
5d038c4c 1673 l = XOBNEW (&tlist_obstack, struct tlist);
235cfbc4
BS
1674 l->next = next;
1675 l->expr = t;
1676 l->writer = writer;
1677 return l;
1678}
1679
1680/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1681 is nonnull, we ignore any node we find which has a writer equal to it. */
1682
1683static void
35b1a6fa 1684add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
235cfbc4
BS
1685{
1686 while (add)
1687 {
1688 struct tlist *next = add->next;
3f75a254 1689 if (!copy)
235cfbc4 1690 add->next = *to;
3f75a254 1691 if (!exclude_writer || add->writer != exclude_writer)
235cfbc4
BS
1692 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1693 add = next;
1694 }
1695}
1696
1697/* Merge the nodes of ADD into TO. This merging process is done so that for
1698 each variable that already exists in TO, no new node is added; however if
1699 there is a write access recorded in ADD, and an occurrence on TO is only
1700 a read access, then the occurrence in TO will be modified to record the
1701 write. */
2683ed8d
BS
1702
1703static void
35b1a6fa 1704merge_tlist (struct tlist **to, struct tlist *add, int copy)
235cfbc4
BS
1705{
1706 struct tlist **end = to;
1707
1708 while (*end)
1709 end = &(*end)->next;
1710
1711 while (add)
1712 {
1713 int found = 0;
1714 struct tlist *tmp2;
1715 struct tlist *next = add->next;
1716
1717 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1718 if (tmp2->expr == add->expr)
1719 {
1720 found = 1;
3f75a254 1721 if (!tmp2->writer)
235cfbc4
BS
1722 tmp2->writer = add->writer;
1723 }
3f75a254 1724 if (!found)
235cfbc4
BS
1725 {
1726 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
1727 end = &(*end)->next;
1728 *end = 0;
1729 }
1730 add = next;
1731 }
1732}
1733
1734/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1735 references in list LIST conflict with it, excluding reads if ONLY writers
1736 is nonzero. */
1737
1738static void
35b1a6fa
AJ
1739warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1740 int only_writes)
235cfbc4
BS
1741{
1742 struct tlist *tmp;
1743
1744 /* Avoid duplicate warnings. */
1745 for (tmp = warned_ids; tmp; tmp = tmp->next)
1746 if (tmp->expr == written)
1747 return;
1748
1749 while (list)
1750 {
1751 if (list->expr == written
1752 && list->writer != writer
3f75a254 1753 && (!only_writes || list->writer)
e3790655 1754 && DECL_NAME (list->expr))
235cfbc4
BS
1755 {
1756 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
ffc30173
MLI
1757 warning (OPT_Wsequence_point, "operation on %qE may be undefined",
1758 list->expr);
235cfbc4
BS
1759 }
1760 list = list->next;
1761 }
1762}
1763
1764/* Given a list LIST of references to variables, find whether any of these
1765 can cause conflicts due to missing sequence points. */
1766
1767static void
35b1a6fa 1768warn_for_collisions (struct tlist *list)
235cfbc4
BS
1769{
1770 struct tlist *tmp;
35b1a6fa 1771
235cfbc4
BS
1772 for (tmp = list; tmp; tmp = tmp->next)
1773 {
1774 if (tmp->writer)
1775 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1776 }
1777}
1778
684d9f3b 1779/* Return nonzero if X is a tree that can be verified by the sequence point
235cfbc4
BS
1780 warnings. */
1781static int
35b1a6fa 1782warning_candidate_p (tree x)
2683ed8d 1783{
235cfbc4
BS
1784 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
1785}
2683ed8d 1786
235cfbc4
BS
1787/* Walk the tree X, and record accesses to variables. If X is written by the
1788 parent tree, WRITER is the parent.
1789 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1790 expression or its only operand forces a sequence point, then everything up
1791 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1792 in PNO_SP.
1793 Once we return, we will have emitted warnings if any subexpression before
1794 such a sequence point could be undefined. On a higher level, however, the
1795 sequence point may not be relevant, and we'll merge the two lists.
1796
1797 Example: (b++, a) + b;
1798 The call that processes the COMPOUND_EXPR will store the increment of B
1799 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1800 processes the PLUS_EXPR will need to merge the two lists so that
1801 eventually, all accesses end up on the same list (and we'll warn about the
1802 unordered subexpressions b++ and b.
1803
1804 A note on merging. If we modify the former example so that our expression
1805 becomes
1806 (b++, b) + a
1807 care must be taken not simply to add all three expressions into the final
1808 PNO_SP list. The function merge_tlist takes care of that by merging the
1809 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1810 way, so that no more than one access to B is recorded. */
2683ed8d 1811
235cfbc4 1812static void
35b1a6fa
AJ
1813verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1814 tree writer)
235cfbc4
BS
1815{
1816 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1817 enum tree_code code;
6615c446 1818 enum tree_code_class cl;
2683ed8d 1819
f9e1917e
JM
1820 /* X may be NULL if it is the operand of an empty statement expression
1821 ({ }). */
1822 if (x == NULL)
1823 return;
1824
235cfbc4
BS
1825 restart:
1826 code = TREE_CODE (x);
e3a64162 1827 cl = TREE_CODE_CLASS (code);
2683ed8d 1828
235cfbc4 1829 if (warning_candidate_p (x))
2683ed8d 1830 {
235cfbc4
BS
1831 *pno_sp = new_tlist (*pno_sp, x, writer);
1832 return;
1833 }
1834
1835 switch (code)
1836 {
52a84e42
BS
1837 case CONSTRUCTOR:
1838 return;
1839
235cfbc4
BS
1840 case COMPOUND_EXPR:
1841 case TRUTH_ANDIF_EXPR:
1842 case TRUTH_ORIF_EXPR:
1843 tmp_before = tmp_nosp = tmp_list3 = 0;
1844 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1845 warn_for_collisions (tmp_nosp);
1846 merge_tlist (pbefore_sp, tmp_before, 0);
1847 merge_tlist (pbefore_sp, tmp_nosp, 0);
1848 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
1849 merge_tlist (pbefore_sp, tmp_list3, 0);
1850 return;
1851
1852 case COND_EXPR:
1853 tmp_before = tmp_list2 = 0;
1854 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1855 warn_for_collisions (tmp_list2);
1856 merge_tlist (pbefore_sp, tmp_before, 0);
1857 merge_tlist (pbefore_sp, tmp_list2, 1);
1858
1859 tmp_list3 = tmp_nosp = 0;
1860 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1861 warn_for_collisions (tmp_nosp);
1862 merge_tlist (pbefore_sp, tmp_list3, 0);
1863
1864 tmp_list3 = tmp_list2 = 0;
1865 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1866 warn_for_collisions (tmp_list2);
1867 merge_tlist (pbefore_sp, tmp_list3, 0);
1868 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1869 two first, to avoid warning for (a ? b++ : b++). */
1870 merge_tlist (&tmp_nosp, tmp_list2, 0);
1871 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1872 return;
1873
2683ed8d
BS
1874 case PREDECREMENT_EXPR:
1875 case PREINCREMENT_EXPR:
1876 case POSTDECREMENT_EXPR:
1877 case POSTINCREMENT_EXPR:
235cfbc4
BS
1878 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1879 return;
1880
1881 case MODIFY_EXPR:
1882 tmp_before = tmp_nosp = tmp_list3 = 0;
1883 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1884 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1885 /* Expressions inside the LHS are not ordered wrt. the sequence points
1886 in the RHS. Example:
1887 *a = (a++, 2)
1888 Despite the fact that the modification of "a" is in the before_sp
1889 list (tmp_before), it conflicts with the use of "a" in the LHS.
1890 We can handle this by adding the contents of tmp_list3
1891 to those of tmp_before, and redoing the collision warnings for that
1892 list. */
1893 add_tlist (&tmp_before, tmp_list3, x, 1);
1894 warn_for_collisions (tmp_before);
1895 /* Exclude the LHS itself here; we first have to merge it into the
1896 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1897 didn't exclude the LHS, we'd get it twice, once as a read and once
1898 as a write. */
1899 add_tlist (pno_sp, tmp_list3, x, 0);
1900 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1901
1902 merge_tlist (pbefore_sp, tmp_before, 0);
1903 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1904 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1905 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1906 return;
2683ed8d
BS
1907
1908 case CALL_EXPR:
235cfbc4
BS
1909 /* We need to warn about conflicts among arguments and conflicts between
1910 args and the function address. Side effects of the function address,
1911 however, are not ordered by the sequence point of the call. */
5039610b
SL
1912 {
1913 call_expr_arg_iterator iter;
1914 tree arg;
1915 tmp_before = tmp_nosp = 0;
1916 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1917 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1918 {
1919 tmp_list2 = tmp_list3 = 0;
1920 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1921 merge_tlist (&tmp_list3, tmp_list2, 0);
1922 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1923 }
1924 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1925 warn_for_collisions (tmp_before);
1926 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1927 return;
1928 }
2683ed8d
BS
1929
1930 case TREE_LIST:
1931 /* Scan all the list, e.g. indices of multi dimensional array. */
1932 while (x)
1933 {
235cfbc4
BS
1934 tmp_before = tmp_nosp = 0;
1935 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1936 merge_tlist (&tmp_nosp, tmp_before, 0);
1937 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
1938 x = TREE_CHAIN (x);
1939 }
235cfbc4 1940 return;
2683ed8d 1941
235cfbc4
BS
1942 case SAVE_EXPR:
1943 {
1944 struct tlist_cache *t;
1945 for (t = save_expr_cache; t; t = t->next)
1946 if (t->expr == x)
1947 break;
2683ed8d 1948
3f75a254 1949 if (!t)
2683ed8d 1950 {
5d038c4c 1951 t = XOBNEW (&tlist_obstack, struct tlist_cache);
235cfbc4
BS
1952 t->next = save_expr_cache;
1953 t->expr = x;
1954 save_expr_cache = t;
1955
1956 tmp_before = tmp_nosp = 0;
1957 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1958 warn_for_collisions (tmp_nosp);
1959
1960 tmp_list3 = 0;
1961 while (tmp_nosp)
1962 {
1963 struct tlist *t = tmp_nosp;
1964 tmp_nosp = t->next;
1965 merge_tlist (&tmp_list3, t, 0);
1966 }
1967 t->cache_before_sp = tmp_before;
1968 t->cache_after_sp = tmp_list3;
2683ed8d 1969 }
235cfbc4
BS
1970 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1971 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1972 return;
1973 }
2683ed8d 1974
6615c446
JO
1975 default:
1976 /* For other expressions, simply recurse on their operands.
c22cacf3 1977 Manual tail recursion for unary expressions.
6615c446
JO
1978 Other non-expressions need not be processed. */
1979 if (cl == tcc_unary)
1980 {
6615c446
JO
1981 x = TREE_OPERAND (x, 0);
1982 writer = 0;
1983 goto restart;
1984 }
1985 else if (IS_EXPR_CODE_CLASS (cl))
1986 {
1987 int lp;
5039610b 1988 int max = TREE_OPERAND_LENGTH (x);
6615c446
JO
1989 for (lp = 0; lp < max; lp++)
1990 {
1991 tmp_before = tmp_nosp = 0;
1992 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
1993 merge_tlist (&tmp_nosp, tmp_before, 0);
1994 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1995 }
1996 }
1997 return;
2683ed8d 1998 }
2683ed8d
BS
1999}
2000
8d9afc4e 2001/* Try to warn for undefined behavior in EXPR due to missing sequence
2683ed8d
BS
2002 points. */
2003
3a5b9284 2004void
35b1a6fa 2005verify_sequence_points (tree expr)
2683ed8d 2006{
235cfbc4 2007 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 2008
235cfbc4
BS
2009 warned_ids = 0;
2010 save_expr_cache = 0;
2011 if (tlist_firstobj == 0)
2683ed8d 2012 {
235cfbc4 2013 gcc_obstack_init (&tlist_obstack);
28dab132 2014 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
2015 }
2016
235cfbc4
BS
2017 verify_tree (expr, &before_sp, &after_sp, 0);
2018 warn_for_collisions (after_sp);
2019 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d 2020}
b30f223b
RS
2021\f
2022/* Validate the expression after `case' and apply default promotions. */
2023
a6c0a76c 2024static tree
35b1a6fa 2025check_case_value (tree value)
b30f223b
RS
2026{
2027 if (value == NULL_TREE)
2028 return value;
2029
ed248cf7
JM
2030 /* ??? Can we ever get nops here for a valid case value? We
2031 shouldn't for C. */
8493738b 2032 STRIP_TYPE_NOPS (value);
56cb9733
MM
2033 /* In C++, the following is allowed:
2034
2035 const int i = 3;
2036 switch (...) { case i: ... }
2037
2038 So, we try to reduce the VALUE to a constant that way. */
37fa72e9 2039 if (c_dialect_cxx ())
56cb9733
MM
2040 {
2041 value = decl_constant_value (value);
2042 STRIP_TYPE_NOPS (value);
2043 value = fold (value);
2044 }
b30f223b 2045
522ddfa2
JM
2046 if (TREE_CODE (value) == INTEGER_CST)
2047 /* Promote char or short to int. */
2048 value = perform_integral_promotions (value);
2049 else if (value != error_mark_node)
b30f223b
RS
2050 {
2051 error ("case label does not reduce to an integer constant");
2052 value = error_mark_node;
2053 }
b30f223b 2054
bc690db1
RS
2055 constant_expression_warning (value);
2056
b30f223b
RS
2057 return value;
2058}
2059\f
a6c0a76c 2060/* See if the case values LOW and HIGH are in the range of the original
89dbed81 2061 type (i.e. before the default conversion to int) of the switch testing
a6c0a76c
SB
2062 expression.
2063 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2a7e31df 2064 the type before promoting it. CASE_LOW_P is a pointer to the lower
a6c0a76c
SB
2065 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2066 if the case is not a case range.
2067 The caller has to make sure that we are not called with NULL for
89dbed81 2068 CASE_LOW_P (i.e. the default case).
0fa2e4df 2069 Returns true if the case label is in range of ORIG_TYPE (saturated or
a6c0a76c
SB
2070 untouched) or false if the label is out of range. */
2071
2072static bool
2073check_case_bounds (tree type, tree orig_type,
2074 tree *case_low_p, tree *case_high_p)
2075{
2076 tree min_value, max_value;
2077 tree case_low = *case_low_p;
2078 tree case_high = case_high_p ? *case_high_p : case_low;
2079
2080 /* If there was a problem with the original type, do nothing. */
2081 if (orig_type == error_mark_node)
2082 return true;
2083
2084 min_value = TYPE_MIN_VALUE (orig_type);
2085 max_value = TYPE_MAX_VALUE (orig_type);
2086
2087 /* Case label is less than minimum for type. */
2088 if (tree_int_cst_compare (case_low, min_value) < 0
2089 && tree_int_cst_compare (case_high, min_value) < 0)
2090 {
d4ee4d25 2091 warning (0, "case label value is less than minimum value for type");
a6c0a76c
SB
2092 return false;
2093 }
9f63daea 2094
a6c0a76c
SB
2095 /* Case value is greater than maximum for type. */
2096 if (tree_int_cst_compare (case_low, max_value) > 0
2097 && tree_int_cst_compare (case_high, max_value) > 0)
2098 {
d4ee4d25 2099 warning (0, "case label value exceeds maximum value for type");
a6c0a76c
SB
2100 return false;
2101 }
2102
2103 /* Saturate lower case label value to minimum. */
2104 if (tree_int_cst_compare (case_high, min_value) >= 0
2105 && tree_int_cst_compare (case_low, min_value) < 0)
2106 {
d4ee4d25 2107 warning (0, "lower value in case label range"
a6c0a76c
SB
2108 " less than minimum value for type");
2109 case_low = min_value;
2110 }
9f63daea 2111
a6c0a76c
SB
2112 /* Saturate upper case label value to maximum. */
2113 if (tree_int_cst_compare (case_low, max_value) <= 0
2114 && tree_int_cst_compare (case_high, max_value) > 0)
2115 {
d4ee4d25 2116 warning (0, "upper value in case label range"
a6c0a76c
SB
2117 " exceeds maximum value for type");
2118 case_high = max_value;
2119 }
2120
2121 if (*case_low_p != case_low)
2122 *case_low_p = convert (type, case_low);
2123 if (case_high_p && *case_high_p != case_high)
2124 *case_high_p = convert (type, case_high);
2125
2126 return true;
2127}
2128\f
b30f223b
RS
2129/* Return an integer type with BITS bits of precision,
2130 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2131
2132tree
35b1a6fa 2133c_common_type_for_size (unsigned int bits, int unsignedp)
b30f223b 2134{
a311b52c
JM
2135 if (bits == TYPE_PRECISION (integer_type_node))
2136 return unsignedp ? unsigned_type_node : integer_type_node;
2137
3fc7e390 2138 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
2139 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2140
3fc7e390 2141 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
2142 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2143
3fc7e390 2144 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
2145 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2146
3fc7e390 2147 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
2148 return (unsignedp ? long_long_unsigned_type_node
2149 : long_long_integer_type_node);
2150
835f9b4d
GRK
2151 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2152 return (unsignedp ? widest_unsigned_literal_type_node
2153 : widest_integer_literal_type_node);
2154
3fc7e390
RS
2155 if (bits <= TYPE_PRECISION (intQI_type_node))
2156 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2157
2158 if (bits <= TYPE_PRECISION (intHI_type_node))
2159 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2160
2161 if (bits <= TYPE_PRECISION (intSI_type_node))
2162 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2163
2164 if (bits <= TYPE_PRECISION (intDI_type_node))
2165 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2166
b30f223b
RS
2167 return 0;
2168}
2169
ab22c1fa
CF
2170/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2171 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2172 and saturating if SATP is nonzero, otherwise not saturating. */
2173
2174tree
2175c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2176 int unsignedp, int satp)
2177{
2178 enum machine_mode mode;
2179 if (ibit == 0)
2180 mode = unsignedp ? UQQmode : QQmode;
2181 else
2182 mode = unsignedp ? UHAmode : HAmode;
2183
2184 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2185 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2186 break;
2187
2188 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2189 {
2190 sorry ("GCC cannot support operators with integer types and "
2191 "fixed-point types that have too many integral and "
2192 "fractional bits together");
2193 return 0;
2194 }
2195
2196 return c_common_type_for_mode (mode, satp);
2197}
2198
d1d3865f
ZW
2199/* Used for communication between c_common_type_for_mode and
2200 c_register_builtin_type. */
2201static GTY(()) tree registered_builtin_types;
2202
b30f223b
RS
2203/* Return a data type that has machine mode MODE.
2204 If the mode is an integer,
ab22c1fa
CF
2205 then UNSIGNEDP selects between signed and unsigned types.
2206 If the mode is a fixed-point mode,
2207 then UNSIGNEDP selects between saturating and nonsaturating types. */
b30f223b
RS
2208
2209tree
35b1a6fa 2210c_common_type_for_mode (enum machine_mode mode, int unsignedp)
b30f223b 2211{
d1d3865f
ZW
2212 tree t;
2213
a311b52c
JM
2214 if (mode == TYPE_MODE (integer_type_node))
2215 return unsignedp ? unsigned_type_node : integer_type_node;
2216
b30f223b
RS
2217 if (mode == TYPE_MODE (signed_char_type_node))
2218 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2219
2220 if (mode == TYPE_MODE (short_integer_type_node))
2221 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2222
b30f223b
RS
2223 if (mode == TYPE_MODE (long_integer_type_node))
2224 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2225
2226 if (mode == TYPE_MODE (long_long_integer_type_node))
2227 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2228
835f9b4d 2229 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 2230 return unsignedp ? widest_unsigned_literal_type_node
6de9cd9a 2231 : widest_integer_literal_type_node;
835f9b4d 2232
0afeef64 2233 if (mode == QImode)
3fc7e390
RS
2234 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2235
0afeef64 2236 if (mode == HImode)
3fc7e390
RS
2237 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2238
0afeef64 2239 if (mode == SImode)
3fc7e390
RS
2240 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2241
0afeef64 2242 if (mode == DImode)
3fc7e390
RS
2243 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2244
21a9616b 2245#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
2246 if (mode == TYPE_MODE (intTI_type_node))
2247 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 2248#endif
a6d7e156 2249
b30f223b
RS
2250 if (mode == TYPE_MODE (float_type_node))
2251 return float_type_node;
2252
2253 if (mode == TYPE_MODE (double_type_node))
2254 return double_type_node;
2255
2256 if (mode == TYPE_MODE (long_double_type_node))
2257 return long_double_type_node;
2258
ff42324e
NS
2259 if (mode == TYPE_MODE (void_type_node))
2260 return void_type_node;
9f63daea 2261
b30f223b 2262 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
19b3ffbc
DD
2263 return (unsignedp
2264 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2265 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b
RS
2266
2267 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
19b3ffbc
DD
2268 return (unsignedp
2269 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2270 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b 2271
7e7e470f
RH
2272 if (COMPLEX_MODE_P (mode))
2273 {
2274 enum machine_mode inner_mode;
2275 tree inner_type;
2276
2277 if (mode == TYPE_MODE (complex_float_type_node))
2278 return complex_float_type_node;
2279 if (mode == TYPE_MODE (complex_double_type_node))
2280 return complex_double_type_node;
2281 if (mode == TYPE_MODE (complex_long_double_type_node))
2282 return complex_long_double_type_node;
2283
2284 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2285 return complex_integer_type_node;
2286
2287 inner_mode = GET_MODE_INNER (mode);
2288 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2289 if (inner_type != NULL_TREE)
2290 return build_complex_type (inner_type);
2291 }
2292 else if (VECTOR_MODE_P (mode))
4a5eab38
PB
2293 {
2294 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2295 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2296 if (inner_type != NULL_TREE)
2297 return build_vector_type_for_mode (inner_type, mode);
0afeef64 2298 }
4061f623 2299
9a8ce21f
JG
2300 if (mode == TYPE_MODE (dfloat32_type_node))
2301 return dfloat32_type_node;
2302 if (mode == TYPE_MODE (dfloat64_type_node))
2303 return dfloat64_type_node;
2304 if (mode == TYPE_MODE (dfloat128_type_node))
2305 return dfloat128_type_node;
2306
ab22c1fa
CF
2307 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2308 {
2309 if (mode == TYPE_MODE (short_fract_type_node))
2310 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2311 if (mode == TYPE_MODE (fract_type_node))
2312 return unsignedp ? sat_fract_type_node : fract_type_node;
2313 if (mode == TYPE_MODE (long_fract_type_node))
2314 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2315 if (mode == TYPE_MODE (long_long_fract_type_node))
2316 return unsignedp ? sat_long_long_fract_type_node
2317 : long_long_fract_type_node;
2318
2319 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2320 return unsignedp ? sat_unsigned_short_fract_type_node
2321 : unsigned_short_fract_type_node;
2322 if (mode == TYPE_MODE (unsigned_fract_type_node))
2323 return unsignedp ? sat_unsigned_fract_type_node
2324 : unsigned_fract_type_node;
2325 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2326 return unsignedp ? sat_unsigned_long_fract_type_node
2327 : unsigned_long_fract_type_node;
2328 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2329 return unsignedp ? sat_unsigned_long_long_fract_type_node
2330 : unsigned_long_long_fract_type_node;
2331
2332 if (mode == TYPE_MODE (short_accum_type_node))
2333 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2334 if (mode == TYPE_MODE (accum_type_node))
2335 return unsignedp ? sat_accum_type_node : accum_type_node;
2336 if (mode == TYPE_MODE (long_accum_type_node))
2337 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2338 if (mode == TYPE_MODE (long_long_accum_type_node))
2339 return unsignedp ? sat_long_long_accum_type_node
2340 : long_long_accum_type_node;
2341
2342 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2343 return unsignedp ? sat_unsigned_short_accum_type_node
2344 : unsigned_short_accum_type_node;
2345 if (mode == TYPE_MODE (unsigned_accum_type_node))
2346 return unsignedp ? sat_unsigned_accum_type_node
2347 : unsigned_accum_type_node;
2348 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2349 return unsignedp ? sat_unsigned_long_accum_type_node
2350 : unsigned_long_accum_type_node;
2351 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2352 return unsignedp ? sat_unsigned_long_long_accum_type_node
2353 : unsigned_long_long_accum_type_node;
2354
2355 if (mode == QQmode)
2356 return unsignedp ? sat_qq_type_node : qq_type_node;
2357 if (mode == HQmode)
2358 return unsignedp ? sat_hq_type_node : hq_type_node;
2359 if (mode == SQmode)
2360 return unsignedp ? sat_sq_type_node : sq_type_node;
2361 if (mode == DQmode)
2362 return unsignedp ? sat_dq_type_node : dq_type_node;
2363 if (mode == TQmode)
2364 return unsignedp ? sat_tq_type_node : tq_type_node;
2365
2366 if (mode == UQQmode)
2367 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2368 if (mode == UHQmode)
2369 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2370 if (mode == USQmode)
2371 return unsignedp ? sat_usq_type_node : usq_type_node;
2372 if (mode == UDQmode)
2373 return unsignedp ? sat_udq_type_node : udq_type_node;
2374 if (mode == UTQmode)
2375 return unsignedp ? sat_utq_type_node : utq_type_node;
2376
2377 if (mode == HAmode)
2378 return unsignedp ? sat_ha_type_node : ha_type_node;
2379 if (mode == SAmode)
2380 return unsignedp ? sat_sa_type_node : sa_type_node;
2381 if (mode == DAmode)
2382 return unsignedp ? sat_da_type_node : da_type_node;
2383 if (mode == TAmode)
2384 return unsignedp ? sat_ta_type_node : ta_type_node;
2385
2386 if (mode == UHAmode)
2387 return unsignedp ? sat_uha_type_node : uha_type_node;
2388 if (mode == USAmode)
2389 return unsignedp ? sat_usa_type_node : usa_type_node;
2390 if (mode == UDAmode)
2391 return unsignedp ? sat_uda_type_node : uda_type_node;
2392 if (mode == UTAmode)
2393 return unsignedp ? sat_uta_type_node : uta_type_node;
2394 }
2395
d1d3865f
ZW
2396 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2397 if (TYPE_MODE (TREE_VALUE (t)) == mode)
2398 return TREE_VALUE (t);
2399
b30f223b
RS
2400 return 0;
2401}
693a6128 2402
12753674
RE
2403tree
2404c_common_unsigned_type (tree type)
2405{
2406 return c_common_signed_or_unsigned_type (1, type);
2407}
2408
693a6128
GRK
2409/* Return a signed type the same as TYPE in other respects. */
2410
2411tree
35b1a6fa 2412c_common_signed_type (tree type)
693a6128 2413{
ceef8ce4 2414 return c_common_signed_or_unsigned_type (0, type);
693a6128
GRK
2415}
2416
2417/* Return a type the same as TYPE except unsigned or
2418 signed according to UNSIGNEDP. */
2419
2420tree
35b1a6fa 2421c_common_signed_or_unsigned_type (int unsignedp, tree type)
693a6128 2422{
c74a03d2 2423 tree type1;
693a6128 2424
c74a03d2
RAE
2425 /* This block of code emulates the behavior of the old
2426 c_common_unsigned_type. In particular, it returns
2427 long_unsigned_type_node if passed a long, even when a int would
2428 have the same size. This is necessary for warnings to work
2429 correctly in archs where sizeof(int) == sizeof(long) */
2430
2431 type1 = TYPE_MAIN_VARIANT (type);
2432 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2433 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2434 if (type1 == integer_type_node || type1 == unsigned_type_node)
2435 return unsignedp ? unsigned_type_node : integer_type_node;
2436 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2437 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2438 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2439 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2440 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2441 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2442 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
2443 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
2444#if HOST_BITS_PER_WIDE_INT >= 64
2445 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2446 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2447#endif
2448 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2449 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2450 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2451 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2452 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2453 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2454 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2455 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2456
70d3fcab
AH
2457#define C_COMMON_FIXED_TYPES(NAME) \
2458 if (type1 == short_ ## NAME ## _type_node \
2459 || type1 == unsigned_short_ ## NAME ## _type_node) \
2460 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2461 : short_ ## NAME ## _type_node; \
2462 if (type1 == NAME ## _type_node \
2463 || type1 == unsigned_ ## NAME ## _type_node) \
2464 return unsignedp ? unsigned_ ## NAME ## _type_node \
2465 : NAME ## _type_node; \
2466 if (type1 == long_ ## NAME ## _type_node \
2467 || type1 == unsigned_long_ ## NAME ## _type_node) \
2468 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2469 : long_ ## NAME ## _type_node; \
2470 if (type1 == long_long_ ## NAME ## _type_node \
2471 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2472 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2473 : long_long_ ## NAME ## _type_node;
2474
2475#define C_COMMON_FIXED_MODE_TYPES(NAME) \
2476 if (type1 == NAME ## _type_node \
2477 || type1 == u ## NAME ## _type_node) \
2478 return unsignedp ? u ## NAME ## _type_node \
2479 : NAME ## _type_node;
2480
2481#define C_COMMON_FIXED_TYPES_SAT(NAME) \
2482 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2483 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2484 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2485 : sat_ ## short_ ## NAME ## _type_node; \
2486 if (type1 == sat_ ## NAME ## _type_node \
2487 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2488 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2489 : sat_ ## NAME ## _type_node; \
2490 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2491 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2492 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2493 : sat_ ## long_ ## NAME ## _type_node; \
2494 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2495 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2496 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2497 : sat_ ## long_long_ ## NAME ## _type_node;
2498
2499#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2500 if (type1 == sat_ ## NAME ## _type_node \
2501 || type1 == sat_ ## u ## NAME ## _type_node) \
2502 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2503 : sat_ ## NAME ## _type_node;
2504
2505 C_COMMON_FIXED_TYPES (fract);
2506 C_COMMON_FIXED_TYPES_SAT (fract);
2507 C_COMMON_FIXED_TYPES (accum);
2508 C_COMMON_FIXED_TYPES_SAT (accum);
2509
2510 C_COMMON_FIXED_MODE_TYPES (qq);
2511 C_COMMON_FIXED_MODE_TYPES (hq);
2512 C_COMMON_FIXED_MODE_TYPES (sq);
2513 C_COMMON_FIXED_MODE_TYPES (dq);
2514 C_COMMON_FIXED_MODE_TYPES (tq);
2515 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2516 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2517 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2518 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2519 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2520 C_COMMON_FIXED_MODE_TYPES (ha);
2521 C_COMMON_FIXED_MODE_TYPES (sa);
2522 C_COMMON_FIXED_MODE_TYPES (da);
2523 C_COMMON_FIXED_MODE_TYPES (ta);
2524 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2525 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2526 C_COMMON_FIXED_MODE_TYPES_SAT (da);
2527 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
ab22c1fa 2528
bc15d0ef
JM
2529 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2530 the precision; they have precision set to match their range, but
2531 may use a wider mode to match an ABI. If we change modes, we may
2532 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2533 the precision as well, so as to yield correct results for
2534 bit-field types. C++ does not have these separate bit-field
2535 types, and producing a signed or unsigned variant of an
2536 ENUMERAL_TYPE may cause other problems as well. */
2537
1e204133
RAE
2538 if (!INTEGRAL_TYPE_P (type)
2539 || TYPE_UNSIGNED (type) == unsignedp)
2540 return type;
2541
bc15d0ef
JM
2542#define TYPE_OK(node) \
2543 (TYPE_MODE (type) == TYPE_MODE (node) \
2544 && (c_dialect_cxx () || TYPE_PRECISION (type) == TYPE_PRECISION (node)))
2545 if (TYPE_OK (signed_char_type_node))
693a6128 2546 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
bc15d0ef 2547 if (TYPE_OK (integer_type_node))
693a6128 2548 return unsignedp ? unsigned_type_node : integer_type_node;
bc15d0ef 2549 if (TYPE_OK (short_integer_type_node))
693a6128 2550 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
bc15d0ef 2551 if (TYPE_OK (long_integer_type_node))
693a6128 2552 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
bc15d0ef 2553 if (TYPE_OK (long_long_integer_type_node))
693a6128
GRK
2554 return (unsignedp ? long_long_unsigned_type_node
2555 : long_long_integer_type_node);
bc15d0ef 2556 if (TYPE_OK (widest_integer_literal_type_node))
693a6128
GRK
2557 return (unsignedp ? widest_unsigned_literal_type_node
2558 : widest_integer_literal_type_node);
4a063bec
RH
2559
2560#if HOST_BITS_PER_WIDE_INT >= 64
bc15d0ef 2561 if (TYPE_OK (intTI_type_node))
4a063bec
RH
2562 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2563#endif
bc15d0ef 2564 if (TYPE_OK (intDI_type_node))
4a063bec 2565 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
bc15d0ef 2566 if (TYPE_OK (intSI_type_node))
4a063bec 2567 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
bc15d0ef 2568 if (TYPE_OK (intHI_type_node))
4a063bec 2569 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
bc15d0ef 2570 if (TYPE_OK (intQI_type_node))
4a063bec 2571 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
bc15d0ef 2572#undef TYPE_OK
4a063bec 2573
bc15d0ef
JM
2574 if (c_dialect_cxx ())
2575 return type;
2576 else
2577 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
693a6128 2578}
9649812a 2579
38a4afee
MM
2580/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2581
2582tree
2583c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2584{
2585 /* Extended integer types of the same width as a standard type have
2586 lesser rank, so those of the same width as int promote to int or
2587 unsigned int and are valid for printf formats expecting int or
2588 unsigned int. To avoid such special cases, avoid creating
2589 extended integer types for bit-fields if a standard integer type
2590 is available. */
2591 if (width == TYPE_PRECISION (integer_type_node))
2592 return unsignedp ? unsigned_type_node : integer_type_node;
2593 if (width == TYPE_PRECISION (signed_char_type_node))
2594 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2595 if (width == TYPE_PRECISION (short_integer_type_node))
2596 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2597 if (width == TYPE_PRECISION (long_integer_type_node))
2598 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2599 if (width == TYPE_PRECISION (long_long_integer_type_node))
2600 return (unsignedp ? long_long_unsigned_type_node
2601 : long_long_integer_type_node);
2602 return build_nonstandard_integer_type (width, unsignedp);
2603}
2604
9649812a
MM
2605/* The C version of the register_builtin_type langhook. */
2606
2607void
2608c_register_builtin_type (tree type, const char* name)
2609{
2610 tree decl;
2611
2612 decl = build_decl (TYPE_DECL, get_identifier (name), type);
2613 DECL_ARTIFICIAL (decl) = 1;
2614 if (!TYPE_NAME (type))
2615 TYPE_NAME (type) = decl;
2616 pushdecl (decl);
d1d3865f
ZW
2617
2618 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
9649812a
MM
2619}
2620
b30f223b 2621\f
6acfe908
JM
2622/* Return the minimum number of bits needed to represent VALUE in a
2623 signed or unsigned type, UNSIGNEDP says which. */
2624
05bccae2 2625unsigned int
35b1a6fa 2626min_precision (tree value, int unsignedp)
6acfe908
JM
2627{
2628 int log;
2629
2630 /* If the value is negative, compute its negative minus 1. The latter
2631 adjustment is because the absolute value of the largest negative value
2632 is one larger than the largest positive value. This is equivalent to
2633 a bit-wise negation, so use that operation instead. */
2634
2635 if (tree_int_cst_sgn (value) < 0)
987b67bc 2636 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6acfe908
JM
2637
2638 /* Return the number of bits needed, taking into account the fact
2639 that we need one more bit for a signed than unsigned type. */
2640
2641 if (integer_zerop (value))
2642 log = 0;
6acfe908 2643 else
05bccae2 2644 log = tree_floor_log2 (value);
6acfe908 2645
3f75a254 2646 return log + 1 + !unsignedp;
6acfe908
JM
2647}
2648\f
78ef5b89 2649/* Print an error message for invalid operands to arith operation
2c751309 2650 CODE with TYPE0 for operand 0, and TYPE1 for operand 1. */
b30f223b
RS
2651
2652void
2c751309 2653binary_op_error (enum tree_code code, tree type0, tree type1)
b30f223b 2654{
b3694847 2655 const char *opname;
89c78d7d 2656
b30f223b
RS
2657 switch (code)
2658 {
b30f223b
RS
2659 case PLUS_EXPR:
2660 opname = "+"; break;
2661 case MINUS_EXPR:
2662 opname = "-"; break;
2663 case MULT_EXPR:
2664 opname = "*"; break;
2665 case MAX_EXPR:
2666 opname = "max"; break;
2667 case MIN_EXPR:
2668 opname = "min"; break;
2669 case EQ_EXPR:
2670 opname = "=="; break;
2671 case NE_EXPR:
2672 opname = "!="; break;
2673 case LE_EXPR:
2674 opname = "<="; break;
2675 case GE_EXPR:
2676 opname = ">="; break;
2677 case LT_EXPR:
2678 opname = "<"; break;
2679 case GT_EXPR:
2680 opname = ">"; break;
2681 case LSHIFT_EXPR:
2682 opname = "<<"; break;
2683 case RSHIFT_EXPR:
2684 opname = ">>"; break;
2685 case TRUNC_MOD_EXPR:
047de90b 2686 case FLOOR_MOD_EXPR:
b30f223b
RS
2687 opname = "%"; break;
2688 case TRUNC_DIV_EXPR:
047de90b 2689 case FLOOR_DIV_EXPR:
b30f223b
RS
2690 opname = "/"; break;
2691 case BIT_AND_EXPR:
2692 opname = "&"; break;
2693 case BIT_IOR_EXPR:
2694 opname = "|"; break;
2695 case TRUTH_ANDIF_EXPR:
2696 opname = "&&"; break;
2697 case TRUTH_ORIF_EXPR:
2698 opname = "||"; break;
2699 case BIT_XOR_EXPR:
2700 opname = "^"; break;
6d819282 2701 default:
37b2f290 2702 gcc_unreachable ();
b30f223b 2703 }
2c751309
DB
2704 error ("invalid operands to binary %s (have %qT and %qT)", opname,
2705 type0, type1);
b30f223b
RS
2706}
2707\f
2708/* Subroutine of build_binary_op, used for comparison operations.
2709 See if the operands have both been converted from subword integer types
2710 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
2711 This function is also responsible for converting the two operands
2712 to the proper common type for comparison.
b30f223b
RS
2713
2714 The arguments of this function are all pointers to local variables
2715 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2716 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2717
2718 If this function returns nonzero, it means that the comparison has
2719 a constant value. What this function returns is an expression for
2720 that value. */
2721
2722tree
35b1a6fa
AJ
2723shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
2724 enum tree_code *rescode_ptr)
b30f223b 2725{
b3694847 2726 tree type;
b30f223b
RS
2727 tree op0 = *op0_ptr;
2728 tree op1 = *op1_ptr;
2729 int unsignedp0, unsignedp1;
2730 int real1, real2;
2731 tree primop0, primop1;
2732 enum tree_code code = *rescode_ptr;
2733
2734 /* Throw away any conversions to wider types
2735 already present in the operands. */
2736
2737 primop0 = get_narrower (op0, &unsignedp0);
2738 primop1 = get_narrower (op1, &unsignedp1);
2739
2740 /* Handle the case that OP0 does not *contain* a conversion
2741 but it *requires* conversion to FINAL_TYPE. */
2742
2743 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
8df83eae 2744 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b30f223b 2745 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
8df83eae 2746 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b30f223b
RS
2747
2748 /* If one of the operands must be floated, we cannot optimize. */
2749 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2750 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2751
2752 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
2753 so value is preserved), for canonicalization. Don't do this if
2754 the second arg is 0. */
b30f223b 2755
5af6001b 2756 if (TREE_CONSTANT (primop0)
ab22c1fa
CF
2757 && !integer_zerop (primop1) && !real_zerop (primop1)
2758 && !fixed_zerop (primop1))
b30f223b 2759 {
b3694847
SS
2760 tree tem = primop0;
2761 int temi = unsignedp0;
b30f223b
RS
2762 primop0 = primop1;
2763 primop1 = tem;
2764 tem = op0;
2765 op0 = op1;
2766 op1 = tem;
2767 *op0_ptr = op0;
2768 *op1_ptr = op1;
2769 unsignedp0 = unsignedp1;
2770 unsignedp1 = temi;
2771 temi = real1;
2772 real1 = real2;
2773 real2 = temi;
2774
2775 switch (code)
2776 {
2777 case LT_EXPR:
2778 code = GT_EXPR;
2779 break;
2780 case GT_EXPR:
2781 code = LT_EXPR;
2782 break;
2783 case LE_EXPR:
2784 code = GE_EXPR;
2785 break;
2786 case GE_EXPR:
2787 code = LE_EXPR;
2788 break;
6d819282
MK
2789 default:
2790 break;
b30f223b
RS
2791 }
2792 *rescode_ptr = code;
2793 }
2794
2795 /* If comparing an integer against a constant more bits wide,
2796 maybe we can deduce a value of 1 or 0 independent of the data.
2797 Or else truncate the constant now
2798 rather than extend the variable at run time.
2799
2800 This is only interesting if the constant is the wider arg.
2801 Also, it is not safe if the constant is unsigned and the
2802 variable arg is signed, since in this case the variable
2803 would be sign-extended and then regarded as unsigned.
2804 Our technique fails in this case because the lowest/highest
2805 possible unsigned results don't follow naturally from the
2806 lowest/highest possible values of the variable operand.
2807 For just EQ_EXPR and NE_EXPR there is another technique that
2808 could be used: see if the constant can be faithfully represented
2809 in the other operand's type, by truncating it and reextending it
2810 and see if that preserves the constant's value. */
2811
2812 if (!real1 && !real2
ab22c1fa 2813 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b30f223b
RS
2814 && TREE_CODE (primop1) == INTEGER_CST
2815 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2816 {
2817 int min_gt, max_gt, min_lt, max_lt;
2818 tree maxval, minval;
2819 /* 1 if comparison is nominally unsigned. */
8df83eae 2820 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b30f223b
RS
2821 tree val;
2822
ceef8ce4
NB
2823 type = c_common_signed_or_unsigned_type (unsignedp0,
2824 TREE_TYPE (primop0));
8bbd5685 2825
b30f223b
RS
2826 maxval = TYPE_MAX_VALUE (type);
2827 minval = TYPE_MIN_VALUE (type);
2828
2829 if (unsignedp && !unsignedp0)
ceef8ce4 2830 *restype_ptr = c_common_signed_type (*restype_ptr);
b30f223b
RS
2831
2832 if (TREE_TYPE (primop1) != *restype_ptr)
fae1b38d 2833 {
af9c6659
NS
2834 /* Convert primop1 to target type, but do not introduce
2835 additional overflow. We know primop1 is an int_cst. */
b8fca551
RG
2836 primop1 = force_fit_type_double (*restype_ptr,
2837 TREE_INT_CST_LOW (primop1),
2838 TREE_INT_CST_HIGH (primop1), 0,
d95787e6 2839 TREE_OVERFLOW (primop1));
fae1b38d 2840 }
b30f223b
RS
2841 if (type != *restype_ptr)
2842 {
2843 minval = convert (*restype_ptr, minval);
2844 maxval = convert (*restype_ptr, maxval);
2845 }
2846
2847 if (unsignedp && unsignedp0)
2848 {
2849 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2850 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2851 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2852 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2853 }
2854 else
2855 {
2856 min_gt = INT_CST_LT (primop1, minval);
2857 max_gt = INT_CST_LT (primop1, maxval);
2858 min_lt = INT_CST_LT (minval, primop1);
2859 max_lt = INT_CST_LT (maxval, primop1);
2860 }
2861
2862 val = 0;
2863 /* This used to be a switch, but Genix compiler can't handle that. */
2864 if (code == NE_EXPR)
2865 {
2866 if (max_lt || min_gt)
de7df9eb 2867 val = truthvalue_true_node;
b30f223b
RS
2868 }
2869 else if (code == EQ_EXPR)
2870 {
2871 if (max_lt || min_gt)
de7df9eb 2872 val = truthvalue_false_node;
b30f223b
RS
2873 }
2874 else if (code == LT_EXPR)
2875 {
2876 if (max_lt)
de7df9eb 2877 val = truthvalue_true_node;
b30f223b 2878 if (!min_lt)
de7df9eb 2879 val = truthvalue_false_node;
b30f223b
RS
2880 }
2881 else if (code == GT_EXPR)
2882 {
2883 if (min_gt)
de7df9eb 2884 val = truthvalue_true_node;
b30f223b 2885 if (!max_gt)
de7df9eb 2886 val = truthvalue_false_node;
b30f223b
RS
2887 }
2888 else if (code == LE_EXPR)
2889 {
2890 if (!max_gt)
de7df9eb 2891 val = truthvalue_true_node;
b30f223b 2892 if (min_gt)
de7df9eb 2893 val = truthvalue_false_node;
b30f223b
RS
2894 }
2895 else if (code == GE_EXPR)
2896 {
2897 if (!min_lt)
de7df9eb 2898 val = truthvalue_true_node;
b30f223b 2899 if (max_lt)
de7df9eb 2900 val = truthvalue_false_node;
b30f223b
RS
2901 }
2902
2903 /* If primop0 was sign-extended and unsigned comparison specd,
2904 we did a signed comparison above using the signed type bounds.
2905 But the comparison we output must be unsigned.
2906
2907 Also, for inequalities, VAL is no good; but if the signed
2908 comparison had *any* fixed result, it follows that the
2909 unsigned comparison just tests the sign in reverse
2910 (positive values are LE, negative ones GE).
2911 So we can generate an unsigned comparison
2912 against an extreme value of the signed type. */
2913
2914 if (unsignedp && !unsignedp0)
2915 {
2916 if (val != 0)
2917 switch (code)
2918 {
2919 case LT_EXPR:
2920 case GE_EXPR:
2921 primop1 = TYPE_MIN_VALUE (type);
2922 val = 0;
2923 break;
2924
2925 case LE_EXPR:
2926 case GT_EXPR:
2927 primop1 = TYPE_MAX_VALUE (type);
2928 val = 0;
2929 break;
6d819282
MK
2930
2931 default:
2932 break;
b30f223b 2933 }
12753674 2934 type = c_common_unsigned_type (type);
b30f223b
RS
2935 }
2936
5c5b2283 2937 if (TREE_CODE (primop0) != INTEGER_CST)
b30f223b 2938 {
de7df9eb 2939 if (val == truthvalue_false_node)
f6aa72dd 2940 warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type");
de7df9eb 2941 if (val == truthvalue_true_node)
f6aa72dd 2942 warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type");
b30f223b
RS
2943 }
2944
2945 if (val != 0)
2946 {
2947 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2948 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3 2949 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b30f223b
RS
2950 return val;
2951 }
2952
2953 /* Value is not predetermined, but do the comparison
2954 in the type of the operand that is not constant.
2955 TYPE is already properly set. */
2956 }
9a8ce21f
JG
2957
2958 /* If either arg is decimal float and the other is float, find the
2959 proper common type to use for comparison. */
2960 else if (real1 && real2
2961 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2962 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
2963 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2964
b30f223b 2965 else if (real1 && real2
766f6c30
RS
2966 && (TYPE_PRECISION (TREE_TYPE (primop0))
2967 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
2968 type = TREE_TYPE (primop0);
2969
2970 /* If args' natural types are both narrower than nominal type
2971 and both extend in the same manner, compare them
2972 in the type of the wider arg.
2973 Otherwise must actually extend both to the nominal
2974 common type lest different ways of extending
2975 alter the result.
2976 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2977
2978 else if (unsignedp0 == unsignedp1 && real1 == real2
2979 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2980 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2981 {
2982 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
ceef8ce4 2983 type = c_common_signed_or_unsigned_type (unsignedp0
8df83eae 2984 || TYPE_UNSIGNED (*restype_ptr),
ceef8ce4 2985 type);
b30f223b
RS
2986 /* Make sure shorter operand is extended the right way
2987 to match the longer operand. */
ceef8ce4
NB
2988 primop0
2989 = convert (c_common_signed_or_unsigned_type (unsignedp0,
2990 TREE_TYPE (primop0)),
2991 primop0);
2992 primop1
2993 = convert (c_common_signed_or_unsigned_type (unsignedp1,
2994 TREE_TYPE (primop1)),
2995 primop1);
b30f223b
RS
2996 }
2997 else
2998 {
2999 /* Here we must do the comparison on the nominal type
3000 using the args exactly as we received them. */
3001 type = *restype_ptr;
3002 primop0 = op0;
3003 primop1 = op1;
3004
3005 if (!real1 && !real2 && integer_zerop (primop1)
8df83eae 3006 && TYPE_UNSIGNED (*restype_ptr))
b30f223b
RS
3007 {
3008 tree value = 0;
3009 switch (code)
3010 {
3011 case GE_EXPR:
f6aa72dd
MLI
3012 /* All unsigned values are >= 0, so we warn. However,
3013 if OP0 is a constant that is >= 0, the signedness of
3014 the comparison isn't an issue, so suppress the
3015 warning. */
3016 if (warn_type_limits && !in_system_header
3f75a254
JM
3017 && !(TREE_CODE (primop0) == INTEGER_CST
3018 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3019 primop0))))
f6aa72dd
MLI
3020 warning (OPT_Wtype_limits,
3021 "comparison of unsigned expression >= 0 is always true");
de7df9eb 3022 value = truthvalue_true_node;
b30f223b
RS
3023 break;
3024
3025 case LT_EXPR:
f6aa72dd 3026 if (warn_type_limits && !in_system_header
3f75a254
JM
3027 && !(TREE_CODE (primop0) == INTEGER_CST
3028 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3029 primop0))))
f6aa72dd
MLI
3030 warning (OPT_Wtype_limits,
3031 "comparison of unsigned expression < 0 is always false");
de7df9eb 3032 value = truthvalue_false_node;
6d819282
MK
3033 break;
3034
3035 default:
3036 break;
b30f223b
RS
3037 }
3038
3039 if (value != 0)
3040 {
3041 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3042 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3
RS
3043 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3044 primop0, value);
b30f223b
RS
3045 return value;
3046 }
3047 }
3048 }
3049
3050 *op0_ptr = convert (type, primop0);
3051 *op1_ptr = convert (type, primop1);
3052
de7df9eb 3053 *restype_ptr = truthvalue_type_node;
b30f223b
RS
3054
3055 return 0;
3056}
3057\f
7552da58
JJ
3058/* Return a tree for the sum or difference (RESULTCODE says which)
3059 of pointer PTROP and integer INTOP. */
3060
3061tree
35b1a6fa 3062pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
7552da58 3063{
6ac01510 3064 tree size_exp, ret;
7552da58 3065
7552da58 3066 /* The result is a pointer of the same type that is being added. */
7552da58
JJ
3067 tree result_type = TREE_TYPE (ptrop);
3068
3069 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3070 {
3071 if (pedantic || warn_pointer_arith)
bda67431 3072 pedwarn ("pointer of type %<void *%> used in arithmetic");
7552da58
JJ
3073 size_exp = integer_one_node;
3074 }
3075 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3076 {
3077 if (pedantic || warn_pointer_arith)
3078 pedwarn ("pointer to a function used in arithmetic");
3079 size_exp = integer_one_node;
3080 }
3081 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3082 {
3083 if (pedantic || warn_pointer_arith)
3084 pedwarn ("pointer to member function used in arithmetic");
3085 size_exp = integer_one_node;
3086 }
7552da58
JJ
3087 else
3088 size_exp = size_in_bytes (TREE_TYPE (result_type));
3089
6ac01510
ILT
3090 /* We are manipulating pointer values, so we don't need to warn
3091 about relying on undefined signed overflow. We disable the
3092 warning here because we use integer types so fold won't know that
3093 they are really pointers. */
3094 fold_defer_overflow_warnings ();
3095
7552da58
JJ
3096 /* If what we are about to multiply by the size of the elements
3097 contains a constant term, apply distributive law
3098 and multiply that constant term separately.
3099 This helps produce common subexpressions. */
7552da58 3100 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3f75a254 3101 && !TREE_CONSTANT (intop)
7552da58
JJ
3102 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3103 && TREE_CONSTANT (size_exp)
3104 /* If the constant comes from pointer subtraction,
3105 skip this optimization--it would cause an error. */
3106 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3107 /* If the constant is unsigned, and smaller than the pointer size,
3108 then we must skip this optimization. This is because it could cause
3109 an overflow error if the constant is negative but INTOP is not. */
3f75a254 3110 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
7552da58
JJ
3111 || (TYPE_PRECISION (TREE_TYPE (intop))
3112 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3113 {
3114 enum tree_code subcode = resultcode;
3115 tree int_type = TREE_TYPE (intop);
3116 if (TREE_CODE (intop) == MINUS_EXPR)
3117 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3118 /* Convert both subexpression types to the type of intop,
3119 because weird cases involving pointer arithmetic
3120 can result in a sum or difference with different type args. */
3121 ptrop = build_binary_op (subcode, ptrop,
3122 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3123 intop = convert (int_type, TREE_OPERAND (intop, 0));
3124 }
3125
3126 /* Convert the integer argument to a type the same size as sizetype
3127 so the multiply won't overflow spuriously. */
7552da58 3128 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
8df83eae 3129 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
35b1a6fa 3130 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
8df83eae 3131 TYPE_UNSIGNED (sizetype)), intop);
7552da58
JJ
3132
3133 /* Replace the integer argument with a suitable product by the object size.
3134 Do this multiplication as signed, then convert to the appropriate
280f1ffa
EB
3135 type for the pointer operation. */
3136 intop = convert (sizetype,
3137 build_binary_op (MULT_EXPR, intop,
3138 convert (TREE_TYPE (intop), size_exp), 1));
5be014d5 3139
280f1ffa 3140 /* Create the sum or difference. */
5be014d5 3141 if (resultcode == MINUS_EXPR)
280f1ffa 3142 intop = fold_build1 (NEGATE_EXPR, sizetype, intop);
7552da58 3143
5be014d5 3144 ret = fold_build2 (POINTER_PLUS_EXPR, result_type, ptrop, intop);
6ac01510
ILT
3145
3146 fold_undefer_and_ignore_overflow_warnings ();
3147
3148 return ret;
7552da58
JJ
3149}
3150\f
b3c6d2ea
ILT
3151/* Return whether EXPR is a declaration whose address can never be
3152 NULL. */
3153
3154bool
58f9752a 3155decl_with_nonnull_addr_p (const_tree expr)
b3c6d2ea
ILT
3156{
3157 return (DECL_P (expr)
3158 && (TREE_CODE (expr) == PARM_DECL
3159 || TREE_CODE (expr) == LABEL_DECL
3160 || !DECL_WEAK (expr)));
3161}
3162
b30f223b 3163/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
85498824
JM
3164 or for an `if' or `while' statement or ?..: exp. It should already
3165 have been validated to be of suitable type; otherwise, a bad
3166 diagnostic may result.
b30f223b
RS
3167
3168 This preparation consists of taking the ordinary
3169 representation of an expression expr and producing a valid tree
3170 boolean expression describing whether expr is nonzero. We could
de7df9eb 3171 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b30f223b
RS
3172 but we optimize comparisons, &&, ||, and !.
3173
de7df9eb 3174 The resulting type should always be `truthvalue_type_node'. */
b30f223b
RS
3175
3176tree
35b1a6fa 3177c_common_truthvalue_conversion (tree expr)
b30f223b 3178{
b30f223b
RS
3179 switch (TREE_CODE (expr))
3180 {
d1a7edaf 3181 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
6f312d18
ZW
3182 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3183 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3184 case ORDERED_EXPR: case UNORDERED_EXPR:
90ec750d
RS
3185 if (TREE_TYPE (expr) == truthvalue_type_node)
3186 return expr;
3187 return build2 (TREE_CODE (expr), truthvalue_type_node,
3188 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3189
b30f223b
RS
3190 case TRUTH_ANDIF_EXPR:
3191 case TRUTH_ORIF_EXPR:
3192 case TRUTH_AND_EXPR:
3193 case TRUTH_OR_EXPR:
9379fac9 3194 case TRUTH_XOR_EXPR:
90ec750d
RS
3195 if (TREE_TYPE (expr) == truthvalue_type_node)
3196 return expr;
3197 return build2 (TREE_CODE (expr), truthvalue_type_node,
85498824
JM
3198 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)),
3199 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)));
18c0f675 3200
18d00205 3201 case TRUTH_NOT_EXPR:
90ec750d
RS
3202 if (TREE_TYPE (expr) == truthvalue_type_node)
3203 return expr;
3204 return build1 (TREE_CODE (expr), truthvalue_type_node,
85498824 3205 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)));
18d00205 3206
b30f223b
RS
3207 case ERROR_MARK:
3208 return expr;
3209
3210 case INTEGER_CST:
d95787e6
RS
3211 return integer_zerop (expr) ? truthvalue_false_node
3212 : truthvalue_true_node;
b30f223b
RS
3213
3214 case REAL_CST:
010c4d9c
RS
3215 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3216 ? truthvalue_true_node
3217 : truthvalue_false_node;
b30f223b 3218
ab22c1fa
CF
3219 case FIXED_CST:
3220 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3221 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3222 ? truthvalue_true_node
3223 : truthvalue_false_node;
3224
90ec750d
RS
3225 case FUNCTION_DECL:
3226 expr = build_unary_op (ADDR_EXPR, expr, 0);
3227 /* Fall through. */
3228
b30f223b 3229 case ADDR_EXPR:
1998463c 3230 {
f6f08360 3231 tree inner = TREE_OPERAND (expr, 0);
b3c6d2ea 3232 if (decl_with_nonnull_addr_p (inner))
1998463c 3233 {
b3c6d2ea 3234 /* Common Ada/Pascal programmer's mistake. */
c116cd05 3235 warning (OPT_Waddress,
b3c6d2ea 3236 "the address of %qD will always evaluate as %<true%>",
f6f08360 3237 inner);
1998463c
SB
3238 return truthvalue_true_node;
3239 }
fc0c675f 3240
b3c6d2ea
ILT
3241 /* If we still have a decl, it is possible for its address to
3242 be NULL, so we cannot optimize. */
3243 if (DECL_P (inner))
3244 {
3245 gcc_assert (DECL_WEAK (inner));
3246 break;
3247 }
1998463c 3248
f6f08360 3249 if (TREE_SIDE_EFFECTS (inner))
53fb4de3 3250 return build2 (COMPOUND_EXPR, truthvalue_type_node,
f6f08360 3251 inner, truthvalue_true_node);
1998463c
SB
3252 else
3253 return truthvalue_true_node;
3254 }
b30f223b 3255
766f6c30 3256 case COMPLEX_EXPR:
f0b996c5 3257 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 3258 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
85498824
JM
3259 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)),
3260 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
3261 0);
3262
b30f223b
RS
3263 case NEGATE_EXPR:
3264 case ABS_EXPR:
3265 case FLOAT_EXPR:
da7d8304 3266 /* These don't change whether an object is nonzero or zero. */
85498824 3267 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
b30f223b
RS
3268
3269 case LROTATE_EXPR:
3270 case RROTATE_EXPR:
da7d8304 3271 /* These don't change whether an object is zero or nonzero, but
b30f223b
RS
3272 we can't ignore them if their second arg has side-effects. */
3273 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
53fb4de3
RS
3274 return build2 (COMPOUND_EXPR, truthvalue_type_node,
3275 TREE_OPERAND (expr, 1),
85498824 3276 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)));
b30f223b 3277 else
85498824 3278 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
b57062ca 3279
b30f223b
RS
3280 case COND_EXPR:
3281 /* Distribute the conversion into the arms of a COND_EXPR. */
987b67bc 3282 return fold_build3 (COND_EXPR, truthvalue_type_node,
53fb4de3 3283 TREE_OPERAND (expr, 0),
85498824 3284 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
987b67bc 3285 c_common_truthvalue_conversion (TREE_OPERAND (expr, 2)));
b30f223b 3286
1043771b 3287 CASE_CONVERT:
b30f223b
RS
3288 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3289 since that affects how `default_conversion' will behave. */
3290 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
3291 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
3292 break;
b30f223b
RS
3293 /* If this is widening the argument, we can ignore it. */
3294 if (TYPE_PRECISION (TREE_TYPE (expr))
3295 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
85498824 3296 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
b30f223b
RS
3297 break;
3298
e2aab13d 3299 case MODIFY_EXPR:
fbc8d2d3
ILT
3300 if (!TREE_NO_WARNING (expr)
3301 && warn_parentheses)
3302 {
3303 warning (OPT_Wparentheses,
3304 "suggest parentheses around assignment used as truth value");
3305 TREE_NO_WARNING (expr) = 1;
3306 }
e2aab13d 3307 break;
b57062ca 3308
6d819282
MK
3309 default:
3310 break;
b30f223b
RS
3311 }
3312
f0b996c5 3313 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa 3314 {
78ef5b89 3315 tree t = save_expr (expr);
f0b8d9aa
AS
3316 return (build_binary_op
3317 ((TREE_SIDE_EFFECTS (expr)
3318 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
85498824
JM
3319 c_common_truthvalue_conversion (build_unary_op (REALPART_EXPR, t, 0)),
3320 c_common_truthvalue_conversion (build_unary_op (IMAGPART_EXPR, t, 0)),
f0b8d9aa
AS
3321 0));
3322 }
f0b996c5 3323
ab22c1fa
CF
3324 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3325 {
3326 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3327 FCONST0 (TYPE_MODE
3328 (TREE_TYPE (expr))));
3329 return build_binary_op (NE_EXPR, expr, fixed_zero_node, 1);
3330 }
3331
b30f223b
RS
3332 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3333}
3334\f
9bc15050
RG
3335static void def_builtin_1 (enum built_in_function fncode,
3336 const char *name,
3337 enum built_in_class fnclass,
3338 tree fntype, tree libtype,
3339 bool both_p, bool fallback_p, bool nonansi_p,
3340 tree fnattrs, bool implicit_p);
fc2aaf30 3341
3932261a
MM
3342
3343/* Apply the TYPE_QUALS to the new DECL. */
3344
3345void
35b1a6fa 3346c_apply_type_quals_to_decl (int type_quals, tree decl)
3932261a 3347{
4b011bbf 3348 tree type = TREE_TYPE (decl);
9f63daea 3349
5a6159dd
AP
3350 if (type == error_mark_node)
3351 return;
4b011bbf
JM
3352
3353 if (((type_quals & TYPE_QUAL_CONST)
3354 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3355 /* An object declared 'const' is only readonly after it is
3356 initialized. We don't have any way of expressing this currently,
3357 so we need to be conservative and unset TREE_READONLY for types
3358 with constructors. Otherwise aliasing code will ignore stores in
3359 an inline constructor. */
3360 && !(type && TYPE_NEEDS_CONSTRUCTING (type)))
3932261a
MM
3361 TREE_READONLY (decl) = 1;
3362 if (type_quals & TYPE_QUAL_VOLATILE)
3363 {
3364 TREE_SIDE_EFFECTS (decl) = 1;
3365 TREE_THIS_VOLATILE (decl) = 1;
3366 }
6946bc60 3367 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 3368 {
4b011bbf
JM
3369 while (type && TREE_CODE (type) == ARRAY_TYPE)
3370 /* Allow 'restrict' on arrays of pointers.
3371 FIXME currently we just ignore it. */
3372 type = TREE_TYPE (type);
3373 if (!type
3374 || !POINTER_TYPE_P (type)
3375 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
bda67431 3376 error ("invalid use of %<restrict%>");
4b011bbf 3377 else if (flag_strict_aliasing && type == TREE_TYPE (decl))
3c5ad1ff
RK
3378 /* Indicate we need to make a unique alias set for this pointer.
3379 We can't do it here because it might be pointing to an
3380 incomplete type. */
3381 DECL_POINTER_ALIAS_SET (decl) = -2;
3932261a
MM
3382 }
3383}
3384
6de9cd9a
DN
3385/* Hash function for the problem of multiple type definitions in
3386 different files. This must hash all types that will compare
3387 equal via comptypes to the same value. In practice it hashes
9cf737f8 3388 on some of the simple stuff and leaves the details to comptypes. */
6de9cd9a
DN
3389
3390static hashval_t
3391c_type_hash (const void *p)
3392{
3393 int i = 0;
3394 int shift, size;
741ac903 3395 const_tree const t = (const_tree) p;
6de9cd9a
DN
3396 tree t2;
3397 switch (TREE_CODE (t))
3398 {
8c27b7d4 3399 /* For pointers, hash on pointee type plus some swizzling. */
325c3691
RH
3400 case POINTER_TYPE:
3401 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
3402 /* Hash on number of elements and total size. */
3403 case ENUMERAL_TYPE:
3404 shift = 3;
3405 t2 = TYPE_VALUES (t);
3406 break;
3407 case RECORD_TYPE:
3408 shift = 0;
3409 t2 = TYPE_FIELDS (t);
3410 break;
3411 case QUAL_UNION_TYPE:
3412 shift = 1;
3413 t2 = TYPE_FIELDS (t);
3414 break;
3415 case UNION_TYPE:
3416 shift = 2;
3417 t2 = TYPE_FIELDS (t);
3418 break;
3419 default:
366de0ce 3420 gcc_unreachable ();
6de9cd9a
DN
3421 }
3422 for (; t2; t2 = TREE_CHAIN (t2))
3423 i++;
6fc3c3c0
TT
3424 /* We might have a VLA here. */
3425 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
3426 size = 0;
3427 else
3428 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
6de9cd9a
DN
3429 return ((size << 24) | (i << shift));
3430}
3431
4fe52ce9
DJ
3432static GTY((param_is (union tree_node))) htab_t type_hash_table;
3433
41472af8 3434/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 3435 or a type. Return -1 if we don't do anything special. */
41472af8 3436
4862826d 3437alias_set_type
35b1a6fa 3438c_common_get_alias_set (tree t)
41472af8 3439{
08bc2431 3440 tree u;
6de9cd9a 3441 PTR *slot;
35b1a6fa 3442
08bc2431
MM
3443 /* Permit type-punning when accessing a union, provided the access
3444 is directly through the union. For example, this code does not
3445 permit taking the address of a union member and then storing
3446 through it. Even the type-punning allowed here is a GCC
3447 extension, albeit a common and useful one; the C standard says
3448 that such accesses have implementation-defined behavior. */
3449 for (u = t;
3450 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3451 u = TREE_OPERAND (u, 0))
3452 if (TREE_CODE (u) == COMPONENT_REF
3453 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3454 return 0;
ece32014 3455
74d86f4f 3456 /* That's all the expressions we handle specially. */
3f75a254 3457 if (!TYPE_P (t))
74d86f4f
RH
3458 return -1;
3459
95bd1dd7 3460 /* The C standard guarantees that any object may be accessed via an
74d86f4f
RH
3461 lvalue that has character type. */
3462 if (t == char_type_node
3463 || t == signed_char_type_node
3464 || t == unsigned_char_type_node)
3bdf5ad1 3465 return 0;
3932261a 3466
d18b1ed8 3467 /* If it has the may_alias attribute, it can alias anything. */
74d86f4f 3468 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
d18b1ed8
OS
3469 return 0;
3470
f824e5c3
RK
3471 /* The C standard specifically allows aliasing between signed and
3472 unsigned variants of the same type. We treat the signed
3473 variant as canonical. */
8df83eae 3474 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
8f215dce 3475 {
ceef8ce4 3476 tree t1 = c_common_signed_type (t);
f824e5c3 3477
8f215dce
JJ
3478 /* t1 == t can happen for boolean nodes which are always unsigned. */
3479 if (t1 != t)
3480 return get_alias_set (t1);
3481 }
3bdf5ad1 3482 else if (POINTER_TYPE_P (t))
02af3af6 3483 {
3bdf5ad1 3484 tree t1;
02af3af6
MS
3485
3486 /* Unfortunately, there is no canonical form of a pointer type.
3487 In particular, if we have `typedef int I', then `int *', and
3488 `I *' are different types. So, we have to pick a canonical
3489 representative. We do this below.
d125d268 3490
b61148dd
MM
3491 Technically, this approach is actually more conservative that
3492 it needs to be. In particular, `const int *' and `int *'
684d9f3b 3493 should be in different alias sets, according to the C and C++
b61148dd
MM
3494 standard, since their types are not the same, and so,
3495 technically, an `int **' and `const int **' cannot point at
3496 the same thing.
3497
6de9cd9a 3498 But, the standard is wrong. In particular, this code is
b61148dd
MM
3499 legal C++:
3500
c22cacf3
MS
3501 int *ip;
3502 int **ipp = &ip;
3503 const int* const* cipp = ipp;
b61148dd 3504
6de9cd9a 3505 And, it doesn't make sense for that to be legal unless you
b61148dd
MM
3506 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3507 the pointed-to types. This issue has been reported to the
3508 C++ committee. */
12e1243e 3509 t1 = build_type_no_quals (t);
3bdf5ad1
RK
3510 if (t1 != t)
3511 return get_alias_set (t1);
02af3af6 3512 }
ece32014 3513
6de9cd9a
DN
3514 /* Handle the case of multiple type nodes referring to "the same" type,
3515 which occurs with IMA. These share an alias set. FIXME: Currently only
3516 C90 is handled. (In C99 type compatibility is not transitive, which
3517 complicates things mightily. The alias set splay trees can theoretically
3518 represent this, but insertion is tricky when you consider all the
3519 different orders things might arrive in.) */
3520
3521 if (c_language != clk_c || flag_isoc99)
3522 return -1;
3523
9cf737f8 3524 /* Save time if there's only one input file. */
d974312d 3525 if (num_in_fnames == 1)
6de9cd9a
DN
3526 return -1;
3527
3528 /* Pointers need special handling if they point to any type that
3529 needs special handling (below). */
3530 if (TREE_CODE (t) == POINTER_TYPE)
3531 {
3532 tree t2;
3533 /* Find bottom type under any nested POINTERs. */
9f63daea 3534 for (t2 = TREE_TYPE (t);
762f7d9d
TT
3535 TREE_CODE (t2) == POINTER_TYPE;
3536 t2 = TREE_TYPE (t2))
3537 ;
9f63daea 3538 if (TREE_CODE (t2) != RECORD_TYPE
762f7d9d
TT
3539 && TREE_CODE (t2) != ENUMERAL_TYPE
3540 && TREE_CODE (t2) != QUAL_UNION_TYPE
3541 && TREE_CODE (t2) != UNION_TYPE)
3542 return -1;
6de9cd9a 3543 if (TYPE_SIZE (t2) == 0)
762f7d9d 3544 return -1;
6de9cd9a
DN
3545 }
3546 /* These are the only cases that need special handling. */
9f63daea 3547 if (TREE_CODE (t) != RECORD_TYPE
6de9cd9a
DN
3548 && TREE_CODE (t) != ENUMERAL_TYPE
3549 && TREE_CODE (t) != QUAL_UNION_TYPE
3550 && TREE_CODE (t) != UNION_TYPE
3551 && TREE_CODE (t) != POINTER_TYPE)
3552 return -1;
3553 /* Undefined? */
3554 if (TYPE_SIZE (t) == 0)
3555 return -1;
3556
9f63daea 3557 /* Look up t in hash table. Only one of the compatible types within each
6de9cd9a
DN
3558 alias set is recorded in the table. */
3559 if (!type_hash_table)
4fe52ce9 3560 type_hash_table = htab_create_ggc (1021, c_type_hash,
6de9cd9a
DN
3561 (htab_eq) lang_hooks.types_compatible_p,
3562 NULL);
3563 slot = htab_find_slot (type_hash_table, t, INSERT);
3564 if (*slot != NULL)
6a3203c8
AP
3565 {
3566 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
3567 return TYPE_ALIAS_SET ((tree)*slot);
3568 }
6de9cd9a
DN
3569 else
3570 /* Our caller will assign and record (in t) a new alias set; all we need
3571 to do is remember t in the hash table. */
3572 *slot = t;
3573
3bdf5ad1 3574 return -1;
41472af8 3575}
0213a355 3576\f
fa72b064 3577/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where the
ea793912
GDR
3578 second parameter indicates which OPERATOR is being applied. The COMPLAIN
3579 flag controls whether we should diagnose possibly ill-formed
3580 constructs or not. */
03a08664 3581
0213a355 3582tree
03a08664 3583c_sizeof_or_alignof_type (tree type, bool is_sizeof, int complain)
0213a355 3584{
fa72b064
GDR
3585 const char *op_name;
3586 tree value = NULL;
3587 enum tree_code type_code = TREE_CODE (type);
35b1a6fa 3588
03a08664 3589 op_name = is_sizeof ? "sizeof" : "__alignof__";
35b1a6fa 3590
fa72b064 3591 if (type_code == FUNCTION_TYPE)
0213a355 3592 {
03a08664 3593 if (is_sizeof)
fa72b064 3594 {
ea793912 3595 if (complain && (pedantic || warn_pointer_arith))
bda67431 3596 pedwarn ("invalid application of %<sizeof%> to a function type");
5ade1ed2
DG
3597 else if (!complain)
3598 return error_mark_node;
fa72b064
GDR
3599 value = size_one_node;
3600 }
3601 else
3602 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3603 }
3604 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3605 {
35b1a6fa 3606 if (type_code == VOID_TYPE
ea793912 3607 && complain && (pedantic || warn_pointer_arith))
bda67431 3608 pedwarn ("invalid application of %qs to a void type", op_name);
5ade1ed2
DG
3609 else if (!complain)
3610 return error_mark_node;
fa72b064 3611 value = size_one_node;
0213a355 3612 }
0213a355
JM
3613 else if (!COMPLETE_TYPE_P (type))
3614 {
ea793912 3615 if (complain)
bda67431 3616 error ("invalid application of %qs to incomplete type %qT ",
d5a10cf0 3617 op_name, type);
fa72b064 3618 value = size_zero_node;
0213a355
JM
3619 }
3620 else
fa72b064 3621 {
03a08664 3622 if (is_sizeof)
fa72b064
GDR
3623 /* Convert in case a char is more than one unit. */
3624 value = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3625 size_int (TYPE_PRECISION (char_type_node)
3626 / BITS_PER_UNIT));
3627 else
a4e9ffe5 3628 value = size_int (TYPE_ALIGN_UNIT (type));
fa72b064 3629 }
0213a355 3630
fa72b064
GDR
3631 /* VALUE will have an integer type with TYPE_IS_SIZETYPE set.
3632 TYPE_IS_SIZETYPE means that certain things (like overflow) will
3633 never happen. However, this node should really have type
3634 `size_t', which is just a typedef for an ordinary integer type. */
4c7a6c1b 3635 value = fold_convert (size_type_node, value);
4094f4d2 3636 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
35b1a6fa 3637
fa72b064 3638 return value;
0213a355
JM
3639}
3640
3641/* Implement the __alignof keyword: Return the minimum required
837edd5f
GK
3642 alignment of EXPR, measured in bytes. For VAR_DECLs,
3643 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3644 from an "aligned" __attribute__ specification). */
7f4edbcb 3645
0213a355 3646tree
35b1a6fa 3647c_alignof_expr (tree expr)
0213a355
JM
3648{
3649 tree t;
3650
837edd5f 3651 if (VAR_OR_FUNCTION_DECL_P (expr))
a4e9ffe5 3652 t = size_int (DECL_ALIGN_UNIT (expr));
35b1a6fa 3653
0213a355
JM
3654 else if (TREE_CODE (expr) == COMPONENT_REF
3655 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3656 {
bda67431 3657 error ("%<__alignof%> applied to a bit-field");
0213a355
JM
3658 t = size_one_node;
3659 }
3660 else if (TREE_CODE (expr) == COMPONENT_REF
173bf5be 3661 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
a4e9ffe5 3662 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
35b1a6fa 3663
0213a355
JM
3664 else if (TREE_CODE (expr) == INDIRECT_REF)
3665 {
3666 tree t = TREE_OPERAND (expr, 0);
3667 tree best = t;
3668 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
35b1a6fa 3669
1043771b 3670 while (CONVERT_EXPR_P (t)
173bf5be 3671 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
0213a355
JM
3672 {
3673 int thisalign;
3674
3675 t = TREE_OPERAND (t, 0);
3676 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3677 if (thisalign > bestalign)
3678 best = t, bestalign = thisalign;
3679 }
3680 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
3681 }
3682 else
3683 return c_alignof (TREE_TYPE (expr));
3684
4c7a6c1b 3685 return fold_convert (size_type_node, t);
0213a355
JM
3686}
3687\f
df061a43
RS
3688/* Handle C and C++ default attributes. */
3689
3690enum built_in_attribute
3691{
3692#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3693#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3694#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3695#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
df061a43
RS
3696#include "builtin-attrs.def"
3697#undef DEF_ATTR_NULL_TREE
3698#undef DEF_ATTR_INT
3699#undef DEF_ATTR_IDENT
3700#undef DEF_ATTR_TREE_LIST
df061a43
RS
3701 ATTR_LAST
3702};
3703
3704static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3705
35b1a6fa 3706static void c_init_attributes (void);
df061a43 3707
a0274e3e 3708enum c_builtin_type
7f4edbcb 3709{
10841285
MM
3710#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3711#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3712#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3713#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3714#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3715#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
10a0d495 3716#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
a0274e3e
JJ
3717#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
3718#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
10841285
MM
3719#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3720#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3721#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
08291658 3722#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
10a0d495
JJ
3723#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3724#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
3725 NAME,
10841285
MM
3726#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3727#include "builtin-types.def"
3728#undef DEF_PRIMITIVE_TYPE
3729#undef DEF_FUNCTION_TYPE_0
3730#undef DEF_FUNCTION_TYPE_1
3731#undef DEF_FUNCTION_TYPE_2
3732#undef DEF_FUNCTION_TYPE_3
3733#undef DEF_FUNCTION_TYPE_4
10a0d495
JJ
3734#undef DEF_FUNCTION_TYPE_5
3735#undef DEF_FUNCTION_TYPE_6
a0274e3e 3736#undef DEF_FUNCTION_TYPE_7
10841285
MM
3737#undef DEF_FUNCTION_TYPE_VAR_0
3738#undef DEF_FUNCTION_TYPE_VAR_1
3739#undef DEF_FUNCTION_TYPE_VAR_2
08291658 3740#undef DEF_FUNCTION_TYPE_VAR_3
10a0d495
JJ
3741#undef DEF_FUNCTION_TYPE_VAR_4
3742#undef DEF_FUNCTION_TYPE_VAR_5
10841285 3743#undef DEF_POINTER_TYPE
a0274e3e
JJ
3744 BT_LAST
3745};
3746
3747typedef enum c_builtin_type builtin_type;
10841285 3748
a0274e3e
JJ
3749/* A temporary array for c_common_nodes_and_builtins. Used in
3750 communication with def_fn_type. */
3751static tree builtin_types[(int) BT_LAST + 1];
10841285 3752
a0274e3e
JJ
3753/* A helper function for c_common_nodes_and_builtins. Build function type
3754 for DEF with return type RET and N arguments. If VAR is true, then the
3755 function should be variadic after those N arguments.
3756
3757 Takes special care not to ICE if any of the types involved are
3758 error_mark_node, which indicates that said type is not in fact available
3759 (see builtin_type_for_size). In which case the function type as a whole
3760 should be error_mark_node. */
3761
3762static void
3763def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3764{
3765 tree args = NULL, t;
3766 va_list list;
3767 int i;
3768
3769 va_start (list, n);
3770 for (i = 0; i < n; ++i)
3771 {
d75d71e0 3772 builtin_type a = (builtin_type) va_arg (list, int);
a0274e3e
JJ
3773 t = builtin_types[a];
3774 if (t == error_mark_node)
3775 goto egress;
3776 args = tree_cons (NULL_TREE, t, args);
3777 }
3778 va_end (list);
3779
3780 args = nreverse (args);
3781 if (!var)
3782 args = chainon (args, void_list_node);
3783
3784 t = builtin_types[ret];
3785 if (t == error_mark_node)
3786 goto egress;
3787 t = build_function_type (t, args);
3788
3789 egress:
3790 builtin_types[def] = t;
3791}
3792
c6d86fce
ILT
3793/* Build builtin functions common to both C and C++ language
3794 frontends. */
3795
3796static void
3797c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3798{
3799#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3800 builtin_types[ENUM] = VALUE;
3801#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3802 def_fn_type (ENUM, RETURN, 0, 0);
3803#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3804 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3805#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3806 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3807#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3808 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3809#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3810 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3811#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3812 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3813#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3814 ARG6) \
3815 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3816#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3817 ARG6, ARG7) \
3818 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3819#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3820 def_fn_type (ENUM, RETURN, 1, 0);
3821#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3822 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3823#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3824 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3825#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3826 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3827#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3828 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3829#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3830 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3831#define DEF_POINTER_TYPE(ENUM, TYPE) \
3832 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
3833
3834#include "builtin-types.def"
3835
3836#undef DEF_PRIMITIVE_TYPE
3837#undef DEF_FUNCTION_TYPE_1
3838#undef DEF_FUNCTION_TYPE_2
3839#undef DEF_FUNCTION_TYPE_3
3840#undef DEF_FUNCTION_TYPE_4
3841#undef DEF_FUNCTION_TYPE_5
3842#undef DEF_FUNCTION_TYPE_6
3843#undef DEF_FUNCTION_TYPE_VAR_0
3844#undef DEF_FUNCTION_TYPE_VAR_1
3845#undef DEF_FUNCTION_TYPE_VAR_2
3846#undef DEF_FUNCTION_TYPE_VAR_3
3847#undef DEF_FUNCTION_TYPE_VAR_4
3848#undef DEF_FUNCTION_TYPE_VAR_5
3849#undef DEF_POINTER_TYPE
3850 builtin_types[(int) BT_LAST] = NULL_TREE;
3851
3852 c_init_attributes ();
3853
3854#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
3855 NONANSI_P, ATTRS, IMPLICIT, COND) \
3856 if (NAME && COND) \
3857 def_builtin_1 (ENUM, NAME, CLASS, \
3858 builtin_types[(int) TYPE], \
3859 builtin_types[(int) LIBTYPE], \
3860 BOTH_P, FALLBACK_P, NONANSI_P, \
3861 built_in_attributes[(int) ATTRS], IMPLICIT);
3862#include "builtins.def"
3863#undef DEF_BUILTIN
3864
8de7ef2a
UB
3865 targetm.init_builtins ();
3866
c6d86fce
ILT
3867 build_common_builtin_nodes ();
3868
c6d86fce
ILT
3869 if (flag_mudflap)
3870 mudflap_init ();
3871}
3872
a0274e3e
JJ
3873/* Build tree nodes and builtin functions common to both C and C++ language
3874 frontends. */
3875
3876void
3877c_common_nodes_and_builtins (void)
3878{
b6baa67d
KVH
3879 int char16_type_size;
3880 int char32_type_size;
eaa7c03f
JM
3881 int wchar_type_size;
3882 tree array_domain_type;
9f720c3e 3883 tree va_list_ref_type_node;
daf68dd7 3884 tree va_list_arg_type_node;
d3707adb 3885
eaa7c03f 3886 /* Define `int' and `char' first so that dbx will output them first. */
6496a589 3887 record_builtin_type (RID_INT, NULL, integer_type_node);
eaa7c03f
JM
3888 record_builtin_type (RID_CHAR, "char", char_type_node);
3889
3890 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
3891 "unsigned long", "long long unsigned" and "unsigned short" were in C++
3892 but not C. Are the conditionals here needed? */
37fa72e9 3893 if (c_dialect_cxx ())
6496a589 3894 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
eaa7c03f
JM
3895 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
3896 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
3897 record_builtin_type (RID_MAX, "long unsigned int",
3898 long_unsigned_type_node);
37fa72e9 3899 if (c_dialect_cxx ())
eaa7c03f
JM
3900 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
3901 record_builtin_type (RID_MAX, "long long int",
3902 long_long_integer_type_node);
3903 record_builtin_type (RID_MAX, "long long unsigned int",
3904 long_long_unsigned_type_node);
37fa72e9 3905 if (c_dialect_cxx ())
eaa7c03f
JM
3906 record_builtin_type (RID_MAX, "long long unsigned",
3907 long_long_unsigned_type_node);
3908 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
3909 record_builtin_type (RID_MAX, "short unsigned int",
3910 short_unsigned_type_node);
37fa72e9 3911 if (c_dialect_cxx ())
eaa7c03f
JM
3912 record_builtin_type (RID_MAX, "unsigned short",
3913 short_unsigned_type_node);
3914
3915 /* Define both `signed char' and `unsigned char'. */
3916 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
3917 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
3918
b0c48229
NB
3919 /* These are types that c_common_type_for_size and
3920 c_common_type_for_mode use. */
ae2bcd98
RS
3921 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3922 intQI_type_node));
3923 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3924 intHI_type_node));
3925 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3926 intSI_type_node));
3927 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3928 intDI_type_node));
eaa7c03f 3929#if HOST_BITS_PER_WIDE_INT >= 64
1e1b8649
AP
3930 if (targetm.scalar_mode_supported_p (TImode))
3931 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
3932 get_identifier ("__int128_t"),
3933 intTI_type_node));
eaa7c03f 3934#endif
ae2bcd98
RS
3935 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3936 unsigned_intQI_type_node));
3937 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3938 unsigned_intHI_type_node));
3939 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3940 unsigned_intSI_type_node));
3941 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3942 unsigned_intDI_type_node));
eaa7c03f 3943#if HOST_BITS_PER_WIDE_INT >= 64
1e1b8649
AP
3944 if (targetm.scalar_mode_supported_p (TImode))
3945 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
3946 get_identifier ("__uint128_t"),
3947 unsigned_intTI_type_node));
eaa7c03f
JM
3948#endif
3949
3950 /* Create the widest literal types. */
3951 widest_integer_literal_type_node
3952 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
ae2bcd98
RS
3953 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3954 widest_integer_literal_type_node));
eaa7c03f
JM
3955
3956 widest_unsigned_literal_type_node
3957 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
ae2bcd98
RS
3958 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
3959 widest_unsigned_literal_type_node));
eaa7c03f
JM
3960
3961 /* `unsigned long' is the standard type for sizeof.
3962 Note that stddef.h uses `unsigned long',
3963 and this must agree, even if long and int are the same size. */
c9f8536c 3964 size_type_node =
eaa7c03f 3965 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
c9f8536c
MM
3966 signed_size_type_node = c_common_signed_type (size_type_node);
3967 set_sizetype (size_type_node);
eaa7c03f 3968
d1c38823
ZD
3969 pid_type_node =
3970 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
3971
eaa7c03f
JM
3972 build_common_tree_nodes_2 (flag_short_double);
3973
6496a589
KG
3974 record_builtin_type (RID_FLOAT, NULL, float_type_node);
3975 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
eaa7c03f
JM
3976 record_builtin_type (RID_MAX, "long double", long_double_type_node);
3977
9a8ce21f
JG
3978 /* Only supported decimal floating point extension if the target
3979 actually supports underlying modes. */
3980 if (targetm.scalar_mode_supported_p (SDmode)
3981 && targetm.scalar_mode_supported_p (DDmode)
3982 && targetm.scalar_mode_supported_p (TDmode))
3983 {
3984 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
3985 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
3986 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
3987 }
3988
ab22c1fa
CF
3989 if (targetm.fixed_point_supported_p ())
3990 {
3991 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
3992 record_builtin_type (RID_FRACT, NULL, fract_type_node);
3993 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
3994 record_builtin_type (RID_MAX, "long long _Fract",
3995 long_long_fract_type_node);
3996 record_builtin_type (RID_MAX, "unsigned short _Fract",
3997 unsigned_short_fract_type_node);
3998 record_builtin_type (RID_MAX, "unsigned _Fract",
3999 unsigned_fract_type_node);
4000 record_builtin_type (RID_MAX, "unsigned long _Fract",
4001 unsigned_long_fract_type_node);
4002 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4003 unsigned_long_long_fract_type_node);
4004 record_builtin_type (RID_MAX, "_Sat short _Fract",
4005 sat_short_fract_type_node);
4006 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4007 record_builtin_type (RID_MAX, "_Sat long _Fract",
4008 sat_long_fract_type_node);
4009 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4010 sat_long_long_fract_type_node);
4011 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4012 sat_unsigned_short_fract_type_node);
4013 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4014 sat_unsigned_fract_type_node);
4015 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4016 sat_unsigned_long_fract_type_node);
4017 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4018 sat_unsigned_long_long_fract_type_node);
4019 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4020 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4021 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4022 record_builtin_type (RID_MAX, "long long _Accum",
4023 long_long_accum_type_node);
4024 record_builtin_type (RID_MAX, "unsigned short _Accum",
4025 unsigned_short_accum_type_node);
4026 record_builtin_type (RID_MAX, "unsigned _Accum",
4027 unsigned_accum_type_node);
4028 record_builtin_type (RID_MAX, "unsigned long _Accum",
4029 unsigned_long_accum_type_node);
4030 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4031 unsigned_long_long_accum_type_node);
4032 record_builtin_type (RID_MAX, "_Sat short _Accum",
4033 sat_short_accum_type_node);
4034 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4035 record_builtin_type (RID_MAX, "_Sat long _Accum",
4036 sat_long_accum_type_node);
4037 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4038 sat_long_long_accum_type_node);
4039 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4040 sat_unsigned_short_accum_type_node);
4041 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4042 sat_unsigned_accum_type_node);
4043 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4044 sat_unsigned_long_accum_type_node);
4045 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4046 sat_unsigned_long_long_accum_type_node);
4047
4048 }
4049
ae2bcd98
RS
4050 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
4051 get_identifier ("complex int"),
4052 complex_integer_type_node));
4053 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
4054 get_identifier ("complex float"),
4055 complex_float_type_node));
4056 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
4057 get_identifier ("complex double"),
4058 complex_double_type_node));
4059 lang_hooks.decls.pushdecl
43577e6b
NB
4060 (build_decl (TYPE_DECL, get_identifier ("complex long double"),
4061 complex_long_double_type_node));
eaa7c03f 4062
498c0f27
JJ
4063 if (c_dialect_cxx ())
4064 /* For C++, make fileptr_type_node a distinct void * type until
4065 FILE type is defined. */
8dd16ecc 4066 fileptr_type_node = build_variant_type_copy (ptr_type_node);
498c0f27 4067
6496a589 4068 record_builtin_type (RID_VOID, NULL, void_type_node);
eaa7c03f 4069
06d40de8
DG
4070 /* Set the TYPE_NAME for any variants that were built before
4071 record_builtin_type gave names to the built-in types. */
4072 {
4073 tree void_name = TYPE_NAME (void_type_node);
4074 TYPE_NAME (void_type_node) = NULL_TREE;
4075 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4076 = void_name;
4077 TYPE_NAME (void_type_node) = void_name;
4078 }
4079
8c27b7d4 4080 /* This node must not be shared. */
4a90aeeb 4081 void_zero_node = make_node (INTEGER_CST);
10841285
MM
4082 TREE_TYPE (void_zero_node) = void_type_node;
4083
eaa7c03f
JM
4084 void_list_node = build_void_list_node ();
4085
4086 /* Make a type to be the domain of a few array types
4087 whose domains don't really matter.
4088 200 is small enough that it always fits in size_t
4089 and large enough that it can hold most function names for the
4090 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4091 array_domain_type = build_index_type (size_int (200));
4092
4093 /* Make a type for arrays of characters.
4094 With luck nothing will ever really depend on the length of this
4095 array type. */
4096 char_array_type_node
4097 = build_array_type (char_type_node, array_domain_type);
4098
4099 /* Likewise for arrays of ints. */
4100 int_array_type_node
4101 = build_array_type (integer_type_node, array_domain_type);
4102
10841285
MM
4103 string_type_node = build_pointer_type (char_type_node);
4104 const_string_type_node
4105 = build_pointer_type (build_qualified_type
4106 (char_type_node, TYPE_QUAL_CONST));
4107
eaa7c03f 4108 /* This is special for C++ so functions can be overloaded. */
a11eba95 4109 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
eaa7c03f
JM
4110 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4111 wchar_type_size = TYPE_PRECISION (wchar_type_node);
37fa72e9 4112 if (c_dialect_cxx ())
eaa7c03f 4113 {
8df83eae 4114 if (TYPE_UNSIGNED (wchar_type_node))
eaa7c03f
JM
4115 wchar_type_node = make_unsigned_type (wchar_type_size);
4116 else
4117 wchar_type_node = make_signed_type (wchar_type_size);
4118 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4119 }
4120 else
4121 {
ceef8ce4 4122 signed_wchar_type_node = c_common_signed_type (wchar_type_node);
12753674 4123 unsigned_wchar_type_node = c_common_unsigned_type (wchar_type_node);
eaa7c03f
JM
4124 }
4125
4126 /* This is for wide string constants. */
4127 wchar_array_type_node
4128 = build_array_type (wchar_type_node, array_domain_type);
4129
b6baa67d
KVH
4130 /* Define 'char16_t'. */
4131 char16_type_node = get_identifier (CHAR16_TYPE);
4132 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4133 char16_type_size = TYPE_PRECISION (char16_type_node);
4134 if (c_dialect_cxx ())
4135 {
4136 char16_type_node = make_unsigned_type (char16_type_size);
4137
4138 if (cxx_dialect == cxx0x)
4139 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4140 }
4141
4142 /* This is for UTF-16 string constants. */
4143 char16_array_type_node
4144 = build_array_type (char16_type_node, array_domain_type);
4145
4146 /* Define 'char32_t'. */
4147 char32_type_node = get_identifier (CHAR32_TYPE);
4148 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4149 char32_type_size = TYPE_PRECISION (char32_type_node);
4150 if (c_dialect_cxx ())
4151 {
4152 char32_type_node = make_unsigned_type (char32_type_size);
4153
4154 if (cxx_dialect == cxx0x)
4155 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4156 }
4157
4158 /* This is for UTF-32 string constants. */
4159 char32_array_type_node
4160 = build_array_type (char32_type_node, array_domain_type);
4161
5fd8e536
JM
4162 wint_type_node =
4163 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4164
4165 intmax_type_node =
4166 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4167 uintmax_type_node =
4168 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4169
4170 default_function_type = build_function_type (integer_type_node, NULL_TREE);
4171 ptrdiff_type_node
4172 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
12753674 4173 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5fd8e536 4174
ae2bcd98 4175 lang_hooks.decls.pushdecl
43577e6b
NB
4176 (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
4177 va_list_type_node));
35cbb299
KT
4178#ifdef TARGET_ENUM_VA_LIST
4179 {
4180 int l;
4181 const char *pname;
4182 tree ptype;
4183 for (l = 0; TARGET_ENUM_VA_LIST (l, &pname, &ptype); ++l)
4184 {
4185 lang_hooks.decls.pushdecl
4186 (build_decl (TYPE_DECL, get_identifier (pname),
4187 ptype));
4188
4189 }
4190 }
4191#endif
daf68dd7 4192
daf68dd7 4193 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
4194 {
4195 va_list_arg_type_node = va_list_ref_type_node =
4196 build_pointer_type (TREE_TYPE (va_list_type_node));
4197 }
daf68dd7 4198 else
9f720c3e
GK
4199 {
4200 va_list_arg_type_node = va_list_type_node;
4201 va_list_ref_type_node = build_reference_type (va_list_type_node);
4202 }
35b1a6fa 4203
c6d86fce
ILT
4204 if (!flag_preprocess_only)
4205 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4677862a 4206
5b47282c 4207 main_identifier_node = get_identifier ("main");
b2f97e4a
MM
4208
4209 /* Create the built-in __null node. It is important that this is
4210 not shared. */
4211 null_node = make_node (INTEGER_CST);
4212 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
a0274e3e
JJ
4213
4214 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4215 memset (builtin_types, 0, sizeof (builtin_types));
7f4edbcb 4216}
d3707adb 4217
b482789c
MA
4218/* Look up the function in built_in_decls that corresponds to DECL
4219 and set ASMSPEC as its user assembler name. DECL must be a
6c6cfbfd 4220 function decl that declares a builtin. */
b482789c
MA
4221
4222void
4223set_builtin_user_assembler_name (tree decl, const char *asmspec)
4224{
4225 tree builtin;
4226 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
4227 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
4228 && asmspec != 0);
4229
4230 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
4231 set_user_assembler_name (builtin, asmspec);
4232 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
4233 init_block_move_fn (asmspec);
4234 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
4235 init_block_clear_fn (asmspec);
4236}
4237
3b2db49f
MM
4238/* The number of named compound-literals generated thus far. */
4239static GTY(()) int compound_literal_number;
4240
4241/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4242
4243void
4244set_compound_literal_name (tree decl)
4245{
4246 char *name;
4247 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4248 compound_literal_number);
4249 compound_literal_number++;
4250 DECL_NAME (decl) = get_identifier (name);
4251}
4252
d3707adb 4253tree
35b1a6fa 4254build_va_arg (tree expr, tree type)
d3707adb
RH
4255{
4256 return build1 (VA_ARG_EXPR, type, expr);
4257}
fc2aaf30
JM
4258
4259
7d14c755
JM
4260/* Linked list of disabled built-in functions. */
4261
4262typedef struct disabled_builtin
4263{
4264 const char *name;
4265 struct disabled_builtin *next;
4266} disabled_builtin;
4267static disabled_builtin *disabled_builtins = NULL;
4268
35b1a6fa 4269static bool builtin_function_disabled_p (const char *);
7d14c755
JM
4270
4271/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4272 begins with "__builtin_", give an error. */
4273
4274void
35b1a6fa 4275disable_builtin_function (const char *name)
7d14c755
JM
4276{
4277 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
bda67431 4278 error ("cannot disable built-in function %qs", name);
7d14c755
JM
4279 else
4280 {
1ad463f4
BI
4281 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4282 new_disabled_builtin->name = name;
4283 new_disabled_builtin->next = disabled_builtins;
4284 disabled_builtins = new_disabled_builtin;
7d14c755
JM
4285 }
4286}
4287
4288
4289/* Return true if the built-in function NAME has been disabled, false
4290 otherwise. */
4291
4292static bool
35b1a6fa 4293builtin_function_disabled_p (const char *name)
7d14c755
JM
4294{
4295 disabled_builtin *p;
4296 for (p = disabled_builtins; p != NULL; p = p->next)
4297 {
4298 if (strcmp (name, p->name) == 0)
4299 return true;
4300 }
4301 return false;
4302}
4303
4304
9bc15050
RG
4305/* Worker for DEF_BUILTIN.
4306 Possibly define a builtin function with one or two names.
4307 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4308 nonansi_p and flag_no_nonansi_builtin. */
fc2aaf30 4309
9bc15050
RG
4310static void
4311def_builtin_1 (enum built_in_function fncode,
4312 const char *name,
4313 enum built_in_class fnclass,
4314 tree fntype, tree libtype,
4315 bool both_p, bool fallback_p, bool nonansi_p,
4316 tree fnattrs, bool implicit_p)
fc2aaf30 4317{
9bc15050
RG
4318 tree decl;
4319 const char *libname;
4320
a0274e3e
JJ
4321 if (fntype == error_mark_node)
4322 return;
4323
9bc15050
RG
4324 gcc_assert ((!both_p && !fallback_p)
4325 || !strncmp (name, "__builtin_",
4326 strlen ("__builtin_")));
4327
4328 libname = name + strlen ("__builtin_");
c79efc4d
RÁE
4329 decl = add_builtin_function (name, fntype, fncode, fnclass,
4330 (fallback_p ? libname : NULL),
4331 fnattrs);
9bc15050
RG
4332 if (both_p
4333 && !flag_no_builtin && !builtin_function_disabled_p (libname)
7d14c755 4334 && !(nonansi_p && flag_no_nonansi_builtin))
c79efc4d
RÁE
4335 add_builtin_function (libname, libtype, fncode, fnclass,
4336 NULL, fnattrs);
7f685e17 4337
9bc15050
RG
4338 built_in_decls[(int) fncode] = decl;
4339 if (implicit_p)
4340 implicit_built_in_decls[(int) fncode] = decl;
fc2aaf30 4341}
c530479e 4342\f
d72040f5
RH
4343/* Nonzero if the type T promotes to int. This is (nearly) the
4344 integral promotions defined in ISO C99 6.3.1.1/2. */
4345
4346bool
58f9752a 4347c_promoting_integer_type_p (const_tree t)
d72040f5
RH
4348{
4349 switch (TREE_CODE (t))
4350 {
4351 case INTEGER_TYPE:
4352 return (TYPE_MAIN_VARIANT (t) == char_type_node
4353 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
4354 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
4355 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
c6c04fca
RL
4356 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
4357 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d72040f5
RH
4358
4359 case ENUMERAL_TYPE:
4360 /* ??? Technically all enumerations not larger than an int
4361 promote to an int. But this is used along code paths
4362 that only want to notice a size change. */
4363 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
4364
4365 case BOOLEAN_TYPE:
4366 return 1;
4367
4368 default:
4369 return 0;
4370 }
4371}
4372
c530479e
RH
4373/* Return 1 if PARMS specifies a fixed number of parameters
4374 and none of their types is affected by default promotions. */
4375
4376int
58f9752a 4377self_promoting_args_p (const_tree parms)
c530479e 4378{
58f9752a 4379 const_tree t;
c530479e
RH
4380 for (t = parms; t; t = TREE_CHAIN (t))
4381 {
b3694847 4382 tree type = TREE_VALUE (t);
7e8176d7 4383
694fea20
VR
4384 if (type == error_mark_node)
4385 continue;
4386
c530479e
RH
4387 if (TREE_CHAIN (t) == 0 && type != void_type_node)
4388 return 0;
4389
4390 if (type == 0)
4391 return 0;
4392
4393 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4394 return 0;
4395
d72040f5 4396 if (c_promoting_integer_type_p (type))
c530479e
RH
4397 return 0;
4398 }
4399 return 1;
4400}
5eda3d66 4401
12ea3302
GDR
4402/* Recursively remove any '*' or '&' operator from TYPE. */
4403tree
4404strip_pointer_operator (tree t)
4405{
4406 while (POINTER_TYPE_P (t))
4407 t = TREE_TYPE (t);
4408 return t;
4409}
4410
ba992967
SP
4411/* Recursively remove pointer or array type from TYPE. */
4412tree
4413strip_pointer_or_array_types (tree t)
4414{
4415 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
4416 t = TREE_TYPE (t);
4417 return t;
4418}
4419
8f17b5c5
MM
4420/* Used to compare case labels. K1 and K2 are actually tree nodes
4421 representing case labels, or NULL_TREE for a `default' label.
4422 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4423 K2, and 0 if K1 and K2 are equal. */
4424
4425int
35b1a6fa 4426case_compare (splay_tree_key k1, splay_tree_key k2)
8f17b5c5
MM
4427{
4428 /* Consider a NULL key (such as arises with a `default' label) to be
4429 smaller than anything else. */
4430 if (!k1)
4431 return k2 ? -1 : 0;
4432 else if (!k2)
4433 return k1 ? 1 : 0;
4434
4435 return tree_int_cst_compare ((tree) k1, (tree) k2);
4436}
4437
4438/* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
4439 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
4440 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
4441 case label was declared using the usual C/C++ syntax, rather than
4442 the GNU case range extension. CASES is a tree containing all the
4443 case ranges processed so far; COND is the condition for the
8c161995
RH
4444 switch-statement itself. Returns the CASE_LABEL_EXPR created, or
4445 ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
8f17b5c5
MM
4446
4447tree
a6c0a76c
SB
4448c_add_case_label (splay_tree cases, tree cond, tree orig_type,
4449 tree low_value, tree high_value)
8f17b5c5
MM
4450{
4451 tree type;
4452 tree label;
4453 tree case_label;
4454 splay_tree_node node;
4455
4456 /* Create the LABEL_DECL itself. */
6de9cd9a 4457 label = create_artificial_label ();
8f17b5c5
MM
4458
4459 /* If there was an error processing the switch condition, bail now
4460 before we get more confused. */
4461 if (!cond || cond == error_mark_node)
6de9cd9a 4462 goto error_out;
8f17b5c5 4463
35b1a6fa
AJ
4464 if ((low_value && TREE_TYPE (low_value)
4465 && POINTER_TYPE_P (TREE_TYPE (low_value)))
8f17b5c5
MM
4466 || (high_value && TREE_TYPE (high_value)
4467 && POINTER_TYPE_P (TREE_TYPE (high_value))))
522ddfa2
JM
4468 {
4469 error ("pointers are not permitted as case values");
4470 goto error_out;
4471 }
8f17b5c5
MM
4472
4473 /* Case ranges are a GNU extension. */
4474 if (high_value && pedantic)
37fa72e9 4475 pedwarn ("range expressions in switch statements are non-standard");
8f17b5c5
MM
4476
4477 type = TREE_TYPE (cond);
4478 if (low_value)
4479 {
4480 low_value = check_case_value (low_value);
4481 low_value = convert_and_check (type, low_value);
c0e22534
NS
4482 if (low_value == error_mark_node)
4483 goto error_out;
8f17b5c5
MM
4484 }
4485 if (high_value)
4486 {
4487 high_value = check_case_value (high_value);
4488 high_value = convert_and_check (type, high_value);
c0e22534
NS
4489 if (high_value == error_mark_node)
4490 goto error_out;
8f17b5c5
MM
4491 }
4492
c0e22534
NS
4493 if (low_value && high_value)
4494 {
4495 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
c22cacf3
MS
4496 really a case range, even though it was written that way.
4497 Remove the HIGH_VALUE to simplify later processing. */
c0e22534
NS
4498 if (tree_int_cst_equal (low_value, high_value))
4499 high_value = NULL_TREE;
4500 else if (!tree_int_cst_lt (low_value, high_value))
4501 warning (0, "empty range specified");
4502 }
8f17b5c5 4503
a6c0a76c
SB
4504 /* See if the case is in range of the type of the original testing
4505 expression. If both low_value and high_value are out of range,
4506 don't insert the case label and return NULL_TREE. */
4507 if (low_value
3f75a254
JM
4508 && !check_case_bounds (type, orig_type,
4509 &low_value, high_value ? &high_value : NULL))
a6c0a76c
SB
4510 return NULL_TREE;
4511
8f17b5c5
MM
4512 /* Look up the LOW_VALUE in the table of case labels we already
4513 have. */
4514 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4515 /* If there was not an exact match, check for overlapping ranges.
4516 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4517 that's a `default' label and the only overlap is an exact match. */
4518 if (!node && (low_value || high_value))
4519 {
4520 splay_tree_node low_bound;
4521 splay_tree_node high_bound;
4522
4523 /* Even though there wasn't an exact match, there might be an
4524 overlap between this case range and another case range.
4525 Since we've (inductively) not allowed any overlapping case
4526 ranges, we simply need to find the greatest low case label
4527 that is smaller that LOW_VALUE, and the smallest low case
4528 label that is greater than LOW_VALUE. If there is an overlap
4529 it will occur in one of these two ranges. */
4530 low_bound = splay_tree_predecessor (cases,
4531 (splay_tree_key) low_value);
4532 high_bound = splay_tree_successor (cases,
4533 (splay_tree_key) low_value);
4534
4535 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4536 the LOW_VALUE, so there is no need to check unless the
4537 LOW_BOUND is in fact itself a case range. */
4538 if (low_bound
4539 && CASE_HIGH ((tree) low_bound->value)
4540 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4541 low_value) >= 0)
4542 node = low_bound;
4543 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4544 range is bigger than the low end of the current range, so we
4545 are only interested if the current range is a real range, and
4546 not an ordinary case label. */
35b1a6fa 4547 else if (high_bound
8f17b5c5
MM
4548 && high_value
4549 && (tree_int_cst_compare ((tree) high_bound->key,
4550 high_value)
4551 <= 0))
4552 node = high_bound;
4553 }
4554 /* If there was an overlap, issue an error. */
4555 if (node)
4556 {
8c161995 4557 tree duplicate = CASE_LABEL ((tree) node->value);
8f17b5c5
MM
4558
4559 if (high_value)
4560 {
4561 error ("duplicate (or overlapping) case value");
ddd2d57e 4562 error ("%Jthis is the first entry overlapping that value", duplicate);
8f17b5c5
MM
4563 }
4564 else if (low_value)
4565 {
4566 error ("duplicate case value") ;
ddd2d57e 4567 error ("%Jpreviously used here", duplicate);
8f17b5c5
MM
4568 }
4569 else
4570 {
4571 error ("multiple default labels in one switch");
ddd2d57e 4572 error ("%Jthis is the first default label", duplicate);
8f17b5c5 4573 }
6de9cd9a 4574 goto error_out;
8f17b5c5
MM
4575 }
4576
4577 /* Add a CASE_LABEL to the statement-tree. */
4578 case_label = add_stmt (build_case_label (low_value, high_value, label));
4579 /* Register this case label in the splay tree. */
35b1a6fa 4580 splay_tree_insert (cases,
8f17b5c5
MM
4581 (splay_tree_key) low_value,
4582 (splay_tree_value) case_label);
4583
4584 return case_label;
6de9cd9a
DN
4585
4586 error_out:
9e14e18f 4587 /* Add a label so that the back-end doesn't think that the beginning of
6de9cd9a 4588 the switch is unreachable. Note that we do not add a case label, as
41806d92 4589 that just leads to duplicates and thence to failure later on. */
6de9cd9a
DN
4590 if (!cases->root)
4591 {
4592 tree t = create_artificial_label ();
9e14e18f 4593 add_stmt (build_stmt (LABEL_EXPR, t));
6de9cd9a
DN
4594 }
4595 return error_mark_node;
4596}
4597
4598/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
4599 Used to verify that case values match up with enumerator values. */
4600
4601static void
4602match_case_to_enum_1 (tree key, tree type, tree label)
4603{
4604 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
4605
4606 /* ??? Not working too hard to print the double-word value.
4607 Should perhaps be done with %lwd in the diagnostic routines? */
4608 if (TREE_INT_CST_HIGH (key) == 0)
4609 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
4610 TREE_INT_CST_LOW (key));
4611 else if (!TYPE_UNSIGNED (type)
4612 && TREE_INT_CST_HIGH (key) == -1
4613 && TREE_INT_CST_LOW (key) != 0)
4614 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
4615 -TREE_INT_CST_LOW (key));
4616 else
4617 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
3d57d7ce
DK
4618 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
4619 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6de9cd9a
DN
4620
4621 if (TYPE_NAME (type) == 0)
683d6ff9
MLI
4622 warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
4623 "%Jcase value %qs not in enumerated type",
8c161995 4624 CASE_LABEL (label), buf);
6de9cd9a 4625 else
683d6ff9
MLI
4626 warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
4627 "%Jcase value %qs not in enumerated type %qT",
8c161995 4628 CASE_LABEL (label), buf, type);
6de9cd9a
DN
4629}
4630
c782c2fe
RS
4631/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
4632 Used to verify that case values match up with enumerator values. */
4633
6de9cd9a
DN
4634static int
4635match_case_to_enum (splay_tree_node node, void *data)
4636{
4637 tree label = (tree) node->value;
28dab132 4638 tree type = (tree) data;
6de9cd9a
DN
4639
4640 /* Skip default case. */
4641 if (!CASE_LOW (label))
4642 return 0;
4643
c782c2fe 4644 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6de9cd9a 4645 when we did our enum->case scan. Reset our scratch bit after. */
c782c2fe 4646 if (!CASE_LOW_SEEN (label))
6de9cd9a
DN
4647 match_case_to_enum_1 (CASE_LOW (label), type, label);
4648 else
c782c2fe 4649 CASE_LOW_SEEN (label) = 0;
6de9cd9a 4650
c782c2fe
RS
4651 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
4652 not set, that means that CASE_HIGH did not appear when we did our
4653 enum->case scan. Reset our scratch bit after. */
6de9cd9a
DN
4654 if (CASE_HIGH (label))
4655 {
c782c2fe
RS
4656 if (!CASE_HIGH_SEEN (label))
4657 match_case_to_enum_1 (CASE_HIGH (label), type, label);
4658 else
4659 CASE_HIGH_SEEN (label) = 0;
6de9cd9a
DN
4660 }
4661
4662 return 0;
4663}
4664
fbc315db
ILT
4665/* Handle -Wswitch*. Called from the front end after parsing the
4666 switch construct. */
4667/* ??? Should probably be somewhere generic, since other languages
4668 besides C and C++ would want this. At the moment, however, C/C++
4669 are the only tree-ssa languages that support enumerations at all,
4670 so the point is moot. */
6de9cd9a 4671
fbc315db
ILT
4672void
4673c_do_switch_warnings (splay_tree cases, location_t switch_location,
4674 tree type, tree cond)
6de9cd9a 4675{
9f63daea 4676 splay_tree_node default_node;
c782c2fe
RS
4677 splay_tree_node node;
4678 tree chain;
683d6ff9 4679 int saved_warn_switch;
6de9cd9a
DN
4680
4681 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
4682 return;
4683
6de9cd9a 4684 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
44c21c7f
DD
4685 if (!default_node)
4686 warning (OPT_Wswitch_default, "%Hswitch missing default case",
4687 &switch_location);
6de9cd9a 4688
c782c2fe
RS
4689 /* From here on, we only care about about enumerated types. */
4690 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
4691 return;
4692
6de9cd9a
DN
4693 /* If the switch expression was an enumerated type, check that
4694 exactly all enumeration literals are covered by the cases.
4695 The check is made when -Wswitch was specified and there is no
4696 default case, or when -Wswitch-enum was specified. */
6de9cd9a 4697
c782c2fe
RS
4698 if (!warn_switch_enum
4699 && !(warn_switch && !default_node))
4700 return;
4701
4702 /* Clearing COND if it is not an integer constant simplifies
4703 the tests inside the loop below. */
4704 if (TREE_CODE (cond) != INTEGER_CST)
4705 cond = NULL_TREE;
4706
4707 /* The time complexity here is O(N*lg(N)) worst case, but for the
4708 common case of monotonically increasing enumerators, it is
4709 O(N), since the nature of the splay tree will keep the next
4710 element adjacent to the root at all times. */
6de9cd9a 4711
c782c2fe
RS
4712 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
4713 {
4714 tree value = TREE_VALUE (chain);
4715 node = splay_tree_lookup (cases, (splay_tree_key) value);
4716 if (node)
6de9cd9a 4717 {
c782c2fe
RS
4718 /* Mark the CASE_LOW part of the case entry as seen. */
4719 tree label = (tree) node->value;
4720 CASE_LOW_SEEN (label) = 1;
4721 continue;
4722 }
4723
4724 /* Even though there wasn't an exact match, there might be a
fa10beec 4725 case range which includes the enumerator's value. */
c782c2fe
RS
4726 node = splay_tree_predecessor (cases, (splay_tree_key) value);
4727 if (node && CASE_HIGH ((tree) node->value))
4728 {
4729 tree label = (tree) node->value;
4730 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
4731 if (cmp >= 0)
6de9cd9a 4732 {
c782c2fe
RS
4733 /* If we match the upper bound exactly, mark the CASE_HIGH
4734 part of the case entry as seen. */
4735 if (cmp == 0)
4736 CASE_HIGH_SEEN (label) = 1;
4737 continue;
6de9cd9a
DN
4738 }
4739 }
4740
c782c2fe
RS
4741 /* We've now determined that this enumerated literal isn't
4742 handled by the case labels of the switch statement. */
6de9cd9a 4743
c782c2fe
RS
4744 /* If the switch expression is a constant, we only really care
4745 about whether that constant is handled by the switch. */
4746 if (cond && tree_int_cst_compare (cond, value))
4747 continue;
6de9cd9a 4748
683d6ff9
MLI
4749 /* If there is a default_node, the only relevant option is
4750 Wswitch-enum. Otherwise, if both are enabled then we prefer
4751 to warn using -Wswitch because -Wswitch is enabled by -Wall
4752 while -Wswitch-enum is explicit. */
4753 warning ((default_node || !warn_switch)
4754 ? OPT_Wswitch_enum : OPT_Wswitch,
4755 "%Henumeration value %qE not handled in switch",
c782c2fe 4756 &switch_location, TREE_PURPOSE (chain));
6de9cd9a 4757 }
c782c2fe
RS
4758
4759 /* Warn if there are case expressions that don't correspond to
4760 enumerators. This can occur since C and C++ don't enforce
4761 type-checking of assignments to enumeration variables.
4762
4763 The time complexity here is now always O(N) worst case, since
4764 we should have marked both the lower bound and upper bound of
4765 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
4766 above. This scan also resets those fields. */
683d6ff9
MLI
4767
4768 /* If there is a default_node, the only relevant option is
4769 Wswitch-enum. Otherwise, if both are enabled then we prefer
4770 to warn using -Wswitch because -Wswitch is enabled by -Wall
4771 while -Wswitch-enum is explicit. */
4772 saved_warn_switch = warn_switch;
4773 if (default_node)
4774 warn_switch = 0;
c782c2fe 4775 splay_tree_foreach (cases, match_case_to_enum, type);
683d6ff9
MLI
4776 warn_switch = saved_warn_switch;
4777
8f17b5c5
MM
4778}
4779
6b665219
MM
4780/* Finish an expression taking the address of LABEL (an
4781 IDENTIFIER_NODE). Returns an expression for the address. */
15b732b2 4782
35b1a6fa
AJ
4783tree
4784finish_label_address_expr (tree label)
15b732b2
NB
4785{
4786 tree result;
4787
4788 if (pedantic)
37fa72e9 4789 pedwarn ("taking the address of a label is non-standard");
15b732b2 4790
6b665219
MM
4791 if (label == error_mark_node)
4792 return error_mark_node;
4793
15b732b2
NB
4794 label = lookup_label (label);
4795 if (label == NULL_TREE)
4796 result = null_pointer_node;
4797 else
4798 {
4799 TREE_USED (label) = 1;
4800 result = build1 (ADDR_EXPR, ptr_type_node, label);
15b732b2
NB
4801 /* The current function in not necessarily uninlinable.
4802 Computed gotos are incompatible with inlining, but the value
4803 here could be used only in a diagnostic, for example. */
4804 }
4805
4806 return result;
4807}
4808
8f17b5c5 4809/* Hook used by expand_expr to expand language-specific tree codes. */
9f63daea 4810/* The only things that should go here are bits needed to expand
1ea7e6ad 4811 constant initializers. Everything else should be handled by the
6de9cd9a 4812 gimplification routines. */
8f17b5c5
MM
4813
4814rtx
9f63daea 4815c_expand_expr (tree exp, rtx target, enum machine_mode tmode,
d75d71e0 4816 int modifiera /* Actually enum expand_modifier. */,
0fab64a3 4817 rtx *alt_rtl)
8f17b5c5 4818{
d75d71e0 4819 enum expand_modifier modifier = (enum expand_modifier) modifiera;
8f17b5c5
MM
4820 switch (TREE_CODE (exp))
4821 {
db3acfa5
JM
4822 case COMPOUND_LITERAL_EXPR:
4823 {
4824 /* Initialize the anonymous variable declared in the compound
4825 literal, then return the variable. */
4826 tree decl = COMPOUND_LITERAL_EXPR_DECL (exp);
4827 emit_local_var (decl);
0fab64a3 4828 return expand_expr_real (decl, target, tmode, modifier, alt_rtl);
db3acfa5
JM
4829 }
4830
8f17b5c5 4831 default:
366de0ce 4832 gcc_unreachable ();
8f17b5c5 4833 }
8f17b5c5
MM
4834}
4835
270c60bb
DB
4836/* Hook used by staticp to handle language-specific tree codes. */
4837
4838tree
4839c_staticp (tree exp)
4840{
4841 return (TREE_CODE (exp) == COMPOUND_LITERAL_EXPR
4842 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp))
4843 ? exp : NULL);
4844}
19552aa5
JM
4845\f
4846
4847/* Given a boolean expression ARG, return a tree representing an increment
4848 or decrement (as indicated by CODE) of ARG. The front end must check for
4849 invalid cases (e.g., decrement in C++). */
4850tree
35b1a6fa 4851boolean_increment (enum tree_code code, tree arg)
19552aa5
JM
4852{
4853 tree val;
b5119fa1 4854 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
37fa72e9 4855
19552aa5
JM
4856 arg = stabilize_reference (arg);
4857 switch (code)
4858 {
4859 case PREINCREMENT_EXPR:
53fb4de3 4860 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5
JM
4861 break;
4862 case POSTINCREMENT_EXPR:
53fb4de3 4863 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5 4864 arg = save_expr (arg);
53fb4de3
RS
4865 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4866 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
4867 break;
4868 case PREDECREMENT_EXPR:
53fb4de3
RS
4869 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
4870 invert_truthvalue (arg));
19552aa5
JM
4871 break;
4872 case POSTDECREMENT_EXPR:
53fb4de3
RS
4873 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
4874 invert_truthvalue (arg));
19552aa5 4875 arg = save_expr (arg);
53fb4de3
RS
4876 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4877 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
4878 break;
4879 default:
366de0ce 4880 gcc_unreachable ();
19552aa5
JM
4881 }
4882 TREE_SIDE_EFFECTS (val) = 1;
4883 return val;
4884}
03dc0325 4885\f
cb60f38d
NB
4886/* Built-in macros for stddef.h, that require macros defined in this
4887 file. */
460bd0e3 4888void
35b1a6fa 4889c_stddef_cpp_builtins(void)
3df89291 4890{
5279d739
ZW
4891 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
4892 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
4893 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
4894 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
85291069
JM
4895 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
4896 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
676997cf
RH
4897}
4898
6431177a 4899static void
35b1a6fa 4900c_init_attributes (void)
6431177a
JM
4901{
4902 /* Fill in the built_in_attributes array. */
4a90aeeb 4903#define DEF_ATTR_NULL_TREE(ENUM) \
6431177a 4904 built_in_attributes[(int) ENUM] = NULL_TREE;
4a90aeeb 4905#define DEF_ATTR_INT(ENUM, VALUE) \
7d60be94 4906 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
6431177a
JM
4907#define DEF_ATTR_IDENT(ENUM, STRING) \
4908 built_in_attributes[(int) ENUM] = get_identifier (STRING);
4909#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
4910 built_in_attributes[(int) ENUM] \
4911 = tree_cons (built_in_attributes[(int) PURPOSE], \
4912 built_in_attributes[(int) VALUE], \
4913 built_in_attributes[(int) CHAIN]);
6431177a
JM
4914#include "builtin-attrs.def"
4915#undef DEF_ATTR_NULL_TREE
4916#undef DEF_ATTR_INT
4917#undef DEF_ATTR_IDENT
4918#undef DEF_ATTR_TREE_LIST
03dc0325 4919}
26f943fd 4920
349ae713
NB
4921/* Attribute handlers common to C front ends. */
4922
4923/* Handle a "packed" attribute; arguments as in
4924 struct attribute_spec.handler. */
4925
4926static tree
e18476eb 4927handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
a742c759 4928 int flags, bool *no_add_attrs)
349ae713 4929{
c6e4cc53 4930 if (TYPE_P (*node))
349ae713
NB
4931 {
4932 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 4933 *node = build_variant_type_copy (*node);
c6e4cc53 4934 TYPE_PACKED (*node) = 1;
349ae713
NB
4935 }
4936 else if (TREE_CODE (*node) == FIELD_DECL)
646c0835
NS
4937 {
4938 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT)
4939 warning (OPT_Wattributes,
4940 "%qE attribute ignored for field of type %qT",
4941 name, TREE_TYPE (*node));
4942 else
4943 DECL_PACKED (*node) = 1;
4944 }
349ae713 4945 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
c6e4cc53
NS
4946 used for DECL_REGISTER. It wouldn't mean anything anyway.
4947 We can't set DECL_PACKED on the type of a TYPE_DECL, because
4948 that changes what the typedef is typing. */
349ae713
NB
4949 else
4950 {
5c498b10 4951 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
4952 *no_add_attrs = true;
4953 }
4954
4955 return NULL_TREE;
4956}
4957
4958/* Handle a "nocommon" attribute; arguments as in
4959 struct attribute_spec.handler. */
4960
4961static tree
35b1a6fa 4962handle_nocommon_attribute (tree *node, tree name,
e18476eb
BI
4963 tree ARG_UNUSED (args),
4964 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
4965{
4966 if (TREE_CODE (*node) == VAR_DECL)
4967 DECL_COMMON (*node) = 0;
4968 else
4969 {
5c498b10 4970 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
4971 *no_add_attrs = true;
4972 }
4973
4974 return NULL_TREE;
4975}
4976
4977/* Handle a "common" attribute; arguments as in
4978 struct attribute_spec.handler. */
4979
4980static tree
e18476eb
BI
4981handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4982 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
4983{
4984 if (TREE_CODE (*node) == VAR_DECL)
4985 DECL_COMMON (*node) = 1;
4986 else
4987 {
5c498b10 4988 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
4989 *no_add_attrs = true;
4990 }
4991
4992 return NULL_TREE;
4993}
4994
4995/* Handle a "noreturn" attribute; arguments as in
4996 struct attribute_spec.handler. */
4997
4998static tree
e18476eb
BI
4999handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5000 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
5001{
5002 tree type = TREE_TYPE (*node);
5003
5004 /* See FIXME comment in c_common_attribute_table. */
5005 if (TREE_CODE (*node) == FUNCTION_DECL)
5006 TREE_THIS_VOLATILE (*node) = 1;
5007 else if (TREE_CODE (type) == POINTER_TYPE
5008 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5009 TREE_TYPE (*node)
5010 = build_pointer_type
5011 (build_type_variant (TREE_TYPE (type),
c29726e2 5012 TYPE_READONLY (TREE_TYPE (type)), 1));
349ae713
NB
5013 else
5014 {
5c498b10 5015 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5016 *no_add_attrs = true;
5017 }
5018
5019 return NULL_TREE;
5020}
5021
52bf96d2
JH
5022/* Handle a "hot" and attribute; arguments as in
5023 struct attribute_spec.handler. */
5024
5025static tree
5026handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5027 int ARG_UNUSED (flags), bool *no_add_attrs)
5028{
5029 if (TREE_CODE (*node) == FUNCTION_DECL)
5030 {
5031 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
5032 {
5033 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5034 name, "cold");
5035 *no_add_attrs = true;
5036 }
5037 /* Do nothing else, just set the attribute. We'll get at
5038 it later with lookup_attribute. */
5039 }
5040 else
5041 {
5042 warning (OPT_Wattributes, "%qE attribute ignored", name);
5043 *no_add_attrs = true;
5044 }
5045
5046 return NULL_TREE;
5047}
5048/* Handle a "cold" and attribute; arguments as in
5049 struct attribute_spec.handler. */
5050
5051static tree
5052handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5053 int ARG_UNUSED (flags), bool *no_add_attrs)
5054{
5055 if (TREE_CODE (*node) == FUNCTION_DECL)
5056 {
5057 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
5058 {
5059 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5060 name, "hot");
5061 *no_add_attrs = true;
5062 }
5063 /* Do nothing else, just set the attribute. We'll get at
5064 it later with lookup_attribute. */
5065 }
5066 else
5067 {
5068 warning (OPT_Wattributes, "%qE attribute ignored", name);
5069 *no_add_attrs = true;
5070 }
5071
5072 return NULL_TREE;
5073}
5074
349ae713
NB
5075/* Handle a "noinline" attribute; arguments as in
5076 struct attribute_spec.handler. */
5077
5078static tree
35b1a6fa 5079handle_noinline_attribute (tree *node, tree name,
e18476eb
BI
5080 tree ARG_UNUSED (args),
5081 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
5082{
5083 if (TREE_CODE (*node) == FUNCTION_DECL)
5084 DECL_UNINLINABLE (*node) = 1;
5085 else
5086 {
5c498b10 5087 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5088 *no_add_attrs = true;
5089 }
5090
5091 return NULL_TREE;
5092}
5093
5094/* Handle a "always_inline" attribute; arguments as in
5095 struct attribute_spec.handler. */
5096
5097static tree
35b1a6fa 5098handle_always_inline_attribute (tree *node, tree name,
e18476eb
BI
5099 tree ARG_UNUSED (args),
5100 int ARG_UNUSED (flags),
a742c759 5101 bool *no_add_attrs)
349ae713
NB
5102{
5103 if (TREE_CODE (*node) == FUNCTION_DECL)
5104 {
c536a6a7
RG
5105 /* Set the attribute and mark it for disregarding inline
5106 limits. */
5107 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
349ae713
NB
5108 }
5109 else
5110 {
5c498b10 5111 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713 5112 *no_add_attrs = true;
4eb7fd83
JJ
5113 }
5114
5115 return NULL_TREE;
5116}
5117
5118/* Handle a "gnu_inline" attribute; arguments as in
5119 struct attribute_spec.handler. */
5120
5121static tree
5122handle_gnu_inline_attribute (tree *node, tree name,
5123 tree ARG_UNUSED (args),
5124 int ARG_UNUSED (flags),
5125 bool *no_add_attrs)
5126{
5127 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
5128 {
5129 /* Do nothing else, just set the attribute. We'll get at
5130 it later with lookup_attribute. */
5131 }
5132 else
5133 {
5134 warning (OPT_Wattributes, "%qE attribute ignored", name);
5135 *no_add_attrs = true;
349ae713
NB
5136 }
5137
5138 return NULL_TREE;
5139}
5140
d752cfdb
JJ
5141/* Handle an "artificial" attribute; arguments as in
5142 struct attribute_spec.handler. */
5143
5144static tree
5145handle_artificial_attribute (tree *node, tree name,
5146 tree ARG_UNUSED (args),
5147 int ARG_UNUSED (flags),
5148 bool *no_add_attrs)
5149{
5150 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
5151 {
5152 /* Do nothing else, just set the attribute. We'll get at
5153 it later with lookup_attribute. */
5154 }
5155 else
5156 {
5157 warning (OPT_Wattributes, "%qE attribute ignored", name);
5158 *no_add_attrs = true;
5159 }
5160
5161 return NULL_TREE;
5162}
5163
0691d1d4
RG
5164/* Handle a "flatten" attribute; arguments as in
5165 struct attribute_spec.handler. */
5166
5167static tree
5168handle_flatten_attribute (tree *node, tree name,
c22cacf3
MS
5169 tree args ATTRIBUTE_UNUSED,
5170 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0691d1d4
RG
5171{
5172 if (TREE_CODE (*node) == FUNCTION_DECL)
5173 /* Do nothing else, just set the attribute. We'll get at
5174 it later with lookup_attribute. */
5175 ;
5176 else
5177 {
5178 warning (OPT_Wattributes, "%qE attribute ignored", name);
5179 *no_add_attrs = true;
5180 }
5181
5182 return NULL_TREE;
5183}
5184
d2af6a68
JJ
5185/* Handle a "warning" or "error" attribute; arguments as in
5186 struct attribute_spec.handler. */
5187
5188static tree
5189handle_error_attribute (tree *node, tree name, tree args,
5190 int ARG_UNUSED (flags), bool *no_add_attrs)
5191{
5192 if (TREE_CODE (*node) == FUNCTION_DECL
5193 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5194 /* Do nothing else, just set the attribute. We'll get at
5195 it later with lookup_attribute. */
5196 ;
5197 else
5198 {
5199 warning (OPT_Wattributes, "%qE attribute ignored", name);
5200 *no_add_attrs = true;
5201 }
5202
5203 return NULL_TREE;
5204}
0691d1d4 5205
349ae713
NB
5206/* Handle a "used" attribute; arguments as in
5207 struct attribute_spec.handler. */
5208
5209static tree
e18476eb
BI
5210handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
5211 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 5212{
d7ddbe24
RH
5213 tree node = *pnode;
5214
5215 if (TREE_CODE (node) == FUNCTION_DECL
5216 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)))
4d7d0451 5217 {
4d7d0451 5218 TREE_USED (node) = 1;
8e3e233b 5219 DECL_PRESERVE_P (node) = 1;
4d7d0451 5220 }
349ae713
NB
5221 else
5222 {
5c498b10 5223 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5224 *no_add_attrs = true;
5225 }
5226
5227 return NULL_TREE;
5228}
5229
5230/* Handle a "unused" attribute; arguments as in
5231 struct attribute_spec.handler. */
5232
5233static tree
e18476eb
BI
5234handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5235 int flags, bool *no_add_attrs)
349ae713
NB
5236{
5237 if (DECL_P (*node))
5238 {
5239 tree decl = *node;
5240
5241 if (TREE_CODE (decl) == PARM_DECL
5242 || TREE_CODE (decl) == VAR_DECL
5243 || TREE_CODE (decl) == FUNCTION_DECL
5244 || TREE_CODE (decl) == LABEL_DECL
5245 || TREE_CODE (decl) == TYPE_DECL)
5246 TREE_USED (decl) = 1;
5247 else
5248 {
5c498b10 5249 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5250 *no_add_attrs = true;
5251 }
5252 }
5253 else
5254 {
5255 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 5256 *node = build_variant_type_copy (*node);
349ae713
NB
5257 TREE_USED (*node) = 1;
5258 }
5259
5260 return NULL_TREE;
5261}
5262
ce91e74c
JH
5263/* Handle a "externally_visible" attribute; arguments as in
5264 struct attribute_spec.handler. */
5265
5266static tree
5267handle_externally_visible_attribute (tree *pnode, tree name,
5268 tree ARG_UNUSED (args),
5269 int ARG_UNUSED (flags),
5270 bool *no_add_attrs)
5271{
5272 tree node = *pnode;
5273
343d4b27 5274 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
ce91e74c 5275 {
343d4b27
JJ
5276 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
5277 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
5278 {
5279 warning (OPT_Wattributes,
5280 "%qE attribute have effect only on public objects", name);
5281 *no_add_attrs = true;
5282 }
ce91e74c 5283 }
ce91e74c
JH
5284 else
5285 {
5286 warning (OPT_Wattributes, "%qE attribute ignored", name);
5287 *no_add_attrs = true;
5288 }
5289
5290 return NULL_TREE;
5291}
5292
349ae713
NB
5293/* Handle a "const" attribute; arguments as in
5294 struct attribute_spec.handler. */
5295
5296static tree
e18476eb
BI
5297handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5298 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
5299{
5300 tree type = TREE_TYPE (*node);
5301
5302 /* See FIXME comment on noreturn in c_common_attribute_table. */
5303 if (TREE_CODE (*node) == FUNCTION_DECL)
5304 TREE_READONLY (*node) = 1;
5305 else if (TREE_CODE (type) == POINTER_TYPE
5306 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5307 TREE_TYPE (*node)
5308 = build_pointer_type
5309 (build_type_variant (TREE_TYPE (type), 1,
5310 TREE_THIS_VOLATILE (TREE_TYPE (type))));
5311 else
5312 {
5c498b10 5313 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5314 *no_add_attrs = true;
5315 }
5316
5317 return NULL_TREE;
5318}
5319
5320/* Handle a "transparent_union" attribute; arguments as in
5321 struct attribute_spec.handler. */
5322
5323static tree
35b1a6fa 5324handle_transparent_union_attribute (tree *node, tree name,
e18476eb 5325 tree ARG_UNUSED (args), int flags,
a742c759 5326 bool *no_add_attrs)
349ae713 5327{
4009f2e7 5328 tree type;
52dd234b
RH
5329
5330 *no_add_attrs = true;
349ae713 5331
4009f2e7
JM
5332 if (TREE_CODE (*node) == TYPE_DECL)
5333 node = &TREE_TYPE (*node);
5334 type = *node;
349ae713 5335
52dd234b 5336 if (TREE_CODE (type) == UNION_TYPE)
349ae713 5337 {
52dd234b
RH
5338 /* When IN_PLACE is set, leave the check for FIELDS and MODE to
5339 the code in finish_struct. */
349ae713 5340 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
52dd234b
RH
5341 {
5342 if (TYPE_FIELDS (type) == NULL_TREE
5343 || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type)))
5344 goto ignored;
5345
5346 /* A type variant isn't good enough, since we don't a cast
5347 to such a type removed as a no-op. */
5348 *node = type = build_duplicate_type (type);
5349 }
5350
5351 TYPE_TRANSPARENT_UNION (type) = 1;
5352 return NULL_TREE;
349ae713
NB
5353 }
5354
52dd234b
RH
5355 ignored:
5356 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5357 return NULL_TREE;
5358}
5359
fc8600f9
MM
5360/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
5361 get the requested priority for a constructor or destructor,
5362 possibly issuing diagnostics for invalid or reserved
5363 priorities. */
5364
5365static priority_type
5366get_priority (tree args, bool is_destructor)
5367{
5368 HOST_WIDE_INT pri;
b2f4bed8 5369 tree arg;
fc8600f9
MM
5370
5371 if (!args)
5372 return DEFAULT_INIT_PRIORITY;
b2f4bed8 5373
f6fc5c86
MM
5374 if (!SUPPORTS_INIT_PRIORITY)
5375 {
5376 if (is_destructor)
5377 error ("destructor priorities are not supported");
5378 else
5379 error ("constructor priorities are not supported");
5380 return DEFAULT_INIT_PRIORITY;
5381 }
5382
b2f4bed8
MM
5383 arg = TREE_VALUE (args);
5384 if (!host_integerp (arg, /*pos=*/0)
5385 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
fc8600f9
MM
5386 goto invalid;
5387
5388 pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0);
5389 if (pri < 0 || pri > MAX_INIT_PRIORITY)
5390 goto invalid;
5391
5392 if (pri <= MAX_RESERVED_INIT_PRIORITY)
5393 {
5394 if (is_destructor)
5395 warning (0,
5396 "destructor priorities from 0 to %d are reserved "
5397 "for the implementation",
5398 MAX_RESERVED_INIT_PRIORITY);
5399 else
5400 warning (0,
5401 "constructor priorities from 0 to %d are reserved "
5402 "for the implementation",
5403 MAX_RESERVED_INIT_PRIORITY);
5404 }
5405 return pri;
5406
5407 invalid:
5408 if (is_destructor)
5409 error ("destructor priorities must be integers from 0 to %d inclusive",
5410 MAX_INIT_PRIORITY);
5411 else
5412 error ("constructor priorities must be integers from 0 to %d inclusive",
5413 MAX_INIT_PRIORITY);
5414 return DEFAULT_INIT_PRIORITY;
5415}
5416
349ae713
NB
5417/* Handle a "constructor" attribute; arguments as in
5418 struct attribute_spec.handler. */
5419
5420static tree
fc8600f9 5421handle_constructor_attribute (tree *node, tree name, tree args,
e18476eb 5422 int ARG_UNUSED (flags),
a742c759 5423 bool *no_add_attrs)
349ae713
NB
5424{
5425 tree decl = *node;
5426 tree type = TREE_TYPE (decl);
5427
5428 if (TREE_CODE (decl) == FUNCTION_DECL
5429 && TREE_CODE (type) == FUNCTION_TYPE
5430 && decl_function_context (decl) == 0)
5431 {
fc8600f9 5432 priority_type priority;
349ae713 5433 DECL_STATIC_CONSTRUCTOR (decl) = 1;
fc8600f9
MM
5434 priority = get_priority (args, /*is_destructor=*/false);
5435 SET_DECL_INIT_PRIORITY (decl, priority);
349ae713
NB
5436 TREE_USED (decl) = 1;
5437 }
5438 else
5439 {
5c498b10 5440 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5441 *no_add_attrs = true;
5442 }
5443
5444 return NULL_TREE;
5445}
5446
5447/* Handle a "destructor" attribute; arguments as in
5448 struct attribute_spec.handler. */
5449
5450static tree
fc8600f9 5451handle_destructor_attribute (tree *node, tree name, tree args,
e18476eb 5452 int ARG_UNUSED (flags),
a742c759 5453 bool *no_add_attrs)
349ae713
NB
5454{
5455 tree decl = *node;
5456 tree type = TREE_TYPE (decl);
5457
5458 if (TREE_CODE (decl) == FUNCTION_DECL
5459 && TREE_CODE (type) == FUNCTION_TYPE
5460 && decl_function_context (decl) == 0)
5461 {
fc8600f9 5462 priority_type priority;
349ae713 5463 DECL_STATIC_DESTRUCTOR (decl) = 1;
fc8600f9
MM
5464 priority = get_priority (args, /*is_destructor=*/true);
5465 SET_DECL_FINI_PRIORITY (decl, priority);
349ae713
NB
5466 TREE_USED (decl) = 1;
5467 }
5468 else
5469 {
5c498b10 5470 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5471 *no_add_attrs = true;
5472 }
5473
5474 return NULL_TREE;
5475}
5476
5477/* Handle a "mode" attribute; arguments as in
5478 struct attribute_spec.handler. */
5479
5480static tree
e18476eb
BI
5481handle_mode_attribute (tree *node, tree name, tree args,
5482 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
5483{
5484 tree type = *node;
5485
5486 *no_add_attrs = true;
5487
5488 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
5c498b10 5489 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5490 else
5491 {
5492 int j;
5493 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
5494 int len = strlen (p);
5495 enum machine_mode mode = VOIDmode;
5496 tree typefm;
6dd53648 5497 bool valid_mode;
349ae713
NB
5498
5499 if (len > 4 && p[0] == '_' && p[1] == '_'
5500 && p[len - 1] == '_' && p[len - 2] == '_')
5501 {
28dab132 5502 char *newp = (char *) alloca (len - 1);
349ae713
NB
5503
5504 strcpy (newp, &p[2]);
5505 newp[len - 4] = '\0';
5506 p = newp;
5507 }
5508
5509 /* Change this type to have a type with the specified mode.
5510 First check for the special modes. */
3f75a254 5511 if (!strcmp (p, "byte"))
349ae713
NB
5512 mode = byte_mode;
5513 else if (!strcmp (p, "word"))
5514 mode = word_mode;
3f75a254 5515 else if (!strcmp (p, "pointer"))
349ae713 5516 mode = ptr_mode;
c7ff6e7a
AK
5517 else if (!strcmp (p, "libgcc_cmp_return"))
5518 mode = targetm.libgcc_cmp_return_mode ();
5519 else if (!strcmp (p, "libgcc_shift_count"))
5520 mode = targetm.libgcc_shift_count_mode ();
7b0518e3
UW
5521 else if (!strcmp (p, "unwind_word"))
5522 mode = targetm.unwind_word_mode ();
349ae713
NB
5523 else
5524 for (j = 0; j < NUM_MACHINE_MODES; j++)
5525 if (!strcmp (p, GET_MODE_NAME (j)))
61f03aba
RH
5526 {
5527 mode = (enum machine_mode) j;
5528 break;
5529 }
349ae713
NB
5530
5531 if (mode == VOIDmode)
4a5eab38 5532 {
9e637a26 5533 error ("unknown machine mode %qs", p);
4a5eab38
PB
5534 return NULL_TREE;
5535 }
5536
6dd53648
RH
5537 valid_mode = false;
5538 switch (GET_MODE_CLASS (mode))
4a5eab38 5539 {
6dd53648
RH
5540 case MODE_INT:
5541 case MODE_PARTIAL_INT:
5542 case MODE_FLOAT:
9a8ce21f 5543 case MODE_DECIMAL_FLOAT:
ab22c1fa
CF
5544 case MODE_FRACT:
5545 case MODE_UFRACT:
5546 case MODE_ACCUM:
5547 case MODE_UACCUM:
6dd53648
RH
5548 valid_mode = targetm.scalar_mode_supported_p (mode);
5549 break;
5550
5551 case MODE_COMPLEX_INT:
5552 case MODE_COMPLEX_FLOAT:
5553 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
5554 break;
5555
5556 case MODE_VECTOR_INT:
5557 case MODE_VECTOR_FLOAT:
ab22c1fa
CF
5558 case MODE_VECTOR_FRACT:
5559 case MODE_VECTOR_UFRACT:
5560 case MODE_VECTOR_ACCUM:
5561 case MODE_VECTOR_UACCUM:
5c498b10
DD
5562 warning (OPT_Wattributes, "specifying vector types with "
5563 "__attribute__ ((mode)) is deprecated");
5564 warning (OPT_Wattributes,
5565 "use __attribute__ ((vector_size)) instead");
6dd53648
RH
5566 valid_mode = vector_mode_valid_p (mode);
5567 break;
4a5eab38 5568
6dd53648
RH
5569 default:
5570 break;
5571 }
5572 if (!valid_mode)
5573 {
9e637a26 5574 error ("unable to emulate %qs", p);
6dd53648
RH
5575 return NULL_TREE;
5576 }
4a5eab38 5577
6dd53648 5578 if (POINTER_TYPE_P (type))
cb2a532e 5579 {
6dd53648
RH
5580 tree (*fn)(tree, enum machine_mode, bool);
5581
5582 if (!targetm.valid_pointer_mode (mode))
cb2a532e 5583 {
9e637a26 5584 error ("invalid pointer mode %qs", p);
cb2a532e
AH
5585 return NULL_TREE;
5586 }
5587
c22cacf3 5588 if (TREE_CODE (type) == POINTER_TYPE)
6dd53648 5589 fn = build_pointer_type_for_mode;
4977bab6 5590 else
6dd53648
RH
5591 fn = build_reference_type_for_mode;
5592 typefm = fn (TREE_TYPE (type), mode, false);
cb2a532e 5593 }
6dd53648 5594 else
ab22c1fa
CF
5595 {
5596 /* For fixed-point modes, we need to test if the signness of type
5597 and the machine mode are consistent. */
5598 if (ALL_FIXED_POINT_MODE_P (mode)
5599 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
5600 {
5601 error ("signness of type and machine mode %qs don't match", p);
5602 return NULL_TREE;
5603 }
5604 /* For fixed-point modes, we need to pass saturating info. */
5605 typefm = lang_hooks.types.type_for_mode (mode,
5606 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
5607 : TYPE_UNSIGNED (type));
5608 }
ec8465a5 5609
6dd53648
RH
5610 if (typefm == NULL_TREE)
5611 {
61f03aba 5612 error ("no data type for mode %qs", p);
6dd53648
RH
5613 return NULL_TREE;
5614 }
ec8465a5
RK
5615 else if (TREE_CODE (type) == ENUMERAL_TYPE)
5616 {
5617 /* For enumeral types, copy the precision from the integer
5618 type returned above. If not an INTEGER_TYPE, we can't use
5619 this mode for this type. */
5620 if (TREE_CODE (typefm) != INTEGER_TYPE)
5621 {
61f03aba 5622 error ("cannot use mode %qs for enumeral types", p);
ec8465a5
RK
5623 return NULL_TREE;
5624 }
5625
99db1ef0
RH
5626 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
5627 {
5628 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
5629 typefm = type;
5630 }
5631 else
5632 {
5633 /* We cannot build a type variant, as there's code that assumes
5634 that TYPE_MAIN_VARIANT has the same mode. This includes the
5635 debug generators. Instead, create a subrange type. This
5636 results in all of the enumeral values being emitted only once
5637 in the original, and the subtype gets them by reference. */
5638 if (TYPE_UNSIGNED (type))
5639 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
5640 else
5641 typefm = make_signed_type (TYPE_PRECISION (typefm));
5642 TREE_TYPE (typefm) = type;
5643 }
ec8465a5 5644 }
a2d36602
RH
5645 else if (VECTOR_MODE_P (mode)
5646 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
5647 : TREE_CODE (type) != TREE_CODE (typefm))
61f03aba
RH
5648 {
5649 error ("mode %qs applied to inappropriate type", p);
5650 return NULL_TREE;
5651 }
5652
6dd53648 5653 *node = typefm;
349ae713
NB
5654 }
5655
5656 return NULL_TREE;
5657}
5658
5659/* Handle a "section" attribute; arguments as in
5660 struct attribute_spec.handler. */
5661
5662static tree
e18476eb
BI
5663handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
5664 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
5665{
5666 tree decl = *node;
5667
5668 if (targetm.have_named_sections)
5669 {
9fb32434
CT
5670 user_defined_section_attribute = true;
5671
349ae713
NB
5672 if ((TREE_CODE (decl) == FUNCTION_DECL
5673 || TREE_CODE (decl) == VAR_DECL)
5674 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5675 {
5676 if (TREE_CODE (decl) == VAR_DECL
5677 && current_function_decl != NULL_TREE
3f75a254 5678 && !TREE_STATIC (decl))
349ae713 5679 {
ddd2d57e 5680 error ("%Jsection attribute cannot be specified for "
6de9cd9a 5681 "local variables", decl);
349ae713
NB
5682 *no_add_attrs = true;
5683 }
5684
5685 /* The decl may have already been given a section attribute
5686 from a previous declaration. Ensure they match. */
5687 else if (DECL_SECTION_NAME (decl) != NULL_TREE
5688 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
5689 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
5690 {
dee15844
JM
5691 error ("section of %q+D conflicts with previous declaration",
5692 *node);
349ae713
NB
5693 *no_add_attrs = true;
5694 }
feb60f03
NS
5695 else if (TREE_CODE (decl) == VAR_DECL
5696 && !targetm.have_tls && targetm.emutls.tmpl_section
5697 && DECL_THREAD_LOCAL_P (decl))
5698 {
5699 error ("section of %q+D cannot be overridden", *node);
5700 *no_add_attrs = true;
5701 }
349ae713
NB
5702 else
5703 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
5704 }
5705 else
5706 {
dee15844 5707 error ("section attribute not allowed for %q+D", *node);
349ae713
NB
5708 *no_add_attrs = true;
5709 }
5710 }
5711 else
5712 {
ddd2d57e 5713 error ("%Jsection attributes are not supported for this target", *node);
349ae713
NB
5714 *no_add_attrs = true;
5715 }
5716
5717 return NULL_TREE;
5718}
5719
5720/* Handle a "aligned" attribute; arguments as in
5721 struct attribute_spec.handler. */
5722
5723static tree
e18476eb 5724handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
a742c759 5725 int flags, bool *no_add_attrs)
349ae713
NB
5726{
5727 tree decl = NULL_TREE;
5728 tree *type = NULL;
5729 int is_type = 0;
5730 tree align_expr = (args ? TREE_VALUE (args)
5731 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
5732 int i;
5733
5734 if (DECL_P (*node))
5735 {
5736 decl = *node;
5737 type = &TREE_TYPE (decl);
5738 is_type = TREE_CODE (*node) == TYPE_DECL;
5739 }
5740 else if (TYPE_P (*node))
5741 type = node, is_type = 1;
5742
349ae713
NB
5743 if (TREE_CODE (align_expr) != INTEGER_CST)
5744 {
5745 error ("requested alignment is not a constant");
5746 *no_add_attrs = true;
5747 }
5748 else if ((i = tree_log2 (align_expr)) == -1)
5749 {
5750 error ("requested alignment is not a power of 2");
5751 *no_add_attrs = true;
5752 }
5753 else if (i > HOST_BITS_PER_INT - 2)
5754 {
5755 error ("requested alignment is too large");
5756 *no_add_attrs = true;
5757 }
5758 else if (is_type)
5759 {
5760 /* If we have a TYPE_DECL, then copy the type, so that we
5761 don't accidentally modify a builtin type. See pushdecl. */
5762 if (decl && TREE_TYPE (decl) != error_mark_node
5763 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
5764 {
5765 tree tt = TREE_TYPE (decl);
8dd16ecc 5766 *type = build_variant_type_copy (*type);
349ae713
NB
5767 DECL_ORIGINAL_TYPE (decl) = tt;
5768 TYPE_NAME (*type) = decl;
5769 TREE_USED (*type) = TREE_USED (decl);
5770 TREE_TYPE (decl) = *type;
5771 }
5772 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 5773 *type = build_variant_type_copy (*type);
349ae713
NB
5774
5775 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
5776 TYPE_USER_ALIGN (*type) = 1;
5777 }
837edd5f 5778 else if (! VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
5779 && TREE_CODE (decl) != FIELD_DECL)
5780 {
dee15844 5781 error ("alignment may not be specified for %q+D", decl);
349ae713
NB
5782 *no_add_attrs = true;
5783 }
837edd5f
GK
5784 else if (TREE_CODE (decl) == FUNCTION_DECL
5785 && DECL_ALIGN (decl) > (1 << i) * BITS_PER_UNIT)
5786 {
5787 if (DECL_USER_ALIGN (decl))
5788 error ("alignment for %q+D was previously specified as %d "
5789 "and may not be decreased", decl,
5790 DECL_ALIGN (decl) / BITS_PER_UNIT);
5791 else
5792 error ("alignment for %q+D must be at least %d", decl,
5793 DECL_ALIGN (decl) / BITS_PER_UNIT);
5794 *no_add_attrs = true;
5795 }
349ae713
NB
5796 else
5797 {
5798 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
5799 DECL_USER_ALIGN (decl) = 1;
5800 }
5801
5802 return NULL_TREE;
5803}
5804
5805/* Handle a "weak" attribute; arguments as in
5806 struct attribute_spec.handler. */
5807
5808static tree
55af93a8 5809handle_weak_attribute (tree *node, tree name,
e18476eb
BI
5810 tree ARG_UNUSED (args),
5811 int ARG_UNUSED (flags),
5812 bool * ARG_UNUSED (no_add_attrs))
349ae713 5813{
55af93a8 5814 if (TREE_CODE (*node) == FUNCTION_DECL
6b4e94bc
RG
5815 && DECL_DECLARED_INLINE_P (*node))
5816 {
5817 error ("inline function %q+D cannot be declared weak", *node);
5818 *no_add_attrs = true;
5819 }
5820 else if (TREE_CODE (*node) == FUNCTION_DECL
5821 || TREE_CODE (*node) == VAR_DECL)
55af93a8
DS
5822 declare_weak (*node);
5823 else
5824 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5825
5826 return NULL_TREE;
5827}
5828
5829/* Handle an "alias" attribute; arguments as in
5830 struct attribute_spec.handler. */
5831
5832static tree
35b1a6fa 5833handle_alias_attribute (tree *node, tree name, tree args,
e18476eb 5834 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
5835{
5836 tree decl = *node;
5837
5838 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
a9b0b825
GK
5839 || (TREE_CODE (decl) != FUNCTION_DECL
5840 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
5841 /* A static variable declaration is always a tentative definition,
5842 but the alias is a non-tentative definition which overrides. */
5843 || (TREE_CODE (decl) != FUNCTION_DECL
5844 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
349ae713 5845 {
dee15844 5846 error ("%q+D defined both normally and as an alias", decl);
349ae713
NB
5847 *no_add_attrs = true;
5848 }
f6a76b9f
RH
5849
5850 /* Note that the very first time we process a nested declaration,
5851 decl_function_context will not be set. Indeed, *would* never
5852 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
5853 we do below. After such frobbery, pushdecl would set the context.
5854 In any case, this is never what we want. */
5855 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
349ae713
NB
5856 {
5857 tree id;
5858
5859 id = TREE_VALUE (args);
5860 if (TREE_CODE (id) != STRING_CST)
5861 {
40b97a2e 5862 error ("alias argument not a string");
349ae713
NB
5863 *no_add_attrs = true;
5864 return NULL_TREE;
5865 }
5866 id = get_identifier (TREE_STRING_POINTER (id));
5867 /* This counts as a use of the object pointed to. */
5868 TREE_USED (id) = 1;
5869
5870 if (TREE_CODE (decl) == FUNCTION_DECL)
5871 DECL_INITIAL (decl) = error_mark_node;
5872 else
6de9cd9a 5873 {
a0203ca7
AO
5874 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5875 DECL_EXTERNAL (decl) = 1;
5876 else
5877 DECL_EXTERNAL (decl) = 0;
6de9cd9a
DN
5878 TREE_STATIC (decl) = 1;
5879 }
349ae713
NB
5880 }
5881 else
5882 {
5c498b10 5883 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
5884 *no_add_attrs = true;
5885 }
5886
5887 return NULL_TREE;
5888}
5889
a0203ca7
AO
5890/* Handle a "weakref" attribute; arguments as in struct
5891 attribute_spec.handler. */
5892
5893static tree
5894handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
5895 int flags, bool *no_add_attrs)
5896{
5897 tree attr = NULL_TREE;
5898
e1cf56b1
AO
5899 /* We must ignore the attribute when it is associated with
5900 local-scoped decls, since attribute alias is ignored and many
5901 such symbols do not even have a DECL_WEAK field. */
5902 if (decl_function_context (*node) || current_function_decl)
5903 {
5904 warning (OPT_Wattributes, "%qE attribute ignored", name);
5905 *no_add_attrs = true;
5906 return NULL_TREE;
5907 }
5908
a0203ca7
AO
5909 /* The idea here is that `weakref("name")' mutates into `weakref,
5910 alias("name")', and weakref without arguments, in turn,
5911 implicitly adds weak. */
5912
5913 if (args)
5914 {
5915 attr = tree_cons (get_identifier ("alias"), args, attr);
5916 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
5917
5918 *no_add_attrs = true;
a9b0b825
GK
5919
5920 decl_attributes (node, attr, flags);
a0203ca7
AO
5921 }
5922 else
5923 {
5924 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
5925 error ("%Jweakref attribute must appear before alias attribute",
5926 *node);
5927
a9b0b825
GK
5928 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
5929 and that isn't supported; and because it wants to add it to
5930 the list of weak decls, which isn't helpful. */
5931 DECL_WEAK (*node) = 1;
a0203ca7
AO
5932 }
5933
a0203ca7
AO
5934 return NULL_TREE;
5935}
5936
349ae713
NB
5937/* Handle an "visibility" attribute; arguments as in
5938 struct attribute_spec.handler. */
5939
5940static tree
35b1a6fa 5941handle_visibility_attribute (tree *node, tree name, tree args,
e18476eb 5942 int ARG_UNUSED (flags),
b9e75696 5943 bool *ARG_UNUSED (no_add_attrs))
349ae713
NB
5944{
5945 tree decl = *node;
968b41a1 5946 tree id = TREE_VALUE (args);
b9e75696 5947 enum symbol_visibility vis;
349ae713 5948
d7afec4b
ND
5949 if (TYPE_P (*node))
5950 {
b9e75696
JM
5951 if (TREE_CODE (*node) == ENUMERAL_TYPE)
5952 /* OK */;
5953 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
5954 {
5955 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
5956 name);
5957 return NULL_TREE;
5958 }
5959 else if (TYPE_FIELDS (*node))
5960 {
5961 error ("%qE attribute ignored because %qT is already defined",
5962 name, *node);
5963 return NULL_TREE;
5964 }
d7afec4b 5965 }
3f75a254 5966 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
349ae713 5967 {
5c498b10 5968 warning (OPT_Wattributes, "%qE attribute ignored", name);
968b41a1 5969 return NULL_TREE;
349ae713 5970 }
349ae713 5971
968b41a1
MA
5972 if (TREE_CODE (id) != STRING_CST)
5973 {
40b97a2e 5974 error ("visibility argument not a string");
968b41a1 5975 return NULL_TREE;
349ae713 5976 }
9f63daea 5977
d7afec4b
ND
5978 /* If this is a type, set the visibility on the type decl. */
5979 if (TYPE_P (decl))
5980 {
5981 decl = TYPE_NAME (decl);
3f75a254 5982 if (!decl)
c22cacf3 5983 return NULL_TREE;
e8233ac2
AP
5984 if (TREE_CODE (decl) == IDENTIFIER_NODE)
5985 {
5c498b10 5986 warning (OPT_Wattributes, "%qE attribute ignored on types",
e8233ac2
AP
5987 name);
5988 return NULL_TREE;
5989 }
d7afec4b 5990 }
349ae713 5991
968b41a1 5992 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
b9e75696 5993 vis = VISIBILITY_DEFAULT;
968b41a1 5994 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
b9e75696 5995 vis = VISIBILITY_INTERNAL;
968b41a1 5996 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
b9e75696 5997 vis = VISIBILITY_HIDDEN;
968b41a1 5998 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
b9e75696 5999 vis = VISIBILITY_PROTECTED;
968b41a1 6000 else
b9e75696
JM
6001 {
6002 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
6003 vis = VISIBILITY_DEFAULT;
6004 }
6005
6006 if (DECL_VISIBILITY_SPECIFIED (decl)
3a687f8b
MM
6007 && vis != DECL_VISIBILITY (decl))
6008 {
6009 tree attributes = (TYPE_P (*node)
6010 ? TYPE_ATTRIBUTES (*node)
6011 : DECL_ATTRIBUTES (decl));
6012 if (lookup_attribute ("visibility", attributes))
6013 error ("%qD redeclared with different visibility", decl);
6014 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6015 && lookup_attribute ("dllimport", attributes))
6016 error ("%qD was declared %qs which implies default visibility",
6017 decl, "dllimport");
6018 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6019 && lookup_attribute ("dllexport", attributes))
6020 error ("%qD was declared %qs which implies default visibility",
6021 decl, "dllexport");
6022 }
b9e75696
JM
6023
6024 DECL_VISIBILITY (decl) = vis;
d7afec4b
ND
6025 DECL_VISIBILITY_SPECIFIED (decl) = 1;
6026
b9e75696
JM
6027 /* Go ahead and attach the attribute to the node as well. This is needed
6028 so we can determine whether we have VISIBILITY_DEFAULT because the
6029 visibility was not specified, or because it was explicitly overridden
6030 from the containing scope. */
968b41a1 6031
349ae713
NB
6032 return NULL_TREE;
6033}
6034
b2ca3702
MM
6035/* Determine the ELF symbol visibility for DECL, which is either a
6036 variable or a function. It is an error to use this function if a
6037 definition of DECL is not available in this translation unit.
6038 Returns true if the final visibility has been determined by this
6039 function; false if the caller is free to make additional
6040 modifications. */
6041
6042bool
6043c_determine_visibility (tree decl)
6044{
4094f4d2
NS
6045 gcc_assert (TREE_CODE (decl) == VAR_DECL
6046 || TREE_CODE (decl) == FUNCTION_DECL);
b2ca3702
MM
6047
6048 /* If the user explicitly specified the visibility with an
6049 attribute, honor that. DECL_VISIBILITY will have been set during
6050 the processing of the attribute. We check for an explicit
6051 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
6052 to distinguish the use of an attribute from the use of a "#pragma
6053 GCC visibility push(...)"; in the latter case we still want other
6054 considerations to be able to overrule the #pragma. */
3a687f8b
MM
6055 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
6056 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6057 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
6058 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
b2ca3702
MM
6059 return true;
6060
b9e75696
JM
6061 /* Set default visibility to whatever the user supplied with
6062 visibility_specified depending on #pragma GCC visibility. */
6063 if (!DECL_VISIBILITY_SPECIFIED (decl))
6064 {
6065 DECL_VISIBILITY (decl) = default_visibility;
6066 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
6067 }
b2ca3702
MM
6068 return false;
6069}
6070
dce81a1a
JJ
6071/* Handle an "tls_model" attribute; arguments as in
6072 struct attribute_spec.handler. */
6073
6074static tree
35b1a6fa 6075handle_tls_model_attribute (tree *node, tree name, tree args,
e18476eb 6076 int ARG_UNUSED (flags), bool *no_add_attrs)
dce81a1a 6077{
c2f7fa15 6078 tree id;
dce81a1a 6079 tree decl = *node;
c2f7fa15 6080 enum tls_model kind;
dce81a1a 6081
c2f7fa15
SB
6082 *no_add_attrs = true;
6083
6084 if (!DECL_THREAD_LOCAL_P (decl))
dce81a1a 6085 {
5c498b10 6086 warning (OPT_Wattributes, "%qE attribute ignored", name);
c2f7fa15 6087 return NULL_TREE;
dce81a1a 6088 }
dce81a1a 6089
c2f7fa15
SB
6090 kind = DECL_TLS_MODEL (decl);
6091 id = TREE_VALUE (args);
6092 if (TREE_CODE (id) != STRING_CST)
6093 {
6094 error ("tls_model argument not a string");
6095 return NULL_TREE;
dce81a1a
JJ
6096 }
6097
c2f7fa15
SB
6098 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
6099 kind = TLS_MODEL_LOCAL_EXEC;
6100 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
6101 kind = TLS_MODEL_INITIAL_EXEC;
6102 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
6103 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
6104 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
6105 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6106 else
6107 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
6108
6109 DECL_TLS_MODEL (decl) = kind;
dce81a1a
JJ
6110 return NULL_TREE;
6111}
6112
349ae713
NB
6113/* Handle a "no_instrument_function" attribute; arguments as in
6114 struct attribute_spec.handler. */
6115
6116static tree
35b1a6fa 6117handle_no_instrument_function_attribute (tree *node, tree name,
e18476eb
BI
6118 tree ARG_UNUSED (args),
6119 int ARG_UNUSED (flags),
a742c759 6120 bool *no_add_attrs)
349ae713
NB
6121{
6122 tree decl = *node;
6123
6124 if (TREE_CODE (decl) != FUNCTION_DECL)
6125 {
bda67431 6126 error ("%J%qE attribute applies only to functions", decl, name);
349ae713
NB
6127 *no_add_attrs = true;
6128 }
6129 else if (DECL_INITIAL (decl))
6130 {
bda67431 6131 error ("%Jcan%'t set %qE attribute after definition", decl, name);
349ae713
NB
6132 *no_add_attrs = true;
6133 }
6134 else
6135 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
6136
6137 return NULL_TREE;
6138}
6139
6140/* Handle a "malloc" attribute; arguments as in
6141 struct attribute_spec.handler. */
6142
6143static tree
e18476eb
BI
6144handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6145 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 6146{
3425638a
JM
6147 if (TREE_CODE (*node) == FUNCTION_DECL
6148 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
349ae713 6149 DECL_IS_MALLOC (*node) = 1;
349ae713
NB
6150 else
6151 {
5c498b10 6152 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6153 *no_add_attrs = true;
6154 }
6155
6156 return NULL_TREE;
6157}
6158
51bc54a6
DM
6159/* Handle a "alloc_size" attribute; arguments as in
6160 struct attribute_spec.handler. */
6161
6162static tree
6163handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6164 int ARG_UNUSED (flags), bool *no_add_attrs)
6165{
f3f75f69 6166 unsigned arg_count = type_num_arguments (*node);
51bc54a6
DM
6167 for (; args; args = TREE_CHAIN (args))
6168 {
6169 tree position = TREE_VALUE (args);
6170
6171 if (TREE_CODE (position) != INTEGER_CST
6172 || TREE_INT_CST_HIGH (position)
6173 || TREE_INT_CST_LOW (position) < 1
6174 || TREE_INT_CST_LOW (position) > arg_count )
6175 {
6176 warning (OPT_Wattributes,
6177 "alloc_size parameter outside range");
6178 *no_add_attrs = true;
6179 return NULL_TREE;
6180 }
6181 }
6182 return NULL_TREE;
6183}
6184
6e9a3221
AN
6185/* Handle a "returns_twice" attribute; arguments as in
6186 struct attribute_spec.handler. */
6187
6188static tree
6189handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6190 int ARG_UNUSED (flags), bool *no_add_attrs)
6191{
6192 if (TREE_CODE (*node) == FUNCTION_DECL)
6193 DECL_IS_RETURNS_TWICE (*node) = 1;
6194 else
6195 {
5c498b10 6196 warning (OPT_Wattributes, "%qE attribute ignored", name);
6e9a3221
AN
6197 *no_add_attrs = true;
6198 }
6199
6200 return NULL_TREE;
6201}
6202
349ae713
NB
6203/* Handle a "no_limit_stack" attribute; arguments as in
6204 struct attribute_spec.handler. */
6205
6206static tree
35b1a6fa 6207handle_no_limit_stack_attribute (tree *node, tree name,
e18476eb
BI
6208 tree ARG_UNUSED (args),
6209 int ARG_UNUSED (flags),
a742c759 6210 bool *no_add_attrs)
349ae713
NB
6211{
6212 tree decl = *node;
6213
6214 if (TREE_CODE (decl) != FUNCTION_DECL)
6215 {
bda67431 6216 error ("%J%qE attribute applies only to functions", decl, name);
349ae713
NB
6217 *no_add_attrs = true;
6218 }
6219 else if (DECL_INITIAL (decl))
6220 {
bda67431 6221 error ("%Jcan%'t set %qE attribute after definition", decl, name);
349ae713
NB
6222 *no_add_attrs = true;
6223 }
6224 else
6225 DECL_NO_LIMIT_STACK (decl) = 1;
6226
6227 return NULL_TREE;
6228}
6229
6230/* Handle a "pure" attribute; arguments as in
6231 struct attribute_spec.handler. */
6232
6233static tree
e18476eb
BI
6234handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6235 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6236{
6237 if (TREE_CODE (*node) == FUNCTION_DECL)
becfd6e5 6238 DECL_PURE_P (*node) = 1;
349ae713
NB
6239 /* ??? TODO: Support types. */
6240 else
6241 {
5c498b10 6242 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6243 *no_add_attrs = true;
6244 }
6245
6246 return NULL_TREE;
6247}
6248
dcd6de6d
ZD
6249/* Handle a "no vops" attribute; arguments as in
6250 struct attribute_spec.handler. */
6251
6252static tree
6253handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
6254 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
6255 bool *ARG_UNUSED (no_add_attrs))
6256{
6257 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
6258 DECL_IS_NOVOPS (*node) = 1;
6259 return NULL_TREE;
6260}
6261
349ae713
NB
6262/* Handle a "deprecated" attribute; arguments as in
6263 struct attribute_spec.handler. */
35b1a6fa 6264
349ae713 6265static tree
35b1a6fa 6266handle_deprecated_attribute (tree *node, tree name,
e18476eb 6267 tree ARG_UNUSED (args), int flags,
a742c759 6268 bool *no_add_attrs)
349ae713
NB
6269{
6270 tree type = NULL_TREE;
6271 int warn = 0;
c51a1ba9 6272 tree what = NULL_TREE;
35b1a6fa 6273
349ae713
NB
6274 if (DECL_P (*node))
6275 {
6276 tree decl = *node;
6277 type = TREE_TYPE (decl);
35b1a6fa 6278
349ae713
NB
6279 if (TREE_CODE (decl) == TYPE_DECL
6280 || TREE_CODE (decl) == PARM_DECL
6281 || TREE_CODE (decl) == VAR_DECL
6282 || TREE_CODE (decl) == FUNCTION_DECL
6283 || TREE_CODE (decl) == FIELD_DECL)
6284 TREE_DEPRECATED (decl) = 1;
6285 else
6286 warn = 1;
6287 }
6288 else if (TYPE_P (*node))
6289 {
6290 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 6291 *node = build_variant_type_copy (*node);
349ae713
NB
6292 TREE_DEPRECATED (*node) = 1;
6293 type = *node;
6294 }
6295 else
6296 warn = 1;
35b1a6fa 6297
349ae713
NB
6298 if (warn)
6299 {
6300 *no_add_attrs = true;
6301 if (type && TYPE_NAME (type))
6302 {
6303 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9 6304 what = TYPE_NAME (*node);
349ae713
NB
6305 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6306 && DECL_NAME (TYPE_NAME (type)))
c51a1ba9 6307 what = DECL_NAME (TYPE_NAME (type));
349ae713
NB
6308 }
6309 if (what)
5c498b10 6310 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
349ae713 6311 else
5c498b10 6312 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6313 }
6314
6315 return NULL_TREE;
6316}
6317
349ae713
NB
6318/* Handle a "vector_size" attribute; arguments as in
6319 struct attribute_spec.handler. */
6320
6321static tree
35b1a6fa 6322handle_vector_size_attribute (tree *node, tree name, tree args,
e18476eb 6323 int ARG_UNUSED (flags),
a742c759 6324 bool *no_add_attrs)
349ae713
NB
6325{
6326 unsigned HOST_WIDE_INT vecsize, nunits;
26277d41 6327 enum machine_mode orig_mode;
4a5eab38 6328 tree type = *node, new_type, size;
349ae713
NB
6329
6330 *no_add_attrs = true;
6331
4a5eab38 6332 size = TREE_VALUE (args);
4a5eab38 6333
3f75a254 6334 if (!host_integerp (size, 1))
349ae713 6335 {
5c498b10 6336 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6337 return NULL_TREE;
6338 }
6339
6340 /* Get the vector size (in bytes). */
4a5eab38 6341 vecsize = tree_low_cst (size, 1);
349ae713
NB
6342
6343 /* We need to provide for vector pointers, vector arrays, and
6344 functions returning vectors. For example:
6345
6346 __attribute__((vector_size(16))) short *foo;
6347
6348 In this case, the mode is SI, but the type being modified is
6349 HI, so we need to look further. */
6350
6351 while (POINTER_TYPE_P (type)
6352 || TREE_CODE (type) == FUNCTION_TYPE
43dc123f 6353 || TREE_CODE (type) == METHOD_TYPE
270e749d
JJ
6354 || TREE_CODE (type) == ARRAY_TYPE
6355 || TREE_CODE (type) == OFFSET_TYPE)
349ae713
NB
6356 type = TREE_TYPE (type);
6357
6358 /* Get the mode of the type being modified. */
6359 orig_mode = TYPE_MODE (type);
6360
270e749d
JJ
6361 if ((!INTEGRAL_TYPE_P (type)
6362 && !SCALAR_FLOAT_TYPE_P (type)
6363 && !FIXED_POINT_TYPE_P (type))
3d8bf70f 6364 || (!SCALAR_FLOAT_MODE_P (orig_mode)
ab22c1fa
CF
6365 && GET_MODE_CLASS (orig_mode) != MODE_INT
6366 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
3f75a254 6367 || !host_integerp (TYPE_SIZE_UNIT (type), 1))
349ae713 6368 {
c51a1ba9 6369 error ("invalid vector type for attribute %qE", name);
349ae713
NB
6370 return NULL_TREE;
6371 }
6372
ee8960e5
JJ
6373 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
6374 {
6375 error ("vector size not an integral multiple of component size");
6376 return NULL;
6377 }
6378
6379 if (vecsize == 0)
6380 {
6381 error ("zero vector size");
6382 return NULL;
6383 }
6384
349ae713
NB
6385 /* Calculate how many units fit in the vector. */
6386 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
26277d41 6387 if (nunits & (nunits - 1))
349ae713 6388 {
26277d41 6389 error ("number of components of the vector not a power of two");
349ae713
NB
6390 return NULL_TREE;
6391 }
6392
26277d41 6393 new_type = build_vector_type (type, nunits);
349ae713
NB
6394
6395 /* Build back pointers if needed. */
5dc11954 6396 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
349ae713
NB
6397
6398 return NULL_TREE;
6399}
6400
b34c7881
JT
6401/* Handle the "nonnull" attribute. */
6402static tree
e18476eb
BI
6403handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
6404 tree args, int ARG_UNUSED (flags),
a742c759 6405 bool *no_add_attrs)
b34c7881
JT
6406{
6407 tree type = *node;
6408 unsigned HOST_WIDE_INT attr_arg_num;
6409
6410 /* If no arguments are specified, all pointer arguments should be
95bd1dd7 6411 non-null. Verify a full prototype is given so that the arguments
b34c7881 6412 will have the correct types when we actually check them later. */
3f75a254 6413 if (!args)
b34c7881 6414 {
3f75a254 6415 if (!TYPE_ARG_TYPES (type))
b34c7881
JT
6416 {
6417 error ("nonnull attribute without arguments on a non-prototype");
6de9cd9a 6418 *no_add_attrs = true;
b34c7881
JT
6419 }
6420 return NULL_TREE;
6421 }
6422
6423 /* Argument list specified. Verify that each argument number references
6424 a pointer argument. */
6425 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
6426 {
6427 tree argument;
6de9cd9a 6428 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
b34c7881 6429
3f75a254 6430 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
b34c7881 6431 {
40b97a2e 6432 error ("nonnull argument has invalid operand number (argument %lu)",
b34c7881
JT
6433 (unsigned long) attr_arg_num);
6434 *no_add_attrs = true;
6435 return NULL_TREE;
6436 }
6437
6438 argument = TYPE_ARG_TYPES (type);
6439 if (argument)
6440 {
6441 for (ck_num = 1; ; ck_num++)
6442 {
3f75a254 6443 if (!argument || ck_num == arg_num)
b34c7881
JT
6444 break;
6445 argument = TREE_CHAIN (argument);
6446 }
6447
3f75a254 6448 if (!argument
b34c7881
JT
6449 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
6450 {
40b97a2e 6451 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
b34c7881
JT
6452 (unsigned long) attr_arg_num, (unsigned long) arg_num);
6453 *no_add_attrs = true;
6454 return NULL_TREE;
6455 }
6456
6de9cd9a 6457 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
b34c7881 6458 {
40b97a2e 6459 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
b34c7881
JT
6460 (unsigned long) attr_arg_num, (unsigned long) arg_num);
6461 *no_add_attrs = true;
6462 return NULL_TREE;
6463 }
6464 }
6465 }
6466
6467 return NULL_TREE;
6468}
6469
6470/* Check the argument list of a function call for null in argument slots
94a0dd7b
SL
6471 that are marked as requiring a non-null pointer argument. The NARGS
6472 arguments are passed in the array ARGARRAY.
6473*/
b34c7881
JT
6474
6475static void
94a0dd7b 6476check_function_nonnull (tree attrs, int nargs, tree *argarray)
b34c7881 6477{
94a0dd7b
SL
6478 tree a, args;
6479 int i;
b34c7881
JT
6480
6481 for (a = attrs; a; a = TREE_CHAIN (a))
6482 {
6483 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
6484 {
6de9cd9a 6485 args = TREE_VALUE (a);
b34c7881 6486
6de9cd9a
DN
6487 /* Walk the argument list. If we encounter an argument number we
6488 should check for non-null, do it. If the attribute has no args,
6489 then every pointer argument is checked (in which case the check
b34c7881 6490 for pointer type is done in check_nonnull_arg). */
94a0dd7b 6491 for (i = 0; i < nargs; i++)
6de9cd9a 6492 {
94a0dd7b 6493 if (!args || nonnull_check_p (args, i + 1))
dfd0a3de 6494 check_function_arguments_recurse (check_nonnull_arg, NULL,
94a0dd7b
SL
6495 argarray[i],
6496 i + 1);
6de9cd9a 6497 }
b34c7881
JT
6498 }
6499 }
6500}
6501
254986c7 6502/* Check that the Nth argument of a function call (counting backwards
94a0dd7b
SL
6503 from the end) is a (pointer)0. The NARGS arguments are passed in the
6504 array ARGARRAY. */
3d091dac
KG
6505
6506static void
94a0dd7b 6507check_function_sentinel (tree attrs, int nargs, tree *argarray, tree typelist)
3d091dac
KG
6508{
6509 tree attr = lookup_attribute ("sentinel", attrs);
6510
6511 if (attr)
6512 {
94a0dd7b
SL
6513 int len = 0;
6514 int pos = 0;
6515 tree sentinel;
c22cacf3 6516
94a0dd7b
SL
6517 /* Skip over the named arguments. */
6518 while (typelist && len < nargs)
c22cacf3 6519 {
94a0dd7b
SL
6520 typelist = TREE_CHAIN (typelist);
6521 len++;
6522 }
254986c7 6523
94a0dd7b
SL
6524 if (TREE_VALUE (attr))
6525 {
6526 tree p = TREE_VALUE (TREE_VALUE (attr));
6527 pos = TREE_INT_CST_LOW (p);
6528 }
254986c7 6529
94a0dd7b
SL
6530 /* The sentinel must be one of the varargs, i.e.
6531 in position >= the number of fixed arguments. */
6532 if ((nargs - 1 - pos) < len)
6533 {
6534 warning (OPT_Wformat,
6535 "not enough variable arguments to fit a sentinel");
6536 return;
3d091dac 6537 }
94a0dd7b
SL
6538
6539 /* Validate the sentinel. */
6540 sentinel = argarray[nargs - 1 - pos];
6541 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
6542 || !integer_zerop (sentinel))
6543 /* Although __null (in C++) is only an integer we allow it
6544 nevertheless, as we are guaranteed that it's exactly
6545 as wide as a pointer, and we don't want to force
6546 users to cast the NULL they have written there.
6547 We warn with -Wstrict-null-sentinel, though. */
6548 && (warn_strict_null_sentinel || null_node != sentinel))
6549 warning (OPT_Wformat, "missing sentinel in function call");
3d091dac
KG
6550 }
6551}
6552
b34c7881
JT
6553/* Helper for check_function_nonnull; given a list of operands which
6554 must be non-null in ARGS, determine if operand PARAM_NUM should be
6555 checked. */
6556
6557static bool
35b1a6fa 6558nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
b34c7881 6559{
6de9cd9a 6560 unsigned HOST_WIDE_INT arg_num = 0;
b34c7881
JT
6561
6562 for (; args; args = TREE_CHAIN (args))
6563 {
366de0ce
NS
6564 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
6565
6566 gcc_assert (found);
b34c7881
JT
6567
6568 if (arg_num == param_num)
6569 return true;
6570 }
6571 return false;
6572}
6573
6574/* Check that the function argument PARAM (which is operand number
6575 PARAM_NUM) is non-null. This is called by check_function_nonnull
6576 via check_function_arguments_recurse. */
6577
6578static void
e18476eb 6579check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
35b1a6fa 6580 unsigned HOST_WIDE_INT param_num)
b34c7881
JT
6581{
6582 /* Just skip checking the argument if it's not a pointer. This can
6583 happen if the "nonnull" attribute was given without an operand
6584 list (which means to check every pointer argument). */
6585
6586 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
6587 return;
6588
6589 if (integer_zerop (param))
30480ec4
DD
6590 warning (OPT_Wnonnull, "null argument where non-null required "
6591 "(argument %lu)", (unsigned long) param_num);
b34c7881
JT
6592}
6593
6594/* Helper for nonnull attribute handling; fetch the operand number
6595 from the attribute argument list. */
6596
6597static bool
35b1a6fa 6598get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
b34c7881 6599{
f6d1c3a6 6600 /* Verify the arg number is a constant. */
b34c7881
JT
6601 if (TREE_CODE (arg_num_expr) != INTEGER_CST
6602 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
6603 return false;
6604
6605 *valp = TREE_INT_CST_LOW (arg_num_expr);
6606 return true;
6607}
39f2f3c8
RS
6608
6609/* Handle a "nothrow" attribute; arguments as in
6610 struct attribute_spec.handler. */
6611
6612static tree
e18476eb
BI
6613handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6614 int ARG_UNUSED (flags), bool *no_add_attrs)
39f2f3c8
RS
6615{
6616 if (TREE_CODE (*node) == FUNCTION_DECL)
6617 TREE_NOTHROW (*node) = 1;
6618 /* ??? TODO: Support types. */
6619 else
6620 {
5c498b10 6621 warning (OPT_Wattributes, "%qE attribute ignored", name);
39f2f3c8
RS
6622 *no_add_attrs = true;
6623 }
6624
6625 return NULL_TREE;
6626}
0bfa5f65
RH
6627
6628/* Handle a "cleanup" attribute; arguments as in
6629 struct attribute_spec.handler. */
6630
6631static tree
35b1a6fa 6632handle_cleanup_attribute (tree *node, tree name, tree args,
e18476eb 6633 int ARG_UNUSED (flags), bool *no_add_attrs)
0bfa5f65
RH
6634{
6635 tree decl = *node;
6636 tree cleanup_id, cleanup_decl;
6637
6638 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
6639 for global destructors in C++. This requires infrastructure that
6640 we don't have generically at the moment. It's also not a feature
6641 we'd be missing too much, since we do have attribute constructor. */
6642 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
6643 {
5c498b10 6644 warning (OPT_Wattributes, "%qE attribute ignored", name);
0bfa5f65
RH
6645 *no_add_attrs = true;
6646 return NULL_TREE;
6647 }
6648
6649 /* Verify that the argument is a function in scope. */
6650 /* ??? We could support pointers to functions here as well, if
6651 that was considered desirable. */
6652 cleanup_id = TREE_VALUE (args);
6653 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
6654 {
40b97a2e 6655 error ("cleanup argument not an identifier");
0bfa5f65
RH
6656 *no_add_attrs = true;
6657 return NULL_TREE;
6658 }
10e6657a 6659 cleanup_decl = lookup_name (cleanup_id);
0bfa5f65
RH
6660 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
6661 {
40b97a2e 6662 error ("cleanup argument not a function");
0bfa5f65
RH
6663 *no_add_attrs = true;
6664 return NULL_TREE;
6665 }
6666
35b1a6fa 6667 /* That the function has proper type is checked with the
0bfa5f65
RH
6668 eventual call to build_function_call. */
6669
6670 return NULL_TREE;
6671}
72954a4f
JM
6672
6673/* Handle a "warn_unused_result" attribute. No special handling. */
6674
6675static tree
6676handle_warn_unused_result_attribute (tree *node, tree name,
e18476eb
BI
6677 tree ARG_UNUSED (args),
6678 int ARG_UNUSED (flags), bool *no_add_attrs)
72954a4f
JM
6679{
6680 /* Ignore the attribute for functions not returning any value. */
6681 if (VOID_TYPE_P (TREE_TYPE (*node)))
6682 {
5c498b10 6683 warning (OPT_Wattributes, "%qE attribute ignored", name);
72954a4f
JM
6684 *no_add_attrs = true;
6685 }
6686
6687 return NULL_TREE;
6688}
3d091dac
KG
6689
6690/* Handle a "sentinel" attribute. */
6691
6692static tree
254986c7 6693handle_sentinel_attribute (tree *node, tree name, tree args,
3d091dac
KG
6694 int ARG_UNUSED (flags), bool *no_add_attrs)
6695{
6696 tree params = TYPE_ARG_TYPES (*node);
6697
6698 if (!params)
6699 {
5c498b10
DD
6700 warning (OPT_Wattributes,
6701 "%qE attribute requires prototypes with named arguments", name);
3d091dac 6702 *no_add_attrs = true;
3d091dac 6703 }
254986c7
KG
6704 else
6705 {
6706 while (TREE_CHAIN (params))
6707 params = TREE_CHAIN (params);
3d091dac 6708
254986c7 6709 if (VOID_TYPE_P (TREE_VALUE (params)))
c22cacf3 6710 {
5c498b10
DD
6711 warning (OPT_Wattributes,
6712 "%qE attribute only applies to variadic functions", name);
254986c7
KG
6713 *no_add_attrs = true;
6714 }
6715 }
c22cacf3 6716
254986c7 6717 if (args)
3d091dac 6718 {
254986c7
KG
6719 tree position = TREE_VALUE (args);
6720
254986c7 6721 if (TREE_CODE (position) != INTEGER_CST)
c22cacf3 6722 {
aa86a51b
DM
6723 warning (OPT_Wattributes,
6724 "requested position is not an integer constant");
254986c7
KG
6725 *no_add_attrs = true;
6726 }
6727 else
c22cacf3 6728 {
254986c7
KG
6729 if (tree_int_cst_lt (position, integer_zero_node))
6730 {
aa86a51b
DM
6731 warning (OPT_Wattributes,
6732 "requested position is less than zero");
254986c7
KG
6733 *no_add_attrs = true;
6734 }
6735 }
3d091dac 6736 }
c22cacf3 6737
3d091dac
KG
6738 return NULL_TREE;
6739}
b5d32c25
KG
6740
6741/* Handle a "type_generic" attribute. */
6742
6743static tree
6744handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
6745 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
6746 bool * ARG_UNUSED (no_add_attrs))
6747{
3bf5906b
KG
6748 tree params;
6749
6750 /* Ensure we have a function type. */
6751 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
6752
6753 params = TYPE_ARG_TYPES (*node);
6754 while (params && ! VOID_TYPE_P (TREE_VALUE (params)))
6755 params = TREE_CHAIN (params);
6756
6757 /* Ensure we have a variadic function. */
6758 gcc_assert (!params);
b5d32c25
KG
6759
6760 return NULL_TREE;
6761}
b34c7881 6762\f
94a0dd7b
SL
6763/* Check for valid arguments being passed to a function.
6764 ATTRS is a list of attributes. There are NARGS arguments in the array
6765 ARGARRAY. TYPELIST is the list of argument types for the function.
6766 */
b34c7881 6767void
94a0dd7b 6768check_function_arguments (tree attrs, int nargs, tree *argarray, tree typelist)
b34c7881
JT
6769{
6770 /* Check for null being passed in a pointer argument that must be
6771 non-null. We also need to do this if format checking is enabled. */
6772
6773 if (warn_nonnull)
94a0dd7b 6774 check_function_nonnull (attrs, nargs, argarray);
b34c7881
JT
6775
6776 /* Check for errors in format strings. */
6777
7876a414 6778 if (warn_format || warn_missing_format_attribute)
94a0dd7b 6779 check_function_format (attrs, nargs, argarray);
7876a414
KG
6780
6781 if (warn_format)
94a0dd7b 6782 check_function_sentinel (attrs, nargs, argarray, typelist);
b34c7881
JT
6783}
6784
6785/* Generic argument checking recursion routine. PARAM is the argument to
6786 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
6787 once the argument is resolved. CTX is context for the callback. */
6788void
35b1a6fa
AJ
6789check_function_arguments_recurse (void (*callback)
6790 (void *, tree, unsigned HOST_WIDE_INT),
6791 void *ctx, tree param,
6792 unsigned HOST_WIDE_INT param_num)
b34c7881 6793{
1043771b 6794 if (CONVERT_EXPR_P (param)
1344f9a3
JM
6795 && (TYPE_PRECISION (TREE_TYPE (param))
6796 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
b34c7881
JT
6797 {
6798 /* Strip coercion. */
6799 check_function_arguments_recurse (callback, ctx,
6de9cd9a 6800 TREE_OPERAND (param, 0), param_num);
b34c7881
JT
6801 return;
6802 }
6803
6804 if (TREE_CODE (param) == CALL_EXPR)
6805 {
5039610b 6806 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
b34c7881
JT
6807 tree attrs;
6808 bool found_format_arg = false;
6809
6810 /* See if this is a call to a known internationalization function
6811 that modifies a format arg. Such a function may have multiple
6812 format_arg attributes (for example, ngettext). */
6813
6814 for (attrs = TYPE_ATTRIBUTES (type);
6815 attrs;
6816 attrs = TREE_CHAIN (attrs))
6817 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
6818 {
5039610b 6819 tree inner_arg;
b34c7881
JT
6820 tree format_num_expr;
6821 int format_num;
6822 int i;
5039610b 6823 call_expr_arg_iterator iter;
b34c7881
JT
6824
6825 /* Extract the argument number, which was previously checked
6826 to be valid. */
6827 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
b34c7881 6828
366de0ce
NS
6829 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
6830 && !TREE_INT_CST_HIGH (format_num_expr));
b34c7881
JT
6831
6832 format_num = TREE_INT_CST_LOW (format_num_expr);
6833
5039610b
SL
6834 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
6835 inner_arg != 0;
6836 inner_arg = next_call_expr_arg (&iter), i++)
b34c7881
JT
6837 if (i == format_num)
6838 {
6839 check_function_arguments_recurse (callback, ctx,
5039610b 6840 inner_arg, param_num);
b34c7881
JT
6841 found_format_arg = true;
6842 break;
6843 }
6844 }
6845
6846 /* If we found a format_arg attribute and did a recursive check,
6847 we are done with checking this argument. Otherwise, we continue
6848 and this will be considered a non-literal. */
6849 if (found_format_arg)
6850 return;
6851 }
6852
6853 if (TREE_CODE (param) == COND_EXPR)
6854 {
6855 /* Check both halves of the conditional expression. */
6856 check_function_arguments_recurse (callback, ctx,
6de9cd9a 6857 TREE_OPERAND (param, 1), param_num);
b34c7881 6858 check_function_arguments_recurse (callback, ctx,
6de9cd9a 6859 TREE_OPERAND (param, 2), param_num);
b34c7881
JT
6860 return;
6861 }
6862
6863 (*callback) (ctx, param, param_num);
6864}
e2500fed 6865
83322951
RG
6866/* Checks the number of arguments NARGS against the required number
6867 REQUIRED and issues an error if there is a mismatch. Returns true
6868 if the number of arguments is correct, otherwise false. */
6869
6870static bool
6871validate_nargs (tree fndecl, int nargs, int required)
6872{
6873 if (nargs < required)
6874 {
6875 error ("not enough arguments to function %qE", fndecl);
6876 return false;
6877 }
6878 else if (nargs > required)
6879 {
6880 error ("too many arguments to function %qE", fndecl);
6881 return false;
6882 }
6883 return true;
6884}
6885
6886/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
6887 Returns false if there was an error, otherwise true. */
6888
6889bool
6890check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
6891{
6892 if (!DECL_BUILT_IN (fndecl)
6893 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
6894 return true;
6895
6896 switch (DECL_FUNCTION_CODE (fndecl))
6897 {
6898 case BUILT_IN_CONSTANT_P:
6899 return validate_nargs (fndecl, nargs, 1);
6900
6901 case BUILT_IN_ISFINITE:
6902 case BUILT_IN_ISINF:
05f41289 6903 case BUILT_IN_ISINF_SIGN:
83322951
RG
6904 case BUILT_IN_ISNAN:
6905 case BUILT_IN_ISNORMAL:
6906 if (validate_nargs (fndecl, nargs, 1))
6907 {
6908 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
6909 {
6910 error ("non-floating-point argument in call to "
6911 "function %qE", fndecl);
6912 return false;
6913 }
6914 return true;
6915 }
6916 return false;
6917
6918 case BUILT_IN_ISGREATER:
6919 case BUILT_IN_ISGREATEREQUAL:
6920 case BUILT_IN_ISLESS:
6921 case BUILT_IN_ISLESSEQUAL:
6922 case BUILT_IN_ISLESSGREATER:
6923 case BUILT_IN_ISUNORDERED:
6924 if (validate_nargs (fndecl, nargs, 2))
6925 {
6926 enum tree_code code0, code1;
6927 code0 = TREE_CODE (TREE_TYPE (args[0]));
6928 code1 = TREE_CODE (TREE_TYPE (args[1]));
6929 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
6930 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
6931 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
6932 {
6933 error ("non-floating-point arguments in call to "
6934 "function %qE", fndecl);
6935 return false;
6936 }
6937 return true;
6938 }
6939 return false;
6940
3bf5906b
KG
6941 case BUILT_IN_FPCLASSIFY:
6942 if (validate_nargs (fndecl, nargs, 6))
6943 {
6944 unsigned i;
6945
6946 for (i=0; i<5; i++)
6947 if (TREE_CODE (args[i]) != INTEGER_CST)
6948 {
6949 error ("non-const integer argument %u in call to function %qE",
6950 i+1, fndecl);
6951 return false;
6952 }
6953
6954 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
6955 {
6956 error ("non-floating-point argument in call to function %qE",
6957 fndecl);
6958 return false;
6959 }
6960 return true;
6961 }
6962 return false;
6963
83322951
RG
6964 default:
6965 return true;
6966 }
6967}
6968
d07605f5
AP
6969/* Function to help qsort sort FIELD_DECLs by name order. */
6970
6971int
6972field_decl_cmp (const void *x_p, const void *y_p)
6973{
28dab132
BI
6974 const tree *const x = (const tree *const) x_p;
6975 const tree *const y = (const tree *const) y_p;
6976
d07605f5
AP
6977 if (DECL_NAME (*x) == DECL_NAME (*y))
6978 /* A nontype is "greater" than a type. */
6979 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
6980 if (DECL_NAME (*x) == NULL_TREE)
6981 return -1;
6982 if (DECL_NAME (*y) == NULL_TREE)
6983 return 1;
6984 if (DECL_NAME (*x) < DECL_NAME (*y))
6985 return -1;
6986 return 1;
6987}
6988
6989static struct {
6990 gt_pointer_operator new_value;
6991 void *cookie;
6992} resort_data;
6993
6994/* This routine compares two fields like field_decl_cmp but using the
6995pointer operator in resort_data. */
6996
6997static int
6998resort_field_decl_cmp (const void *x_p, const void *y_p)
6999{
28dab132
BI
7000 const tree *const x = (const tree *const) x_p;
7001 const tree *const y = (const tree *const) y_p;
d07605f5
AP
7002
7003 if (DECL_NAME (*x) == DECL_NAME (*y))
7004 /* A nontype is "greater" than a type. */
7005 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
7006 if (DECL_NAME (*x) == NULL_TREE)
7007 return -1;
7008 if (DECL_NAME (*y) == NULL_TREE)
7009 return 1;
7010 {
7011 tree d1 = DECL_NAME (*x);
7012 tree d2 = DECL_NAME (*y);
7013 resort_data.new_value (&d1, resort_data.cookie);
7014 resort_data.new_value (&d2, resort_data.cookie);
7015 if (d1 < d2)
7016 return -1;
7017 }
7018 return 1;
7019}
7020
7021/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
7022
7023void
7024resort_sorted_fields (void *obj,
e18476eb 7025 void * ARG_UNUSED (orig_obj),
6de9cd9a
DN
7026 gt_pointer_operator new_value,
7027 void *cookie)
d07605f5 7028{
e18476eb 7029 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
d07605f5
AP
7030 resort_data.new_value = new_value;
7031 resort_data.cookie = cookie;
7032 qsort (&sf->elts[0], sf->len, sizeof (tree),
6de9cd9a 7033 resort_field_decl_cmp);
d07605f5
AP
7034}
7035
0a3ee0fd
GDR
7036/* Subroutine of c_parse_error.
7037 Return the result of concatenating LHS and RHS. RHS is really
7038 a string literal, its first character is indicated by RHS_START and
3292fb42 7039 RHS_SIZE is its length (including the terminating NUL character).
0a3ee0fd
GDR
7040
7041 The caller is responsible for deleting the returned pointer. */
7042
7043static char *
7044catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
7045{
7046 const int lhs_size = strlen (lhs);
7047 char *result = XNEWVEC (char, lhs_size + rhs_size);
7048 strncpy (result, lhs, lhs_size);
7049 strncpy (result + lhs_size, rhs_start, rhs_size);
7050 return result;
7051}
7052
4b794eaf 7053/* Issue the error given by GMSGID, indicating that it occurred before
4bb8ca28
MM
7054 TOKEN, which had the associated VALUE. */
7055
7056void
4b794eaf 7057c_parse_error (const char *gmsgid, enum cpp_ttype token, tree value)
4bb8ca28 7058{
0a3ee0fd
GDR
7059#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
7060
7061 char *message = NULL;
4bb8ca28
MM
7062
7063 if (token == CPP_EOF)
4b794eaf 7064 message = catenate_messages (gmsgid, " at end of input");
b6baa67d
KVH
7065 else if (token == CPP_CHAR || token == CPP_WCHAR || token == CPP_CHAR16
7066 || token == CPP_CHAR32)
4bb8ca28
MM
7067 {
7068 unsigned int val = TREE_INT_CST_LOW (value);
b6baa67d
KVH
7069 const char *prefix;
7070
7071 switch (token)
7072 {
7073 default:
7074 prefix = "";
7075 break;
7076 case CPP_WCHAR:
7077 prefix = "L";
7078 break;
7079 case CPP_CHAR16:
7080 prefix = "u";
7081 break;
7082 case CPP_CHAR32:
7083 prefix = "U";
7084 break;
7085 }
7086
4bb8ca28 7087 if (val <= UCHAR_MAX && ISGRAPH (val))
c22cacf3 7088 message = catenate_messages (gmsgid, " before %s'%c'");
4bb8ca28 7089 else
c22cacf3 7090 message = catenate_messages (gmsgid, " before %s'\\x%x'");
0a3ee0fd 7091
b6baa67d 7092 error (message, prefix, val);
0a3ee0fd
GDR
7093 free (message);
7094 message = NULL;
4bb8ca28 7095 }
b6baa67d
KVH
7096 else if (token == CPP_STRING || token == CPP_WSTRING || token == CPP_STRING16
7097 || token == CPP_STRING32)
4b794eaf 7098 message = catenate_messages (gmsgid, " before string constant");
4bb8ca28 7099 else if (token == CPP_NUMBER)
4b794eaf 7100 message = catenate_messages (gmsgid, " before numeric constant");
4bb8ca28 7101 else if (token == CPP_NAME)
0a3ee0fd 7102 {
4b794eaf 7103 message = catenate_messages (gmsgid, " before %qE");
c51a1ba9 7104 error (message, value);
0a3ee0fd
GDR
7105 free (message);
7106 message = NULL;
7107 }
bc4071dd
RH
7108 else if (token == CPP_PRAGMA)
7109 message = catenate_messages (gmsgid, " before %<#pragma%>");
7110 else if (token == CPP_PRAGMA_EOL)
7111 message = catenate_messages (gmsgid, " before end of line");
4bb8ca28 7112 else if (token < N_TTYPES)
0a3ee0fd 7113 {
4b794eaf 7114 message = catenate_messages (gmsgid, " before %qs token");
0a3ee0fd
GDR
7115 error (message, cpp_type2name (token));
7116 free (message);
7117 message = NULL;
7118 }
4bb8ca28 7119 else
4b794eaf 7120 error (gmsgid);
0a3ee0fd
GDR
7121
7122 if (message)
7123 {
7124 error (message);
7125 free (message);
7126 }
c22cacf3 7127#undef catenate_messages
4bb8ca28
MM
7128}
7129
6de9cd9a
DN
7130/* Walk a gimplified function and warn for functions whose return value is
7131 ignored and attribute((warn_unused_result)) is set. This is done before
9f63daea
EC
7132 inlining, so we don't have to worry about that. */
7133
6de9cd9a
DN
7134void
7135c_warn_unused_result (tree *top_p)
7136{
7137 tree t = *top_p;
7138 tree_stmt_iterator i;
7139 tree fdecl, ftype;
7140
7141 switch (TREE_CODE (t))
7142 {
7143 case STATEMENT_LIST:
7144 for (i = tsi_start (*top_p); !tsi_end_p (i); tsi_next (&i))
7145 c_warn_unused_result (tsi_stmt_ptr (i));
7146 break;
7147
7148 case COND_EXPR:
7149 c_warn_unused_result (&COND_EXPR_THEN (t));
7150 c_warn_unused_result (&COND_EXPR_ELSE (t));
7151 break;
7152 case BIND_EXPR:
7153 c_warn_unused_result (&BIND_EXPR_BODY (t));
7154 break;
7155 case TRY_FINALLY_EXPR:
7156 case TRY_CATCH_EXPR:
7157 c_warn_unused_result (&TREE_OPERAND (t, 0));
7158 c_warn_unused_result (&TREE_OPERAND (t, 1));
7159 break;
7160 case CATCH_EXPR:
7161 c_warn_unused_result (&CATCH_BODY (t));
7162 break;
7163 case EH_FILTER_EXPR:
7164 c_warn_unused_result (&EH_FILTER_FAILURE (t));
7165 break;
7166
7167 case CALL_EXPR:
5201931e
JM
7168 if (TREE_USED (t))
7169 break;
7170
6de9cd9a
DN
7171 /* This is a naked call, as opposed to a CALL_EXPR nested inside
7172 a MODIFY_EXPR. All calls whose value is ignored should be
7173 represented like this. Look for the attribute. */
7174 fdecl = get_callee_fndecl (t);
7175 if (fdecl)
7176 ftype = TREE_TYPE (fdecl);
7177 else
7178 {
5039610b 7179 ftype = TREE_TYPE (CALL_EXPR_FN (t));
6de9cd9a
DN
7180 /* Look past pointer-to-function to the function type itself. */
7181 ftype = TREE_TYPE (ftype);
7182 }
7183
7184 if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
7185 {
7186 if (fdecl)
d4ee4d25 7187 warning (0, "%Hignoring return value of %qD, "
6de9cd9a
DN
7188 "declared with attribute warn_unused_result",
7189 EXPR_LOCUS (t), fdecl);
7190 else
d4ee4d25 7191 warning (0, "%Hignoring return value of function "
6de9cd9a
DN
7192 "declared with attribute warn_unused_result",
7193 EXPR_LOCUS (t));
7194 }
7195 break;
7196
7197 default:
7198 /* Not a container, not a call, or a call whose value is used. */
7199 break;
7200 }
7201}
7202
c5ff069d
ZW
7203/* Convert a character from the host to the target execution character
7204 set. cpplib handles this, mostly. */
7205
7206HOST_WIDE_INT
7207c_common_to_target_charset (HOST_WIDE_INT c)
7208{
7209 /* Character constants in GCC proper are sign-extended under -fsigned-char,
7210 zero-extended under -fno-signed-char. cpplib insists that characters
7211 and character constants are always unsigned. Hence we must convert
7212 back and forth. */
7213 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
7214
7215 uc = cpp_host_to_exec_charset (parse_in, uc);
7216
7217 if (flag_signed_char)
7218 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
7219 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
7220 else
7221 return uc;
7222}
7223
ee8a6a3e 7224/* Build the result of __builtin_offsetof. EXPR is a nested sequence of
6d4d7b0e
PB
7225 component references, with STOP_REF, or alternatively an INDIRECT_REF of
7226 NULL, at the bottom; much like the traditional rendering of offsetof as a
7227 macro. Returns the folded and properly cast result. */
ee8a6a3e
RH
7228
7229static tree
6d4d7b0e 7230fold_offsetof_1 (tree expr, tree stop_ref)
ee8a6a3e
RH
7231{
7232 enum tree_code code = PLUS_EXPR;
7233 tree base, off, t;
7234
6d4d7b0e
PB
7235 if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK)
7236 return size_zero_node;
7237
ee8a6a3e
RH
7238 switch (TREE_CODE (expr))
7239 {
7240 case ERROR_MARK:
7241 return expr;
7242
545b7d8c
VR
7243 case VAR_DECL:
7244 error ("cannot apply %<offsetof%> to static data member %qD", expr);
7245 return error_mark_node;
7246
6d4d7b0e
PB
7247 case CALL_EXPR:
7248 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
7249 return error_mark_node;
7250
7251 case INTEGER_CST:
7252 gcc_assert (integer_zerop (expr));
ee8a6a3e
RH
7253 return size_zero_node;
7254
6d4d7b0e
PB
7255 case NOP_EXPR:
7256 case INDIRECT_REF:
7257 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
7258 gcc_assert (base == error_mark_node || base == size_zero_node);
7259 return base;
7260
ee8a6a3e 7261 case COMPONENT_REF:
6d4d7b0e 7262 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
ee8a6a3e
RH
7263 if (base == error_mark_node)
7264 return base;
7265
7266 t = TREE_OPERAND (expr, 1);
7267 if (DECL_C_BIT_FIELD (t))
7268 {
7269 error ("attempt to take address of bit-field structure "
c51a1ba9 7270 "member %qD", t);
ee8a6a3e
RH
7271 return error_mark_node;
7272 }
7273 off = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (t),
7274 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t), 1)
7275 / BITS_PER_UNIT));
7276 break;
7277
7278 case ARRAY_REF:
6d4d7b0e 7279 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
ee8a6a3e
RH
7280 if (base == error_mark_node)
7281 return base;
7282
7283 t = TREE_OPERAND (expr, 1);
7284 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0)
7285 {
7286 code = MINUS_EXPR;
987b67bc 7287 t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
ee8a6a3e
RH
7288 }
7289 t = convert (sizetype, t);
7290 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
7291 break;
7292
1916c916
VR
7293 case COMPOUND_EXPR:
7294 /* Handle static members of volatile structs. */
7295 t = TREE_OPERAND (expr, 1);
7296 gcc_assert (TREE_CODE (t) == VAR_DECL);
7297 return fold_offsetof_1 (t, stop_ref);
7298
ee8a6a3e 7299 default:
366de0ce 7300 gcc_unreachable ();
ee8a6a3e
RH
7301 }
7302
7303 return size_binop (code, base, off);
7304}
7305
7306tree
6d4d7b0e 7307fold_offsetof (tree expr, tree stop_ref)
ee8a6a3e
RH
7308{
7309 /* Convert back from the internal sizetype to size_t. */
6d4d7b0e 7310 return convert (size_type_node, fold_offsetof_1 (expr, stop_ref));
ee8a6a3e
RH
7311}
7312
37dc0d8d 7313/* Print an error message for an invalid lvalue. USE says
5ae9ba3e
MM
7314 how the lvalue is being used and so selects the error message. */
7315
37dc0d8d
JM
7316void
7317lvalue_error (enum lvalue_use use)
5ae9ba3e 7318{
37dc0d8d 7319 switch (use)
5ae9ba3e 7320 {
37dc0d8d 7321 case lv_assign:
5d352b2d 7322 error ("lvalue required as left operand of assignment");
37dc0d8d
JM
7323 break;
7324 case lv_increment:
5d352b2d 7325 error ("lvalue required as increment operand");
37dc0d8d
JM
7326 break;
7327 case lv_decrement:
5d352b2d 7328 error ("lvalue required as decrement operand");
37dc0d8d
JM
7329 break;
7330 case lv_addressof:
5d352b2d 7331 error ("lvalue required as unary %<&%> operand");
37dc0d8d
JM
7332 break;
7333 case lv_asm:
5d352b2d 7334 error ("lvalue required in asm statement");
37dc0d8d
JM
7335 break;
7336 default:
7337 gcc_unreachable ();
5ae9ba3e 7338 }
5ae9ba3e 7339}
aab038d5
RH
7340\f
7341/* *PTYPE is an incomplete array. Complete it with a domain based on
7342 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
7343 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7344 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
7345
7346int
7347complete_array_type (tree *ptype, tree initial_value, bool do_default)
7348{
7349 tree maxindex, type, main_type, elt, unqual_elt;
7350 int failure = 0, quals;
06d40de8 7351 hashval_t hashcode = 0;
aab038d5
RH
7352
7353 maxindex = size_zero_node;
7354 if (initial_value)
7355 {
7356 if (TREE_CODE (initial_value) == STRING_CST)
7357 {
7358 int eltsize
7359 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
7360 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
7361 }
7362 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
7363 {
4038c495 7364 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
aab038d5 7365
4038c495 7366 if (VEC_empty (constructor_elt, v))
aab038d5
RH
7367 {
7368 if (pedantic)
7369 failure = 3;
7370 maxindex = integer_minus_one_node;
7371 }
7372 else
7373 {
7374 tree curindex;
4038c495
GB
7375 unsigned HOST_WIDE_INT cnt;
7376 constructor_elt *ce;
aab038d5 7377
4038c495
GB
7378 if (VEC_index (constructor_elt, v, 0)->index)
7379 maxindex = fold_convert (sizetype,
7380 VEC_index (constructor_elt,
7381 v, 0)->index);
aab038d5
RH
7382 curindex = maxindex;
7383
4038c495
GB
7384 for (cnt = 1;
7385 VEC_iterate (constructor_elt, v, cnt, ce);
7386 cnt++)
aab038d5 7387 {
4038c495
GB
7388 if (ce->index)
7389 curindex = fold_convert (sizetype, ce->index);
aab038d5
RH
7390 else
7391 curindex = size_binop (PLUS_EXPR, curindex, size_one_node);
7392
7393 if (tree_int_cst_lt (maxindex, curindex))
7394 maxindex = curindex;
7395 }
7396 }
7397 }
7398 else
7399 {
7400 /* Make an error message unless that happened already. */
7401 if (initial_value != error_mark_node)
7402 failure = 1;
7403 }
7404 }
7405 else
7406 {
7407 failure = 2;
7408 if (!do_default)
7409 return failure;
7410 }
7411
7412 type = *ptype;
7413 elt = TREE_TYPE (type);
7414 quals = TYPE_QUALS (strip_array_types (elt));
7415 if (quals == 0)
7416 unqual_elt = elt;
7417 else
7418 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
7419
7420 /* Using build_distinct_type_copy and modifying things afterward instead
7421 of using build_array_type to create a new type preserves all of the
7422 TYPE_LANG_FLAG_? bits that the front end may have set. */
7423 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7424 TREE_TYPE (main_type) = unqual_elt;
7425 TYPE_DOMAIN (main_type) = build_index_type (maxindex);
7426 layout_type (main_type);
7427
06d40de8
DG
7428 /* Make sure we have the canonical MAIN_TYPE. */
7429 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
7430 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
7431 hashcode);
7432 main_type = type_hash_canon (hashcode, main_type);
7433
9ae165a0
DG
7434 /* Fix the canonical type. */
7435 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
7436 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
7437 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
7438 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
7439 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
7440 != TYPE_DOMAIN (main_type)))
7441 TYPE_CANONICAL (main_type)
7442 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
7443 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
7444 else
7445 TYPE_CANONICAL (main_type) = main_type;
7446
aab038d5
RH
7447 if (quals == 0)
7448 type = main_type;
7449 else
7450 type = c_build_qualified_type (main_type, quals);
7451
7bfcb402
JM
7452 if (COMPLETE_TYPE_P (type)
7453 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7454 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
7455 {
7456 error ("size of array is too large");
7457 /* If we proceed with the array type as it is, we'll eventually
7458 crash in tree_low_cst(). */
7459 type = error_mark_node;
7460 }
7461
aab038d5
RH
7462 *ptype = type;
7463 return failure;
7464}
5ae9ba3e 7465
48ae6c13
RH
7466\f
7467/* Used to help initialize the builtin-types.def table. When a type of
7468 the correct size doesn't exist, use error_mark_node instead of NULL.
7469 The later results in segfaults even when a decl using the type doesn't
7470 get invoked. */
7471
7472tree
7473builtin_type_for_size (int size, bool unsignedp)
7474{
7475 tree type = lang_hooks.types.type_for_size (size, unsignedp);
7476 return type ? type : error_mark_node;
7477}
7478
7479/* A helper function for resolve_overloaded_builtin in resolving the
7480 overloaded __sync_ builtins. Returns a positive power of 2 if the
7481 first operand of PARAMS is a pointer to a supported data type.
7482 Returns 0 if an error is encountered. */
7483
7484static int
7485sync_resolve_size (tree function, tree params)
7486{
7487 tree type;
7488 int size;
7489
7490 if (params == NULL)
7491 {
7492 error ("too few arguments to function %qE", function);
7493 return 0;
7494 }
7495
7496 type = TREE_TYPE (TREE_VALUE (params));
7497 if (TREE_CODE (type) != POINTER_TYPE)
7498 goto incompatible;
7499
7500 type = TREE_TYPE (type);
7501 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
7502 goto incompatible;
7503
7504 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
a0274e3e 7505 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
48ae6c13
RH
7506 return size;
7507
7508 incompatible:
7509 error ("incompatible type for argument %d of %qE", 1, function);
7510 return 0;
7511}
7512
c22cacf3 7513/* A helper function for resolve_overloaded_builtin. Adds casts to
48ae6c13
RH
7514 PARAMS to make arguments match up with those of FUNCTION. Drops
7515 the variadic arguments at the end. Returns false if some error
7516 was encountered; true on success. */
7517
7518static bool
7519sync_resolve_params (tree orig_function, tree function, tree params)
7520{
7521 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
7522 tree ptype;
7523 int number;
7524
7525 /* We've declared the implementation functions to use "volatile void *"
7526 as the pointer parameter, so we shouldn't get any complaints from the
7527 call to check_function_arguments what ever type the user used. */
7528 arg_types = TREE_CHAIN (arg_types);
7529 ptype = TREE_TYPE (TREE_TYPE (TREE_VALUE (params)));
7530 number = 2;
7531
7532 /* For the rest of the values, we need to cast these to FTYPE, so that we
7533 don't get warnings for passing pointer types, etc. */
7534 while (arg_types != void_list_node)
7535 {
7536 tree val;
7537
7538 params = TREE_CHAIN (params);
7539 if (params == NULL)
7540 {
7541 error ("too few arguments to function %qE", orig_function);
7542 return false;
7543 }
7544
7545 /* ??? Ideally for the first conversion we'd use convert_for_assignment
7546 so that we get warnings for anything that doesn't match the pointer
7547 type. This isn't portable across the C and C++ front ends atm. */
7548 val = TREE_VALUE (params);
7549 val = convert (ptype, val);
7550 val = convert (TREE_VALUE (arg_types), val);
7551 TREE_VALUE (params) = val;
7552
7553 arg_types = TREE_CHAIN (arg_types);
7554 number++;
7555 }
7556
7557 /* The definition of these primitives is variadic, with the remaining
7558 being "an optional list of variables protected by the memory barrier".
7559 No clue what that's supposed to mean, precisely, but we consider all
7560 call-clobbered variables to be protected so we're safe. */
7561 TREE_CHAIN (params) = NULL;
7562
7563 return true;
7564}
7565
c22cacf3 7566/* A helper function for resolve_overloaded_builtin. Adds a cast to
48ae6c13
RH
7567 RESULT to make it match the type of the first pointer argument in
7568 PARAMS. */
7569
7570static tree
7571sync_resolve_return (tree params, tree result)
7572{
7573 tree ptype = TREE_TYPE (TREE_TYPE (TREE_VALUE (params)));
99db1ef0 7574 ptype = TYPE_MAIN_VARIANT (ptype);
48ae6c13
RH
7575 return convert (ptype, result);
7576}
7577
7578/* Some builtin functions are placeholders for other expressions. This
7579 function should be called immediately after parsing the call expression
7580 before surrounding code has committed to the type of the expression.
7581
7582 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7583 PARAMS is the argument list for the call. The return value is non-null
7584 when expansion is complete, and null if normal processing should
7585 continue. */
7586
7587tree
7588resolve_overloaded_builtin (tree function, tree params)
7589{
7590 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
58646b77
PB
7591 switch (DECL_BUILT_IN_CLASS (function))
7592 {
7593 case BUILT_IN_NORMAL:
7594 break;
7595 case BUILT_IN_MD:
7596 if (targetm.resolve_overloaded_builtin)
c22cacf3 7597 return targetm.resolve_overloaded_builtin (function, params);
58646b77 7598 else
c22cacf3 7599 return NULL_TREE;
58646b77
PB
7600 default:
7601 return NULL_TREE;
7602 }
c22cacf3 7603
58646b77 7604 /* Handle BUILT_IN_NORMAL here. */
48ae6c13
RH
7605 switch (orig_code)
7606 {
7607 case BUILT_IN_FETCH_AND_ADD_N:
7608 case BUILT_IN_FETCH_AND_SUB_N:
7609 case BUILT_IN_FETCH_AND_OR_N:
7610 case BUILT_IN_FETCH_AND_AND_N:
7611 case BUILT_IN_FETCH_AND_XOR_N:
7612 case BUILT_IN_FETCH_AND_NAND_N:
7613 case BUILT_IN_ADD_AND_FETCH_N:
7614 case BUILT_IN_SUB_AND_FETCH_N:
7615 case BUILT_IN_OR_AND_FETCH_N:
7616 case BUILT_IN_AND_AND_FETCH_N:
7617 case BUILT_IN_XOR_AND_FETCH_N:
7618 case BUILT_IN_NAND_AND_FETCH_N:
7619 case BUILT_IN_BOOL_COMPARE_AND_SWAP_N:
7620 case BUILT_IN_VAL_COMPARE_AND_SWAP_N:
7621 case BUILT_IN_LOCK_TEST_AND_SET_N:
7622 case BUILT_IN_LOCK_RELEASE_N:
7623 {
7624 int n = sync_resolve_size (function, params);
7625 tree new_function, result;
7626
7627 if (n == 0)
7628 return error_mark_node;
7629
7630 new_function = built_in_decls[orig_code + exact_log2 (n) + 1];
7631 if (!sync_resolve_params (function, new_function, params))
7632 return error_mark_node;
7633
7634 result = build_function_call (new_function, params);
7635 if (orig_code != BUILT_IN_BOOL_COMPARE_AND_SWAP_N
7636 && orig_code != BUILT_IN_LOCK_RELEASE_N)
7637 result = sync_resolve_return (params, result);
7638
7639 return result;
7640 }
7641
7642 default:
58646b77 7643 return NULL_TREE;
48ae6c13
RH
7644 }
7645}
7646
5bed876a
AH
7647/* Ignoring their sign, return true if two scalar types are the same. */
7648bool
7649same_scalar_type_ignoring_signedness (tree t1, tree t2)
7650{
7651 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7652
ab22c1fa
CF
7653 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
7654 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
7655 || c2 == FIXED_POINT_TYPE));
5bed876a
AH
7656
7657 /* Equality works here because c_common_signed_type uses
7658 TYPE_MAIN_VARIANT. */
12753674
RE
7659 return c_common_signed_type (t1)
7660 == c_common_signed_type (t2);
5bed876a
AH
7661}
7662
104f8784
KG
7663/* Check for missing format attributes on function pointers. LTYPE is
7664 the new type or left-hand side type. RTYPE is the old type or
7665 right-hand side type. Returns TRUE if LTYPE is missing the desired
7666 attribute. */
7667
7668bool
7669check_missing_format_attribute (tree ltype, tree rtype)
7670{
7671 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7672 tree ra;
7673
7674 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7675 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7676 break;
7677 if (ra)
7678 {
7679 tree la;
7680 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7681 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7682 break;
7683 return !la;
7684 }
7685 else
7686 return false;
7687}
7688
ff6b6641
GDR
7689/* Subscripting with type char is likely to lose on a machine where
7690 chars are signed. So warn on any machine, but optionally. Don't
7691 warn for unsigned char since that type is safe. Don't warn for
7692 signed char because anyone who uses that must have done so
7693 deliberately. Furthermore, we reduce the false positive load by
7694 warning only for non-constant value of type char. */
7695
7696void
7697warn_array_subscript_with_type_char (tree index)
7698{
7699 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
7700 && TREE_CODE (index) != INTEGER_CST)
7701 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
7702}
7703
2a67bec2
ILT
7704/* Implement -Wparentheses for the unexpected C precedence rules, to
7705 cover cases like x + y << z which readers are likely to
7706 misinterpret. We have seen an expression in which CODE is a binary
7707 operator used to combine expressions headed by CODE_LEFT and
7708 CODE_RIGHT. CODE_LEFT and CODE_RIGHT may be ERROR_MARK, which
7709 means that that side of the expression was not formed using a
7710 binary operator, or it was enclosed in parentheses. */
7711
7712void
7713warn_about_parentheses (enum tree_code code, enum tree_code code_left,
7714 enum tree_code code_right)
7715{
7716 if (!warn_parentheses)
7717 return;
7718
7719 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
7720 {
7721 if (code_left == PLUS_EXPR || code_left == MINUS_EXPR
7722 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
7723 warning (OPT_Wparentheses,
7724 "suggest parentheses around + or - inside shift");
7725 }
7726
7727 if (code == TRUTH_ORIF_EXPR)
7728 {
7729 if (code_left == TRUTH_ANDIF_EXPR
7730 || code_right == TRUTH_ANDIF_EXPR)
7731 warning (OPT_Wparentheses,
7732 "suggest parentheses around && within ||");
7733 }
7734
7735 if (code == BIT_IOR_EXPR)
7736 {
7737 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
7738 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
7739 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
7740 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
7741 warning (OPT_Wparentheses,
7742 "suggest parentheses around arithmetic in operand of |");
7743 /* Check cases like x|y==z */
7744 if (TREE_CODE_CLASS (code_left) == tcc_comparison
7745 || TREE_CODE_CLASS (code_right) == tcc_comparison)
7746 warning (OPT_Wparentheses,
7747 "suggest parentheses around comparison in operand of |");
7748 }
7749
7750 if (code == BIT_XOR_EXPR)
7751 {
7752 if (code_left == BIT_AND_EXPR
7753 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
7754 || code_right == BIT_AND_EXPR
7755 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
7756 warning (OPT_Wparentheses,
7757 "suggest parentheses around arithmetic in operand of ^");
7758 /* Check cases like x^y==z */
7759 if (TREE_CODE_CLASS (code_left) == tcc_comparison
7760 || TREE_CODE_CLASS (code_right) == tcc_comparison)
7761 warning (OPT_Wparentheses,
7762 "suggest parentheses around comparison in operand of ^");
7763 }
7764
7765 if (code == BIT_AND_EXPR)
7766 {
7767 if (code_left == PLUS_EXPR || code_left == MINUS_EXPR
7768 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
7769 warning (OPT_Wparentheses,
7770 "suggest parentheses around + or - in operand of &");
7771 /* Check cases like x&y==z */
7772 if (TREE_CODE_CLASS (code_left) == tcc_comparison
7773 || TREE_CODE_CLASS (code_right) == tcc_comparison)
7774 warning (OPT_Wparentheses,
7775 "suggest parentheses around comparison in operand of &");
7776 }
7777
e7917d06
DM
7778 if (code == EQ_EXPR || code == NE_EXPR)
7779 {
7780 if (TREE_CODE_CLASS (code_left) == tcc_comparison
7781 || TREE_CODE_CLASS (code_right) == tcc_comparison)
7782 warning (OPT_Wparentheses,
7783 "suggest parentheses around comparison in operand of %s",
7784 code == EQ_EXPR ? "==" : "!=");
7785 }
7786 else if (TREE_CODE_CLASS (code) == tcc_comparison)
7787 {
7788 if ((TREE_CODE_CLASS (code_left) == tcc_comparison
7789 && code_left != NE_EXPR && code_left != EQ_EXPR)
7790 || (TREE_CODE_CLASS (code_right) == tcc_comparison
7791 && code_right != NE_EXPR && code_right != EQ_EXPR))
7792 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
7793 "have their mathematical meaning");
7794 }
2a67bec2
ILT
7795}
7796
c616e51b
MLI
7797/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
7798
7799void
7800warn_for_unused_label (tree label)
7801{
7802 if (!TREE_USED (label))
7803 {
7804 if (DECL_INITIAL (label))
7805 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
7806 else
7807 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
7808 }
7809}
ff6b6641 7810
1f1d5130
MS
7811#ifndef TARGET_HAS_TARGETCM
7812struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;
7813#endif
7814
2e9cb75e
MLI
7815/* Warn for division by zero according to the value of DIVISOR. */
7816
7817void
7818warn_for_div_by_zero (tree divisor)
7819{
ab22c1fa
CF
7820 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
7821 about division by zero. Do not issue a warning if DIVISOR has a
2e9cb75e
MLI
7822 floating-point type, since we consider 0.0/0.0 a valid way of
7823 generating a NaN. */
ab22c1fa
CF
7824 if (skip_evaluation == 0
7825 && (integer_zerop (divisor) || fixed_zerop (divisor)))
2e9cb75e
MLI
7826 warning (OPT_Wdiv_by_zero, "division by zero");
7827}
7828
e2500fed 7829#include "gt-c-common.h"