ref: 67dd2d72eacab11d263530d495d0019168ea9d6e
parent: da40be16dc3adcabf313f467b6ac91bca512038b
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Sep 24 18:22:45 EDT 2013
Add bit fields Bit fields are small integers with a varible size of bits. C99 defines that bit fields can be of type int or type bool.
--- a/decl.c
+++ b/decl.c
@@ -122,6 +122,18 @@
do {
declarator(base, ns);
tp = decl_type(base);
+ if (accept(':')) {
+ expect(CONSTANT);
+ switch (tp->type) {
+ case INT: case BOOL:
+ tp = btype(NULL, BITFLD);
+ tp->len = yyval.sym->val;
+ break;
+ default:
+ error("bit-field '%s' has invalid type",
+ cursym->name);
+ }
+ }
(cursym->ctype = tp)->refcnt++;
} while (accept(','));
--- a/tokens.h
+++ b/tokens.h
@@ -11,7 +11,7 @@
/* types */
INT = 1, CHAR, FLOAT, LONG, LLONG, SHORT, VOID, DOUBLE,
LDOUBLE, STRUCT, UNION, ENUM, BOOL, ARY, PTR, FTN,
- COMPLEX, IMAGINARY,
+ COMPLEX, IMAGINARY, BITFLD,
/* storage specifier */
TYPEDEF, EXTERN, STATIC, AUTO, REGISTER,
/* type qualifier */
--- a/types.c
+++ b/types.c
@@ -93,7 +93,7 @@
type = tp->type;
switch (tok) {
- case VOID: case BOOL: case STRUCT: case UNION: case ENUM:
+ case VOID: case BOOL: case STRUCT: case UNION: case ENUM: case BITFLD:
if (type)
goto two_or_more;;
type = tok;