overloading resolution for getAst; breaking change
This commit is contained in:
parent
242fb3240f
commit
aa4260ffa8
5 changed files with 44 additions and 18 deletions
|
|
@ -758,10 +758,11 @@ macro `=~`(x: PNode, pat: untyped): bool =
|
||||||
of nnkIdent:
|
of nnkIdent:
|
||||||
let c = newTree(nnkStmtListExpr, newLetStmt(pat, x))
|
let c = newTree(nnkStmtListExpr, newLetStmt(pat, x))
|
||||||
conds.add c
|
conds.add c
|
||||||
if ($pat)[^1] == 'c': c.add(getAst(isVal(pat)))
|
# XXX why is this 'isVal(pat)' and not 'isVal(x)'?
|
||||||
|
if ($pat)[^1] == 'c': c.add(getAst(isVal(x)))
|
||||||
else: c.add bindSym"true"
|
else: c.add bindSym"true"
|
||||||
of nnkIntLit:
|
of nnkIntLit:
|
||||||
conds.add(getAst(isIntVal(pat.intVal)))
|
conds.add(getAst(isIntVal(x, pat.intVal)))
|
||||||
else:
|
else:
|
||||||
error("invalid pattern")
|
error("invalid pattern")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1594,17 +1594,23 @@ proc newAnonSym(c: PContext; kind: TSymKind, info: TLineInfo): PSym =
|
||||||
|
|
||||||
proc semExpandToAst(c: PContext, n: PNode): PNode =
|
proc semExpandToAst(c: PContext, n: PNode): PNode =
|
||||||
let macroCall = n[1]
|
let macroCall = n[1]
|
||||||
let expandedSym = expectMacroOrTemplateCall(c, macroCall)
|
|
||||||
if expandedSym.kind == skError: return n
|
|
||||||
|
|
||||||
macroCall.sons[0] = newSymNode(expandedSym, macroCall.info)
|
when false:
|
||||||
markUsed(n.info, expandedSym, c.graph.usageSym)
|
let expandedSym = expectMacroOrTemplateCall(c, macroCall)
|
||||||
styleCheckUse(n.info, expandedSym)
|
if expandedSym.kind == skError: return n
|
||||||
|
|
||||||
for i in countup(1, macroCall.len-1):
|
macroCall.sons[0] = newSymNode(expandedSym, macroCall.info)
|
||||||
#if macroCall.sons[0].typ.sons[i].kind != tyExpr:
|
markUsed(n.info, expandedSym, c.graph.usageSym)
|
||||||
macroCall.sons[i] = semExprWithType(c, macroCall[i], {})
|
styleCheckUse(n.info, expandedSym)
|
||||||
|
|
||||||
|
if isCallExpr(macroCall):
|
||||||
|
for i in countup(1, macroCall.len-1):
|
||||||
|
#if macroCall.sons[0].typ.sons[i].kind != tyExpr:
|
||||||
|
macroCall.sons[i] = semExprWithType(c, macroCall[i], {})
|
||||||
|
# we just perform overloading resolution here:
|
||||||
|
n.sons[1] = semOverloadedCall(c, macroCall, macroCall, {skTemplate, skMacro})
|
||||||
|
else:
|
||||||
|
localError(n.info, "getAst takes a call, but got " & n.renderTree)
|
||||||
# Preserve the magic symbol in order to be handled in evals.nim
|
# Preserve the magic symbol in order to be handled in evals.nim
|
||||||
internalAssert n.sons[0].sym.magic == mExpandToAst
|
internalAssert n.sons[0].sym.magic == mExpandToAst
|
||||||
#n.typ = getSysSym("NimNode").typ # expandedSym.getReturnType
|
#n.typ = getSysSym("NimNode").typ # expandedSym.getReturnType
|
||||||
|
|
|
||||||
|
|
@ -324,8 +324,8 @@ macro check*(conditions: untyped): untyped =
|
||||||
|
|
||||||
case checked.kind
|
case checked.kind
|
||||||
of nnkCallKinds:
|
of nnkCallKinds:
|
||||||
template rewrite(call, lineInfoLit: expr, callLit: string,
|
template rewrite(call, lineInfoLit, callLit,
|
||||||
argAssgs, argPrintOuts: stmt): stmt =
|
argAssgs, argPrintOuts) =
|
||||||
block:
|
block:
|
||||||
argAssgs #all callables (and assignments) are run here
|
argAssgs #all callables (and assignments) are run here
|
||||||
if not call:
|
if not call:
|
||||||
|
|
@ -345,8 +345,8 @@ macro check*(conditions: untyped): untyped =
|
||||||
result.add(newCall(!"check", checked[i]))
|
result.add(newCall(!"check", checked[i]))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
template rewrite(Exp, lineInfoLit: expr, expLit: string): stmt =
|
template rewrite(exp, lineInfoLit, expLit) =
|
||||||
if not Exp:
|
if not exp:
|
||||||
checkpoint(lineInfoLit & ": Check failed: " & expLit)
|
checkpoint(lineInfoLit & ": Check failed: " & expLit)
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
import macros
|
import macros
|
||||||
|
|
||||||
template plus(a, b: expr): expr {.dirty} =
|
template plus(a, b: untyped): untyped {.dirty} =
|
||||||
a + b
|
a + b
|
||||||
|
|
||||||
macro call(e: expr): expr =
|
macro call(e: untyped): untyped =
|
||||||
result = newCall("foo", newStrLitNode("bar"))
|
result = newCall("foo", newStrLitNode("bar"))
|
||||||
|
|
||||||
macro dumpAST(n: stmt): stmt {.immediate.} =
|
macro dumpAST(n: untyped): untyped =
|
||||||
# dump AST as a side-effect and return the inner node
|
# dump AST as a side-effect and return the inner node
|
||||||
let n = callsite()
|
let n = callsite()
|
||||||
echo n.lispRepr
|
echo n.lispRepr
|
||||||
|
|
@ -17,7 +17,7 @@ macro dumpAST(n: stmt): stmt {.immediate.} =
|
||||||
var plusAst = getAst(plus(1, 2))
|
var plusAst = getAst(plus(1, 2))
|
||||||
echo plusAst.lispRepr
|
echo plusAst.lispRepr
|
||||||
|
|
||||||
var callAst = getAst(call())
|
var callAst = getAst(call(4))
|
||||||
echo callAst.lispRepr
|
echo callAst.lispRepr
|
||||||
|
|
||||||
var e = parseExpr("foo(bar + baz)")
|
var e = parseExpr("foo(bar + baz)")
|
||||||
|
|
|
||||||
19
tests/template/twrong_getast.nim
Normal file
19
tests/template/twrong_getast.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
discard """
|
||||||
|
errormsg: "type mismatch"
|
||||||
|
line: 16
|
||||||
|
"""
|
||||||
|
|
||||||
|
import macros
|
||||||
|
|
||||||
|
template grainBlock(proxyTypeName: untyped, proxyProcs: untyped): typed =
|
||||||
|
discard
|
||||||
|
|
||||||
|
var
|
||||||
|
proxyTypeName: string
|
||||||
|
proxyProcs: string
|
||||||
|
|
||||||
|
macro foo(): untyped =
|
||||||
|
let x = getAst grainBlock(proxyTypeName, proxyProcs, proxyTypeName)
|
||||||
|
|
||||||
|
foo()
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue