4 /* combine two declaration_specifires structs that are bits of the same list of
5 * declaration specifiers, checking relevant constraints on the way */
6 struct declaration_specifiers
*merge_declaration_specifiers
7 (struct declaration_specifiers
*a
,
8 struct declaration_specifiers
*b
) {
9 unsigned long basic
, ci
, length
, sign
, definition
;
11 /* constraints comes from C99 6.7.2 */
13 /* can only have one basic type specifier */
14 if((basic
= a
->type_specifiers
& TS__BASIC
)) {
15 if(b
->type_specifiers
& TS__BASIC
)
16 inputerror(0, "invalid combination of type specifiers");
18 basic
= b
->type_specifiers
& TS__BASIC
;
20 /* can only have one _Complex or _Imaginary */
21 if((ci
= a
->type_specifiers
& TS__CI
)) {
22 if(b
->type_specifiers
& TS__CI
)
23 inputerror(0, "invalid combination of complex type specifiers");
25 ci
= b
->type_specifiers
& TS__CI
;
27 /* _Complex and _Imaginary can't be used with basic type specifiers other
28 * than float or double */
36 inputerror(0, "invalid use of complex type specifier");
37 ci
= 0; /* don't repeat errors */
41 /* can only have one length specifer except that 'long long' is permitted */
42 if((length
= a
->type_specifiers
& TS__LENGTH
)) {
43 switch(b
->type_specifiers
& TS__LENGTH
) {
51 inputerror(0, "invalid use of type length specifiers");
52 length
= 0; /* don't repeat errors */
57 length
= b
->type_specifiers
& TS__LENGTH
;
59 /* length specifiers can only be used with int, or long with double */
66 if(length
!= TS_LONG
) {
67 inputerror(0, "invalid floating type length specifier");
68 length
= 0; /* don't repeat errors */
74 /* can only have one sign specifier */
75 if((sign
= a
->type_specifiers
& TS__SIGN
)) {
76 if(b
->type_specifiers
& TS__SIGN
)
77 inputerror(0, "invalid combination of type signedness specifier");
79 sign
= b
->type_specifiers
& TS__SIGN
;
81 /* only int and char can be signed or unsigned */
89 inputerror(0, "invalid use of type signedness specifier");
90 sign
= 0; /* don't repeat errors */
95 /* C99 6.7.1 only one storage class specifier per declaration */
96 if(a
->storage_class_specifiers
&& b
->storage_class_specifiers
)
97 inputerror(0, "at most one storage class specifier is allowed in a declaration");
99 /* C99 6.7.3, 6.7.4 duplicate type qualifiers and function speciciers are
102 /* if basic didn't come from a it must have come from b, in which case we
103 * copy the relevant extra fields and zap the irrelevant ones */
104 if(basic
!= (a
->type_specifiers
& TS__BASIC
)) {
106 a
->structure
= b
->structure
;
107 a
->enumerators
= b
->enumerators
;
109 definition
= b
->type_specifiers
& TS_DEFINITION
;
111 definition
= a
->type_specifiers
& TS_DEFINITION
;
113 /* put it all together */
114 a
->type_specifiers
= basic
| ci
| length
| sign
| definition
;
115 a
->storage_class_specifiers
|= b
->storage_class_specifiers
;
116 a
->type_qualifiers
|= b
->type_qualifiers
;
117 a
->function_specifiers
|= b
->function_specifiers
;
122 /* return true if D has integral type. typedefs should have been resolved. */
123 int is_integral_type(const struct declarator
*d
) {
124 /* pointers, functions, arrays aren't integral */
125 if(d
->declarator_type
)
127 /* check the basic type */
128 switch(d
->declaration_specifiers
->type_specifiers
& TS__BASIC
) {
140 /* return true if D has arithmetic type. typedefs should have been
142 int is_arithmetic_type(const struct declarator
*d
) {
143 /* pointers, functions, arrays aren't integral */
144 if(d
->declarator_type
)
146 /* check the basic type */
147 switch(d
->declaration_specifiers
->type_specifiers
& TS__BASIC
) {
161 /* return true if D has scalar type. typedefs should have been resolved. */
162 int is_scalar_type(const struct declarator
*d
) {
163 /* functions, arrays aren't integral */
164 if(d
->declarator_type
)
165 return d
->declarator_type
->type
== dt_pointer
;
166 /* check the basic type */
167 switch(d
->declaration_specifiers
->type_specifiers
& TS__BASIC
) {
181 /* return true if D has pointer type. typedefs should have been resolved. */
182 int is_pointer_type(const struct declarator
*d
) {
183 return d
->declarator_type
&& d
->declarator_type
->type
== dt_pointer
;
186 /* resolve typedefs in value types */
187 struct declarator
*resolve_typedefs(struct declarator
*d
) {
188 struct declarator_type
*dt
, *dt_head
, **dt_end
, *du
;
189 struct declaration_specifiers
*ds
= d
->declaration_specifiers
, *nds
;
190 struct declarator
*nd
;
198 /* We must duplicate our declarator_type chain, hang the typedef's
199 * declarator_type chain off the end, and use the typedef's declarator
200 * instead of ours, and do this as many times as necessary (hence the
203 * Naturally we don't want to duplicate ever lengthening versions of our
204 * declarator chain if there are multiple typdefs to resolve so we should
205 * go through the whole thing in a single go building up the new
206 * declarator_type chain as we go.
209 /* first we duplicate our own declarator_type chain. These values mean
210 * {decorations} of <base type> where <base type> is the typedef name. */
212 du
= d
->declarator_type
;
221 /* now for each typedef name we encounter we append its declarator_type
224 /* this is the set of qualifiers to add to typedef's type */
225 tq
= ds
->type_qualifiers
;
228 /* The set of type qualifiers that are being added to the typedef should
229 * be applied to the head of the typedef's bit of the declarator_type
230 * chain: we are asking for (e.g.) a 'const foo'; if foo is an 'int *'
231 * then it is the * that is const, not the int
233 * If it doesn't have one then we'll trickle them down to the next type.
235 du
= ds
->type
->declarator_type
;
239 dt
->type_qualifiers
|= tq
;
240 tq
= 0; /* no more type qualifiers */
245 ds
= ds
->type
->declaration_specifiers
;
246 tq
|= ds
->type_qualifiers
;
248 /* we end when we hit a built-in type or some kind of weird anomalous
249 * typedef without a definition */
251 assert(dt_end
== &dt_head
|| *dt_end
== 0);
254 /* if there are any left over type qualifiers we might need a new
255 * declaration_specifiers */
256 if((ds
->type_qualifiers
| tq
) != ds
->type_qualifiers
) {
259 nds
->type_qualifiers
|= tq
;
262 /* make up a new declarator with the right declarator_type chain and
263 * declaration_specifiers */
266 nd
->declarator_type
= dt_head
;
267 nd
->declaration_specifiers
= ds
;
275 /* return (as a declarator) the target type of an array or whatever. Usually
276 * only makes sense after typedefs have been resolved. */
277 struct declarator
*target_type(struct declarator
*d
) {
278 struct declarator
*nd
;
282 nd
->declarator_type
= d
->declarator_type
->next
;
286 size_t width(unsigned long ts
) {
287 switch(ts
& ~TS__SIGN
) {
288 case TS_CHAR
: return 1;
289 case TS_BOOL
: return P_BOOL_SIZE
;
290 case TS_INT
|TS_SHORT
: case TS_SHORT
: return P_SHORT_SIZE
;
291 case TS_INT
: case 0: return P_INT_SIZE
;
292 case TS_INT
|TS_LONG
: case TS_LONG
: return P_LONG_SIZE
;
293 case TS_INT
|TS_LONGLONG
: case TS_LONGLONG
: return P_LLONG_SIZE
;