Offsetof fixes (#11690)

* first fixes

* more tests and fixes

* code normalization
This commit is contained in:
Arne Döring 2019-07-09 09:07:45 +02:00 • committed by Andreas Rumpf
commit 11dad688fe
8 changed files with 241 additions and 81 deletions

View file

@ -375,52 +375,11 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
of mTypeOf:
result = semTypeOf(c, n)
of mSizeOf:
# TODO there is no proper way to find out if a type cannot be queried for the size.
let size = getSize(c.config, n[1].typ)
# We just assume here that the type might come from the c backend
if size == szUnknownSize:
# Forward to the c code generation to emit a `sizeof` in the C code.
result = n
elif size >= 0:
result = newIntNode(nkIntLit, size)
result.info = n.info
result.typ = n.typ
else:
localError(c.config, n.info, "cannot evaluate 'sizeof' because its type is not defined completely, type: " & n[1].typ.typeToString)
result = n
result = foldSizeOf(c.config, n, n)
of mAlignOf:
# this is 100% analog to mSizeOf, could be made more dry.
let align = getAlign(c.config, n[1].typ)
if align == szUnknownSize:
result = n
elif align >= 0:
result = newIntNode(nkIntLit, align)
result.info = n.info
result.typ = n.typ
else:
localError(c.config, n.info, "cannot evaluate 'alignof' because its type is not defined completely, type: " & n[1].typ.typeToString)
result = n
result = foldAlignOf(c.config, n, n)
of mOffsetOf:
var dotExpr: PNode
block findDotExpr:
if n[1].kind == nkDotExpr:
dotExpr = n[1]
elif n[1].kind == nkCheckedFieldExpr:
dotExpr = n[1][0]
else:
illFormedAst(n, c.config)
assert dotExpr != nil
let value = dotExpr[0]
let member = dotExpr[1]
discard computeSize(c.config, value.typ)
result = newIntNode(nkIntLit, member.sym.offset)
result.info = n.info
result.typ = n.typ
result = foldOffsetOf(c.config, n, n)
of mArrGet:
result = semArrGet(c, n, flags)
of mArrPut: