make tsizeof run on C++ backend (#9734)

This commit is contained in:
Arne Döring 2018-11-18 14:36:20 +01:00 • committed by Andreas Rumpf
commit 5dfcc4e91c
2 changed files with 53 additions and 41 deletions

View file

@ -318,6 +318,14 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) =
var headerAlign: int16 var headerAlign: int16
if typ.sons[0] != nil: if typ.sons[0] != nil:
# compute header size # compute header size
if conf.cmd == cmdCompileToCpp:
# if the target is C++ the members of this type are written
# into the padding byets at the end of the parent type. At the
# moment it is not supported to calculate that.
headerSize = szUnknownSize
headerAlign = szUncomputedSize
else:
var st = typ.sons[0] var st = typ.sons[0]
while st.kind in skipPtrs: while st.kind in skipPtrs:
st = st.sons[^1] st = st.sons[^1]

View file

@ -48,7 +48,7 @@ macro testOffsetOf(a,b1,b2: untyped): untyped =
c_offset = c_offsetof(`a`,`b1`) c_offset = c_offsetof(`a`,`b1`)
nim_offset = offsetof(`a`,`b2`) nim_offset = offsetof(`a`,`b2`)
if c_offset != nim_offset: if c_offset != nim_offset:
echo `typeName`, ".", `member`, " offset: ", c_offset, " != ", nim_offset echo `typeName`, ".", `member`, " offsetError, C: ", c_offset, " nim: ", nim_offset
template testOffsetOf(a,b: untyped): untyped = template testOffsetOf(a,b: untyped): untyped =
testOffsetOf(a,b,b) testOffsetOf(a,b,b)
@ -101,21 +101,6 @@ macro testAlign(arg:untyped):untyped =
if cAlign != nimAlign: if cAlign != nimAlign:
echo `prefix`, cAlign, " != ", nimAlign echo `prefix`, cAlign, " != ", nimAlign
testAlign(pointer)
testAlign(int)
testAlign(uint)
testAlign(int8)
testAlign(int16)
testAlign(int32)
testAlign(int64)
testAlign(uint8)
testAlign(uint16)
testAlign(uint32)
testAlign(uint64)
testAlign(float)
testAlign(float32)
testAlign(float64)
type type
MyEnum {.pure.} = enum MyEnum {.pure.} = enum
ValueA ValueA
@ -142,14 +127,6 @@ type
ValueA ValueA
ValueB ValueB
testAlign(MyEnum)
testAlign(OtherEnum)
testAlign(Enum1)
testAlign(Enum2)
testAlign(Enum4)
testAlign(Enum8)
template testinstance(body: untyped): untyped = template testinstance(body: untyped): untyped =
block: block:
{.pragma: objectconfig.} {.pragma: objectconfig.}
@ -159,6 +136,32 @@ template testinstance(body: untyped): untyped =
{.pragma: objectconfig, packed.} {.pragma: objectconfig, packed.}
body body
proc testPrimitiveTypes(): void =
testAlign(pointer)
testAlign(int)
testAlign(uint)
testAlign(int8)
testAlign(int16)
testAlign(int32)
testAlign(int64)
testAlign(uint8)
testAlign(uint16)
testAlign(uint32)
testAlign(uint64)
testAlign(float)
testAlign(float32)
testAlign(float64)
testAlign(MyEnum)
testAlign(OtherEnum)
testAlign(Enum1)
testAlign(Enum2)
testAlign(Enum4)
testAlign(Enum8)
testPrimitiveTypes()
testinstance: testinstance:
type type
@ -274,8 +277,6 @@ testinstance:
const trivialSize = sizeof(TrivialType) # needs to be able to evaluate at compile time const trivialSize = sizeof(TrivialType) # needs to be able to evaluate at compile time
testAlign(SimpleAlignment)
proc main(): void = proc main(): void =
var t : TrivialType var t : TrivialType
var a : SimpleAlignment var a : SimpleAlignment
@ -286,6 +287,7 @@ testinstance:
var f : PaddingAfterBranch var f : PaddingAfterBranch
var g : RecursiveStuff var g : RecursiveStuff
var ro : RootObj var ro : RootObj
var var
e1: Enum1 e1: Enum1
e2: Enum2 e2: Enum2
@ -295,6 +297,8 @@ testinstance:
eoa: EnumObjectA eoa: EnumObjectA
eob: EnumObjectB eob: EnumObjectB
testAlign(SimpleAlignment)
testSizeAlignOf(t,a,b,c,d,e,f,g,ro, e1, e2, e4, e8, eoa, eob) testSizeAlignOf(t,a,b,c,d,e,f,g,ro, e1, e2, e4, e8, eoa, eob)
testOffsetOf(TrivialType, x) testOffsetOf(TrivialType, x)
@ -322,8 +326,8 @@ testinstance:
testOffsetOf(Foobar, c) testOffsetOf(Foobar, c)
when not defined(cpp):
testOffsetOf(Bazing, a) testOffsetOf(Bazing, a)
testOffsetOf(InheritanceA, a) testOffsetOf(InheritanceA, a)
testOffsetOf(InheritanceB, b) testOffsetOf(InheritanceB, b)
testOffsetOf(InheritanceC, c) testOffsetOf(InheritanceC, c)