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