Skip to content

Commit 684a0a9

Browse files
committed
improve cname handling
1 parent 73e9930 commit 684a0a9

File tree

4 files changed

+6
-8
lines changed

4 files changed

+6
-8
lines changed

compiler/ast.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,9 @@ type
857857
loc*: TLoc
858858
annex*: PLib # additional fields (seldom used, so we use a
859859
# reference to another object to save space)
860-
cname*: string # resolved C declaration name in importc decl, eg:
861-
# {.importc: "foo".} => cname = foo
860+
when hasFFI:
861+
cname*: string # resolved C declaration name in importc decl, eg:
862+
# proc fun() {.importc: "$1aux".} => cname = funaux
862863
constraint*: PNode # additional constraints like 'lit|result'; also
863864
# misused for the codegenDecl pragma in the hope
864865
# it won't cause problems

compiler/cgen.nim

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,6 @@ proc loadDynamicLib(m: BModule, lib: PLib) =
701701
proc mangleDynLibProc(sym: PSym): Rope =
702702
# we have to build this as a single rope in order not to trip the
703703
# optimization in genInfixCall, see test tests/cpp/t8241.nim
704-
if sym.loc.r != nil:
705-
# `return rope($sym.loc.r)` would cause on windows a SIGSEGV in
706-
# stdlib_winleanDatInit000 () at generated_not_to_break_here
707-
if sym.cname.len == 0: sym.cname = $sym.loc.r
708704
if sfCompilerProc in sym.flags:
709705
# NOTE: sym.loc.r is the external name!
710706
result = rope(sym.name.s)

compiler/evalffi.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ const
4747
nkPtrLit = nkIntLit # hopefully we can get rid of this hack soon
4848
4949
proc importcSymbol*(conf: ConfigRef, sym: PSym): PNode =
50-
var name = $sym.cname
51-
if name.len == 0: name = $sym.loc.r
50+
let name = sym.cname # $sym.loc.r would point to internal name
5251
# the AST does not support untyped pointers directly, so we use an nkIntLit
5352
# that contains the address instead:
5453
result = newNodeIT(nkPtrLit, sym.info, sym.typ)

compiler/pragmas.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ proc setExternName(c: PContext; s: PSym, extname: string, info: TLineInfo) =
129129
s.loc.r = rope(extname % s.name.s)
130130
except ValueError:
131131
localError(c.config, info, "invalid extern name: '" & extname & "'. (Forgot to escape '$'?)")
132+
when hasFFI:
133+
s.cname = $s.loc.r
132134
if c.config.cmd == cmdPretty and '$' notin extname:
133135
# note that '{.importc.}' is transformed into '{.importc: "$1".}'
134136
s.loc.flags.incl(lfFullExternalName)

0 commit comments

Comments
 (0)