]> git.draconx.ca Git - cdecl99.git/blob - src/explain.c
Add support for empty declarators.
[cdecl99.git] / src / explain.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <assert.h>
5
6 #include "cdecl.h"
7 #include "typemap.h"
8
9 /* declare [ident] as [storage] [function specs] [other crap] [qualifiers] [type specs] */
10
11 static size_t
12 voutput(char *buf, size_t n, size_t off, const char *fmt, va_list ap)
13 {
14         if (off >= n)
15                 return vsnprintf(NULL, 0, fmt, ap);
16         return vsnprintf(buf+off, n-off, fmt, ap);
17 }
18
19 static size_t output(char *buf, size_t n, size_t off, const char *fmt, ...)
20 {
21         va_list ap;
22         size_t ret;
23
24         va_start(ap, fmt);
25         ret = voutput(buf, n, off, fmt, ap);
26         va_end(ap);
27
28         return ret;
29 }
30
31 static size_t advance(char **buf, size_t *n, size_t amount)
32 {
33         if (!amount)
34                 return 0;
35
36         if (amount >= *n) {
37                 *n   = 0;
38                 *buf = 0;
39         } else {
40                 (*buf)[amount] = ' ';
41                 if (amount + 1 >= *n) {
42                         *buf = 0;
43                         *n = 0;
44                 } else {
45                         *buf += amount + 1;
46                         *n   -= amount + 1;
47                 }
48         }
49
50         return amount + 1;
51 }
52
53 static size_t
54 explain_qualifiers(char *buf, size_t n, struct cdecl_declspec *s)
55 {
56         unsigned long qualmap = 0;
57         size_t ret = 0, rc = 0;
58
59         for (struct cdecl_declspec *c = s; c; c = c->next) {
60                 if (cdecl_spec_kind(c) != CDECL_SPEC_QUAL)
61                         continue;
62                 qualmap |= 1ul << (c->type & 0xff);
63         }
64
65         if (qualmap & (1ul << (CDECL_QUAL_RESTRICT & 0xff))) {
66                 ret += advance(&buf, &n, rc);
67                 rc = snprintf(buf, n, "restrict");
68         }
69         if (qualmap & (1ul << (CDECL_QUAL_VOLATILE & 0xff))) {
70                 ret += advance(&buf, &n, rc);
71                 rc = snprintf(buf, n, "volatile");
72         }
73         if (qualmap & (1ul << (CDECL_QUAL_CONST & 0xff))) {
74                 ret += advance(&buf, &n, rc);
75                 rc = snprintf(buf, n, "const");
76         }
77
78         return ret + rc;
79 }
80
81 /* Renders the type qualifiers and type specifiers in canonical form. */
82 static size_t
83 explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
84 {
85         const char *tag = NULL;
86         unsigned long typemap;
87         size_t ret = 0, rc;
88
89         typemap = cdecl__build_typemap(s);
90         if (typemap == -1)
91                 return 0;
92
93         for (struct cdecl_declspec *c = s; c; c = c->next) {
94                 if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
95                         continue;
96
97                 /* Valid C types have at most one identifier. */
98                 if (c->ident)
99                         tag = c->ident;
100         }
101
102         rc = explain_qualifiers(buf, n, s);
103         ret += advance(&buf, &n, rc);
104
105         rc = snprintf(buf, n, "%s", cdecl__explain_typemap(typemap));
106         if (tag) {
107                 ret += advance(&buf, &n, rc);
108                 rc = snprintf(buf, n, "%s", tag);
109         }
110
111         return ret + rc;
112 }
113
114 static const char *explain_storage(unsigned spec)
115 {
116         switch (spec) {
117         case CDECL_STOR_TYPEDEF:
118                 return "typedef";
119         case CDECL_STOR_EXTERN:
120                 return "extern";
121         case CDECL_STOR_STATIC:
122                 return "static";
123         case CDECL_STOR_AUTO:
124                 return "auto";
125         case CDECL_STOR_REGISTER:
126                 return "register";
127         default:
128                 assert(0);
129         }
130 }
131
132 /* Renders the storage-class and function specifiers in canonical form. */
133 static size_t explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
134 {
135         unsigned long funcmap = 0;
136         const char *storage = NULL;
137         size_t ret = 0;
138
139         for (struct cdecl_declspec *c = s; c; c = c->next) {
140                 switch (cdecl_spec_kind(c)) {
141                 case CDECL_SPEC_FUNC:
142                         funcmap |= 1ul << (c->type & 0xff);
143                         break;
144                 case CDECL_SPEC_STOR:
145                         /* Valid C declarations have at most one
146                          * storage-class specifier. */
147                         storage = explain_storage(c->type);
148                         break;
149                 }
150         }
151
152         if (storage)
153                 ret += output(buf, n, ret, "%s", storage);
154         if (funcmap & (1ul << (CDECL_FUNC_INLINE & 0xff)))
155                 ret += output(buf, n, ret, "%.*s%s", !!ret, "", "inline");
156         return ret;
157 }
158
159 static struct cdecl_declarator *next_declarator(struct cdecl_declarator *d)
160 {
161         switch (d->type) {
162         case CDECL_DECL_IDENT:
163                 return NULL;
164         case CDECL_DECL_POINTER:
165                 return d->u.pointer.declarator;
166         case CDECL_DECL_ARRAY:
167                 return d->u.array.declarator;
168         default:
169                 assert(0);
170         }
171 }
172
173 /* Renders the name of the thing being declared. */
174 static size_t
175 explain_prologue(char *buf, size_t n, struct cdecl_declarator *d)
176 {
177         while (d) {
178                 if (d->type == CDECL_DECL_IDENT) {
179                         if (!d->u.ident)
180                                 return snprintf(buf, n, "type");
181                         return snprintf(buf, n, "declare %s as", d->u.ident);
182                 }
183                 d = next_declarator(d);
184         }
185 }
186
187 static size_t
188 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
189 {
190         size_t ret = 0, rc;
191
192         rc = explain_qualifiers(buf, n, p->qualifiers);
193         ret += advance(&buf, &n, rc);
194
195         return ret + snprintf(buf, n, "pointer to");
196 }
197
198 static size_t
199 explain_array(char *buf, size_t n, struct cdecl_array *a)
200 {
201         size_t ret = 0, rc = 0;
202
203         if (a->vla)
204                 rc = snprintf(buf, n, "variable-length array");
205         else
206                 rc = snprintf(buf, n, "array");
207         ret += advance(&buf, &n, rc);
208
209         if (a->vla) {
210                 rc = snprintf(buf, n, "%s", a->vla);
211                 ret += advance(&buf, &n, rc);
212         } else if (a->length) {
213                 rc = snprintf(buf, n, "%ju", a->length);
214                 ret += advance(&buf, &n, rc);
215         }
216
217         return ret + snprintf(buf, n, "of");
218 }
219
220 static size_t
221 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
222 {
223         size_t ret = 0, rc;
224
225         if (d->type == CDECL_DECL_IDENT)
226                 return 0;
227
228         rc = explain_declarators(buf, n, next_declarator(d));
229         ret += advance(&buf, &n, rc);
230
231         switch (d->type) {
232         case CDECL_DECL_POINTER:
233                 return ret + explain_pointer(buf, n, &d->u.pointer);
234         case CDECL_DECL_ARRAY:
235                 return ret + explain_array(buf, n, &d->u.array);
236         default:
237                 assert(0);
238         }
239 }
240
241 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
242 {
243         size_t ret = 0, rc;
244
245         rc = explain_prologue(buf, n, decl->declarators);
246         ret += advance(&buf, &n, rc);
247
248         rc = explain_pre_specs(buf, n, decl->specifiers);
249         ret += advance(&buf, &n, rc);
250
251         rc = explain_declarators(buf, n, decl->declarators);
252         ret += advance(&buf, &n, rc);
253
254         return ret + explain_post_specs(buf, n, decl->specifiers);
255 }