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/>.
28 * Renders the start of the thing being declared. If top is true, print
29 * the "declare" or "type" keywords at the front, as appropriate.
32 explain_prologue(char *buf, size_t n, struct cdecl_declarator *d, bool top)
34 size_t ret = 0, rc = 0;
40 return snprintf(buf, n, "type");
42 case CDECL_DECL_IDENT:
44 rc = snprintf(buf, n, "declare");
45 ret += cdecl__advance(&buf, &n, rc);
46 return ret + snprintf(buf, n, "%s as", d->u.ident);
54 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
58 rc = cdecl__explain_qualifiers(buf, n, p->qualifiers);
59 ret += cdecl__advance(&buf, &n, rc);
61 return ret + snprintf(buf, n, "pointer to");
65 explain_array(char *buf, size_t n, struct cdecl_array *a)
67 size_t ret = 0, rc = 0;
70 rc = snprintf(buf, n, "variable-length array");
72 rc = snprintf(buf, n, "array");
73 ret += cdecl__advance(&buf, &n, rc);
76 rc = snprintf(buf, n, "%s", a->vla);
77 ret += cdecl__advance(&buf, &n, rc);
78 } else if (a->length) {
79 rc = snprintf(buf, n, "%ju", a->length);
80 ret += cdecl__advance(&buf, &n, rc);
83 return ret + snprintf(buf, n, "of");
87 explain_declarators(char *buf, size_t n, struct cdecl_declarator *decl);
89 static size_t explain_decl(char *buf, size_t n, struct cdecl *decl, bool top)
93 rc = explain_prologue(buf, n, decl->declarators, top);
94 ret += cdecl__advance(&buf, &n, rc);
96 rc = cdecl__explain_pre_specs(buf, n, decl->specifiers);
97 ret += cdecl__advance(&buf, &n, rc);
99 rc = explain_declarators(buf, n, decl->declarators);
100 ret += cdecl__advance(&buf, &n, rc);
102 return ret + cdecl__explain_post_specs(buf, n, decl->specifiers);
105 static size_t explain_function(char *buf, size_t n, struct cdecl_function *f)
107 size_t ret = 0, rc = 0;
109 rc = snprintf(buf, n, "function");
110 ret += cdecl__advance(&buf, &n, rc);
113 rc = snprintf(buf, n, "(");
114 ret += cdecl__advance_(&buf, &n, rc);
116 for (struct cdecl *p = f->parameters; p; p = p->next) {
117 rc = explain_decl(buf, n, p, false);
118 ret += cdecl__advance_(&buf, &n, rc);
121 rc = snprintf(buf, n, ",");
122 else if (f->variadic)
123 rc = snprintf(buf, n, ", ...)");
125 rc = snprintf(buf, n, ")");
126 ret += cdecl__advance(&buf, &n, rc);
130 return ret + snprintf(buf, n, "returning");
134 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
138 if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
141 rc = explain_declarators(buf, n, d->child);
142 ret += cdecl__advance(&buf, &n, rc);
145 case CDECL_DECL_POINTER:
146 return ret + explain_pointer(buf, n, &d->u.pointer);
147 case CDECL_DECL_ARRAY:
148 return ret + explain_array(buf, n, &d->u.array);
149 case CDECL_DECL_FUNCTION:
150 return ret + explain_function(buf, n, &d->u.function);
156 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
158 return explain_decl(buf, n, decl, true);