]> git.draconx.ca Git - cdecl99.git/blob - src/explain.c
libcdecl: Avoid snprintf for integer conversions.
[cdecl99.git] / src / explain.c
1 /*
2  *  Render C declarations as English.
3  *  Copyright © 2011, 2021, 2023 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
19 #include <config.h>
20 #include <stdio.h>
21 #include <inttypes.h>
22 #include <assert.h>
23
24 #include "cdecl.h"
25 #include "cdecl-internal.h"
26
27 #define PRE_SPECS (CDECL_SPEC_FUNC|CDECL_SPEC_STOR)
28 #define POST_SPECS (CDECL_SPEC_QUAL|CDECL_SPEC_TYPE)
29
30 static void explain_specs(struct output_state *dst, struct cdecl_declspec *s, unsigned mask)
31 {
32         cdecl__emit(dst, cdecl__emit_specs(dst, s, mask));
33 }
34
35 /*
36  * If declarator declares an identifier foo, then emit "foo as "; otherwise
37  * emit nothing.
38  */
39 static void
40 explain_name(struct output_state *dst, struct cdecl_declarator *d)
41 {
42         while (d->child)
43                 d = d->child;
44
45         if (d->type == CDECL_DECL_IDENT) {
46                 cdecl__emit(dst, d->u.ident);
47                 cdecl__emit(dst, " as ");
48         }
49 }
50
51 /*
52  * For a pointer declarator, emit "[QUAL ]pointer to ", where
53  * QUAL is the (possibly empty) list of qualifiers.
54  */
55 static void
56 explain_pointer(struct output_state *dst, struct cdecl_pointer *p)
57 {
58         explain_specs(dst, p->qualifiers, -1);
59         cdecl__emit(dst, "pointer to ");
60 }
61
62 /*
63  * For an array declarator, emit "[variable-length ]array [X ]of ", where
64  * for a variable-length array X is the (possibly omitted) identifier name,
65  * and for normal arrays X is the (possibly omitted) length.
66  */
67 static void
68 explain_array(struct output_state *dst, struct cdecl_array *a)
69 {
70         size_t rc = 0;
71
72         if (a->vla)
73                 cdecl__emit(dst, "variable-length ");
74         cdecl__emit(dst, "array ");
75
76         if (a->vla) {
77                 rc = cdecl__emit(dst, a->vla);
78         } else {
79                 rc = cdecl__emit_uint(dst, a->length);
80         }
81
82         cdecl__emit(dst, " of " + !rc);
83 }
84
85 static void
86 explain_declarators(struct output_state *dst, struct cdecl_declarator *decl);
87
88 static void explain_decl(struct output_state *dst, struct cdecl *decl)
89 {
90         explain_name(dst, decl->declarators);
91         explain_specs(dst, decl->specifiers, PRE_SPECS);
92         explain_declarators(dst, decl->declarators);
93         cdecl__emit_specs(dst, decl->specifiers, POST_SPECS);
94 }
95
96 /*
97  * For a function declarator, emit "function [PARAM ]returning ", where
98  * PARAM is the (omitted in the case of an empty non-prototype identifier
99  * list) parameter or identifier lists enclosed in parentheses.
100  */
101 static void
102 explain_function(struct output_state *dst, struct cdecl_function *f)
103 {
104         cdecl__emit(dst, "function ");
105
106         if (f->parameters) {
107                 struct cdecl *p;
108
109                 cdecl__emit(dst, "(");
110
111                 for (p = f->parameters; p; p = p->next) {
112                         explain_decl(dst, p);
113                         if (p->next)
114                                 cdecl__emit(dst, ", ");
115                 }
116
117                 if (f->variadic)
118                         cdecl__emit(dst, ", ...) ");
119                 else
120                         cdecl__emit(dst, ") ");
121         }
122
123         cdecl__emit(dst, "returning ");
124 }
125
126 static void
127 explain_declarators(struct output_state *dst, struct cdecl_declarator *d)
128 {
129         if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
130                 return;
131
132         explain_declarators(dst, d->child);
133
134         switch (d->type) {
135         case CDECL_DECL_POINTER:
136                 explain_pointer(dst, &d->u.pointer);
137                 return;
138         case CDECL_DECL_ARRAY:
139                 explain_array(dst, &d->u.array);
140                 return;
141         case CDECL_DECL_FUNCTION:
142                 explain_function(dst, &d->u.function);
143                 return;
144         default:
145                 assert(0);
146         }
147 }
148
149 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
150 {
151         struct output_state dst = { buf, n };
152
153         if (cdecl_is_abstract(decl->declarators))
154                 cdecl__emit(&dst, "type ");
155         else
156                 cdecl__emit(&dst, "declare ");
157
158         explain_decl(&dst, decl);
159         return dst.accum;
160 }