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