8 #define PASTE(a, b) a ## b
9 #define PASTE2(a, b) PASTE(a, b)
11 #define BIT1(a) ((1ul<<(CDECL_TYPE_ ## a)))
12 #define BIT2(a, b) ((1ul<<(CDECL_TYPE_ ## a))|(1ul<<(CDECL_TYPE_ ## b)))
13 #define BIT3(a, b, c) ((1ul<<(CDECL_TYPE_ ## a))|(1ul<<(CDECL_TYPE_ ## b))|(1ul<<(CDECL_TYPE_ ## c)))
14 #define BIT4(a, b, c, d) ((1ul<<(CDECL_TYPE_ ## a))|(1ul<<(CDECL_TYPE_ ## b))|(1ul<<(CDECL_TYPE_ ## c))|(1ul<<(CDECL_TYPE_ ## d)))
16 #define NARG_(_4, _3, _2, _1, n, ...) n
17 #define NARG(...) NARG_(__VA_ARGS__, 4, 3, 2, 1)
19 #define BITS(...) PASTE2(BIT, NARG(__VA_ARGS__))(__VA_ARGS__)
22 * We can represent type specifiers as a bitmap, which gives us a finite
23 * list of acceptable bitmap values according to the C standard. However,
24 * the "long" specifier is allowed to occur more than once, but only at most
25 * 2 times. Treat it as a special case, assigning an unused bit to represent
28 #define CDECL_TYPE_LLONG 32
30 static int typemap_verify(unsigned long map)
33 * This is the complete list of valid type specifiers from C99ยง6.7.2#2
39 case BITS(SIGNED, CHAR):
40 case BITS(UNSIGNED, CHAR):
42 case BITS(SIGNED, SHORT):
43 case BITS(SHORT, INT):
44 case BITS(SIGNED, SHORT, INT):
45 case BITS(UNSIGNED, SHORT):
46 case BITS(UNSIGNED, SHORT, INT):
49 case BITS(SIGNED, INT):
51 case BITS(UNSIGNED, INT):
53 case BITS(SIGNED, LONG):
54 case BITS(SIGNED, LONG, INT):
55 case BITS(UNSIGNED, LONG):
56 case BITS(UNSIGNED, LONG, INT):
57 case BITS(LLONG, LONG):
58 case BITS(SIGNED, LLONG, LONG):
59 case BITS(SIGNED, LLONG, LONG, INT):
60 case BITS(UNSIGNED, LLONG, LONG):
61 case BITS(UNSIGNED, LLONG, LONG, INT):
65 case BITS(LONG, DOUBLE):
66 case BITS(FLOAT, COMPLEX):
67 case BITS(DOUBLE, COMPLEX):
68 case BITS(LONG, DOUBLE, COMPLEX):
80 typemap_add_typespec(unsigned long map, struct cdecl_declspec *s)
82 assert(s->type < CDECL_TYPE_LLONG);
84 if (s->type == CDECL_TYPE_LONG) {
85 if (map & BITS(LLONG)) {
86 fprintf(stderr, "too many long specifiers\n");
88 } else if (map & BITS(LONG)) {
89 return map | BITS(LLONG);
93 if (map & (1ul<<s->type)) {
94 fprintf(stderr, "duplicate type specifier\n");
98 return map | (1<<s->type);
101 static int verify_specs(struct cdecl_declspec *s)
103 unsigned long typemap = 0;
104 unsigned num_storage = 0;
106 for (struct cdecl_declspec *c = s; c; c = c->next) {
107 switch (cdecl_spec_kind(c)) {
108 case CDECL_SPEC_TYPE:
109 typemap = typemap_add_typespec(typemap, c);
114 case CDECL_SPEC_STOR:
115 if (++num_storage > 1) {
116 fprintf(stderr, "too many storage-class specifiers\n");
120 case CDECL_SPEC_QUAL:
122 * Since we don't support pointer types yet, all
123 * restrict qualifiers are invalid. Other qualifiers
126 if (c->type == CDECL_QUAL_RESTRICT) {
127 fprintf(stderr, "only pointer types can be restrict-qualified.\n");
131 case CDECL_SPEC_FUNC:
133 * Likewise for function specifiers.
135 fprintf(stderr, "only function declarations may have function specifiers.\n");
142 if (typemap_verify(typemap) == -1) {
143 fprintf(stderr, "conflicting type specifiers\n");
150 static int verify_decl(struct cdecl *decl)
152 return verify_specs(decl->specifiers);
155 struct cdecl *cdecl_parse_decl(const char *declstr)
157 YY_BUFFER_STATE state;
161 state = yy_scan_string(declstr);
163 yy_delete_buffer(state);
168 rc = verify_decl(decl);