Skip to content

Commit

Permalink
noTypeInfo pragma to workaround C++ Atomic [backport]
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Sep 30, 2024
1 parent b0e6d28 commit 8bc37f8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ type
tfCompleteStruct
# (for importc types); type is fully specified, allowing to compute
# sizeof, alignof, offsetof at CT
tfNoTypeInfo
# do not generate typeinfo for imported struct
# workaround for Atomic[T] on C++
tfExplicitCallConv
tfIsConstructor
tfEffectSystemWorkaround
Expand Down
3 changes: 2 additions & 1 deletion compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,8 @@ proc genObjectInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo
typeToString(typ))
genTypeInfoAux(m, typ, origType, name, info)
var tmp = getNimNode(m)
if (not isImportedType(typ)) or tfCompleteStruct in typ.flags:
if (not isImportedType(typ)) or
{tfCompleteStruct, tfNoTypeInfo} * typ.flags == {tfCompleteStruct}:
genObjectFields(m, typ, origType, typ.n, tmp, info)
m.s[cfsTypeInit3].addf("$1.node = &$2;$n", [tiNameForHcr(m, name), tmp])
var t = typ.baseClass
Expand Down
1 change: 1 addition & 0 deletions compiler/condsyms.nim
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,4 @@ proc initDefines*(symbols: StringTableRef) =
defineSymbol("nimHasGenericsOpenSym2")
defineSymbol("nimHasGenericsOpenSym3")
defineSymbol("nimHasJsNoLambdaLifting")
defineSymbol("nimHasNoTypeInfoPragma")
6 changes: 5 additions & 1 deletion compiler/pragmas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const
wGcSafe, wCodegenDecl, wNoInit, wCompileTime}
typePragmas* = declPragmas + {wMagic, wAcyclic,
wPure, wHeader, wCompilerProc, wCore, wFinal, wSize, wShallow,
wIncompleteStruct, wCompleteStruct, wByCopy, wByRef,
wIncompleteStruct, wCompleteStruct, wNoTypeInfo, wByCopy, wByRef,
wInheritable, wGensym, wInject, wRequiresInit, wUnchecked, wUnion, wPacked,
wCppNonPod, wBorrow, wGcSafe, wPartial, wExplain, wPackage, wCodegenDecl,
wSendable, wNoInit}
Expand Down Expand Up @@ -1216,6 +1216,10 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int,
noVal(c, it)
if sym.typ == nil: invalidPragma(c, it)
else: incl(sym.typ.flags, tfCompleteStruct)
of wNoTypeInfo:
noVal(c, it)
if sym.typ == nil or sfImportc notin sym.flags: invalidPragma(c, it)
else: incl(sym.typ.flags, tfNoTypeInfo)
of wUnchecked:
noVal(c, it)
if sym.typ == nil or sym.typ.kind notin {tyArray, tyUncheckedArray}:
Expand Down
3 changes: 2 additions & 1 deletion compiler/wordrecg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ type
wImportc = "importc", wImportJs = "importjs", wExportc = "exportc", wExportCpp = "exportcpp",
wExportNims = "exportnims",
wIncompleteStruct = "incompleteStruct", # deprecated
wCompleteStruct = "completeStruct", wRequiresInit = "requiresInit", wAlign = "align",
wCompleteStruct = "completeStruct", wNoTypeInfo = "notypeinfo",
wRequiresInit = "requiresInit", wAlign = "align",
wNodecl = "nodecl", wPure = "pure", wSideEffect = "sideEffect", wHeader = "header",
wNoSideEffect = "noSideEffect", wGcSafe = "gcsafe", wNoreturn = "noreturn",
wNosinks = "nosinks", wLib = "lib", wDynlib = "dynlib",
Expand Down
5 changes: 4 additions & 1 deletion lib/pure/concurrency/atomics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ when defined(cpp) or defined(nimdoc):
## Also guarantees that all threads observe the same total ordering
## with other moSequentiallyConsistent operations.

when not defined(nimHasNoTypeInfoPragma):
{.pragma: noTypeInfo.}

type
Atomic*[T] {.importcpp: "std::atomic", completeStruct.} = object
Atomic*[T] {.importcpp: "std::atomic", completeStruct, noTypeInfo.} = object
## An atomic object with underlying type `T`.
raw: T

Expand Down
14 changes: 14 additions & 0 deletions tests/ccgbugs/tatomictypeinfo.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
discard """
matrix: "--mm:refc; --mm:orc"
targets: "c cpp"
"""

# issue #24159

import std/atomics

type N = object
u: ptr Atomic[int]
proc w(args: N) = discard
var e: Thread[N]
createThread(e, w, default(N))
1 change: 1 addition & 0 deletions tests/ccgbugs/tcgbug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ success
M1 M2
ok
'''
targets: "c cpp"
matrix: "--mm:refc;--mm:orc"
"""

Expand Down

0 comments on commit 8bc37f8

Please sign in to comment.