OSDN Git Service

* stmt.c (cost_table): Remove.
[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, 2001
3    Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "toplev.h"
27 #include "output.h"
28 #include "c-pragma.h"
29 #include "rtl.h"
30 #include "ggc.h"
31 #include "expr.h"
32 #include "c-common.h"
33 #include "defaults.h"
34 #include "tm_p.h"
35 #include "obstack.h"
36 #include "cpplib.h"
37 cpp_reader *parse_in;           /* Declared in c-lex.h.  */
38
39 #undef WCHAR_TYPE_SIZE
40 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
41
42 /* We let tm.h override the types used here, to handle trivial differences
43    such as the choice of unsigned int or long unsigned int for size_t.
44    When machines start needing nontrivial differences in the size type,
45    it would be best to do something here to figure out automatically
46    from other information what type to use.  */
47
48 #ifndef SIZE_TYPE
49 #define SIZE_TYPE "long unsigned int"
50 #endif
51
52 #ifndef WCHAR_TYPE
53 #define WCHAR_TYPE "int"
54 #endif
55
56 #ifndef PTRDIFF_TYPE
57 #define PTRDIFF_TYPE "long int"
58 #endif
59
60 #ifndef WINT_TYPE
61 #define WINT_TYPE "unsigned int"
62 #endif
63
64 #ifndef INTMAX_TYPE
65 #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE)     \
66                      ? "int"                                    \
67                      : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
68                         ? "long int"                            \
69                         : "long long int"))
70 #endif
71
72 #ifndef UINTMAX_TYPE
73 #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE)    \
74                      ? "unsigned int"                           \
75                      : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
76                         ? "long unsigned int"                   \
77                         : "long long unsigned int"))
78 #endif
79
80 /* The following symbols are subsumed in the c_global_trees array, and
81    listed here individually for documentation purposes.
82
83    INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
84
85         tree short_integer_type_node;
86         tree long_integer_type_node;
87         tree long_long_integer_type_node;
88
89         tree short_unsigned_type_node;
90         tree long_unsigned_type_node;
91         tree long_long_unsigned_type_node;
92
93         tree boolean_type_node;
94         tree boolean_false_node;
95         tree boolean_true_node;
96
97         tree ptrdiff_type_node;
98
99         tree unsigned_char_type_node;
100         tree signed_char_type_node;
101         tree wchar_type_node;
102         tree signed_wchar_type_node;
103         tree unsigned_wchar_type_node;
104
105         tree float_type_node;
106         tree double_type_node;
107         tree long_double_type_node;
108
109         tree complex_integer_type_node;
110         tree complex_float_type_node;
111         tree complex_double_type_node;
112         tree complex_long_double_type_node;
113
114         tree intQI_type_node;
115         tree intHI_type_node;
116         tree intSI_type_node;
117         tree intDI_type_node;
118         tree intTI_type_node;
119
120         tree unsigned_intQI_type_node;
121         tree unsigned_intHI_type_node;
122         tree unsigned_intSI_type_node;
123         tree unsigned_intDI_type_node;
124         tree unsigned_intTI_type_node;
125
126         tree widest_integer_literal_type_node;
127         tree widest_unsigned_literal_type_node;
128
129    Nodes for types `void *' and `const void *'.
130
131         tree ptr_type_node, const_ptr_type_node;
132
133    Nodes for types `char *' and `const char *'.
134
135         tree string_type_node, const_string_type_node;
136
137    Type `char[SOMENUMBER]'.
138    Used when an array of char is needed and the size is irrelevant.
139
140         tree char_array_type_node;
141
142    Type `int[SOMENUMBER]' or something like it.
143    Used when an array of int needed and the size is irrelevant.
144
145         tree int_array_type_node;
146
147    Type `wchar_t[SOMENUMBER]' or something like it.
148    Used when a wide string literal is created.
149
150         tree wchar_array_type_node;
151
152    Type `int ()' -- used for implicit declaration of functions.
153
154         tree default_function_type;
155
156    Function types `int (int)', etc.
157
158         tree int_ftype_int;
159         tree void_ftype;
160         tree void_ftype_ptr;
161         tree int_ftype_int;
162         tree ptr_ftype_sizetype;
163
164    A VOID_TYPE node, packaged in a TREE_LIST.
165
166         tree void_list_node;
167
168   The identifiers __FUNCTION__, __PRETTY_FUNCTION__, and __func__.
169
170         tree function_id_node;
171         tree pretty_function_id_node;
172         tree func_id_node;
173
174 */
175
176 tree c_global_trees[CTI_MAX];
177
178 /* Nonzero means don't recognize the non-ANSI builtin functions.  */
179
180 int flag_no_builtin;
181
182 /* Nonzero means don't recognize the non-ANSI builtin functions.
183    -ansi sets this.  */
184
185 int flag_no_nonansi_builtin;
186
187 /* Nonzero means give `double' the same size as `float'.  */
188
189 int flag_short_double;
190
191 /* Nonzero means give `wchar_t' the same size as `short'.  */
192
193 int flag_short_wchar;
194
195 /* If non-NULL, dump the tree structure for the entire translation
196    unit to this file.  */
197
198 const char *flag_dump_translation_unit;
199
200 /* Nonzero means warn about possible violations of sequence point rules.  */
201
202 int warn_sequence_point;
203
204 /* The elements of `ridpointers' are identifier nodes for the reserved
205    type names and storage classes.  It is indexed by a RID_... value.  */
206 tree *ridpointers;
207
208 tree (*make_fname_decl)                PARAMS ((tree, const char *, int));
209
210 /* If non-NULL, the address of a language-specific function that
211    returns 1 for language-specific statement codes.  */
212 int (*lang_statement_code_p)           PARAMS ((enum tree_code));
213
214 /* If non-NULL, the address of a language-specific function that takes
215    any action required right before expand_function_end is called.  */
216 void (*lang_expand_function_end)       PARAMS ((void));
217
218 /* If this variable is defined to a non-NULL value, it will be called
219    after the file has been completely parsed.  */
220 void (*back_end_hook) PARAMS ((tree));
221
222 /* Nonzero means the expression being parsed will never be evaluated.
223    This is a count, since unevaluated expressions can nest.  */
224 int skip_evaluation;
225
226 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
227             A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
228             A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
229             A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
230             A_NO_LIMIT_STACK, A_PURE};
231
232 static void add_attribute               PARAMS ((enum attrs, const char *,
233                                                  int, int, int));
234 static void init_attributes             PARAMS ((void));
235 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
236
237 /* Keep a stack of if statements.  We record the number of compound
238    statements seen up to the if keyword, as well as the line number
239    and file of the if.  If a potentially ambiguous else is seen, that
240    fact is recorded; the warning is issued when we can be sure that
241    the enclosing if statement does not have an else branch.  */
242 typedef struct
243 {
244   int compstmt_count;
245   int line;
246   const char *file;
247   int needs_warning;
248   tree if_stmt;
249 } if_elt;
250
251 static if_elt *if_stack;
252
253 /* Amount of space in the if statement stack.  */
254 static int if_stack_space = 0;
255
256 /* Stack pointer.  */
257 static int if_stack_pointer = 0;
258
259 /* Record the start of an if-then, and record the start of it
260    for ambiguous else detection.  */
261
262 void
263 c_expand_start_cond (cond, compstmt_count)
264      tree cond;
265      int compstmt_count;
266 {
267   tree if_stmt;
268
269   /* Make sure there is enough space on the stack.  */
270   if (if_stack_space == 0)
271     {
272       if_stack_space = 10;
273       if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
274     }
275   else if (if_stack_space == if_stack_pointer)
276     {
277       if_stack_space += 10;
278       if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
279     }
280
281   if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
282   IF_COND (if_stmt) = cond;
283   add_stmt (if_stmt);
284
285   /* Record this if statement.  */
286   if_stack[if_stack_pointer].compstmt_count = compstmt_count;
287   if_stack[if_stack_pointer].file = input_filename;
288   if_stack[if_stack_pointer].line = lineno;
289   if_stack[if_stack_pointer].needs_warning = 0;
290   if_stack[if_stack_pointer].if_stmt = if_stmt;
291   if_stack_pointer++;
292 }
293
294 /* Called after the then-clause for an if-statement is processed.  */
295
296 void
297 c_finish_then ()
298 {
299   tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
300   RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
301 }
302
303 /* Record the end of an if-then.  Optionally warn if a nested
304    if statement had an ambiguous else clause.  */
305
306 void
307 c_expand_end_cond ()
308 {
309   if_stack_pointer--;
310   if (if_stack[if_stack_pointer].needs_warning)
311     warning_with_file_and_line (if_stack[if_stack_pointer].file,
312                                 if_stack[if_stack_pointer].line,
313                                 "suggest explicit braces to avoid ambiguous `else'");
314   last_expr_type = NULL_TREE;
315 }
316
317 /* Called between the then-clause and the else-clause
318    of an if-then-else.  */
319
320 void
321 c_expand_start_else ()
322 {
323   /* An ambiguous else warning must be generated for the enclosing if
324      statement, unless we see an else branch for that one, too.  */
325   if (warn_parentheses
326       && if_stack_pointer > 1
327       && (if_stack[if_stack_pointer - 1].compstmt_count
328           == if_stack[if_stack_pointer - 2].compstmt_count))
329     if_stack[if_stack_pointer - 2].needs_warning = 1;
330
331   /* Even if a nested if statement had an else branch, it can't be
332      ambiguous if this one also has an else.  So don't warn in that
333      case.  Also don't warn for any if statements nested in this else.  */
334   if_stack[if_stack_pointer - 1].needs_warning = 0;
335   if_stack[if_stack_pointer - 1].compstmt_count--;
336 }
337
338 /* Called after the else-clause for an if-statement is processed.  */
339
340 void
341 c_finish_else ()
342 {
343   tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
344   RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
345 }
346
347 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__.  */
348
349 void
350 declare_function_name ()
351 {
352   const char *name, *printable_name;
353
354   if (current_function_decl == NULL)
355     {
356       name = "";
357       printable_name = "top level";
358     }
359   else
360     {
361       /* Allow functions to be nameless (such as artificial ones).  */
362       if (DECL_NAME (current_function_decl))
363         name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
364       else
365         name = "";
366       printable_name = (*decl_printable_name) (current_function_decl, 2);
367
368       /* ISO C99 defines __func__, which is a variable, not a string
369          constant, and which is not a defined symbol at file scope.  */
370       (*make_fname_decl) (func_id_node, name, 0);
371     }
372   
373   (*make_fname_decl) (function_id_node, name, 0);
374   (*make_fname_decl) (pretty_function_id_node, printable_name, 1);
375 }
376
377 /* Given a chain of STRING_CST nodes,
378    concatenate them into one STRING_CST
379    and give it a suitable array-of-chars data type.  */
380
381 tree
382 combine_strings (strings)
383      tree strings;
384 {
385   register tree value, t;
386   register int length = 1;
387   int wide_length = 0;
388   int wide_flag = 0;
389   int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
390   int nchars;
391   const int nchars_max = flag_isoc99 ? 4095 : 509;
392
393   if (TREE_CHAIN (strings))
394     {
395       /* More than one in the chain, so concatenate.  */
396       register char *p, *q;
397
398       /* Don't include the \0 at the end of each substring,
399          except for the last one.
400          Count wide strings and ordinary strings separately.  */
401       for (t = strings; t; t = TREE_CHAIN (t))
402         {
403           if (TREE_TYPE (t) == wchar_array_type_node)
404             {
405               wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
406               wide_flag = 1;
407             }
408           else
409             length += (TREE_STRING_LENGTH (t) - 1);
410         }
411
412       /* If anything is wide, the non-wides will be converted,
413          which makes them take more space.  */
414       if (wide_flag)
415         length = length * wchar_bytes + wide_length;
416
417       p = alloca (length);
418
419       /* Copy the individual strings into the new combined string.
420          If the combined string is wide, convert the chars to ints
421          for any individual strings that are not wide.  */
422
423       q = p;
424       for (t = strings; t; t = TREE_CHAIN (t))
425         {
426           int len = (TREE_STRING_LENGTH (t)
427                      - ((TREE_TYPE (t) == wchar_array_type_node)
428                         ? wchar_bytes : 1));
429           if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
430             {
431               memcpy (q, TREE_STRING_POINTER (t), len);
432               q += len;
433             }
434           else
435             {
436               int i;
437               for (i = 0; i < len; i++)
438                 {
439                   if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
440                     ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
441                   else
442                     ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
443                 }
444               q += len * wchar_bytes;
445             }
446         }
447       if (wide_flag)
448         {
449           int i;
450           for (i = 0; i < wchar_bytes; i++)
451             *q++ = 0;
452         }
453       else
454         *q = 0;
455
456       value = build_string (length, p);
457     }
458   else
459     {
460       value = strings;
461       length = TREE_STRING_LENGTH (value);
462       if (TREE_TYPE (value) == wchar_array_type_node)
463         wide_flag = 1;
464     }
465
466   /* Compute the number of elements, for the array type.  */
467   nchars = wide_flag ? length / wchar_bytes : length;
468
469   if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
470     pedwarn ("string length `%d' is greater than the minimum length `%d' ISO C%d is required to support",
471              nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
472
473   /* Create the array type for the string constant.
474      -Wwrite-strings says make the string constant an array of const char
475      so that copying it to a non-const pointer will get a warning.
476      For C++, this is the standard behavior.  */
477   if (flag_const_strings
478       && (! flag_traditional  && ! flag_writable_strings))
479     {
480       tree elements
481         = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
482                               1, 0);
483       TREE_TYPE (value)
484         = build_array_type (elements,
485                             build_index_type (build_int_2 (nchars - 1, 0)));
486     }
487   else
488     TREE_TYPE (value)
489       = build_array_type (wide_flag ? wchar_type_node : char_type_node,
490                           build_index_type (build_int_2 (nchars - 1, 0)));
491
492   TREE_CONSTANT (value) = 1;
493   TREE_READONLY (value) = ! flag_writable_strings;
494   TREE_STATIC (value) = 1;
495   return value;
496 }
497 \f
498 /* To speed up processing of attributes, we maintain an array of
499    IDENTIFIER_NODES and the corresponding attribute types.  */
500
501 /* Array to hold attribute information.  */
502
503 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
504
505 static int attrtab_idx = 0;
506
507 /* Add an entry to the attribute table above.  */
508
509 static void
510 add_attribute (id, string, min_len, max_len, decl_req)
511      enum attrs id;
512      const char *string;
513      int min_len, max_len;
514      int decl_req;
515 {
516   char buf[100];
517
518   attrtab[attrtab_idx].id = id;
519   attrtab[attrtab_idx].name = get_identifier (string);
520   attrtab[attrtab_idx].min = min_len;
521   attrtab[attrtab_idx].max = max_len;
522   attrtab[attrtab_idx++].decl_req = decl_req;
523
524   sprintf (buf, "__%s__", string);
525
526   attrtab[attrtab_idx].id = id;
527   attrtab[attrtab_idx].name = get_identifier (buf);
528   attrtab[attrtab_idx].min = min_len;
529   attrtab[attrtab_idx].max = max_len;
530   attrtab[attrtab_idx++].decl_req = decl_req;
531 }
532
533 /* Initialize attribute table.  */
534
535 static void
536 init_attributes ()
537 {
538   add_attribute (A_PACKED, "packed", 0, 0, 0);
539   add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
540   add_attribute (A_COMMON, "common", 0, 0, 1);
541   add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
542   add_attribute (A_NORETURN, "volatile", 0, 0, 1);
543   add_attribute (A_UNUSED, "unused", 0, 0, 0);
544   add_attribute (A_CONST, "const", 0, 0, 1);
545   add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
546   add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
547   add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
548   add_attribute (A_MODE, "mode", 1, 1, 1);
549   add_attribute (A_SECTION, "section", 1, 1, 1);
550   add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
551   add_attribute (A_FORMAT, "format", 3, 3, 1);
552   add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
553   add_attribute (A_WEAK, "weak", 0, 0, 1);
554   add_attribute (A_ALIAS, "alias", 1, 1, 1);
555   add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
556   add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
557   add_attribute (A_MALLOC, "malloc", 0, 0, 1);
558   add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
559   add_attribute (A_PURE, "pure", 0, 0, 1);
560 }
561 \f
562 /* Default implementation of valid_lang_attribute, below.  By default, there
563    are no language-specific attributes.  */
564
565 static int
566 default_valid_lang_attribute (attr_name, attr_args, decl, type)
567   tree attr_name ATTRIBUTE_UNUSED;
568   tree attr_args ATTRIBUTE_UNUSED;
569   tree decl ATTRIBUTE_UNUSED;
570   tree type ATTRIBUTE_UNUSED;
571 {
572   return 0;
573 }
574
575 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
576    attribute for either declaration DECL or type TYPE and 0 otherwise.  */
577
578 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
579      = default_valid_lang_attribute;
580
581 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
582    and install them in NODE, which is either a DECL (including a TYPE_DECL)
583    or a TYPE.  PREFIX_ATTRIBUTES can appear after the declaration specifiers
584    and declaration modifiers but before the declaration proper.  */
585
586 void
587 decl_attributes (node, attributes, prefix_attributes)
588      tree node, attributes, prefix_attributes;
589 {
590   tree decl = 0, type = 0;
591   int is_type = 0;
592   tree a;
593
594   if (attrtab_idx == 0)
595     init_attributes ();
596
597   if (DECL_P (node))
598     {
599       decl = node;
600       type = TREE_TYPE (decl);
601       is_type = TREE_CODE (node) == TYPE_DECL;
602     }
603   else if (TYPE_P (node))
604     type = node, is_type = 1;
605
606 #ifdef PRAGMA_INSERT_ATTRIBUTES
607   /* If the code in c-pragma.c wants to insert some attributes then
608      allow it to do so.  Do this before allowing machine back ends to
609      insert attributes, so that they have the opportunity to override
610      anything done here.  */
611   PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
612 #endif
613
614 #ifdef INSERT_ATTRIBUTES
615   INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
616 #endif
617
618   attributes = chainon (prefix_attributes, attributes);
619
620   for (a = attributes; a; a = TREE_CHAIN (a))
621     {
622       tree name = TREE_PURPOSE (a);
623       tree args = TREE_VALUE (a);
624       int i;
625       enum attrs id;
626
627       for (i = 0; i < attrtab_idx; i++)
628         if (attrtab[i].name == name)
629           break;
630
631       if (i == attrtab_idx)
632         {
633           if (! valid_machine_attribute (name, args, decl, type)
634               && ! (* valid_lang_attribute) (name, args, decl, type))
635             warning ("`%s' attribute directive ignored",
636                      IDENTIFIER_POINTER (name));
637           else if (decl != 0)
638             type = TREE_TYPE (decl);
639           continue;
640         }
641       else if (attrtab[i].decl_req && decl == 0)
642         {
643           warning ("`%s' attribute does not apply to types",
644                    IDENTIFIER_POINTER (name));
645           continue;
646         }
647       else if (list_length (args) < attrtab[i].min
648                || list_length (args) > attrtab[i].max)
649         {
650           error ("wrong number of arguments specified for `%s' attribute",
651                  IDENTIFIER_POINTER (name));
652           continue;
653         }
654
655       id = attrtab[i].id;
656       switch (id)
657         {
658         case A_PACKED:
659           if (is_type)
660             TYPE_PACKED (type) = 1;
661           else if (TREE_CODE (decl) == FIELD_DECL)
662             DECL_PACKED (decl) = 1;
663           /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
664              used for DECL_REGISTER.  It wouldn't mean anything anyway.  */
665           else
666             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
667           break;
668
669         case A_NOCOMMON:
670           if (TREE_CODE (decl) == VAR_DECL)
671             DECL_COMMON (decl) = 0;
672           else
673             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
674           break;
675
676         case A_COMMON:
677           if (TREE_CODE (decl) == VAR_DECL)
678             DECL_COMMON (decl) = 1;
679           else
680             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
681           break;
682
683         case A_NORETURN:
684           if (TREE_CODE (decl) == FUNCTION_DECL)
685             TREE_THIS_VOLATILE (decl) = 1;
686           else if (TREE_CODE (type) == POINTER_TYPE
687                    && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
688             TREE_TYPE (decl) = type
689               = build_pointer_type
690                 (build_type_variant (TREE_TYPE (type),
691                                      TREE_READONLY (TREE_TYPE (type)), 1));
692           else
693             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
694           break;
695
696         case A_MALLOC:
697           if (TREE_CODE (decl) == FUNCTION_DECL)
698             DECL_IS_MALLOC (decl) = 1;
699           /* ??? TODO: Support types.  */
700           else
701             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
702           break;
703
704         case A_UNUSED:
705           if (is_type)
706             if (decl)
707               TREE_USED (decl) = 1;
708             else
709               TREE_USED (type) = 1;
710           else if (TREE_CODE (decl) == PARM_DECL
711                    || TREE_CODE (decl) == VAR_DECL
712                    || TREE_CODE (decl) == FUNCTION_DECL
713                    || TREE_CODE (decl) == LABEL_DECL)
714             TREE_USED (decl) = 1;
715           else
716             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
717           break;
718
719         case A_CONST:
720           if (TREE_CODE (decl) == FUNCTION_DECL)
721             TREE_READONLY (decl) = 1;
722           else if (TREE_CODE (type) == POINTER_TYPE
723                    && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
724             TREE_TYPE (decl) = type
725               = build_pointer_type
726                 (build_type_variant (TREE_TYPE (type), 1,
727                                      TREE_THIS_VOLATILE (TREE_TYPE (type))));
728           else
729             warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
730           break;
731
732         case A_PURE:
733           if (TREE_CODE (decl) == FUNCTION_DECL)
734             DECL_IS_PURE (decl) = 1;
735           /* ??? TODO: Support types.  */
736           else
737             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
738           break;
739
740
741         case A_T_UNION:
742           if (is_type
743               && TREE_CODE (type) == UNION_TYPE
744               && (decl == 0
745                   || (TYPE_FIELDS (type) != 0
746                       && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
747             TYPE_TRANSPARENT_UNION (type) = 1;
748           else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
749                    && TREE_CODE (type) == UNION_TYPE
750                    && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
751             DECL_TRANSPARENT_UNION (decl) = 1;
752           else
753             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
754           break;
755
756         case A_CONSTRUCTOR:
757           if (TREE_CODE (decl) == FUNCTION_DECL
758               && TREE_CODE (type) == FUNCTION_TYPE
759               && decl_function_context (decl) == 0)
760             {
761               DECL_STATIC_CONSTRUCTOR (decl) = 1;
762               TREE_USED (decl) = 1;
763             }
764           else
765             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
766           break;
767
768         case A_DESTRUCTOR:
769           if (TREE_CODE (decl) == FUNCTION_DECL
770               && TREE_CODE (type) == FUNCTION_TYPE
771               && decl_function_context (decl) == 0)
772             {
773               DECL_STATIC_DESTRUCTOR (decl) = 1;
774               TREE_USED (decl) = 1;
775             }
776           else
777             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
778           break;
779
780         case A_MODE:
781           if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
782             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
783           else
784             {
785               int j;
786               const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
787               int len = strlen (p);
788               enum machine_mode mode = VOIDmode;
789               tree typefm;
790
791               if (len > 4 && p[0] == '_' && p[1] == '_'
792                   && p[len - 1] == '_' && p[len - 2] == '_')
793                 {
794                   char *newp = (char *) alloca (len - 1);
795
796                   strcpy (newp, &p[2]);
797                   newp[len - 4] = '\0';
798                   p = newp;
799                 }
800
801               /* Give this decl a type with the specified mode.
802                  First check for the special modes.  */
803               if (! strcmp (p, "byte"))
804                 mode = byte_mode;
805               else if (!strcmp (p, "word"))
806                 mode = word_mode;
807               else if (! strcmp (p, "pointer"))
808                 mode = ptr_mode;
809               else
810                 for (j = 0; j < NUM_MACHINE_MODES; j++)
811                   if (!strcmp (p, GET_MODE_NAME (j)))
812                     mode = (enum machine_mode) j;
813
814               if (mode == VOIDmode)
815                 error ("unknown machine mode `%s'", p);
816               else if (0 == (typefm = type_for_mode (mode,
817                                                      TREE_UNSIGNED (type))))
818                 error ("no data type for mode `%s'", p);
819               else
820                 {
821                   if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type)
822                                                  ? TYPE_PRECISION(uintmax_type_node)
823                                                  : TYPE_PRECISION(intmax_type_node))
824                       && pedantic)
825                     pedwarn ("type with more precision than %s",
826                              TREE_UNSIGNED (type) ? "uintmax_t" : "intmax_t");
827                   TREE_TYPE (decl) = type = typefm;
828                   DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
829                   layout_decl (decl, 0);
830                 }
831             }
832           break;
833
834         case A_SECTION:
835 #ifdef ASM_OUTPUT_SECTION_NAME
836           if ((TREE_CODE (decl) == FUNCTION_DECL
837                || TREE_CODE (decl) == VAR_DECL)
838               && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
839             {
840               if (TREE_CODE (decl) == VAR_DECL
841                   && current_function_decl != NULL_TREE
842                   && ! TREE_STATIC (decl))
843                 error_with_decl (decl,
844                   "section attribute cannot be specified for local variables");
845               /* The decl may have already been given a section attribute from
846                  a previous declaration.  Ensure they match.  */
847               else if (DECL_SECTION_NAME (decl) != NULL_TREE
848                        && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
849                                   TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
850                 error_with_decl (node,
851                                  "section of `%s' conflicts with previous declaration");
852               else
853                 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
854             }
855           else
856             error_with_decl (node,
857                            "section attribute not allowed for `%s'");
858 #else
859           error_with_decl (node,
860                   "section attributes are not supported for this target");
861 #endif
862           break;
863
864         case A_ALIGNED:
865           {
866             tree align_expr
867               = (args ? TREE_VALUE (args)
868                  : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
869             int i;
870
871             /* Strip any NOPs of any kind.  */
872             while (TREE_CODE (align_expr) == NOP_EXPR
873                    || TREE_CODE (align_expr) == CONVERT_EXPR
874                    || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
875               align_expr = TREE_OPERAND (align_expr, 0);
876
877             if (TREE_CODE (align_expr) != INTEGER_CST)
878               {
879                 error ("requested alignment is not a constant");
880                 continue;
881               }
882
883             if ((i = tree_log2 (align_expr)) == -1)
884               error ("requested alignment is not a power of 2");
885             else if (i > HOST_BITS_PER_INT - 2)
886               error ("requested alignment is too large");
887             else if (is_type)
888               {
889                 /* If we have a TYPE_DECL, then copy the type, so that we
890                    don't accidentally modify a builtin type.  See pushdecl.  */
891                 if (decl && TREE_TYPE (decl) != error_mark_node
892                     && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
893                   {
894                     tree tt = TREE_TYPE (decl);
895                     DECL_ORIGINAL_TYPE (decl) = tt;
896                     tt = build_type_copy (tt);
897                     TYPE_NAME (tt) = decl;
898                     TREE_USED (tt) = TREE_USED (decl);
899                     TREE_TYPE (decl) = tt;
900                     type = tt;
901                   }
902
903                 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
904                 TYPE_USER_ALIGN (type) = 1;
905               }
906             else if (TREE_CODE (decl) != VAR_DECL
907                      && TREE_CODE (decl) != FIELD_DECL)
908               error_with_decl (decl,
909                                "alignment may not be specified for `%s'");
910             else
911               {
912                 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
913                 DECL_USER_ALIGN (decl) = 1;
914               }
915           }
916           break;
917
918         case A_FORMAT:
919           decl_handle_format_attribute (decl, args);
920           break;
921
922         case A_FORMAT_ARG:
923           decl_handle_format_arg_attribute (decl, args);
924           break;
925
926         case A_WEAK:
927           declare_weak (decl);
928           break;
929
930         case A_ALIAS:
931           if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
932               || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
933             error_with_decl (decl,
934                              "`%s' defined both normally and as an alias");
935           else if (decl_function_context (decl) == 0)
936             {
937               tree id;
938
939               id = TREE_VALUE (args);
940               if (TREE_CODE (id) != STRING_CST)
941                 {
942                   error ("alias arg not a string");
943                   break;
944                 }
945               id = get_identifier (TREE_STRING_POINTER (id));
946               /* This counts as a use of the object pointed to.  */
947               TREE_USED (id) = 1;
948
949               if (TREE_CODE (decl) == FUNCTION_DECL)
950                 DECL_INITIAL (decl) = error_mark_node;
951               else
952                 DECL_EXTERNAL (decl) = 0;
953               assemble_alias (decl, id);
954             }
955           else
956             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
957           break;
958
959         case A_NO_CHECK_MEMORY_USAGE:
960           if (TREE_CODE (decl) != FUNCTION_DECL)
961             {
962               error_with_decl (decl,
963                                "`%s' attribute applies only to functions",
964                                IDENTIFIER_POINTER (name));
965             }
966           else if (DECL_INITIAL (decl))
967             {
968               error_with_decl (decl,
969                                "can't set `%s' attribute after definition",
970                                IDENTIFIER_POINTER (name));
971             }
972           else
973             DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
974           break;
975
976         case A_NO_INSTRUMENT_FUNCTION:
977           if (TREE_CODE (decl) != FUNCTION_DECL)
978             {
979               error_with_decl (decl,
980                                "`%s' attribute applies only to functions",
981                                IDENTIFIER_POINTER (name));
982             }
983           else if (DECL_INITIAL (decl))
984             {
985               error_with_decl (decl,
986                                "can't set `%s' attribute after definition",
987                                IDENTIFIER_POINTER (name));
988             }
989           else
990             DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
991           break;
992
993         case A_NO_LIMIT_STACK:
994           if (TREE_CODE (decl) != FUNCTION_DECL)
995             {
996               error_with_decl (decl,
997                                "`%s' attribute applies only to functions",
998                                IDENTIFIER_POINTER (name));
999             }
1000           else if (DECL_INITIAL (decl))
1001             {
1002               error_with_decl (decl,
1003                                "can't set `%s' attribute after definition",
1004                                IDENTIFIER_POINTER (name));
1005             }
1006           else
1007             DECL_NO_LIMIT_STACK (decl) = 1;
1008           break;
1009         }
1010     }
1011 }
1012
1013 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1014    lists.  SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1015
1016    The head of the declspec list is stored in DECLSPECS.
1017    The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1018
1019    Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1020    the list elements.  We drop the containing TREE_LIST nodes and link the
1021    resulting attributes together the way decl_attributes expects them.  */
1022
1023 void
1024 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1025      tree specs_attrs;
1026      tree *declspecs, *prefix_attributes;
1027 {
1028   tree t, s, a, next, specs, attrs;
1029
1030   /* This can happen after an __extension__ in pedantic mode.  */
1031   if (specs_attrs != NULL_TREE 
1032       && TREE_CODE (specs_attrs) == INTEGER_CST)
1033     {
1034       *declspecs = NULL_TREE;
1035       *prefix_attributes = NULL_TREE;
1036       return;
1037     }
1038
1039   /* This can happen in c++ (eg: decl: typespec initdecls ';').  */
1040   if (specs_attrs != NULL_TREE
1041       && TREE_CODE (specs_attrs) != TREE_LIST)
1042     {
1043       *declspecs = specs_attrs;
1044       *prefix_attributes = NULL_TREE;
1045       return;
1046     }
1047
1048   /* Remember to keep the lists in the same order, element-wise.  */
1049
1050   specs = s = NULL_TREE;
1051   attrs = a = NULL_TREE;
1052   for (t = specs_attrs; t; t = next)
1053     {
1054       next = TREE_CHAIN (t);
1055       /* Declspecs have a non-NULL TREE_VALUE.  */
1056       if (TREE_VALUE (t) != NULL_TREE)
1057         {
1058           if (specs == NULL_TREE)
1059             specs = s = t;
1060           else
1061             {
1062               TREE_CHAIN (s) = t;
1063               s = t;
1064             }
1065         }
1066       else
1067         {
1068           if (attrs == NULL_TREE)
1069             attrs = a = TREE_PURPOSE (t);
1070           else
1071             {
1072               TREE_CHAIN (a) = TREE_PURPOSE (t);
1073               a = TREE_PURPOSE (t);
1074             }
1075           /* More attrs can be linked here, move A to the end.  */
1076           while (TREE_CHAIN (a) != NULL_TREE)
1077             a = TREE_CHAIN (a);
1078         }
1079     }
1080
1081   /* Terminate the lists.  */
1082   if (s != NULL_TREE)
1083     TREE_CHAIN (s) = NULL_TREE;
1084   if (a != NULL_TREE)
1085     TREE_CHAIN (a) = NULL_TREE;
1086
1087   /* All done.  */
1088   *declspecs = specs;
1089   *prefix_attributes = attrs;
1090 }
1091
1092 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1093    This function is used by the parser when a rule will accept attributes
1094    in a particular position, but we don't want to support that just yet.
1095
1096    A warning is issued for every ignored attribute.  */
1097
1098 tree
1099 strip_attrs (specs_attrs)
1100      tree specs_attrs;
1101 {
1102   tree specs, attrs;
1103
1104   split_specs_attrs (specs_attrs, &specs, &attrs);
1105
1106   while (attrs)
1107     {
1108       warning ("`%s' attribute ignored",
1109                IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1110       attrs = TREE_CHAIN (attrs);
1111     }
1112
1113   return specs;
1114 }
1115 \f
1116 static int is_valid_printf_arglist PARAMS ((tree));
1117 static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
1118 static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1119                                             enum expand_modifier, int));
1120 static rtx c_expand_builtin_fprintf PARAMS ((tree, rtx, enum machine_mode,
1121                                              enum expand_modifier, int));
1122 \f
1123 /* Print a warning if a constant expression had overflow in folding.
1124    Invoke this function on every expression that the language
1125    requires to be a constant expression.
1126    Note the ANSI C standard says it is erroneous for a
1127    constant expression to overflow.  */
1128
1129 void
1130 constant_expression_warning (value)
1131      tree value;
1132 {
1133   if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1134        || TREE_CODE (value) == COMPLEX_CST)
1135       && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1136     pedwarn ("overflow in constant expression");
1137 }
1138
1139 /* Print a warning if an expression had overflow in folding.
1140    Invoke this function on every expression that
1141    (1) appears in the source code, and
1142    (2) might be a constant expression that overflowed, and
1143    (3) is not already checked by convert_and_check;
1144    however, do not invoke this function on operands of explicit casts.  */
1145
1146 void
1147 overflow_warning (value)
1148      tree value;
1149 {
1150   if ((TREE_CODE (value) == INTEGER_CST
1151        || (TREE_CODE (value) == COMPLEX_CST
1152            && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1153       && TREE_OVERFLOW (value))
1154     {
1155       TREE_OVERFLOW (value) = 0;
1156       if (skip_evaluation == 0)
1157         warning ("integer overflow in expression");
1158     }
1159   else if ((TREE_CODE (value) == REAL_CST
1160             || (TREE_CODE (value) == COMPLEX_CST
1161                 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1162            && TREE_OVERFLOW (value))
1163     {
1164       TREE_OVERFLOW (value) = 0;
1165       if (skip_evaluation == 0)
1166         warning ("floating point overflow in expression");
1167     }
1168 }
1169
1170 /* Print a warning if a large constant is truncated to unsigned,
1171    or if -Wconversion is used and a constant < 0 is converted to unsigned.
1172    Invoke this function on every expression that might be implicitly
1173    converted to an unsigned type.  */
1174
1175 void
1176 unsigned_conversion_warning (result, operand)
1177      tree result, operand;
1178 {
1179   if (TREE_CODE (operand) == INTEGER_CST
1180       && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1181       && TREE_UNSIGNED (TREE_TYPE (result))
1182       && skip_evaluation == 0
1183       && !int_fits_type_p (operand, TREE_TYPE (result)))
1184     {
1185       if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1186         /* This detects cases like converting -129 or 256 to unsigned char.  */
1187         warning ("large integer implicitly truncated to unsigned type");
1188       else if (warn_conversion)
1189         warning ("negative integer implicitly converted to unsigned type");
1190     }
1191 }
1192
1193 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1194    Invoke this function on every expression that is converted implicitly,
1195    i.e. because of language rules and not because of an explicit cast.  */
1196
1197 tree
1198 convert_and_check (type, expr)
1199      tree type, expr;
1200 {
1201   tree t = convert (type, expr);
1202   if (TREE_CODE (t) == INTEGER_CST)
1203     {
1204       if (TREE_OVERFLOW (t))
1205         {
1206           TREE_OVERFLOW (t) = 0;
1207
1208           /* Do not diagnose overflow in a constant expression merely
1209              because a conversion overflowed.  */
1210           TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1211
1212           /* No warning for converting 0x80000000 to int.  */
1213           if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1214                 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1215                 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1216             /* If EXPR fits in the unsigned version of TYPE,
1217                don't warn unless pedantic.  */
1218             if ((pedantic
1219                  || TREE_UNSIGNED (type)
1220                  || ! int_fits_type_p (expr, unsigned_type (type)))
1221                 && skip_evaluation == 0)
1222               warning ("overflow in implicit constant conversion");
1223         }
1224       else
1225         unsigned_conversion_warning (t, expr);
1226     }
1227   return t;
1228 }
1229 \f
1230 /* A node in a list that describes references to variables (EXPR), which are
1231    either read accesses if WRITER is zero, or write accesses, in which case
1232    WRITER is the parent of EXPR.  */
1233 struct tlist
1234 {
1235   struct tlist *next;
1236   tree expr, writer;
1237 };
1238
1239 /* Used to implement a cache the results of a call to verify_tree.  We only
1240    use this for SAVE_EXPRs.  */
1241 struct tlist_cache
1242 {
1243   struct tlist_cache *next;
1244   struct tlist *cache_before_sp;
1245   struct tlist *cache_after_sp;
1246   tree expr;
1247 };
1248
1249 /* Obstack to use when allocating tlist structures, and corresponding
1250    firstobj.  */
1251 static struct obstack tlist_obstack;
1252 static char *tlist_firstobj = 0;
1253
1254 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1255    warnings.  */
1256 static struct tlist *warned_ids;
1257 /* SAVE_EXPRs need special treatment.  We process them only once and then
1258    cache the results.  */
1259 static struct tlist_cache *save_expr_cache;
1260
1261 static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
1262 static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
1263 static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
1264 static int warning_candidate_p PARAMS ((tree));
1265 static void warn_for_collisions PARAMS ((struct tlist *));
1266 static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
1267 static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
1268 static void verify_sequence_points PARAMS ((tree));
1269
1270 /* Create a new struct tlist and fill in its fields.  */
1271 static struct tlist *
1272 new_tlist (next, t, writer)
1273      struct tlist *next;
1274      tree t;
1275      tree writer;
1276 {
1277   struct tlist *l;
1278   l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
1279   l->next = next;
1280   l->expr = t;
1281   l->writer = writer;
1282   return l;
1283 }
1284
1285 /* Add duplicates of the nodes found in ADD to the list *TO.  If EXCLUDE_WRITER
1286    is nonnull, we ignore any node we find which has a writer equal to it.  */
1287
1288 static void
1289 add_tlist (to, add, exclude_writer, copy)
1290      struct tlist **to;
1291      struct tlist *add;
1292      tree exclude_writer;
1293      int copy;
1294 {
1295   while (add)
1296     {
1297       struct tlist *next = add->next;
1298       if (! copy)
1299         add->next = *to;
1300       if (! exclude_writer || add->writer != exclude_writer)
1301         *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1302       add = next;
1303     }
1304 }
1305
1306 /* Merge the nodes of ADD into TO.  This merging process is done so that for
1307    each variable that already exists in TO, no new node is added; however if
1308    there is a write access recorded in ADD, and an occurrence on TO is only
1309    a read access, then the occurrence in TO will be modified to record the
1310    write.  */
1311
1312 static void
1313 merge_tlist (to, add, copy)
1314      struct tlist **to;
1315      struct tlist *add;
1316      int copy;
1317 {
1318   struct tlist **end = to;
1319
1320   while (*end)
1321     end = &(*end)->next;
1322
1323   while (add)
1324     {
1325       int found = 0;
1326       struct tlist *tmp2;
1327       struct tlist *next = add->next;
1328
1329       for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1330         if (tmp2->expr == add->expr)
1331           {
1332             found = 1;
1333             if (! tmp2->writer)
1334               tmp2->writer = add->writer;
1335           }
1336       if (! found)
1337         {
1338           *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
1339           end = &(*end)->next;
1340           *end = 0;
1341         }
1342       add = next;
1343     }
1344 }
1345
1346 /* WRITTEN is a variable, WRITER is its parent.  Warn if any of the variable
1347    references in list LIST conflict with it, excluding reads if ONLY writers
1348    is nonzero.  */
1349
1350 static void
1351 warn_for_collisions_1 (written, writer, list, only_writes)
1352      tree written, writer;
1353      struct tlist *list;
1354      int only_writes;
1355 {
1356   struct tlist *tmp;
1357
1358   /* Avoid duplicate warnings.  */
1359   for (tmp = warned_ids; tmp; tmp = tmp->next)
1360     if (tmp->expr == written)
1361       return;
1362
1363   while (list)
1364     {
1365       if (list->expr == written
1366           && list->writer != writer
1367           && (! only_writes || list->writer))
1368         {
1369           warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1370           warning ("operation on `%s' may be undefined",
1371                    IDENTIFIER_POINTER (DECL_NAME (list->expr)));
1372         }
1373       list = list->next;
1374     }
1375 }
1376
1377 /* Given a list LIST of references to variables, find whether any of these
1378    can cause conflicts due to missing sequence points.  */
1379
1380 static void
1381 warn_for_collisions (list)
1382      struct tlist *list;
1383 {
1384   struct tlist *tmp;
1385   
1386   for (tmp = list; tmp; tmp = tmp->next)
1387     {
1388       if (tmp->writer)
1389         warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1390     }
1391 }
1392
1393 /* Return nonzero if X is a tree that can be verified by the sequence poitn
1394    warnings.  */
1395 static int
1396 warning_candidate_p (x)
1397      tree x;
1398 {
1399   return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
1400 }
1401
1402 /* Walk the tree X, and record accesses to variables.  If X is written by the
1403    parent tree, WRITER is the parent.
1404    We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP.  If this
1405    expression or its only operand forces a sequence point, then everything up
1406    to the sequence point is stored in PBEFORE_SP.  Everything else gets stored
1407    in PNO_SP.
1408    Once we return, we will have emitted warnings if any subexpression before
1409    such a sequence point could be undefined.  On a higher level, however, the
1410    sequence point may not be relevant, and we'll merge the two lists.
1411
1412    Example: (b++, a) + b;
1413    The call that processes the COMPOUND_EXPR will store the increment of B
1414    in PBEFORE_SP, and the use of A in PNO_SP.  The higher-level call that
1415    processes the PLUS_EXPR will need to merge the two lists so that
1416    eventually, all accesses end up on the same list (and we'll warn about the
1417    unordered subexpressions b++ and b.
1418
1419    A note on merging.  If we modify the former example so that our expression
1420    becomes
1421      (b++, b) + a
1422    care must be taken not simply to add all three expressions into the final
1423    PNO_SP list.  The function merge_tlist takes care of that by merging the
1424    before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1425    way, so that no more than one access to B is recorded.  */
1426
1427 static void
1428 verify_tree (x, pbefore_sp, pno_sp, writer)
1429      tree x;
1430      struct tlist **pbefore_sp, **pno_sp;
1431      tree writer;
1432 {
1433   struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1434   enum tree_code code;
1435   char class;
1436
1437  restart:
1438   code = TREE_CODE (x);
1439   class = TREE_CODE_CLASS (code);
1440
1441   if (warning_candidate_p (x))
1442     {
1443       *pno_sp = new_tlist (*pno_sp, x, writer);
1444       return;
1445     }
1446
1447   switch (code)
1448     {
1449     case CONSTRUCTOR:
1450       return;
1451
1452     case COMPOUND_EXPR:
1453     case TRUTH_ANDIF_EXPR:
1454     case TRUTH_ORIF_EXPR:
1455       tmp_before = tmp_nosp = tmp_list3 = 0;
1456       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1457       warn_for_collisions (tmp_nosp);
1458       merge_tlist (pbefore_sp, tmp_before, 0);
1459       merge_tlist (pbefore_sp, tmp_nosp, 0);
1460       verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
1461       merge_tlist (pbefore_sp, tmp_list3, 0);
1462       return;
1463
1464     case COND_EXPR:
1465       tmp_before = tmp_list2 = 0;
1466       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1467       warn_for_collisions (tmp_list2);
1468       merge_tlist (pbefore_sp, tmp_before, 0);
1469       merge_tlist (pbefore_sp, tmp_list2, 1);
1470
1471       tmp_list3 = tmp_nosp = 0;
1472       verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1473       warn_for_collisions (tmp_nosp);
1474       merge_tlist (pbefore_sp, tmp_list3, 0);
1475
1476       tmp_list3 = tmp_list2 = 0;
1477       verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1478       warn_for_collisions (tmp_list2);
1479       merge_tlist (pbefore_sp, tmp_list3, 0);
1480       /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1481          two first, to avoid warning for (a ? b++ : b++).  */
1482       merge_tlist (&tmp_nosp, tmp_list2, 0);
1483       add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1484       return;
1485
1486     case PREDECREMENT_EXPR:
1487     case PREINCREMENT_EXPR:
1488     case POSTDECREMENT_EXPR:
1489     case POSTINCREMENT_EXPR:
1490       verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1491       return;
1492
1493     case MODIFY_EXPR:
1494       tmp_before = tmp_nosp = tmp_list3 = 0;
1495       verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1496       verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1497       /* Expressions inside the LHS are not ordered wrt. the sequence points
1498          in the RHS.  Example:
1499            *a = (a++, 2)
1500          Despite the fact that the modification of "a" is in the before_sp
1501          list (tmp_before), it conflicts with the use of "a" in the LHS.
1502          We can handle this by adding the contents of tmp_list3
1503          to those of tmp_before, and redoing the collision warnings for that
1504          list.  */
1505       add_tlist (&tmp_before, tmp_list3, x, 1);
1506       warn_for_collisions (tmp_before);
1507       /* Exclude the LHS itself here; we first have to merge it into the
1508          tmp_nosp list.  This is done to avoid warning for "a = a"; if we
1509          didn't exclude the LHS, we'd get it twice, once as a read and once
1510          as a write.  */
1511       add_tlist (pno_sp, tmp_list3, x, 0);
1512       warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1513
1514       merge_tlist (pbefore_sp, tmp_before, 0);
1515       if (warning_candidate_p (TREE_OPERAND (x, 0)))
1516         merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1517       add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1518       return;
1519
1520     case CALL_EXPR:
1521       /* We need to warn about conflicts among arguments and conflicts between
1522          args and the function address.  Side effects of the function address,
1523          however, are not ordered by the sequence point of the call.  */
1524       tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1525       verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1526       if (TREE_OPERAND (x, 1))
1527         verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
1528       merge_tlist (&tmp_list3, tmp_list2, 0);
1529       add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1530       add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1531       warn_for_collisions (tmp_before);
1532       add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1533       return;
1534
1535     case TREE_LIST:
1536       /* Scan all the list, e.g. indices of multi dimensional array.  */
1537       while (x)
1538         {
1539           tmp_before = tmp_nosp = 0;
1540           verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1541           merge_tlist (&tmp_nosp, tmp_before, 0);
1542           add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1543           x = TREE_CHAIN (x);
1544         }
1545       return;
1546
1547     case SAVE_EXPR:
1548       {
1549         struct tlist_cache *t;
1550         for (t = save_expr_cache; t; t = t->next)
1551           if (t->expr == x)
1552             break;
1553
1554         if (! t)
1555           {
1556             t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
1557                                                       sizeof *t);
1558             t->next = save_expr_cache;
1559             t->expr = x;
1560             save_expr_cache = t;
1561
1562             tmp_before = tmp_nosp = 0;
1563             verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1564             warn_for_collisions (tmp_nosp);
1565
1566             tmp_list3 = 0;
1567             while (tmp_nosp)
1568               {
1569                 struct tlist *t = tmp_nosp;
1570                 tmp_nosp = t->next;
1571                 merge_tlist (&tmp_list3, t, 0);
1572               }
1573             t->cache_before_sp = tmp_before;
1574             t->cache_after_sp = tmp_list3;
1575           }
1576         merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1577         add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1578         return;
1579       }
1580     default:
1581       break;
1582     }
1583
1584   if (class == '1')
1585     {
1586       if (first_rtl_op (code) == 0)
1587         return;
1588       x = TREE_OPERAND (x, 0);
1589       writer = 0;
1590       goto restart;
1591     }
1592
1593   switch (class)
1594     {
1595     case 'r':
1596     case '<':
1597     case '2':
1598     case 'b':
1599     case 'e':
1600     case 's':
1601     case 'x':
1602       {
1603         int lp;
1604         int max = first_rtl_op (TREE_CODE (x));
1605         for (lp = 0; lp < max; lp++)
1606           {
1607             tmp_before = tmp_nosp = 0;
1608             verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
1609             merge_tlist (&tmp_nosp, tmp_before, 0);
1610             add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1611           }
1612         break;
1613       }
1614     }
1615 }
1616
1617 /* Try to warn for undefined behaviour in EXPR due to missing sequence
1618    points.  */
1619
1620 static void
1621 verify_sequence_points (expr)
1622      tree expr;
1623 {
1624   struct tlist *before_sp = 0, *after_sp = 0;
1625
1626   warned_ids = 0;
1627   save_expr_cache = 0;
1628   if (tlist_firstobj == 0)
1629     {
1630       gcc_obstack_init (&tlist_obstack);
1631       tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
1632     }
1633
1634   verify_tree (expr, &before_sp, &after_sp, 0);
1635   warn_for_collisions (after_sp);
1636   obstack_free (&tlist_obstack, tlist_firstobj);
1637 }
1638
1639 tree
1640 c_expand_expr_stmt (expr)
1641      tree expr;
1642 {
1643   /* Do default conversion if safe and possibly important,
1644      in case within ({...}).  */
1645   if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1646       || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1647     expr = default_conversion (expr);
1648
1649   if (warn_sequence_point)
1650     verify_sequence_points (expr);
1651
1652   if (TREE_TYPE (expr) != error_mark_node
1653       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
1654       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1655     error ("expression statement has incomplete type");
1656
1657   last_expr_type = TREE_TYPE (expr); 
1658   return add_stmt (build_stmt (EXPR_STMT, expr));
1659 }
1660 \f
1661 /* Validate the expression after `case' and apply default promotions.  */
1662
1663 tree
1664 check_case_value (value)
1665      tree value;
1666 {
1667   if (value == NULL_TREE)
1668     return value;
1669
1670   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
1671   STRIP_TYPE_NOPS (value);
1672   /* In C++, the following is allowed:
1673
1674        const int i = 3;
1675        switch (...) { case i: ... }
1676
1677      So, we try to reduce the VALUE to a constant that way.  */
1678   if (c_language == clk_cplusplus)
1679     {
1680       value = decl_constant_value (value);
1681       STRIP_TYPE_NOPS (value);
1682       value = fold (value);
1683     }
1684
1685   if (TREE_CODE (value) != INTEGER_CST
1686       && value != error_mark_node)
1687     {
1688       error ("case label does not reduce to an integer constant");
1689       value = error_mark_node;
1690     }
1691   else
1692     /* Promote char or short to int.  */
1693     value = default_conversion (value);
1694
1695   constant_expression_warning (value);
1696
1697   return value;
1698 }
1699 \f
1700 /* Return an integer type with BITS bits of precision,
1701    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
1702
1703 tree
1704 type_for_size (bits, unsignedp)
1705      unsigned bits;
1706      int unsignedp;
1707 {
1708   if (bits == TYPE_PRECISION (integer_type_node))
1709     return unsignedp ? unsigned_type_node : integer_type_node;
1710
1711   if (bits == TYPE_PRECISION (signed_char_type_node))
1712     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1713
1714   if (bits == TYPE_PRECISION (short_integer_type_node))
1715     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1716
1717   if (bits == TYPE_PRECISION (long_integer_type_node))
1718     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1719
1720   if (bits == TYPE_PRECISION (long_long_integer_type_node))
1721     return (unsignedp ? long_long_unsigned_type_node
1722             : long_long_integer_type_node);
1723
1724   if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1725     return (unsignedp ? widest_unsigned_literal_type_node
1726             : widest_integer_literal_type_node);
1727
1728   if (bits <= TYPE_PRECISION (intQI_type_node))
1729     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1730
1731   if (bits <= TYPE_PRECISION (intHI_type_node))
1732     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1733
1734   if (bits <= TYPE_PRECISION (intSI_type_node))
1735     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1736
1737   if (bits <= TYPE_PRECISION (intDI_type_node))
1738     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1739
1740   return 0;
1741 }
1742
1743 /* Return a data type that has machine mode MODE.
1744    If the mode is an integer,
1745    then UNSIGNEDP selects between signed and unsigned types.  */
1746
1747 tree
1748 type_for_mode (mode, unsignedp)
1749      enum machine_mode mode;
1750      int unsignedp;
1751 {
1752   if (mode == TYPE_MODE (integer_type_node))
1753     return unsignedp ? unsigned_type_node : integer_type_node;
1754
1755   if (mode == TYPE_MODE (signed_char_type_node))
1756     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1757
1758   if (mode == TYPE_MODE (short_integer_type_node))
1759     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1760
1761   if (mode == TYPE_MODE (long_integer_type_node))
1762     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1763
1764   if (mode == TYPE_MODE (long_long_integer_type_node))
1765     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1766
1767   if (mode == TYPE_MODE (widest_integer_literal_type_node))
1768     return unsignedp ? widest_unsigned_literal_type_node
1769                      : widest_integer_literal_type_node;
1770
1771   if (mode == TYPE_MODE (intQI_type_node))
1772     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1773
1774   if (mode == TYPE_MODE (intHI_type_node))
1775     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1776
1777   if (mode == TYPE_MODE (intSI_type_node))
1778     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1779
1780   if (mode == TYPE_MODE (intDI_type_node))
1781     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1782
1783 #if HOST_BITS_PER_WIDE_INT >= 64
1784   if (mode == TYPE_MODE (intTI_type_node))
1785     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1786 #endif
1787
1788   if (mode == TYPE_MODE (float_type_node))
1789     return float_type_node;
1790
1791   if (mode == TYPE_MODE (double_type_node))
1792     return double_type_node;
1793
1794   if (mode == TYPE_MODE (long_double_type_node))
1795     return long_double_type_node;
1796
1797   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1798     return build_pointer_type (char_type_node);
1799
1800   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1801     return build_pointer_type (integer_type_node);
1802
1803 #ifdef VECTOR_MODE_SUPPORTED_P
1804   if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1805     return V4SF_type_node;
1806   if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1807     return V4SI_type_node;
1808   if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1809     return V2SI_type_node;
1810   if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1811     return V4HI_type_node;
1812   if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1813     return V8QI_type_node;
1814 #endif
1815
1816   return 0;
1817 }
1818
1819 /* Return an unsigned type the same as TYPE in other respects. */
1820 tree
1821 unsigned_type (type)
1822      tree type;
1823 {
1824   tree type1 = TYPE_MAIN_VARIANT (type);
1825   if (type1 == signed_char_type_node || type1 == char_type_node)
1826     return unsigned_char_type_node;
1827   if (type1 == integer_type_node)
1828     return unsigned_type_node;
1829   if (type1 == short_integer_type_node)
1830     return short_unsigned_type_node;
1831   if (type1 == long_integer_type_node)
1832     return long_unsigned_type_node;
1833   if (type1 == long_long_integer_type_node)
1834     return long_long_unsigned_type_node;
1835   if (type1 == widest_integer_literal_type_node)
1836     return widest_unsigned_literal_type_node;
1837 #if HOST_BITS_PER_WIDE_INT >= 64
1838   if (type1 == intTI_type_node)
1839     return unsigned_intTI_type_node;
1840 #endif
1841   if (type1 == intDI_type_node)
1842     return unsigned_intDI_type_node;
1843   if (type1 == intSI_type_node)
1844     return unsigned_intSI_type_node;
1845   if (type1 == intHI_type_node)
1846     return unsigned_intHI_type_node;
1847   if (type1 == intQI_type_node)
1848     return unsigned_intQI_type_node;
1849
1850   return signed_or_unsigned_type (1, type);
1851 }
1852
1853 /* Return a signed type the same as TYPE in other respects.  */
1854
1855 tree
1856 signed_type (type)
1857      tree type;
1858 {
1859   tree type1 = TYPE_MAIN_VARIANT (type);
1860   if (type1 == unsigned_char_type_node || type1 == char_type_node)
1861     return signed_char_type_node;
1862   if (type1 == unsigned_type_node)
1863     return integer_type_node;
1864   if (type1 == short_unsigned_type_node)
1865     return short_integer_type_node;
1866   if (type1 == long_unsigned_type_node)
1867     return long_integer_type_node;
1868   if (type1 == long_long_unsigned_type_node)
1869     return long_long_integer_type_node;
1870   if (type1 == widest_unsigned_literal_type_node)
1871     return widest_integer_literal_type_node;
1872 #if HOST_BITS_PER_WIDE_INT >= 64
1873   if (type1 == unsigned_intTI_type_node)
1874     return intTI_type_node;
1875 #endif
1876   if (type1 == unsigned_intDI_type_node)
1877     return intDI_type_node;
1878   if (type1 == unsigned_intSI_type_node)
1879     return intSI_type_node;
1880   if (type1 == unsigned_intHI_type_node)
1881     return intHI_type_node;
1882   if (type1 == unsigned_intQI_type_node)
1883     return intQI_type_node;
1884
1885   return signed_or_unsigned_type (0, type);
1886 }
1887
1888 /* Return a type the same as TYPE except unsigned or
1889    signed according to UNSIGNEDP.  */
1890
1891 tree
1892 signed_or_unsigned_type (unsignedp, type)
1893      int unsignedp;
1894      tree type;
1895 {
1896   if (! INTEGRAL_TYPE_P (type)
1897       || TREE_UNSIGNED (type) == unsignedp)
1898     return type;
1899
1900   if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1901     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1902   if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1903     return unsignedp ? unsigned_type_node : integer_type_node;
1904   if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1905     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1906   if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1907     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1908   if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1909     return (unsignedp ? long_long_unsigned_type_node
1910             : long_long_integer_type_node);
1911   if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
1912     return (unsignedp ? widest_unsigned_literal_type_node
1913             : widest_integer_literal_type_node);
1914   return type;
1915 }
1916 \f
1917 /* Return the minimum number of bits needed to represent VALUE in a
1918    signed or unsigned type, UNSIGNEDP says which.  */
1919
1920 unsigned int
1921 min_precision (value, unsignedp)
1922      tree value;
1923      int unsignedp;
1924 {
1925   int log;
1926
1927   /* If the value is negative, compute its negative minus 1.  The latter
1928      adjustment is because the absolute value of the largest negative value
1929      is one larger than the largest positive value.  This is equivalent to
1930      a bit-wise negation, so use that operation instead.  */
1931
1932   if (tree_int_cst_sgn (value) < 0)
1933     value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1934
1935   /* Return the number of bits needed, taking into account the fact
1936      that we need one more bit for a signed than unsigned type.  */
1937
1938   if (integer_zerop (value))
1939     log = 0;
1940   else
1941     log = tree_floor_log2 (value);
1942
1943   return log + 1 + ! unsignedp;
1944 }
1945 \f
1946 /* Print an error message for invalid operands to arith operation CODE.
1947    NOP_EXPR is used as a special case (see truthvalue_conversion).  */
1948
1949 void
1950 binary_op_error (code)
1951      enum tree_code code;
1952 {
1953   register const char *opname;
1954
1955   switch (code)
1956     {
1957     case NOP_EXPR:
1958       error ("invalid truth-value expression");
1959       return;
1960
1961     case PLUS_EXPR:
1962       opname = "+"; break;
1963     case MINUS_EXPR:
1964       opname = "-"; break;
1965     case MULT_EXPR:
1966       opname = "*"; break;
1967     case MAX_EXPR:
1968       opname = "max"; break;
1969     case MIN_EXPR:
1970       opname = "min"; break;
1971     case EQ_EXPR:
1972       opname = "=="; break;
1973     case NE_EXPR:
1974       opname = "!="; break;
1975     case LE_EXPR:
1976       opname = "<="; break;
1977     case GE_EXPR:
1978       opname = ">="; break;
1979     case LT_EXPR:
1980       opname = "<"; break;
1981     case GT_EXPR:
1982       opname = ">"; break;
1983     case LSHIFT_EXPR:
1984       opname = "<<"; break;
1985     case RSHIFT_EXPR:
1986       opname = ">>"; break;
1987     case TRUNC_MOD_EXPR:
1988     case FLOOR_MOD_EXPR:
1989       opname = "%"; break;
1990     case TRUNC_DIV_EXPR:
1991     case FLOOR_DIV_EXPR:
1992       opname = "/"; break;
1993     case BIT_AND_EXPR:
1994       opname = "&"; break;
1995     case BIT_IOR_EXPR:
1996       opname = "|"; break;
1997     case TRUTH_ANDIF_EXPR:
1998       opname = "&&"; break;
1999     case TRUTH_ORIF_EXPR:
2000       opname = "||"; break;
2001     case BIT_XOR_EXPR:
2002       opname = "^"; break;
2003     case LROTATE_EXPR:
2004     case RROTATE_EXPR:
2005       opname = "rotate"; break;
2006     default:
2007       opname = "unknown"; break;
2008     }
2009   error ("invalid operands to binary %s", opname);
2010 }
2011 \f
2012 /* Subroutine of build_binary_op, used for comparison operations.
2013    See if the operands have both been converted from subword integer types
2014    and, if so, perhaps change them both back to their original type.
2015    This function is also responsible for converting the two operands
2016    to the proper common type for comparison.
2017
2018    The arguments of this function are all pointers to local variables
2019    of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2020    RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2021
2022    If this function returns nonzero, it means that the comparison has
2023    a constant value.  What this function returns is an expression for
2024    that value.  */
2025
2026 tree
2027 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2028      tree *op0_ptr, *op1_ptr;
2029      tree *restype_ptr;
2030      enum tree_code *rescode_ptr;
2031 {
2032   register tree type;
2033   tree op0 = *op0_ptr;
2034   tree op1 = *op1_ptr;
2035   int unsignedp0, unsignedp1;
2036   int real1, real2;
2037   tree primop0, primop1;
2038   enum tree_code code = *rescode_ptr;
2039
2040   /* Throw away any conversions to wider types
2041      already present in the operands.  */
2042
2043   primop0 = get_narrower (op0, &unsignedp0);
2044   primop1 = get_narrower (op1, &unsignedp1);
2045
2046   /* Handle the case that OP0 does not *contain* a conversion
2047      but it *requires* conversion to FINAL_TYPE.  */
2048
2049   if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2050     unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2051   if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2052     unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2053
2054   /* If one of the operands must be floated, we cannot optimize.  */
2055   real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2056   real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2057
2058   /* If first arg is constant, swap the args (changing operation
2059      so value is preserved), for canonicalization.  Don't do this if
2060      the second arg is 0.  */
2061
2062   if (TREE_CONSTANT (primop0)
2063       && ! integer_zerop (primop1) && ! real_zerop (primop1))
2064     {
2065       register tree tem = primop0;
2066       register int temi = unsignedp0;
2067       primop0 = primop1;
2068       primop1 = tem;
2069       tem = op0;
2070       op0 = op1;
2071       op1 = tem;
2072       *op0_ptr = op0;
2073       *op1_ptr = op1;
2074       unsignedp0 = unsignedp1;
2075       unsignedp1 = temi;
2076       temi = real1;
2077       real1 = real2;
2078       real2 = temi;
2079
2080       switch (code)
2081         {
2082         case LT_EXPR:
2083           code = GT_EXPR;
2084           break;
2085         case GT_EXPR:
2086           code = LT_EXPR;
2087           break;
2088         case LE_EXPR:
2089           code = GE_EXPR;
2090           break;
2091         case GE_EXPR:
2092           code = LE_EXPR;
2093           break;
2094         default:
2095           break;
2096         }
2097       *rescode_ptr = code;
2098     }
2099
2100   /* If comparing an integer against a constant more bits wide,
2101      maybe we can deduce a value of 1 or 0 independent of the data.
2102      Or else truncate the constant now
2103      rather than extend the variable at run time.
2104
2105      This is only interesting if the constant is the wider arg.
2106      Also, it is not safe if the constant is unsigned and the
2107      variable arg is signed, since in this case the variable
2108      would be sign-extended and then regarded as unsigned.
2109      Our technique fails in this case because the lowest/highest
2110      possible unsigned results don't follow naturally from the
2111      lowest/highest possible values of the variable operand.
2112      For just EQ_EXPR and NE_EXPR there is another technique that
2113      could be used: see if the constant can be faithfully represented
2114      in the other operand's type, by truncating it and reextending it
2115      and see if that preserves the constant's value.  */
2116
2117   if (!real1 && !real2
2118       && TREE_CODE (primop1) == INTEGER_CST
2119       && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2120     {
2121       int min_gt, max_gt, min_lt, max_lt;
2122       tree maxval, minval;
2123       /* 1 if comparison is nominally unsigned.  */
2124       int unsignedp = TREE_UNSIGNED (*restype_ptr);
2125       tree val;
2126
2127       type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2128
2129       /* If TYPE is an enumeration, then we need to get its min/max
2130          values from it's underlying integral type, not the enumerated
2131          type itself.  */
2132       if (TREE_CODE (type) == ENUMERAL_TYPE)
2133         type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2134
2135       maxval = TYPE_MAX_VALUE (type);
2136       minval = TYPE_MIN_VALUE (type);
2137
2138       if (unsignedp && !unsignedp0)
2139         *restype_ptr = signed_type (*restype_ptr);
2140
2141       if (TREE_TYPE (primop1) != *restype_ptr)
2142         primop1 = convert (*restype_ptr, primop1);
2143       if (type != *restype_ptr)
2144         {
2145           minval = convert (*restype_ptr, minval);
2146           maxval = convert (*restype_ptr, maxval);
2147         }
2148
2149       if (unsignedp && unsignedp0)
2150         {
2151           min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2152           max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2153           min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2154           max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2155         }
2156       else
2157         {
2158           min_gt = INT_CST_LT (primop1, minval);
2159           max_gt = INT_CST_LT (primop1, maxval);
2160           min_lt = INT_CST_LT (minval, primop1);
2161           max_lt = INT_CST_LT (maxval, primop1);
2162         }
2163
2164       val = 0;
2165       /* This used to be a switch, but Genix compiler can't handle that.  */
2166       if (code == NE_EXPR)
2167         {
2168           if (max_lt || min_gt)
2169             val = boolean_true_node;
2170         }
2171       else if (code == EQ_EXPR)
2172         {
2173           if (max_lt || min_gt)
2174             val = boolean_false_node;
2175         }
2176       else if (code == LT_EXPR)
2177         {
2178           if (max_lt)
2179             val = boolean_true_node;
2180           if (!min_lt)
2181             val = boolean_false_node;
2182         }
2183       else if (code == GT_EXPR)
2184         {
2185           if (min_gt)
2186             val = boolean_true_node;
2187           if (!max_gt)
2188             val = boolean_false_node;
2189         }
2190       else if (code == LE_EXPR)
2191         {
2192           if (!max_gt)
2193             val = boolean_true_node;
2194           if (min_gt)
2195             val = boolean_false_node;
2196         }
2197       else if (code == GE_EXPR)
2198         {
2199           if (!min_lt)
2200             val = boolean_true_node;
2201           if (max_lt)
2202             val = boolean_false_node;
2203         }
2204
2205       /* If primop0 was sign-extended and unsigned comparison specd,
2206          we did a signed comparison above using the signed type bounds.
2207          But the comparison we output must be unsigned.
2208
2209          Also, for inequalities, VAL is no good; but if the signed
2210          comparison had *any* fixed result, it follows that the
2211          unsigned comparison just tests the sign in reverse
2212          (positive values are LE, negative ones GE).
2213          So we can generate an unsigned comparison
2214          against an extreme value of the signed type.  */
2215
2216       if (unsignedp && !unsignedp0)
2217         {
2218           if (val != 0)
2219             switch (code)
2220               {
2221               case LT_EXPR:
2222               case GE_EXPR:
2223                 primop1 = TYPE_MIN_VALUE (type);
2224                 val = 0;
2225                 break;
2226
2227               case LE_EXPR:
2228               case GT_EXPR:
2229                 primop1 = TYPE_MAX_VALUE (type);
2230                 val = 0;
2231                 break;
2232
2233               default:
2234                 break;
2235               }
2236           type = unsigned_type (type);
2237         }
2238
2239       if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2240         {
2241           /* This is the case of (char)x >?< 0x80, which people used to use
2242              expecting old C compilers to change the 0x80 into -0x80.  */
2243           if (val == boolean_false_node)
2244             warning ("comparison is always false due to limited range of data type");
2245           if (val == boolean_true_node)
2246             warning ("comparison is always true due to limited range of data type");
2247         }
2248
2249       if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2250         {
2251           /* This is the case of (unsigned char)x >?< -1 or < 0.  */
2252           if (val == boolean_false_node)
2253             warning ("comparison is always false due to limited range of data type");
2254           if (val == boolean_true_node)
2255             warning ("comparison is always true due to limited range of data type");
2256         }
2257
2258       if (val != 0)
2259         {
2260           /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
2261           if (TREE_SIDE_EFFECTS (primop0))
2262             return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2263           return val;
2264         }
2265
2266       /* Value is not predetermined, but do the comparison
2267          in the type of the operand that is not constant.
2268          TYPE is already properly set.  */
2269     }
2270   else if (real1 && real2
2271            && (TYPE_PRECISION (TREE_TYPE (primop0))
2272                == TYPE_PRECISION (TREE_TYPE (primop1))))
2273     type = TREE_TYPE (primop0);
2274
2275   /* If args' natural types are both narrower than nominal type
2276      and both extend in the same manner, compare them
2277      in the type of the wider arg.
2278      Otherwise must actually extend both to the nominal
2279      common type lest different ways of extending
2280      alter the result.
2281      (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
2282
2283   else if (unsignedp0 == unsignedp1 && real1 == real2
2284            && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2285            && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2286     {
2287       type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2288       type = signed_or_unsigned_type (unsignedp0
2289                                       || TREE_UNSIGNED (*restype_ptr),
2290                                       type);
2291       /* Make sure shorter operand is extended the right way
2292          to match the longer operand.  */
2293       primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2294                          primop0);
2295       primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2296                          primop1);
2297     }
2298   else
2299     {
2300       /* Here we must do the comparison on the nominal type
2301          using the args exactly as we received them.  */
2302       type = *restype_ptr;
2303       primop0 = op0;
2304       primop1 = op1;
2305
2306       if (!real1 && !real2 && integer_zerop (primop1)
2307           && TREE_UNSIGNED (*restype_ptr))
2308         {
2309           tree value = 0;
2310           switch (code)
2311             {
2312             case GE_EXPR:
2313               /* All unsigned values are >= 0, so we warn if extra warnings
2314                  are requested.  However, if OP0 is a constant that is
2315                  >= 0, the signedness of the comparison isn't an issue,
2316                  so suppress the warning.  */
2317               if (extra_warnings && !in_system_header
2318                   && ! (TREE_CODE (primop0) == INTEGER_CST
2319                         && ! TREE_OVERFLOW (convert (signed_type (type),
2320                                                      primop0))))
2321                 warning ("comparison of unsigned expression >= 0 is always true");
2322               value = boolean_true_node;
2323               break;
2324
2325             case LT_EXPR:
2326               if (extra_warnings && !in_system_header
2327                   && ! (TREE_CODE (primop0) == INTEGER_CST
2328                         && ! TREE_OVERFLOW (convert (signed_type (type),
2329                                                      primop0))))
2330                 warning ("comparison of unsigned expression < 0 is always false");
2331               value = boolean_false_node;
2332               break;
2333
2334             default:
2335               break;
2336             }
2337
2338           if (value != 0)
2339             {
2340               /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
2341               if (TREE_SIDE_EFFECTS (primop0))
2342                 return build (COMPOUND_EXPR, TREE_TYPE (value),
2343                               primop0, value);
2344               return value;
2345             }
2346         }
2347     }
2348
2349   *op0_ptr = convert (type, primop0);
2350   *op1_ptr = convert (type, primop1);
2351
2352   *restype_ptr = boolean_type_node;
2353
2354   return 0;
2355 }
2356 \f
2357 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2358    or validate its data type for an `if' or `while' statement or ?..: exp.
2359
2360    This preparation consists of taking the ordinary
2361    representation of an expression expr and producing a valid tree
2362    boolean expression describing whether expr is nonzero.  We could
2363    simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2364    but we optimize comparisons, &&, ||, and !.
2365
2366    The resulting type should always be `boolean_type_node'.  */
2367
2368 tree
2369 truthvalue_conversion (expr)
2370      tree expr;
2371 {
2372   if (TREE_CODE (expr) == ERROR_MARK)
2373     return expr;
2374
2375 #if 0 /* This appears to be wrong for C++.  */
2376   /* These really should return error_mark_node after 2.4 is stable.
2377      But not all callers handle ERROR_MARK properly.  */
2378   switch (TREE_CODE (TREE_TYPE (expr)))
2379     {
2380     case RECORD_TYPE:
2381       error ("struct type value used where scalar is required");
2382       return boolean_false_node;
2383
2384     case UNION_TYPE:
2385       error ("union type value used where scalar is required");
2386       return boolean_false_node;
2387
2388     case ARRAY_TYPE:
2389       error ("array type value used where scalar is required");
2390       return boolean_false_node;
2391
2392     default:
2393       break;
2394     }
2395 #endif /* 0 */
2396
2397   switch (TREE_CODE (expr))
2398     {
2399     case EQ_EXPR:
2400     case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2401     case TRUTH_ANDIF_EXPR:
2402     case TRUTH_ORIF_EXPR:
2403     case TRUTH_AND_EXPR:
2404     case TRUTH_OR_EXPR:
2405     case TRUTH_XOR_EXPR:
2406     case TRUTH_NOT_EXPR:
2407       TREE_TYPE (expr) = boolean_type_node;
2408       return expr;
2409
2410     case ERROR_MARK:
2411       return expr;
2412
2413     case INTEGER_CST:
2414       return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2415
2416     case REAL_CST:
2417       return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2418
2419     case ADDR_EXPR:
2420       /* If we are taking the address of a external decl, it might be zero
2421          if it is weak, so we cannot optimize.  */
2422       if (DECL_P (TREE_OPERAND (expr, 0))
2423           && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2424         break;
2425
2426       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2427         return build (COMPOUND_EXPR, boolean_type_node,
2428                       TREE_OPERAND (expr, 0), boolean_true_node);
2429       else
2430         return boolean_true_node;
2431
2432     case COMPLEX_EXPR:
2433       return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2434                                ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2435                               truthvalue_conversion (TREE_OPERAND (expr, 0)),
2436                               truthvalue_conversion (TREE_OPERAND (expr, 1)),
2437                               0);
2438
2439     case NEGATE_EXPR:
2440     case ABS_EXPR:
2441     case FLOAT_EXPR:
2442     case FFS_EXPR:
2443       /* These don't change whether an object is non-zero or zero.  */
2444       return truthvalue_conversion (TREE_OPERAND (expr, 0));
2445
2446     case LROTATE_EXPR:
2447     case RROTATE_EXPR:
2448       /* These don't change whether an object is zero or non-zero, but
2449          we can't ignore them if their second arg has side-effects.  */
2450       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2451         return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2452                       truthvalue_conversion (TREE_OPERAND (expr, 0)));
2453       else
2454         return truthvalue_conversion (TREE_OPERAND (expr, 0));
2455
2456     case COND_EXPR:
2457       /* Distribute the conversion into the arms of a COND_EXPR.  */
2458       return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2459                           truthvalue_conversion (TREE_OPERAND (expr, 1)),
2460                           truthvalue_conversion (TREE_OPERAND (expr, 2))));
2461
2462     case CONVERT_EXPR:
2463       /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2464          since that affects how `default_conversion' will behave.  */
2465       if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2466           || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2467         break;
2468       /* fall through...  */
2469     case NOP_EXPR:
2470       /* If this is widening the argument, we can ignore it.  */
2471       if (TYPE_PRECISION (TREE_TYPE (expr))
2472           >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2473         return truthvalue_conversion (TREE_OPERAND (expr, 0));
2474       break;
2475
2476     case MINUS_EXPR:
2477       /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2478          this case.  */
2479       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2480           && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2481         break;
2482       /* fall through...  */
2483     case BIT_XOR_EXPR:
2484       /* This and MINUS_EXPR can be changed into a comparison of the
2485          two objects.  */
2486       if (TREE_TYPE (TREE_OPERAND (expr, 0))
2487           == TREE_TYPE (TREE_OPERAND (expr, 1)))
2488         return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2489                                 TREE_OPERAND (expr, 1), 1);
2490       return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2491                               fold (build1 (NOP_EXPR,
2492                                             TREE_TYPE (TREE_OPERAND (expr, 0)),
2493                                             TREE_OPERAND (expr, 1))), 1);
2494
2495     case BIT_AND_EXPR:
2496       if (integer_onep (TREE_OPERAND (expr, 1))
2497           && TREE_TYPE (expr) != boolean_type_node)
2498         /* Using convert here would cause infinite recursion.  */
2499         return build1 (NOP_EXPR, boolean_type_node, expr);
2500       break;
2501
2502     case MODIFY_EXPR:
2503       if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2504         warning ("suggest parentheses around assignment used as truth value");
2505       break;
2506
2507     default:
2508       break;
2509     }
2510
2511   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2512     {
2513       tree tem = save_expr (expr);
2514       return (build_binary_op
2515               ((TREE_SIDE_EFFECTS (expr)
2516                 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2517                truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2518                truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2519                0));
2520     }
2521
2522   return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2523 }
2524 \f
2525 static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree,
2526                                         int, enum built_in_class, int, int,
2527                                         int));
2528
2529 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2530    down to the element type of an array.  */
2531
2532 tree
2533 c_build_qualified_type (type, type_quals)
2534      tree type;
2535      int type_quals;
2536 {
2537   /* A restrict-qualified pointer type must be a pointer to object or
2538      incomplete type.  Note that the use of POINTER_TYPE_P also allows
2539      REFERENCE_TYPEs, which is appropriate for C++.  Unfortunately,
2540      the C++ front-end also use POINTER_TYPE for pointer-to-member
2541      values, so even though it should be illegal to use `restrict'
2542      with such an entity we don't flag that here.  Thus, special case
2543      code for that case is required in the C++ front-end.  */
2544   if ((type_quals & TYPE_QUAL_RESTRICT)
2545       && (!POINTER_TYPE_P (type)
2546           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
2547     {
2548       error ("invalid use of `restrict'");
2549       type_quals &= ~TYPE_QUAL_RESTRICT;
2550     }
2551
2552   if (TREE_CODE (type) == ARRAY_TYPE)
2553     return build_array_type (c_build_qualified_type (TREE_TYPE (type),
2554                                                      type_quals),
2555                              TYPE_DOMAIN (type));
2556   return build_qualified_type (type, type_quals);
2557 }
2558
2559 /* Apply the TYPE_QUALS to the new DECL.  */
2560
2561 void
2562 c_apply_type_quals_to_decl (type_quals, decl)
2563      int type_quals;
2564      tree decl;
2565 {
2566   if ((type_quals & TYPE_QUAL_CONST)
2567       || (TREE_TYPE (decl) 
2568           && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
2569     TREE_READONLY (decl) = 1;
2570   if (type_quals & TYPE_QUAL_VOLATILE)
2571     {
2572       TREE_SIDE_EFFECTS (decl) = 1;
2573       TREE_THIS_VOLATILE (decl) = 1;
2574     }
2575   if (type_quals & TYPE_QUAL_RESTRICT)
2576     {
2577       if (!TREE_TYPE (decl)
2578           || !POINTER_TYPE_P (TREE_TYPE (decl))
2579           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
2580         error ("invalid use of `restrict'");
2581       else if (flag_strict_aliasing)
2582         {
2583           /* No two restricted pointers can point at the same thing.
2584              However, a restricted pointer can point at the same thing
2585              as an unrestricted pointer, if that unrestricted pointer
2586              is based on the restricted pointer.  So, we make the
2587              alias set for the restricted pointer a subset of the
2588              alias set for the type pointed to by the type of the
2589              decl.  */
2590
2591           HOST_WIDE_INT pointed_to_alias_set
2592             = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
2593
2594           if (pointed_to_alias_set == 0)
2595             /* It's not legal to make a subset of alias set zero.  */
2596             ;
2597           else
2598             {
2599               DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
2600               record_alias_subset  (pointed_to_alias_set,
2601                                     DECL_POINTER_ALIAS_SET (decl));
2602             }
2603         }
2604     }
2605 }
2606
2607
2608 /* Return the typed-based alias set for T, which may be an expression
2609    or a type.  Return -1 if we don't do anything special.  */
2610
2611 HOST_WIDE_INT
2612 lang_get_alias_set (t)
2613      tree t;
2614 {
2615   tree u;
2616   
2617   /* We know nothing about vector types */
2618   if (TREE_CODE (t) == VECTOR_TYPE)
2619     return 0;          
2620   
2621   /* Permit type-punning when accessing a union, provided the access
2622      is directly through the union.  For example, this code does not
2623      permit taking the address of a union member and then storing
2624      through it.  Even the type-punning allowed here is a GCC
2625      extension, albeit a common and useful one; the C standard says
2626      that such accesses have implementation-defined behavior.  */
2627   for (u = t;
2628        TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2629        u = TREE_OPERAND (u, 0))
2630     if (TREE_CODE (u) == COMPONENT_REF
2631         && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2632       return 0;
2633
2634   /* If this is a char *, the ANSI C standard says it can alias
2635      anything.  Note that all references need do this.  */
2636   if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
2637       && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
2638       && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
2639     return 0;
2640
2641   /* That's all the expressions we handle specially.  */
2642   if (! TYPE_P (t))
2643     return -1;
2644
2645   /* The C standard specifically allows aliasing between signed and
2646      unsigned variants of the same type.  We treat the signed
2647      variant as canonical.  */
2648   if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
2649     {
2650       tree t1 = signed_type (t);
2651
2652       /* t1 == t can happen for boolean nodes which are always unsigned.  */
2653       if (t1 != t)
2654         return get_alias_set (t1);
2655     }
2656   else if (POINTER_TYPE_P (t))
2657     {
2658       tree t1;
2659
2660       /* Unfortunately, there is no canonical form of a pointer type.
2661          In particular, if we have `typedef int I', then `int *', and
2662          `I *' are different types.  So, we have to pick a canonical
2663          representative.  We do this below.
2664
2665          Technically, this approach is actually more conservative that
2666          it needs to be.  In particular, `const int *' and `int *'
2667          chould be in different alias sets, according to the C and C++
2668          standard, since their types are not the same, and so,
2669          technically, an `int **' and `const int **' cannot point at
2670          the same thing.
2671
2672          But, the standard is wrong.  In particular, this code is
2673          legal C++:
2674
2675             int *ip;
2676             int **ipp = &ip;
2677             const int* const* cipp = &ip;
2678
2679          And, it doesn't make sense for that to be legal unless you
2680          can dereference IPP and CIPP.  So, we ignore cv-qualifiers on
2681          the pointed-to types.  This issue has been reported to the
2682          C++ committee.  */
2683       t1 = build_type_no_quals (t);
2684       if (t1 != t)
2685         return get_alias_set (t1);
2686     }
2687   /* It's not yet safe to use alias sets for classes in C++ because
2688      the TYPE_FIELDs list for a class doesn't mention base classes.  */
2689   else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
2690     return 0;
2691
2692   return -1;
2693 }
2694
2695 /* Build tree nodes and builtin functions common to both C and C++ language
2696    frontends.  */
2697
2698 void
2699 c_common_nodes_and_builtins ()
2700 {
2701   int wchar_type_size;
2702   tree array_domain_type;
2703   tree temp;
2704   tree memcpy_ftype, memset_ftype, strlen_ftype;
2705   tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype;
2706   tree fputs_ftype, fputc_ftype, fwrite_ftype, fprintf_ftype;
2707   tree endlink, int_endlink, double_endlink, unsigned_endlink;
2708   tree cstring_endlink, sizetype_endlink;
2709   tree ptr_ftype, ptr_ftype_unsigned;
2710   tree void_ftype_any, void_ftype_int, int_ftype_any;
2711   tree double_ftype_double, double_ftype_double_double;
2712   tree float_ftype_float, ldouble_ftype_ldouble;
2713   tree cfloat_ftype_cfloat, cdouble_ftype_cdouble, cldouble_ftype_cldouble;
2714   tree float_ftype_cfloat, double_ftype_cdouble, ldouble_ftype_cldouble;
2715   tree int_ftype_cptr_cptr_sizet, sizet_ftype_cstring_cstring;
2716   tree int_ftype_cstring_cstring, string_ftype_string_cstring;
2717   tree string_ftype_cstring_int, string_ftype_cstring_cstring;
2718   tree string_ftype_string_cstring_sizet, int_ftype_cstring_cstring_sizet;
2719   tree long_ftype_long;
2720   tree longlong_ftype_longlong;
2721   tree intmax_ftype_intmax;
2722   /* Either char* or void*.  */
2723   tree traditional_ptr_type_node;
2724   /* Either const char* or const void*.  */
2725   tree traditional_cptr_type_node;
2726   tree traditional_len_type_node;
2727   tree traditional_len_endlink;
2728   tree va_list_ref_type_node;
2729   tree va_list_arg_type_node;
2730
2731   /* Define `int' and `char' first so that dbx will output them first.  */
2732   record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
2733   record_builtin_type (RID_CHAR, "char", char_type_node);
2734
2735   /* `signed' is the same as `int'.  FIXME: the declarations of "signed",
2736      "unsigned long", "long long unsigned" and "unsigned short" were in C++
2737      but not C.  Are the conditionals here needed?  */
2738   if (c_language == clk_cplusplus)
2739     record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node);
2740   record_builtin_type (RID_LONG, "long int", long_integer_type_node);
2741   record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
2742   record_builtin_type (RID_MAX, "long unsigned int",
2743                        long_unsigned_type_node);
2744   if (c_language == clk_cplusplus)
2745     record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
2746   record_builtin_type (RID_MAX, "long long int",
2747                        long_long_integer_type_node);
2748   record_builtin_type (RID_MAX, "long long unsigned int",
2749                        long_long_unsigned_type_node);
2750   if (c_language == clk_cplusplus)
2751     record_builtin_type (RID_MAX, "long long unsigned",
2752                          long_long_unsigned_type_node);
2753   record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
2754   record_builtin_type (RID_MAX, "short unsigned int",
2755                        short_unsigned_type_node);
2756   if (c_language == clk_cplusplus)
2757     record_builtin_type (RID_MAX, "unsigned short",
2758                          short_unsigned_type_node);
2759
2760   /* Define both `signed char' and `unsigned char'.  */
2761   record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
2762   record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
2763
2764   /* These are types that type_for_size and type_for_mode use.  */
2765   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
2766   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
2767   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
2768   pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
2769 #if HOST_BITS_PER_WIDE_INT >= 64
2770   pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
2771 #endif
2772   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
2773   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
2774   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
2775   pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
2776 #if HOST_BITS_PER_WIDE_INT >= 64
2777   pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
2778 #endif
2779
2780   /* Create the widest literal types.  */
2781   widest_integer_literal_type_node
2782     = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
2783   pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2784                         widest_integer_literal_type_node));
2785
2786   widest_unsigned_literal_type_node
2787     = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
2788   pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2789                         widest_unsigned_literal_type_node));
2790
2791   /* `unsigned long' is the standard type for sizeof.
2792      Note that stddef.h uses `unsigned long',
2793      and this must agree, even if long and int are the same size.  */
2794   c_size_type_node =
2795     TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
2796   signed_size_type_node = signed_type (c_size_type_node);
2797   if (flag_traditional)
2798     c_size_type_node = signed_size_type_node;
2799   set_sizetype (c_size_type_node);
2800
2801   build_common_tree_nodes_2 (flag_short_double);
2802
2803   record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
2804   record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
2805   record_builtin_type (RID_MAX, "long double", long_double_type_node);
2806
2807   pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
2808                         complex_integer_type_node));
2809   pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
2810                         complex_float_type_node));
2811   pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
2812                         complex_double_type_node));
2813   pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
2814                         complex_long_double_type_node));
2815
2816   record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
2817
2818   void_list_node = build_void_list_node ();
2819
2820   /* Make a type to be the domain of a few array types
2821      whose domains don't really matter.
2822      200 is small enough that it always fits in size_t
2823      and large enough that it can hold most function names for the
2824      initializations of __FUNCTION__ and __PRETTY_FUNCTION__.  */
2825   array_domain_type = build_index_type (size_int (200));
2826
2827   /* Make a type for arrays of characters.
2828      With luck nothing will ever really depend on the length of this
2829      array type.  */
2830   char_array_type_node
2831     = build_array_type (char_type_node, array_domain_type);
2832
2833   /* Likewise for arrays of ints.  */
2834   int_array_type_node
2835     = build_array_type (integer_type_node, array_domain_type);
2836
2837 #ifdef MD_INIT_BUILTINS
2838   MD_INIT_BUILTINS;
2839 #endif
2840
2841   /* This is special for C++ so functions can be overloaded.  */
2842   wchar_type_node = get_identifier (flag_short_wchar
2843                                     ? "short unsigned int"
2844                                     : WCHAR_TYPE);
2845   wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
2846   wchar_type_size = TYPE_PRECISION (wchar_type_node);
2847   if (c_language == clk_cplusplus)
2848     {
2849       if (TREE_UNSIGNED (wchar_type_node))
2850         wchar_type_node = make_unsigned_type (wchar_type_size);
2851       else
2852         wchar_type_node = make_signed_type (wchar_type_size);
2853       record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
2854     }
2855   else
2856     {
2857       signed_wchar_type_node = signed_type (wchar_type_node);
2858       unsigned_wchar_type_node = unsigned_type (wchar_type_node);
2859     }
2860
2861   /* This is for wide string constants.  */
2862   wchar_array_type_node
2863     = build_array_type (wchar_type_node, array_domain_type);
2864
2865   string_type_node = build_pointer_type (char_type_node);
2866   const_string_type_node
2867     = build_pointer_type (build_type_variant (char_type_node, 1, 0));
2868
2869   wint_type_node =
2870     TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
2871
2872   intmax_type_node =
2873     TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
2874   uintmax_type_node =
2875     TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
2876
2877   default_function_type = build_function_type (integer_type_node, NULL_TREE);
2878   ptrdiff_type_node
2879     = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
2880   unsigned_ptrdiff_type_node = unsigned_type (ptrdiff_type_node);
2881
2882   pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
2883                         va_list_type_node));
2884
2885   pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
2886                         ptrdiff_type_node));
2887
2888   pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
2889                         sizetype));
2890
2891   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2892     {
2893       va_list_arg_type_node = va_list_ref_type_node =
2894         build_pointer_type (TREE_TYPE (va_list_type_node));
2895     }
2896   else
2897     {
2898       va_list_arg_type_node = va_list_type_node;
2899       va_list_ref_type_node = build_reference_type (va_list_type_node);
2900     }
2901  
2902   endlink = void_list_node;
2903   int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
2904   double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
2905   unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
2906   cstring_endlink = tree_cons (NULL_TREE, const_string_type_node, endlink);
2907
2908   ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
2909   ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
2910   sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
2911   /* We realloc here because sizetype could be int or unsigned.  S'ok.  */
2912   ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
2913
2914   int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
2915   void_ftype_any = build_function_type (void_type_node, NULL_TREE);
2916   void_ftype = build_function_type (void_type_node, endlink);
2917   void_ftype_int = build_function_type (void_type_node, int_endlink);
2918   void_ftype_ptr
2919     = build_function_type (void_type_node,
2920                            tree_cons (NULL_TREE, ptr_type_node, endlink));
2921
2922   float_ftype_float
2923     = build_function_type (float_type_node,
2924                            tree_cons (NULL_TREE, float_type_node, endlink));
2925
2926   double_ftype_double
2927     = build_function_type (double_type_node, double_endlink);
2928
2929   ldouble_ftype_ldouble
2930     = build_function_type (long_double_type_node,
2931                            tree_cons (NULL_TREE, long_double_type_node,
2932                                       endlink));
2933
2934   double_ftype_double_double
2935     = build_function_type (double_type_node,
2936                            tree_cons (NULL_TREE, double_type_node,
2937                                       double_endlink));
2938
2939   cfloat_ftype_cfloat
2940     = build_function_type (complex_float_type_node,
2941                            tree_cons (NULL_TREE, complex_float_type_node,
2942                                       endlink));
2943   cdouble_ftype_cdouble
2944     = build_function_type (complex_double_type_node,
2945                            tree_cons (NULL_TREE, complex_double_type_node,
2946                                       endlink));
2947   cldouble_ftype_cldouble
2948     = build_function_type (complex_long_double_type_node,
2949                            tree_cons (NULL_TREE, complex_long_double_type_node,
2950                                       endlink));
2951
2952   float_ftype_cfloat
2953     = build_function_type (float_type_node,
2954                            tree_cons (NULL_TREE, complex_float_type_node,
2955                                       endlink));
2956   double_ftype_cdouble
2957     = build_function_type (double_type_node,
2958                            tree_cons (NULL_TREE, complex_double_type_node,
2959                                       endlink));
2960   ldouble_ftype_cldouble
2961     = build_function_type (long_double_type_node,
2962                            tree_cons (NULL_TREE, complex_long_double_type_node,
2963                                       endlink));
2964
2965   int_ftype_int
2966     = build_function_type (integer_type_node, int_endlink);
2967
2968   long_ftype_long
2969     = build_function_type (long_integer_type_node,
2970                            tree_cons (NULL_TREE, long_integer_type_node,
2971                                       endlink));
2972
2973   longlong_ftype_longlong
2974     = build_function_type (long_long_integer_type_node,
2975                            tree_cons (NULL_TREE, long_long_integer_type_node,
2976                                       endlink));
2977
2978   intmax_ftype_intmax
2979     = build_function_type (intmax_type_node,
2980                            tree_cons (NULL_TREE, intmax_type_node,
2981                                       endlink));
2982
2983   int_ftype_cptr_cptr_sizet
2984     = build_function_type (integer_type_node,
2985                            tree_cons (NULL_TREE, const_ptr_type_node,
2986                                       tree_cons (NULL_TREE,
2987                                                  const_ptr_type_node,
2988                                                  sizetype_endlink)));
2989
2990   void_zero_node = build_int_2 (0, 0);
2991   TREE_TYPE (void_zero_node) = void_type_node;
2992
2993   /* Prototype for strcpy/strcat.  */
2994   string_ftype_string_cstring
2995     = build_function_type (string_type_node,
2996                            tree_cons (NULL_TREE, string_type_node,
2997                                       cstring_endlink));
2998
2999   /* Prototype for strncpy/strncat.  */
3000   string_ftype_string_cstring_sizet
3001     = build_function_type (string_type_node,
3002                            tree_cons (NULL_TREE, string_type_node,
3003                                       tree_cons (NULL_TREE,
3004                                                  const_string_type_node,
3005                                                  sizetype_endlink)));
3006
3007   traditional_len_type_node = ((flag_traditional && 
3008                                 c_language != clk_cplusplus)
3009                                ? integer_type_node : sizetype);
3010   traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
3011                                        endlink);
3012
3013   /* Prototype for strcmp.  */
3014   int_ftype_cstring_cstring
3015     = build_function_type (integer_type_node,
3016                            tree_cons (NULL_TREE, const_string_type_node,
3017                                       cstring_endlink));
3018
3019   /* Prototype for strspn/strcspn.  */
3020   sizet_ftype_cstring_cstring
3021     = build_function_type (c_size_type_node,
3022                            tree_cons (NULL_TREE, const_string_type_node,
3023                                       cstring_endlink));
3024
3025   /* Prototype for strncmp.  */
3026   int_ftype_cstring_cstring_sizet
3027     = build_function_type (integer_type_node,
3028                            tree_cons (NULL_TREE, const_string_type_node,
3029                                       tree_cons (NULL_TREE,
3030                                                  const_string_type_node,
3031                                                  sizetype_endlink)));
3032
3033   /* Prototype for strstr, strpbrk, etc.  */
3034   string_ftype_cstring_cstring
3035     = build_function_type (string_type_node,
3036                            tree_cons (NULL_TREE, const_string_type_node,
3037                                       cstring_endlink));
3038
3039   /* Prototype for strchr.  */
3040   string_ftype_cstring_int
3041     = build_function_type (string_type_node,
3042                            tree_cons (NULL_TREE, const_string_type_node,
3043                                       int_endlink));
3044
3045   /* Prototype for strlen.  */
3046   strlen_ftype
3047     = build_function_type (traditional_len_type_node, cstring_endlink);
3048
3049   traditional_ptr_type_node = ((flag_traditional && 
3050                                 c_language != clk_cplusplus)
3051                                ? string_type_node : ptr_type_node);
3052   traditional_cptr_type_node = ((flag_traditional && 
3053                                  c_language != clk_cplusplus)
3054                                ? const_string_type_node : const_ptr_type_node);
3055
3056   /* Prototype for memcpy.  */
3057   memcpy_ftype
3058     = build_function_type (traditional_ptr_type_node,
3059                            tree_cons (NULL_TREE, ptr_type_node,
3060                                       tree_cons (NULL_TREE, const_ptr_type_node,
3061                                                  sizetype_endlink)));
3062
3063   /* Prototype for memset.  */
3064   memset_ftype
3065     = build_function_type (traditional_ptr_type_node,
3066                            tree_cons (NULL_TREE, ptr_type_node,
3067                                       tree_cons (NULL_TREE, integer_type_node,
3068                                                  sizetype_endlink)));
3069
3070   /* Prototype for bzero.  */
3071   bzero_ftype
3072     = build_function_type (void_type_node,
3073                            tree_cons (NULL_TREE, traditional_ptr_type_node,
3074                                       traditional_len_endlink));
3075
3076   /* Prototype for bcmp.  */
3077   bcmp_ftype
3078     = build_function_type (integer_type_node,
3079                            tree_cons (NULL_TREE, traditional_cptr_type_node,
3080                                       tree_cons (NULL_TREE,
3081                                                  traditional_cptr_type_node,
3082                                                  traditional_len_endlink)));
3083
3084   /* Prototype for puts.  */
3085   puts_ftype
3086     = build_function_type (integer_type_node, cstring_endlink);
3087
3088   /* Prototype for printf.  */
3089   printf_ftype
3090     = build_function_type (integer_type_node,
3091                            tree_cons (NULL_TREE, const_string_type_node,
3092                                       NULL_TREE));
3093
3094   /* These stdio prototypes are declared using void* in place of
3095      FILE*.  They are only used for __builtin_ style calls, regular
3096      style builtin prototypes omit the arguments and merge those
3097      provided by stdio.h.  */
3098   /* Prototype for fwrite.  */
3099   fwrite_ftype
3100     = build_function_type (c_size_type_node,
3101                            tree_cons (NULL_TREE, const_ptr_type_node,
3102                                       tree_cons (NULL_TREE, c_size_type_node,
3103                                                  tree_cons (NULL_TREE, c_size_type_node,
3104                                                             tree_cons (NULL_TREE, ptr_type_node, endlink)))));
3105
3106   /* Prototype for fputc.  */
3107   fputc_ftype
3108     = build_function_type (integer_type_node,
3109                            tree_cons (NULL_TREE, integer_type_node,
3110                                       tree_cons (NULL_TREE, ptr_type_node, endlink)));
3111
3112   /* Prototype for fputs.  */
3113   fputs_ftype
3114     = build_function_type (integer_type_node,
3115                            tree_cons (NULL_TREE, const_string_type_node,
3116                                       tree_cons (NULL_TREE, ptr_type_node, endlink)));
3117
3118   /* Prototype for fprintf.  */
3119   fprintf_ftype
3120     = build_function_type (integer_type_node,
3121                            tree_cons (NULL_TREE, ptr_type_node,
3122                                       tree_cons (NULL_TREE,
3123                                                  const_string_type_node,
3124                                                  NULL_TREE)));
3125
3126   builtin_function ("__builtin_constant_p", default_function_type,
3127                     BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
3128
3129   builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
3130                     BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3131
3132   builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
3133                     BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3134
3135   builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
3136                     BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
3137   builtin_function_2 ("__builtin_ffs", "ffs",
3138                       int_ftype_int, int_ftype_int,
3139                       BUILT_IN_FFS, BUILT_IN_NORMAL, 0, 1, 0);
3140   /* Define alloca as builtin, unless SMALL_STACK.  */
3141 #ifndef SMALL_STACK
3142   builtin_function_2 (NULL_PTR, "alloca", NULL_TREE, ptr_ftype_sizetype,
3143                       BUILT_IN_ALLOCA, BUILT_IN_NORMAL, 0, 1, 0);
3144 #endif
3145   /* Declare _exit and _Exit just to mark them as non-returning.  */
3146   builtin_function_2 (NULL_PTR, "_exit", NULL_TREE, void_ftype_int,
3147                       0, NOT_BUILT_IN, 0, 1, 1);
3148   builtin_function_2 (NULL_PTR, "_Exit", NULL_TREE, void_ftype_int,
3149                       0, NOT_BUILT_IN, 0, !flag_isoc99, 1);
3150
3151   builtin_function_2 ("__builtin_index", "index",
3152                       string_ftype_cstring_int, string_ftype_cstring_int,
3153                       BUILT_IN_INDEX, BUILT_IN_NORMAL, 1, 1, 0);
3154   builtin_function_2 ("__builtin_rindex", "rindex",
3155                       string_ftype_cstring_int, string_ftype_cstring_int,
3156                       BUILT_IN_RINDEX, BUILT_IN_NORMAL, 1, 1, 0);
3157
3158   /* The system prototypes for these functions have many
3159      variations, so don't specify parameters to avoid conflicts.
3160      The expand_* functions check the argument types anyway.  */
3161   builtin_function_2 ("__builtin_bzero", "bzero",
3162                       bzero_ftype, void_ftype_any,
3163                       BUILT_IN_BZERO, BUILT_IN_NORMAL, 1, 1, 0);
3164   builtin_function_2 ("__builtin_bcmp", "bcmp",
3165                       bcmp_ftype, int_ftype_any,
3166                       BUILT_IN_BCMP, BUILT_IN_NORMAL, 1, 1, 0);
3167
3168   builtin_function_2 ("__builtin_abs", "abs",
3169                       int_ftype_int, int_ftype_int,
3170                       BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
3171   builtin_function_2 ("__builtin_fabsf", "fabsf",
3172                       float_ftype_float, float_ftype_float,
3173                       BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3174   builtin_function_2 ("__builtin_fabs", "fabs",
3175                       double_ftype_double, double_ftype_double,
3176                       BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3177   builtin_function_2 ("__builtin_fabsl", "fabsl",
3178                       ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3179                       BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3180   builtin_function_2 ("__builtin_labs", "labs",
3181                       long_ftype_long, long_ftype_long,
3182                       BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
3183   builtin_function_2 ("__builtin_llabs", "llabs",
3184                       longlong_ftype_longlong, longlong_ftype_longlong,
3185                       BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3186   builtin_function_2 ("__builtin_imaxabs", "imaxabs",
3187                       intmax_ftype_intmax, intmax_ftype_intmax,
3188                       BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3189
3190   builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
3191                     BUILT_IN_NORMAL, NULL_PTR);
3192   builtin_function ("__builtin_classify_type", default_function_type,
3193                     BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
3194   builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
3195                     BUILT_IN_NORMAL, NULL_PTR);
3196   builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
3197                     BUILT_IN_NORMAL, NULL_PTR);
3198   builtin_function ("__builtin_setjmp",
3199                     build_function_type (integer_type_node,
3200                                          tree_cons (NULL_TREE, ptr_type_node,
3201                                                     endlink)),
3202                     BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
3203   builtin_function ("__builtin_longjmp",
3204                     build_function_type (void_type_node,
3205                                          tree_cons (NULL_TREE, ptr_type_node,
3206                                                     int_endlink)),
3207                     BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
3208   builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3209                     BUILT_IN_NORMAL, NULL_PTR);
3210
3211   /* ISO C99 IEEE Unordered compares.  */
3212   builtin_function ("__builtin_isgreater", default_function_type,
3213                     BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
3214   builtin_function ("__builtin_isgreaterequal", default_function_type,
3215                     BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
3216   builtin_function ("__builtin_isless", default_function_type,
3217                     BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
3218   builtin_function ("__builtin_islessequal", default_function_type,
3219                     BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
3220   builtin_function ("__builtin_islessgreater", default_function_type,
3221                     BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
3222   builtin_function ("__builtin_isunordered", default_function_type,
3223                     BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
3224
3225   /* Untyped call and return.  */
3226   builtin_function ("__builtin_apply_args", ptr_ftype,
3227                     BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
3228
3229   temp = tree_cons (NULL_TREE,
3230                     build_pointer_type (build_function_type (void_type_node,
3231                                                              NULL_TREE)),
3232                     tree_cons (NULL_TREE, ptr_type_node, sizetype_endlink));
3233   builtin_function ("__builtin_apply",
3234                     build_function_type (ptr_type_node, temp),
3235                     BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
3236   builtin_function ("__builtin_return", void_ftype_ptr,
3237                     BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
3238
3239   /* Support for varargs.h and stdarg.h.  */
3240   builtin_function ("__builtin_varargs_start",
3241                     build_function_type (void_type_node,
3242                                          tree_cons (NULL_TREE,
3243                                                     va_list_ref_type_node,
3244                                                     endlink)),
3245                     BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
3246
3247   builtin_function ("__builtin_stdarg_start",
3248                     build_function_type (void_type_node,
3249                                          tree_cons (NULL_TREE,
3250                                                     va_list_ref_type_node,
3251                                                     NULL_TREE)),
3252                     BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
3253
3254   builtin_function ("__builtin_va_end",
3255                     build_function_type (void_type_node,
3256                                          tree_cons (NULL_TREE,
3257                                                     va_list_ref_type_node,
3258                                                     endlink)),
3259                     BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
3260
3261   builtin_function ("__builtin_va_copy",
3262                     build_function_type (void_type_node,
3263                                          tree_cons (NULL_TREE,
3264                                                     va_list_ref_type_node,
3265                                                     tree_cons (NULL_TREE,
3266                                                       va_list_arg_type_node,
3267                                                       endlink))),
3268                     BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
3269
3270   /* ??? Ought to be `T __builtin_expect(T, T)' for any type T.  */
3271   builtin_function ("__builtin_expect",
3272                     build_function_type (long_integer_type_node,
3273                                          tree_cons (NULL_TREE,
3274                                                     long_integer_type_node,
3275                                                     tree_cons (NULL_TREE,
3276                                                         long_integer_type_node,
3277                                                         endlink))),
3278                     BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
3279
3280   /* Currently under experimentation.  */
3281   builtin_function_2 ("__builtin_memcpy", "memcpy",
3282                       memcpy_ftype, memcpy_ftype,
3283                       BUILT_IN_MEMCPY, BUILT_IN_NORMAL, 1, 0, 0);
3284   builtin_function_2 ("__builtin_memcmp", "memcmp",
3285                       int_ftype_cptr_cptr_sizet, int_ftype_cptr_cptr_sizet,
3286                       BUILT_IN_MEMCMP, BUILT_IN_NORMAL, 1, 0, 0);
3287   builtin_function_2 ("__builtin_memset", "memset",
3288                       memset_ftype, memset_ftype,
3289                       BUILT_IN_MEMSET, BUILT_IN_NORMAL, 1, 0, 0);
3290   built_in_decls[BUILT_IN_STRCMP] =
3291     builtin_function_2 ("__builtin_strcmp", "strcmp",
3292                         int_ftype_cstring_cstring, int_ftype_cstring_cstring,
3293                         BUILT_IN_STRCMP, BUILT_IN_NORMAL, 1, 0, 0);
3294   builtin_function_2 ("__builtin_strncmp", "strncmp",
3295                       int_ftype_cstring_cstring_sizet,
3296                       int_ftype_cstring_cstring_sizet,
3297                       BUILT_IN_STRNCMP, BUILT_IN_NORMAL, 1, 0, 0);
3298   builtin_function_2 ("__builtin_strstr", "strstr",
3299                       string_ftype_cstring_cstring, string_ftype_cstring_cstring,
3300                       BUILT_IN_STRSTR, BUILT_IN_NORMAL, 1, 0, 0);
3301   builtin_function_2 ("__builtin_strpbrk", "strpbrk",
3302                       string_ftype_cstring_cstring, string_ftype_cstring_cstring,
3303                       BUILT_IN_STRPBRK, BUILT_IN_NORMAL, 1, 0, 0);
3304   built_in_decls[BUILT_IN_STRCHR] =
3305     builtin_function_2 ("__builtin_strchr", "strchr",
3306                         string_ftype_cstring_int, string_ftype_cstring_int,
3307                         BUILT_IN_STRCHR, BUILT_IN_NORMAL, 1, 0, 0);
3308   builtin_function_2 ("__builtin_strrchr", "strrchr",
3309                       string_ftype_cstring_int, string_ftype_cstring_int,
3310                       BUILT_IN_STRRCHR, BUILT_IN_NORMAL, 1, 0, 0);
3311   builtin_function_2 ("__builtin_strcpy", "strcpy",
3312                       string_ftype_string_cstring, string_ftype_string_cstring,
3313                       BUILT_IN_STRCPY, BUILT_IN_NORMAL, 1, 0, 0);
3314   builtin_function_2 ("__builtin_strncpy", "strncpy",
3315                       string_ftype_string_cstring_sizet,
3316                       string_ftype_string_cstring_sizet,
3317                       BUILT_IN_STRNCPY, BUILT_IN_NORMAL, 1, 0, 0);
3318   built_in_decls[BUILT_IN_STRCAT] =
3319     builtin_function_2 ("__builtin_strcat", "strcat",
3320                         string_ftype_string_cstring,
3321                         string_ftype_string_cstring,
3322                         BUILT_IN_STRCAT, BUILT_IN_NORMAL, 1, 0, 0);
3323   builtin_function_2 ("__builtin_strncat", "strncat",
3324                       string_ftype_string_cstring_sizet,
3325                       string_ftype_string_cstring_sizet,
3326                       BUILT_IN_STRNCAT, BUILT_IN_NORMAL, 1, 0, 0);
3327   builtin_function_2 ("__builtin_strspn", "strspn",
3328                       sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
3329                       BUILT_IN_STRSPN, BUILT_IN_NORMAL, 1, 0, 0);
3330   builtin_function_2 ("__builtin_strcspn", "strcspn",
3331                       sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
3332                       BUILT_IN_STRCSPN, BUILT_IN_NORMAL, 1, 0, 0);
3333   built_in_decls[BUILT_IN_STRLEN] =
3334     builtin_function_2 ("__builtin_strlen", "strlen",
3335                         strlen_ftype, strlen_ftype,
3336                         BUILT_IN_STRLEN, BUILT_IN_NORMAL, 1, 0, 0);
3337
3338   builtin_function_2 ("__builtin_sqrtf", "sqrtf",
3339                       float_ftype_float, float_ftype_float,
3340                       BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3341   builtin_function_2 ("__builtin_fsqrt", "sqrt",
3342                       double_ftype_double, double_ftype_double,
3343                       BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3344   builtin_function_2 ("__builtin_sqrtl", "sqrtl",
3345                       ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3346                       BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3347   builtin_function_2 ("__builtin_sinf", "sinf",
3348                       float_ftype_float, float_ftype_float,
3349                       BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3350   builtin_function_2 ("__builtin_sin", "sin",
3351                       double_ftype_double, double_ftype_double,
3352                       BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3353   builtin_function_2 ("__builtin_sinl", "sinl",
3354                       ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3355                       BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3356   builtin_function_2 ("__builtin_cosf", "cosf",
3357                       float_ftype_float, float_ftype_float,
3358                       BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3359   builtin_function_2 ("__builtin_cos", "cos",
3360                       double_ftype_double, double_ftype_double,
3361                       BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3362   builtin_function_2 ("__builtin_cosl", "cosl",
3363                       ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3364                       BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3365
3366   /* ISO C99 complex arithmetic functions.  */
3367   builtin_function_2 ("__builtin_conjf", "conjf",
3368                       cfloat_ftype_cfloat, cfloat_ftype_cfloat,
3369                       BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3370   builtin_function_2 ("__builtin_conj", "conj",
3371                       cdouble_ftype_cdouble, cdouble_ftype_cdouble,
3372                       BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3373   builtin_function_2 ("__builtin_conjl", "conjl",
3374                       cldouble_ftype_cldouble, cldouble_ftype_cldouble,
3375                       BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3376   builtin_function_2 ("__builtin_crealf", "crealf",
3377                       float_ftype_cfloat, float_ftype_cfloat,
3378                       BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3379   builtin_function_2 ("__builtin_creal", "creal",
3380                       double_ftype_cdouble, double_ftype_cdouble,
3381                       BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3382   builtin_function_2 ("__builtin_creall", "creall",
3383                       ldouble_ftype_cldouble, ldouble_ftype_cldouble,
3384                       BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3385   builtin_function_2 ("__builtin_cimagf", "cimagf",
3386                       float_ftype_cfloat, float_ftype_cfloat,
3387                       BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3388   builtin_function_2 ("__builtin_cimag", "cimag",
3389                       double_ftype_cdouble, double_ftype_cdouble,
3390                       BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3391   builtin_function_2 ("__builtin_cimagl", "cimagl",
3392                       ldouble_ftype_cldouble, ldouble_ftype_cldouble,
3393                       BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3394
3395   built_in_decls[BUILT_IN_PUTCHAR] =
3396     builtin_function ("__builtin_putchar", int_ftype_int,
3397                       BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar");
3398   built_in_decls[BUILT_IN_PUTS] =
3399     builtin_function ("__builtin_puts", puts_ftype,
3400                       BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts");
3401   builtin_function_2 ("__builtin_printf", "printf",
3402                       printf_ftype, printf_ftype,
3403                       BUILT_IN_PRINTF, BUILT_IN_FRONTEND, 1, 0, 0);
3404   builtin_function_2 ("__builtin_fprintf", "fprintf",
3405                       fprintf_ftype, fprintf_ftype,
3406                       BUILT_IN_FPRINTF, BUILT_IN_FRONTEND, 1, 0, 0);
3407   built_in_decls[BUILT_IN_FWRITE] =
3408     builtin_function ("__builtin_fwrite", fwrite_ftype,
3409                       BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite");
3410   built_in_decls[BUILT_IN_FPUTC] =
3411     builtin_function ("__builtin_fputc", fputc_ftype,
3412                       BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
3413   /* Declare the __builtin_ style with arguments and the regular style
3414      without them.  We rely on stdio.h to supply the arguments for the
3415      regular style declaration since we had to use void* instead of
3416      FILE* in the __builtin_ prototype supplied here.  */
3417   built_in_decls[BUILT_IN_FPUTS] =
3418     builtin_function_2 ("__builtin_fputs", "fputs",
3419                         fputs_ftype, int_ftype_any,
3420                         BUILT_IN_FPUTS, BUILT_IN_NORMAL, 1, 0, 0);
3421
3422   /* Declare these functions non-returning
3423      to avoid spurious "control drops through" warnings.  */
3424   builtin_function_2 (NULL_PTR, "abort",
3425                       NULL_TREE, ((c_language == clk_cplusplus)
3426                                   ? void_ftype : void_ftype_any),
3427                       0, NOT_BUILT_IN, 0, 0, 1);
3428
3429   builtin_function_2 (NULL_PTR, "exit",
3430                       NULL_TREE, ((c_language == clk_cplusplus)
3431                                   ? void_ftype_int : void_ftype_any),
3432                       0, NOT_BUILT_IN, 0, 0, 1);
3433
3434 #if 0
3435   /* Support for these has not been written in either expand_builtin
3436      or build_function_call.  */
3437   builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3438                     BUILT_IN_NORMAL, NULL_PTR);
3439   builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3440                     BUILT_IN_NORMAL, NULL_PTR);
3441   builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3442                     BUILT_IN_NORMAL, NULL_PTR);
3443   builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3444                     BUILT_IN_NORMAL, NULL_PTR);
3445   builtin_function ("__builtin_fmod", double_ftype_double_double,
3446                     BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
3447   builtin_function ("__builtin_frem", double_ftype_double_double,
3448                     BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
3449   builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3450                     BUILT_IN_NORMAL, NULL_PTR);
3451   builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3452                     BUILT_IN_NORMAL, NULL_PTR);
3453 #endif
3454
3455   main_identifier_node = get_identifier ("main");
3456
3457   /* ??? Perhaps there's a better place to do this.  But it is related
3458      to __builtin_va_arg, so it isn't that off-the-wall.  */
3459   lang_type_promotes_to = simple_type_promotes_to;
3460 }
3461
3462 tree
3463 build_va_arg (expr, type)
3464      tree expr, type;
3465 {
3466   return build1 (VA_ARG_EXPR, type, expr);
3467 }
3468
3469
3470 /* Possibly define a builtin function with one or two names.  BUILTIN_NAME
3471    is an __builtin_-prefixed name; NAME is the ordinary name; one or both
3472    of these may be NULL (though both being NULL is useless).
3473    BUILTIN_TYPE is the type of the __builtin_-prefixed function;
3474    TYPE is the type of the function with the ordinary name.  These
3475    may differ if the ordinary name is declared with a looser type to avoid
3476    conflicts with headers.  FUNCTION_CODE and CLASS are as for
3477    builtin_function.  If LIBRARY_NAME_P is nonzero, NAME is passed as
3478    the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
3479    If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if
3480    NORETURN_P is nonzero, the function is marked as non-returning.
3481    Returns the declaration of BUILTIN_NAME, if any, otherwise
3482    the declaration of NAME.  Does not declare NAME if flag_no_builtin,
3483    or if NONANSI_P and flag_no_nonansi_builtin.  */
3484
3485 static tree
3486 builtin_function_2 (builtin_name, name, builtin_type, type, function_code,
3487                     class, library_name_p, nonansi_p, noreturn_p)
3488      const char *builtin_name;
3489      const char *name;
3490      tree builtin_type;
3491      tree type;
3492      int function_code;
3493      enum built_in_class class;
3494      int library_name_p;
3495      int nonansi_p;
3496      int noreturn_p;
3497 {
3498   tree bdecl = NULL_TREE;
3499   tree decl = NULL_TREE;
3500   if (builtin_name != 0)
3501     {
3502       bdecl = builtin_function (builtin_name, builtin_type, function_code,
3503                                 class, library_name_p ? name : NULL_PTR);
3504       if (noreturn_p)
3505         {
3506           TREE_THIS_VOLATILE (bdecl) = 1;
3507           TREE_SIDE_EFFECTS (bdecl) = 1;
3508         }
3509     }
3510   if (name != 0 && !flag_no_builtin && !(nonansi_p && flag_no_nonansi_builtin))
3511     {
3512       decl = builtin_function (name, type, function_code, class, NULL_PTR);
3513       if (nonansi_p)
3514         DECL_BUILT_IN_NONANSI (decl) = 1;
3515       if (noreturn_p)
3516         {
3517           TREE_THIS_VOLATILE (decl) = 1;
3518           TREE_SIDE_EFFECTS (decl) = 1;
3519         }
3520     }
3521   return (bdecl != 0 ? bdecl : decl);
3522 }
3523 \f
3524 /* Given a type, apply default promotions wrt unnamed function arguments
3525    and return the new type.  Return NULL_TREE if no change.  */
3526 /* ??? There is a function of the same name in the C++ front end that
3527    does something similar, but is more thorough and does not return NULL
3528    if no change.  We could perhaps share code, but it would make the
3529    self_promoting_type property harder to identify.  */
3530
3531 tree
3532 simple_type_promotes_to (type)
3533      tree type;
3534 {
3535   if (TYPE_MAIN_VARIANT (type) == float_type_node)
3536     return double_type_node;
3537
3538   if (C_PROMOTING_INTEGER_TYPE_P (type))
3539     {
3540       /* Traditionally, unsignedness is preserved in default promotions.
3541          Also preserve unsignedness if not really getting any wider.  */
3542       if (TREE_UNSIGNED (type)
3543           && (flag_traditional
3544               || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3545         return unsigned_type_node;
3546       return integer_type_node;
3547     }
3548
3549   return NULL_TREE;
3550 }
3551
3552 /* Return 1 if PARMS specifies a fixed number of parameters
3553    and none of their types is affected by default promotions.  */
3554
3555 int
3556 self_promoting_args_p (parms)
3557      tree parms;
3558 {
3559   register tree t;
3560   for (t = parms; t; t = TREE_CHAIN (t))
3561     {
3562       register tree type = TREE_VALUE (t);
3563
3564       if (TREE_CHAIN (t) == 0 && type != void_type_node)
3565         return 0;
3566
3567       if (type == 0)
3568         return 0;
3569
3570       if (TYPE_MAIN_VARIANT (type) == float_type_node)
3571         return 0;
3572
3573       if (C_PROMOTING_INTEGER_TYPE_P (type))
3574         return 0;
3575     }
3576   return 1;
3577 }
3578
3579 /* Recursively examines the array elements of TYPE, until a non-array
3580    element type is found.  */
3581
3582 tree
3583 strip_array_types (type)
3584      tree type;
3585 {
3586   while (TREE_CODE (type) == ARRAY_TYPE)
3587     type = TREE_TYPE (type);
3588
3589   return type;
3590 }
3591
3592 /* Recognize certain built-in functions so we can make tree-codes
3593    other than CALL_EXPR.  We do this when it enables fold-const.c
3594    to do something useful.  */
3595 /* ??? By rights this should go in builtins.c, but only C and C++
3596    implement build_{binary,unary}_op.  Not exactly sure what bits
3597    of functionality are actually needed from those functions, or
3598    where the similar functionality exists in the other front ends.  */
3599
3600 tree
3601 expand_tree_builtin (function, params, coerced_params)
3602      tree function, params, coerced_params;
3603 {
3604   enum tree_code code;
3605
3606   if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3607     return NULL_TREE;
3608
3609   switch (DECL_FUNCTION_CODE (function))
3610     {
3611     case BUILT_IN_ABS:
3612     case BUILT_IN_FABS:
3613       if (coerced_params == 0)
3614         return integer_zero_node;
3615       return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3616
3617     case BUILT_IN_CONJ:
3618       if (coerced_params == 0)
3619         return integer_zero_node;
3620       return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0);
3621
3622     case BUILT_IN_CREAL:
3623       if (coerced_params == 0)
3624         return integer_zero_node;
3625       return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0);
3626
3627     case BUILT_IN_CIMAG:
3628       if (coerced_params == 0)
3629         return integer_zero_node;
3630       return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0);
3631
3632     case BUILT_IN_ISGREATER:
3633       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3634         code = UNLE_EXPR;
3635       else
3636         code = LE_EXPR;
3637       goto unordered_cmp;
3638
3639     case BUILT_IN_ISGREATEREQUAL:
3640       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3641         code = UNLT_EXPR;
3642       else
3643         code = LT_EXPR;
3644       goto unordered_cmp;
3645
3646     case BUILT_IN_ISLESS:
3647       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3648         code = UNGE_EXPR;
3649       else
3650         code = GE_EXPR;
3651       goto unordered_cmp;
3652
3653     case BUILT_IN_ISLESSEQUAL:
3654       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3655         code = UNGT_EXPR;
3656       else
3657         code = GT_EXPR;
3658       goto unordered_cmp;
3659
3660     case BUILT_IN_ISLESSGREATER:
3661       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3662         code = UNEQ_EXPR;
3663       else
3664         code = EQ_EXPR;
3665       goto unordered_cmp;
3666
3667     case BUILT_IN_ISUNORDERED:
3668       if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
3669         return integer_zero_node;
3670       code = UNORDERED_EXPR;
3671       goto unordered_cmp;
3672
3673     unordered_cmp:
3674       {
3675         tree arg0, arg1;
3676
3677         if (params == 0
3678             || TREE_CHAIN (params) == 0)
3679           {
3680             error ("too few arguments to function `%s'",
3681                    IDENTIFIER_POINTER (DECL_NAME (function)));
3682             return error_mark_node;
3683           }
3684         else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
3685           {
3686             error ("too many arguments to function `%s'",
3687                    IDENTIFIER_POINTER (DECL_NAME (function)));
3688             return error_mark_node;
3689           }
3690
3691         arg0 = TREE_VALUE (params);
3692         arg1 = TREE_VALUE (TREE_CHAIN (params));
3693         arg0 = build_binary_op (code, arg0, arg1, 0);
3694         if (code != UNORDERED_EXPR)
3695           arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
3696         return arg0;
3697       }
3698       break;
3699
3700     default:
3701       break;
3702     }
3703
3704   return NULL_TREE;
3705 }
3706
3707 /* Returns non-zero if CODE is the code for a statement.  */
3708
3709 int
3710 statement_code_p (code)
3711      enum tree_code code;
3712 {
3713   switch (code)
3714     {
3715     case EXPR_STMT:
3716     case COMPOUND_STMT:
3717     case DECL_STMT:
3718     case IF_STMT:
3719     case FOR_STMT:
3720     case WHILE_STMT:
3721     case DO_STMT:
3722     case RETURN_STMT:
3723     case BREAK_STMT:
3724     case CONTINUE_STMT:
3725     case SCOPE_STMT:
3726     case SWITCH_STMT:
3727     case GOTO_STMT:
3728     case LABEL_STMT:
3729     case ASM_STMT:
3730     case CASE_LABEL:
3731       return 1;
3732
3733     default:
3734       if (lang_statement_code_p)
3735         return (*lang_statement_code_p) (code);
3736       return 0;
3737     }
3738 }
3739
3740 /* Walk the statemen tree, rooted at *tp.  Apply FUNC to all the
3741    sub-trees of *TP in a pre-order traversal.  FUNC is called with the
3742    DATA and the address of each sub-tree.  If FUNC returns a non-NULL
3743    value, the traversal is aborted, and the value returned by FUNC is
3744    returned.  If FUNC sets WALK_SUBTREES to zero, then the subtrees of
3745    the node being visited are not walked.
3746
3747    We don't need a without_duplicates variant of this one because the
3748    statement tree is a tree, not a graph.  */
3749
3750 tree 
3751 walk_stmt_tree (tp, func, data)
3752      tree *tp;
3753      walk_tree_fn func;
3754      void *data;
3755 {
3756   enum tree_code code;
3757   int walk_subtrees;
3758   tree result;
3759   int i, len;
3760
3761 #define WALK_SUBTREE(NODE)                              \
3762   do                                                    \
3763     {                                                   \
3764       result = walk_stmt_tree (&(NODE), func, data);    \
3765       if (result)                                       \
3766         return result;                                  \
3767     }                                                   \
3768   while (0)
3769
3770   /* Skip empty subtrees.  */
3771   if (!*tp)
3772     return NULL_TREE;
3773
3774   /* Skip subtrees below non-statement nodes.  */
3775   if (!statement_code_p (TREE_CODE (*tp)))
3776     return NULL_TREE;
3777
3778   /* Call the function.  */
3779   walk_subtrees = 1;
3780   result = (*func) (tp, &walk_subtrees, data);
3781
3782   /* If we found something, return it.  */
3783   if (result)
3784     return result;
3785
3786   /* Even if we didn't, FUNC may have decided that there was nothing
3787      interesting below this point in the tree.  */
3788   if (!walk_subtrees)
3789     return NULL_TREE;
3790
3791   /* FUNC may have modified the tree, recheck that we're looking at a
3792      statement node.  */
3793   code = TREE_CODE (*tp);
3794   if (!statement_code_p (code))
3795     return NULL_TREE;
3796
3797   /* Walk over all the sub-trees of this operand.  Statement nodes never
3798      contain RTL, and we needn't worry about TARGET_EXPRs.  */
3799   len = TREE_CODE_LENGTH (code);
3800
3801   /* Go through the subtrees.  We need to do this in forward order so
3802      that the scope of a FOR_EXPR is handled properly.  */
3803   for (i = 0; i < len; ++i)
3804     WALK_SUBTREE (TREE_OPERAND (*tp, i));
3805
3806   /* Finally visit the chain.  This can be tail-recursion optimized if
3807      we write it this way.  */
3808   return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
3809
3810 #undef WALK_SUBTREE
3811 }
3812
3813 /* Used to compare case labels.  K1 and K2 are actually tree nodes
3814    representing case labels, or NULL_TREE for a `default' label.
3815    Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
3816    K2, and 0 if K1 and K2 are equal.  */
3817
3818 int
3819 case_compare (k1, k2)
3820      splay_tree_key k1;
3821      splay_tree_key k2;
3822 {
3823   /* Consider a NULL key (such as arises with a `default' label) to be
3824      smaller than anything else.  */
3825   if (!k1)
3826     return k2 ? -1 : 0;
3827   else if (!k2)
3828     return k1 ? 1 : 0;
3829
3830   return tree_int_cst_compare ((tree) k1, (tree) k2);
3831 }
3832
3833 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE.  If
3834    LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
3835    actually a `default' label.  If only HIGH_VALUE is NULL_TREE, then
3836    case label was declared using the usual C/C++ syntax, rather than
3837    the GNU case range extension.  CASES is a tree containing all the
3838    case ranges processed so far; COND is the condition for the
3839    switch-statement itself.  Returns the CASE_LABEL created, or
3840    ERROR_MARK_NODE if no CASE_LABEL is created.  */
3841
3842 tree
3843 c_add_case_label (cases, cond, low_value, high_value)
3844      splay_tree cases;
3845      tree cond;
3846      tree low_value;
3847      tree high_value;
3848 {
3849   tree type;
3850   tree label;
3851   tree case_label;
3852   splay_tree_node node;
3853
3854   /* Create the LABEL_DECL itself.  */
3855   label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3856   DECL_CONTEXT (label) = current_function_decl;
3857
3858   /* If there was an error processing the switch condition, bail now
3859      before we get more confused.  */
3860   if (!cond || cond == error_mark_node)
3861     {
3862       /* Add a label anyhow so that the back-end doesn't think that
3863          the beginning of the switch is unreachable.  */
3864       if (!cases->root)
3865         add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3866       return error_mark_node;
3867     }
3868
3869   if ((low_value && TREE_TYPE (low_value) 
3870        && POINTER_TYPE_P (TREE_TYPE (low_value))) 
3871       || (high_value && TREE_TYPE (high_value)
3872           && POINTER_TYPE_P (TREE_TYPE (high_value))))
3873     error ("pointers are not permitted as case values");
3874
3875   /* Case ranges are a GNU extension.  */
3876   if (high_value && pedantic)
3877     {
3878       if (c_language == clk_cplusplus)
3879         pedwarn ("ISO C++ forbids range expressions in switch statements");
3880       else
3881         pedwarn ("ISO C forbids range expressions in switch statements");
3882     }
3883
3884   type = TREE_TYPE (cond);
3885   if (low_value)
3886     {
3887       low_value = check_case_value (low_value);
3888       low_value = convert_and_check (type, low_value);
3889     }
3890   if (high_value)
3891     {
3892       high_value = check_case_value (high_value);
3893       high_value = convert_and_check (type, high_value);
3894     }
3895
3896   /* If an error has occurred, bail out now.  */
3897   if (low_value == error_mark_node || high_value == error_mark_node)
3898     {
3899       if (!cases->root)
3900         add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3901       return error_mark_node;
3902     }
3903
3904   /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
3905      really a case range, even though it was written that way.  Remove
3906      the HIGH_VALUE to simplify later processing.  */
3907   if (tree_int_cst_equal (low_value, high_value))
3908     high_value = NULL_TREE;
3909   if (low_value && high_value 
3910       && !tree_int_cst_lt (low_value, high_value)) 
3911     warning ("empty range specified");
3912
3913   /* Look up the LOW_VALUE in the table of case labels we already
3914      have.  */
3915   node = splay_tree_lookup (cases, (splay_tree_key) low_value);
3916   /* If there was not an exact match, check for overlapping ranges.
3917      There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
3918      that's a `default' label and the only overlap is an exact match.  */
3919   if (!node && (low_value || high_value))
3920     {
3921       splay_tree_node low_bound;
3922       splay_tree_node high_bound;
3923
3924       /* Even though there wasn't an exact match, there might be an
3925          overlap between this case range and another case range.
3926          Since we've (inductively) not allowed any overlapping case
3927          ranges, we simply need to find the greatest low case label
3928          that is smaller that LOW_VALUE, and the smallest low case
3929          label that is greater than LOW_VALUE.  If there is an overlap
3930          it will occur in one of these two ranges.  */
3931       low_bound = splay_tree_predecessor (cases,
3932                                           (splay_tree_key) low_value);
3933       high_bound = splay_tree_successor (cases,
3934                                          (splay_tree_key) low_value);
3935
3936       /* Check to see if the LOW_BOUND overlaps.  It is smaller than
3937          the LOW_VALUE, so there is no need to check unless the
3938          LOW_BOUND is in fact itself a case range.  */
3939       if (low_bound
3940           && CASE_HIGH ((tree) low_bound->value)
3941           && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
3942                                     low_value) >= 0)
3943         node = low_bound;
3944       /* Check to see if the HIGH_BOUND overlaps.  The low end of that
3945          range is bigger than the low end of the current range, so we
3946          are only interested if the current range is a real range, and
3947          not an ordinary case label.  */
3948       else if (high_bound 
3949                && high_value
3950                && (tree_int_cst_compare ((tree) high_bound->key,
3951                                          high_value)
3952                    <= 0))
3953         node = high_bound;
3954     }
3955   /* If there was an overlap, issue an error.  */
3956   if (node)
3957     {
3958       tree duplicate = CASE_LABEL_DECL ((tree) node->value);
3959
3960       if (high_value)
3961         {
3962           error ("duplicate (or overlapping) case value");
3963           error_with_decl (duplicate, 
3964                            "this is the first entry overlapping that value");
3965         }
3966       else if (low_value)
3967         {
3968           error ("duplicate case value") ;
3969           error_with_decl (duplicate, "previously used here");
3970         }
3971       else
3972         {
3973           error ("multiple default labels in one switch");
3974           error_with_decl (duplicate, "this is the first default label");
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 /* Mark P (a stmt_tree) for GC.  The use of a `void *' for the
3991    parameter allows this function to be used as a GC-marking
3992    function.  */
3993
3994 void
3995 mark_stmt_tree (p)
3996      void *p;
3997 {
3998   stmt_tree st = (stmt_tree) p;
3999
4000   ggc_mark_tree (st->x_last_stmt);
4001   ggc_mark_tree (st->x_last_expr_type);
4002 }
4003
4004 /* Mark LD for GC.  */
4005
4006 void
4007 c_mark_lang_decl (c)
4008      struct c_lang_decl *c;
4009 {
4010   ggc_mark_tree (c->saved_tree);
4011 }
4012
4013 /* Mark F for GC.  */
4014
4015 void
4016 mark_c_language_function (f)
4017      struct language_function *f;
4018 {
4019   if (!f)
4020     return;
4021
4022   mark_stmt_tree (&f->x_stmt_tree);
4023   ggc_mark_tree (f->x_scope_stmt_stack);
4024 }
4025
4026 /* Hook used by expand_expr to expand language-specific tree codes.  */
4027
4028 rtx
4029 c_expand_expr (exp, target, tmode, modifier)
4030      tree exp;
4031      rtx target;
4032      enum machine_mode tmode;
4033      enum expand_modifier modifier;
4034 {
4035   switch (TREE_CODE (exp))
4036     {
4037     case STMT_EXPR:
4038       {
4039         tree rtl_expr;
4040         rtx result;
4041
4042         /* Since expand_expr_stmt calls free_temp_slots after every
4043            expression statement, we must call push_temp_slots here.
4044            Otherwise, any temporaries in use now would be considered
4045            out-of-scope after the first EXPR_STMT from within the
4046            STMT_EXPR.  */
4047         push_temp_slots ();
4048         rtl_expr = expand_start_stmt_expr ();
4049         expand_stmt (STMT_EXPR_STMT (exp));
4050         expand_end_stmt_expr (rtl_expr);
4051         result = expand_expr (rtl_expr, target, tmode, modifier);
4052         pop_temp_slots ();
4053         return result;
4054       }
4055       break;
4056       
4057     case CALL_EXPR:
4058       {
4059         if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
4060             && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4061                 == FUNCTION_DECL)
4062             && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4063             && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4064                 == BUILT_IN_FRONTEND))
4065           return c_expand_builtin (exp, target, tmode, modifier);
4066         else
4067           abort();
4068       }
4069       break;
4070
4071     default:
4072       abort ();
4073     }
4074
4075   abort ();
4076   return NULL;
4077 }
4078
4079 /* Hook used by safe_from_p to handle language-specific tree codes.  */
4080
4081 int
4082 c_safe_from_p (target, exp)
4083      rtx target;
4084      tree exp;
4085 {
4086   /* We can see statements here when processing the body of a
4087      statement-expression.  For a declaration statement declaring a
4088      variable, look at the variable's initializer.  */
4089   if (TREE_CODE (exp) == DECL_STMT) 
4090     {
4091       tree decl = DECL_STMT_DECL (exp);
4092
4093       if (TREE_CODE (decl) == VAR_DECL
4094           && DECL_INITIAL (decl)
4095           && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
4096         return 0;
4097     }
4098
4099   /* For any statement, we must follow the statement-chain.  */
4100   if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
4101     return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
4102
4103   /* Assume everything else is safe.  */
4104   return 1;
4105 }
4106
4107 /* Hook used by unsafe_for_reeval to handle language-specific tree codes.  */
4108
4109 int
4110 c_unsafe_for_reeval (exp)
4111      tree exp;
4112 {
4113   /* Statement expressions may not be reevaluated.  */
4114   if (TREE_CODE (exp) == STMT_EXPR)
4115     return 2;
4116
4117   /* Walk all other expressions.  */
4118   return -1;
4119 }
4120
4121 /* Tree code classes. */
4122
4123 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
4124
4125 static char c_tree_code_type[] = {
4126   'x',
4127 #include "c-common.def"
4128 };
4129 #undef DEFTREECODE
4130
4131 /* Table indexed by tree code giving number of expression
4132    operands beyond the fixed part of the node structure.
4133    Not used for types or decls.  */
4134
4135 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
4136
4137 static int c_tree_code_length[] = {
4138   0,
4139 #include "c-common.def"
4140 };
4141 #undef DEFTREECODE
4142
4143 /* Names of tree components.
4144    Used for printing out the tree and error messages.  */
4145 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
4146
4147 static const char *c_tree_code_name[] = {
4148   "@@dummy",
4149 #include "c-common.def"
4150 };
4151 #undef DEFTREECODE
4152
4153 /* Adds the tree codes specific to the C front end to the list of all
4154    tree codes. */
4155
4156 void
4157 add_c_tree_codes ()
4158 {
4159   memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
4160           c_tree_code_type,
4161           (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
4162   memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
4163           c_tree_code_length,
4164           (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
4165   memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
4166           c_tree_code_name,
4167           (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
4168   lang_unsafe_for_reeval = c_unsafe_for_reeval;
4169 }
4170
4171 #define CALLED_AS_BUILT_IN(NODE) \
4172    (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
4173
4174 static rtx
4175 c_expand_builtin (exp, target, tmode, modifier)
4176      tree exp;
4177      rtx target;
4178      enum machine_mode tmode;
4179      enum expand_modifier modifier;
4180 {
4181   tree type = TREE_TYPE (exp);
4182   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4183   tree arglist = TREE_OPERAND (exp, 1);
4184   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4185   enum tree_code code = TREE_CODE (exp);
4186   const int ignore = (target == const0_rtx
4187                       || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
4188                            || code == CONVERT_EXPR || code == REFERENCE_EXPR
4189                            || code == COND_EXPR)
4190                           && TREE_CODE (type) == VOID_TYPE));
4191
4192   if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
4193     return expand_call (exp, target, ignore);
4194
4195   switch (fcode)
4196     {
4197     case BUILT_IN_PRINTF:
4198       target = c_expand_builtin_printf (arglist, target, tmode,
4199                                         modifier, ignore);
4200       if (target)
4201         return target;
4202       break;
4203
4204     case BUILT_IN_FPRINTF:
4205       target = c_expand_builtin_fprintf (arglist, target, tmode,
4206                                          modifier, ignore);
4207       if (target)
4208         return target;
4209       break;
4210
4211     default:                    /* just do library call, if unknown builtin */
4212       error ("built-in function `%s' not currently supported",
4213              IDENTIFIER_POINTER (DECL_NAME (fndecl)));
4214     }
4215
4216   /* The switch statement above can drop through to cause the function
4217      to be called normally.  */
4218   return expand_call (exp, target, ignore);
4219 }
4220
4221 /* Check an arglist to *printf for problems.  The arglist should start
4222    at the format specifier, with the remaining arguments immediately
4223    following it. */
4224 static int
4225 is_valid_printf_arglist (arglist)
4226   tree arglist;
4227 {
4228   /* Save this value so we can restore it later. */
4229   const int SAVE_pedantic = pedantic;
4230   int diagnostic_occurred = 0;
4231
4232   /* Set this to a known value so the user setting won't affect code
4233      generation.  */
4234   pedantic = 1;
4235   /* Check to make sure there are no format specifier errors. */
4236   check_function_format (&diagnostic_occurred,
4237                          maybe_get_identifier("printf"),
4238                          NULL_TREE, arglist);
4239
4240   /* Restore the value of `pedantic'. */
4241   pedantic = SAVE_pedantic;
4242
4243   /* If calling `check_function_format_ptr' produces a warning, we
4244      return false, otherwise we return true. */
4245   return ! diagnostic_occurred;
4246 }
4247
4248 /* If the arguments passed to printf are suitable for optimizations,
4249    we attempt to transform the call. */
4250 static rtx
4251 c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
4252      tree arglist;
4253      rtx target;
4254      enum machine_mode tmode;
4255      enum expand_modifier modifier;
4256      int ignore;
4257 {
4258   tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
4259     fn_puts = built_in_decls[BUILT_IN_PUTS];
4260   tree fn, format_arg, stripped_string;
4261
4262   /* If the return value is used, or the replacement _DECL isn't
4263      initialized, don't do the transformation. */
4264   if (!ignore || !fn_putchar || !fn_puts)
4265     return 0;
4266
4267   /* Verify the required arguments in the original call. */
4268   if (arglist == 0
4269       || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
4270     return 0;
4271   
4272   /* Check the specifier vs. the parameters. */
4273   if (!is_valid_printf_arglist (arglist))
4274     return 0;
4275   
4276   format_arg = TREE_VALUE (arglist);
4277   stripped_string = format_arg;
4278   STRIP_NOPS (stripped_string);
4279   if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4280     stripped_string = TREE_OPERAND (stripped_string, 0);
4281
4282   /* If the format specifier isn't a STRING_CST, punt.  */
4283   if (TREE_CODE (stripped_string) != STRING_CST)
4284     return 0;
4285   
4286   /* OK!  We can attempt optimization.  */
4287
4288   /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
4289   if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
4290     {
4291       arglist = TREE_CHAIN (arglist);
4292       fn = fn_puts;
4293     }
4294   /* If the format specifier was "%c", call __builtin_putchar (arg2). */
4295   else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4296     {
4297       arglist = TREE_CHAIN (arglist);
4298       fn = fn_putchar;
4299     }
4300   else
4301     {
4302      /* We can't handle anything else with % args or %% ... yet. */
4303       if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4304         return 0;
4305       
4306       /* If the resulting constant string has a length of 1, call
4307          putchar.  Note, TREE_STRING_LENGTH includes the terminating
4308          NULL in its count.  */
4309       if (TREE_STRING_LENGTH (stripped_string) == 2)
4310         {
4311           /* Given printf("c"), (where c is any one character,)
4312              convert "c"[0] to an int and pass that to the replacement
4313              function. */
4314           arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
4315           arglist = build_tree_list (NULL_TREE, arglist);
4316           
4317           fn = fn_putchar;
4318         }
4319       /* If the resulting constant was "string\n", call
4320          __builtin_puts("string").  Ensure "string" has at least one
4321          character besides the trailing \n.  Note, TREE_STRING_LENGTH
4322          includes the terminating NULL in its count.  */
4323       else if (TREE_STRING_LENGTH (stripped_string) > 2
4324                && TREE_STRING_POINTER (stripped_string)
4325                [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
4326         {
4327           /* Create a NULL-terminated string that's one char shorter
4328              than the original, stripping off the trailing '\n'.  */
4329           const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
4330           char *newstr = (char *) alloca (newlen);
4331           memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
4332           newstr[newlen - 1] = 0;
4333           
4334           arglist = combine_strings (build_string (newlen, newstr));
4335           arglist = build_tree_list (NULL_TREE, arglist);
4336           fn = fn_puts;
4337         }
4338       else
4339         /* We'd like to arrange to call fputs(string) here, but we
4340            need stdout and don't have a way to get it ... yet.  */
4341         return 0;
4342     }
4343   
4344   return expand_expr (build_function_call (fn, arglist),
4345                       (ignore ? const0_rtx : target),
4346                       tmode, modifier);
4347 }
4348
4349 /* If the arguments passed to fprintf are suitable for optimizations,
4350    we attempt to transform the call. */
4351 static rtx
4352 c_expand_builtin_fprintf (arglist, target, tmode, modifier, ignore)
4353      tree arglist;
4354      rtx target;
4355      enum machine_mode tmode;
4356      enum expand_modifier modifier;
4357      int ignore;
4358 {
4359   tree fn_fputc = built_in_decls[BUILT_IN_FPUTC],
4360     fn_fputs = built_in_decls[BUILT_IN_FPUTS];
4361   tree fn, format_arg, stripped_string;
4362
4363   /* If the return value is used, or the replacement _DECL isn't
4364      initialized, don't do the transformation. */
4365   if (!ignore || !fn_fputc || !fn_fputs)
4366     return 0;
4367
4368   /* Verify the required arguments in the original call. */
4369   if (arglist == 0
4370       || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
4371       || (TREE_CHAIN (arglist) == 0)
4372       || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) !=
4373           POINTER_TYPE))
4374     return 0;
4375   
4376   /* Check the specifier vs. the parameters. */
4377   if (!is_valid_printf_arglist (TREE_CHAIN (arglist)))
4378     return 0;
4379   
4380   format_arg = TREE_VALUE (TREE_CHAIN (arglist));
4381   stripped_string = format_arg;
4382   STRIP_NOPS (stripped_string);
4383   if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4384     stripped_string = TREE_OPERAND (stripped_string, 0);
4385
4386   /* If the format specifier isn't a STRING_CST, punt.  */
4387   if (TREE_CODE (stripped_string) != STRING_CST)
4388     return 0;
4389   
4390   /* OK!  We can attempt optimization.  */
4391
4392   /* If the format specifier was "%s", call __builtin_fputs(arg3, arg1). */
4393   if (strcmp (TREE_STRING_POINTER (stripped_string), "%s") == 0)
4394     {
4395       tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4396       arglist = tree_cons (NULL_TREE,
4397                            TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4398                            newarglist);
4399       fn = fn_fputs;
4400     }
4401   /* If the format specifier was "%c", call __builtin_fputc (arg3, arg1). */
4402   else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4403     {
4404       tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4405       arglist = tree_cons (NULL_TREE,
4406                            TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4407                            newarglist);
4408       fn = fn_fputc;
4409     }
4410   else
4411     {
4412      /* We can't handle anything else with % args or %% ... yet. */
4413       if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4414         return 0;
4415       
4416       /* When "string" doesn't contain %, replace all cases of
4417          fprintf(stream,string) with fputs(string,stream).  The fputs
4418          builtin will take take of special cases like length==1.  */
4419       arglist = tree_cons (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)),
4420                            build_tree_list (NULL_TREE, TREE_VALUE (arglist)));
4421       fn = fn_fputs;
4422     }
4423   
4424   return expand_expr (build_function_call (fn, arglist),
4425                       (ignore ? const0_rtx : target),
4426                       tmode, modifier);
4427 }
4428 \f
4429
4430 /* Given a boolean expression ARG, return a tree representing an increment
4431    or decrement (as indicated by CODE) of ARG.  The front end must check for
4432    invalid cases (e.g., decrement in C++).  */
4433 tree
4434 boolean_increment (code, arg)
4435      enum tree_code code;
4436      tree arg;
4437 {
4438   tree val;
4439   tree true_res = (c_language == clk_cplusplus
4440                    ? boolean_true_node
4441                    : c_bool_true_node);
4442   arg = stabilize_reference (arg);
4443   switch (code)
4444     {
4445     case PREINCREMENT_EXPR:
4446       val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4447       break;
4448     case POSTINCREMENT_EXPR:
4449       val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4450       arg = save_expr (arg);
4451       val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4452       val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4453       break;
4454     case PREDECREMENT_EXPR:
4455       val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4456       break;
4457     case POSTDECREMENT_EXPR:
4458       val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4459       arg = save_expr (arg);
4460       val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4461       val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4462       break;
4463     default:
4464       abort ();
4465     }
4466   TREE_SIDE_EFFECTS (val) = 1;
4467   return val;
4468 }
4469 \f
4470
4471 /* Do the parts of lang_init common to C and C++.  */
4472 void
4473 c_common_lang_init ()
4474 {
4475   /* If still "unspecified", make it match -fbounded-pointers.  */
4476   if (flag_bounds_check < 0)
4477     flag_bounds_check = flag_bounded_pointers;
4478
4479   /* Special format checking options don't work without -Wformat; warn if
4480      they are used.  */
4481   if (warn_format_y2k && !warn_format)
4482     warning ("-Wformat-y2k ignored without -Wformat");
4483   if (warn_format_extra_args && !warn_format)
4484     warning ("-Wformat-extra-args ignored without -Wformat");
4485   if (warn_format_nonliteral && !warn_format)
4486     warning ("-Wformat-nonliteral ignored without -Wformat");
4487   if (warn_format_security && !warn_format)
4488     warning ("-Wformat-security ignored without -Wformat");
4489   if (warn_missing_format_attribute && !warn_format)
4490     warning ("-Wmissing-format-attribute ignored without -Wformat");
4491 }