2 * Render C declarations as English.
3 * Copyright © 2011, 2021 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 #include "cdecl-internal.h"
31 * Renders the start of the thing being declared. If top is true, print
32 * the "declare" or "type" keywords at the front, as appropriate.
35 explain_prologue(char *buf, size_t n, struct cdecl_declarator *d, bool top)
37 size_t ret = 0, rc = 0;
43 return snprintf(buf, n, "type");
45 case CDECL_DECL_IDENT:
47 rc = snprintf(buf, n, "declare");
48 ret += cdecl__advance(&buf, &n, rc);
49 return ret + snprintf(buf, n, "%s as", d->u.ident);
59 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
63 rc = cdecl__explain_specs(buf, n, p->qualifiers, CDECL_SPEC_QUAL);
64 ret += cdecl__advance(&buf, &n, rc);
66 return ret + snprintf(buf, n, "pointer to");
70 explain_array(char *buf, size_t n, struct cdecl_array *a)
72 size_t ret = 0, rc = 0;
75 rc = snprintf(buf, n, "variable-length array");
77 rc = snprintf(buf, n, "array");
78 ret += cdecl__advance(&buf, &n, rc);
81 rc = snprintf(buf, n, "%s", a->vla);
82 ret += cdecl__advance(&buf, &n, rc);
83 } else if (a->length) {
84 rc = snprintf(buf, n, "%" PRIuMAX, a->length);
85 ret += cdecl__advance(&buf, &n, rc);
88 return ret + snprintf(buf, n, "of");
92 explain_declarators(char *buf, size_t n, struct cdecl_declarator *decl);
94 static size_t explain_decl(char *buf, size_t n, struct cdecl *decl, bool top)
98 rc = explain_prologue(buf, n, decl->declarators, top);
99 ret += cdecl__advance(&buf, &n, rc);
101 rc = cdecl__explain_pre_specs(buf, n, decl->specifiers);
102 ret += cdecl__advance(&buf, &n, rc);
104 rc = explain_declarators(buf, n, decl->declarators);
105 ret += cdecl__advance(&buf, &n, rc);
107 return ret + cdecl__explain_post_specs(buf, n, decl->specifiers);
110 static size_t explain_function(char *buf, size_t n, struct cdecl_function *f)
112 size_t ret = 0, rc = 0;
114 rc = snprintf(buf, n, "function");
115 ret += cdecl__advance(&buf, &n, rc);
118 rc = snprintf(buf, n, "(");
119 ret += cdecl__advance_(&buf, &n, rc);
121 for (struct cdecl *p = f->parameters; p; p = p->next) {
122 rc = explain_decl(buf, n, p, false);
123 ret += cdecl__advance_(&buf, &n, rc);
126 rc = snprintf(buf, n, ",");
127 else if (f->variadic)
128 rc = snprintf(buf, n, ", ...)");
130 rc = snprintf(buf, n, ")");
131 ret += cdecl__advance(&buf, &n, rc);
135 return ret + snprintf(buf, n, "returning");
139 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
143 if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
146 rc = explain_declarators(buf, n, d->child);
147 ret += cdecl__advance(&buf, &n, rc);
150 case CDECL_DECL_POINTER:
151 return ret + explain_pointer(buf, n, &d->u.pointer);
152 case CDECL_DECL_ARRAY:
153 return ret + explain_array(buf, n, &d->u.array);
154 case CDECL_DECL_FUNCTION:
155 return ret + explain_function(buf, n, &d->u.function);
161 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
163 return explain_decl(buf, n, decl, true);