Merge pull request #9912 from nim-lang/araq-fixes-nested-gensym

fixes nested gensym'ed parameters; fixes #9476
This commit is contained in:
Andreas Rumpf 2018-12-09 12:58:33 +01:00 • committed by GitHub
commit c16963083d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 82 additions and 23 deletions

View file

@ -70,3 +70,43 @@ proc genericProc(x: any) =
concreteProc(7) # This works
genericProc(7) # This doesn't compile
import tables
# bug #9476
proc getTypeInfo*(T: typedesc): pointer =
var dummy: T
getTypeInfo(dummy)
macro implementUnary(op: untyped): untyped =
result = newStmtList()
template defineTable(tableSymbol) =
var tableSymbol = initTable[pointer, pointer]()
let tableSymbol = genSym(nskVar, "registeredProcs")
result.add(getAst(defineTable(tableSymbol)))
template defineRegisterInstantiation(tableSym, regTemplSym, instSym, op) =
template regTemplSym*(T: typedesc) =
let ti = getTypeInfo(T)
proc instSym(xOrig: int): int {.gensym, cdecl.} =
let x {.inject.} = xOrig
op
tableSym[ti] = cast[pointer](instSym)
let regTemplSymbol = ident("registerInstantiation")
let instSymbol = ident("instantiation")
result.add(getAst(defineRegisterInstantiation(
tableSymbol, regTemplSymbol, instSymbol, op
)))
echo result.repr
implementUnary(): x*x
registerInstantiation(int)
registerInstantiation(float)