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