7 /* declare [ident] as [storage] [function specs] [other crap] [qualifiers] [type specs] */
10 voutput(char *buf, size_t n, size_t off, const char *fmt, va_list ap)
13 return vsnprintf(NULL, 0, fmt, ap);
14 return vsnprintf(buf+off, n-off, fmt, ap);
17 static size_t output(char *buf, size_t n, size_t off, const char *fmt, ...)
23 ret = voutput(buf, n, off, fmt, ap);
29 static size_t advance(char **buf, size_t *n, size_t amount)
39 if (amount + 1 >= *n) {
52 explain_qualifiers(char *buf, size_t n, struct cdecl_declspec *s)
54 unsigned long qualmap = 0;
55 size_t ret = 0, rc = 0;
57 for (struct cdecl_declspec *c = s; c; c = c->next) {
58 if (cdecl_spec_kind(c) != CDECL_SPEC_QUAL)
60 qualmap |= 1ul << (c->type & 0xff);
63 if (qualmap & (1ul << (CDECL_QUAL_RESTRICT & 0xff))) {
64 ret += advance(&buf, &n, rc);
65 rc = snprintf(buf, n, "restrict");
67 if (qualmap & (1ul << (CDECL_QUAL_VOLATILE & 0xff))) {
68 ret += advance(&buf, &n, rc);
69 rc = snprintf(buf, n, "volatile");
71 if (qualmap & (1ul << (CDECL_QUAL_CONST & 0xff))) {
72 ret += advance(&buf, &n, rc);
73 rc = snprintf(buf, n, "const");
79 /* Renders the type qualifiers and type specifiers in canonical form. */
81 explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
83 const char *tag = NULL;
84 unsigned long typemap;
87 typemap = cdecl__build_typemap(s);
91 for (struct cdecl_declspec *c = s; c; c = c->next) {
92 if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
95 /* Valid C types have at most one identifier. */
100 rc = explain_qualifiers(buf, n, s);
101 ret += advance(&buf, &n, rc);
103 rc = snprintf(buf, n, "%s", cdecl__explain_typemap(typemap));
105 ret += advance(&buf, &n, rc);
106 rc = snprintf(buf, n, "%s", tag);
112 static const char *explain_storage(unsigned spec)
115 case CDECL_STOR_TYPEDEF:
117 case CDECL_STOR_EXTERN:
119 case CDECL_STOR_STATIC:
121 case CDECL_STOR_AUTO:
123 case CDECL_STOR_REGISTER:
130 /* Renders the storage-class and function specifiers in canonical form. */
131 static size_t explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
133 unsigned long funcmap = 0;
134 const char *storage = NULL;
137 for (struct cdecl_declspec *c = s; c; c = c->next) {
138 switch (cdecl_spec_kind(c)) {
139 case CDECL_SPEC_FUNC:
140 funcmap |= 1ul << (c->type & 0xff);
142 case CDECL_SPEC_STOR:
143 /* Valid C declarations have at most one
144 * storage-class specifier. */
145 storage = explain_storage(c->type);
151 ret += output(buf, n, ret, "%s", storage);
152 if (funcmap & (1ul << (CDECL_FUNC_INLINE & 0xff)))
153 ret += output(buf, n, ret, "%.*s%s", !!ret, "", "inline");
157 static struct cdecl_declarator *next_declarator(struct cdecl_declarator *d)
160 case CDECL_DECL_IDENT:
162 case CDECL_DECL_POINTER:
163 return d->u.pointer.declarator;
169 /* Renders the name of the thing being declared. */
171 explain_prologue(char *buf, size_t n, struct cdecl_declarator *d)
174 if (d->type == CDECL_DECL_IDENT)
175 return snprintf(buf, n, "declare %s as", d->u.ident);
176 d = next_declarator(d);
181 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
185 rc = explain_qualifiers(buf, n, p->qualifiers);
186 ret += advance(&buf, &n, rc);
188 return ret + snprintf(buf, n, "pointer to");
192 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
196 if (d->type == CDECL_DECL_IDENT)
199 rc = explain_declarators(buf, n, next_declarator(d));
200 ret += advance(&buf, &n, rc);
203 case CDECL_DECL_POINTER:
204 return ret + explain_pointer(buf, n, &d->u.pointer);
210 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
214 rc = explain_prologue(buf, n, decl->declarators);
215 ret += advance(&buf, &n, rc);
217 rc = explain_pre_specs(buf, n, decl->specifiers);
218 ret += advance(&buf, &n, rc);
220 rc = explain_declarators(buf, n, decl->declarators);
221 ret += advance(&buf, &n, rc);
223 return ret + explain_post_specs(buf, n, decl->specifiers);