Skip to content

Commit

Permalink
allow packed union (#14868)
Browse files Browse the repository at this point in the history
Co-authored-by: Arne Döring <[email protected]>
  • Loading branch information
Clyybber and krux02 authored Jul 1, 2020
1 parent 1edb9a6 commit 1440e70
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
16 changes: 7 additions & 9 deletions compiler/sizealignoffsetimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ proc computeObjectOffsetsFoldFunction(conf: ConfigRef; n: PNode, packed: bool, a
accum.maxAlign = szUnknownSize
accum.offset = szUnknownSize

proc computeUnionObjectOffsetsFoldFunction(conf: ConfigRef; n: PNode; accum: var OffsetAccum) =
proc computeUnionObjectOffsetsFoldFunction(conf: ConfigRef; n: PNode; packed: bool; accum: var OffsetAccum) =
## ``accum.offset`` will the offset from the larget member of the union.
case n.kind
of nkRecCase:
Expand All @@ -175,15 +175,15 @@ proc computeUnionObjectOffsetsFoldFunction(conf: ConfigRef; n: PNode; accum: var
let accumRoot = accum # copy, because each branch should start af the same offset
for i, child in n.sons:
var branchAccum = accumRoot
computeUnionObjectOffsetsFoldFunction(conf, child, branchAccum)
computeUnionObjectOffsetsFoldFunction(conf, child, packed, branchAccum)
accum.mergeBranch(branchAccum)
of nkSym:
var size = szUnknownSize
var align = szUnknownSize
if n.sym.bitsize == 0: # 0 represents bitsize not set
computeSizeAlign(conf, n.sym.typ)
size = n.sym.typ.size.int
align = n.sym.typ.align.int
align = if packed: 1 else: n.sym.typ.align.int
accum.align(align)
if n.sym.alignment > 0:
accum.align(n.sym.alignment)
Expand Down Expand Up @@ -365,16 +365,14 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) =
else:
OffsetAccum(maxAlign: 1)
if tfUnion in typ.flags:
if tfPacked in typ.flags:
let info = if typ.sym != nil: typ.sym.info else: unknownLineInfo
localError(conf, info, "union type may not be packed.")
accum = OffsetAccum(offset: szUnknownSize, maxAlign: szUnknownSize)
elif accum.offset != 0:
if accum.offset != 0:
let info = if typ.sym != nil: typ.sym.info else: unknownLineInfo
localError(conf, info, "union type may not have an object header")
accum = OffsetAccum(offset: szUnknownSize, maxAlign: szUnknownSize)
elif tfPacked in typ.flags:
computeUnionObjectOffsetsFoldFunction(conf, typ.n, true, accum)
else:
computeUnionObjectOffsetsFoldFunction(conf, typ.n, accum)
computeUnionObjectOffsetsFoldFunction(conf, typ.n, false, accum)
elif tfPacked in typ.flags:
accum.maxAlign = 1
computeObjectOffsetsFoldFunction(conf, typ.n, true, accum)
Expand Down
10 changes: 10 additions & 0 deletions tests/misc/tsizeof.nim
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,13 @@ type

doAssert sizeof(O0) == 1
doAssert sizeof(T0) == 1


type
# this thing may not have padding bytes at the end
PackedUnion* {.union, packed.} = object
a*: array[11, byte]
b*: int64

doAssert sizeof(PackedUnion) == 11
doAssert alignof(PackedUnion) == 1

0 comments on commit 1440e70

Please sign in to comment.