OSDN Git Service

* c-common.c: Convert to using ctype macros defined in system.h.
[pf3gnuchains/gcc-fork.git] / gcc / gen-protos.c
1 /* gen-protos.c - massages a list of prototypes, for use by fixproto.
2    Copyright (C) 1993, 94-96, 1998 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #include "hconfig.h"
19 #include "system.h"
20 #include "scan.h"
21 #include "cpplib.h"
22 #include "cpphash.h"
23
24 int verbose = 0;
25 char *progname;
26
27 #define HASH_SIZE 2503 /* a prime */
28 int hash_tab[HASH_SIZE];
29 int next_index;
30
31 static int
32 hashf (name, len, hashsize)
33      register U_CHAR *name;
34      register int len;
35      int hashsize;
36 {
37   register int r = 0;
38
39   while (len--)
40     r = HASHSTEP (r, *name++);
41
42   return MAKE_POS (r) % hashsize;
43 }
44
45 static void
46 add_hash (fname)
47      char *fname;
48 {
49   int i, i0;
50
51   /* NOTE:  If you edit this, also edit lookup_std_proto in fix-header.c !! */
52   i = hashf (fname, strlen (fname), HASH_SIZE);
53   i0 = i;
54   if (hash_tab[i] != 0)
55     {
56       for (;;)
57         {
58           i = (i+1) % HASH_SIZE;
59           if (i == i0)
60             abort ();
61           if (hash_tab[i] == 0)
62             break;
63         }
64     }
65   hash_tab[i] = next_index;
66
67   next_index++;
68 }
69
70 /* Given a function prototype, fill in the fields of FN.
71    The result is a boolean indicating if a function prototype was found.
72
73    The input string is modified (trailing NULs are inserted).
74    The fields of FN point to the input string.  */
75
76 static int
77 parse_fn_proto (start, end, fn)
78      char *start, *end;
79      struct fn_decl *fn;
80 {
81   register char *ptr;
82   int param_nesting = 1;
83   char *param_start, *param_end, *decl_start, *name_start, *name_end;
84
85   ptr = end - 1;
86   while (*ptr == ' ' || *ptr == '\t') ptr--;
87   if (*ptr-- != ';')
88     {
89       fprintf (stderr, "Funny input line: %s\n", start);
90       return 0;
91     }
92   while (*ptr == ' ' || *ptr == '\t') ptr--;
93   if (*ptr != ')')
94     {
95       fprintf (stderr, "Funny input line: %s\n", start);
96       return 0;
97     }
98   param_end = ptr;
99   for (;;)
100     {
101       int c = *--ptr;
102       if (c == '(' && --param_nesting == 0)
103         break;
104       else if (c == ')')
105         param_nesting++;
106     }
107   param_start = ptr+1;
108
109   ptr--;
110   while (*ptr == ' ' || *ptr == '\t') ptr--;
111
112   if (!ISALNUM (*ptr))
113     {
114       if (verbose)
115         fprintf (stderr, "%s: Can't handle this complex prototype: %s\n",
116                  progname, start);
117       return 0;
118     }
119   name_end = ptr+1;
120
121   while (ISALNUM (*ptr) || *ptr == '_') --ptr;
122   name_start = ptr+1;
123   while (*ptr == ' ' || *ptr == '\t') ptr--;
124   ptr[1] = 0;
125   *param_end = 0;
126   *name_end = 0;
127
128   decl_start = start;
129   if (strncmp (decl_start, "typedef ", 8) == 0)
130     return 0;
131   if (strncmp (decl_start, "extern ", 7) == 0)
132     decl_start += 7;
133
134   fn->fname = name_start;
135   fn->rtype = decl_start;
136   fn->params = param_start;
137   return 1;
138 }
139
140 int
141 main (argc, argv)
142      int argc;
143      char **argv;
144 {
145   FILE *inf = stdin;
146   FILE *outf = stdout;
147   int i;
148   sstring linebuf;
149   struct fn_decl fn_decl;
150
151   i = strlen (argv[0]);
152   while (i > 0 && argv[0][i-1] != '/') --i;
153   progname = &argv[0][i];
154
155   INIT_SSTRING (&linebuf);
156
157   fprintf (outf, "struct fn_decl std_protos[] = {\n");
158
159   /* A hash table entry of 0 means "unused" so reserve it.  */
160   fprintf (outf, "  {\"\", \"\", \"\"},\n");
161   next_index = 1;
162   
163   for (;;)
164     {
165       int c = skip_spaces (inf, ' ');
166
167       if (c == EOF)
168         break;
169       linebuf.ptr = linebuf.base;
170       ungetc (c, inf);
171       c = read_upto (inf, &linebuf, '\n');
172       if (linebuf.base[0] == '#') /* skip cpp command */
173         continue;
174       if (linebuf.base[0] == '\0') /* skip empty line */
175         continue;
176
177       if (! parse_fn_proto (linebuf.base, linebuf.ptr, &fn_decl))
178         continue;
179
180       add_hash (fn_decl.fname);
181
182       fprintf (outf, "  {\"%s\", \"%s\", \"%s\"},\n",
183                fn_decl.fname, fn_decl.rtype, fn_decl.params);
184
185       if (c == EOF)
186         break;
187     }
188   fprintf (outf, "  {0, 0, 0}\n};\n");
189
190
191   fprintf (outf, "#define HASH_SIZE %d\n", HASH_SIZE);
192   fprintf (outf, "short hash_tab[HASH_SIZE] = {\n");
193   for (i = 0; i < HASH_SIZE; i++)
194     fprintf (outf, "  %d,\n", hash_tab[i]);
195   fprintf (outf, "};\n");
196
197   return 0;
198 }
199
200 /* Avoid error if config defines abort as fancy_abort.
201    It's not worth "really" implementing this because ordinary
202    compiler users never run fix-header.  */
203
204 void
205 fancy_abort ()
206 {
207   abort ();
208 }
209
210 void
211 fatal (s)
212      char *s;
213 {
214   fprintf (stderr, "%s: %s\n", "gen-protos", s);
215   exit (FATAL_EXIT_CODE);
216 }