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