Skip to content

Instantly share code, notes, and snippets.

@9rnsr
Created February 1, 2013 11:38
Show Gist options
  • Save 9rnsr/4690822 to your computer and use it in GitHub Desktop.
Save 9rnsr/4690822 to your computer and use it in GitHub Desktop.
A patch to allow "nested union".
diff --git a/src/struct.c b/src/struct.c
index f6fd857..3a6fe5f 100644
--- a/src/struct.c
+++ b/src/struct.c
@@ -684,9 +684,19 @@ void StructDeclaration::finalizeSize(Scope *sc)
// Set the offsets of the fields and determine the size of the struct
unsigned offset = 0;
bool isunion = isUnionDeclaration() != NULL;
+ unsigned maxofs = 0;
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = (*members)[i];
- s->setFieldOffset(this, &offset, isunion);
+ //printf("%s [%d] offset = %d, u = %d\n", toChars(), i, offset, maxofs);
+ if (isunion)
+ {
+ if (i == members->dim - 1 && s->isThisDeclaration())
+ offset = maxofs;
+ else
+ offset = 0;
+ }
+ s->setFieldOffset(this, &offset, false);
+ if (maxofs < offset) maxofs = offset;
}
if (sizeok == SIZEOKfwd)
return;
void main()
{
union U {
int x;
float y;
void foo() {}
}
pragma(msg, U.sizeof); // prints 12 in 32bit system
pragma(msg, U.tupleof[0].offsetof); // prints 0
pragma(msg, U.tupleof[1].offsetof); // prints 0
pragma(msg, U.tupleof[2].offsetof); // prints 8
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment