]> git.draconx.ca Git - cdecl99.git/blob - src/explain.c
Split out qualifier explanations from explain_post_specs.
[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
158 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
159 {
160         size_t ret = 0, rc;
161
162         /*
163          * XXX: This won't work when we add support for more complicated
164          * declarators.
165          */
166         rc = output(buf, n, 0, "declare %s as", decl->declarators->ident);
167         ret += advance(&buf, &n, rc);
168
169         rc = explain_pre_specs(buf, n, decl->specifiers);
170         ret += advance(&buf, &n, rc);
171
172         return ret + explain_post_specs(buf, n, decl->specifiers);
173 }