]> git.draconx.ca Git - cdecl99.git/blob - src/typemap.c
b8965a1b05d816d680a2375c3099e1bbe305e30f
[cdecl99.git] / src / typemap.c
1 /*
2  *  Helpers for dealing with type specifiers.
3  *  Copyright © 2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include <config.h>
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include "cdecl.h"
22 #include "typemap.h"
23
24 /*
25  * We can represent type specifiers as a bitmap, which gives us a finite
26  * list of acceptable bitmap values according to the C standard.  However,
27  * the "long" specifier is allowed to occur more than once, but only at most
28  * 2 times.  Treat it as a special case, assigning an unused bit to represent
29  * the second long.
30  */
31 #define CDECL_TYPE_LLONG 31
32
33 static unsigned long add_typespec(unsigned long map, struct cdecl_declspec *s)
34 {
35         if (s->type >= CDECL_TYPE_LLONG) {
36                 fprintf(stderr, "invalid type specifier\n");
37                 return -1;
38         }
39
40         if (s->type == CDECL_TYPE_LONG) {
41                 if (map & (1ul<<CDECL_TYPE_LLONG)) {
42                         fprintf(stderr, "too many long specifiers\n");
43                         return -1;
44                 } else if (map & (1ul<<CDECL_TYPE_LONG)) {
45                         return map | (1ul<<CDECL_TYPE_LLONG);
46                 }
47         }
48
49         if (map & (1ul<<s->type)) {
50                 fprintf(stderr, "duplicate type specifier\n");
51                 return -1;
52         }
53
54         return map | (1ul<<s->type);
55 }
56
57 unsigned long cdecl__build_typemap(struct cdecl_declspec *s)
58 {
59         unsigned long map = 0;
60
61         for (struct cdecl_declspec *c = s; c; c = c->next) {
62                 if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
63                         continue;
64
65                 map = add_typespec(map, c);
66                 if (map == -1)
67                         return -1;
68         }
69
70         switch (map) {
71         case 0:
72                 fprintf(stderr, "no type specifiers\n");
73                 return -1;
74 #       include "validtypes.h"
75                 return map;
76         default:
77                 fprintf(stderr, "conflicting type specifiers\n");
78                 return -1;
79         }
80 }
81
82 const char *cdecl__explain_typemap(unsigned long map)
83 {
84         switch (map) {
85 #       include "typenames.h"
86         default:
87                 fprintf(stderr, "invalid type specifiers\n");
88                 return NULL;
89         }
90 }