1# 2# 3# The Nim Compiler 4# (c) Copyright 2015 Andreas Rumpf 5# 6# See the file "copying.txt", included in this 7# distribution, for details about the copyright. 8# 9 10## Plugin to transform an inline iterator into a data structure. 11 12import ".." / [ast, modulegraphs, lookups, semdata, lambdalifting, msgs] 13 14proc iterToProcImpl*(c: PContext, n: PNode): PNode = 15 result = newNodeI(nkStmtList, n.info) 16 let iter = n[1] 17 if iter.kind != nkSym or iter.sym.kind != skIterator: 18 localError(c.config, iter.info, "first argument needs to be an iterator") 19 return 20 if n[2].typ.isNil: 21 localError(c.config, n[2].info, "second argument needs to be a type") 22 return 23 if n[3].kind != nkIdent: 24 localError(c.config, n[3].info, "third argument needs to be an identifier") 25 return 26 27 let t = n[2].typ.skipTypes({tyTypeDesc, tyGenericInst}) 28 if t.kind notin {tyRef, tyPtr} or t.lastSon.kind != tyObject: 29 localError(c.config, n[2].info, 30 "type must be a non-generic ref|ptr to object with state field") 31 return 32 let body = liftIterToProc(c.graph, iter.sym, getBody(c.graph, iter.sym), t, c.idgen) 33 34 let prc = newSym(skProc, n[3].ident, nextSymId c.idgen, iter.sym.owner, iter.sym.info) 35 prc.typ = copyType(iter.sym.typ, nextTypeId c.idgen, prc) 36 excl prc.typ.flags, tfCapturesEnv 37 prc.typ.n.add newSymNode(getEnvParam(iter.sym)) 38 prc.typ.rawAddSon t 39 let orig = iter.sym.ast 40 prc.ast = newProcNode(nkProcDef, n.info, 41 body = body, params = orig[paramsPos], name = newSymNode(prc), 42 pattern = c.graph.emptyNode, genericParams = c.graph.emptyNode, 43 pragmas = orig[pragmasPos], exceptions = c.graph.emptyNode) 44 45 prc.ast.add iter.sym.ast[resultPos] 46 addInterfaceDecl(c, prc) 47