]> git.draconx.ca Git - cdecl99.git/blob - src/explain.c
1b46d20a98640d8890526577c5b3e4a2793a0db6
[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 <config.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <stdbool.h>
23 #include <assert.h>
24
25 #include "cdecl.h"
26 #include "output.h"
27
28 /*
29  * Renders the start of the thing being declared.  If top is true, print
30  * the "declare" or "type" keywords at the front, as appropriate.
31  */
32 static size_t
33 explain_prologue(char *buf, size_t n, struct cdecl_declarator *d, bool top)
34 {
35         size_t ret = 0, rc = 0;
36
37         while (d) {
38                 switch (d->type) {
39                 case CDECL_DECL_NULL:
40                         if (top)
41                                 return snprintf(buf, n, "type");
42                         return 0;
43                 case CDECL_DECL_IDENT:
44                         if (top)
45                                 rc = snprintf(buf, n, "declare");
46                         ret += cdecl__advance(&buf, &n, rc);
47                         return ret + snprintf(buf, n, "%s as", d->u.ident);
48                 }
49
50                 d = d->child;
51         }
52
53         assert(0);
54 }
55
56 static size_t
57 explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
58 {
59         size_t ret = 0, rc;
60
61         rc = cdecl__explain_specs(buf, n, p->qualifiers, CDECL_SPEC_QUAL);
62         ret += cdecl__advance(&buf, &n, rc);
63
64         return ret + snprintf(buf, n, "pointer to");
65 }
66
67 static size_t
68 explain_array(char *buf, size_t n, struct cdecl_array *a)
69 {
70         size_t ret = 0, rc = 0;
71
72         if (a->vla)
73                 rc = snprintf(buf, n, "variable-length array");
74         else
75                 rc = snprintf(buf, n, "array");
76         ret += cdecl__advance(&buf, &n, rc);
77
78         if (a->vla) {
79                 rc = snprintf(buf, n, "%s", a->vla);
80                 ret += cdecl__advance(&buf, &n, rc);
81         } else if (a->length) {
82                 rc = snprintf(buf, n, "%ju", a->length);
83                 ret += cdecl__advance(&buf, &n, rc);
84         }
85
86         return ret + snprintf(buf, n, "of");
87 }
88
89 static size_t
90 explain_declarators(char *buf, size_t n, struct cdecl_declarator *decl);
91
92 static size_t explain_decl(char *buf, size_t n, struct cdecl *decl, bool top)
93 {
94         size_t ret = 0, rc;
95
96         rc = explain_prologue(buf, n, decl->declarators, top);
97         ret += cdecl__advance(&buf, &n, rc);
98
99         rc = cdecl__explain_pre_specs(buf, n, decl->specifiers);
100         ret += cdecl__advance(&buf, &n, rc);
101
102         rc = explain_declarators(buf, n, decl->declarators);
103         ret += cdecl__advance(&buf, &n, rc);
104
105         return ret + cdecl__explain_post_specs(buf, n, decl->specifiers);
106 }
107
108 static size_t explain_function(char *buf, size_t n, struct cdecl_function *f)
109 {
110         size_t ret = 0, rc = 0;
111
112         rc = snprintf(buf, n, "function");
113         ret += cdecl__advance(&buf, &n, rc);
114
115         if (f->parameters) {
116                 rc = snprintf(buf, n, "(");
117                 ret += cdecl__advance_(&buf, &n, rc);
118
119                 for (struct cdecl *p = f->parameters; p; p = p->next) {
120                         rc = explain_decl(buf, n, p, false);
121                         ret += cdecl__advance_(&buf, &n, rc);
122
123                         if (p->next)
124                                 rc = snprintf(buf, n, ",");
125                         else if (f->variadic)
126                                 rc = snprintf(buf, n, ", ...)");
127                         else
128                                 rc = snprintf(buf, n, ")");
129                         ret += cdecl__advance(&buf, &n, rc);
130                 }
131         }
132
133         return ret + snprintf(buf, n, "returning");
134 }
135
136 static size_t
137 explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
138 {
139         size_t ret = 0, rc;
140
141         if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
142                 return 0;
143
144         rc = explain_declarators(buf, n, d->child);
145         ret += cdecl__advance(&buf, &n, rc);
146
147         switch (d->type) {
148         case CDECL_DECL_POINTER:
149                 return ret + explain_pointer(buf, n, &d->u.pointer);
150         case CDECL_DECL_ARRAY:
151                 return ret + explain_array(buf, n, &d->u.array);
152         case CDECL_DECL_FUNCTION:
153                 return ret + explain_function(buf, n, &d->u.function);
154         default:
155                 assert(0);
156         }
157 }
158
159 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
160 {
161         return explain_decl(buf, n, decl, true);
162 }