itersgen.nim 1.7 KB

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