Added support for {.packed.} pragma on objects

Added tests for packed and union pragmas
This commit is contained in:
Audun Wilhelmsen
2014-03-18 21:11:57 +01:00
parent 261a6ca017
commit 1e45bb79ab
8 changed files with 96 additions and 13 deletions

View File

@@ -450,6 +450,7 @@ type
tfHasMeta, # type contains "wildcard" sub-types such as generic params
# or other type classes
tfHasGCedMem, # type contains GC'ed memory
tfPacked
tfHasStatic
tfGenericTypeParam
tfImplicitTypeParam

View File

@@ -464,24 +464,33 @@ proc getRecordDesc(m: BModule, typ: PType, name: PRope,
check: var TIntSet): PRope =
# declare the record:
var hasField = false
let aStruct = structOrUnion(typ)
var attribute: PRope =
if tfPacked in typ.flags: toRope(CC[ccompiler].packedPragma)
else: nil
result = ropecg(m, CC[ccompiler].structStmtFmt,
[structOrUnion(typ), name, attribute])
if typ.kind == tyObject:
if typ.sons[0] == nil:
if (typ.sym != nil and sfPure in typ.sym.flags) or tfFinal in typ.flags:
result = ropecg(m, "$1 $2 {$n", [aStruct, name])
appcg(m, result, " {$n", [])
else:
result = ropecg(m, "$1 $2 {$n#TNimType* m_type;$n", [aStruct, name])
appcg(m, result, " {$n#TNimType* m_type;$n", [name, attribute])
hasField = true
elif gCmd == cmdCompileToCpp:
result = ropecg(m, "$1 $2 : public $3 {$n",
[aStruct, name, getTypeDescAux(m, typ.sons[0], check)])
appcg(m, result, " : public $1 {$n",
[getTypeDescAux(m, typ.sons[0], check)])
hasField = true
else:
result = ropecg(m, "$1 $2 {$n $3 Sup;$n",
[aStruct, name, getTypeDescAux(m, typ.sons[0], check)])
appcg(m, result, " {$n $1 Sup;$n",
[getTypeDescAux(m, typ.sons[0], check)])
hasField = true
else:
result = ropef("$1 $2 {$n", [aStruct, name])
appf(result, " {$n", [name])
var desc = getRecordFields(m, typ, check)
if (desc == nil) and not hasField:
appf(result, "char dummy;$n", [])

View File

@@ -47,6 +47,8 @@ type
pic: string, # command for position independent code
# used on some platforms
asmStmtFrmt: string, # format of ASM statement
structStmtFmt: string, # Format for struct statement
packedPragma: string, # Attribute/pragma to make struct packed (1-byte aligned)
props: TInfoCCProps] # properties of the C compiler
@@ -77,6 +79,8 @@ compiler gcc:
debug: "",
pic: "-fPIC",
asmStmtFrmt: "asm($1);$n",
structStmtFmt: "$1 $3 $2 ", # struct|union [packed] $name
packedPragma: "__attribute__((__packed__))",
props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, hasGnuAsm,
hasNakedAttribute})
@@ -115,6 +119,8 @@ compiler vcc:
debug: " /GZ /Zi ",
pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$3$#1 $2",
packedPragma: "#pragma pack(1)",
props: {hasCpp, hasAssume, hasNakedDeclspec})
compiler icl:
@@ -148,6 +154,8 @@ compiler lcc:
debug: " -g5 ",
pic: "",
asmStmtFrmt: "_asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {})
compiler bcc:
@@ -170,6 +178,8 @@ compiler bcc:
debug: "",
pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {hasCpp})
compiler dmc:
@@ -192,6 +202,8 @@ compiler dmc:
debug: " -g ",
pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$3$n$1 $2",
packedPragma: "#pragma pack(1)",
props: {hasCpp})
compiler wcc:
@@ -214,6 +226,8 @@ compiler wcc:
debug: " -d2 ",
pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {hasCpp})
compiler tcc:
@@ -236,6 +250,8 @@ compiler tcc:
debug: " -g ",
pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {hasSwitchRange, hasComputedGoto})
compiler pcc:
@@ -259,6 +275,8 @@ compiler pcc:
debug: " -Zi ",
pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {})
compiler ucc:
@@ -281,6 +299,8 @@ compiler ucc:
debug: "",
pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {})
const

