OSDN Git Service

* Makefile.in (GEN_PROTOS_OBJS): Remove libcpp.a.
[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, 1999 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 #undef abort
22
23 int verbose = 0;
24 const char *progname;
25
26 static void add_hash            PARAMS ((const char *));
27 static int parse_fn_proto       PARAMS ((char *, char *, struct fn_decl *));
28
29 #define HASH_SIZE 2503 /* a prime */
30 int hash_tab[HASH_SIZE];
31 int next_index;
32 int collisions;
33
34 static void
35 add_hash (fname)
36      const char *fname;
37 {
38   int i, i0;
39
40   /* NOTE:  If you edit this, also edit lookup_std_proto in fix-header.c !! */
41   i = hashstr (fname, strlen (fname)) % HASH_SIZE;
42   i0 = i;
43   if (hash_tab[i] != 0)
44     {
45       collisions++;
46       for (;;)
47         {
48           i = (i+1) % HASH_SIZE;
49           if (i == i0)
50             abort ();
51           if (hash_tab[i] == 0)
52             break;
53         }
54     }
55   hash_tab[i] = next_index;
56
57   next_index++;
58 }
59
60 /* Given a function prototype, fill in the fields of FN.
61    The result is a boolean indicating if a function prototype was found.
62
63    The input string is modified (trailing NULs are inserted).
64    The fields of FN point to the input string.  */
65
66 static int
67 parse_fn_proto (start, end, fn)
68      char *start, *end;
69      struct fn_decl *fn;
70 {
71   register char *ptr;
72   int param_nesting = 1;
73   char *param_start, *param_end, *decl_start, *name_start, *name_end;
74
75   ptr = end - 1;
76   while (*ptr == ' ' || *ptr == '\t') ptr--;
77   if (*ptr-- != ';')
78     {
79       fprintf (stderr, "Funny input line: %s\n", start);
80       return 0;
81     }
82   while (*ptr == ' ' || *ptr == '\t') ptr--;
83   if (*ptr != ')')
84     {
85       fprintf (stderr, "Funny input line: %s\n", start);
86       return 0;
87     }
88   param_end = ptr;
89   for (;;)
90     {
91       int c = *--ptr;
92       if (c == '(' && --param_nesting == 0)
93         break;
94       else if (c == ')')
95         param_nesting++;
96     }
97   param_start = ptr+1;
98
99   ptr--;
100   while (*ptr == ' ' || *ptr == '\t') ptr--;
101
102   if (!ISALNUM ((unsigned char)*ptr))
103     {
104       if (verbose)
105         fprintf (stderr, "%s: Can't handle this complex prototype: %s\n",
106                  progname, start);
107       return 0;
108     }
109   name_end = ptr+1;
110
111   while (ISALNUM ((unsigned char)*ptr) || *ptr == '_') --ptr;
112   name_start = ptr+1;
113   while (*ptr == ' ' || *ptr == '\t') ptr--;
114   ptr[1] = 0;
115   *param_end = 0;
116   *name_end = 0;
117
118   decl_start = start;
119   if (strncmp (decl_start, "typedef ", 8) == 0)
120     return 0;
121   if (strncmp (decl_start, "extern ", 7) == 0)
122     decl_start += 7;
123
124   fn->fname = name_start;
125   fn->rtype = decl_start;
126   fn->params = param_start;
127   return 1;
128 }
129
130 extern int main PARAMS ((int, char **));
131
132 int
133 main (argc, argv)
134      int argc ATTRIBUTE_UNUSED;
135      char **argv;
136 {
137   FILE *inf = stdin;
138   FILE *outf = stdout;
139   int i;
140   sstring linebuf;
141   struct fn_decl fn_decl;
142
143   i = strlen (argv[0]);
144   while (i > 0 && argv[0][i-1] != '/') --i;
145   progname = &argv[0][i];
146
147   INIT_SSTRING (&linebuf);
148
149   fprintf (outf, "struct fn_decl std_protos[] = {\n");
150
151   /* A hash table entry of 0 means "unused" so reserve it.  */
152   fprintf (outf, "  {\"\", \"\", \"\", 0},\n");
153   next_index = 1;
154   
155   for (;;)
156     {
157       int c = skip_spaces (inf, ' ');
158
159       if (c == EOF)
160         break;
161       linebuf.ptr = linebuf.base;
162       ungetc (c, inf);
163       c = read_upto (inf, &linebuf, '\n');
164       if (linebuf.base[0] == '#') /* skip cpp command */
165         continue;
166       if (linebuf.base[0] == '\0') /* skip empty line */
167         continue;
168
169       if (! parse_fn_proto (linebuf.base, linebuf.ptr, &fn_decl))
170         continue;
171
172       add_hash (fn_decl.fname);
173
174       fprintf (outf, "  {\"%s\", \"%s\", \"%s\", 0},\n",
175                fn_decl.fname, fn_decl.rtype, fn_decl.params);
176
177       if (c == EOF)
178         break;
179     }
180   fprintf (outf, "  {0, 0, 0, 0}\n};\n");
181
182
183   fprintf (outf, "#define HASH_SIZE %d\n", HASH_SIZE);
184   fprintf (outf, "short hash_tab[HASH_SIZE] = {\n");
185   for (i = 0; i < HASH_SIZE; i++)
186     fprintf (outf, "  %d,\n", hash_tab[i]);
187   fprintf (outf, "};\n");
188
189   fprintf (stderr, "gen-protos: %d entries %d collisions\n",
190            next_index, collisions);
191   
192   return 0;
193 }
194
195 /* Needed by scan.o.  We can't use libiberty here.  */
196 PTR
197 xrealloc (p, s)
198      PTR p;
199      size_t s;
200 {
201   PTR r;
202   if (s == 0)
203     s = 1;
204   if (p)
205     r = realloc (p, s);
206   else
207     r = malloc (s);
208   if (!r)
209     abort ();
210   return r;
211 }