lift parameter-less do block to lambdas

This commit is contained in:
Zahary Karadjov 2017-04-10 21:28:11 +03:00
commit a3f19c87fb
10 changed files with 46 additions and 26 deletions

View file

@ -452,6 +452,7 @@ type
nfExprCall # this is an attempt to call a regular expression
nfIsRef # this node is a 'ref' node; used for the VM
nfPreventCg # this node should be ignored by the codegen
nfBlockArg # this a stmtlist appearing in a call (e.g. a do block)
TNodeFlags* = set[TNodeFlag]
TTypeFlag* = enum # keep below 32 for efficiency reasons (now: 30)

View file

@ -90,11 +90,7 @@ proc evalTemplateArgs(n: PNode, s: PSym; fromHlo: bool): PNode =
result = newNodeI(nkArgList, n.info)
for i in 1 .. givenRegularParams:
let p = n[i]
if p != nil and p.kind == nkDo and s.typ.sons[i].kind in {tyStmt, tyExpr}:
result.addSon p[bodyPos]
else:
result.addSon p
result.addSon n[i]
# handle parameters with default values, which were
# not supplied by the user

View file

@ -1170,13 +1170,11 @@ proc postExprBlocks(p: var TParser, x: PNode): PNode =
skipComment(p, result)
if p.tok.tokType notin {tkOf, tkElif, tkElse, tkExcept}:
var stmtList = newNodeP(nkStmtList, p)
let body = parseStmt(p)
stmtList.add body
if stmtList.len == 1 and stmtList[0].kind == nkStmtList:
# to keep backwards compatibility (see tests/vm/tstringnil)
stmtList = stmtList[0]
stmtList.add parseStmt(p)
# to keep backwards compatibility (see tests/vm/tstringnil)
if stmtList[0].kind == nkStmtList: stmtList = stmtList[0]
stmtList.flags.incl nfBlockArg
if openingParams.kind != nkEmpty:
result.add newProcNode(nkDo, stmtList.info, stmtList,
params = openingParams, pragmas = openingPragmas)
@ -1209,7 +1207,10 @@ proc postExprBlocks(p: var TParser, x: PNode): PNode =
else: break
eat(p, tkColon)
nextBlock.addSon parseStmt(p)
nextBlock.flags.incl nfBlockArg
result.add nextBlock
if nextBlock.kind == nkElse: break
else:
if openingParams.kind != nkEmpty:

View file

@ -1652,7 +1652,11 @@ proc semStmtList(c: PContext, n: PNode, flags: TExprFlags): PNode =
else: discard
if result.len == 1 and
c.inTypeClass == 0 and # concept bodies should be preserved as a stmt list
# concept bodies should be preserved as a stmt list:
c.inTypeClass == 0 and
# also, don't make life complicated for macros.
# they will always expect a proper stmtlist:
nfBlockArg notin n.flags and
result.sons[0].kind != nkDefer:
result = result.sons[0]

View file

@ -1259,7 +1259,6 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
of tyGenericInvocation:
var x = a.skipGenericAlias
# XXX: This is very hacky. It should be moved back into liftTypeParam
if x.kind == tyGenericInst and c.calleeSym != nil and c.calleeSym.kind == skProc:
let inst = prepareMetatypeForSigmatch(c.c, c.bindings, c.call.info, f)
@ -1639,6 +1638,10 @@ proc incMatches(m: var TCandidate; r: TTypeRelation; convMatch = 1) =
of isEqual: inc(m.exactMatches)
of isNone: discard
template matchesVoidProc(t: PType): bool =
(t.kind == tyProc and t.len == 1 and t.sons[0] == nil) or
(t.kind == tyBuiltInTypeClass and t.sons[0].kind == tyProc)
proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
argSemantized, argOrig: PNode): PNode =
var
@ -1775,6 +1778,14 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
inc(m.genericMatches)
m.fauxMatch = a.kind
return arg
elif a.kind == tyVoid and f.matchesVoidProc and argOrig.kind == nkStmtList:
# lift do blocks without params to lambdas
let lifted = c.semExpr(c, newProcNode(nkDo, argOrig.info, argOrig), {})
if f.kind == tyBuiltInTypeClass:
inc m.genericMatches
put(m, f, lifted.typ)
inc m.convMatches
return implicitConv(nkHiddenStdConv, f, lifted, m, c)
result = userConvMatch(c, m, f, a, arg)
# check for a base type match, which supports varargs[T] without []
# constructor in a call:

View file

@ -1599,8 +1599,7 @@ proc setupMacroParam(x: PNode, typ: PType): TFullReg =
putIntoReg(result, x)
else:
result.kind = rkNode
var n = if typ.kind in {tyStmt,tyExpr} and x.kind == nkDo: x[bodyPos]
else: x
var n = x
if n.kind in {nkHiddenSubConv, nkHiddenStdConv}: n = n.sons[1]
n = n.canonValue
n.flags.incl nfIsRef