]> git.draconx.ca Git - cdecl99.git/blob - src/declare.c
libcdecl: Avoid snprintf for integer conversions.
[cdecl99.git] / src / declare.c
1 /*
2  *  Render C declarations.
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 <stdbool.h>
22 #include <inttypes.h>
23 #include <assert.h>
24
25 #include "cdecl.h"
26 #include "cdecl-internal.h"
27
28 static void
29 declare_declarator(struct output_state *dst, struct cdecl_declarator *d);
30
31 static void declare_decl(struct output_state *dst, struct cdecl *decl)
32 {
33         const char *sep;
34
35         sep = cdecl__emit_specs(dst, decl->specifiers, -1);
36         if (decl->declarators->type != CDECL_DECL_NULL)
37                 cdecl__emit(dst, sep);
38
39         declare_declarator(dst, decl->declarators);
40 }
41
42 static void
43 declare_postfix_child(struct output_state *dst, struct cdecl_declarator *d)
44 {
45         if (d->type == CDECL_DECL_POINTER)
46                 cdecl__emit(dst, "(");
47
48         declare_declarator(dst, d);
49
50         if (d->type == CDECL_DECL_POINTER)
51                 cdecl__emit(dst, ")");
52 }
53
54 static void declare_pointer(struct output_state *dst, struct cdecl_pointer *p)
55 {
56         struct cdecl_declspec *q = p->qualifiers;
57
58         if (q) {
59                 cdecl__emit(dst, "* ");
60                 cdecl__emit_specs(dst, q, -1);
61                 cdecl__emit(dst, " ");
62         } else {
63                 cdecl__emit(dst, "*");
64         }
65 }
66
67 static void declare_array(struct output_state *dst, struct cdecl_array *a)
68 {
69         cdecl__emit(dst, "[");
70
71         if (a->vla) {
72                 const char *s = a->vla[0] ? a->vla : "*";
73                 cdecl__emit(dst, s);
74         } else {
75                 cdecl__emit_uint(dst, a->length);
76         }
77
78         cdecl__emit(dst, "]");
79 }
80
81 static void
82 declare_function(struct output_state *dst, struct cdecl_function *f)
83 {
84         struct cdecl *p;
85
86         cdecl__emit(dst, "(");
87
88         for (p = f->parameters; p; p = p->next) {
89                 declare_decl(dst, p);
90
91                 if (p->next)
92                         cdecl__emit(dst, ", ");
93                 else if (f->variadic)
94                         cdecl__emit(dst, ", ...");
95         }
96
97         cdecl__emit(dst, ")");
98 }
99
100 static void
101 declare_declarator(struct output_state *dst, struct cdecl_declarator *d)
102 {
103         for (; d; d = d->child) {
104                 switch (d->type) {
105                 case CDECL_DECL_NULL:
106                         break;
107                 case CDECL_DECL_IDENT:
108                         cdecl__emit(dst, d->u.ident);
109                         break;
110                 case CDECL_DECL_POINTER:
111                         declare_pointer(dst, &d->u.pointer);
112                         break;
113                 /*
114                  * Arrays and functions are special: since they are postfix,
115                  * we need to render the children before rendering their
116                  * "bodies".
117                  */
118                 case CDECL_DECL_ARRAY:
119                         declare_postfix_child(dst, d->child);
120                         declare_array(dst, &d->u.array);
121                         return;
122                 case CDECL_DECL_FUNCTION:
123                         declare_postfix_child(dst, d->child);
124                         declare_function(dst, &d->u.function);
125                         return;
126                 default:
127                         assert(0);
128                 }
129         }
130 }
131
132 size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl)
133 {
134         struct output_state dst = { buf, n };
135
136         declare_decl(&dst, decl);
137
138         return dst.accum;
139 }