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