OSDN Git Service

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