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