]> git.draconx.ca Git - cdecl99.git/blob - src/output.c
8fffd24af2e8129455e9e07f5e2ee4da356a2956
[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 const char *explain_spec_simple(struct cdecl_declspec *s)
52 {
53         switch (s->type) {
54 #       include "namespecs.h"
55         }
56
57         assert(0);
58 }
59
60 static size_t explain_spec(char *buf, size_t n, struct cdecl_declspec *s)
61 {
62         const char *keyword = explain_spec_simple(s);
63
64         if (keyword[0] && s->ident)
65                 return snprintf(buf, n, "%s %s", keyword, s->ident);
66         else if (s->ident)
67                 return snprintf(buf, n, "%s", s->ident);
68         return snprintf(buf, n, "%s", keyword);
69 }
70
71 /*
72  * Render a list of declaration specifiers.  Only the declaration specifiers
73  * listed in mask, which is the bitwise OR of the desired specifier kinds, are
74  * printed.
75  */
76 size_t cdecl__explain_specs(char *buf, size_t n, struct cdecl_declspec *s,
77                                                  unsigned mask)
78 {
79         size_t ret = 0, rc = 0;
80
81         for (struct cdecl_declspec *c = s; c; c = c->next) {
82                 switch (cdecl_spec_kind(c) & mask) {
83                 case CDECL_SPEC_FUNC:
84                 case CDECL_SPEC_STOR:
85                 case CDECL_SPEC_QUAL:
86                 case CDECL_SPEC_TYPE:
87                         ret += cdecl__advance(&buf, &n, rc);
88                         rc = explain_spec(buf, n, c);
89                 }
90         }
91
92         return ret + rc;
93 }
94
95 /* Renders the storage-class and function specifiers in canonical form. */
96 size_t cdecl__explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
97 {
98         return cdecl__explain_specs(buf, n, s, CDECL_SPEC_FUNC|CDECL_SPEC_STOR);
99 }
100
101 /* Renders the type qualifiers and type specifiers in canonical form. */
102 size_t cdecl__explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
103 {
104         return cdecl__explain_specs(buf, n, s, CDECL_SPEC_QUAL|CDECL_SPEC_TYPE);
105 }