summary refs log tree commit diff stats
path: root/tests/template
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2018-12-08 13:30:06 +0100
committerAraq <rumpf_a@web.de>2018-12-08 13:30:06 +0100
commite8747059710370819800bdc0742fa86761c6737b (patch)
tree7cba169ecf4990667ff15d4828403b820f222f53 /tests/template
parent7a0191ac7ea2c3adfa303cfd6b1d1759a361e5c7 (diff)
downloadNim-e8747059710370819800bdc0742fa86761c6737b.tar.gz
fixes nested gensym'ed parameters; fixes #9476
Diffstat (limited to 'tests/template')
-rw-r--r--tests/template/tparams_gensymed.nim40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/template/tparams_gensymed.nim b/tests/template/tparams_gensymed.nim
index da86d63dc..91fa26596 100644
--- a/tests/template/tparams_gensymed.nim
+++ b/tests/template/tparams_gensymed.nim
@@ -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)