]> git.draconx.ca Git - cdecl99.git/blob - src/output.c
Include config.h in all C source files.
[cdecl99.git] / src / output.c
1 #include <config.h>
2 #include <stdio.h>
3 #include <assert.h>
4
5 #include "typemap.h"
6 #include "output.h"
7 #include "cdecl.h"
8
9 size_t cdecl__advance_(char **buf, size_t *n, size_t amount)
10 {
11         if (amount >= *n) {
12                 *n   = 0;
13                 *buf = 0;
14         } else {
15                 *buf += amount;
16                 *n   -= amount;
17         }
18
19         return amount;
20 }
21
22 size_t cdecl__advance(char **buf, size_t *n, size_t amount)
23 {
24         size_t ret, rc;
25
26         if (!amount)
27                 return 0;
28
29         ret = cdecl__advance_(buf, n, amount);
30         rc = snprintf(*buf, *n, " ");
31         return ret + cdecl__advance_(buf, n, rc);
32 }
33
34 static size_t explain_storage(char *buf, size_t n, unsigned spec)
35 {
36         switch (spec) {
37         case CDECL_STOR_TYPEDEF:
38                 return snprintf(buf, n, "typedef");
39         case CDECL_STOR_EXTERN:
40                 return snprintf(buf, n, "extern");
41         case CDECL_STOR_STATIC:
42                 return snprintf(buf, n, "static");
43         case CDECL_STOR_AUTO:
44                 return snprintf(buf, n, "auto");
45         case CDECL_STOR_REGISTER:
46                 return snprintf(buf, n, "register");
47         default:
48                 assert(0);
49         }
50 }
51
52 /* Renders the storage-class and function specifiers in canonical form. */
53 size_t cdecl__explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
54 {
55         unsigned long funcmap = 0;
56         size_t ret = 0, rc = 0;
57
58         for (struct cdecl_declspec *c = s; c; c = c->next) {
59                 switch (cdecl_spec_kind(c)) {
60                 case CDECL_SPEC_FUNC:
61                         funcmap |= 1ul << (c->type & 0xff);
62                         break;
63                 case CDECL_SPEC_STOR:
64                         /* Valid C declarations have at most one
65                          * storage-class specifier. */
66                         rc = explain_storage(buf, n, c->type);
67                         break;
68                 }
69         }
70
71         if (funcmap & (1ul << (CDECL_FUNC_INLINE & 0xff))) {
72                 ret += cdecl__advance(&buf, &n, rc);
73                 rc = snprintf(buf, n, "inline");
74         }
75
76         return ret + rc;
77 }
78
79 size_t cdecl__explain_qualifiers(char *buf, size_t n, struct cdecl_declspec *s)
80 {
81         unsigned long qualmap = 0;
82         size_t ret = 0, rc = 0;
83
84         for (struct cdecl_declspec *c = s; c; c = c->next) {
85                 if (cdecl_spec_kind(c) != CDECL_SPEC_QUAL)
86                         continue;
87                 qualmap |= 1ul << (c->type & 0xff);
88         }
89
90         if (qualmap & (1ul << (CDECL_QUAL_RESTRICT & 0xff))) {
91                 ret += cdecl__advance(&buf, &n, rc);
92                 rc = snprintf(buf, n, "restrict");
93         }
94         if (qualmap & (1ul << (CDECL_QUAL_VOLATILE & 0xff))) {
95                 ret += cdecl__advance(&buf, &n, rc);
96                 rc = snprintf(buf, n, "volatile");
97         }
98         if (qualmap & (1ul << (CDECL_QUAL_CONST & 0xff))) {
99                 ret += cdecl__advance(&buf, &n, rc);
100                 rc = snprintf(buf, n, "const");
101         }
102
103         return ret + rc;
104 }
105
106 /* Renders the type qualifiers and type specifiers in canonical form. */
107 size_t cdecl__explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
108 {
109         const char *tag = NULL;
110         unsigned long typemap;
111         size_t ret = 0, rc;
112
113         typemap = cdecl__build_typemap(s);
114         if (typemap == -1)
115                 return 0;
116
117         for (struct cdecl_declspec *c = s; c; c = c->next) {
118                 if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
119                         continue;
120
121                 /* Valid C types have at most one identifier. */
122                 if (c->ident)
123                         tag = c->ident;
124         }
125
126         rc = cdecl__explain_qualifiers(buf, n, s);
127         ret += cdecl__advance(&buf, &n, rc);
128
129         rc = snprintf(buf, n, "%s", cdecl__explain_typemap(typemap));
130         if (tag) {
131                 ret += cdecl__advance(&buf, &n, rc);
132                 rc = snprintf(buf, n, "%s", tag);
133         }
134
135         return ret + rc;
136 }