]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
PR c++/40139
[thirdparty/gcc.git] / gcc / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
d513ec2f 2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
2d9d8740 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
b0fc3e72 5
f12b58b3 6This file is part of GCC.
b0fc3e72 7
f12b58b3 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
8c4c00c1 10Software Foundation; either version 3, or (at your option) any later
f12b58b3 11version.
b0fc3e72 12
f12b58b3 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.
b0fc3e72 17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
b0fc3e72 21
22#include "config.h"
405711de 23#include "system.h"
805e22b2 24#include "coretypes.h"
25#include "tm.h"
e48d0f41 26#include "intl.h"
b0fc3e72 27#include "tree.h"
b0fc3e72 28#include "flags.h"
cd03a192 29#include "output.h"
a3fa7feb 30#include "c-pragma.h"
a5b1863e 31#include "rtl.h"
dc12af01 32#include "ggc.h"
573aba85 33#include "varray.h"
74647769 34#include "expr.h"
e41f0d80 35#include "c-common.h"
d8c9779c 36#include "tm_p.h"
4e91a871 37#include "obstack.h"
a654e028 38#include "cpplib.h"
8ee295a7 39#include "target.h"
96554925 40#include "langhooks.h"
f3dde807 41#include "tree-inline.h"
7acb29a3 42#include "c-tree.h"
69579044 43#include "toplev.h"
7f5f3953 44#include "diagnostic.h"
4ee9c684 45#include "tree-iterator.h"
46#include "hashtab.h"
b55af61c 47#include "tree-mudflap.h"
e08bd2f4 48#include "opts.h"
5000e21c 49#include "real.h"
62eec3b4 50#include "cgraph.h"
b9fc964a 51#include "target-def.h"
75a70cf9 52#include "gimple.h"
9421ebb9 53#include "fixed-value.h"
979d3efc 54#include "libfuncs.h"
fd6f6435 55
90cc7820 56cpp_reader *parse_in; /* Declared in c-pragma.h. */
a654e028 57
174fcc61 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
73673831 68#ifndef PID_TYPE
69#define PID_TYPE "int"
70#endif
71
f3449a3c 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
924bbf02 81#define CHAR16_TYPE "short unsigned int"
82#endif
83
f3449a3c 84#ifdef UINT_LEAST32_TYPE
85#define CHAR32_TYPE UINT_LEAST32_TYPE
86#else
924bbf02 87#define CHAR32_TYPE "unsigned int"
88#endif
89
174fcc61 90#ifndef WCHAR_TYPE
91#define WCHAR_TYPE "int"
92#endif
93
18ef7ac2 94/* WCHAR_TYPE gets overridden by -fshort-wchar. */
95#define MODIFIED_WCHAR_TYPE \
96 (flag_short_wchar ? "short unsigned int" : WCHAR_TYPE)
97
194c4d9f 98#ifndef PTRDIFF_TYPE
99#define PTRDIFF_TYPE "long int"
100#endif
101
6bf5ed8d 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
f3449a3c 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
72040e7e 232/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 233 listed here individually for documentation purposes.
72040e7e 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
3c2239cf 245 tree truthvalue_type_node;
246 tree truthvalue_false_node;
247 tree truthvalue_true_node;
72040e7e 248
249 tree ptrdiff_type_node;
250
251 tree unsigned_char_type_node;
252 tree signed_char_type_node;
253 tree wchar_type_node;
72040e7e 254
924bbf02 255 tree char16_type_node;
256 tree char32_type_node;
257
72040e7e 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
c4503c0a 267 tree dfloat32_type_node;
268 tree dfloat64_type_node;
269 tree_dfloat128_type_node;
270
72040e7e 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
924bbf02 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
72040e7e 319 Type `int ()' -- used for implicit declaration of functions.
320
321 tree default_function_type;
322
72040e7e 323 A VOID_TYPE node, packaged in a TREE_LIST.
324
325 tree void_list_node;
326
734c98be 327 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
65b7f83f 328 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
329 VAR_DECLS, but C++ does.)
71d9fc9b 330
65b7f83f 331 tree function_name_decl_node;
734c98be 332 tree pretty_function_name_decl_node;
65b7f83f 333 tree c99_function_name_decl_node;
334
335 Stack of nested function name VAR_DECLs.
1cae46be 336
65b7f83f 337 tree saved_function_name_decls;
71d9fc9b 338
72040e7e 339*/
340
341tree c_global_trees[CTI_MAX];
573aba85 342\f
574a6990 343/* Switches common to the C front ends. */
344
f0b5f617 345/* Nonzero if preprocessing only. */
f7070933 346
ffd56b21 347int flag_preprocess_only;
348
f7070933 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
d718b525 366/* Nonzero means process PCH files while preprocessing. */
367
368bool flag_pch_preprocess;
369
573aba85 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
1ed9d5f5 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
0270ae90 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
174fcc61 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
546c4794 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
07ebd091 404/* Nonzero means allow Microsoft extensions without warnings or errors. */
405int flag_ms_extensions;
406
574a6990 407/* Nonzero means don't recognize the keyword `asm'. */
408
409int flag_no_asm;
410
574a6990 411/* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
412
413int flag_signed_bitfields = 1;
574a6990 414
1cae46be 415/* Warn about #pragma directives that are not recognized. */
574a6990 416
1cae46be 417int warn_unknown_pragmas; /* Tri state variable. */
574a6990 418
574a6990 419/* Warn about format/argument anomalies in calls to formatted I/O functions
420 (*printf, *scanf, strftime, strfmon, etc.). */
421
422int warn_format;
423
ae84079f 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
c17b85ea 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
c17b85ea 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;
b27ac6b5 444
574a6990 445/* C/ObjC language option variables. */
446
447
574a6990 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
8b332087 461/* Nonzero means that we have builtin functions, and main is an int. */
574a6990 462
463int flag_hosted = 1;
464
574a6990 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
574a6990 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
f0b5f617 480/* Tells the compiler what is the constant string class for ObjC. */
574a6990 481
482const char *constant_string_class_name;
483
574a6990 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
574a6990 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
6dcdb5de 547/* The C++ dialect being used. C++98 is the default. */
0fe6eeac 548
6dcdb5de 549enum cxx_dialect cxx_dialect = cxx98;
0fe6eeac 550
574a6990 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
e7aa92b2 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
574a6990 573/* Nonzero to use __cxa_atexit, rather than atexit, to register
3bf418bd 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. */
574a6990 576
577int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT;
578
3bf418bd 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
574a6990 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
4813f5af 591/* Nonzero means to generate thread-safe code for initializing local
592 statics. */
593
594int flag_threadsafe_statics = 1;
595
fc8ee10c 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
574a6990 601/* Nonzero means warn about implicit declarations. */
602
603int warn_implicit = 1;
604
574a6990 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
988fc1d1 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
1cae46be 617tree (*make_fname_decl) (tree, int);
9e5a737d 618
e78703c1 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
7f5f3953 623/* Whether lexing has been completed, so subsequent preprocessor
624 errors should use the compiler's input_location. */
625bool done_lexing = false;
626
2c0e001b 627/* Information about how a function name is generated. */
65b7f83f 628struct fname_var_t
629{
e99c3a1d 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? */
65b7f83f 633};
634
2c0e001b 635/* The three ways of getting then name of the current function. */
65b7f83f 636
637const struct fname_var_t fname_vars[] =
638{
2c0e001b 639 /* C99 compliant __func__, must be first. */
65b7f83f 640 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 641 /* GCC __FUNCTION__ compliant. */
65b7f83f 642 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 643 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 644 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
645 {NULL, 0, 0},
646};
647
a75b1c71 648static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
2ca392fd 649static tree check_case_value (tree);
650static bool check_case_bounds (tree, tree, tree *, tree *);
be43ff5a 651
1cae46be 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 *);
5de92639 656static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
657static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
1cae46be 658static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
659static tree handle_always_inline_attribute (tree *, tree, tree, int,
660 bool *);
1b16fc45 661static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
662static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 663static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 664static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 665static tree handle_used_attribute (tree *, tree, tree, int, bool *);
666static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
62eec3b4 667static tree handle_externally_visible_attribute (tree *, tree, tree, int,
668 bool *);
1cae46be 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 *);
f4a30bd7 679static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 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 *);
26d1c5ff 687static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 688static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
689 bool *);
690static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
fc09b200 691static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 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 *);
8a8cdb8d 699static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
700 bool *);
bf6c8de0 701static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 702static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 703static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
24470055 704static tree handle_target_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 705static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
1cae46be 706
d01f58f9 707static void check_function_nonnull (tree, int, tree *);
1cae46be 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 *);
860251be 711static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 712
5c6e5756 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.
51030405 727 In C with -Wc++-compat, we warn if D_CXXWARN is set. */
5c6e5756 728
729const struct c_common_resword c_common_reswords[] =
730{
731 { "_Bool", RID_BOOL, D_CONLY },
732 { "_Complex", RID_COMPLEX, 0 },
c1800156 733 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 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 },
dbd982c9 795 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 796 { "break", RID_BREAK, 0 },
797 { "case", RID_CASE, 0 },
51030405 798 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 799 { "char", RID_CHAR, 0 },
51030405 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 },
5c6e5756 803 { "const", RID_CONST, 0 },
804 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
805 { "continue", RID_CONTINUE, 0 },
51030405 806 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 807 { "default", RID_DEFAULT, 0 },
51030405 808 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 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 },
51030405 814 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
815 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 816 { "extern", RID_EXTERN, 0 },
51030405 817 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 818 { "float", RID_FLOAT, 0 },
819 { "for", RID_FOR, 0 },
51030405 820 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 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 },
51030405 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 },
5c6e5756 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 },
51030405 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 },
5c6e5756 850 { "typedef", RID_TYPEDEF, 0 },
51030405 851 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
852 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 853 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
854 { "union", RID_UNION, 0 },
855 { "unsigned", RID_UNSIGNED, 0 },
51030405 856 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
857 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 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
f8e93a2e 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,
1cae46be 892 handle_packed_attribute },
f8e93a2e 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 },
541e4101 910 { "gnu_inline", 0, 0, true, false, false,
911 handle_gnu_inline_attribute },
1b16fc45 912 { "artificial", 0, 0, true, false, false,
913 handle_artificial_attribute },
0cdd9887 914 { "flatten", 0, 0, true, false, false,
a0c938f0 915 handle_flatten_attribute },
f8e93a2e 916 { "used", 0, 0, true, false, false,
917 handle_used_attribute },
918 { "unused", 0, 0, false, false, false,
919 handle_unused_attribute },
62eec3b4 920 { "externally_visible", 0, 0, true, false, false,
921 handle_externally_visible_attribute },
f8e93a2e 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 },
9af7fd5b 927 { "constructor", 0, 1, true, false, false,
f8e93a2e 928 handle_constructor_attribute },
9af7fd5b 929 { "destructor", 0, 1, true, false, false,
f8e93a2e 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 },
f4a30bd7 941 { "weakref", 0, 1, true, false, false,
942 handle_weakref_attribute },
f8e93a2e 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 },
26d1c5ff 947 { "returns_twice", 0, 0, true, false, false,
948 handle_returns_twice_attribute },
f8e93a2e 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 },
fc09b200 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 },
45c4e798 957 { "deprecated", 0, 1, false, false, false,
f8e93a2e 958 handle_deprecated_attribute },
959 { "vector_size", 1, 1, false, true, false,
960 handle_vector_size_attribute },
b212f378 961 { "visibility", 1, 1, false, false, false,
f8e93a2e 962 handle_visibility_attribute },
24dfead4 963 { "tls_model", 1, 1, true, false, false,
964 handle_tls_model_attribute },
dbf6c367 965 { "nonnull", 0, -1, false, true, true,
966 handle_nonnull_attribute },
fa987697 967 { "nothrow", 0, 0, true, false, false,
968 handle_nothrow_attribute },
cb59f969 969 { "may_alias", 0, 0, false, true, false, NULL },
7acb29a3 970 { "cleanup", 1, 1, true, false, false,
971 handle_cleanup_attribute },
8a8cdb8d 972 { "warn_unused_result", 0, 0, false, true, true,
973 handle_warn_unused_result_attribute },
50ca527f 974 { "sentinel", 0, 1, false, true, true,
bf6c8de0 975 handle_sentinel_attribute },
b5c26b42 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 },
4a29c97c 980 { "alloc_size", 1, 2, false, true, true,
981 handle_alloc_size_attribute },
5de92639 982 { "cold", 0, 0, true, false, false,
983 handle_cold_attribute },
984 { "hot", 0, 0, true, false, false,
985 handle_hot_attribute },
10fc867f 986 { "warning", 1, 1, true, false, false,
987 handle_error_attribute },
988 { "error", 1, 1, true, false, false,
989 handle_error_attribute },
24470055 990 { "target", 1, -1, true, false, false,
991 handle_target_attribute },
46f8e3b0 992 { "optimize", 1, -1, true, false, false,
993 handle_optimize_attribute },
f8e93a2e 994 { NULL, 0, 0, false, false, false, NULL }
995};
996
997/* Give the specifications for the format attributes, used by C and all
d716ce75 998 descendants. */
f8e93a2e 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
2c0e001b 1010/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 1011
1012void
1cae46be 1013start_fname_decls (void)
f4e3c278 1014{
65b7f83f 1015 unsigned ix;
1016 tree saved = NULL_TREE;
1cae46be 1017
65b7f83f 1018 for (ix = 0; fname_vars[ix].decl; ix++)
1019 {
1020 tree decl = *fname_vars[ix].decl;
f4e3c278 1021
65b7f83f 1022 if (decl)
1023 {
7016c612 1024 saved = tree_cons (decl, build_int_cst (NULL_TREE, ix), saved);
65b7f83f 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
2363ef00 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. */
65b7f83f 1039
1040void
1cae46be 1041finish_fname_decls (void)
65b7f83f 1042{
1043 unsigned ix;
2363ef00 1044 tree stmts = NULL_TREE;
65b7f83f 1045 tree stack = saved_function_name_decls;
1046
1047 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 1048 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 1049
2363ef00 1050 if (stmts)
65b7f83f 1051 {
2363ef00 1052 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 1053
2363ef00 1054 if (TREE_CODE (*bodyp) == BIND_EXPR)
1055 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 1056
bc2b76e0 1057 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 1058 *bodyp = stmts;
65b7f83f 1059 }
1cae46be 1060
65b7f83f 1061 for (ix = 0; fname_vars[ix].decl; ix++)
1062 *fname_vars[ix].decl = NULL_TREE;
1cae46be 1063
65b7f83f 1064 if (stack)
f4e3c278 1065 {
2c0e001b 1066 /* We had saved values, restore them. */
65b7f83f 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));
1cae46be 1073
65b7f83f 1074 *fname_vars[ix].decl = decl;
1075 }
1076 stack = TREE_CHAIN (stack);
f4e3c278 1077 }
65b7f83f 1078 saved_function_name_decls = stack;
1079}
1080
81010c97 1081/* Return the text name of the current function, suitably prettified
5fc7fa69 1082 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 1083
1084const char *
1cae46be 1085fname_as_string (int pretty_p)
65b7f83f 1086{
9ad4bb1e 1087 const char *name = "top level";
5fc7fa69 1088 char *namep;
8115b8be 1089 int vrb = 2, len;
1090 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 1091
84166705 1092 if (!pretty_p)
9ad4bb1e 1093 {
1094 name = "";
1095 vrb = 0;
1096 }
1097
1098 if (current_function_decl)
dc24ddbd 1099 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 1100
8115b8be 1101 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 1102
8115b8be 1103 namep = XNEWVEC (char, len);
1104 snprintf (namep, len, "\"%s\"", name);
1105 strname.text = (unsigned char *) namep;
1106 strname.len = len - 1;
5fc7fa69 1107
924bbf02 1108 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 1109 {
1110 XDELETEVEC (namep);
1111 return (const char *) cstr.text;
5fc7fa69 1112 }
5fc7fa69 1113
1114 return namep;
65b7f83f 1115}
1116
65b7f83f 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
e3b80d49 1122 this language independent code. LOC is the location of the
1123 function. */
65b7f83f 1124
1125tree
e3b80d49 1126fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 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)
f4e3c278 1137 {
2222b3c6 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
7299020b 1142 it from appearing in the RTL. */
2363ef00 1143 tree stmts;
9a6486a6 1144 location_t saved_location = input_location;
9a6486a6 1145 input_location = UNKNOWN_LOCATION;
1cae46be 1146
2363ef00 1147 stmts = push_stmt_list ();
65b7f83f 1148 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
2363ef00 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);
65b7f83f 1153 *fname_vars[ix].decl = decl;
9a6486a6 1154 input_location = saved_location;
f4e3c278 1155 }
65b7f83f 1156 if (!ix && !current_function_decl)
e3b80d49 1157 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 1158
65b7f83f 1159 return decl;
f4e3c278 1160}
1161
070236f0 1162/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1163
1164tree
1cae46be 1165fix_string_type (tree value)
b0fc3e72 1166{
070236f0 1167 int length = TREE_STRING_LENGTH (value);
1168 int nchars;
00d26680 1169 tree e_type, i_type, a_type;
1170
73be5127 1171 /* Compute the number of elements, for the array type. */
924bbf02 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 }
b0fc3e72 1192
1d752508 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. */
21ca8540 1206 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1207 "string length %qd is greater than the length %qd "
1d752508 1208 "ISO C%d compilers are required to support",
1209 nchars - 1, nchars_max, relevant_std);
1210 }
82cfc7f7 1211
390be14e 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*'.
00d26680 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. */
7016c612 1227 i_type = build_index_type (build_int_cst (NULL_TREE, nchars - 1));
00d26680 1228 a_type = build_array_type (e_type, i_type);
390be14e 1229 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1230 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1231
00d26680 1232 TREE_TYPE (value) = a_type;
b8e3b7ad 1233 TREE_CONSTANT (value) = 1;
a814bad5 1234 TREE_READONLY (value) = 1;
b0fc3e72 1235 TREE_STATIC (value) = 1;
1236 return value;
1237}
1238\f
a75b1c71 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;
c6418a4e 1258 tree eptype = NULL_TREE;
a75b1c71 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;
c6418a4e 1270 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1271 {
1272 eptype = TREE_TYPE (expr);
1273 expr = TREE_OPERAND (expr, 0);
1274 }
a75b1c71 1275 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1276 &maybe_const_itself);
c6418a4e 1277 if (eptype)
1278 ret = fold_convert (eptype, ret);
a75b1c71 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
c6418a4e 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
a75b1c71 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
2a1736ed 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. */
b2806639 1645
1646void
1cae46be 1647constant_expression_warning (tree value)
07317e69 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))
21ca8540 1655 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1656}
1657
1658/* The same as above but print an unconditional error. */
1659void
1660constant_expression_error (tree value)
b2806639 1661{
837e1122 1662 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1663 || TREE_CODE (value) == FIXED_CST
886cfd4f 1664 || TREE_CODE (value) == VECTOR_CST
837e1122 1665 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1666 && TREE_OVERFLOW (value))
1667 error ("overflow in constant expression");
2a1736ed 1668}
1669
f170d67f 1670/* Print a warning if an expression had overflow in folding and its
1671 operands hadn't.
1672
2a1736ed 1673 Invoke this function on every expression that
1674 (1) appears in the source code, and
f170d67f 1675 (2) is a constant expression that overflowed, and
2a1736ed 1676 (3) is not already checked by convert_and_check;
f170d67f 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. */
2a1736ed 1680
1681void
1cae46be 1682overflow_warning (tree value)
2a1736ed 1683{
f170d67f 1684 if (skip_evaluation) return;
1685
1686 switch (TREE_CODE (value))
886cfd4f 1687 {
f170d67f 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
9421ebb9 1696 case FIXED_CST:
1697 warning (OPT_Woverflow, "fixed-point overflow in expression");
1698 break;
1699
f170d67f 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;
886cfd4f 1713 }
2a1736ed 1714}
1715
03033af4 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
9c20c4fc 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. */
b13d1547 1721void
9c20c4fc 1722warn_logical_operator (location_t location, enum tree_code code, tree type,
03033af4 1723 enum tree_code code_left, tree op_left,
1724 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1725{
9c20c4fc 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
03033af4 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))
b13d1547 1749 {
9c20c4fc 1750 if (or_op)
03033af4 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;
9c20c4fc 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");
b13d1547 1809 }
1810}
1811
1812
bcf22371 1813/* Print a warning about casts that might indicate violation
1814 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 1815 strict aliasing mode is in effect. OTYPE is the original
1816 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 1817
e6fa0ea6 1818bool
1e31ff37 1819strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 1820{
45bb3afb 1821 /* Strip pointer conversion chains and get to the correct original type. */
1822 STRIP_NOPS (expr);
1823 otype = TREE_TYPE (expr);
1824
f06537f2 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))
e6fa0ea6 1832 return false;
1833
1834 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 1835 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 1836 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 1837 {
1838 /* Casting the address of an object to non void pointer. Warn
1839 if the cast breaks type based aliasing. */
e6fa0ea6 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 }
bcf22371 1846 else
1847 {
e6fa0ea6 1848 /* warn_strict_aliasing >= 3. This includes the default (3).
1849 Only warn if the cast is dereferenced immediately. */
32c2fdea 1850 alias_set_type set1 =
e6fa0ea6 1851 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 1852 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 1853
62d823d0 1854 if (set1 != set2 && set2 != 0
1855 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
e6fa0ea6 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
879f881c 1862 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 1863 {
1864 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1865 "pointer might break strict-aliasing rules");
1866 return true;
1867 }
bcf22371 1868 }
1869 }
e6fa0ea6 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. */
32c2fdea 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)
879f881c 1880 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 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;
bcf22371 1889}
1890
3f08e399 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
7accad14 1905 if (type == void_type_node || type == error_mark_node )
3f08e399 1906 break;
1907
1908 ++argct;
1909 switch (argct)
1910 {
1911 case 1:
1912 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
21ca8540 1913 pedwarn (input_location, OPT_Wmain, "first argument of %q+D should be %<int%>",
23efcc02 1914 decl);
3f08e399 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))
21ca8540 1922 pedwarn (input_location, OPT_Wmain, "second argument of %q+D should be %<char **%>",
23efcc02 1923 decl);
3f08e399 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))
21ca8540 1931 pedwarn (input_location, OPT_Wmain, "third argument of %q+D should probably be "
23efcc02 1932 "%<char **%>", decl);
3f08e399 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))
21ca8540 1941 pedwarn (input_location, OPT_Wmain, "%q+D takes only zero or two arguments", decl);
3f08e399 1942}
1943
ed7c4e62 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
8d125f7d 1951 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 1952 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1953 return true;
1954
1955 return false;
1956}
1957
546c4794 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
9f627b1a 1964vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 1965{
546c4794 1966 static bool emitted_lax_note = false;
ae6db8ab 1967 bool convertible_lax;
1968
8d125f7d 1969 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 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))));
546c4794 1979
1980 if (!convertible_lax || flag_lax_vector_conversions)
1981 return convertible_lax;
1982
1983 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 1984 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 1985 return true;
1986
1987 if (emit_lax_note && !emitted_lax_note)
1988 {
1989 emitted_lax_note = true;
5bcc316e 1990 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 1991 "conversions between vectors with differing "
1992 "element types or numbers of subparts");
1993 }
1994
1995 return false;
8b4b9810 1996}
1997
2561cea2 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
d31d55f0 2102/* Warns if the conversion of EXPR to TYPE may alter a value.
59dd8856 2103 This is a helper function for warnings_for_convert_and_check. */
d31d55f0 2104
2105static void
2106conversion_warning (tree type, tree expr)
2107{
2108 bool give_warning = false;
2109
5b16c152 2110 int i;
2111 const int expr_num_operands = TREE_OPERAND_LENGTH (expr);
27259707 2112 tree expr_type = TREE_TYPE (expr);
d31d55f0 2113
7ee0d227 2114 if (!warn_conversion && !warn_sign_conversion)
2115 return;
2116
5b16c152 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);
7bdc5064 2122 if (op && DECL_P (op) && DECL_ARTIFICIAL (op))
5b16c152 2123 return;
2124 }
2125
27259707 2126 switch (TREE_CODE (expr))
d31d55f0 2127 {
27259707 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
d31d55f0 2151 /* Warn for real constant that is not an exact integer converted
2152 to integer type. */
27259707 2153 if (TREE_CODE (expr_type) == REAL_TYPE
d31d55f0 2154 && TREE_CODE (type) == INTEGER_TYPE)
2155 {
27259707 2156 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
d31d55f0 2157 give_warning = true;
2158 }
da1fb07b 2159 /* Warn for an integer constant that does not fit into integer type. */
27259707 2160 else if (TREE_CODE (expr_type) == INTEGER_TYPE
d31d55f0 2161 && TREE_CODE (type) == INTEGER_TYPE
da1fb07b 2162 && !int_fits_type_p (expr, type))
2163 {
27259707 2164 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2165 && tree_int_cst_sgn (expr) < 0)
7ee0d227 2166 warning (OPT_Wsign_conversion,
2167 "negative integer implicitly converted to unsigned type");
27259707 2168 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2169 warning (OPT_Wsign_conversion, "conversion of unsigned constant "
2170 "value to negative integer");
7ee0d227 2171 else
2172 give_warning = true;
da1fb07b 2173 }
d31d55f0 2174 else if (TREE_CODE (type) == REAL_TYPE)
2175 {
2176 /* Warn for an integer constant that does not fit into real type. */
27259707 2177 if (TREE_CODE (expr_type) == INTEGER_TYPE)
d31d55f0 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. */
27259707 2185 else if (TREE_CODE (expr_type) == REAL_TYPE
2186 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
d31d55f0 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",
27259707 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. */
2561cea2 2222
d31d55f0 2223 /* Warn for real types converted to integer types. */
2561cea2 2224 if (TREE_CODE (expr_type) == REAL_TYPE
d31d55f0 2225 && TREE_CODE (type) == INTEGER_TYPE)
2226 give_warning = true;
2227
2561cea2 2228 else if (TREE_CODE (expr_type) == INTEGER_TYPE
d31d55f0 2229 && TREE_CODE (type) == INTEGER_TYPE)
2230 {
69609004 2231 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2232 expr = get_unwidened (expr, 0);
2561cea2 2233 expr_type = TREE_TYPE (expr);
69609004 2234
2561cea2 2235 /* Don't warn for short y; short x = ((int)y & 0xff); */
2236 if (TREE_CODE (expr) == BIT_AND_EXPR
27259707 2237 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2238 || TREE_CODE (expr) == BIT_XOR_EXPR)
2239 {
27259707 2240 /* If both args were extended from a shortest type,
2241 use that type if that is safe. */
2561cea2 2242 expr_type = shorten_binary_op (expr_type,
2243 TREE_OPERAND (expr, 0),
2244 TREE_OPERAND (expr, 1),
2245 /* bitwise */1);
2246
2561cea2 2247 if (TREE_CODE (expr) == BIT_AND_EXPR)
2248 {
2249 tree op0 = TREE_OPERAND (expr, 0);
2250 tree op1 = TREE_OPERAND (expr, 1);
30de145b 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. */
2561cea2 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
27259707 2261 && int_fits_type_p (op1, c_common_signed_type (type))
2262 && int_fits_type_p (op1,
2263 c_common_unsigned_type (type))))
2561cea2 2264 return;
30de145b 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;
2561cea2 2274 }
2275 }
d31d55f0 2276 /* Warn for integer types converted to smaller integer types. */
27259707 2277 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
7ee0d227 2278 give_warning = true;
2279
2280 /* When they are the same width but different signedness,
2281 then the value may change. */
27259707 2282 else if ((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 2283 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 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. */
2561cea2 2287 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
27259707 2288 warning (OPT_Wsign_conversion, "conversion to %qT from %qT "
2289 "may change the sign of the result",
2561cea2 2290 type, expr_type);
d31d55f0 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. */
2561cea2 2296 else if (TREE_CODE (expr_type) == INTEGER_TYPE
d31d55f0 2297 && TREE_CODE (type) == REAL_TYPE)
2298 {
2561cea2 2299 tree type_low_bound = TYPE_MIN_VALUE (expr_type);
2300 tree type_high_bound = TYPE_MAX_VALUE (expr_type);
27259707 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);
d31d55f0 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. */
2561cea2 2312 else if (TREE_CODE (expr_type) == REAL_TYPE
d31d55f0 2313 && TREE_CODE (type) == REAL_TYPE
27259707 2314 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
d31d55f0 2315 give_warning = true;
2316
2317
2318 if (give_warning)
2319 warning (OPT_Wconversion,
2320 "conversion to %qT from %qT may alter its value",
2561cea2 2321 type, expr_type);
d31d55f0 2322 }
2323}
2324
59dd8856 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. */
2a1736ed 2328
59dd8856 2329void
2330warnings_for_convert_and_check (tree type, tree expr, tree result)
2a1736ed 2331{
da1fb07b 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 {
d31d55f0 2337 /* Do not diagnose overflow in a constant expression merely
2338 because a conversion overflowed. */
da1fb07b 2339 if (TREE_OVERFLOW (result))
eddad94a 2340 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2341
da1fb07b 2342 if (TYPE_UNSIGNED (type))
d31d55f0 2343 {
da1fb07b 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");
7ee0d227 2349 else
da1fb07b 2350 conversion_warning (type, expr);
2351 }
11773141 2352 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
e0913805 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
7ee0d227 2363 else
e0913805 2364 conversion_warning (type, expr);
2a1736ed 2365 }
9421ebb9 2366 else if ((TREE_CODE (result) == INTEGER_CST
2367 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
da1fb07b 2368 warning (OPT_Woverflow,
2369 "overflow in implicit constant conversion");
7ee0d227 2370 else
da1fb07b 2371 conversion_warning (type, expr);
59dd8856 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;
c6418a4e 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;
59dd8856 2398
2399 if (TREE_TYPE (expr) == type)
2400 return expr;
da1fb07b 2401
59dd8856 2402 result = convert (type, expr);
2403
7ee0d227 2404 if (!skip_evaluation && !TREE_OVERFLOW_P (expr) && result != error_mark_node)
c6418a4e 2405 warnings_for_convert_and_check (type, expr_for_warning, result);
59dd8856 2406
da1fb07b 2407 return result;
b2806639 2408}
2409\f
4e91a871 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;
481c6ce6 2427};
2428
4e91a871 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
1cae46be 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);
79973b57 2445static bool candidate_equal_p (const_tree, const_tree);
1cae46be 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);
481c6ce6 2449
4e91a871 2450/* Create a new struct tlist and fill in its fields. */
2451static struct tlist *
1cae46be 2452new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 2453{
2454 struct tlist *l;
9318f22c 2455 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 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
1cae46be 2466add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 2467{
2468 while (add)
2469 {
2470 struct tlist *next = add->next;
84166705 2471 if (!copy)
4e91a871 2472 add->next = *to;
79973b57 2473 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 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. */
481c6ce6 2484
2485static void
1cae46be 2486merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 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)
79973b57 2500 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 2501 {
2502 found = 1;
84166705 2503 if (!tmp2->writer)
4e91a871 2504 tmp2->writer = add->writer;
2505 }
84166705 2506 if (!found)
4e91a871 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
1cae46be 2521warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2522 int only_writes)
4e91a871 2523{
2524 struct tlist *tmp;
2525
2526 /* Avoid duplicate warnings. */
2527 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 2528 if (candidate_equal_p (tmp->expr, written))
4e91a871 2529 return;
2530
2531 while (list)
2532 {
79973b57 2533 if (candidate_equal_p (list->expr, written)
2534 && !candidate_equal_p (list->writer, writer)
2535 && (!only_writes || list->writer))
4e91a871 2536 {
2537 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
6513b50d 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);
4e91a871 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
1cae46be 2551warn_for_collisions (struct tlist *list)
4e91a871 2552{
2553 struct tlist *tmp;
1cae46be 2554
4e91a871 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
734c98be 2562/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 2563 warnings. */
2564static int
1cae46be 2565warning_candidate_p (tree x)
481c6ce6 2566{
79973b57 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));
4e91a871 2578}
481c6ce6 2579
4e91a871 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. */
481c6ce6 2604
4e91a871 2605static void
1cae46be 2606verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2607 tree writer)
4e91a871 2608{
2609 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2610 enum tree_code code;
ce45a448 2611 enum tree_code_class cl;
481c6ce6 2612
e5b75768 2613 /* X may be NULL if it is the operand of an empty statement expression
2614 ({ }). */
2615 if (x == NULL)
2616 return;
2617
4e91a871 2618 restart:
2619 code = TREE_CODE (x);
e916c70c 2620 cl = TREE_CODE_CLASS (code);
481c6ce6 2621
4e91a871 2622 if (warning_candidate_p (x))
79973b57 2623 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 2624
2625 switch (code)
2626 {
67b28e3e 2627 case CONSTRUCTOR:
2628 return;
2629
4e91a871 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
481c6ce6 2664 case PREDECREMENT_EXPR:
2665 case PREINCREMENT_EXPR:
2666 case POSTDECREMENT_EXPR:
2667 case POSTINCREMENT_EXPR:
4e91a871 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;
481c6ce6 2697
2698 case CALL_EXPR:
4e91a871 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. */
c2f47e15 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 }
481c6ce6 2719
2720 case TREE_LIST:
2721 /* Scan all the list, e.g. indices of multi dimensional array. */
2722 while (x)
2723 {
4e91a871 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);
481c6ce6 2728 x = TREE_CHAIN (x);
2729 }
4e91a871 2730 return;
481c6ce6 2731
4e91a871 2732 case SAVE_EXPR:
2733 {
2734 struct tlist_cache *t;
2735 for (t = save_expr_cache; t; t = t->next)
79973b57 2736 if (candidate_equal_p (t->expr, x))
4e91a871 2737 break;
481c6ce6 2738
84166705 2739 if (!t)
481c6ce6 2740 {
9318f22c 2741 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 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;
481c6ce6 2759 }
4e91a871 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 }
481c6ce6 2764
012916cb 2765 case ADDR_EXPR:
2766 x = TREE_OPERAND (x, 0);
2767 if (DECL_P (x))
2768 return;
2769 writer = 0;
2770 goto restart;
2771
ce45a448 2772 default:
2773 /* For other expressions, simply recurse on their operands.
a0c938f0 2774 Manual tail recursion for unary expressions.
ce45a448 2775 Other non-expressions need not be processed. */
2776 if (cl == tcc_unary)
2777 {
ce45a448 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;
c2f47e15 2785 int max = TREE_OPERAND_LENGTH (x);
ce45a448 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;
481c6ce6 2795 }
481c6ce6 2796}
2797
974e2c0c 2798/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 2799 points. */
2800
2569a1be 2801void
1cae46be 2802verify_sequence_points (tree expr)
481c6ce6 2803{
4e91a871 2804 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 2805
4e91a871 2806 warned_ids = 0;
2807 save_expr_cache = 0;
2808 if (tlist_firstobj == 0)
481c6ce6 2809 {
4e91a871 2810 gcc_obstack_init (&tlist_obstack);
4fd61bc6 2811 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 2812 }
2813
4e91a871 2814 verify_tree (expr, &before_sp, &after_sp, 0);
2815 warn_for_collisions (after_sp);
2816 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 2817}
b0fc3e72 2818\f
2819/* Validate the expression after `case' and apply default promotions. */
2820
2ca392fd 2821static tree
1cae46be 2822check_case_value (tree value)
b0fc3e72 2823{
2824 if (value == NULL_TREE)
2825 return value;
2826
a97c952c 2827 /* ??? Can we ever get nops here for a valid case value? We
2828 shouldn't for C. */
fce1d6af 2829 STRIP_TYPE_NOPS (value);
225ec6aa 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. */
c0f19401 2836 if (c_dialect_cxx ())
225ec6aa 2837 {
2838 value = decl_constant_value (value);
2839 STRIP_TYPE_NOPS (value);
2840 value = fold (value);
2841 }
b0fc3e72 2842
b96dc121 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)
b0fc3e72 2847 {
2848 error ("case label does not reduce to an integer constant");
2849 value = error_mark_node;
2850 }
b0fc3e72 2851
6433f1c2 2852 constant_expression_warning (value);
2853
b0fc3e72 2854 return value;
2855}
2856\f
2ca392fd 2857/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 2858 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 2859 expression.
2860 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 2861 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 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
5c9dae64 2865 CASE_LOW_P (i.e. the default case).
442e3cb9 2866 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 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 {
c3ceba8e 2888 warning (0, "case label value is less than minimum value for type");
2ca392fd 2889 return false;
2890 }
b27ac6b5 2891
2ca392fd 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 {
c3ceba8e 2896 warning (0, "case label value exceeds maximum value for type");
2ca392fd 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 {
c3ceba8e 2904 warning (0, "lower value in case label range"
2ca392fd 2905 " less than minimum value for type");
2906 case_low = min_value;
2907 }
b27ac6b5 2908
2ca392fd 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 {
c3ceba8e 2913 warning (0, "upper value in case label range"
2ca392fd 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
b0fc3e72 2926/* Return an integer type with BITS bits of precision,
2927 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2928
2929tree
1cae46be 2930c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 2931{
46375237 2932 if (bits == TYPE_PRECISION (integer_type_node))
2933 return unsignedp ? unsigned_type_node : integer_type_node;
2934
bacde65a 2935 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 2936 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2937
bacde65a 2938 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 2939 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2940
bacde65a 2941 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 2942 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2943
bacde65a 2944 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 2945 return (unsignedp ? long_long_unsigned_type_node
2946 : long_long_integer_type_node);
2947
f57fa2ea 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
bacde65a 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
b0fc3e72 2964 return 0;
2965}
2966
9421ebb9 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
5b247e9f 2996/* Used for communication between c_common_type_for_mode and
2997 c_register_builtin_type. */
2998static GTY(()) tree registered_builtin_types;
2999
b0fc3e72 3000/* Return a data type that has machine mode MODE.
3001 If the mode is an integer,
9421ebb9 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. */
b0fc3e72 3005
3006tree
1cae46be 3007c_common_type_for_mode (enum machine_mode mode, int unsignedp)
b0fc3e72 3008{
5b247e9f 3009 tree t;
3010
46375237 3011 if (mode == TYPE_MODE (integer_type_node))
3012 return unsignedp ? unsigned_type_node : integer_type_node;
3013
b0fc3e72 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
b0fc3e72 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
f57fa2ea 3026 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3027 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3028 : widest_integer_literal_type_node;
f57fa2ea 3029
88ae7f04 3030 if (mode == QImode)
bacde65a 3031 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3032
88ae7f04 3033 if (mode == HImode)
bacde65a 3034 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3035
88ae7f04 3036 if (mode == SImode)
bacde65a 3037 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3038
88ae7f04 3039 if (mode == DImode)
bacde65a 3040 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3041
cc1cc1c7 3042#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3043 if (mode == TYPE_MODE (intTI_type_node))
3044 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3045#endif
6274009c 3046
b0fc3e72 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
545c2bde 3056 if (mode == TYPE_MODE (void_type_node))
3057 return void_type_node;
b27ac6b5 3058
b0fc3e72 3059 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3060 return (unsignedp
3061 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3062 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3063
3064 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3065 return (unsignedp
3066 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3067 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3068
0dfc45b5 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))
4917c376 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);
88ae7f04 3095 }
e2ea7e3a 3096
c4503c0a 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
9421ebb9 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
5b247e9f 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
b0fc3e72 3197 return 0;
3198}
20d39783 3199
11773141 3200tree
3201c_common_unsigned_type (tree type)
3202{
3203 return c_common_signed_or_unsigned_type (1, type);
3204}
3205
20d39783 3206/* Return a signed type the same as TYPE in other respects. */
3207
3208tree
1cae46be 3209c_common_signed_type (tree type)
20d39783 3210{
4070745f 3211 return c_common_signed_or_unsigned_type (0, type);
20d39783 3212}
3213
3214/* Return a type the same as TYPE except unsigned or
3215 signed according to UNSIGNEDP. */
3216
3217tree
1cae46be 3218c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 3219{
7a91101f 3220 tree type1;
20d39783 3221
7a91101f 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
9f92e1a2 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);
9421ebb9 3325
4f7f7efd 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
ac265864 3335 if (!INTEGRAL_TYPE_P (type)
3336 || TYPE_UNSIGNED (type) == unsignedp)
3337 return type;
3338
4f7f7efd 3339#define TYPE_OK(node) \
3340 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 3341 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 3342 if (TYPE_OK (signed_char_type_node))
20d39783 3343 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 3344 if (TYPE_OK (integer_type_node))
20d39783 3345 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 3346 if (TYPE_OK (short_integer_type_node))
20d39783 3347 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 3348 if (TYPE_OK (long_integer_type_node))
20d39783 3349 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 3350 if (TYPE_OK (long_long_integer_type_node))
20d39783 3351 return (unsignedp ? long_long_unsigned_type_node
3352 : long_long_integer_type_node);
4f7f7efd 3353 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 3354 return (unsignedp ? widest_unsigned_literal_type_node
3355 : widest_integer_literal_type_node);
ef11801e 3356
3357#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 3358 if (TYPE_OK (intTI_type_node))
ef11801e 3359 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3360#endif
4f7f7efd 3361 if (TYPE_OK (intDI_type_node))
ef11801e 3362 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 3363 if (TYPE_OK (intSI_type_node))
ef11801e 3364 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 3365 if (TYPE_OK (intHI_type_node))
ef11801e 3366 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 3367 if (TYPE_OK (intQI_type_node))
ef11801e 3368 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 3369#undef TYPE_OK
ef11801e 3370
0c4abe5b 3371 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 3372}
b268e47e 3373
c0e47fd4 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
b268e47e 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);
5b247e9f 3411
3412 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 3413}
a9b9d10c 3414\f
aff9e656 3415/* Print an error message for invalid operands to arith operation
8e70fb09 3416 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3417 LOCATION is the location of the message. */
b0fc3e72 3418
3419void
8e70fb09 3420binary_op_error (location_t location, enum tree_code code,
3421 tree type0, tree type1)
b0fc3e72 3422{
19cb6b50 3423 const char *opname;
f03946e4 3424
b0fc3e72 3425 switch (code)
3426 {
b0fc3e72 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:
66618a1e 3454 case FLOOR_MOD_EXPR:
b0fc3e72 3455 opname = "%"; break;
3456 case TRUNC_DIV_EXPR:
66618a1e 3457 case FLOOR_DIV_EXPR:
b0fc3e72 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;
31f820d2 3469 default:
315ba355 3470 gcc_unreachable ();
b0fc3e72 3471 }
8e70fb09 3472 error_at (location,
3473 "invalid operands to binary %s (have %qT and %qT)", opname,
3474 type0, type1);
b0fc3e72 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.
5b511807 3480 This function is also responsible for converting the two operands
3481 to the proper common type for comparison.
b0fc3e72 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
1cae46be 3492shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3493 enum tree_code *rescode_ptr)
b0fc3e72 3494{
19cb6b50 3495 tree type;
b0fc3e72 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)
78a8ed03 3513 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 3514 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 3515 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 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
2bd278cc 3522 so value is preserved), for canonicalization. Don't do this if
3523 the second arg is 0. */
b0fc3e72 3524
2bd278cc 3525 if (TREE_CONSTANT (primop0)
9421ebb9 3526 && !integer_zerop (primop1) && !real_zerop (primop1)
3527 && !fixed_zerop (primop1))
b0fc3e72 3528 {
19cb6b50 3529 tree tem = primop0;
3530 int temi = unsignedp0;
b0fc3e72 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;
31f820d2 3558 default:
3559 break;
b0fc3e72 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
9421ebb9 3582 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 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. */
78a8ed03 3589 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 3590 tree val;
3591
4070745f 3592 type = c_common_signed_or_unsigned_type (unsignedp0,
3593 TREE_TYPE (primop0));
cda09c61 3594
b0fc3e72 3595 maxval = TYPE_MAX_VALUE (type);
3596 minval = TYPE_MIN_VALUE (type);
3597
3598 if (unsignedp && !unsignedp0)
4070745f 3599 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 3600
3601 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 3602 {
9a5e8086 3603 /* Convert primop1 to target type, but do not introduce
3604 additional overflow. We know primop1 is an int_cst. */
c8110c8f 3605 primop1 = force_fit_type_double (*restype_ptr,
3606 TREE_INT_CST_LOW (primop1),
3607 TREE_INT_CST_HIGH (primop1), 0,
eddad94a 3608 TREE_OVERFLOW (primop1));
18dbec6f 3609 }
b0fc3e72 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)
3c2239cf 3636 val = truthvalue_true_node;
b0fc3e72 3637 }
3638 else if (code == EQ_EXPR)
3639 {
3640 if (max_lt || min_gt)
3c2239cf 3641 val = truthvalue_false_node;
b0fc3e72 3642 }
3643 else if (code == LT_EXPR)
3644 {
3645 if (max_lt)
3c2239cf 3646 val = truthvalue_true_node;
b0fc3e72 3647 if (!min_lt)
3c2239cf 3648 val = truthvalue_false_node;
b0fc3e72 3649 }
3650 else if (code == GT_EXPR)
3651 {
3652 if (min_gt)
3c2239cf 3653 val = truthvalue_true_node;
b0fc3e72 3654 if (!max_gt)
3c2239cf 3655 val = truthvalue_false_node;
b0fc3e72 3656 }
3657 else if (code == LE_EXPR)
3658 {
3659 if (!max_gt)
3c2239cf 3660 val = truthvalue_true_node;
b0fc3e72 3661 if (min_gt)
3c2239cf 3662 val = truthvalue_false_node;
b0fc3e72 3663 }
3664 else if (code == GE_EXPR)
3665 {
3666 if (!min_lt)
3c2239cf 3667 val = truthvalue_true_node;
b0fc3e72 3668 if (max_lt)
3c2239cf 3669 val = truthvalue_false_node;
b0fc3e72 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;
31f820d2 3699
3700 default:
3701 break;
b0fc3e72 3702 }
11773141 3703 type = c_common_unsigned_type (type);
b0fc3e72 3704 }
3705
fe5f2366 3706 if (TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 3707 {
3c2239cf 3708 if (val == truthvalue_false_node)
e6e352cb 3709 warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type");
3c2239cf 3710 if (val == truthvalue_true_node)
e6e352cb 3711 warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type");
b0fc3e72 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))
14ae0310 3718 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 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 }
c4503c0a 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
b0fc3e72 3734 else if (real1 && real2
2203bd5c 3735 && (TYPE_PRECISION (TREE_TYPE (primop0))
3736 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 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));
4070745f 3752 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 3753 || TYPE_UNSIGNED (*restype_ptr),
4070745f 3754 type);
b0fc3e72 3755 /* Make sure shorter operand is extended the right way
3756 to match the longer operand. */
4070745f 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);
b0fc3e72 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)
78a8ed03 3775 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 3776 {
3777 tree value = 0;
3778 switch (code)
3779 {
3780 case GE_EXPR:
e6e352cb 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
84166705 3786 && !(TREE_CODE (primop0) == INTEGER_CST
3787 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3788 primop0))))
e6e352cb 3789 warning (OPT_Wtype_limits,
3790 "comparison of unsigned expression >= 0 is always true");
3c2239cf 3791 value = truthvalue_true_node;
b0fc3e72 3792 break;
3793
3794 case LT_EXPR:
e6e352cb 3795 if (warn_type_limits && !in_system_header
84166705 3796 && !(TREE_CODE (primop0) == INTEGER_CST
3797 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3798 primop0))))
e6e352cb 3799 warning (OPT_Wtype_limits,
3800 "comparison of unsigned expression < 0 is always false");
3c2239cf 3801 value = truthvalue_false_node;
31f820d2 3802 break;
3803
3804 default:
3805 break;
b0fc3e72 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))
14ae0310 3812 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3813 primop0, value);
b0fc3e72 3814 return value;
3815 }
3816 }
3817 }
3818
3819 *op0_ptr = convert (type, primop0);
3820 *op1_ptr = convert (type, primop1);
3821
3c2239cf 3822 *restype_ptr = truthvalue_type_node;
b0fc3e72 3823
3824 return 0;
3825}
3826\f
1c26100f 3827/* Return a tree for the sum or difference (RESULTCODE says which)
3828 of pointer PTROP and integer INTOP. */
3829
3830tree
1f63a7d6 3831pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
1c26100f 3832{
add6ee5e 3833 tree size_exp, ret;
1c26100f 3834
1c26100f 3835 /* The result is a pointer of the same type that is being added. */
1c26100f 3836 tree result_type = TREE_TYPE (ptrop);
3837
3838 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3839 {
1f63a7d6 3840 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
8864917d 3841 "pointer of type %<void *%> used in arithmetic");
1c26100f 3842 size_exp = integer_one_node;
3843 }
3844 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3845 {
1f63a7d6 3846 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
8864917d 3847 "pointer to a function used in arithmetic");
1c26100f 3848 size_exp = integer_one_node;
3849 }
3850 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3851 {
1f63a7d6 3852 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
8864917d 3853 "pointer to member function used in arithmetic");
1c26100f 3854 size_exp = integer_one_node;
3855 }
1c26100f 3856 else
3857 size_exp = size_in_bytes (TREE_TYPE (result_type));
3858
add6ee5e 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
1c26100f 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. */
1c26100f 3869 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 3870 && !TREE_CONSTANT (intop)
1c26100f 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. */
84166705 3879 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 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. */
8e70fb09 3890 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3891 subcode, ptrop,
1c26100f 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. */
1c26100f 3898 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 3899 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 3900 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 3901 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 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
499e523f 3905 type for the pointer operation. */
3906 intop = convert (sizetype,
8e70fb09 3907 build_binary_op (EXPR_LOCATION (intop),
3908 MULT_EXPR, intop,
499e523f 3909 convert (TREE_TYPE (intop), size_exp), 1));
0de36bdb 3910
499e523f 3911 /* Create the sum or difference. */
0de36bdb 3912 if (resultcode == MINUS_EXPR)
499e523f 3913 intop = fold_build1 (NEGATE_EXPR, sizetype, intop);
1c26100f 3914
0de36bdb 3915 ret = fold_build2 (POINTER_PLUS_EXPR, result_type, ptrop, intop);
add6ee5e 3916
3917 fold_undefer_and_ignore_overflow_warnings ();
3918
3919 return ret;
1c26100f 3920}
3921\f
a75b1c71 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
6b68e71a 3943/* Return whether EXPR is a declaration whose address can never be
3944 NULL. */
3945
3946bool
9f627b1a 3947decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 3948{
3949 return (DECL_P (expr)
3950 && (TREE_CODE (expr) == PARM_DECL
3951 || TREE_CODE (expr) == LABEL_DECL
3952 || !DECL_WEAK (expr)));
3953}
3954
b0fc3e72 3955/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 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.
b0fc3e72 3959
8e70fb09 3960 The EXPR is located at LOCATION.
3961
b0fc3e72 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
3c2239cf 3965 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 3966 but we optimize comparisons, &&, ||, and !.
3967
3c2239cf 3968 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 3969
3970tree
8e70fb09 3971c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 3972{
b0fc3e72 3973 switch (TREE_CODE (expr))
3974 {
318a728f 3975 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 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:
ce04dcdc 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
b0fc3e72 3984 case TRUTH_ANDIF_EXPR:
3985 case TRUTH_ORIF_EXPR:
3986 case TRUTH_AND_EXPR:
3987 case TRUTH_OR_EXPR:
31f6e93c 3988 case TRUTH_XOR_EXPR:
ce04dcdc 3989 if (TREE_TYPE (expr) == truthvalue_type_node)
3990 return expr;
3991 return build2 (TREE_CODE (expr), truthvalue_type_node,
8e70fb09 3992 c_common_truthvalue_conversion (location,
3993 TREE_OPERAND (expr, 0)),
3994 c_common_truthvalue_conversion (location,
3995 TREE_OPERAND (expr, 1)));
3e851b85 3996
f6e28f72 3997 case TRUTH_NOT_EXPR:
ce04dcdc 3998 if (TREE_TYPE (expr) == truthvalue_type_node)
3999 return expr;
4000 return build1 (TREE_CODE (expr), truthvalue_type_node,
8e70fb09 4001 c_common_truthvalue_conversion (location,
4002 TREE_OPERAND (expr, 0)));
f6e28f72 4003
b0fc3e72 4004 case ERROR_MARK:
4005 return expr;
4006
4007 case INTEGER_CST:
eddad94a 4008 return integer_zerop (expr) ? truthvalue_false_node
4009 : truthvalue_true_node;
b0fc3e72 4010
4011 case REAL_CST:
5000e21c 4012 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4013 ? truthvalue_true_node
4014 : truthvalue_false_node;
b0fc3e72 4015
9421ebb9 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
ce04dcdc 4022 case FUNCTION_DECL:
b6889cb0 4023 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 4024 /* Fall through. */
4025
b0fc3e72 4026 case ADDR_EXPR:
ce871053 4027 {
ee7d4d6a 4028 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 4029 if (decl_with_nonnull_addr_p (inner))
ce871053 4030 {
6b68e71a 4031 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 4032 warning_at (location,
4033 OPT_Waddress,
4034 "the address of %qD will always evaluate as %<true%>",
4035 inner);
ce871053 4036 return truthvalue_true_node;
4037 }
65b5e6a6 4038
6b68e71a 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 }
ce871053 4046
ee7d4d6a 4047 if (TREE_SIDE_EFFECTS (inner))
14ae0310 4048 return build2 (COMPOUND_EXPR, truthvalue_type_node,
ee7d4d6a 4049 inner, truthvalue_true_node);
ce871053 4050 else
4051 return truthvalue_true_node;
4052 }
b0fc3e72 4053
2203bd5c 4054 case COMPLEX_EXPR:
8e70fb09 4055 return build_binary_op (EXPR_LOCATION (expr),
4056 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 4057 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 4058 c_common_truthvalue_conversion (location,
4059 TREE_OPERAND (expr, 0)),
4060 c_common_truthvalue_conversion (location,
4061 TREE_OPERAND (expr, 1)),
2203bd5c 4062 0);
4063
b0fc3e72 4064 case NEGATE_EXPR:
4065 case ABS_EXPR:
4066 case FLOAT_EXPR:
c6418a4e 4067 case EXCESS_PRECISION_EXPR:
d10cfa8d 4068 /* These don't change whether an object is nonzero or zero. */
8e70fb09 4069 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 4070
4071 case LROTATE_EXPR:
4072 case RROTATE_EXPR:
d10cfa8d 4073 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 4074 we can't ignore them if their second arg has side-effects. */
4075 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
14ae0310 4076 return build2 (COMPOUND_EXPR, truthvalue_type_node,
4077 TREE_OPERAND (expr, 1),
8e70fb09 4078 c_common_truthvalue_conversion
4079 (location, TREE_OPERAND (expr, 0)));
b0fc3e72 4080 else
8e70fb09 4081 return c_common_truthvalue_conversion (location,
4082 TREE_OPERAND (expr, 0));
73be5127 4083
b0fc3e72 4084 case COND_EXPR:
4085 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 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)));
b0fc3e72 4103
72dd6141 4104 CASE_CONVERT:
b0fc3e72 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;
b0fc3e72 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))))
8e70fb09 4113 return c_common_truthvalue_conversion (location,
4114 TREE_OPERAND (expr, 0));
b0fc3e72 4115 break;
4116
16837b18 4117 case MODIFY_EXPR:
60a0513e 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 }
16837b18 4125 break;
73be5127 4126
31f820d2 4127 default:
4128 break;
b0fc3e72 4129 }
4130
2ba726d2 4131 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 4132 {
a75b1c71 4133 tree t = c_save_expr (expr);
a0748b7d 4134 return (build_binary_op
8e70fb09 4135 (EXPR_LOCATION (expr),
4136 (TREE_SIDE_EFFECTS (expr)
a0748b7d 4137 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 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)),
a0748b7d 4144 0));
4145 }
2ba726d2 4146
9421ebb9 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))));
43158006 4152 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 4153 }
4154
43158006 4155 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
b0fc3e72 4156}
4157\f
3237155d 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);
0d4238dc 4164
a5b1863e 4165
4166/* Apply the TYPE_QUALS to the new DECL. */
4167
4168void
1cae46be 4169c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 4170{
adfb367f 4171 tree type = TREE_TYPE (decl);
b27ac6b5 4172
e4eabbe4 4173 if (type == error_mark_node)
4174 return;
adfb367f 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)))
a5b1863e 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 }
d91a20bc 4190 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 4191 {
adfb367f 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)))
b0b1af64 4199 error ("invalid use of %<restrict%>");
adfb367f 4200 else if (flag_strict_aliasing && type == TREE_TYPE (decl))
ed83aafb 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;
a5b1863e 4205 }
4206}
4207
4ee9c684 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
0bed3869 4211 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 4212
4213static hashval_t
4214c_type_hash (const void *p)
4215{
4216 int i = 0;
4217 int shift, size;
aae87fc3 4218 const_tree const t = (const_tree) p;
4ee9c684 4219 tree t2;
4220 switch (TREE_CODE (t))
4221 {
fbf0afd1 4222 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 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:
231bd014 4243 gcc_unreachable ();
4ee9c684 4244 }
4245 for (; t2; t2 = TREE_CHAIN (t2))
4246 i++;
78c2e180 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));
4ee9c684 4252 return ((size << 24) | (i << shift));
4253}
4254
1ecd4018 4255static GTY((param_is (union tree_node))) htab_t type_hash_table;
4256
b5ba9f3a 4257/* Return the typed-based alias set for T, which may be an expression
f7c44134 4258 or a type. Return -1 if we don't do anything special. */
b5ba9f3a 4259
32c2fdea 4260alias_set_type
1cae46be 4261c_common_get_alias_set (tree t)
b5ba9f3a 4262{
be4f2de7 4263 tree u;
4ee9c684 4264 PTR *slot;
1cae46be 4265
be4f2de7 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;
1e2513d9 4278
9fcc3e54 4279 /* That's all the expressions we handle specially. */
84166705 4280 if (!TYPE_P (t))
9fcc3e54 4281 return -1;
4282
d716ce75 4283 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 4284 lvalue that has character type. */
4285 if (t == char_type_node
4286 || t == signed_char_type_node
4287 || t == unsigned_char_type_node)
f7c44134 4288 return 0;
a5b1863e 4289
1607663f 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. */
78a8ed03 4293 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 4294 {
4070745f 4295 tree t1 = c_common_signed_type (t);
1607663f 4296
a8868e19 4297 /* t1 == t can happen for boolean nodes which are always unsigned. */
4298 if (t1 != t)
4299 return get_alias_set (t1);
4300 }
f7c44134 4301 else if (POINTER_TYPE_P (t))
87d2d17e 4302 {
f7c44134 4303 tree t1;
87d2d17e 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.
44e9fa65 4309
40bdc593 4310 Technically, this approach is actually more conservative that
4311 it needs to be. In particular, `const int *' and `int *'
734c98be 4312 should be in different alias sets, according to the C and C++
40bdc593 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
4ee9c684 4317 But, the standard is wrong. In particular, this code is
40bdc593 4318 legal C++:
4319
a0c938f0 4320 int *ip;
4321 int **ipp = &ip;
4322 const int* const* cipp = ipp;
40bdc593 4323
4ee9c684 4324 And, it doesn't make sense for that to be legal unless you
40bdc593 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. */
211f3116 4328 t1 = build_type_no_quals (t);
f7c44134 4329 if (t1 != t)
4330 return get_alias_set (t1);
87d2d17e 4331 }
1e2513d9 4332
4ee9c684 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
0bed3869 4343 /* Save time if there's only one input file. */
e08bd2f4 4344 if (num_in_fnames == 1)
4ee9c684 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. */
b27ac6b5 4353 for (t2 = TREE_TYPE (t);
af592f67 4354 TREE_CODE (t2) == POINTER_TYPE;
4355 t2 = TREE_TYPE (t2))
4356 ;
b27ac6b5 4357 if (TREE_CODE (t2) != RECORD_TYPE
af592f67 4358 && TREE_CODE (t2) != ENUMERAL_TYPE
4359 && TREE_CODE (t2) != QUAL_UNION_TYPE
4360 && TREE_CODE (t2) != UNION_TYPE)
4361 return -1;
4ee9c684 4362 if (TYPE_SIZE (t2) == 0)
af592f67 4363 return -1;
4ee9c684 4364 }
4365 /* These are the only cases that need special handling. */
b27ac6b5 4366 if (TREE_CODE (t) != RECORD_TYPE
4ee9c684 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
b27ac6b5 4376 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 4377 alias set is recorded in the table. */
4378 if (!type_hash_table)
1ecd4018 4379 type_hash_table = htab_create_ggc (1021, c_type_hash,
4ee9c684 4380 (htab_eq) lang_hooks.types_compatible_p,
4381 NULL);
4382 slot = htab_find_slot (type_hash_table, t, INSERT);
4383 if (*slot != NULL)
ad16cb2c 4384 {
4385 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4386 return TYPE_ALIAS_SET ((tree)*slot);
4387 }
4ee9c684 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
f7c44134 4393 return -1;
b5ba9f3a 4394}
902b4e01 4395\f
d4c4d95c 4396/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where the
d3a4d008 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. */
908c697e 4400
902b4e01 4401tree
908c697e 4402c_sizeof_or_alignof_type (tree type, bool is_sizeof, int complain)
902b4e01 4403{
d4c4d95c 4404 const char *op_name;
4405 tree value = NULL;
4406 enum tree_code type_code = TREE_CODE (type);
1cae46be 4407
908c697e 4408 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 4409
d4c4d95c 4410 if (type_code == FUNCTION_TYPE)
902b4e01 4411 {
908c697e 4412 if (is_sizeof)
d4c4d95c 4413 {
d3a4d008 4414 if (complain && (pedantic || warn_pointer_arith))
21ca8540 4415 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
8864917d 4416 "invalid application of %<sizeof%> to a function type");
ebd21de4 4417 else if (!complain)
4418 return error_mark_node;
d4c4d95c 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 {
1cae46be 4426 if (type_code == VOID_TYPE
d3a4d008 4427 && complain && (pedantic || warn_pointer_arith))
21ca8540 4428 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
8864917d 4429 "invalid application of %qs to a void type", op_name);
ebd21de4 4430 else if (!complain)
4431 return error_mark_node;
d4c4d95c 4432 value = size_one_node;
902b4e01 4433 }
902b4e01 4434 else if (!COMPLETE_TYPE_P (type))
4435 {
d3a4d008 4436 if (complain)
b0b1af64 4437 error ("invalid application of %qs to incomplete type %qT ",
3ad0e8cc 4438 op_name, type);
d4c4d95c 4439 value = size_zero_node;
902b4e01 4440 }
4441 else
d4c4d95c 4442 {
908c697e 4443 if (is_sizeof)
d4c4d95c 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
d37625c0 4449 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 4450 }
902b4e01 4451
d4c4d95c 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. */
143d62d7 4456 value = fold_convert (size_type_node, value);
1d8fc210 4457 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
1cae46be 4458
d4c4d95c 4459 return value;
902b4e01 4460}
4461
4462/* Implement the __alignof keyword: Return the minimum required
097b5c8b 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). */
72040e7e 4466
902b4e01 4467tree
1cae46be 4468c_alignof_expr (tree expr)
902b4e01 4469{
4470 tree t;
4471
097b5c8b 4472 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 4473 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 4474
902b4e01 4475 else if (TREE_CODE (expr) == COMPONENT_REF
4476 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4477 {
b0b1af64 4478 error ("%<__alignof%> applied to a bit-field");
902b4e01 4479 t = size_one_node;
4480 }
4481 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 4482 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 4483 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 4484
902b4e01 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)));
1cae46be 4490
72dd6141 4491 while (CONVERT_EXPR_P (t)
7cc7e163 4492 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 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
143d62d7 4506 return fold_convert (size_type_node, t);
902b4e01 4507}
4508\f
8fe4a266 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,
8fe4a266 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
8fe4a266 4522 ATTR_LAST
4523};
4524
4525static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4526
1cae46be 4527static void c_init_attributes (void);
8fe4a266 4528
27213ba3 4529enum c_builtin_type
72040e7e 4530{
d2d4bdde 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,
0a39fd54 4537#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
27213ba3 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,
d2d4bdde 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,
03901330 4543#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 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,
d2d4bdde 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
0a39fd54 4555#undef DEF_FUNCTION_TYPE_5
4556#undef DEF_FUNCTION_TYPE_6
27213ba3 4557#undef DEF_FUNCTION_TYPE_7
d2d4bdde 4558#undef DEF_FUNCTION_TYPE_VAR_0
4559#undef DEF_FUNCTION_TYPE_VAR_1
4560#undef DEF_FUNCTION_TYPE_VAR_2
03901330 4561#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 4562#undef DEF_FUNCTION_TYPE_VAR_4
4563#undef DEF_FUNCTION_TYPE_VAR_5
d2d4bdde 4564#undef DEF_POINTER_TYPE
27213ba3 4565 BT_LAST
4566};
4567
4568typedef enum c_builtin_type builtin_type;
d2d4bdde 4569
27213ba3 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];
d2d4bdde 4573
27213ba3 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 {
7d339f93 4593 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 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
dce22712 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
87eb1c28 4686 targetm.init_builtins ();
4687
dce22712 4688 build_common_builtin_nodes ();
4689
dce22712 4690 if (flag_mudflap)
4691 mudflap_init ();
4692}
4693
9e6687c8 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
27213ba3 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{
924bbf02 4710 int char16_type_size;
4711 int char32_type_size;
174fcc61 4712 int wchar_type_size;
4713 tree array_domain_type;
2d47cc32 4714 tree va_list_ref_type_node;
8a15c04a 4715 tree va_list_arg_type_node;
a66c9326 4716
174fcc61 4717 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 4718 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 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? */
c0f19401 4724 if (c_dialect_cxx ())
d946ea19 4725 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 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);
c0f19401 4730 if (c_dialect_cxx ())
174fcc61 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);
c0f19401 4736 if (c_dialect_cxx ())
174fcc61 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);
c0f19401 4742 if (c_dialect_cxx ())
174fcc61 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
771d21fa 4750 /* These are types that c_common_type_for_size and
4751 c_common_type_for_mode use. */
dc24ddbd 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));
174fcc61 4760#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 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));
174fcc61 4765#endif
dc24ddbd 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));
174fcc61 4774#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 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));
174fcc61 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);
dc24ddbd 4784 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4785 widest_integer_literal_type_node));
174fcc61 4786
4787 widest_unsigned_literal_type_node
4788 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
dc24ddbd 4789 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4790 widest_unsigned_literal_type_node));
174fcc61 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. */
654ef926 4795 size_type_node =
174fcc61 4796 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
654ef926 4797 signed_size_type_node = c_common_signed_type (size_type_node);
4798 set_sizetype (size_type_node);
174fcc61 4799
73673831 4800 pid_type_node =
4801 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4802
174fcc61 4803 build_common_tree_nodes_2 (flag_short_double);
4804
d946ea19 4805 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4806 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 4807 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4808
c4503c0a 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
9421ebb9 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
dc24ddbd 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
20325f61 4891 (build_decl (TYPE_DECL, get_identifier ("complex long double"),
4892 complex_long_double_type_node));
174fcc61 4893
e256d445 4894 if (c_dialect_cxx ())
4895 /* For C++, make fileptr_type_node a distinct void * type until
4896 FILE type is defined. */
e086912e 4897 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 4898
d946ea19 4899 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 4900
6753bca0 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
fbf0afd1 4911 /* This node must not be shared. */
7c446c95 4912 void_zero_node = make_node (INTEGER_CST);
d2d4bdde 4913 TREE_TYPE (void_zero_node) = void_type_node;
4914
174fcc61 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
d2d4bdde 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
174fcc61 4939 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 4940 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 4941 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4942 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 4943 underlying_wchar_type_node = wchar_type_node;
c0f19401 4944 if (c_dialect_cxx ())
174fcc61 4945 {
78a8ed03 4946 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 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 }
174fcc61 4952
4953 /* This is for wide string constants. */
4954 wchar_array_type_node
4955 = build_array_type (wchar_type_node, array_domain_type);
4956
924bbf02 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
6bf5ed8d 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
f3449a3c 4997 if (SIG_ATOMIC_TYPE)
4998 sig_atomic_type_node =
9e6687c8 4999 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 5000 if (INT8_TYPE)
5001 int8_type_node =
9e6687c8 5002 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 5003 if (INT16_TYPE)
5004 int16_type_node =
9e6687c8 5005 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 5006 if (INT32_TYPE)
5007 int32_type_node =
9e6687c8 5008 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 5009 if (INT64_TYPE)
5010 int64_type_node =
9e6687c8 5011 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 5012 if (UINT8_TYPE)
5013 uint8_type_node =
9e6687c8 5014 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 5015 if (UINT16_TYPE)
5016 uint16_type_node =
9e6687c8 5017 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 5018 if (UINT32_TYPE)
5019 c_uint32_type_node =
9e6687c8 5020 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 5021 if (UINT64_TYPE)
5022 c_uint64_type_node =
9e6687c8 5023 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 5024 if (INT_LEAST8_TYPE)
5025 int_least8_type_node =
9e6687c8 5026 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 5027 if (INT_LEAST16_TYPE)
5028 int_least16_type_node =
9e6687c8 5029 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 5030 if (INT_LEAST32_TYPE)
5031 int_least32_type_node =
9e6687c8 5032 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 5033 if (INT_LEAST64_TYPE)
5034 int_least64_type_node =
9e6687c8 5035 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 5036 if (UINT_LEAST8_TYPE)
5037 uint_least8_type_node =
9e6687c8 5038 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 5039 if (UINT_LEAST16_TYPE)
5040 uint_least16_type_node =
9e6687c8 5041 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 5042 if (UINT_LEAST32_TYPE)
5043 uint_least32_type_node =
9e6687c8 5044 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 5045 if (UINT_LEAST64_TYPE)
5046 uint_least64_type_node =
9e6687c8 5047 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 5048 if (INT_FAST8_TYPE)
5049 int_fast8_type_node =
9e6687c8 5050 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 5051 if (INT_FAST16_TYPE)
5052 int_fast16_type_node =
9e6687c8 5053 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 5054 if (INT_FAST32_TYPE)
5055 int_fast32_type_node =
9e6687c8 5056 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 5057 if (INT_FAST64_TYPE)
5058 int_fast64_type_node =
9e6687c8 5059 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 5060 if (UINT_FAST8_TYPE)
5061 uint_fast8_type_node =
9e6687c8 5062 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 5063 if (UINT_FAST16_TYPE)
5064 uint_fast16_type_node =
9e6687c8 5065 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 5066 if (UINT_FAST32_TYPE)
5067 uint_fast32_type_node =
9e6687c8 5068 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 5069 if (UINT_FAST64_TYPE)
5070 uint_fast64_type_node =
9e6687c8 5071 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 5072 if (INTPTR_TYPE)
5073 intptr_type_node =
9e6687c8 5074 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 5075 if (UINTPTR_TYPE)
5076 uintptr_type_node =
9e6687c8 5077 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 5078
6bf5ed8d 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)));
11773141 5082 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 5083
dc24ddbd 5084 lang_hooks.decls.pushdecl
20325f61 5085 (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
5086 va_list_type_node));
5f57a8b1 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
8a15c04a 5101
8a15c04a 5102 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 5103 {
5104 va_list_arg_type_node = va_list_ref_type_node =
5105 build_pointer_type (TREE_TYPE (va_list_type_node));
5106 }
8a15c04a 5107 else
2d47cc32 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 }
1cae46be 5112
dce22712 5113 if (!flag_preprocess_only)
5114 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 5115
5c62f199 5116 main_identifier_node = get_identifier ("main");
ae84079f 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);
27213ba3 5122
5123 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5124 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 5125}
a66c9326 5126
46a7e216 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
f7f07c95 5129 function decl that declares a builtin. */
46a7e216 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);
979d3efc 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 }
46a7e216 5163}
5164
79b01846 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
a66c9326 5180tree
1cae46be 5181build_va_arg (tree expr, tree type)
a66c9326 5182{
5183 return build1 (VA_ARG_EXPR, type, expr);
5184}
0d4238dc 5185
5186
dd878098 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
1cae46be 5196static bool builtin_function_disabled_p (const char *);
dd878098 5197
5198/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5199 begins with "__builtin_", give an error. */
5200
5201void
1cae46be 5202disable_builtin_function (const char *name)
dd878098 5203{
5204 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 5205 error ("cannot disable built-in function %qs", name);
dd878098 5206 else
5207 {
e85905e5 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;
dd878098 5212 }
5213}
5214
5215
5216/* Return true if the built-in function NAME has been disabled, false
5217 otherwise. */
5218
5219static bool
1cae46be 5220builtin_function_disabled_p (const char *name)
dd878098 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
3237155d 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. */
0d4238dc 5236
3237155d 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)
0d4238dc 5244{
3237155d 5245 tree decl;
5246 const char *libname;
5247
27213ba3 5248 if (fntype == error_mark_node)
5249 return;
5250
3237155d 5251 gcc_assert ((!both_p && !fallback_p)
5252 || !strncmp (name, "__builtin_",
5253 strlen ("__builtin_")));
5254
5255 libname = name + strlen ("__builtin_");
54be5d7e 5256 decl = add_builtin_function (name, fntype, fncode, fnclass,
5257 (fallback_p ? libname : NULL),
5258 fnattrs);
3237155d 5259 if (both_p
5260 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 5261 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 5262 add_builtin_function (libname, libtype, fncode, fnclass,
5263 NULL, fnattrs);
a40609e6 5264
3237155d 5265 built_in_decls[(int) fncode] = decl;
5266 if (implicit_p)
5267 implicit_built_in_decls[(int) fncode] = decl;
0d4238dc 5268}
e94026da 5269\f
d7aeef06 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
9f627b1a 5274c_promoting_integer_type_p (const_tree t)
d7aeef06 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
7aa1e6eb 5283 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5284 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 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
e94026da 5300/* Return 1 if PARMS specifies a fixed number of parameters
5301 and none of their types is affected by default promotions. */
5302
5303int
9f627b1a 5304self_promoting_args_p (const_tree parms)
e94026da 5305{
9f627b1a 5306 const_tree t;
e94026da 5307 for (t = parms; t; t = TREE_CHAIN (t))
5308 {
19cb6b50 5309 tree type = TREE_VALUE (t);
43f74bc4 5310
e1d8e198 5311 if (type == error_mark_node)
5312 continue;
5313
e94026da 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
d7aeef06 5323 if (c_promoting_integer_type_p (type))
e94026da 5324 return 0;
5325 }
5326 return 1;
5327}
605fb01e 5328
c10de5e7 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
57a0ed23 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
e41f0d80 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
1cae46be 5353case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 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
eaae3b75 5371 switch-statement itself. Returns the CASE_LABEL_EXPR created, or
5372 ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
e41f0d80 5373
5374tree
2ca392fd 5375c_add_case_label (splay_tree cases, tree cond, tree orig_type,
5376 tree low_value, tree high_value)
e41f0d80 5377{
5378 tree type;
5379 tree label;
5380 tree case_label;
5381 splay_tree_node node;
5382
5383 /* Create the LABEL_DECL itself. */
4ee9c684 5384 label = create_artificial_label ();
e41f0d80 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)
4ee9c684 5389 goto error_out;
e41f0d80 5390
1cae46be 5391 if ((low_value && TREE_TYPE (low_value)
5392 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 5393 || (high_value && TREE_TYPE (high_value)
5394 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 5395 {
5396 error ("pointers are not permitted as case values");
5397 goto error_out;
5398 }
e41f0d80 5399
5400 /* Case ranges are a GNU extension. */
8864917d 5401 if (high_value)
21ca8540 5402 pedwarn (input_location, OPT_pedantic,
8864917d 5403 "range expressions in switch statements are non-standard");
e41f0d80 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);
96722196 5410 if (low_value == error_mark_node)
5411 goto error_out;
e41f0d80 5412 }
5413 if (high_value)
5414 {
5415 high_value = check_case_value (high_value);
5416 high_value = convert_and_check (type, high_value);
96722196 5417 if (high_value == error_mark_node)
5418 goto error_out;
e41f0d80 5419 }
5420
96722196 5421 if (low_value && high_value)
5422 {
5423 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 5424 really a case range, even though it was written that way.
5425 Remove the HIGH_VALUE to simplify later processing. */
96722196 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 }
e41f0d80 5431
2ca392fd 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
84166705 5436 && !check_case_bounds (type, orig_type,
5437 &low_value, high_value ? &high_value : NULL))
2ca392fd 5438 return NULL_TREE;
5439
e41f0d80 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. */
1cae46be 5475 else if (high_bound
e41f0d80 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 {
eaae3b75 5485 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 5486
5487 if (high_value)
5488 {
5489 error ("duplicate (or overlapping) case value");
9bc3739f 5490 error ("%Jthis is the first entry overlapping that value", duplicate);
e41f0d80 5491 }
5492 else if (low_value)
5493 {
5494 error ("duplicate case value") ;
9bc3739f 5495 error ("%Jpreviously used here", duplicate);
e41f0d80 5496 }
5497 else
5498 {
5499 error ("multiple default labels in one switch");
9bc3739f 5500 error ("%Jthis is the first default label", duplicate);
e41f0d80 5501 }
4ee9c684 5502 goto error_out;
e41f0d80 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. */
1cae46be 5508 splay_tree_insert (cases,
e41f0d80 5509 (splay_tree_key) low_value,
5510 (splay_tree_value) case_label);
5511
5512 return case_label;
4ee9c684 5513
5514 error_out:
daf6dff5 5515 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 5516 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 5517 that just leads to duplicates and thence to failure later on. */
4ee9c684 5518 if (!cases->root)
5519 {
5520 tree t = create_artificial_label ();
daf6dff5 5521 add_stmt (build_stmt (LABEL_EXPR, t));
4ee9c684 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,
7df7561b 5546 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5547 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
4ee9c684 5548
5549 if (TYPE_NAME (type) == 0)
6cbbbc89 5550 warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5551 "%Jcase value %qs not in enumerated type",
eaae3b75 5552 CASE_LABEL (label), buf);
4ee9c684 5553 else
6cbbbc89 5554 warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5555 "%Jcase value %qs not in enumerated type %qT",
eaae3b75 5556 CASE_LABEL (label), buf, type);
4ee9c684 5557}
5558
359d87c6 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
4ee9c684 5562static int
5563match_case_to_enum (splay_tree_node node, void *data)
5564{
5565 tree label = (tree) node->value;
4fd61bc6 5566 tree type = (tree) data;
4ee9c684 5567
5568 /* Skip default case. */
5569 if (!CASE_LOW (label))
5570 return 0;
5571
359d87c6 5572 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 5573 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 5574 if (!CASE_LOW_SEEN (label))
4ee9c684 5575 match_case_to_enum_1 (CASE_LOW (label), type, label);
5576 else
359d87c6 5577 CASE_LOW_SEEN (label) = 0;
4ee9c684 5578
359d87c6 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. */
4ee9c684 5582 if (CASE_HIGH (label))
5583 {
359d87c6 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;
4ee9c684 5588 }
5589
5590 return 0;
5591}
5592
e7911019 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. */
4ee9c684 5599
e7911019 5600void
5601c_do_switch_warnings (splay_tree cases, location_t switch_location,
5602 tree type, tree cond)
4ee9c684 5603{
b27ac6b5 5604 splay_tree_node default_node;
359d87c6 5605 splay_tree_node node;
5606 tree chain;
6cbbbc89 5607 int saved_warn_switch;
4ee9c684 5608
5609 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5610 return;
5611
4ee9c684 5612 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 5613 if (!default_node)
5614 warning (OPT_Wswitch_default, "%Hswitch missing default case",
5615 &switch_location);
4ee9c684 5616
359d87c6 5617 /* From here on, we only care about about enumerated types. */
5618 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5619 return;
5620
4ee9c684 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. */
4ee9c684 5625
359d87c6 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. */
4ee9c684 5639
359d87c6 5640 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5641 {
5642 tree value = TREE_VALUE (chain);
3f00a6c0 5643 if (TREE_CODE (value) == CONST_DECL)
5644 value = DECL_INITIAL (value);
359d87c6 5645 node = splay_tree_lookup (cases, (splay_tree_key) value);
5646 if (node)
4ee9c684 5647 {
359d87c6 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
f0b5f617 5655 case range which includes the enumerator's value. */
359d87c6 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)
4ee9c684 5662 {
359d87c6 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;
4ee9c684 5668 }
5669 }
5670
359d87c6 5671 /* We've now determined that this enumerated literal isn't
5672 handled by the case labels of the switch statement. */
4ee9c684 5673
359d87c6 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;
4ee9c684 5678
6cbbbc89 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",
359d87c6 5686 &switch_location, TREE_PURPOSE (chain));
4ee9c684 5687 }
359d87c6 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. */
6cbbbc89 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;
359d87c6 5705 splay_tree_foreach (cases, match_case_to_enum, type);
6cbbbc89 5706 warn_switch = saved_warn_switch;
5707
e41f0d80 5708}
5709
9dd48740 5710/* Finish an expression taking the address of LABEL (an
dda49785 5711 IDENTIFIER_NODE). Returns an expression for the address.
5712
5713 LOC is the location for the expression returned. */
d0a47c8d 5714
1cae46be 5715tree
dda49785 5716finish_label_address_expr (tree label, location_t loc)
d0a47c8d 5717{
5718 tree result;
5719
21ca8540 5720 pedwarn (input_location, OPT_pedantic, "taking the address of a label is non-standard");
d0a47c8d 5721
9dd48740 5722 if (label == error_mark_node)
5723 return error_mark_node;
5724
d0a47c8d 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);
d0a47c8d 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. */
dda49785 5735 protected_set_expr_location (result, loc);
d0a47c8d 5736 }
5737
5738 return result;
5739}
4f9a1c9b 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
1cae46be 5746boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 5747{
5748 tree val;
69db191c 5749 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 5750
4f9a1c9b 5751 arg = stabilize_reference (arg);
5752 switch (code)
5753 {
5754 case PREINCREMENT_EXPR:
14ae0310 5755 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 5756 break;
5757 case POSTINCREMENT_EXPR:
14ae0310 5758 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 5759 arg = save_expr (arg);
14ae0310 5760 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5761 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 5762 break;
5763 case PREDECREMENT_EXPR:
14ae0310 5764 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5765 invert_truthvalue (arg));
4f9a1c9b 5766 break;
5767 case POSTDECREMENT_EXPR:
14ae0310 5768 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5769 invert_truthvalue (arg));
4f9a1c9b 5770 arg = save_expr (arg);
14ae0310 5771 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5772 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 5773 break;
5774 default:
231bd014 5775 gcc_unreachable ();
4f9a1c9b 5776 }
5777 TREE_SIDE_EFFECTS (val) = 1;
5778 return val;
5779}
76a6e674 5780\f
f3449a3c 5781/* Built-in macros for stddef.h and stdint.h, that require macros
5782 defined in this file. */
79cf3ec1 5783void
1cae46be 5784c_stddef_cpp_builtins(void)
1ed9d5f5 5785{
63994318 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);
36bccbfc 5790 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5791 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 5792 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5793 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 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);
574006c3 5848}
5849
7d3b509a 5850static void
1cae46be 5851c_init_attributes (void)
7d3b509a 5852{
5853 /* Fill in the built_in_attributes array. */
7c446c95 5854#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 5855 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 5856#define DEF_ATTR_INT(ENUM, VALUE) \
7016c612 5857 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
7d3b509a 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]);
7d3b509a 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
76a6e674 5870}
5f3cead1 5871
f8e93a2e 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
9a03a746 5878handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 5879 int flags, bool *no_add_attrs)
f8e93a2e 5880{
f40175cb 5881 if (TYPE_P (*node))
f8e93a2e 5882 {
5883 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 5884 *node = build_variant_type_copy (*node);
f40175cb 5885 TYPE_PACKED (*node) = 1;
f8e93a2e 5886 }
5887 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 5888 {
9fd767c5 5889 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
5890 /* Still pack bitfields. */
5891 && ! DECL_INITIAL (*node))
c2ab04f9 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 }
f8e93a2e 5898 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 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. */
f8e93a2e 5902 else
5903 {
9b2d6d13 5904 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 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
1cae46be 5915handle_nocommon_attribute (tree *node, tree name,
9a03a746 5916 tree ARG_UNUSED (args),
5917 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 5918{
5919 if (TREE_CODE (*node) == VAR_DECL)
5920 DECL_COMMON (*node) = 0;
5921 else
5922 {
9b2d6d13 5923 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 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
9a03a746 5934handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5935 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 5936{
5937 if (TREE_CODE (*node) == VAR_DECL)
5938 DECL_COMMON (*node) = 1;
5939 else
5940 {
9b2d6d13 5941 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 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
9a03a746 5952handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5953 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 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),
71bda45d 5965 TYPE_READONLY (TREE_TYPE (type)), 1));
f8e93a2e 5966 else
5967 {
9b2d6d13 5968 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 5969 *no_add_attrs = true;
5970 }
5971
5972 return NULL_TREE;
5973}
5974
5de92639 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),
46f8e3b0 5980 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 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 }
24470055 5990 /* Most of the rest of the hot processing is done later with
5991 lookup_attribute. */
5de92639 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 }
24470055 6016 /* Most of the rest of the cold processing is done later with
6017 lookup_attribute. */
5de92639 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
f8e93a2e 6028/* Handle a "noinline" attribute; arguments as in
6029 struct attribute_spec.handler. */
6030
6031static tree
1cae46be 6032handle_noinline_attribute (tree *node, tree name,
9a03a746 6033 tree ARG_UNUSED (args),
6034 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6035{
6036 if (TREE_CODE (*node) == FUNCTION_DECL)
6037 DECL_UNINLINABLE (*node) = 1;
6038 else
6039 {
9b2d6d13 6040 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 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
1cae46be 6051handle_always_inline_attribute (tree *node, tree name,
9a03a746 6052 tree ARG_UNUSED (args),
6053 int ARG_UNUSED (flags),
09347743 6054 bool *no_add_attrs)
f8e93a2e 6055{
6056 if (TREE_CODE (*node) == FUNCTION_DECL)
6057 {
ebb7d626 6058 /* Set the attribute and mark it for disregarding inline
6059 limits. */
6060 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 6061 }
6062 else
6063 {
9b2d6d13 6064 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6065 *no_add_attrs = true;
541e4101 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;
f8e93a2e 6089 }
6090
6091 return NULL_TREE;
6092}
6093
1b16fc45 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
0cdd9887 6117/* Handle a "flatten" attribute; arguments as in
6118 struct attribute_spec.handler. */
6119
6120static tree
6121handle_flatten_attribute (tree *node, tree name,
a0c938f0 6122 tree args ATTRIBUTE_UNUSED,
6123 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 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
10fc867f 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}
0cdd9887 6158
f8e93a2e 6159/* Handle a "used" attribute; arguments as in
6160 struct attribute_spec.handler. */
6161
6162static tree
9a03a746 6163handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6164 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6165{
d0a31bd8 6166 tree node = *pnode;
6167
6168 if (TREE_CODE (node) == FUNCTION_DECL
6169 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)))
f54ed8bc 6170 {
f54ed8bc 6171 TREE_USED (node) = 1;
9423c9b7 6172 DECL_PRESERVE_P (node) = 1;
f54ed8bc 6173 }
f8e93a2e 6174 else
6175 {
9b2d6d13 6176 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 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
9a03a746 6187handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6188 int flags, bool *no_add_attrs)
f8e93a2e 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 {
9b2d6d13 6202 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6203 *no_add_attrs = true;
6204 }
6205 }
6206 else
6207 {
6208 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6209 *node = build_variant_type_copy (*node);
f8e93a2e 6210 TREE_USED (*node) = 1;
6211 }
6212
6213 return NULL_TREE;
6214}
6215
62eec3b4 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
ba12ea31 6227 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
62eec3b4 6228 {
ba12ea31 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 }
62eec3b4 6236 }
62eec3b4 6237 else
6238 {
6239 warning (OPT_Wattributes, "%qE attribute ignored", name);
6240 *no_add_attrs = true;
6241 }
6242
6243 return NULL_TREE;
6244}
6245
f8e93a2e 6246/* Handle a "const" attribute; arguments as in
6247 struct attribute_spec.handler. */
6248
6249static tree
9a03a746 6250handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6251 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 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 {
9b2d6d13 6266 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 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
1cae46be 6277handle_transparent_union_attribute (tree *node, tree name,
9a03a746 6278 tree ARG_UNUSED (args), int flags,
09347743 6279 bool *no_add_attrs)
f8e93a2e 6280{
881eb642 6281 tree type;
03908818 6282
6283 *no_add_attrs = true;
f8e93a2e 6284
881eb642 6285 if (TREE_CODE (*node) == TYPE_DECL)
6286 node = &TREE_TYPE (*node);
6287 type = *node;
f8e93a2e 6288
03908818 6289 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 6290 {
03908818 6291 /* When IN_PLACE is set, leave the check for FIELDS and MODE to
6292 the code in finish_struct. */
f8e93a2e 6293 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 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;
f8e93a2e 6306 }
6307
03908818 6308 ignored:
6309 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6310 return NULL_TREE;
6311}
6312
9af7fd5b 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;
6c181a06 6322 tree arg;
9af7fd5b 6323
6324 if (!args)
6325 return DEFAULT_INIT_PRIORITY;
6c181a06 6326
28fbc04f 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
6c181a06 6336 arg = TREE_VALUE (args);
6337 if (!host_integerp (arg, /*pos=*/0)
6338 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 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
f8e93a2e 6370/* Handle a "constructor" attribute; arguments as in
6371 struct attribute_spec.handler. */
6372
6373static tree
9af7fd5b 6374handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 6375 int ARG_UNUSED (flags),
09347743 6376 bool *no_add_attrs)
f8e93a2e 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 {
9af7fd5b 6385 priority_type priority;
f8e93a2e 6386 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 6387 priority = get_priority (args, /*is_destructor=*/false);
6388 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 6389 TREE_USED (decl) = 1;
6390 }
6391 else
6392 {
9b2d6d13 6393 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 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
9af7fd5b 6404handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 6405 int ARG_UNUSED (flags),
09347743 6406 bool *no_add_attrs)
f8e93a2e 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 {
9af7fd5b 6415 priority_type priority;
f8e93a2e 6416 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 6417 priority = get_priority (args, /*is_destructor=*/true);
6418 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 6419 TREE_USED (decl) = 1;
6420 }
6421 else
6422 {
9b2d6d13 6423 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 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
9a03a746 6434handle_mode_attribute (tree *node, tree name, tree args,
6435 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6436{
6437 tree type = *node;
d1dd9ac0 6438 tree ident = TREE_VALUE (args);
f8e93a2e 6439
6440 *no_add_attrs = true;
6441
d1dd9ac0 6442 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 6443 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6444 else
6445 {
6446 int j;
d1dd9ac0 6447 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 6448 int len = strlen (p);
6449 enum machine_mode mode = VOIDmode;
6450 tree typefm;
b2aef146 6451 bool valid_mode;
f8e93a2e 6452
6453 if (len > 4 && p[0] == '_' && p[1] == '_'
6454 && p[len - 1] == '_' && p[len - 2] == '_')
6455 {
4fd61bc6 6456 char *newp = (char *) alloca (len - 1);
f8e93a2e 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. */
84166705 6465 if (!strcmp (p, "byte"))
f8e93a2e 6466 mode = byte_mode;
6467 else if (!strcmp (p, "word"))
6468 mode = word_mode;
84166705 6469 else if (!strcmp (p, "pointer"))
f8e93a2e 6470 mode = ptr_mode;
0ef89dfd 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 ();
1bd43494 6475 else if (!strcmp (p, "unwind_word"))
6476 mode = targetm.unwind_word_mode ();
f8e93a2e 6477 else
6478 for (j = 0; j < NUM_MACHINE_MODES; j++)
6479 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 6480 {
6481 mode = (enum machine_mode) j;
6482 break;
6483 }
f8e93a2e 6484
6485 if (mode == VOIDmode)
4917c376 6486 {
d1dd9ac0 6487 error ("unknown machine mode %qE", ident);
4917c376 6488 return NULL_TREE;
6489 }
6490
b2aef146 6491 valid_mode = false;
6492 switch (GET_MODE_CLASS (mode))
4917c376 6493 {
b2aef146 6494 case MODE_INT:
6495 case MODE_PARTIAL_INT:
6496 case MODE_FLOAT:
c4503c0a 6497 case MODE_DECIMAL_FLOAT:
9421ebb9 6498 case MODE_FRACT:
6499 case MODE_UFRACT:
6500 case MODE_ACCUM:
6501 case MODE_UACCUM:
b2aef146 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:
9421ebb9 6512 case MODE_VECTOR_FRACT:
6513 case MODE_VECTOR_UFRACT:
6514 case MODE_VECTOR_ACCUM:
6515 case MODE_VECTOR_UACCUM:
9b2d6d13 6516 warning (OPT_Wattributes, "specifying vector types with "
6517 "__attribute__ ((mode)) is deprecated");
6518 warning (OPT_Wattributes,
6519 "use __attribute__ ((vector_size)) instead");
b2aef146 6520 valid_mode = vector_mode_valid_p (mode);
6521 break;
4917c376 6522
b2aef146 6523 default:
6524 break;
6525 }
6526 if (!valid_mode)
6527 {
1e5fcbe2 6528 error ("unable to emulate %qs", p);
b2aef146 6529 return NULL_TREE;
6530 }
4917c376 6531
b2aef146 6532 if (POINTER_TYPE_P (type))
ead34f59 6533 {
b2aef146 6534 tree (*fn)(tree, enum machine_mode, bool);
6535
6536 if (!targetm.valid_pointer_mode (mode))
ead34f59 6537 {
1e5fcbe2 6538 error ("invalid pointer mode %qs", p);
ead34f59 6539 return NULL_TREE;
6540 }
6541
a0c938f0 6542 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 6543 fn = build_pointer_type_for_mode;
805e22b2 6544 else
b2aef146 6545 fn = build_reference_type_for_mode;
6546 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 6547 }
b2aef146 6548 else
9421ebb9 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 }
3a648ab9 6563
b2aef146 6564 if (typefm == NULL_TREE)
6565 {
743a6f47 6566 error ("no data type for mode %qs", p);
b2aef146 6567 return NULL_TREE;
6568 }
3a648ab9 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 {
743a6f47 6576 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 6577 return NULL_TREE;
6578 }
6579
10080eac 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 }
3a648ab9 6598 }
4bf450a1 6599 else if (VECTOR_MODE_P (mode)
6600 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
6601 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 6602 {
6603 error ("mode %qs applied to inappropriate type", p);
6604 return NULL_TREE;
6605 }
6606
b2aef146 6607 *node = typefm;
f8e93a2e 6608 }
6609
6610 return NULL_TREE;
6611}
6612
6613/* Handle a "section" attribute; arguments as in
6614 struct attribute_spec.handler. */
6615
6616static tree
9a03a746 6617handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6618 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6619{
6620 tree decl = *node;
6621
6622 if (targetm.have_named_sections)
6623 {
065efcb1 6624 user_defined_section_attribute = true;
6625
f8e93a2e 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
84166705 6632 && !TREE_STATIC (decl))
f8e93a2e 6633 {
9bc3739f 6634 error ("%Jsection attribute cannot be specified for "
4ee9c684 6635 "local variables", decl);
f8e93a2e 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 {
3cf8b391 6645 error ("section of %q+D conflicts with previous declaration",
6646 *node);
f8e93a2e 6647 *no_add_attrs = true;
6648 }
38475469 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 }
f8e93a2e 6656 else
6657 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
6658 }
6659 else
6660 {
3cf8b391 6661 error ("section attribute not allowed for %q+D", *node);
f8e93a2e 6662 *no_add_attrs = true;
6663 }
6664 }
6665 else
6666 {
9bc3739f 6667 error ("%Jsection attributes are not supported for this target", *node);
f8e93a2e 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
9a03a746 6678handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 6679 int flags, bool *no_add_attrs)
f8e93a2e 6680{
6681 tree decl = NULL_TREE;
6682 tree *type = NULL;
6683 int is_type = 0;
6684 tree align_expr = (args ? TREE_VALUE (args)
02421213 6685 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
f8e93a2e 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
f8e93a2e 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 }
7cfdc2f0 6707 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
f8e93a2e 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);
e086912e 6720 *type = build_variant_type_copy (*type);
f8e93a2e 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))
e086912e 6727 *type = build_variant_type_copy (*type);
f8e93a2e 6728
7cfdc2f0 6729 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 6730 TYPE_USER_ALIGN (*type) = 1;
6731 }
097b5c8b 6732 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 6733 && TREE_CODE (decl) != FIELD_DECL)
6734 {
3cf8b391 6735 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 6736 *no_add_attrs = true;
6737 }
097b5c8b 6738 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 6739 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 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 }
f8e93a2e 6750 else
6751 {
7cfdc2f0 6752 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 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
f948b309 6763handle_weak_attribute (tree *node, tree name,
9a03a746 6764 tree ARG_UNUSED (args),
6765 int ARG_UNUSED (flags),
6766 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 6767{
f948b309 6768 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 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)
f948b309 6776 declare_weak (*node);
6777 else
6778 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6779
6780 return NULL_TREE;
6781}
6782
6783/* Handle an "alias" attribute; arguments as in
6784 struct attribute_spec.handler. */
6785
6786static tree
1cae46be 6787handle_alias_attribute (tree *node, tree name, tree args,
9a03a746 6788 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6789{
6790 tree decl = *node;
6791
9e830260 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))
0a3ecdc1 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)))
f8e93a2e 6804 {
3cf8b391 6805 error ("%q+D defined both normally and as an alias", decl);
f8e93a2e 6806 *no_add_attrs = true;
6807 }
8c42f0d9 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)
f8e93a2e 6815 {
6816 tree id;
6817
6818 id = TREE_VALUE (args);
6819 if (TREE_CODE (id) != STRING_CST)
6820 {
07e3a3d2 6821 error ("alias argument not a string");
f8e93a2e 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
4ee9c684 6832 {
f4a30bd7 6833 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6834 DECL_EXTERNAL (decl) = 1;
6835 else
6836 DECL_EXTERNAL (decl) = 0;
4ee9c684 6837 TREE_STATIC (decl) = 1;
6838 }
f8e93a2e 6839 }
6840 else
6841 {
9b2d6d13 6842 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6843 *no_add_attrs = true;
6844 }
6845
6846 return NULL_TREE;
6847}
6848
f4a30bd7 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
83852912 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. */
64a7bd81 6861 if (decl_function_context (*node)
6862 || current_function_decl
6863 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
83852912 6864 {
6865 warning (OPT_Wattributes, "%qE attribute ignored", name);
6866 *no_add_attrs = true;
6867 return NULL_TREE;
6868 }
6869
f4a30bd7 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;
0a3ecdc1 6880
6881 decl_attributes (node, attr, flags);
f4a30bd7 6882 }
6883 else
6884 {
6885 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
6886 error ("%Jweakref attribute must appear before alias attribute",
6887 *node);
6888
0a3ecdc1 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;
f4a30bd7 6893 }
6894
f4a30bd7 6895 return NULL_TREE;
6896}
6897
f8e93a2e 6898/* Handle an "visibility" attribute; arguments as in
6899 struct attribute_spec.handler. */
6900
6901static tree
1cae46be 6902handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 6903 int ARG_UNUSED (flags),
4a2849cb 6904 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 6905{
6906 tree decl = *node;
9c40570a 6907 tree id = TREE_VALUE (args);
4a2849cb 6908 enum symbol_visibility vis;
f8e93a2e 6909
b212f378 6910 if (TYPE_P (*node))
6911 {
4a2849cb 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 }
b212f378 6926 }
84166705 6927 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 6928 {
9b2d6d13 6929 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 6930 return NULL_TREE;
f8e93a2e 6931 }
f8e93a2e 6932
9c40570a 6933 if (TREE_CODE (id) != STRING_CST)
6934 {
07e3a3d2 6935 error ("visibility argument not a string");
9c40570a 6936 return NULL_TREE;
f8e93a2e 6937 }
b27ac6b5 6938
b212f378 6939 /* If this is a type, set the visibility on the type decl. */
6940 if (TYPE_P (decl))
6941 {
6942 decl = TYPE_NAME (decl);
84166705 6943 if (!decl)
a0c938f0 6944 return NULL_TREE;
e147aab3 6945 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6946 {
9b2d6d13 6947 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 6948 name);
6949 return NULL_TREE;
6950 }
b212f378 6951 }
f8e93a2e 6952
9c40570a 6953 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 6954 vis = VISIBILITY_DEFAULT;
9c40570a 6955 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 6956 vis = VISIBILITY_INTERNAL;
9c40570a 6957 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 6958 vis = VISIBILITY_HIDDEN;
9c40570a 6959 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 6960 vis = VISIBILITY_PROTECTED;
9c40570a 6961 else
4a2849cb 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)
098a01e7 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 }
4a2849cb 6984
6985 DECL_VISIBILITY (decl) = vis;
b212f378 6986 DECL_VISIBILITY_SPECIFIED (decl) = 1;
6987
4a2849cb 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. */
9c40570a 6992
f8e93a2e 6993 return NULL_TREE;
6994}
6995
3aa0c315 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{
1d8fc210 7006 gcc_assert (TREE_CODE (decl) == VAR_DECL
7007 || TREE_CODE (decl) == FUNCTION_DECL);
3aa0c315 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. */
098a01e7 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)))))
3aa0c315 7020 return true;
7021
4a2849cb 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 {
2d9d8740 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 }
4a2849cb 7038 }
3aa0c315 7039 return false;
7040}
7041
24dfead4 7042/* Handle an "tls_model" attribute; arguments as in
7043 struct attribute_spec.handler. */
7044
7045static tree
1cae46be 7046handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 7047 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 7048{
1b53eb20 7049 tree id;
24dfead4 7050 tree decl = *node;
1b53eb20 7051 enum tls_model kind;
24dfead4 7052
1b53eb20 7053 *no_add_attrs = true;
7054
c34f8a78 7055 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
24dfead4 7056 {
9b2d6d13 7057 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 7058 return NULL_TREE;
24dfead4 7059 }
24dfead4 7060
1b53eb20 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;
24dfead4 7067 }
7068
1b53eb20 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;
24dfead4 7081 return NULL_TREE;
7082}
7083
f8e93a2e 7084/* Handle a "no_instrument_function" attribute; arguments as in
7085 struct attribute_spec.handler. */
7086
7087static tree
1cae46be 7088handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 7089 tree ARG_UNUSED (args),
7090 int ARG_UNUSED (flags),
09347743 7091 bool *no_add_attrs)
f8e93a2e 7092{
7093 tree decl = *node;
7094
7095 if (TREE_CODE (decl) != FUNCTION_DECL)
7096 {
b0b1af64 7097 error ("%J%qE attribute applies only to functions", decl, name);
f8e93a2e 7098 *no_add_attrs = true;
7099 }
7100 else if (DECL_INITIAL (decl))
7101 {
b0b1af64 7102 error ("%Jcan%'t set %qE attribute after definition", decl, name);
f8e93a2e 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
9a03a746 7115handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7116 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7117{
a5147fca 7118 if (TREE_CODE (*node) == FUNCTION_DECL
7119 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 7120 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 7121 else
7122 {
9b2d6d13 7123 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7124 *no_add_attrs = true;
7125 }
7126
7127 return NULL_TREE;
7128}
7129
4a29c97c 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{
2802826e 7137 unsigned arg_count = type_num_arguments (*node);
4a29c97c 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
26d1c5ff 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 {
9b2d6d13 7167 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 7168 *no_add_attrs = true;
7169 }
7170
7171 return NULL_TREE;
7172}
7173
f8e93a2e 7174/* Handle a "no_limit_stack" attribute; arguments as in
7175 struct attribute_spec.handler. */
7176
7177static tree
1cae46be 7178handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 7179 tree ARG_UNUSED (args),
7180 int ARG_UNUSED (flags),
09347743 7181 bool *no_add_attrs)
f8e93a2e 7182{
7183 tree decl = *node;
7184
7185 if (TREE_CODE (decl) != FUNCTION_DECL)
7186 {
b0b1af64 7187 error ("%J%qE attribute applies only to functions", decl, name);
f8e93a2e 7188 *no_add_attrs = true;
7189 }
7190 else if (DECL_INITIAL (decl))
7191 {
b0b1af64 7192 error ("%Jcan%'t set %qE attribute after definition", decl, name);
f8e93a2e 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
9a03a746 7205handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7206 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7207{
7208 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 7209 DECL_PURE_P (*node) = 1;
f8e93a2e 7210 /* ??? TODO: Support types. */
7211 else
7212 {
9b2d6d13 7213 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7214 *no_add_attrs = true;
7215 }
7216
7217 return NULL_TREE;
7218}
7219
fc09b200 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
f8e93a2e 7233/* Handle a "deprecated" attribute; arguments as in
7234 struct attribute_spec.handler. */
1cae46be 7235
f8e93a2e 7236static tree
1cae46be 7237handle_deprecated_attribute (tree *node, tree name,
45c4e798 7238 tree args, int flags,
09347743 7239 bool *no_add_attrs)
f8e93a2e 7240{
7241 tree type = NULL_TREE;
7242 int warn = 0;
782858b8 7243 tree what = NULL_TREE;
1cae46be 7244
45c4e798 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
f8e93a2e 7253 if (DECL_P (*node))
7254 {
7255 tree decl = *node;
7256 type = TREE_TYPE (decl);
1cae46be 7257
f8e93a2e 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))
e086912e 7270 *node = build_variant_type_copy (*node);
f8e93a2e 7271 TREE_DEPRECATED (*node) = 1;
7272 type = *node;
7273 }
7274 else
7275 warn = 1;
1cae46be 7276
f8e93a2e 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)
782858b8 7283 what = TYPE_NAME (*node);
f8e93a2e 7284 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7285 && DECL_NAME (TYPE_NAME (type)))
782858b8 7286 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 7287 }
7288 if (what)
9b2d6d13 7289 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 7290 else
9b2d6d13 7291 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7292 }
7293
7294 return NULL_TREE;
7295}
7296
f8e93a2e 7297/* Handle a "vector_size" attribute; arguments as in
7298 struct attribute_spec.handler. */
7299
7300static tree
1cae46be 7301handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 7302 int ARG_UNUSED (flags),
09347743 7303 bool *no_add_attrs)
f8e93a2e 7304{
7305 unsigned HOST_WIDE_INT vecsize, nunits;
83e2a11b 7306 enum machine_mode orig_mode;
4917c376 7307 tree type = *node, new_type, size;
f8e93a2e 7308
7309 *no_add_attrs = true;
7310
4917c376 7311 size = TREE_VALUE (args);
4917c376 7312
84166705 7313 if (!host_integerp (size, 1))
f8e93a2e 7314 {
9b2d6d13 7315 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7316 return NULL_TREE;
7317 }
7318
7319 /* Get the vector size (in bytes). */
4917c376 7320 vecsize = tree_low_cst (size, 1);
f8e93a2e 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
5bfb0742 7332 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 7333 || TREE_CODE (type) == ARRAY_TYPE
7334 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 7335 type = TREE_TYPE (type);
7336
7337 /* Get the mode of the type being modified. */
7338 orig_mode = TYPE_MODE (type);
7339
2cb0e5d9 7340 if ((!INTEGRAL_TYPE_P (type)
7341 && !SCALAR_FLOAT_TYPE_P (type)
7342 && !FIXED_POINT_TYPE_P (type))
cee7491d 7343 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 7344 && GET_MODE_CLASS (orig_mode) != MODE_INT
7345 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
7ec31215 7346 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
7347 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 7348 {
782858b8 7349 error ("invalid vector type for attribute %qE", name);
f8e93a2e 7350 return NULL_TREE;
7351 }
7352
39cc3e6d 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
f8e93a2e 7365 /* Calculate how many units fit in the vector. */
7366 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
83e2a11b 7367 if (nunits & (nunits - 1))
f8e93a2e 7368 {
83e2a11b 7369 error ("number of components of the vector not a power of two");
f8e93a2e 7370 return NULL_TREE;
7371 }
7372
83e2a11b 7373 new_type = build_vector_type (type, nunits);
f8e93a2e 7374
7375 /* Build back pointers if needed. */
d991e6e8 7376 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 7377
7378 return NULL_TREE;
7379}
7380
dbf6c367 7381/* Handle the "nonnull" attribute. */
7382static tree
9a03a746 7383handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
7384 tree args, int ARG_UNUSED (flags),
09347743 7385 bool *no_add_attrs)
dbf6c367 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
d716ce75 7391 non-null. Verify a full prototype is given so that the arguments
dbf6c367 7392 will have the correct types when we actually check them later. */
84166705 7393 if (!args)
dbf6c367 7394 {
84166705 7395 if (!TYPE_ARG_TYPES (type))
dbf6c367 7396 {
7397 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 7398 *no_add_attrs = true;
dbf6c367 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;
4ee9c684 7408 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 7409
84166705 7410 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
dbf6c367 7411 {
07e3a3d2 7412 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 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 {
84166705 7423 if (!argument || ck_num == arg_num)
dbf6c367 7424 break;
7425 argument = TREE_CHAIN (argument);
7426 }
7427
84166705 7428 if (!argument
dbf6c367 7429 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
7430 {
07e3a3d2 7431 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 7432 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7433 *no_add_attrs = true;
7434 return NULL_TREE;
7435 }
7436
4ee9c684 7437 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
dbf6c367 7438 {
07e3a3d2 7439 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 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
d01f58f9 7451 that are marked as requiring a non-null pointer argument. The NARGS
7452 arguments are passed in the array ARGARRAY.
7453*/
dbf6c367 7454
7455static void
d01f58f9 7456check_function_nonnull (tree attrs, int nargs, tree *argarray)
dbf6c367 7457{
d01f58f9 7458 tree a, args;
7459 int i;
dbf6c367 7460
7461 for (a = attrs; a; a = TREE_CHAIN (a))
7462 {
7463 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
7464 {
4ee9c684 7465 args = TREE_VALUE (a);
dbf6c367 7466
4ee9c684 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
dbf6c367 7470 for pointer type is done in check_nonnull_arg). */
d01f58f9 7471 for (i = 0; i < nargs; i++)
4ee9c684 7472 {
d01f58f9 7473 if (!args || nonnull_check_p (args, i + 1))
e4603171 7474 check_function_arguments_recurse (check_nonnull_arg, NULL,
d01f58f9 7475 argarray[i],
7476 i + 1);
4ee9c684 7477 }
dbf6c367 7478 }
7479 }
7480}
7481
50ca527f 7482/* Check that the Nth argument of a function call (counting backwards
d01f58f9 7483 from the end) is a (pointer)0. The NARGS arguments are passed in the
7484 array ARGARRAY. */
bf6c8de0 7485
7486static void
d01f58f9 7487check_function_sentinel (tree attrs, int nargs, tree *argarray, tree typelist)
bf6c8de0 7488{
7489 tree attr = lookup_attribute ("sentinel", attrs);
7490
7491 if (attr)
7492 {
d01f58f9 7493 int len = 0;
7494 int pos = 0;
7495 tree sentinel;
a0c938f0 7496
d01f58f9 7497 /* Skip over the named arguments. */
7498 while (typelist && len < nargs)
a0c938f0 7499 {
d01f58f9 7500 typelist = TREE_CHAIN (typelist);
7501 len++;
7502 }
50ca527f 7503
d01f58f9 7504 if (TREE_VALUE (attr))
7505 {
7506 tree p = TREE_VALUE (TREE_VALUE (attr));
7507 pos = TREE_INT_CST_LOW (p);
7508 }
50ca527f 7509
d01f58f9 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;
bf6c8de0 7517 }
d01f58f9 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");
bf6c8de0 7530 }
7531}
7532
dbf6c367 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
1cae46be 7538nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 7539{
4ee9c684 7540 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 7541
7542 for (; args; args = TREE_CHAIN (args))
7543 {
231bd014 7544 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
7545
7546 gcc_assert (found);
dbf6c367 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
9a03a746 7559check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1cae46be 7560 unsigned HOST_WIDE_INT param_num)
dbf6c367 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))
155b601b 7570 warning (OPT_Wnonnull, "null argument where non-null required "
7571 "(argument %lu)", (unsigned long) param_num);
dbf6c367 7572}
7573
7574/* Helper for nonnull attribute handling; fetch the operand number
7575 from the attribute argument list. */
7576
7577static bool
1cae46be 7578get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 7579{
67409385 7580 /* Verify the arg number is a constant. */
dbf6c367 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}
fa987697 7588
7589/* Handle a "nothrow" attribute; arguments as in
7590 struct attribute_spec.handler. */
7591
7592static tree
9a03a746 7593handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7594 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 7595{
7596 if (TREE_CODE (*node) == FUNCTION_DECL)
7597 TREE_NOTHROW (*node) = 1;
7598 /* ??? TODO: Support types. */
7599 else
7600 {
9b2d6d13 7601 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 7602 *no_add_attrs = true;
7603 }
7604
7605 return NULL_TREE;
7606}
7acb29a3 7607
7608/* Handle a "cleanup" attribute; arguments as in
7609 struct attribute_spec.handler. */
7610
7611static tree
1cae46be 7612handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 7613 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 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 {
9b2d6d13 7624 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 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 {
07e3a3d2 7635 error ("cleanup argument not an identifier");
7acb29a3 7636 *no_add_attrs = true;
7637 return NULL_TREE;
7638 }
d1c41717 7639 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 7640 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
7641 {
07e3a3d2 7642 error ("cleanup argument not a function");
7acb29a3 7643 *no_add_attrs = true;
7644 return NULL_TREE;
7645 }
7646
1cae46be 7647 /* That the function has proper type is checked with the
7acb29a3 7648 eventual call to build_function_call. */
7649
7650 return NULL_TREE;
7651}
8a8cdb8d 7652
7653/* Handle a "warn_unused_result" attribute. No special handling. */
7654
7655static tree
7656handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 7657 tree ARG_UNUSED (args),
7658 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 7659{
7660 /* Ignore the attribute for functions not returning any value. */
7661 if (VOID_TYPE_P (TREE_TYPE (*node)))
7662 {
9b2d6d13 7663 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 7664 *no_add_attrs = true;
7665 }
7666
7667 return NULL_TREE;
7668}
bf6c8de0 7669
7670/* Handle a "sentinel" attribute. */
7671
7672static tree
50ca527f 7673handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 7674 int ARG_UNUSED (flags), bool *no_add_attrs)
7675{
7676 tree params = TYPE_ARG_TYPES (*node);
7677
7678 if (!params)
7679 {
9b2d6d13 7680 warning (OPT_Wattributes,
7681 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 7682 *no_add_attrs = true;
bf6c8de0 7683 }
50ca527f 7684 else
7685 {
7686 while (TREE_CHAIN (params))
7687 params = TREE_CHAIN (params);
bf6c8de0 7688
50ca527f 7689 if (VOID_TYPE_P (TREE_VALUE (params)))
a0c938f0 7690 {
9b2d6d13 7691 warning (OPT_Wattributes,
7692 "%qE attribute only applies to variadic functions", name);
50ca527f 7693 *no_add_attrs = true;
7694 }
7695 }
a0c938f0 7696
50ca527f 7697 if (args)
bf6c8de0 7698 {
50ca527f 7699 tree position = TREE_VALUE (args);
7700
50ca527f 7701 if (TREE_CODE (position) != INTEGER_CST)
a0c938f0 7702 {
01b54db5 7703 warning (OPT_Wattributes,
7704 "requested position is not an integer constant");
50ca527f 7705 *no_add_attrs = true;
7706 }
7707 else
a0c938f0 7708 {
50ca527f 7709 if (tree_int_cst_lt (position, integer_zero_node))
7710 {
01b54db5 7711 warning (OPT_Wattributes,
7712 "requested position is less than zero");
50ca527f 7713 *no_add_attrs = true;
7714 }
7715 }
bf6c8de0 7716 }
a0c938f0 7717
bf6c8de0 7718 return NULL_TREE;
7719}
b5c26b42 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{
19fbe3a4 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);
b5c26b42 7739
7740 return NULL_TREE;
7741}
46f8e3b0 7742
24470055 7743/* Handle a "target" attribute. */
46f8e3b0 7744
7745static tree
24470055 7746handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 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 }
46f8e3b0 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;
2becf397 7779 int saved_flag_strict_aliasing;
46f8e3b0 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
2becf397 7870 saved_flag_strict_aliasing = flag_strict_aliasing;
7871
46f8e3b0 7872 /* Now parse the options. */
7873 decode_options (opt_argc, opt_argv);
7874
2becf397 7875 /* Don't allow changing -fstrict-aliasing. */
7876 flag_strict_aliasing = saved_flag_strict_aliasing;
7877
46f8e3b0 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}
dbf6c367 7919\f
d01f58f9 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 */
dbf6c367 7924void
d01f58f9 7925check_function_arguments (tree attrs, int nargs, tree *argarray, tree typelist)
dbf6c367 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)
d01f58f9 7931 check_function_nonnull (attrs, nargs, argarray);
dbf6c367 7932
7933 /* Check for errors in format strings. */
7934
95c90e04 7935 if (warn_format || warn_missing_format_attribute)
d01f58f9 7936 check_function_format (attrs, nargs, argarray);
95c90e04 7937
7938 if (warn_format)
d01f58f9 7939 check_function_sentinel (attrs, nargs, argarray, typelist);
dbf6c367 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
1cae46be 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)
dbf6c367 7950{
72dd6141 7951 if (CONVERT_EXPR_P (param)
c44afe23 7952 && (TYPE_PRECISION (TREE_TYPE (param))
7953 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 7954 {
7955 /* Strip coercion. */
7956 check_function_arguments_recurse (callback, ctx,
4ee9c684 7957 TREE_OPERAND (param, 0), param_num);
dbf6c367 7958 return;
7959 }
7960
7961 if (TREE_CODE (param) == CALL_EXPR)
7962 {
c2f47e15 7963 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 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 {
c2f47e15 7976 tree inner_arg;
dbf6c367 7977 tree format_num_expr;
7978 int format_num;
7979 int i;
c2f47e15 7980 call_expr_arg_iterator iter;
dbf6c367 7981
7982 /* Extract the argument number, which was previously checked
7983 to be valid. */
7984 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
dbf6c367 7985
231bd014 7986 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
7987 && !TREE_INT_CST_HIGH (format_num_expr));
dbf6c367 7988
7989 format_num = TREE_INT_CST_LOW (format_num_expr);
7990
c2f47e15 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++)
dbf6c367 7994 if (i == format_num)
7995 {
7996 check_function_arguments_recurse (callback, ctx,
c2f47e15 7997 inner_arg, param_num);
dbf6c367 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,
4ee9c684 8014 TREE_OPERAND (param, 1), param_num);
dbf6c367 8015 check_function_arguments_recurse (callback, ctx,
4ee9c684 8016 TREE_OPERAND (param, 2), param_num);
dbf6c367 8017 return;
8018 }
8019
8020 (*callback) (ctx, param, param_num);
8021}
1f3233d1 8022
d43cee80 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:
c319d56a 8060 case BUILT_IN_ISINF_SIGN:
d43cee80 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
19fbe3a4 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
d43cee80 8121 default:
8122 return true;
8123 }
8124}
8125
860251be 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{
4fd61bc6 8131 const tree *const x = (const tree *const) x_p;
8132 const tree *const y = (const tree *const) y_p;
8133
860251be 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{
4fd61bc6 8157 const tree *const x = (const tree *const) x_p;
8158 const tree *const y = (const tree *const) y_p;
860251be 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,
9a03a746 8182 void * ARG_UNUSED (orig_obj),
4ee9c684 8183 gt_pointer_operator new_value,
8184 void *cookie)
860251be 8185{
9a03a746 8186 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 8187 resort_data.new_value = new_value;
8188 resort_data.cookie = cookie;
8189 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 8190 resort_field_decl_cmp);
860251be 8191}
8192
209c9752 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
cfee01e3 8196 RHS_SIZE is its length (including the terminating NUL character).
209c9752 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
380c6697 8210/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 8211 TOKEN, which had the associated VALUE. */
8212
8213void
ba99525e 8214c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
8215 tree value, unsigned char token_flags)
92b128ed 8216{
209c9752 8217#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
8218
8219 char *message = NULL;
92b128ed 8220
ba99525e 8221 if (token_type == CPP_EOF)
380c6697 8222 message = catenate_messages (gmsgid, " at end of input");
ba99525e 8223 else if (token_type == CPP_CHAR
8224 || token_type == CPP_WCHAR
8225 || token_type == CPP_CHAR16
8226 || token_type == CPP_CHAR32)
92b128ed 8227 {
8228 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 8229 const char *prefix;
8230
ba99525e 8231 switch (token_type)
924bbf02 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
92b128ed 8247 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 8248 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 8249 else
a0c938f0 8250 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 8251
924bbf02 8252 error (message, prefix, val);
209c9752 8253 free (message);
8254 message = NULL;
92b128ed 8255 }
ba99525e 8256 else if (token_type == CPP_STRING
8257 || token_type == CPP_WSTRING
8258 || token_type == CPP_STRING16
8259 || token_type == CPP_STRING32)
380c6697 8260 message = catenate_messages (gmsgid, " before string constant");
ba99525e 8261 else if (token_type == CPP_NUMBER)
380c6697 8262 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 8263 else if (token_type == CPP_NAME)
209c9752 8264 {
380c6697 8265 message = catenate_messages (gmsgid, " before %qE");
782858b8 8266 error (message, value);
209c9752 8267 free (message);
8268 message = NULL;
8269 }
ba99525e 8270 else if (token_type == CPP_PRAGMA)
b75b98aa 8271 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 8272 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 8273 message = catenate_messages (gmsgid, " before end of line");
ba99525e 8274 else if (token_type < N_TTYPES)
209c9752 8275 {
380c6697 8276 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 8277 error (message, cpp_type2name (token_type, token_flags));
209c9752 8278 free (message);
8279 message = NULL;
8280 }
92b128ed 8281 else
380c6697 8282 error (gmsgid);
209c9752 8283
8284 if (message)
8285 {
8286 error (message);
8287 free (message);
8288 }
a0c938f0 8289#undef catenate_messages
92b128ed 8290}
8291
7f5f3953 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;
ff903809 8336 case CPP_DL_FATAL:
8337 dlevel = DK_FATAL;
8338 break;
7f5f3953 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
4ee9c684 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
b27ac6b5 8356 inlining, so we don't have to worry about that. */
8357
4ee9c684 8358void
75a70cf9 8359c_warn_unused_result (gimple_seq seq)
4ee9c684 8360{
4ee9c684 8361 tree fdecl, ftype;
75a70cf9 8362 gimple_stmt_iterator i;
4ee9c684 8363
75a70cf9 8364 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
4ee9c684 8365 {
75a70cf9 8366 gimple g = gsi_stmt (i);
4ee9c684 8367
75a70cf9 8368 switch (gimple_code (g))
4ee9c684 8369 {
75a70cf9 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;
4ee9c684 8383
75a70cf9 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. */
0acacf9e 8391 fdecl = gimple_call_fndecl (g);
8392 ftype = TREE_TYPE (TREE_TYPE (gimple_call_fn (g)));
4ee9c684 8393
75a70cf9 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 }
4ee9c684 8413 }
8414}
8415
624d37a6 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
af28855b 8437/* Build the result of __builtin_offsetof. EXPR is a nested sequence of
d897f7c2 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. */
af28855b 8441
8442static tree
d897f7c2 8443fold_offsetof_1 (tree expr, tree stop_ref)
af28855b 8444{
8445 enum tree_code code = PLUS_EXPR;
8446 tree base, off, t;
8447
d897f7c2 8448 if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK)
8449 return size_zero_node;
8450
af28855b 8451 switch (TREE_CODE (expr))
8452 {
8453 case ERROR_MARK:
8454 return expr;
8455
6b11d2e3 8456 case VAR_DECL:
8457 error ("cannot apply %<offsetof%> to static data member %qD", expr);
8458 return error_mark_node;
8459
d897f7c2 8460 case CALL_EXPR:
cf1a89a3 8461 case TARGET_EXPR:
d897f7c2 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));
af28855b 8467 return size_zero_node;
8468
d897f7c2 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
af28855b 8475 case COMPONENT_REF:
d897f7c2 8476 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
af28855b 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 "
782858b8 8484 "member %qD", t);
af28855b 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:
d897f7c2 8493 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
af28855b 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;
49d00087 8501 t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
af28855b 8502 }
8503 t = convert (sizetype, t);
8504 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
8505 break;
8506
ede90cc2 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
af28855b 8513 default:
231bd014 8514 gcc_unreachable ();
af28855b 8515 }
8516
8517 return size_binop (code, base, off);
8518}
8519
8520tree
d897f7c2 8521fold_offsetof (tree expr, tree stop_ref)
af28855b 8522{
8523 /* Convert back from the internal sizetype to size_t. */
d897f7c2 8524 return convert (size_type_node, fold_offsetof_1 (expr, stop_ref));
af28855b 8525}
8526
e35976b1 8527/* Print an error message for an invalid lvalue. USE says
ab6bb714 8528 how the lvalue is being used and so selects the error message. */
8529
e35976b1 8530void
8531lvalue_error (enum lvalue_use use)
ab6bb714 8532{
e35976b1 8533 switch (use)
ab6bb714 8534 {
e35976b1 8535 case lv_assign:
82d76a54 8536 error ("lvalue required as left operand of assignment");
e35976b1 8537 break;
8538 case lv_increment:
82d76a54 8539 error ("lvalue required as increment operand");
e35976b1 8540 break;
8541 case lv_decrement:
82d76a54 8542 error ("lvalue required as decrement operand");
e35976b1 8543 break;
8544 case lv_addressof:
82d76a54 8545 error ("lvalue required as unary %<&%> operand");
e35976b1 8546 break;
8547 case lv_asm:
82d76a54 8548 error ("lvalue required in asm statement");
e35976b1 8549 break;
8550 default:
8551 gcc_unreachable ();
ab6bb714 8552 }
ab6bb714 8553}
c271bdb2 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;
6753bca0 8565 hashval_t hashcode = 0;
c271bdb2 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 {
c75b4594 8578 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 8579
c75b4594 8580 if (VEC_empty (constructor_elt, v))
c271bdb2 8581 {
8582 if (pedantic)
8583 failure = 3;
8584 maxindex = integer_minus_one_node;
8585 }
8586 else
8587 {
8588 tree curindex;
c75b4594 8589 unsigned HOST_WIDE_INT cnt;
8590 constructor_elt *ce;
cee43f7e 8591 bool fold_p = false;
c271bdb2 8592
c75b4594 8593 if (VEC_index (constructor_elt, v, 0)->index)
8594 maxindex = fold_convert (sizetype,
8595 VEC_index (constructor_elt,
8596 v, 0)->index);
c271bdb2 8597 curindex = maxindex;
8598
c75b4594 8599 for (cnt = 1;
8600 VEC_iterate (constructor_elt, v, cnt, ce);
8601 cnt++)
c271bdb2 8602 {
cee43f7e 8603 bool curfold_p = false;
c75b4594 8604 if (ce->index)
cee43f7e 8605 curindex = ce->index, curfold_p = true;
c271bdb2 8606 else
cee43f7e 8607 {
8608 if (fold_p)
8609 curindex = fold_convert (sizetype, curindex);
8610 curindex = size_binop (PLUS_EXPR, curindex, size_one_node);
8611 }
c271bdb2 8612 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 8613 maxindex = curindex, fold_p = curfold_p;
c271bdb2 8614 }
cee43f7e 8615 if (fold_p)
8616 maxindex = fold_convert (sizetype, maxindex);
c271bdb2 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
6753bca0 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
796735dc 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
c271bdb2 8668 if (quals == 0)
8669 type = main_type;
8670 else
8671 type = c_build_qualified_type (main_type, quals);
8672
4f5b8f2a 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
c271bdb2 8683 *ptype = type;
8684 return failure;
8685}
ab6bb714 8686
b6a5fc45 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
b9c74b4d 8706sync_resolve_size (tree function, VEC(tree,gc) *params)
b6a5fc45 8707{
8708 tree type;
8709 int size;
8710
b9c74b4d 8711 if (VEC_empty (tree, params))
b6a5fc45 8712 {
8713 error ("too few arguments to function %qE", function);
8714 return 0;
8715 }
8716
b9c74b4d 8717 type = TREE_TYPE (VEC_index (tree, params, 0));
b6a5fc45 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);
27213ba3 8726 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 8727 return size;
8728
8729 incompatible:
8730 error ("incompatible type for argument %d of %qE", 1, function);
8731 return 0;
8732}
8733
a0c938f0 8734/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 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
b9c74b4d 8740sync_resolve_params (tree orig_function, tree function, VEC(tree, gc) *params)
b6a5fc45 8741{
8742 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
8743 tree ptype;
8744 int number;
b9c74b4d 8745 unsigned int parmnum;
b6a5fc45 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);
b9c74b4d 8751 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
b6a5fc45 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. */
b9c74b4d 8756 parmnum = 0;
b6a5fc45 8757 while (arg_types != void_list_node)
8758 {
8759 tree val;
8760
b9c74b4d 8761 ++parmnum;
8762 if (VEC_length (tree, params) <= parmnum)
b6a5fc45 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. */
b9c74b4d 8771 val = VEC_index (tree, params, parmnum);
b6a5fc45 8772 val = convert (ptype, val);
8773 val = convert (TREE_VALUE (arg_types), val);
b9c74b4d 8774 VEC_replace (tree, params, parmnum, val);
b6a5fc45 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. */
b9c74b4d 8784 VEC_truncate (tree, params, parmnum + 1);
b6a5fc45 8785
8786 return true;
8787}
8788
a0c938f0 8789/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 8790 RESULT to make it match the type of the first pointer argument in
8791 PARAMS. */
8792
8793static tree
b9c74b4d 8794sync_resolve_return (tree first_param, tree result)
b6a5fc45 8795{
b9c74b4d 8796 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10080eac 8797 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 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
b9c74b4d 8811resolve_overloaded_builtin (tree function, VEC(tree,gc) *params)
b6a5fc45 8812{
8813 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
65441f6f 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)
a0c938f0 8820 return targetm.resolve_overloaded_builtin (function, params);
65441f6f 8821 else
a0c938f0 8822 return NULL_TREE;
65441f6f 8823 default:
8824 return NULL_TREE;
8825 }
a0c938f0 8826
65441f6f 8827 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 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);
b9c74b4d 8848 tree new_function, first_param, result;
b6a5fc45 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
b9c74b4d 8857 first_param = VEC_index (tree, params, 0);
8858 result = build_function_call_vec (new_function, params, NULL);
b6a5fc45 8859 if (orig_code != BUILT_IN_BOOL_COMPARE_AND_SWAP_N
8860 && orig_code != BUILT_IN_LOCK_RELEASE_N)
b9c74b4d 8861 result = sync_resolve_return (first_param, result);
b6a5fc45 8862
8863 return result;
8864 }
8865
8866 default:
65441f6f 8867 return NULL_TREE;
b6a5fc45 8868 }
8869}
8870
491255f5 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
9421ebb9 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));
491255f5 8880
8881 /* Equality works here because c_common_signed_type uses
8882 TYPE_MAIN_VARIANT. */
11773141 8883 return c_common_signed_type (t1)
8884 == c_common_signed_type (t2);
491255f5 8885}
8886
be7350e7 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
2840aae4 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
e534436e 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
82012ffe 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. */
e534436e 8936
8937void
82012ffe 8938warn_about_parentheses (enum tree_code code,
269f7979 8939 enum tree_code code_left, tree arg_left,
82012ffe 8940 enum tree_code code_right, tree arg_right)
e534436e 8941{
8942 if (!warn_parentheses)
8943 return;
8944
82012ffe 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)
e534436e 8956 {
82012ffe 8957 case LSHIFT_EXPR:
8958 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
e534436e 8959 warning (OPT_Wparentheses,
82012ffe 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;
e534436e 8965
82012ffe 8966 case RSHIFT_EXPR:
8967 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
e534436e 8968 warning (OPT_Wparentheses,
82012ffe 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;
e534436e 8974
82012ffe 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:
e534436e 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,
82012ffe 8987 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 8988 /* Check cases like x|y==z */
82012ffe 8989 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
8990 || TREE_CODE_CLASS (code_right) == tcc_comparison)
e534436e 8991 warning (OPT_Wparentheses,
82012ffe 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))
96bda40f 8996 warning (OPT_Wparentheses, "suggest parentheses around operand of "
82012ffe 8997 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
8998 return;
e534436e 8999
82012ffe 9000 case BIT_XOR_EXPR:
e534436e 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,
82012ffe 9006 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 9007 /* Check cases like x^y==z */
82012ffe 9008 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9009 || TREE_CODE_CLASS (code_right) == tcc_comparison)
e534436e 9010 warning (OPT_Wparentheses,
82012ffe 9011 "suggest parentheses around comparison in operand of %<^%>");
9012 return;
e534436e 9013
82012ffe 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)
e534436e 9019 warning (OPT_Wparentheses,
82012ffe 9020 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 9021 /* Check cases like x&y==z */
82012ffe 9022 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9023 || TREE_CODE_CLASS (code_right) == tcc_comparison)
e534436e 9024 warning (OPT_Wparentheses,
82012ffe 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))
96bda40f 9029 warning (OPT_Wparentheses, "suggest parentheses around operand of "
82012ffe 9030 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
9031 return;
e534436e 9032
82012ffe 9033 case EQ_EXPR:
6ce0c450 9034 if (TREE_CODE_CLASS (code_left) == tcc_comparison
9035 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9036 warning (OPT_Wparentheses,
82012ffe 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
269f7979 9049 && code_left != NE_EXPR && code_left != EQ_EXPR
9050 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
82012ffe 9051 || (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 9052 && code_right != NE_EXPR && code_right != EQ_EXPR
9053 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
82012ffe 9054 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
6ce0c450 9055 "have their mathematical meaning");
82012ffe 9056 return;
6ce0c450 9057 }
82012ffe 9058#undef NOT_A_BOOLEAN_EXPR_P
e534436e 9059}
9060
92fccaaa 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}
2840aae4 9074
b9fc964a 9075#ifndef TARGET_HAS_TARGETCM
9076struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;
9077#endif
9078
b6889cb0 9079/* Warn for division by zero according to the value of DIVISOR. LOC
9080 is the location of the division operator. */
f092582b 9081
9082void
b6889cb0 9083warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 9084{
9421ebb9 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
f092582b 9087 floating-point type, since we consider 0.0/0.0 a valid way of
9088 generating a NaN. */
9421ebb9 9089 if (skip_evaluation == 0
9090 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 9091 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 9092}
9093
13869a99 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
8e70fb09 9098 be.
9099
9100 LOCATION is the location of the comparison operator.
13869a99 9101
9102 The arguments of this function map directly to local variables
9103 of build_binary_op. */
9104
9105void
8e70fb09 9106warn_for_sign_compare (location_t location,
9107 tree orig_op0, tree orig_op1,
13869a99 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))
895b662f 9120 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 9121 {
8e70fb09 9122 warning_at (location,
9123 OPT_Wsign_compare, "comparison between types %qT and %qT",
9124 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 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 {
895b662f 9137 tree sop, uop, base_type;
13869a99 9138 bool ovf;
895b662f 9139
13869a99 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);
895b662f 9147 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
9148 ? TREE_TYPE (result_type) : result_type);
13869a99 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)
895b662f 9161 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 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)),
895b662f 9169 c_common_signed_type (base_type)))
13869a99 9170 /* OK */;
9171 else
8e70fb09 9172 warning_at (location,
9173 OPT_Wsign_compare,
9174 "comparison between signed and unsigned integer expressions");
13869a99 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
8e70fb09 9227 warning_at (location, OPT_Wsign_compare,
9228 "comparison of promoted ~unsigned with constant");
13869a99 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)))
8e70fb09 9237 warning_at (location, OPT_Wsign_compare,
13869a99 9238 "comparison of promoted ~unsigned with unsigned");
9239 }
9240}
9241
41771881 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
1f3233d1 9314#include "gt-c-common.h"