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