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