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 * Renders the start of the thing being declared. If top is true, print
30 * the "declare" or "type" keywords at the front, as appropriate.
33 explain_prologue(char *buf, size_t n, struct cdecl_declarator *d, bool top)
35 size_t ret = 0, rc = 0;
41 return snprintf(buf, n, "type");
43 case CDECL_DECL_IDENT:
45 rc = snprintf(buf, n, "declare");
46 ret += cdecl__advance(&buf, &n, rc);
47 return ret + snprintf(buf, n, "%s as", d->u.ident);
57 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
61 rc = cdecl__explain_specs(buf, n, p->qualifiers, CDECL_SPEC_QUAL);
62 ret += cdecl__advance(&buf, &n, rc);
64 return ret + snprintf(buf, n, "pointer to");
68 explain_array(char *buf, size_t n, struct cdecl_array *a)
70 size_t ret = 0, rc = 0;
73 rc = snprintf(buf, n, "variable-length array");
75 rc = snprintf(buf, n, "array");
76 ret += cdecl__advance(&buf, &n, rc);
79 rc = snprintf(buf, n, "%s", a->vla);
80 ret += cdecl__advance(&buf, &n, rc);
81 } else if (a->length) {
82 rc = snprintf(buf, n, "%ju", a->length);
83 ret += cdecl__advance(&buf, &n, rc);
86 return ret + snprintf(buf, n, "of");
90 explain_declarators(char *buf, size_t n, struct cdecl_declarator *decl);
92 static size_t explain_decl(char *buf, size_t n, struct cdecl *decl, bool top)
96 rc = explain_prologue(buf, n, decl->declarators, top);
97 ret += cdecl__advance(&buf, &n, rc);
99 rc = cdecl__explain_pre_specs(buf, n, decl->specifiers);
100 ret += cdecl__advance(&buf, &n, rc);
102 rc = explain_declarators(buf, n, decl->declarators);
103 ret += cdecl__advance(&buf, &n, rc);
105 return ret + cdecl__explain_post_specs(buf, n, decl->specifiers);
108 static size_t explain_function(char *buf, size_t n, struct cdecl_function *f)
110 size_t ret = 0, rc = 0;
112 rc = snprintf(buf, n, "function");
113 ret += cdecl__advance(&buf, &n, rc);
116 rc = snprintf(buf, n, "(");
117 ret += cdecl__advance_(&buf, &n, rc);
119 for (struct cdecl *p = f->parameters; p; p = p->next) {
120 rc = explain_decl(buf, n, p, false);
121 ret += cdecl__advance_(&buf, &n, rc);
124 rc = snprintf(buf, n, ",");
125 else if (f->variadic)
126 rc = snprintf(buf, n, ", ...)");
128 rc = snprintf(buf, n, ")");
129 ret += cdecl__advance(&buf, &n, rc);
133 return ret + snprintf(buf, n, "returning");
137 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
141 if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
144 rc = explain_declarators(buf, n, d->child);
145 ret += cdecl__advance(&buf, &n, rc);
148 case CDECL_DECL_POINTER:
149 return ret + explain_pointer(buf, n, &d->u.pointer);
150 case CDECL_DECL_ARRAY:
151 return ret + explain_array(buf, n, &d->u.array);
152 case CDECL_DECL_FUNCTION:
153 return ret + explain_function(buf, n, &d->u.function);
159 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
161 return explain_decl(buf, n, decl, true);