OSDN Git Service

Initial revision
[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 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #include <stdio.h>
19 #include "scan.h"
20
21 #define HASH_SIZE 2503 /* a prime */
22
23 int hash_tab[HASH_SIZE];
24
25 sstring linebuf;
26
27 int
28 main (argc, argv)
29      int argc;
30      char** argv;
31 {
32   FILE *inf = stdin;
33   FILE *outf = stdout;
34   int next_index = 0;
35   int i, i0;
36
37   fprintf (outf, "struct fn_decl std_protos[] = {\n");
38
39   for (;;)
40     {
41       int c = skip_spaces (inf, ' ');
42       int param_nesting = 1;
43       char *param_start, *param_end, *decl_start,
44       *name_start, *name_end;
45       register char *ptr;
46       if (c == EOF)
47         break;
48       linebuf.ptr = linebuf.base;
49       ungetc (c, inf);
50       c = read_upto (inf, &linebuf, '\n');
51       if (linebuf.base[0] == '#') /* skip cpp command */
52         continue;
53       if (linebuf.base[0] == '\0') /* skip empty line */
54         continue;
55
56       ptr = linebuf.ptr - 1;
57       while (*ptr == ' ' || *ptr == '\t') ptr--;
58       if (*ptr-- != ';')
59         {
60           fprintf (stderr, "Funny input line: %s\n", linebuf.base);
61           continue;
62         }
63       while (*ptr == ' ' || *ptr == '\t') ptr--;
64       if (*ptr != ')')
65         {
66           fprintf (stderr, "Funny input line: %s\n", linebuf.base);
67           continue;
68         }
69       param_end = ptr;
70       for (;;)
71         {
72           int c = *--ptr;
73           if (c == '(' && --param_nesting == 0)
74             break;
75           else if (c == ')')
76             param_nesting++;
77         }
78       param_start = ptr+1;
79
80       ptr--;
81       while (*ptr == ' ' || *ptr == '\t') ptr--;
82
83       if (!isalnum (*ptr))
84         {
85           fprintf (stderr, "%s: Can't handle this complex prototype: %s\n",
86                    argv[0], linebuf.base);
87           continue;
88         }
89       name_end = ptr+1;
90
91       while (isalnum (*ptr) || *ptr == '_') --ptr;
92       name_start = ptr+1;
93       while (*ptr == ' ' || *ptr == '\t') ptr--;
94       ptr[1] = 0;
95       *name_end = 0;
96       *param_end = 0;
97       *name_end = 0;
98
99       decl_start = linebuf.base;
100       if (strncmp (decl_start, "typedef ", 8) == 0)
101         continue;
102       if (strncmp (decl_start, "extern ", 7) == 0)
103         decl_start += 7;
104
105
106       /* NOTE:  If you edit this,
107          also edit lookup_std_proto in patch-header.c !! */
108       i = hash(name_start) % HASH_SIZE;
109       i0 = i;
110       if (hash_tab[i] != 0)
111         {
112           for (;;)
113             {
114               i = (i+1) % HASH_SIZE;
115               if (i == i0)
116                 abort();
117               if (hash_tab[i] == 0)
118                 break;
119             }
120         }
121       hash_tab[i] = next_index;
122
123       fprintf (outf, "  {\"%s\", \"%s\", \"%s\" }, /* ix: %d, i0: %d */\n",
124                name_start, decl_start, param_start, i, i0);
125
126       next_index++;
127
128       if (c == EOF)
129         break;
130     }
131   fprintf (outf, "{0, 0, 0}\n};\n");
132
133
134   fprintf (outf, "#define HASH_SIZE %d\n", HASH_SIZE);
135   fprintf (outf, "short hash_tab[HASH_SIZE] = {\n");
136   for (i = 0; i < HASH_SIZE; i++)
137     fprintf (outf, "  %d,\n", hash_tab[i]);
138   fprintf (outf, "};\n");
139
140   return 0;
141 }