refactor sizealignoffset (#12077)

* small refactoring
* refactor computeObjectOffsetFoldFunction with AccumObject
* refactor packed object offstes fold function
* refactor compute union object offsets fold function
* merge normal/packed object offset fold function
* compiletime offsetof in c++ inheritance objects
* enable c++ inheritance offsetof tests
* correct alignment for big sets/enums on weird 32bit platforms
* uncomputedSize -> unknownSize
* workaround for travis
* fixes win32 alignment problems
This commit is contained in:
Andreas Rumpf 2019-08-28 19:36:29 +02:00 • committed by GitHub
commit 21fc8b4d4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 198 additions and 230 deletions

View file

@ -1,4 +1,5 @@
discard """
targets: "c cpp"
output: '''
body executed
body executed
@ -7,6 +8,14 @@ macros api OK
'''
"""
# This is for travis. The keyword ``alignof`` only exists in ``c++11``
# and newer. On travis gcc does not default to c++11 yet.
when defined(cpp) and not defined(windows):
{.passC: "-std=c++11".}
# Object offsets are different for inheritance objects when compiling
# to c++.
type
TMyEnum = enum
tmOne, tmTwo, tmThree, tmFour
@ -143,6 +152,14 @@ type
ValueA
ValueB
# Must have more than 32 elements so that set[MyEnum33] will become compile to an int64.
MyEnum33 {.pure.} = enum
Value1, Value2, Value3, Value4, Value5, Value6,
Value7, Value8, Value9, Value10, Value11, Value12,
Value13, Value14, Value15, Value16, Value17, Value18,
Value19, Value20, Value21, Value22, Value23, Value24,
Value25, Value26, Value27, Value28, Value29, Value30,
Value31, Value32, Value33
proc transformObjectconfigPacked(arg: NimNode): NimNode =
let debug = arg.kind == nnkPragmaExpr
@ -296,6 +313,10 @@ testinstance:
b: int8
c: int8
PaddingOfSetEnum33 = object
cause: int8
theSet: set[MyEnum33]
Bazing {.objectconfig.} = object of RootObj
a: int64
# TODO test on 32 bit system
@ -328,6 +349,7 @@ testinstance:
var g : RecursiveStuff
var ro : RootObj
var go : GenericObject[int64]
var po : PaddingOfSetEnum33
var
e1: Enum1
@ -346,16 +368,16 @@ testinstance:
else:
doAssert sizeof(SimpleAlignment) > 10
testSizeAlignOf(t,a,b,c,d,e,f,g,ro,go, e1, e2, e4, e8, eoa, eob)
testSizeAlignOf(t,a,b,c,d,e,f,g,ro,go,po, e1, e2, e4, e8, eoa, eob)
when not defined(cpp):
type
WithBitsize {.objectconfig.} = object
bitfieldA {.bitsize: 16.}: uint32
bitfieldB {.bitsize: 16.}: uint32
var wbs: WithBitsize
testSize(wbs)
type
WithBitsize {.objectconfig.} = object
bitfieldA {.bitsize: 16.}: uint32
bitfieldB {.bitsize: 16.}: uint32
var wbs: WithBitsize
testSize(wbs)
testOffsetOf(TrivialType, x)
testOffsetOf(TrivialType, y)
@ -383,11 +405,13 @@ testinstance:
testOffsetOf(Foobar, c)
when not defined(cpp):
testOffsetOf(Bazing, a)
testOffsetOf(InheritanceA, a)
testOffsetOf(InheritanceB, b)
testOffsetOf(InheritanceC, c)
testOffsetOf(PaddingOfSetEnum33, cause)
testOffsetOf(PaddingOfSetEnum33, theSet)
testOffsetOf(Bazing, a)
testOffsetOf(InheritanceA, a)
testOffsetOf(InheritanceB, b)
testOffsetOf(InheritanceC, c)
testOffsetOf(EnumObjectA, a)
testOffsetOf(EnumObjectA, b)
@ -619,9 +643,6 @@ doAssert offsetof(MyPackedCaseObject, val3) == 13
doAssert offsetof(MyPackedCaseObject, val4) == 9
doAssert offsetof(MyPackedCaseObject, val5) == 13
reject:
const off4 = offsetof(MyPackedCaseObject, val1)
reject:
const off5 = offsetof(MyPackedCaseObject, val2)