]> git.draconx.ca Git - cdecl99.git/blob - src/typemap.c
a280be20499f4777c727b0e0b761aee2764bbcb3
[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 <assert.h>
22 #include "cdecl.h"
23 #include "typemap.h"
24
25 /*
26  * We can represent type specifiers as a bitmap, which gives us a finite
27  * list of acceptable bitmap values according to the C standard.  However,
28  * the "long" specifier is allowed to occur more than once, but only at most
29  * 2 times.  Treat it as a special case, assigning an unused bit to represent
30  * the second long.
31  */
32 #define CDECL_TYPE_LLONG (CDECL_SPEC_TYPE + 31)
33
34 static inline unsigned long spec_bit(unsigned type)
35 {
36         return 1ul << (type & (CDECL_SPEC_TYPE - 1));
37 }
38
39 static unsigned long add_typespec(unsigned long map, struct cdecl_declspec *s)
40 {
41         assert(s->type >= CDECL_SPEC_TYPE && s->type < CDECL_TYPE_LLONG);
42
43         if (s->type == CDECL_TYPE_LONG) {
44                 if (map & spec_bit(CDECL_TYPE_LLONG)) {
45                         fprintf(stderr, "too many long specifiers\n");
46                         return -1;
47                 } else if (map & spec_bit(CDECL_TYPE_LONG)) {
48                         return map | spec_bit(CDECL_TYPE_LLONG);
49                 }
50         }
51
52         if (map & spec_bit(s->type)) {
53                 fprintf(stderr, "duplicate type specifier\n");
54                 return -1;
55         }
56
57         return map | spec_bit(s->type);
58 }
59
60 unsigned long cdecl__build_typemap(struct cdecl_declspec *s)
61 {
62         unsigned long map = 0;
63
64         for (struct cdecl_declspec *c = s; c; c = c->next) {
65                 if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
66                         continue;
67
68                 map = add_typespec(map, c);
69                 if (map == -1)
70                         return -1;
71         }
72
73         switch (map) {
74         case 0:
75                 fprintf(stderr, "no type specifiers\n");
76                 return -1;
77 #       include "validtypes.h"
78                 return map;
79         default:
80                 fprintf(stderr, "conflicting type specifiers\n");
81                 return -1;
82         }
83 }