]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl.h
97f39bca800d8b49fb9be56daeccc5a806798fff
[cdecl99.git] / src / cdecl.h
1 /*
2  * Copyright © 2011, 2021, 2023 Nick Bowler
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #ifndef CDECL_H_
19 #define CDECL_H_
20
21 #include <stddef.h>
22 #include <stdint.h>
23
24 /* Compatibility typedefs */
25 #if HAVE__BOOL
26 typedef _Bool cdecl_bool;
27 #else
28 typedef signed char cdecl_bool;
29 #endif
30
31 /* Declaration specifier kinds. */
32 enum {
33         CDECL_SPEC_TYPE = 256,
34         CDECL_SPEC_STOR = 512,
35         CDECL_SPEC_QUAL = 1024,
36         CDECL_SPEC_FUNC = 2048
37 };
38
39 enum {
40         CDECL_TYPE_VOID = CDECL_SPEC_TYPE,
41         CDECL_TYPE_CHAR,
42         CDECL_TYPE_SHORT,
43         CDECL_TYPE_INT,
44         CDECL_TYPE_LONG,
45         CDECL_TYPE_FLOAT,
46         CDECL_TYPE_DOUBLE,
47         CDECL_TYPE_SIGNED,
48         CDECL_TYPE_UNSIGNED,
49         CDECL_TYPE_BOOL,
50         CDECL_TYPE_COMPLEX,
51         CDECL_TYPE_IMAGINARY,
52         CDECL_TYPE_STRUCT,
53         CDECL_TYPE_UNION,
54         CDECL_TYPE_ENUM,
55         CDECL_TYPE_IDENT,
56         CDECL_STOR_TYPEDEF = CDECL_SPEC_STOR,
57         CDECL_STOR_EXTERN,
58         CDECL_STOR_STATIC,
59         CDECL_STOR_AUTO,
60         CDECL_STOR_REGISTER,
61         CDECL_QUAL_RESTRICT = CDECL_SPEC_QUAL,
62         CDECL_QUAL_VOLATILE,
63         CDECL_QUAL_CONST,
64         CDECL_FUNC_INLINE = CDECL_SPEC_FUNC
65 };
66
67 /* Declarator types. */
68 enum {
69         CDECL_DECL_NULL,
70         CDECL_DECL_IDENT,
71         CDECL_DECL_POINTER,
72         CDECL_DECL_ARRAY,
73         CDECL_DECL_FUNCTION
74 };
75
76 struct cdecl {
77         struct cdecl *next;
78
79         struct cdecl_declspec {
80                 struct cdecl_declspec *next;
81                 unsigned type;
82                 char *ident;
83         } *specifiers;
84
85         struct cdecl_declarator {
86                 struct cdecl_declarator *child;
87                 unsigned type;
88                 union {
89                         char *ident;
90                         struct cdecl_pointer {
91                                 struct cdecl_declspec *qualifiers;
92                         } pointer;
93                         struct cdecl_array {
94                                 char *vla;
95                                 uintmax_t length;
96                         } array;
97                         struct cdecl_function {
98                                 struct cdecl *parameters;
99                                 cdecl_bool variadic;
100                         } function;
101                 } u;
102         } *declarators;
103 };
104
105 struct cdecl *cdecl_parse_decl(const char *declstr);
106 struct cdecl *cdecl_parse_english(const char *english);
107 void cdecl_free(struct cdecl *decl);
108
109 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl);
110 size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl);
111
112 static inline int cdecl_spec_kind(const struct cdecl_declspec *spec)
113 {
114         return spec->type & ~(CDECL_SPEC_TYPE-1u);
115 }
116
117 static inline int cdecl_is_abstract(const struct cdecl_declarator *d)
118 {
119         while (d->child)
120                 d = d->child;
121
122         return d->type != CDECL_DECL_IDENT;
123 }
124
125 /* Error handling. */
126 enum {
127         CDECL_ENOMEM = 1,
128         CDECL_ENOPARSE,
129
130         /* Obsolete error codes (no longer returned by the library) */
131         CDECL_EBADARRAY,
132         CDECL_EBADDECL,
133         CDECL_EBADPARAMS,
134         CDECL_EBADPOINTER,
135         CDECL_EBADQUAL,
136         CDECL_EBADRETURN,
137         CDECL_EBADSTOR,
138         CDECL_EBADTYPE,
139         CDECL_ENOTFUNC,
140         CDECL_EVOIDPARAM
141 };
142
143 struct cdecl_error {
144         unsigned code;
145         const char *str;
146 };
147
148 const struct cdecl_error *cdecl_get_error(void);
149
150 #endif