[hackers] [scc] [cc1] Add basic cupport for bit fields || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Fri, 9 Dec 2016 19:25:36 +0100 (CET)

commit f46933b11d1f0f91c0898fd180facd09b1899301
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Fri Dec 9 18:54:29 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
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
_AT_@ -609,6 +609,7 @@ type(struct decl *dcl)
         return sym;
 }
 
+#define NAME(s) (*(s)->name ? (s)->name : "<anonymous>")
 static Symbol *
 field(struct decl *dcl)
 {
_AT_@ -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;
_AT_@ -649,6 +672,7 @@ field(struct decl *dcl)
 
         return sym;
 }
+#undef NAME
 
 static void
 bad_storage(Type *tp, char *name)
Received on Fri Dec 09 2016 - 19:25:36 CET

This archive was generated by hypermail 2.3.0 : Fri Dec 09 2016 - 19:36:22 CET