]> git.draconx.ca Git - cdecl99.git/blob - src/parse-decl.c
Kill typemap_is_valid.
[cdecl99.git] / src / parse-decl.c
1 #include <stdio.h>
2 #include <assert.h>
3
4 #include "cdecl.h"
5 #include "typemap.h"
6 #include "parse.h"
7 #include "scan.h"
8
9 static int verify_specs(struct cdecl_declspec *s)
10 {
11         unsigned num_storage = 0;
12         unsigned long typemap;
13
14         typemap = cdecl__build_typemap(s);
15         if (typemap == -1)
16                 return -1;
17
18         for (struct cdecl_declspec *c = s; c; c = c->next) {
19                 switch (cdecl_spec_kind(c)) {
20                 case CDECL_SPEC_TYPE:
21                         continue;
22                 case CDECL_SPEC_STOR:
23                         if (++num_storage > 1) {
24                                 fprintf(stderr, "too many storage-class specifiers\n");
25                                 return -1;
26                         }
27                         break;
28                 case CDECL_SPEC_QUAL:
29                         /*
30                          * Since we don't support pointer types yet, all
31                          * restrict qualifiers are invalid.  Other qualifiers
32                          * are always valid.
33                          */
34                         if (c->type == CDECL_QUAL_RESTRICT) {
35                                 fprintf(stderr, "only pointer types can be restrict-qualified.\n");
36                                 return -1;
37                         }
38                         break;
39                 case CDECL_SPEC_FUNC:
40                         /*
41                          * Likewise for function specifiers.
42                          */
43                         fprintf(stderr, "only function declarations may have function specifiers.\n");
44                         return -1;
45                 default:
46                         abort();
47                 }
48         }
49
50         return 0;
51 }
52
53 static int verify_decl(struct cdecl *decl)
54 {
55         return verify_specs(decl->specifiers);
56 }
57
58 struct cdecl *cdecl_parse_decl(const char *declstr)
59 {
60         YY_BUFFER_STATE state;
61         struct cdecl *decl;
62         int rc;
63
64         state = yy_scan_string(declstr);
65         rc = yyparse(&decl);
66         yy_delete_buffer(state);
67
68         if (rc != 0)
69                 return NULL;
70
71         rc = verify_decl(decl);
72         if (rc != 0) {
73                 cdecl_free(decl);
74                 return NULL;
75         }
76
77         return decl;
78 }