View File

@@ -52,7 +52,7 @@ const
typePragmas* = {wImportc, wExportc, wDeprecated, wMagic, wAcyclic, wNodecl,
wPure, wHeader, wCompilerproc, wFinal, wSize, wExtern, wShallow,
wImportCpp, wImportObjC, wError, wIncompleteStruct, wByCopy, wByRef,
wInheritable, wGensym, wInject, wRequiresInit, wUnchecked, wUnion}
wInheritable, wGensym, wInject, wRequiresInit, wUnchecked, wUnion, wPacked}
fieldPragmas* = {wImportc, wExportc, wDeprecated, wExtern,
wImportCpp, wImportObjC, wError}
varPragmas* = {wImportc, wExportc, wVolatile, wRegister, wThreadVar, wNodecl,
@@ -655,6 +655,10 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int,
incl(sym.flags, sfThread)
incl(sym.flags, sfProcvar)
if sym.typ != nil: incl(sym.typ.flags, tfThread)
of wPacked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfPacked)
of wHint: message(it.info, hintUser, expectStrLit(c, it))
of wWarning: message(it.info, warnUser, expectStrLit(c, it))
of wError:

View File

@@ -921,7 +921,7 @@ const tyTypeParamsHolders = {tyGenericInst, tyCompositeTypeClass}
proc readTypeParameter(c: PContext, typ: PType,
paramName: PIdent, info: TLineInfo): PNode =
let ty = if typ.kind == tyGenericInst: typ.skipGenericAlias
else: (internalAssert typ.kind == tyCompositeTypeClass; typ.sons[1])
else: (internalAssert(typ.kind == tyCompositeTypeClass); typ.sons[1])
let tbody = ty.sons[0]
for s in countup(0, tbody.len-2):

View File

@@ -204,7 +204,7 @@ proc nMinusOne(n: PNode): PNode =
n])
proc makeRangeWithStaticExpr(c: PContext, n: PNode): PType =
let intType = getSysType tyInt
let intType = getSysType(tyInt)
result = newTypeS(tyRange, c)
result.sons = @[intType]
result.n = newNode(nkRange, n.info, @[

View File

@@ -72,7 +72,7 @@ type
wPrivate, wProtected, wPublic, wRegister, wReinterpret_cast,
wShort, wSigned, wSizeof, wStatic_cast, wStruct, wSwitch,
wThis, wThrow, wTrue, wTypedef, wTypeid, wTypename,
wUnion, wUnsigned, wVirtual, wVoid, wVolatile, wWchar_t,
wUnion, wPacked, wUnsigned, wVirtual, wVoid, wVolatile, wWchar_t,
wAlignas, wAlignof, wConstexpr, wDecltype, wNullptr, wNoexcept,
wThread_local, wStatic_assert, wChar16_t, wChar32_t,
@@ -155,7 +155,7 @@ const
"private", "protected", "public", "register", "reinterpret_cast",
"short", "signed", "sizeof", "static_cast", "struct", "switch",
"this", "throw", "true", "typedef", "typeid",
"typename", "union", "unsigned", "virtual", "void", "volatile",
"typename", "union", "packed", "unsigned", "virtual", "void", "volatile",
"wchar_t",
"alignas", "alignof", "constexpr", "decltype", "nullptr", "noexcept",

View File

@@ -0,0 +1,49 @@
discard """
file: "tobjpragma.nim"
output: '''2
3
9
257
1
2
3'''
"""
# Test
type
Foo {.packed.} = object
a: int8
b: int8
Bar {.packed.} = object
a: int8
b: int16
Daz {.packed.} = object
a: int32
b: int8
c: int32
var f = Foo(a: 1, b: 1)
var b: Bar
var d: Daz
echo sizeof(f)
echo sizeof(b)
echo sizeof(d)
echo (cast[ptr int16](f.addr)[])
type
Union {.union.} = object
a: int8
b: int8
var u: Union
u.a = 1
echo u.b
u.a = 2
echo u.b
u.b = 3
echo u.a