commit 627efdb4af18c26fa5707e247fc276837cfed3db
parent eaf8259846c2b6d77467cddf8fe143c6cce568dd
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 17 Mar 2014 16:43:36 +0100
Unify STRUCT and UNIONS declaration
There are the same thing, and the only difference between them is the
offset of the fields, that in the case of the unions is allways 0
for all the fields of the union.
Diffstat:
2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/decl.c b/decl.c
@@ -252,7 +252,7 @@ fielddcl(uint8_t ns, uint8_t type)
{
struct ctype *tp;
struct symbol *sym;
- short offset = 0;
+ short offset = 0, size;
switch (yytoken) {
case IDEN:
@@ -275,8 +275,11 @@ fielddcl(uint8_t ns, uint8_t type)
do {
sym = declarator(tp, ns, 1);
sym->u.offset = offset;
+ size = sym->type->size;
if (type == STRUCT)
- offset += sym->type->size;
+ offset += size;
+ else if (offset < size)
+ offset = size;
} while (accept(','));
}
@@ -305,7 +308,7 @@ structdcl(uint8_t tag)
} else {
sym = install(NULL, NS_TAG);
}
- sym->type = tp = mktype(NULL, tag, NULL, 0);
+ sym->type = tp = mktype(NULL, STRUCT, NULL, 0);
++namespace;
if (yytoken != ';') {
diff --git a/types.c b/types.c
@@ -162,7 +162,7 @@ mktype(struct ctype *tp, uint8_t op,
t = (op ^ (uint8_t) ((unsigned short) tp >> 3))
& NR_TYPE_HASH-1;
tbl = &typetab[t];
- if (op != FTN || op != STRUCT || op != UNION || op != ENUM) {
+ if (op != FTN || op != STRUCT || op != ENUM) {
for (bp = *tbl; bp; bp = bp->next) {
if (bp->type == tp && bp->op == op &&
bp->sym == sym && bp->nelem == nelem) {
@@ -176,7 +176,7 @@ mktype(struct ctype *tp, uint8_t op,
case FTN: size = 0; break;
case ARY: size = tp->size * nelem; break;
case ENUM: size = INTSIZE;
- case STRUCT: case UNION: size = 0; break;
+ case STRUCT: size = 0; break;
default: size = tp->size; break;
}
bp = xmalloc(sizeof(*bp));