OSDN Git Service

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