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/>.
26 /* declare [ident] as [storage] [function specs] [other crap] [qualifiers] [type specs] */
29 voutput(char *buf, size_t n, size_t off, const char *fmt, va_list ap)
32 return vsnprintf(NULL, 0, fmt, ap);
33 return vsnprintf(buf+off, n-off, fmt, ap);
36 static size_t output(char *buf, size_t n, size_t off, const char *fmt, ...)
42 ret = voutput(buf, n, off, fmt, ap);
48 static size_t advance_(char **buf, size_t *n, size_t amount)
61 static size_t advance(char **buf, size_t *n, size_t amount)
68 ret = advance_(buf, n, amount);
69 rc = snprintf(*buf, *n, " ");
70 return ret + advance_(buf, n, rc);
74 explain_qualifiers(char *buf, size_t n, struct cdecl_declspec *s)
76 unsigned long qualmap = 0;
77 size_t ret = 0, rc = 0;
79 for (struct cdecl_declspec *c = s; c; c = c->next) {
80 if (cdecl_spec_kind(c) != CDECL_SPEC_QUAL)
82 qualmap |= 1ul << (c->type & 0xff);
85 if (qualmap & (1ul << (CDECL_QUAL_RESTRICT & 0xff))) {
86 ret += advance(&buf, &n, rc);
87 rc = snprintf(buf, n, "restrict");
89 if (qualmap & (1ul << (CDECL_QUAL_VOLATILE & 0xff))) {
90 ret += advance(&buf, &n, rc);
91 rc = snprintf(buf, n, "volatile");
93 if (qualmap & (1ul << (CDECL_QUAL_CONST & 0xff))) {
94 ret += advance(&buf, &n, rc);
95 rc = snprintf(buf, n, "const");
101 /* Renders the type qualifiers and type specifiers in canonical form. */
103 explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
105 const char *tag = NULL;
106 unsigned long typemap;
109 typemap = cdecl__build_typemap(s);
113 for (struct cdecl_declspec *c = s; c; c = c->next) {
114 if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
117 /* Valid C types have at most one identifier. */
122 rc = explain_qualifiers(buf, n, s);
123 ret += advance(&buf, &n, rc);
125 rc = snprintf(buf, n, "%s", cdecl__explain_typemap(typemap));
127 ret += advance(&buf, &n, rc);
128 rc = snprintf(buf, n, "%s", tag);
134 static const char *explain_storage(unsigned spec)
137 case CDECL_STOR_TYPEDEF:
139 case CDECL_STOR_EXTERN:
141 case CDECL_STOR_STATIC:
143 case CDECL_STOR_AUTO:
145 case CDECL_STOR_REGISTER:
152 /* Renders the storage-class and function specifiers in canonical form. */
153 static size_t explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
155 unsigned long funcmap = 0;
156 const char *storage = NULL;
159 for (struct cdecl_declspec *c = s; c; c = c->next) {
160 switch (cdecl_spec_kind(c)) {
161 case CDECL_SPEC_FUNC:
162 funcmap |= 1ul << (c->type & 0xff);
164 case CDECL_SPEC_STOR:
165 /* Valid C declarations have at most one
166 * storage-class specifier. */
167 storage = explain_storage(c->type);
173 ret += output(buf, n, ret, "%s", storage);
174 if (funcmap & (1ul << (CDECL_FUNC_INLINE & 0xff)))
175 ret += output(buf, n, ret, "%.*s%s", !!ret, "", "inline");
179 /* Renders the name of the thing being declared. */
181 explain_prologue(char *buf, size_t n, struct cdecl_declarator *d)
185 case CDECL_DECL_NULL:
186 return snprintf(buf, n, "type");
187 case CDECL_DECL_IDENT:
188 return snprintf(buf, n, "declare %s as", d->u.ident);
196 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
200 rc = explain_qualifiers(buf, n, p->qualifiers);
201 ret += advance(&buf, &n, rc);
203 return ret + snprintf(buf, n, "pointer to");
207 explain_array(char *buf, size_t n, struct cdecl_array *a)
209 size_t ret = 0, rc = 0;
212 rc = snprintf(buf, n, "variable-length array");
214 rc = snprintf(buf, n, "array");
215 ret += advance(&buf, &n, rc);
218 rc = snprintf(buf, n, "%s", a->vla);
219 ret += advance(&buf, &n, rc);
220 } else if (a->length) {
221 rc = snprintf(buf, n, "%ju", a->length);
222 ret += advance(&buf, &n, rc);
225 return ret + snprintf(buf, n, "of");
229 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
233 if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
236 rc = explain_declarators(buf, n, d->child);
237 ret += advance(&buf, &n, rc);
240 case CDECL_DECL_POINTER:
241 return ret + explain_pointer(buf, n, &d->u.pointer);
242 case CDECL_DECL_ARRAY:
243 return ret + explain_array(buf, n, &d->u.array);
249 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
253 rc = explain_prologue(buf, n, decl->declarators);
254 ret += advance(&buf, &n, rc);
256 rc = explain_pre_specs(buf, n, decl->specifiers);
257 ret += advance(&buf, &n, rc);
259 rc = explain_declarators(buf, n, decl->declarators);
260 ret += advance(&buf, &n, rc);
262 return ret + explain_post_specs(buf, n, decl->specifiers);