2 * Render C declarations.
3 * Copyright © 2011 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 static size_t declare_specs(char *buf, size_t n, struct cdecl_declspec *s)
33 rc = cdecl__explain_pre_specs(buf, n, s);
34 ret += cdecl__advance(&buf, &n, rc);
36 rc = cdecl__explain_post_specs(buf, n, s);
41 declare_declarator(char *buf, size_t n, struct cdecl_declarator *d);
43 static size_t declare_decl(char *buf, size_t n, struct cdecl *decl)
47 rc = declare_specs(buf, n, decl->specifiers);
48 if (decl->declarators->type != CDECL_DECL_NULL)
49 ret += cdecl__advance(&buf, &n, rc);
51 ret += cdecl__advance_(&buf, &n, rc);
53 return ret + declare_declarator(buf, n, decl->declarators);
57 declare_postfix_child(char *buf, size_t n, struct cdecl_declarator *d)
61 if (d->type == CDECL_DECL_POINTER) {
62 rc = snprintf(buf, n, "(");
63 ret += cdecl__advance_(&buf, &n, rc);
66 rc = declare_declarator(buf, n, d);
67 ret += cdecl__advance_(&buf, &n, rc);
69 if (d->type == CDECL_DECL_POINTER) {
70 rc = snprintf(buf, n, ")");
71 ret += cdecl__advance_(&buf, &n, rc);
77 static size_t declare_pointer(char *buf, size_t n, struct cdecl_pointer *p)
81 rc = snprintf(buf, n, "*");
83 ret += cdecl__advance(&buf, &n, rc);
85 ret += cdecl__advance_(&buf, &n, rc);
87 rc = cdecl__explain_specs(buf, n, p->qualifiers, CDECL_SPEC_QUAL);
91 static bool pointer_needs_space(struct cdecl_declarator *d)
93 assert(d->type == CDECL_DECL_POINTER);
95 if (d->u.pointer.qualifiers)
96 return d->child->type != CDECL_DECL_NULL;
101 static size_t declare_array(char *buf, size_t n, struct cdecl_array *a)
105 rc = snprintf(buf, n, "[");
106 ret += cdecl__advance_(&buf, &n, rc);
109 rc = snprintf(buf, n, "%s", a->vla[0] ? a->vla : "*");
111 rc = snprintf(buf, n, "%.0ju", a->length);
112 ret += cdecl__advance_(&buf, &n, rc);
114 return ret + snprintf(buf, n, "]");
117 static size_t declare_function(char *buf, size_t n, struct cdecl_function *f)
121 rc = snprintf(buf, n, "(");
122 ret += cdecl__advance_(&buf, &n, rc);
124 for (struct cdecl *p = f->parameters; p; p = p->next) {
125 rc = declare_decl(buf, n, p);
126 ret += cdecl__advance_(&buf, &n, rc);
130 rc = snprintf(buf, n, ", ");
131 else if (f->variadic)
132 rc = snprintf(buf, n, ", ...");
133 ret += cdecl__advance_(&buf, &n, rc);
136 return ret + snprintf(buf, n, ")");
140 declare_declarator(char *buf, size_t n, struct cdecl_declarator *d)
144 for (; d; d = d->child) {
146 case CDECL_DECL_NULL:
148 case CDECL_DECL_IDENT:
149 rc = snprintf(buf, n, "%s", d->u.ident);
150 ret += cdecl__advance_(&buf, &n, rc);
152 case CDECL_DECL_POINTER:
153 rc = declare_pointer(buf, n, &d->u.pointer);
154 if (pointer_needs_space(d))
155 ret += cdecl__advance(&buf, &n, rc);
157 ret += cdecl__advance_(&buf, &n, rc);
160 * Arrays and functions are special: since they are postfix,
161 * we need to render the children before rendering their
164 case CDECL_DECL_ARRAY:
165 rc = declare_postfix_child(buf, n, d->child);
166 ret += cdecl__advance_(&buf, &n, rc);
167 return ret + declare_array(buf, n, &d->u.array);
168 case CDECL_DECL_FUNCTION:
169 rc = declare_postfix_child(buf, n, d->child);
170 ret += cdecl__advance_(&buf, &n, rc);
171 return ret + declare_function(buf, n, &d->u.function);
180 size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl)
182 return declare_decl(buf, n, decl);