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/>.
25 #include "cdecl-internal.h"
28 declare_declarator(char *buf, size_t n, struct cdecl_declarator *d);
30 static size_t declare_decl(char *buf, size_t n, struct cdecl *decl)
34 rc = cdecl__explain_specs(buf, n, decl->specifiers, -1);
35 if (decl->declarators->type != CDECL_DECL_NULL)
36 ret += cdecl__advance(&buf, &n, rc);
38 ret += cdecl__advance_(&buf, &n, rc);
40 return ret + declare_declarator(buf, n, decl->declarators);
44 declare_postfix_child(char *buf, size_t n, struct cdecl_declarator *d)
48 if (d->type == CDECL_DECL_POINTER) {
49 rc = snprintf(buf, n, "(");
50 ret += cdecl__advance_(&buf, &n, rc);
53 rc = declare_declarator(buf, n, d);
54 ret += cdecl__advance_(&buf, &n, rc);
56 if (d->type == CDECL_DECL_POINTER) {
57 rc = snprintf(buf, n, ")");
58 ret += cdecl__advance_(&buf, &n, rc);
64 static size_t declare_pointer(char *buf, size_t n, struct cdecl_pointer *p)
68 rc = snprintf(buf, n, "*");
70 ret += cdecl__advance(&buf, &n, rc);
72 ret += cdecl__advance_(&buf, &n, rc);
74 rc = cdecl__explain_specs(buf, n, p->qualifiers, CDECL_SPEC_QUAL);
78 static bool pointer_needs_space(struct cdecl_declarator *d)
80 assert(d->type == CDECL_DECL_POINTER);
82 if (d->u.pointer.qualifiers)
83 return d->child->type != CDECL_DECL_NULL;
88 static size_t declare_array(char *buf, size_t n, struct cdecl_array *a)
92 rc = snprintf(buf, n, "[");
93 ret += cdecl__advance_(&buf, &n, rc);
96 rc = snprintf(buf, n, "%s", a->vla[0] ? a->vla : "*");
98 rc = snprintf(buf, n, "%.0ju", a->length);
99 ret += cdecl__advance_(&buf, &n, rc);
101 return ret + snprintf(buf, n, "]");
104 static size_t declare_function(char *buf, size_t n, struct cdecl_function *f)
108 rc = snprintf(buf, n, "(");
109 ret += cdecl__advance_(&buf, &n, rc);
111 for (struct cdecl *p = f->parameters; p; p = p->next) {
112 rc = declare_decl(buf, n, p);
113 ret += cdecl__advance_(&buf, &n, rc);
117 rc = snprintf(buf, n, ", ");
118 else if (f->variadic)
119 rc = snprintf(buf, n, ", ...");
120 ret += cdecl__advance_(&buf, &n, rc);
123 return ret + snprintf(buf, n, ")");
127 declare_declarator(char *buf, size_t n, struct cdecl_declarator *d)
131 for (; d; d = d->child) {
133 case CDECL_DECL_NULL:
135 case CDECL_DECL_IDENT:
136 rc = snprintf(buf, n, "%s", d->u.ident);
137 ret += cdecl__advance_(&buf, &n, rc);
139 case CDECL_DECL_POINTER:
140 rc = declare_pointer(buf, n, &d->u.pointer);
141 if (pointer_needs_space(d))
142 ret += cdecl__advance(&buf, &n, rc);
144 ret += cdecl__advance_(&buf, &n, rc);
147 * Arrays and functions are special: since they are postfix,
148 * we need to render the children before rendering their
151 case CDECL_DECL_ARRAY:
152 rc = declare_postfix_child(buf, n, d->child);
153 ret += cdecl__advance_(&buf, &n, rc);
154 return ret + declare_array(buf, n, &d->u.array);
155 case CDECL_DECL_FUNCTION:
156 rc = declare_postfix_child(buf, n, d->child);
157 ret += cdecl__advance_(&buf, &n, rc);
158 return ret + declare_function(buf, n, &d->u.function);
167 size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl)
169 return declare_decl(buf, n, decl);