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