2 * Render C declarations.
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/>.
25 #include "cdecl-internal.h"
27 static size_t declare_specs(char *buf, size_t n, struct cdecl_declspec *s)
34 rc = cdecl__explain_pre_specs(buf, n, s);
35 ret += cdecl__advance(&buf, &n, rc);
37 rc = cdecl__explain_post_specs(buf, n, s);
42 declare_declarator(char *buf, size_t n, struct cdecl_declarator *d);
44 static size_t declare_decl(char *buf, size_t n, struct cdecl *decl)
48 rc = declare_specs(buf, n, decl->specifiers);
49 if (decl->declarators->type != CDECL_DECL_NULL)
50 ret += cdecl__advance(&buf, &n, rc);
52 ret += cdecl__advance_(&buf, &n, rc);
54 return ret + declare_declarator(buf, n, decl->declarators);
58 declare_postfix_child(char *buf, size_t n, struct cdecl_declarator *d)
62 if (d->type == CDECL_DECL_POINTER) {
63 rc = snprintf(buf, n, "(");
64 ret += cdecl__advance_(&buf, &n, rc);
67 rc = declare_declarator(buf, n, d);
68 ret += cdecl__advance_(&buf, &n, rc);
70 if (d->type == CDECL_DECL_POINTER) {
71 rc = snprintf(buf, n, ")");
72 ret += cdecl__advance_(&buf, &n, rc);
78 static size_t declare_pointer(char *buf, size_t n, struct cdecl_pointer *p)
82 rc = snprintf(buf, n, "*");
84 ret += cdecl__advance(&buf, &n, rc);
86 ret += cdecl__advance_(&buf, &n, rc);
88 rc = cdecl__explain_specs(buf, n, p->qualifiers, CDECL_SPEC_QUAL);
92 static bool pointer_needs_space(struct cdecl_declarator *d)
94 assert(d->type == CDECL_DECL_POINTER);
96 if (d->u.pointer.qualifiers)
97 return d->child->type != CDECL_DECL_NULL;
102 static size_t declare_array(char *buf, size_t n, struct cdecl_array *a)
106 rc = snprintf(buf, n, "[");
107 ret += cdecl__advance_(&buf, &n, rc);
110 rc = snprintf(buf, n, "%s", a->vla[0] ? a->vla : "*");
112 rc = snprintf(buf, n, "%.0ju", a->length);
113 ret += cdecl__advance_(&buf, &n, rc);
115 return ret + snprintf(buf, n, "]");
118 static size_t declare_function(char *buf, size_t n, struct cdecl_function *f)
122 rc = snprintf(buf, n, "(");
123 ret += cdecl__advance_(&buf, &n, rc);
125 for (struct cdecl *p = f->parameters; p; p = p->next) {
126 rc = declare_decl(buf, n, p);
127 ret += cdecl__advance_(&buf, &n, rc);
131 rc = snprintf(buf, n, ", ");
132 else if (f->variadic)
133 rc = snprintf(buf, n, ", ...");
134 ret += cdecl__advance_(&buf, &n, rc);
137 return ret + snprintf(buf, n, ")");
141 declare_declarator(char *buf, size_t n, struct cdecl_declarator *d)
145 for (; d; d = d->child) {
147 case CDECL_DECL_NULL:
149 case CDECL_DECL_IDENT:
150 rc = snprintf(buf, n, "%s", d->u.ident);
151 ret += cdecl__advance_(&buf, &n, rc);
153 case CDECL_DECL_POINTER:
154 rc = declare_pointer(buf, n, &d->u.pointer);
155 if (pointer_needs_space(d))
156 ret += cdecl__advance(&buf, &n, rc);
158 ret += cdecl__advance_(&buf, &n, rc);
161 * Arrays and functions are special: since they are postfix,
162 * we need to render the children before rendering their
165 case CDECL_DECL_ARRAY:
166 rc = declare_postfix_child(buf, n, d->child);
167 ret += cdecl__advance_(&buf, &n, rc);
168 return ret + declare_array(buf, n, &d->u.array);
169 case CDECL_DECL_FUNCTION:
170 rc = declare_postfix_child(buf, n, d->child);
171 ret += cdecl__advance_(&buf, &n, rc);
172 return ret + declare_function(buf, n, &d->u.function);
181 size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl)
183 return declare_decl(buf, n, decl);