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