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