Fix typo in ChangeLog entry date.
[official-gcc.git] / gcc / c-common.c
blob45b4192d446b1bea78b92cfad3f0766dd69df1dd
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
4 Free Software Foundation, Inc.
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "intl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "output.h"
30 #include "c-pragma.h"
31 #include "rtl.h"
32 #include "ggc.h"
33 #include "varray.h"
34 #include "expr.h"
35 #include "c-common.h"
36 #include "tm_p.h"
37 #include "obstack.h"
38 #include "cpplib.h"
39 #include "target.h"
40 #include "langhooks.h"
41 #include "tree-inline.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "diagnostic.h"
45 #include "tree-iterator.h"
46 #include "hashtab.h"
47 #include "tree-mudflap.h"
48 #include "opts.h"
49 #include "real.h"
50 #include "cgraph.h"
51 #include "target-def.h"
52 #include "gimple.h"
53 #include "fixed-value.h"
54 #include "libfuncs.h"
56 cpp_reader *parse_in; /* Declared in c-pragma.h. */
58 /* The following symbols are subsumed in the c_global_trees array, and
59 listed here individually for documentation purposes.
61 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
63 tree short_integer_type_node;
64 tree long_integer_type_node;
65 tree long_long_integer_type_node;
67 tree short_unsigned_type_node;
68 tree long_unsigned_type_node;
69 tree long_long_unsigned_type_node;
71 tree truthvalue_type_node;
72 tree truthvalue_false_node;
73 tree truthvalue_true_node;
75 tree ptrdiff_type_node;
77 tree unsigned_char_type_node;
78 tree signed_char_type_node;
79 tree wchar_type_node;
81 tree char16_type_node;
82 tree char32_type_node;
84 tree float_type_node;
85 tree double_type_node;
86 tree long_double_type_node;
88 tree complex_integer_type_node;
89 tree complex_float_type_node;
90 tree complex_double_type_node;
91 tree complex_long_double_type_node;
93 tree dfloat32_type_node;
94 tree dfloat64_type_node;
95 tree_dfloat128_type_node;
97 tree intQI_type_node;
98 tree intHI_type_node;
99 tree intSI_type_node;
100 tree intDI_type_node;
101 tree intTI_type_node;
103 tree unsigned_intQI_type_node;
104 tree unsigned_intHI_type_node;
105 tree unsigned_intSI_type_node;
106 tree unsigned_intDI_type_node;
107 tree unsigned_intTI_type_node;
109 tree widest_integer_literal_type_node;
110 tree widest_unsigned_literal_type_node;
112 Nodes for types `void *' and `const void *'.
114 tree ptr_type_node, const_ptr_type_node;
116 Nodes for types `char *' and `const char *'.
118 tree string_type_node, const_string_type_node;
120 Type `char[SOMENUMBER]'.
121 Used when an array of char is needed and the size is irrelevant.
123 tree char_array_type_node;
125 Type `int[SOMENUMBER]' or something like it.
126 Used when an array of int needed and the size is irrelevant.
128 tree int_array_type_node;
130 Type `wchar_t[SOMENUMBER]' or something like it.
131 Used when a wide string literal is created.
133 tree wchar_array_type_node;
135 Type `char16_t[SOMENUMBER]' or something like it.
136 Used when a UTF-16 string literal is created.
138 tree char16_array_type_node;
140 Type `char32_t[SOMENUMBER]' or something like it.
141 Used when a UTF-32 string literal is created.
143 tree char32_array_type_node;
145 Type `int ()' -- used for implicit declaration of functions.
147 tree default_function_type;
149 A VOID_TYPE node, packaged in a TREE_LIST.
151 tree void_list_node;
153 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
154 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
155 VAR_DECLS, but C++ does.)
157 tree function_name_decl_node;
158 tree pretty_function_name_decl_node;
159 tree c99_function_name_decl_node;
161 Stack of nested function name VAR_DECLs.
163 tree saved_function_name_decls;
167 tree c_global_trees[CTI_MAX];
169 /* Switches common to the C front ends. */
171 /* Nonzero if preprocessing only. */
173 int flag_preprocess_only;
175 /* Nonzero means don't output line number information. */
177 char flag_no_line_commands;
179 /* Nonzero causes -E output not to be done, but directives such as
180 #define that have side effects are still obeyed. */
182 char flag_no_output;
184 /* Nonzero means dump macros in some fashion. */
186 char flag_dump_macros;
188 /* Nonzero means pass #include lines through to the output. */
190 char flag_dump_includes;
192 /* Nonzero means process PCH files while preprocessing. */
194 bool flag_pch_preprocess;
196 /* The file name to which we should write a precompiled header, or
197 NULL if no header will be written in this compile. */
199 const char *pch_file;
201 /* Nonzero if an ISO standard was selected. It rejects macros in the
202 user's namespace. */
203 int flag_iso;
205 /* Nonzero if -undef was given. It suppresses target built-in macros
206 and assertions. */
207 int flag_undef;
209 /* Nonzero means don't recognize the non-ANSI builtin functions. */
211 int flag_no_builtin;
213 /* Nonzero means don't recognize the non-ANSI builtin functions.
214 -ansi sets this. */
216 int flag_no_nonansi_builtin;
218 /* Nonzero means give `double' the same size as `float'. */
220 int flag_short_double;
222 /* Nonzero means give `wchar_t' the same size as `short'. */
224 int flag_short_wchar;
226 /* Nonzero means allow implicit conversions between vectors with
227 differing numbers of subparts and/or differing element types. */
228 int flag_lax_vector_conversions;
230 /* Nonzero means allow Microsoft extensions without warnings or errors. */
231 int flag_ms_extensions;
233 /* Nonzero means don't recognize the keyword `asm'. */
235 int flag_no_asm;
237 /* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
239 int flag_signed_bitfields = 1;
241 /* Warn about #pragma directives that are not recognized. */
243 int warn_unknown_pragmas; /* Tri state variable. */
245 /* Warn about format/argument anomalies in calls to formatted I/O functions
246 (*printf, *scanf, strftime, strfmon, etc.). */
248 int warn_format;
250 /* Warn about using __null (as NULL in C++) as sentinel. For code compiled
251 with GCC this doesn't matter as __null is guaranteed to have the right
252 size. */
254 int warn_strict_null_sentinel;
256 /* Zero means that faster, ...NonNil variants of objc_msgSend...
257 calls will be used in ObjC; passing nil receivers to such calls
258 will most likely result in crashes. */
259 int flag_nil_receivers = 1;
261 /* Nonzero means that code generation will be altered to support
262 "zero-link" execution. This currently affects ObjC only, but may
263 affect other languages in the future. */
264 int flag_zero_link = 0;
266 /* Nonzero means emit an '__OBJC, __image_info' for the current translation
267 unit. It will inform the ObjC runtime that class definition(s) herein
268 contained are to replace one(s) previously loaded. */
269 int flag_replace_objc_classes = 0;
271 /* C/ObjC language option variables. */
274 /* Nonzero means allow type mismatches in conditional expressions;
275 just make their values `void'. */
277 int flag_cond_mismatch;
279 /* Nonzero means enable C89 Amendment 1 features. */
281 int flag_isoc94;
283 /* Nonzero means use the ISO C99 dialect of C. */
285 int flag_isoc99;
287 /* Nonzero means that we have builtin functions, and main is an int. */
289 int flag_hosted = 1;
292 /* ObjC language option variables. */
295 /* Open and close the file for outputting class declarations, if
296 requested (ObjC). */
298 int flag_gen_declaration;
300 /* Tells the compiler that this is a special run. Do not perform any
301 compiling, instead we are to test some platform dependent features
302 and output a C header file with appropriate definitions. */
304 int print_struct_values;
306 /* Tells the compiler what is the constant string class for ObjC. */
308 const char *constant_string_class_name;
311 /* C++ language option variables. */
314 /* Nonzero means don't recognize any extension keywords. */
316 int flag_no_gnu_keywords;
318 /* Nonzero means do emit exported implementations of functions even if
319 they can be inlined. */
321 int flag_implement_inlines = 1;
323 /* Nonzero means that implicit instantiations will be emitted if needed. */
325 int flag_implicit_templates = 1;
327 /* Nonzero means that implicit instantiations of inline templates will be
328 emitted if needed, even if instantiations of non-inline templates
329 aren't. */
331 int flag_implicit_inline_templates = 1;
333 /* Nonzero means generate separate instantiation control files and
334 juggle them at link time. */
336 int flag_use_repository;
338 /* Nonzero if we want to issue diagnostics that the standard says are not
339 required. */
341 int flag_optional_diags = 1;
343 /* Nonzero means we should attempt to elide constructors when possible. */
345 int flag_elide_constructors = 1;
347 /* Nonzero means that member functions defined in class scope are
348 inline by default. */
350 int flag_default_inline = 1;
352 /* Controls whether compiler generates 'type descriptor' that give
353 run-time type information. */
355 int flag_rtti = 1;
357 /* Nonzero if we want to conserve space in the .o files. We do this
358 by putting uninitialized data and runtime initialized data into
359 .common instead of .data at the expense of not flagging multiple
360 definitions. */
362 int flag_conserve_space;
364 /* Nonzero if we want to obey access control semantics. */
366 int flag_access_control = 1;
368 /* Nonzero if we want to check the return value of new and avoid calling
369 constructors if it is a null pointer. */
371 int flag_check_new;
373 /* The C++ dialect being used. C++98 is the default. */
375 enum cxx_dialect cxx_dialect = cxx98;
377 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
378 initialization variables.
379 0: Old rules, set by -fno-for-scope.
380 2: New ISO rules, set by -ffor-scope.
381 1: Try to implement new ISO rules, but with backup compatibility
382 (and warnings). This is the default, for now. */
384 int flag_new_for_scope = 1;
386 /* Nonzero if we want to emit defined symbols with common-like linkage as
387 weak symbols where possible, in order to conform to C++ semantics.
388 Otherwise, emit them as local symbols. */
390 int flag_weak = 1;
392 /* 0 means we want the preprocessor to not emit line directives for
393 the current working directory. 1 means we want it to do it. -1
394 means we should decide depending on whether debugging information
395 is being emitted or not. */
397 int flag_working_directory = -1;
399 /* Nonzero to use __cxa_atexit, rather than atexit, to register
400 destructors for local statics and global objects. '2' means it has been
401 set nonzero as a default, not by a command-line flag. */
403 int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT;
405 /* Nonzero to use __cxa_get_exception_ptr in C++ exception-handling
406 code. '2' means it has not been set explicitly on the command line. */
408 int flag_use_cxa_get_exception_ptr = 2;
410 /* Nonzero means to implement standard semantics for exception
411 specifications, calling unexpected if an exception is thrown that
412 doesn't match the specification. Zero means to treat them as
413 assertions and optimize accordingly, but not check them. */
415 int flag_enforce_eh_specs = 1;
417 /* Nonzero means to generate thread-safe code for initializing local
418 statics. */
420 int flag_threadsafe_statics = 1;
422 /* Nonzero if we want to pretty-print template specializations as the
423 template signature followed by the arguments. */
425 int flag_pretty_templates = 1;
427 /* Nonzero means warn about implicit declarations. */
429 int warn_implicit = 1;
431 /* Maximum template instantiation depth. This limit is rather
432 arbitrary, but it exists to limit the time it takes to notice
433 infinite template instantiations. */
435 int max_tinst_depth = 500;
439 /* The elements of `ridpointers' are identifier nodes for the reserved
440 type names and storage classes. It is indexed by a RID_... value. */
441 tree *ridpointers;
443 tree (*make_fname_decl) (tree, int);
445 /* Nonzero means the expression being parsed will never be evaluated.
446 This is a count, since unevaluated expressions can nest. */
447 int skip_evaluation;
449 /* Whether lexing has been completed, so subsequent preprocessor
450 errors should use the compiler's input_location. */
451 bool done_lexing = false;
453 /* Information about how a function name is generated. */
454 struct fname_var_t
456 tree *const decl; /* pointer to the VAR_DECL. */
457 const unsigned rid; /* RID number for the identifier. */
458 const int pretty; /* How pretty is it? */
461 /* The three ways of getting then name of the current function. */
463 const struct fname_var_t fname_vars[] =
465 /* C99 compliant __func__, must be first. */
466 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
467 /* GCC __FUNCTION__ compliant. */
468 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
469 /* GCC __PRETTY_FUNCTION__ compliant. */
470 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
471 {NULL, 0, 0},
474 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
475 static tree check_case_value (tree);
476 static bool check_case_bounds (tree, tree, tree *, tree *);
478 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
479 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
480 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
481 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
482 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
483 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
484 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
485 static tree handle_always_inline_attribute (tree *, tree, tree, int,
486 bool *);
487 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
488 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
489 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
490 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
491 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
492 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
493 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
494 bool *);
495 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
496 static tree handle_transparent_union_attribute (tree *, tree, tree,
497 int, bool *);
498 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
499 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
500 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
501 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
502 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
503 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
504 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
505 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
506 static tree handle_visibility_attribute (tree *, tree, tree, int,
507 bool *);
508 static tree handle_tls_model_attribute (tree *, tree, tree, int,
509 bool *);
510 static tree handle_no_instrument_function_attribute (tree *, tree,
511 tree, int, bool *);
512 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
513 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
514 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
515 bool *);
516 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
517 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
518 static tree handle_deprecated_attribute (tree *, tree, tree, int,
519 bool *);
520 static tree handle_vector_size_attribute (tree *, tree, tree, int,
521 bool *);
522 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
523 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
524 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
525 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
526 bool *);
527 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
528 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
529 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
530 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
531 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
533 static void check_function_nonnull (tree, int, tree *);
534 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
535 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
536 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
537 static int resort_field_decl_cmp (const void *, const void *);
539 /* Reserved words. The third field is a mask: keywords are disabled
540 if they match the mask.
542 Masks for languages:
543 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
544 C --std=c99: D_CXXONLY | D_OBJC
545 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
546 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
547 C++ --std=c0x: D_CONLY | D_OBJC
548 ObjC++ is like C++ except that D_OBJC is not set
550 If -fno-asm is used, D_ASM is added to the mask. If
551 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
552 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
553 In C with -Wc++-compat, we warn if D_CXXWARN is set. */
555 const struct c_common_resword c_common_reswords[] =
557 { "_Bool", RID_BOOL, D_CONLY },
558 { "_Complex", RID_COMPLEX, 0 },
559 { "_Imaginary", RID_IMAGINARY, D_CONLY },
560 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
561 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
562 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
563 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
564 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
565 { "_Sat", RID_SAT, D_CONLY | D_EXT },
566 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
567 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
568 { "__alignof", RID_ALIGNOF, 0 },
569 { "__alignof__", RID_ALIGNOF, 0 },
570 { "__asm", RID_ASM, 0 },
571 { "__asm__", RID_ASM, 0 },
572 { "__attribute", RID_ATTRIBUTE, 0 },
573 { "__attribute__", RID_ATTRIBUTE, 0 },
574 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
575 { "__builtin_offsetof", RID_OFFSETOF, 0 },
576 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
577 { "__builtin_va_arg", RID_VA_ARG, 0 },
578 { "__complex", RID_COMPLEX, 0 },
579 { "__complex__", RID_COMPLEX, 0 },
580 { "__const", RID_CONST, 0 },
581 { "__const__", RID_CONST, 0 },
582 { "__decltype", RID_DECLTYPE, D_CXXONLY },
583 { "__extension__", RID_EXTENSION, 0 },
584 { "__func__", RID_C99_FUNCTION_NAME, 0 },
585 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
586 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
587 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
588 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
589 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
590 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
591 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
592 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
593 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
594 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
595 { "__is_class", RID_IS_CLASS, D_CXXONLY },
596 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
597 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
598 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
599 { "__is_pod", RID_IS_POD, D_CXXONLY },
600 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
601 { "__is_union", RID_IS_UNION, D_CXXONLY },
602 { "__imag", RID_IMAGPART, 0 },
603 { "__imag__", RID_IMAGPART, 0 },
604 { "__inline", RID_INLINE, 0 },
605 { "__inline__", RID_INLINE, 0 },
606 { "__label__", RID_LABEL, 0 },
607 { "__null", RID_NULL, 0 },
608 { "__real", RID_REALPART, 0 },
609 { "__real__", RID_REALPART, 0 },
610 { "__restrict", RID_RESTRICT, 0 },
611 { "__restrict__", RID_RESTRICT, 0 },
612 { "__signed", RID_SIGNED, 0 },
613 { "__signed__", RID_SIGNED, 0 },
614 { "__thread", RID_THREAD, 0 },
615 { "__typeof", RID_TYPEOF, 0 },
616 { "__typeof__", RID_TYPEOF, 0 },
617 { "__volatile", RID_VOLATILE, 0 },
618 { "__volatile__", RID_VOLATILE, 0 },
619 { "asm", RID_ASM, D_ASM },
620 { "auto", RID_AUTO, 0 },
621 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
622 { "break", RID_BREAK, 0 },
623 { "case", RID_CASE, 0 },
624 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
625 { "char", RID_CHAR, 0 },
626 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
627 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
628 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
629 { "const", RID_CONST, 0 },
630 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
631 { "continue", RID_CONTINUE, 0 },
632 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
633 { "default", RID_DEFAULT, 0 },
634 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
635 { "do", RID_DO, 0 },
636 { "double", RID_DOUBLE, 0 },
637 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
638 { "else", RID_ELSE, 0 },
639 { "enum", RID_ENUM, 0 },
640 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
641 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
642 { "extern", RID_EXTERN, 0 },
643 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
644 { "float", RID_FLOAT, 0 },
645 { "for", RID_FOR, 0 },
646 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
647 { "goto", RID_GOTO, 0 },
648 { "if", RID_IF, 0 },
649 { "inline", RID_INLINE, D_EXT89 },
650 { "int", RID_INT, 0 },
651 { "long", RID_LONG, 0 },
652 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
653 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
654 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
655 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
656 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
657 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
658 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
659 { "register", RID_REGISTER, 0 },
660 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
661 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
662 { "return", RID_RETURN, 0 },
663 { "short", RID_SHORT, 0 },
664 { "signed", RID_SIGNED, 0 },
665 { "sizeof", RID_SIZEOF, 0 },
666 { "static", RID_STATIC, 0 },
667 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
668 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
669 { "struct", RID_STRUCT, 0 },
670 { "switch", RID_SWITCH, 0 },
671 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
672 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
673 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
674 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
675 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
676 { "typedef", RID_TYPEDEF, 0 },
677 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
678 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
679 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
680 { "union", RID_UNION, 0 },
681 { "unsigned", RID_UNSIGNED, 0 },
682 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
683 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
684 { "void", RID_VOID, 0 },
685 { "volatile", RID_VOLATILE, 0 },
686 { "wchar_t", RID_WCHAR, D_CXXONLY },
687 { "while", RID_WHILE, 0 },
688 /* These Objective-C keywords are recognized only immediately after
689 an '@'. */
690 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
691 { "defs", RID_AT_DEFS, D_OBJC },
692 { "encode", RID_AT_ENCODE, D_OBJC },
693 { "end", RID_AT_END, D_OBJC },
694 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
695 { "interface", RID_AT_INTERFACE, D_OBJC },
696 { "protocol", RID_AT_PROTOCOL, D_OBJC },
697 { "selector", RID_AT_SELECTOR, D_OBJC },
698 { "finally", RID_AT_FINALLY, D_OBJC },
699 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
700 /* These are recognized only in protocol-qualifier context
701 (see above) */
702 { "bycopy", RID_BYCOPY, D_OBJC },
703 { "byref", RID_BYREF, D_OBJC },
704 { "in", RID_IN, D_OBJC },
705 { "inout", RID_INOUT, D_OBJC },
706 { "oneway", RID_ONEWAY, D_OBJC },
707 { "out", RID_OUT, D_OBJC },
710 const unsigned int num_c_common_reswords =
711 sizeof c_common_reswords / sizeof (struct c_common_resword);
713 /* Table of machine-independent attributes common to all C-like languages. */
714 const struct attribute_spec c_common_attribute_table[] =
716 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
717 { "packed", 0, 0, false, false, false,
718 handle_packed_attribute },
719 { "nocommon", 0, 0, true, false, false,
720 handle_nocommon_attribute },
721 { "common", 0, 0, true, false, false,
722 handle_common_attribute },
723 /* FIXME: logically, noreturn attributes should be listed as
724 "false, true, true" and apply to function types. But implementing this
725 would require all the places in the compiler that use TREE_THIS_VOLATILE
726 on a decl to identify non-returning functions to be located and fixed
727 to check the function type instead. */
728 { "noreturn", 0, 0, true, false, false,
729 handle_noreturn_attribute },
730 { "volatile", 0, 0, true, false, false,
731 handle_noreturn_attribute },
732 { "noinline", 0, 0, true, false, false,
733 handle_noinline_attribute },
734 { "always_inline", 0, 0, true, false, false,
735 handle_always_inline_attribute },
736 { "gnu_inline", 0, 0, true, false, false,
737 handle_gnu_inline_attribute },
738 { "artificial", 0, 0, true, false, false,
739 handle_artificial_attribute },
740 { "flatten", 0, 0, true, false, false,
741 handle_flatten_attribute },
742 { "used", 0, 0, true, false, false,
743 handle_used_attribute },
744 { "unused", 0, 0, false, false, false,
745 handle_unused_attribute },
746 { "externally_visible", 0, 0, true, false, false,
747 handle_externally_visible_attribute },
748 /* The same comments as for noreturn attributes apply to const ones. */
749 { "const", 0, 0, true, false, false,
750 handle_const_attribute },
751 { "transparent_union", 0, 0, false, false, false,
752 handle_transparent_union_attribute },
753 { "constructor", 0, 1, true, false, false,
754 handle_constructor_attribute },
755 { "destructor", 0, 1, true, false, false,
756 handle_destructor_attribute },
757 { "mode", 1, 1, false, true, false,
758 handle_mode_attribute },
759 { "section", 1, 1, true, false, false,
760 handle_section_attribute },
761 { "aligned", 0, 1, false, false, false,
762 handle_aligned_attribute },
763 { "weak", 0, 0, true, false, false,
764 handle_weak_attribute },
765 { "alias", 1, 1, true, false, false,
766 handle_alias_attribute },
767 { "weakref", 0, 1, true, false, false,
768 handle_weakref_attribute },
769 { "no_instrument_function", 0, 0, true, false, false,
770 handle_no_instrument_function_attribute },
771 { "malloc", 0, 0, true, false, false,
772 handle_malloc_attribute },
773 { "returns_twice", 0, 0, true, false, false,
774 handle_returns_twice_attribute },
775 { "no_stack_limit", 0, 0, true, false, false,
776 handle_no_limit_stack_attribute },
777 { "pure", 0, 0, true, false, false,
778 handle_pure_attribute },
779 /* For internal use (marking of builtins) only. The name contains space
780 to prevent its usage in source code. */
781 { "no vops", 0, 0, true, false, false,
782 handle_novops_attribute },
783 { "deprecated", 0, 1, false, false, false,
784 handle_deprecated_attribute },
785 { "vector_size", 1, 1, false, true, false,
786 handle_vector_size_attribute },
787 { "visibility", 1, 1, false, false, false,
788 handle_visibility_attribute },
789 { "tls_model", 1, 1, true, false, false,
790 handle_tls_model_attribute },
791 { "nonnull", 0, -1, false, true, true,
792 handle_nonnull_attribute },
793 { "nothrow", 0, 0, true, false, false,
794 handle_nothrow_attribute },
795 { "may_alias", 0, 0, false, true, false, NULL },
796 { "cleanup", 1, 1, true, false, false,
797 handle_cleanup_attribute },
798 { "warn_unused_result", 0, 0, false, true, true,
799 handle_warn_unused_result_attribute },
800 { "sentinel", 0, 1, false, true, true,
801 handle_sentinel_attribute },
802 /* For internal use (marking of builtins) only. The name contains space
803 to prevent its usage in source code. */
804 { "type generic", 0, 0, false, true, true,
805 handle_type_generic_attribute },
806 { "alloc_size", 1, 2, false, true, true,
807 handle_alloc_size_attribute },
808 { "cold", 0, 0, true, false, false,
809 handle_cold_attribute },
810 { "hot", 0, 0, true, false, false,
811 handle_hot_attribute },
812 { "warning", 1, 1, true, false, false,
813 handle_error_attribute },
814 { "error", 1, 1, true, false, false,
815 handle_error_attribute },
816 { "target", 1, -1, true, false, false,
817 handle_target_attribute },
818 { "optimize", 1, -1, true, false, false,
819 handle_optimize_attribute },
820 { NULL, 0, 0, false, false, false, NULL }
823 /* Give the specifications for the format attributes, used by C and all
824 descendants. */
826 const struct attribute_spec c_common_format_attribute_table[] =
828 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
829 { "format", 3, 3, false, true, true,
830 handle_format_attribute },
831 { "format_arg", 1, 1, false, true, true,
832 handle_format_arg_attribute },
833 { NULL, 0, 0, false, false, false, NULL }
836 /* Push current bindings for the function name VAR_DECLS. */
838 void
839 start_fname_decls (void)
841 unsigned ix;
842 tree saved = NULL_TREE;
844 for (ix = 0; fname_vars[ix].decl; ix++)
846 tree decl = *fname_vars[ix].decl;
848 if (decl)
850 saved = tree_cons (decl, build_int_cst (NULL_TREE, ix), saved);
851 *fname_vars[ix].decl = NULL_TREE;
854 if (saved || saved_function_name_decls)
855 /* Normally they'll have been NULL, so only push if we've got a
856 stack, or they are non-NULL. */
857 saved_function_name_decls = tree_cons (saved, NULL_TREE,
858 saved_function_name_decls);
861 /* Finish up the current bindings, adding them into the current function's
862 statement tree. This must be done _before_ finish_stmt_tree is called.
863 If there is no current function, we must be at file scope and no statements
864 are involved. Pop the previous bindings. */
866 void
867 finish_fname_decls (void)
869 unsigned ix;
870 tree stmts = NULL_TREE;
871 tree stack = saved_function_name_decls;
873 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
874 append_to_statement_list (TREE_VALUE (stack), &stmts);
876 if (stmts)
878 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
880 if (TREE_CODE (*bodyp) == BIND_EXPR)
881 bodyp = &BIND_EXPR_BODY (*bodyp);
883 append_to_statement_list_force (*bodyp, &stmts);
884 *bodyp = stmts;
887 for (ix = 0; fname_vars[ix].decl; ix++)
888 *fname_vars[ix].decl = NULL_TREE;
890 if (stack)
892 /* We had saved values, restore them. */
893 tree saved;
895 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
897 tree decl = TREE_PURPOSE (saved);
898 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
900 *fname_vars[ix].decl = decl;
902 stack = TREE_CHAIN (stack);
904 saved_function_name_decls = stack;
907 /* Return the text name of the current function, suitably prettified
908 by PRETTY_P. Return string must be freed by caller. */
910 const char *
911 fname_as_string (int pretty_p)
913 const char *name = "top level";
914 char *namep;
915 int vrb = 2, len;
916 cpp_string cstr = { 0, 0 }, strname;
918 if (!pretty_p)
920 name = "";
921 vrb = 0;
924 if (current_function_decl)
925 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
927 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
929 namep = XNEWVEC (char, len);
930 snprintf (namep, len, "\"%s\"", name);
931 strname.text = (unsigned char *) namep;
932 strname.len = len - 1;
934 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
936 XDELETEVEC (namep);
937 return (const char *) cstr.text;
940 return namep;
943 /* Return the VAR_DECL for a const char array naming the current
944 function. If the VAR_DECL has not yet been created, create it
945 now. RID indicates how it should be formatted and IDENTIFIER_NODE
946 ID is its name (unfortunately C and C++ hold the RID values of
947 keywords in different places, so we can't derive RID from ID in
948 this language independent code. LOC is the location of the
949 function. */
951 tree
952 fname_decl (location_t loc, unsigned int rid, tree id)
954 unsigned ix;
955 tree decl = NULL_TREE;
957 for (ix = 0; fname_vars[ix].decl; ix++)
958 if (fname_vars[ix].rid == rid)
959 break;
961 decl = *fname_vars[ix].decl;
962 if (!decl)
964 /* If a tree is built here, it would normally have the lineno of
965 the current statement. Later this tree will be moved to the
966 beginning of the function and this line number will be wrong.
967 To avoid this problem set the lineno to 0 here; that prevents
968 it from appearing in the RTL. */
969 tree stmts;
970 location_t saved_location = input_location;
971 input_location = UNKNOWN_LOCATION;
973 stmts = push_stmt_list ();
974 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
975 stmts = pop_stmt_list (stmts);
976 if (!IS_EMPTY_STMT (stmts))
977 saved_function_name_decls
978 = tree_cons (decl, stmts, saved_function_name_decls);
979 *fname_vars[ix].decl = decl;
980 input_location = saved_location;
982 if (!ix && !current_function_decl)
983 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
985 return decl;
988 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
990 tree
991 fix_string_type (tree value)
993 int length = TREE_STRING_LENGTH (value);
994 int nchars;
995 tree e_type, i_type, a_type;
997 /* Compute the number of elements, for the array type. */
998 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1000 nchars = length;
1001 e_type = char_type_node;
1003 else if (TREE_TYPE (value) == char16_array_type_node)
1005 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1006 e_type = char16_type_node;
1008 else if (TREE_TYPE (value) == char32_array_type_node)
1010 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1011 e_type = char32_type_node;
1013 else
1015 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1016 e_type = wchar_type_node;
1019 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1020 limit in C++98 Annex B is very large (65536) and is not normative,
1021 so we do not diagnose it (warn_overlength_strings is forced off
1022 in c_common_post_options). */
1023 if (warn_overlength_strings)
1025 const int nchars_max = flag_isoc99 ? 4095 : 509;
1026 const int relevant_std = flag_isoc99 ? 99 : 90;
1027 if (nchars - 1 > nchars_max)
1028 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1029 separate the %d from the 'C'. 'ISO' should not be
1030 translated, but it may be moved after 'C%d' in languages
1031 where modifiers follow nouns. */
1032 pedwarn (input_location, OPT_Woverlength_strings,
1033 "string length %qd is greater than the length %qd "
1034 "ISO C%d compilers are required to support",
1035 nchars - 1, nchars_max, relevant_std);
1038 /* Create the array type for the string constant. The ISO C++
1039 standard says that a string literal has type `const char[N]' or
1040 `const wchar_t[N]'. We use the same logic when invoked as a C
1041 front-end with -Wwrite-strings.
1042 ??? We should change the type of an expression depending on the
1043 state of a warning flag. We should just be warning -- see how
1044 this is handled in the C++ front-end for the deprecated implicit
1045 conversion from string literals to `char*' or `wchar_t*'.
1047 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1048 array type being the unqualified version of that type.
1049 Therefore, if we are constructing an array of const char, we must
1050 construct the matching unqualified array type first. The C front
1051 end does not require this, but it does no harm, so we do it
1052 unconditionally. */
1053 i_type = build_index_type (build_int_cst (NULL_TREE, nchars - 1));
1054 a_type = build_array_type (e_type, i_type);
1055 if (c_dialect_cxx() || warn_write_strings)
1056 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1058 TREE_TYPE (value) = a_type;
1059 TREE_CONSTANT (value) = 1;
1060 TREE_READONLY (value) = 1;
1061 TREE_STATIC (value) = 1;
1062 return value;
1065 /* Fully fold EXPR, an expression that was not folded (beyond integer
1066 constant expressions and null pointer constants) when being built
1067 up. If IN_INIT, this is in a static initializer and certain
1068 changes are made to the folding done. Clear *MAYBE_CONST if
1069 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1070 expression because it contains an evaluated operator (in C99) or an
1071 operator outside of sizeof returning an integer constant (in C90)
1072 not permitted in constant expressions, or because it contains an
1073 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1074 set to true by callers before calling this function.) Return the
1075 folded expression. Function arguments have already been folded
1076 before calling this function, as have the contents of SAVE_EXPR,
1077 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1078 C_MAYBE_CONST_EXPR. */
1080 tree
1081 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1083 tree ret;
1084 tree eptype = NULL_TREE;
1085 bool dummy = true;
1086 bool maybe_const_itself = true;
1088 /* This function is not relevant to C++ because C++ folds while
1089 parsing, and may need changes to be correct for C++ when C++
1090 stops folding while parsing. */
1091 if (c_dialect_cxx ())
1092 gcc_unreachable ();
1094 if (!maybe_const)
1095 maybe_const = &dummy;
1096 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1098 eptype = TREE_TYPE (expr);
1099 expr = TREE_OPERAND (expr, 0);
1101 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1102 &maybe_const_itself);
1103 if (eptype)
1104 ret = fold_convert (eptype, ret);
1105 *maybe_const &= maybe_const_itself;
1106 return ret;
1109 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1110 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1111 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1112 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1113 both evaluated and unevaluated subexpressions while
1114 *MAYBE_CONST_ITSELF is carried from only evaluated
1115 subexpressions). */
1117 static tree
1118 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1119 bool *maybe_const_itself)
1121 tree ret = expr;
1122 enum tree_code code = TREE_CODE (expr);
1123 enum tree_code_class kind = TREE_CODE_CLASS (code);
1124 location_t loc = EXPR_LOCATION (expr);
1125 tree op0, op1, op2, op3;
1126 tree orig_op0, orig_op1, orig_op2;
1127 bool op0_const = true, op1_const = true, op2_const = true;
1128 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1129 bool nowarning = TREE_NO_WARNING (expr);
1131 /* This function is not relevant to C++ because C++ folds while
1132 parsing, and may need changes to be correct for C++ when C++
1133 stops folding while parsing. */
1134 if (c_dialect_cxx ())
1135 gcc_unreachable ();
1137 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1138 anything else not counted as an expression cannot usefully be
1139 folded further at this point. */
1140 if (!IS_EXPR_CODE_CLASS (kind)
1141 || kind == tcc_statement
1142 || code == SAVE_EXPR)
1143 return expr;
1145 /* Operands of variable-length expressions (function calls) have
1146 already been folded, as have __builtin_* function calls, and such
1147 expressions cannot occur in constant expressions. */
1148 if (kind == tcc_vl_exp)
1150 *maybe_const_operands = false;
1151 ret = fold (expr);
1152 goto out;
1155 if (code == C_MAYBE_CONST_EXPR)
1157 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1158 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1159 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1160 *maybe_const_operands = false;
1161 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1162 *maybe_const_itself = false;
1163 if (pre && !in_init)
1164 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1165 else
1166 ret = inner;
1167 goto out;
1170 /* Assignment, increment, decrement, function call and comma
1171 operators, and statement expressions, cannot occur in constant
1172 expressions if evaluated / outside of sizeof. (Function calls
1173 were handled above, though VA_ARG_EXPR is treated like a function
1174 call here, and statement expressions are handled through
1175 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1176 switch (code)
1178 case MODIFY_EXPR:
1179 case PREDECREMENT_EXPR:
1180 case PREINCREMENT_EXPR:
1181 case POSTDECREMENT_EXPR:
1182 case POSTINCREMENT_EXPR:
1183 case COMPOUND_EXPR:
1184 *maybe_const_operands = false;
1185 break;
1187 case VA_ARG_EXPR:
1188 case TARGET_EXPR:
1189 case BIND_EXPR:
1190 case OBJ_TYPE_REF:
1191 *maybe_const_operands = false;
1192 ret = fold (expr);
1193 goto out;
1195 default:
1196 break;
1199 /* Fold individual tree codes as appropriate. */
1200 switch (code)
1202 case COMPOUND_LITERAL_EXPR:
1203 /* Any non-constancy will have been marked in a containing
1204 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1205 goto out;
1207 case COMPONENT_REF:
1208 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1209 op1 = TREE_OPERAND (expr, 1);
1210 op2 = TREE_OPERAND (expr, 2);
1211 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1212 maybe_const_itself);
1213 if (op0 != orig_op0)
1214 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1215 if (ret != expr)
1217 TREE_READONLY (ret) = TREE_READONLY (expr);
1218 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1220 goto out;
1222 case ARRAY_REF:
1223 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1224 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1225 op2 = TREE_OPERAND (expr, 2);
1226 op3 = TREE_OPERAND (expr, 3);
1227 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1228 maybe_const_itself);
1229 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1230 maybe_const_itself);
1231 op1 = decl_constant_value_for_optimization (op1);
1232 if (op0 != orig_op0 || op1 != orig_op1)
1233 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1234 if (ret != expr)
1236 TREE_READONLY (ret) = TREE_READONLY (expr);
1237 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1238 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1240 ret = fold (ret);
1241 goto out;
1243 case COMPOUND_EXPR:
1244 case MODIFY_EXPR:
1245 case PREDECREMENT_EXPR:
1246 case PREINCREMENT_EXPR:
1247 case POSTDECREMENT_EXPR:
1248 case POSTINCREMENT_EXPR:
1249 case PLUS_EXPR:
1250 case MINUS_EXPR:
1251 case MULT_EXPR:
1252 case POINTER_PLUS_EXPR:
1253 case TRUNC_DIV_EXPR:
1254 case CEIL_DIV_EXPR:
1255 case FLOOR_DIV_EXPR:
1256 case TRUNC_MOD_EXPR:
1257 case RDIV_EXPR:
1258 case EXACT_DIV_EXPR:
1259 case LSHIFT_EXPR:
1260 case RSHIFT_EXPR:
1261 case BIT_IOR_EXPR:
1262 case BIT_XOR_EXPR:
1263 case BIT_AND_EXPR:
1264 case LT_EXPR:
1265 case LE_EXPR:
1266 case GT_EXPR:
1267 case GE_EXPR:
1268 case EQ_EXPR:
1269 case NE_EXPR:
1270 case COMPLEX_EXPR:
1271 case TRUTH_AND_EXPR:
1272 case TRUTH_OR_EXPR:
1273 case TRUTH_XOR_EXPR:
1274 case UNORDERED_EXPR:
1275 case ORDERED_EXPR:
1276 case UNLT_EXPR:
1277 case UNLE_EXPR:
1278 case UNGT_EXPR:
1279 case UNGE_EXPR:
1280 case UNEQ_EXPR:
1281 /* Binary operations evaluating both arguments (increment and
1282 decrement are binary internally in GCC). */
1283 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1284 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1285 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1286 maybe_const_itself);
1287 if (code != MODIFY_EXPR
1288 && code != PREDECREMENT_EXPR
1289 && code != PREINCREMENT_EXPR
1290 && code != POSTDECREMENT_EXPR
1291 && code != POSTINCREMENT_EXPR)
1292 op0 = decl_constant_value_for_optimization (op0);
1293 /* The RHS of a MODIFY_EXPR was fully folded when building that
1294 expression for the sake of conversion warnings. */
1295 if (code != MODIFY_EXPR)
1296 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1297 maybe_const_itself);
1298 op1 = decl_constant_value_for_optimization (op1);
1299 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1300 ret = in_init
1301 ? fold_build2_initializer (code, TREE_TYPE (expr), op0, op1)
1302 : fold_build2 (code, TREE_TYPE (expr), op0, op1);
1303 else
1304 ret = fold (expr);
1305 goto out;
1307 case INDIRECT_REF:
1308 case FIX_TRUNC_EXPR:
1309 case FLOAT_EXPR:
1310 CASE_CONVERT:
1311 case NON_LVALUE_EXPR:
1312 case NEGATE_EXPR:
1313 case BIT_NOT_EXPR:
1314 case TRUTH_NOT_EXPR:
1315 case ADDR_EXPR:
1316 case CONJ_EXPR:
1317 case REALPART_EXPR:
1318 case IMAGPART_EXPR:
1319 /* Unary operations. */
1320 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1321 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1322 maybe_const_itself);
1323 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1324 op0 = decl_constant_value_for_optimization (op0);
1325 if (op0 != orig_op0 || in_init)
1326 ret = in_init
1327 ? fold_build1_initializer (code, TREE_TYPE (expr), op0)
1328 : fold_build1 (code, TREE_TYPE (expr), op0);
1329 else
1330 ret = fold (expr);
1331 if (code == INDIRECT_REF
1332 && ret != expr
1333 && TREE_CODE (ret) == INDIRECT_REF)
1335 TREE_READONLY (ret) = TREE_READONLY (expr);
1336 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1337 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1339 goto out;
1341 case TRUTH_ANDIF_EXPR:
1342 case TRUTH_ORIF_EXPR:
1343 /* Binary operations not necessarily evaluating both
1344 arguments. */
1345 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1346 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1347 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1348 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1349 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1350 ret = in_init
1351 ? fold_build2_initializer (code, TREE_TYPE (expr), op0, op1)
1352 : fold_build2 (code, TREE_TYPE (expr), op0, op1);
1353 else
1354 ret = fold (expr);
1355 *maybe_const_operands &= op0_const;
1356 *maybe_const_itself &= op0_const_self;
1357 if (!(flag_isoc99
1358 && op0_const
1359 && op0_const_self
1360 && (code == TRUTH_ANDIF_EXPR
1361 ? op0 == truthvalue_false_node
1362 : op0 == truthvalue_true_node)))
1363 *maybe_const_operands &= op1_const;
1364 if (!(op0_const
1365 && op0_const_self
1366 && (code == TRUTH_ANDIF_EXPR
1367 ? op0 == truthvalue_false_node
1368 : op0 == truthvalue_true_node)))
1369 *maybe_const_itself &= op1_const_self;
1370 goto out;
1372 case COND_EXPR:
1373 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1374 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1375 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1376 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1377 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1378 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1379 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1380 ret = fold_build3 (code, TREE_TYPE (expr), op0, op1, op2);
1381 else
1382 ret = fold (expr);
1383 *maybe_const_operands &= op0_const;
1384 *maybe_const_itself &= op0_const_self;
1385 if (!(flag_isoc99
1386 && op0_const
1387 && op0_const_self
1388 && op0 == truthvalue_false_node))
1389 *maybe_const_operands &= op1_const;
1390 if (!(op0_const
1391 && op0_const_self
1392 && op0 == truthvalue_false_node))
1393 *maybe_const_itself &= op1_const_self;
1394 if (!(flag_isoc99
1395 && op0_const
1396 && op0_const_self
1397 && op0 == truthvalue_true_node))
1398 *maybe_const_operands &= op2_const;
1399 if (!(op0_const
1400 && op0_const_self
1401 && op0 == truthvalue_true_node))
1402 *maybe_const_itself &= op2_const_self;
1403 goto out;
1405 case EXCESS_PRECISION_EXPR:
1406 /* Each case where an operand with excess precision may be
1407 encountered must remove the EXCESS_PRECISION_EXPR around
1408 inner operands and possibly put one around the whole
1409 expression or possibly convert to the semantic type (which
1410 c_fully_fold does); we cannot tell at this stage which is
1411 appropriate in any particular case. */
1412 gcc_unreachable ();
1414 default:
1415 /* Various codes may appear through folding built-in functions
1416 and their arguments. */
1417 goto out;
1420 out:
1421 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1422 have been done by this point, so remove them again. */
1423 nowarning |= TREE_NO_WARNING (ret);
1424 STRIP_TYPE_NOPS (ret);
1425 if (nowarning && !TREE_NO_WARNING (ret))
1427 if (!CAN_HAVE_LOCATION_P (ret))
1428 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1429 TREE_NO_WARNING (ret) = 1;
1431 if (ret != expr)
1432 protected_set_expr_location (ret, loc);
1433 return ret;
1436 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1437 return EXP. Otherwise, return either EXP or its known constant
1438 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1439 Is the BLKmode test appropriate? */
1441 tree
1442 decl_constant_value_for_optimization (tree exp)
1444 tree ret;
1446 /* This function is only used by C, for c_fully_fold and other
1447 optimization, and may not be correct for C++. */
1448 if (c_dialect_cxx ())
1449 gcc_unreachable ();
1451 if (!optimize
1452 || TREE_CODE (exp) != VAR_DECL
1453 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1454 || DECL_MODE (exp) == BLKmode)
1455 return exp;
1457 ret = decl_constant_value (exp);
1458 /* Avoid unwanted tree sharing between the initializer and current
1459 function's body where the tree can be modified e.g. by the
1460 gimplifier. */
1461 if (ret != exp && TREE_STATIC (exp))
1462 ret = unshare_expr (ret);
1463 return ret;
1466 /* Print a warning if a constant expression had overflow in folding.
1467 Invoke this function on every expression that the language
1468 requires to be a constant expression.
1469 Note the ANSI C standard says it is erroneous for a
1470 constant expression to overflow. */
1472 void
1473 constant_expression_warning (tree value)
1475 if (warn_overflow && pedantic
1476 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1477 || TREE_CODE (value) == FIXED_CST
1478 || TREE_CODE (value) == VECTOR_CST
1479 || TREE_CODE (value) == COMPLEX_CST)
1480 && TREE_OVERFLOW (value))
1481 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1484 /* The same as above but print an unconditional error. */
1485 void
1486 constant_expression_error (tree value)
1488 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1489 || TREE_CODE (value) == FIXED_CST
1490 || TREE_CODE (value) == VECTOR_CST
1491 || TREE_CODE (value) == COMPLEX_CST)
1492 && TREE_OVERFLOW (value))
1493 error ("overflow in constant expression");
1496 /* Print a warning if an expression had overflow in folding and its
1497 operands hadn't.
1499 Invoke this function on every expression that
1500 (1) appears in the source code, and
1501 (2) is a constant expression that overflowed, and
1502 (3) is not already checked by convert_and_check;
1503 however, do not invoke this function on operands of explicit casts
1504 or when the expression is the result of an operator and any operand
1505 already overflowed. */
1507 void
1508 overflow_warning (tree value)
1510 if (skip_evaluation) return;
1512 switch (TREE_CODE (value))
1514 case INTEGER_CST:
1515 warning (OPT_Woverflow, "integer overflow in expression");
1516 break;
1518 case REAL_CST:
1519 warning (OPT_Woverflow, "floating point overflow in expression");
1520 break;
1522 case FIXED_CST:
1523 warning (OPT_Woverflow, "fixed-point overflow in expression");
1524 break;
1526 case VECTOR_CST:
1527 warning (OPT_Woverflow, "vector overflow in expression");
1528 break;
1530 case COMPLEX_CST:
1531 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1532 warning (OPT_Woverflow, "complex integer overflow in expression");
1533 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1534 warning (OPT_Woverflow, "complex floating point overflow in expression");
1535 break;
1537 default:
1538 break;
1542 /* Warn about uses of logical || / && operator in a context where it
1543 is likely that the bitwise equivalent was intended by the
1544 programmer. We have seen an expression in which CODE is a binary
1545 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1546 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1547 void
1548 warn_logical_operator (location_t location, enum tree_code code, tree type,
1549 enum tree_code code_left, tree op_left,
1550 enum tree_code ARG_UNUSED (code_right), tree op_right)
1552 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1553 int in0_p, in1_p, in_p;
1554 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1555 bool strict_overflow_p = false;
1557 if (code != TRUTH_ANDIF_EXPR
1558 && code != TRUTH_AND_EXPR
1559 && code != TRUTH_ORIF_EXPR
1560 && code != TRUTH_OR_EXPR)
1561 return;
1563 /* Warn if &&/|| are being used in a context where it is
1564 likely that the bitwise equivalent was intended by the
1565 programmer. That is, an expression such as op && MASK
1566 where op should not be any boolean expression, nor a
1567 constant, and mask seems to be a non-boolean integer constant. */
1568 if (!truth_value_p (code_left)
1569 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1570 && !CONSTANT_CLASS_P (op_left)
1571 && !TREE_NO_WARNING (op_left)
1572 && TREE_CODE (op_right) == INTEGER_CST
1573 && !integer_zerop (op_right)
1574 && !integer_onep (op_right))
1576 if (or_op)
1577 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1578 " applied to non-boolean constant");
1579 else
1580 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1581 " applied to non-boolean constant");
1582 TREE_NO_WARNING (op_left) = true;
1583 return;
1586 /* We do not warn for constants because they are typical of macro
1587 expansions that test for features. */
1588 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1589 return;
1591 /* This warning only makes sense with logical operands. */
1592 if (!(truth_value_p (TREE_CODE (op_left))
1593 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1594 || !(truth_value_p (TREE_CODE (op_right))
1595 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1596 return;
1598 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1599 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1601 if (lhs && TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1602 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1604 if (rhs && TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1605 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1607 /* If this is an OR operation, invert both sides; we will invert
1608 again at the end. */
1609 if (or_op)
1610 in0_p = !in0_p, in1_p = !in1_p;
1612 /* If both expressions are the same, if we can merge the ranges, and we
1613 can build the range test, return it or it inverted. */
1614 if (lhs && rhs && operand_equal_p (lhs, rhs, 0)
1615 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1616 in1_p, low1, high1)
1617 && 0 != (tem = build_range_check (type, lhs, in_p, low, high)))
1619 if (TREE_CODE (tem) != INTEGER_CST)
1620 return;
1622 if (or_op)
1623 warning_at (location, OPT_Wlogical_op,
1624 "logical %<or%> "
1625 "of collectively exhaustive tests is always true");
1626 else
1627 warning_at (location, OPT_Wlogical_op,
1628 "logical %<and%> "
1629 "of mutually exclusive tests is always false");
1634 /* Print a warning about casts that might indicate violation
1635 of strict aliasing rules if -Wstrict-aliasing is used and
1636 strict aliasing mode is in effect. OTYPE is the original
1637 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1639 bool
1640 strict_aliasing_warning (tree otype, tree type, tree expr)
1642 /* Strip pointer conversion chains and get to the correct original type. */
1643 STRIP_NOPS (expr);
1644 otype = TREE_TYPE (expr);
1646 if (!(flag_strict_aliasing
1647 && POINTER_TYPE_P (type)
1648 && POINTER_TYPE_P (otype)
1649 && !VOID_TYPE_P (TREE_TYPE (type)))
1650 /* If the type we are casting to is a ref-all pointer
1651 dereferencing it is always valid. */
1652 || TYPE_REF_CAN_ALIAS_ALL (type))
1653 return false;
1655 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1656 && (DECL_P (TREE_OPERAND (expr, 0))
1657 || handled_component_p (TREE_OPERAND (expr, 0))))
1659 /* Casting the address of an object to non void pointer. Warn
1660 if the cast breaks type based aliasing. */
1661 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1663 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1664 "might break strict-aliasing rules");
1665 return true;
1667 else
1669 /* warn_strict_aliasing >= 3. This includes the default (3).
1670 Only warn if the cast is dereferenced immediately. */
1671 alias_set_type set1 =
1672 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1673 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1675 if (set1 != set2 && set2 != 0
1676 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1678 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1679 "pointer will break strict-aliasing rules");
1680 return true;
1682 else if (warn_strict_aliasing == 2
1683 && !alias_sets_must_conflict_p (set1, set2))
1685 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1686 "pointer might break strict-aliasing rules");
1687 return true;
1691 else
1692 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1694 /* At this level, warn for any conversions, even if an address is
1695 not taken in the same statement. This will likely produce many
1696 false positives, but could be useful to pinpoint problems that
1697 are not revealed at higher levels. */
1698 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1699 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1700 if (!COMPLETE_TYPE_P (type)
1701 || !alias_sets_must_conflict_p (set1, set2))
1703 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1704 "pointer might break strict-aliasing rules");
1705 return true;
1709 return false;
1712 /* Warn for unlikely, improbable, or stupid DECL declarations
1713 of `main'. */
1715 void
1716 check_main_parameter_types (tree decl)
1718 tree args;
1719 int argct = 0;
1721 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl)); args;
1722 args = TREE_CHAIN (args))
1724 tree type = args ? TREE_VALUE (args) : 0;
1726 if (type == void_type_node || type == error_mark_node )
1727 break;
1729 ++argct;
1730 switch (argct)
1732 case 1:
1733 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1734 pedwarn (input_location, OPT_Wmain, "first argument of %q+D should be %<int%>",
1735 decl);
1736 break;
1738 case 2:
1739 if (TREE_CODE (type) != POINTER_TYPE
1740 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1741 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1742 != char_type_node))
1743 pedwarn (input_location, OPT_Wmain, "second argument of %q+D should be %<char **%>",
1744 decl);
1745 break;
1747 case 3:
1748 if (TREE_CODE (type) != POINTER_TYPE
1749 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1750 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1751 != char_type_node))
1752 pedwarn (input_location, OPT_Wmain, "third argument of %q+D should probably be "
1753 "%<char **%>", decl);
1754 break;
1758 /* It is intentional that this message does not mention the third
1759 argument because it's only mentioned in an appendix of the
1760 standard. */
1761 if (argct > 0 && (argct < 2 || argct > 3))
1762 pedwarn (input_location, OPT_Wmain, "%q+D takes only zero or two arguments", decl);
1765 /* True if pointers to distinct types T1 and T2 can be converted to
1766 each other without an explicit cast. Only returns true for opaque
1767 vector types. */
1768 bool
1769 vector_targets_convertible_p (const_tree t1, const_tree t2)
1771 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
1772 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1773 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1774 return true;
1776 return false;
1779 /* True if vector types T1 and T2 can be converted to each other
1780 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1781 can only be converted with -flax-vector-conversions yet that is not
1782 in effect, emit a note telling the user about that option if such
1783 a note has not previously been emitted. */
1784 bool
1785 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1787 static bool emitted_lax_note = false;
1788 bool convertible_lax;
1790 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1791 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1792 return true;
1794 convertible_lax =
1795 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1796 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
1797 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1798 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1799 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1801 if (!convertible_lax || flag_lax_vector_conversions)
1802 return convertible_lax;
1804 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1805 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1806 return true;
1808 if (emit_lax_note && !emitted_lax_note)
1810 emitted_lax_note = true;
1811 inform (input_location, "use -flax-vector-conversions to permit "
1812 "conversions between vectors with differing "
1813 "element types or numbers of subparts");
1816 return false;
1819 /* This is a helper function of build_binary_op.
1821 For certain operations if both args were extended from the same
1822 smaller type, do the arithmetic in that type and then extend.
1824 BITWISE indicates a bitwise operation.
1825 For them, this optimization is safe only if
1826 both args are zero-extended or both are sign-extended.
1827 Otherwise, we might change the result.
1828 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1829 but calculated in (unsigned short) it would be (unsigned short)-1.
1831 tree shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1833 int unsigned0, unsigned1;
1834 tree arg0, arg1;
1835 int uns;
1836 tree type;
1838 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1839 excessive narrowing when we call get_narrower below. For
1840 example, suppose that OP0 is of unsigned int extended
1841 from signed char and that RESULT_TYPE is long long int.
1842 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1843 like
1845 (long long int) (unsigned int) signed_char
1847 which get_narrower would narrow down to
1849 (unsigned int) signed char
1851 If we do not cast OP0 first, get_narrower would return
1852 signed_char, which is inconsistent with the case of the
1853 explicit cast. */
1854 op0 = convert (result_type, op0);
1855 op1 = convert (result_type, op1);
1857 arg0 = get_narrower (op0, &unsigned0);
1858 arg1 = get_narrower (op1, &unsigned1);
1860 /* UNS is 1 if the operation to be done is an unsigned one. */
1861 uns = TYPE_UNSIGNED (result_type);
1863 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1864 but it *requires* conversion to FINAL_TYPE. */
1866 if ((TYPE_PRECISION (TREE_TYPE (op0))
1867 == TYPE_PRECISION (TREE_TYPE (arg0)))
1868 && TREE_TYPE (op0) != result_type)
1869 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1870 if ((TYPE_PRECISION (TREE_TYPE (op1))
1871 == TYPE_PRECISION (TREE_TYPE (arg1)))
1872 && TREE_TYPE (op1) != result_type)
1873 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1875 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1877 /* For bitwise operations, signedness of nominal type
1878 does not matter. Consider only how operands were extended. */
1879 if (bitwise)
1880 uns = unsigned0;
1882 /* Note that in all three cases below we refrain from optimizing
1883 an unsigned operation on sign-extended args.
1884 That would not be valid. */
1886 /* Both args variable: if both extended in same way
1887 from same width, do it in that width.
1888 Do it unsigned if args were zero-extended. */
1889 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1890 < TYPE_PRECISION (result_type))
1891 && (TYPE_PRECISION (TREE_TYPE (arg1))
1892 == TYPE_PRECISION (TREE_TYPE (arg0)))
1893 && unsigned0 == unsigned1
1894 && (unsigned0 || !uns))
1895 return c_common_signed_or_unsigned_type
1896 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1898 else if (TREE_CODE (arg0) == INTEGER_CST
1899 && (unsigned1 || !uns)
1900 && (TYPE_PRECISION (TREE_TYPE (arg1))
1901 < TYPE_PRECISION (result_type))
1902 && (type
1903 = c_common_signed_or_unsigned_type (unsigned1,
1904 TREE_TYPE (arg1)))
1905 && !POINTER_TYPE_P (type)
1906 && int_fits_type_p (arg0, type))
1907 return type;
1909 else if (TREE_CODE (arg1) == INTEGER_CST
1910 && (unsigned0 || !uns)
1911 && (TYPE_PRECISION (TREE_TYPE (arg0))
1912 < TYPE_PRECISION (result_type))
1913 && (type
1914 = c_common_signed_or_unsigned_type (unsigned0,
1915 TREE_TYPE (arg0)))
1916 && !POINTER_TYPE_P (type)
1917 && int_fits_type_p (arg1, type))
1918 return type;
1920 return result_type;
1923 /* Warns if the conversion of EXPR to TYPE may alter a value.
1924 This is a helper function for warnings_for_convert_and_check. */
1926 static void
1927 conversion_warning (tree type, tree expr)
1929 bool give_warning = false;
1931 int i;
1932 const int expr_num_operands = TREE_OPERAND_LENGTH (expr);
1933 tree expr_type = TREE_TYPE (expr);
1935 if (!warn_conversion && !warn_sign_conversion)
1936 return;
1938 /* If any operand is artificial, then this expression was generated
1939 by the compiler and we do not warn. */
1940 for (i = 0; i < expr_num_operands; i++)
1942 tree op = TREE_OPERAND (expr, i);
1943 if (op && DECL_P (op) && DECL_ARTIFICIAL (op))
1944 return;
1947 switch (TREE_CODE (expr))
1949 case EQ_EXPR:
1950 case NE_EXPR:
1951 case LE_EXPR:
1952 case GE_EXPR:
1953 case LT_EXPR:
1954 case GT_EXPR:
1955 case TRUTH_ANDIF_EXPR:
1956 case TRUTH_ORIF_EXPR:
1957 case TRUTH_AND_EXPR:
1958 case TRUTH_OR_EXPR:
1959 case TRUTH_XOR_EXPR:
1960 case TRUTH_NOT_EXPR:
1961 /* Conversion from boolean to a signed:1 bit-field (which only
1962 can hold the values 0 and -1) doesn't lose information - but
1963 it does change the value. */
1964 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
1965 warning (OPT_Wconversion,
1966 "conversion to %qT from boolean expression", type);
1967 return;
1969 case REAL_CST:
1970 case INTEGER_CST:
1972 /* Warn for real constant that is not an exact integer converted
1973 to integer type. */
1974 if (TREE_CODE (expr_type) == REAL_TYPE
1975 && TREE_CODE (type) == INTEGER_TYPE)
1977 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1978 give_warning = true;
1980 /* Warn for an integer constant that does not fit into integer type. */
1981 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1982 && TREE_CODE (type) == INTEGER_TYPE
1983 && !int_fits_type_p (expr, type))
1985 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1986 && tree_int_cst_sgn (expr) < 0)
1987 warning (OPT_Wsign_conversion,
1988 "negative integer implicitly converted to unsigned type");
1989 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1990 warning (OPT_Wsign_conversion, "conversion of unsigned constant "
1991 "value to negative integer");
1992 else
1993 give_warning = true;
1995 else if (TREE_CODE (type) == REAL_TYPE)
1997 /* Warn for an integer constant that does not fit into real type. */
1998 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2000 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2001 if (!exact_real_truncate (TYPE_MODE (type), &a))
2002 give_warning = true;
2004 /* Warn for a real constant that does not fit into a smaller
2005 real type. */
2006 else if (TREE_CODE (expr_type) == REAL_TYPE
2007 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2009 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2010 if (!exact_real_truncate (TYPE_MODE (type), &a))
2011 give_warning = true;
2015 if (give_warning)
2016 warning (OPT_Wconversion,
2017 "conversion to %qT alters %qT constant value",
2018 type, expr_type);
2020 return;
2022 case COND_EXPR:
2024 /* In case of COND_EXPR, if both operands are constants or
2025 COND_EXPR, then we do not care about the type of COND_EXPR,
2026 only about the conversion of each operand. */
2027 tree op1 = TREE_OPERAND (expr, 1);
2028 tree op2 = TREE_OPERAND (expr, 2);
2030 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2031 || TREE_CODE (op1) == COND_EXPR)
2032 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2033 || TREE_CODE (op2) == COND_EXPR))
2035 conversion_warning (type, op1);
2036 conversion_warning (type, op2);
2037 return;
2039 /* Fall through. */
2042 default: /* 'expr' is not a constant. */
2044 /* Warn for real types converted to integer types. */
2045 if (TREE_CODE (expr_type) == REAL_TYPE
2046 && TREE_CODE (type) == INTEGER_TYPE)
2047 give_warning = true;
2049 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2050 && TREE_CODE (type) == INTEGER_TYPE)
2052 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2053 expr = get_unwidened (expr, 0);
2054 expr_type = TREE_TYPE (expr);
2056 /* Don't warn for short y; short x = ((int)y & 0xff); */
2057 if (TREE_CODE (expr) == BIT_AND_EXPR
2058 || TREE_CODE (expr) == BIT_IOR_EXPR
2059 || TREE_CODE (expr) == BIT_XOR_EXPR)
2061 /* If both args were extended from a shortest type,
2062 use that type if that is safe. */
2063 expr_type = shorten_binary_op (expr_type,
2064 TREE_OPERAND (expr, 0),
2065 TREE_OPERAND (expr, 1),
2066 /* bitwise */1);
2068 if (TREE_CODE (expr) == BIT_AND_EXPR)
2070 tree op0 = TREE_OPERAND (expr, 0);
2071 tree op1 = TREE_OPERAND (expr, 1);
2072 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2073 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2075 /* If one of the operands is a non-negative constant
2076 that fits in the target type, then the type of the
2077 other operand does not matter. */
2078 if ((TREE_CODE (op0) == INTEGER_CST
2079 && int_fits_type_p (op0, c_common_signed_type (type))
2080 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2081 || (TREE_CODE (op1) == INTEGER_CST
2082 && int_fits_type_p (op1, c_common_signed_type (type))
2083 && int_fits_type_p (op1,
2084 c_common_unsigned_type (type))))
2085 return;
2086 /* If constant is unsigned and fits in the target
2087 type, then the result will also fit. */
2088 else if ((TREE_CODE (op0) == INTEGER_CST
2089 && unsigned0
2090 && int_fits_type_p (op0, type))
2091 || (TREE_CODE (op1) == INTEGER_CST
2092 && unsigned1
2093 && int_fits_type_p (op1, type)))
2094 return;
2097 /* Warn for integer types converted to smaller integer types. */
2098 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2099 give_warning = true;
2101 /* When they are the same width but different signedness,
2102 then the value may change. */
2103 else if ((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2104 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2105 /* Even when converted to a bigger type, if the type is
2106 unsigned but expr is signed, then negative values
2107 will be changed. */
2108 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2109 warning (OPT_Wsign_conversion, "conversion to %qT from %qT "
2110 "may change the sign of the result",
2111 type, expr_type);
2114 /* Warn for integer types converted to real types if and only if
2115 all the range of values of the integer type cannot be
2116 represented by the real type. */
2117 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2118 && TREE_CODE (type) == REAL_TYPE)
2120 tree type_low_bound = TYPE_MIN_VALUE (expr_type);
2121 tree type_high_bound = TYPE_MAX_VALUE (expr_type);
2122 REAL_VALUE_TYPE real_low_bound
2123 = real_value_from_int_cst (0, type_low_bound);
2124 REAL_VALUE_TYPE real_high_bound
2125 = real_value_from_int_cst (0, type_high_bound);
2127 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2128 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2129 give_warning = true;
2132 /* Warn for real types converted to smaller real types. */
2133 else if (TREE_CODE (expr_type) == REAL_TYPE
2134 && TREE_CODE (type) == REAL_TYPE
2135 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2136 give_warning = true;
2139 if (give_warning)
2140 warning (OPT_Wconversion,
2141 "conversion to %qT from %qT may alter its value",
2142 type, expr_type);
2146 /* Produce warnings after a conversion. RESULT is the result of
2147 converting EXPR to TYPE. This is a helper function for
2148 convert_and_check and cp_convert_and_check. */
2150 void
2151 warnings_for_convert_and_check (tree type, tree expr, tree result)
2153 if (TREE_CODE (expr) == INTEGER_CST
2154 && (TREE_CODE (type) == INTEGER_TYPE
2155 || TREE_CODE (type) == ENUMERAL_TYPE)
2156 && !int_fits_type_p (expr, type))
2158 /* Do not diagnose overflow in a constant expression merely
2159 because a conversion overflowed. */
2160 if (TREE_OVERFLOW (result))
2161 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2163 if (TYPE_UNSIGNED (type))
2165 /* This detects cases like converting -129 or 256 to
2166 unsigned char. */
2167 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2168 warning (OPT_Woverflow,
2169 "large integer implicitly truncated to unsigned type");
2170 else
2171 conversion_warning (type, expr);
2173 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2174 warning (OPT_Woverflow,
2175 "overflow in implicit constant conversion");
2176 /* No warning for converting 0x80000000 to int. */
2177 else if (pedantic
2178 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2179 || TYPE_PRECISION (TREE_TYPE (expr))
2180 != TYPE_PRECISION (type)))
2181 warning (OPT_Woverflow,
2182 "overflow in implicit constant conversion");
2184 else
2185 conversion_warning (type, expr);
2187 else if ((TREE_CODE (result) == INTEGER_CST
2188 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2189 warning (OPT_Woverflow,
2190 "overflow in implicit constant conversion");
2191 else
2192 conversion_warning (type, expr);
2196 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2197 Invoke this function on every expression that is converted implicitly,
2198 i.e. because of language rules and not because of an explicit cast. */
2200 tree
2201 convert_and_check (tree type, tree expr)
2203 tree result;
2204 tree expr_for_warning;
2206 /* Convert from a value with possible excess precision rather than
2207 via the semantic type, but do not warn about values not fitting
2208 exactly in the semantic type. */
2209 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2211 tree orig_type = TREE_TYPE (expr);
2212 expr = TREE_OPERAND (expr, 0);
2213 expr_for_warning = convert (orig_type, expr);
2214 if (orig_type == type)
2215 return expr_for_warning;
2217 else
2218 expr_for_warning = expr;
2220 if (TREE_TYPE (expr) == type)
2221 return expr;
2223 result = convert (type, expr);
2225 if (!skip_evaluation && !TREE_OVERFLOW_P (expr) && result != error_mark_node)
2226 warnings_for_convert_and_check (type, expr_for_warning, result);
2228 return result;
2231 /* A node in a list that describes references to variables (EXPR), which are
2232 either read accesses if WRITER is zero, or write accesses, in which case
2233 WRITER is the parent of EXPR. */
2234 struct tlist
2236 struct tlist *next;
2237 tree expr, writer;
2240 /* Used to implement a cache the results of a call to verify_tree. We only
2241 use this for SAVE_EXPRs. */
2242 struct tlist_cache
2244 struct tlist_cache *next;
2245 struct tlist *cache_before_sp;
2246 struct tlist *cache_after_sp;
2247 tree expr;
2250 /* Obstack to use when allocating tlist structures, and corresponding
2251 firstobj. */
2252 static struct obstack tlist_obstack;
2253 static char *tlist_firstobj = 0;
2255 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2256 warnings. */
2257 static struct tlist *warned_ids;
2258 /* SAVE_EXPRs need special treatment. We process them only once and then
2259 cache the results. */
2260 static struct tlist_cache *save_expr_cache;
2262 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2263 static void merge_tlist (struct tlist **, struct tlist *, int);
2264 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2265 static int warning_candidate_p (tree);
2266 static bool candidate_equal_p (const_tree, const_tree);
2267 static void warn_for_collisions (struct tlist *);
2268 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2269 static struct tlist *new_tlist (struct tlist *, tree, tree);
2271 /* Create a new struct tlist and fill in its fields. */
2272 static struct tlist *
2273 new_tlist (struct tlist *next, tree t, tree writer)
2275 struct tlist *l;
2276 l = XOBNEW (&tlist_obstack, struct tlist);
2277 l->next = next;
2278 l->expr = t;
2279 l->writer = writer;
2280 return l;
2283 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2284 is nonnull, we ignore any node we find which has a writer equal to it. */
2286 static void
2287 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2289 while (add)
2291 struct tlist *next = add->next;
2292 if (!copy)
2293 add->next = *to;
2294 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2295 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2296 add = next;
2300 /* Merge the nodes of ADD into TO. This merging process is done so that for
2301 each variable that already exists in TO, no new node is added; however if
2302 there is a write access recorded in ADD, and an occurrence on TO is only
2303 a read access, then the occurrence in TO will be modified to record the
2304 write. */
2306 static void
2307 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2309 struct tlist **end = to;
2311 while (*end)
2312 end = &(*end)->next;
2314 while (add)
2316 int found = 0;
2317 struct tlist *tmp2;
2318 struct tlist *next = add->next;
2320 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2321 if (candidate_equal_p (tmp2->expr, add->expr))
2323 found = 1;
2324 if (!tmp2->writer)
2325 tmp2->writer = add->writer;
2327 if (!found)
2329 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2330 end = &(*end)->next;
2331 *end = 0;
2333 add = next;
2337 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2338 references in list LIST conflict with it, excluding reads if ONLY writers
2339 is nonzero. */
2341 static void
2342 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2343 int only_writes)
2345 struct tlist *tmp;
2347 /* Avoid duplicate warnings. */
2348 for (tmp = warned_ids; tmp; tmp = tmp->next)
2349 if (candidate_equal_p (tmp->expr, written))
2350 return;
2352 while (list)
2354 if (candidate_equal_p (list->expr, written)
2355 && !candidate_equal_p (list->writer, writer)
2356 && (!only_writes || list->writer))
2358 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2359 warning_at (EXPR_HAS_LOCATION (writer)
2360 ? EXPR_LOCATION (writer) : input_location,
2361 OPT_Wsequence_point, "operation on %qE may be undefined",
2362 list->expr);
2364 list = list->next;
2368 /* Given a list LIST of references to variables, find whether any of these
2369 can cause conflicts due to missing sequence points. */
2371 static void
2372 warn_for_collisions (struct tlist *list)
2374 struct tlist *tmp;
2376 for (tmp = list; tmp; tmp = tmp->next)
2378 if (tmp->writer)
2379 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2383 /* Return nonzero if X is a tree that can be verified by the sequence point
2384 warnings. */
2385 static int
2386 warning_candidate_p (tree x)
2388 /* !VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2389 (lvalue_p) crash on TRY/CATCH. */
2390 return !(DECL_P (x) && DECL_ARTIFICIAL (x))
2391 && TREE_TYPE (x) && !VOID_TYPE_P (TREE_TYPE (x)) && lvalue_p (x);
2394 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2395 static bool
2396 candidate_equal_p (const_tree x, const_tree y)
2398 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2401 /* Walk the tree X, and record accesses to variables. If X is written by the
2402 parent tree, WRITER is the parent.
2403 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2404 expression or its only operand forces a sequence point, then everything up
2405 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2406 in PNO_SP.
2407 Once we return, we will have emitted warnings if any subexpression before
2408 such a sequence point could be undefined. On a higher level, however, the
2409 sequence point may not be relevant, and we'll merge the two lists.
2411 Example: (b++, a) + b;
2412 The call that processes the COMPOUND_EXPR will store the increment of B
2413 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2414 processes the PLUS_EXPR will need to merge the two lists so that
2415 eventually, all accesses end up on the same list (and we'll warn about the
2416 unordered subexpressions b++ and b.
2418 A note on merging. If we modify the former example so that our expression
2419 becomes
2420 (b++, b) + a
2421 care must be taken not simply to add all three expressions into the final
2422 PNO_SP list. The function merge_tlist takes care of that by merging the
2423 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2424 way, so that no more than one access to B is recorded. */
2426 static void
2427 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2428 tree writer)
2430 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2431 enum tree_code code;
2432 enum tree_code_class cl;
2434 /* X may be NULL if it is the operand of an empty statement expression
2435 ({ }). */
2436 if (x == NULL)
2437 return;
2439 restart:
2440 code = TREE_CODE (x);
2441 cl = TREE_CODE_CLASS (code);
2443 if (warning_candidate_p (x))
2444 *pno_sp = new_tlist (*pno_sp, x, writer);
2446 switch (code)
2448 case CONSTRUCTOR:
2449 return;
2451 case COMPOUND_EXPR:
2452 case TRUTH_ANDIF_EXPR:
2453 case TRUTH_ORIF_EXPR:
2454 tmp_before = tmp_nosp = tmp_list3 = 0;
2455 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2456 warn_for_collisions (tmp_nosp);
2457 merge_tlist (pbefore_sp, tmp_before, 0);
2458 merge_tlist (pbefore_sp, tmp_nosp, 0);
2459 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2460 merge_tlist (pbefore_sp, tmp_list3, 0);
2461 return;
2463 case COND_EXPR:
2464 tmp_before = tmp_list2 = 0;
2465 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2466 warn_for_collisions (tmp_list2);
2467 merge_tlist (pbefore_sp, tmp_before, 0);
2468 merge_tlist (pbefore_sp, tmp_list2, 1);
2470 tmp_list3 = tmp_nosp = 0;
2471 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2472 warn_for_collisions (tmp_nosp);
2473 merge_tlist (pbefore_sp, tmp_list3, 0);
2475 tmp_list3 = tmp_list2 = 0;
2476 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2477 warn_for_collisions (tmp_list2);
2478 merge_tlist (pbefore_sp, tmp_list3, 0);
2479 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2480 two first, to avoid warning for (a ? b++ : b++). */
2481 merge_tlist (&tmp_nosp, tmp_list2, 0);
2482 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2483 return;
2485 case PREDECREMENT_EXPR:
2486 case PREINCREMENT_EXPR:
2487 case POSTDECREMENT_EXPR:
2488 case POSTINCREMENT_EXPR:
2489 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2490 return;
2492 case MODIFY_EXPR:
2493 tmp_before = tmp_nosp = tmp_list3 = 0;
2494 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2495 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2496 /* Expressions inside the LHS are not ordered wrt. the sequence points
2497 in the RHS. Example:
2498 *a = (a++, 2)
2499 Despite the fact that the modification of "a" is in the before_sp
2500 list (tmp_before), it conflicts with the use of "a" in the LHS.
2501 We can handle this by adding the contents of tmp_list3
2502 to those of tmp_before, and redoing the collision warnings for that
2503 list. */
2504 add_tlist (&tmp_before, tmp_list3, x, 1);
2505 warn_for_collisions (tmp_before);
2506 /* Exclude the LHS itself here; we first have to merge it into the
2507 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2508 didn't exclude the LHS, we'd get it twice, once as a read and once
2509 as a write. */
2510 add_tlist (pno_sp, tmp_list3, x, 0);
2511 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2513 merge_tlist (pbefore_sp, tmp_before, 0);
2514 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2515 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2516 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2517 return;
2519 case CALL_EXPR:
2520 /* We need to warn about conflicts among arguments and conflicts between
2521 args and the function address. Side effects of the function address,
2522 however, are not ordered by the sequence point of the call. */
2524 call_expr_arg_iterator iter;
2525 tree arg;
2526 tmp_before = tmp_nosp = 0;
2527 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2528 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2530 tmp_list2 = tmp_list3 = 0;
2531 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2532 merge_tlist (&tmp_list3, tmp_list2, 0);
2533 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2535 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2536 warn_for_collisions (tmp_before);
2537 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2538 return;
2541 case TREE_LIST:
2542 /* Scan all the list, e.g. indices of multi dimensional array. */
2543 while (x)
2545 tmp_before = tmp_nosp = 0;
2546 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2547 merge_tlist (&tmp_nosp, tmp_before, 0);
2548 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2549 x = TREE_CHAIN (x);
2551 return;
2553 case SAVE_EXPR:
2555 struct tlist_cache *t;
2556 for (t = save_expr_cache; t; t = t->next)
2557 if (candidate_equal_p (t->expr, x))
2558 break;
2560 if (!t)
2562 t = XOBNEW (&tlist_obstack, struct tlist_cache);
2563 t->next = save_expr_cache;
2564 t->expr = x;
2565 save_expr_cache = t;
2567 tmp_before = tmp_nosp = 0;
2568 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2569 warn_for_collisions (tmp_nosp);
2571 tmp_list3 = 0;
2572 while (tmp_nosp)
2574 struct tlist *t = tmp_nosp;
2575 tmp_nosp = t->next;
2576 merge_tlist (&tmp_list3, t, 0);
2578 t->cache_before_sp = tmp_before;
2579 t->cache_after_sp = tmp_list3;
2581 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2582 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2583 return;
2586 case ADDR_EXPR:
2587 x = TREE_OPERAND (x, 0);
2588 if (DECL_P (x))
2589 return;
2590 writer = 0;
2591 goto restart;
2593 default:
2594 /* For other expressions, simply recurse on their operands.
2595 Manual tail recursion for unary expressions.
2596 Other non-expressions need not be processed. */
2597 if (cl == tcc_unary)
2599 x = TREE_OPERAND (x, 0);
2600 writer = 0;
2601 goto restart;
2603 else if (IS_EXPR_CODE_CLASS (cl))
2605 int lp;
2606 int max = TREE_OPERAND_LENGTH (x);
2607 for (lp = 0; lp < max; lp++)
2609 tmp_before = tmp_nosp = 0;
2610 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2611 merge_tlist (&tmp_nosp, tmp_before, 0);
2612 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2615 return;
2619 /* Try to warn for undefined behavior in EXPR due to missing sequence
2620 points. */
2622 void
2623 verify_sequence_points (tree expr)
2625 struct tlist *before_sp = 0, *after_sp = 0;
2627 warned_ids = 0;
2628 save_expr_cache = 0;
2629 if (tlist_firstobj == 0)
2631 gcc_obstack_init (&tlist_obstack);
2632 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2635 verify_tree (expr, &before_sp, &after_sp, 0);
2636 warn_for_collisions (after_sp);
2637 obstack_free (&tlist_obstack, tlist_firstobj);
2640 /* Validate the expression after `case' and apply default promotions. */
2642 static tree
2643 check_case_value (tree value)
2645 if (value == NULL_TREE)
2646 return value;
2648 /* ??? Can we ever get nops here for a valid case value? We
2649 shouldn't for C. */
2650 STRIP_TYPE_NOPS (value);
2651 /* In C++, the following is allowed:
2653 const int i = 3;
2654 switch (...) { case i: ... }
2656 So, we try to reduce the VALUE to a constant that way. */
2657 if (c_dialect_cxx ())
2659 value = decl_constant_value (value);
2660 STRIP_TYPE_NOPS (value);
2661 value = fold (value);
2664 if (TREE_CODE (value) == INTEGER_CST)
2665 /* Promote char or short to int. */
2666 value = perform_integral_promotions (value);
2667 else if (value != error_mark_node)
2669 error ("case label does not reduce to an integer constant");
2670 value = error_mark_node;
2673 constant_expression_warning (value);
2675 return value;
2678 /* See if the case values LOW and HIGH are in the range of the original
2679 type (i.e. before the default conversion to int) of the switch testing
2680 expression.
2681 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2682 the type before promoting it. CASE_LOW_P is a pointer to the lower
2683 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2684 if the case is not a case range.
2685 The caller has to make sure that we are not called with NULL for
2686 CASE_LOW_P (i.e. the default case).
2687 Returns true if the case label is in range of ORIG_TYPE (saturated or
2688 untouched) or false if the label is out of range. */
2690 static bool
2691 check_case_bounds (tree type, tree orig_type,
2692 tree *case_low_p, tree *case_high_p)
2694 tree min_value, max_value;
2695 tree case_low = *case_low_p;
2696 tree case_high = case_high_p ? *case_high_p : case_low;
2698 /* If there was a problem with the original type, do nothing. */
2699 if (orig_type == error_mark_node)
2700 return true;
2702 min_value = TYPE_MIN_VALUE (orig_type);
2703 max_value = TYPE_MAX_VALUE (orig_type);
2705 /* Case label is less than minimum for type. */
2706 if (tree_int_cst_compare (case_low, min_value) < 0
2707 && tree_int_cst_compare (case_high, min_value) < 0)
2709 warning (0, "case label value is less than minimum value for type");
2710 return false;
2713 /* Case value is greater than maximum for type. */
2714 if (tree_int_cst_compare (case_low, max_value) > 0
2715 && tree_int_cst_compare (case_high, max_value) > 0)
2717 warning (0, "case label value exceeds maximum value for type");
2718 return false;
2721 /* Saturate lower case label value to minimum. */
2722 if (tree_int_cst_compare (case_high, min_value) >= 0
2723 && tree_int_cst_compare (case_low, min_value) < 0)
2725 warning (0, "lower value in case label range"
2726 " less than minimum value for type");
2727 case_low = min_value;
2730 /* Saturate upper case label value to maximum. */
2731 if (tree_int_cst_compare (case_low, max_value) <= 0
2732 && tree_int_cst_compare (case_high, max_value) > 0)
2734 warning (0, "upper value in case label range"
2735 " exceeds maximum value for type");
2736 case_high = max_value;
2739 if (*case_low_p != case_low)
2740 *case_low_p = convert (type, case_low);
2741 if (case_high_p && *case_high_p != case_high)
2742 *case_high_p = convert (type, case_high);
2744 return true;
2747 /* Return an integer type with BITS bits of precision,
2748 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2750 tree
2751 c_common_type_for_size (unsigned int bits, int unsignedp)
2753 if (bits == TYPE_PRECISION (integer_type_node))
2754 return unsignedp ? unsigned_type_node : integer_type_node;
2756 if (bits == TYPE_PRECISION (signed_char_type_node))
2757 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2759 if (bits == TYPE_PRECISION (short_integer_type_node))
2760 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2762 if (bits == TYPE_PRECISION (long_integer_type_node))
2763 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2765 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2766 return (unsignedp ? long_long_unsigned_type_node
2767 : long_long_integer_type_node);
2769 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2770 return (unsignedp ? widest_unsigned_literal_type_node
2771 : widest_integer_literal_type_node);
2773 if (bits <= TYPE_PRECISION (intQI_type_node))
2774 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2776 if (bits <= TYPE_PRECISION (intHI_type_node))
2777 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2779 if (bits <= TYPE_PRECISION (intSI_type_node))
2780 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2782 if (bits <= TYPE_PRECISION (intDI_type_node))
2783 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2785 return 0;
2788 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2789 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2790 and saturating if SATP is nonzero, otherwise not saturating. */
2792 tree
2793 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2794 int unsignedp, int satp)
2796 enum machine_mode mode;
2797 if (ibit == 0)
2798 mode = unsignedp ? UQQmode : QQmode;
2799 else
2800 mode = unsignedp ? UHAmode : HAmode;
2802 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2803 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2804 break;
2806 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2808 sorry ("GCC cannot support operators with integer types and "
2809 "fixed-point types that have too many integral and "
2810 "fractional bits together");
2811 return 0;
2814 return c_common_type_for_mode (mode, satp);
2817 /* Used for communication between c_common_type_for_mode and
2818 c_register_builtin_type. */
2819 static GTY(()) tree registered_builtin_types;
2821 /* Return a data type that has machine mode MODE.
2822 If the mode is an integer,
2823 then UNSIGNEDP selects between signed and unsigned types.
2824 If the mode is a fixed-point mode,
2825 then UNSIGNEDP selects between saturating and nonsaturating types. */
2827 tree
2828 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
2830 tree t;
2832 if (mode == TYPE_MODE (integer_type_node))
2833 return unsignedp ? unsigned_type_node : integer_type_node;
2835 if (mode == TYPE_MODE (signed_char_type_node))
2836 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2838 if (mode == TYPE_MODE (short_integer_type_node))
2839 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2841 if (mode == TYPE_MODE (long_integer_type_node))
2842 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2844 if (mode == TYPE_MODE (long_long_integer_type_node))
2845 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2847 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2848 return unsignedp ? widest_unsigned_literal_type_node
2849 : widest_integer_literal_type_node;
2851 if (mode == QImode)
2852 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2854 if (mode == HImode)
2855 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2857 if (mode == SImode)
2858 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2860 if (mode == DImode)
2861 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2863 #if HOST_BITS_PER_WIDE_INT >= 64
2864 if (mode == TYPE_MODE (intTI_type_node))
2865 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2866 #endif
2868 if (mode == TYPE_MODE (float_type_node))
2869 return float_type_node;
2871 if (mode == TYPE_MODE (double_type_node))
2872 return double_type_node;
2874 if (mode == TYPE_MODE (long_double_type_node))
2875 return long_double_type_node;
2877 if (mode == TYPE_MODE (void_type_node))
2878 return void_type_node;
2880 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2881 return (unsignedp
2882 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2883 : make_signed_type (GET_MODE_PRECISION (mode)));
2885 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2886 return (unsignedp
2887 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2888 : make_signed_type (GET_MODE_PRECISION (mode)));
2890 if (COMPLEX_MODE_P (mode))
2892 enum machine_mode inner_mode;
2893 tree inner_type;
2895 if (mode == TYPE_MODE (complex_float_type_node))
2896 return complex_float_type_node;
2897 if (mode == TYPE_MODE (complex_double_type_node))
2898 return complex_double_type_node;
2899 if (mode == TYPE_MODE (complex_long_double_type_node))
2900 return complex_long_double_type_node;
2902 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2903 return complex_integer_type_node;
2905 inner_mode = GET_MODE_INNER (mode);
2906 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2907 if (inner_type != NULL_TREE)
2908 return build_complex_type (inner_type);
2910 else if (VECTOR_MODE_P (mode))
2912 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2913 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2914 if (inner_type != NULL_TREE)
2915 return build_vector_type_for_mode (inner_type, mode);
2918 if (mode == TYPE_MODE (dfloat32_type_node))
2919 return dfloat32_type_node;
2920 if (mode == TYPE_MODE (dfloat64_type_node))
2921 return dfloat64_type_node;
2922 if (mode == TYPE_MODE (dfloat128_type_node))
2923 return dfloat128_type_node;
2925 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2927 if (mode == TYPE_MODE (short_fract_type_node))
2928 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2929 if (mode == TYPE_MODE (fract_type_node))
2930 return unsignedp ? sat_fract_type_node : fract_type_node;
2931 if (mode == TYPE_MODE (long_fract_type_node))
2932 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2933 if (mode == TYPE_MODE (long_long_fract_type_node))
2934 return unsignedp ? sat_long_long_fract_type_node
2935 : long_long_fract_type_node;
2937 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2938 return unsignedp ? sat_unsigned_short_fract_type_node
2939 : unsigned_short_fract_type_node;
2940 if (mode == TYPE_MODE (unsigned_fract_type_node))
2941 return unsignedp ? sat_unsigned_fract_type_node
2942 : unsigned_fract_type_node;
2943 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2944 return unsignedp ? sat_unsigned_long_fract_type_node
2945 : unsigned_long_fract_type_node;
2946 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2947 return unsignedp ? sat_unsigned_long_long_fract_type_node
2948 : unsigned_long_long_fract_type_node;
2950 if (mode == TYPE_MODE (short_accum_type_node))
2951 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2952 if (mode == TYPE_MODE (accum_type_node))
2953 return unsignedp ? sat_accum_type_node : accum_type_node;
2954 if (mode == TYPE_MODE (long_accum_type_node))
2955 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2956 if (mode == TYPE_MODE (long_long_accum_type_node))
2957 return unsignedp ? sat_long_long_accum_type_node
2958 : long_long_accum_type_node;
2960 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2961 return unsignedp ? sat_unsigned_short_accum_type_node
2962 : unsigned_short_accum_type_node;
2963 if (mode == TYPE_MODE (unsigned_accum_type_node))
2964 return unsignedp ? sat_unsigned_accum_type_node
2965 : unsigned_accum_type_node;
2966 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2967 return unsignedp ? sat_unsigned_long_accum_type_node
2968 : unsigned_long_accum_type_node;
2969 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2970 return unsignedp ? sat_unsigned_long_long_accum_type_node
2971 : unsigned_long_long_accum_type_node;
2973 if (mode == QQmode)
2974 return unsignedp ? sat_qq_type_node : qq_type_node;
2975 if (mode == HQmode)
2976 return unsignedp ? sat_hq_type_node : hq_type_node;
2977 if (mode == SQmode)
2978 return unsignedp ? sat_sq_type_node : sq_type_node;
2979 if (mode == DQmode)
2980 return unsignedp ? sat_dq_type_node : dq_type_node;
2981 if (mode == TQmode)
2982 return unsignedp ? sat_tq_type_node : tq_type_node;
2984 if (mode == UQQmode)
2985 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2986 if (mode == UHQmode)
2987 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2988 if (mode == USQmode)
2989 return unsignedp ? sat_usq_type_node : usq_type_node;
2990 if (mode == UDQmode)
2991 return unsignedp ? sat_udq_type_node : udq_type_node;
2992 if (mode == UTQmode)
2993 return unsignedp ? sat_utq_type_node : utq_type_node;
2995 if (mode == HAmode)
2996 return unsignedp ? sat_ha_type_node : ha_type_node;
2997 if (mode == SAmode)
2998 return unsignedp ? sat_sa_type_node : sa_type_node;
2999 if (mode == DAmode)
3000 return unsignedp ? sat_da_type_node : da_type_node;
3001 if (mode == TAmode)
3002 return unsignedp ? sat_ta_type_node : ta_type_node;
3004 if (mode == UHAmode)
3005 return unsignedp ? sat_uha_type_node : uha_type_node;
3006 if (mode == USAmode)
3007 return unsignedp ? sat_usa_type_node : usa_type_node;
3008 if (mode == UDAmode)
3009 return unsignedp ? sat_uda_type_node : uda_type_node;
3010 if (mode == UTAmode)
3011 return unsignedp ? sat_uta_type_node : uta_type_node;
3014 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3015 if (TYPE_MODE (TREE_VALUE (t)) == mode)
3016 return TREE_VALUE (t);
3018 return 0;
3021 tree
3022 c_common_unsigned_type (tree type)
3024 return c_common_signed_or_unsigned_type (1, type);
3027 /* Return a signed type the same as TYPE in other respects. */
3029 tree
3030 c_common_signed_type (tree type)
3032 return c_common_signed_or_unsigned_type (0, type);
3035 /* Return a type the same as TYPE except unsigned or
3036 signed according to UNSIGNEDP. */
3038 tree
3039 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3041 tree type1;
3043 /* This block of code emulates the behavior of the old
3044 c_common_unsigned_type. In particular, it returns
3045 long_unsigned_type_node if passed a long, even when a int would
3046 have the same size. This is necessary for warnings to work
3047 correctly in archs where sizeof(int) == sizeof(long) */
3049 type1 = TYPE_MAIN_VARIANT (type);
3050 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3051 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3052 if (type1 == integer_type_node || type1 == unsigned_type_node)
3053 return unsignedp ? unsigned_type_node : integer_type_node;
3054 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3055 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3056 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3057 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3058 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3059 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3060 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3061 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3062 #if HOST_BITS_PER_WIDE_INT >= 64
3063 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3064 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3065 #endif
3066 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3067 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3068 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3069 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3070 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3071 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3072 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3073 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3075 #define C_COMMON_FIXED_TYPES(NAME) \
3076 if (type1 == short_ ## NAME ## _type_node \
3077 || type1 == unsigned_short_ ## NAME ## _type_node) \
3078 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3079 : short_ ## NAME ## _type_node; \
3080 if (type1 == NAME ## _type_node \
3081 || type1 == unsigned_ ## NAME ## _type_node) \
3082 return unsignedp ? unsigned_ ## NAME ## _type_node \
3083 : NAME ## _type_node; \
3084 if (type1 == long_ ## NAME ## _type_node \
3085 || type1 == unsigned_long_ ## NAME ## _type_node) \
3086 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3087 : long_ ## NAME ## _type_node; \
3088 if (type1 == long_long_ ## NAME ## _type_node \
3089 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3090 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3091 : long_long_ ## NAME ## _type_node;
3093 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3094 if (type1 == NAME ## _type_node \
3095 || type1 == u ## NAME ## _type_node) \
3096 return unsignedp ? u ## NAME ## _type_node \
3097 : NAME ## _type_node;
3099 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3100 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3101 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3102 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3103 : sat_ ## short_ ## NAME ## _type_node; \
3104 if (type1 == sat_ ## NAME ## _type_node \
3105 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3106 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3107 : sat_ ## NAME ## _type_node; \
3108 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3109 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3110 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3111 : sat_ ## long_ ## NAME ## _type_node; \
3112 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3113 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3114 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3115 : sat_ ## long_long_ ## NAME ## _type_node;
3117 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3118 if (type1 == sat_ ## NAME ## _type_node \
3119 || type1 == sat_ ## u ## NAME ## _type_node) \
3120 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3121 : sat_ ## NAME ## _type_node;
3123 C_COMMON_FIXED_TYPES (fract);
3124 C_COMMON_FIXED_TYPES_SAT (fract);
3125 C_COMMON_FIXED_TYPES (accum);
3126 C_COMMON_FIXED_TYPES_SAT (accum);
3128 C_COMMON_FIXED_MODE_TYPES (qq);
3129 C_COMMON_FIXED_MODE_TYPES (hq);
3130 C_COMMON_FIXED_MODE_TYPES (sq);
3131 C_COMMON_FIXED_MODE_TYPES (dq);
3132 C_COMMON_FIXED_MODE_TYPES (tq);
3133 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3134 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3135 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3136 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3137 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3138 C_COMMON_FIXED_MODE_TYPES (ha);
3139 C_COMMON_FIXED_MODE_TYPES (sa);
3140 C_COMMON_FIXED_MODE_TYPES (da);
3141 C_COMMON_FIXED_MODE_TYPES (ta);
3142 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3143 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3144 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3145 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3147 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3148 the precision; they have precision set to match their range, but
3149 may use a wider mode to match an ABI. If we change modes, we may
3150 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3151 the precision as well, so as to yield correct results for
3152 bit-field types. C++ does not have these separate bit-field
3153 types, and producing a signed or unsigned variant of an
3154 ENUMERAL_TYPE may cause other problems as well. */
3156 if (!INTEGRAL_TYPE_P (type)
3157 || TYPE_UNSIGNED (type) == unsignedp)
3158 return type;
3160 #define TYPE_OK(node) \
3161 (TYPE_MODE (type) == TYPE_MODE (node) \
3162 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3163 if (TYPE_OK (signed_char_type_node))
3164 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3165 if (TYPE_OK (integer_type_node))
3166 return unsignedp ? unsigned_type_node : integer_type_node;
3167 if (TYPE_OK (short_integer_type_node))
3168 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3169 if (TYPE_OK (long_integer_type_node))
3170 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3171 if (TYPE_OK (long_long_integer_type_node))
3172 return (unsignedp ? long_long_unsigned_type_node
3173 : long_long_integer_type_node);
3174 if (TYPE_OK (widest_integer_literal_type_node))
3175 return (unsignedp ? widest_unsigned_literal_type_node
3176 : widest_integer_literal_type_node);
3178 #if HOST_BITS_PER_WIDE_INT >= 64
3179 if (TYPE_OK (intTI_type_node))
3180 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3181 #endif
3182 if (TYPE_OK (intDI_type_node))
3183 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3184 if (TYPE_OK (intSI_type_node))
3185 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3186 if (TYPE_OK (intHI_type_node))
3187 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3188 if (TYPE_OK (intQI_type_node))
3189 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3190 #undef TYPE_OK
3192 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3195 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3197 tree
3198 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3200 /* Extended integer types of the same width as a standard type have
3201 lesser rank, so those of the same width as int promote to int or
3202 unsigned int and are valid for printf formats expecting int or
3203 unsigned int. To avoid such special cases, avoid creating
3204 extended integer types for bit-fields if a standard integer type
3205 is available. */
3206 if (width == TYPE_PRECISION (integer_type_node))
3207 return unsignedp ? unsigned_type_node : integer_type_node;
3208 if (width == TYPE_PRECISION (signed_char_type_node))
3209 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3210 if (width == TYPE_PRECISION (short_integer_type_node))
3211 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3212 if (width == TYPE_PRECISION (long_integer_type_node))
3213 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3214 if (width == TYPE_PRECISION (long_long_integer_type_node))
3215 return (unsignedp ? long_long_unsigned_type_node
3216 : long_long_integer_type_node);
3217 return build_nonstandard_integer_type (width, unsignedp);
3220 /* The C version of the register_builtin_type langhook. */
3222 void
3223 c_register_builtin_type (tree type, const char* name)
3225 tree decl;
3227 decl = build_decl (TYPE_DECL, get_identifier (name), type);
3228 DECL_ARTIFICIAL (decl) = 1;
3229 if (!TYPE_NAME (type))
3230 TYPE_NAME (type) = decl;
3231 pushdecl (decl);
3233 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3236 /* Print an error message for invalid operands to arith operation
3237 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3238 LOCATION is the location of the message. */
3240 void
3241 binary_op_error (location_t location, enum tree_code code,
3242 tree type0, tree type1)
3244 const char *opname;
3246 switch (code)
3248 case PLUS_EXPR:
3249 opname = "+"; break;
3250 case MINUS_EXPR:
3251 opname = "-"; break;
3252 case MULT_EXPR:
3253 opname = "*"; break;
3254 case MAX_EXPR:
3255 opname = "max"; break;
3256 case MIN_EXPR:
3257 opname = "min"; break;
3258 case EQ_EXPR:
3259 opname = "=="; break;
3260 case NE_EXPR:
3261 opname = "!="; break;
3262 case LE_EXPR:
3263 opname = "<="; break;
3264 case GE_EXPR:
3265 opname = ">="; break;
3266 case LT_EXPR:
3267 opname = "<"; break;
3268 case GT_EXPR:
3269 opname = ">"; break;
3270 case LSHIFT_EXPR:
3271 opname = "<<"; break;
3272 case RSHIFT_EXPR:
3273 opname = ">>"; break;
3274 case TRUNC_MOD_EXPR:
3275 case FLOOR_MOD_EXPR:
3276 opname = "%"; break;
3277 case TRUNC_DIV_EXPR:
3278 case FLOOR_DIV_EXPR:
3279 opname = "/"; break;
3280 case BIT_AND_EXPR:
3281 opname = "&"; break;
3282 case BIT_IOR_EXPR:
3283 opname = "|"; break;
3284 case TRUTH_ANDIF_EXPR:
3285 opname = "&&"; break;
3286 case TRUTH_ORIF_EXPR:
3287 opname = "||"; break;
3288 case BIT_XOR_EXPR:
3289 opname = "^"; break;
3290 default:
3291 gcc_unreachable ();
3293 error_at (location,
3294 "invalid operands to binary %s (have %qT and %qT)", opname,
3295 type0, type1);
3298 /* Subroutine of build_binary_op, used for comparison operations.
3299 See if the operands have both been converted from subword integer types
3300 and, if so, perhaps change them both back to their original type.
3301 This function is also responsible for converting the two operands
3302 to the proper common type for comparison.
3304 The arguments of this function are all pointers to local variables
3305 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3306 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3308 If this function returns nonzero, it means that the comparison has
3309 a constant value. What this function returns is an expression for
3310 that value. */
3312 tree
3313 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3314 enum tree_code *rescode_ptr)
3316 tree type;
3317 tree op0 = *op0_ptr;
3318 tree op1 = *op1_ptr;
3319 int unsignedp0, unsignedp1;
3320 int real1, real2;
3321 tree primop0, primop1;
3322 enum tree_code code = *rescode_ptr;
3324 /* Throw away any conversions to wider types
3325 already present in the operands. */
3327 primop0 = get_narrower (op0, &unsignedp0);
3328 primop1 = get_narrower (op1, &unsignedp1);
3330 /* Handle the case that OP0 does not *contain* a conversion
3331 but it *requires* conversion to FINAL_TYPE. */
3333 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3334 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3335 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3336 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3338 /* If one of the operands must be floated, we cannot optimize. */
3339 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3340 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3342 /* If first arg is constant, swap the args (changing operation
3343 so value is preserved), for canonicalization. Don't do this if
3344 the second arg is 0. */
3346 if (TREE_CONSTANT (primop0)
3347 && !integer_zerop (primop1) && !real_zerop (primop1)
3348 && !fixed_zerop (primop1))
3350 tree tem = primop0;
3351 int temi = unsignedp0;
3352 primop0 = primop1;
3353 primop1 = tem;
3354 tem = op0;
3355 op0 = op1;
3356 op1 = tem;
3357 *op0_ptr = op0;
3358 *op1_ptr = op1;
3359 unsignedp0 = unsignedp1;
3360 unsignedp1 = temi;
3361 temi = real1;
3362 real1 = real2;
3363 real2 = temi;
3365 switch (code)
3367 case LT_EXPR:
3368 code = GT_EXPR;
3369 break;
3370 case GT_EXPR:
3371 code = LT_EXPR;
3372 break;
3373 case LE_EXPR:
3374 code = GE_EXPR;
3375 break;
3376 case GE_EXPR:
3377 code = LE_EXPR;
3378 break;
3379 default:
3380 break;
3382 *rescode_ptr = code;
3385 /* If comparing an integer against a constant more bits wide,
3386 maybe we can deduce a value of 1 or 0 independent of the data.
3387 Or else truncate the constant now
3388 rather than extend the variable at run time.
3390 This is only interesting if the constant is the wider arg.
3391 Also, it is not safe if the constant is unsigned and the
3392 variable arg is signed, since in this case the variable
3393 would be sign-extended and then regarded as unsigned.
3394 Our technique fails in this case because the lowest/highest
3395 possible unsigned results don't follow naturally from the
3396 lowest/highest possible values of the variable operand.
3397 For just EQ_EXPR and NE_EXPR there is another technique that
3398 could be used: see if the constant can be faithfully represented
3399 in the other operand's type, by truncating it and reextending it
3400 and see if that preserves the constant's value. */
3402 if (!real1 && !real2
3403 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3404 && TREE_CODE (primop1) == INTEGER_CST
3405 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3407 int min_gt, max_gt, min_lt, max_lt;
3408 tree maxval, minval;
3409 /* 1 if comparison is nominally unsigned. */
3410 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3411 tree val;
3413 type = c_common_signed_or_unsigned_type (unsignedp0,
3414 TREE_TYPE (primop0));
3416 maxval = TYPE_MAX_VALUE (type);
3417 minval = TYPE_MIN_VALUE (type);
3419 if (unsignedp && !unsignedp0)
3420 *restype_ptr = c_common_signed_type (*restype_ptr);
3422 if (TREE_TYPE (primop1) != *restype_ptr)
3424 /* Convert primop1 to target type, but do not introduce
3425 additional overflow. We know primop1 is an int_cst. */
3426 primop1 = force_fit_type_double (*restype_ptr,
3427 TREE_INT_CST_LOW (primop1),
3428 TREE_INT_CST_HIGH (primop1), 0,
3429 TREE_OVERFLOW (primop1));
3431 if (type != *restype_ptr)
3433 minval = convert (*restype_ptr, minval);
3434 maxval = convert (*restype_ptr, maxval);
3437 if (unsignedp && unsignedp0)
3439 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3440 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3441 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3442 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3444 else
3446 min_gt = INT_CST_LT (primop1, minval);
3447 max_gt = INT_CST_LT (primop1, maxval);
3448 min_lt = INT_CST_LT (minval, primop1);
3449 max_lt = INT_CST_LT (maxval, primop1);
3452 val = 0;
3453 /* This used to be a switch, but Genix compiler can't handle that. */
3454 if (code == NE_EXPR)
3456 if (max_lt || min_gt)
3457 val = truthvalue_true_node;
3459 else if (code == EQ_EXPR)
3461 if (max_lt || min_gt)
3462 val = truthvalue_false_node;
3464 else if (code == LT_EXPR)
3466 if (max_lt)
3467 val = truthvalue_true_node;
3468 if (!min_lt)
3469 val = truthvalue_false_node;
3471 else if (code == GT_EXPR)
3473 if (min_gt)
3474 val = truthvalue_true_node;
3475 if (!max_gt)
3476 val = truthvalue_false_node;
3478 else if (code == LE_EXPR)
3480 if (!max_gt)
3481 val = truthvalue_true_node;
3482 if (min_gt)
3483 val = truthvalue_false_node;
3485 else if (code == GE_EXPR)
3487 if (!min_lt)
3488 val = truthvalue_true_node;
3489 if (max_lt)
3490 val = truthvalue_false_node;
3493 /* If primop0 was sign-extended and unsigned comparison specd,
3494 we did a signed comparison above using the signed type bounds.
3495 But the comparison we output must be unsigned.
3497 Also, for inequalities, VAL is no good; but if the signed
3498 comparison had *any* fixed result, it follows that the
3499 unsigned comparison just tests the sign in reverse
3500 (positive values are LE, negative ones GE).
3501 So we can generate an unsigned comparison
3502 against an extreme value of the signed type. */
3504 if (unsignedp && !unsignedp0)
3506 if (val != 0)
3507 switch (code)
3509 case LT_EXPR:
3510 case GE_EXPR:
3511 primop1 = TYPE_MIN_VALUE (type);
3512 val = 0;
3513 break;
3515 case LE_EXPR:
3516 case GT_EXPR:
3517 primop1 = TYPE_MAX_VALUE (type);
3518 val = 0;
3519 break;
3521 default:
3522 break;
3524 type = c_common_unsigned_type (type);
3527 if (TREE_CODE (primop0) != INTEGER_CST)
3529 if (val == truthvalue_false_node)
3530 warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type");
3531 if (val == truthvalue_true_node)
3532 warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type");
3535 if (val != 0)
3537 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3538 if (TREE_SIDE_EFFECTS (primop0))
3539 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3540 return val;
3543 /* Value is not predetermined, but do the comparison
3544 in the type of the operand that is not constant.
3545 TYPE is already properly set. */
3548 /* If either arg is decimal float and the other is float, find the
3549 proper common type to use for comparison. */
3550 else if (real1 && real2
3551 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3552 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3553 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3555 else if (real1 && real2
3556 && (TYPE_PRECISION (TREE_TYPE (primop0))
3557 == TYPE_PRECISION (TREE_TYPE (primop1))))
3558 type = TREE_TYPE (primop0);
3560 /* If args' natural types are both narrower than nominal type
3561 and both extend in the same manner, compare them
3562 in the type of the wider arg.
3563 Otherwise must actually extend both to the nominal
3564 common type lest different ways of extending
3565 alter the result.
3566 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3568 else if (unsignedp0 == unsignedp1 && real1 == real2
3569 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3570 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3572 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3573 type = c_common_signed_or_unsigned_type (unsignedp0
3574 || TYPE_UNSIGNED (*restype_ptr),
3575 type);
3576 /* Make sure shorter operand is extended the right way
3577 to match the longer operand. */
3578 primop0
3579 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3580 TREE_TYPE (primop0)),
3581 primop0);
3582 primop1
3583 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3584 TREE_TYPE (primop1)),
3585 primop1);
3587 else
3589 /* Here we must do the comparison on the nominal type
3590 using the args exactly as we received them. */
3591 type = *restype_ptr;
3592 primop0 = op0;
3593 primop1 = op1;
3595 if (!real1 && !real2 && integer_zerop (primop1)
3596 && TYPE_UNSIGNED (*restype_ptr))
3598 tree value = 0;
3599 switch (code)
3601 case GE_EXPR:
3602 /* All unsigned values are >= 0, so we warn. However,
3603 if OP0 is a constant that is >= 0, the signedness of
3604 the comparison isn't an issue, so suppress the
3605 warning. */
3606 if (warn_type_limits && !in_system_header
3607 && !(TREE_CODE (primop0) == INTEGER_CST
3608 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3609 primop0))))
3610 warning (OPT_Wtype_limits,
3611 "comparison of unsigned expression >= 0 is always true");
3612 value = truthvalue_true_node;
3613 break;
3615 case LT_EXPR:
3616 if (warn_type_limits && !in_system_header
3617 && !(TREE_CODE (primop0) == INTEGER_CST
3618 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3619 primop0))))
3620 warning (OPT_Wtype_limits,
3621 "comparison of unsigned expression < 0 is always false");
3622 value = truthvalue_false_node;
3623 break;
3625 default:
3626 break;
3629 if (value != 0)
3631 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3632 if (TREE_SIDE_EFFECTS (primop0))
3633 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3634 primop0, value);
3635 return value;
3640 *op0_ptr = convert (type, primop0);
3641 *op1_ptr = convert (type, primop1);
3643 *restype_ptr = truthvalue_type_node;
3645 return 0;
3648 /* Return a tree for the sum or difference (RESULTCODE says which)
3649 of pointer PTROP and integer INTOP. */
3651 tree
3652 pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
3654 tree size_exp, ret;
3656 /* The result is a pointer of the same type that is being added. */
3657 tree result_type = TREE_TYPE (ptrop);
3659 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3661 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3662 "pointer of type %<void *%> used in arithmetic");
3663 size_exp = integer_one_node;
3665 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3667 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3668 "pointer to a function used in arithmetic");
3669 size_exp = integer_one_node;
3671 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3673 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3674 "pointer to member function used in arithmetic");
3675 size_exp = integer_one_node;
3677 else
3678 size_exp = size_in_bytes (TREE_TYPE (result_type));
3680 /* We are manipulating pointer values, so we don't need to warn
3681 about relying on undefined signed overflow. We disable the
3682 warning here because we use integer types so fold won't know that
3683 they are really pointers. */
3684 fold_defer_overflow_warnings ();
3686 /* If what we are about to multiply by the size of the elements
3687 contains a constant term, apply distributive law
3688 and multiply that constant term separately.
3689 This helps produce common subexpressions. */
3690 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3691 && !TREE_CONSTANT (intop)
3692 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3693 && TREE_CONSTANT (size_exp)
3694 /* If the constant comes from pointer subtraction,
3695 skip this optimization--it would cause an error. */
3696 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3697 /* If the constant is unsigned, and smaller than the pointer size,
3698 then we must skip this optimization. This is because it could cause
3699 an overflow error if the constant is negative but INTOP is not. */
3700 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3701 || (TYPE_PRECISION (TREE_TYPE (intop))
3702 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3704 enum tree_code subcode = resultcode;
3705 tree int_type = TREE_TYPE (intop);
3706 if (TREE_CODE (intop) == MINUS_EXPR)
3707 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3708 /* Convert both subexpression types to the type of intop,
3709 because weird cases involving pointer arithmetic
3710 can result in a sum or difference with different type args. */
3711 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3712 subcode, ptrop,
3713 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3714 intop = convert (int_type, TREE_OPERAND (intop, 0));
3717 /* Convert the integer argument to a type the same size as sizetype
3718 so the multiply won't overflow spuriously. */
3719 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3720 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3721 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3722 TYPE_UNSIGNED (sizetype)), intop);
3724 /* Replace the integer argument with a suitable product by the object size.
3725 Do this multiplication as signed, then convert to the appropriate
3726 type for the pointer operation. */
3727 intop = convert (sizetype,
3728 build_binary_op (EXPR_LOCATION (intop),
3729 MULT_EXPR, intop,
3730 convert (TREE_TYPE (intop), size_exp), 1));
3732 /* Create the sum or difference. */
3733 if (resultcode == MINUS_EXPR)
3734 intop = fold_build1 (NEGATE_EXPR, sizetype, intop);
3736 ret = fold_build2 (POINTER_PLUS_EXPR, result_type, ptrop, intop);
3738 fold_undefer_and_ignore_overflow_warnings ();
3740 return ret;
3743 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
3744 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3745 around the SAVE_EXPR if needed so that c_fully_fold does not need
3746 to look inside SAVE_EXPRs. */
3748 tree
3749 c_save_expr (tree expr)
3751 bool maybe_const = true;
3752 if (c_dialect_cxx ())
3753 return save_expr (expr);
3754 expr = c_fully_fold (expr, false, &maybe_const);
3755 expr = save_expr (expr);
3756 if (!maybe_const)
3758 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3759 C_MAYBE_CONST_EXPR_NON_CONST (expr) = 1;
3761 return expr;
3764 /* Return whether EXPR is a declaration whose address can never be
3765 NULL. */
3767 bool
3768 decl_with_nonnull_addr_p (const_tree expr)
3770 return (DECL_P (expr)
3771 && (TREE_CODE (expr) == PARM_DECL
3772 || TREE_CODE (expr) == LABEL_DECL
3773 || !DECL_WEAK (expr)));
3776 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3777 or for an `if' or `while' statement or ?..: exp. It should already
3778 have been validated to be of suitable type; otherwise, a bad
3779 diagnostic may result.
3781 The EXPR is located at LOCATION.
3783 This preparation consists of taking the ordinary
3784 representation of an expression expr and producing a valid tree
3785 boolean expression describing whether expr is nonzero. We could
3786 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3787 but we optimize comparisons, &&, ||, and !.
3789 The resulting type should always be `truthvalue_type_node'. */
3791 tree
3792 c_common_truthvalue_conversion (location_t location, tree expr)
3794 switch (TREE_CODE (expr))
3796 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3797 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3798 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3799 case ORDERED_EXPR: case UNORDERED_EXPR:
3800 if (TREE_TYPE (expr) == truthvalue_type_node)
3801 return expr;
3802 return build2 (TREE_CODE (expr), truthvalue_type_node,
3803 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3805 case TRUTH_ANDIF_EXPR:
3806 case TRUTH_ORIF_EXPR:
3807 case TRUTH_AND_EXPR:
3808 case TRUTH_OR_EXPR:
3809 case TRUTH_XOR_EXPR:
3810 if (TREE_TYPE (expr) == truthvalue_type_node)
3811 return expr;
3812 return build2 (TREE_CODE (expr), truthvalue_type_node,
3813 c_common_truthvalue_conversion (location,
3814 TREE_OPERAND (expr, 0)),
3815 c_common_truthvalue_conversion (location,
3816 TREE_OPERAND (expr, 1)));
3818 case TRUTH_NOT_EXPR:
3819 if (TREE_TYPE (expr) == truthvalue_type_node)
3820 return expr;
3821 return build1 (TREE_CODE (expr), truthvalue_type_node,
3822 c_common_truthvalue_conversion (location,
3823 TREE_OPERAND (expr, 0)));
3825 case ERROR_MARK:
3826 return expr;
3828 case INTEGER_CST:
3829 return integer_zerop (expr) ? truthvalue_false_node
3830 : truthvalue_true_node;
3832 case REAL_CST:
3833 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3834 ? truthvalue_true_node
3835 : truthvalue_false_node;
3837 case FIXED_CST:
3838 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3839 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3840 ? truthvalue_true_node
3841 : truthvalue_false_node;
3843 case FUNCTION_DECL:
3844 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
3845 /* Fall through. */
3847 case ADDR_EXPR:
3849 tree inner = TREE_OPERAND (expr, 0);
3850 if (decl_with_nonnull_addr_p (inner))
3852 /* Common Ada/Pascal programmer's mistake. */
3853 warning_at (location,
3854 OPT_Waddress,
3855 "the address of %qD will always evaluate as %<true%>",
3856 inner);
3857 return truthvalue_true_node;
3860 /* If we still have a decl, it is possible for its address to
3861 be NULL, so we cannot optimize. */
3862 if (DECL_P (inner))
3864 gcc_assert (DECL_WEAK (inner));
3865 break;
3868 if (TREE_SIDE_EFFECTS (inner))
3869 return build2 (COMPOUND_EXPR, truthvalue_type_node,
3870 inner, truthvalue_true_node);
3871 else
3872 return truthvalue_true_node;
3875 case COMPLEX_EXPR:
3876 return build_binary_op (EXPR_LOCATION (expr),
3877 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3878 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3879 c_common_truthvalue_conversion (location,
3880 TREE_OPERAND (expr, 0)),
3881 c_common_truthvalue_conversion (location,
3882 TREE_OPERAND (expr, 1)),
3885 case NEGATE_EXPR:
3886 case ABS_EXPR:
3887 case FLOAT_EXPR:
3888 case EXCESS_PRECISION_EXPR:
3889 /* These don't change whether an object is nonzero or zero. */
3890 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3892 case LROTATE_EXPR:
3893 case RROTATE_EXPR:
3894 /* These don't change whether an object is zero or nonzero, but
3895 we can't ignore them if their second arg has side-effects. */
3896 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3897 return build2 (COMPOUND_EXPR, truthvalue_type_node,
3898 TREE_OPERAND (expr, 1),
3899 c_common_truthvalue_conversion
3900 (location, TREE_OPERAND (expr, 0)));
3901 else
3902 return c_common_truthvalue_conversion (location,
3903 TREE_OPERAND (expr, 0));
3905 case COND_EXPR:
3906 /* Distribute the conversion into the arms of a COND_EXPR. */
3907 if (c_dialect_cxx ())
3908 return fold_build3 (COND_EXPR, truthvalue_type_node,
3909 TREE_OPERAND (expr, 0),
3910 c_common_truthvalue_conversion (location,
3911 TREE_OPERAND (expr,
3912 1)),
3913 c_common_truthvalue_conversion (location,
3914 TREE_OPERAND (expr,
3915 2)));
3916 else
3917 /* Folding will happen later for C. */
3918 return build3 (COND_EXPR, truthvalue_type_node,
3919 TREE_OPERAND (expr, 0),
3920 c_common_truthvalue_conversion (location,
3921 TREE_OPERAND (expr, 1)),
3922 c_common_truthvalue_conversion (location,
3923 TREE_OPERAND (expr, 2)));
3925 CASE_CONVERT:
3926 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3927 since that affects how `default_conversion' will behave. */
3928 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
3929 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
3930 break;
3931 /* If this is widening the argument, we can ignore it. */
3932 if (TYPE_PRECISION (TREE_TYPE (expr))
3933 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3934 return c_common_truthvalue_conversion (location,
3935 TREE_OPERAND (expr, 0));
3936 break;
3938 case MODIFY_EXPR:
3939 if (!TREE_NO_WARNING (expr)
3940 && warn_parentheses)
3942 warning (OPT_Wparentheses,
3943 "suggest parentheses around assignment used as truth value");
3944 TREE_NO_WARNING (expr) = 1;
3946 break;
3948 default:
3949 break;
3952 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3954 tree t = c_save_expr (expr);
3955 return (build_binary_op
3956 (EXPR_LOCATION (expr),
3957 (TREE_SIDE_EFFECTS (expr)
3958 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3959 c_common_truthvalue_conversion
3960 (location,
3961 build_unary_op (location, REALPART_EXPR, t, 0)),
3962 c_common_truthvalue_conversion
3963 (location,
3964 build_unary_op (location, IMAGPART_EXPR, t, 0)),
3965 0));
3968 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3970 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3971 FCONST0 (TYPE_MODE
3972 (TREE_TYPE (expr))));
3973 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
3976 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
3979 static void def_builtin_1 (enum built_in_function fncode,
3980 const char *name,
3981 enum built_in_class fnclass,
3982 tree fntype, tree libtype,
3983 bool both_p, bool fallback_p, bool nonansi_p,
3984 tree fnattrs, bool implicit_p);
3987 /* Apply the TYPE_QUALS to the new DECL. */
3989 void
3990 c_apply_type_quals_to_decl (int type_quals, tree decl)
3992 tree type = TREE_TYPE (decl);
3994 if (type == error_mark_node)
3995 return;
3997 if (((type_quals & TYPE_QUAL_CONST)
3998 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3999 /* An object declared 'const' is only readonly after it is
4000 initialized. We don't have any way of expressing this currently,
4001 so we need to be conservative and unset TREE_READONLY for types
4002 with constructors. Otherwise aliasing code will ignore stores in
4003 an inline constructor. */
4004 && !(type && TYPE_NEEDS_CONSTRUCTING (type)))
4005 TREE_READONLY (decl) = 1;
4006 if (type_quals & TYPE_QUAL_VOLATILE)
4008 TREE_SIDE_EFFECTS (decl) = 1;
4009 TREE_THIS_VOLATILE (decl) = 1;
4011 if (type_quals & TYPE_QUAL_RESTRICT)
4013 while (type && TREE_CODE (type) == ARRAY_TYPE)
4014 /* Allow 'restrict' on arrays of pointers.
4015 FIXME currently we just ignore it. */
4016 type = TREE_TYPE (type);
4017 if (!type
4018 || !POINTER_TYPE_P (type)
4019 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4020 error ("invalid use of %<restrict%>");
4021 else if (flag_strict_aliasing && type == TREE_TYPE (decl))
4022 /* Indicate we need to make a unique alias set for this pointer.
4023 We can't do it here because it might be pointing to an
4024 incomplete type. */
4025 DECL_POINTER_ALIAS_SET (decl) = -2;
4029 /* Hash function for the problem of multiple type definitions in
4030 different files. This must hash all types that will compare
4031 equal via comptypes to the same value. In practice it hashes
4032 on some of the simple stuff and leaves the details to comptypes. */
4034 static hashval_t
4035 c_type_hash (const void *p)
4037 int i = 0;
4038 int shift, size;
4039 const_tree const t = (const_tree) p;
4040 tree t2;
4041 switch (TREE_CODE (t))
4043 /* For pointers, hash on pointee type plus some swizzling. */
4044 case POINTER_TYPE:
4045 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4046 /* Hash on number of elements and total size. */
4047 case ENUMERAL_TYPE:
4048 shift = 3;
4049 t2 = TYPE_VALUES (t);
4050 break;
4051 case RECORD_TYPE:
4052 shift = 0;
4053 t2 = TYPE_FIELDS (t);
4054 break;
4055 case QUAL_UNION_TYPE:
4056 shift = 1;
4057 t2 = TYPE_FIELDS (t);
4058 break;
4059 case UNION_TYPE:
4060 shift = 2;
4061 t2 = TYPE_FIELDS (t);
4062 break;
4063 default:
4064 gcc_unreachable ();
4066 for (; t2; t2 = TREE_CHAIN (t2))
4067 i++;
4068 /* We might have a VLA here. */
4069 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4070 size = 0;
4071 else
4072 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4073 return ((size << 24) | (i << shift));
4076 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4078 /* Return the typed-based alias set for T, which may be an expression
4079 or a type. Return -1 if we don't do anything special. */
4081 alias_set_type
4082 c_common_get_alias_set (tree t)
4084 tree u;
4085 PTR *slot;
4087 /* Permit type-punning when accessing a union, provided the access
4088 is directly through the union. For example, this code does not
4089 permit taking the address of a union member and then storing
4090 through it. Even the type-punning allowed here is a GCC
4091 extension, albeit a common and useful one; the C standard says
4092 that such accesses have implementation-defined behavior. */
4093 for (u = t;
4094 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4095 u = TREE_OPERAND (u, 0))
4096 if (TREE_CODE (u) == COMPONENT_REF
4097 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4098 return 0;
4100 /* That's all the expressions we handle specially. */
4101 if (!TYPE_P (t))
4102 return -1;
4104 /* The C standard guarantees that any object may be accessed via an
4105 lvalue that has character type. */
4106 if (t == char_type_node
4107 || t == signed_char_type_node
4108 || t == unsigned_char_type_node)
4109 return 0;
4111 /* The C standard specifically allows aliasing between signed and
4112 unsigned variants of the same type. We treat the signed
4113 variant as canonical. */
4114 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4116 tree t1 = c_common_signed_type (t);
4118 /* t1 == t can happen for boolean nodes which are always unsigned. */
4119 if (t1 != t)
4120 return get_alias_set (t1);
4122 else if (POINTER_TYPE_P (t))
4124 tree t1;
4126 /* Unfortunately, there is no canonical form of a pointer type.
4127 In particular, if we have `typedef int I', then `int *', and
4128 `I *' are different types. So, we have to pick a canonical
4129 representative. We do this below.
4131 Technically, this approach is actually more conservative that
4132 it needs to be. In particular, `const int *' and `int *'
4133 should be in different alias sets, according to the C and C++
4134 standard, since their types are not the same, and so,
4135 technically, an `int **' and `const int **' cannot point at
4136 the same thing.
4138 But, the standard is wrong. In particular, this code is
4139 legal C++:
4141 int *ip;
4142 int **ipp = &ip;
4143 const int* const* cipp = ipp;
4145 And, it doesn't make sense for that to be legal unless you
4146 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
4147 the pointed-to types. This issue has been reported to the
4148 C++ committee. */
4149 t1 = build_type_no_quals (t);
4150 if (t1 != t)
4151 return get_alias_set (t1);
4154 /* Handle the case of multiple type nodes referring to "the same" type,
4155 which occurs with IMA. These share an alias set. FIXME: Currently only
4156 C90 is handled. (In C99 type compatibility is not transitive, which
4157 complicates things mightily. The alias set splay trees can theoretically
4158 represent this, but insertion is tricky when you consider all the
4159 different orders things might arrive in.) */
4161 if (c_language != clk_c || flag_isoc99)
4162 return -1;
4164 /* Save time if there's only one input file. */
4165 if (num_in_fnames == 1)
4166 return -1;
4168 /* Pointers need special handling if they point to any type that
4169 needs special handling (below). */
4170 if (TREE_CODE (t) == POINTER_TYPE)
4172 tree t2;
4173 /* Find bottom type under any nested POINTERs. */
4174 for (t2 = TREE_TYPE (t);
4175 TREE_CODE (t2) == POINTER_TYPE;
4176 t2 = TREE_TYPE (t2))
4178 if (TREE_CODE (t2) != RECORD_TYPE
4179 && TREE_CODE (t2) != ENUMERAL_TYPE
4180 && TREE_CODE (t2) != QUAL_UNION_TYPE
4181 && TREE_CODE (t2) != UNION_TYPE)
4182 return -1;
4183 if (TYPE_SIZE (t2) == 0)
4184 return -1;
4186 /* These are the only cases that need special handling. */
4187 if (TREE_CODE (t) != RECORD_TYPE
4188 && TREE_CODE (t) != ENUMERAL_TYPE
4189 && TREE_CODE (t) != QUAL_UNION_TYPE
4190 && TREE_CODE (t) != UNION_TYPE
4191 && TREE_CODE (t) != POINTER_TYPE)
4192 return -1;
4193 /* Undefined? */
4194 if (TYPE_SIZE (t) == 0)
4195 return -1;
4197 /* Look up t in hash table. Only one of the compatible types within each
4198 alias set is recorded in the table. */
4199 if (!type_hash_table)
4200 type_hash_table = htab_create_ggc (1021, c_type_hash,
4201 (htab_eq) lang_hooks.types_compatible_p,
4202 NULL);
4203 slot = htab_find_slot (type_hash_table, t, INSERT);
4204 if (*slot != NULL)
4206 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4207 return TYPE_ALIAS_SET ((tree)*slot);
4209 else
4210 /* Our caller will assign and record (in t) a new alias set; all we need
4211 to do is remember t in the hash table. */
4212 *slot = t;
4214 return -1;
4217 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where the
4218 second parameter indicates which OPERATOR is being applied. The COMPLAIN
4219 flag controls whether we should diagnose possibly ill-formed
4220 constructs or not. */
4222 tree
4223 c_sizeof_or_alignof_type (tree type, bool is_sizeof, int complain)
4225 const char *op_name;
4226 tree value = NULL;
4227 enum tree_code type_code = TREE_CODE (type);
4229 op_name = is_sizeof ? "sizeof" : "__alignof__";
4231 if (type_code == FUNCTION_TYPE)
4233 if (is_sizeof)
4235 if (complain && (pedantic || warn_pointer_arith))
4236 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4237 "invalid application of %<sizeof%> to a function type");
4238 else if (!complain)
4239 return error_mark_node;
4240 value = size_one_node;
4242 else
4243 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4245 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4247 if (type_code == VOID_TYPE
4248 && complain && (pedantic || warn_pointer_arith))
4249 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4250 "invalid application of %qs to a void type", op_name);
4251 else if (!complain)
4252 return error_mark_node;
4253 value = size_one_node;
4255 else if (!COMPLETE_TYPE_P (type))
4257 if (complain)
4258 error ("invalid application of %qs to incomplete type %qT ",
4259 op_name, type);
4260 value = size_zero_node;
4262 else
4264 if (is_sizeof)
4265 /* Convert in case a char is more than one unit. */
4266 value = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4267 size_int (TYPE_PRECISION (char_type_node)
4268 / BITS_PER_UNIT));
4269 else
4270 value = size_int (TYPE_ALIGN_UNIT (type));
4273 /* VALUE will have an integer type with TYPE_IS_SIZETYPE set.
4274 TYPE_IS_SIZETYPE means that certain things (like overflow) will
4275 never happen. However, this node should really have type
4276 `size_t', which is just a typedef for an ordinary integer type. */
4277 value = fold_convert (size_type_node, value);
4278 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
4280 return value;
4283 /* Implement the __alignof keyword: Return the minimum required
4284 alignment of EXPR, measured in bytes. For VAR_DECLs,
4285 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4286 from an "aligned" __attribute__ specification). */
4288 tree
4289 c_alignof_expr (tree expr)
4291 tree t;
4293 if (VAR_OR_FUNCTION_DECL_P (expr))
4294 t = size_int (DECL_ALIGN_UNIT (expr));
4296 else if (TREE_CODE (expr) == COMPONENT_REF
4297 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4299 error ("%<__alignof%> applied to a bit-field");
4300 t = size_one_node;
4302 else if (TREE_CODE (expr) == COMPONENT_REF
4303 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4304 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4306 else if (TREE_CODE (expr) == INDIRECT_REF)
4308 tree t = TREE_OPERAND (expr, 0);
4309 tree best = t;
4310 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4312 while (CONVERT_EXPR_P (t)
4313 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4315 int thisalign;
4317 t = TREE_OPERAND (t, 0);
4318 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4319 if (thisalign > bestalign)
4320 best = t, bestalign = thisalign;
4322 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
4324 else
4325 return c_alignof (TREE_TYPE (expr));
4327 return fold_convert (size_type_node, t);
4330 /* Handle C and C++ default attributes. */
4332 enum built_in_attribute
4334 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4335 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4336 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4337 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4338 #include "builtin-attrs.def"
4339 #undef DEF_ATTR_NULL_TREE
4340 #undef DEF_ATTR_INT
4341 #undef DEF_ATTR_IDENT
4342 #undef DEF_ATTR_TREE_LIST
4343 ATTR_LAST
4346 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4348 static void c_init_attributes (void);
4350 enum c_builtin_type
4352 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4353 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4354 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4355 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4356 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4357 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4358 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4359 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4360 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4361 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4362 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4363 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4364 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4365 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4366 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4367 NAME,
4368 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4369 #include "builtin-types.def"
4370 #undef DEF_PRIMITIVE_TYPE
4371 #undef DEF_FUNCTION_TYPE_0
4372 #undef DEF_FUNCTION_TYPE_1
4373 #undef DEF_FUNCTION_TYPE_2
4374 #undef DEF_FUNCTION_TYPE_3
4375 #undef DEF_FUNCTION_TYPE_4
4376 #undef DEF_FUNCTION_TYPE_5
4377 #undef DEF_FUNCTION_TYPE_6
4378 #undef DEF_FUNCTION_TYPE_7
4379 #undef DEF_FUNCTION_TYPE_VAR_0
4380 #undef DEF_FUNCTION_TYPE_VAR_1
4381 #undef DEF_FUNCTION_TYPE_VAR_2
4382 #undef DEF_FUNCTION_TYPE_VAR_3
4383 #undef DEF_FUNCTION_TYPE_VAR_4
4384 #undef DEF_FUNCTION_TYPE_VAR_5
4385 #undef DEF_POINTER_TYPE
4386 BT_LAST
4389 typedef enum c_builtin_type builtin_type;
4391 /* A temporary array for c_common_nodes_and_builtins. Used in
4392 communication with def_fn_type. */
4393 static tree builtin_types[(int) BT_LAST + 1];
4395 /* A helper function for c_common_nodes_and_builtins. Build function type
4396 for DEF with return type RET and N arguments. If VAR is true, then the
4397 function should be variadic after those N arguments.
4399 Takes special care not to ICE if any of the types involved are
4400 error_mark_node, which indicates that said type is not in fact available
4401 (see builtin_type_for_size). In which case the function type as a whole
4402 should be error_mark_node. */
4404 static void
4405 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4407 tree args = NULL, t;
4408 va_list list;
4409 int i;
4411 va_start (list, n);
4412 for (i = 0; i < n; ++i)
4414 builtin_type a = (builtin_type) va_arg (list, int);
4415 t = builtin_types[a];
4416 if (t == error_mark_node)
4417 goto egress;
4418 args = tree_cons (NULL_TREE, t, args);
4420 va_end (list);
4422 args = nreverse (args);
4423 if (!var)
4424 args = chainon (args, void_list_node);
4426 t = builtin_types[ret];
4427 if (t == error_mark_node)
4428 goto egress;
4429 t = build_function_type (t, args);
4431 egress:
4432 builtin_types[def] = t;
4435 /* Build builtin functions common to both C and C++ language
4436 frontends. */
4438 static void
4439 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4441 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4442 builtin_types[ENUM] = VALUE;
4443 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4444 def_fn_type (ENUM, RETURN, 0, 0);
4445 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4446 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4447 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4448 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4449 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4450 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4451 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4452 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4453 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4454 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4455 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4456 ARG6) \
4457 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4458 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4459 ARG6, ARG7) \
4460 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4461 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4462 def_fn_type (ENUM, RETURN, 1, 0);
4463 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4464 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4465 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4466 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4467 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4468 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4469 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4470 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4471 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4472 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4473 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4474 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4476 #include "builtin-types.def"
4478 #undef DEF_PRIMITIVE_TYPE
4479 #undef DEF_FUNCTION_TYPE_1
4480 #undef DEF_FUNCTION_TYPE_2
4481 #undef DEF_FUNCTION_TYPE_3
4482 #undef DEF_FUNCTION_TYPE_4
4483 #undef DEF_FUNCTION_TYPE_5
4484 #undef DEF_FUNCTION_TYPE_6
4485 #undef DEF_FUNCTION_TYPE_VAR_0
4486 #undef DEF_FUNCTION_TYPE_VAR_1
4487 #undef DEF_FUNCTION_TYPE_VAR_2
4488 #undef DEF_FUNCTION_TYPE_VAR_3
4489 #undef DEF_FUNCTION_TYPE_VAR_4
4490 #undef DEF_FUNCTION_TYPE_VAR_5
4491 #undef DEF_POINTER_TYPE
4492 builtin_types[(int) BT_LAST] = NULL_TREE;
4494 c_init_attributes ();
4496 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4497 NONANSI_P, ATTRS, IMPLICIT, COND) \
4498 if (NAME && COND) \
4499 def_builtin_1 (ENUM, NAME, CLASS, \
4500 builtin_types[(int) TYPE], \
4501 builtin_types[(int) LIBTYPE], \
4502 BOTH_P, FALLBACK_P, NONANSI_P, \
4503 built_in_attributes[(int) ATTRS], IMPLICIT);
4504 #include "builtins.def"
4505 #undef DEF_BUILTIN
4507 targetm.init_builtins ();
4509 build_common_builtin_nodes ();
4511 if (flag_mudflap)
4512 mudflap_init ();
4515 /* Like get_identifier, but avoid warnings about null arguments when
4516 the argument may be NULL for targets where GCC lacks stdint.h type
4517 information. */
4519 static inline tree
4520 c_get_ident (const char *id)
4522 return get_identifier (id);
4525 /* Build tree nodes and builtin functions common to both C and C++ language
4526 frontends. */
4528 void
4529 c_common_nodes_and_builtins (void)
4531 int char16_type_size;
4532 int char32_type_size;
4533 int wchar_type_size;
4534 tree array_domain_type;
4535 tree va_list_ref_type_node;
4536 tree va_list_arg_type_node;
4538 /* Define `int' and `char' first so that dbx will output them first. */
4539 record_builtin_type (RID_INT, NULL, integer_type_node);
4540 record_builtin_type (RID_CHAR, "char", char_type_node);
4542 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4543 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4544 but not C. Are the conditionals here needed? */
4545 if (c_dialect_cxx ())
4546 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4547 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4548 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4549 record_builtin_type (RID_MAX, "long unsigned int",
4550 long_unsigned_type_node);
4551 if (c_dialect_cxx ())
4552 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4553 record_builtin_type (RID_MAX, "long long int",
4554 long_long_integer_type_node);
4555 record_builtin_type (RID_MAX, "long long unsigned int",
4556 long_long_unsigned_type_node);
4557 if (c_dialect_cxx ())
4558 record_builtin_type (RID_MAX, "long long unsigned",
4559 long_long_unsigned_type_node);
4560 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4561 record_builtin_type (RID_MAX, "short unsigned int",
4562 short_unsigned_type_node);
4563 if (c_dialect_cxx ())
4564 record_builtin_type (RID_MAX, "unsigned short",
4565 short_unsigned_type_node);
4567 /* Define both `signed char' and `unsigned char'. */
4568 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4569 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4571 /* These are types that c_common_type_for_size and
4572 c_common_type_for_mode use. */
4573 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4574 intQI_type_node));
4575 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4576 intHI_type_node));
4577 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4578 intSI_type_node));
4579 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4580 intDI_type_node));
4581 #if HOST_BITS_PER_WIDE_INT >= 64
4582 if (targetm.scalar_mode_supported_p (TImode))
4583 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
4584 get_identifier ("__int128_t"),
4585 intTI_type_node));
4586 #endif
4587 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4588 unsigned_intQI_type_node));
4589 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4590 unsigned_intHI_type_node));
4591 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4592 unsigned_intSI_type_node));
4593 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4594 unsigned_intDI_type_node));
4595 #if HOST_BITS_PER_WIDE_INT >= 64
4596 if (targetm.scalar_mode_supported_p (TImode))
4597 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
4598 get_identifier ("__uint128_t"),
4599 unsigned_intTI_type_node));
4600 #endif
4602 /* Create the widest literal types. */
4603 widest_integer_literal_type_node
4604 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
4605 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4606 widest_integer_literal_type_node));
4608 widest_unsigned_literal_type_node
4609 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
4610 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL, NULL_TREE,
4611 widest_unsigned_literal_type_node));
4613 /* `unsigned long' is the standard type for sizeof.
4614 Note that stddef.h uses `unsigned long',
4615 and this must agree, even if long and int are the same size. */
4616 size_type_node =
4617 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
4618 signed_size_type_node = c_common_signed_type (size_type_node);
4619 set_sizetype (size_type_node);
4621 pid_type_node =
4622 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4624 build_common_tree_nodes_2 (flag_short_double);
4626 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4627 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4628 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4630 /* Only supported decimal floating point extension if the target
4631 actually supports underlying modes. */
4632 if (targetm.scalar_mode_supported_p (SDmode)
4633 && targetm.scalar_mode_supported_p (DDmode)
4634 && targetm.scalar_mode_supported_p (TDmode))
4636 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4637 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4638 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4641 if (targetm.fixed_point_supported_p ())
4643 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4644 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4645 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4646 record_builtin_type (RID_MAX, "long long _Fract",
4647 long_long_fract_type_node);
4648 record_builtin_type (RID_MAX, "unsigned short _Fract",
4649 unsigned_short_fract_type_node);
4650 record_builtin_type (RID_MAX, "unsigned _Fract",
4651 unsigned_fract_type_node);
4652 record_builtin_type (RID_MAX, "unsigned long _Fract",
4653 unsigned_long_fract_type_node);
4654 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4655 unsigned_long_long_fract_type_node);
4656 record_builtin_type (RID_MAX, "_Sat short _Fract",
4657 sat_short_fract_type_node);
4658 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4659 record_builtin_type (RID_MAX, "_Sat long _Fract",
4660 sat_long_fract_type_node);
4661 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4662 sat_long_long_fract_type_node);
4663 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4664 sat_unsigned_short_fract_type_node);
4665 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4666 sat_unsigned_fract_type_node);
4667 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4668 sat_unsigned_long_fract_type_node);
4669 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4670 sat_unsigned_long_long_fract_type_node);
4671 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4672 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4673 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4674 record_builtin_type (RID_MAX, "long long _Accum",
4675 long_long_accum_type_node);
4676 record_builtin_type (RID_MAX, "unsigned short _Accum",
4677 unsigned_short_accum_type_node);
4678 record_builtin_type (RID_MAX, "unsigned _Accum",
4679 unsigned_accum_type_node);
4680 record_builtin_type (RID_MAX, "unsigned long _Accum",
4681 unsigned_long_accum_type_node);
4682 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4683 unsigned_long_long_accum_type_node);
4684 record_builtin_type (RID_MAX, "_Sat short _Accum",
4685 sat_short_accum_type_node);
4686 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4687 record_builtin_type (RID_MAX, "_Sat long _Accum",
4688 sat_long_accum_type_node);
4689 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4690 sat_long_long_accum_type_node);
4691 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4692 sat_unsigned_short_accum_type_node);
4693 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4694 sat_unsigned_accum_type_node);
4695 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4696 sat_unsigned_long_accum_type_node);
4697 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4698 sat_unsigned_long_long_accum_type_node);
4702 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
4703 get_identifier ("complex int"),
4704 complex_integer_type_node));
4705 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
4706 get_identifier ("complex float"),
4707 complex_float_type_node));
4708 lang_hooks.decls.pushdecl (build_decl (TYPE_DECL,
4709 get_identifier ("complex double"),
4710 complex_double_type_node));
4711 lang_hooks.decls.pushdecl
4712 (build_decl (TYPE_DECL, get_identifier ("complex long double"),
4713 complex_long_double_type_node));
4715 if (c_dialect_cxx ())
4716 /* For C++, make fileptr_type_node a distinct void * type until
4717 FILE type is defined. */
4718 fileptr_type_node = build_variant_type_copy (ptr_type_node);
4720 record_builtin_type (RID_VOID, NULL, void_type_node);
4722 /* Set the TYPE_NAME for any variants that were built before
4723 record_builtin_type gave names to the built-in types. */
4725 tree void_name = TYPE_NAME (void_type_node);
4726 TYPE_NAME (void_type_node) = NULL_TREE;
4727 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4728 = void_name;
4729 TYPE_NAME (void_type_node) = void_name;
4732 /* This node must not be shared. */
4733 void_zero_node = make_node (INTEGER_CST);
4734 TREE_TYPE (void_zero_node) = void_type_node;
4736 void_list_node = build_void_list_node ();
4738 /* Make a type to be the domain of a few array types
4739 whose domains don't really matter.
4740 200 is small enough that it always fits in size_t
4741 and large enough that it can hold most function names for the
4742 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4743 array_domain_type = build_index_type (size_int (200));
4745 /* Make a type for arrays of characters.
4746 With luck nothing will ever really depend on the length of this
4747 array type. */
4748 char_array_type_node
4749 = build_array_type (char_type_node, array_domain_type);
4751 /* Likewise for arrays of ints. */
4752 int_array_type_node
4753 = build_array_type (integer_type_node, array_domain_type);
4755 string_type_node = build_pointer_type (char_type_node);
4756 const_string_type_node
4757 = build_pointer_type (build_qualified_type
4758 (char_type_node, TYPE_QUAL_CONST));
4760 /* This is special for C++ so functions can be overloaded. */
4761 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4762 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4763 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4764 underlying_wchar_type_node = wchar_type_node;
4765 if (c_dialect_cxx ())
4767 if (TYPE_UNSIGNED (wchar_type_node))
4768 wchar_type_node = make_unsigned_type (wchar_type_size);
4769 else
4770 wchar_type_node = make_signed_type (wchar_type_size);
4771 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4774 /* This is for wide string constants. */
4775 wchar_array_type_node
4776 = build_array_type (wchar_type_node, array_domain_type);
4778 /* Define 'char16_t'. */
4779 char16_type_node = get_identifier (CHAR16_TYPE);
4780 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4781 char16_type_size = TYPE_PRECISION (char16_type_node);
4782 if (c_dialect_cxx ())
4784 char16_type_node = make_unsigned_type (char16_type_size);
4786 if (cxx_dialect == cxx0x)
4787 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4790 /* This is for UTF-16 string constants. */
4791 char16_array_type_node
4792 = build_array_type (char16_type_node, array_domain_type);
4794 /* Define 'char32_t'. */
4795 char32_type_node = get_identifier (CHAR32_TYPE);
4796 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4797 char32_type_size = TYPE_PRECISION (char32_type_node);
4798 if (c_dialect_cxx ())
4800 char32_type_node = make_unsigned_type (char32_type_size);
4802 if (cxx_dialect == cxx0x)
4803 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4806 /* This is for UTF-32 string constants. */
4807 char32_array_type_node
4808 = build_array_type (char32_type_node, array_domain_type);
4810 wint_type_node =
4811 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4813 intmax_type_node =
4814 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4815 uintmax_type_node =
4816 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4818 if (SIG_ATOMIC_TYPE)
4819 sig_atomic_type_node =
4820 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4821 if (INT8_TYPE)
4822 int8_type_node =
4823 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4824 if (INT16_TYPE)
4825 int16_type_node =
4826 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4827 if (INT32_TYPE)
4828 int32_type_node =
4829 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4830 if (INT64_TYPE)
4831 int64_type_node =
4832 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4833 if (UINT8_TYPE)
4834 uint8_type_node =
4835 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4836 if (UINT16_TYPE)
4837 uint16_type_node =
4838 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4839 if (UINT32_TYPE)
4840 c_uint32_type_node =
4841 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4842 if (UINT64_TYPE)
4843 c_uint64_type_node =
4844 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4845 if (INT_LEAST8_TYPE)
4846 int_least8_type_node =
4847 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4848 if (INT_LEAST16_TYPE)
4849 int_least16_type_node =
4850 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4851 if (INT_LEAST32_TYPE)
4852 int_least32_type_node =
4853 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4854 if (INT_LEAST64_TYPE)
4855 int_least64_type_node =
4856 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4857 if (UINT_LEAST8_TYPE)
4858 uint_least8_type_node =
4859 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4860 if (UINT_LEAST16_TYPE)
4861 uint_least16_type_node =
4862 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4863 if (UINT_LEAST32_TYPE)
4864 uint_least32_type_node =
4865 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4866 if (UINT_LEAST64_TYPE)
4867 uint_least64_type_node =
4868 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4869 if (INT_FAST8_TYPE)
4870 int_fast8_type_node =
4871 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4872 if (INT_FAST16_TYPE)
4873 int_fast16_type_node =
4874 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4875 if (INT_FAST32_TYPE)
4876 int_fast32_type_node =
4877 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4878 if (INT_FAST64_TYPE)
4879 int_fast64_type_node =
4880 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4881 if (UINT_FAST8_TYPE)
4882 uint_fast8_type_node =
4883 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4884 if (UINT_FAST16_TYPE)
4885 uint_fast16_type_node =
4886 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4887 if (UINT_FAST32_TYPE)
4888 uint_fast32_type_node =
4889 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4890 if (UINT_FAST64_TYPE)
4891 uint_fast64_type_node =
4892 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4893 if (INTPTR_TYPE)
4894 intptr_type_node =
4895 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4896 if (UINTPTR_TYPE)
4897 uintptr_type_node =
4898 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4900 default_function_type = build_function_type (integer_type_node, NULL_TREE);
4901 ptrdiff_type_node
4902 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
4903 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4905 lang_hooks.decls.pushdecl
4906 (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
4907 va_list_type_node));
4908 #ifdef TARGET_ENUM_VA_LIST
4910 int l;
4911 const char *pname;
4912 tree ptype;
4913 for (l = 0; TARGET_ENUM_VA_LIST (l, &pname, &ptype); ++l)
4915 lang_hooks.decls.pushdecl
4916 (build_decl (TYPE_DECL, get_identifier (pname),
4917 ptype));
4921 #endif
4923 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4925 va_list_arg_type_node = va_list_ref_type_node =
4926 build_pointer_type (TREE_TYPE (va_list_type_node));
4928 else
4930 va_list_arg_type_node = va_list_type_node;
4931 va_list_ref_type_node = build_reference_type (va_list_type_node);
4934 if (!flag_preprocess_only)
4935 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4937 main_identifier_node = get_identifier ("main");
4939 /* Create the built-in __null node. It is important that this is
4940 not shared. */
4941 null_node = make_node (INTEGER_CST);
4942 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4944 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4945 memset (builtin_types, 0, sizeof (builtin_types));
4948 /* Look up the function in built_in_decls that corresponds to DECL
4949 and set ASMSPEC as its user assembler name. DECL must be a
4950 function decl that declares a builtin. */
4952 void
4953 set_builtin_user_assembler_name (tree decl, const char *asmspec)
4955 tree builtin;
4956 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
4957 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
4958 && asmspec != 0);
4960 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
4961 set_user_assembler_name (builtin, asmspec);
4962 switch (DECL_FUNCTION_CODE (decl))
4964 case BUILT_IN_MEMCPY:
4965 init_block_move_fn (asmspec);
4966 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
4967 break;
4968 case BUILT_IN_MEMSET:
4969 init_block_clear_fn (asmspec);
4970 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
4971 break;
4972 case BUILT_IN_MEMMOVE:
4973 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
4974 break;
4975 case BUILT_IN_MEMCMP:
4976 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
4977 break;
4978 case BUILT_IN_ABORT:
4979 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
4980 break;
4981 default:
4982 break;
4986 /* The number of named compound-literals generated thus far. */
4987 static GTY(()) int compound_literal_number;
4989 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4991 void
4992 set_compound_literal_name (tree decl)
4994 char *name;
4995 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4996 compound_literal_number);
4997 compound_literal_number++;
4998 DECL_NAME (decl) = get_identifier (name);
5001 tree
5002 build_va_arg (tree expr, tree type)
5004 return build1 (VA_ARG_EXPR, type, expr);
5008 /* Linked list of disabled built-in functions. */
5010 typedef struct disabled_builtin
5012 const char *name;
5013 struct disabled_builtin *next;
5014 } disabled_builtin;
5015 static disabled_builtin *disabled_builtins = NULL;
5017 static bool builtin_function_disabled_p (const char *);
5019 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5020 begins with "__builtin_", give an error. */
5022 void
5023 disable_builtin_function (const char *name)
5025 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5026 error ("cannot disable built-in function %qs", name);
5027 else
5029 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5030 new_disabled_builtin->name = name;
5031 new_disabled_builtin->next = disabled_builtins;
5032 disabled_builtins = new_disabled_builtin;
5037 /* Return true if the built-in function NAME has been disabled, false
5038 otherwise. */
5040 static bool
5041 builtin_function_disabled_p (const char *name)
5043 disabled_builtin *p;
5044 for (p = disabled_builtins; p != NULL; p = p->next)
5046 if (strcmp (name, p->name) == 0)
5047 return true;
5049 return false;
5053 /* Worker for DEF_BUILTIN.
5054 Possibly define a builtin function with one or two names.
5055 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5056 nonansi_p and flag_no_nonansi_builtin. */
5058 static void
5059 def_builtin_1 (enum built_in_function fncode,
5060 const char *name,
5061 enum built_in_class fnclass,
5062 tree fntype, tree libtype,
5063 bool both_p, bool fallback_p, bool nonansi_p,
5064 tree fnattrs, bool implicit_p)
5066 tree decl;
5067 const char *libname;
5069 if (fntype == error_mark_node)
5070 return;
5072 gcc_assert ((!both_p && !fallback_p)
5073 || !strncmp (name, "__builtin_",
5074 strlen ("__builtin_")));
5076 libname = name + strlen ("__builtin_");
5077 decl = add_builtin_function (name, fntype, fncode, fnclass,
5078 (fallback_p ? libname : NULL),
5079 fnattrs);
5080 if (both_p
5081 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5082 && !(nonansi_p && flag_no_nonansi_builtin))
5083 add_builtin_function (libname, libtype, fncode, fnclass,
5084 NULL, fnattrs);
5086 built_in_decls[(int) fncode] = decl;
5087 if (implicit_p)
5088 implicit_built_in_decls[(int) fncode] = decl;
5091 /* Nonzero if the type T promotes to int. This is (nearly) the
5092 integral promotions defined in ISO C99 6.3.1.1/2. */
5094 bool
5095 c_promoting_integer_type_p (const_tree t)
5097 switch (TREE_CODE (t))
5099 case INTEGER_TYPE:
5100 return (TYPE_MAIN_VARIANT (t) == char_type_node
5101 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5102 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5103 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5104 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5105 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5107 case ENUMERAL_TYPE:
5108 /* ??? Technically all enumerations not larger than an int
5109 promote to an int. But this is used along code paths
5110 that only want to notice a size change. */
5111 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5113 case BOOLEAN_TYPE:
5114 return 1;
5116 default:
5117 return 0;
5121 /* Return 1 if PARMS specifies a fixed number of parameters
5122 and none of their types is affected by default promotions. */
5125 self_promoting_args_p (const_tree parms)
5127 const_tree t;
5128 for (t = parms; t; t = TREE_CHAIN (t))
5130 tree type = TREE_VALUE (t);
5132 if (type == error_mark_node)
5133 continue;
5135 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5136 return 0;
5138 if (type == 0)
5139 return 0;
5141 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5142 return 0;
5144 if (c_promoting_integer_type_p (type))
5145 return 0;
5147 return 1;
5150 /* Recursively remove any '*' or '&' operator from TYPE. */
5151 tree
5152 strip_pointer_operator (tree t)
5154 while (POINTER_TYPE_P (t))
5155 t = TREE_TYPE (t);
5156 return t;
5159 /* Recursively remove pointer or array type from TYPE. */
5160 tree
5161 strip_pointer_or_array_types (tree t)
5163 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5164 t = TREE_TYPE (t);
5165 return t;
5168 /* Used to compare case labels. K1 and K2 are actually tree nodes
5169 representing case labels, or NULL_TREE for a `default' label.
5170 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5171 K2, and 0 if K1 and K2 are equal. */
5174 case_compare (splay_tree_key k1, splay_tree_key k2)
5176 /* Consider a NULL key (such as arises with a `default' label) to be
5177 smaller than anything else. */
5178 if (!k1)
5179 return k2 ? -1 : 0;
5180 else if (!k2)
5181 return k1 ? 1 : 0;
5183 return tree_int_cst_compare ((tree) k1, (tree) k2);
5186 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
5187 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
5188 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
5189 case label was declared using the usual C/C++ syntax, rather than
5190 the GNU case range extension. CASES is a tree containing all the
5191 case ranges processed so far; COND is the condition for the
5192 switch-statement itself. Returns the CASE_LABEL_EXPR created, or
5193 ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
5195 tree
5196 c_add_case_label (splay_tree cases, tree cond, tree orig_type,
5197 tree low_value, tree high_value)
5199 tree type;
5200 tree label;
5201 tree case_label;
5202 splay_tree_node node;
5204 /* Create the LABEL_DECL itself. */
5205 label = create_artificial_label ();
5207 /* If there was an error processing the switch condition, bail now
5208 before we get more confused. */
5209 if (!cond || cond == error_mark_node)
5210 goto error_out;
5212 if ((low_value && TREE_TYPE (low_value)
5213 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5214 || (high_value && TREE_TYPE (high_value)
5215 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5217 error ("pointers are not permitted as case values");
5218 goto error_out;
5221 /* Case ranges are a GNU extension. */
5222 if (high_value)
5223 pedwarn (input_location, OPT_pedantic,
5224 "range expressions in switch statements are non-standard");
5226 type = TREE_TYPE (cond);
5227 if (low_value)
5229 low_value = check_case_value (low_value);
5230 low_value = convert_and_check (type, low_value);
5231 if (low_value == error_mark_node)
5232 goto error_out;
5234 if (high_value)
5236 high_value = check_case_value (high_value);
5237 high_value = convert_and_check (type, high_value);
5238 if (high_value == error_mark_node)
5239 goto error_out;
5242 if (low_value && high_value)
5244 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5245 really a case range, even though it was written that way.
5246 Remove the HIGH_VALUE to simplify later processing. */
5247 if (tree_int_cst_equal (low_value, high_value))
5248 high_value = NULL_TREE;
5249 else if (!tree_int_cst_lt (low_value, high_value))
5250 warning (0, "empty range specified");
5253 /* See if the case is in range of the type of the original testing
5254 expression. If both low_value and high_value are out of range,
5255 don't insert the case label and return NULL_TREE. */
5256 if (low_value
5257 && !check_case_bounds (type, orig_type,
5258 &low_value, high_value ? &high_value : NULL))
5259 return NULL_TREE;
5261 /* Look up the LOW_VALUE in the table of case labels we already
5262 have. */
5263 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5264 /* If there was not an exact match, check for overlapping ranges.
5265 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5266 that's a `default' label and the only overlap is an exact match. */
5267 if (!node && (low_value || high_value))
5269 splay_tree_node low_bound;
5270 splay_tree_node high_bound;
5272 /* Even though there wasn't an exact match, there might be an
5273 overlap between this case range and another case range.
5274 Since we've (inductively) not allowed any overlapping case
5275 ranges, we simply need to find the greatest low case label
5276 that is smaller that LOW_VALUE, and the smallest low case
5277 label that is greater than LOW_VALUE. If there is an overlap
5278 it will occur in one of these two ranges. */
5279 low_bound = splay_tree_predecessor (cases,
5280 (splay_tree_key) low_value);
5281 high_bound = splay_tree_successor (cases,
5282 (splay_tree_key) low_value);
5284 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5285 the LOW_VALUE, so there is no need to check unless the
5286 LOW_BOUND is in fact itself a case range. */
5287 if (low_bound
5288 && CASE_HIGH ((tree) low_bound->value)
5289 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5290 low_value) >= 0)
5291 node = low_bound;
5292 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5293 range is bigger than the low end of the current range, so we
5294 are only interested if the current range is a real range, and
5295 not an ordinary case label. */
5296 else if (high_bound
5297 && high_value
5298 && (tree_int_cst_compare ((tree) high_bound->key,
5299 high_value)
5300 <= 0))
5301 node = high_bound;
5303 /* If there was an overlap, issue an error. */
5304 if (node)
5306 tree duplicate = CASE_LABEL ((tree) node->value);
5308 if (high_value)
5310 error ("duplicate (or overlapping) case value");
5311 error ("%Jthis is the first entry overlapping that value", duplicate);
5313 else if (low_value)
5315 error ("duplicate case value") ;
5316 error ("%Jpreviously used here", duplicate);
5318 else
5320 error ("multiple default labels in one switch");
5321 error ("%Jthis is the first default label", duplicate);
5323 goto error_out;
5326 /* Add a CASE_LABEL to the statement-tree. */
5327 case_label = add_stmt (build_case_label (low_value, high_value, label));
5328 /* Register this case label in the splay tree. */
5329 splay_tree_insert (cases,
5330 (splay_tree_key) low_value,
5331 (splay_tree_value) case_label);
5333 return case_label;
5335 error_out:
5336 /* Add a label so that the back-end doesn't think that the beginning of
5337 the switch is unreachable. Note that we do not add a case label, as
5338 that just leads to duplicates and thence to failure later on. */
5339 if (!cases->root)
5341 tree t = create_artificial_label ();
5342 add_stmt (build_stmt (LABEL_EXPR, t));
5344 return error_mark_node;
5347 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5348 Used to verify that case values match up with enumerator values. */
5350 static void
5351 match_case_to_enum_1 (tree key, tree type, tree label)
5353 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5355 /* ??? Not working too hard to print the double-word value.
5356 Should perhaps be done with %lwd in the diagnostic routines? */
5357 if (TREE_INT_CST_HIGH (key) == 0)
5358 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5359 TREE_INT_CST_LOW (key));
5360 else if (!TYPE_UNSIGNED (type)
5361 && TREE_INT_CST_HIGH (key) == -1
5362 && TREE_INT_CST_LOW (key) != 0)
5363 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5364 -TREE_INT_CST_LOW (key));
5365 else
5366 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5367 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5368 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5370 if (TYPE_NAME (type) == 0)
5371 warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5372 "%Jcase value %qs not in enumerated type",
5373 CASE_LABEL (label), buf);
5374 else
5375 warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5376 "%Jcase value %qs not in enumerated type %qT",
5377 CASE_LABEL (label), buf, type);
5380 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5381 Used to verify that case values match up with enumerator values. */
5383 static int
5384 match_case_to_enum (splay_tree_node node, void *data)
5386 tree label = (tree) node->value;
5387 tree type = (tree) data;
5389 /* Skip default case. */
5390 if (!CASE_LOW (label))
5391 return 0;
5393 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5394 when we did our enum->case scan. Reset our scratch bit after. */
5395 if (!CASE_LOW_SEEN (label))
5396 match_case_to_enum_1 (CASE_LOW (label), type, label);
5397 else
5398 CASE_LOW_SEEN (label) = 0;
5400 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
5401 not set, that means that CASE_HIGH did not appear when we did our
5402 enum->case scan. Reset our scratch bit after. */
5403 if (CASE_HIGH (label))
5405 if (!CASE_HIGH_SEEN (label))
5406 match_case_to_enum_1 (CASE_HIGH (label), type, label);
5407 else
5408 CASE_HIGH_SEEN (label) = 0;
5411 return 0;
5414 /* Handle -Wswitch*. Called from the front end after parsing the
5415 switch construct. */
5416 /* ??? Should probably be somewhere generic, since other languages
5417 besides C and C++ would want this. At the moment, however, C/C++
5418 are the only tree-ssa languages that support enumerations at all,
5419 so the point is moot. */
5421 void
5422 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5423 tree type, tree cond)
5425 splay_tree_node default_node;
5426 splay_tree_node node;
5427 tree chain;
5428 int saved_warn_switch;
5430 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5431 return;
5433 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5434 if (!default_node)
5435 warning (OPT_Wswitch_default, "%Hswitch missing default case",
5436 &switch_location);
5438 /* From here on, we only care about about enumerated types. */
5439 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5440 return;
5442 /* If the switch expression was an enumerated type, check that
5443 exactly all enumeration literals are covered by the cases.
5444 The check is made when -Wswitch was specified and there is no
5445 default case, or when -Wswitch-enum was specified. */
5447 if (!warn_switch_enum
5448 && !(warn_switch && !default_node))
5449 return;
5451 /* Clearing COND if it is not an integer constant simplifies
5452 the tests inside the loop below. */
5453 if (TREE_CODE (cond) != INTEGER_CST)
5454 cond = NULL_TREE;
5456 /* The time complexity here is O(N*lg(N)) worst case, but for the
5457 common case of monotonically increasing enumerators, it is
5458 O(N), since the nature of the splay tree will keep the next
5459 element adjacent to the root at all times. */
5461 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5463 tree value = TREE_VALUE (chain);
5464 if (TREE_CODE (value) == CONST_DECL)
5465 value = DECL_INITIAL (value);
5466 node = splay_tree_lookup (cases, (splay_tree_key) value);
5467 if (node)
5469 /* Mark the CASE_LOW part of the case entry as seen. */
5470 tree label = (tree) node->value;
5471 CASE_LOW_SEEN (label) = 1;
5472 continue;
5475 /* Even though there wasn't an exact match, there might be a
5476 case range which includes the enumerator's value. */
5477 node = splay_tree_predecessor (cases, (splay_tree_key) value);
5478 if (node && CASE_HIGH ((tree) node->value))
5480 tree label = (tree) node->value;
5481 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
5482 if (cmp >= 0)
5484 /* If we match the upper bound exactly, mark the CASE_HIGH
5485 part of the case entry as seen. */
5486 if (cmp == 0)
5487 CASE_HIGH_SEEN (label) = 1;
5488 continue;
5492 /* We've now determined that this enumerated literal isn't
5493 handled by the case labels of the switch statement. */
5495 /* If the switch expression is a constant, we only really care
5496 about whether that constant is handled by the switch. */
5497 if (cond && tree_int_cst_compare (cond, value))
5498 continue;
5500 /* If there is a default_node, the only relevant option is
5501 Wswitch-enum. Otherwise, if both are enabled then we prefer
5502 to warn using -Wswitch because -Wswitch is enabled by -Wall
5503 while -Wswitch-enum is explicit. */
5504 warning ((default_node || !warn_switch)
5505 ? OPT_Wswitch_enum : OPT_Wswitch,
5506 "%Henumeration value %qE not handled in switch",
5507 &switch_location, TREE_PURPOSE (chain));
5510 /* Warn if there are case expressions that don't correspond to
5511 enumerators. This can occur since C and C++ don't enforce
5512 type-checking of assignments to enumeration variables.
5514 The time complexity here is now always O(N) worst case, since
5515 we should have marked both the lower bound and upper bound of
5516 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
5517 above. This scan also resets those fields. */
5519 /* If there is a default_node, the only relevant option is
5520 Wswitch-enum. Otherwise, if both are enabled then we prefer
5521 to warn using -Wswitch because -Wswitch is enabled by -Wall
5522 while -Wswitch-enum is explicit. */
5523 saved_warn_switch = warn_switch;
5524 if (default_node)
5525 warn_switch = 0;
5526 splay_tree_foreach (cases, match_case_to_enum, type);
5527 warn_switch = saved_warn_switch;
5531 /* Finish an expression taking the address of LABEL (an
5532 IDENTIFIER_NODE). Returns an expression for the address.
5534 LOC is the location for the expression returned. */
5536 tree
5537 finish_label_address_expr (tree label, location_t loc)
5539 tree result;
5541 pedwarn (input_location, OPT_pedantic, "taking the address of a label is non-standard");
5543 if (label == error_mark_node)
5544 return error_mark_node;
5546 label = lookup_label (label);
5547 if (label == NULL_TREE)
5548 result = null_pointer_node;
5549 else
5551 TREE_USED (label) = 1;
5552 result = build1 (ADDR_EXPR, ptr_type_node, label);
5553 /* The current function in not necessarily uninlinable.
5554 Computed gotos are incompatible with inlining, but the value
5555 here could be used only in a diagnostic, for example. */
5556 protected_set_expr_location (result, loc);
5559 return result;
5563 /* Given a boolean expression ARG, return a tree representing an increment
5564 or decrement (as indicated by CODE) of ARG. The front end must check for
5565 invalid cases (e.g., decrement in C++). */
5566 tree
5567 boolean_increment (enum tree_code code, tree arg)
5569 tree val;
5570 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5572 arg = stabilize_reference (arg);
5573 switch (code)
5575 case PREINCREMENT_EXPR:
5576 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5577 break;
5578 case POSTINCREMENT_EXPR:
5579 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5580 arg = save_expr (arg);
5581 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5582 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5583 break;
5584 case PREDECREMENT_EXPR:
5585 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5586 invert_truthvalue (arg));
5587 break;
5588 case POSTDECREMENT_EXPR:
5589 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5590 invert_truthvalue (arg));
5591 arg = save_expr (arg);
5592 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5593 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5594 break;
5595 default:
5596 gcc_unreachable ();
5598 TREE_SIDE_EFFECTS (val) = 1;
5599 return val;
5602 /* Built-in macros for stddef.h and stdint.h, that require macros
5603 defined in this file. */
5604 void
5605 c_stddef_cpp_builtins(void)
5607 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5608 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5609 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5610 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5611 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5612 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5613 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5614 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5615 if (SIG_ATOMIC_TYPE)
5616 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5617 if (INT8_TYPE)
5618 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5619 if (INT16_TYPE)
5620 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5621 if (INT32_TYPE)
5622 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5623 if (INT64_TYPE)
5624 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5625 if (UINT8_TYPE)
5626 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5627 if (UINT16_TYPE)
5628 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5629 if (UINT32_TYPE)
5630 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5631 if (UINT64_TYPE)
5632 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5633 if (INT_LEAST8_TYPE)
5634 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5635 if (INT_LEAST16_TYPE)
5636 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5637 if (INT_LEAST32_TYPE)
5638 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5639 if (INT_LEAST64_TYPE)
5640 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5641 if (UINT_LEAST8_TYPE)
5642 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5643 if (UINT_LEAST16_TYPE)
5644 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5645 if (UINT_LEAST32_TYPE)
5646 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5647 if (UINT_LEAST64_TYPE)
5648 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5649 if (INT_FAST8_TYPE)
5650 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5651 if (INT_FAST16_TYPE)
5652 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5653 if (INT_FAST32_TYPE)
5654 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5655 if (INT_FAST64_TYPE)
5656 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5657 if (UINT_FAST8_TYPE)
5658 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5659 if (UINT_FAST16_TYPE)
5660 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5661 if (UINT_FAST32_TYPE)
5662 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5663 if (UINT_FAST64_TYPE)
5664 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5665 if (INTPTR_TYPE)
5666 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5667 if (UINTPTR_TYPE)
5668 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5671 static void
5672 c_init_attributes (void)
5674 /* Fill in the built_in_attributes array. */
5675 #define DEF_ATTR_NULL_TREE(ENUM) \
5676 built_in_attributes[(int) ENUM] = NULL_TREE;
5677 #define DEF_ATTR_INT(ENUM, VALUE) \
5678 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
5679 #define DEF_ATTR_IDENT(ENUM, STRING) \
5680 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5681 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5682 built_in_attributes[(int) ENUM] \
5683 = tree_cons (built_in_attributes[(int) PURPOSE], \
5684 built_in_attributes[(int) VALUE], \
5685 built_in_attributes[(int) CHAIN]);
5686 #include "builtin-attrs.def"
5687 #undef DEF_ATTR_NULL_TREE
5688 #undef DEF_ATTR_INT
5689 #undef DEF_ATTR_IDENT
5690 #undef DEF_ATTR_TREE_LIST
5693 /* Attribute handlers common to C front ends. */
5695 /* Handle a "packed" attribute; arguments as in
5696 struct attribute_spec.handler. */
5698 static tree
5699 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5700 int flags, bool *no_add_attrs)
5702 if (TYPE_P (*node))
5704 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5705 *node = build_variant_type_copy (*node);
5706 TYPE_PACKED (*node) = 1;
5708 else if (TREE_CODE (*node) == FIELD_DECL)
5710 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
5711 /* Still pack bitfields. */
5712 && ! DECL_INITIAL (*node))
5713 warning (OPT_Wattributes,
5714 "%qE attribute ignored for field of type %qT",
5715 name, TREE_TYPE (*node));
5716 else
5717 DECL_PACKED (*node) = 1;
5719 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
5720 used for DECL_REGISTER. It wouldn't mean anything anyway.
5721 We can't set DECL_PACKED on the type of a TYPE_DECL, because
5722 that changes what the typedef is typing. */
5723 else
5725 warning (OPT_Wattributes, "%qE attribute ignored", name);
5726 *no_add_attrs = true;
5729 return NULL_TREE;
5732 /* Handle a "nocommon" attribute; arguments as in
5733 struct attribute_spec.handler. */
5735 static tree
5736 handle_nocommon_attribute (tree *node, tree name,
5737 tree ARG_UNUSED (args),
5738 int ARG_UNUSED (flags), bool *no_add_attrs)
5740 if (TREE_CODE (*node) == VAR_DECL)
5741 DECL_COMMON (*node) = 0;
5742 else
5744 warning (OPT_Wattributes, "%qE attribute ignored", name);
5745 *no_add_attrs = true;
5748 return NULL_TREE;
5751 /* Handle a "common" attribute; arguments as in
5752 struct attribute_spec.handler. */
5754 static tree
5755 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5756 int ARG_UNUSED (flags), bool *no_add_attrs)
5758 if (TREE_CODE (*node) == VAR_DECL)
5759 DECL_COMMON (*node) = 1;
5760 else
5762 warning (OPT_Wattributes, "%qE attribute ignored", name);
5763 *no_add_attrs = true;
5766 return NULL_TREE;
5769 /* Handle a "noreturn" attribute; arguments as in
5770 struct attribute_spec.handler. */
5772 static tree
5773 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5774 int ARG_UNUSED (flags), bool *no_add_attrs)
5776 tree type = TREE_TYPE (*node);
5778 /* See FIXME comment in c_common_attribute_table. */
5779 if (TREE_CODE (*node) == FUNCTION_DECL)
5780 TREE_THIS_VOLATILE (*node) = 1;
5781 else if (TREE_CODE (type) == POINTER_TYPE
5782 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5783 TREE_TYPE (*node)
5784 = build_pointer_type
5785 (build_type_variant (TREE_TYPE (type),
5786 TYPE_READONLY (TREE_TYPE (type)), 1));
5787 else
5789 warning (OPT_Wattributes, "%qE attribute ignored", name);
5790 *no_add_attrs = true;
5793 return NULL_TREE;
5796 /* Handle a "hot" and attribute; arguments as in
5797 struct attribute_spec.handler. */
5799 static tree
5800 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5801 int ARG_UNUSED (flags), bool *no_add_attrs)
5803 if (TREE_CODE (*node) == FUNCTION_DECL)
5805 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
5807 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5808 name, "cold");
5809 *no_add_attrs = true;
5811 /* Most of the rest of the hot processing is done later with
5812 lookup_attribute. */
5814 else
5816 warning (OPT_Wattributes, "%qE attribute ignored", name);
5817 *no_add_attrs = true;
5820 return NULL_TREE;
5822 /* Handle a "cold" and attribute; arguments as in
5823 struct attribute_spec.handler. */
5825 static tree
5826 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5827 int ARG_UNUSED (flags), bool *no_add_attrs)
5829 if (TREE_CODE (*node) == FUNCTION_DECL)
5831 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
5833 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5834 name, "hot");
5835 *no_add_attrs = true;
5837 /* Most of the rest of the cold processing is done later with
5838 lookup_attribute. */
5840 else
5842 warning (OPT_Wattributes, "%qE attribute ignored", name);
5843 *no_add_attrs = true;
5846 return NULL_TREE;
5849 /* Handle a "noinline" attribute; arguments as in
5850 struct attribute_spec.handler. */
5852 static tree
5853 handle_noinline_attribute (tree *node, tree name,
5854 tree ARG_UNUSED (args),
5855 int ARG_UNUSED (flags), bool *no_add_attrs)
5857 if (TREE_CODE (*node) == FUNCTION_DECL)
5858 DECL_UNINLINABLE (*node) = 1;
5859 else
5861 warning (OPT_Wattributes, "%qE attribute ignored", name);
5862 *no_add_attrs = true;
5865 return NULL_TREE;
5868 /* Handle a "always_inline" attribute; arguments as in
5869 struct attribute_spec.handler. */
5871 static tree
5872 handle_always_inline_attribute (tree *node, tree name,
5873 tree ARG_UNUSED (args),
5874 int ARG_UNUSED (flags),
5875 bool *no_add_attrs)
5877 if (TREE_CODE (*node) == FUNCTION_DECL)
5879 /* Set the attribute and mark it for disregarding inline
5880 limits. */
5881 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
5883 else
5885 warning (OPT_Wattributes, "%qE attribute ignored", name);
5886 *no_add_attrs = true;
5889 return NULL_TREE;
5892 /* Handle a "gnu_inline" attribute; arguments as in
5893 struct attribute_spec.handler. */
5895 static tree
5896 handle_gnu_inline_attribute (tree *node, tree name,
5897 tree ARG_UNUSED (args),
5898 int ARG_UNUSED (flags),
5899 bool *no_add_attrs)
5901 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
5903 /* Do nothing else, just set the attribute. We'll get at
5904 it later with lookup_attribute. */
5906 else
5908 warning (OPT_Wattributes, "%qE attribute ignored", name);
5909 *no_add_attrs = true;
5912 return NULL_TREE;
5915 /* Handle an "artificial" attribute; arguments as in
5916 struct attribute_spec.handler. */
5918 static tree
5919 handle_artificial_attribute (tree *node, tree name,
5920 tree ARG_UNUSED (args),
5921 int ARG_UNUSED (flags),
5922 bool *no_add_attrs)
5924 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
5926 /* Do nothing else, just set the attribute. We'll get at
5927 it later with lookup_attribute. */
5929 else
5931 warning (OPT_Wattributes, "%qE attribute ignored", name);
5932 *no_add_attrs = true;
5935 return NULL_TREE;
5938 /* Handle a "flatten" attribute; arguments as in
5939 struct attribute_spec.handler. */
5941 static tree
5942 handle_flatten_attribute (tree *node, tree name,
5943 tree args ATTRIBUTE_UNUSED,
5944 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
5946 if (TREE_CODE (*node) == FUNCTION_DECL)
5947 /* Do nothing else, just set the attribute. We'll get at
5948 it later with lookup_attribute. */
5950 else
5952 warning (OPT_Wattributes, "%qE attribute ignored", name);
5953 *no_add_attrs = true;
5956 return NULL_TREE;
5959 /* Handle a "warning" or "error" attribute; arguments as in
5960 struct attribute_spec.handler. */
5962 static tree
5963 handle_error_attribute (tree *node, tree name, tree args,
5964 int ARG_UNUSED (flags), bool *no_add_attrs)
5966 if (TREE_CODE (*node) == FUNCTION_DECL
5967 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5968 /* Do nothing else, just set the attribute. We'll get at
5969 it later with lookup_attribute. */
5971 else
5973 warning (OPT_Wattributes, "%qE attribute ignored", name);
5974 *no_add_attrs = true;
5977 return NULL_TREE;
5980 /* Handle a "used" attribute; arguments as in
5981 struct attribute_spec.handler. */
5983 static tree
5984 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
5985 int ARG_UNUSED (flags), bool *no_add_attrs)
5987 tree node = *pnode;
5989 if (TREE_CODE (node) == FUNCTION_DECL
5990 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)))
5992 TREE_USED (node) = 1;
5993 DECL_PRESERVE_P (node) = 1;
5995 else
5997 warning (OPT_Wattributes, "%qE attribute ignored", name);
5998 *no_add_attrs = true;
6001 return NULL_TREE;
6004 /* Handle a "unused" attribute; arguments as in
6005 struct attribute_spec.handler. */
6007 static tree
6008 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6009 int flags, bool *no_add_attrs)
6011 if (DECL_P (*node))
6013 tree decl = *node;
6015 if (TREE_CODE (decl) == PARM_DECL
6016 || TREE_CODE (decl) == VAR_DECL
6017 || TREE_CODE (decl) == FUNCTION_DECL
6018 || TREE_CODE (decl) == LABEL_DECL
6019 || TREE_CODE (decl) == TYPE_DECL)
6020 TREE_USED (decl) = 1;
6021 else
6023 warning (OPT_Wattributes, "%qE attribute ignored", name);
6024 *no_add_attrs = true;
6027 else
6029 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6030 *node = build_variant_type_copy (*node);
6031 TREE_USED (*node) = 1;
6034 return NULL_TREE;
6037 /* Handle a "externally_visible" attribute; arguments as in
6038 struct attribute_spec.handler. */
6040 static tree
6041 handle_externally_visible_attribute (tree *pnode, tree name,
6042 tree ARG_UNUSED (args),
6043 int ARG_UNUSED (flags),
6044 bool *no_add_attrs)
6046 tree node = *pnode;
6048 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6050 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6051 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6053 warning (OPT_Wattributes,
6054 "%qE attribute have effect only on public objects", name);
6055 *no_add_attrs = true;
6058 else
6060 warning (OPT_Wattributes, "%qE attribute ignored", name);
6061 *no_add_attrs = true;
6064 return NULL_TREE;
6067 /* Handle a "const" attribute; arguments as in
6068 struct attribute_spec.handler. */
6070 static tree
6071 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6072 int ARG_UNUSED (flags), bool *no_add_attrs)
6074 tree type = TREE_TYPE (*node);
6076 /* See FIXME comment on noreturn in c_common_attribute_table. */
6077 if (TREE_CODE (*node) == FUNCTION_DECL)
6078 TREE_READONLY (*node) = 1;
6079 else if (TREE_CODE (type) == POINTER_TYPE
6080 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6081 TREE_TYPE (*node)
6082 = build_pointer_type
6083 (build_type_variant (TREE_TYPE (type), 1,
6084 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6085 else
6087 warning (OPT_Wattributes, "%qE attribute ignored", name);
6088 *no_add_attrs = true;
6091 return NULL_TREE;
6094 /* Handle a "transparent_union" attribute; arguments as in
6095 struct attribute_spec.handler. */
6097 static tree
6098 handle_transparent_union_attribute (tree *node, tree name,
6099 tree ARG_UNUSED (args), int flags,
6100 bool *no_add_attrs)
6102 tree type;
6104 *no_add_attrs = true;
6106 if (TREE_CODE (*node) == TYPE_DECL)
6107 node = &TREE_TYPE (*node);
6108 type = *node;
6110 if (TREE_CODE (type) == UNION_TYPE)
6112 /* When IN_PLACE is set, leave the check for FIELDS and MODE to
6113 the code in finish_struct. */
6114 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6116 if (TYPE_FIELDS (type) == NULL_TREE
6117 || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type)))
6118 goto ignored;
6120 /* A type variant isn't good enough, since we don't a cast
6121 to such a type removed as a no-op. */
6122 *node = type = build_duplicate_type (type);
6125 TYPE_TRANSPARENT_UNION (type) = 1;
6126 return NULL_TREE;
6129 ignored:
6130 warning (OPT_Wattributes, "%qE attribute ignored", name);
6131 return NULL_TREE;
6134 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6135 get the requested priority for a constructor or destructor,
6136 possibly issuing diagnostics for invalid or reserved
6137 priorities. */
6139 static priority_type
6140 get_priority (tree args, bool is_destructor)
6142 HOST_WIDE_INT pri;
6143 tree arg;
6145 if (!args)
6146 return DEFAULT_INIT_PRIORITY;
6148 if (!SUPPORTS_INIT_PRIORITY)
6150 if (is_destructor)
6151 error ("destructor priorities are not supported");
6152 else
6153 error ("constructor priorities are not supported");
6154 return DEFAULT_INIT_PRIORITY;
6157 arg = TREE_VALUE (args);
6158 if (!host_integerp (arg, /*pos=*/0)
6159 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6160 goto invalid;
6162 pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0);
6163 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6164 goto invalid;
6166 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6168 if (is_destructor)
6169 warning (0,
6170 "destructor priorities from 0 to %d are reserved "
6171 "for the implementation",
6172 MAX_RESERVED_INIT_PRIORITY);
6173 else
6174 warning (0,
6175 "constructor priorities from 0 to %d are reserved "
6176 "for the implementation",
6177 MAX_RESERVED_INIT_PRIORITY);
6179 return pri;
6181 invalid:
6182 if (is_destructor)
6183 error ("destructor priorities must be integers from 0 to %d inclusive",
6184 MAX_INIT_PRIORITY);
6185 else
6186 error ("constructor priorities must be integers from 0 to %d inclusive",
6187 MAX_INIT_PRIORITY);
6188 return DEFAULT_INIT_PRIORITY;
6191 /* Handle a "constructor" attribute; arguments as in
6192 struct attribute_spec.handler. */
6194 static tree
6195 handle_constructor_attribute (tree *node, tree name, tree args,
6196 int ARG_UNUSED (flags),
6197 bool *no_add_attrs)
6199 tree decl = *node;
6200 tree type = TREE_TYPE (decl);
6202 if (TREE_CODE (decl) == FUNCTION_DECL
6203 && TREE_CODE (type) == FUNCTION_TYPE
6204 && decl_function_context (decl) == 0)
6206 priority_type priority;
6207 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6208 priority = get_priority (args, /*is_destructor=*/false);
6209 SET_DECL_INIT_PRIORITY (decl, priority);
6210 TREE_USED (decl) = 1;
6212 else
6214 warning (OPT_Wattributes, "%qE attribute ignored", name);
6215 *no_add_attrs = true;
6218 return NULL_TREE;
6221 /* Handle a "destructor" attribute; arguments as in
6222 struct attribute_spec.handler. */
6224 static tree
6225 handle_destructor_attribute (tree *node, tree name, tree args,
6226 int ARG_UNUSED (flags),
6227 bool *no_add_attrs)
6229 tree decl = *node;
6230 tree type = TREE_TYPE (decl);
6232 if (TREE_CODE (decl) == FUNCTION_DECL
6233 && TREE_CODE (type) == FUNCTION_TYPE
6234 && decl_function_context (decl) == 0)
6236 priority_type priority;
6237 DECL_STATIC_DESTRUCTOR (decl) = 1;
6238 priority = get_priority (args, /*is_destructor=*/true);
6239 SET_DECL_FINI_PRIORITY (decl, priority);
6240 TREE_USED (decl) = 1;
6242 else
6244 warning (OPT_Wattributes, "%qE attribute ignored", name);
6245 *no_add_attrs = true;
6248 return NULL_TREE;
6251 /* Handle a "mode" attribute; arguments as in
6252 struct attribute_spec.handler. */
6254 static tree
6255 handle_mode_attribute (tree *node, tree name, tree args,
6256 int ARG_UNUSED (flags), bool *no_add_attrs)
6258 tree type = *node;
6259 tree ident = TREE_VALUE (args);
6261 *no_add_attrs = true;
6263 if (TREE_CODE (ident) != IDENTIFIER_NODE)
6264 warning (OPT_Wattributes, "%qE attribute ignored", name);
6265 else
6267 int j;
6268 const char *p = IDENTIFIER_POINTER (ident);
6269 int len = strlen (p);
6270 enum machine_mode mode = VOIDmode;
6271 tree typefm;
6272 bool valid_mode;
6274 if (len > 4 && p[0] == '_' && p[1] == '_'
6275 && p[len - 1] == '_' && p[len - 2] == '_')
6277 char *newp = (char *) alloca (len - 1);
6279 strcpy (newp, &p[2]);
6280 newp[len - 4] = '\0';
6281 p = newp;
6284 /* Change this type to have a type with the specified mode.
6285 First check for the special modes. */
6286 if (!strcmp (p, "byte"))
6287 mode = byte_mode;
6288 else if (!strcmp (p, "word"))
6289 mode = word_mode;
6290 else if (!strcmp (p, "pointer"))
6291 mode = ptr_mode;
6292 else if (!strcmp (p, "libgcc_cmp_return"))
6293 mode = targetm.libgcc_cmp_return_mode ();
6294 else if (!strcmp (p, "libgcc_shift_count"))
6295 mode = targetm.libgcc_shift_count_mode ();
6296 else if (!strcmp (p, "unwind_word"))
6297 mode = targetm.unwind_word_mode ();
6298 else
6299 for (j = 0; j < NUM_MACHINE_MODES; j++)
6300 if (!strcmp (p, GET_MODE_NAME (j)))
6302 mode = (enum machine_mode) j;
6303 break;
6306 if (mode == VOIDmode)
6308 error ("unknown machine mode %qE", ident);
6309 return NULL_TREE;
6312 valid_mode = false;
6313 switch (GET_MODE_CLASS (mode))
6315 case MODE_INT:
6316 case MODE_PARTIAL_INT:
6317 case MODE_FLOAT:
6318 case MODE_DECIMAL_FLOAT:
6319 case MODE_FRACT:
6320 case MODE_UFRACT:
6321 case MODE_ACCUM:
6322 case MODE_UACCUM:
6323 valid_mode = targetm.scalar_mode_supported_p (mode);
6324 break;
6326 case MODE_COMPLEX_INT:
6327 case MODE_COMPLEX_FLOAT:
6328 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
6329 break;
6331 case MODE_VECTOR_INT:
6332 case MODE_VECTOR_FLOAT:
6333 case MODE_VECTOR_FRACT:
6334 case MODE_VECTOR_UFRACT:
6335 case MODE_VECTOR_ACCUM:
6336 case MODE_VECTOR_UACCUM:
6337 warning (OPT_Wattributes, "specifying vector types with "
6338 "__attribute__ ((mode)) is deprecated");
6339 warning (OPT_Wattributes,
6340 "use __attribute__ ((vector_size)) instead");
6341 valid_mode = vector_mode_valid_p (mode);
6342 break;
6344 default:
6345 break;
6347 if (!valid_mode)
6349 error ("unable to emulate %qs", p);
6350 return NULL_TREE;
6353 if (POINTER_TYPE_P (type))
6355 tree (*fn)(tree, enum machine_mode, bool);
6357 if (!targetm.valid_pointer_mode (mode))
6359 error ("invalid pointer mode %qs", p);
6360 return NULL_TREE;
6363 if (TREE_CODE (type) == POINTER_TYPE)
6364 fn = build_pointer_type_for_mode;
6365 else
6366 fn = build_reference_type_for_mode;
6367 typefm = fn (TREE_TYPE (type), mode, false);
6369 else
6371 /* For fixed-point modes, we need to test if the signness of type
6372 and the machine mode are consistent. */
6373 if (ALL_FIXED_POINT_MODE_P (mode)
6374 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
6376 error ("signness of type and machine mode %qs don't match", p);
6377 return NULL_TREE;
6379 /* For fixed-point modes, we need to pass saturating info. */
6380 typefm = lang_hooks.types.type_for_mode (mode,
6381 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
6382 : TYPE_UNSIGNED (type));
6385 if (typefm == NULL_TREE)
6387 error ("no data type for mode %qs", p);
6388 return NULL_TREE;
6390 else if (TREE_CODE (type) == ENUMERAL_TYPE)
6392 /* For enumeral types, copy the precision from the integer
6393 type returned above. If not an INTEGER_TYPE, we can't use
6394 this mode for this type. */
6395 if (TREE_CODE (typefm) != INTEGER_TYPE)
6397 error ("cannot use mode %qs for enumeral types", p);
6398 return NULL_TREE;
6401 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
6403 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
6404 typefm = type;
6406 else
6408 /* We cannot build a type variant, as there's code that assumes
6409 that TYPE_MAIN_VARIANT has the same mode. This includes the
6410 debug generators. Instead, create a subrange type. This
6411 results in all of the enumeral values being emitted only once
6412 in the original, and the subtype gets them by reference. */
6413 if (TYPE_UNSIGNED (type))
6414 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
6415 else
6416 typefm = make_signed_type (TYPE_PRECISION (typefm));
6417 TREE_TYPE (typefm) = type;
6420 else if (VECTOR_MODE_P (mode)
6421 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
6422 : TREE_CODE (type) != TREE_CODE (typefm))
6424 error ("mode %qs applied to inappropriate type", p);
6425 return NULL_TREE;
6428 *node = typefm;
6431 return NULL_TREE;
6434 /* Handle a "section" attribute; arguments as in
6435 struct attribute_spec.handler. */
6437 static tree
6438 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6439 int ARG_UNUSED (flags), bool *no_add_attrs)
6441 tree decl = *node;
6443 if (targetm.have_named_sections)
6445 user_defined_section_attribute = true;
6447 if ((TREE_CODE (decl) == FUNCTION_DECL
6448 || TREE_CODE (decl) == VAR_DECL)
6449 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6451 if (TREE_CODE (decl) == VAR_DECL
6452 && current_function_decl != NULL_TREE
6453 && !TREE_STATIC (decl))
6455 error ("%Jsection attribute cannot be specified for "
6456 "local variables", decl);
6457 *no_add_attrs = true;
6460 /* The decl may have already been given a section attribute
6461 from a previous declaration. Ensure they match. */
6462 else if (DECL_SECTION_NAME (decl) != NULL_TREE
6463 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
6464 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
6466 error ("section of %q+D conflicts with previous declaration",
6467 *node);
6468 *no_add_attrs = true;
6470 else if (TREE_CODE (decl) == VAR_DECL
6471 && !targetm.have_tls && targetm.emutls.tmpl_section
6472 && DECL_THREAD_LOCAL_P (decl))
6474 error ("section of %q+D cannot be overridden", *node);
6475 *no_add_attrs = true;
6477 else
6478 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
6480 else
6482 error ("section attribute not allowed for %q+D", *node);
6483 *no_add_attrs = true;
6486 else
6488 error ("%Jsection attributes are not supported for this target", *node);
6489 *no_add_attrs = true;
6492 return NULL_TREE;
6495 /* Handle a "aligned" attribute; arguments as in
6496 struct attribute_spec.handler. */
6498 static tree
6499 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6500 int flags, bool *no_add_attrs)
6502 tree decl = NULL_TREE;
6503 tree *type = NULL;
6504 int is_type = 0;
6505 tree align_expr = (args ? TREE_VALUE (args)
6506 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
6507 int i;
6509 if (DECL_P (*node))
6511 decl = *node;
6512 type = &TREE_TYPE (decl);
6513 is_type = TREE_CODE (*node) == TYPE_DECL;
6515 else if (TYPE_P (*node))
6516 type = node, is_type = 1;
6518 if (TREE_CODE (align_expr) != INTEGER_CST)
6520 error ("requested alignment is not a constant");
6521 *no_add_attrs = true;
6523 else if ((i = tree_log2 (align_expr)) == -1)
6525 error ("requested alignment is not a power of 2");
6526 *no_add_attrs = true;
6528 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
6530 error ("requested alignment is too large");
6531 *no_add_attrs = true;
6533 else if (is_type)
6535 /* If we have a TYPE_DECL, then copy the type, so that we
6536 don't accidentally modify a builtin type. See pushdecl. */
6537 if (decl && TREE_TYPE (decl) != error_mark_node
6538 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
6540 tree tt = TREE_TYPE (decl);
6541 *type = build_variant_type_copy (*type);
6542 DECL_ORIGINAL_TYPE (decl) = tt;
6543 TYPE_NAME (*type) = decl;
6544 TREE_USED (*type) = TREE_USED (decl);
6545 TREE_TYPE (decl) = *type;
6547 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6548 *type = build_variant_type_copy (*type);
6550 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
6551 TYPE_USER_ALIGN (*type) = 1;
6553 else if (! VAR_OR_FUNCTION_DECL_P (decl)
6554 && TREE_CODE (decl) != FIELD_DECL)
6556 error ("alignment may not be specified for %q+D", decl);
6557 *no_add_attrs = true;
6559 else if (TREE_CODE (decl) == FUNCTION_DECL
6560 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
6562 if (DECL_USER_ALIGN (decl))
6563 error ("alignment for %q+D was previously specified as %d "
6564 "and may not be decreased", decl,
6565 DECL_ALIGN (decl) / BITS_PER_UNIT);
6566 else
6567 error ("alignment for %q+D must be at least %d", decl,
6568 DECL_ALIGN (decl) / BITS_PER_UNIT);
6569 *no_add_attrs = true;
6571 else
6573 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
6574 DECL_USER_ALIGN (decl) = 1;
6577 return NULL_TREE;
6580 /* Handle a "weak" attribute; arguments as in
6581 struct attribute_spec.handler. */
6583 static tree
6584 handle_weak_attribute (tree *node, tree name,
6585 tree ARG_UNUSED (args),
6586 int ARG_UNUSED (flags),
6587 bool * ARG_UNUSED (no_add_attrs))
6589 if (TREE_CODE (*node) == FUNCTION_DECL
6590 && DECL_DECLARED_INLINE_P (*node))
6592 error ("inline function %q+D cannot be declared weak", *node);
6593 *no_add_attrs = true;
6595 else if (TREE_CODE (*node) == FUNCTION_DECL
6596 || TREE_CODE (*node) == VAR_DECL)
6597 declare_weak (*node);
6598 else
6599 warning (OPT_Wattributes, "%qE attribute ignored", name);
6601 return NULL_TREE;
6604 /* Handle an "alias" attribute; arguments as in
6605 struct attribute_spec.handler. */
6607 static tree
6608 handle_alias_attribute (tree *node, tree name, tree args,
6609 int ARG_UNUSED (flags), bool *no_add_attrs)
6611 tree decl = *node;
6613 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
6615 warning (OPT_Wattributes, "%qE attribute ignored", name);
6616 *no_add_attrs = true;
6618 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
6619 || (TREE_CODE (decl) != FUNCTION_DECL
6620 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
6621 /* A static variable declaration is always a tentative definition,
6622 but the alias is a non-tentative definition which overrides. */
6623 || (TREE_CODE (decl) != FUNCTION_DECL
6624 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
6626 error ("%q+D defined both normally and as an alias", decl);
6627 *no_add_attrs = true;
6630 /* Note that the very first time we process a nested declaration,
6631 decl_function_context will not be set. Indeed, *would* never
6632 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
6633 we do below. After such frobbery, pushdecl would set the context.
6634 In any case, this is never what we want. */
6635 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
6637 tree id;
6639 id = TREE_VALUE (args);
6640 if (TREE_CODE (id) != STRING_CST)
6642 error ("alias argument not a string");
6643 *no_add_attrs = true;
6644 return NULL_TREE;
6646 id = get_identifier (TREE_STRING_POINTER (id));
6647 /* This counts as a use of the object pointed to. */
6648 TREE_USED (id) = 1;
6650 if (TREE_CODE (decl) == FUNCTION_DECL)
6651 DECL_INITIAL (decl) = error_mark_node;
6652 else
6654 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6655 DECL_EXTERNAL (decl) = 1;
6656 else
6657 DECL_EXTERNAL (decl) = 0;
6658 TREE_STATIC (decl) = 1;
6661 else
6663 warning (OPT_Wattributes, "%qE attribute ignored", name);
6664 *no_add_attrs = true;
6667 return NULL_TREE;
6670 /* Handle a "weakref" attribute; arguments as in struct
6671 attribute_spec.handler. */
6673 static tree
6674 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6675 int flags, bool *no_add_attrs)
6677 tree attr = NULL_TREE;
6679 /* We must ignore the attribute when it is associated with
6680 local-scoped decls, since attribute alias is ignored and many
6681 such symbols do not even have a DECL_WEAK field. */
6682 if (decl_function_context (*node)
6683 || current_function_decl
6684 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
6686 warning (OPT_Wattributes, "%qE attribute ignored", name);
6687 *no_add_attrs = true;
6688 return NULL_TREE;
6691 /* The idea here is that `weakref("name")' mutates into `weakref,
6692 alias("name")', and weakref without arguments, in turn,
6693 implicitly adds weak. */
6695 if (args)
6697 attr = tree_cons (get_identifier ("alias"), args, attr);
6698 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
6700 *no_add_attrs = true;
6702 decl_attributes (node, attr, flags);
6704 else
6706 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
6707 error ("%Jweakref attribute must appear before alias attribute",
6708 *node);
6710 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
6711 and that isn't supported; and because it wants to add it to
6712 the list of weak decls, which isn't helpful. */
6713 DECL_WEAK (*node) = 1;
6716 return NULL_TREE;
6719 /* Handle an "visibility" attribute; arguments as in
6720 struct attribute_spec.handler. */
6722 static tree
6723 handle_visibility_attribute (tree *node, tree name, tree args,
6724 int ARG_UNUSED (flags),
6725 bool *ARG_UNUSED (no_add_attrs))
6727 tree decl = *node;
6728 tree id = TREE_VALUE (args);
6729 enum symbol_visibility vis;
6731 if (TYPE_P (*node))
6733 if (TREE_CODE (*node) == ENUMERAL_TYPE)
6734 /* OK */;
6735 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
6737 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
6738 name);
6739 return NULL_TREE;
6741 else if (TYPE_FIELDS (*node))
6743 error ("%qE attribute ignored because %qT is already defined",
6744 name, *node);
6745 return NULL_TREE;
6748 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
6750 warning (OPT_Wattributes, "%qE attribute ignored", name);
6751 return NULL_TREE;
6754 if (TREE_CODE (id) != STRING_CST)
6756 error ("visibility argument not a string");
6757 return NULL_TREE;
6760 /* If this is a type, set the visibility on the type decl. */
6761 if (TYPE_P (decl))
6763 decl = TYPE_NAME (decl);
6764 if (!decl)
6765 return NULL_TREE;
6766 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6768 warning (OPT_Wattributes, "%qE attribute ignored on types",
6769 name);
6770 return NULL_TREE;
6774 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
6775 vis = VISIBILITY_DEFAULT;
6776 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
6777 vis = VISIBILITY_INTERNAL;
6778 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
6779 vis = VISIBILITY_HIDDEN;
6780 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
6781 vis = VISIBILITY_PROTECTED;
6782 else
6784 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
6785 vis = VISIBILITY_DEFAULT;
6788 if (DECL_VISIBILITY_SPECIFIED (decl)
6789 && vis != DECL_VISIBILITY (decl))
6791 tree attributes = (TYPE_P (*node)
6792 ? TYPE_ATTRIBUTES (*node)
6793 : DECL_ATTRIBUTES (decl));
6794 if (lookup_attribute ("visibility", attributes))
6795 error ("%qD redeclared with different visibility", decl);
6796 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6797 && lookup_attribute ("dllimport", attributes))
6798 error ("%qD was declared %qs which implies default visibility",
6799 decl, "dllimport");
6800 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6801 && lookup_attribute ("dllexport", attributes))
6802 error ("%qD was declared %qs which implies default visibility",
6803 decl, "dllexport");
6806 DECL_VISIBILITY (decl) = vis;
6807 DECL_VISIBILITY_SPECIFIED (decl) = 1;
6809 /* Go ahead and attach the attribute to the node as well. This is needed
6810 so we can determine whether we have VISIBILITY_DEFAULT because the
6811 visibility was not specified, or because it was explicitly overridden
6812 from the containing scope. */
6814 return NULL_TREE;
6817 /* Determine the ELF symbol visibility for DECL, which is either a
6818 variable or a function. It is an error to use this function if a
6819 definition of DECL is not available in this translation unit.
6820 Returns true if the final visibility has been determined by this
6821 function; false if the caller is free to make additional
6822 modifications. */
6824 bool
6825 c_determine_visibility (tree decl)
6827 gcc_assert (TREE_CODE (decl) == VAR_DECL
6828 || TREE_CODE (decl) == FUNCTION_DECL);
6830 /* If the user explicitly specified the visibility with an
6831 attribute, honor that. DECL_VISIBILITY will have been set during
6832 the processing of the attribute. We check for an explicit
6833 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
6834 to distinguish the use of an attribute from the use of a "#pragma
6835 GCC visibility push(...)"; in the latter case we still want other
6836 considerations to be able to overrule the #pragma. */
6837 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
6838 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6839 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
6840 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
6841 return true;
6843 /* Set default visibility to whatever the user supplied with
6844 visibility_specified depending on #pragma GCC visibility. */
6845 if (!DECL_VISIBILITY_SPECIFIED (decl))
6847 if (visibility_options.inpragma
6848 || DECL_VISIBILITY (decl) != default_visibility)
6850 DECL_VISIBILITY (decl) = default_visibility;
6851 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
6852 /* If visibility changed and DECL already has DECL_RTL, ensure
6853 symbol flags are updated. */
6854 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
6855 || TREE_CODE (decl) == FUNCTION_DECL)
6856 && DECL_RTL_SET_P (decl))
6857 make_decl_rtl (decl);
6860 return false;
6863 /* Handle an "tls_model" attribute; arguments as in
6864 struct attribute_spec.handler. */
6866 static tree
6867 handle_tls_model_attribute (tree *node, tree name, tree args,
6868 int ARG_UNUSED (flags), bool *no_add_attrs)
6870 tree id;
6871 tree decl = *node;
6872 enum tls_model kind;
6874 *no_add_attrs = true;
6876 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
6878 warning (OPT_Wattributes, "%qE attribute ignored", name);
6879 return NULL_TREE;
6882 kind = DECL_TLS_MODEL (decl);
6883 id = TREE_VALUE (args);
6884 if (TREE_CODE (id) != STRING_CST)
6886 error ("tls_model argument not a string");
6887 return NULL_TREE;
6890 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
6891 kind = TLS_MODEL_LOCAL_EXEC;
6892 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
6893 kind = TLS_MODEL_INITIAL_EXEC;
6894 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
6895 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
6896 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
6897 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6898 else
6899 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
6901 DECL_TLS_MODEL (decl) = kind;
6902 return NULL_TREE;
6905 /* Handle a "no_instrument_function" attribute; arguments as in
6906 struct attribute_spec.handler. */
6908 static tree
6909 handle_no_instrument_function_attribute (tree *node, tree name,
6910 tree ARG_UNUSED (args),
6911 int ARG_UNUSED (flags),
6912 bool *no_add_attrs)
6914 tree decl = *node;
6916 if (TREE_CODE (decl) != FUNCTION_DECL)
6918 error ("%J%qE attribute applies only to functions", decl, name);
6919 *no_add_attrs = true;
6921 else if (DECL_INITIAL (decl))
6923 error ("%Jcan%'t set %qE attribute after definition", decl, name);
6924 *no_add_attrs = true;
6926 else
6927 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
6929 return NULL_TREE;
6932 /* Handle a "malloc" attribute; arguments as in
6933 struct attribute_spec.handler. */
6935 static tree
6936 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6937 int ARG_UNUSED (flags), bool *no_add_attrs)
6939 if (TREE_CODE (*node) == FUNCTION_DECL
6940 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
6941 DECL_IS_MALLOC (*node) = 1;
6942 else
6944 warning (OPT_Wattributes, "%qE attribute ignored", name);
6945 *no_add_attrs = true;
6948 return NULL_TREE;
6951 /* Handle a "alloc_size" attribute; arguments as in
6952 struct attribute_spec.handler. */
6954 static tree
6955 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6956 int ARG_UNUSED (flags), bool *no_add_attrs)
6958 unsigned arg_count = type_num_arguments (*node);
6959 for (; args; args = TREE_CHAIN (args))
6961 tree position = TREE_VALUE (args);
6963 if (TREE_CODE (position) != INTEGER_CST
6964 || TREE_INT_CST_HIGH (position)
6965 || TREE_INT_CST_LOW (position) < 1
6966 || TREE_INT_CST_LOW (position) > arg_count )
6968 warning (OPT_Wattributes,
6969 "alloc_size parameter outside range");
6970 *no_add_attrs = true;
6971 return NULL_TREE;
6974 return NULL_TREE;
6977 /* Handle a "returns_twice" attribute; arguments as in
6978 struct attribute_spec.handler. */
6980 static tree
6981 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6982 int ARG_UNUSED (flags), bool *no_add_attrs)
6984 if (TREE_CODE (*node) == FUNCTION_DECL)
6985 DECL_IS_RETURNS_TWICE (*node) = 1;
6986 else
6988 warning (OPT_Wattributes, "%qE attribute ignored", name);
6989 *no_add_attrs = true;
6992 return NULL_TREE;
6995 /* Handle a "no_limit_stack" attribute; arguments as in
6996 struct attribute_spec.handler. */
6998 static tree
6999 handle_no_limit_stack_attribute (tree *node, tree name,
7000 tree ARG_UNUSED (args),
7001 int ARG_UNUSED (flags),
7002 bool *no_add_attrs)
7004 tree decl = *node;
7006 if (TREE_CODE (decl) != FUNCTION_DECL)
7008 error ("%J%qE attribute applies only to functions", decl, name);
7009 *no_add_attrs = true;
7011 else if (DECL_INITIAL (decl))
7013 error ("%Jcan%'t set %qE attribute after definition", decl, name);
7014 *no_add_attrs = true;
7016 else
7017 DECL_NO_LIMIT_STACK (decl) = 1;
7019 return NULL_TREE;
7022 /* Handle a "pure" attribute; arguments as in
7023 struct attribute_spec.handler. */
7025 static tree
7026 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7027 int ARG_UNUSED (flags), bool *no_add_attrs)
7029 if (TREE_CODE (*node) == FUNCTION_DECL)
7030 DECL_PURE_P (*node) = 1;
7031 /* ??? TODO: Support types. */
7032 else
7034 warning (OPT_Wattributes, "%qE attribute ignored", name);
7035 *no_add_attrs = true;
7038 return NULL_TREE;
7041 /* Handle a "no vops" attribute; arguments as in
7042 struct attribute_spec.handler. */
7044 static tree
7045 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
7046 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7047 bool *ARG_UNUSED (no_add_attrs))
7049 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
7050 DECL_IS_NOVOPS (*node) = 1;
7051 return NULL_TREE;
7054 /* Handle a "deprecated" attribute; arguments as in
7055 struct attribute_spec.handler. */
7057 static tree
7058 handle_deprecated_attribute (tree *node, tree name,
7059 tree args, int flags,
7060 bool *no_add_attrs)
7062 tree type = NULL_TREE;
7063 int warn = 0;
7064 tree what = NULL_TREE;
7066 if (!args)
7067 *no_add_attrs = true;
7068 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7070 error ("deprecated message is not a string");
7071 *no_add_attrs = true;
7074 if (DECL_P (*node))
7076 tree decl = *node;
7077 type = TREE_TYPE (decl);
7079 if (TREE_CODE (decl) == TYPE_DECL
7080 || TREE_CODE (decl) == PARM_DECL
7081 || TREE_CODE (decl) == VAR_DECL
7082 || TREE_CODE (decl) == FUNCTION_DECL
7083 || TREE_CODE (decl) == FIELD_DECL)
7084 TREE_DEPRECATED (decl) = 1;
7085 else
7086 warn = 1;
7088 else if (TYPE_P (*node))
7090 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7091 *node = build_variant_type_copy (*node);
7092 TREE_DEPRECATED (*node) = 1;
7093 type = *node;
7095 else
7096 warn = 1;
7098 if (warn)
7100 *no_add_attrs = true;
7101 if (type && TYPE_NAME (type))
7103 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
7104 what = TYPE_NAME (*node);
7105 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7106 && DECL_NAME (TYPE_NAME (type)))
7107 what = DECL_NAME (TYPE_NAME (type));
7109 if (what)
7110 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
7111 else
7112 warning (OPT_Wattributes, "%qE attribute ignored", name);
7115 return NULL_TREE;
7118 /* Handle a "vector_size" attribute; arguments as in
7119 struct attribute_spec.handler. */
7121 static tree
7122 handle_vector_size_attribute (tree *node, tree name, tree args,
7123 int ARG_UNUSED (flags),
7124 bool *no_add_attrs)
7126 unsigned HOST_WIDE_INT vecsize, nunits;
7127 enum machine_mode orig_mode;
7128 tree type = *node, new_type, size;
7130 *no_add_attrs = true;
7132 size = TREE_VALUE (args);
7134 if (!host_integerp (size, 1))
7136 warning (OPT_Wattributes, "%qE attribute ignored", name);
7137 return NULL_TREE;
7140 /* Get the vector size (in bytes). */
7141 vecsize = tree_low_cst (size, 1);
7143 /* We need to provide for vector pointers, vector arrays, and
7144 functions returning vectors. For example:
7146 __attribute__((vector_size(16))) short *foo;
7148 In this case, the mode is SI, but the type being modified is
7149 HI, so we need to look further. */
7151 while (POINTER_TYPE_P (type)
7152 || TREE_CODE (type) == FUNCTION_TYPE
7153 || TREE_CODE (type) == METHOD_TYPE
7154 || TREE_CODE (type) == ARRAY_TYPE
7155 || TREE_CODE (type) == OFFSET_TYPE)
7156 type = TREE_TYPE (type);
7158 /* Get the mode of the type being modified. */
7159 orig_mode = TYPE_MODE (type);
7161 if ((!INTEGRAL_TYPE_P (type)
7162 && !SCALAR_FLOAT_TYPE_P (type)
7163 && !FIXED_POINT_TYPE_P (type))
7164 || (!SCALAR_FLOAT_MODE_P (orig_mode)
7165 && GET_MODE_CLASS (orig_mode) != MODE_INT
7166 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
7167 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
7168 || TREE_CODE (type) == BOOLEAN_TYPE)
7170 error ("invalid vector type for attribute %qE", name);
7171 return NULL_TREE;
7174 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
7176 error ("vector size not an integral multiple of component size");
7177 return NULL;
7180 if (vecsize == 0)
7182 error ("zero vector size");
7183 return NULL;
7186 /* Calculate how many units fit in the vector. */
7187 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
7188 if (nunits & (nunits - 1))
7190 error ("number of components of the vector not a power of two");
7191 return NULL_TREE;
7194 new_type = build_vector_type (type, nunits);
7196 /* Build back pointers if needed. */
7197 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
7199 return NULL_TREE;
7202 /* Handle the "nonnull" attribute. */
7203 static tree
7204 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
7205 tree args, int ARG_UNUSED (flags),
7206 bool *no_add_attrs)
7208 tree type = *node;
7209 unsigned HOST_WIDE_INT attr_arg_num;
7211 /* If no arguments are specified, all pointer arguments should be
7212 non-null. Verify a full prototype is given so that the arguments
7213 will have the correct types when we actually check them later. */
7214 if (!args)
7216 if (!TYPE_ARG_TYPES (type))
7218 error ("nonnull attribute without arguments on a non-prototype");
7219 *no_add_attrs = true;
7221 return NULL_TREE;
7224 /* Argument list specified. Verify that each argument number references
7225 a pointer argument. */
7226 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
7228 tree argument;
7229 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
7231 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
7233 error ("nonnull argument has invalid operand number (argument %lu)",
7234 (unsigned long) attr_arg_num);
7235 *no_add_attrs = true;
7236 return NULL_TREE;
7239 argument = TYPE_ARG_TYPES (type);
7240 if (argument)
7242 for (ck_num = 1; ; ck_num++)
7244 if (!argument || ck_num == arg_num)
7245 break;
7246 argument = TREE_CHAIN (argument);
7249 if (!argument
7250 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
7252 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
7253 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7254 *no_add_attrs = true;
7255 return NULL_TREE;
7258 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
7260 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
7261 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7262 *no_add_attrs = true;
7263 return NULL_TREE;
7268 return NULL_TREE;
7271 /* Check the argument list of a function call for null in argument slots
7272 that are marked as requiring a non-null pointer argument. The NARGS
7273 arguments are passed in the array ARGARRAY.
7276 static void
7277 check_function_nonnull (tree attrs, int nargs, tree *argarray)
7279 tree a, args;
7280 int i;
7282 for (a = attrs; a; a = TREE_CHAIN (a))
7284 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
7286 args = TREE_VALUE (a);
7288 /* Walk the argument list. If we encounter an argument number we
7289 should check for non-null, do it. If the attribute has no args,
7290 then every pointer argument is checked (in which case the check
7291 for pointer type is done in check_nonnull_arg). */
7292 for (i = 0; i < nargs; i++)
7294 if (!args || nonnull_check_p (args, i + 1))
7295 check_function_arguments_recurse (check_nonnull_arg, NULL,
7296 argarray[i],
7297 i + 1);
7303 /* Check that the Nth argument of a function call (counting backwards
7304 from the end) is a (pointer)0. The NARGS arguments are passed in the
7305 array ARGARRAY. */
7307 static void
7308 check_function_sentinel (tree attrs, int nargs, tree *argarray, tree typelist)
7310 tree attr = lookup_attribute ("sentinel", attrs);
7312 if (attr)
7314 int len = 0;
7315 int pos = 0;
7316 tree sentinel;
7318 /* Skip over the named arguments. */
7319 while (typelist && len < nargs)
7321 typelist = TREE_CHAIN (typelist);
7322 len++;
7325 if (TREE_VALUE (attr))
7327 tree p = TREE_VALUE (TREE_VALUE (attr));
7328 pos = TREE_INT_CST_LOW (p);
7331 /* The sentinel must be one of the varargs, i.e.
7332 in position >= the number of fixed arguments. */
7333 if ((nargs - 1 - pos) < len)
7335 warning (OPT_Wformat,
7336 "not enough variable arguments to fit a sentinel");
7337 return;
7340 /* Validate the sentinel. */
7341 sentinel = argarray[nargs - 1 - pos];
7342 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
7343 || !integer_zerop (sentinel))
7344 /* Although __null (in C++) is only an integer we allow it
7345 nevertheless, as we are guaranteed that it's exactly
7346 as wide as a pointer, and we don't want to force
7347 users to cast the NULL they have written there.
7348 We warn with -Wstrict-null-sentinel, though. */
7349 && (warn_strict_null_sentinel || null_node != sentinel))
7350 warning (OPT_Wformat, "missing sentinel in function call");
7354 /* Helper for check_function_nonnull; given a list of operands which
7355 must be non-null in ARGS, determine if operand PARAM_NUM should be
7356 checked. */
7358 static bool
7359 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
7361 unsigned HOST_WIDE_INT arg_num = 0;
7363 for (; args; args = TREE_CHAIN (args))
7365 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
7367 gcc_assert (found);
7369 if (arg_num == param_num)
7370 return true;
7372 return false;
7375 /* Check that the function argument PARAM (which is operand number
7376 PARAM_NUM) is non-null. This is called by check_function_nonnull
7377 via check_function_arguments_recurse. */
7379 static void
7380 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
7381 unsigned HOST_WIDE_INT param_num)
7383 /* Just skip checking the argument if it's not a pointer. This can
7384 happen if the "nonnull" attribute was given without an operand
7385 list (which means to check every pointer argument). */
7387 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
7388 return;
7390 if (integer_zerop (param))
7391 warning (OPT_Wnonnull, "null argument where non-null required "
7392 "(argument %lu)", (unsigned long) param_num);
7395 /* Helper for nonnull attribute handling; fetch the operand number
7396 from the attribute argument list. */
7398 static bool
7399 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
7401 /* Verify the arg number is a constant. */
7402 if (TREE_CODE (arg_num_expr) != INTEGER_CST
7403 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
7404 return false;
7406 *valp = TREE_INT_CST_LOW (arg_num_expr);
7407 return true;
7410 /* Handle a "nothrow" attribute; arguments as in
7411 struct attribute_spec.handler. */
7413 static tree
7414 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7415 int ARG_UNUSED (flags), bool *no_add_attrs)
7417 if (TREE_CODE (*node) == FUNCTION_DECL)
7418 TREE_NOTHROW (*node) = 1;
7419 /* ??? TODO: Support types. */
7420 else
7422 warning (OPT_Wattributes, "%qE attribute ignored", name);
7423 *no_add_attrs = true;
7426 return NULL_TREE;
7429 /* Handle a "cleanup" attribute; arguments as in
7430 struct attribute_spec.handler. */
7432 static tree
7433 handle_cleanup_attribute (tree *node, tree name, tree args,
7434 int ARG_UNUSED (flags), bool *no_add_attrs)
7436 tree decl = *node;
7437 tree cleanup_id, cleanup_decl;
7439 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
7440 for global destructors in C++. This requires infrastructure that
7441 we don't have generically at the moment. It's also not a feature
7442 we'd be missing too much, since we do have attribute constructor. */
7443 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
7445 warning (OPT_Wattributes, "%qE attribute ignored", name);
7446 *no_add_attrs = true;
7447 return NULL_TREE;
7450 /* Verify that the argument is a function in scope. */
7451 /* ??? We could support pointers to functions here as well, if
7452 that was considered desirable. */
7453 cleanup_id = TREE_VALUE (args);
7454 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
7456 error ("cleanup argument not an identifier");
7457 *no_add_attrs = true;
7458 return NULL_TREE;
7460 cleanup_decl = lookup_name (cleanup_id);
7461 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
7463 error ("cleanup argument not a function");
7464 *no_add_attrs = true;
7465 return NULL_TREE;
7468 /* That the function has proper type is checked with the
7469 eventual call to build_function_call. */
7471 return NULL_TREE;
7474 /* Handle a "warn_unused_result" attribute. No special handling. */
7476 static tree
7477 handle_warn_unused_result_attribute (tree *node, tree name,
7478 tree ARG_UNUSED (args),
7479 int ARG_UNUSED (flags), bool *no_add_attrs)
7481 /* Ignore the attribute for functions not returning any value. */
7482 if (VOID_TYPE_P (TREE_TYPE (*node)))
7484 warning (OPT_Wattributes, "%qE attribute ignored", name);
7485 *no_add_attrs = true;
7488 return NULL_TREE;
7491 /* Handle a "sentinel" attribute. */
7493 static tree
7494 handle_sentinel_attribute (tree *node, tree name, tree args,
7495 int ARG_UNUSED (flags), bool *no_add_attrs)
7497 tree params = TYPE_ARG_TYPES (*node);
7499 if (!params)
7501 warning (OPT_Wattributes,
7502 "%qE attribute requires prototypes with named arguments", name);
7503 *no_add_attrs = true;
7505 else
7507 while (TREE_CHAIN (params))
7508 params = TREE_CHAIN (params);
7510 if (VOID_TYPE_P (TREE_VALUE (params)))
7512 warning (OPT_Wattributes,
7513 "%qE attribute only applies to variadic functions", name);
7514 *no_add_attrs = true;
7518 if (args)
7520 tree position = TREE_VALUE (args);
7522 if (TREE_CODE (position) != INTEGER_CST)
7524 warning (OPT_Wattributes,
7525 "requested position is not an integer constant");
7526 *no_add_attrs = true;
7528 else
7530 if (tree_int_cst_lt (position, integer_zero_node))
7532 warning (OPT_Wattributes,
7533 "requested position is less than zero");
7534 *no_add_attrs = true;
7539 return NULL_TREE;
7542 /* Handle a "type_generic" attribute. */
7544 static tree
7545 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
7546 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7547 bool * ARG_UNUSED (no_add_attrs))
7549 tree params;
7551 /* Ensure we have a function type. */
7552 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
7554 params = TYPE_ARG_TYPES (*node);
7555 while (params && ! VOID_TYPE_P (TREE_VALUE (params)))
7556 params = TREE_CHAIN (params);
7558 /* Ensure we have a variadic function. */
7559 gcc_assert (!params);
7561 return NULL_TREE;
7564 /* Handle a "target" attribute. */
7566 static tree
7567 handle_target_attribute (tree *node, tree name, tree args, int flags,
7568 bool *no_add_attrs)
7570 /* Ensure we have a function type. */
7571 if (TREE_CODE (*node) != FUNCTION_DECL)
7573 warning (OPT_Wattributes, "%qE attribute ignored", name);
7574 *no_add_attrs = true;
7576 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
7577 flags))
7578 *no_add_attrs = true;
7580 return NULL_TREE;
7583 /* Arguments being collected for optimization. */
7584 typedef const char *const_char_p; /* For DEF_VEC_P. */
7585 DEF_VEC_P(const_char_p);
7586 DEF_VEC_ALLOC_P(const_char_p, gc);
7587 static GTY(()) VEC(const_char_p, gc) *optimize_args;
7590 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
7591 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
7592 false for #pragma GCC optimize. */
7594 bool
7595 parse_optimize_options (tree args, bool attr_p)
7597 bool ret = true;
7598 unsigned opt_argc;
7599 unsigned i;
7600 int saved_flag_strict_aliasing;
7601 const char **opt_argv;
7602 tree ap;
7604 /* Build up argv vector. Just in case the string is stored away, use garbage
7605 collected strings. */
7606 VEC_truncate (const_char_p, optimize_args, 0);
7607 VEC_safe_push (const_char_p, gc, optimize_args, NULL);
7609 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
7611 tree value = TREE_VALUE (ap);
7613 if (TREE_CODE (value) == INTEGER_CST)
7615 char buffer[20];
7616 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
7617 VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
7620 else if (TREE_CODE (value) == STRING_CST)
7622 /* Split string into multiple substrings. */
7623 size_t len = TREE_STRING_LENGTH (value);
7624 char *p = ASTRDUP (TREE_STRING_POINTER (value));
7625 char *end = p + len;
7626 char *comma;
7627 char *next_p = p;
7629 while (next_p != NULL)
7631 size_t len2;
7632 char *q, *r;
7634 p = next_p;
7635 comma = strchr (p, ',');
7636 if (comma)
7638 len2 = comma - p;
7639 *comma = '\0';
7640 next_p = comma+1;
7642 else
7644 len2 = end - p;
7645 next_p = NULL;
7648 r = q = (char *) ggc_alloc (len2 + 3);
7650 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
7651 options. */
7652 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
7654 ret = false;
7655 if (attr_p)
7656 warning (OPT_Wattributes,
7657 "Bad option %s to optimize attribute.", p);
7658 else
7659 warning (OPT_Wpragmas,
7660 "Bad option %s to pragma attribute", p);
7661 continue;
7664 if (*p != '-')
7666 *r++ = '-';
7668 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
7669 itself is -Os, and any other switch begins with a -f. */
7670 if ((*p >= '0' && *p <= '9')
7671 || (p[0] == 's' && p[1] == '\0'))
7672 *r++ = 'O';
7673 else if (*p != 'O')
7674 *r++ = 'f';
7677 memcpy (r, p, len2);
7678 r[len2] = '\0';
7679 VEC_safe_push (const_char_p, gc, optimize_args, q);
7685 opt_argc = VEC_length (const_char_p, optimize_args);
7686 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
7688 for (i = 1; i < opt_argc; i++)
7689 opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
7691 saved_flag_strict_aliasing = flag_strict_aliasing;
7693 /* Now parse the options. */
7694 decode_options (opt_argc, opt_argv);
7696 /* Don't allow changing -fstrict-aliasing. */
7697 flag_strict_aliasing = saved_flag_strict_aliasing;
7699 VEC_truncate (const_char_p, optimize_args, 0);
7700 return ret;
7703 /* For handling "optimize" attribute. arguments as in
7704 struct attribute_spec.handler. */
7706 static tree
7707 handle_optimize_attribute (tree *node, tree name, tree args,
7708 int ARG_UNUSED (flags), bool *no_add_attrs)
7710 /* Ensure we have a function type. */
7711 if (TREE_CODE (*node) != FUNCTION_DECL)
7713 warning (OPT_Wattributes, "%qE attribute ignored", name);
7714 *no_add_attrs = true;
7716 else
7718 struct cl_optimization cur_opts;
7719 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
7721 /* Save current options. */
7722 cl_optimization_save (&cur_opts);
7724 /* If we previously had some optimization options, use them as the
7725 default. */
7726 if (old_opts)
7727 cl_optimization_restore (TREE_OPTIMIZATION (old_opts));
7729 /* Parse options, and update the vector. */
7730 parse_optimize_options (args, true);
7731 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
7732 = build_optimization_node ();
7734 /* Restore current options. */
7735 cl_optimization_restore (&cur_opts);
7738 return NULL_TREE;
7741 /* Check for valid arguments being passed to a function.
7742 ATTRS is a list of attributes. There are NARGS arguments in the array
7743 ARGARRAY. TYPELIST is the list of argument types for the function.
7745 void
7746 check_function_arguments (tree attrs, int nargs, tree *argarray, tree typelist)
7748 /* Check for null being passed in a pointer argument that must be
7749 non-null. We also need to do this if format checking is enabled. */
7751 if (warn_nonnull)
7752 check_function_nonnull (attrs, nargs, argarray);
7754 /* Check for errors in format strings. */
7756 if (warn_format || warn_missing_format_attribute)
7757 check_function_format (attrs, nargs, argarray);
7759 if (warn_format)
7760 check_function_sentinel (attrs, nargs, argarray, typelist);
7763 /* Generic argument checking recursion routine. PARAM is the argument to
7764 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
7765 once the argument is resolved. CTX is context for the callback. */
7766 void
7767 check_function_arguments_recurse (void (*callback)
7768 (void *, tree, unsigned HOST_WIDE_INT),
7769 void *ctx, tree param,
7770 unsigned HOST_WIDE_INT param_num)
7772 if (CONVERT_EXPR_P (param)
7773 && (TYPE_PRECISION (TREE_TYPE (param))
7774 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
7776 /* Strip coercion. */
7777 check_function_arguments_recurse (callback, ctx,
7778 TREE_OPERAND (param, 0), param_num);
7779 return;
7782 if (TREE_CODE (param) == CALL_EXPR)
7784 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
7785 tree attrs;
7786 bool found_format_arg = false;
7788 /* See if this is a call to a known internationalization function
7789 that modifies a format arg. Such a function may have multiple
7790 format_arg attributes (for example, ngettext). */
7792 for (attrs = TYPE_ATTRIBUTES (type);
7793 attrs;
7794 attrs = TREE_CHAIN (attrs))
7795 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
7797 tree inner_arg;
7798 tree format_num_expr;
7799 int format_num;
7800 int i;
7801 call_expr_arg_iterator iter;
7803 /* Extract the argument number, which was previously checked
7804 to be valid. */
7805 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
7807 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
7808 && !TREE_INT_CST_HIGH (format_num_expr));
7810 format_num = TREE_INT_CST_LOW (format_num_expr);
7812 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
7813 inner_arg != 0;
7814 inner_arg = next_call_expr_arg (&iter), i++)
7815 if (i == format_num)
7817 check_function_arguments_recurse (callback, ctx,
7818 inner_arg, param_num);
7819 found_format_arg = true;
7820 break;
7824 /* If we found a format_arg attribute and did a recursive check,
7825 we are done with checking this argument. Otherwise, we continue
7826 and this will be considered a non-literal. */
7827 if (found_format_arg)
7828 return;
7831 if (TREE_CODE (param) == COND_EXPR)
7833 /* Check both halves of the conditional expression. */
7834 check_function_arguments_recurse (callback, ctx,
7835 TREE_OPERAND (param, 1), param_num);
7836 check_function_arguments_recurse (callback, ctx,
7837 TREE_OPERAND (param, 2), param_num);
7838 return;
7841 (*callback) (ctx, param, param_num);
7844 /* Checks the number of arguments NARGS against the required number
7845 REQUIRED and issues an error if there is a mismatch. Returns true
7846 if the number of arguments is correct, otherwise false. */
7848 static bool
7849 validate_nargs (tree fndecl, int nargs, int required)
7851 if (nargs < required)
7853 error ("not enough arguments to function %qE", fndecl);
7854 return false;
7856 else if (nargs > required)
7858 error ("too many arguments to function %qE", fndecl);
7859 return false;
7861 return true;
7864 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
7865 Returns false if there was an error, otherwise true. */
7867 bool
7868 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
7870 if (!DECL_BUILT_IN (fndecl)
7871 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
7872 return true;
7874 switch (DECL_FUNCTION_CODE (fndecl))
7876 case BUILT_IN_CONSTANT_P:
7877 return validate_nargs (fndecl, nargs, 1);
7879 case BUILT_IN_ISFINITE:
7880 case BUILT_IN_ISINF:
7881 case BUILT_IN_ISINF_SIGN:
7882 case BUILT_IN_ISNAN:
7883 case BUILT_IN_ISNORMAL:
7884 if (validate_nargs (fndecl, nargs, 1))
7886 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
7888 error ("non-floating-point argument in call to "
7889 "function %qE", fndecl);
7890 return false;
7892 return true;
7894 return false;
7896 case BUILT_IN_ISGREATER:
7897 case BUILT_IN_ISGREATEREQUAL:
7898 case BUILT_IN_ISLESS:
7899 case BUILT_IN_ISLESSEQUAL:
7900 case BUILT_IN_ISLESSGREATER:
7901 case BUILT_IN_ISUNORDERED:
7902 if (validate_nargs (fndecl, nargs, 2))
7904 enum tree_code code0, code1;
7905 code0 = TREE_CODE (TREE_TYPE (args[0]));
7906 code1 = TREE_CODE (TREE_TYPE (args[1]));
7907 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
7908 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
7909 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
7911 error ("non-floating-point arguments in call to "
7912 "function %qE", fndecl);
7913 return false;
7915 return true;
7917 return false;
7919 case BUILT_IN_FPCLASSIFY:
7920 if (validate_nargs (fndecl, nargs, 6))
7922 unsigned i;
7924 for (i=0; i<5; i++)
7925 if (TREE_CODE (args[i]) != INTEGER_CST)
7927 error ("non-const integer argument %u in call to function %qE",
7928 i+1, fndecl);
7929 return false;
7932 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
7934 error ("non-floating-point argument in call to function %qE",
7935 fndecl);
7936 return false;
7938 return true;
7940 return false;
7942 default:
7943 return true;
7947 /* Function to help qsort sort FIELD_DECLs by name order. */
7950 field_decl_cmp (const void *x_p, const void *y_p)
7952 const tree *const x = (const tree *const) x_p;
7953 const tree *const y = (const tree *const) y_p;
7955 if (DECL_NAME (*x) == DECL_NAME (*y))
7956 /* A nontype is "greater" than a type. */
7957 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
7958 if (DECL_NAME (*x) == NULL_TREE)
7959 return -1;
7960 if (DECL_NAME (*y) == NULL_TREE)
7961 return 1;
7962 if (DECL_NAME (*x) < DECL_NAME (*y))
7963 return -1;
7964 return 1;
7967 static struct {
7968 gt_pointer_operator new_value;
7969 void *cookie;
7970 } resort_data;
7972 /* This routine compares two fields like field_decl_cmp but using the
7973 pointer operator in resort_data. */
7975 static int
7976 resort_field_decl_cmp (const void *x_p, const void *y_p)
7978 const tree *const x = (const tree *const) x_p;
7979 const tree *const y = (const tree *const) y_p;
7981 if (DECL_NAME (*x) == DECL_NAME (*y))
7982 /* A nontype is "greater" than a type. */
7983 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
7984 if (DECL_NAME (*x) == NULL_TREE)
7985 return -1;
7986 if (DECL_NAME (*y) == NULL_TREE)
7987 return 1;
7989 tree d1 = DECL_NAME (*x);
7990 tree d2 = DECL_NAME (*y);
7991 resort_data.new_value (&d1, resort_data.cookie);
7992 resort_data.new_value (&d2, resort_data.cookie);
7993 if (d1 < d2)
7994 return -1;
7996 return 1;
7999 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8001 void
8002 resort_sorted_fields (void *obj,
8003 void * ARG_UNUSED (orig_obj),
8004 gt_pointer_operator new_value,
8005 void *cookie)
8007 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8008 resort_data.new_value = new_value;
8009 resort_data.cookie = cookie;
8010 qsort (&sf->elts[0], sf->len, sizeof (tree),
8011 resort_field_decl_cmp);
8014 /* Subroutine of c_parse_error.
8015 Return the result of concatenating LHS and RHS. RHS is really
8016 a string literal, its first character is indicated by RHS_START and
8017 RHS_SIZE is its length (including the terminating NUL character).
8019 The caller is responsible for deleting the returned pointer. */
8021 static char *
8022 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
8024 const int lhs_size = strlen (lhs);
8025 char *result = XNEWVEC (char, lhs_size + rhs_size);
8026 strncpy (result, lhs, lhs_size);
8027 strncpy (result + lhs_size, rhs_start, rhs_size);
8028 return result;
8031 /* Issue the error given by GMSGID, indicating that it occurred before
8032 TOKEN, which had the associated VALUE. */
8034 void
8035 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
8036 tree value, unsigned char token_flags)
8038 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
8040 char *message = NULL;
8042 if (token_type == CPP_EOF)
8043 message = catenate_messages (gmsgid, " at end of input");
8044 else if (token_type == CPP_CHAR
8045 || token_type == CPP_WCHAR
8046 || token_type == CPP_CHAR16
8047 || token_type == CPP_CHAR32)
8049 unsigned int val = TREE_INT_CST_LOW (value);
8050 const char *prefix;
8052 switch (token_type)
8054 default:
8055 prefix = "";
8056 break;
8057 case CPP_WCHAR:
8058 prefix = "L";
8059 break;
8060 case CPP_CHAR16:
8061 prefix = "u";
8062 break;
8063 case CPP_CHAR32:
8064 prefix = "U";
8065 break;
8068 if (val <= UCHAR_MAX && ISGRAPH (val))
8069 message = catenate_messages (gmsgid, " before %s'%c'");
8070 else
8071 message = catenate_messages (gmsgid, " before %s'\\x%x'");
8073 error (message, prefix, val);
8074 free (message);
8075 message = NULL;
8077 else if (token_type == CPP_STRING
8078 || token_type == CPP_WSTRING
8079 || token_type == CPP_STRING16
8080 || token_type == CPP_STRING32)
8081 message = catenate_messages (gmsgid, " before string constant");
8082 else if (token_type == CPP_NUMBER)
8083 message = catenate_messages (gmsgid, " before numeric constant");
8084 else if (token_type == CPP_NAME)
8086 message = catenate_messages (gmsgid, " before %qE");
8087 error (message, value);
8088 free (message);
8089 message = NULL;
8091 else if (token_type == CPP_PRAGMA)
8092 message = catenate_messages (gmsgid, " before %<#pragma%>");
8093 else if (token_type == CPP_PRAGMA_EOL)
8094 message = catenate_messages (gmsgid, " before end of line");
8095 else if (token_type < N_TTYPES)
8097 message = catenate_messages (gmsgid, " before %qs token");
8098 error (message, cpp_type2name (token_type, token_flags));
8099 free (message);
8100 message = NULL;
8102 else
8103 error (gmsgid);
8105 if (message)
8107 error (message);
8108 free (message);
8110 #undef catenate_messages
8113 /* Callback from cpp_error for PFILE to print diagnostics from the
8114 preprocessor. The diagnostic is of type LEVEL, at location
8115 LOCATION unless this is after lexing and the compiler's location
8116 should be used instead, with column number possibly overridden by
8117 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
8118 the arguments. Returns true if a diagnostic was emitted, false
8119 otherwise. */
8121 bool
8122 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level,
8123 location_t location, unsigned int column_override,
8124 const char *msg, va_list *ap)
8126 diagnostic_info diagnostic;
8127 diagnostic_t dlevel;
8128 int save_warn_system_headers = warn_system_headers;
8129 bool ret;
8131 switch (level)
8133 case CPP_DL_WARNING_SYSHDR:
8134 if (flag_no_output)
8135 return false;
8136 warn_system_headers = 1;
8137 /* Fall through. */
8138 case CPP_DL_WARNING:
8139 if (flag_no_output)
8140 return false;
8141 dlevel = DK_WARNING;
8142 break;
8143 case CPP_DL_PEDWARN:
8144 if (flag_no_output && !flag_pedantic_errors)
8145 return false;
8146 dlevel = DK_PEDWARN;
8147 break;
8148 case CPP_DL_ERROR:
8149 dlevel = DK_ERROR;
8150 break;
8151 case CPP_DL_ICE:
8152 dlevel = DK_ICE;
8153 break;
8154 case CPP_DL_NOTE:
8155 dlevel = DK_NOTE;
8156 break;
8157 case CPP_DL_FATAL:
8158 dlevel = DK_FATAL;
8159 break;
8160 default:
8161 gcc_unreachable ();
8163 if (done_lexing)
8164 location = input_location;
8165 diagnostic_set_info_translated (&diagnostic, msg, ap,
8166 location, dlevel);
8167 if (column_override)
8168 diagnostic_override_column (&diagnostic, column_override);
8169 ret = report_diagnostic (&diagnostic);
8170 if (level == CPP_DL_WARNING_SYSHDR)
8171 warn_system_headers = save_warn_system_headers;
8172 return ret;
8175 /* Walk a gimplified function and warn for functions whose return value is
8176 ignored and attribute((warn_unused_result)) is set. This is done before
8177 inlining, so we don't have to worry about that. */
8179 void
8180 c_warn_unused_result (gimple_seq seq)
8182 tree fdecl, ftype;
8183 gimple_stmt_iterator i;
8185 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
8187 gimple g = gsi_stmt (i);
8189 switch (gimple_code (g))
8191 case GIMPLE_BIND:
8192 c_warn_unused_result (gimple_bind_body (g));
8193 break;
8194 case GIMPLE_TRY:
8195 c_warn_unused_result (gimple_try_eval (g));
8196 c_warn_unused_result (gimple_try_cleanup (g));
8197 break;
8198 case GIMPLE_CATCH:
8199 c_warn_unused_result (gimple_catch_handler (g));
8200 break;
8201 case GIMPLE_EH_FILTER:
8202 c_warn_unused_result (gimple_eh_filter_failure (g));
8203 break;
8205 case GIMPLE_CALL:
8206 if (gimple_call_lhs (g))
8207 break;
8209 /* This is a naked call, as opposed to a GIMPLE_CALL with an
8210 LHS. All calls whose value is ignored should be
8211 represented like this. Look for the attribute. */
8212 fdecl = gimple_call_fndecl (g);
8213 ftype = TREE_TYPE (TREE_TYPE (gimple_call_fn (g)));
8215 if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
8217 location_t loc = gimple_location (g);
8219 if (fdecl)
8220 warning (0, "%Hignoring return value of %qD, "
8221 "declared with attribute warn_unused_result",
8222 &loc, fdecl);
8223 else
8224 warning (0, "%Hignoring return value of function "
8225 "declared with attribute warn_unused_result",
8226 &loc);
8228 break;
8230 default:
8231 /* Not a container, not a call, or a call whose value is used. */
8232 break;
8237 /* Convert a character from the host to the target execution character
8238 set. cpplib handles this, mostly. */
8240 HOST_WIDE_INT
8241 c_common_to_target_charset (HOST_WIDE_INT c)
8243 /* Character constants in GCC proper are sign-extended under -fsigned-char,
8244 zero-extended under -fno-signed-char. cpplib insists that characters
8245 and character constants are always unsigned. Hence we must convert
8246 back and forth. */
8247 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
8249 uc = cpp_host_to_exec_charset (parse_in, uc);
8251 if (flag_signed_char)
8252 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
8253 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
8254 else
8255 return uc;
8258 /* Build the result of __builtin_offsetof. EXPR is a nested sequence of
8259 component references, with STOP_REF, or alternatively an INDIRECT_REF of
8260 NULL, at the bottom; much like the traditional rendering of offsetof as a
8261 macro. Returns the folded and properly cast result. */
8263 static tree
8264 fold_offsetof_1 (tree expr, tree stop_ref)
8266 enum tree_code code = PLUS_EXPR;
8267 tree base, off, t;
8269 if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK)
8270 return size_zero_node;
8272 switch (TREE_CODE (expr))
8274 case ERROR_MARK:
8275 return expr;
8277 case VAR_DECL:
8278 error ("cannot apply %<offsetof%> to static data member %qD", expr);
8279 return error_mark_node;
8281 case CALL_EXPR:
8282 case TARGET_EXPR:
8283 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
8284 return error_mark_node;
8286 case INTEGER_CST:
8287 gcc_assert (integer_zerop (expr));
8288 return size_zero_node;
8290 case NOP_EXPR:
8291 case INDIRECT_REF:
8292 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
8293 gcc_assert (base == error_mark_node || base == size_zero_node);
8294 return base;
8296 case COMPONENT_REF:
8297 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
8298 if (base == error_mark_node)
8299 return base;
8301 t = TREE_OPERAND (expr, 1);
8302 if (DECL_C_BIT_FIELD (t))
8304 error ("attempt to take address of bit-field structure "
8305 "member %qD", t);
8306 return error_mark_node;
8308 off = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (t),
8309 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t), 1)
8310 / BITS_PER_UNIT));
8311 break;
8313 case ARRAY_REF:
8314 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
8315 if (base == error_mark_node)
8316 return base;
8318 t = TREE_OPERAND (expr, 1);
8319 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0)
8321 code = MINUS_EXPR;
8322 t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
8324 t = convert (sizetype, t);
8325 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
8326 break;
8328 case COMPOUND_EXPR:
8329 /* Handle static members of volatile structs. */
8330 t = TREE_OPERAND (expr, 1);
8331 gcc_assert (TREE_CODE (t) == VAR_DECL);
8332 return fold_offsetof_1 (t, stop_ref);
8334 default:
8335 gcc_unreachable ();
8338 return size_binop (code, base, off);
8341 tree
8342 fold_offsetof (tree expr, tree stop_ref)
8344 /* Convert back from the internal sizetype to size_t. */
8345 return convert (size_type_node, fold_offsetof_1 (expr, stop_ref));
8348 /* Print an error message for an invalid lvalue. USE says
8349 how the lvalue is being used and so selects the error message. */
8351 void
8352 lvalue_error (enum lvalue_use use)
8354 switch (use)
8356 case lv_assign:
8357 error ("lvalue required as left operand of assignment");
8358 break;
8359 case lv_increment:
8360 error ("lvalue required as increment operand");
8361 break;
8362 case lv_decrement:
8363 error ("lvalue required as decrement operand");
8364 break;
8365 case lv_addressof:
8366 error ("lvalue required as unary %<&%> operand");
8367 break;
8368 case lv_asm:
8369 error ("lvalue required in asm statement");
8370 break;
8371 default:
8372 gcc_unreachable ();
8376 /* *PTYPE is an incomplete array. Complete it with a domain based on
8377 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
8378 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8379 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
8382 complete_array_type (tree *ptype, tree initial_value, bool do_default)
8384 tree maxindex, type, main_type, elt, unqual_elt;
8385 int failure = 0, quals;
8386 hashval_t hashcode = 0;
8388 maxindex = size_zero_node;
8389 if (initial_value)
8391 if (TREE_CODE (initial_value) == STRING_CST)
8393 int eltsize
8394 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8395 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
8397 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8399 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
8401 if (VEC_empty (constructor_elt, v))
8403 if (pedantic)
8404 failure = 3;
8405 maxindex = integer_minus_one_node;
8407 else
8409 tree curindex;
8410 unsigned HOST_WIDE_INT cnt;
8411 constructor_elt *ce;
8412 bool fold_p = false;
8414 if (VEC_index (constructor_elt, v, 0)->index)
8415 maxindex = fold_convert (sizetype,
8416 VEC_index (constructor_elt,
8417 v, 0)->index);
8418 curindex = maxindex;
8420 for (cnt = 1;
8421 VEC_iterate (constructor_elt, v, cnt, ce);
8422 cnt++)
8424 bool curfold_p = false;
8425 if (ce->index)
8426 curindex = ce->index, curfold_p = true;
8427 else
8429 if (fold_p)
8430 curindex = fold_convert (sizetype, curindex);
8431 curindex = size_binop (PLUS_EXPR, curindex, size_one_node);
8433 if (tree_int_cst_lt (maxindex, curindex))
8434 maxindex = curindex, fold_p = curfold_p;
8436 if (fold_p)
8437 maxindex = fold_convert (sizetype, maxindex);
8440 else
8442 /* Make an error message unless that happened already. */
8443 if (initial_value != error_mark_node)
8444 failure = 1;
8447 else
8449 failure = 2;
8450 if (!do_default)
8451 return failure;
8454 type = *ptype;
8455 elt = TREE_TYPE (type);
8456 quals = TYPE_QUALS (strip_array_types (elt));
8457 if (quals == 0)
8458 unqual_elt = elt;
8459 else
8460 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
8462 /* Using build_distinct_type_copy and modifying things afterward instead
8463 of using build_array_type to create a new type preserves all of the
8464 TYPE_LANG_FLAG_? bits that the front end may have set. */
8465 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8466 TREE_TYPE (main_type) = unqual_elt;
8467 TYPE_DOMAIN (main_type) = build_index_type (maxindex);
8468 layout_type (main_type);
8470 /* Make sure we have the canonical MAIN_TYPE. */
8471 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
8472 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
8473 hashcode);
8474 main_type = type_hash_canon (hashcode, main_type);
8476 /* Fix the canonical type. */
8477 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
8478 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
8479 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
8480 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
8481 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
8482 != TYPE_DOMAIN (main_type)))
8483 TYPE_CANONICAL (main_type)
8484 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
8485 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
8486 else
8487 TYPE_CANONICAL (main_type) = main_type;
8489 if (quals == 0)
8490 type = main_type;
8491 else
8492 type = c_build_qualified_type (main_type, quals);
8494 if (COMPLETE_TYPE_P (type)
8495 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8496 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
8498 error ("size of array is too large");
8499 /* If we proceed with the array type as it is, we'll eventually
8500 crash in tree_low_cst(). */
8501 type = error_mark_node;
8504 *ptype = type;
8505 return failure;
8509 /* Used to help initialize the builtin-types.def table. When a type of
8510 the correct size doesn't exist, use error_mark_node instead of NULL.
8511 The later results in segfaults even when a decl using the type doesn't
8512 get invoked. */
8514 tree
8515 builtin_type_for_size (int size, bool unsignedp)
8517 tree type = lang_hooks.types.type_for_size (size, unsignedp);
8518 return type ? type : error_mark_node;
8521 /* A helper function for resolve_overloaded_builtin in resolving the
8522 overloaded __sync_ builtins. Returns a positive power of 2 if the
8523 first operand of PARAMS is a pointer to a supported data type.
8524 Returns 0 if an error is encountered. */
8526 static int
8527 sync_resolve_size (tree function, VEC(tree,gc) *params)
8529 tree type;
8530 int size;
8532 if (VEC_empty (tree, params))
8534 error ("too few arguments to function %qE", function);
8535 return 0;
8538 type = TREE_TYPE (VEC_index (tree, params, 0));
8539 if (TREE_CODE (type) != POINTER_TYPE)
8540 goto incompatible;
8542 type = TREE_TYPE (type);
8543 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8544 goto incompatible;
8546 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8547 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
8548 return size;
8550 incompatible:
8551 error ("incompatible type for argument %d of %qE", 1, function);
8552 return 0;
8555 /* A helper function for resolve_overloaded_builtin. Adds casts to
8556 PARAMS to make arguments match up with those of FUNCTION. Drops
8557 the variadic arguments at the end. Returns false if some error
8558 was encountered; true on success. */
8560 static bool
8561 sync_resolve_params (tree orig_function, tree function, VEC(tree, gc) *params)
8563 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
8564 tree ptype;
8565 int number;
8566 unsigned int parmnum;
8568 /* We've declared the implementation functions to use "volatile void *"
8569 as the pointer parameter, so we shouldn't get any complaints from the
8570 call to check_function_arguments what ever type the user used. */
8571 arg_types = TREE_CHAIN (arg_types);
8572 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
8573 number = 2;
8575 /* For the rest of the values, we need to cast these to FTYPE, so that we
8576 don't get warnings for passing pointer types, etc. */
8577 parmnum = 0;
8578 while (arg_types != void_list_node)
8580 tree val;
8582 ++parmnum;
8583 if (VEC_length (tree, params) <= parmnum)
8585 error ("too few arguments to function %qE", orig_function);
8586 return false;
8589 /* ??? Ideally for the first conversion we'd use convert_for_assignment
8590 so that we get warnings for anything that doesn't match the pointer
8591 type. This isn't portable across the C and C++ front ends atm. */
8592 val = VEC_index (tree, params, parmnum);
8593 val = convert (ptype, val);
8594 val = convert (TREE_VALUE (arg_types), val);
8595 VEC_replace (tree, params, parmnum, val);
8597 arg_types = TREE_CHAIN (arg_types);
8598 number++;
8601 /* The definition of these primitives is variadic, with the remaining
8602 being "an optional list of variables protected by the memory barrier".
8603 No clue what that's supposed to mean, precisely, but we consider all
8604 call-clobbered variables to be protected so we're safe. */
8605 VEC_truncate (tree, params, parmnum + 1);
8607 return true;
8610 /* A helper function for resolve_overloaded_builtin. Adds a cast to
8611 RESULT to make it match the type of the first pointer argument in
8612 PARAMS. */
8614 static tree
8615 sync_resolve_return (tree first_param, tree result)
8617 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
8618 ptype = TYPE_MAIN_VARIANT (ptype);
8619 return convert (ptype, result);
8622 /* Some builtin functions are placeholders for other expressions. This
8623 function should be called immediately after parsing the call expression
8624 before surrounding code has committed to the type of the expression.
8626 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
8627 PARAMS is the argument list for the call. The return value is non-null
8628 when expansion is complete, and null if normal processing should
8629 continue. */
8631 tree
8632 resolve_overloaded_builtin (tree function, VEC(tree,gc) *params)
8634 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
8635 switch (DECL_BUILT_IN_CLASS (function))
8637 case BUILT_IN_NORMAL:
8638 break;
8639 case BUILT_IN_MD:
8640 if (targetm.resolve_overloaded_builtin)
8641 return targetm.resolve_overloaded_builtin (function, params);
8642 else
8643 return NULL_TREE;
8644 default:
8645 return NULL_TREE;
8648 /* Handle BUILT_IN_NORMAL here. */
8649 switch (orig_code)
8651 case BUILT_IN_FETCH_AND_ADD_N:
8652 case BUILT_IN_FETCH_AND_SUB_N:
8653 case BUILT_IN_FETCH_AND_OR_N:
8654 case BUILT_IN_FETCH_AND_AND_N:
8655 case BUILT_IN_FETCH_AND_XOR_N:
8656 case BUILT_IN_FETCH_AND_NAND_N:
8657 case BUILT_IN_ADD_AND_FETCH_N:
8658 case BUILT_IN_SUB_AND_FETCH_N:
8659 case BUILT_IN_OR_AND_FETCH_N:
8660 case BUILT_IN_AND_AND_FETCH_N:
8661 case BUILT_IN_XOR_AND_FETCH_N:
8662 case BUILT_IN_NAND_AND_FETCH_N:
8663 case BUILT_IN_BOOL_COMPARE_AND_SWAP_N:
8664 case BUILT_IN_VAL_COMPARE_AND_SWAP_N:
8665 case BUILT_IN_LOCK_TEST_AND_SET_N:
8666 case BUILT_IN_LOCK_RELEASE_N:
8668 int n = sync_resolve_size (function, params);
8669 tree new_function, first_param, result;
8671 if (n == 0)
8672 return error_mark_node;
8674 new_function = built_in_decls[orig_code + exact_log2 (n) + 1];
8675 if (!sync_resolve_params (function, new_function, params))
8676 return error_mark_node;
8678 first_param = VEC_index (tree, params, 0);
8679 result = build_function_call_vec (new_function, params, NULL);
8680 if (orig_code != BUILT_IN_BOOL_COMPARE_AND_SWAP_N
8681 && orig_code != BUILT_IN_LOCK_RELEASE_N)
8682 result = sync_resolve_return (first_param, result);
8684 return result;
8687 default:
8688 return NULL_TREE;
8692 /* Ignoring their sign, return true if two scalar types are the same. */
8693 bool
8694 same_scalar_type_ignoring_signedness (tree t1, tree t2)
8696 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
8698 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
8699 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
8700 || c2 == FIXED_POINT_TYPE));
8702 /* Equality works here because c_common_signed_type uses
8703 TYPE_MAIN_VARIANT. */
8704 return c_common_signed_type (t1)
8705 == c_common_signed_type (t2);
8708 /* Check for missing format attributes on function pointers. LTYPE is
8709 the new type or left-hand side type. RTYPE is the old type or
8710 right-hand side type. Returns TRUE if LTYPE is missing the desired
8711 attribute. */
8713 bool
8714 check_missing_format_attribute (tree ltype, tree rtype)
8716 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
8717 tree ra;
8719 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
8720 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
8721 break;
8722 if (ra)
8724 tree la;
8725 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
8726 if (is_attribute_p ("format", TREE_PURPOSE (la)))
8727 break;
8728 return !la;
8730 else
8731 return false;
8734 /* Subscripting with type char is likely to lose on a machine where
8735 chars are signed. So warn on any machine, but optionally. Don't
8736 warn for unsigned char since that type is safe. Don't warn for
8737 signed char because anyone who uses that must have done so
8738 deliberately. Furthermore, we reduce the false positive load by
8739 warning only for non-constant value of type char. */
8741 void
8742 warn_array_subscript_with_type_char (tree index)
8744 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
8745 && TREE_CODE (index) != INTEGER_CST)
8746 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
8749 /* Implement -Wparentheses for the unexpected C precedence rules, to
8750 cover cases like x + y << z which readers are likely to
8751 misinterpret. We have seen an expression in which CODE is a binary
8752 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
8753 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
8754 CODE_RIGHT may be ERROR_MARK, which means that that side of the
8755 expression was not formed using a binary or unary operator, or it
8756 was enclosed in parentheses. */
8758 void
8759 warn_about_parentheses (enum tree_code code,
8760 enum tree_code code_left, tree arg_left,
8761 enum tree_code code_right, tree arg_right)
8763 if (!warn_parentheses)
8764 return;
8766 /* This macro tests that the expression ARG with original tree code
8767 CODE appears to be a boolean expression. or the result of folding a
8768 boolean expression. */
8769 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
8770 (truth_value_p (TREE_CODE (ARG)) \
8771 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
8772 /* Folding may create 0 or 1 integers from other expressions. */ \
8773 || ((CODE) != INTEGER_CST \
8774 && (integer_onep (ARG) || integer_zerop (ARG))))
8776 switch (code)
8778 case LSHIFT_EXPR:
8779 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
8780 warning (OPT_Wparentheses,
8781 "suggest parentheses around %<+%> inside %<<<%>");
8782 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
8783 warning (OPT_Wparentheses,
8784 "suggest parentheses around %<-%> inside %<<<%>");
8785 return;
8787 case RSHIFT_EXPR:
8788 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
8789 warning (OPT_Wparentheses,
8790 "suggest parentheses around %<+%> inside %<>>%>");
8791 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
8792 warning (OPT_Wparentheses,
8793 "suggest parentheses around %<-%> inside %<>>%>");
8794 return;
8796 case TRUTH_ORIF_EXPR:
8797 if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
8798 warning (OPT_Wparentheses,
8799 "suggest parentheses around %<&&%> within %<||%>");
8800 return;
8802 case BIT_IOR_EXPR:
8803 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
8804 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
8805 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
8806 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
8807 warning (OPT_Wparentheses,
8808 "suggest parentheses around arithmetic in operand of %<|%>");
8809 /* Check cases like x|y==z */
8810 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
8811 || TREE_CODE_CLASS (code_right) == tcc_comparison)
8812 warning (OPT_Wparentheses,
8813 "suggest parentheses around comparison in operand of %<|%>");
8814 /* Check cases like !x | y */
8815 else if (code_left == TRUTH_NOT_EXPR
8816 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
8817 warning (OPT_Wparentheses, "suggest parentheses around operand of "
8818 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
8819 return;
8821 case BIT_XOR_EXPR:
8822 if (code_left == BIT_AND_EXPR
8823 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
8824 || code_right == BIT_AND_EXPR
8825 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
8826 warning (OPT_Wparentheses,
8827 "suggest parentheses around arithmetic in operand of %<^%>");
8828 /* Check cases like x^y==z */
8829 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
8830 || TREE_CODE_CLASS (code_right) == tcc_comparison)
8831 warning (OPT_Wparentheses,
8832 "suggest parentheses around comparison in operand of %<^%>");
8833 return;
8835 case BIT_AND_EXPR:
8836 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
8837 warning (OPT_Wparentheses,
8838 "suggest parentheses around %<+%> in operand of %<&%>");
8839 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
8840 warning (OPT_Wparentheses,
8841 "suggest parentheses around %<-%> in operand of %<&%>");
8842 /* Check cases like x&y==z */
8843 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
8844 || TREE_CODE_CLASS (code_right) == tcc_comparison)
8845 warning (OPT_Wparentheses,
8846 "suggest parentheses around comparison in operand of %<&%>");
8847 /* Check cases like !x & y */
8848 else if (code_left == TRUTH_NOT_EXPR
8849 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
8850 warning (OPT_Wparentheses, "suggest parentheses around operand of "
8851 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
8852 return;
8854 case EQ_EXPR:
8855 if (TREE_CODE_CLASS (code_left) == tcc_comparison
8856 || TREE_CODE_CLASS (code_right) == tcc_comparison)
8857 warning (OPT_Wparentheses,
8858 "suggest parentheses around comparison in operand of %<==%>");
8859 return;
8860 case NE_EXPR:
8861 if (TREE_CODE_CLASS (code_left) == tcc_comparison
8862 || TREE_CODE_CLASS (code_right) == tcc_comparison)
8863 warning (OPT_Wparentheses,
8864 "suggest parentheses around comparison in operand of %<!=%>");
8865 return;
8867 default:
8868 if (TREE_CODE_CLASS (code) == tcc_comparison
8869 && ((TREE_CODE_CLASS (code_left) == tcc_comparison
8870 && code_left != NE_EXPR && code_left != EQ_EXPR
8871 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
8872 || (TREE_CODE_CLASS (code_right) == tcc_comparison
8873 && code_right != NE_EXPR && code_right != EQ_EXPR
8874 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
8875 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
8876 "have their mathematical meaning");
8877 return;
8879 #undef NOT_A_BOOLEAN_EXPR_P
8882 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
8884 void
8885 warn_for_unused_label (tree label)
8887 if (!TREE_USED (label))
8889 if (DECL_INITIAL (label))
8890 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
8891 else
8892 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
8896 #ifndef TARGET_HAS_TARGETCM
8897 struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;
8898 #endif
8900 /* Warn for division by zero according to the value of DIVISOR. LOC
8901 is the location of the division operator. */
8903 void
8904 warn_for_div_by_zero (location_t loc, tree divisor)
8906 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
8907 about division by zero. Do not issue a warning if DIVISOR has a
8908 floating-point type, since we consider 0.0/0.0 a valid way of
8909 generating a NaN. */
8910 if (skip_evaluation == 0
8911 && (integer_zerop (divisor) || fixed_zerop (divisor)))
8912 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
8915 /* Subroutine of build_binary_op. Give warnings for comparisons
8916 between signed and unsigned quantities that may fail. Do the
8917 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
8918 so that casts will be considered, but default promotions won't
8921 LOCATION is the location of the comparison operator.
8923 The arguments of this function map directly to local variables
8924 of build_binary_op. */
8926 void
8927 warn_for_sign_compare (location_t location,
8928 tree orig_op0, tree orig_op1,
8929 tree op0, tree op1,
8930 tree result_type, enum tree_code resultcode)
8932 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8933 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8934 int unsignedp0, unsignedp1;
8936 /* In C++, check for comparison of different enum types. */
8937 if (c_dialect_cxx()
8938 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
8939 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
8940 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
8941 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
8943 warning_at (location,
8944 OPT_Wsign_compare, "comparison between types %qT and %qT",
8945 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
8948 /* Do not warn if the comparison is being done in a signed type,
8949 since the signed type will only be chosen if it can represent
8950 all the values of the unsigned type. */
8951 if (!TYPE_UNSIGNED (result_type))
8952 /* OK */;
8953 /* Do not warn if both operands are unsigned. */
8954 else if (op0_signed == op1_signed)
8955 /* OK */;
8956 else
8958 tree sop, uop, base_type;
8959 bool ovf;
8961 if (op0_signed)
8962 sop = orig_op0, uop = orig_op1;
8963 else
8964 sop = orig_op1, uop = orig_op0;
8966 STRIP_TYPE_NOPS (sop);
8967 STRIP_TYPE_NOPS (uop);
8968 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
8969 ? TREE_TYPE (result_type) : result_type);
8971 /* Do not warn if the signed quantity is an unsuffixed integer
8972 literal (or some static constant expression involving such
8973 literals or a conditional expression involving such literals)
8974 and it is non-negative. */
8975 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
8976 /* OK */;
8977 /* Do not warn if the comparison is an equality operation, the
8978 unsigned quantity is an integral constant, and it would fit
8979 in the result if the result were signed. */
8980 else if (TREE_CODE (uop) == INTEGER_CST
8981 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8982 && int_fits_type_p (uop, c_common_signed_type (base_type)))
8983 /* OK */;
8984 /* In C, do not warn if the unsigned quantity is an enumeration
8985 constant and its maximum value would fit in the result if the
8986 result were signed. */
8987 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
8988 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8989 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8990 c_common_signed_type (base_type)))
8991 /* OK */;
8992 else
8993 warning_at (location,
8994 OPT_Wsign_compare,
8995 "comparison between signed and unsigned integer expressions");
8998 /* Warn if two unsigned values are being compared in a size larger
8999 than their original size, and one (and only one) is the result of
9000 a `~' operator. This comparison will always fail.
9002 Also warn if one operand is a constant, and the constant does not
9003 have all bits set that are set in the ~ operand when it is
9004 extended. */
9006 op0 = get_narrower (op0, &unsignedp0);
9007 op1 = get_narrower (op1, &unsignedp1);
9009 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
9010 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
9012 if (TREE_CODE (op0) == BIT_NOT_EXPR)
9013 op0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
9014 if (TREE_CODE (op1) == BIT_NOT_EXPR)
9015 op1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
9017 if (host_integerp (op0, 0) || host_integerp (op1, 0))
9019 tree primop;
9020 HOST_WIDE_INT constant, mask;
9021 int unsignedp;
9022 unsigned int bits;
9024 if (host_integerp (op0, 0))
9026 primop = op1;
9027 unsignedp = unsignedp1;
9028 constant = tree_low_cst (op0, 0);
9030 else
9032 primop = op0;
9033 unsignedp = unsignedp0;
9034 constant = tree_low_cst (op1, 0);
9037 bits = TYPE_PRECISION (TREE_TYPE (primop));
9038 if (bits < TYPE_PRECISION (result_type)
9039 && bits < HOST_BITS_PER_LONG && unsignedp)
9041 mask = (~ (HOST_WIDE_INT) 0) << bits;
9042 if ((mask & constant) != mask)
9044 if (constant == 0)
9045 warning (OPT_Wsign_compare,
9046 "promoted ~unsigned is always non-zero");
9047 else
9048 warning_at (location, OPT_Wsign_compare,
9049 "comparison of promoted ~unsigned with constant");
9053 else if (unsignedp0 && unsignedp1
9054 && (TYPE_PRECISION (TREE_TYPE (op0))
9055 < TYPE_PRECISION (result_type))
9056 && (TYPE_PRECISION (TREE_TYPE (op1))
9057 < TYPE_PRECISION (result_type)))
9058 warning_at (location, OPT_Wsign_compare,
9059 "comparison of promoted ~unsigned with unsigned");
9063 /* Setup a TYPE_DECL node as a typedef representation.
9065 X is a TYPE_DECL for a typedef statement. Create a brand new
9066 ..._TYPE node (which will be just a variant of the existing
9067 ..._TYPE node with identical properties) and then install X
9068 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
9070 The whole point here is to end up with a situation where each
9071 and every ..._TYPE node the compiler creates will be uniquely
9072 associated with AT MOST one node representing a typedef name.
9073 This way, even though the compiler substitutes corresponding
9074 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
9075 early on, later parts of the compiler can always do the reverse
9076 translation and get back the corresponding typedef name. For
9077 example, given:
9079 typedef struct S MY_TYPE;
9080 MY_TYPE object;
9082 Later parts of the compiler might only know that `object' was of
9083 type `struct S' if it were not for code just below. With this
9084 code however, later parts of the compiler see something like:
9086 struct S' == struct S
9087 typedef struct S' MY_TYPE;
9088 struct S' object;
9090 And they can then deduce (from the node for type struct S') that
9091 the original object declaration was:
9093 MY_TYPE object;
9095 Being able to do this is important for proper support of protoize,
9096 and also for generating precise symbolic debugging information
9097 which takes full account of the programmer's (typedef) vocabulary.
9099 Obviously, we don't want to generate a duplicate ..._TYPE node if
9100 the TYPE_DECL node that we are now processing really represents a
9101 standard built-in type. */
9103 void
9104 set_underlying_type (tree x)
9106 if (x == error_mark_node)
9107 return;
9108 if (DECL_IS_BUILTIN (x))
9110 if (TYPE_NAME (TREE_TYPE (x)) == 0)
9111 TYPE_NAME (TREE_TYPE (x)) = x;
9113 else if (TREE_TYPE (x) != error_mark_node
9114 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
9116 tree tt = TREE_TYPE (x);
9117 DECL_ORIGINAL_TYPE (x) = tt;
9118 tt = build_variant_type_copy (tt);
9119 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
9120 TYPE_NAME (tt) = x;
9121 TREE_USED (tt) = TREE_USED (x);
9122 TREE_TYPE (x) = tt;
9126 /* Returns true if X is a typedef decl. */
9128 bool
9129 is_typedef_decl (tree x)
9131 return (x && TREE_CODE (x) == TYPE_DECL
9132 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
9135 /* The C and C++ parsers both use vectors to hold function arguments.
9136 For efficiency, we keep a cache of unused vectors. This is the
9137 cache. */
9139 typedef VEC(tree,gc)* tree_gc_vec;
9140 DEF_VEC_P(tree_gc_vec);
9141 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
9142 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
9144 /* Return a new vector from the cache. If the cache is empty,
9145 allocate a new vector. These vectors are GC'ed, so it is OK if the
9146 pointer is not released.. */
9148 VEC(tree,gc) *
9149 make_tree_vector (void)
9151 if (!VEC_empty (tree_gc_vec, tree_vector_cache))
9152 return VEC_pop (tree_gc_vec, tree_vector_cache);
9153 else
9155 /* Passing 0 to VEC_alloc returns NULL, and our callers require
9156 that we always return a non-NULL value. The vector code uses
9157 4 when growing a NULL vector, so we do too. */
9158 return VEC_alloc (tree, gc, 4);
9162 /* Release a vector of trees back to the cache. */
9164 void
9165 release_tree_vector (VEC(tree,gc) *vec)
9167 if (vec != NULL)
9169 VEC_truncate (tree, vec, 0);
9170 VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
9174 /* Get a new tree vector holding a single tree. */
9176 VEC(tree,gc) *
9177 make_tree_vector_single (tree t)
9179 VEC(tree,gc) *ret = make_tree_vector ();
9180 VEC_quick_push (tree, ret, t);
9181 return ret;
9184 /* Get a new tree vector which is a copy of an existing one. */
9186 VEC(tree,gc) *
9187 make_tree_vector_copy (const VEC(tree,gc) *orig)
9189 VEC(tree,gc) *ret;
9190 unsigned int ix;
9191 tree t;
9193 ret = make_tree_vector ();
9194 VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
9195 for (ix = 0; VEC_iterate (tree, orig, ix, t); ++ix)
9196 VEC_quick_push (tree, ret, t);
9197 return ret;
9200 #include "gt-c-common.h"