2 * Render C declarations.
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/>.
26 #include "cdecl-internal.h"
29 declare_declarator(char **dst, size_t *dstlen, struct cdecl_declarator *d);
31 static size_t declare_decl(char **dst, size_t *dstlen, struct cdecl *decl)
35 rc = cdecl__explain_specs(*dst, *dstlen, decl->specifiers, -1);
36 if (decl->declarators->type != CDECL_DECL_NULL)
37 ret = cdecl__advance(dst, dstlen, rc);
39 ret = cdecl__advance_(dst, dstlen, rc);
41 return ret + declare_declarator(dst, dstlen, decl->declarators);
45 declare_postfix_child(char **dst, size_t *dstlen, struct cdecl_declarator *d)
49 if (d->type == CDECL_DECL_POINTER)
50 ret += cdecl__emit(dst, dstlen, "(");
52 ret += declare_declarator(dst, dstlen, d);
54 if (d->type == CDECL_DECL_POINTER)
55 ret += cdecl__emit(dst, dstlen, ")");
61 declare_pointer(char **dst, size_t *dstlen, struct cdecl_pointer *p)
63 struct cdecl_declspec *q = p->qualifiers;
68 ret = cdecl__emit(dst, dstlen, "* ");
69 rc = cdecl__explain_specs(*dst, *dstlen, q, -1);
70 return ret + cdecl__advance(dst, dstlen, rc);
73 return cdecl__emit(dst, dstlen, "*");
76 static size_t declare_array(char **dst, size_t *dstlen, struct cdecl_array *a)
80 ret = cdecl__emit(dst, dstlen, "[");
83 const char *s = a->vla[0] ? a->vla : "*";
84 ret += cdecl__emit(dst, dstlen, s);
86 size_t rc = snprintf(*dst, *dstlen, "%.0" PRIuMAX, a->length);
87 ret += cdecl__advance_(dst, dstlen, rc);
90 return ret + cdecl__emit(dst, dstlen, "]");
94 declare_function(char **dst, size_t *dstlen, struct cdecl_function *f)
99 ret = cdecl__emit(dst, dstlen, "(");
101 for (p = f->parameters; p; p = p->next) {
102 ret += declare_decl(dst, dstlen, p);
105 ret += cdecl__emit(dst, dstlen, ", ");
106 else if (f->variadic)
107 ret += cdecl__emit(dst, dstlen, ", ...");
110 return ret + cdecl__emit(dst, dstlen, ")");
114 declare_declarator(char **dst, size_t *dstlen, struct cdecl_declarator *d)
118 for (; d; d = d->child) {
120 case CDECL_DECL_NULL:
122 case CDECL_DECL_IDENT:
123 ret += cdecl__emit(dst, dstlen, d->u.ident);
125 case CDECL_DECL_POINTER:
126 ret += declare_pointer(dst, dstlen, &d->u.pointer);
129 * Arrays and functions are special: since they are postfix,
130 * we need to render the children before rendering their
133 case CDECL_DECL_ARRAY:
134 ret += declare_postfix_child(dst, dstlen, d->child);
135 return ret + declare_array(dst, dstlen, &d->u.array);
136 case CDECL_DECL_FUNCTION:
137 ret += declare_postfix_child(dst, dstlen, d->child);
138 return ret + declare_function(dst, dstlen, &d->u.function);
147 size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl)
149 return declare_decl(&buf, &n, decl);