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/>.
29 explain_qualifiers(char *buf, size_t n, struct cdecl_declspec *s)
31 unsigned long qualmap = 0;
32 size_t ret = 0, rc = 0;
34 for (struct cdecl_declspec *c = s; c; c = c->next) {
35 if (cdecl_spec_kind(c) != CDECL_SPEC_QUAL)
37 qualmap |= 1ul << (c->type & 0xff);
40 if (qualmap & (1ul << (CDECL_QUAL_RESTRICT & 0xff))) {
41 ret += cdecl__advance(&buf, &n, rc);
42 rc = snprintf(buf, n, "restrict");
44 if (qualmap & (1ul << (CDECL_QUAL_VOLATILE & 0xff))) {
45 ret += cdecl__advance(&buf, &n, rc);
46 rc = snprintf(buf, n, "volatile");
48 if (qualmap & (1ul << (CDECL_QUAL_CONST & 0xff))) {
49 ret += cdecl__advance(&buf, &n, rc);
50 rc = snprintf(buf, n, "const");
56 /* Renders the type qualifiers and type specifiers in canonical form. */
58 explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
60 const char *tag = NULL;
61 unsigned long typemap;
64 typemap = cdecl__build_typemap(s);
68 for (struct cdecl_declspec *c = s; c; c = c->next) {
69 if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
72 /* Valid C types have at most one identifier. */
77 rc = explain_qualifiers(buf, n, s);
78 ret += cdecl__advance(&buf, &n, rc);
80 rc = snprintf(buf, n, "%s", cdecl__explain_typemap(typemap));
82 ret += cdecl__advance(&buf, &n, rc);
83 rc = snprintf(buf, n, "%s", tag);
89 static size_t explain_storage(char *buf, size_t n, unsigned spec)
92 case CDECL_STOR_TYPEDEF:
93 return snprintf(buf, n, "typedef");
94 case CDECL_STOR_EXTERN:
95 return snprintf(buf, n, "extern");
96 case CDECL_STOR_STATIC:
97 return snprintf(buf, n, "static");
99 return snprintf(buf, n, "auto");
100 case CDECL_STOR_REGISTER:
101 return snprintf(buf, n, "register");
107 /* Renders the storage-class and function specifiers in canonical form. */
108 static size_t explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
110 unsigned long funcmap = 0;
111 size_t ret = 0, rc = 0;
113 for (struct cdecl_declspec *c = s; c; c = c->next) {
114 switch (cdecl_spec_kind(c)) {
115 case CDECL_SPEC_FUNC:
116 funcmap |= 1ul << (c->type & 0xff);
118 case CDECL_SPEC_STOR:
119 /* Valid C declarations have at most one
120 * storage-class specifier. */
121 rc = explain_storage(buf, n, c->type);
126 if (funcmap & (1ul << (CDECL_FUNC_INLINE & 0xff))) {
127 ret += cdecl__advance(&buf, &n, rc);
128 rc = snprintf(buf, n, "inline");
135 * Renders the start of the thing being declared. If top is true, print
136 * the "declare" or "type" keywords at the front, as appropriate.
139 explain_prologue(char *buf, size_t n, struct cdecl_declarator *d, bool top)
141 size_t ret = 0, rc = 0;
145 case CDECL_DECL_NULL:
147 return snprintf(buf, n, "type");
149 case CDECL_DECL_IDENT:
151 rc = snprintf(buf, n, "declare");
152 ret += cdecl__advance(&buf, &n, rc);
153 return ret + snprintf(buf, n, "%s as", d->u.ident);
161 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
165 rc = explain_qualifiers(buf, n, p->qualifiers);
166 ret += cdecl__advance(&buf, &n, rc);
168 return ret + snprintf(buf, n, "pointer to");
172 explain_array(char *buf, size_t n, struct cdecl_array *a)
174 size_t ret = 0, rc = 0;
177 rc = snprintf(buf, n, "variable-length array");
179 rc = snprintf(buf, n, "array");
180 ret += cdecl__advance(&buf, &n, rc);
183 rc = snprintf(buf, n, "%s", a->vla);
184 ret += cdecl__advance(&buf, &n, rc);
185 } else if (a->length) {
186 rc = snprintf(buf, n, "%ju", a->length);
187 ret += cdecl__advance(&buf, &n, rc);
190 return ret + snprintf(buf, n, "of");
194 explain_declarators(char *buf, size_t n, struct cdecl_declarator *decl);
196 static size_t explain_decl(char *buf, size_t n, struct cdecl *decl, bool top)
200 rc = explain_prologue(buf, n, decl->declarators, top);
201 ret += cdecl__advance(&buf, &n, rc);
203 rc = explain_pre_specs(buf, n, decl->specifiers);
204 ret += cdecl__advance(&buf, &n, rc);
206 rc = explain_declarators(buf, n, decl->declarators);
207 ret += cdecl__advance(&buf, &n, rc);
209 return ret + explain_post_specs(buf, n, decl->specifiers);
212 static size_t explain_function(char *buf, size_t n, struct cdecl_function *f)
214 size_t ret = 0, rc = 0;
216 rc = snprintf(buf, n, "function");
217 ret += cdecl__advance(&buf, &n, rc);
220 rc = snprintf(buf, n, "(");
221 ret += cdecl__advance_(&buf, &n, rc);
223 for (struct cdecl *p = f->parameters; p; p = p->next) {
224 rc = explain_decl(buf, n, p, false);
225 ret += cdecl__advance_(&buf, &n, rc);
228 rc = snprintf(buf, n, ",");
229 else if (f->variadic)
230 rc = snprintf(buf, n, ", ...)");
232 rc = snprintf(buf, n, ")");
233 ret += cdecl__advance(&buf, &n, rc);
237 return ret + snprintf(buf, n, "returning");
241 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
245 if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
248 rc = explain_declarators(buf, n, d->child);
249 ret += cdecl__advance(&buf, &n, rc);
252 case CDECL_DECL_POINTER:
253 return ret + explain_pointer(buf, n, &d->u.pointer);
254 case CDECL_DECL_ARRAY:
255 return ret + explain_array(buf, n, &d->u.array);
256 case CDECL_DECL_FUNCTION:
257 return ret + explain_function(buf, n, &d->u.function);
263 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
265 return explain_decl(buf, n, decl, true);