Skip to content

Commit

Permalink
fix #8405: -d:useNimRtl does not prevent using procs at CT
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jul 3, 2019
1 parent c522a45 commit 6e453cf
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 54 deletions.
11 changes: 6 additions & 5 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
" operator has to be enabled with {.experimental: \"callOperator\".}")

if n.sons[bodyPos].kind != nkEmpty and sfError notin s.flags:
# for DLL generation it is annoying to check for sfImportc!
# for DLL generation we allow sfImportc to have a body, for use in VM
if sfBorrow in s.flags:
localError(c.config, n.sons[bodyPos].info, errImplOfXNotAllowed % s.name.s)
let usePseudoGenerics = kind in {skMacro, skTemplate}
Expand All @@ -1881,8 +1881,8 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,

c.p.wasForwarded = proto != nil
maybeAddResult(c, s, n)
if lfDynamicLib notin s.loc.flags:
# no semantic checking for importc:
if lfDynamicLib notin s.loc.flags or n.sons[bodyPos] != nil:
# semantic checking for importc needed in case used in VM
s.ast[bodyPos] = hloBody(c, semProcBody(c, n.sons[bodyPos]))
# unfortunately we cannot skip this step when in 'system.compiles'
# context as it may even be evaluated in 'system.compiles':
Expand All @@ -1899,8 +1899,9 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
fixupInstantiatedSymbols(c, s)
if s.kind == skMethod: semMethodPrototype(c, s, n)
if sfImportc in s.flags:
# so we just ignore the body after semantic checking for importc:
n.sons[bodyPos] = c.graph.emptyNode
# don't ignore the body in case used in VM
# n.sons[bodyPos] = c.graph.emptyNode
discard
popProcCon(c)
else:
if s.kind == skMethod: semMethodPrototype(c, s, n)
Expand Down
5 changes: 4 additions & 1 deletion compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1082,12 +1082,15 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
VmArgs(ra: ra, rb: rb, rc: rc, slots: cast[pointer](regs),
currentException: c.currentExceptionA,
currentLineInfo: c.debug[pc]))
elif sfImportc in prc.flags:
elif shouldImportcSymbol(prc):
if compiletimeFFI notin c.config.features:
globalError(c.config, c.debug[pc], "VM not allowed to do FFI, see `compiletimeFFI`")
# we pass 'tos.slots' instead of 'regs' so that the compiler can keep
# 'regs' in a register:
when hasFFI:
if prc.position - 1 < 0:
globalError(c.config, c.debug[pc],
"VM call invalid: prc.position: " & $prc.position)
let prcValue = c.globals.sons[prc.position-1]
if prcValue.kind == nkEmpty:
globalError(c.config, c.debug[pc], "cannot run " & prc.name.s)
Expand Down
6 changes: 6 additions & 0 deletions compiler/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,12 @@ proc genTypeLit(c: PCtx; t: PType; dest: var TDest) =
proc importcCond(s: PSym): bool {.inline.} =
sfImportc in s.flags and (lfDynamicLib notin s.loc.flags or s.ast == nil)

proc shouldImportcSymbol*(s: PSym): bool =
## return true to importc `s`, false to execute its body instead (refs #8405)
if sfImportc in s.flags:
assert s.kind == skProc
return s.ast.sons[bodyPos].kind == nkEmpty

proc importcSym(c: PCtx; info: TLineInfo; s: PSym) =
when hasFFI:
if compiletimeFFI in c.config.features:
Expand Down
71 changes: 35 additions & 36 deletions lib/pure/parseopt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -366,43 +366,42 @@ proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
inc p.idx
p.pos = 0

when declared(os.paramCount):
proc cmdLineRest*(p: OptParser): TaintedString {.rtl, extern: "npo$1".} =
## Retrieves the rest of the command line that has not been parsed yet.
##
## See also:
## * `remainingArgs proc<#remainingArgs,OptParser>`_
##
## **Examples:**
##
## .. code-block::
## var p = initOptParser("--left -r:2 -- foo.txt bar.txt")
## while true:
## p.next()
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
## break
## else: continue
## doAssert p.cmdLineRest == "foo.txt bar.txt"
result = p.cmds[p.idx .. ^1].quoteShellCommand.TaintedString
proc cmdLineRest*(p: OptParser): TaintedString {.rtl, extern: "npo$1".} =
## Retrieves the rest of the command line that has not been parsed yet.
##
## See also:
## * `remainingArgs proc<#remainingArgs,OptParser>`_
##
## **Examples:**
##
## .. code-block::
## var p = initOptParser("--left -r:2 -- foo.txt bar.txt")
## while true:
## p.next()
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
## break
## else: continue
## doAssert p.cmdLineRest == "foo.txt bar.txt"
result = p.cmds[p.idx .. ^1].quoteShellCommand.TaintedString

proc remainingArgs*(p: OptParser): seq[TaintedString] {.rtl, extern: "npo$1".} =
## Retrieves a sequence of the arguments that have not been parsed yet.
##
## See also:
## * `cmdLineRest proc<#cmdLineRest,OptParser>`_
##
## **Examples:**
##
## .. code-block::
## var p = initOptParser("--left -r:2 -- foo.txt bar.txt")
## while true:
## p.next()
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
## break
## else: continue
## doAssert p.remainingArgs == @["foo.txt", "bar.txt"]
result = @[]
for i in p.idx..<p.cmds.len: result.add TaintedString(p.cmds[i])
proc remainingArgs*(p: OptParser): seq[TaintedString] {.rtl, extern: "npo$1".} =
## Retrieves a sequence of the arguments that have not been parsed yet.
##
## See also:
## * `cmdLineRest proc<#cmdLineRest,OptParser>`_
##
## **Examples:**
##
## .. code-block::
## var p = initOptParser("--left -r:2 -- foo.txt bar.txt")
## while true:
## p.next()
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
## break
## else: continue
## doAssert p.remainingArgs == @["foo.txt", "bar.txt"]
result = @[]
for i in p.idx..<p.cmds.len: result.add TaintedString(p.cmds[i])

iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedString] =
## Convenience iterator for iterating over the given
Expand Down
25 changes: 13 additions & 12 deletions lib/system/repr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,19 @@ when not defined(useNimRtl):
add result, "(invalid data!)"
inc(cl.recdepth)

proc reprOpenArray(p: pointer, length: int, elemtyp: PNimType): string {.
compilerRtl.} =
var
cl: ReprClosure
initReprClosure(cl)
result = "["
var bs = elemtyp.size
for i in 0..length - 1:
if i > 0: add result, ", "
reprAux(result, cast[pointer](cast[ByteAddress](p) + i*bs), elemtyp, cl)
add result, "]"
deinitReprClosure(cl)
when not defined(useNimRtl):
proc reprOpenArray(p: pointer, length: int, elemtyp: PNimType): string {.
compilerRtl.} =
var
cl: ReprClosure
initReprClosure(cl)
result = "["
var bs = elemtyp.size
for i in 0..length - 1:
if i > 0: add result, ", "
reprAux(result, cast[pointer](cast[ByteAddress](p) + i*bs), elemtyp, cl)
add result, "]"
deinitReprClosure(cl)

when not defined(useNimRtl):
proc reprAny(p: pointer, typ: PNimType): string =
Expand Down

0 comments on commit 6e453cf

Please sign in to comment.