Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / gcc / c-common.c
1 /* Subroutines shared by all languages that are variants of C.
2    Copyright (C) 1992, 93-98, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 /* $FreeBSD: src/contrib/gcc/c-common.c,v 1.8.2.5 2002/06/20 23:12:24 obrien Exp $ */
22
23 #include "config.h"
24 #include "system.h"
25 #include "tree.h"
26 #include "c-lex.h"
27 #include "c-tree.h"
28 #include "flags.h"
29 #include "obstack.h"
30 #include "toplev.h"
31 #include "output.h"
32 #include "c-pragma.h"
33 #include "rtl.h"
34
35 #if USE_CPPLIB
36 #include "cpplib.h"
37 cpp_reader  parse_in;
38 cpp_options parse_options;
39 static enum cpp_token cpp_token;
40 #endif
41
42 #ifndef WCHAR_TYPE_SIZE
43 #ifdef INT_TYPE_SIZE
44 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
45 #else
46 #define WCHAR_TYPE_SIZE BITS_PER_WORD
47 #endif
48 #endif
49
50 extern struct obstack permanent_obstack;
51
52 /* Nonzero means the expression being parsed will never be evaluated.
53    This is a count, since unevaluated expressions can nest.  */
54 int skip_evaluation;
55
56 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
57             A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
58             A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
59             A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
60
61 enum format_type { printf_format_type, scanf_format_type,
62                    strftime_format_type };
63
64 static void declare_hidden_char_array   PROTO((const char *, const char *));
65 static void add_attribute               PROTO((enum attrs, const char *,
66                                                int, int, int));
67 static void init_attributes             PROTO((void));
68 static void record_function_format      PROTO((tree, tree, enum format_type,
69                                                int, int, int));
70 static void record_international_format PROTO((tree, tree, int));
71 static tree c_find_base_decl            PROTO((tree));
72 static int default_valid_lang_attribute PROTO ((tree, tree, tree, tree));
73
74 /* Keep a stack of if statements.  We record the number of compound
75    statements seen up to the if keyword, as well as the line number
76    and file of the if.  If a potentially ambiguous else is seen, that
77    fact is recorded; the warning is issued when we can be sure that
78    the enclosing if statement does not have an else branch.  */
79 typedef struct
80 {
81   int compstmt_count;
82   int line;
83   const char *file;
84   int needs_warning;
85 } if_elt;
86 static void tfaff                       PROTO((void));
87
88 static if_elt *if_stack;
89
90 /* Amount of space in the if statement stack.  */
91 static int if_stack_space = 0;
92
93 /* Stack pointer.  */
94 static int if_stack_pointer = 0;
95
96 /* Generate RTL for the start of an if-then, and record the start of it
97    for ambiguous else detection.  */
98
99 void
100 c_expand_start_cond (cond, exitflag, compstmt_count)
101      tree cond;
102      int exitflag;
103      int compstmt_count;
104 {
105   /* Make sure there is enough space on the stack.  */
106   if (if_stack_space == 0)
107     {
108       if_stack_space = 10;
109       if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
110     }
111   else if (if_stack_space == if_stack_pointer)
112     {
113       if_stack_space += 10;
114       if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
115     }
116
117   /* Record this if statement.  */
118   if_stack[if_stack_pointer].compstmt_count = compstmt_count;
119   if_stack[if_stack_pointer].file = input_filename;
120   if_stack[if_stack_pointer].line = lineno;
121   if_stack[if_stack_pointer].needs_warning = 0;
122   if_stack_pointer++;
123
124   expand_start_cond (cond, exitflag);
125 }
126
127 /* Generate RTL for the end of an if-then.  Optionally warn if a nested
128    if statement had an ambiguous else clause.  */
129
130 void
131 c_expand_end_cond ()
132 {
133   if_stack_pointer--;
134   if (if_stack[if_stack_pointer].needs_warning)
135     warning_with_file_and_line (if_stack[if_stack_pointer].file,
136                                 if_stack[if_stack_pointer].line,
137                                 "suggest explicit braces to avoid ambiguous `else'");
138   expand_end_cond ();
139 }
140
141 /* Generate RTL between the then-clause and the else-clause
142    of an if-then-else.  */
143
144 void
145 c_expand_start_else ()
146 {
147   /* An ambiguous else warning must be generated for the enclosing if
148      statement, unless we see an else branch for that one, too.  */
149   if (warn_parentheses
150       && if_stack_pointer > 1
151       && (if_stack[if_stack_pointer - 1].compstmt_count
152           == if_stack[if_stack_pointer - 2].compstmt_count))
153     if_stack[if_stack_pointer - 2].needs_warning = 1;
154
155   /* Even if a nested if statement had an else branch, it can't be
156      ambiguous if this one also has an else.  So don't warn in that
157      case.  Also don't warn for any if statements nested in this else.  */
158   if_stack[if_stack_pointer - 1].needs_warning = 0;
159   if_stack[if_stack_pointer - 1].compstmt_count--;
160
161   expand_start_else ();
162 }
163
164 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__.  */
165
166 void
167 declare_function_name ()
168 {
169   const char *name, *printable_name;
170
171   if (current_function_decl == NULL)
172     {
173       name = "";
174       printable_name = "top level";
175     }
176   else
177     {
178       /* Allow functions to be nameless (such as artificial ones).  */
179       if (DECL_NAME (current_function_decl))
180         name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
181       else
182         name = "";
183       printable_name = (*decl_printable_name) (current_function_decl, 2);
184     }
185
186   declare_hidden_char_array ("__FUNCTION__", name);
187   declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
188   /* The ISO C people "of course" couldn't use __FUNCTION__ in the
189      ISO C 9x standard; instead a new variable is invented.  */
190   declare_hidden_char_array ("__func__", name);
191 }
192
193 static void
194 declare_hidden_char_array (name, value)
195      const char *name, *value;
196 {
197   tree decl, type, init;
198   int vlen;
199
200   /* If the default size of char arrays isn't big enough for the name,
201      or if we want to give warnings for large objects, make a bigger one.  */
202   vlen = strlen (value) + 1;
203   type = char_array_type_node;
204   if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
205       || warn_larger_than)
206     type = build_array_type (char_type_node,
207                              build_index_type (build_int_2 (vlen, 0)));
208   push_obstacks_nochange ();
209   decl = build_decl (VAR_DECL, get_identifier (name), type);
210   TREE_STATIC (decl) = 1;
211   TREE_READONLY (decl) = 1;
212   TREE_ASM_WRITTEN (decl) = 1;
213   DECL_SOURCE_LINE (decl) = 0;
214   DECL_ARTIFICIAL (decl) = 1;
215   DECL_IN_SYSTEM_HEADER (decl) = 1;
216   DECL_IGNORED_P (decl) = 1;
217   init = build_string (vlen, value);
218   TREE_TYPE (init) = type;
219   DECL_INITIAL (decl) = init;
220   finish_decl (pushdecl (decl), init, NULL_TREE);
221 }
222
223 /* Given a chain of STRING_CST nodes,
224    concatenate them into one STRING_CST
225    and give it a suitable array-of-chars data type.  */
226
227 tree
228 combine_strings (strings)
229      tree strings;
230 {
231   register tree value, t;
232   register int length = 1;
233   int wide_length = 0;
234   int wide_flag = 0;
235   int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
236   int nchars;
237
238   if (TREE_CHAIN (strings))
239     {
240       /* More than one in the chain, so concatenate.  */
241       register char *p, *q;
242
243       /* Don't include the \0 at the end of each substring,
244          except for the last one.
245          Count wide strings and ordinary strings separately.  */
246       for (t = strings; t; t = TREE_CHAIN (t))
247         {
248           if (TREE_TYPE (t) == wchar_array_type_node)
249             {
250               wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
251               wide_flag = 1;
252             }
253           else
254             length += (TREE_STRING_LENGTH (t) - 1);
255         }
256
257       /* If anything is wide, the non-wides will be converted,
258          which makes them take more space.  */
259       if (wide_flag)
260         length = length * wchar_bytes + wide_length;
261
262       p = savealloc (length);
263
264       /* Copy the individual strings into the new combined string.
265          If the combined string is wide, convert the chars to ints
266          for any individual strings that are not wide.  */
267
268       q = p;
269       for (t = strings; t; t = TREE_CHAIN (t))
270         {
271           int len = (TREE_STRING_LENGTH (t)
272                      - ((TREE_TYPE (t) == wchar_array_type_node)
273                         ? wchar_bytes : 1));
274           if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
275             {
276               memcpy (q, TREE_STRING_POINTER (t), len);
277               q += len;
278             }
279           else
280             {
281               int i;
282               for (i = 0; i < len; i++)
283                 {
284                   if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
285                     ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
286                   else
287                     ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
288                 }
289               q += len * wchar_bytes;
290             }
291         }
292       if (wide_flag)
293         {
294           int i;
295           for (i = 0; i < wchar_bytes; i++)
296             *q++ = 0;
297         }
298       else
299         *q = 0;
300
301       value = make_node (STRING_CST);
302       TREE_STRING_POINTER (value) = p;
303       TREE_STRING_LENGTH (value) = length;
304     }
305   else
306     {
307       value = strings;
308       length = TREE_STRING_LENGTH (value);
309       if (TREE_TYPE (value) == wchar_array_type_node)
310         wide_flag = 1;
311     }
312
313   /* Compute the number of elements, for the array type.  */
314   nchars = wide_flag ? length / wchar_bytes : length;
315
316   /* Create the array type for the string constant.
317      -Wwrite-strings says make the string constant an array of const char
318      so that copying it to a non-const pointer will get a warning.
319      For C++, this is the standard behavior.  */
320   if (flag_const_strings
321       && (! flag_traditional  && ! flag_writable_strings))
322     {
323       tree elements
324         = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
325                               1, 0);
326       TREE_TYPE (value)
327         = build_array_type (elements,
328                             build_index_type (build_int_2 (nchars - 1, 0)));
329     }
330   else
331     TREE_TYPE (value)
332       = build_array_type (wide_flag ? wchar_type_node : char_type_node,
333                           build_index_type (build_int_2 (nchars - 1, 0)));
334
335   TREE_CONSTANT (value) = 1;
336   TREE_READONLY (value) = ! flag_writable_strings;
337   TREE_STATIC (value) = 1;
338   return value;
339 }
340 \f
341 /* To speed up processing of attributes, we maintain an array of
342    IDENTIFIER_NODES and the corresponding attribute types.  */
343
344 /* Array to hold attribute information.  */
345
346 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
347
348 static int attrtab_idx = 0;
349
350 /* Add an entry to the attribute table above.  */
351
352 static void
353 add_attribute (id, string, min_len, max_len, decl_req)
354      enum attrs id;
355      const char *string;
356      int min_len, max_len;
357      int decl_req;
358 {
359   char buf[100];
360
361   attrtab[attrtab_idx].id = id;
362   attrtab[attrtab_idx].name = get_identifier (string);
363   attrtab[attrtab_idx].min = min_len;
364   attrtab[attrtab_idx].max = max_len;
365   attrtab[attrtab_idx++].decl_req = decl_req;
366
367   sprintf (buf, "__%s__", string);
368
369   attrtab[attrtab_idx].id = id;
370   attrtab[attrtab_idx].name = get_identifier (buf);
371   attrtab[attrtab_idx].min = min_len;
372   attrtab[attrtab_idx].max = max_len;
373   attrtab[attrtab_idx++].decl_req = decl_req;
374 }
375
376 /* Initialize attribute table.  */
377
378 static void
379 init_attributes ()
380 {
381   add_attribute (A_PACKED, "packed", 0, 0, 0);
382   add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
383   add_attribute (A_COMMON, "common", 0, 0, 1);
384   add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
385   add_attribute (A_NORETURN, "volatile", 0, 0, 1);
386   add_attribute (A_UNUSED, "unused", 0, 0, 0);
387   add_attribute (A_CONST, "const", 0, 0, 1);
388   add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
389   add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
390   add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
391   add_attribute (A_MODE, "mode", 1, 1, 1);
392   add_attribute (A_SECTION, "section", 1, 1, 1);
393   add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
394   add_attribute (A_FORMAT, "format", 3, 3, 1);
395   add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
396   add_attribute (A_WEAK, "weak", 0, 0, 1);
397   add_attribute (A_ALIAS, "alias", 1, 1, 1);
398   add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
399   add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
400 }
401 \f
402 /* Default implementation of valid_lang_attribute, below.  By default, there
403    are no language-specific attributes.  */
404
405 static int
406 default_valid_lang_attribute (attr_name, attr_args, decl, type)
407   tree attr_name ATTRIBUTE_UNUSED;
408   tree attr_args ATTRIBUTE_UNUSED;
409   tree decl ATTRIBUTE_UNUSED;
410   tree type ATTRIBUTE_UNUSED;
411 {
412   return 0;
413 }
414
415 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
416    attribute for either declaration DECL or type TYPE and 0 otherwise.  */
417
418 int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree))
419      = default_valid_lang_attribute;
420
421 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
422    and install them in NODE, which is either a DECL (including a TYPE_DECL)
423    or a TYPE.  PREFIX_ATTRIBUTES can appear after the declaration specifiers
424    and declaration modifiers but before the declaration proper.  */
425
426 void
427 decl_attributes (node, attributes, prefix_attributes)
428      tree node, attributes, prefix_attributes;
429 {
430   tree decl = 0, type = 0;
431   int is_type = 0;
432   tree a;
433
434   if (attrtab_idx == 0)
435     init_attributes ();
436
437   if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
438     {
439       decl = node;
440       type = TREE_TYPE (decl);
441       is_type = TREE_CODE (node) == TYPE_DECL;
442     }
443   else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
444     type = node, is_type = 1;
445
446 #ifdef PRAGMA_INSERT_ATTRIBUTES
447   /* If the code in c-pragma.c wants to insert some attributes then
448      allow it to do so.  Do this before allowing machine back ends to
449      insert attributes, so that they have the opportunity to override
450      anything done here.  */
451   PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
452 #endif
453   
454 #ifdef INSERT_ATTRIBUTES
455   INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
456 #endif
457   
458   attributes = chainon (prefix_attributes, attributes);
459
460   for (a = attributes; a; a = TREE_CHAIN (a))
461     {
462       tree name = TREE_PURPOSE (a);
463       tree args = TREE_VALUE (a);
464       int i;
465       enum attrs id;
466
467       for (i = 0; i < attrtab_idx; i++)
468         if (attrtab[i].name == name)
469           break;
470
471       if (i == attrtab_idx)
472         {
473           if (! valid_machine_attribute (name, args, decl, type)
474               && ! (* valid_lang_attribute) (name, args, decl, type))
475             warning ("`%s' attribute directive ignored",
476                      IDENTIFIER_POINTER (name));
477           else if (decl != 0)
478             type = TREE_TYPE (decl);
479           continue;
480         }
481       else if (attrtab[i].decl_req && decl == 0)
482         {
483           warning ("`%s' attribute does not apply to types",
484                    IDENTIFIER_POINTER (name));
485           continue;
486         }
487       else if (list_length (args) < attrtab[i].min
488                || list_length (args) > attrtab[i].max)
489         {
490           error ("wrong number of arguments specified for `%s' attribute",
491                  IDENTIFIER_POINTER (name));
492           continue;
493         }
494
495       id = attrtab[i].id;
496       switch (id)
497         {
498         case A_PACKED:
499           if (is_type)
500             TYPE_PACKED (type) = 1;
501           else if (TREE_CODE (decl) == FIELD_DECL)
502             DECL_PACKED (decl) = 1;
503           /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
504              used for DECL_REGISTER.  It wouldn't mean anything anyway.  */
505           else
506             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
507           break;
508
509         case A_NOCOMMON:
510           if (TREE_CODE (decl) == VAR_DECL)
511             DECL_COMMON (decl) = 0;
512           else
513             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
514           break;
515
516         case A_COMMON:
517           if (TREE_CODE (decl) == VAR_DECL)
518             DECL_COMMON (decl) = 1;
519           else
520             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
521           break;
522
523         case A_NORETURN:
524           if (TREE_CODE (decl) == FUNCTION_DECL)
525             TREE_THIS_VOLATILE (decl) = 1;
526           else if (TREE_CODE (type) == POINTER_TYPE
527                    && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
528             TREE_TYPE (decl) = type
529               = build_pointer_type
530                 (build_type_variant (TREE_TYPE (type),
531                                      TREE_READONLY (TREE_TYPE (type)), 1));
532           else
533             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
534           break;
535
536         case A_UNUSED:
537           if (is_type)
538             TREE_USED (type) = 1;
539           else if (TREE_CODE (decl) == PARM_DECL
540                    || TREE_CODE (decl) == VAR_DECL
541                    || TREE_CODE (decl) == FUNCTION_DECL
542                    || TREE_CODE (decl) == LABEL_DECL)
543             TREE_USED (decl) = 1;
544           else
545             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
546           break;
547
548         case A_CONST:
549           if (TREE_CODE (decl) == FUNCTION_DECL)
550             TREE_READONLY (decl) = 1;
551           else if (TREE_CODE (type) == POINTER_TYPE
552                    && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
553             TREE_TYPE (decl) = type
554               = build_pointer_type
555                 (build_type_variant (TREE_TYPE (type), 1,
556                                      TREE_THIS_VOLATILE (TREE_TYPE (type))));
557           else
558             warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
559           break;
560
561         case A_T_UNION:
562           if (is_type
563               && TREE_CODE (type) == UNION_TYPE
564               && (decl == 0
565                   || (TYPE_FIELDS (type) != 0
566                       && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
567             TYPE_TRANSPARENT_UNION (type) = 1;
568           else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
569                    && TREE_CODE (type) == UNION_TYPE
570                    && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
571             DECL_TRANSPARENT_UNION (decl) = 1;
572           else
573             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
574           break;
575
576         case A_CONSTRUCTOR:
577           if (TREE_CODE (decl) == FUNCTION_DECL
578               && TREE_CODE (type) == FUNCTION_TYPE
579               && decl_function_context (decl) == 0)
580             {
581               DECL_STATIC_CONSTRUCTOR (decl) = 1;
582               TREE_USED (decl) = 1;
583             }
584           else
585             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
586           break;
587
588         case A_DESTRUCTOR:
589           if (TREE_CODE (decl) == FUNCTION_DECL
590               && TREE_CODE (type) == FUNCTION_TYPE
591               && decl_function_context (decl) == 0)
592             {
593               DECL_STATIC_DESTRUCTOR (decl) = 1;
594               TREE_USED (decl) = 1;
595             }
596           else
597             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
598           break;
599
600         case A_MODE:
601           if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
602             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
603           else
604             {
605               int j;
606               const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
607               int len = strlen (p);
608               enum machine_mode mode = VOIDmode;
609               tree typefm;
610
611               if (len > 4 && p[0] == '_' && p[1] == '_'
612                   && p[len - 1] == '_' && p[len - 2] == '_')
613                 {
614                   char *newp = (char *) alloca (len - 1);
615
616                   strcpy (newp, &p[2]);
617                   newp[len - 4] = '\0';
618                   p = newp;
619                 }
620
621               /* Give this decl a type with the specified mode.
622                  First check for the special modes.  */
623               if (! strcmp (p, "byte"))
624                 mode = byte_mode;
625               else if (!strcmp (p, "word"))
626                 mode = word_mode;
627               else if (! strcmp (p, "pointer"))
628                 mode = ptr_mode;
629               else
630                 for (j = 0; j < NUM_MACHINE_MODES; j++)
631                   if (!strcmp (p, GET_MODE_NAME (j)))
632                     mode = (enum machine_mode) j;
633
634               if (mode == VOIDmode)
635                 error ("unknown machine mode `%s'", p);
636               else if (0 == (typefm = type_for_mode (mode,
637                                                      TREE_UNSIGNED (type))))
638                 error ("no data type for mode `%s'", p);
639               else
640                 {
641                   TREE_TYPE (decl) = type = typefm;
642                   DECL_SIZE (decl) = 0;
643                   layout_decl (decl, 0);
644                 }
645             }
646           break;
647
648         case A_SECTION:
649 #ifdef ASM_OUTPUT_SECTION_NAME
650           if ((TREE_CODE (decl) == FUNCTION_DECL
651                || TREE_CODE (decl) == VAR_DECL)
652               && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
653             {
654               if (TREE_CODE (decl) == VAR_DECL
655                   && current_function_decl != NULL_TREE
656                   && ! TREE_STATIC (decl))
657                 error_with_decl (decl,
658                   "section attribute cannot be specified for local variables");
659               /* The decl may have already been given a section attribute from
660                  a previous declaration.  Ensure they match.  */
661               else if (DECL_SECTION_NAME (decl) != NULL_TREE
662                        && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
663                                   TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
664                 error_with_decl (node,
665                                  "section of `%s' conflicts with previous declaration");
666               else
667                 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
668             }
669           else
670             error_with_decl (node,
671                            "section attribute not allowed for `%s'");
672 #else
673           error_with_decl (node,
674                   "section attributes are not supported for this target");
675 #endif
676           break;
677
678         case A_ALIGNED:
679           {
680             tree align_expr
681               = (args ? TREE_VALUE (args)
682                  : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
683             int align;
684             
685             /* Strip any NOPs of any kind.  */
686             while (TREE_CODE (align_expr) == NOP_EXPR
687                    || TREE_CODE (align_expr) == CONVERT_EXPR
688                    || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
689               align_expr = TREE_OPERAND (align_expr, 0);
690
691             if (TREE_CODE (align_expr) != INTEGER_CST)
692               {
693                 error ("requested alignment is not a constant");
694                 continue;
695               }
696
697             align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
698
699             if (exact_log2 (align) == -1)
700               error ("requested alignment is not a power of 2");
701             else if (is_type)
702               TYPE_ALIGN (type) = align;
703             else if (TREE_CODE (decl) != VAR_DECL
704                      && TREE_CODE (decl) != FIELD_DECL)
705               error_with_decl (decl,
706                                "alignment may not be specified for `%s'");
707             else
708               DECL_ALIGN (decl) = align;
709           }
710           break;
711
712         case A_FORMAT:
713           {
714             tree format_type_id = TREE_VALUE (args);
715             tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
716             tree first_arg_num_expr
717               = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
718             int format_num;
719             int first_arg_num;
720             int null_format_ok;
721             enum format_type format_type;
722             tree argument;
723             int arg_num;
724
725             if (TREE_CODE (decl) != FUNCTION_DECL)
726               {
727                 error_with_decl (decl,
728                          "argument format specified for non-function `%s'");
729                 continue;
730               }
731         
732             if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
733               {
734                 error_with_decl (decl, "unrecognized format specifier");
735                 continue;
736               }
737             else
738               {
739                 const char *p = IDENTIFIER_POINTER (format_type_id);
740                 
741                 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
742                   {
743                   format_type = printf_format_type;
744                   null_format_ok = 0;
745                   }
746                 else if (!strcmp (p, "printf0") || !strcmp (p, "__printf0__"))
747                   {
748                   format_type = printf_format_type;
749                   null_format_ok = 1;
750                   }
751                 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
752                   {
753                   format_type = scanf_format_type;
754                   null_format_ok = 0;
755                   }
756                 else if (!strcmp (p, "strftime")
757                          || !strcmp (p, "__strftime__"))
758                   {
759                   format_type = strftime_format_type;
760                   null_format_ok = 0;
761                   }
762                 else
763                   {
764                     warning ("`%s' is an unrecognized format function type", p);
765                     continue;
766                   }
767               }
768
769             /* Strip any conversions from the string index and first arg number
770                and verify they are constants.  */
771             while (TREE_CODE (format_num_expr) == NOP_EXPR
772                    || TREE_CODE (format_num_expr) == CONVERT_EXPR
773                    || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
774               format_num_expr = TREE_OPERAND (format_num_expr, 0);
775
776             while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
777                    || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
778                    || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
779               first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
780
781             if (TREE_CODE (format_num_expr) != INTEGER_CST
782                 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
783               {
784                 error ("format string has non-constant operand number");
785                 continue;
786               }
787
788             format_num = TREE_INT_CST_LOW (format_num_expr);
789             first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
790             if (first_arg_num != 0 && first_arg_num <= format_num)
791               {
792                 error ("format string arg follows the args to be formatted");
793                 continue;
794               }
795
796             /* If a parameter list is specified, verify that the format_num
797                argument is actually a string, in case the format attribute
798                is in error.  */
799             argument = TYPE_ARG_TYPES (type);
800             if (argument)
801               {
802                 for (arg_num = 1; ; ++arg_num)
803                   {
804                     if (argument == 0 || arg_num == format_num)
805                       break;
806                     argument = TREE_CHAIN (argument);
807                   }
808                 if (! argument
809                     || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
810                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
811                       != char_type_node))
812                   {
813                     error ("format string arg not a string type");
814                     continue;
815                   }
816                 if (first_arg_num != 0)
817                   {
818                     /* Verify that first_arg_num points to the last arg,
819                        the ...  */
820                     while (argument)
821                       arg_num++, argument = TREE_CHAIN (argument);
822                   if (arg_num != first_arg_num)
823                     {
824                       error ("args to be formatted is not ...");
825                       continue;
826                     }
827                   }
828               }
829
830             record_function_format (DECL_NAME (decl),
831                                     DECL_ASSEMBLER_NAME (decl),
832                                     format_type,  null_format_ok, format_num,
833                                     first_arg_num);
834             break;
835           }
836
837         case A_FORMAT_ARG:
838           {
839             tree format_num_expr = TREE_VALUE (args);
840             int format_num, arg_num;
841             tree argument;
842
843             if (TREE_CODE (decl) != FUNCTION_DECL)
844               {
845                 error_with_decl (decl,
846                          "argument format specified for non-function `%s'");
847                 continue;
848               }
849
850             /* Strip any conversions from the first arg number and verify it
851                is a constant.  */
852             while (TREE_CODE (format_num_expr) == NOP_EXPR
853                    || TREE_CODE (format_num_expr) == CONVERT_EXPR
854                    || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
855               format_num_expr = TREE_OPERAND (format_num_expr, 0);
856
857             if (TREE_CODE (format_num_expr) != INTEGER_CST)
858               {
859                 error ("format string has non-constant operand number");
860                 continue;
861               }
862
863             format_num = TREE_INT_CST_LOW (format_num_expr);
864
865             /* If a parameter list is specified, verify that the format_num
866                argument is actually a string, in case the format attribute
867                is in error.  */
868             argument = TYPE_ARG_TYPES (type);
869             if (argument)
870               {
871                 for (arg_num = 1; ; ++arg_num)
872                   {
873                     if (argument == 0 || arg_num == format_num)
874                       break;
875                     argument = TREE_CHAIN (argument);
876                   }
877                 if (! argument
878                     || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
879                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
880                       != char_type_node))
881                   {
882                     error ("format string arg not a string type");
883                     continue;
884                   }
885               }
886
887             if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
888                 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
889                     != char_type_node))
890               {
891                 error ("function does not return string type");
892                 continue;
893               }
894
895             record_international_format (DECL_NAME (decl),
896                                          DECL_ASSEMBLER_NAME (decl),
897                                          format_num);
898             break;
899           }
900
901         case A_WEAK:
902           declare_weak (decl);
903           break;
904
905         case A_ALIAS:
906           if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
907               || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
908             error_with_decl (decl,
909                              "`%s' defined both normally and as an alias");
910           else if (decl_function_context (decl) == 0)
911             {
912               tree id;
913
914               id = TREE_VALUE (args);
915               if (TREE_CODE (id) != STRING_CST)
916                 {
917                   error ("alias arg not a string");
918                   break;
919                 }
920               id = get_identifier (TREE_STRING_POINTER (id));
921
922               if (TREE_CODE (decl) == FUNCTION_DECL)
923                 DECL_INITIAL (decl) = error_mark_node;
924               else
925                 DECL_EXTERNAL (decl) = 0;
926               assemble_alias (decl, id);
927             }
928           else
929             warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
930           break;
931
932         case A_NO_CHECK_MEMORY_USAGE:
933           if (TREE_CODE (decl) != FUNCTION_DECL)
934             {
935               error_with_decl (decl,
936                                "`%s' attribute applies only to functions",
937                                IDENTIFIER_POINTER (name));
938             }
939           else if (DECL_INITIAL (decl))
940             {
941               error_with_decl (decl,
942                                "can't set `%s' attribute after definition",
943                                IDENTIFIER_POINTER (name));
944             }
945           else
946             DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
947           break;
948
949         case A_NO_INSTRUMENT_FUNCTION:
950           if (TREE_CODE (decl) != FUNCTION_DECL)
951             {
952               error_with_decl (decl,
953                                "`%s' attribute applies only to functions",
954                                IDENTIFIER_POINTER (name));
955             }
956           else if (DECL_INITIAL (decl))
957             {
958               error_with_decl (decl,
959                                "can't set `%s' attribute after definition",
960                                IDENTIFIER_POINTER (name));
961             }
962           else
963             DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
964           break;
965         }
966     }
967 }
968
969 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
970    lists.  SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
971
972    The head of the declspec list is stored in DECLSPECS.
973    The head of the attribute list is stored in PREFIX_ATTRIBUTES.
974
975    Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
976    the list elements.  We drop the containing TREE_LIST nodes and link the
977    resulting attributes together the way decl_attributes expects them.  */
978
979 void
980 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
981      tree specs_attrs;
982      tree *declspecs, *prefix_attributes;
983 {
984   tree t, s, a, next, specs, attrs;
985
986   /* This can happen after an __extension__ in pedantic mode.  */
987   if (specs_attrs != NULL_TREE 
988       && TREE_CODE (specs_attrs) == INTEGER_CST)
989     {
990       *declspecs = NULL_TREE;
991       *prefix_attributes = NULL_TREE;
992       return;
993     }
994
995   /* This can happen in c++ (eg: decl: typespec initdecls ';').  */
996   if (specs_attrs != NULL_TREE
997       && TREE_CODE (specs_attrs) != TREE_LIST)
998     {
999       *declspecs = specs_attrs;
1000       *prefix_attributes = NULL_TREE;
1001       return;
1002     }
1003
1004   /* Remember to keep the lists in the same order, element-wise.  */
1005
1006   specs = s = NULL_TREE;
1007   attrs = a = NULL_TREE;
1008   for (t = specs_attrs; t; t = next)
1009     {
1010       next = TREE_CHAIN (t);
1011       /* Declspecs have a non-NULL TREE_VALUE.  */
1012       if (TREE_VALUE (t) != NULL_TREE)
1013         {
1014           if (specs == NULL_TREE)
1015             specs = s = t;
1016           else
1017             {
1018               TREE_CHAIN (s) = t;
1019               s = t;
1020             }
1021         }
1022       else
1023         {
1024           if (attrs == NULL_TREE)
1025             attrs = a = TREE_PURPOSE (t);
1026           else
1027             {
1028               TREE_CHAIN (a) = TREE_PURPOSE (t);
1029               a = TREE_PURPOSE (t);
1030             }
1031           /* More attrs can be linked here, move A to the end.  */
1032           while (TREE_CHAIN (a) != NULL_TREE)
1033             a = TREE_CHAIN (a);
1034         }
1035     }
1036
1037   /* Terminate the lists.  */
1038   if (s != NULL_TREE)
1039     TREE_CHAIN (s) = NULL_TREE;
1040   if (a != NULL_TREE)
1041     TREE_CHAIN (a) = NULL_TREE;
1042
1043   /* All done.  */
1044   *declspecs = specs;
1045   *prefix_attributes = attrs;
1046 }
1047
1048 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1049    This function is used by the parser when a rule will accept attributes
1050    in a particular position, but we don't want to support that just yet.
1051
1052    A warning is issued for every ignored attribute.  */
1053
1054 tree
1055 strip_attrs (specs_attrs)
1056      tree specs_attrs;
1057 {
1058   tree specs, attrs;
1059
1060   split_specs_attrs (specs_attrs, &specs, &attrs);
1061
1062   while (attrs)
1063     {
1064       warning ("`%s' attribute ignored",
1065                IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1066       attrs = TREE_CHAIN (attrs);
1067     }
1068
1069   return specs;
1070 }
1071 \f
1072 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1073    a parameter list.  */
1074
1075 #define T_I     &integer_type_node
1076 #define T_L     &long_integer_type_node
1077 #define T_LL    &long_long_integer_type_node
1078 #define T_S     &short_integer_type_node
1079 #define T_UI    &unsigned_type_node
1080 #define T_UL    &long_unsigned_type_node
1081 #define T_ULL   &long_long_unsigned_type_node
1082 #define T_US    &short_unsigned_type_node
1083 #define T_F     &float_type_node
1084 #define T_D     &double_type_node
1085 #define T_LD    &long_double_type_node
1086 #define T_C     &char_type_node
1087 #define T_UC    &unsigned_char_type_node
1088 #define T_V     &void_type_node
1089 #define T_W     &wchar_type_node
1090 #define T_ST    &sizetype
1091
1092 typedef struct {
1093   const char *format_chars;
1094   int pointer_count;
1095   /* Type of argument if no length modifier is used.  */
1096   tree *nolen;
1097   /* Type of argument if length modifier for shortening to byte is used.
1098      If NULL, then this modifier is not allowed.  */
1099   tree *hhlen;
1100   /* Type of argument if length modifier for shortening is used.
1101      If NULL, then this modifier is not allowed.  */
1102   tree *hlen;
1103   /* Type of argument if length modifier `l' is used.
1104      If NULL, then this modifier is not allowed.  */
1105   tree *llen;
1106   /* Type of argument if length modifier `q' or `ll' is used.
1107      If NULL, then this modifier is not allowed.  */
1108   tree *qlen;
1109   /* Type of argument if length modifier `L' is used.
1110      If NULL, then this modifier is not allowed.  */
1111   tree *bigllen;
1112   /* Type of argument if length modifier `Z' is used.
1113      If NULL, then this modifier is not allowed.  */
1114   tree *zlen;
1115   /* List of other modifier characters allowed with these options.  */
1116   const char *flag_chars;
1117 } format_char_info;
1118
1119 static format_char_info print_char_table[] = {
1120 /* FreeBSD kernel extensions.  */
1121   { "D",        1,      T_C,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "-wp"           },
1122   { "b",        1,      T_C,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "-wp"           },
1123   { "rz",       0,      T_I,    NULL,   NULL,   T_L,    NULL,   NULL,   NULL,   "-wp0 +#"       },
1124 #define unextended_print_char_table     (print_char_table + 3)
1125   { "di",       0,      T_I,    T_I,    T_I,    T_L,    T_LL,   T_LL,   T_ST,   "-wp0 +"        },
1126   { "oxX",      0,      T_UI,   T_UI,   T_UI,   T_UL,   T_ULL,  T_ULL,  T_ST,   "-wp0#"         },
1127   { "u",        0,      T_UI,   T_UI,   T_UI,   T_UL,   T_ULL,  T_ULL,  T_ST,   "-wp0"          },
1128 /* A GNU extension.  */
1129   { "m",        0,      T_V,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "-wp"           },
1130   { "feEgGaA",  0,      T_D,    NULL,   NULL,   NULL,   NULL,   T_LD,   NULL,   "-wp0 +#"       },
1131   { "c",        0,      T_I,    NULL,   NULL,   T_W,    NULL,   NULL,   NULL,   "-w"            },
1132   { "C",        0,      T_W,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "-w"            },
1133   { "s",        1,      T_C,    NULL,   NULL,   T_W,    NULL,   NULL,   NULL,   "-wp"           },
1134   { "S",        1,      T_W,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "-wp"           },
1135   { "p",        1,      T_V,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "-w"            },
1136   { "n",        1,      T_I,    NULL,   T_S,    T_L,    T_LL,   NULL,   NULL,   ""              },
1137   { NULL,       0,      NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL            }
1138 };
1139
1140 static format_char_info scan_char_table[] = {
1141   { "di",       1,      T_I,    T_C,    T_S,    T_L,    T_LL,   T_LL,   NULL,   "*"     },
1142   { "ouxX",     1,      T_UI,   T_UC,   T_US,   T_UL,   T_ULL,  T_ULL,  NULL,   "*"     },
1143   { "efgEGaA",  1,      T_F,    NULL,   NULL,   T_D,    NULL,   T_LD,   NULL,   "*"     },
1144   { "c",        1,      T_C,    NULL,   NULL,   T_W,    NULL,   NULL,   NULL,   "*"     },
1145   { "s",        1,      T_C,    NULL,   NULL,   T_W,    NULL,   NULL,   NULL,   "*a"    },
1146   { "[",        1,      T_C,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "*a"    },
1147   { "C",        1,      T_W,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "*"     },
1148   { "S",        1,      T_W,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "*a"    },
1149   { "p",        2,      T_V,    NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   "*"     },
1150   { "n",        1,      T_I,    T_C,    T_S,    T_L,    T_LL,   NULL,   NULL,   ""      },
1151   { NULL,       0,      NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL    }
1152 };
1153
1154 /* Handle format characters recognized by glibc's strftime.c.
1155    '2' - MUST do years as only two digits
1156    '3' - MAY do years as only two digits (depending on locale)
1157    'E' - E modifier is acceptable
1158    'O' - O modifier is acceptable to Standard C
1159    'o' - O modifier is acceptable as a GNU extension
1160    'G' - other GNU extensions  */
1161
1162 static format_char_info time_char_table[] = {
1163   { "y",                0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1164   { "D",                0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1165   { "g",                0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1166   { "cx",               0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1167   { "%+RTXnrt",         0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1168   { "P",                0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1169   { "HIMSUWdemw",       0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1170   { "Vju",              0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1171   { "Gklsz",            0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1172   { "ABZa",             0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1173   { "p",                0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1174   { "bh",               0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1175   { "CY",               0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1176   { NULL,               0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1177 };
1178
1179 typedef struct function_format_info
1180 {
1181   struct function_format_info *next;  /* next structure on the list */
1182   tree name;                    /* identifier such as "printf" */
1183   tree assembler_name;          /* optional mangled identifier (for C++) */
1184   enum format_type format_type; /* type of format (printf, scanf, etc.) */
1185   int null_format_ok;           /* TRUE if the format string may be NULL */
1186   int format_num;               /* number of format argument */
1187   int first_arg_num;            /* number of first arg (zero for varargs) */
1188 } function_format_info;
1189
1190 static function_format_info *function_format_list = NULL;
1191
1192 typedef struct international_format_info
1193 {
1194   struct international_format_info *next;  /* next structure on the list */
1195   tree name;                    /* identifier such as "gettext" */
1196   tree assembler_name;          /* optional mangled identifier (for C++) */
1197   int format_num;               /* number of format argument */
1198 } international_format_info;
1199
1200 static international_format_info *international_format_list = NULL;
1201
1202 static void check_format_info           PROTO((function_format_info *, tree));
1203
1204 /* Initialize the table of functions to perform format checking on.
1205    The ANSI functions are always checked (whether <stdio.h> is
1206    included or not), since it is common to call printf without
1207    including <stdio.h>.  There shouldn't be a problem with this,
1208    since ANSI reserves these function names whether you include the
1209    header file or not.  In any case, the checking is harmless.
1210
1211    Also initialize the name of function that modify the format string for
1212    internationalization purposes.  */
1213
1214 void
1215 init_function_format_info ()
1216 {
1217   record_function_format (get_identifier ("printf"), NULL_TREE,
1218                           printf_format_type, 0, 1, 2);
1219   record_function_format (get_identifier ("fprintf"), NULL_TREE,
1220                           printf_format_type, 0, 2, 3);
1221   record_function_format (get_identifier ("sprintf"), NULL_TREE,
1222                           printf_format_type, 0, 2, 3);
1223   record_function_format (get_identifier ("scanf"), NULL_TREE,
1224                           scanf_format_type, 0, 1, 2);
1225   record_function_format (get_identifier ("fscanf"), NULL_TREE,
1226                           scanf_format_type, 0, 2, 3);
1227   record_function_format (get_identifier ("sscanf"), NULL_TREE,
1228                           scanf_format_type, 0, 2, 3);
1229   record_function_format (get_identifier ("vprintf"), NULL_TREE,
1230                           printf_format_type, 0, 1, 0);
1231   record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1232                           printf_format_type, 0, 2, 0);
1233   record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1234                           printf_format_type, 0, 2, 0);
1235   record_function_format (get_identifier ("strftime"), NULL_TREE,
1236                           strftime_format_type, 0, 3, 0);
1237
1238   record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1239   record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1240   record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1241 }
1242
1243 /* Record information for argument format checking.  FUNCTION_IDENT is
1244    the identifier node for the name of the function to check (its decl
1245    need not exist yet).
1246    FORMAT_TYPE specifies the type of format checking.  FORMAT_NUM is the number
1247    of the argument which is the format control string (starting from 1).
1248    FIRST_ARG_NUM is the number of the first actual argument to check
1249    against the format string, or zero if no checking is not be done
1250    (e.g. for varargs such as vfprintf).  */
1251
1252 static void
1253 record_function_format (name, assembler_name, format_type, null_format_ok,
1254                         format_num, first_arg_num)
1255       tree name;
1256       tree assembler_name;
1257       enum format_type format_type;
1258       int null_format_ok;
1259       int format_num;
1260       int first_arg_num;
1261 {
1262   function_format_info *info;
1263
1264   /* Re-use existing structure if it's there.  */
1265
1266   for (info = function_format_list; info; info = info->next)
1267     {
1268       if (info->name == name && info->assembler_name == assembler_name)
1269         break;
1270     }
1271   if (! info)
1272     {
1273       info = (function_format_info *) xmalloc (sizeof (function_format_info));
1274       info->next = function_format_list;
1275       function_format_list = info;
1276
1277       info->name = name;
1278       info->assembler_name = assembler_name;
1279     }
1280
1281   info->format_type = format_type;
1282   info->null_format_ok = null_format_ok;
1283   info->format_num = format_num;
1284   info->first_arg_num = first_arg_num;
1285 }
1286
1287 /* Record information for the names of function that modify the format
1288    argument to format functions.  FUNCTION_IDENT is the identifier node for
1289    the name of the function (its decl need not exist yet) and FORMAT_NUM is
1290    the number of the argument which is the format control string (starting
1291    from 1).  */
1292
1293 static void
1294 record_international_format (name, assembler_name, format_num)
1295       tree name;
1296       tree assembler_name;
1297       int format_num;
1298 {
1299   international_format_info *info;
1300
1301   /* Re-use existing structure if it's there.  */
1302
1303   for (info = international_format_list; info; info = info->next)
1304     {
1305       if (info->name == name && info->assembler_name == assembler_name)
1306         break;
1307     }
1308
1309   if (! info)
1310     {
1311       info
1312         = (international_format_info *)
1313           xmalloc (sizeof (international_format_info));
1314       info->next = international_format_list;
1315       international_format_list = info;
1316
1317       info->name = name;
1318       info->assembler_name = assembler_name;
1319     }
1320
1321   info->format_num = format_num;
1322 }
1323
1324 static void
1325 tfaff ()
1326 {
1327   warning ("too few arguments for format");
1328 }
1329 \f
1330 function_format_info *
1331 find_function_format (name, assembler_name)
1332      tree name;
1333      tree assembler_name;
1334 {
1335   function_format_info *info;
1336
1337   for (info = function_format_list; info; info = info->next)
1338     if (info->assembler_name
1339         ? (info->assembler_name == assembler_name)
1340         : (info->name == name))
1341       return info;
1342   return 0;
1343 }
1344
1345 /* Check the argument list of a call to printf, scanf, etc.
1346    NAME is the function identifier.
1347    ASSEMBLER_NAME is the function's assembler identifier.
1348    (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1349    PARAMS is the list of argument values.  */
1350
1351 void
1352 check_function_format (name, assembler_name, params)
1353      tree name;
1354      tree assembler_name;
1355      tree params;
1356 {
1357   function_format_info *info;
1358
1359   /* See if this function is a format function.  */
1360   info = find_function_format (name, assembler_name);
1361
1362   if (info)
1363     check_format_info (info, params);
1364 }
1365
1366 /* Check the argument list of a call to printf, scanf, etc.
1367    INFO points to the function_format_info structure.
1368    PARAMS is the list of argument values.  */
1369
1370 static void
1371 check_format_info (info, params)
1372      function_format_info *info;
1373      tree params;
1374 {
1375   int i;
1376   int arg_num;
1377   int suppressed, wide, precise;
1378   int length_char = 0;
1379   int format_char;
1380   int format_length;
1381   tree format_tree;
1382   tree cur_param;
1383   tree cur_type;
1384   tree wanted_type;
1385   tree first_fillin_param;
1386   const char *format_chars;
1387   format_char_info *fci = NULL;
1388   char flag_chars[8];
1389   int has_operand_number = 0;
1390
1391   /* Skip to format argument.  If the argument isn't available, there's
1392      no work for us to do; prototype checking will catch the problem.  */
1393   for (arg_num = 1; ; ++arg_num)
1394     {
1395       if (params == 0)
1396         return;
1397       if (arg_num == info->format_num)
1398         break;
1399       params = TREE_CHAIN (params);
1400     }
1401   format_tree = TREE_VALUE (params);
1402   params = TREE_CHAIN (params);
1403   if (format_tree == 0)
1404     return;
1405
1406   /* We can only check the format if it's a string constant.  */
1407  again:
1408   while (TREE_CODE (format_tree) == NOP_EXPR)
1409     format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1410
1411   if (TREE_CODE (format_tree) == CALL_EXPR
1412       && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1413       && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1414           == FUNCTION_DECL))
1415     {
1416       tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1417
1418       /* See if this is a call to a known internationalization function
1419          that modifies the format arg.  */
1420       international_format_info *info;
1421
1422       for (info = international_format_list; info; info = info->next)
1423         if (info->assembler_name
1424             ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1425             : (info->name == DECL_NAME (function)))
1426           {
1427             tree inner_args;
1428             int i;
1429
1430             for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1431                  inner_args != 0;
1432                  inner_args = TREE_CHAIN (inner_args), i++)
1433               if (i == info->format_num)
1434                 {
1435                   format_tree = TREE_VALUE (inner_args);
1436
1437                   while (TREE_CODE (format_tree) == NOP_EXPR)
1438                     format_tree = TREE_OPERAND (format_tree, 0);
1439                 }
1440           }
1441     }
1442
1443   if (TREE_CODE (format_tree) == COND_EXPR) 
1444     {
1445       format_tree = TREE_OPERAND(format_tree, 1);
1446       goto again;
1447     }
1448
1449   if (integer_zerop (format_tree))
1450     {
1451       if (!info->null_format_ok)
1452         warning ("null format string");
1453       return;
1454     }
1455   if (TREE_CODE (format_tree) != ADDR_EXPR) 
1456     {
1457       if ((info->first_arg_num == 0) &&
1458           (TREE_CODE(format_tree) == PARM_DECL)) 
1459         {
1460           function_format_info *i2;
1461           tree p;
1462           int n;
1463
1464           /* Now, we need to determine if the current function is printf-like,
1465              and, if so, if the parameter we have here is as a parameter of 
1466              the current function and is in the argument slot declared to 
1467              contain the format argument. */
1468
1469           p = current_function_decl;
1470
1471           i2 = find_function_format (p->decl.name, p->decl.assembler_name);
1472
1473           if (i2 == NULL) 
1474             {
1475               if (warn_format > 1)
1476                 warning("non-constant format parameter");
1477             }
1478           else
1479             {
1480               for (n = 1, p = current_function_decl->decl.arguments;
1481                    (n < i2->format_num) && (p != NULL); 
1482                    n++, p = TREE_CHAIN(p)) 
1483                 ;
1484               if ((p == NULL) || (n != i2->format_num)) 
1485                 warning("can't find format arg for current format function");
1486               else if (p != format_tree)
1487                 warning("format argument passed here is not declared as format argument");
1488             }
1489         }
1490       else if ((info->format_type != strftime_format_type) &&
1491                (warn_format > 1))
1492         warning("non-constant format parameter");
1493       return;
1494     }
1495   format_tree = TREE_OPERAND (format_tree, 0);
1496   if (warn_format > 1 &&
1497       (TREE_CODE (format_tree) == VAR_DECL) &&
1498       TREE_READONLY(format_tree) &&
1499       (DECL_INITIAL(format_tree) != NULL) && 
1500       TREE_CODE(DECL_INITIAL(format_tree)) == STRING_CST)
1501     format_tree = DECL_INITIAL(format_tree);
1502        
1503   if (TREE_CODE (format_tree) != STRING_CST) 
1504     {
1505       if ((info->format_type != strftime_format_type) &&
1506           (warn_format > 1))
1507         warning("non-constant format parameter");
1508       return;
1509     }
1510   format_chars = TREE_STRING_POINTER (format_tree);
1511   format_length = TREE_STRING_LENGTH (format_tree);
1512   if (format_length <= 1)
1513     warning ("zero-length format string");
1514   if (format_chars[--format_length] != 0)
1515     {
1516       warning ("unterminated format string");
1517       return;
1518     }
1519   /* Skip to first argument to check.  */
1520   while (arg_num + 1 < info->first_arg_num)
1521     {
1522       if (params == 0)
1523         return;
1524       params = TREE_CHAIN (params);
1525       ++arg_num;
1526     }
1527
1528   first_fillin_param = params;
1529   while (1)
1530     {
1531       int aflag;
1532       if (*format_chars == 0)
1533         {
1534           if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1535             warning ("embedded `\\0' in format");
1536           if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1537             {
1538               if (warn_format_extra_args)
1539                 warning ("too many arguments for format");
1540             }
1541           return;
1542         }
1543       if (*format_chars++ != '%')
1544         continue;
1545       if (*format_chars == 0)
1546         {
1547           warning ("spurious trailing `%%' in format");
1548           continue;
1549         }
1550       if (*format_chars == '%')
1551         {
1552           ++format_chars;
1553           continue;
1554         }
1555       flag_chars[0] = 0;
1556       suppressed = wide = precise = FALSE;
1557       if (info->format_type == scanf_format_type)
1558         {
1559           suppressed = *format_chars == '*';
1560           if (suppressed)
1561             ++format_chars;
1562           while (ISDIGIT (*format_chars))
1563             ++format_chars;
1564         }
1565       else if (info->format_type == strftime_format_type)
1566         {
1567           while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1568             {
1569               if (pedantic)
1570                 warning ("ANSI C does not support the strftime `%c' flag",
1571                          *format_chars);
1572               if (index (flag_chars, *format_chars) != 0)
1573                 {
1574                   warning ("repeated `%c' flag in format",
1575                            *format_chars);
1576                   ++format_chars;
1577                 }
1578               else
1579                 {
1580                   i = strlen (flag_chars);
1581                   flag_chars[i++] = *format_chars++;
1582                   flag_chars[i] = 0;
1583                 }
1584             }
1585           while (ISDIGIT ((unsigned char) *format_chars))
1586             {
1587               wide = TRUE;
1588               ++format_chars;
1589             }
1590           if (wide && pedantic)
1591             warning ("ANSI C does not support strftime format width");
1592           if (*format_chars == 'E' || *format_chars == 'O')
1593             {
1594               i = strlen (flag_chars);
1595               flag_chars[i++] = *format_chars++;
1596               flag_chars[i] = 0;
1597               if (*format_chars == 'E' || *format_chars == 'O')
1598                 {
1599                   warning ("multiple E/O modifiers in format");
1600                   while (*format_chars == 'E' || *format_chars == 'O')
1601                     ++format_chars;
1602                 }
1603             }
1604         }
1605       else if (info->format_type == printf_format_type)
1606         {
1607           /* See if we have a number followed by a dollar sign.  If we do,
1608              it is an operand number, so set PARAMS to that operand.  */
1609           if (*format_chars >= '0' && *format_chars <= '9')
1610             {
1611               const char *p = format_chars;
1612
1613               while (*p >= '0' && *p++ <= '9')
1614                 ;
1615
1616               if (*p == '$')
1617                 {
1618                   int opnum = atoi (format_chars);
1619
1620                   params = first_fillin_param;
1621                   format_chars = p + 1;
1622                   has_operand_number = 1;
1623
1624                   for (i = 1; i < opnum && params != 0; i++)
1625                     params = TREE_CHAIN (params);
1626
1627                   if (opnum == 0 || params == 0)
1628                     {
1629                       warning ("operand number out of range in format");
1630                       return;
1631                     }
1632                 }
1633             }
1634
1635           while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1636             {
1637               if (index (flag_chars, *format_chars) != 0)
1638                 warning ("repeated `%c' flag in format", *format_chars++);
1639               else
1640                 {
1641                   i = strlen (flag_chars);
1642                   flag_chars[i++] = *format_chars++;
1643                   flag_chars[i] = 0;
1644                 }
1645             }
1646           /* "If the space and + flags both appear,
1647              the space flag will be ignored."  */
1648           if (index (flag_chars, ' ') != 0
1649               && index (flag_chars, '+') != 0)
1650             warning ("use of both ` ' and `+' flags in format");
1651           /* "If the 0 and - flags both appear,
1652              the 0 flag will be ignored."  */
1653           if (index (flag_chars, '0') != 0
1654               && index (flag_chars, '-') != 0)
1655             warning ("use of both `0' and `-' flags in format");
1656           if (*format_chars == '*')
1657             {
1658               wide = TRUE;
1659               /* "...a field width...may be indicated by an asterisk.
1660                  In this case, an int argument supplies the field width..."  */
1661               ++format_chars;
1662               if (params == 0)
1663                 {
1664                   tfaff ();
1665                   return;
1666                 }
1667               if (info->first_arg_num != 0)
1668                 {
1669                   cur_param = TREE_VALUE (params);
1670                   params = TREE_CHAIN (params);
1671                   ++arg_num;
1672                   /* size_t is generally not valid here.
1673                      It will work on most machines, because size_t and int
1674                      have the same mode.  But might as well warn anyway,
1675                      since it will fail on other machines.  */
1676                   /* XXX should we allow unsigned ints here?  */
1677                   if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1678                        != integer_type_node)
1679                       &&
1680                       (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1681                        != unsigned_type_node))
1682                     warning ("precision is not type int (arg %d)", arg_num);
1683                 }
1684             }
1685           else
1686             {
1687               while (ISDIGIT (*format_chars))
1688                 {
1689                   wide = TRUE;
1690                   ++format_chars;
1691                 }
1692             }
1693           if (*format_chars == '.')
1694             {
1695               precise = TRUE;
1696               ++format_chars;
1697               if (*format_chars != '*' && !ISDIGIT (*format_chars))
1698                 warning ("`.' not followed by `*' or digit in format");
1699               /* "...a...precision...may be indicated by an asterisk.
1700                  In this case, an int argument supplies the...precision."  */
1701               if (*format_chars == '*')
1702                 {
1703                   if (info->first_arg_num != 0)
1704                     {
1705                       ++format_chars;
1706                       if (params == 0)
1707                         {
1708                           tfaff ();
1709                           return;
1710                         }
1711                       cur_param = TREE_VALUE (params);
1712                       params = TREE_CHAIN (params);
1713                       ++arg_num;
1714                       if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1715                           != integer_type_node)
1716                         warning ("field width is not type int (arg %d)",
1717                                  arg_num);
1718                     }
1719                 }
1720               else
1721                 {
1722                   while (ISDIGIT (*format_chars))
1723                     ++format_chars;
1724                 }
1725             }
1726         }
1727       if (*format_chars == 'b')
1728         {
1729           /* There should be an int arg to control the string arg.  */
1730           if (params == 0)
1731             {
1732               tfaff ();
1733               return;
1734             }
1735             if (info->first_arg_num != 0)
1736             {
1737               cur_param = TREE_VALUE (params);
1738               params = TREE_CHAIN (params);
1739               ++arg_num;
1740               if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1741                    != integer_type_node)
1742                   &&
1743                   (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1744                    != unsigned_type_node))
1745                 {
1746                   warning ("bitmap is not type int (arg %d)", arg_num);
1747                 }
1748             }
1749         }
1750       if (*format_chars == 'D')
1751         {
1752           /* There should be an unsigned char * arg before the string arg.  */
1753           if (params == 0)
1754             {
1755               tfaff ();
1756               return;
1757             }
1758             if (info->first_arg_num != 0)
1759             {
1760               cur_param = TREE_VALUE (params);
1761               params = TREE_CHAIN (params);
1762               ++arg_num;
1763               cur_type = TREE_TYPE (cur_param);
1764               if (TREE_CODE (cur_type) != POINTER_TYPE
1765                   || TYPE_MAIN_VARIANT (TREE_TYPE (cur_type))
1766                      != unsigned_char_type_node)
1767                 {
1768                   warning ("ethernet address is not type unsigned char *"
1769                            " (arg %d)",
1770                            arg_num);
1771                 }
1772             }
1773         }
1774
1775       aflag = 0;
1776
1777       if (info->format_type != strftime_format_type)
1778         {
1779           if (*format_chars == 'h' || *format_chars == 'l')
1780             length_char = *format_chars++;
1781           else if ((*format_chars == 'q' || *format_chars == 'L')
1782                    && !flag_format_extensions)
1783             {
1784               length_char = *format_chars++;
1785               if (pedantic)
1786                 warning ("ANSI C does not support the `%c' length modifier",
1787                          length_char);
1788             }
1789           else if (*format_chars == 'Z')
1790             {
1791               length_char = *format_chars++;
1792               if (pedantic)
1793                 warning ("ANSI C does not support the `Z' length modifier");
1794             }
1795           else
1796             length_char = 0;
1797           if (length_char == 'l' && *format_chars == 'l')
1798             {
1799               length_char = 'q', format_chars++;
1800               /* FIXME: Is allowed in ISO C 9x.  */
1801               if (pedantic)
1802                 warning ("ANSI C does not support the `ll' length modifier");
1803             }
1804           else if (length_char == 'h' && *format_chars == 'h')
1805             {
1806               length_char = 'H', format_chars++;
1807               /* FIXME: Is allowed in ISO C 9x.  */
1808               if (pedantic)
1809                 warning ("ANSI C does not support the `hh' length modifier");
1810             }
1811           if (*format_chars == 'a' && info->format_type == scanf_format_type)
1812             {
1813               if (format_chars[1] == 's' || format_chars[1] == 'S'
1814                   || format_chars[1] == '[')
1815                 {
1816                   /* `a' is used as a flag.  */
1817                   aflag = 1;
1818                   format_chars++;
1819                 }
1820             }
1821           if (suppressed && length_char != 0)
1822             warning ("use of `*' and `%c' together in format", length_char);
1823         }
1824       format_char = *format_chars;
1825       if (format_char == 0
1826           || (info->format_type != strftime_format_type && format_char == '%'))
1827         {
1828           warning ("conversion lacks type at end of format");
1829           continue;
1830         }
1831       /* The m, C, and S formats are GNU extensions.  */
1832       if (pedantic && info->format_type != strftime_format_type
1833           && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1834         warning ("ANSI C does not support the `%c' format", format_char);
1835       /* ??? The a and A formats are C9X extensions, and should be allowed
1836          when a C9X option is added.  */
1837       if (pedantic && info->format_type != strftime_format_type
1838           && (format_char == 'a' || format_char == 'A'))
1839         warning ("ANSI C does not support the `%c' format", format_char);
1840       format_chars++;
1841       switch (info->format_type)
1842         {
1843         case printf_format_type:
1844           fci = flag_format_extensions ? print_char_table
1845                 : unextended_print_char_table;
1846           break;
1847         case scanf_format_type:
1848           fci = scan_char_table;
1849           break;
1850         case strftime_format_type:
1851           fci = time_char_table;
1852           break;
1853         default:
1854           abort ();
1855         }
1856       while (fci->format_chars != 0
1857              && index (fci->format_chars, format_char) == 0)
1858           ++fci;
1859       if (fci->format_chars == 0)
1860         {
1861           if (format_char >= 040 && format_char < 0177)
1862             warning ("unknown conversion type character `%c' in format",
1863                      format_char);
1864           else
1865             warning ("unknown conversion type character 0x%x in format",
1866                      format_char);
1867           continue;
1868         }
1869       if (pedantic)
1870         {
1871           if (index (fci->flag_chars, 'G') != 0)
1872             warning ("ANSI C does not support `%%%c'", format_char);
1873           if (index (fci->flag_chars, 'o') != 0
1874               && index (flag_chars, 'O') != 0)
1875             warning ("ANSI C does not support `%%O%c'", format_char);
1876         }
1877       if (wide && index (fci->flag_chars, 'w') == 0)
1878         warning ("width used with `%c' format", format_char);
1879       if (index (fci->flag_chars, '2') != 0)
1880         warning ("`%%%c' yields only last 2 digits of year", format_char);
1881       else if (index (fci->flag_chars, '3') != 0)
1882         warning ("`%%%c' yields only last 2 digits of year in some locales on non-BSD systems",
1883                  format_char);
1884       if (precise && index (fci->flag_chars, 'p') == 0)
1885         warning ("precision used with `%c' format", format_char);
1886       if (aflag && index (fci->flag_chars, 'a') == 0)
1887         {
1888           warning ("`a' flag used with `%c' format", format_char);
1889           /* To simplify the following code.  */
1890           aflag = 0;
1891         }
1892       /* The a flag is a GNU extension.  */
1893       else if (pedantic && aflag)
1894         warning ("ANSI C does not support the `a' flag");
1895       if (info->format_type == scanf_format_type && format_char == '[')
1896         {
1897           /* Skip over scan set, in case it happens to have '%' in it.  */
1898           if (*format_chars == '^')
1899             ++format_chars;
1900           /* Find closing bracket; if one is hit immediately, then
1901              it's part of the scan set rather than a terminator.  */
1902           if (*format_chars == ']')
1903             ++format_chars;
1904           while (*format_chars && *format_chars != ']')
1905             ++format_chars;
1906           if (*format_chars != ']')
1907             /* The end of the format string was reached.  */
1908             warning ("no closing `]' for `%%[' format");
1909         }
1910       if (suppressed)
1911         {
1912           if (index (fci->flag_chars, '*') == 0)
1913             warning ("suppression of `%c' conversion in format", format_char);
1914           continue;
1915         }
1916       for (i = 0; flag_chars[i] != 0; ++i)
1917         {
1918           if (index (fci->flag_chars, flag_chars[i]) == 0)
1919             warning ("flag `%c' used with type `%c'",
1920                      flag_chars[i], format_char);
1921         }
1922       if (info->format_type == strftime_format_type)
1923         continue;
1924       if (precise && index (flag_chars, '0') != 0
1925           && (format_char == 'd' || format_char == 'i'
1926               || format_char == 'o' || format_char == 'u'
1927               || format_char == 'x' || format_char == 'X'))
1928         warning ("`0' flag ignored with precision specifier and `%c' format",
1929                  format_char);
1930       switch (length_char)
1931         {
1932         default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1933         case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1934         case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1935         case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1936         case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1937         case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1938         case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1939         }
1940       if (wanted_type == 0)
1941         warning ("use of `%c' length character with `%c' type character",
1942                  length_char, format_char);
1943
1944       /* Finally. . .check type of argument against desired type!  */
1945       if (info->first_arg_num == 0)
1946         continue;
1947       if (fci->pointer_count == 0 && wanted_type == void_type_node)
1948         /* This specifier takes no argument.  */
1949         continue;
1950       if (params == 0)
1951         {
1952           tfaff ();
1953           return;
1954         }
1955       cur_param = TREE_VALUE (params);
1956       params = TREE_CHAIN (params);
1957       ++arg_num;
1958       cur_type = TREE_TYPE (cur_param);
1959
1960       STRIP_NOPS (cur_param);
1961
1962       /* Check the types of any additional pointer arguments
1963          that precede the "real" argument.  */
1964       for (i = 0; i < fci->pointer_count + aflag; ++i)
1965         {
1966           if (TREE_CODE (cur_type) == POINTER_TYPE)
1967             {
1968               cur_type = TREE_TYPE (cur_type);
1969
1970               if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1971                 cur_param = TREE_OPERAND (cur_param, 0);
1972               else
1973                 cur_param = 0;
1974
1975               continue;
1976             }
1977           if (TREE_CODE (cur_type) != ERROR_MARK)
1978             warning ((fci->pointer_count + aflag == 1
1979                       ? "format argument is not a pointer (arg %d)"
1980                       : "format argument is not a pointer to a pointer (arg %d)"),
1981                      arg_num);
1982           break;
1983         }
1984
1985       /* See if this is an attempt to write into a const type with
1986          scanf or with printf "%n".  */
1987       if ((info->format_type == scanf_format_type
1988            || (info->format_type == printf_format_type
1989                && format_char == 'n'))
1990           && i == fci->pointer_count + aflag
1991           && wanted_type != 0
1992           && TREE_CODE (cur_type) != ERROR_MARK
1993           && (TYPE_READONLY (cur_type)
1994               || (cur_param != 0
1995                   && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1996                       || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1997                           && TREE_READONLY (cur_param))))))
1998         warning ("writing into constant object (arg %d)", arg_num);
1999
2000       /* Check the type of the "real" argument, if there's a type we want.  */
2001       if (i == fci->pointer_count + aflag && wanted_type != 0
2002           && TREE_CODE (cur_type) != ERROR_MARK
2003           && wanted_type != TYPE_MAIN_VARIANT (cur_type)
2004           /* If we want `void *', allow any pointer type.
2005              (Anything else would already have got a warning.)  */
2006           && ! (wanted_type == void_type_node
2007                 && fci->pointer_count > 0)
2008           /* Don't warn about differences merely in signedness.  */
2009           && !(TREE_CODE (wanted_type) == INTEGER_TYPE
2010                && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
2011                && (TREE_UNSIGNED (wanted_type)
2012                    ? wanted_type == (cur_type = unsigned_type (cur_type))
2013                    : wanted_type == (cur_type = signed_type (cur_type))))
2014           /* Likewise, "signed char", "unsigned char" and "char" are
2015              equivalent but the above test won't consider them equivalent.  */
2016           && ! (wanted_type == char_type_node
2017                 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
2018                     || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
2019         {
2020           register const char *this;
2021           register const char *that;
2022
2023           this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2024           that = 0;
2025           if (TREE_CODE (cur_type) != ERROR_MARK
2026               && TYPE_NAME (cur_type) != 0
2027               && TREE_CODE (cur_type) != INTEGER_TYPE
2028               && !(TREE_CODE (cur_type) == POINTER_TYPE
2029                    && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
2030             {
2031               if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
2032                   && DECL_NAME (TYPE_NAME (cur_type)) != 0)
2033                 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2034               else
2035                 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
2036             }
2037
2038           /* A nameless type can't possibly match what the format wants.
2039              So there will be a warning for it.
2040              Make up a string to describe vaguely what it is.  */
2041           if (that == 0)
2042             {
2043               if (TREE_CODE (cur_type) == POINTER_TYPE)
2044                 that = "pointer";
2045               else
2046                 that = "different type";
2047             }
2048
2049           /* Make the warning better in case of mismatch of int vs long.  */
2050           if (TREE_CODE (cur_type) == INTEGER_TYPE
2051               && TREE_CODE (wanted_type) == INTEGER_TYPE
2052               && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
2053               && TYPE_NAME (cur_type) != 0
2054               && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
2055             that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2056
2057           if (strcmp (this, that) != 0)
2058             warning ("%s format, %s arg (arg %d)", this, that, arg_num);
2059         }
2060     }
2061 }
2062 \f
2063 /* Print a warning if a constant expression had overflow in folding.
2064    Invoke this function on every expression that the language
2065    requires to be a constant expression.
2066    Note the ANSI C standard says it is erroneous for a
2067    constant expression to overflow.  */
2068
2069 void
2070 constant_expression_warning (value)
2071      tree value;
2072 {
2073   if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2074        || TREE_CODE (value) == COMPLEX_CST)
2075       && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2076     pedwarn ("overflow in constant expression");
2077 }
2078
2079 /* Print a warning if an expression had overflow in folding.
2080    Invoke this function on every expression that
2081    (1) appears in the source code, and
2082    (2) might be a constant expression that overflowed, and
2083    (3) is not already checked by convert_and_check;
2084    however, do not invoke this function on operands of explicit casts.  */
2085
2086 void
2087 overflow_warning (value)
2088      tree value;
2089 {
2090   if ((TREE_CODE (value) == INTEGER_CST
2091        || (TREE_CODE (value) == COMPLEX_CST
2092            && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2093       && TREE_OVERFLOW (value))
2094     {
2095       TREE_OVERFLOW (value) = 0;
2096       if (skip_evaluation == 0)
2097         warning ("integer overflow in expression");
2098     }
2099   else if ((TREE_CODE (value) == REAL_CST
2100             || (TREE_CODE (value) == COMPLEX_CST
2101                 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2102            && TREE_OVERFLOW (value))
2103     {
2104       TREE_OVERFLOW (value) = 0;
2105       if (skip_evaluation == 0)
2106         warning ("floating point overflow in expression");
2107     }
2108 }
2109
2110 /* Print a warning if a large constant is truncated to unsigned,
2111    or if -Wconversion is used and a constant < 0 is converted to unsigned.
2112    Invoke this function on every expression that might be implicitly
2113    converted to an unsigned type.  */
2114
2115 void
2116 unsigned_conversion_warning (result, operand)
2117      tree result, operand;
2118 {
2119   if (TREE_CODE (operand) == INTEGER_CST
2120       && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2121       && TREE_UNSIGNED (TREE_TYPE (result))
2122       && skip_evaluation == 0
2123       && !int_fits_type_p (operand, TREE_TYPE (result)))
2124     {
2125       if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2126         /* This detects cases like converting -129 or 256 to unsigned char.  */
2127         warning ("large integer implicitly truncated to unsigned type");
2128       else if (warn_conversion)
2129         warning ("negative integer implicitly converted to unsigned type");
2130     }
2131 }
2132
2133 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2134    Invoke this function on every expression that is converted implicitly,
2135    i.e. because of language rules and not because of an explicit cast.  */
2136
2137 tree
2138 convert_and_check (type, expr)
2139      tree type, expr;
2140 {
2141   tree t = convert (type, expr);
2142   if (TREE_CODE (t) == INTEGER_CST)
2143     {
2144       if (TREE_OVERFLOW (t))
2145         {
2146           TREE_OVERFLOW (t) = 0;
2147
2148           /* Do not diagnose overflow in a constant expression merely
2149              because a conversion overflowed.  */
2150           TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2151
2152           /* No warning for converting 0x80000000 to int.  */
2153           if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2154                 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2155                 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2156             /* If EXPR fits in the unsigned version of TYPE,
2157                don't warn unless pedantic.  */
2158             if ((pedantic
2159                  || TREE_UNSIGNED (type)
2160                  || ! int_fits_type_p (expr, unsigned_type (type)))
2161                 && skip_evaluation == 0)
2162               warning ("overflow in implicit constant conversion");
2163         }
2164       else
2165         unsigned_conversion_warning (t, expr);
2166     }
2167   return t;
2168 }
2169 \f
2170 void
2171 c_expand_expr_stmt (expr)
2172      tree expr;
2173 {
2174   /* Do default conversion if safe and possibly important,
2175      in case within ({...}).  */
2176   if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2177       || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2178     expr = default_conversion (expr);
2179
2180   if (TREE_TYPE (expr) != error_mark_node
2181       && TYPE_SIZE (TREE_TYPE (expr)) == 0
2182       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2183     error ("expression statement has incomplete type");
2184
2185   expand_expr_stmt (expr);
2186 }
2187 \f
2188 /* Validate the expression after `case' and apply default promotions.  */
2189
2190 tree
2191 check_case_value (value)
2192      tree value;
2193 {
2194   if (value == NULL_TREE)
2195     return value;
2196
2197   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
2198   STRIP_TYPE_NOPS (value);
2199
2200   if (TREE_CODE (value) != INTEGER_CST
2201       && value != error_mark_node)
2202     {
2203       error ("case label does not reduce to an integer constant");
2204       value = error_mark_node;
2205     }
2206   else
2207     /* Promote char or short to int.  */
2208     value = default_conversion (value);
2209
2210   constant_expression_warning (value);
2211
2212   return value;
2213 }
2214 \f
2215 /* Return an integer type with BITS bits of precision,
2216    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
2217
2218 tree
2219 type_for_size (bits, unsignedp)
2220      unsigned bits;
2221      int unsignedp;
2222 {
2223   if (bits == TYPE_PRECISION (integer_type_node))
2224     return unsignedp ? unsigned_type_node : integer_type_node;
2225
2226   if (bits == TYPE_PRECISION (signed_char_type_node))
2227     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2228
2229   if (bits == TYPE_PRECISION (short_integer_type_node))
2230     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2231
2232   if (bits == TYPE_PRECISION (long_integer_type_node))
2233     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2234
2235   if (bits == TYPE_PRECISION (long_long_integer_type_node))
2236     return (unsignedp ? long_long_unsigned_type_node
2237             : long_long_integer_type_node);
2238
2239   if (bits <= TYPE_PRECISION (intQI_type_node))
2240     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2241
2242   if (bits <= TYPE_PRECISION (intHI_type_node))
2243     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2244
2245   if (bits <= TYPE_PRECISION (intSI_type_node))
2246     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2247
2248   if (bits <= TYPE_PRECISION (intDI_type_node))
2249     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2250
2251   return 0;
2252 }
2253
2254 /* Return a data type that has machine mode MODE.
2255    If the mode is an integer,
2256    then UNSIGNEDP selects between signed and unsigned types.  */
2257
2258 tree
2259 type_for_mode (mode, unsignedp)
2260      enum machine_mode mode;
2261      int unsignedp;
2262 {
2263   if (mode == TYPE_MODE (integer_type_node))
2264     return unsignedp ? unsigned_type_node : integer_type_node;
2265
2266   if (mode == TYPE_MODE (signed_char_type_node))
2267     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2268
2269   if (mode == TYPE_MODE (short_integer_type_node))
2270     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2271
2272   if (mode == TYPE_MODE (long_integer_type_node))
2273     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2274
2275   if (mode == TYPE_MODE (long_long_integer_type_node))
2276     return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2277
2278   if (mode == TYPE_MODE (intQI_type_node))
2279     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2280
2281   if (mode == TYPE_MODE (intHI_type_node))
2282     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2283
2284   if (mode == TYPE_MODE (intSI_type_node))
2285     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2286
2287   if (mode == TYPE_MODE (intDI_type_node))
2288     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2289
2290 #if HOST_BITS_PER_WIDE_INT >= 64
2291   if (mode == TYPE_MODE (intTI_type_node))
2292     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2293 #endif
2294
2295   if (mode == TYPE_MODE (float_type_node))
2296     return float_type_node;
2297
2298   if (mode == TYPE_MODE (double_type_node))
2299     return double_type_node;
2300
2301   if (mode == TYPE_MODE (long_double_type_node))
2302     return long_double_type_node;
2303
2304   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2305     return build_pointer_type (char_type_node);
2306
2307   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2308     return build_pointer_type (integer_type_node);
2309
2310   return 0;
2311 }
2312 \f
2313 /* Return the minimum number of bits needed to represent VALUE in a
2314    signed or unsigned type, UNSIGNEDP says which.  */
2315
2316 int
2317 min_precision (value, unsignedp)
2318      tree value;
2319      int unsignedp;
2320 {
2321   int log;
2322
2323   /* If the value is negative, compute its negative minus 1.  The latter
2324      adjustment is because the absolute value of the largest negative value
2325      is one larger than the largest positive value.  This is equivalent to
2326      a bit-wise negation, so use that operation instead.  */
2327
2328   if (tree_int_cst_sgn (value) < 0)
2329     value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2330
2331   /* Return the number of bits needed, taking into account the fact
2332      that we need one more bit for a signed than unsigned type.  */
2333
2334   if (integer_zerop (value))
2335     log = 0;
2336   else if (TREE_INT_CST_HIGH (value) != 0)
2337     log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2338   else
2339     log = floor_log2 (TREE_INT_CST_LOW (value));
2340
2341   return log + 1 + ! unsignedp;
2342 }
2343 \f
2344 /* Print an error message for invalid operands to arith operation CODE.
2345    NOP_EXPR is used as a special case (see truthvalue_conversion).  */
2346
2347 void
2348 binary_op_error (code)
2349      enum tree_code code;
2350 {
2351   register const char *opname;
2352
2353   switch (code)
2354     {
2355     case NOP_EXPR:
2356       error ("invalid truth-value expression");
2357       return;
2358
2359     case PLUS_EXPR:
2360       opname = "+"; break;
2361     case MINUS_EXPR:
2362       opname = "-"; break;
2363     case MULT_EXPR:
2364       opname = "*"; break;
2365     case MAX_EXPR:
2366       opname = "max"; break;
2367     case MIN_EXPR:
2368       opname = "min"; break;
2369     case EQ_EXPR:
2370       opname = "=="; break;
2371     case NE_EXPR:
2372       opname = "!="; break;
2373     case LE_EXPR:
2374       opname = "<="; break;
2375     case GE_EXPR:
2376       opname = ">="; break;
2377     case LT_EXPR:
2378       opname = "<"; break;
2379     case GT_EXPR:
2380       opname = ">"; break;
2381     case LSHIFT_EXPR:
2382       opname = "<<"; break;
2383     case RSHIFT_EXPR:
2384       opname = ">>"; break;
2385     case TRUNC_MOD_EXPR:
2386     case FLOOR_MOD_EXPR:
2387       opname = "%"; break;
2388     case TRUNC_DIV_EXPR:
2389     case FLOOR_DIV_EXPR:
2390       opname = "/"; break;
2391     case BIT_AND_EXPR:
2392       opname = "&"; break;
2393     case BIT_IOR_EXPR:
2394       opname = "|"; break;
2395     case TRUTH_ANDIF_EXPR:
2396       opname = "&&"; break;
2397     case TRUTH_ORIF_EXPR:
2398       opname = "||"; break;
2399     case BIT_XOR_EXPR:
2400       opname = "^"; break;
2401     case LROTATE_EXPR:
2402     case RROTATE_EXPR:
2403       opname = "rotate"; break;
2404     default:
2405       opname = "unknown"; break;
2406     }
2407   error ("invalid operands to binary %s", opname);
2408 }
2409 \f
2410 /* Subroutine of build_binary_op, used for comparison operations.
2411    See if the operands have both been converted from subword integer types
2412    and, if so, perhaps change them both back to their original type.
2413    This function is also responsible for converting the two operands
2414    to the proper common type for comparison.
2415
2416    The arguments of this function are all pointers to local variables
2417    of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2418    RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2419
2420    If this function returns nonzero, it means that the comparison has
2421    a constant value.  What this function returns is an expression for
2422    that value.  */
2423
2424 tree
2425 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2426      tree *op0_ptr, *op1_ptr;
2427      tree *restype_ptr;
2428      enum tree_code *rescode_ptr;
2429 {
2430   register tree type;
2431   tree op0 = *op0_ptr;
2432   tree op1 = *op1_ptr;
2433   int unsignedp0, unsignedp1;
2434   int real1, real2;
2435   tree primop0, primop1;
2436   enum tree_code code = *rescode_ptr;
2437
2438   /* Throw away any conversions to wider types
2439      already present in the operands.  */
2440
2441   primop0 = get_narrower (op0, &unsignedp0);
2442   primop1 = get_narrower (op1, &unsignedp1);
2443
2444   /* Handle the case that OP0 does not *contain* a conversion
2445      but it *requires* conversion to FINAL_TYPE.  */
2446
2447   if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2448     unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2449   if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2450     unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2451
2452   /* If one of the operands must be floated, we cannot optimize.  */
2453   real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2454   real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2455
2456   /* If first arg is constant, swap the args (changing operation
2457      so value is preserved), for canonicalization.  Don't do this if
2458      the second arg is 0.  */
2459
2460   if (TREE_CONSTANT (primop0)
2461       && ! integer_zerop (primop1) && ! real_zerop (primop1))
2462     {
2463       register tree tem = primop0;
2464       register int temi = unsignedp0;
2465       primop0 = primop1;
2466       primop1 = tem;
2467       tem = op0;
2468       op0 = op1;
2469       op1 = tem;
2470       *op0_ptr = op0;
2471       *op1_ptr = op1;
2472       unsignedp0 = unsignedp1;
2473       unsignedp1 = temi;
2474       temi = real1;
2475       real1 = real2;
2476       real2 = temi;
2477
2478       switch (code)
2479         {
2480         case LT_EXPR:
2481           code = GT_EXPR;
2482           break;
2483         case GT_EXPR:
2484           code = LT_EXPR;
2485           break;
2486         case LE_EXPR:
2487           code = GE_EXPR;
2488           break;
2489         case GE_EXPR:
2490           code = LE_EXPR;
2491           break;
2492         default:
2493           break;
2494         }
2495       *rescode_ptr = code;
2496     }
2497
2498   /* If comparing an integer against a constant more bits wide,
2499      maybe we can deduce a value of 1 or 0 independent of the data.
2500      Or else truncate the constant now
2501      rather than extend the variable at run time.
2502
2503      This is only interesting if the constant is the wider arg.
2504      Also, it is not safe if the constant is unsigned and the
2505      variable arg is signed, since in this case the variable
2506      would be sign-extended and then regarded as unsigned.
2507      Our technique fails in this case because the lowest/highest
2508      possible unsigned results don't follow naturally from the
2509      lowest/highest possible values of the variable operand.
2510      For just EQ_EXPR and NE_EXPR there is another technique that
2511      could be used: see if the constant can be faithfully represented
2512      in the other operand's type, by truncating it and reextending it
2513      and see if that preserves the constant's value.  */
2514
2515   if (!real1 && !real2
2516       && TREE_CODE (primop1) == INTEGER_CST
2517       && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2518     {
2519       int min_gt, max_gt, min_lt, max_lt;
2520       tree maxval, minval;
2521       /* 1 if comparison is nominally unsigned.  */
2522       int unsignedp = TREE_UNSIGNED (*restype_ptr);
2523       tree val;
2524
2525       type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2526
2527       /* If TYPE is an enumeration, then we need to get its min/max
2528          values from it's underlying integral type, not the enumerated
2529          type itself.  */
2530       if (TREE_CODE (type) == ENUMERAL_TYPE)
2531         type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2532
2533       maxval = TYPE_MAX_VALUE (type);
2534       minval = TYPE_MIN_VALUE (type);
2535
2536       if (unsignedp && !unsignedp0)
2537         *restype_ptr = signed_type (*restype_ptr);
2538
2539       if (TREE_TYPE (primop1) != *restype_ptr)
2540         primop1 = convert (*restype_ptr, primop1);
2541       if (type != *restype_ptr)
2542         {
2543           minval = convert (*restype_ptr, minval);
2544           maxval = convert (*restype_ptr, maxval);
2545         }
2546
2547       if (unsignedp && unsignedp0)
2548         {
2549           min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2550           max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2551           min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2552           max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2553         }
2554       else
2555         {
2556           min_gt = INT_CST_LT (primop1, minval);
2557           max_gt = INT_CST_LT (primop1, maxval);
2558           min_lt = INT_CST_LT (minval, primop1);
2559           max_lt = INT_CST_LT (maxval, primop1);
2560         }
2561
2562       val = 0;
2563       /* This used to be a switch, but Genix compiler can't handle that.  */
2564       if (code == NE_EXPR)
2565         {
2566           if (max_lt || min_gt)
2567             val = boolean_true_node;
2568         }
2569       else if (code == EQ_EXPR)
2570         {
2571           if (max_lt || min_gt)
2572             val = boolean_false_node;
2573         }
2574       else if (code == LT_EXPR)
2575         {
2576           if (max_lt)
2577             val = boolean_true_node;
2578           if (!min_lt)
2579             val = boolean_false_node;
2580         }
2581       else if (code == GT_EXPR)
2582         {
2583           if (min_gt)
2584             val = boolean_true_node;
2585           if (!max_gt)
2586             val = boolean_false_node;
2587         }
2588       else if (code == LE_EXPR)
2589         {
2590           if (!max_gt)
2591             val = boolean_true_node;
2592           if (min_gt)
2593             val = boolean_false_node;
2594         }
2595       else if (code == GE_EXPR)
2596         {
2597           if (!min_lt)
2598             val = boolean_true_node;
2599           if (max_lt)
2600             val = boolean_false_node;
2601         }
2602
2603       /* If primop0 was sign-extended and unsigned comparison specd,
2604          we did a signed comparison above using the signed type bounds.
2605          But the comparison we output must be unsigned.
2606
2607          Also, for inequalities, VAL is no good; but if the signed
2608          comparison had *any* fixed result, it follows that the
2609          unsigned comparison just tests the sign in reverse
2610          (positive values are LE, negative ones GE).
2611          So we can generate an unsigned comparison
2612          against an extreme value of the signed type.  */
2613
2614       if (unsignedp && !unsignedp0)
2615         {
2616           if (val != 0)
2617             switch (code)
2618               {
2619               case LT_EXPR:
2620               case GE_EXPR:
2621                 primop1 = TYPE_MIN_VALUE (type);
2622                 val = 0;
2623                 break;
2624
2625               case LE_EXPR:
2626               case GT_EXPR:
2627                 primop1 = TYPE_MAX_VALUE (type);
2628                 val = 0;
2629                 break;
2630
2631               default:
2632                 break;
2633               }
2634           type = unsigned_type (type);
2635         }
2636
2637       if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2638         {
2639           /* This is the case of (char)x >?< 0x80, which people used to use
2640              expecting old C compilers to change the 0x80 into -0x80.  */
2641           if (val == boolean_false_node)
2642             warning ("comparison is always false due to limited range of data type");
2643           if (val == boolean_true_node)
2644             warning ("comparison is always true due to limited range of data type");
2645         }
2646
2647       if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2648         {
2649           /* This is the case of (unsigned char)x >?< -1 or < 0.  */
2650           if (val == boolean_false_node)
2651             warning ("comparison is always false due to limited range of data type");
2652           if (val == boolean_true_node)
2653             warning ("comparison is always true due to limited range of data type");
2654         }
2655
2656       if (val != 0)
2657         {
2658           /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
2659           if (TREE_SIDE_EFFECTS (primop0))
2660             return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2661           return val;
2662         }
2663
2664       /* Value is not predetermined, but do the comparison
2665          in the type of the operand that is not constant.
2666          TYPE is already properly set.  */
2667     }
2668   else if (real1 && real2
2669            && (TYPE_PRECISION (TREE_TYPE (primop0))
2670                == TYPE_PRECISION (TREE_TYPE (primop1))))
2671     type = TREE_TYPE (primop0);
2672
2673   /* If args' natural types are both narrower than nominal type
2674      and both extend in the same manner, compare them
2675      in the type of the wider arg.
2676      Otherwise must actually extend both to the nominal
2677      common type lest different ways of extending
2678      alter the result.
2679      (eg, (short)-1 == (unsigned short)-1  should be 0.)  */
2680
2681   else if (unsignedp0 == unsignedp1 && real1 == real2
2682            && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2683            && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2684     {
2685       type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2686       type = signed_or_unsigned_type (unsignedp0
2687                                       || TREE_UNSIGNED (*restype_ptr),
2688                                       type);
2689       /* Make sure shorter operand is extended the right way
2690          to match the longer operand.  */
2691       primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2692                          primop0);
2693       primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2694                          primop1);
2695     }
2696   else
2697     {
2698       /* Here we must do the comparison on the nominal type
2699          using the args exactly as we received them.  */
2700       type = *restype_ptr;
2701       primop0 = op0;
2702       primop1 = op1;
2703
2704       if (!real1 && !real2 && integer_zerop (primop1)
2705           && TREE_UNSIGNED (*restype_ptr))
2706         {
2707           tree value = 0;
2708           switch (code)
2709             {
2710             case GE_EXPR:
2711               /* All unsigned values are >= 0, so we warn if extra warnings
2712                  are requested.  However, if OP0 is a constant that is
2713                  >= 0, the signedness of the comparison isn't an issue,
2714                  so suppress the warning.  */
2715               if (extra_warnings
2716                   && ! (TREE_CODE (primop0) == INTEGER_CST
2717                         && ! TREE_OVERFLOW (convert (signed_type (type),
2718                                                      primop0))))
2719                 warning ("comparison of unsigned expression >= 0 is always true");
2720               value = boolean_true_node;
2721               break;
2722
2723             case LT_EXPR:
2724               if (extra_warnings
2725                   && ! (TREE_CODE (primop0) == INTEGER_CST
2726                         && ! TREE_OVERFLOW (convert (signed_type (type),
2727                                                      primop0))))
2728                 warning ("comparison of unsigned expression < 0 is always false");
2729               value = boolean_false_node;
2730               break;
2731
2732             default:
2733               break;
2734             }
2735
2736           if (value != 0)
2737             {
2738               /* Don't forget to evaluate PRIMOP0 if it has side effects.  */
2739               if (TREE_SIDE_EFFECTS (primop0))
2740                 return build (COMPOUND_EXPR, TREE_TYPE (value),
2741                               primop0, value);
2742               return value;
2743             }
2744         }
2745     }
2746
2747   *op0_ptr = convert (type, primop0);
2748   *op1_ptr = convert (type, primop1);
2749
2750   *restype_ptr = boolean_type_node;
2751
2752   return 0;
2753 }
2754 \f
2755 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2756    or validate its data type for an `if' or `while' statement or ?..: exp.
2757
2758    This preparation consists of taking the ordinary
2759    representation of an expression expr and producing a valid tree
2760    boolean expression describing whether expr is nonzero.  We could
2761    simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2762    but we optimize comparisons, &&, ||, and !.
2763
2764    The resulting type should always be `boolean_type_node'.  */
2765
2766 tree
2767 truthvalue_conversion (expr)
2768      tree expr;
2769 {
2770   if (TREE_CODE (expr) == ERROR_MARK)
2771     return expr;
2772
2773 #if 0 /* This appears to be wrong for C++.  */
2774   /* These really should return error_mark_node after 2.4 is stable.
2775      But not all callers handle ERROR_MARK properly.  */
2776   switch (TREE_CODE (TREE_TYPE (expr)))
2777     {
2778     case RECORD_TYPE:
2779       error ("struct type value used where scalar is required");
2780       return boolean_false_node;
2781
2782     case UNION_TYPE:
2783       error ("union type value used where scalar is required");
2784       return boolean_false_node;
2785
2786     case ARRAY_TYPE:
2787       error ("array type value used where scalar is required");
2788       return boolean_false_node;
2789
2790     default:
2791       break;
2792     }
2793 #endif /* 0 */
2794
2795   switch (TREE_CODE (expr))
2796     {
2797       /* It is simpler and generates better code to have only TRUTH_*_EXPR
2798          or comparison expressions as truth values at this level.  */
2799 #if 0
2800     case COMPONENT_REF:
2801       /* A one-bit unsigned bit-field is already acceptable.  */
2802       if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2803           && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2804         return expr;
2805       break;
2806 #endif
2807
2808     case EQ_EXPR:
2809       /* It is simpler and generates better code to have only TRUTH_*_EXPR
2810          or comparison expressions as truth values at this level.  */
2811 #if 0
2812       if (integer_zerop (TREE_OPERAND (expr, 1)))
2813         return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2814 #endif
2815     case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2816     case TRUTH_ANDIF_EXPR:
2817     case TRUTH_ORIF_EXPR:
2818     case TRUTH_AND_EXPR:
2819     case TRUTH_OR_EXPR:
2820     case TRUTH_XOR_EXPR:
2821     case TRUTH_NOT_EXPR:
2822       TREE_TYPE (expr) = boolean_type_node;
2823       return expr;
2824
2825     case ERROR_MARK:
2826       return expr;
2827
2828     case INTEGER_CST:
2829       return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2830
2831     case REAL_CST:
2832       return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2833
2834     case ADDR_EXPR:
2835       /* If we are taking the address of a external decl, it might be zero
2836          if it is weak, so we cannot optimize.  */
2837       if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2838           && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2839         break;
2840
2841       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2842         return build (COMPOUND_EXPR, boolean_type_node,
2843                       TREE_OPERAND (expr, 0), boolean_true_node);
2844       else
2845         return boolean_true_node;
2846
2847     case COMPLEX_EXPR:
2848       return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2849                                ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2850                               truthvalue_conversion (TREE_OPERAND (expr, 0)),
2851                               truthvalue_conversion (TREE_OPERAND (expr, 1)),
2852                               0);
2853
2854     case NEGATE_EXPR:
2855     case ABS_EXPR:
2856     case FLOAT_EXPR:
2857     case FFS_EXPR:
2858       /* These don't change whether an object is non-zero or zero.  */
2859       return truthvalue_conversion (TREE_OPERAND (expr, 0));
2860
2861     case LROTATE_EXPR:
2862     case RROTATE_EXPR:
2863       /* These don't change whether an object is zero or non-zero, but
2864          we can't ignore them if their second arg has side-effects.  */
2865       if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2866         return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2867                       truthvalue_conversion (TREE_OPERAND (expr, 0)));
2868       else
2869         return truthvalue_conversion (TREE_OPERAND (expr, 0));
2870
2871     case COND_EXPR:
2872       /* Distribute the conversion into the arms of a COND_EXPR.  */
2873       return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2874                           truthvalue_conversion (TREE_OPERAND (expr, 1)),
2875                           truthvalue_conversion (TREE_OPERAND (expr, 2))));
2876
2877     case CONVERT_EXPR:
2878       /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2879          since that affects how `default_conversion' will behave.  */
2880       if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2881           || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2882         break;
2883       /* fall through...  */
2884     case NOP_EXPR:
2885       /* If this is widening the argument, we can ignore it.  */
2886       if (TYPE_PRECISION (TREE_TYPE (expr))
2887           >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2888         return truthvalue_conversion (TREE_OPERAND (expr, 0));
2889       break;
2890
2891     case MINUS_EXPR:
2892       /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2893          this case.  */
2894       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2895           && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2896         break;
2897       /* fall through...  */
2898     case BIT_XOR_EXPR:
2899       /* This and MINUS_EXPR can be changed into a comparison of the
2900          two objects.  */
2901       if (TREE_TYPE (TREE_OPERAND (expr, 0))
2902           == TREE_TYPE (TREE_OPERAND (expr, 1)))
2903         return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2904                                 TREE_OPERAND (expr, 1), 1);
2905       return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2906                               fold (build1 (NOP_EXPR,
2907                                             TREE_TYPE (TREE_OPERAND (expr, 0)),
2908                                             TREE_OPERAND (expr, 1))), 1);
2909
2910     case BIT_AND_EXPR:
2911       if (integer_onep (TREE_OPERAND (expr, 1))
2912           && TREE_TYPE (expr) != boolean_type_node)
2913         /* Using convert here would cause infinite recursion.  */
2914         return build1 (NOP_EXPR, boolean_type_node, expr);
2915       break;
2916
2917     case MODIFY_EXPR:
2918       if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2919         warning ("suggest parentheses around assignment used as truth value");
2920       break;
2921
2922     default:
2923       break;
2924     }
2925
2926   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2927     {
2928       tree tem = save_expr (expr);
2929       return (build_binary_op
2930               ((TREE_SIDE_EFFECTS (expr)
2931                 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2932                truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2933                truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2934                0));
2935     }
2936
2937   return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2938 }
2939 \f
2940 #if USE_CPPLIB
2941 /* Read the rest of a #-directive from input stream FINPUT.
2942    In normal use, the directive name and the white space after it
2943    have already been read, so they won't be included in the result.
2944    We allow for the fact that the directive line may contain
2945    a newline embedded within a character or string literal which forms
2946    a part of the directive.
2947
2948    The value is a string in a reusable buffer.  It remains valid
2949    only until the next time this function is called.  */
2950 unsigned char *yy_cur, *yy_lim;
2951
2952 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2953 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
2954
2955 int
2956 yy_get_token ()
2957 {
2958   for (;;)
2959     {
2960       parse_in.limit = parse_in.token_buffer;
2961       cpp_token = cpp_get_token (&parse_in);
2962       if (cpp_token == CPP_EOF)
2963         return -1;
2964       yy_lim = CPP_PWRITTEN (&parse_in);
2965       yy_cur = parse_in.token_buffer;
2966       if (yy_cur < yy_lim)
2967         return *yy_cur++;
2968     }
2969 }
2970
2971 char *
2972 get_directive_line ()
2973 {
2974   static char *directive_buffer = NULL;
2975   static unsigned buffer_length = 0;
2976   register char *p;
2977   register char *buffer_limit;
2978   register int looking_for = 0;
2979   register int char_escaped = 0;
2980
2981   if (buffer_length == 0)
2982     {
2983       directive_buffer = (char *)xmalloc (128);
2984       buffer_length = 128;
2985     }
2986
2987   buffer_limit = &directive_buffer[buffer_length];
2988
2989   for (p = directive_buffer; ; )
2990     {
2991       int c;
2992
2993       /* Make buffer bigger if it is full.  */
2994       if (p >= buffer_limit)
2995         {
2996           register unsigned bytes_used = (p - directive_buffer);
2997
2998           buffer_length *= 2;
2999           directive_buffer
3000             = (char *)xrealloc (directive_buffer, buffer_length);
3001           p = &directive_buffer[bytes_used];
3002           buffer_limit = &directive_buffer[buffer_length];
3003         }
3004
3005       c = GETC ();
3006
3007       /* Discard initial whitespace.  */
3008       if ((c == ' ' || c == '\t') && p == directive_buffer)
3009         continue;
3010
3011       /* Detect the end of the directive.  */
3012       if (c == '\n' && looking_for == 0)
3013         {
3014           UNGETC (c);
3015           c = '\0';
3016         }
3017
3018       *p++ = c;
3019
3020       if (c == 0)
3021         return directive_buffer;
3022
3023       /* Handle string and character constant syntax.  */
3024       if (looking_for)
3025         {
3026           if (looking_for == c && !char_escaped)
3027             looking_for = 0;    /* Found terminator... stop looking.  */
3028         }
3029       else
3030         if (c == '\'' || c == '"')
3031           looking_for = c;      /* Don't stop buffering until we see another
3032                                    another one of these (or an EOF).  */
3033
3034       /* Handle backslash.  */
3035       char_escaped = (c == '\\' && ! char_escaped);
3036     }
3037 }
3038 #else
3039 /* Read the rest of a #-directive from input stream FINPUT.
3040    In normal use, the directive name and the white space after it
3041    have already been read, so they won't be included in the result.
3042    We allow for the fact that the directive line may contain
3043    a newline embedded within a character or string literal which forms
3044    a part of the directive.
3045
3046    The value is a string in a reusable buffer.  It remains valid
3047    only until the next time this function is called.
3048
3049    The terminating character ('\n' or EOF) is left in FINPUT for the
3050    caller to re-read.  */
3051
3052 char *
3053 get_directive_line (finput)
3054      register FILE *finput;
3055 {
3056   static char *directive_buffer = NULL;
3057   static unsigned buffer_length = 0;
3058   register char *p;
3059   register char *buffer_limit;
3060   register int looking_for = 0;
3061   register int char_escaped = 0;
3062
3063   if (buffer_length == 0)
3064     {
3065       directive_buffer = (char *)xmalloc (128);
3066       buffer_length = 128;
3067     }
3068
3069   buffer_limit = &directive_buffer[buffer_length];
3070
3071   for (p = directive_buffer; ; )
3072     {
3073       int c;
3074
3075       /* Make buffer bigger if it is full.  */
3076       if (p >= buffer_limit)
3077         {
3078           register unsigned bytes_used = (p - directive_buffer);
3079
3080           buffer_length *= 2;
3081           directive_buffer
3082             = (char *)xrealloc (directive_buffer, buffer_length);
3083           p = &directive_buffer[bytes_used];
3084           buffer_limit = &directive_buffer[buffer_length];
3085         }
3086
3087       c = getc (finput);
3088
3089       /* Discard initial whitespace.  */
3090       if ((c == ' ' || c == '\t') && p == directive_buffer)
3091         continue;
3092
3093       /* Detect the end of the directive.  */
3094       if (looking_for == 0
3095           && (c == '\n' || c == EOF))
3096         {
3097           ungetc (c, finput);
3098           c = '\0';
3099         }
3100
3101       *p++ = c;
3102
3103       if (c == 0)
3104         return directive_buffer;
3105
3106       /* Handle string and character constant syntax.  */
3107       if (looking_for)
3108         {
3109           if (looking_for == c && !char_escaped)
3110             looking_for = 0;    /* Found terminator... stop looking.  */
3111         }
3112       else
3113         if (c == '\'' || c == '"')
3114           looking_for = c;      /* Don't stop buffering until we see another
3115                                    one of these (or an EOF).  */
3116
3117       /* Handle backslash.  */
3118       char_escaped = (c == '\\' && ! char_escaped);
3119     }
3120 }
3121 #endif /* !USE_CPPLIB */
3122 \f
3123 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3124    down to the element type of an array.  */
3125
3126 tree
3127 c_build_qualified_type (type, type_quals)
3128      tree type;
3129      int type_quals;
3130 {
3131   /* A restrict-qualified pointer type must be a pointer to object or
3132      incomplete type.  Note that the use of POINTER_TYPE_P also allows
3133      REFERENCE_TYPEs, which is appropriate for C++.  Unfortunately,
3134      the C++ front-end also use POINTER_TYPE for pointer-to-member
3135      values, so even though it should be illegal to use `restrict'
3136      with such an entity we don't flag that here.  Thus, special case
3137      code for that case is required in the C++ front-end.  */
3138   if ((type_quals & TYPE_QUAL_RESTRICT)
3139       && (!POINTER_TYPE_P (type)
3140           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3141     {
3142       error ("invalid use of `restrict'");
3143       type_quals &= ~TYPE_QUAL_RESTRICT;
3144     }
3145
3146   if (TREE_CODE (type) == ARRAY_TYPE)
3147     return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3148                                                      type_quals),
3149                              TYPE_DOMAIN (type));
3150   return build_qualified_type (type, type_quals);
3151 }
3152
3153 /* Apply the TYPE_QUALS to the new DECL.  */
3154
3155 void
3156 c_apply_type_quals_to_decl (type_quals, decl)
3157      int type_quals;
3158      tree decl;
3159 {
3160   if (type_quals & TYPE_QUAL_CONST)
3161     TREE_READONLY (decl) = 1;
3162   if (type_quals & TYPE_QUAL_VOLATILE)
3163     {
3164       TREE_SIDE_EFFECTS (decl) = 1;
3165       TREE_THIS_VOLATILE (decl) = 1;
3166     }
3167   if (type_quals & TYPE_QUAL_RESTRICT)
3168     {
3169       if (!TREE_TYPE (decl)
3170           || !POINTER_TYPE_P (TREE_TYPE (decl))
3171           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3172         error ("invalid use of `restrict'");
3173       else if (flag_strict_aliasing)
3174         {
3175           /* No two restricted pointers can point at the same thing.
3176              However, a restricted pointer can point at the same thing
3177              as an unrestricted pointer, if that unrestricted pointer
3178              is based on the restricted pointer.  So, we make the
3179              alias set for the restricted pointer a subset of the
3180              alias set for the type pointed to by the type of the
3181              decl.  */
3182
3183           int pointed_to_alias_set 
3184             = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3185           
3186           if (!pointed_to_alias_set)
3187             /* It's not legal to make a subset of alias set zero.  */
3188             ;
3189           else
3190             {
3191               DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3192               record_alias_subset  (pointed_to_alias_set,
3193                                     DECL_POINTER_ALIAS_SET (decl));
3194             }
3195         }
3196     }
3197 }
3198
3199 /* T is an expression with pointer type.  Find the DECL on which this
3200    expression is based.  (For example, in `a[i]' this would be `a'.)
3201    If there is no such DECL, or a unique decl cannot be determined,
3202    NULL_TREE is retured.  */
3203
3204 static tree
3205 c_find_base_decl (t)
3206      tree t;
3207 {
3208   int i;
3209   tree decl;
3210
3211   if (t == NULL_TREE || t == error_mark_node)
3212     return NULL_TREE;
3213
3214   if (!POINTER_TYPE_P (TREE_TYPE (t)))
3215     return NULL_TREE;
3216
3217   decl = NULL_TREE;
3218
3219   if (TREE_CODE (t) == FIELD_DECL 
3220       || TREE_CODE (t) == PARM_DECL
3221       || TREE_CODE (t) == VAR_DECL)
3222     /* Aha, we found a pointer-typed declaration.  */
3223     return t;
3224
3225   /* It would be nice to deal with COMPONENT_REFs here.  If we could
3226      tell that `a' and `b' were the same, then `a->f' and `b->f' are
3227      also the same.  */
3228
3229   /* Handle general expressions.  */
3230   switch (TREE_CODE_CLASS (TREE_CODE (t)))
3231     {
3232     case '1':
3233     case '2':
3234     case '3':
3235       for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3236         {
3237           tree d = c_find_base_decl (TREE_OPERAND (t, i));
3238           if (d)
3239             {
3240               if (!decl)
3241                 decl = d;
3242               else if (d && d != decl)
3243                 /* Two different declarations.  That's confusing; let's
3244                    just assume we don't know what's going on.  */
3245                 decl = NULL_TREE;
3246             }
3247         }
3248       break;
3249
3250     default:
3251       break;
3252     }
3253
3254   return decl;
3255 }
3256
3257 /* Return the typed-based alias set for T, which may be an expression
3258    or a type.  */
3259
3260 int
3261 c_get_alias_set (t)
3262      tree t;
3263 {
3264   tree type;
3265   tree u;
3266
3267   if (t == error_mark_node)
3268     return 0;
3269
3270   type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3271     ? t : TREE_TYPE (t);
3272
3273   if (type == error_mark_node)
3274     return 0;
3275
3276   /* Deal with special cases first; for certain kinds of references
3277      we're interested in more than just the type.  */
3278
3279   if (TREE_CODE (t) == BIT_FIELD_REF)
3280     /* Perhaps reads and writes to this piece of data alias fields
3281        neighboring the bitfield.  Perhaps that's impossible.  For now,
3282        let's just assume that bitfields can alias everything, which is
3283        the conservative assumption.  */
3284     return 0;
3285
3286   /* Permit type-punning when accessing a union, provided the access
3287      is directly through the union.  For example, this code does not
3288      permit taking the address of a union member and then storing
3289      through it.  Even the type-punning allowed here is a GCC
3290      extension, albeit a common and useful one; the C standard says
3291      that such accesses have implementation-defined behavior.  */
3292   for (u = t;
3293        TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3294        u = TREE_OPERAND (u, 0))
3295     if (TREE_CODE (u) == COMPONENT_REF
3296         && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3297       return 0;
3298
3299   if (TREE_CODE (t) == INDIRECT_REF)
3300     {
3301       /* Check for accesses through restrict-qualified pointers.  */
3302       tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3303
3304       if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3305         /* We use the alias set indicated in the declaration.  */
3306         return DECL_POINTER_ALIAS_SET (decl);
3307     }
3308
3309   /* From here on, only the type matters.  */
3310
3311   if (TREE_CODE (t) == COMPONENT_REF
3312       && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3313     /* Since build_modify_expr calls get_unwidened for stores to
3314        component references, the type of a bit field can be changed
3315        from (say) `unsigned int : 16' to `unsigned short' or from 
3316        `enum E : 16' to `short'.  We want the real type of the
3317        bit-field in this case, not some the integral equivalent.  */
3318     type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3319
3320   if (TYPE_ALIAS_SET_KNOWN_P (type))
3321     /* If we've already calculated the value, just return it.  */
3322     return TYPE_ALIAS_SET (type);
3323   else if (TYPE_MAIN_VARIANT (type) != type)
3324     /* The C standard specifically allows aliasing between
3325        cv-qualified variants of types.  */
3326     TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3327   else if (TREE_CODE (type) == INTEGER_TYPE)
3328     {
3329       tree signed_variant;
3330
3331       /* The C standard specifically allows aliasing between signed and
3332          unsigned variants of the same type.  We treat the signed
3333          variant as canonical.  */
3334       signed_variant = signed_type (type);
3335
3336       if (signed_variant != type)
3337         TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3338       else if (signed_variant == signed_char_type_node)
3339         /* The C standard guarantess that any object may be accessed
3340            via an lvalue that has character type.  We don't have to
3341            check for unsigned_char_type_node or char_type_node because
3342            we are specifically looking at the signed variant.  */
3343         TYPE_ALIAS_SET (type) = 0;
3344     }
3345   else if (TREE_CODE (type) == ARRAY_TYPE)
3346     /* Anything that can alias one of the array elements can alias
3347        the entire array as well.  */
3348     TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3349   else if (TREE_CODE (type) == FUNCTION_TYPE)
3350     /* There are no objects of FUNCTION_TYPE, so there's no point in
3351        using up an alias set for them.  (There are, of course,
3352        pointers and references to functions, but that's 
3353        different.)  */
3354     TYPE_ALIAS_SET (type) = 0;
3355   else if (TREE_CODE (type) == RECORD_TYPE
3356            || TREE_CODE (type) == UNION_TYPE)
3357     /* If TYPE is a struct or union type then we're reading or
3358        writing an entire struct.  Thus, we don't know anything about
3359        aliasing.  (In theory, such an access can only alias objects
3360        whose type is the same as one of the fields, recursively, but
3361        we don't yet make any use of that information.)  */
3362     TYPE_ALIAS_SET (type) = 0;
3363   else if (TREE_CODE (type) == POINTER_TYPE
3364            || TREE_CODE (type) == REFERENCE_TYPE)
3365     {
3366       tree t;
3367
3368       /* Unfortunately, there is no canonical form of a pointer type.
3369          In particular, if we have `typedef int I', then `int *', and
3370          `I *' are different types.  So, we have to pick a canonical
3371          representative.  We do this below.
3372          
3373          Technically, this approach is actually more conservative that
3374          it needs to be.  In particular, `const int *' and `int *'
3375          chould be in different alias sets, according to the C and C++
3376          standard, since their types are not the same, and so,
3377          technically, an `int **' and `const int **' cannot point at
3378          the same thing.
3379
3380          But, the standard is wrong.  In particular, this code is
3381          legal C++:
3382
3383             int *ip;
3384             int **ipp = &ip;
3385             const int* const* cipp = &ip;
3386
3387          And, it doesn't make sense for that to be legal unless you
3388          can dereference IPP and CIPP.  So, we ignore cv-qualifiers on
3389          the pointed-to types.  This issue has been reported to the
3390          C++ committee.  */
3391       t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3392       t = ((TREE_CODE (type) == POINTER_TYPE)
3393            ? build_pointer_type (t) : build_reference_type (t));
3394       if (t != type)
3395         TYPE_ALIAS_SET (type) = c_get_alias_set (t);
3396     }
3397
3398   if (!TYPE_ALIAS_SET_KNOWN_P (type)) 
3399     {
3400       /* Types that are not allocated on the permanent obstack are not
3401          placed in the type hash table.  Thus, there can be multiple
3402          copies of identical types in local scopes.  In the long run,
3403          all types should be permanent.  */
3404       if (! TREE_PERMANENT (type))
3405         TYPE_ALIAS_SET (type) = 0;
3406       else
3407         /* TYPE is something we haven't seen before.  Put it in a new
3408            alias set.  */
3409         TYPE_ALIAS_SET (type) = new_alias_set ();
3410     }
3411
3412   return TYPE_ALIAS_SET (type);
3413 }