2004-10-07 J"orn Rennecke <joern.rennecke@st.com>
[official-gcc.git] / gcc / gen-protos.c
blob17e435fde77975c182f56f6ab2e4cb3937587840
1 /* gen-protos.c - massages a list of prototypes, for use by fixproto.
2 Copyright (C) 1993, 1994, 1995, 1996, 1998,
3 1999, 2003 Free Software Foundation, Inc.
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.
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.
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. */
19 #include "bconfig.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "tm.h"
23 #include "scan.h"
24 #include "errors.h"
26 int verbose = 0;
28 static void add_hash (const char *);
29 static int parse_fn_proto (char *, char *, struct fn_decl *);
31 #define HASH_SIZE 2503 /* a prime */
32 int hash_tab[HASH_SIZE];
33 int next_index;
34 int collisions;
36 static void
37 add_hash (const char *fname)
39 int i, i0;
41 /* NOTE: If you edit this, also edit lookup_std_proto in fix-header.c !! */
42 i = hashstr (fname, strlen (fname)) % HASH_SIZE;
43 i0 = i;
44 if (hash_tab[i] != 0)
46 collisions++;
47 for (;;)
49 i = (i+1) % HASH_SIZE;
50 gcc_assert (i != i0);
51 if (hash_tab[i] == 0)
52 break;
55 hash_tab[i] = next_index;
57 next_index++;
60 /* Given a function prototype, fill in the fields of FN.
61 The result is a boolean indicating if a function prototype was found.
63 The input string is modified (trailing NULs are inserted).
64 The fields of FN point to the input string. */
66 static int
67 parse_fn_proto (char *start, char *end, struct fn_decl *fn)
69 char *ptr;
70 int param_nesting = 1;
71 char *param_start, *param_end, *decl_start, *name_start, *name_end;
73 ptr = end - 1;
74 while (*ptr == ' ' || *ptr == '\t') ptr--;
75 if (*ptr-- != ';')
77 fprintf (stderr, "Funny input line: %s\n", start);
78 return 0;
80 while (*ptr == ' ' || *ptr == '\t') ptr--;
81 if (*ptr != ')')
83 fprintf (stderr, "Funny input line: %s\n", start);
84 return 0;
86 param_end = ptr;
87 for (;;)
89 int c = *--ptr;
90 if (c == '(' && --param_nesting == 0)
91 break;
92 else if (c == ')')
93 param_nesting++;
95 param_start = ptr+1;
97 ptr--;
98 while (*ptr == ' ' || *ptr == '\t') ptr--;
100 if (!ISALNUM ((unsigned char)*ptr))
102 if (verbose)
103 fprintf (stderr, "%s: Can't handle this complex prototype: %s\n",
104 progname, start);
105 return 0;
107 name_end = ptr+1;
109 while (ISIDNUM (*ptr))
110 --ptr;
111 name_start = ptr+1;
112 while (*ptr == ' ' || *ptr == '\t') ptr--;
113 ptr[1] = 0;
114 *param_end = 0;
115 *name_end = 0;
117 decl_start = start;
118 if (strncmp (decl_start, "typedef ", 8) == 0)
119 return 0;
120 if (strncmp (decl_start, "extern ", 7) == 0)
121 decl_start += 7;
123 fn->fname = name_start;
124 fn->rtype = decl_start;
125 fn->params = param_start;
126 return 1;
130 main (int argc ATTRIBUTE_UNUSED, char **argv)
132 FILE *inf = stdin;
133 FILE *outf = stdout;
134 int i;
135 sstring linebuf;
136 struct fn_decl fn_decl;
138 i = strlen (argv[0]);
139 while (i > 0 && argv[0][i-1] != '/') --i;
140 progname = &argv[0][i];
142 INIT_SSTRING (&linebuf);
144 fprintf (outf, "struct fn_decl std_protos[] = {\n");
146 /* A hash table entry of 0 means "unused" so reserve it. */
147 fprintf (outf, " {\"\", \"\", \"\", 0},\n");
148 next_index = 1;
150 for (;;)
152 int c = skip_spaces (inf, ' ');
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;
164 if (! parse_fn_proto (linebuf.base, linebuf.ptr, &fn_decl))
165 continue;
167 add_hash (fn_decl.fname);
169 fprintf (outf, " {\"%s\", \"%s\", \"%s\", 0},\n",
170 fn_decl.fname, fn_decl.rtype, fn_decl.params);
172 if (c == EOF)
173 break;
175 fprintf (outf, " {0, 0, 0, 0}\n};\n");
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");
184 fprintf (stderr, "gen-protos: %d entries %d collisions\n",
185 next_index, collisions);
187 return 0;