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