lambdalifting.nim 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. # This file implements lambda lifting for the transformator.
  10. import
  11. intsets, strutils, options, ast, astalgo, trees, treetab, msgs,
  12. idents, renderer, types, magicsys, lowerings, tables, modulegraphs, lineinfos
  13. discard """
  14. The basic approach is that captured vars need to be put on the heap and
  15. that the calling chain needs to be explicitly modelled. Things to consider:
  16. proc a =
  17. var v = 0
  18. proc b =
  19. var w = 2
  20. for x in 0..3:
  21. proc c = capture v, w, x
  22. c()
  23. b()
  24. for x in 0..4:
  25. proc d = capture x
  26. d()
  27. Needs to be translated into:
  28. proc a =
  29. var cl: *
  30. new cl
  31. cl.v = 0
  32. proc b(cl) =
  33. var bcl: *
  34. new bcl
  35. bcl.w = 2
  36. bcl.up = cl
  37. for x in 0..3:
  38. var bcl2: *
  39. new bcl2
  40. bcl2.up = bcl
  41. bcl2.up2 = cl
  42. bcl2.x = x
  43. proc c(cl) = capture cl.up2.v, cl.up.w, cl.x
  44. c(bcl2)
  45. c(bcl)
  46. b(cl)
  47. for x in 0..4:
  48. var acl2: *
  49. new acl2
  50. acl2.x = x
  51. proc d(cl) = capture cl.x
  52. d(acl2)
  53. Closures as interfaces:
  54. proc outer: T =
  55. var captureMe: TObject # value type required for efficiency
  56. proc getter(): int = result = captureMe.x
  57. proc setter(x: int) = captureMe.x = x
  58. result = (getter, setter)
  59. Is translated to:
  60. proc outer: T =
  61. var cl: *
  62. new cl
  63. proc getter(cl): int = result = cl.captureMe.x
  64. proc setter(cl: *, x: int) = cl.captureMe.x = x
  65. result = ((cl, getter), (cl, setter))
  66. For 'byref' capture, the outer proc needs to access the captured var through
  67. the indirection too. For 'bycopy' capture, the outer proc accesses the var
  68. not through the indirection.
  69. Possible optimizations:
  70. 1) If the closure contains a single 'ref' and this
  71. reference is not re-assigned (check ``sfAddrTaken`` flag) make this the
  72. closure. This is an important optimization if closures are used as
  73. interfaces.
  74. 2) If the closure does not escape, put it onto the stack, not on the heap.
  75. 3) Dataflow analysis would help to eliminate the 'up' indirections.
  76. 4) If the captured var is not actually used in the outer proc (common?),
  77. put it into an inner proc.
  78. """
  79. # Important things to keep in mind:
  80. # * Don't base the analysis on nkProcDef et al. This doesn't work for
  81. # instantiated (formerly generic) procs. The analysis has to look at nkSym.
  82. # This also means we need to prevent the same proc is processed multiple
  83. # times via the 'processed' set.
  84. # * Keep in mind that the owner of some temporaries used to be unreliable.
  85. # * For closure iterators we merge the "real" potential closure with the
  86. # local storage requirements for efficiency. This means closure iterators
  87. # have slightly different semantics from ordinary closures.
  88. # ---------------- essential helpers -------------------------------------
  89. const
  90. upName* = ":up" # field name for the 'up' reference
  91. paramName* = ":envP"
  92. envName* = ":env"
  93. proc newCall(a: PSym, b: PNode): PNode =
  94. result = newNodeI(nkCall, a.info)
  95. result.add newSymNode(a)
  96. result.add b
  97. proc createClosureIterStateType*(g: ModuleGraph; iter: PSym): PType =
  98. var n = newNodeI(nkRange, iter.info)
  99. addSon(n, newIntNode(nkIntLit, -1))
  100. addSon(n, newIntNode(nkIntLit, 0))
  101. result = newType(tyRange, iter)
  102. result.n = n
  103. var intType = nilOrSysInt(g)
  104. if intType.isNil: intType = newType(tyInt, iter)
  105. rawAddSon(result, intType)
  106. proc createStateField(g: ModuleGraph; iter: PSym): PSym =
  107. result = newSym(skField, getIdent(g.cache, ":state"), iter, iter.info)
  108. result.typ = createClosureIterStateType(g, iter)
  109. proc createEnvObj(g: ModuleGraph; owner: PSym; info: TLineInfo): PType =
  110. # YYY meh, just add the state field for every closure for now, it's too
  111. # hard to figure out if it comes from a closure iterator:
  112. result = createObj(g, owner, info, final=false)
  113. rawAddField(result, createStateField(g, owner))
  114. proc getClosureIterResult*(g: ModuleGraph; iter: PSym): PSym =
  115. if resultPos < iter.ast.len:
  116. result = iter.ast.sons[resultPos].sym
  117. else:
  118. # XXX a bit hacky:
  119. result = newSym(skResult, getIdent(g.cache, ":result"), iter, iter.info, {})
  120. result.typ = iter.typ.sons[0]
  121. incl(result.flags, sfUsed)
  122. iter.ast.add newSymNode(result)
  123. proc addHiddenParam(routine: PSym, param: PSym) =
  124. assert param.kind == skParam
  125. var params = routine.ast.sons[paramsPos]
  126. # -1 is correct here as param.position is 0 based but we have at position 0
  127. # some nkEffect node:
  128. param.position = routine.typ.n.len-1
  129. addSon(params, newSymNode(param))
  130. #incl(routine.typ.flags, tfCapturesEnv)
  131. assert sfFromGeneric in param.flags
  132. #echo "produced environment: ", param.id, " for ", routine.id
  133. proc getHiddenParam(g: ModuleGraph; routine: PSym): PSym =
  134. let params = routine.ast.sons[paramsPos]
  135. let hidden = lastSon(params)
  136. if hidden.kind == nkSym and hidden.sym.kind == skParam and hidden.sym.name.s == paramName:
  137. result = hidden.sym
  138. assert sfFromGeneric in result.flags
  139. else:
  140. # writeStackTrace()
  141. localError(g.config, routine.info, "internal error: could not find env param for " & routine.name.s)
  142. result = routine
  143. proc getEnvParam*(routine: PSym): PSym =
  144. let params = routine.ast.sons[paramsPos]
  145. let hidden = lastSon(params)
  146. if hidden.kind == nkSym and hidden.sym.name.s == paramName:
  147. result = hidden.sym
  148. assert sfFromGeneric in result.flags
  149. proc interestingVar(s: PSym): bool {.inline.} =
  150. result = s.kind in {skVar, skLet, skTemp, skForVar, skParam, skResult} and
  151. sfGlobal notin s.flags and
  152. s.typ.kind notin {tyStatic, tyTypeDesc}
  153. proc illegalCapture(s: PSym): bool {.inline.} =
  154. result = skipTypes(s.typ, abstractInst).kind in
  155. {tyVar, tyOpenArray, tyVarargs, tyLent} or
  156. s.kind == skResult
  157. proc isInnerProc(s: PSym): bool =
  158. if s.kind in {skProc, skFunc, skMethod, skConverter, skIterator} and s.magic == mNone:
  159. result = s.skipGenericOwner.kind in routineKinds
  160. proc newAsgnStmt(le, ri: PNode, info: TLineInfo): PNode =
  161. # Bugfix: unfortunately we cannot use 'nkFastAsgn' here as that would
  162. # mean to be able to capture string literals which have no GC header.
  163. # However this can only happen if the capture happens through a parameter,
  164. # which is however the only case when we generate an assignment in the first
  165. # place.
  166. result = newNodeI(nkAsgn, info, 2)
  167. result.sons[0] = le
  168. result.sons[1] = ri
  169. proc makeClosure*(g: ModuleGraph; prc: PSym; env: PNode; info: TLineInfo): PNode =
  170. result = newNodeIT(nkClosure, info, prc.typ)
  171. result.add(newSymNode(prc))
  172. if env == nil:
  173. result.add(newNodeIT(nkNilLit, info, getSysType(g, info, tyNil)))
  174. else:
  175. if env.skipConv.kind == nkClosure:
  176. localError(g.config, info, "internal error: taking closure of closure")
  177. result.add(env)
  178. proc interestingIterVar(s: PSym): bool {.inline.} =
  179. # XXX optimization: Only lift the variable if it lives across
  180. # yield/return boundaries! This can potentially speed up
  181. # closure iterators quite a bit.
  182. result = s.kind in {skResult, skVar, skLet, skTemp, skForVar} and sfGlobal notin s.flags
  183. template isIterator*(owner: PSym): bool =
  184. owner.kind == skIterator and owner.typ.callConv == ccClosure
  185. proc liftingHarmful(conf: ConfigRef; owner: PSym): bool {.inline.} =
  186. ## lambda lifting can be harmful for JS-like code generators.
  187. let isCompileTime = sfCompileTime in owner.flags or owner.kind == skMacro
  188. result = conf.cmd == cmdCompileToJS and not isCompileTime
  189. proc liftIterSym*(g: ModuleGraph; n: PNode; owner: PSym): PNode =
  190. # transforms (iter) to (let env = newClosure[iter](); (iter, env))
  191. if liftingHarmful(g.config, owner): return n
  192. let iter = n.sym
  193. assert iter.isIterator
  194. result = newNodeIT(nkStmtListExpr, n.info, n.typ)
  195. let hp = getHiddenParam(g, iter)
  196. var env: PNode
  197. if owner.isIterator:
  198. let it = getHiddenParam(g, owner)
  199. addUniqueField(it.typ.sons[0], hp, g.cache)
  200. env = indirectAccess(newSymNode(it), hp, hp.info)
  201. else:
  202. let e = newSym(skLet, iter.name, owner, n.info)
  203. e.typ = hp.typ
  204. e.flags = hp.flags
  205. env = newSymNode(e)
  206. var v = newNodeI(nkVarSection, n.info)
  207. addVar(v, env)
  208. result.add(v)
  209. # add 'new' statement:
  210. result.add newCall(getSysSym(g, n.info, "internalNew"), env)
  211. result.add makeClosure(g, iter, env, n.info)
  212. proc freshVarForClosureIter*(g: ModuleGraph; s, owner: PSym): PNode =
  213. let envParam = getHiddenParam(g, owner)
  214. let obj = envParam.typ.lastSon
  215. addField(obj, s, g.cache)
  216. var access = newSymNode(envParam)
  217. assert obj.kind == tyObject
  218. let field = getFieldFromObj(obj, s)
  219. if field != nil:
  220. result = rawIndirectAccess(access, field, s.info)
  221. else:
  222. localError(g.config, s.info, "internal error: cannot generate fresh variable")
  223. result = access
  224. # ------------------ new stuff -------------------------------------------
  225. proc markAsClosure(g: ModuleGraph; owner: PSym; n: PNode) =
  226. let s = n.sym
  227. if illegalCapture(s):
  228. localError(g.config, n.info,
  229. ("'$1' is of type <$2> which cannot be captured as it would violate memory" &
  230. " safety, declared here: $3") % [s.name.s, typeToString(s.typ), g.config$s.info])
  231. elif owner.typ.callConv notin {ccClosure, ccDefault}:
  232. localError(g.config, n.info, "illegal capture '$1' because '$2' has the calling convention: <$3>" %
  233. [s.name.s, owner.name.s, CallingConvToStr[owner.typ.callConv]])
  234. incl(owner.typ.flags, tfCapturesEnv)
  235. owner.typ.callConv = ccClosure
  236. type
  237. DetectionPass = object
  238. processed, capturedVars: IntSet
  239. ownerToType: Table[int, PType]
  240. somethingToDo: bool
  241. graph: ModuleGraph
  242. proc initDetectionPass(g: ModuleGraph; fn: PSym): DetectionPass =
  243. result.processed = initIntSet()
  244. result.capturedVars = initIntSet()
  245. result.ownerToType = initTable[int, PType]()
  246. result.processed.incl(fn.id)
  247. result.graph = g
  248. discard """
  249. proc outer =
  250. var a, b: int
  251. proc innerA = use(a)
  252. proc innerB = use(b); innerA()
  253. # --> innerA and innerB need to *share* the closure type!
  254. This is why need to store the 'ownerToType' table and use it
  255. during .closure'fication.
  256. """
  257. proc getEnvTypeForOwner(c: var DetectionPass; owner: PSym;
  258. info: TLineInfo): PType =
  259. result = c.ownerToType.getOrDefault(owner.id)
  260. if result.isNil:
  261. result = newType(tyRef, owner)
  262. let obj = createEnvObj(c.graph, owner, info)
  263. rawAddSon(result, obj)
  264. c.ownerToType[owner.id] = result
  265. proc createUpField(c: var DetectionPass; dest, dep: PSym; info: TLineInfo) =
  266. let refObj = c.getEnvTypeForOwner(dest, info) # getHiddenParam(dest).typ
  267. let obj = refObj.lastSon
  268. let fieldType = c.getEnvTypeForOwner(dep, info) #getHiddenParam(dep).typ
  269. if refObj == fieldType:
  270. localError(c.graph.config, dep.info, "internal error: invalid up reference computed")
  271. let upIdent = getIdent(c.graph.cache, upName)
  272. let upField = lookupInRecord(obj.n, upIdent)
  273. if upField != nil:
  274. if upField.typ != fieldType:
  275. localError(c.graph.config, dep.info, "internal error: up references do not agree")
  276. else:
  277. let result = newSym(skField, upIdent, obj.owner, obj.owner.info)
  278. result.typ = fieldType
  279. rawAddField(obj, result)
  280. discard """
  281. There are a couple of possibilities of how to implement closure
  282. iterators that capture outer variables in a traditional sense
  283. (aka closure closure iterators).
  284. 1. Transform iter() to iter(state, capturedEnv). So use 2 hidden
  285. parameters.
  286. 2. Add the captured vars directly to 'state'.
  287. 3. Make capturedEnv an up-reference of 'state'.
  288. We do (3) here because (2) is obviously wrong and (1) is wrong too.
  289. Consider:
  290. proc outer =
  291. var xx = 9
  292. iterator foo() =
  293. var someState = 3
  294. proc bar = echo someState
  295. proc baz = someState = 0
  296. baz()
  297. bar()
  298. """
  299. proc addClosureParam(c: var DetectionPass; fn: PSym; info: TLineInfo) =
  300. var cp = getEnvParam(fn)
  301. let owner = if fn.kind == skIterator: fn else: fn.skipGenericOwner
  302. let t = c.getEnvTypeForOwner(owner, info)
  303. if cp == nil:
  304. cp = newSym(skParam, getIdent(c.graph.cache, paramName), fn, fn.info)
  305. incl(cp.flags, sfFromGeneric)
  306. cp.typ = t
  307. addHiddenParam(fn, cp)
  308. elif cp.typ != t and fn.kind != skIterator:
  309. localError(c.graph.config, fn.info, "internal error: inconsistent environment type")
  310. #echo "adding closure to ", fn.name.s
  311. proc detectCapturedVars(n: PNode; owner: PSym; c: var DetectionPass) =
  312. case n.kind
  313. of nkSym:
  314. let s = n.sym
  315. if s.kind in {skProc, skFunc, skMethod, skConverter, skIterator} and
  316. s.typ != nil and s.typ.callConv == ccClosure:
  317. # this handles the case that the inner proc was declared as
  318. # .closure but does not actually capture anything:
  319. addClosureParam(c, s, n.info)
  320. c.somethingToDo = true
  321. let innerProc = isInnerProc(s)
  322. if innerProc:
  323. if s.isIterator: c.somethingToDo = true
  324. if not c.processed.containsOrIncl(s.id):
  325. detectCapturedVars(s.getBody, s, c)
  326. let ow = s.skipGenericOwner
  327. if ow == owner:
  328. if owner.isIterator:
  329. c.somethingToDo = true
  330. addClosureParam(c, owner, n.info)
  331. if interestingIterVar(s):
  332. if not c.capturedVars.containsOrIncl(s.id):
  333. let obj = getHiddenParam(c.graph, owner).typ.lastSon
  334. #let obj = c.getEnvTypeForOwner(s.owner).lastSon
  335. if s.name.id == getIdent(c.graph.cache, ":state").id:
  336. obj.n[0].sym.id = -s.id
  337. else:
  338. addField(obj, s, c.graph.cache)
  339. # but always return because the rest of the proc is only relevant when
  340. # ow != owner:
  341. return
  342. # direct or indirect dependency:
  343. if (innerProc and s.typ.callConv == ccClosure) or interestingVar(s):
  344. discard """
  345. proc outer() =
  346. var x: int
  347. proc inner() =
  348. proc innerInner() =
  349. echo x
  350. innerInner()
  351. inner()
  352. # inner() takes a closure too!
  353. """
  354. # mark 'owner' as taking a closure:
  355. c.somethingToDo = true
  356. markAsClosure(c.graph, owner, n)
  357. addClosureParam(c, owner, n.info)
  358. #echo "capturing ", n.info
  359. # variable 's' is actually captured:
  360. if interestingVar(s) and not c.capturedVars.containsOrIncl(s.id):
  361. let obj = c.getEnvTypeForOwner(ow, n.info).lastSon
  362. #getHiddenParam(owner).typ.lastSon
  363. addField(obj, s, c.graph.cache)
  364. # create required upFields:
  365. var w = owner.skipGenericOwner
  366. if isInnerProc(w) or owner.isIterator:
  367. if owner.isIterator: w = owner
  368. let last = if ow.isIterator: ow.skipGenericOwner else: ow
  369. while w != nil and w.kind != skModule and last != w:
  370. discard """
  371. proc outer =
  372. var a, b: int
  373. proc outerB =
  374. proc innerA = use(a)
  375. proc innerB = use(b); innerA()
  376. # --> make outerB of calling convention .closure and
  377. # give it the same env type that outer's env var gets:
  378. """
  379. let up = w.skipGenericOwner
  380. #echo "up for ", w.name.s, " up ", up.name.s
  381. markAsClosure(c.graph, w, n)
  382. addClosureParam(c, w, n.info) # , ow
  383. createUpField(c, w, up, n.info)
  384. w = up
  385. of nkEmpty..pred(nkSym), succ(nkSym)..nkNilLit,
  386. nkTemplateDef, nkTypeSection:
  387. discard
  388. of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef:
  389. discard
  390. of nkLambdaKinds, nkIteratorDef, nkFuncDef:
  391. if n.typ != nil:
  392. detectCapturedVars(n[namePos], owner, c)
  393. of nkReturnStmt:
  394. if n[0].kind in {nkAsgn, nkFastAsgn}:
  395. detectCapturedVars(n[0].sons[1], owner, c)
  396. else: assert n[0].kind == nkEmpty
  397. else:
  398. for i in 0..<n.len:
  399. detectCapturedVars(n[i], owner, c)
  400. type
  401. LiftingPass = object
  402. processed: IntSet
  403. envVars: Table[int, PNode]
  404. inContainer: int
  405. proc initLiftingPass(fn: PSym): LiftingPass =
  406. result.processed = initIntSet()
  407. result.processed.incl(fn.id)
  408. result.envVars = initTable[int, PNode]()
  409. proc accessViaEnvParam(g: ModuleGraph; n: PNode; owner: PSym): PNode =
  410. let s = n.sym
  411. # Type based expression construction for simplicity:
  412. let envParam = getHiddenParam(g, owner)
  413. if not envParam.isNil:
  414. var access = newSymNode(envParam)
  415. while true:
  416. let obj = access.typ.sons[0]
  417. assert obj.kind == tyObject
  418. let field = getFieldFromObj(obj, s)
  419. if field != nil:
  420. return rawIndirectAccess(access, field, n.info)
  421. let upField = lookupInRecord(obj.n, getIdent(g.cache, upName))
  422. if upField == nil: break
  423. access = rawIndirectAccess(access, upField, n.info)
  424. localError(g.config, n.info, "internal error: environment misses: " & s.name.s)
  425. result = n
  426. proc newEnvVar(cache: IdentCache; owner: PSym; typ: PType): PNode =
  427. var v = newSym(skVar, getIdent(cache, envName), owner, owner.info)
  428. incl(v.flags, sfShadowed)
  429. v.typ = typ
  430. result = newSymNode(v)
  431. when false:
  432. if owner.kind == skIterator and owner.typ.callConv == ccClosure:
  433. let it = getHiddenParam(owner)
  434. addUniqueField(it.typ.sons[0], v)
  435. result = indirectAccess(newSymNode(it), v, v.info)
  436. else:
  437. result = newSymNode(v)
  438. proc setupEnvVar(owner: PSym; d: DetectionPass;
  439. c: var LiftingPass): PNode =
  440. if owner.isIterator:
  441. return getHiddenParam(d.graph, owner).newSymNode
  442. result = c.envvars.getOrDefault(owner.id)
  443. if result.isNil:
  444. let envVarType = d.ownerToType.getOrDefault(owner.id)
  445. if envVarType.isNil:
  446. localError d.graph.config, owner.info, "internal error: could not determine closure type"
  447. result = newEnvVar(d.graph.cache, owner, envVarType)
  448. c.envVars[owner.id] = result
  449. proc getUpViaParam(g: ModuleGraph; owner: PSym): PNode =
  450. let p = getHiddenParam(g, owner)
  451. result = p.newSymNode
  452. if owner.isIterator:
  453. let upField = lookupInRecord(p.typ.lastSon.n, getIdent(g.cache, upName))
  454. if upField == nil:
  455. localError(g.config, owner.info, "could not find up reference for closure iter")
  456. else:
  457. result = rawIndirectAccess(result, upField, p.info)
  458. proc rawClosureCreation(owner: PSym;
  459. d: DetectionPass; c: var LiftingPass): PNode =
  460. result = newNodeI(nkStmtList, owner.info)
  461. var env: PNode
  462. if owner.isIterator:
  463. env = getHiddenParam(d.graph, owner).newSymNode
  464. else:
  465. env = setupEnvVar(owner, d, c)
  466. if env.kind == nkSym:
  467. var v = newNodeI(nkVarSection, env.info)
  468. addVar(v, env)
  469. result.add(v)
  470. # add 'new' statement:
  471. result.add(newCall(getSysSym(d.graph, env.info, "internalNew"), env))
  472. # add assignment statements for captured parameters:
  473. for i in 1..<owner.typ.n.len:
  474. let local = owner.typ.n[i].sym
  475. if local.id in d.capturedVars:
  476. let fieldAccess = indirectAccess(env, local, env.info)
  477. # add ``env.param = param``
  478. result.add(newAsgnStmt(fieldAccess, newSymNode(local), env.info))
  479. let upField = lookupInRecord(env.typ.lastSon.n, getIdent(d.graph.cache, upName))
  480. if upField != nil:
  481. let up = getUpViaParam(d.graph, owner)
  482. if up != nil and upField.typ == up.typ:
  483. result.add(newAsgnStmt(rawIndirectAccess(env, upField, env.info),
  484. up, env.info))
  485. #elif oldenv != nil and oldenv.typ == upField.typ:
  486. # result.add(newAsgnStmt(rawIndirectAccess(env, upField, env.info),
  487. # oldenv, env.info))
  488. else:
  489. localError(d.graph.config, env.info, "internal error: cannot create up reference")
  490. proc closureCreationForIter(iter: PNode;
  491. d: DetectionPass; c: var LiftingPass): PNode =
  492. result = newNodeIT(nkStmtListExpr, iter.info, iter.sym.typ)
  493. let owner = iter.sym.skipGenericOwner
  494. var v = newSym(skVar, getIdent(d.graph.cache, envName), owner, iter.info)
  495. incl(v.flags, sfShadowed)
  496. v.typ = getHiddenParam(d.graph, iter.sym).typ
  497. var vnode: PNode
  498. if owner.isIterator:
  499. let it = getHiddenParam(d.graph, owner)
  500. addUniqueField(it.typ.sons[0], v, d.graph.cache)
  501. vnode = indirectAccess(newSymNode(it), v, v.info)
  502. else:
  503. vnode = v.newSymNode
  504. var vs = newNodeI(nkVarSection, iter.info)
  505. addVar(vs, vnode)
  506. result.add(vs)
  507. result.add(newCall(getSysSym(d.graph, iter.info, "internalNew"), vnode))
  508. let upField = lookupInRecord(v.typ.lastSon.n, getIdent(d.graph.cache, upName))
  509. if upField != nil:
  510. let u = setupEnvVar(owner, d, c)
  511. if u.typ == upField.typ:
  512. result.add(newAsgnStmt(rawIndirectAccess(vnode, upField, iter.info),
  513. u, iter.info))
  514. else:
  515. localError(d.graph.config, iter.info, "internal error: cannot create up reference for iter")
  516. result.add makeClosure(d.graph, iter.sym, vnode, iter.info)
  517. proc accessViaEnvVar(n: PNode; owner: PSym; d: DetectionPass;
  518. c: var LiftingPass): PNode =
  519. let access = setupEnvVar(owner, d, c)
  520. let obj = access.typ.sons[0]
  521. let field = getFieldFromObj(obj, n.sym)
  522. if field != nil:
  523. result = rawIndirectAccess(access, field, n.info)
  524. else:
  525. localError(d.graph.config, n.info, "internal error: not part of closure object type")
  526. result = n
  527. proc getStateField*(g: ModuleGraph; owner: PSym): PSym =
  528. getHiddenParam(g, owner).typ.sons[0].n.sons[0].sym
  529. proc liftCapturedVars(n: PNode; owner: PSym; d: DetectionPass;
  530. c: var LiftingPass): PNode
  531. proc symToClosure(n: PNode; owner: PSym; d: DetectionPass;
  532. c: var LiftingPass): PNode =
  533. let s = n.sym
  534. if s == owner:
  535. # recursive calls go through (lambda, hiddenParam):
  536. let available = getHiddenParam(d.graph, owner)
  537. result = makeClosure(d.graph, s, available.newSymNode, n.info)
  538. elif s.isIterator:
  539. result = closureCreationForIter(n, d, c)
  540. elif s.skipGenericOwner == owner:
  541. # direct dependency, so use the outer's env variable:
  542. result = makeClosure(d.graph, s, setupEnvVar(owner, d, c), n.info)
  543. else:
  544. let available = getHiddenParam(d.graph, owner)
  545. let wanted = getHiddenParam(d.graph, s).typ
  546. # ugh: call through some other inner proc;
  547. var access = newSymNode(available)
  548. while true:
  549. if access.typ == wanted:
  550. return makeClosure(d.graph, s, access, n.info)
  551. let obj = access.typ.sons[0]
  552. let upField = lookupInRecord(obj.n, getIdent(d.graph.cache, upName))
  553. if upField == nil:
  554. localError(d.graph.config, n.info, "internal error: no environment found")
  555. return n
  556. access = rawIndirectAccess(access, upField, n.info)
  557. proc liftCapturedVars(n: PNode; owner: PSym; d: DetectionPass;
  558. c: var LiftingPass): PNode =
  559. result = n
  560. case n.kind
  561. of nkSym:
  562. let s = n.sym
  563. if isInnerProc(s):
  564. if not c.processed.containsOrIncl(s.id):
  565. #if s.name.s == "temp":
  566. # echo renderTree(s.getBody, {renderIds})
  567. let oldInContainer = c.inContainer
  568. c.inContainer = 0
  569. var body = liftCapturedVars(s.getBody, s, d, c)
  570. if c.envvars.getOrDefault(s.id).isNil:
  571. s.ast.sons[bodyPos] = body
  572. else:
  573. s.ast.sons[bodyPos] = newTree(nkStmtList, rawClosureCreation(s, d, c), body)
  574. c.inContainer = oldInContainer
  575. if s.typ.callConv == ccClosure:
  576. result = symToClosure(n, owner, d, c)
  577. elif s.id in d.capturedVars:
  578. if s.owner != owner:
  579. result = accessViaEnvParam(d.graph, n, owner)
  580. elif owner.isIterator and interestingIterVar(s):
  581. result = accessViaEnvParam(d.graph, n, owner)
  582. else:
  583. result = accessViaEnvVar(n, owner, d, c)
  584. of nkEmpty..pred(nkSym), succ(nkSym)..nkNilLit, nkComesFrom,
  585. nkTemplateDef, nkTypeSection:
  586. discard
  587. of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef:
  588. discard
  589. of nkClosure:
  590. if n[1].kind == nkNilLit:
  591. n.sons[0] = liftCapturedVars(n[0], owner, d, c)
  592. let x = n.sons[0].skipConv
  593. if x.kind == nkClosure:
  594. #localError(n.info, "internal error: closure to closure created")
  595. # now we know better, so patch it:
  596. n.sons[0] = x.sons[0]
  597. n.sons[1] = x.sons[1]
  598. of nkLambdaKinds, nkIteratorDef, nkFuncDef:
  599. if n.typ != nil and n[namePos].kind == nkSym:
  600. let oldInContainer = c.inContainer
  601. c.inContainer = 0
  602. let m = newSymNode(n[namePos].sym)
  603. m.typ = n.typ
  604. result = liftCapturedVars(m, owner, d, c)
  605. c.inContainer = oldInContainer
  606. of nkHiddenStdConv:
  607. if n.len == 2:
  608. n.sons[1] = liftCapturedVars(n[1], owner, d, c)
  609. if n[1].kind == nkClosure: result = n[1]
  610. of nkReturnStmt:
  611. if n[0].kind in {nkAsgn, nkFastAsgn}:
  612. # we have a `result = result` expression produced by the closure
  613. # transform, let's not touch the LHS in order to make the lifting pass
  614. # correct when `result` is lifted
  615. n[0].sons[1] = liftCapturedVars(n[0].sons[1], owner, d, c)
  616. else: assert n[0].kind == nkEmpty
  617. else:
  618. if owner.isIterator:
  619. if nfLL in n.flags:
  620. # special case 'when nimVm' due to bug #3636:
  621. n.sons[1] = liftCapturedVars(n[1], owner, d, c)
  622. return
  623. let inContainer = n.kind in {nkObjConstr, nkBracket}
  624. if inContainer: inc c.inContainer
  625. for i in 0..<n.len:
  626. n.sons[i] = liftCapturedVars(n[i], owner, d, c)
  627. if inContainer: dec c.inContainer
  628. # ------------------ old stuff -------------------------------------------
  629. proc semCaptureSym*(s, owner: PSym) =
  630. if interestingVar(s) and s.kind != skResult:
  631. if owner.typ != nil and not isGenericRoutine(owner):
  632. # XXX: is this really safe?
  633. # if we capture a var from another generic routine,
  634. # it won't be consider captured.
  635. var o = owner.skipGenericOwner
  636. while o.kind != skModule and o != nil:
  637. if s.owner == o:
  638. if owner.typ.callConv in {ccClosure, ccDefault} or owner.kind == skIterator:
  639. owner.typ.callConv = ccClosure
  640. else:
  641. discard "do not produce an error here, but later"
  642. #echo "computing .closure for ", owner.name.s, " ", owner.info, " because of ", s.name.s
  643. o = o.skipGenericOwner
  644. # since the analysis is not entirely correct, we don't set 'tfCapturesEnv'
  645. # here
  646. proc liftIterToProc*(g: ModuleGraph; fn: PSym; body: PNode; ptrType: PType): PNode =
  647. var d = initDetectionPass(g, fn)
  648. var c = initLiftingPass(fn)
  649. # pretend 'fn' is a closure iterator for the analysis:
  650. let oldKind = fn.kind
  651. let oldCC = fn.typ.callConv
  652. fn.kind = skIterator
  653. fn.typ.callConv = ccClosure
  654. d.ownerToType[fn.id] = ptrType
  655. detectCapturedVars(body, fn, d)
  656. result = liftCapturedVars(body, fn, d, c)
  657. fn.kind = oldKind
  658. fn.typ.callConv = oldCC
  659. proc liftLambdas*(g: ModuleGraph; fn: PSym, body: PNode; tooEarly: var bool): PNode =
  660. # XXX gCmd == cmdCompileToJS does not suffice! The compiletime stuff needs
  661. # the transformation even when compiling to JS ...
  662. # However we can do lifting for the stuff which is *only* compiletime.
  663. let isCompileTime = sfCompileTime in fn.flags or fn.kind == skMacro
  664. if body.kind == nkEmpty or (
  665. g.config.cmd == cmdCompileToJS and not isCompileTime) or
  666. fn.skipGenericOwner.kind != skModule:
  667. # ignore forward declaration:
  668. result = body
  669. tooEarly = true
  670. else:
  671. var d = initDetectionPass(g, fn)
  672. detectCapturedVars(body, fn, d)
  673. if not d.somethingToDo and fn.isIterator:
  674. addClosureParam(d, fn, body.info)
  675. d.somethingToDo = true
  676. if d.somethingToDo:
  677. var c = initLiftingPass(fn)
  678. result = liftCapturedVars(body, fn, d, c)
  679. # echo renderTree(result, {renderIds})
  680. if c.envvars.getOrDefault(fn.id) != nil:
  681. result = newTree(nkStmtList, rawClosureCreation(fn, d, c), result)
  682. else:
  683. result = body
  684. #if fn.name.s == "get2":
  685. # echo "had something to do ", d.somethingToDo
  686. # echo renderTree(result, {renderIds})
  687. proc liftLambdasForTopLevel*(module: PSym, body: PNode): PNode =
  688. # XXX implement it properly
  689. result = body
  690. # ------------------- iterator transformation --------------------------------
  691. proc liftForLoop*(g: ModuleGraph; body: PNode; owner: PSym): PNode =
  692. # problem ahead: the iterator could be invoked indirectly, but then
  693. # we don't know what environment to create here:
  694. #
  695. # iterator count(): int =
  696. # yield 0
  697. #
  698. # iterator count2(): int =
  699. # var x = 3
  700. # yield x
  701. # inc x
  702. # yield x
  703. #
  704. # proc invoke(iter: iterator(): int) =
  705. # for x in iter(): echo x
  706. #
  707. # --> When to create the closure? --> for the (count) occurrence!
  708. discard """
  709. for i in foo(): ...
  710. Is transformed to:
  711. cl = createClosure()
  712. while true:
  713. let i = foo(cl)
  714. if (nkBreakState(cl.state)):
  715. break
  716. ...
  717. """
  718. if liftingHarmful(g.config, owner): return body
  719. var L = body.len
  720. if not (body.kind == nkForStmt and body[L-2].kind in nkCallKinds):
  721. localError(g.config, body.info, "ignored invalid for loop")
  722. return body
  723. var call = body[L-2]
  724. result = newNodeI(nkStmtList, body.info)
  725. # static binding?
  726. var env: PSym
  727. let op = call[0]
  728. if op.kind == nkSym and op.sym.isIterator:
  729. # createClosure()
  730. let iter = op.sym
  731. let hp = getHiddenParam(g, iter)
  732. env = newSym(skLet, iter.name, owner, body.info)
  733. env.typ = hp.typ
  734. env.flags = hp.flags
  735. var v = newNodeI(nkVarSection, body.info)
  736. addVar(v, newSymNode(env))
  737. result.add(v)
  738. # add 'new' statement:
  739. result.add(newCall(getSysSym(g, env.info, "internalNew"), env.newSymNode))
  740. elif op.kind == nkStmtListExpr:
  741. let closure = op.lastSon
  742. if closure.kind == nkClosure:
  743. call.sons[0] = closure
  744. for i in 0 .. op.len-2:
  745. result.add op[i]
  746. var loopBody = newNodeI(nkStmtList, body.info, 3)
  747. var whileLoop = newNodeI(nkWhileStmt, body.info, 2)
  748. whileLoop.sons[0] = newIntTypeNode(nkIntLit, 1, getSysType(g, body.info, tyBool))
  749. whileLoop.sons[1] = loopBody
  750. result.add whileLoop
  751. # setup loopBody:
  752. # gather vars in a tuple:
  753. var v2 = newNodeI(nkLetSection, body.info)
  754. var vpart = newNodeI(if L == 3: nkIdentDefs else: nkVarTuple, body.info)
  755. for i in 0 .. L-3:
  756. if body[i].kind == nkSym:
  757. body[i].sym.kind = skLet
  758. addSon(vpart, body[i])
  759. addSon(vpart, newNodeI(nkEmpty, body.info)) # no explicit type
  760. if not env.isNil:
  761. call.sons[0] = makeClosure(g, call.sons[0].sym, env.newSymNode, body.info)
  762. addSon(vpart, call)
  763. addSon(v2, vpart)
  764. loopBody.sons[0] = v2
  765. var bs = newNodeI(nkBreakState, body.info)
  766. bs.addSon(call.sons[0])
  767. let ibs = newNodeI(nkIfStmt, body.info)
  768. let elifBranch = newNodeI(nkElifBranch, body.info)
  769. elifBranch.add(bs)
  770. let br = newNodeI(nkBreakStmt, body.info)
  771. br.add(g.emptyNode)
  772. elifBranch.add(br)
  773. ibs.add(elifBranch)
  774. loopBody.sons[1] = ibs
  775. loopBody.sons[2] = body[L-1]