2 * Render C declarations as English.
3 * Copyright © 2011, 2021, 2023-2024 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/>.
23 #include "cdecl-internal.h"
25 #define PRE_SPECS (CDECL_SPEC_FUNC|CDECL_SPEC_STOR)
26 #define POST_SPECS (CDECL_SPEC_QUAL|CDECL_SPEC_TYPE)
28 static void explain_specs(struct output_state *dst, struct cdecl_declspec *s, unsigned mask)
30 cdecl__emit(dst, cdecl__emit_specs(dst, s, mask));
34 * If declarator declares an identifier foo, then emit "foo as "; otherwise
38 explain_name(struct output_state *dst, struct cdecl_declarator *d)
43 if (d->type == CDECL_DECL_IDENT) {
44 cdecl__emit(dst, d->u.ident);
45 cdecl__emit(dst, " as ");
50 * For a pointer declarator, emit "[QUAL ]pointer to ", where
51 * QUAL is the (possibly empty) list of qualifiers.
54 explain_pointer(struct output_state *dst, struct cdecl_pointer *p)
56 explain_specs(dst, p->qualifiers, -1);
57 cdecl__emit(dst, "pointer to ");
61 * For an array declarator, emit "[variable-length ]array [X ]of ", where
62 * for a variable-length array X is the (possibly omitted) identifier name,
63 * and for normal arrays X is the (possibly omitted) length.
66 explain_array(struct output_state *dst, struct cdecl_array *a)
70 cdecl__emit(dst, "variable-length array " + (a->vla ? 0 : 16));
72 rc = cdecl__emit(dst, a->vla);
74 rc = cdecl__emit_uint(dst, a->length);
76 cdecl__emit(dst, " of " + !rc);
80 explain_declarators(struct output_state *dst, struct cdecl_declarator *decl);
82 static void explain_decl(struct output_state *dst, struct cdecl *decl)
84 explain_name(dst, decl->declarators);
85 explain_specs(dst, decl->specifiers, PRE_SPECS);
86 explain_declarators(dst, decl->declarators);
87 cdecl__emit_specs(dst, decl->specifiers, POST_SPECS);
91 * For a function declarator, emit "function [PARAM ]returning ", where
92 * PARAM is the (omitted in the case of an empty non-prototype identifier
93 * list) parameter or identifier lists enclosed in parentheses.
96 explain_function(struct output_state *dst, struct cdecl_function *f)
100 cdecl__emit(dst, "function ");
104 cdecl__emit(dst, "(");
106 for (p = f->parameters; p; p = p->next) {
107 explain_decl(dst, p);
109 cdecl__emit(dst, ", ");
112 tail_offset = f->variadic ? 0 : 5;
114 cdecl__emit(dst, ", ...) returning " + tail_offset);
118 explain_declarators(struct output_state *dst, struct cdecl_declarator *d)
121 explain_declarators(dst, d->child);
124 case CDECL_DECL_POINTER:
125 explain_pointer(dst, &d->u.pointer);
127 case CDECL_DECL_ARRAY:
128 explain_array(dst, &d->u.array);
130 case CDECL_DECL_FUNCTION:
131 explain_function(dst, &d->u.function);
136 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
138 struct output_state dst = { buf, n };
140 if (cdecl_is_abstract(decl->declarators))
141 cdecl__emit(&dst, "type ");
143 cdecl__emit(&dst, "declare ");
145 explain_decl(&dst, decl);