]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl.h
Add support for array declarators.
[cdecl99.git] / src / cdecl.h
1 #ifndef CDECL_H_
2 #define CEDCL_H_
3
4 #include <stddef.h>
5 #include <stdint.h>
6
7 /* Declaration specifier kinds. */
8 enum {
9         CDECL_SPEC_TYPE = 0,
10         CDECL_SPEC_STOR = 256,
11         CDECL_SPEC_QUAL = 512,
12         CDECL_SPEC_FUNC = 1024,
13 };
14
15 enum {
16         CDECL_TYPE_VOID = CDECL_SPEC_TYPE,
17         CDECL_TYPE_CHAR,
18         CDECL_TYPE_SHORT,
19         CDECL_TYPE_INT,
20         CDECL_TYPE_LONG,
21         CDECL_TYPE_FLOAT,
22         CDECL_TYPE_DOUBLE,
23         CDECL_TYPE_SIGNED,
24         CDECL_TYPE_UNSIGNED,
25         CDECL_TYPE_BOOL,
26         CDECL_TYPE_COMPLEX,
27         CDECL_TYPE_STRUCT,
28         CDECL_TYPE_UNION,
29         CDECL_TYPE_ENUM,
30         CDECL_TYPE_IDENT,
31         CDECL_STOR_TYPEDEF = CDECL_SPEC_STOR,
32         CDECL_STOR_EXTERN,
33         CDECL_STOR_STATIC,
34         CDECL_STOR_AUTO,
35         CDECL_STOR_REGISTER,
36         CDECL_QUAL_RESTRICT = CDECL_SPEC_QUAL,
37         CDECL_QUAL_VOLATILE,
38         CDECL_QUAL_CONST,
39         CDECL_FUNC_INLINE = CDECL_SPEC_FUNC,
40 };
41
42 /* Declarator types. */
43 enum {
44         CDECL_DECL_IDENT,
45         CDECL_DECL_POINTER,
46         CDECL_DECL_ARRAY,
47 };
48
49 struct cdecl {
50         struct cdecl_declspec {
51                 struct cdecl_declspec *next;
52                 unsigned type;
53                 char *ident;
54         } *specifiers;
55
56         struct cdecl_declarator {
57                 struct cdecl_declarator *next;
58                 unsigned type;
59                 union {
60                         char *ident;
61                         struct cdecl_pointer {
62                                 struct cdecl_declspec   *qualifiers;
63                                 struct cdecl_declarator *declarator;
64                         } pointer;
65                         struct cdecl_array {
66                                 char *vla;
67                                 uintmax_t length;
68                                 struct cdecl_declarator *declarator;
69                         } array;
70                 } u;
71         } *declarators;
72 };
73
74 struct cdecl *cdecl_parse_decl(const char *declstr);
75 void cdecl_free(struct cdecl *decl);
76
77 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl);
78
79 static inline int cdecl_spec_kind(struct cdecl_declspec *spec)
80 {
81         return spec->type & ~0xffu;
82 }
83
84 #endif