commit f46933b11d1f0f91c0898fd180facd09b1899301
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Fri Dec 9 18:54:29 2016 +0100
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Fri Dec 9 18:54:29 2016 +0100

    [cc1] Add basic cupport for bit fields
    
    This is a first implementation of bitfields which adds the parsing
    of the expression and checks all the errors that can happen in a
    bitfield declaration, but does nothing with the bit field definition,
    so they will act like usual integers (which is allowed by the
    standard).

diff --git a/cc1/decl.c b/cc1/decl.c
index 47aa36a..b314c6f 100644
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -609,6 +609,7 @@ type(struct decl *dcl)
        return sym;
 }
 
+#define NAME(s) (*(s)->name ? (s)->name : "<anonymous>")
 static Symbol *
 field(struct decl *dcl)
 {
@@ -618,8 +619,30 @@ field(struct decl *dcl)
        TINT n = structp->n.elem;
        int err = 0;
 
-       if (empty(sym, tp, 0))
+       /* TODO: print name of the field in the errors */
+       if (accept(':')) {
+               Node *np;
+               TINT n;
+
+               if ((np = iconstexpr()) == NULL) {
+                       unexpected();
+                       n = 0;
+               } else {
+                       n = np->sym->u.i;
+                       freetree(np);
+               }
+               if (n == 0 && *sym->name)
+                       errorp("zero width for bit-field '%s'", sym->name);
+               if (tp != booltype && tp != inttype && tp != uinttype)
+                       errorp("bit-field '%s' has invalid type", NAME(sym));
+               if (n < 0)
+                       errorp("negative width in bit-field '%s'", NAME(sym));
+               else if (n > tp->size*8)
+                       errorp("width of '%s' exceeds its type", NAME(sym));
+       } else if (empty(sym, tp, 0)) {
                return sym;
+       }
+
        if (tp->op == FTN) {
                errorp("invalid type in struct/union");
                err = 1;
@@ -649,6 +672,7 @@ field(struct decl *dcl)
 
        return sym;
 }
+#undef NAME
 
 static void
 bad_storage(Type *tp, char *name)

Reply via email to