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