OSDN Git Service

2003-09-24 Ziemowit Laski <zlaski@apple.com>
[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 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "intl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "output.h"
30 #include "c-pragma.h"
31 #include "rtl.h"
32 #include "ggc.h"
33 #include "varray.h"
34 #include "expr.h"
35 #include "c-common.h"
36 #include "diagnostic.h"
37 #include "tm_p.h"
38 #include "obstack.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "langhooks.h"
42 #include "tree-inline.h"
43 #include "c-tree.h"
44 #include "toplev.h"
45
46 cpp_reader *parse_in;           /* Declared in c-pragma.h.  */
47
48 /* We let tm.h override the types used here, to handle trivial differences
49    such as the choice of unsigned int or long unsigned int for size_t.
50    When machines start needing nontrivial differences in the size type,
51    it would be best to do something here to figure out automatically
52    from other information what type to use.  */
53
54 #ifndef SIZE_TYPE
55 #define SIZE_TYPE "long unsigned int"
56 #endif
57
58 #ifndef WCHAR_TYPE
59 #define WCHAR_TYPE "int"
60 #endif
61
62 /* WCHAR_TYPE gets overridden by -fshort-wchar.  */
63 #define MODIFIED_WCHAR_TYPE \
64         (flag_short_wchar ? "short unsigned int" : WCHAR_TYPE)
65
66 #ifndef PTRDIFF_TYPE
67 #define PTRDIFF_TYPE "long int"
68 #endif
69
70 #ifndef WINT_TYPE
71 #define WINT_TYPE "unsigned int"
72 #endif
73
74 #ifndef INTMAX_TYPE
75 #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE)     \
76                      ? "int"                                    \
77                      : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
78                         ? "long int"                            \
79                         : "long long int"))
80 #endif
81
82 #ifndef UINTMAX_TYPE
83 #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE)    \
84                      ? "unsigned int"                           \
85                      : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
86                         ? "long unsigned int"                   \
87                         : "long long unsigned int"))
88 #endif
89
90 /* The following symbols are subsumed in the c_global_trees array, and
91    listed here individually for documentation purposes.
92
93    INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
94
95         tree short_integer_type_node;
96         tree long_integer_type_node;
97         tree long_long_integer_type_node;
98
99         tree short_unsigned_type_node;
100         tree long_unsigned_type_node;
101         tree long_long_unsigned_type_node;
102
103         tree truthvalue_type_node;
104         tree truthvalue_false_node;
105         tree truthvalue_true_node;
106
107         tree ptrdiff_type_node;
108
109         tree unsigned_char_type_node;
110         tree signed_char_type_node;
111         tree wchar_type_node;
112         tree signed_wchar_type_node;
113         tree unsigned_wchar_type_node;
114
115         tree float_type_node;
116         tree double_type_node;
117         tree long_double_type_node;
118
119         tree complex_integer_type_node;
120         tree complex_float_type_node;
121         tree complex_double_type_node;
122         tree complex_long_double_type_node;
123
124         tree intQI_type_node;
125         tree intHI_type_node;
126         tree intSI_type_node;
127         tree intDI_type_node;
128         tree intTI_type_node;
129
130         tree unsigned_intQI_type_node;
131         tree unsigned_intHI_type_node;
132         tree unsigned_intSI_type_node;
133         tree unsigned_intDI_type_node;
134         tree unsigned_intTI_type_node;
135
136         tree widest_integer_literal_type_node;
137         tree widest_unsigned_literal_type_node;
138
139    Nodes for types `void *' and `const void *'.
140
141         tree ptr_type_node, const_ptr_type_node;
142
143    Nodes for types `char *' and `const char *'.
144
145         tree string_type_node, const_string_type_node;
146
147    Type `char[SOMENUMBER]'.
148    Used when an array of char is needed and the size is irrelevant.
149
150         tree char_array_type_node;
151
152    Type `int[SOMENUMBER]' or something like it.
153    Used when an array of int needed and the size is irrelevant.
154
155         tree int_array_type_node;
156
157    Type `wchar_t[SOMENUMBER]' or something like it.
158    Used when a wide string literal is created.
159
160         tree wchar_array_type_node;
161
162    Type `int ()' -- used for implicit declaration of functions.
163
164         tree default_function_type;
165
166    A VOID_TYPE node, packaged in a TREE_LIST.
167
168         tree void_list_node;
169
170   The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
171   and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
172   VAR_DECLS, but C++ does.)
173
174         tree function_name_decl_node;
175         tree pretty_function_name_decl_node;
176         tree c99_function_name_decl_node;
177
178   Stack of nested function name VAR_DECLs.
179
180         tree saved_function_name_decls;
181
182 */
183
184 tree c_global_trees[CTI_MAX];
185
186 /* TRUE if a code represents a statement.  The front end init
187    langhook should take care of initialization of this array.  */
188
189 bool statement_code_p[MAX_TREE_CODES];
190 \f
191 /* Switches common to the C front ends.  */
192
193 /* Nonzero if prepreprocessing only.  */
194
195 int flag_preprocess_only;
196
197 /* Nonzero means don't output line number information.  */
198
199 char flag_no_line_commands;
200
201 /* Nonzero causes -E output not to be done, but directives such as
202    #define that have side effects are still obeyed.  */
203
204 char flag_no_output;
205
206 /* Nonzero means dump macros in some fashion.  */
207
208 char flag_dump_macros;
209
210 /* Nonzero means pass #include lines through to the output.  */
211
212 char flag_dump_includes;
213
214 /* The file name to which we should write a precompiled header, or
215    NULL if no header will be written in this compile.  */
216
217 const char *pch_file;
218
219 /* Nonzero if an ISO standard was selected.  It rejects macros in the
220    user's namespace.  */
221 int flag_iso;
222
223 /* Nonzero if -undef was given.  It suppresses target built-in macros
224    and assertions.  */
225 int flag_undef;
226
227 /* Nonzero means don't recognize the non-ANSI builtin functions.  */
228
229 int flag_no_builtin;
230
231 /* Nonzero means don't recognize the non-ANSI builtin functions.
232    -ansi sets this.  */
233
234 int flag_no_nonansi_builtin;
235
236 /* Nonzero means give `double' the same size as `float'.  */
237
238 int flag_short_double;
239
240 /* Nonzero means give `wchar_t' the same size as `short'.  */
241
242 int flag_short_wchar;
243
244 /* Nonzero means allow Microsoft extensions without warnings or errors.  */
245 int flag_ms_extensions;
246
247 /* Nonzero means don't recognize the keyword `asm'.  */
248
249 int flag_no_asm;
250
251 /* Nonzero means give string constants the type `const char *', as mandated
252    by the standard.  */
253
254 int flag_const_strings;
255
256 /* Nonzero means to treat bitfields as signed unless they say `unsigned'.  */
257
258 int flag_signed_bitfields = 1;
259 int explicit_flag_signed_bitfields;
260
261 /* Nonzero means warn about pointer casts that can drop a type qualifier
262    from the pointer target type.  */
263
264 int warn_cast_qual;
265
266 /* Warn about functions which might be candidates for format attributes.  */
267
268 int warn_missing_format_attribute;
269
270 /* Nonzero means warn about sizeof(function) or addition/subtraction
271    of function pointers.  */
272
273 int warn_pointer_arith;
274
275 /* Nonzero means warn for any global function def
276    without separate previous prototype decl.  */
277
278 int warn_missing_prototypes;
279
280 /* Warn if adding () is suggested.  */
281
282 int warn_parentheses;
283
284 /* Warn if initializer is not completely bracketed.  */
285
286 int warn_missing_braces;
287
288 /* Warn about comparison of signed and unsigned values.
289    If -1, neither -Wsign-compare nor -Wno-sign-compare has been specified
290    (in which case -Wextra gets to decide).  */
291
292 int warn_sign_compare = -1;
293
294 /* Nonzero means warn about usage of long long when `-pedantic'.  */
295
296 int warn_long_long = 1;
297
298 /* Nonzero means warn about deprecated conversion from string constant to
299    `char *'.  */
300
301 int warn_write_strings;
302
303 /* Nonzero means warn about multiple (redundant) decls for the same single
304    variable or function.  */
305
306 int warn_redundant_decls;
307
308 /* Warn about testing equality of floating point numbers.  */
309
310 int warn_float_equal;
311
312 /* Warn about a subscript that has type char.  */
313
314 int warn_char_subscripts;
315
316 /* Warn if a type conversion is done that might have confusing results.  */
317
318 int warn_conversion;
319
320 /* Warn about #pragma directives that are not recognized.  */
321
322 int warn_unknown_pragmas; /* Tri state variable.  */
323
324 /* Warn about format/argument anomalies in calls to formatted I/O functions
325    (*printf, *scanf, strftime, strfmon, etc.).  */
326
327 int warn_format;
328
329 /* Warn about Y2K problems with strftime formats.  */
330
331 int warn_format_y2k;
332
333 /* Warn about excess arguments to formats.  */
334
335 int warn_format_extra_args;
336
337 /* Warn about zero-length formats.  */
338
339 int warn_format_zero_length;
340
341 /* Warn about non-literal format arguments.  */
342
343 int warn_format_nonliteral;
344
345 /* Warn about possible security problems with calls to format functions.  */
346
347 int warn_format_security;
348
349 /* Zero means that faster, ...NonNil variants of objc_msgSend...
350    calls will be used in ObjC; passing nil receivers to such calls
351    will most likely result in crashes.  */
352 int flag_nil_receivers = 1;
353
354 /* Nonzero means that we will allow new ObjC exception syntax (@throw,
355    @try, etc.) in source code.  */
356 int flag_objc_exceptions = 0;
357
358 /* Nonzero means that code generation will be altered to support
359    "zero-link" execution.  This currently affects ObjC only, but may
360    affect other languages in the future.  */
361 int flag_zero_link = 0;
362
363 /* Nonzero means emit an '__OBJC, __image_info' for the current translation
364    unit.  It will inform the ObjC runtime that class definition(s) herein
365    contained are to replace one(s) previously loaded.  */
366 int flag_replace_objc_classes = 0;
367    
368 /* C/ObjC language option variables.  */
369
370
371 /* Nonzero means message about use of implicit function declarations;
372  1 means warning; 2 means error.  */
373
374 int mesg_implicit_function_declaration = -1;
375
376 /* Nonzero means allow type mismatches in conditional expressions;
377    just make their values `void'.  */
378
379 int flag_cond_mismatch;
380
381 /* Nonzero means enable C89 Amendment 1 features.  */
382
383 int flag_isoc94;
384
385 /* Nonzero means use the ISO C99 dialect of C.  */
386
387 int flag_isoc99;
388
389 /* Nonzero means that we have builtin functions, and main is an int */
390
391 int flag_hosted = 1;
392
393 /* Nonzero means warn when casting a function call to a type that does
394    not match the return type (e.g. (float)sqrt() or (anything*)malloc()
395    when there is no previous declaration of sqrt or malloc.  */
396
397 int warn_bad_function_cast;
398
399 /* Warn about traditional constructs whose meanings changed in ANSI C.  */
400
401 int warn_traditional;
402
403 /* Nonzero means warn for a declaration found after a statement.  */
404
405 int warn_declaration_after_statement;
406
407 /* Nonzero means warn for non-prototype function decls
408    or non-prototyped defs without previous prototype.  */
409
410 int warn_strict_prototypes;
411
412 /* Nonzero means warn for any global function def
413    without separate previous decl.  */
414
415 int warn_missing_declarations;
416
417 /* Nonzero means warn about declarations of objects not at
418    file-scope level and about *all* declarations of functions (whether
419    or static) not at file-scope level.  Note that we exclude
420    implicit function declarations.  To get warnings about those, use
421    -Wimplicit.  */
422
423 int warn_nested_externs;
424
425 /* Warn if main is suspicious.  */
426
427 int warn_main;
428
429 /* Nonzero means warn about possible violations of sequence point rules.  */
430
431 int warn_sequence_point;
432
433 /* Nonzero means warn about uninitialized variable when it is initialized with itself.
434    For example: int i = i;, GCC will not warn about this when warn_init_self is nonzero.  */
435
436 int warn_init_self;
437
438 /* Nonzero means to warn about compile-time division by zero.  */
439 int warn_div_by_zero = 1;
440
441 /* Nonzero means warn about use of implicit int.  */
442
443 int warn_implicit_int;
444
445 /* Warn about NULL being passed to argument slots marked as requiring
446    non-NULL.  */
447
448 int warn_nonnull;
449
450 /* Warn about old-style parameter declaration.  */
451
452 int warn_old_style_definition;
453
454
455 /* ObjC language option variables.  */
456
457
458 /* Open and close the file for outputting class declarations, if
459    requested (ObjC).  */
460
461 int flag_gen_declaration;
462
463 /* Generate code for GNU or NeXT runtime environment.  */
464
465 #ifdef NEXT_OBJC_RUNTIME
466 int flag_next_runtime = 1;
467 #else
468 int flag_next_runtime = 0;
469 #endif
470
471 /* Tells the compiler that this is a special run.  Do not perform any
472    compiling, instead we are to test some platform dependent features
473    and output a C header file with appropriate definitions.  */
474
475 int print_struct_values;
476
477 /* ???.  Undocumented.  */
478
479 const char *constant_string_class_name;
480
481 /* Warn if multiple methods are seen for the same selector, but with
482    different argument types.  Performs the check on the whole selector
483    table at the end of compilation.  */
484
485 int warn_selector;
486
487 /* Warn if a @selector() is found, and no method with that selector
488    has been previously declared.  The check is done on each
489    @selector() as soon as it is found - so it warns about forward
490    declarations.  */
491
492 int warn_undeclared_selector;
493
494 /* Warn if methods required by a protocol are not implemented in the
495    class adopting it.  When turned off, methods inherited to that
496    class are also considered implemented.  */
497
498 int warn_protocol = 1;
499
500
501 /* C++ language option variables.  */
502
503
504 /* Nonzero means don't recognize any extension keywords.  */
505
506 int flag_no_gnu_keywords;
507
508 /* Nonzero means do emit exported implementations of functions even if
509    they can be inlined.  */
510
511 int flag_implement_inlines = 1;
512
513 /* Nonzero means do emit exported implementations of templates, instead of
514    multiple static copies in each file that needs a definition.  */
515
516 int flag_external_templates;
517
518 /* Nonzero means that the decision to emit or not emit the implementation of a
519    template depends on where the template is instantiated, rather than where
520    it is defined.  */
521
522 int flag_alt_external_templates;
523
524 /* Nonzero means that implicit instantiations will be emitted if needed.  */
525
526 int flag_implicit_templates = 1;
527
528 /* Nonzero means that implicit instantiations of inline templates will be
529    emitted if needed, even if instantiations of non-inline templates
530    aren't.  */
531
532 int flag_implicit_inline_templates = 1;
533
534 /* Nonzero means generate separate instantiation control files and
535    juggle them at link time.  */
536
537 int flag_use_repository;
538
539 /* Nonzero if we want to issue diagnostics that the standard says are not
540    required.  */
541
542 int flag_optional_diags = 1;
543
544 /* Nonzero means we should attempt to elide constructors when possible.  */
545
546 int flag_elide_constructors = 1;
547
548 /* Nonzero means that member functions defined in class scope are
549    inline by default.  */
550
551 int flag_default_inline = 1;
552
553 /* Controls whether compiler generates 'type descriptor' that give
554    run-time type information.  */
555
556 int flag_rtti = 1;
557
558 /* Nonzero if we want to conserve space in the .o files.  We do this
559    by putting uninitialized data and runtime initialized data into
560    .common instead of .data at the expense of not flagging multiple
561    definitions.  */
562
563 int flag_conserve_space;
564
565 /* Nonzero if we want to obey access control semantics.  */
566
567 int flag_access_control = 1;
568
569 /* Nonzero if we want to check the return value of new and avoid calling
570    constructors if it is a null pointer.  */
571
572 int flag_check_new;
573
574 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
575    initialization variables.
576    0: Old rules, set by -fno-for-scope.
577    2: New ISO rules, set by -ffor-scope.
578    1: Try to implement new ISO rules, but with backup compatibility
579    (and warnings).  This is the default, for now.  */
580
581 int flag_new_for_scope = 1;
582
583 /* Nonzero if we want to emit defined symbols with common-like linkage as
584    weak symbols where possible, in order to conform to C++ semantics.
585    Otherwise, emit them as local symbols.  */
586
587 int flag_weak = 1;
588
589 /* 0 means we want the preprocessor to not emit line directives for
590    the current working directory.  1 means we want it to do it.  -1
591    means we should decide depending on whether debugging information
592    is being emitted or not.  */
593
594 int flag_working_directory = -1;
595
596 /* Nonzero to use __cxa_atexit, rather than atexit, to register
597    destructors for local statics and global objects.  */
598
599 int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT;
600
601 /* Nonzero means make the default pedwarns warnings instead of errors.
602    The value of this flag is ignored if -pedantic is specified.  */
603
604 int flag_permissive;
605
606 /* Nonzero means to implement standard semantics for exception
607    specifications, calling unexpected if an exception is thrown that
608    doesn't match the specification.  Zero means to treat them as
609    assertions and optimize accordingly, but not check them.  */
610
611 int flag_enforce_eh_specs = 1;
612
613 /*  The version of the C++ ABI in use.  The following values are
614     allowed:
615
616     0: The version of the ABI believed most conformant with the
617        C++ ABI specification.  This ABI may change as bugs are
618        discovered and fixed.  Therefore, 0 will not necessarily
619        indicate the same ABI in different versions of G++.
620
621     1: The version of the ABI first used in G++ 3.2.
622
623     Additional positive integers will be assigned as new versions of
624     the ABI become the default version of the ABI.  */
625
626 int flag_abi_version = 1;
627
628 /* Nonzero means warn about things that will change when compiling
629    with an ABI-compliant compiler.  */
630
631 int warn_abi = 0;
632
633 /* Nonzero means warn about invalid uses of offsetof.  */
634
635 int warn_invalid_offsetof = 1;
636
637 /* Nonzero means warn about implicit declarations.  */
638
639 int warn_implicit = 1;
640
641 /* Nonzero means warn when all ctors or dtors are private, and the class
642    has no friends.  */
643
644 int warn_ctor_dtor_privacy = 0;
645
646 /* Nonzero means warn in function declared in derived class has the
647    same name as a virtual in the base class, but fails to match the
648    type signature of any virtual function in the base class.  */
649
650 int warn_overloaded_virtual;
651
652 /* Nonzero means warn when declaring a class that has a non virtual
653    destructor, when it really ought to have a virtual one.  */
654
655 int warn_nonvdtor;
656
657 /* Nonzero means warn when the compiler will reorder code.  */
658
659 int warn_reorder;
660
661 /* Nonzero means warn when synthesis behavior differs from Cfront's.  */
662
663 int warn_synth;
664
665 /* Nonzero means warn when we convert a pointer to member function
666    into a pointer to (void or function).  */
667
668 int warn_pmf2ptr = 1;
669
670 /* Nonzero means warn about violation of some Effective C++ style rules.  */
671
672 int warn_ecpp;
673
674 /* Nonzero means warn where overload resolution chooses a promotion from
675    unsigned to signed over a conversion to an unsigned of the same size.  */
676
677 int warn_sign_promo;
678
679 /* Nonzero means warn when an old-style cast is used.  */
680
681 int warn_old_style_cast;
682
683 /* Nonzero means warn when non-templatized friend functions are
684    declared within a template */
685
686 int warn_nontemplate_friend = 1;
687
688 /* Nonzero means complain about deprecated features.  */
689
690 int warn_deprecated = 1;
691
692 /* Maximum template instantiation depth.  This limit is rather
693    arbitrary, but it exists to limit the time it takes to notice
694    infinite template instantiations.  */
695
696 int max_tinst_depth = 500;
697
698
699
700 /* The elements of `ridpointers' are identifier nodes for the reserved
701    type names and storage classes.  It is indexed by a RID_... value.  */
702 tree *ridpointers;
703
704 tree (*make_fname_decl) (tree, int);
705
706 /* If non-NULL, the address of a language-specific function that takes
707    any action required right before expand_function_end is called.  */
708 void (*lang_expand_function_end) (void);
709
710 /* Nonzero means the expression being parsed will never be evaluated.
711    This is a count, since unevaluated expressions can nest.  */
712 int skip_evaluation;
713
714 /* Information about how a function name is generated.  */
715 struct fname_var_t
716 {
717   tree *const decl;     /* pointer to the VAR_DECL.  */
718   const unsigned rid;   /* RID number for the identifier.  */
719   const int pretty;     /* How pretty is it? */
720 };
721
722 /* The three ways of getting then name of the current function.  */
723
724 const struct fname_var_t fname_vars[] =
725 {
726   /* C99 compliant __func__, must be first.  */
727   {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
728   /* GCC __FUNCTION__ compliant.  */
729   {&function_name_decl_node, RID_FUNCTION_NAME, 0},
730   /* GCC __PRETTY_FUNCTION__ compliant.  */
731   {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
732   {NULL, 0, 0},
733 };
734
735 static int constant_fits_type_p (tree, tree);
736
737 /* Keep a stack of if statements.  We record the number of compound
738    statements seen up to the if keyword, as well as the line number
739    and file of the if.  If a potentially ambiguous else is seen, that
740    fact is recorded; the warning is issued when we can be sure that
741    the enclosing if statement does not have an else branch.  */
742 typedef struct
743 {
744   int compstmt_count;
745   location_t locus;
746   int needs_warning;
747   tree if_stmt;
748 } if_elt;
749
750 static if_elt *if_stack;
751
752 /* Amount of space in the if statement stack.  */
753 static int if_stack_space = 0;
754
755 /* Stack pointer.  */
756 static int if_stack_pointer = 0;
757
758 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
759 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
760 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
761 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
762 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
763 static tree handle_always_inline_attribute (tree *, tree, tree, int,
764                                             bool *);
765 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
766 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
767 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
768 static tree handle_transparent_union_attribute (tree *, tree, tree,
769                                                 int, bool *);
770 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
771 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
772 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
773 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
774 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
775 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
776 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
777 static tree handle_visibility_attribute (tree *, tree, tree, int,
778                                          bool *);
779 static tree handle_tls_model_attribute (tree *, tree, tree, int,
780                                         bool *);
781 static tree handle_no_instrument_function_attribute (tree *, tree,
782                                                      tree, int, bool *);
783 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
784 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
785                                              bool *);
786 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
787 static tree handle_deprecated_attribute (tree *, tree, tree, int,
788                                          bool *);
789 static tree handle_vector_size_attribute (tree *, tree, tree, int,
790                                           bool *);
791 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
792 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
793 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
794 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
795                                                  bool *);
796 static tree vector_size_helper (tree, tree);
797
798 static void check_function_nonnull (tree, tree);
799 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
800 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
801 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
802 static int resort_field_decl_cmp (const void *, const void *);
803
804 /* Table of machine-independent attributes common to all C-like languages.  */
805 const struct attribute_spec c_common_attribute_table[] =
806 {
807   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
808   { "packed",                 0, 0, false, false, false,
809                               handle_packed_attribute },
810   { "nocommon",               0, 0, true,  false, false,
811                               handle_nocommon_attribute },
812   { "common",                 0, 0, true,  false, false,
813                               handle_common_attribute },
814   /* FIXME: logically, noreturn attributes should be listed as
815      "false, true, true" and apply to function types.  But implementing this
816      would require all the places in the compiler that use TREE_THIS_VOLATILE
817      on a decl to identify non-returning functions to be located and fixed
818      to check the function type instead.  */
819   { "noreturn",               0, 0, true,  false, false,
820                               handle_noreturn_attribute },
821   { "volatile",               0, 0, true,  false, false,
822                               handle_noreturn_attribute },
823   { "noinline",               0, 0, true,  false, false,
824                               handle_noinline_attribute },
825   { "always_inline",          0, 0, true,  false, false,
826                               handle_always_inline_attribute },
827   { "used",                   0, 0, true,  false, false,
828                               handle_used_attribute },
829   { "unused",                 0, 0, false, false, false,
830                               handle_unused_attribute },
831   /* The same comments as for noreturn attributes apply to const ones.  */
832   { "const",                  0, 0, true,  false, false,
833                               handle_const_attribute },
834   { "transparent_union",      0, 0, false, false, false,
835                               handle_transparent_union_attribute },
836   { "constructor",            0, 0, true,  false, false,
837                               handle_constructor_attribute },
838   { "destructor",             0, 0, true,  false, false,
839                               handle_destructor_attribute },
840   { "mode",                   1, 1, false,  true, false,
841                               handle_mode_attribute },
842   { "section",                1, 1, true,  false, false,
843                               handle_section_attribute },
844   { "aligned",                0, 1, false, false, false,
845                               handle_aligned_attribute },
846   { "weak",                   0, 0, true,  false, false,
847                               handle_weak_attribute },
848   { "alias",                  1, 1, true,  false, false,
849                               handle_alias_attribute },
850   { "no_instrument_function", 0, 0, true,  false, false,
851                               handle_no_instrument_function_attribute },
852   { "malloc",                 0, 0, true,  false, false,
853                               handle_malloc_attribute },
854   { "no_stack_limit",         0, 0, true,  false, false,
855                               handle_no_limit_stack_attribute },
856   { "pure",                   0, 0, true,  false, false,
857                               handle_pure_attribute },
858   { "deprecated",             0, 0, false, false, false,
859                               handle_deprecated_attribute },
860   { "vector_size",            1, 1, false, true, false,
861                               handle_vector_size_attribute },
862   { "visibility",             1, 1, true,  false, false,
863                               handle_visibility_attribute },
864   { "tls_model",              1, 1, true,  false, false,
865                               handle_tls_model_attribute },
866   { "nonnull",                0, -1, false, true, true,
867                               handle_nonnull_attribute },
868   { "nothrow",                0, 0, true,  false, false,
869                               handle_nothrow_attribute },
870   { "may_alias",              0, 0, false, true, false, NULL },
871   { "cleanup",                1, 1, true, false, false,
872                               handle_cleanup_attribute },
873   { "warn_unused_result",     0, 0, false, true, true,
874                               handle_warn_unused_result_attribute },
875   { NULL,                     0, 0, false, false, false, NULL }
876 };
877
878 /* Give the specifications for the format attributes, used by C and all
879    descendants.  */
880
881 const struct attribute_spec c_common_format_attribute_table[] =
882 {
883   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
884   { "format",                 3, 3, false, true,  true,
885                               handle_format_attribute },
886   { "format_arg",             1, 1, false, true,  true,
887                               handle_format_arg_attribute },
888   { NULL,                     0, 0, false, false, false, NULL }
889 };
890
891 /* Record the start of an if-then, and record the start of it
892    for ambiguous else detection.
893
894    COND is the condition for the if-then statement.
895
896    IF_STMT is the statement node that has already been created for
897    this if-then statement.  It is created before parsing the
898    condition to keep line number information accurate.  */
899
900 void
901 c_expand_start_cond (tree cond, int compstmt_count, tree if_stmt)
902 {
903   /* Make sure there is enough space on the stack.  */
904   if (if_stack_space == 0)
905     {
906       if_stack_space = 10;
907       if_stack = xmalloc (10 * sizeof (if_elt));
908     }
909   else if (if_stack_space == if_stack_pointer)
910     {
911       if_stack_space += 10;
912       if_stack = xrealloc (if_stack, if_stack_space * sizeof (if_elt));
913     }
914
915   IF_COND (if_stmt) = cond;
916   add_stmt (if_stmt);
917
918   /* Record this if statement.  */
919   if_stack[if_stack_pointer].compstmt_count = compstmt_count;
920   if_stack[if_stack_pointer].locus = input_location;
921   if_stack[if_stack_pointer].needs_warning = 0;
922   if_stack[if_stack_pointer].if_stmt = if_stmt;
923   if_stack_pointer++;
924 }
925
926 /* Called after the then-clause for an if-statement is processed.  */
927
928 void
929 c_finish_then (void)
930 {
931   tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
932   RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
933 }
934
935 /* Record the end of an if-then.  Optionally warn if a nested
936    if statement had an ambiguous else clause.  */
937
938 void
939 c_expand_end_cond (void)
940 {
941   if_stack_pointer--;
942   if (if_stack[if_stack_pointer].needs_warning)
943     warning ("%Hsuggest explicit braces to avoid ambiguous `else'",
944              &if_stack[if_stack_pointer].locus);
945   last_expr_type = NULL_TREE;
946 }
947
948 /* Called between the then-clause and the else-clause
949    of an if-then-else.  */
950
951 void
952 c_expand_start_else (void)
953 {
954   /* An ambiguous else warning must be generated for the enclosing if
955      statement, unless we see an else branch for that one, too.  */
956   if (warn_parentheses
957       && if_stack_pointer > 1
958       && (if_stack[if_stack_pointer - 1].compstmt_count
959           == if_stack[if_stack_pointer - 2].compstmt_count))
960     if_stack[if_stack_pointer - 2].needs_warning = 1;
961
962   /* Even if a nested if statement had an else branch, it can't be
963      ambiguous if this one also has an else.  So don't warn in that
964      case.  Also don't warn for any if statements nested in this else.  */
965   if_stack[if_stack_pointer - 1].needs_warning = 0;
966   if_stack[if_stack_pointer - 1].compstmt_count--;
967 }
968
969 /* Called after the else-clause for an if-statement is processed.  */
970
971 void
972 c_finish_else (void)
973 {
974   tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
975   RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
976 }
977
978 /* Begin an if-statement.  Returns a newly created IF_STMT if
979    appropriate.
980
981    Unlike the C++ front-end, we do not call add_stmt here; it is
982    probably safe to do so, but I am not very familiar with this
983    code so I am being extra careful not to change its behavior
984    beyond what is strictly necessary for correctness.  */
985
986 tree
987 c_begin_if_stmt (void)
988 {
989   tree r;
990   r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
991   return r;
992 }
993
994 /* Begin a while statement.  Returns a newly created WHILE_STMT if
995    appropriate.
996
997    Unlike the C++ front-end, we do not call add_stmt here; it is
998    probably safe to do so, but I am not very familiar with this
999    code so I am being extra careful not to change its behavior
1000    beyond what is strictly necessary for correctness.  */
1001
1002 tree
1003 c_begin_while_stmt (void)
1004 {
1005   tree r;
1006   r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
1007   return r;
1008 }
1009
1010 void
1011 c_finish_while_stmt_cond (tree cond, tree while_stmt)
1012 {
1013   WHILE_COND (while_stmt) = cond;
1014 }
1015
1016 /* Push current bindings for the function name VAR_DECLS.  */
1017
1018 void
1019 start_fname_decls (void)
1020 {
1021   unsigned ix;
1022   tree saved = NULL_TREE;
1023
1024   for (ix = 0; fname_vars[ix].decl; ix++)
1025     {
1026       tree decl = *fname_vars[ix].decl;
1027
1028       if (decl)
1029         {
1030           saved = tree_cons (decl, build_int_2 (ix, 0), saved);
1031           *fname_vars[ix].decl = NULL_TREE;
1032         }
1033     }
1034   if (saved || saved_function_name_decls)
1035     /* Normally they'll have been NULL, so only push if we've got a
1036        stack, or they are non-NULL.  */
1037     saved_function_name_decls = tree_cons (saved, NULL_TREE,
1038                                            saved_function_name_decls);
1039 }
1040
1041 /* Finish up the current bindings, adding them into the
1042    current function's statement tree. This is done by wrapping the
1043    function's body in a COMPOUND_STMT containing these decls too. This
1044    must be done _before_ finish_stmt_tree is called. If there is no
1045    current function, we must be at file scope and no statements are
1046    involved. Pop the previous bindings.  */
1047
1048 void
1049 finish_fname_decls (void)
1050 {
1051   unsigned ix;
1052   tree body = NULL_TREE;
1053   tree stack = saved_function_name_decls;
1054
1055   for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
1056     body = chainon (TREE_VALUE (stack), body);
1057
1058   if (body)
1059     {
1060       /* They were called into existence, so add to statement tree.  Add
1061          the DECL_STMTs inside the outermost scope.  */
1062       tree *p = &DECL_SAVED_TREE (current_function_decl);
1063       /* Skip the dummy EXPR_STMT and any EH_SPEC_BLOCK.  */
1064       while (TREE_CODE (*p) != COMPOUND_STMT)
1065         p = &TREE_CHAIN (*p);
1066       p = &COMPOUND_BODY (*p);
1067       if (TREE_CODE (*p) == SCOPE_STMT)
1068         p = &TREE_CHAIN (*p);
1069
1070       body = chainon (body, *p);
1071       *p = body;
1072     }
1073
1074   for (ix = 0; fname_vars[ix].decl; ix++)
1075     *fname_vars[ix].decl = NULL_TREE;
1076
1077   if (stack)
1078     {
1079       /* We had saved values, restore them.  */
1080       tree saved;
1081
1082       for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
1083         {
1084           tree decl = TREE_PURPOSE (saved);
1085           unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1086
1087           *fname_vars[ix].decl = decl;
1088         }
1089       stack = TREE_CHAIN (stack);
1090     }
1091   saved_function_name_decls = stack;
1092 }
1093
1094 /* Return the text name of the current function, suitably prettified
1095    by PRETTY_P.  */
1096
1097 const char *
1098 fname_as_string (int pretty_p)
1099 {
1100   const char *name = "top level";
1101   int vrb = 2;
1102
1103   if (! pretty_p)
1104     {
1105       name = "";
1106       vrb = 0;
1107     }
1108
1109   if (current_function_decl)
1110     name = (*lang_hooks.decl_printable_name) (current_function_decl, vrb);
1111
1112   return name;
1113 }
1114
1115 /* Return the VAR_DECL for a const char array naming the current
1116    function. If the VAR_DECL has not yet been created, create it
1117    now. RID indicates how it should be formatted and IDENTIFIER_NODE
1118    ID is its name (unfortunately C and C++ hold the RID values of
1119    keywords in different places, so we can't derive RID from ID in
1120    this language independent code.  */
1121
1122 tree
1123 fname_decl (unsigned int rid, tree id)
1124 {
1125   unsigned ix;
1126   tree decl = NULL_TREE;
1127
1128   for (ix = 0; fname_vars[ix].decl; ix++)
1129     if (fname_vars[ix].rid == rid)
1130       break;
1131
1132   decl = *fname_vars[ix].decl;
1133   if (!decl)
1134     {
1135       tree saved_last_tree = last_tree;
1136       /* If a tree is built here, it would normally have the lineno of
1137          the current statement.  Later this tree will be moved to the
1138          beginning of the function and this line number will be wrong.
1139          To avoid this problem set the lineno to 0 here; that prevents
1140          it from appearing in the RTL.  */
1141       int saved_lineno = input_line;
1142       input_line = 0;
1143
1144       decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
1145       if (last_tree != saved_last_tree)
1146         {
1147           /* We created some statement tree for the decl. This belongs
1148              at the start of the function, so remove it now and reinsert
1149              it after the function is complete.  */
1150           tree stmts = TREE_CHAIN (saved_last_tree);
1151
1152           TREE_CHAIN (saved_last_tree) = NULL_TREE;
1153           last_tree = saved_last_tree;
1154           saved_function_name_decls = tree_cons (decl, stmts,
1155                                                  saved_function_name_decls);
1156         }
1157       *fname_vars[ix].decl = decl;
1158       input_line = saved_lineno;
1159     }
1160   if (!ix && !current_function_decl)
1161     pedwarn ("%J'%D' is not defined outside of function scope", decl, decl);
1162
1163   return decl;
1164 }
1165
1166 /* Given a STRING_CST, give it a suitable array-of-chars data type.  */
1167
1168 tree
1169 fix_string_type (tree value)
1170 {
1171   const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
1172   const int wide_flag = TREE_TYPE (value) == wchar_array_type_node;
1173   const int nchars_max = flag_isoc99 ? 4095 : 509;
1174   int length = TREE_STRING_LENGTH (value);
1175   int nchars;
1176
1177   /* Compute the number of elements, for the array type.  */
1178   nchars = wide_flag ? length / wchar_bytes : length;
1179
1180   if (pedantic && nchars - 1 > nchars_max && !c_dialect_cxx ())
1181     pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support",
1182              nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
1183
1184   /* Create the array type for the string constant.
1185      -Wwrite-strings says make the string constant an array of const char
1186      so that copying it to a non-const pointer will get a warning.
1187      For C++, this is the standard behavior.  */
1188   if (flag_const_strings && ! flag_writable_strings)
1189     {
1190       tree elements
1191         = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
1192                               1, 0);
1193       TREE_TYPE (value)
1194         = build_array_type (elements,
1195                             build_index_type (build_int_2 (nchars - 1, 0)));
1196     }
1197   else
1198     TREE_TYPE (value)
1199       = build_array_type (wide_flag ? wchar_type_node : char_type_node,
1200                           build_index_type (build_int_2 (nchars - 1, 0)));
1201
1202   TREE_CONSTANT (value) = 1;
1203   TREE_READONLY (value) = ! flag_writable_strings;
1204   TREE_STATIC (value) = 1;
1205   return value;
1206 }
1207 \f
1208 /* Print a warning if a constant expression had overflow in folding.
1209    Invoke this function on every expression that the language
1210    requires to be a constant expression.
1211    Note the ANSI C standard says it is erroneous for a
1212    constant expression to overflow.  */
1213
1214 void
1215 constant_expression_warning (tree value)
1216 {
1217   if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1218        || TREE_CODE (value) == VECTOR_CST
1219        || TREE_CODE (value) == COMPLEX_CST)
1220       && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1221     pedwarn ("overflow in constant expression");
1222 }
1223
1224 /* Print a warning if an expression had overflow in folding.
1225    Invoke this function on every expression that
1226    (1) appears in the source code, and
1227    (2) might be a constant expression that overflowed, and
1228    (3) is not already checked by convert_and_check;
1229    however, do not invoke this function on operands of explicit casts.  */
1230
1231 void
1232 overflow_warning (tree value)
1233 {
1234   if ((TREE_CODE (value) == INTEGER_CST
1235        || (TREE_CODE (value) == COMPLEX_CST
1236            && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1237       && TREE_OVERFLOW (value))
1238     {
1239       TREE_OVERFLOW (value) = 0;
1240       if (skip_evaluation == 0)
1241         warning ("integer overflow in expression");
1242     }
1243   else if ((TREE_CODE (value) == REAL_CST
1244             || (TREE_CODE (value) == COMPLEX_CST
1245                 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1246            && TREE_OVERFLOW (value))
1247     {
1248       TREE_OVERFLOW (value) = 0;
1249       if (skip_evaluation == 0)
1250         warning ("floating point overflow in expression");
1251     }
1252   else if (TREE_CODE (value) == VECTOR_CST && TREE_OVERFLOW (value))
1253     {
1254       TREE_OVERFLOW (value) = 0;
1255       if (skip_evaluation == 0)
1256         warning ("vector overflow in expression");
1257     }
1258 }
1259
1260 /* Print a warning if a large constant is truncated to unsigned,
1261    or if -Wconversion is used and a constant < 0 is converted to unsigned.
1262    Invoke this function on every expression that might be implicitly
1263    converted to an unsigned type.  */
1264
1265 void
1266 unsigned_conversion_warning (tree result, tree operand)
1267 {
1268   tree type = TREE_TYPE (result);
1269
1270   if (TREE_CODE (operand) == INTEGER_CST
1271       && TREE_CODE (type) == INTEGER_TYPE
1272       && TREE_UNSIGNED (type)
1273       && skip_evaluation == 0
1274       && !int_fits_type_p (operand, type))
1275     {
1276       if (!int_fits_type_p (operand, c_common_signed_type (type)))
1277         /* This detects cases like converting -129 or 256 to unsigned char.  */
1278         warning ("large integer implicitly truncated to unsigned type");
1279       else if (warn_conversion)
1280         warning ("negative integer implicitly converted to unsigned type");
1281     }
1282 }
1283
1284 /* Nonzero if constant C has a value that is permissible
1285    for type TYPE (an INTEGER_TYPE).  */
1286
1287 static int
1288 constant_fits_type_p (tree c, tree type)
1289 {
1290   if (TREE_CODE (c) == INTEGER_CST)
1291     return int_fits_type_p (c, type);
1292
1293   c = convert (type, c);
1294   return !TREE_OVERFLOW (c);
1295 }
1296
1297 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1298    Invoke this function on every expression that is converted implicitly,
1299    i.e. because of language rules and not because of an explicit cast.  */
1300
1301 tree
1302 convert_and_check (tree type, tree expr)
1303 {
1304   tree t = convert (type, expr);
1305   if (TREE_CODE (t) == INTEGER_CST)
1306     {
1307       if (TREE_OVERFLOW (t))
1308         {
1309           TREE_OVERFLOW (t) = 0;
1310
1311           /* Do not diagnose overflow in a constant expression merely
1312              because a conversion overflowed.  */
1313           TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1314
1315           /* No warning for converting 0x80000000 to int.  */
1316           if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1317                 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1318                 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1319             /* If EXPR fits in the unsigned version of TYPE,
1320                don't warn unless pedantic.  */
1321             if ((pedantic
1322                  || TREE_UNSIGNED (type)
1323                  || ! constant_fits_type_p (expr,
1324                                             c_common_unsigned_type (type)))
1325                 && skip_evaluation == 0)
1326               warning ("overflow in implicit constant conversion");
1327         }
1328       else
1329         unsigned_conversion_warning (t, expr);
1330     }
1331   return t;
1332 }
1333 \f
1334 /* A node in a list that describes references to variables (EXPR), which are
1335    either read accesses if WRITER is zero, or write accesses, in which case
1336    WRITER is the parent of EXPR.  */
1337 struct tlist
1338 {
1339   struct tlist *next;
1340   tree expr, writer;
1341 };
1342
1343 /* Used to implement a cache the results of a call to verify_tree.  We only
1344    use this for SAVE_EXPRs.  */
1345 struct tlist_cache
1346 {
1347   struct tlist_cache *next;
1348   struct tlist *cache_before_sp;
1349   struct tlist *cache_after_sp;
1350   tree expr;
1351 };
1352
1353 /* Obstack to use when allocating tlist structures, and corresponding
1354    firstobj.  */
1355 static struct obstack tlist_obstack;
1356 static char *tlist_firstobj = 0;
1357
1358 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1359    warnings.  */
1360 static struct tlist *warned_ids;
1361 /* SAVE_EXPRs need special treatment.  We process them only once and then
1362    cache the results.  */
1363 static struct tlist_cache *save_expr_cache;
1364
1365 static void add_tlist (struct tlist **, struct tlist *, tree, int);
1366 static void merge_tlist (struct tlist **, struct tlist *, int);
1367 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1368 static int warning_candidate_p (tree);
1369 static void warn_for_collisions (struct tlist *);
1370 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1371 static struct tlist *new_tlist (struct tlist *, tree, tree);
1372 static void verify_sequence_points (tree);
1373
1374 /* Create a new struct tlist and fill in its fields.  */
1375 static struct tlist *
1376 new_tlist (struct tlist *next, tree t, tree writer)
1377 {
1378   struct tlist *l;
1379   l = obstack_alloc (&tlist_obstack, sizeof *l);
1380   l->next = next;
1381   l->expr = t;
1382   l->writer = writer;
1383   return l;
1384 }
1385
1386 /* Add duplicates of the nodes found in ADD to the list *TO.  If EXCLUDE_WRITER
1387    is nonnull, we ignore any node we find which has a writer equal to it.  */
1388
1389 static void
1390 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1391 {
1392   while (add)
1393     {
1394       struct tlist *next = add->next;
1395       if (! copy)
1396         add->next = *to;
1397       if (! exclude_writer || add->writer != exclude_writer)
1398         *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1399       add = next;
1400     }
1401 }
1402
1403 /* Merge the nodes of ADD into TO.  This merging process is done so that for
1404    each variable that already exists in TO, no new node is added; however if
1405    there is a write access recorded in ADD, and an occurrence on TO is only
1406    a read access, then the occurrence in TO will be modified to record the
1407    write.  */
1408
1409 static void
1410 merge_tlist (struct tlist **to, struct tlist *add, int copy)
1411 {
1412   struct tlist **end = to;
1413
1414   while (*end)
1415     end = &(*end)->next;
1416
1417   while (add)
1418     {
1419       int found = 0;
1420       struct tlist *tmp2;
1421       struct tlist *next = add->next;
1422
1423       for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1424         if (tmp2->expr == add->expr)
1425           {
1426             found = 1;
1427             if (! tmp2->writer)
1428               tmp2->writer = add->writer;
1429           }
1430       if (! found)
1431         {
1432           *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
1433           end = &(*end)->next;
1434           *end = 0;
1435         }
1436       add = next;
1437     }
1438 }
1439
1440 /* WRITTEN is a variable, WRITER is its parent.  Warn if any of the variable
1441    references in list LIST conflict with it, excluding reads if ONLY writers
1442    is nonzero.  */
1443
1444 static void
1445 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1446                        int only_writes)
1447 {
1448   struct tlist *tmp;
1449
1450   /* Avoid duplicate warnings.  */
1451   for (tmp = warned_ids; tmp; tmp = tmp->next)
1452     if (tmp->expr == written)
1453       return;
1454
1455   while (list)
1456     {
1457       if (list->expr == written
1458           && list->writer != writer
1459           && (! only_writes || list->writer))
1460         {
1461           warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1462           warning ("operation on `%s' may be undefined",
1463                    IDENTIFIER_POINTER (DECL_NAME (list->expr)));
1464         }
1465       list = list->next;
1466     }
1467 }
1468
1469 /* Given a list LIST of references to variables, find whether any of these
1470    can cause conflicts due to missing sequence points.  */
1471
1472 static void
1473 warn_for_collisions (struct tlist *list)
1474 {
1475   struct tlist *tmp;
1476
1477   for (tmp = list; tmp; tmp = tmp->next)
1478     {
1479       if (tmp->writer)
1480         warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1481     }
1482 }
1483
1484 /* Return nonzero if X is a tree that can be verified by the sequence point
1485    warnings.  */
1486 static int
1487 warning_candidate_p (tree x)
1488 {
1489   return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
1490 }
1491
1492 /* Walk the tree X, and record accesses to variables.  If X is written by the
1493    parent tree, WRITER is the parent.
1494    We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP.  If this
1495    expression or its only operand forces a sequence point, then everything up
1496    to the sequence point is stored in PBEFORE_SP.  Everything else gets stored
1497    in PNO_SP.
1498    Once we return, we will have emitted warnings if any subexpression before
1499    such a sequence point could be undefined.  On a higher level, however, the
1500    sequence point may not be relevant, and we'll merge the two lists.
1501
1502    Example: (b++, a) + b;
1503    The call that processes the COMPOUND_EXPR will store the increment of B
1504    in PBEFORE_SP, and the use of A in PNO_SP.  The higher-level call that
1505    processes the PLUS_EXPR will need to merge the two lists so that
1506    eventually, all accesses end up on the same list (and we'll warn about the
1507    unordered subexpressions b++ and b.
1508
1509    A note on merging.  If we modify the former example so that our expression
1510    becomes
1511      (b++, b) + a
1512    care must be taken not simply to add all three expressions into the final
1513    PNO_SP list.  The function merge_tlist takes care of that by merging the
1514    before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1515    way, so that no more than one access to B is recorded.  */
1516
1517 static void
1518 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1519              tree writer)
1520 {
1521   struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1522   enum tree_code code;
1523   char class;
1524
1525   /* X may be NULL if it is the operand of an empty statement expression
1526      ({ }).  */
1527   if (x == NULL)
1528     return;
1529
1530  restart:
1531   code = TREE_CODE (x);
1532   class = TREE_CODE_CLASS (code);
1533
1534   if (warning_candidate_p (x))
1535     {
1536       *pno_sp = new_tlist (*pno_sp, x, writer);
1537       return;
1538     }
1539
1540   switch (code)
1541     {
1542     case CONSTRUCTOR:
1543       return;
1544
1545     case COMPOUND_EXPR:
1546     case TRUTH_ANDIF_EXPR:
1547     case TRUTH_ORIF_EXPR:
1548       tmp_before = tmp_nosp = tmp_list3 = 0;
1549       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1550       warn_for_collisions (tmp_nosp);
1551       merge_tlist (pbefore_sp, tmp_before, 0);
1552       merge_tlist (pbefore_sp, tmp_nosp, 0);
1553       verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
1554       merge_tlist (pbefore_sp, tmp_list3, 0);
1555       return;
1556
1557     case COND_EXPR:
1558       tmp_before = tmp_list2 = 0;
1559       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1560       warn_for_collisions (tmp_list2);
1561       merge_tlist (pbefore_sp, tmp_before, 0);
1562       merge_tlist (pbefore_sp, tmp_list2, 1);
1563
1564       tmp_list3 = tmp_nosp = 0;
1565       verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1566       warn_for_collisions (tmp_nosp);
1567       merge_tlist (pbefore_sp, tmp_list3, 0);
1568
1569       tmp_list3 = tmp_list2 = 0;
1570       verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1571       warn_for_collisions (tmp_list2);
1572       merge_tlist (pbefore_sp, tmp_list3, 0);
1573       /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1574          two first, to avoid warning for (a ? b++ : b++).  */
1575       merge_tlist (&tmp_nosp, tmp_list2, 0);
1576       add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1577       return;
1578
1579     case PREDECREMENT_EXPR:
1580     case PREINCREMENT_EXPR:
1581     case POSTDECREMENT_EXPR:
1582     case POSTINCREMENT_EXPR:
1583       verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1584       return;
1585
1586     case MODIFY_EXPR:
1587       tmp_before = tmp_nosp = tmp_list3 = 0;
1588       verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1589       verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1590       /* Expressions inside the LHS are not ordered wrt. the sequence points
1591          in the RHS.  Example:
1592            *a = (a++, 2)
1593          Despite the fact that the modification of "a" is in the before_sp
1594          list (tmp_before), it conflicts with the use of "a" in the LHS.
1595          We can handle this by adding the contents of tmp_list3
1596          to those of tmp_before, and redoing the collision warnings for that
1597          list.  */
1598       add_tlist (&tmp_before, tmp_list3, x, 1);
1599       warn_for_collisions (tmp_before);
1600       /* Exclude the LHS itself here; we first have to merge it into the
1601          tmp_nosp list.  This is done to avoid warning for "a = a"; if we
1602          didn't exclude the LHS, we'd get it twice, once as a read and once
1603          as a write.  */
1604       add_tlist (pno_sp, tmp_list3, x, 0);
1605       warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1606
1607       merge_tlist (pbefore_sp, tmp_before, 0);
1608       if (warning_candidate_p (TREE_OPERAND (x, 0)))
1609         merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1610       add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1611       return;
1612
1613     case CALL_EXPR:
1614       /* We need to warn about conflicts among arguments and conflicts between
1615          args and the function address.  Side effects of the function address,
1616          however, are not ordered by the sequence point of the call.  */
1617       tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1618       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1619       if (TREE_OPERAND (x, 1))
1620         verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
1621       merge_tlist (&tmp_list3, tmp_list2, 0);
1622       add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1623       add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1624       warn_for_collisions (tmp_before);
1625       add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1626       return;
1627
1628     case TREE_LIST:
1629       /* Scan all the list, e.g. indices of multi dimensional array.  */
1630       while (x)
1631         {
1632           tmp_before = tmp_nosp = 0;
1633           verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1634           merge_tlist (&tmp_nosp, tmp_before, 0);
1635           add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1636           x = TREE_CHAIN (x);
1637         }
1638       return;
1639
1640     case SAVE_EXPR:
1641       {
1642         struct tlist_cache *t;
1643         for (t = save_expr_cache; t; t = t->next)
1644           if (t->expr == x)
1645             break;
1646
1647         if (! t)
1648           {
1649             t = obstack_alloc (&tlist_obstack, sizeof *t);
1650             t->next = save_expr_cache;
1651             t->expr = x;
1652             save_expr_cache = t;
1653
1654             tmp_before = tmp_nosp = 0;
1655             verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1656             warn_for_collisions (tmp_nosp);
1657
1658             tmp_list3 = 0;
1659             while (tmp_nosp)
1660               {
1661                 struct tlist *t = tmp_nosp;
1662                 tmp_nosp = t->next;
1663                 merge_tlist (&tmp_list3, t, 0);
1664               }
1665             t->cache_before_sp = tmp_before;
1666             t->cache_after_sp = tmp_list3;
1667           }
1668         merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1669         add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1670         return;
1671       }
1672     default:
1673       break;
1674     }
1675
1676   if (class == '1')
1677     {
1678       if (first_rtl_op (code) == 0)
1679         return;
1680       x = TREE_OPERAND (x, 0);
1681       writer = 0;
1682       goto restart;
1683     }
1684
1685   switch (class)
1686     {
1687     case 'r':
1688     case '<':
1689     case '2':
1690     case 'b':
1691     case 'e':
1692     case 's':
1693     case 'x':
1694       {
1695         int lp;
1696         int max = first_rtl_op (TREE_CODE (x));
1697         for (lp = 0; lp < max; lp++)
1698           {
1699             tmp_before = tmp_nosp = 0;
1700             verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
1701             merge_tlist (&tmp_nosp, tmp_before, 0);
1702             add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1703           }
1704         break;
1705       }
1706     }
1707 }
1708
1709 /* Try to warn for undefined behavior in EXPR due to missing sequence
1710    points.  */
1711
1712 static void
1713 verify_sequence_points (tree expr)
1714 {
1715   struct tlist *before_sp = 0, *after_sp = 0;
1716
1717   warned_ids = 0;
1718   save_expr_cache = 0;
1719   if (tlist_firstobj == 0)
1720     {
1721       gcc_obstack_init (&tlist_obstack);
1722       tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
1723     }
1724
1725   verify_tree (expr, &before_sp, &after_sp, 0);
1726   warn_for_collisions (after_sp);
1727   obstack_free (&tlist_obstack, tlist_firstobj);
1728 }
1729
1730 tree
1731 c_expand_expr_stmt (tree expr)
1732 {
1733   /* Do default conversion if safe and possibly important,
1734      in case within ({...}).  */
1735   if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
1736        && (flag_isoc99 || lvalue_p (expr)))
1737       || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1738     expr = default_conversion (expr);
1739
1740   if (warn_sequence_point)
1741     verify_sequence_points (expr);
1742
1743   if (TREE_TYPE (expr) != error_mark_node
1744       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
1745       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1746     error ("expression statement has incomplete type");
1747
1748   last_expr_type = TREE_TYPE (expr);
1749   return add_stmt (build_stmt (EXPR_STMT, expr));
1750 }
1751 \f
1752 /* Validate the expression after `case' and apply default promotions.  */
1753
1754 tree
1755 check_case_value (tree value)
1756 {
1757   if (value == NULL_TREE)
1758     return value;
1759
1760   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
1761   STRIP_TYPE_NOPS (value);
1762   /* In C++, the following is allowed:
1763
1764        const int i = 3;
1765        switch (...) { case i: ... }
1766
1767      So, we try to reduce the VALUE to a constant that way.  */
1768   if (c_dialect_cxx ())
1769     {
1770       value = decl_constant_value (value);
1771       STRIP_TYPE_NOPS (value);
1772       value = fold (value);
1773     }
1774
1775   if (TREE_CODE (value) != INTEGER_CST
1776       && value != error_mark_node)
1777     {
1778       error ("case label does not reduce to an integer constant");
1779       value = error_mark_node;
1780     }
1781   else
1782     /* Promote char or short to int.  */
1783     value = default_conversion (value);
1784
1785   constant_expression_warning (value);
1786
1787   return value;
1788 }
1789 \f
1790 /* Return an integer type with BITS bits of precision,
1791    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
1792
1793 tree
1794 c_common_type_for_size (unsigned int bits, int unsignedp)
1795 {
1796   if (bits == TYPE_PRECISION (integer_type_node))
1797     return unsignedp ? unsigned_type_node : integer_type_node;
1798
1799   if (bits == TYPE_PRECISION (signed_char_type_node))
1800     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1801
1802   if (bits == TYPE_PRECISION (short_integer_type_node))
1803     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1804
1805   if (bits == TYPE_PRECISION (long_integer_type_node))
1806     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1807
1808   if (bits == TYPE_PRECISION (long_long_integer_type_node))
1809     return (unsignedp ? long_long_unsigned_type_node
1810             : long_long_integer_type_node);
1811
1812   if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1813     return (unsignedp ? widest_unsigned_literal_type_node
1814             : widest_integer_literal_type_node);
1815
1816   if (bits <= TYPE_PRECISION (intQI_type_node))
1817     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1818
1819   if (bits <= TYPE_PRECISION (intHI_type_node))
1820     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1821
1822   if (bits <= TYPE_PRECISION (intSI_type_node))
1823     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1824
1825   if (bits <= TYPE_PRECISION (intDI_type_node))
1826     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1827
1828   return 0;
1829 }
1830
1831 /* Return a data type that has machine mode MODE.
1832    If the mode is an integer,
1833    then UNSIGNEDP selects between signed and unsigned types.  */
1834
1835 tree
1836 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
1837 {
1838   if (mode == TYPE_MODE (integer_type_node))
1839     return unsignedp ? unsigned_type_node : integer_type_node;
1840
1841   if (mode == TYPE_MODE (signed_char_type_node))
1842     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1843
1844   if (mode == TYPE_MODE (short_integer_type_node))
1845     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1846
1847   if (mode == TYPE_MODE (long_integer_type_node))
1848     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1849
1850   if (mode == TYPE_MODE (long_long_integer_type_node))
1851     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1852
1853   if (mode == TYPE_MODE (widest_integer_literal_type_node))
1854     return unsignedp ? widest_unsigned_literal_type_node
1855                      : widest_integer_literal_type_node;
1856
1857   if (mode == QImode)
1858     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1859
1860   if (mode == HImode)
1861     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1862
1863   if (mode == SImode)
1864     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1865
1866   if (mode == DImode)
1867     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1868
1869 #if HOST_BITS_PER_WIDE_INT >= 64
1870   if (mode == TYPE_MODE (intTI_type_node))
1871     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1872 #endif
1873
1874   if (mode == TYPE_MODE (float_type_node))
1875     return float_type_node;
1876
1877   if (mode == TYPE_MODE (double_type_node))
1878     return double_type_node;
1879
1880   if (mode == TYPE_MODE (long_double_type_node))
1881     return long_double_type_node;
1882
1883   if (mode == TYPE_MODE (void_type_node))
1884     return void_type_node;
1885   
1886   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1887     return unsignedp ? make_unsigned_type (mode) : make_signed_type (mode);
1888
1889   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1890     return unsignedp ? make_unsigned_type (mode) : make_signed_type (mode);
1891
1892   switch (mode)
1893     {
1894     case V16QImode:
1895       return unsignedp ? unsigned_V16QI_type_node : V16QI_type_node;
1896     case V8HImode:
1897       return unsignedp ? unsigned_V8HI_type_node : V8HI_type_node;
1898     case V4SImode:
1899       return unsignedp ? unsigned_V4SI_type_node : V4SI_type_node;
1900     case V2DImode:
1901       return unsignedp ? unsigned_V2DI_type_node : V2DI_type_node;
1902     case V2SImode:
1903       return unsignedp ? unsigned_V2SI_type_node : V2SI_type_node;
1904     case V2HImode:
1905       return unsignedp ? unsigned_V2HI_type_node : V2HI_type_node;
1906     case V4HImode:
1907       return unsignedp ? unsigned_V4HI_type_node : V4HI_type_node;
1908     case V8QImode:
1909       return unsignedp ? unsigned_V8QI_type_node : V8QI_type_node;
1910     case V1DImode:
1911       return unsignedp ? unsigned_V1DI_type_node : V1DI_type_node;
1912     case V16SFmode:
1913       return V16SF_type_node;
1914     case V4SFmode:
1915       return V4SF_type_node;
1916     case V2SFmode:
1917       return V2SF_type_node;
1918     case V2DFmode:
1919       return V2DF_type_node;
1920     case V4DFmode:
1921       return V4DF_type_node;
1922     default:
1923       break;
1924     }
1925
1926   return 0;
1927 }
1928
1929 /* Return an unsigned type the same as TYPE in other respects.  */
1930 tree
1931 c_common_unsigned_type (tree type)
1932 {
1933   tree type1 = TYPE_MAIN_VARIANT (type);
1934   if (type1 == signed_char_type_node || type1 == char_type_node)
1935     return unsigned_char_type_node;
1936   if (type1 == integer_type_node)
1937     return unsigned_type_node;
1938   if (type1 == short_integer_type_node)
1939     return short_unsigned_type_node;
1940   if (type1 == long_integer_type_node)
1941     return long_unsigned_type_node;
1942   if (type1 == long_long_integer_type_node)
1943     return long_long_unsigned_type_node;
1944   if (type1 == widest_integer_literal_type_node)
1945     return widest_unsigned_literal_type_node;
1946 #if HOST_BITS_PER_WIDE_INT >= 64
1947   if (type1 == intTI_type_node)
1948     return unsigned_intTI_type_node;
1949 #endif
1950   if (type1 == intDI_type_node)
1951     return unsigned_intDI_type_node;
1952   if (type1 == intSI_type_node)
1953     return unsigned_intSI_type_node;
1954   if (type1 == intHI_type_node)
1955     return unsigned_intHI_type_node;
1956   if (type1 == intQI_type_node)
1957     return unsigned_intQI_type_node;
1958
1959   return c_common_signed_or_unsigned_type (1, type);
1960 }
1961
1962 /* Return a signed type the same as TYPE in other respects.  */
1963
1964 tree
1965 c_common_signed_type (tree type)
1966 {
1967   tree type1 = TYPE_MAIN_VARIANT (type);
1968   if (type1 == unsigned_char_type_node || type1 == char_type_node)
1969     return signed_char_type_node;
1970   if (type1 == unsigned_type_node)
1971     return integer_type_node;
1972   if (type1 == short_unsigned_type_node)
1973     return short_integer_type_node;
1974   if (type1 == long_unsigned_type_node)
1975     return long_integer_type_node;
1976   if (type1 == long_long_unsigned_type_node)
1977     return long_long_integer_type_node;
1978   if (type1 == widest_unsigned_literal_type_node)
1979     return widest_integer_literal_type_node;
1980 #if HOST_BITS_PER_WIDE_INT >= 64
1981   if (type1 == unsigned_intTI_type_node)
1982     return intTI_type_node;
1983 #endif
1984   if (type1 == unsigned_intDI_type_node)
1985     return intDI_type_node;
1986   if (type1 == unsigned_intSI_type_node)
1987     return intSI_type_node;
1988   if (type1 == unsigned_intHI_type_node)
1989     return intHI_type_node;
1990   if (type1 == unsigned_intQI_type_node)
1991     return intQI_type_node;
1992
1993   return c_common_signed_or_unsigned_type (0, type);
1994 }
1995
1996 /* Return a type the same as TYPE except unsigned or
1997    signed according to UNSIGNEDP.  */
1998
1999 tree
2000 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2001 {
2002   if (! INTEGRAL_TYPE_P (type)
2003       || TREE_UNSIGNED (type) == unsignedp)
2004     return type;
2005
2006   /* Must check the mode of the types, not the precision.  Enumeral types
2007      in C++ have precision set to match their range, but may use a wider
2008      mode to match an ABI.  If we change modes, we may wind up with bad
2009      conversions.  */
2010
2011   if (TYPE_MODE (type) == TYPE_MODE (signed_char_type_node))
2012     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2013   if (TYPE_MODE (type) == TYPE_MODE (integer_type_node))
2014     return unsignedp ? unsigned_type_node : integer_type_node;
2015   if (TYPE_MODE (type) == TYPE_MODE (short_integer_type_node))
2016     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2017   if (TYPE_MODE (type) == TYPE_MODE (long_integer_type_node))
2018     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2019   if (TYPE_MODE (type) == TYPE_MODE (long_long_integer_type_node))
2020     return (unsignedp ? long_long_unsigned_type_node
2021             : long_long_integer_type_node);
2022   if (TYPE_MODE (type) == TYPE_MODE (widest_integer_literal_type_node))
2023     return (unsignedp ? widest_unsigned_literal_type_node
2024             : widest_integer_literal_type_node);
2025
2026 #if HOST_BITS_PER_WIDE_INT >= 64
2027   if (TYPE_MODE (type) == TYPE_MODE (intTI_type_node))
2028     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2029 #endif
2030   if (TYPE_MODE (type) == TYPE_MODE (intDI_type_node))
2031     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2032   if (TYPE_MODE (type) == TYPE_MODE (intSI_type_node))
2033     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2034   if (TYPE_MODE (type) == TYPE_MODE (intHI_type_node))
2035     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2036   if (TYPE_MODE (type) == TYPE_MODE (intQI_type_node))
2037     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2038
2039   return type;
2040 }
2041
2042 /* The C version of the register_builtin_type langhook.  */
2043
2044 void
2045 c_register_builtin_type (tree type, const char* name)
2046 {
2047   tree decl;
2048
2049   decl = build_decl (TYPE_DECL, get_identifier (name), type);
2050   DECL_ARTIFICIAL (decl) = 1;
2051   if (!TYPE_NAME (type))
2052     TYPE_NAME (type) = decl;
2053   pushdecl (decl);
2054 }
2055
2056 \f
2057 /* Return the minimum number of bits needed to represent VALUE in a
2058    signed or unsigned type, UNSIGNEDP says which.  */
2059
2060 unsigned int
2061 min_precision (tree value, int unsignedp)
2062 {
2063   int log;
2064
2065   /* If the value is negative, compute its negative minus 1.  The latter
2066      adjustment is because the absolute value of the largest negative value
2067      is one larger than the largest positive value.  This is equivalent to
2068      a bit-wise negation, so use that operation instead.  */
2069
2070   if (tree_int_cst_sgn (value) < 0)
2071     value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2072
2073   /* Return the number of bits needed, taking into account the fact
2074      that we need one more bit for a signed than unsigned type.  */
2075
2076   if (integer_zerop (value))
2077     log = 0;
2078   else
2079     log = tree_floor_log2 (value);
2080
2081   return log + 1 + ! unsignedp;
2082 }
2083 \f
2084 /* Print an error message for invalid operands to arith operation
2085    CODE.  NOP_EXPR is used as a special case (see
2086    c_common_truthvalue_conversion).  */
2087
2088 void
2089 binary_op_error (enum tree_code code)
2090 {
2091   const char *opname;
2092
2093   switch (code)
2094     {
2095     case NOP_EXPR:
2096       error ("invalid truth-value expression");
2097       return;
2098
2099     case PLUS_EXPR:
2100       opname = "+"; break;
2101     case MINUS_EXPR:
2102       opname = "-"; break;
2103     case MULT_EXPR:
2104       opname = "*"; break;
2105     case MAX_EXPR:
2106       opname = "max"; break;
2107     case MIN_EXPR:
2108       opname = "min"; break;
2109     case EQ_EXPR:
2110       opname = "=="; break;
2111     case NE_EXPR:
2112       opname = "!="; break;
2113     case LE_EXPR:
2114       opname = "<="; break;
2115     case GE_EXPR:
2116       opname = ">="; break;
2117     case LT_EXPR:
2118       opname = "<"; break;
2119     case GT_EXPR:
2120       opname = ">"; break;
2121     case LSHIFT_EXPR:
2122       opname = "<<"; break;
2123     case RSHIFT_EXPR:
2124       opname = ">>"; break;
2125     case TRUNC_MOD_EXPR:
2126     case FLOOR_MOD_EXPR:
2127       opname = "%"; break;
2128     case TRUNC_DIV_EXPR:
2129     case FLOOR_DIV_EXPR:
2130       opname = "/"; break;
2131     case BIT_AND_EXPR:
2132       opname = "&"; break;
2133     case BIT_IOR_EXPR:
2134       opname = "|"; break;
2135     case TRUTH_ANDIF_EXPR:
2136       opname = "&&"; break;
2137     case TRUTH_ORIF_EXPR:
2138       opname = "||"; break;
2139     case BIT_XOR_EXPR:
2140       opname = "^"; break;
2141     case LROTATE_EXPR:
2142     case RROTATE_EXPR:
2143       opname = "rotate"; break;
2144     default:
2145       opname = "unknown"; break;
2146     }
2147   error ("invalid operands to binary %s", opname);
2148 }
2149 \f
2150 /* Subroutine of build_binary_op, used for comparison operations.
2151    See if the operands have both been converted from subword integer types
2152    and, if so, perhaps change them both back to their original type.
2153    This function is also responsible for converting the two operands
2154    to the proper common type for comparison.
2155
2156    The arguments of this function are all pointers to local variables
2157    of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2158    RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2159
2160    If this function returns nonzero, it means that the comparison has
2161    a constant value.  What this function returns is an expression for
2162    that value.  */
2163
2164 tree
2165 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
2166                  enum tree_code *rescode_ptr)
2167 {
2168   tree type;
2169   tree op0 = *op0_ptr;
2170   tree op1 = *op1_ptr;
2171   int unsignedp0, unsignedp1;
2172   int real1, real2;
2173   tree primop0, primop1;
2174   enum tree_code code = *rescode_ptr;
2175
2176   /* Throw away any conversions to wider types
2177      already present in the operands.  */
2178
2179   primop0 = get_narrower (op0, &unsignedp0);
2180   primop1 = get_narrower (op1, &unsignedp1);
2181
2182   /* Handle the case that OP0 does not *contain* a conversion
2183      but it *requires* conversion to FINAL_TYPE.  */
2184
2185   if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2186     unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2187   if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2188     unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2189
2190   /* If one of the operands must be floated, we cannot optimize.  */
2191   real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2192   real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2193
2194   /* If first arg is constant, swap the args (changing operation
2195      so value is preserved), for canonicalization.  Don't do this if
2196      the second arg is 0.  */
2197
2198   if (TREE_CONSTANT (primop0)
2199       && ! integer_zerop (primop1) && ! real_zerop (primop1))
2200     {
2201       tree tem = primop0;
2202       int temi = unsignedp0;
2203       primop0 = primop1;
2204       primop1 = tem;
2205       tem = op0;
2206       op0 = op1;
2207       op1 = tem;
2208       *op0_ptr = op0;
2209       *op1_ptr = op1;
2210       unsignedp0 = unsignedp1;
2211       unsignedp1 = temi;
2212       temi = real1;
2213       real1 = real2;
2214       real2 = temi;
2215
2216       switch (code)
2217         {
2218         case LT_EXPR:
2219           code = GT_EXPR;
2220           break;
2221         case GT_EXPR:
2222           code = LT_EXPR;
2223           break;
2224         case LE_EXPR:
2225           code = GE_EXPR;
2226           break;
2227         case GE_EXPR:
2228           code = LE_EXPR;
2229           break;
2230         default:
2231           break;
2232         }
2233       *rescode_ptr = code;
2234     }
2235
2236   /* If comparing an integer against a constant more bits wide,
2237      maybe we can deduce a value of 1 or 0 independent of the data.
2238      Or else truncate the constant now
2239      rather than extend the variable at run time.
2240
2241      This is only interesting if the constant is the wider arg.
2242      Also, it is not safe if the constant is unsigned and the
2243      variable arg is signed, since in this case the variable
2244      would be sign-extended and then regarded as unsigned.
2245      Our technique fails in this case because the lowest/highest
2246      possible unsigned results don't follow naturally from the
2247      lowest/highest possible values of the variable operand.
2248      For just EQ_EXPR and NE_EXPR there is another technique that
2249      could be used: see if the constant can be faithfully represented
2250      in the other operand's type, by truncating it and reextending it
2251      and see if that preserves the constant's value.  */
2252
2253   if (!real1 && !real2
2254       && TREE_CODE (primop1) == INTEGER_CST
2255       && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2256     {
2257       int min_gt, max_gt, min_lt, max_lt;
2258       tree maxval, minval;
2259       /* 1 if comparison is nominally unsigned.  */
2260       int unsignedp = TREE_UNSIGNED (*restype_ptr);
2261       tree val;
2262
2263       type = c_common_signed_or_unsigned_type (unsignedp0,
2264                                                TREE_TYPE (primop0));
2265
2266       /* In C, if TYPE is an enumeration, then we need to get its
2267          min/max values from it's underlying integral type, not the
2268          enumerated type itself.  In C++, TYPE_MAX_VALUE and
2269          TYPE_MIN_VALUE have already been set correctly on the
2270          enumeration type.  */
2271       if (!c_dialect_cxx() && TREE_CODE (type) == ENUMERAL_TYPE)
2272         type = c_common_type_for_size (TYPE_PRECISION (type), unsignedp0);
2273
2274       maxval = TYPE_MAX_VALUE (type);
2275       minval = TYPE_MIN_VALUE (type);
2276
2277       if (unsignedp && !unsignedp0)
2278         *restype_ptr = c_common_signed_type (*restype_ptr);
2279
2280       if (TREE_TYPE (primop1) != *restype_ptr)
2281         primop1 = convert (*restype_ptr, primop1);
2282       if (type != *restype_ptr)
2283         {
2284           minval = convert (*restype_ptr, minval);
2285           maxval = convert (*restype_ptr, maxval);
2286         }
2287
2288       if (unsignedp && unsignedp0)
2289         {
2290           min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2291           max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2292           min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2293           max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2294         }
2295       else
2296         {
2297           min_gt = INT_CST_LT (primop1, minval);
2298           max_gt = INT_CST_LT (primop1, maxval);
2299           min_lt = INT_CST_LT (minval, primop1);
2300           max_lt = INT_CST_LT (maxval, primop1);
2301         }
2302
2303       val = 0;
2304       /* This used to be a switch, but Genix compiler can't handle that.  */
2305       if (code == NE_EXPR)
2306         {
2307           if (max_lt || min_gt)
2308             val = truthvalue_true_node;
2309         }
2310       else if (code == EQ_EXPR)
2311         {
2312           if (max_lt || min_gt)
2313             val = truthvalue_false_node;
2314         }
2315       else if (code == LT_EXPR)
2316         {
2317           if (max_lt)
2318             val = truthvalue_true_node;
2319           if (!min_lt)
2320             val = truthvalue_false_node;
2321         }
2322       else if (code == GT_EXPR)
2323         {
2324           if (min_gt)
2325             val = truthvalue_true_node;
2326           if (!max_gt)
2327             val = truthvalue_false_node;
2328         }
2329       else if (code == LE_EXPR)
2330         {
2331           if (!max_gt)
2332             val = truthvalue_true_node;
2333           if (min_gt)
2334             val = truthvalue_false_node;
2335         }
2336       else if (code == GE_EXPR)
2337         {
2338           if (!min_lt)
2339             val = truthvalue_true_node;
2340           if (max_lt)
2341             val = truthvalue_false_node;
2342         }
2343
2344       /* If primop0 was sign-extended and unsigned comparison specd,
2345          we did a signed comparison above using the signed type bounds.
2346          But the comparison we output must be unsigned.
2347
2348          Also, for inequalities, VAL is no good; but if the signed
2349          comparison had *any* fixed result, it follows that the
2350          unsigned comparison just tests the sign in reverse
2351          (positive values are LE, negative ones GE).
2352          So we can generate an unsigned comparison
2353          against an extreme value of the signed type.  */
2354
2355       if (unsignedp && !unsignedp0)
2356         {
2357           if (val != 0)
2358             switch (code)
2359               {
2360               case LT_EXPR:
2361               case GE_EXPR:
2362                 primop1 = TYPE_MIN_VALUE (type);
2363                 val = 0;
2364                 break;
2365
2366               case LE_EXPR:
2367               case GT_EXPR:
2368                 primop1 = TYPE_MAX_VALUE (type);
2369                 val = 0;
2370                 break;
2371
2372               default:
2373                 break;
2374               }
2375           type = c_common_unsigned_type (type);
2376         }
2377
2378       if (TREE_CODE (primop0) != INTEGER_CST)
2379         {
2380           if (val == truthvalue_false_node)
2381             warning ("comparison is always false due to limited range of data type");
2382           if (val == truthvalue_true_node)
2383             warning ("comparison is always true due to limited range of data type");
2384         }
2385
2386       if (val != 0)
2387         {
2388           /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
2389           if (TREE_SIDE_EFFECTS (primop0))
2390             return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2391           return val;
2392         }
2393
2394       /* Value is not predetermined, but do the comparison
2395          in the type of the operand that is not constant.
2396          TYPE is already properly set.  */
2397     }
2398   else if (real1 && real2
2399            && (TYPE_PRECISION (TREE_TYPE (primop0))
2400                == TYPE_PRECISION (TREE_TYPE (primop1))))
2401     type = TREE_TYPE (primop0);
2402
2403   /* If args' natural types are both narrower than nominal type
2404      and both extend in the same manner, compare them
2405      in the type of the wider arg.
2406      Otherwise must actually extend both to the nominal
2407      common type lest different ways of extending
2408      alter the result.
2409      (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
2410
2411   else if (unsignedp0 == unsignedp1 && real1 == real2
2412            && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2413            && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2414     {
2415       type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2416       type = c_common_signed_or_unsigned_type (unsignedp0
2417                                                || TREE_UNSIGNED (*restype_ptr),
2418                                                type);
2419       /* Make sure shorter operand is extended the right way
2420          to match the longer operand.  */
2421       primop0
2422         = convert (c_common_signed_or_unsigned_type (unsignedp0,
2423                                                      TREE_TYPE (primop0)),
2424                    primop0);
2425       primop1
2426         = convert (c_common_signed_or_unsigned_type (unsignedp1,
2427                                                      TREE_TYPE (primop1)),
2428                    primop1);
2429     }
2430   else
2431     {
2432       /* Here we must do the comparison on the nominal type
2433          using the args exactly as we received them.  */
2434       type = *restype_ptr;
2435       primop0 = op0;
2436       primop1 = op1;
2437
2438       if (!real1 && !real2 && integer_zerop (primop1)
2439           && TREE_UNSIGNED (*restype_ptr))
2440         {
2441           tree value = 0;
2442           switch (code)
2443             {
2444             case GE_EXPR:
2445               /* All unsigned values are >= 0, so we warn if extra warnings
2446                  are requested.  However, if OP0 is a constant that is
2447                  >= 0, the signedness of the comparison isn't an issue,
2448                  so suppress the warning.  */
2449               if (extra_warnings && !in_system_header
2450                   && ! (TREE_CODE (primop0) == INTEGER_CST
2451                         && ! TREE_OVERFLOW (convert (c_common_signed_type (type),
2452                                                      primop0))))
2453                 warning ("comparison of unsigned expression >= 0 is always true");
2454               value = truthvalue_true_node;
2455               break;
2456
2457             case LT_EXPR:
2458               if (extra_warnings && !in_system_header
2459                   && ! (TREE_CODE (primop0) == INTEGER_CST
2460                         && ! TREE_OVERFLOW (convert (c_common_signed_type (type),
2461                                                      primop0))))
2462                 warning ("comparison of unsigned expression < 0 is always false");
2463               value = truthvalue_false_node;
2464               break;
2465
2466             default:
2467               break;
2468             }
2469
2470           if (value != 0)
2471             {
2472               /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
2473               if (TREE_SIDE_EFFECTS (primop0))
2474                 return build (COMPOUND_EXPR, TREE_TYPE (value),
2475                               primop0, value);
2476               return value;
2477             }
2478         }
2479     }
2480
2481   *op0_ptr = convert (type, primop0);
2482   *op1_ptr = convert (type, primop1);
2483
2484   *restype_ptr = truthvalue_type_node;
2485
2486   return 0;
2487 }
2488 \f
2489 /* Return a tree for the sum or difference (RESULTCODE says which)
2490    of pointer PTROP and integer INTOP.  */
2491
2492 tree
2493 pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
2494 {
2495   tree size_exp;
2496
2497   tree result;
2498   tree folded;
2499
2500   /* The result is a pointer of the same type that is being added.  */
2501
2502   tree result_type = TREE_TYPE (ptrop);
2503
2504   if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2505     {
2506       if (pedantic || warn_pointer_arith)
2507         pedwarn ("pointer of type `void *' used in arithmetic");
2508       size_exp = integer_one_node;
2509     }
2510   else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2511     {
2512       if (pedantic || warn_pointer_arith)
2513         pedwarn ("pointer to a function used in arithmetic");
2514       size_exp = integer_one_node;
2515     }
2516   else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
2517     {
2518       if (pedantic || warn_pointer_arith)
2519         pedwarn ("pointer to member function used in arithmetic");
2520       size_exp = integer_one_node;
2521     }
2522   else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
2523     {
2524       if (pedantic || warn_pointer_arith)
2525         pedwarn ("pointer to a member used in arithmetic");
2526       size_exp = integer_one_node;
2527     }
2528   else
2529     size_exp = size_in_bytes (TREE_TYPE (result_type));
2530
2531   /* If what we are about to multiply by the size of the elements
2532      contains a constant term, apply distributive law
2533      and multiply that constant term separately.
2534      This helps produce common subexpressions.  */
2535
2536   if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2537       && ! TREE_CONSTANT (intop)
2538       && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2539       && TREE_CONSTANT (size_exp)
2540       /* If the constant comes from pointer subtraction,
2541          skip this optimization--it would cause an error.  */
2542       && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2543       /* If the constant is unsigned, and smaller than the pointer size,
2544          then we must skip this optimization.  This is because it could cause
2545          an overflow error if the constant is negative but INTOP is not.  */
2546       && (! TREE_UNSIGNED (TREE_TYPE (intop))
2547           || (TYPE_PRECISION (TREE_TYPE (intop))
2548               == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2549     {
2550       enum tree_code subcode = resultcode;
2551       tree int_type = TREE_TYPE (intop);
2552       if (TREE_CODE (intop) == MINUS_EXPR)
2553         subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2554       /* Convert both subexpression types to the type of intop,
2555          because weird cases involving pointer arithmetic
2556          can result in a sum or difference with different type args.  */
2557       ptrop = build_binary_op (subcode, ptrop,
2558                                convert (int_type, TREE_OPERAND (intop, 1)), 1);
2559       intop = convert (int_type, TREE_OPERAND (intop, 0));
2560     }
2561
2562   /* Convert the integer argument to a type the same size as sizetype
2563      so the multiply won't overflow spuriously.  */
2564
2565   if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2566       || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2567     intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
2568                                              TREE_UNSIGNED (sizetype)), intop);
2569
2570   /* Replace the integer argument with a suitable product by the object size.
2571      Do this multiplication as signed, then convert to the appropriate
2572      pointer type (actually unsigned integral).  */
2573
2574   intop = convert (result_type,
2575                    build_binary_op (MULT_EXPR, intop,
2576                                     convert (TREE_TYPE (intop), size_exp), 1));
2577
2578   /* Create the sum or difference.  */
2579
2580   result = build (resultcode, result_type, ptrop, intop);
2581
2582   folded = fold (result);
2583   if (folded == result)
2584     TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2585   return folded;
2586 }
2587 \f
2588 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2589    or validate its data type for an `if' or `while' statement or ?..: exp.
2590
2591    This preparation consists of taking the ordinary
2592    representation of an expression expr and producing a valid tree
2593    boolean expression describing whether expr is nonzero.  We could
2594    simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
2595    but we optimize comparisons, &&, ||, and !.
2596
2597    The resulting type should always be `truthvalue_type_node'.  */
2598
2599 tree
2600 c_common_truthvalue_conversion (tree expr)
2601 {
2602   if (TREE_CODE (expr) == ERROR_MARK)
2603     return expr;
2604
2605 #if 0 /* This appears to be wrong for C++.  */
2606   /* These really should return error_mark_node after 2.4 is stable.
2607      But not all callers handle ERROR_MARK properly.  */
2608   switch (TREE_CODE (TREE_TYPE (expr)))
2609     {
2610     case RECORD_TYPE:
2611       error ("struct type value used where scalar is required");
2612       return truthvalue_false_node;
2613
2614     case UNION_TYPE:
2615       error ("union type value used where scalar is required");
2616       return truthvalue_false_node;
2617
2618     case ARRAY_TYPE:
2619       error ("array type value used where scalar is required");
2620       return truthvalue_false_node;
2621
2622     default:
2623       break;
2624     }
2625 #endif /* 0 */
2626
2627   switch (TREE_CODE (expr))
2628     {
2629     case EQ_EXPR:
2630     case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2631     case TRUTH_ANDIF_EXPR:
2632     case TRUTH_ORIF_EXPR:
2633     case TRUTH_AND_EXPR:
2634     case TRUTH_OR_EXPR:
2635     case TRUTH_XOR_EXPR:
2636     case TRUTH_NOT_EXPR:
2637       TREE_TYPE (expr) = truthvalue_type_node;
2638       return expr;
2639
2640     case ERROR_MARK:
2641       return expr;
2642
2643     case INTEGER_CST:
2644       return integer_zerop (expr) ? truthvalue_false_node : truthvalue_true_node;
2645
2646     case REAL_CST:
2647       return real_zerop (expr) ? truthvalue_false_node : truthvalue_true_node;
2648
2649     case ADDR_EXPR:
2650       /* If we are taking the address of an external decl, it might be zero
2651          if it is weak, so we cannot optimize.  */
2652       if (DECL_P (TREE_OPERAND (expr, 0))
2653           && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2654         break;
2655
2656       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2657         return build (COMPOUND_EXPR, truthvalue_type_node,
2658                       TREE_OPERAND (expr, 0), truthvalue_true_node);
2659       else
2660         return truthvalue_true_node;
2661
2662     case COMPLEX_EXPR:
2663       return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2664                                ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2665                 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)),
2666                 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
2667                               0);
2668
2669     case NEGATE_EXPR:
2670     case ABS_EXPR:
2671     case FLOAT_EXPR:
2672       /* These don't change whether an object is nonzero or zero.  */
2673       return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
2674
2675     case LROTATE_EXPR:
2676     case RROTATE_EXPR:
2677       /* These don't change whether an object is zero or nonzero, but
2678          we can't ignore them if their second arg has side-effects.  */
2679       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2680         return build (COMPOUND_EXPR, truthvalue_type_node, TREE_OPERAND (expr, 1),
2681                       c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)));
2682       else
2683         return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
2684
2685     case COND_EXPR:
2686       /* Distribute the conversion into the arms of a COND_EXPR.  */
2687       return fold (build (COND_EXPR, truthvalue_type_node, TREE_OPERAND (expr, 0),
2688                 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
2689                 c_common_truthvalue_conversion (TREE_OPERAND (expr, 2))));
2690
2691     case CONVERT_EXPR:
2692       /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2693          since that affects how `default_conversion' will behave.  */
2694       if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2695           || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2696         break;
2697       /* Fall through....  */
2698     case NOP_EXPR:
2699       /* If this is widening the argument, we can ignore it.  */
2700       if (TYPE_PRECISION (TREE_TYPE (expr))
2701           >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2702         return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
2703       break;
2704
2705     case MINUS_EXPR:
2706       /* Perhaps reduce (x - y) != 0 to (x != y).  The expressions
2707          aren't guaranteed to the be same for modes that can represent
2708          infinity, since if x and y are both +infinity, or both
2709          -infinity, then x - y is not a number.
2710
2711          Note that this transformation is safe when x or y is NaN.
2712          (x - y) is then NaN, and both (x - y) != 0 and x != y will
2713          be false.  */
2714       if (HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0)))))
2715         break;
2716       /* Fall through....  */
2717     case BIT_XOR_EXPR:
2718       /* This and MINUS_EXPR can be changed into a comparison of the
2719          two objects.  */
2720       if (TREE_TYPE (TREE_OPERAND (expr, 0))
2721           == TREE_TYPE (TREE_OPERAND (expr, 1)))
2722         return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2723                                 TREE_OPERAND (expr, 1), 1);
2724       return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2725                               fold (build1 (NOP_EXPR,
2726                                             TREE_TYPE (TREE_OPERAND (expr, 0)),
2727                                             TREE_OPERAND (expr, 1))), 1);
2728
2729     case BIT_AND_EXPR:
2730       if (integer_onep (TREE_OPERAND (expr, 1))
2731           && TREE_TYPE (expr) != truthvalue_type_node)
2732         /* Using convert here would cause infinite recursion.  */
2733         return build1 (NOP_EXPR, truthvalue_type_node, expr);
2734       break;
2735
2736     case MODIFY_EXPR:
2737       if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2738         warning ("suggest parentheses around assignment used as truth value");
2739       break;
2740
2741     default:
2742       break;
2743     }
2744
2745   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2746     {
2747       tree t = save_expr (expr);
2748       return (build_binary_op
2749               ((TREE_SIDE_EFFECTS (expr)
2750                 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2751         c_common_truthvalue_conversion (build_unary_op (REALPART_EXPR, t, 0)),
2752         c_common_truthvalue_conversion (build_unary_op (IMAGPART_EXPR, t, 0)),
2753                0));
2754     }
2755
2756   return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2757 }
2758 \f
2759 static tree builtin_function_2 (const char *, const char *, tree, tree,
2760                                 int, enum built_in_class, int, int,
2761                                 tree);
2762
2763 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2764    down to the element type of an array.  */
2765
2766 tree
2767 c_build_qualified_type (tree type, int type_quals)
2768 {
2769   /* A restrict-qualified pointer type must be a pointer to object or
2770      incomplete type.  Note that the use of POINTER_TYPE_P also allows
2771      REFERENCE_TYPEs, which is appropriate for C++.  Unfortunately,
2772      the C++ front-end also use POINTER_TYPE for pointer-to-member
2773      values, so even though it should be illegal to use `restrict'
2774      with such an entity we don't flag that here.  Thus, special case
2775      code for that case is required in the C++ front-end.  */
2776   if ((type_quals & TYPE_QUAL_RESTRICT)
2777       && (!POINTER_TYPE_P (type)
2778           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
2779     {
2780       error ("invalid use of `restrict'");
2781       type_quals &= ~TYPE_QUAL_RESTRICT;
2782     }
2783
2784   if (TREE_CODE (type) == ARRAY_TYPE)
2785     return build_array_type (c_build_qualified_type (TREE_TYPE (type),
2786                                                      type_quals),
2787                              TYPE_DOMAIN (type));
2788   return build_qualified_type (type, type_quals);
2789 }
2790
2791 /* Apply the TYPE_QUALS to the new DECL.  */
2792
2793 void
2794 c_apply_type_quals_to_decl (int type_quals, tree decl)
2795 {
2796   if ((type_quals & TYPE_QUAL_CONST)
2797       || (TREE_TYPE (decl)
2798           && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
2799     TREE_READONLY (decl) = 1;
2800   if (type_quals & TYPE_QUAL_VOLATILE)
2801     {
2802       TREE_SIDE_EFFECTS (decl) = 1;
2803       TREE_THIS_VOLATILE (decl) = 1;
2804     }
2805   if (type_quals & TYPE_QUAL_RESTRICT)
2806     {
2807       if (!TREE_TYPE (decl)
2808           || !POINTER_TYPE_P (TREE_TYPE (decl))
2809           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
2810         error ("invalid use of `restrict'");
2811       else if (flag_strict_aliasing)
2812         /* Indicate we need to make a unique alias set for this pointer.
2813            We can't do it here because it might be pointing to an
2814            incomplete type.  */
2815         DECL_POINTER_ALIAS_SET (decl) = -2;
2816     }
2817 }
2818
2819 /* Return the typed-based alias set for T, which may be an expression
2820    or a type.  Return -1 if we don't do anything special.  */
2821
2822 HOST_WIDE_INT
2823 c_common_get_alias_set (tree t)
2824 {
2825   tree u;
2826
2827   /* Permit type-punning when accessing a union, provided the access
2828      is directly through the union.  For example, this code does not
2829      permit taking the address of a union member and then storing
2830      through it.  Even the type-punning allowed here is a GCC
2831      extension, albeit a common and useful one; the C standard says
2832      that such accesses have implementation-defined behavior.  */
2833   for (u = t;
2834        TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2835        u = TREE_OPERAND (u, 0))
2836     if (TREE_CODE (u) == COMPONENT_REF
2837         && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2838       return 0;
2839
2840   /* That's all the expressions we handle specially.  */
2841   if (! TYPE_P (t))
2842     return -1;
2843
2844   /* The C standard guarantees that any object may be accessed via an
2845      lvalue that has character type.  */
2846   if (t == char_type_node
2847       || t == signed_char_type_node
2848       || t == unsigned_char_type_node)
2849     return 0;
2850
2851   /* If it has the may_alias attribute, it can alias anything.  */
2852   if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
2853     return 0;
2854
2855   /* The C standard specifically allows aliasing between signed and
2856      unsigned variants of the same type.  We treat the signed
2857      variant as canonical.  */
2858   if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
2859     {
2860       tree t1 = c_common_signed_type (t);
2861
2862       /* t1 == t can happen for boolean nodes which are always unsigned.  */
2863       if (t1 != t)
2864         return get_alias_set (t1);
2865     }
2866   else if (POINTER_TYPE_P (t))
2867     {
2868       tree t1;
2869
2870       /* Unfortunately, there is no canonical form of a pointer type.
2871          In particular, if we have `typedef int I', then `int *', and
2872          `I *' are different types.  So, we have to pick a canonical
2873          representative.  We do this below.
2874
2875          Technically, this approach is actually more conservative that
2876          it needs to be.  In particular, `const int *' and `int *'
2877          should be in different alias sets, according to the C and C++
2878          standard, since their types are not the same, and so,
2879          technically, an `int **' and `const int **' cannot point at
2880          the same thing.
2881
2882          But, the standard is wrong.  In particular, this code is
2883          legal C++:
2884
2885             int *ip;
2886             int **ipp = &ip;
2887             const int* const* cipp = &ipp;
2888
2889          And, it doesn't make sense for that to be legal unless you
2890          can dereference IPP and CIPP.  So, we ignore cv-qualifiers on
2891          the pointed-to types.  This issue has been reported to the
2892          C++ committee.  */
2893       t1 = build_type_no_quals (t);
2894       if (t1 != t)
2895         return get_alias_set (t1);
2896     }
2897
2898   return -1;
2899 }
2900 \f
2901 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where the
2902    second parameter indicates which OPERATOR is being applied.  The COMPLAIN
2903    flag controls whether we should diagnose possibly ill-formed
2904    constructs or not.  */
2905 tree
2906 c_sizeof_or_alignof_type (tree type, enum tree_code op, int complain)
2907 {
2908   const char *op_name;
2909   tree value = NULL;
2910   enum tree_code type_code = TREE_CODE (type);
2911
2912   my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
2913   op_name = op == SIZEOF_EXPR ? "sizeof" : "__alignof__";
2914
2915   if (type_code == FUNCTION_TYPE)
2916     {
2917       if (op == SIZEOF_EXPR)
2918         {
2919           if (complain && (pedantic || warn_pointer_arith))
2920             pedwarn ("invalid application of `sizeof' to a function type");
2921           value = size_one_node;
2922         }
2923       else
2924         value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
2925     }
2926   else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
2927     {
2928       if (type_code == VOID_TYPE
2929           && complain && (pedantic || warn_pointer_arith))
2930         pedwarn ("invalid application of `%s' to a void type", op_name);
2931       value = size_one_node;
2932     }
2933   else if (!COMPLETE_TYPE_P (type))
2934     {
2935       if (complain)
2936         error ("invalid application of `%s' to an incomplete type", op_name);
2937       value = size_zero_node;
2938     }
2939   else
2940     {
2941       if (op == SIZEOF_EXPR)
2942         /* Convert in case a char is more than one unit.  */
2943         value = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2944                             size_int (TYPE_PRECISION (char_type_node)
2945                                       / BITS_PER_UNIT));
2946       else
2947         value = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
2948     }
2949
2950   /* VALUE will have an integer type with TYPE_IS_SIZETYPE set.
2951      TYPE_IS_SIZETYPE means that certain things (like overflow) will
2952      never happen.  However, this node should really have type
2953      `size_t', which is just a typedef for an ordinary integer type.  */
2954   value = fold (build1 (NOP_EXPR, size_type_node, value));
2955   my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)), 20001021);
2956
2957   return value;
2958 }
2959
2960 /* Implement the __alignof keyword: Return the minimum required
2961    alignment of EXPR, measured in bytes.  For VAR_DECL's and
2962    FIELD_DECL's return DECL_ALIGN (which can be set from an
2963    "aligned" __attribute__ specification).  */
2964
2965 tree
2966 c_alignof_expr (tree expr)
2967 {
2968   tree t;
2969
2970   if (TREE_CODE (expr) == VAR_DECL)
2971     t = size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
2972
2973   else if (TREE_CODE (expr) == COMPONENT_REF
2974            && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2975     {
2976       error ("`__alignof' applied to a bit-field");
2977       t = size_one_node;
2978     }
2979   else if (TREE_CODE (expr) == COMPONENT_REF
2980            && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
2981     t = size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
2982
2983   else if (TREE_CODE (expr) == INDIRECT_REF)
2984     {
2985       tree t = TREE_OPERAND (expr, 0);
2986       tree best = t;
2987       int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
2988
2989       while (TREE_CODE (t) == NOP_EXPR
2990              && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
2991         {
2992           int thisalign;
2993
2994           t = TREE_OPERAND (t, 0);
2995           thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
2996           if (thisalign > bestalign)
2997             best = t, bestalign = thisalign;
2998         }
2999       return c_alignof (TREE_TYPE (TREE_TYPE (best)));
3000     }
3001   else
3002     return c_alignof (TREE_TYPE (expr));
3003
3004   return fold (build1 (NOP_EXPR, size_type_node, t));
3005 }
3006 \f
3007 /* Handle C and C++ default attributes.  */
3008
3009 enum built_in_attribute
3010 {
3011 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3012 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3013 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3014 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3015 #include "builtin-attrs.def"
3016 #undef DEF_ATTR_NULL_TREE
3017 #undef DEF_ATTR_INT
3018 #undef DEF_ATTR_IDENT
3019 #undef DEF_ATTR_TREE_LIST
3020   ATTR_LAST
3021 };
3022
3023 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3024
3025 static void c_init_attributes (void);
3026
3027 /* Build tree nodes and builtin functions common to both C and C++ language
3028    frontends.  */
3029
3030 void
3031 c_common_nodes_and_builtins (void)
3032 {
3033   enum builtin_type
3034   {
3035 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3036 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3037 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3038 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3039 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3040 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3041 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3042 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3043 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3044 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3045 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3046 #include "builtin-types.def"
3047 #undef DEF_PRIMITIVE_TYPE
3048 #undef DEF_FUNCTION_TYPE_0
3049 #undef DEF_FUNCTION_TYPE_1
3050 #undef DEF_FUNCTION_TYPE_2
3051 #undef DEF_FUNCTION_TYPE_3
3052 #undef DEF_FUNCTION_TYPE_4
3053 #undef DEF_FUNCTION_TYPE_VAR_0
3054 #undef DEF_FUNCTION_TYPE_VAR_1
3055 #undef DEF_FUNCTION_TYPE_VAR_2
3056 #undef DEF_FUNCTION_TYPE_VAR_3
3057 #undef DEF_POINTER_TYPE
3058     BT_LAST
3059   };
3060
3061   typedef enum builtin_type builtin_type;
3062
3063   tree builtin_types[(int) BT_LAST];
3064   int wchar_type_size;
3065   tree array_domain_type;
3066   tree va_list_ref_type_node;
3067   tree va_list_arg_type_node;
3068
3069   /* Define `int' and `char' first so that dbx will output them first.  */
3070   record_builtin_type (RID_INT, NULL, integer_type_node);
3071   record_builtin_type (RID_CHAR, "char", char_type_node);
3072
3073   /* `signed' is the same as `int'.  FIXME: the declarations of "signed",
3074      "unsigned long", "long long unsigned" and "unsigned short" were in C++
3075      but not C.  Are the conditionals here needed?  */
3076   if (c_dialect_cxx ())
3077     record_builtin_type (RID_SIGNED, NULL, integer_type_node);
3078   record_builtin_type (RID_LONG, "long int", long_integer_type_node);
3079   record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
3080   record_builtin_type (RID_MAX, "long unsigned int",
3081                        long_unsigned_type_node);
3082   if (c_dialect_cxx ())
3083     record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
3084   record_builtin_type (RID_MAX, "long long int",
3085                        long_long_integer_type_node);
3086   record_builtin_type (RID_MAX, "long long unsigned int",
3087                        long_long_unsigned_type_node);
3088   if (c_dialect_cxx ())
3089     record_builtin_type (RID_MAX, "long long unsigned",
3090                          long_long_unsigned_type_node);
3091   record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
3092   record_builtin_type (RID_MAX, "short unsigned int",
3093                        short_unsigned_type_node);
3094   if (c_dialect_cxx ())
3095     record_builtin_type (RID_MAX, "unsigned short",
3096                          short_unsigned_type_node);
3097
3098   /* Define both `signed char' and `unsigned char'.  */
3099   record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
3100   record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
3101
3102   /* These are types that c_common_type_for_size and
3103      c_common_type_for_mode use.  */
3104   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3105                                             intQI_type_node));
3106   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3107                                             intHI_type_node));
3108   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3109                                             intSI_type_node));
3110   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3111                                             intDI_type_node));
3112 #if HOST_BITS_PER_WIDE_INT >= 64
3113   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3114                                             get_identifier ("__int128_t"),
3115                                             intTI_type_node));
3116 #endif
3117   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3118                                             unsigned_intQI_type_node));
3119   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3120                                             unsigned_intHI_type_node));
3121   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3122                                             unsigned_intSI_type_node));
3123   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3124                                             unsigned_intDI_type_node));
3125 #if HOST_BITS_PER_WIDE_INT >= 64
3126   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3127                                             get_identifier ("__uint128_t"),
3128                                             unsigned_intTI_type_node));
3129 #endif
3130
3131   /* Create the widest literal types.  */
3132   widest_integer_literal_type_node
3133     = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
3134   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3135                                             widest_integer_literal_type_node));
3136
3137   widest_unsigned_literal_type_node
3138     = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
3139   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3140                                             widest_unsigned_literal_type_node));
3141
3142   /* `unsigned long' is the standard type for sizeof.
3143      Note that stddef.h uses `unsigned long',
3144      and this must agree, even if long and int are the same size.  */
3145   size_type_node =
3146     TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
3147   signed_size_type_node = c_common_signed_type (size_type_node);
3148   set_sizetype (size_type_node);
3149
3150   build_common_tree_nodes_2 (flag_short_double);
3151
3152   record_builtin_type (RID_FLOAT, NULL, float_type_node);
3153   record_builtin_type (RID_DOUBLE, NULL, double_type_node);
3154   record_builtin_type (RID_MAX, "long double", long_double_type_node);
3155
3156   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3157                                             get_identifier ("complex int"),
3158                                             complex_integer_type_node));
3159   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3160                                             get_identifier ("complex float"),
3161                                             complex_float_type_node));
3162   (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3163                                             get_identifier ("complex double"),
3164                                             complex_double_type_node));
3165   (*lang_hooks.decls.pushdecl)
3166     (build_decl (TYPE_DECL, get_identifier ("complex long double"),
3167                  complex_long_double_type_node));
3168
3169   /* Types which are common to the fortran compiler and libf2c.  When
3170      changing these, you also need to be concerned with f/com.h.  */
3171
3172   if (TYPE_PRECISION (float_type_node)
3173       == TYPE_PRECISION (long_integer_type_node))
3174     {
3175       g77_integer_type_node = long_integer_type_node;
3176       g77_uinteger_type_node = long_unsigned_type_node;
3177     }
3178   else if (TYPE_PRECISION (float_type_node)
3179            == TYPE_PRECISION (integer_type_node))
3180     {
3181       g77_integer_type_node = integer_type_node;
3182       g77_uinteger_type_node = unsigned_type_node;
3183     }
3184   else
3185     g77_integer_type_node = g77_uinteger_type_node = NULL_TREE;
3186
3187   if (g77_integer_type_node != NULL_TREE)
3188     {
3189       (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3190                                                 get_identifier ("__g77_integer"),
3191                                                 g77_integer_type_node));
3192       (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3193                                                 get_identifier ("__g77_uinteger"),
3194                                                 g77_uinteger_type_node));
3195     }
3196
3197   if (TYPE_PRECISION (float_type_node) * 2
3198       == TYPE_PRECISION (long_integer_type_node))
3199     {
3200       g77_longint_type_node = long_integer_type_node;
3201       g77_ulongint_type_node = long_unsigned_type_node;
3202     }
3203   else if (TYPE_PRECISION (float_type_node) * 2
3204            == TYPE_PRECISION (long_long_integer_type_node))
3205     {
3206       g77_longint_type_node = long_long_integer_type_node;
3207       g77_ulongint_type_node = long_long_unsigned_type_node;
3208     }
3209   else
3210     g77_longint_type_node = g77_ulongint_type_node = NULL_TREE;
3211
3212   if (g77_longint_type_node != NULL_TREE)
3213     {
3214       (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3215                                                 get_identifier ("__g77_longint"),
3216                                                 g77_longint_type_node));
3217       (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3218                                                 get_identifier ("__g77_ulongint"),
3219                                                 g77_ulongint_type_node));
3220     }
3221
3222   record_builtin_type (RID_VOID, NULL, void_type_node);
3223
3224   void_zero_node = build_int_2 (0, 0);
3225   TREE_TYPE (void_zero_node) = void_type_node;
3226
3227   void_list_node = build_void_list_node ();
3228
3229   /* Make a type to be the domain of a few array types
3230      whose domains don't really matter.
3231      200 is small enough that it always fits in size_t
3232      and large enough that it can hold most function names for the
3233      initializations of __FUNCTION__ and __PRETTY_FUNCTION__.  */
3234   array_domain_type = build_index_type (size_int (200));
3235
3236   /* Make a type for arrays of characters.
3237      With luck nothing will ever really depend on the length of this
3238      array type.  */
3239   char_array_type_node
3240     = build_array_type (char_type_node, array_domain_type);
3241
3242   /* Likewise for arrays of ints.  */
3243   int_array_type_node
3244     = build_array_type (integer_type_node, array_domain_type);
3245
3246   string_type_node = build_pointer_type (char_type_node);
3247   const_string_type_node
3248     = build_pointer_type (build_qualified_type
3249                           (char_type_node, TYPE_QUAL_CONST));
3250
3251   /* This is special for C++ so functions can be overloaded.  */
3252   wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
3253   wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
3254   wchar_type_size = TYPE_PRECISION (wchar_type_node);
3255   if (c_dialect_cxx ())
3256     {
3257       if (TREE_UNSIGNED (wchar_type_node))
3258         wchar_type_node = make_unsigned_type (wchar_type_size);
3259       else
3260         wchar_type_node = make_signed_type (wchar_type_size);
3261       record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
3262     }
3263   else
3264     {
3265       signed_wchar_type_node = c_common_signed_type (wchar_type_node);
3266       unsigned_wchar_type_node = c_common_unsigned_type (wchar_type_node);
3267     }
3268
3269   /* This is for wide string constants.  */
3270   wchar_array_type_node
3271     = build_array_type (wchar_type_node, array_domain_type);
3272
3273   wint_type_node =
3274     TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
3275
3276   intmax_type_node =
3277     TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
3278   uintmax_type_node =
3279     TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
3280
3281   default_function_type = build_function_type (integer_type_node, NULL_TREE);
3282   ptrdiff_type_node
3283     = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
3284   unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
3285
3286   (*lang_hooks.decls.pushdecl)
3287     (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3288                  va_list_type_node));
3289
3290   (*lang_hooks.decls.pushdecl)
3291     (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3292                  ptrdiff_type_node));
3293
3294   (*lang_hooks.decls.pushdecl)
3295     (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3296                  sizetype));
3297
3298   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3299     {
3300       va_list_arg_type_node = va_list_ref_type_node =
3301         build_pointer_type (TREE_TYPE (va_list_type_node));
3302     }
3303   else
3304     {
3305       va_list_arg_type_node = va_list_type_node;
3306       va_list_ref_type_node = build_reference_type (va_list_type_node);
3307     }
3308
3309 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3310   builtin_types[(int) ENUM] = VALUE;
3311 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN)               \
3312   builtin_types[(int) ENUM]                             \
3313     = build_function_type (builtin_types[(int) RETURN], \
3314                            void_list_node);
3315 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1)                         \
3316   builtin_types[(int) ENUM]                                             \
3317     = build_function_type (builtin_types[(int) RETURN],                 \
3318                            tree_cons (NULL_TREE,                        \
3319                                       builtin_types[(int) ARG1],        \
3320                                       void_list_node));
3321 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2)   \
3322   builtin_types[(int) ENUM]                             \
3323     = build_function_type                               \
3324       (builtin_types[(int) RETURN],                     \
3325        tree_cons (NULL_TREE,                            \
3326                   builtin_types[(int) ARG1],            \
3327                   tree_cons (NULL_TREE,                 \
3328                              builtin_types[(int) ARG2], \
3329                              void_list_node)));
3330 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3)              \
3331   builtin_types[(int) ENUM]                                              \
3332     = build_function_type                                                \
3333       (builtin_types[(int) RETURN],                                      \
3334        tree_cons (NULL_TREE,                                             \
3335                   builtin_types[(int) ARG1],                             \
3336                   tree_cons (NULL_TREE,                                  \
3337                              builtin_types[(int) ARG2],                  \
3338                              tree_cons (NULL_TREE,                       \
3339                                         builtin_types[(int) ARG3],       \
3340                                         void_list_node))));
3341 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4)       \
3342   builtin_types[(int) ENUM]                                             \
3343     = build_function_type                                               \
3344       (builtin_types[(int) RETURN],                                     \
3345        tree_cons (NULL_TREE,                                            \
3346                   builtin_types[(int) ARG1],                            \
3347                   tree_cons (NULL_TREE,                                 \
3348                              builtin_types[(int) ARG2],                 \
3349                              tree_cons                                  \
3350                              (NULL_TREE,                                \
3351                               builtin_types[(int) ARG3],                \
3352                               tree_cons (NULL_TREE,                     \
3353                                          builtin_types[(int) ARG4],     \
3354                                          void_list_node)))));
3355 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN)                           \
3356   builtin_types[(int) ENUM]                                             \
3357     = build_function_type (builtin_types[(int) RETURN], NULL_TREE);
3358 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1)                      \
3359    builtin_types[(int) ENUM]                                             \
3360     = build_function_type (builtin_types[(int) RETURN],          \
3361                            tree_cons (NULL_TREE,                         \
3362                                       builtin_types[(int) ARG1],         \
3363                                       NULL_TREE));
3364
3365 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2)       \
3366    builtin_types[(int) ENUM]                                    \
3367     = build_function_type                                       \
3368       (builtin_types[(int) RETURN],                             \
3369        tree_cons (NULL_TREE,                                    \
3370                   builtin_types[(int) ARG1],                    \
3371                   tree_cons (NULL_TREE,                         \
3372                              builtin_types[(int) ARG2],         \
3373                              NULL_TREE)));
3374
3375 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3)         \
3376    builtin_types[(int) ENUM]                                            \
3377     = build_function_type                                               \
3378       (builtin_types[(int) RETURN],                                     \
3379        tree_cons (NULL_TREE,                                            \
3380                   builtin_types[(int) ARG1],                            \
3381                   tree_cons (NULL_TREE,                                 \
3382                              builtin_types[(int) ARG2],                 \
3383                              tree_cons (NULL_TREE,                      \
3384                                         builtin_types[(int) ARG3],      \
3385                                         NULL_TREE))));
3386
3387 #define DEF_POINTER_TYPE(ENUM, TYPE)                    \
3388   builtin_types[(int) ENUM]                             \
3389     = build_pointer_type (builtin_types[(int) TYPE]);
3390 #include "builtin-types.def"
3391 #undef DEF_PRIMITIVE_TYPE
3392 #undef DEF_FUNCTION_TYPE_1
3393 #undef DEF_FUNCTION_TYPE_2
3394 #undef DEF_FUNCTION_TYPE_3
3395 #undef DEF_FUNCTION_TYPE_4
3396 #undef DEF_FUNCTION_TYPE_VAR_0
3397 #undef DEF_FUNCTION_TYPE_VAR_1
3398 #undef DEF_FUNCTION_TYPE_VAR_2
3399 #undef DEF_FUNCTION_TYPE_VAR_3
3400 #undef DEF_POINTER_TYPE
3401
3402   c_init_attributes ();
3403
3404 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE,                   \
3405                     BOTH_P, FALLBACK_P, NONANSI_P, ATTRS, IMPLICIT)     \
3406   if (NAME)                                                             \
3407     {                                                                   \
3408       tree decl;                                                        \
3409                                                                         \
3410       if (strncmp (NAME, "__builtin_", strlen ("__builtin_")) != 0)     \
3411         abort ();                                                       \
3412                                                                         \
3413       if (!BOTH_P)                                                      \
3414         decl = builtin_function (NAME, builtin_types[TYPE], ENUM,       \
3415                                  CLASS,                                 \
3416                                  (FALLBACK_P                            \
3417                                   ? (NAME + strlen ("__builtin_"))      \
3418                                   : NULL),                              \
3419                                  built_in_attributes[(int) ATTRS]);     \
3420       else                                                              \
3421         decl = builtin_function_2 (NAME,                                \
3422                                    NAME + strlen ("__builtin_"),        \
3423                                    builtin_types[TYPE],                 \
3424                                    builtin_types[LIBTYPE],              \
3425                                    ENUM,                                \
3426                                    CLASS,                               \
3427                                    FALLBACK_P,                          \
3428                                    NONANSI_P,                           \
3429                                    built_in_attributes[(int) ATTRS]);   \
3430                                                                         \
3431       built_in_decls[(int) ENUM] = decl;                                \
3432       if (IMPLICIT)                                                     \
3433         implicit_built_in_decls[(int) ENUM] = decl;                     \
3434     }
3435 #include "builtins.def"
3436 #undef DEF_BUILTIN
3437
3438   (*targetm.init_builtins) ();
3439
3440   main_identifier_node = get_identifier ("main");
3441 }
3442
3443 tree
3444 build_va_arg (tree expr, tree type)
3445 {
3446   return build1 (VA_ARG_EXPR, type, expr);
3447 }
3448
3449
3450 /* Linked list of disabled built-in functions.  */
3451
3452 typedef struct disabled_builtin
3453 {
3454   const char *name;
3455   struct disabled_builtin *next;
3456 } disabled_builtin;
3457 static disabled_builtin *disabled_builtins = NULL;
3458
3459 static bool builtin_function_disabled_p (const char *);
3460
3461 /* Disable a built-in function specified by -fno-builtin-NAME.  If NAME
3462    begins with "__builtin_", give an error.  */
3463
3464 void
3465 disable_builtin_function (const char *name)
3466 {
3467   if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
3468     error ("cannot disable built-in function `%s'", name);
3469   else
3470     {
3471       disabled_builtin *new = xmalloc (sizeof (disabled_builtin));
3472       new->name = name;
3473       new->next = disabled_builtins;
3474       disabled_builtins = new;
3475     }
3476 }
3477
3478
3479 /* Return true if the built-in function NAME has been disabled, false
3480    otherwise.  */
3481
3482 static bool
3483 builtin_function_disabled_p (const char *name)
3484 {
3485   disabled_builtin *p;
3486   for (p = disabled_builtins; p != NULL; p = p->next)
3487     {
3488       if (strcmp (name, p->name) == 0)
3489         return true;
3490     }
3491   return false;
3492 }
3493
3494
3495 /* Possibly define a builtin function with one or two names.  BUILTIN_NAME
3496    is an __builtin_-prefixed name; NAME is the ordinary name; one or both
3497    of these may be NULL (though both being NULL is useless).
3498    BUILTIN_TYPE is the type of the __builtin_-prefixed function;
3499    TYPE is the type of the function with the ordinary name.  These
3500    may differ if the ordinary name is declared with a looser type to avoid
3501    conflicts with headers.  FUNCTION_CODE and CLASS are as for
3502    builtin_function.  If LIBRARY_NAME_P is nonzero, NAME is passed as
3503    the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
3504    If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name;
3505    ATTRS is the tree list representing the builtin's function attributes.
3506    Returns the declaration of BUILTIN_NAME, if any, otherwise
3507    the declaration of NAME.  Does not declare NAME if flag_no_builtin,
3508    or if NONANSI_P and flag_no_nonansi_builtin.  */
3509
3510 static tree
3511 builtin_function_2 (const char *builtin_name, const char *name,
3512                     tree builtin_type, tree type, int function_code,
3513                     enum built_in_class class, int library_name_p,
3514                     int nonansi_p, tree attrs)
3515 {
3516   tree bdecl = NULL_TREE;
3517   tree decl = NULL_TREE;
3518
3519   if (builtin_name != 0)
3520     bdecl = builtin_function (builtin_name, builtin_type, function_code,
3521                               class, library_name_p ? name : NULL, attrs);
3522
3523   if (name != 0 && !flag_no_builtin && !builtin_function_disabled_p (name)
3524       && !(nonansi_p && flag_no_nonansi_builtin))
3525     decl = builtin_function (name, type, function_code, class, NULL, attrs);
3526
3527   return (bdecl != 0 ? bdecl : decl);
3528 }
3529 \f
3530 /* Nonzero if the type T promotes to int.  This is (nearly) the
3531    integral promotions defined in ISO C99 6.3.1.1/2.  */
3532
3533 bool
3534 c_promoting_integer_type_p (tree t)
3535 {
3536   switch (TREE_CODE (t))
3537     {
3538     case INTEGER_TYPE:
3539       return (TYPE_MAIN_VARIANT (t) == char_type_node
3540               || TYPE_MAIN_VARIANT (t) == signed_char_type_node
3541               || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
3542               || TYPE_MAIN_VARIANT (t) == short_integer_type_node
3543               || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
3544               || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
3545
3546     case ENUMERAL_TYPE:
3547       /* ??? Technically all enumerations not larger than an int
3548          promote to an int.  But this is used along code paths
3549          that only want to notice a size change.  */
3550       return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
3551
3552     case BOOLEAN_TYPE:
3553       return 1;
3554
3555     default:
3556       return 0;
3557     }
3558 }
3559
3560 /* Return 1 if PARMS specifies a fixed number of parameters
3561    and none of their types is affected by default promotions.  */
3562
3563 int
3564 self_promoting_args_p (tree parms)
3565 {
3566   tree t;
3567   for (t = parms; t; t = TREE_CHAIN (t))
3568     {
3569       tree type = TREE_VALUE (t);
3570
3571       if (TREE_CHAIN (t) == 0 && type != void_type_node)
3572         return 0;
3573
3574       if (type == 0)
3575         return 0;
3576
3577       if (TYPE_MAIN_VARIANT (type) == float_type_node)
3578         return 0;
3579
3580       if (c_promoting_integer_type_p (type))
3581         return 0;
3582     }
3583   return 1;
3584 }
3585
3586 /* Recursively examines the array elements of TYPE, until a non-array
3587    element type is found.  */
3588
3589 tree
3590 strip_array_types (tree type)
3591 {
3592   while (TREE_CODE (type) == ARRAY_TYPE)
3593     type = TREE_TYPE (type);
3594
3595   return type;
3596 }
3597
3598 /* Recursively remove any '*' or '&' operator from TYPE.  */
3599 tree
3600 strip_pointer_operator (tree t)
3601 {
3602   while (POINTER_TYPE_P (t))
3603     t = TREE_TYPE (t);
3604   return t;
3605 }
3606
3607 static tree expand_unordered_cmp (tree, tree, enum tree_code, enum tree_code);
3608
3609 /* Expand a call to an unordered comparison function such as
3610    __builtin_isgreater().  FUNCTION is the function's declaration and
3611    PARAMS a list of the values passed.  For __builtin_isunordered(),
3612    UNORDERED_CODE is UNORDERED_EXPR and ORDERED_CODE is NOP_EXPR.  In
3613    other cases, UNORDERED_CODE and ORDERED_CODE are comparison codes
3614    that give the opposite of the desired result.  UNORDERED_CODE is
3615    used for modes that can hold NaNs and ORDERED_CODE is used for the
3616    rest.  */
3617
3618 static tree
3619 expand_unordered_cmp (tree function, tree params,
3620                       enum tree_code unordered_code,
3621                       enum tree_code ordered_code)
3622 {
3623   tree arg0, arg1, type;
3624   enum tree_code code0, code1;
3625
3626   /* Check that we have exactly two arguments.  */
3627   if (params == 0 || TREE_CHAIN (params) == 0)
3628     {
3629       error ("too few arguments to function `%s'",
3630              IDENTIFIER_POINTER (DECL_NAME (function)));
3631       return error_mark_node;
3632     }
3633   else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
3634     {
3635       error ("too many arguments to function `%s'",
3636              IDENTIFIER_POINTER (DECL_NAME (function)));
3637       return error_mark_node;
3638     }
3639
3640   arg0 = TREE_VALUE (params);
3641   arg1 = TREE_VALUE (TREE_CHAIN (params));
3642
3643   code0 = TREE_CODE (TREE_TYPE (arg0));
3644   code1 = TREE_CODE (TREE_TYPE (arg1));
3645
3646   /* Make sure that the arguments have a common type of REAL.  */
3647   type = 0;
3648   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3649       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3650     type = common_type (TREE_TYPE (arg0), TREE_TYPE (arg1));
3651
3652   if (type == 0 || TREE_CODE (type) != REAL_TYPE)
3653     {
3654       error ("non-floating-point argument to function `%s'",
3655              IDENTIFIER_POINTER (DECL_NAME (function)));
3656       return error_mark_node;
3657     }
3658
3659   if (unordered_code == UNORDERED_EXPR)
3660     {
3661       if (MODE_HAS_NANS (TYPE_MODE (type)))
3662         return build_binary_op (unordered_code,
3663                                 convert (type, arg0),
3664                                 convert (type, arg1),
3665                                 0);
3666       else
3667         return integer_zero_node;
3668     }
3669
3670   return build_unary_op (TRUTH_NOT_EXPR,
3671                          build_binary_op (MODE_HAS_NANS (TYPE_MODE (type))
3672                                           ? unordered_code
3673                                           : ordered_code,
3674                                           convert (type, arg0),
3675                                           convert (type, arg1),
3676                                           0),
3677                          0);
3678 }
3679
3680
3681 /* Recognize certain built-in functions so we can make tree-codes
3682    other than CALL_EXPR.  We do this when it enables fold-const.c
3683    to do something useful.  */
3684 /* ??? By rights this should go in builtins.c, but only C and C++
3685    implement build_{binary,unary}_op.  Not exactly sure what bits
3686    of functionality are actually needed from those functions, or
3687    where the similar functionality exists in the other front ends.  */
3688
3689 tree
3690 expand_tree_builtin (tree function, tree params, tree coerced_params)
3691 {
3692   if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3693     return NULL_TREE;
3694
3695   switch (DECL_FUNCTION_CODE (function))
3696     {
3697     case BUILT_IN_ABS:
3698     case BUILT_IN_LABS:
3699     case BUILT_IN_LLABS:
3700     case BUILT_IN_IMAXABS:
3701     case BUILT_IN_FABS:
3702     case BUILT_IN_FABSL:
3703     case BUILT_IN_FABSF:
3704       if (coerced_params == 0)
3705         return integer_zero_node;
3706       return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3707
3708     case BUILT_IN_CONJ:
3709     case BUILT_IN_CONJF:
3710     case BUILT_IN_CONJL:
3711       if (coerced_params == 0)
3712         return integer_zero_node;
3713       return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0);
3714
3715     case BUILT_IN_CREAL:
3716     case BUILT_IN_CREALF:
3717     case BUILT_IN_CREALL:
3718       if (coerced_params == 0)
3719         return integer_zero_node;
3720       return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0);
3721
3722     case BUILT_IN_CIMAG:
3723     case BUILT_IN_CIMAGF:
3724     case BUILT_IN_CIMAGL:
3725       if (coerced_params == 0)
3726         return integer_zero_node;
3727       return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0);
3728
3729     case BUILT_IN_ISGREATER:
3730       return expand_unordered_cmp (function, params, UNLE_EXPR, LE_EXPR);
3731
3732     case BUILT_IN_ISGREATEREQUAL:
3733       return expand_unordered_cmp (function, params, UNLT_EXPR, LT_EXPR);
3734
3735     case BUILT_IN_ISLESS:
3736       return expand_unordered_cmp (function, params, UNGE_EXPR, GE_EXPR);
3737
3738     case BUILT_IN_ISLESSEQUAL:
3739       return expand_unordered_cmp (function, params, UNGT_EXPR, GT_EXPR);
3740
3741     case BUILT_IN_ISLESSGREATER:
3742       return expand_unordered_cmp (function, params, UNEQ_EXPR, EQ_EXPR);
3743
3744     case BUILT_IN_ISUNORDERED:
3745       return expand_unordered_cmp (function, params, UNORDERED_EXPR, NOP_EXPR);
3746
3747     default:
3748       break;
3749     }
3750
3751   return NULL_TREE;
3752 }
3753
3754 /* Walk the statement tree, rooted at *tp.  Apply FUNC to all the
3755    sub-trees of *TP in a pre-order traversal.  FUNC is called with the
3756    DATA and the address of each sub-tree.  If FUNC returns a non-NULL
3757    value, the traversal is aborted, and the value returned by FUNC is
3758    returned.  If FUNC sets WALK_SUBTREES to zero, then the subtrees of
3759    the node being visited are not walked.
3760
3761    We don't need a without_duplicates variant of this one because the
3762    statement tree is a tree, not a graph.  */
3763
3764 tree
3765 walk_stmt_tree (tree *tp, walk_tree_fn func, void *data)
3766 {
3767   enum tree_code code;
3768   int walk_subtrees;
3769   tree result;
3770   int i, len;
3771
3772 #define WALK_SUBTREE(NODE)                              \
3773   do                                                    \
3774     {                                                   \
3775       result = walk_stmt_tree (&(NODE), func, data);    \
3776       if (result)                                       \
3777         return result;                                  \
3778     }                                                   \
3779   while (0)
3780
3781   /* Skip empty subtrees.  */
3782   if (!*tp)
3783     return NULL_TREE;
3784
3785   /* Skip subtrees below non-statement nodes.  */
3786   if (!STATEMENT_CODE_P (TREE_CODE (*tp)))
3787     return NULL_TREE;
3788
3789   /* Call the function.  */
3790   walk_subtrees = 1;
3791   result = (*func) (tp, &walk_subtrees, data);
3792
3793   /* If we found something, return it.  */
3794   if (result)
3795     return result;
3796
3797   /* FUNC may have modified the tree, recheck that we're looking at a
3798      statement node.  */
3799   code = TREE_CODE (*tp);
3800   if (!STATEMENT_CODE_P (code))
3801     return NULL_TREE;
3802
3803   /* Visit the subtrees unless FUNC decided that there was nothing
3804      interesting below this point in the tree.  */
3805   if (walk_subtrees)
3806     {
3807       /* Walk over all the sub-trees of this operand.  Statement nodes
3808          never contain RTL, and we needn't worry about TARGET_EXPRs.  */
3809       len = TREE_CODE_LENGTH (code);
3810
3811       /* Go through the subtrees.  We need to do this in forward order so
3812          that the scope of a FOR_EXPR is handled properly.  */
3813       for (i = 0; i < len; ++i)
3814         WALK_SUBTREE (TREE_OPERAND (*tp, i));
3815     }
3816
3817   /* Finally visit the chain.  This can be tail-recursion optimized if
3818      we write it this way.  */
3819   return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
3820
3821 #undef WALK_SUBTREE
3822 }
3823
3824 /* Used to compare case labels.  K1 and K2 are actually tree nodes
3825    representing case labels, or NULL_TREE for a `default' label.
3826    Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
3827    K2, and 0 if K1 and K2 are equal.  */
3828
3829 int
3830 case_compare (splay_tree_key k1, splay_tree_key k2)
3831 {
3832   /* Consider a NULL key (such as arises with a `default' label) to be
3833      smaller than anything else.  */
3834   if (!k1)
3835     return k2 ? -1 : 0;
3836   else if (!k2)
3837     return k1 ? 1 : 0;
3838
3839   return tree_int_cst_compare ((tree) k1, (tree) k2);
3840 }
3841
3842 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE.  If
3843    LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
3844    actually a `default' label.  If only HIGH_VALUE is NULL_TREE, then
3845    case label was declared using the usual C/C++ syntax, rather than
3846    the GNU case range extension.  CASES is a tree containing all the
3847    case ranges processed so far; COND is the condition for the
3848    switch-statement itself.  Returns the CASE_LABEL created, or
3849    ERROR_MARK_NODE if no CASE_LABEL is created.  */
3850
3851 tree
3852 c_add_case_label (splay_tree cases, tree cond, tree low_value,
3853                   tree high_value)
3854 {
3855   tree type;
3856   tree label;
3857   tree case_label;
3858   splay_tree_node node;
3859
3860   /* Create the LABEL_DECL itself.  */
3861   label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3862   DECL_CONTEXT (label) = current_function_decl;
3863
3864   /* If there was an error processing the switch condition, bail now
3865      before we get more confused.  */
3866   if (!cond || cond == error_mark_node)
3867     {
3868       /* Add a label anyhow so that the back-end doesn't think that
3869          the beginning of the switch is unreachable.  */
3870       if (!cases->root)
3871         add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3872       return error_mark_node;
3873     }
3874
3875   if ((low_value && TREE_TYPE (low_value)
3876        && POINTER_TYPE_P (TREE_TYPE (low_value)))
3877       || (high_value && TREE_TYPE (high_value)
3878           && POINTER_TYPE_P (TREE_TYPE (high_value))))
3879     error ("pointers are not permitted as case values");
3880
3881   /* Case ranges are a GNU extension.  */
3882   if (high_value && pedantic)
3883     pedwarn ("range expressions in switch statements are non-standard");
3884
3885   type = TREE_TYPE (cond);
3886   if (low_value)
3887     {
3888       low_value = check_case_value (low_value);
3889       low_value = convert_and_check (type, low_value);
3890     }
3891   if (high_value)
3892     {
3893       high_value = check_case_value (high_value);
3894       high_value = convert_and_check (type, high_value);
3895     }
3896
3897   /* If an error has occurred, bail out now.  */
3898   if (low_value == error_mark_node || high_value == error_mark_node)
3899     {
3900       if (!cases->root)
3901         add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3902       return error_mark_node;
3903     }
3904
3905   /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
3906      really a case range, even though it was written that way.  Remove
3907      the HIGH_VALUE to simplify later processing.  */
3908   if (tree_int_cst_equal (low_value, high_value))
3909     high_value = NULL_TREE;
3910   if (low_value && high_value
3911       && !tree_int_cst_lt (low_value, high_value))
3912     warning ("empty range specified");
3913
3914   /* Look up the LOW_VALUE in the table of case labels we already
3915      have.  */
3916   node = splay_tree_lookup (cases, (splay_tree_key) low_value);
3917   /* If there was not an exact match, check for overlapping ranges.
3918      There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
3919      that's a `default' label and the only overlap is an exact match.  */
3920   if (!node && (low_value || high_value))
3921     {
3922       splay_tree_node low_bound;
3923       splay_tree_node high_bound;
3924
3925       /* Even though there wasn't an exact match, there might be an
3926          overlap between this case range and another case range.
3927          Since we've (inductively) not allowed any overlapping case
3928          ranges, we simply need to find the greatest low case label
3929          that is smaller that LOW_VALUE, and the smallest low case
3930          label that is greater than LOW_VALUE.  If there is an overlap
3931          it will occur in one of these two ranges.  */
3932       low_bound = splay_tree_predecessor (cases,
3933                                           (splay_tree_key) low_value);
3934       high_bound = splay_tree_successor (cases,
3935                                          (splay_tree_key) low_value);
3936
3937       /* Check to see if the LOW_BOUND overlaps.  It is smaller than
3938          the LOW_VALUE, so there is no need to check unless the
3939          LOW_BOUND is in fact itself a case range.  */
3940       if (low_bound
3941           && CASE_HIGH ((tree) low_bound->value)
3942           && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
3943                                     low_value) >= 0)
3944         node = low_bound;
3945       /* Check to see if the HIGH_BOUND overlaps.  The low end of that
3946          range is bigger than the low end of the current range, so we
3947          are only interested if the current range is a real range, and
3948          not an ordinary case label.  */
3949       else if (high_bound
3950                && high_value
3951                && (tree_int_cst_compare ((tree) high_bound->key,
3952                                          high_value)
3953                    <= 0))
3954         node = high_bound;
3955     }
3956   /* If there was an overlap, issue an error.  */
3957   if (node)
3958     {
3959       tree duplicate = CASE_LABEL_DECL ((tree) node->value);
3960
3961       if (high_value)
3962         {
3963           error ("duplicate (or overlapping) case value");
3964           error ("%Jthis is the first entry overlapping that value", duplicate);
3965         }
3966       else if (low_value)
3967         {
3968           error ("duplicate case value") ;
3969           error ("%Jpreviously used here", duplicate);
3970         }
3971       else
3972         {
3973           error ("multiple default labels in one switch");
3974           error ("%Jthis is the first default label", duplicate);
3975         }
3976       if (!cases->root)
3977         add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3978     }
3979
3980   /* Add a CASE_LABEL to the statement-tree.  */
3981   case_label = add_stmt (build_case_label (low_value, high_value, label));
3982   /* Register this case label in the splay tree.  */
3983   splay_tree_insert (cases,
3984                      (splay_tree_key) low_value,
3985                      (splay_tree_value) case_label);
3986
3987   return case_label;
3988 }
3989
3990 /* Finish an expression taking the address of LABEL (an
3991    IDENTIFIER_NODE).  Returns an expression for the address.  */
3992
3993 tree
3994 finish_label_address_expr (tree label)
3995 {
3996   tree result;
3997
3998   if (pedantic)
3999     pedwarn ("taking the address of a label is non-standard");
4000
4001   if (label == error_mark_node)
4002     return error_mark_node;
4003
4004   label = lookup_label (label);
4005   if (label == NULL_TREE)
4006     result = null_pointer_node;
4007   else
4008     {
4009       TREE_USED (label) = 1;
4010       result = build1 (ADDR_EXPR, ptr_type_node, label);
4011       TREE_CONSTANT (result) = 1;
4012       /* The current function in not necessarily uninlinable.
4013          Computed gotos are incompatible with inlining, but the value
4014          here could be used only in a diagnostic, for example.  */
4015     }
4016
4017   return result;
4018 }
4019
4020 /* Hook used by expand_expr to expand language-specific tree codes.  */
4021
4022 rtx
4023 c_expand_expr (tree exp, rtx target, enum machine_mode tmode, int modifier)
4024      /* Actually enum_modifier.  */
4025 {
4026   switch (TREE_CODE (exp))
4027     {
4028     case STMT_EXPR:
4029       {
4030         tree rtl_expr;
4031         rtx result;
4032         bool preserve_result = false;
4033         bool return_target = false;
4034
4035         if (STMT_EXPR_WARN_UNUSED_RESULT (exp) && target == const0_rtx)
4036           {
4037             tree stmt = STMT_EXPR_STMT (exp);
4038             tree scope;
4039
4040             for (scope = COMPOUND_BODY (stmt);
4041                  scope && TREE_CODE (scope) != SCOPE_STMT;
4042                  scope = TREE_CHAIN (scope));
4043
4044             if (scope && SCOPE_STMT_BLOCK (scope))
4045               warning ("%Hignoring return value of `%D', "
4046                        "declared with attribute warn_unused_result",
4047                        &expr_wfl_stack->location,
4048                        BLOCK_ABSTRACT_ORIGIN (SCOPE_STMT_BLOCK (scope)));
4049             else
4050               warning ("%Hignoring return value of function "
4051                        "declared with attribute warn_unused_result",
4052                        &expr_wfl_stack->location);
4053           }
4054
4055         /* Since expand_expr_stmt calls free_temp_slots after every
4056            expression statement, we must call push_temp_slots here.
4057            Otherwise, any temporaries in use now would be considered
4058            out-of-scope after the first EXPR_STMT from within the
4059            STMT_EXPR.  */
4060         push_temp_slots ();
4061         rtl_expr = expand_start_stmt_expr (!STMT_EXPR_NO_SCOPE (exp));
4062
4063         /* If we want the result of this expression, find the last
4064            EXPR_STMT in the COMPOUND_STMT and mark it as addressable.  */
4065         if (target != const0_rtx
4066             && TREE_CODE (STMT_EXPR_STMT (exp)) == COMPOUND_STMT
4067             && TREE_CODE (COMPOUND_BODY (STMT_EXPR_STMT (exp))) == SCOPE_STMT)
4068           {
4069             tree expr = COMPOUND_BODY (STMT_EXPR_STMT (exp));
4070             tree last = TREE_CHAIN (expr);
4071
4072             while (TREE_CHAIN (last))
4073               {
4074                 expr = last;
4075                 last = TREE_CHAIN (last);
4076               }
4077
4078             if (TREE_CODE (last) == SCOPE_STMT
4079                 && TREE_CODE (expr) == EXPR_STMT)
4080               {
4081                 if (target && TREE_CODE (EXPR_STMT_EXPR (expr)) == VAR_DECL
4082                     && DECL_RTL_IF_SET (EXPR_STMT_EXPR (expr)) == target)
4083                   /* If the last expression is a variable whose RTL is the
4084                      same as our target, just return the target; if it
4085                      isn't valid expanding the decl would produce different
4086                      RTL, and store_expr would try to do a copy.  */
4087                   return_target = true;
4088                 else
4089                   {
4090                     /* Otherwise, note that we want the value from the last
4091                        expression.  */
4092                     TREE_ADDRESSABLE (expr) = 1;
4093                     preserve_result = true;
4094                   }
4095               }
4096           }
4097
4098         expand_stmt (STMT_EXPR_STMT (exp));
4099         expand_end_stmt_expr (rtl_expr);
4100
4101         result = expand_expr (rtl_expr, target, tmode, modifier);
4102         if (return_target)
4103           result = target;
4104         else if (preserve_result && GET_CODE (result) == MEM)
4105           {
4106             if (GET_MODE (result) != BLKmode)
4107               result = copy_to_reg (result);
4108             else
4109               preserve_temp_slots (result);
4110           }
4111
4112         /* If the statment-expression does not have a scope, then the
4113            new temporaries we created within it must live beyond the
4114            statement-expression.  */
4115         if (STMT_EXPR_NO_SCOPE (exp))
4116           preserve_temp_slots (NULL_RTX);
4117
4118         pop_temp_slots ();
4119         return result;
4120       }
4121       break;
4122
4123     case COMPOUND_LITERAL_EXPR:
4124       {
4125         /* Initialize the anonymous variable declared in the compound
4126            literal, then return the variable.  */
4127         tree decl = COMPOUND_LITERAL_EXPR_DECL (exp);
4128         emit_local_var (decl);
4129         return expand_expr (decl, target, tmode, modifier);
4130       }
4131
4132     default:
4133       abort ();
4134     }
4135
4136   abort ();
4137   return NULL;
4138 }
4139
4140 /* Hook used by safe_from_p to handle language-specific tree codes.  */
4141
4142 int
4143 c_safe_from_p (rtx target, tree exp)
4144 {
4145   /* We can see statements here when processing the body of a
4146      statement-expression.  For a declaration statement declaring a
4147      variable, look at the variable's initializer.  */
4148   if (TREE_CODE (exp) == DECL_STMT)
4149     {
4150       tree decl = DECL_STMT_DECL (exp);
4151
4152       if (TREE_CODE (decl) == VAR_DECL
4153           && DECL_INITIAL (decl)
4154           && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
4155         return 0;
4156     }
4157
4158   /* For any statement, we must follow the statement-chain.  */
4159   if (STATEMENT_CODE_P (TREE_CODE (exp)) && TREE_CHAIN (exp))
4160     return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
4161
4162   /* Assume everything else is safe.  */
4163   return 1;
4164 }
4165
4166 /* Hook used by unsafe_for_reeval to handle language-specific tree codes.  */
4167
4168 int
4169 c_common_unsafe_for_reeval (tree exp)
4170 {
4171   /* Statement expressions may not be reevaluated, likewise compound
4172      literals.  */
4173   if (TREE_CODE (exp) == STMT_EXPR
4174       || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
4175     return 2;
4176
4177   /* Walk all other expressions.  */
4178   return -1;
4179 }
4180
4181 /* Hook used by staticp to handle language-specific tree codes.  */
4182
4183 int
4184 c_staticp (tree exp)
4185 {
4186   if (TREE_CODE (exp) == COMPOUND_LITERAL_EXPR
4187       && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
4188     return 1;
4189   return 0;
4190 }
4191 \f
4192
4193 /* Given a boolean expression ARG, return a tree representing an increment
4194    or decrement (as indicated by CODE) of ARG.  The front end must check for
4195    invalid cases (e.g., decrement in C++).  */
4196 tree
4197 boolean_increment (enum tree_code code, tree arg)
4198 {
4199   tree val;
4200   tree true_res = boolean_true_node;
4201
4202   arg = stabilize_reference (arg);
4203   switch (code)
4204     {
4205     case PREINCREMENT_EXPR:
4206       val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4207       break;
4208     case POSTINCREMENT_EXPR:
4209       val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4210       arg = save_expr (arg);
4211       val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4212       val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4213       break;
4214     case PREDECREMENT_EXPR:
4215       val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4216       break;
4217     case POSTDECREMENT_EXPR:
4218       val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4219       arg = save_expr (arg);
4220       val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4221       val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4222       break;
4223     default:
4224       abort ();
4225     }
4226   TREE_SIDE_EFFECTS (val) = 1;
4227   return val;
4228 }
4229 \f
4230 /* Built-in macros for stddef.h, that require macros defined in this
4231    file.  */
4232 void
4233 c_stddef_cpp_builtins(void)
4234 {
4235   builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
4236   builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
4237   builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
4238   builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
4239 }
4240
4241 static void
4242 c_init_attributes (void)
4243 {
4244   /* Fill in the built_in_attributes array.  */
4245 #define DEF_ATTR_NULL_TREE(ENUM)                \
4246   built_in_attributes[(int) ENUM] = NULL_TREE;
4247 #define DEF_ATTR_INT(ENUM, VALUE)                                            \
4248   built_in_attributes[(int) ENUM] = build_int_2 (VALUE, VALUE < 0 ? -1 : 0);
4249 #define DEF_ATTR_IDENT(ENUM, STRING)                            \
4250   built_in_attributes[(int) ENUM] = get_identifier (STRING);
4251 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
4252   built_in_attributes[(int) ENUM]                       \
4253     = tree_cons (built_in_attributes[(int) PURPOSE],    \
4254                  built_in_attributes[(int) VALUE],      \
4255                  built_in_attributes[(int) CHAIN]);
4256 #include "builtin-attrs.def"
4257 #undef DEF_ATTR_NULL_TREE
4258 #undef DEF_ATTR_INT
4259 #undef DEF_ATTR_IDENT
4260 #undef DEF_ATTR_TREE_LIST
4261 }
4262
4263 /* Output a -Wshadow warning MSGCODE about NAME, and give the location
4264    of the previous declaration DECL.  */
4265 void
4266 shadow_warning (enum sw_kind msgcode, const char *name, tree decl)
4267 {
4268   static const char *const msgs[] = {
4269     /* SW_PARAM  */ N_("declaration of \"%s\" shadows a parameter"),
4270     /* SW_LOCAL  */ N_("declaration of \"%s\" shadows a previous local"),
4271     /* SW_GLOBAL */ N_("declaration of \"%s\" shadows a global declaration")
4272   };
4273
4274   warning (msgs[msgcode], name);
4275   warning ("%Jshadowed declaration is here", decl);
4276 }
4277
4278 /* Attribute handlers common to C front ends.  */
4279
4280 /* Handle a "packed" attribute; arguments as in
4281    struct attribute_spec.handler.  */
4282
4283 static tree
4284 handle_packed_attribute (tree *node, tree name, tree args  ATTRIBUTE_UNUSED,
4285                          int flags, bool *no_add_attrs)
4286 {
4287   if (TYPE_P (*node))
4288     {
4289       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4290         *node = build_type_copy (*node);
4291       TYPE_PACKED (*node) = 1;
4292       if (TYPE_MAIN_VARIANT (*node) == *node)
4293         {
4294           /* If it is the main variant, then pack the other variants
4295              too. This happens in,
4296              
4297              struct Foo {
4298                struct Foo const *ptr; // creates a variant w/o packed flag
4299                } __ attribute__((packed)); // packs it now.
4300           */
4301           tree probe;
4302           
4303           for (probe = *node; probe; probe = TYPE_NEXT_VARIANT (probe))
4304             TYPE_PACKED (probe) = 1;
4305         }
4306       
4307     }
4308   else if (TREE_CODE (*node) == FIELD_DECL)
4309     DECL_PACKED (*node) = 1;
4310   /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
4311      used for DECL_REGISTER.  It wouldn't mean anything anyway.
4312      We can't set DECL_PACKED on the type of a TYPE_DECL, because
4313      that changes what the typedef is typing.  */
4314   else
4315     {
4316       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4317       *no_add_attrs = true;
4318     }
4319
4320   return NULL_TREE;
4321 }
4322
4323 /* Handle a "nocommon" attribute; arguments as in
4324    struct attribute_spec.handler.  */
4325
4326 static tree
4327 handle_nocommon_attribute (tree *node, tree name,
4328                            tree args ATTRIBUTE_UNUSED,
4329                            int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4330 {
4331   if (TREE_CODE (*node) == VAR_DECL)
4332     DECL_COMMON (*node) = 0;
4333   else
4334     {
4335       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4336       *no_add_attrs = true;
4337     }
4338
4339   return NULL_TREE;
4340 }
4341
4342 /* Handle a "common" attribute; arguments as in
4343    struct attribute_spec.handler.  */
4344
4345 static tree
4346 handle_common_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4347                          int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4348 {
4349   if (TREE_CODE (*node) == VAR_DECL)
4350     DECL_COMMON (*node) = 1;
4351   else
4352     {
4353       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4354       *no_add_attrs = true;
4355     }
4356
4357   return NULL_TREE;
4358 }
4359
4360 /* Handle a "noreturn" attribute; arguments as in
4361    struct attribute_spec.handler.  */
4362
4363 static tree
4364 handle_noreturn_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4365                            int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4366 {
4367   tree type = TREE_TYPE (*node);
4368
4369   /* See FIXME comment in c_common_attribute_table.  */
4370   if (TREE_CODE (*node) == FUNCTION_DECL)
4371     TREE_THIS_VOLATILE (*node) = 1;
4372   else if (TREE_CODE (type) == POINTER_TYPE
4373            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4374     TREE_TYPE (*node)
4375       = build_pointer_type
4376         (build_type_variant (TREE_TYPE (type),
4377                              TREE_READONLY (TREE_TYPE (type)), 1));
4378   else
4379     {
4380       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4381       *no_add_attrs = true;
4382     }
4383
4384   return NULL_TREE;
4385 }
4386
4387 /* Handle a "noinline" attribute; arguments as in
4388    struct attribute_spec.handler.  */
4389
4390 static tree
4391 handle_noinline_attribute (tree *node, tree name,
4392                            tree args ATTRIBUTE_UNUSED,
4393                            int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4394 {
4395   if (TREE_CODE (*node) == FUNCTION_DECL)
4396     DECL_UNINLINABLE (*node) = 1;
4397   else
4398     {
4399       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4400       *no_add_attrs = true;
4401     }
4402
4403   return NULL_TREE;
4404 }
4405
4406 /* Handle a "always_inline" attribute; arguments as in
4407    struct attribute_spec.handler.  */
4408
4409 static tree
4410 handle_always_inline_attribute (tree *node, tree name,
4411                                 tree args ATTRIBUTE_UNUSED,
4412                                 int flags ATTRIBUTE_UNUSED,
4413                                 bool *no_add_attrs)
4414 {
4415   if (TREE_CODE (*node) == FUNCTION_DECL)
4416     {
4417       /* Do nothing else, just set the attribute.  We'll get at
4418          it later with lookup_attribute.  */
4419     }
4420   else
4421     {
4422       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4423       *no_add_attrs = true;
4424     }
4425
4426   return NULL_TREE;
4427 }
4428
4429 /* Handle a "used" attribute; arguments as in
4430    struct attribute_spec.handler.  */
4431
4432 static tree
4433 handle_used_attribute (tree *pnode, tree name, tree args ATTRIBUTE_UNUSED,
4434                        int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4435 {
4436   tree node = *pnode;
4437
4438   if (TREE_CODE (node) == FUNCTION_DECL
4439       || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)))
4440     {
4441       TREE_USED (node) = 1;
4442     }
4443   else
4444     {
4445       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4446       *no_add_attrs = true;
4447     }
4448
4449   return NULL_TREE;
4450 }
4451
4452 /* Handle a "unused" attribute; arguments as in
4453    struct attribute_spec.handler.  */
4454
4455 static tree
4456 handle_unused_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4457                          int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4458 {
4459   if (DECL_P (*node))
4460     {
4461       tree decl = *node;
4462
4463       if (TREE_CODE (decl) == PARM_DECL
4464           || TREE_CODE (decl) == VAR_DECL
4465           || TREE_CODE (decl) == FUNCTION_DECL
4466           || TREE_CODE (decl) == LABEL_DECL
4467           || TREE_CODE (decl) == TYPE_DECL)
4468         TREE_USED (decl) = 1;
4469       else
4470         {
4471           warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4472           *no_add_attrs = true;
4473         }
4474     }
4475   else
4476     {
4477       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4478         *node = build_type_copy (*node);
4479       TREE_USED (*node) = 1;
4480     }
4481
4482   return NULL_TREE;
4483 }
4484
4485 /* Handle a "const" attribute; arguments as in
4486    struct attribute_spec.handler.  */
4487
4488 static tree
4489 handle_const_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4490                         int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4491 {
4492   tree type = TREE_TYPE (*node);
4493
4494   /* See FIXME comment on noreturn in c_common_attribute_table.  */
4495   if (TREE_CODE (*node) == FUNCTION_DECL)
4496     TREE_READONLY (*node) = 1;
4497   else if (TREE_CODE (type) == POINTER_TYPE
4498            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4499     TREE_TYPE (*node)
4500       = build_pointer_type
4501         (build_type_variant (TREE_TYPE (type), 1,
4502                              TREE_THIS_VOLATILE (TREE_TYPE (type))));
4503   else
4504     {
4505       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4506       *no_add_attrs = true;
4507     }
4508
4509   return NULL_TREE;
4510 }
4511
4512 /* Handle a "transparent_union" attribute; arguments as in
4513    struct attribute_spec.handler.  */
4514
4515 static tree
4516 handle_transparent_union_attribute (tree *node, tree name,
4517                                     tree args ATTRIBUTE_UNUSED, int flags,
4518                                     bool *no_add_attrs)
4519 {
4520   tree decl = NULL_TREE;
4521   tree *type = NULL;
4522   int is_type = 0;
4523
4524   if (DECL_P (*node))
4525     {
4526       decl = *node;
4527       type = &TREE_TYPE (decl);
4528       is_type = TREE_CODE (*node) == TYPE_DECL;
4529     }
4530   else if (TYPE_P (*node))
4531     type = node, is_type = 1;
4532
4533   if (is_type
4534       && TREE_CODE (*type) == UNION_TYPE
4535       && (decl == 0
4536           || (TYPE_FIELDS (*type) != 0
4537               && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))))
4538     {
4539       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4540         *type = build_type_copy (*type);
4541       TYPE_TRANSPARENT_UNION (*type) = 1;
4542     }
4543   else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
4544            && TREE_CODE (*type) == UNION_TYPE
4545            && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))
4546     DECL_TRANSPARENT_UNION (decl) = 1;
4547   else
4548     {
4549       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4550       *no_add_attrs = true;
4551     }
4552
4553   return NULL_TREE;
4554 }
4555
4556 /* Handle a "constructor" attribute; arguments as in
4557    struct attribute_spec.handler.  */
4558
4559 static tree
4560 handle_constructor_attribute (tree *node, tree name,
4561                               tree args ATTRIBUTE_UNUSED,
4562                               int flags ATTRIBUTE_UNUSED,
4563                               bool *no_add_attrs)
4564 {
4565   tree decl = *node;
4566   tree type = TREE_TYPE (decl);
4567
4568   if (TREE_CODE (decl) == FUNCTION_DECL
4569       && TREE_CODE (type) == FUNCTION_TYPE
4570       && decl_function_context (decl) == 0)
4571     {
4572       DECL_STATIC_CONSTRUCTOR (decl) = 1;
4573       TREE_USED (decl) = 1;
4574     }
4575   else
4576     {
4577       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4578       *no_add_attrs = true;
4579     }
4580
4581   return NULL_TREE;
4582 }
4583
4584 /* Handle a "destructor" attribute; arguments as in
4585    struct attribute_spec.handler.  */
4586
4587 static tree
4588 handle_destructor_attribute (tree *node, tree name,
4589                              tree args ATTRIBUTE_UNUSED,
4590                              int flags ATTRIBUTE_UNUSED,
4591                              bool *no_add_attrs)
4592 {
4593   tree decl = *node;
4594   tree type = TREE_TYPE (decl);
4595
4596   if (TREE_CODE (decl) == FUNCTION_DECL
4597       && TREE_CODE (type) == FUNCTION_TYPE
4598       && decl_function_context (decl) == 0)
4599     {
4600       DECL_STATIC_DESTRUCTOR (decl) = 1;
4601       TREE_USED (decl) = 1;
4602     }
4603   else
4604     {
4605       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4606       *no_add_attrs = true;
4607     }
4608
4609   return NULL_TREE;
4610 }
4611
4612 /* Handle a "mode" attribute; arguments as in
4613    struct attribute_spec.handler.  */
4614
4615 static tree
4616 handle_mode_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4617                        int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4618 {
4619   tree type = *node;
4620
4621   *no_add_attrs = true;
4622
4623   if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
4624     warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4625   else
4626     {
4627       int j;
4628       const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
4629       int len = strlen (p);
4630       enum machine_mode mode = VOIDmode;
4631       tree typefm;
4632       tree ptr_type;
4633
4634       if (len > 4 && p[0] == '_' && p[1] == '_'
4635           && p[len - 1] == '_' && p[len - 2] == '_')
4636         {
4637           char *newp = alloca (len - 1);
4638
4639           strcpy (newp, &p[2]);
4640           newp[len - 4] = '\0';
4641           p = newp;
4642         }
4643
4644       /* Change this type to have a type with the specified mode.
4645          First check for the special modes.  */
4646       if (! strcmp (p, "byte"))
4647         mode = byte_mode;
4648       else if (!strcmp (p, "word"))
4649         mode = word_mode;
4650       else if (! strcmp (p, "pointer"))
4651         mode = ptr_mode;
4652       else
4653         for (j = 0; j < NUM_MACHINE_MODES; j++)
4654           if (!strcmp (p, GET_MODE_NAME (j)))
4655             mode = (enum machine_mode) j;
4656
4657       if (mode == VOIDmode)
4658         error ("unknown machine mode `%s'", p);
4659       else if (0 == (typefm = (*lang_hooks.types.type_for_mode)
4660                      (mode, TREE_UNSIGNED (type))))
4661         error ("no data type for mode `%s'", p);
4662       else if ((TREE_CODE (type) == POINTER_TYPE
4663                 || TREE_CODE (type) == REFERENCE_TYPE)
4664                && !(*targetm.valid_pointer_mode) (mode))
4665         error ("invalid pointer mode `%s'", p);
4666       else
4667         {
4668           /* If this is a vector, make sure we either have hardware
4669              support, or we can emulate it.  */
4670           if (VECTOR_MODE_P (mode) && !vector_mode_valid_p (mode))
4671             {
4672               error ("unable to emulate '%s'", GET_MODE_NAME (mode));
4673               return NULL_TREE;
4674             }
4675
4676           if (TREE_CODE (type) == POINTER_TYPE)
4677             {
4678               ptr_type = build_pointer_type_for_mode (TREE_TYPE (type),
4679                                                       mode);
4680               *node = ptr_type;
4681             }
4682           else if (TREE_CODE (type) == REFERENCE_TYPE)
4683             {
4684               ptr_type = build_reference_type_for_mode (TREE_TYPE (type),
4685                                                         mode);
4686               *node = ptr_type;
4687             }
4688           else
4689           *node = typefm;
4690           /* No need to layout the type here.  The caller should do this.  */
4691         }
4692     }
4693
4694   return NULL_TREE;
4695 }
4696
4697 /* Handle a "section" attribute; arguments as in
4698    struct attribute_spec.handler.  */
4699
4700 static tree
4701 handle_section_attribute (tree *node, tree name ATTRIBUTE_UNUSED, tree args,
4702                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4703 {
4704   tree decl = *node;
4705
4706   if (targetm.have_named_sections)
4707     {
4708       if ((TREE_CODE (decl) == FUNCTION_DECL
4709            || TREE_CODE (decl) == VAR_DECL)
4710           && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
4711         {
4712           if (TREE_CODE (decl) == VAR_DECL
4713               && current_function_decl != NULL_TREE
4714               && ! TREE_STATIC (decl))
4715             {
4716               error ("%Jsection attribute cannot be specified for "
4717                      "local variables", decl);
4718               *no_add_attrs = true;
4719             }
4720
4721           /* The decl may have already been given a section attribute
4722              from a previous declaration.  Ensure they match.  */
4723           else if (DECL_SECTION_NAME (decl) != NULL_TREE
4724                    && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
4725                               TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
4726             {
4727               error ("%Jsection of '%D' conflicts with previous declaration",
4728                      *node, *node);
4729               *no_add_attrs = true;
4730             }
4731           else
4732             DECL_SECTION_NAME (decl) = TREE_VALUE (args);
4733         }
4734       else
4735         {
4736           error ("%Jsection attribute not allowed for '%D'", *node, *node);
4737           *no_add_attrs = true;
4738         }
4739     }
4740   else
4741     {
4742       error ("%Jsection attributes are not supported for this target", *node);
4743       *no_add_attrs = true;
4744     }
4745
4746   return NULL_TREE;
4747 }
4748
4749 /* Handle a "aligned" attribute; arguments as in
4750    struct attribute_spec.handler.  */
4751
4752 static tree
4753 handle_aligned_attribute (tree *node, tree name ATTRIBUTE_UNUSED, tree args,
4754                           int flags, bool *no_add_attrs)
4755 {
4756   tree decl = NULL_TREE;
4757   tree *type = NULL;
4758   int is_type = 0;
4759   tree align_expr = (args ? TREE_VALUE (args)
4760                      : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
4761   int i;
4762
4763   if (DECL_P (*node))
4764     {
4765       decl = *node;
4766       type = &TREE_TYPE (decl);
4767       is_type = TREE_CODE (*node) == TYPE_DECL;
4768     }
4769   else if (TYPE_P (*node))
4770     type = node, is_type = 1;
4771
4772   /* Strip any NOPs of any kind.  */
4773   while (TREE_CODE (align_expr) == NOP_EXPR
4774          || TREE_CODE (align_expr) == CONVERT_EXPR
4775          || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
4776     align_expr = TREE_OPERAND (align_expr, 0);
4777
4778   if (TREE_CODE (align_expr) != INTEGER_CST)
4779     {
4780       error ("requested alignment is not a constant");
4781       *no_add_attrs = true;
4782     }
4783   else if ((i = tree_log2 (align_expr)) == -1)
4784     {
4785       error ("requested alignment is not a power of 2");
4786       *no_add_attrs = true;
4787     }
4788   else if (i > HOST_BITS_PER_INT - 2)
4789     {
4790       error ("requested alignment is too large");
4791       *no_add_attrs = true;
4792     }
4793   else if (is_type)
4794     {
4795       /* If we have a TYPE_DECL, then copy the type, so that we
4796          don't accidentally modify a builtin type.  See pushdecl.  */
4797       if (decl && TREE_TYPE (decl) != error_mark_node
4798           && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
4799         {
4800           tree tt = TREE_TYPE (decl);
4801           *type = build_type_copy (*type);
4802           DECL_ORIGINAL_TYPE (decl) = tt;
4803           TYPE_NAME (*type) = decl;
4804           TREE_USED (*type) = TREE_USED (decl);
4805           TREE_TYPE (decl) = *type;
4806         }
4807       else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4808         *type = build_type_copy (*type);
4809
4810       TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
4811       TYPE_USER_ALIGN (*type) = 1;
4812     }
4813   else if (TREE_CODE (decl) != VAR_DECL
4814            && TREE_CODE (decl) != FIELD_DECL)
4815     {
4816       error ("%Jalignment may not be specified for '%D'", decl, decl);
4817       *no_add_attrs = true;
4818     }
4819   else
4820     {
4821       DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
4822       DECL_USER_ALIGN (decl) = 1;
4823     }
4824
4825   return NULL_TREE;
4826 }
4827
4828 /* Handle a "weak" attribute; arguments as in
4829    struct attribute_spec.handler.  */
4830
4831 static tree
4832 handle_weak_attribute (tree *node, tree name ATTRIBUTE_UNUSED,
4833                        tree args ATTRIBUTE_UNUSED,
4834                        int flags ATTRIBUTE_UNUSED,
4835                        bool *no_add_attrs ATTRIBUTE_UNUSED)
4836 {
4837   declare_weak (*node);
4838
4839   return NULL_TREE;
4840 }
4841
4842 /* Handle an "alias" attribute; arguments as in
4843    struct attribute_spec.handler.  */
4844
4845 static tree
4846 handle_alias_attribute (tree *node, tree name, tree args,
4847                         int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4848 {
4849   tree decl = *node;
4850
4851   if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
4852       || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
4853     {
4854       error ("%J'%D' defined both normally and as an alias", decl, decl);
4855       *no_add_attrs = true;
4856     }
4857   else if (decl_function_context (decl) == 0)
4858     {
4859       tree id;
4860
4861       id = TREE_VALUE (args);
4862       if (TREE_CODE (id) != STRING_CST)
4863         {
4864           error ("alias arg not a string");
4865           *no_add_attrs = true;
4866           return NULL_TREE;
4867         }
4868       id = get_identifier (TREE_STRING_POINTER (id));
4869       /* This counts as a use of the object pointed to.  */
4870       TREE_USED (id) = 1;
4871
4872       if (TREE_CODE (decl) == FUNCTION_DECL)
4873         DECL_INITIAL (decl) = error_mark_node;
4874       else
4875         DECL_EXTERNAL (decl) = 0;
4876     }
4877   else
4878     {
4879       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4880       *no_add_attrs = true;
4881     }
4882
4883   return NULL_TREE;
4884 }
4885
4886 /* Handle an "visibility" attribute; arguments as in
4887    struct attribute_spec.handler.  */
4888
4889 static tree
4890 handle_visibility_attribute (tree *node, tree name, tree args,
4891                              int flags ATTRIBUTE_UNUSED,
4892                              bool *no_add_attrs)
4893 {
4894   tree decl = *node;
4895
4896   if (decl_function_context (decl) != 0 || ! TREE_PUBLIC (decl))
4897     {
4898       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4899       *no_add_attrs = true;
4900     }
4901   else
4902     {
4903       tree id;
4904
4905       id = TREE_VALUE (args);
4906       if (TREE_CODE (id) != STRING_CST)
4907         {
4908           error ("visibility arg not a string");
4909           *no_add_attrs = true;
4910           return NULL_TREE;
4911         }
4912       if (strcmp (TREE_STRING_POINTER (id), "hidden")
4913           && strcmp (TREE_STRING_POINTER (id), "protected")
4914           && strcmp (TREE_STRING_POINTER (id), "internal")
4915           && strcmp (TREE_STRING_POINTER (id), "default"))
4916         {
4917           error ("visibility arg must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
4918           *no_add_attrs = true;
4919           return NULL_TREE;
4920         }
4921     }
4922
4923   return NULL_TREE;
4924 }
4925
4926 /* Handle an "tls_model" attribute; arguments as in
4927    struct attribute_spec.handler.  */
4928
4929 static tree
4930 handle_tls_model_attribute (tree *node, tree name, tree args,
4931                             int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4932 {
4933   tree decl = *node;
4934
4935   if (! DECL_THREAD_LOCAL (decl))
4936     {
4937       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4938       *no_add_attrs = true;
4939     }
4940   else
4941     {
4942       tree id;
4943
4944       id = TREE_VALUE (args);
4945       if (TREE_CODE (id) != STRING_CST)
4946         {
4947           error ("tls_model arg not a string");
4948           *no_add_attrs = true;
4949           return NULL_TREE;
4950         }
4951       if (strcmp (TREE_STRING_POINTER (id), "local-exec")
4952           && strcmp (TREE_STRING_POINTER (id), "initial-exec")
4953           && strcmp (TREE_STRING_POINTER (id), "local-dynamic")
4954           && strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
4955         {
4956           error ("tls_model arg must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
4957           *no_add_attrs = true;
4958           return NULL_TREE;
4959         }
4960     }
4961
4962   return NULL_TREE;
4963 }
4964
4965 /* Handle a "no_instrument_function" attribute; arguments as in
4966    struct attribute_spec.handler.  */
4967
4968 static tree
4969 handle_no_instrument_function_attribute (tree *node, tree name,
4970                                          tree args ATTRIBUTE_UNUSED,
4971                                          int flags ATTRIBUTE_UNUSED,
4972                                          bool *no_add_attrs)
4973 {
4974   tree decl = *node;
4975
4976   if (TREE_CODE (decl) != FUNCTION_DECL)
4977     {
4978       error ("%J'%E' attribute applies only to functions", decl, name);
4979       *no_add_attrs = true;
4980     }
4981   else if (DECL_INITIAL (decl))
4982     {
4983       error ("%Jcan't set '%E' attribute after definition", decl, name);
4984       *no_add_attrs = true;
4985     }
4986   else
4987     DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
4988
4989   return NULL_TREE;
4990 }
4991
4992 /* Handle a "malloc" attribute; arguments as in
4993    struct attribute_spec.handler.  */
4994
4995 static tree
4996 handle_malloc_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
4997                          int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4998 {
4999   if (TREE_CODE (*node) == FUNCTION_DECL)
5000     DECL_IS_MALLOC (*node) = 1;
5001   /* ??? TODO: Support types.  */
5002   else
5003     {
5004       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5005       *no_add_attrs = true;
5006     }
5007
5008   return NULL_TREE;
5009 }
5010
5011 /* Handle a "no_limit_stack" attribute; arguments as in
5012    struct attribute_spec.handler.  */
5013
5014 static tree
5015 handle_no_limit_stack_attribute (tree *node, tree name,
5016                                  tree args ATTRIBUTE_UNUSED,
5017                                  int flags ATTRIBUTE_UNUSED,
5018                                  bool *no_add_attrs)
5019 {
5020   tree decl = *node;
5021
5022   if (TREE_CODE (decl) != FUNCTION_DECL)
5023     {
5024       error ("%J'%E' attribute applies only to functions", decl, name);
5025       *no_add_attrs = true;
5026     }
5027   else if (DECL_INITIAL (decl))
5028     {
5029       error ("%Jcan't set '%E' attribute after definition", decl, name);
5030       *no_add_attrs = true;
5031     }
5032   else
5033     DECL_NO_LIMIT_STACK (decl) = 1;
5034
5035   return NULL_TREE;
5036 }
5037
5038 /* Handle a "pure" attribute; arguments as in
5039    struct attribute_spec.handler.  */
5040
5041 static tree
5042 handle_pure_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
5043                        int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
5044 {
5045   if (TREE_CODE (*node) == FUNCTION_DECL)
5046     DECL_IS_PURE (*node) = 1;
5047   /* ??? TODO: Support types.  */
5048   else
5049     {
5050       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5051       *no_add_attrs = true;
5052     }
5053
5054   return NULL_TREE;
5055 }
5056
5057 /* Handle a "deprecated" attribute; arguments as in
5058    struct attribute_spec.handler.  */
5059
5060 static tree
5061 handle_deprecated_attribute (tree *node, tree name,
5062                              tree args ATTRIBUTE_UNUSED, int flags,
5063                              bool *no_add_attrs)
5064 {
5065   tree type = NULL_TREE;
5066   int warn = 0;
5067   const char *what = NULL;
5068
5069   if (DECL_P (*node))
5070     {
5071       tree decl = *node;
5072       type = TREE_TYPE (decl);
5073
5074       if (TREE_CODE (decl) == TYPE_DECL
5075           || TREE_CODE (decl) == PARM_DECL
5076           || TREE_CODE (decl) == VAR_DECL
5077           || TREE_CODE (decl) == FUNCTION_DECL
5078           || TREE_CODE (decl) == FIELD_DECL)
5079         TREE_DEPRECATED (decl) = 1;
5080       else
5081         warn = 1;
5082     }
5083   else if (TYPE_P (*node))
5084     {
5085       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5086         *node = build_type_copy (*node);
5087       TREE_DEPRECATED (*node) = 1;
5088       type = *node;
5089     }
5090   else
5091     warn = 1;
5092
5093   if (warn)
5094     {
5095       *no_add_attrs = true;
5096       if (type && TYPE_NAME (type))
5097         {
5098           if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
5099             what = IDENTIFIER_POINTER (TYPE_NAME (*node));
5100           else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5101                    && DECL_NAME (TYPE_NAME (type)))
5102             what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
5103         }
5104       if (what)
5105         warning ("`%s' attribute ignored for `%s'",
5106                   IDENTIFIER_POINTER (name), what);
5107       else
5108         warning ("`%s' attribute ignored",
5109                       IDENTIFIER_POINTER (name));
5110     }
5111
5112   return NULL_TREE;
5113 }
5114
5115 /* Keep a list of vector type nodes we created in handle_vector_size_attribute,
5116    to prevent us from duplicating type nodes unnecessarily.
5117    The normal mechanism to prevent duplicates is to use type_hash_canon, but
5118    since we want to distinguish types that are essentially identical (except
5119    for their debug representation), we use a local list here.  */
5120 static GTY(()) tree vector_type_node_list = 0;
5121
5122 /* Handle a "vector_size" attribute; arguments as in
5123    struct attribute_spec.handler.  */
5124
5125 static tree
5126 handle_vector_size_attribute (tree *node, tree name, tree args,
5127                               int flags ATTRIBUTE_UNUSED,
5128                               bool *no_add_attrs)
5129 {
5130   unsigned HOST_WIDE_INT vecsize, nunits;
5131   enum machine_mode mode, orig_mode, new_mode;
5132   tree type = *node, new_type = NULL_TREE;
5133   tree type_list_node;
5134
5135   *no_add_attrs = true;
5136
5137   if (! host_integerp (TREE_VALUE (args), 1))
5138     {
5139       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5140       return NULL_TREE;
5141     }
5142
5143   /* Get the vector size (in bytes).  */
5144   vecsize = tree_low_cst (TREE_VALUE (args), 1);
5145
5146   /* We need to provide for vector pointers, vector arrays, and
5147      functions returning vectors.  For example:
5148
5149        __attribute__((vector_size(16))) short *foo;
5150
5151      In this case, the mode is SI, but the type being modified is
5152      HI, so we need to look further.  */
5153
5154   while (POINTER_TYPE_P (type)
5155          || TREE_CODE (type) == FUNCTION_TYPE
5156          || TREE_CODE (type) == METHOD_TYPE
5157          || TREE_CODE (type) == ARRAY_TYPE)
5158     type = TREE_TYPE (type);
5159
5160   /* Get the mode of the type being modified.  */
5161   orig_mode = TYPE_MODE (type);
5162
5163   if (TREE_CODE (type) == RECORD_TYPE
5164       || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
5165           && GET_MODE_CLASS (orig_mode) != MODE_INT)
5166       || ! host_integerp (TYPE_SIZE_UNIT (type), 1))
5167     {
5168       error ("invalid vector type for attribute `%s'",
5169              IDENTIFIER_POINTER (name));
5170       return NULL_TREE;
5171     }
5172
5173   /* Calculate how many units fit in the vector.  */
5174   nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5175
5176   /* Find a suitably sized vector.  */
5177   new_mode = VOIDmode;
5178   for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
5179                                         ? MODE_VECTOR_INT
5180                                         : MODE_VECTOR_FLOAT);
5181        mode != VOIDmode;
5182        mode = GET_MODE_WIDER_MODE (mode))
5183     if (vecsize == GET_MODE_SIZE (mode)
5184         && nunits == (unsigned HOST_WIDE_INT) GET_MODE_NUNITS (mode))
5185       {
5186         new_mode = mode;
5187         break;
5188       }
5189
5190     if (new_mode == VOIDmode)
5191     {
5192       error ("no vector mode with the size and type specified could be found");
5193       return NULL_TREE;
5194     }
5195
5196   for (type_list_node = vector_type_node_list; type_list_node;
5197        type_list_node = TREE_CHAIN (type_list_node))
5198     {
5199       tree other_type = TREE_VALUE (type_list_node);
5200       tree record = TYPE_DEBUG_REPRESENTATION_TYPE (other_type);
5201       tree fields = TYPE_FIELDS (record);
5202       tree field_type = TREE_TYPE (fields);
5203       tree array_type = TREE_TYPE (field_type);
5204       if (TREE_CODE (fields) != FIELD_DECL
5205           || TREE_CODE (field_type) != ARRAY_TYPE)
5206         abort ();
5207
5208       if (TYPE_MODE (other_type) == mode && type == array_type)
5209         {
5210           new_type = other_type;
5211           break;
5212         }
5213     }
5214
5215   if (new_type == NULL_TREE)
5216     {
5217       tree index, array, rt, list_node;
5218
5219       new_type = (*lang_hooks.types.type_for_mode) (new_mode,
5220                                                     TREE_UNSIGNED (type));
5221
5222       if (!new_type)
5223         {
5224           error ("no vector mode with the size and type specified could be found");
5225           return NULL_TREE;
5226         }
5227
5228       new_type = build_type_copy (new_type);
5229
5230       /* If this is a vector, make sure we either have hardware
5231          support, or we can emulate it.  */
5232       if ((GET_MODE_CLASS (mode) == MODE_VECTOR_INT
5233            || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
5234           && !vector_mode_valid_p (mode))
5235         {
5236           error ("unable to emulate '%s'", GET_MODE_NAME (mode));
5237           return NULL_TREE;
5238         }
5239
5240       /* Set the debug information here, because this is the only
5241          place where we know the underlying type for a vector made
5242          with vector_size.  For debugging purposes we pretend a vector
5243          is an array within a structure.  */
5244       index = build_int_2 (TYPE_VECTOR_SUBPARTS (new_type) - 1, 0);
5245       array = build_array_type (type, build_index_type (index));
5246       rt = make_node (RECORD_TYPE);
5247
5248       TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
5249       DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
5250       layout_type (rt);
5251       TYPE_DEBUG_REPRESENTATION_TYPE (new_type) = rt;
5252
5253       list_node = build_tree_list (NULL, new_type);
5254       TREE_CHAIN (list_node) = vector_type_node_list;
5255       vector_type_node_list = list_node;
5256     }
5257
5258   /* Build back pointers if needed.  */
5259   *node = vector_size_helper (*node, new_type);
5260
5261   return NULL_TREE;
5262 }
5263
5264 /* HACK.  GROSS.  This is absolutely disgusting.  I wish there was a
5265    better way.
5266
5267    If we requested a pointer to a vector, build up the pointers that
5268    we stripped off while looking for the inner type.  Similarly for
5269    return values from functions.
5270
5271    The argument "type" is the top of the chain, and "bottom" is the
5272    new type which we will point to.  */
5273
5274 static tree
5275 vector_size_helper (tree type, tree bottom)
5276 {
5277   tree inner, outer;
5278
5279   if (POINTER_TYPE_P (type))
5280     {
5281       inner = vector_size_helper (TREE_TYPE (type), bottom);
5282       outer = build_pointer_type (inner);
5283     }
5284   else if (TREE_CODE (type) == ARRAY_TYPE)
5285     {
5286       inner = vector_size_helper (TREE_TYPE (type), bottom);
5287       outer = build_array_type (inner, TYPE_DOMAIN (type));
5288     }
5289   else if (TREE_CODE (type) == FUNCTION_TYPE)
5290     {
5291       inner = vector_size_helper (TREE_TYPE (type), bottom);
5292       outer = build_function_type (inner, TYPE_ARG_TYPES (type));
5293     }
5294   else if (TREE_CODE (type) == METHOD_TYPE)
5295     {
5296       inner = vector_size_helper (TREE_TYPE (type), bottom);
5297       outer = build_method_type_directly (TYPE_METHOD_BASETYPE (type),
5298                                           inner, 
5299                                           TYPE_ARG_TYPES (type));
5300     }
5301   else
5302     return bottom;
5303
5304   TREE_READONLY (outer) = TREE_READONLY (type);
5305   TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
5306
5307   return outer;
5308 }
5309
5310 /* Handle the "nonnull" attribute.  */
5311 static tree
5312 handle_nonnull_attribute (tree *node, tree name ATTRIBUTE_UNUSED,
5313                           tree args, int flags ATTRIBUTE_UNUSED,
5314                           bool *no_add_attrs)
5315 {
5316   tree type = *node;
5317   unsigned HOST_WIDE_INT attr_arg_num;
5318
5319   /* If no arguments are specified, all pointer arguments should be
5320      non-null.  Verify a full prototype is given so that the arguments
5321      will have the correct types when we actually check them later.  */
5322   if (! args)
5323     {
5324       if (! TYPE_ARG_TYPES (type))
5325         {
5326           error ("nonnull attribute without arguments on a non-prototype");
5327           *no_add_attrs = true;
5328         }
5329       return NULL_TREE;
5330     }
5331
5332   /* Argument list specified.  Verify that each argument number references
5333      a pointer argument.  */
5334   for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
5335     {
5336       tree argument;
5337       unsigned HOST_WIDE_INT arg_num, ck_num;
5338
5339       if (! get_nonnull_operand (TREE_VALUE (args), &arg_num))
5340         {
5341           error ("nonnull argument has invalid operand number (arg %lu)",
5342                  (unsigned long) attr_arg_num);
5343           *no_add_attrs = true;
5344           return NULL_TREE;
5345         }
5346
5347       argument = TYPE_ARG_TYPES (type);
5348       if (argument)
5349         {
5350           for (ck_num = 1; ; ck_num++)
5351             {
5352               if (! argument || ck_num == arg_num)
5353                 break;
5354               argument = TREE_CHAIN (argument);
5355             }
5356
5357           if (! argument
5358               || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
5359             {
5360               error ("nonnull argument with out-of-range operand number (arg %lu, operand %lu)",
5361                      (unsigned long) attr_arg_num, (unsigned long) arg_num);
5362               *no_add_attrs = true;
5363               return NULL_TREE;
5364             }
5365
5366           if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
5367             {
5368               error ("nonnull argument references non-pointer operand (arg %lu, operand %lu)",
5369                    (unsigned long) attr_arg_num, (unsigned long) arg_num);
5370               *no_add_attrs = true;
5371               return NULL_TREE;
5372             }
5373         }
5374     }
5375
5376   return NULL_TREE;
5377 }
5378
5379 /* Check the argument list of a function call for null in argument slots
5380    that are marked as requiring a non-null pointer argument.  */
5381
5382 static void
5383 check_function_nonnull (tree attrs, tree params)
5384 {
5385   tree a, args, param;
5386   int param_num;
5387
5388   for (a = attrs; a; a = TREE_CHAIN (a))
5389     {
5390       if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
5391         {
5392           args = TREE_VALUE (a);
5393
5394           /* Walk the argument list.  If we encounter an argument number we
5395              should check for non-null, do it.  If the attribute has no args,
5396              then every pointer argument is checked (in which case the check
5397              for pointer type is done in check_nonnull_arg).  */
5398           for (param = params, param_num = 1; ;
5399                param_num++, param = TREE_CHAIN (param))
5400             {
5401               if (! param)
5402         break;
5403               if (! args || nonnull_check_p (args, param_num))
5404         check_function_arguments_recurse (check_nonnull_arg, NULL,
5405                                           TREE_VALUE (param),
5406                                           param_num);
5407             }
5408         }
5409     }
5410 }
5411
5412 /* Helper for check_function_nonnull; given a list of operands which
5413    must be non-null in ARGS, determine if operand PARAM_NUM should be
5414    checked.  */
5415
5416 static bool
5417 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5418 {
5419   unsigned HOST_WIDE_INT arg_num;
5420
5421   for (; args; args = TREE_CHAIN (args))
5422     {
5423       if (! get_nonnull_operand (TREE_VALUE (args), &arg_num))
5424         abort ();
5425
5426       if (arg_num == param_num)
5427         return true;
5428     }
5429   return false;
5430 }
5431
5432 /* Check that the function argument PARAM (which is operand number
5433    PARAM_NUM) is non-null.  This is called by check_function_nonnull
5434    via check_function_arguments_recurse.  */
5435
5436 static void
5437 check_nonnull_arg (void *ctx ATTRIBUTE_UNUSED, tree param,
5438                    unsigned HOST_WIDE_INT param_num)
5439 {
5440   /* Just skip checking the argument if it's not a pointer.  This can
5441      happen if the "nonnull" attribute was given without an operand
5442      list (which means to check every pointer argument).  */
5443
5444   if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5445     return;
5446
5447   if (integer_zerop (param))
5448     warning ("null argument where non-null required (arg %lu)",
5449              (unsigned long) param_num);
5450 }
5451
5452 /* Helper for nonnull attribute handling; fetch the operand number
5453    from the attribute argument list.  */
5454
5455 static bool
5456 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5457 {
5458   /* Strip any conversions from the arg number and verify they
5459      are constants.  */
5460   while (TREE_CODE (arg_num_expr) == NOP_EXPR
5461          || TREE_CODE (arg_num_expr) == CONVERT_EXPR
5462          || TREE_CODE (arg_num_expr) == NON_LVALUE_EXPR)
5463     arg_num_expr = TREE_OPERAND (arg_num_expr, 0);
5464
5465   if (TREE_CODE (arg_num_expr) != INTEGER_CST
5466       || TREE_INT_CST_HIGH (arg_num_expr) != 0)
5467     return false;
5468
5469   *valp = TREE_INT_CST_LOW (arg_num_expr);
5470   return true;
5471 }
5472
5473 /* Handle a "nothrow" attribute; arguments as in
5474    struct attribute_spec.handler.  */
5475
5476 static tree
5477 handle_nothrow_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
5478                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
5479 {
5480   if (TREE_CODE (*node) == FUNCTION_DECL)
5481     TREE_NOTHROW (*node) = 1;
5482   /* ??? TODO: Support types.  */
5483   else
5484     {
5485       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5486       *no_add_attrs = true;
5487     }
5488
5489   return NULL_TREE;
5490 }
5491
5492 /* Handle a "cleanup" attribute; arguments as in
5493    struct attribute_spec.handler.  */
5494
5495 static tree
5496 handle_cleanup_attribute (tree *node, tree name, tree args,
5497                           int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
5498 {
5499   tree decl = *node;
5500   tree cleanup_id, cleanup_decl;
5501
5502   /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
5503      for global destructors in C++.  This requires infrastructure that
5504      we don't have generically at the moment.  It's also not a feature
5505      we'd be missing too much, since we do have attribute constructor.  */
5506   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
5507     {
5508       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5509       *no_add_attrs = true;
5510       return NULL_TREE;
5511     }
5512
5513   /* Verify that the argument is a function in scope.  */
5514   /* ??? We could support pointers to functions here as well, if
5515      that was considered desirable.  */
5516   cleanup_id = TREE_VALUE (args);
5517   if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
5518     {
5519       error ("cleanup arg not an identifier");
5520       *no_add_attrs = true;
5521       return NULL_TREE;
5522     }
5523   cleanup_decl = lookup_name (cleanup_id);
5524   if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
5525     {
5526       error ("cleanup arg not a function");
5527       *no_add_attrs = true;
5528       return NULL_TREE;
5529     }
5530
5531   /* That the function has proper type is checked with the
5532      eventual call to build_function_call.  */
5533
5534   return NULL_TREE;
5535 }
5536
5537 /* Handle a "warn_unused_result" attribute.  No special handling.  */
5538
5539 static tree
5540 handle_warn_unused_result_attribute (tree *node, tree name,
5541                                tree args ATTRIBUTE_UNUSED,
5542                                int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
5543 {
5544   /* Ignore the attribute for functions not returning any value.  */
5545   if (VOID_TYPE_P (TREE_TYPE (*node)))
5546     {
5547       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5548       *no_add_attrs = true;
5549     }
5550
5551   return NULL_TREE;
5552 }
5553 \f
5554 /* Check for valid arguments being passed to a function.  */
5555 void
5556 check_function_arguments (tree attrs, tree params)
5557 {
5558   /* Check for null being passed in a pointer argument that must be
5559      non-null.  We also need to do this if format checking is enabled.  */
5560
5561   if (warn_nonnull)
5562     check_function_nonnull (attrs, params);
5563
5564   /* Check for errors in format strings.  */
5565
5566   if (warn_format)
5567     check_function_format (NULL, attrs, params);
5568 }
5569
5570 /* Generic argument checking recursion routine.  PARAM is the argument to
5571    be checked.  PARAM_NUM is the number of the argument.  CALLBACK is invoked
5572    once the argument is resolved.  CTX is context for the callback.  */
5573 void
5574 check_function_arguments_recurse (void (*callback)
5575                                   (void *, tree, unsigned HOST_WIDE_INT),
5576                                   void *ctx, tree param,
5577                                   unsigned HOST_WIDE_INT param_num)
5578 {
5579   if (TREE_CODE (param) == NOP_EXPR)
5580     {
5581       /* Strip coercion.  */
5582       check_function_arguments_recurse (callback, ctx,
5583                                         TREE_OPERAND (param, 0), param_num);
5584       return;
5585     }
5586
5587   if (TREE_CODE (param) == CALL_EXPR)
5588     {
5589       tree type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (param, 0)));
5590       tree attrs;
5591       bool found_format_arg = false;
5592
5593       /* See if this is a call to a known internationalization function
5594          that modifies a format arg.  Such a function may have multiple
5595          format_arg attributes (for example, ngettext).  */
5596
5597       for (attrs = TYPE_ATTRIBUTES (type);
5598            attrs;
5599            attrs = TREE_CHAIN (attrs))
5600         if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5601           {
5602             tree inner_args;
5603             tree format_num_expr;
5604             int format_num;
5605             int i;
5606
5607             /* Extract the argument number, which was previously checked
5608                to be valid.  */
5609             format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5610             while (TREE_CODE (format_num_expr) == NOP_EXPR
5611                    || TREE_CODE (format_num_expr) == CONVERT_EXPR
5612                    || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
5613               format_num_expr = TREE_OPERAND (format_num_expr, 0);
5614
5615             if (TREE_CODE (format_num_expr) != INTEGER_CST
5616                 || TREE_INT_CST_HIGH (format_num_expr) != 0)
5617               abort ();
5618
5619             format_num = TREE_INT_CST_LOW (format_num_expr);
5620
5621             for (inner_args = TREE_OPERAND (param, 1), i = 1;
5622                  inner_args != 0;
5623                  inner_args = TREE_CHAIN (inner_args), i++)
5624               if (i == format_num)
5625                 {
5626                   check_function_arguments_recurse (callback, ctx,
5627                                                     TREE_VALUE (inner_args),
5628                                                     param_num);
5629                   found_format_arg = true;
5630                   break;
5631                 }
5632           }
5633
5634       /* If we found a format_arg attribute and did a recursive check,
5635          we are done with checking this argument.  Otherwise, we continue
5636          and this will be considered a non-literal.  */
5637       if (found_format_arg)
5638         return;
5639     }
5640
5641   if (TREE_CODE (param) == COND_EXPR)
5642     {
5643       /* Check both halves of the conditional expression.  */
5644       check_function_arguments_recurse (callback, ctx,
5645                                         TREE_OPERAND (param, 1), param_num);
5646       check_function_arguments_recurse (callback, ctx,
5647                                         TREE_OPERAND (param, 2), param_num);
5648       return;
5649     }
5650
5651   (*callback) (ctx, param, param_num);
5652 }
5653
5654 /* Function to help qsort sort FIELD_DECLs by name order.  */
5655
5656 int
5657 field_decl_cmp (const void *x_p, const void *y_p)
5658 {
5659   const tree *const x = x_p;
5660   const tree *const y = y_p;
5661   if (DECL_NAME (*x) == DECL_NAME (*y))
5662     /* A nontype is "greater" than a type.  */
5663     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5664   if (DECL_NAME (*x) == NULL_TREE)
5665     return -1;
5666   if (DECL_NAME (*y) == NULL_TREE)
5667     return 1;
5668   if (DECL_NAME (*x) < DECL_NAME (*y))
5669     return -1;
5670   return 1;
5671 }
5672
5673 static struct {
5674   gt_pointer_operator new_value;
5675   void *cookie;
5676 } resort_data;
5677
5678 /* This routine compares two fields like field_decl_cmp but using the
5679 pointer operator in resort_data.  */
5680
5681 static int
5682 resort_field_decl_cmp (const void *x_p, const void *y_p)
5683 {
5684   const tree *const x = x_p;
5685   const tree *const y = y_p;
5686
5687   if (DECL_NAME (*x) == DECL_NAME (*y))
5688     /* A nontype is "greater" than a type.  */
5689     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5690   if (DECL_NAME (*x) == NULL_TREE)
5691     return -1;
5692   if (DECL_NAME (*y) == NULL_TREE)
5693     return 1;
5694   {
5695     tree d1 = DECL_NAME (*x);
5696     tree d2 = DECL_NAME (*y);
5697     resort_data.new_value (&d1, resort_data.cookie);
5698     resort_data.new_value (&d2, resort_data.cookie);
5699     if (d1 < d2)
5700       return -1;
5701   }
5702   return 1;
5703 }
5704
5705 /* Resort DECL_SORTED_FIELDS because pointers have been reordered.  */
5706
5707 void
5708 resort_sorted_fields (void *obj,
5709                       void *orig_obj ATTRIBUTE_UNUSED ,
5710                       gt_pointer_operator new_value,
5711                       void *cookie)
5712 {
5713   struct sorted_fields_type *sf = obj;
5714   resort_data.new_value = new_value;
5715   resort_data.cookie = cookie;
5716   qsort (&sf->elts[0], sf->len, sizeof (tree),
5717          resort_field_decl_cmp);
5718 }
5719
5720 /* Used by estimate_num_insns.  Estimate number of instructions seen
5721    by given statement.  */
5722 static tree
5723 c_estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
5724 {
5725   int *count = data;
5726   tree x = *tp;
5727
5728   if (TYPE_P (x) || DECL_P (x))
5729     {
5730       *walk_subtrees = 0;
5731       return NULL;
5732     }
5733   /* Assume that constants and references counts nothing.  These should
5734      be majorized by amount of operations among them we count later
5735      and are common target of CSE and similar optimizations.  */
5736   if (TREE_CODE_CLASS (TREE_CODE (x)) == 'c'
5737       || TREE_CODE_CLASS (TREE_CODE (x)) == 'r')
5738     return NULL;
5739   switch (TREE_CODE (x))
5740     { 
5741     /* Reconginze assignments of large structures and constructors of
5742        big arrays.  */
5743     case MODIFY_EXPR:
5744     case CONSTRUCTOR:
5745       {
5746         int size = int_size_in_bytes (TREE_TYPE (x));
5747
5748         if (!size || size > MOVE_MAX_PIECES)
5749           *count += 10;
5750         else
5751           *count += 2 * (size + MOVE_MAX - 1) / MOVE_MAX;
5752         return NULL;
5753       }
5754       break;
5755     /* Few special cases of expensive operations.  This is usefull
5756        to avoid inlining on functions having too many of these.  */
5757     case TRUNC_DIV_EXPR:
5758     case CEIL_DIV_EXPR:
5759     case FLOOR_DIV_EXPR:
5760     case ROUND_DIV_EXPR:
5761     case TRUNC_MOD_EXPR:
5762     case CEIL_MOD_EXPR:
5763     case FLOOR_MOD_EXPR:
5764     case ROUND_MOD_EXPR:
5765     case RDIV_EXPR:
5766     case CALL_EXPR:
5767       *count += 10;
5768       break;
5769     /* Various containers that will produce no code themselves.  */
5770     case INIT_EXPR:
5771     case TARGET_EXPR:
5772     case BIND_EXPR:
5773     case BLOCK:
5774     case TREE_LIST:
5775     case TREE_VEC:
5776     case IDENTIFIER_NODE:
5777     case PLACEHOLDER_EXPR:
5778     case WITH_CLEANUP_EXPR:
5779     case CLEANUP_POINT_EXPR:
5780     case NOP_EXPR:
5781     case VIEW_CONVERT_EXPR:
5782     case SAVE_EXPR:
5783     case UNSAVE_EXPR:
5784     case COMPLEX_EXPR:
5785     case REALPART_EXPR:
5786     case IMAGPART_EXPR:
5787     case TRY_CATCH_EXPR:
5788     case TRY_FINALLY_EXPR:
5789     case LABEL_EXPR:
5790     case EXIT_EXPR:
5791     case LABELED_BLOCK_EXPR:
5792     case EXIT_BLOCK_EXPR:
5793     case EXPR_WITH_FILE_LOCATION:
5794
5795     case EXPR_STMT:
5796     case COMPOUND_STMT:
5797     case RETURN_STMT:
5798     case LABEL_STMT:
5799     case SCOPE_STMT:
5800     case FILE_STMT:
5801     case CASE_LABEL:
5802     case STMT_EXPR:
5803     case CLEANUP_STMT:
5804
5805     case SIZEOF_EXPR:
5806     case ARROW_EXPR:
5807     case ALIGNOF_EXPR:
5808       break;
5809     case DECL_STMT:
5810       /* Do not account static initializers.  */
5811       if (TREE_STATIC (TREE_OPERAND (x, 0)))
5812         *walk_subtrees = 0;
5813       break;
5814     default:
5815       (*count)++;
5816     }
5817   return NULL;
5818 }
5819
5820 /*  Estimate number of instructions that will be created by expanding the body.  */
5821 int
5822 c_estimate_num_insns (tree decl)
5823 {
5824   int num = 0;
5825   walk_tree_without_duplicates (&DECL_SAVED_TREE (decl), c_estimate_num_insns_1, &num);
5826   return num;
5827 }
5828
5829 /* Used by c_decl_uninit to find where expressions like x = x + 1; */
5830
5831 static tree
5832 c_decl_uninit_1 (tree *t, int *walk_sub_trees, void *x)
5833 {
5834   /* If x = EXP(&x)EXP, then do not warn about the use of x.  */
5835   if (TREE_CODE (*t) == ADDR_EXPR && TREE_OPERAND (*t, 0) == x)
5836     {
5837       *walk_sub_trees = 0;
5838       return NULL_TREE;
5839     }
5840   if (*t == x)
5841     return *t;
5842   return NULL_TREE;
5843 }
5844
5845 /* Find out if a variable is uninitialized based on DECL_INITIAL.  */
5846
5847 bool
5848 c_decl_uninit (tree t)
5849 {
5850   /* int x = x; is GCC extension to turn off this warning, only if warn_init_self is zero.  */
5851   if (DECL_INITIAL (t) == t)
5852     return warn_init_self ? true : false;
5853
5854   /* Walk the trees looking for the variable itself.  */
5855   if (walk_tree_without_duplicates (&DECL_INITIAL (t), c_decl_uninit_1, t))
5856     return true;
5857   return false;
5858 }
5859
5860 #include "gt-c-common.h"