2 * Render C declarations as English.
3 * Copyright © 2011 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any 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, see <http://www.gnu.org/licenses/>.
27 /* declare [ident] as [storage] [function specs] [other crap] [qualifiers] [type specs] */
30 voutput(char *buf, size_t n, size_t off, const char *fmt, va_list ap)
33 return vsnprintf(NULL, 0, fmt, ap);
34 return vsnprintf(buf+off, n-off, fmt, ap);
37 static size_t output(char *buf, size_t n, size_t off, const char *fmt, ...)
43 ret = voutput(buf, n, off, fmt, ap);
49 static size_t advance_(char **buf, size_t *n, size_t amount)
62 static size_t advance(char **buf, size_t *n, size_t amount)
69 ret = advance_(buf, n, amount);
70 rc = snprintf(*buf, *n, " ");
71 return ret + advance_(buf, n, rc);
75 explain_qualifiers(char *buf, size_t n, struct cdecl_declspec *s)
77 unsigned long qualmap = 0;
78 size_t ret = 0, rc = 0;
80 for (struct cdecl_declspec *c = s; c; c = c->next) {
81 if (cdecl_spec_kind(c) != CDECL_SPEC_QUAL)
83 qualmap |= 1ul << (c->type & 0xff);
86 if (qualmap & (1ul << (CDECL_QUAL_RESTRICT & 0xff))) {
87 ret += advance(&buf, &n, rc);
88 rc = snprintf(buf, n, "restrict");
90 if (qualmap & (1ul << (CDECL_QUAL_VOLATILE & 0xff))) {
91 ret += advance(&buf, &n, rc);
92 rc = snprintf(buf, n, "volatile");
94 if (qualmap & (1ul << (CDECL_QUAL_CONST & 0xff))) {
95 ret += advance(&buf, &n, rc);
96 rc = snprintf(buf, n, "const");
102 /* Renders the type qualifiers and type specifiers in canonical form. */
104 explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
106 const char *tag = NULL;
107 unsigned long typemap;
110 typemap = cdecl__build_typemap(s);
114 for (struct cdecl_declspec *c = s; c; c = c->next) {
115 if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
118 /* Valid C types have at most one identifier. */
123 rc = explain_qualifiers(buf, n, s);
124 ret += advance(&buf, &n, rc);
126 rc = snprintf(buf, n, "%s", cdecl__explain_typemap(typemap));
128 ret += advance(&buf, &n, rc);
129 rc = snprintf(buf, n, "%s", tag);
135 static const char *explain_storage(unsigned spec)
138 case CDECL_STOR_TYPEDEF:
140 case CDECL_STOR_EXTERN:
142 case CDECL_STOR_STATIC:
144 case CDECL_STOR_AUTO:
146 case CDECL_STOR_REGISTER:
153 /* Renders the storage-class and function specifiers in canonical form. */
154 static size_t explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
156 unsigned long funcmap = 0;
157 const char *storage = NULL;
160 for (struct cdecl_declspec *c = s; c; c = c->next) {
161 switch (cdecl_spec_kind(c)) {
162 case CDECL_SPEC_FUNC:
163 funcmap |= 1ul << (c->type & 0xff);
165 case CDECL_SPEC_STOR:
166 /* Valid C declarations have at most one
167 * storage-class specifier. */
168 storage = explain_storage(c->type);
174 ret += output(buf, n, ret, "%s", storage);
175 if (funcmap & (1ul << (CDECL_FUNC_INLINE & 0xff)))
176 ret += output(buf, n, ret, "%.*s%s", !!ret, "", "inline");
181 * Renders the start of the thing being declared. If top is true, print
182 * the "declare" or "type" keywords at the front, as appropriate.
185 explain_prologue(char *buf, size_t n, struct cdecl_declarator *d, bool top)
187 size_t ret = 0, rc = 0;
191 case CDECL_DECL_NULL:
193 return snprintf(buf, n, "type");
195 case CDECL_DECL_IDENT:
197 rc = snprintf(buf, n, "declare");
198 ret += advance(&buf, &n, rc);
199 return ret + snprintf(buf, n, "%s as", d->u.ident);
207 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
211 rc = explain_qualifiers(buf, n, p->qualifiers);
212 ret += advance(&buf, &n, rc);
214 return ret + snprintf(buf, n, "pointer to");
218 explain_array(char *buf, size_t n, struct cdecl_array *a)
220 size_t ret = 0, rc = 0;
223 rc = snprintf(buf, n, "variable-length array");
225 rc = snprintf(buf, n, "array");
226 ret += advance(&buf, &n, rc);
229 rc = snprintf(buf, n, "%s", a->vla);
230 ret += advance(&buf, &n, rc);
231 } else if (a->length) {
232 rc = snprintf(buf, n, "%ju", a->length);
233 ret += advance(&buf, &n, rc);
236 return ret + snprintf(buf, n, "of");
240 explain_declarators(char *buf, size_t n, struct cdecl_declarator *decl);
242 static size_t explain_decl(char *buf, size_t n, struct cdecl *decl, bool top)
246 rc = explain_prologue(buf, n, decl->declarators, top);
247 ret += advance(&buf, &n, rc);
249 rc = explain_pre_specs(buf, n, decl->specifiers);
250 ret += advance(&buf, &n, rc);
252 rc = explain_declarators(buf, n, decl->declarators);
253 ret += advance(&buf, &n, rc);
255 return ret + explain_post_specs(buf, n, decl->specifiers);
258 static size_t explain_function(char *buf, size_t n, struct cdecl_function *f)
260 size_t ret = 0, rc = 0;
262 rc = snprintf(buf, n, "function");
263 ret += advance(&buf, &n, rc);
266 rc = snprintf(buf, n, "(");
267 ret += advance_(&buf, &n, rc);
269 for (struct cdecl *p = f->parameters; p; p = p->next) {
270 rc = explain_decl(buf, n, p, false);
271 ret += advance_(&buf, &n, rc);
274 rc = snprintf(buf, n, ",");
275 else if (f->variadic)
276 rc = snprintf(buf, n, ", ...)");
278 rc = snprintf(buf, n, ")");
279 ret += advance(&buf, &n, rc);
283 return ret + snprintf(buf, n, "returning");
287 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
291 if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
294 rc = explain_declarators(buf, n, d->child);
295 ret += advance(&buf, &n, rc);
298 case CDECL_DECL_POINTER:
299 return ret + explain_pointer(buf, n, &d->u.pointer);
300 case CDECL_DECL_ARRAY:
301 return ret + explain_array(buf, n, &d->u.array);
302 case CDECL_DECL_FUNCTION:
303 return ret + explain_function(buf, n, &d->u.function);
309 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
311 return explain_decl(buf, n, decl, true);