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