]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl.h
Initial support for function declarators.
[cdecl99.git] / src / cdecl.h
1 /*
2  *  Copyright © 2011 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 /* Declaration specifier kinds. */
25 enum {
26         CDECL_SPEC_TYPE = 0,
27         CDECL_SPEC_STOR = 256,
28         CDECL_SPEC_QUAL = 512,
29         CDECL_SPEC_FUNC = 1024,
30 };
31
32 enum {
33         CDECL_TYPE_VOID = CDECL_SPEC_TYPE,
34         CDECL_TYPE_CHAR,
35         CDECL_TYPE_SHORT,
36         CDECL_TYPE_INT,
37         CDECL_TYPE_LONG,
38         CDECL_TYPE_FLOAT,
39         CDECL_TYPE_DOUBLE,
40         CDECL_TYPE_SIGNED,
41         CDECL_TYPE_UNSIGNED,
42         CDECL_TYPE_BOOL,
43         CDECL_TYPE_COMPLEX,
44         CDECL_TYPE_STRUCT,
45         CDECL_TYPE_UNION,
46         CDECL_TYPE_ENUM,
47         CDECL_TYPE_IDENT,
48         CDECL_STOR_TYPEDEF = CDECL_SPEC_STOR,
49         CDECL_STOR_EXTERN,
50         CDECL_STOR_STATIC,
51         CDECL_STOR_AUTO,
52         CDECL_STOR_REGISTER,
53         CDECL_QUAL_RESTRICT = CDECL_SPEC_QUAL,
54         CDECL_QUAL_VOLATILE,
55         CDECL_QUAL_CONST,
56         CDECL_FUNC_INLINE = CDECL_SPEC_FUNC,
57 };
58
59 /* Declarator types. */
60 enum {
61         CDECL_DECL_NULL,
62         CDECL_DECL_IDENT,
63         CDECL_DECL_POINTER,
64         CDECL_DECL_ARRAY,
65         CDECL_DECL_FUNCTION,
66 };
67
68 struct cdecl {
69         struct cdecl *next;
70
71         struct cdecl_declspec {
72                 struct cdecl_declspec *next;
73                 unsigned type;
74                 char *ident;
75         } *specifiers;
76
77         struct cdecl_declarator {
78                 struct cdecl_declarator *child;
79                 unsigned type;
80                 union {
81                         char *ident;
82                         struct cdecl_pointer {
83                                 struct cdecl_declspec *qualifiers;
84                         } pointer;
85                         struct cdecl_array {
86                                 char *vla;
87                                 uintmax_t length;
88                         } array;
89                         struct cdecl_function {
90                                 struct cdecl *parameters;
91                                 _Bool variadic;
92                         } function;
93                 } u;
94         } *declarators;
95 };
96
97 struct cdecl *cdecl_parse_decl(const char *declstr);
98 void cdecl_free(struct cdecl *decl);
99
100 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl);
101
102 static inline int cdecl_spec_kind(struct cdecl_declspec *spec)
103 {
104         return spec->type & ~0xffu;
105 }
106
107 #endif