]> git.draconx.ca Git - cdecl99.git/blob - src/explain.c
Use assert(0) instead of abort for exceptional cases.
[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                         return snprintf(buf, n, "declare %s as", d->u.ident);
180                 d = next_declarator(d);
181         }
182 }
183
184 static size_t
185 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
186 {
187         size_t ret = 0, rc;
188
189         rc = explain_qualifiers(buf, n, p->qualifiers);
190         ret += advance(&buf, &n, rc);
191
192         return ret + snprintf(buf, n, "pointer to");
193 }
194
195 static size_t
196 explain_array(char *buf, size_t n, struct cdecl_array *a)
197 {
198         size_t ret = 0, rc = 0;
199
200         if (a->vla)
201                 rc = snprintf(buf, n, "variable-length array");
202         else
203                 rc = snprintf(buf, n, "array");
204         ret += advance(&buf, &n, rc);
205
206         if (a->vla) {
207                 rc = snprintf(buf, n, "%s", a->vla);
208                 ret += advance(&buf, &n, rc);
209         } else if (a->length) {
210                 rc = snprintf(buf, n, "%ju", a->length);
211                 ret += advance(&buf, &n, rc);
212         }
213
214         return ret + snprintf(buf, n, "of");
215 }
216
217 static size_t
218 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
219 {
220         size_t ret = 0, rc;
221
222         if (d->type == CDECL_DECL_IDENT)
223                 return 0;
224
225         rc = explain_declarators(buf, n, next_declarator(d));
226         ret += advance(&buf, &n, rc);
227
228         switch (d->type) {
229         case CDECL_DECL_POINTER:
230                 return ret + explain_pointer(buf, n, &d->u.pointer);
231         case CDECL_DECL_ARRAY:
232                 return ret + explain_array(buf, n, &d->u.array);
233         default:
234                 assert(0);
235         }
236 }
237
238 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
239 {
240         size_t ret = 0, rc;
241
242         rc = explain_prologue(buf, n, decl->declarators);
243         ret += advance(&buf, &n, rc);
244
245         rc = explain_pre_specs(buf, n, decl->specifiers);
246         ret += advance(&buf, &n, rc);
247
248         rc = explain_declarators(buf, n, decl->declarators);
249         ret += advance(&buf, &n, rc);
250
251         return ret + explain_post_specs(buf, n, decl->specifiers);
252 }