OSDN Git Service

* system.h: Include "safe-ctype.h" instead of <safe-ctype.h>.
[pf3gnuchains/gcc-fork.git] / gcc / genhooks.c
1 /* Process target.def to create initialization macros definition in
2    target-hooks-def.h and documentation in target-hooks.texi.
3    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 #include "bconfig.h"
21 #include "system.h"
22 #include "hashtab.h"
23 #include "errors.h"
24
25 struct hook_desc { const char *doc, *type, *name, *param, *init; };
26 static struct hook_desc hook_array[] = {
27 #define HOOK_VECTOR_1(NAME, FRAGMENT) \
28   { 0, 0, #NAME, 0, 0 },
29 #define DEFHOOKPOD(NAME, DOC, TYPE, INIT) \
30   { DOC, #TYPE, HOOK_PREFIX #NAME, 0, #INIT },
31 #define DEFHOOK(NAME, DOC, TYPE, PARAMS, INIT) \
32   { DOC, #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT },
33 #define DEFHOOK_UNDOC(NAME, DOC, TYPE, PARAMS, INIT) \
34   { "*", #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT },
35 #include "target.def"
36 #undef DEFHOOK
37 };
38
39 /* For each @Fcode in the the first paragraph of the documentation string DOC,
40    print an @findex directive.  HOOK_NAME is the name of the hook this bit of
41    documentation pertains to.  */
42 static void
43 emit_findices (const char *doc, const char *hook_name)
44 {
45   const char *end = strstr (doc, "\n\n");
46   const char *fcode;
47
48   while ((fcode = strstr (doc, "@Fcode{")) && (!end || fcode < end))
49     {
50       fcode += strlen ("@Fcode{");
51       doc = strchr (fcode, '}');
52       if (!doc)
53         fatal ("Malformed @Fcode for hook %s\n", hook_name);
54       printf ("@findex %.*s\n", (int) (doc - fcode), fcode);
55       doc = fcode;
56     }
57 }
58
59 /* Return an upper-case copy of IN.  */
60 static char *
61 upstrdup (const char *in)
62 {
63   char *p, *ret = xstrdup (in);
64   for (p = ret; *p; p++)
65     *p = TOUPPER (*p);
66   return ret;
67 }
68
69 /* Struct for 'start hooks' which start a sequence of consecutive hooks
70    that are defined in target.def and to be documented in tm.texi.  */
71 struct s_hook
72 {
73   char *name;
74   int pos;
75 };
76
77 static hashval_t
78 s_hook_hash (const void *p)
79 {
80   const struct s_hook *s_hook = (const struct s_hook *)p;
81   return htab_hash_string (s_hook->name);
82 }
83
84 static int
85 s_hook_eq_p (const void *p1, const void *p2)
86 {
87   return (strcmp (((const struct s_hook *) p1)->name, 
88                   ((const struct s_hook *) p2)->name) == 0);
89 }
90
91 /* Read the documentation file with name IN_FNAME, perform substitutions
92    to incorporate informtion from hook_array, and emit the result on stdout.
93    Hooks defined with DEFHOOK / DEFHOOKPOD are emitted at the place of a
94    matching @hook in the input file; if there is no matching @hook, the
95    hook is emitted after the hook that precedes it in target.def .
96    Usually, the emitted hook documentation starts with the hook
97    signature, followed by the string from the doc field.
98    The documentation is bracketed in @deftypefn / @deftypevr and a matching
99    @end.
100    While emitting the doc field, @Fcode is translated to @code, and an
101    @findex entry is added to the affected paragraph.
102    If the doc field starts with '*', the leading '*' is stripped, and the doc
103    field is otherwise emitted unaltered; no function signature/
104    @deftypefn/deftypevr/@end is emitted.
105    In particular, a doc field of "*" means not to emit any ocumentation for
106    this target.def / hook_array entry at all (there might be documentation
107    for this hook in the file named IN_FNAME, though).
108    A doc field of 0 is used to append the hook signature after the previous
109    hook's signture, so that one description can be used for a group of hooks.
110    When the doc field is "", @deftypefn/@deftypevr and the hook signature
111    is emitted, but not the matching @end.  This allows all the free-form
112    documentation to be placed in IN_FNAME, to work around GPL/GFDL
113    licensing incompatibility issues.  */
114 static void
115 emit_documentation (const char *in_fname)
116 {
117   int i, j;
118   char buf[1000];
119   htab_t start_hooks = htab_create (99, s_hook_hash, s_hook_eq_p, (htab_del) 0);
120   FILE *f;
121   bool found_start = false;
122
123   /* Enter all the start hooks in start_hooks.  */
124   f = fopen (in_fname, "r");
125   if (!f)
126     {
127       perror ("");
128       fatal ("Couldn't open input file");
129     }
130   while (fscanf (f, "%*[^@]"), buf[0] = '\0',
131          fscanf (f, "@%5[^ \n]", buf) != EOF)
132     {
133       void **p;
134       struct s_hook *shp;
135
136       if (strcmp (buf, "hook") != 0)
137         continue;
138       buf[0] = '\0';
139       fscanf (f, "%999s", buf);
140       shp = XNEW (struct s_hook);
141       shp->name = upstrdup (buf);
142       shp->pos = -1;
143       p = htab_find_slot (start_hooks, shp, INSERT);
144       if (*p != HTAB_EMPTY_ENTRY)
145         fatal ("Duplicate placement for hook %s\n", shp->name);
146       *(struct s_hook **) p = shp;
147     }
148   fclose (f);
149   /* For each hook in hook_array, if it is a start hook, store its position.  */
150   for (i = 0; i < (int) (sizeof hook_array / sizeof hook_array[0]); i++)
151     {
152       struct s_hook sh, *shp;
153       void *p;
154
155       if (!hook_array[i].doc || strcmp (hook_array[i].doc, "*") == 0)
156         continue;
157       sh.name = upstrdup (hook_array[i].name);
158       p = htab_find (start_hooks, &sh);
159       if (p)
160         {
161           shp = (struct s_hook *) p;
162           if (shp->pos >= 0)
163             fatal ("Duplicate hook %s\n", sh.name);
164           shp->pos = i;
165           found_start = true;
166         }
167       else if (!found_start)
168         fatal ("No place specified to document hook %s\n", sh.name);
169       free (sh.name);
170     }
171   /* Copy input file to stdout, substituting @hook directives with the
172      corresponding hook documentation sequences.  */
173   f = fopen (in_fname, "r");
174   if (!f)
175     {
176       perror ("");
177       fatal ("Couldn't open input file");
178     }
179   for (;;)
180     {
181       struct s_hook sh, *shp;
182       int c = getc (f);
183       char *name;
184
185       if (c == EOF)
186         break;
187       if (c != '@')
188         {
189           putchar (c);
190           continue;
191         }
192       buf[0] = '\0';
193       fscanf (f, "%5[^ \n]", buf);
194       if (strcmp (buf, "hook") != 0)
195         {
196           printf ("@%s", buf);
197           continue;
198         }
199       fscanf (f, "%999s", buf);
200       sh.name = name = upstrdup (buf);
201       shp = (struct s_hook *) htab_find (start_hooks, &sh);
202       if (!shp || shp->pos < 0)
203         fatal ("No documentation for hook %s\n", sh.name);
204       i = shp->pos;
205       do
206         {
207           const char *q, *e;
208           const char *deftype;
209           const char *doc, *fcode, *p_end;
210
211           /* A leading '*' means to output the documentation string without
212              further processing.  */
213           if (*hook_array[i].doc == '*')
214             printf ("%s", hook_array[i].doc + 1);
215           else
216             {
217               if (i != shp->pos)
218                 printf ("\n\n");
219               emit_findices (hook_array[i].doc, name);
220
221               /* Print header.  Function-valued hooks have a parameter list, 
222                  unlike POD-valued ones.  */
223               deftype = hook_array[i].param ? "deftypefn" : "deftypevr";
224               printf ("@%s {Target Hook} ", deftype);
225               if (strchr (hook_array[i].type, ' '))
226                 printf ("{%s}", hook_array[i].type);
227               else
228                 printf ("%s", hook_array[i].type);
229               printf (" %s", name);
230               if (hook_array[i].param)
231                 {
232                   /* Print the parameter list, with the parameter names
233                      enclosed in @var{}.  */
234                   printf (" ");
235                   for (q = hook_array[i].param; (e = strpbrk (q, " *,)"));
236                        q = e + 1)
237                     /* Type names like 'int' are followed by a space, sometimes
238                        also by '*'.  'void' should appear only in "(void)".  */
239                     if (*e == ' ' || *e == '*' || *q == '(')
240                       printf ("%.*s", (int) (e - q + 1), q);
241                     else
242                       printf ("@var{%.*s}%c", (int) (e - q), q, *e);
243                 }
244               /* POD-valued hooks sometimes come in groups with common
245                  documentation.*/
246               for (j = i + 1;
247                    j < (int) (sizeof hook_array / sizeof hook_array[0])
248                    && hook_array[j].doc == 0 && hook_array[j].type; j++)
249                 {
250                   char *namex = upstrdup (hook_array[j].name);
251
252                   printf ("\n@%sx {Target Hook} {%s} %s",
253                           deftype, hook_array[j].type, namex);
254                 }
255               if (hook_array[i].doc[0])
256                 {
257                   printf ("\n");
258                   /* Print each documentation paragraph in turn.  */
259                   for (doc = hook_array[i].doc; *doc; doc = p_end)
260                     {
261                       /* Find paragraph end.  */
262                       p_end = strstr (doc, "\n\n");
263                       p_end = (p_end ? p_end + 2 : doc + strlen (doc));
264                       /* Print paragraph, emitting @Fcode as @code.  */
265                       for (; (fcode = strstr (doc, "@Fcode{")) && fcode < p_end;
266                            doc = fcode + 2)
267                         printf ("%.*s@", (int) (fcode - doc), doc);
268                       printf ("%.*s", (int) (p_end - doc), doc);
269                       /* Emit function indices for next paragraph.  */
270                       emit_findices (p_end, name);
271                     }
272                   printf ("\n@end %s", deftype);
273                 }
274             }
275           if (++i >= (int) (sizeof hook_array / sizeof hook_array[0])
276               || !hook_array[i].doc)
277             break;
278           free (name);
279           sh.name = name = upstrdup (hook_array[i].name);
280         }
281       while (!htab_find (start_hooks, &sh));
282       free (name);
283     }
284 }
285
286 /* Emit #defines to stdout (this will be redirected to generate
287    target-hook-def.h) which set target hooks initializer macros
288    to their default values.  */
289 static void
290 emit_init_macros (void)
291 {
292   int i;
293   const int MAX_NEST = 2;
294   int print_nest, nest = 0;
295
296   for (print_nest = 0; print_nest <= MAX_NEST; print_nest++)
297     {
298       for (i = 0; i < (int) (sizeof hook_array / sizeof hook_array[0]); i++)
299         {
300           char *name = upstrdup (hook_array[i].name);
301
302           if (!hook_array[i].type)
303             {
304               if (*name)
305                 {
306                   if (nest && nest == print_nest)
307                     printf ("    %s, \\\n", name);
308                   nest++;
309                   if (nest > MAX_NEST)
310                     fatal ("Unexpected nesting of %s\n", name);
311                   if (nest == print_nest)
312                     printf ("\n#define %s \\\n  { \\\n", name);
313                 }
314               else
315                 {
316                   if (nest == print_nest)
317                     printf ("  }\n");
318                   nest--;
319                 }
320               continue;
321             }
322           if (0 == print_nest)
323             {
324               /* Output default definitions of target hooks.  */
325               printf ("#ifndef %s\n#define %s %s\n#endif\n",
326                       name, name, hook_array[i].init);
327             }
328           if (nest == print_nest)
329             printf ("    %s, \\\n", name);
330         }
331     }
332 }
333
334 int
335 main (int argc, char **argv)
336 {
337   if (argc >= 2)
338     emit_documentation (argv[1]);
339   else
340     emit_init_macros ();
341   return 0;
342 }