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