transf.nim 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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 module implements the transformator. It transforms the syntax tree
  10. # to ease the work of the code generators. Does some transformations:
  11. #
  12. # * inlines iterators
  13. # * inlines constants
  14. # * performs constant folding
  15. # * converts "continue" to "break"; disambiguates "break"
  16. # * introduces method dispatchers
  17. # * performs lambda lifting for closure support
  18. # * transforms 'defer' into a 'try finally' statement
  19. import std / tables
  20. import
  21. options, ast, astalgo, trees, msgs,
  22. idents, renderer, types, semfold, magicsys, cgmeth,
  23. lowerings, liftlocals,
  24. modulegraphs, lineinfos
  25. when defined(nimPreviewSlimSystem):
  26. import std/assertions
  27. type
  28. TransformFlag* = enum
  29. useCache, keepOpenArrayConversions, force
  30. TransformFlags* = set[TransformFlag]
  31. proc transformBody*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; flags: TransformFlags): PNode
  32. import closureiters, lambdalifting
  33. type
  34. PTransCon = ref object # part of TContext; stackable
  35. mapping: Table[ItemId, PNode] # mapping from symbols to nodes
  36. owner: PSym # current owner
  37. forStmt: PNode # current for stmt
  38. forLoopBody: PNode # transformed for loop body
  39. yieldStmts: int # we count the number of yield statements,
  40. # because we need to introduce new variables
  41. # if we encounter the 2nd yield statement
  42. next: PTransCon # for stacking
  43. PTransf = ref object
  44. module: PSym
  45. transCon: PTransCon # top of a TransCon stack
  46. inlining: int # > 0 if we are in inlining context (copy vars)
  47. contSyms, breakSyms: seq[PSym] # to transform 'continue' and 'break'
  48. deferDetected, tooEarly: bool
  49. isIntroducingNewLocalVars: bool # true if we are in `introducingNewLocalVars` (don't transform yields)
  50. inAddr: bool
  51. flags: TransformFlags
  52. graph: ModuleGraph
  53. idgen: IdGenerator
  54. proc newTransNode(a: PNode): PNode {.inline.} =
  55. result = shallowCopy(a)
  56. proc newTransNode(kind: TNodeKind, info: TLineInfo,
  57. sons: int): PNode {.inline.} =
  58. var x = newNodeI(kind, info)
  59. newSeq(x.sons, sons)
  60. result = x
  61. proc newTransNode(kind: TNodeKind, n: PNode,
  62. sons: int): PNode {.inline.} =
  63. var x = newNodeIT(kind, n.info, n.typ)
  64. newSeq(x.sons, sons)
  65. # x.flags = n.flags
  66. result = x
  67. proc newTransCon(owner: PSym): PTransCon =
  68. assert owner != nil
  69. result = PTransCon(mapping: initTable[ItemId, PNode](), owner: owner)
  70. proc pushTransCon(c: PTransf, t: PTransCon) =
  71. t.next = c.transCon
  72. c.transCon = t
  73. proc popTransCon(c: PTransf) =
  74. if (c.transCon == nil): internalError(c.graph.config, "popTransCon")
  75. c.transCon = c.transCon.next
  76. proc getCurrOwner(c: PTransf): PSym =
  77. if c.transCon != nil: result = c.transCon.owner
  78. else: result = c.module
  79. proc newTemp(c: PTransf, typ: PType, info: TLineInfo): PNode =
  80. let r = newSym(skTemp, getIdent(c.graph.cache, genPrefix), c.idgen, getCurrOwner(c), info)
  81. r.typ = typ #skipTypes(typ, {tyGenericInst, tyAlias, tySink})
  82. incl(r.flags, sfFromGeneric)
  83. let owner = getCurrOwner(c)
  84. if owner.isIterator and not c.tooEarly and not isDefined(c.graph.config, "nimOptIters"):
  85. result = freshVarForClosureIter(c.graph, r, c.idgen, owner)
  86. else:
  87. result = newSymNode(r)
  88. proc transform(c: PTransf, n: PNode): PNode
  89. proc transformSons(c: PTransf, n: PNode): PNode =
  90. result = newTransNode(n)
  91. for i in 0..<n.len:
  92. result[i] = transform(c, n[i])
  93. proc newAsgnStmt(c: PTransf, kind: TNodeKind, le: PNode, ri: PNode; isFirstWrite: bool): PNode =
  94. result = newTransNode(kind, ri.info, 2)
  95. result[0] = le
  96. if isFirstWrite:
  97. le.flags.incl nfFirstWrite
  98. result[1] = ri
  99. proc transformSymAux(c: PTransf, n: PNode): PNode =
  100. let s = n.sym
  101. if s.typ != nil and s.typ.callConv == ccClosure:
  102. if s.kind in routineKinds:
  103. discard transformBody(c.graph, c.idgen, s, {useCache}+c.flags)
  104. if s.kind == skIterator:
  105. if c.tooEarly: return n
  106. else: return liftIterSym(c.graph, n, c.idgen, getCurrOwner(c))
  107. elif s.kind in {skProc, skFunc, skConverter, skMethod} and not c.tooEarly:
  108. # top level .closure procs are still somewhat supported for 'Nake':
  109. return makeClosure(c.graph, c.idgen, s, nil, n.info)
  110. #elif n.sym.kind in {skVar, skLet} and n.sym.typ.callConv == ccClosure:
  111. # echo n.info, " come heer for ", c.tooEarly
  112. # if not c.tooEarly:
  113. var b: PNode
  114. var tc = c.transCon
  115. if sfBorrow in s.flags and s.kind in routineKinds:
  116. # simply exchange the symbol:
  117. var s = s
  118. while true:
  119. # Skips over all borrowed procs getting the last proc symbol without an implementation
  120. let body = getBody(c.graph, s)
  121. if body.kind == nkSym and sfBorrow in body.sym.flags and getBody(c.graph, body.sym).kind == nkSym:
  122. s = body.sym
  123. else:
  124. break
  125. b = getBody(c.graph, s)
  126. if b.kind != nkSym: internalError(c.graph.config, n.info, "wrong AST for borrowed symbol")
  127. b = newSymNode(b.sym, n.info)
  128. elif c.inlining > 0:
  129. # see bug #13596: we use ref-based equality in the DFA for destruction
  130. # injections so we need to ensure unique nodes after iterator inlining
  131. # which can lead to duplicated for loop bodies! Consider:
  132. #[
  133. while remaining > 0:
  134. if ending == nil:
  135. yield ms
  136. break
  137. ...
  138. yield ms
  139. ]#
  140. b = newSymNode(n.sym, n.info)
  141. else:
  142. b = n
  143. while tc != nil:
  144. result = getOrDefault(tc.mapping, b.sym.itemId)
  145. if result != nil:
  146. # this slightly convoluted way ensures the line info stays correct:
  147. if result.kind == nkSym:
  148. result = copyNode(result)
  149. result.info = n.info
  150. return
  151. tc = tc.next
  152. result = b
  153. proc transformSym(c: PTransf, n: PNode): PNode =
  154. result = transformSymAux(c, n)
  155. proc freshVar(c: PTransf; v: PSym): PNode =
  156. let owner = getCurrOwner(c)
  157. if owner.isIterator and not c.tooEarly and not isDefined(c.graph.config, "nimOptIters"):
  158. result = freshVarForClosureIter(c.graph, v, c.idgen, owner)
  159. else:
  160. var newVar = copySym(v, c.idgen)
  161. incl(newVar.flags, sfFromGeneric)
  162. newVar.owner = owner
  163. result = newSymNode(newVar)
  164. proc transformVarSection(c: PTransf, v: PNode): PNode =
  165. result = newTransNode(v)
  166. for i in 0..<v.len:
  167. var it = v[i]
  168. if it.kind == nkCommentStmt:
  169. result[i] = it
  170. elif it.kind == nkIdentDefs:
  171. var vn = it[0]
  172. if vn.kind == nkPragmaExpr: vn = vn[0]
  173. if vn.kind == nkSym:
  174. internalAssert(c.graph.config, it.len == 3)
  175. let x = freshVar(c, vn.sym)
  176. c.transCon.mapping[vn.sym.itemId] = x
  177. var defs = newTransNode(nkIdentDefs, it.info, 3)
  178. if importantComments(c.graph.config):
  179. # keep documentation information:
  180. defs.comment = it.comment
  181. defs[0] = x
  182. defs[1] = it[1]
  183. defs[2] = transform(c, it[2])
  184. if x.kind == nkSym: x.sym.ast = defs[2]
  185. result[i] = defs
  186. else:
  187. # has been transformed into 'param.x' for closure iterators, so just
  188. # transform it:
  189. result[i] = transform(c, it)
  190. else:
  191. if it.kind != nkVarTuple:
  192. internalError(c.graph.config, it.info, "transformVarSection: not nkVarTuple")
  193. var defs = newTransNode(it.kind, it.info, it.len)
  194. for j in 0..<it.len-2:
  195. if it[j].kind == nkSym:
  196. let x = freshVar(c, it[j].sym)
  197. c.transCon.mapping[it[j].sym.itemId] = x
  198. defs[j] = x
  199. else:
  200. defs[j] = transform(c, it[j])
  201. assert(it[^2].kind == nkEmpty)
  202. defs[^2] = newNodeI(nkEmpty, it.info)
  203. defs[^1] = transform(c, it[^1])
  204. result[i] = defs
  205. proc transformConstSection(c: PTransf, v: PNode): PNode =
  206. result = v
  207. when false:
  208. result = newTransNode(v)
  209. for i in 0..<v.len:
  210. var it = v[i]
  211. if it.kind == nkCommentStmt:
  212. result[i] = it
  213. else:
  214. if it.kind != nkConstDef: internalError(c.graph.config, it.info, "transformConstSection")
  215. if it[0].kind != nkSym:
  216. debug it[0]
  217. internalError(c.graph.config, it.info, "transformConstSection")
  218. result[i] = it
  219. proc hasContinue(n: PNode): bool =
  220. case n.kind
  221. of nkEmpty..nkNilLit, nkForStmt, nkParForStmt, nkWhileStmt: result = false
  222. of nkContinueStmt: result = true
  223. else:
  224. result = false
  225. for i in 0..<n.len:
  226. if hasContinue(n[i]): return true
  227. proc newLabel(c: PTransf, n: PNode): PSym =
  228. result = newSym(skLabel, getIdent(c.graph.cache, genPrefix), c.idgen, getCurrOwner(c), n.info)
  229. proc transformBlock(c: PTransf, n: PNode): PNode =
  230. var labl: PSym
  231. if c.inlining > 0:
  232. labl = newLabel(c, n[0])
  233. c.transCon.mapping[n[0].sym.itemId] = newSymNode(labl)
  234. else:
  235. labl =
  236. if n[0].kind != nkEmpty:
  237. n[0].sym # already named block? -> Push symbol on the stack
  238. else:
  239. newLabel(c, n)
  240. c.breakSyms.add(labl)
  241. result = transformSons(c, n)
  242. discard c.breakSyms.pop
  243. result[0] = newSymNode(labl)
  244. proc transformLoopBody(c: PTransf, n: PNode): PNode =
  245. # What if it contains "continue" and "break"? "break" needs
  246. # an explicit label too, but not the same!
  247. # We fix this here by making every 'break' belong to its enclosing loop
  248. # and changing all breaks that belong to a 'block' by annotating it with
  249. # a label (if it hasn't one already).
  250. if hasContinue(n):
  251. let labl = newLabel(c, n)
  252. c.contSyms.add(labl)
  253. result = newTransNode(nkBlockStmt, n.info, 2)
  254. result[0] = newSymNode(labl)
  255. result[1] = transform(c, n)
  256. discard c.contSyms.pop()
  257. else:
  258. result = transform(c, n)
  259. proc transformWhile(c: PTransf; n: PNode): PNode =
  260. if c.inlining > 0:
  261. result = transformSons(c, n)
  262. else:
  263. let labl = newLabel(c, n)
  264. c.breakSyms.add(labl)
  265. result = newTransNode(nkBlockStmt, n.info, 2)
  266. result[0] = newSymNode(labl)
  267. var body = newTransNode(n)
  268. for i in 0..<n.len-1:
  269. body[i] = transform(c, n[i])
  270. body[^1] = transformLoopBody(c, n[^1])
  271. result[1] = body
  272. discard c.breakSyms.pop
  273. proc transformBreak(c: PTransf, n: PNode): PNode =
  274. result = transformSons(c, n)
  275. if n[0].kind == nkEmpty and c.breakSyms.len > 0:
  276. let labl = c.breakSyms[c.breakSyms.high]
  277. result[0] = newSymNode(labl)
  278. proc introduceNewLocalVars(c: PTransf, n: PNode): PNode =
  279. case n.kind
  280. of nkSym:
  281. result = transformSym(c, n)
  282. of nkEmpty..pred(nkSym), succ(nkSym)..nkNilLit:
  283. # nothing to be done for leaves:
  284. result = n
  285. of nkVarSection, nkLetSection:
  286. result = transformVarSection(c, n)
  287. of nkClosure:
  288. # it can happen that for-loop-inlining produced a fresh
  289. # set of variables, including some computed environment
  290. # (bug #2604). We need to patch this environment here too:
  291. let a = n[1]
  292. if a.kind == nkSym:
  293. n[1] = transformSymAux(c, a)
  294. return n
  295. of nkProcDef: # todo optimize nosideeffects?
  296. result = newTransNode(n)
  297. let x = newSymNode(copySym(n[namePos].sym, c.idgen))
  298. c.transCon.mapping[n[namePos].sym.itemId] = x
  299. result[namePos] = x # we have to copy proc definitions for iters
  300. for i in 1..<n.len:
  301. result[i] = introduceNewLocalVars(c, n[i])
  302. result[namePos].sym.ast = result
  303. else:
  304. result = newTransNode(n)
  305. for i in 0..<n.len:
  306. result[i] = introduceNewLocalVars(c, n[i])
  307. proc transformAsgn(c: PTransf, n: PNode): PNode =
  308. let rhs = n[1]
  309. if rhs.kind != nkTupleConstr:
  310. return transformSons(c, n)
  311. # Unpack the tuple assignment into N temporary variables and then pack them
  312. # into a tuple: this allows us to get the correct results even when the rhs
  313. # depends on the value of the lhs
  314. let letSection = newTransNode(nkLetSection, n.info, rhs.len)
  315. let newTupleConstr = newTransNode(nkTupleConstr, n.info, rhs.len)
  316. for i, field in rhs:
  317. let val = if field.kind == nkExprColonExpr: field[1] else: field
  318. let def = newTransNode(nkIdentDefs, field.info, 3)
  319. def[0] = newTemp(c, val.typ, field.info)
  320. def[1] = newNodeI(nkEmpty, field.info)
  321. def[2] = transform(c, val)
  322. letSection[i] = def
  323. # NOTE: We assume the constructor fields are in the correct order for the
  324. # given tuple type
  325. newTupleConstr[i] = def[0]
  326. newTupleConstr.typ = rhs.typ
  327. let asgnNode = newTransNode(nkAsgn, n.info, 2)
  328. asgnNode[0] = transform(c, n[0])
  329. asgnNode[1] = newTupleConstr
  330. result = newTransNode(nkStmtList, n.info, 2)
  331. result[0] = letSection
  332. result[1] = asgnNode
  333. proc transformYield(c: PTransf, n: PNode): PNode =
  334. proc asgnTo(lhs: PNode, rhs: PNode): PNode =
  335. # Choose the right assignment instruction according to the given ``lhs``
  336. # node since it may not be a nkSym (a stack-allocated skForVar) but a
  337. # nkDotExpr (a heap-allocated slot into the envP block)
  338. case lhs.kind
  339. of nkSym:
  340. internalAssert c.graph.config, lhs.sym.kind == skForVar
  341. result = newAsgnStmt(c, nkFastAsgn, lhs, rhs, false)
  342. of nkDotExpr:
  343. result = newAsgnStmt(c, nkAsgn, lhs, rhs, false)
  344. else:
  345. result = nil
  346. internalAssert c.graph.config, false
  347. result = newTransNode(nkStmtList, n.info, 0)
  348. var e = n[0]
  349. # c.transCon.forStmt.len == 3 means that there is one for loop variable
  350. # and thus no tuple unpacking:
  351. if e.typ.isNil: return result # can happen in nimsuggest for unknown reasons
  352. if c.transCon.forStmt.len != 3:
  353. e = skipConv(e)
  354. if e.kind == nkTupleConstr:
  355. for i in 0..<e.len:
  356. var v = e[i]
  357. if v.kind == nkExprColonExpr: v = v[1]
  358. if c.transCon.forStmt[i].kind == nkVarTuple:
  359. for j in 0..<c.transCon.forStmt[i].len-1:
  360. let lhs = c.transCon.forStmt[i][j]
  361. let rhs = transform(c, newTupleAccess(c.graph, v, j))
  362. result.add(asgnTo(lhs, rhs))
  363. else:
  364. let lhs = c.transCon.forStmt[i]
  365. let rhs = transform(c, v)
  366. result.add(asgnTo(lhs, rhs))
  367. elif e.kind notin {nkAddr, nkHiddenAddr}: # no need to generate temp for address operation
  368. # TODO do not use temp for nodes which cannot have side-effects
  369. var tmp = newTemp(c, e.typ, e.info)
  370. let v = newNodeI(nkVarSection, e.info)
  371. v.addVar(tmp, e)
  372. result.add transform(c, v)
  373. for i in 0..<c.transCon.forStmt.len - 2:
  374. if c.transCon.forStmt[i].kind == nkVarTuple:
  375. for j in 0..<c.transCon.forStmt[i].len-1:
  376. let lhs = c.transCon.forStmt[i][j]
  377. let rhs = transform(c, newTupleAccess(c.graph, newTupleAccess(c.graph, tmp, i), j))
  378. result.add(asgnTo(lhs, rhs))
  379. else:
  380. let lhs = c.transCon.forStmt[i]
  381. let rhs = transform(c, newTupleAccess(c.graph, tmp, i))
  382. result.add(asgnTo(lhs, rhs))
  383. else:
  384. for i in 0..<c.transCon.forStmt.len - 2:
  385. let lhs = c.transCon.forStmt[i]
  386. let rhs = transform(c, newTupleAccess(c.graph, e, i))
  387. result.add(asgnTo(lhs, rhs))
  388. else:
  389. if c.transCon.forStmt[0].kind == nkVarTuple:
  390. var notLiteralTuple = false # we don't generate temp for tuples with const value: (1, 2, 3)
  391. let ev = e.skipConv
  392. if ev.kind == nkTupleConstr:
  393. for i in ev:
  394. if not isConstExpr(i):
  395. notLiteralTuple = true
  396. break
  397. else:
  398. notLiteralTuple = true
  399. if e.kind notin {nkAddr, nkHiddenAddr} and notLiteralTuple:
  400. # TODO do not use temp for nodes which cannot have side-effects
  401. var tmp = newTemp(c, e.typ, e.info)
  402. let v = newNodeI(nkVarSection, e.info)
  403. v.addVar(tmp, e)
  404. result.add transform(c, v)
  405. for i in 0..<c.transCon.forStmt[0].len-1:
  406. let lhs = c.transCon.forStmt[0][i]
  407. let rhs = transform(c, newTupleAccess(c.graph, tmp, i))
  408. result.add(asgnTo(lhs, rhs))
  409. else:
  410. for i in 0..<c.transCon.forStmt[0].len-1:
  411. let lhs = c.transCon.forStmt[0][i]
  412. let rhs = transform(c, newTupleAccess(c.graph, e, i))
  413. result.add(asgnTo(lhs, rhs))
  414. else:
  415. let lhs = c.transCon.forStmt[0]
  416. let rhs = transform(c, e)
  417. result.add(asgnTo(lhs, rhs))
  418. # bug #23536; note that the info of forLoopBody should't change
  419. for idx in 0 ..< result.len:
  420. var changeNode = result[idx]
  421. changeNode.info = c.transCon.forStmt.info
  422. for i, child in changeNode:
  423. child.info = changeNode.info
  424. inc(c.transCon.yieldStmts)
  425. if c.transCon.yieldStmts <= 1:
  426. # common case
  427. result.add(c.transCon.forLoopBody)
  428. else:
  429. # we need to introduce new local variables:
  430. c.isIntroducingNewLocalVars = true # don't transform yields when introducing new local vars
  431. result.add(introduceNewLocalVars(c, c.transCon.forLoopBody))
  432. c.isIntroducingNewLocalVars = false
  433. proc transformAddrDeref(c: PTransf, n: PNode, kinds: TNodeKinds): PNode =
  434. result = transformSons(c, n)
  435. # inlining of 'var openarray' iterators; bug #19977
  436. if n.typ.kind != tyOpenArray and (c.graph.config.backend == backendCpp or sfCompileToCpp in c.module.flags): return
  437. var n = result
  438. case n[0].kind
  439. of nkObjUpConv, nkObjDownConv, nkChckRange, nkChckRangeF, nkChckRange64:
  440. var m = n[0][0]
  441. if m.kind in kinds:
  442. # addr ( nkConv ( deref ( x ) ) ) --> nkConv(x)
  443. n[0][0] = m[0]
  444. result = n[0]
  445. if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
  446. result.typ = n.typ
  447. elif n.typ.skipTypes(abstractInst).kind in {tyVar}:
  448. result.typ = toVar(result.typ, n.typ.skipTypes(abstractInst).kind, c.idgen)
  449. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  450. var m = n[0][1]
  451. if m.kind in kinds:
  452. # addr ( nkConv ( deref ( x ) ) ) --> nkConv(x)
  453. n[0][1] = m[0]
  454. result = n[0]
  455. if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
  456. result.typ = n.typ
  457. elif n.typ.skipTypes(abstractInst).kind in {tyVar}:
  458. result.typ = toVar(result.typ, n.typ.skipTypes(abstractInst).kind, c.idgen)
  459. else:
  460. if n[0].kind in kinds and
  461. not (n[0][0].kind == nkSym and n[0][0].sym.kind == skForVar and
  462. n[0][0].typ.skipTypes(abstractVar).kind == tyTuple
  463. ) and not (n[0][0].kind == nkSym and n[0][0].sym.kind == skParam and
  464. n.typ.kind == tyVar and
  465. n.typ.skipTypes(abstractVar).kind == tyOpenArray and
  466. n[0][0].typ.skipTypes(abstractVar).kind == tyString)
  467. : # elimination is harmful to `for tuple unpack` because of newTupleAccess
  468. # it is also harmful to openArrayLoc (var openArray) for strings
  469. # addr ( deref ( x )) --> x
  470. result = n[0][0]
  471. if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
  472. result.typ = n.typ
  473. proc generateThunk(c: PTransf; prc: PNode, dest: PType): PNode =
  474. ## Converts 'prc' into '(thunk, nil)' so that it's compatible with
  475. ## a closure.
  476. # we cannot generate a proper thunk here for GC-safety reasons
  477. # (see internal documentation):
  478. if jsNoLambdaLifting in c.graph.config.legacyFeatures and c.graph.config.backend == backendJs: return prc
  479. result = newNodeIT(nkClosure, prc.info, dest)
  480. var conv = newNodeIT(nkHiddenSubConv, prc.info, dest)
  481. conv.add(newNodeI(nkEmpty, prc.info))
  482. conv.add(prc)
  483. if prc.kind == nkClosure:
  484. internalError(c.graph.config, prc.info, "closure to closure created")
  485. result.add(conv)
  486. result.add(newNodeIT(nkNilLit, prc.info, getSysType(c.graph, prc.info, tyNil)))
  487. proc transformConv(c: PTransf, n: PNode): PNode =
  488. # numeric types need range checks:
  489. var dest = skipTypes(n.typ, abstractVarRange)
  490. var source = skipTypes(n[1].typ, abstractVarRange)
  491. case dest.kind
  492. of tyInt..tyInt64, tyEnum, tyChar, tyUInt8..tyUInt32:
  493. # we don't include uint and uint64 here as these are no ordinal types ;-)
  494. if not isOrdinalType(source):
  495. # float -> int conversions. ugh.
  496. result = transformSons(c, n)
  497. elif firstOrd(c.graph.config, n.typ) <= firstOrd(c.graph.config, n[1].typ) and
  498. lastOrd(c.graph.config, n[1].typ) <= lastOrd(c.graph.config, n.typ):
  499. # BUGFIX: simply leave n as it is; we need a nkConv node,
  500. # but no range check:
  501. result = transformSons(c, n)
  502. else:
  503. # generate a range check:
  504. if dest.kind == tyInt64 or source.kind == tyInt64:
  505. result = newTransNode(nkChckRange64, n, 3)
  506. else:
  507. result = newTransNode(nkChckRange, n, 3)
  508. dest = skipTypes(n.typ, abstractVar)
  509. result[0] = transform(c, n[1])
  510. result[1] = newIntTypeNode(firstOrd(c.graph.config, dest), dest)
  511. result[2] = newIntTypeNode(lastOrd(c.graph.config, dest), dest)
  512. of tyFloat..tyFloat128:
  513. # XXX int64 -> float conversion?
  514. if skipTypes(n.typ, abstractVar).kind == tyRange:
  515. result = newTransNode(nkChckRangeF, n, 3)
  516. dest = skipTypes(n.typ, abstractVar)
  517. result[0] = transform(c, n[1])
  518. result[1] = copyTree(dest.n[0])
  519. result[2] = copyTree(dest.n[1])
  520. else:
  521. result = transformSons(c, n)
  522. of tyOpenArray, tyVarargs:
  523. if keepOpenArrayConversions in c.flags:
  524. result = transformSons(c, n)
  525. else:
  526. result = transform(c, n[1])
  527. #result = transformSons(c, n)
  528. result.typ = takeType(n.typ, n[1].typ, c.graph, c.idgen)
  529. #echo n.info, " came here and produced ", typeToString(result.typ),
  530. # " from ", typeToString(n.typ), " and ", typeToString(n[1].typ)
  531. of tyCstring:
  532. if source.kind == tyString:
  533. result = newTransNode(nkStringToCString, n, 1)
  534. result[0] = transform(c, n[1])
  535. else:
  536. result = transformSons(c, n)
  537. of tyString:
  538. if source.kind == tyCstring:
  539. result = newTransNode(nkCStringToString, n, 1)
  540. result[0] = transform(c, n[1])
  541. else:
  542. result = transformSons(c, n)
  543. of tyRef, tyPtr:
  544. dest = skipTypes(dest, abstractPtrs)
  545. source = skipTypes(source, abstractPtrs)
  546. if source.kind == tyObject:
  547. var diff = inheritanceDiff(dest, source)
  548. if diff < 0:
  549. result = newTransNode(nkObjUpConv, n, 1)
  550. result[0] = transform(c, n[1])
  551. elif diff > 0 and diff != high(int):
  552. result = newTransNode(nkObjDownConv, n, 1)
  553. result[0] = transform(c, n[1])
  554. else:
  555. result = transform(c, n[1])
  556. result.typ = n.typ
  557. else:
  558. result = transformSons(c, n)
  559. of tyObject:
  560. var diff = inheritanceDiff(dest, source)
  561. if diff < 0:
  562. result = newTransNode(nkObjUpConv, n, 1)
  563. result[0] = transform(c, n[1])
  564. elif diff > 0 and diff != high(int):
  565. result = newTransNode(nkObjDownConv, n, 1)
  566. result[0] = transform(c, n[1])
  567. else:
  568. result = transform(c, n[1])
  569. result.typ = n.typ
  570. of tyGenericParam, tyOrdinal:
  571. result = transform(c, n[1])
  572. # happens sometimes for generated assignments, etc.
  573. of tyProc:
  574. result = transformSons(c, n)
  575. if dest.callConv == ccClosure and source.callConv == ccNimCall:
  576. result = generateThunk(c, result[1], dest)
  577. else:
  578. result = transformSons(c, n)
  579. type
  580. TPutArgInto = enum
  581. paDirectMapping, paFastAsgn, paFastAsgnTakeTypeFromArg
  582. paVarAsgn, paComplexOpenarray, paViaIndirection
  583. proc putArgInto(arg: PNode, formal: PType): TPutArgInto =
  584. # This analyses how to treat the mapping "formal <-> arg" in an
  585. # inline context.
  586. if formal.kind == tyTypeDesc: return paDirectMapping
  587. if skipTypes(formal, abstractInst).kind in {tyOpenArray, tyVarargs}:
  588. case arg.kind
  589. of nkStmtListExpr:
  590. return paComplexOpenarray
  591. of nkBracket:
  592. return paFastAsgnTakeTypeFromArg
  593. else:
  594. # XXX incorrect, causes #13417 when `arg` has side effects.
  595. return paDirectMapping
  596. case arg.kind
  597. of nkEmpty..nkNilLit:
  598. result = paDirectMapping
  599. of nkDotExpr, nkDerefExpr, nkHiddenDeref:
  600. result = putArgInto(arg[0], formal)
  601. of nkAddr, nkHiddenAddr:
  602. result = putArgInto(arg[0], formal)
  603. if result == paViaIndirection: result = paFastAsgn
  604. of nkCurly, nkBracket:
  605. for i in 0..<arg.len:
  606. if putArgInto(arg[i], formal) != paDirectMapping:
  607. return paFastAsgn
  608. result = paDirectMapping
  609. of nkPar, nkTupleConstr, nkObjConstr:
  610. for i in 0..<arg.len:
  611. let a = if arg[i].kind == nkExprColonExpr: arg[i][1]
  612. else: arg[0]
  613. if putArgInto(a, formal) != paDirectMapping:
  614. return paFastAsgn
  615. result = paDirectMapping
  616. of nkBracketExpr:
  617. if skipTypes(formal, abstractInst).kind in {tyVar, tyLent}: result = paVarAsgn
  618. else: result = paViaIndirection
  619. else:
  620. if skipTypes(formal, abstractInst).kind in {tyVar, tyLent}: result = paVarAsgn
  621. else: result = paFastAsgn
  622. proc findWrongOwners(c: PTransf, n: PNode) =
  623. if n.kind == nkVarSection:
  624. let x = n[0][0]
  625. if x.kind == nkSym and x.sym.owner != getCurrOwner(c):
  626. internalError(c.graph.config, x.info, "bah " & x.sym.name.s & " " &
  627. x.sym.owner.name.s & " " & getCurrOwner(c).name.s)
  628. else:
  629. for i in 0..<n.safeLen: findWrongOwners(c, n[i])
  630. proc isSimpleIteratorVar(c: PTransf; iter: PSym; call: PNode; owner: PSym): bool =
  631. proc rec(n: PNode; owner: PSym; dangerousYields: var int) =
  632. case n.kind
  633. of nkEmpty..nkNilLit: discard
  634. of nkYieldStmt:
  635. if n[0].kind == nkSym and n[0].sym.owner == owner:
  636. discard "good: yield a single variable that we own"
  637. else:
  638. inc dangerousYields
  639. else:
  640. for c in n: rec(c, owner, dangerousYields)
  641. proc recSym(n: PNode; owner: PSym; sameOwner: var bool) =
  642. case n.kind
  643. of {nkEmpty..nkNilLit} - {nkSym}: discard
  644. of nkSym:
  645. if n.sym.owner != owner:
  646. sameOwner = false
  647. else:
  648. for c in n: recSym(c, owner, sameOwner)
  649. var dangerousYields = 0
  650. rec(getBody(c.graph, iter), iter, dangerousYields)
  651. result = dangerousYields == 0
  652. # the parameters should be owned by the owner
  653. # bug #22237
  654. for i in 1..<call.len:
  655. recSym(call[i], owner, result)
  656. template destructor(t: PType): PSym = getAttachedOp(c.graph, t, attachedDestructor)
  657. proc transformFor(c: PTransf, n: PNode): PNode =
  658. # generate access statements for the parameters (unless they are constant)
  659. # put mapping from formal parameters to actual parameters
  660. if n.kind != nkForStmt: internalError(c.graph.config, n.info, "transformFor")
  661. var call = n[^2]
  662. let labl = newLabel(c, n)
  663. result = newTransNode(nkBlockStmt, n.info, 2)
  664. result[0] = newSymNode(labl)
  665. if call.typ.isNil:
  666. # see bug #3051
  667. result[1] = newNode(nkEmpty)
  668. return result
  669. c.breakSyms.add(labl)
  670. if call.kind notin nkCallKinds or call[0].kind != nkSym or
  671. call[0].typ.skipTypes(abstractInst).callConv == ccClosure:
  672. result[1] = n
  673. result[1][^1] = transformLoopBody(c, n[^1])
  674. result[1][^2] = transform(c, n[^2])
  675. result[1] = lambdalifting.liftForLoop(c.graph, result[1], c.idgen, getCurrOwner(c))
  676. discard c.breakSyms.pop
  677. return result
  678. #echo "transforming: ", renderTree(n)
  679. var stmtList = newTransNode(nkStmtList, n.info, 0)
  680. result[1] = stmtList
  681. var loopBody = transformLoopBody(c, n[^1])
  682. discard c.breakSyms.pop
  683. let iter = call[0].sym
  684. var v = newNodeI(nkVarSection, n.info)
  685. for i in 0..<n.len - 2:
  686. if n[i].kind == nkVarTuple:
  687. for j in 0..<n[i].len-1:
  688. addVar(v, copyTree(n[i][j])) # declare new vars
  689. else:
  690. if n[i].kind == nkSym and isSimpleIteratorVar(c, iter, call, n[i].sym.owner):
  691. incl n[i].sym.flags, sfCursor
  692. addVar(v, copyTree(n[i])) # declare new vars
  693. stmtList.add(v)
  694. # Bugfix: inlined locals belong to the invoking routine, not to the invoked
  695. # iterator!
  696. var newC = newTransCon(getCurrOwner(c))
  697. newC.forStmt = n
  698. newC.forLoopBody = loopBody
  699. # this can fail for 'nimsuggest' and 'check':
  700. if iter.kind != skIterator: return result
  701. # generate access statements for the parameters (unless they are constant)
  702. pushTransCon(c, newC)
  703. for i in 1..<call.len:
  704. var arg = transform(c, call[i])
  705. let ff = skipTypes(iter.typ, abstractInst)
  706. # can happen for 'nim check':
  707. if i >= ff.n.len: return result
  708. var formal = ff.n[i].sym
  709. let pa = putArgInto(arg, formal.typ)
  710. case pa
  711. of paDirectMapping:
  712. newC.mapping[formal.itemId] = arg
  713. of paFastAsgn, paFastAsgnTakeTypeFromArg:
  714. var t = formal.typ
  715. if pa == paFastAsgnTakeTypeFromArg:
  716. t = arg.typ
  717. elif formal.ast != nil and formal.ast.typ.destructor != nil and t.destructor == nil:
  718. t = formal.ast.typ # better use the type that actually has a destructor.
  719. elif t.destructor == nil and arg.typ.destructor != nil:
  720. t = arg.typ
  721. # generate a temporary and produce an assignment statement:
  722. var temp = newTemp(c, t, formal.info)
  723. #incl(temp.sym.flags, sfCursor)
  724. addVar(v, temp)
  725. stmtList.add(newAsgnStmt(c, nkFastAsgn, temp, arg, true))
  726. newC.mapping[formal.itemId] = temp
  727. of paVarAsgn:
  728. assert(skipTypes(formal.typ, abstractInst).kind in {tyVar, tyLent})
  729. newC.mapping[formal.itemId] = arg
  730. # XXX BUG still not correct if the arg has a side effect!
  731. of paViaIndirection:
  732. let t = formal.typ
  733. let vt = makeVarType(t.owner, t, c.idgen)
  734. vt.flags.incl tfVarIsPtr
  735. var temp = newTemp(c, vt, formal.info)
  736. addVar(v, temp)
  737. var addrExp = newNodeIT(nkHiddenAddr, formal.info, makeVarType(t.owner, t, c.idgen, tyPtr))
  738. addrExp.add(arg)
  739. stmtList.add(newAsgnStmt(c, nkFastAsgn, temp, addrExp, true))
  740. newC.mapping[formal.itemId] = newDeref(temp)
  741. of paComplexOpenarray:
  742. # arrays will deep copy here (pretty bad).
  743. var temp = newTemp(c, arg.typ, formal.info)
  744. addVar(v, temp)
  745. stmtList.add(newAsgnStmt(c, nkFastAsgn, temp, arg, true))
  746. newC.mapping[formal.itemId] = temp
  747. let body = transformBody(c.graph, c.idgen, iter, {useCache}+c.flags)
  748. pushInfoContext(c.graph.config, n.info)
  749. inc(c.inlining)
  750. stmtList.add(transform(c, body))
  751. #findWrongOwners(c, stmtList.PNode)
  752. dec(c.inlining)
  753. popInfoContext(c.graph.config)
  754. popTransCon(c)
  755. # echo "transformed: ", stmtList.renderTree
  756. proc transformCase(c: PTransf, n: PNode): PNode =
  757. # removes `elif` branches of a case stmt
  758. # adds ``else: nil`` if needed for the code generator
  759. result = newTransNode(nkCaseStmt, n, 0)
  760. var ifs: PNode = nil
  761. for it in n:
  762. var e = transform(c, it)
  763. case it.kind
  764. of nkElifBranch:
  765. if ifs == nil:
  766. # Generate the right node depending on whether `n` is used as a stmt or
  767. # as an expr
  768. let kind = if n.typ != nil: nkIfExpr else: nkIfStmt
  769. ifs = newTransNode(kind, it.info, 0)
  770. ifs.typ = n.typ
  771. ifs.add(e)
  772. of nkElse:
  773. if ifs == nil: result.add(e)
  774. else: ifs.add(e)
  775. else:
  776. result.add(e)
  777. if ifs != nil:
  778. var elseBranch = newTransNode(nkElse, n.info, 1)
  779. elseBranch[0] = ifs
  780. result.add(elseBranch)
  781. elif result.lastSon.kind != nkElse and not (
  782. skipTypes(n[0].typ, abstractVarRange).kind in
  783. {tyInt..tyInt64, tyChar, tyEnum, tyUInt..tyUInt64}):
  784. # fix a stupid code gen bug by normalizing:
  785. var elseBranch = newTransNode(nkElse, n.info, 1)
  786. elseBranch[0] = newTransNode(nkNilLit, n.info, 0)
  787. result.add(elseBranch)
  788. proc transformArrayAccess(c: PTransf, n: PNode): PNode =
  789. # XXX this is really bad; transf should use a proper AST visitor
  790. if n[0].kind == nkSym and n[0].sym.kind == skType:
  791. result = n
  792. else:
  793. result = newTransNode(n)
  794. for i in 0..<n.len:
  795. result[i] = transform(c, skipConv(n[i]))
  796. proc getMergeOp(n: PNode): PSym =
  797. case n.kind
  798. of nkCall, nkHiddenCallConv, nkCommand, nkInfix, nkPrefix, nkPostfix,
  799. nkCallStrLit:
  800. if n[0].kind == nkSym and n[0].sym.magic == mConStrStr:
  801. result = n[0].sym
  802. else:
  803. result = nil
  804. else: result = nil
  805. proc flattenTreeAux(d, a: PNode, op: PSym) =
  806. ## Optimizes away the `&` calls in the children nodes and
  807. ## lifts the leaf nodes to the same level as `op2`.
  808. let op2 = getMergeOp(a)
  809. if op2 != nil and
  810. (op2.id == op.id or op.magic != mNone and op2.magic == op.magic):
  811. for i in 1..<a.len: flattenTreeAux(d, a[i], op)
  812. else:
  813. d.add copyTree(a)
  814. proc flattenTree(root: PNode): PNode =
  815. let op = getMergeOp(root)
  816. if op != nil:
  817. result = copyNode(root)
  818. result.add copyTree(root[0])
  819. flattenTreeAux(result, root, op)
  820. else:
  821. result = root
  822. proc transformCall(c: PTransf, n: PNode): PNode =
  823. var n = flattenTree(n)
  824. let op = getMergeOp(n)
  825. let magic = getMagic(n)
  826. if op != nil and op.magic != mNone and n.len >= 3:
  827. result = newTransNode(nkCall, n, 0)
  828. result.add(transform(c, n[0]))
  829. var j = 1
  830. while j < n.len:
  831. var a = transform(c, n[j])
  832. inc(j)
  833. if isConstExpr(a):
  834. while (j < n.len):
  835. let b = transform(c, n[j])
  836. if not isConstExpr(b): break
  837. a = evalOp(op.magic, n, a, b, nil, c.idgen, c.graph)
  838. inc(j)
  839. result.add(a)
  840. if result.len == 2: result = result[1]
  841. elif magic in {mNBindSym, mTypeOf, mRunnableExamples}:
  842. # for bindSym(myconst) we MUST NOT perform constant folding:
  843. result = n
  844. elif magic == mProcCall:
  845. # but do not change to its dispatcher:
  846. result = transformSons(c, n[1])
  847. elif magic == mStrToStr:
  848. result = transform(c, n[1])
  849. else:
  850. let s = transformSons(c, n)
  851. # bugfix: check after 'transformSons' if it's still a method call:
  852. # use the dispatcher for the call:
  853. if s[0].kind == nkSym and s[0].sym.kind == skMethod:
  854. when false:
  855. let t = lastSon(s[0].sym.ast)
  856. if t.kind != nkSym or sfDispatcher notin t.sym.flags:
  857. methodDef(s[0].sym, false)
  858. result = methodCall(s, c.graph.config)
  859. else:
  860. result = s
  861. proc transformExceptBranch(c: PTransf, n: PNode): PNode =
  862. if n[0].isInfixAs() and not isImportedException(n[0][1].typ, c.graph.config):
  863. let excTypeNode = n[0][1]
  864. let actions = newTransNode(nkStmtListExpr, n[1], 2)
  865. # Generating `let exc = (excType)(getCurrentException())`
  866. # -> getCurrentException()
  867. let excCall = callCodegenProc(c.graph, "getCurrentException")
  868. # -> (excType)
  869. let convNode = newTransNode(nkHiddenSubConv, n[1].info, 2)
  870. convNode[0] = newNodeI(nkEmpty, n.info)
  871. convNode[1] = excCall
  872. convNode.typ = excTypeNode.typ.toRef(c.idgen)
  873. # -> let exc = ...
  874. let identDefs = newTransNode(nkIdentDefs, n[1].info, 3)
  875. identDefs[0] = n[0][2]
  876. identDefs[1] = newNodeI(nkEmpty, n.info)
  877. identDefs[2] = convNode
  878. let letSection = newTransNode(nkLetSection, n[1].info, 1)
  879. letSection[0] = identDefs
  880. # Place the let statement and body of the 'except' branch into new stmtList.
  881. actions[0] = letSection
  882. actions[1] = transform(c, n[1])
  883. # Overwrite 'except' branch body with our stmtList.
  884. result = newTransNode(nkExceptBranch, n[1].info, 2)
  885. # Replace the `Exception as foobar` with just `Exception`.
  886. result[0] = transform(c, n[0][1])
  887. result[1] = actions
  888. else:
  889. result = transformSons(c, n)
  890. proc commonOptimizations*(g: ModuleGraph; idgen: IdGenerator; c: PSym, n: PNode): PNode =
  891. ## Merges adjacent constant expressions of the children of the `&` call into
  892. ## a single constant expression. It also inlines constant expressions which are not
  893. ## complex.
  894. result = n
  895. for i in 0..<n.safeLen:
  896. result[i] = commonOptimizations(g, idgen, c, n[i])
  897. var op = getMergeOp(n)
  898. if (op != nil) and (op.magic != mNone) and (n.len >= 3):
  899. result = newNodeIT(nkCall, n.info, n.typ)
  900. result.add(n[0])
  901. var args = newNode(nkArgList)
  902. flattenTreeAux(args, n, op)
  903. var j = 0
  904. while j < args.len:
  905. var a = args[j]
  906. inc(j)
  907. if isConstExpr(a):
  908. while j < args.len:
  909. let b = args[j]
  910. if not isConstExpr(b): break
  911. a = evalOp(op.magic, result, a, b, nil, idgen, g)
  912. inc(j)
  913. result.add(a)
  914. if result.len == 2: result = result[1]
  915. else:
  916. var cnst = getConstExpr(c, n, idgen, g)
  917. # we inline constants if they are not complex constants:
  918. if cnst != nil and not dontInlineConstant(n, cnst):
  919. result = cnst
  920. else:
  921. result = n
  922. proc transformDerefBlock(c: PTransf, n: PNode): PNode =
  923. # We transform (block: x)[] to (block: x[])
  924. let e0 = n[0]
  925. result = shallowCopy(e0)
  926. result.typ = n.typ
  927. for i in 0 ..< e0.len - 1:
  928. result[i] = e0[i]
  929. result[e0.len-1] = newTreeIT(nkHiddenDeref, n.info, n.typ, e0[e0.len-1])
  930. proc transform(c: PTransf, n: PNode): PNode =
  931. when false:
  932. var oldDeferAnchor: PNode
  933. if n.kind in {nkElifBranch, nkOfBranch, nkExceptBranch, nkElifExpr,
  934. nkElseExpr, nkElse, nkForStmt, nkWhileStmt, nkFinally,
  935. nkBlockStmt, nkBlockExpr}:
  936. oldDeferAnchor = c.deferAnchor
  937. c.deferAnchor = n
  938. case n.kind
  939. of nkSym:
  940. result = transformSym(c, n)
  941. of nkEmpty..pred(nkSym), succ(nkSym)..nkNilLit, nkComesFrom:
  942. # nothing to be done for leaves:
  943. result = n
  944. of nkBracketExpr: result = transformArrayAccess(c, n)
  945. of procDefs:
  946. var s = n[namePos].sym
  947. if n.typ != nil and s.typ.callConv == ccClosure:
  948. result = transformSym(c, n[namePos])
  949. # use the same node as before if still a symbol:
  950. if result.kind == nkSym: result = n
  951. else:
  952. result = n
  953. of nkMacroDef:
  954. # XXX no proper closure support yet:
  955. when false:
  956. if n[genericParamsPos].kind == nkEmpty:
  957. var s = n[namePos].sym
  958. n[bodyPos] = transform(c, s.getBody)
  959. if n.kind == nkMethodDef: methodDef(s, false)
  960. result = n
  961. of nkForStmt:
  962. result = transformFor(c, n)
  963. of nkParForStmt:
  964. result = transformSons(c, n)
  965. of nkCaseStmt:
  966. result = transformCase(c, n)
  967. of nkWhileStmt: result = transformWhile(c, n)
  968. of nkBlockStmt, nkBlockExpr:
  969. result = transformBlock(c, n)
  970. of nkDefer:
  971. c.deferDetected = true
  972. result = transformSons(c, n)
  973. when false:
  974. let deferPart = newNodeI(nkFinally, n.info)
  975. deferPart.add n[0]
  976. let tryStmt = newNodeI(nkTryStmt, n.info)
  977. if c.deferAnchor.isNil:
  978. tryStmt.add c.root
  979. c.root = tryStmt
  980. result = tryStmt
  981. else:
  982. # modify the corresponding *action*, don't rely on nkStmtList:
  983. tryStmt.add c.deferAnchor[^1]
  984. c.deferAnchor[^1] = tryStmt
  985. result = newTransNode(nkCommentStmt, n.info, 0)
  986. tryStmt.add deferPart
  987. # disable the original 'defer' statement:
  988. n.kind = nkEmpty
  989. of nkContinueStmt:
  990. result = newNodeI(nkBreakStmt, n.info)
  991. var labl = c.contSyms[c.contSyms.high]
  992. result.add(newSymNode(labl))
  993. of nkBreakStmt: result = transformBreak(c, n)
  994. of nkCallKinds:
  995. result = transformCall(c, n)
  996. of nkHiddenAddr:
  997. result = transformAddrDeref(c, n, {nkHiddenDeref})
  998. of nkAddr:
  999. let oldInAddr = c.inAddr
  1000. c.inAddr = true
  1001. result = transformAddrDeref(c, n, {nkDerefExpr, nkHiddenDeref})
  1002. c.inAddr = oldInAddr
  1003. of nkDerefExpr:
  1004. result = transformAddrDeref(c, n, {nkAddr, nkHiddenAddr})
  1005. of nkHiddenDeref:
  1006. if n[0].kind in {nkBlockExpr, nkBlockStmt}:
  1007. # bug #20107 bug #21540. Watch out to not deref the pointer too late.
  1008. let e = transformDerefBlock(c, n)
  1009. result = transformBlock(c, e)
  1010. else:
  1011. result = transformAddrDeref(c, n, {nkAddr, nkHiddenAddr})
  1012. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  1013. result = transformConv(c, n)
  1014. of nkDiscardStmt:
  1015. result = n
  1016. if n[0].kind != nkEmpty:
  1017. result = transformSons(c, n)
  1018. if isConstExpr(result[0]):
  1019. # ensure that e.g. discard "some comment" gets optimized away
  1020. # completely:
  1021. result = newNode(nkCommentStmt)
  1022. of nkCommentStmt, nkTemplateDef, nkImportStmt, nkStaticStmt,
  1023. nkExportStmt, nkExportExceptStmt:
  1024. return n
  1025. of nkConstSection:
  1026. # do not replace ``const c = 3`` with ``const 3 = 3``
  1027. return transformConstSection(c, n)
  1028. of nkTypeSection, nkTypeOfExpr, nkMixinStmt, nkBindStmt:
  1029. # no need to transform type sections:
  1030. return n
  1031. of nkVarSection, nkLetSection:
  1032. if c.inlining > 0:
  1033. # we need to copy the variables for multiple yield statements:
  1034. result = transformVarSection(c, n)
  1035. else:
  1036. result = transformSons(c, n)
  1037. of nkYieldStmt:
  1038. if c.inlining > 0 and not c.isIntroducingNewLocalVars:
  1039. result = transformYield(c, n)
  1040. else:
  1041. result = transformSons(c, n)
  1042. of nkAsgn:
  1043. result = transformAsgn(c, n)
  1044. of nkIdentDefs, nkConstDef:
  1045. result = newTransNode(n)
  1046. result[0] = transform(c, skipPragmaExpr(n[0]))
  1047. # Skip the second son since it only contains an unsemanticized copy of the
  1048. # variable type used by docgen
  1049. let last = n.len-1
  1050. for i in 1..<last: result[i] = n[i]
  1051. result[last] = transform(c, n[last])
  1052. # XXX comment handling really sucks:
  1053. if importantComments(c.graph.config):
  1054. result.comment = n.comment
  1055. of nkClosure:
  1056. # it can happen that for-loop-inlining produced a fresh
  1057. # set of variables, including some computed environment
  1058. # (bug #2604). We need to patch this environment here too:
  1059. let a = n[1]
  1060. if a.kind == nkSym:
  1061. result = copyTree(n)
  1062. result[1] = transformSymAux(c, a)
  1063. else:
  1064. result = n
  1065. of nkExceptBranch:
  1066. result = transformExceptBranch(c, n)
  1067. of nkCheckedFieldExpr:
  1068. result = transformSons(c, n)
  1069. if result[0].kind != nkDotExpr:
  1070. # simplfied beyond a dot expression --> simplify further.
  1071. result = result[0]
  1072. else:
  1073. result = transformSons(c, n)
  1074. when false:
  1075. if oldDeferAnchor != nil: c.deferAnchor = oldDeferAnchor
  1076. # Constants can be inlined here, but only if they cannot result in a cast
  1077. # in the back-end (e.g. var p: pointer = someProc)
  1078. let exprIsPointerCast = n.kind in {nkCast, nkConv, nkHiddenStdConv} and
  1079. n.typ != nil and
  1080. n.typ.kind == tyPointer
  1081. if not exprIsPointerCast and not c.inAddr:
  1082. var cnst = getConstExpr(c.module, result, c.idgen, c.graph)
  1083. # we inline constants if they are not complex constants:
  1084. if cnst != nil and not dontInlineConstant(n, cnst):
  1085. result = cnst # do not miss an optimization
  1086. proc processTransf(c: PTransf, n: PNode, owner: PSym): PNode =
  1087. # Note: For interactive mode we cannot call 'passes.skipCodegen' and skip
  1088. # this step! We have to rely that the semantic pass transforms too errornous
  1089. # nodes into an empty node.
  1090. if nfTransf in n.flags: return n
  1091. pushTransCon(c, newTransCon(owner))
  1092. result = transform(c, n)
  1093. popTransCon(c)
  1094. incl(result.flags, nfTransf)
  1095. proc openTransf(g: ModuleGraph; module: PSym, filename: string; idgen: IdGenerator; flags: TransformFlags): PTransf =
  1096. result = PTransf(module: module, graph: g, idgen: idgen, flags: flags)
  1097. proc flattenStmts(n: PNode) =
  1098. var goOn = true
  1099. while goOn:
  1100. goOn = false
  1101. var i = 0
  1102. while i < n.len:
  1103. let it = n[i]
  1104. if it.kind in {nkStmtList, nkStmtListExpr}:
  1105. n.sons[i..i] = it.sons[0..<it.len]
  1106. goOn = true
  1107. inc i
  1108. proc liftDeferAux(n: PNode) =
  1109. if n.kind in {nkStmtList, nkStmtListExpr}:
  1110. flattenStmts(n)
  1111. var goOn = true
  1112. while goOn:
  1113. goOn = false
  1114. let last = n.len-1
  1115. for i in 0..last:
  1116. if n[i].kind == nkDefer:
  1117. let deferPart = newNodeI(nkFinally, n[i].info)
  1118. deferPart.add n[i][0]
  1119. var tryStmt = newNodeIT(nkTryStmt, n[i].info, n.typ)
  1120. var body = newNodeIT(n.kind, n[i].info, n.typ)
  1121. if i < last:
  1122. body.sons = n.sons[(i+1)..last]
  1123. tryStmt.add body
  1124. tryStmt.add deferPart
  1125. n[i] = tryStmt
  1126. n.sons.setLen(i+1)
  1127. n.typ = tryStmt.typ
  1128. goOn = true
  1129. break
  1130. for i in 0..n.safeLen-1:
  1131. liftDeferAux(n[i])
  1132. template liftDefer(c, root) =
  1133. if c.deferDetected:
  1134. liftDeferAux(root)
  1135. proc transformBody*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; flags: TransformFlags): PNode =
  1136. assert prc.kind in routineKinds
  1137. if prc.transformedBody != nil:
  1138. result = prc.transformedBody
  1139. elif nfTransf in getBody(g, prc).flags or prc.kind in {skTemplate}:
  1140. result = getBody(g, prc)
  1141. else:
  1142. prc.transformedBody = newNode(nkEmpty) # protects from recursion
  1143. var c = openTransf(g, prc.getModule, "", idgen, flags)
  1144. result = liftLambdas(g, prc, getBody(g, prc), c.tooEarly, c.idgen, flags)
  1145. result = processTransf(c, result, prc)
  1146. liftDefer(c, result)
  1147. result = liftLocalsIfRequested(prc, result, g.cache, g.config, c.idgen)
  1148. if prc.isIterator:
  1149. result = g.transformClosureIterator(c.idgen, prc, result)
  1150. incl(result.flags, nfTransf)
  1151. if useCache in flags or prc.typ.callConv == ccInline:
  1152. # genProc for inline procs will be called multiple times from different modules,
  1153. # it is important to transform exactly once to get sym ids and locations right
  1154. prc.transformedBody = result
  1155. else:
  1156. prc.transformedBody = nil
  1157. # XXX Rodfile support for transformedBody!
  1158. #if prc.name.s == "main":
  1159. # echo "transformed into ", renderTree(result, {renderIds})
  1160. proc transformStmt*(g: ModuleGraph; idgen: IdGenerator; module: PSym, n: PNode; flags: TransformFlags = {}): PNode =
  1161. if nfTransf in n.flags:
  1162. result = n
  1163. else:
  1164. var c = openTransf(g, module, "", idgen, flags)
  1165. result = processTransf(c, n, module)
  1166. liftDefer(c, result)
  1167. #result = liftLambdasForTopLevel(module, result)
  1168. incl(result.flags, nfTransf)
  1169. proc transformExpr*(g: ModuleGraph; idgen: IdGenerator; module: PSym, n: PNode; flags: TransformFlags = {}): PNode =
  1170. if nfTransf in n.flags:
  1171. result = n
  1172. else:
  1173. var c = openTransf(g, module, "", idgen, flags)
  1174. result = processTransf(c, n, module)
  1175. liftDefer(c, result)
  1176. # expressions are not to be injected with destructor calls as that
  1177. # the list of top level statements needs to be collected before.
  1178. incl(result.flags, nfTransf)