2 * Render C declarations as English.
3 * Copyright © 2011, 2021, 2023 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"
30 static size_t explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
32 return cdecl__explain_specs(buf, n, s, CDECL_SPEC_FUNC|CDECL_SPEC_STOR);
35 static size_t explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
37 return cdecl__explain_specs(buf, n, s, CDECL_SPEC_QUAL|CDECL_SPEC_TYPE);
41 explain_name(char *buf, size_t n, struct cdecl_declarator *d)
46 if (d->type != CDECL_DECL_IDENT)
49 return snprintf(buf, n, "%s as", d->u.ident);
53 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
57 rc = cdecl__explain_specs(buf, n, p->qualifiers, CDECL_SPEC_QUAL);
58 ret += cdecl__advance(&buf, &n, rc);
60 return ret + snprintf(buf, n, "pointer to");
64 explain_array(char *buf, size_t n, struct cdecl_array *a)
66 size_t ret = 0, rc = 0;
69 rc = snprintf(buf, n, "variable-length array");
71 rc = snprintf(buf, n, "array");
72 ret += cdecl__advance(&buf, &n, rc);
75 rc = snprintf(buf, n, "%s", a->vla);
76 ret += cdecl__advance(&buf, &n, rc);
77 } else if (a->length) {
78 rc = snprintf(buf, n, "%" PRIuMAX, a->length);
79 ret += cdecl__advance(&buf, &n, rc);
82 return ret + snprintf(buf, n, "of");
86 explain_declarators(char *buf, size_t n, struct cdecl_declarator *decl);
88 static size_t explain_decl(char *buf, size_t n, struct cdecl *decl)
92 rc = explain_name(buf, n, decl->declarators);
93 ret += cdecl__advance(&buf, &n, rc);
95 rc = explain_pre_specs(buf, n, decl->specifiers);
96 ret += cdecl__advance(&buf, &n, rc);
98 rc = explain_declarators(buf, n, decl->declarators);
99 ret += cdecl__advance(&buf, &n, rc);
101 return ret + explain_post_specs(buf, n, decl->specifiers);
104 static size_t explain_function(char *buf, size_t n, struct cdecl_function *f)
106 size_t ret = 0, rc = 0;
108 rc = snprintf(buf, n, "function");
109 ret += cdecl__advance(&buf, &n, rc);
112 rc = snprintf(buf, n, "(");
113 ret += cdecl__advance_(&buf, &n, rc);
115 for (struct cdecl *p = f->parameters; p; p = p->next) {
116 rc = explain_decl(buf, n, p);
117 ret += cdecl__advance_(&buf, &n, rc);
120 rc = snprintf(buf, n, ",");
121 else if (f->variadic)
122 rc = snprintf(buf, n, ", ...)");
124 rc = snprintf(buf, n, ")");
125 ret += cdecl__advance(&buf, &n, rc);
129 return ret + snprintf(buf, n, "returning");
133 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
137 if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
140 rc = explain_declarators(buf, n, d->child);
141 ret += cdecl__advance(&buf, &n, rc);
144 case CDECL_DECL_POINTER:
145 return ret + explain_pointer(buf, n, &d->u.pointer);
146 case CDECL_DECL_ARRAY:
147 return ret + explain_array(buf, n, &d->u.array);
148 case CDECL_DECL_FUNCTION:
149 return ret + explain_function(buf, n, &d->u.function);
155 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
159 if (cdecl_is_abstract(decl->declarators))
160 rc = snprintf(buf, n, "type");
162 rc = snprintf(buf, n, "declare");
163 ret = cdecl__advance(&buf, &n, rc);
165 return ret + explain_decl(buf, n, decl);