]>
Commit | Line | Data |
---|---|---|
38f8b050 JR |
1 | /* Process target.def to create initialization macros definition in |
2 | target-hooks-def.h and documentation in target-hooks.texi. | |
7adcbafe | 3 | Copyright (C) 2009-2022 Free Software Foundation, Inc. |
38f8b050 JR |
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" | |
38f8b050 JR |
22 | #include "errors.h" |
23 | ||
acce4e77 | 24 | struct hook_desc { const char *doc, *type, *name, *param, *init, *docname; }; |
38f8b050 | 25 | static struct hook_desc hook_array[] = { |
acce4e77 JM |
26 | #define HOOK_VECTOR_1(NAME, FRAGMENT) \ |
27 | { 0, 0, #NAME, 0, 0, HOOK_TYPE }, | |
38f8b050 | 28 | #define DEFHOOKPOD(NAME, DOC, TYPE, INIT) \ |
acce4e77 | 29 | { DOC, #TYPE, HOOK_PREFIX #NAME, 0, #INIT, HOOK_TYPE }, |
38f8b050 | 30 | #define DEFHOOK(NAME, DOC, TYPE, PARAMS, INIT) \ |
acce4e77 | 31 | { DOC, #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT, HOOK_TYPE }, |
38f8b050 | 32 | #define DEFHOOK_UNDOC(NAME, DOC, TYPE, PARAMS, INIT) \ |
acce4e77 | 33 | { "*", #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT, HOOK_TYPE }, |
38f8b050 | 34 | #include "target.def" |
acce4e77 | 35 | #include "c-family/c-target.def" |
c49a6962 | 36 | #include "common/common-target.def" |
b4c522fa | 37 | #include "d/d-target.def" |
38f8b050 JR |
38 | #undef DEFHOOK |
39 | }; | |
40 | ||
38f8b050 JR |
41 | /* Return an upper-case copy of IN. */ |
42 | static char * | |
43 | upstrdup (const char *in) | |
44 | { | |
45 | char *p, *ret = xstrdup (in); | |
46 | for (p = ret; *p; p++) | |
47 | *p = TOUPPER (*p); | |
48 | return ret; | |
49 | } | |
50 | ||
41a45cba ML |
51 | /* Emit shared .rst.in file that is used by the corresponding |
52 | .. include:: tm.rst.in | |
53 | :start-after: [HOOK_NAME] | |
54 | :end-before: [HOOK_NAME] | |
38f8b050 | 55 | |
38f8b050 JR |
56 | If the doc field starts with '*', the leading '*' is stripped, and the doc |
57 | field is otherwise emitted unaltered; no function signature/ | |
58 | @deftypefn/deftypevr/@end is emitted. | |
59 | In particular, a doc field of "*" means not to emit any ocumentation for | |
60 | this target.def / hook_array entry at all (there might be documentation | |
61 | for this hook in the file named IN_FNAME, though). | |
41a45cba ML |
62 | |
63 | This allows all the free-form | |
38f8b050 JR |
64 | documentation to be placed in IN_FNAME, to work around GPL/GFDL |
65 | licensing incompatibility issues. */ | |
41a45cba | 66 | |
38f8b050 | 67 | static void |
41a45cba | 68 | emit_documentation (void) |
38f8b050 | 69 | { |
38f8b050 | 70 | /* For each hook in hook_array, if it is a start hook, store its position. */ |
41a45cba | 71 | for (int i = 0; i < (int) (ARRAY_SIZE (hook_array)); i++) |
38f8b050 | 72 | { |
41a45cba ML |
73 | if (hook_array[i].doc == NULL |
74 | || strcmp (hook_array[i].doc, "") == 0 | |
75 | || strcmp (hook_array[i].doc, "*") == 0) | |
38f8b050 | 76 | continue; |
41a45cba ML |
77 | const char *hook_name = upstrdup (hook_array[i].name); |
78 | printf ("[%s]\n", hook_name); | |
79 | /* Print header. Function-valued hooks have a parameter list, | |
80 | unlike POD-valued ones. */ | |
81 | const char *deftype = hook_array[i].param ? "function" : "c:var"; | |
82 | printf (".. %s:: ", deftype); | |
83 | if (strchr (hook_array[i].type, ' ')) | |
84 | printf ("%s", hook_array[i].type); | |
17c4f786 | 85 | else |
41a45cba ML |
86 | printf ("%s", hook_array[i].type); |
87 | printf (" %s", hook_name); | |
88 | if (hook_array[i].param) | |
38f8b050 | 89 | { |
41a45cba ML |
90 | const char *q, *e; |
91 | /* Print the parameter list, with the parameter names | |
92 | enclosed in @var{}. */ | |
93 | printf (" "); | |
94 | for (q = hook_array[i].param; (e = strpbrk (q, " *,)")); | |
95 | q = e + 1) | |
96 | /* Type names like 'int' are followed by a space, sometimes | |
97 | also by '*'. 'void' should appear only in "(void)". */ | |
98 | if (*e == ' ' || *e == '*' || *q == '(') | |
99 | printf ("%.*s", (int) (e - q + 1), q); | |
100 | else | |
101 | printf ("%.*s%c", (int) (e - q), q, *e); | |
38f8b050 | 102 | } |
41a45cba ML |
103 | |
104 | printf ("\n"); | |
105 | if (hook_array[i].doc[0]) | |
38f8b050 | 106 | { |
0bc0e617 | 107 | const char *doc, *p_end; |
41a45cba ML |
108 | printf ("\n"); |
109 | /* Print each documentation paragraph in turn. */ | |
110 | for (doc = hook_array[i].doc; *doc; doc = p_end) | |
38f8b050 | 111 | { |
41a45cba ML |
112 | /* Find paragraph end. */ |
113 | p_end = strstr (doc, "\n"); | |
114 | p_end = (p_end ? p_end + 1 : doc + strlen (doc)); | |
115 | printf (" %.*s", (int) (p_end - doc), doc); | |
38f8b050 | 116 | } |
41a45cba | 117 | printf ("\n"); |
38f8b050 | 118 | } |
41a45cba ML |
119 | |
120 | printf ("\n[%s]\n\n", hook_name); | |
38f8b050 JR |
121 | } |
122 | } | |
123 | ||
124 | /* Emit #defines to stdout (this will be redirected to generate | |
125 | target-hook-def.h) which set target hooks initializer macros | |
acce4e77 JM |
126 | to their default values. These should only be emitted for hooks |
127 | whose type is given by DOCNAME. */ | |
38f8b050 | 128 | static void |
acce4e77 | 129 | emit_init_macros (const char *docname) |
38f8b050 JR |
130 | { |
131 | int i; | |
132 | const int MAX_NEST = 2; | |
133 | int print_nest, nest = 0; | |
134 | ||
135 | for (print_nest = 0; print_nest <= MAX_NEST; print_nest++) | |
136 | { | |
ca32b29e | 137 | for (i = 0; i < (int) (ARRAY_SIZE (hook_array)); i++) |
38f8b050 JR |
138 | { |
139 | char *name = upstrdup (hook_array[i].name); | |
140 | ||
acce4e77 JM |
141 | if (strcmp (hook_array[i].docname, docname) != 0) |
142 | continue; | |
143 | ||
38f8b050 JR |
144 | if (!hook_array[i].type) |
145 | { | |
146 | if (*name) | |
147 | { | |
148 | if (nest && nest == print_nest) | |
149 | printf (" %s, \\\n", name); | |
150 | nest++; | |
151 | if (nest > MAX_NEST) | |
152 | fatal ("Unexpected nesting of %s\n", name); | |
153 | if (nest == print_nest) | |
154 | printf ("\n#define %s \\\n { \\\n", name); | |
155 | } | |
156 | else | |
157 | { | |
158 | if (nest == print_nest) | |
159 | printf (" }\n"); | |
160 | nest--; | |
161 | } | |
162 | continue; | |
163 | } | |
01512446 | 164 | if (print_nest == 0) |
38f8b050 JR |
165 | { |
166 | /* Output default definitions of target hooks. */ | |
167 | printf ("#ifndef %s\n#define %s %s\n#endif\n", | |
168 | name, name, hook_array[i].init); | |
169 | } | |
170 | if (nest == print_nest) | |
171 | printf (" %s, \\\n", name); | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
176 | int | |
177 | main (int argc, char **argv) | |
178 | { | |
17c4f786 AS |
179 | progname = "genhooks"; |
180 | ||
41a45cba ML |
181 | if (argc == 1) |
182 | emit_documentation (); | |
38f8b050 | 183 | else |
acce4e77 | 184 | emit_init_macros (argv[1]); |
38f8b050 JR |
185 | return 0; |
186 | } |