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