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