ccgcalls.nim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. # included from cgen.nim
  11. proc leftAppearsOnRightSide(le, ri: PNode): bool =
  12. if le != nil:
  13. for i in 1 ..< ri.len:
  14. let r = ri[i]
  15. if isPartOf(le, r) != arNo: return true
  16. proc hasNoInit(call: PNode): bool {.inline.} =
  17. result = call.sons[0].kind == nkSym and sfNoInit in call.sons[0].sym.flags
  18. proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc,
  19. callee, params: Rope) =
  20. var pl = callee & ~"(" & params
  21. # getUniqueType() is too expensive here:
  22. var typ = skipTypes(ri.sons[0].typ, abstractInst)
  23. if typ.sons[0] != nil:
  24. if isInvalidReturnType(p.config, typ.sons[0]):
  25. if params != nil: pl.add(~", ")
  26. # beware of 'result = p(result)'. We may need to allocate a temporary:
  27. if d.k in {locTemp, locNone} or not leftAppearsOnRightSide(le, ri):
  28. # Great, we can use 'd':
  29. if d.k == locNone: getTemp(p, typ.sons[0], d, needsInit=true)
  30. elif d.k notin {locTemp} and not hasNoInit(ri):
  31. # reset before pass as 'result' var:
  32. discard "resetLoc(p, d)"
  33. add(pl, addrLoc(p.config, d))
  34. add(pl, ~");$n")
  35. line(p, cpsStmts, pl)
  36. else:
  37. var tmp: TLoc
  38. getTemp(p, typ.sons[0], tmp, needsInit=true)
  39. add(pl, addrLoc(p.config, tmp))
  40. add(pl, ~");$n")
  41. line(p, cpsStmts, pl)
  42. genAssignment(p, d, tmp, {}) # no need for deep copying
  43. else:
  44. add(pl, ~")")
  45. if p.module.compileToCpp and lfSingleUse in d.flags:
  46. # do not generate spurious temporaries for C++! For C we're better off
  47. # with them to prevent undefined behaviour and because the codegen
  48. # is free to emit expressions multiple times!
  49. d.k = locCall
  50. d.r = pl
  51. excl d.flags, lfSingleUse
  52. else:
  53. if d.k == locNone: getTemp(p, typ.sons[0], d)
  54. assert(d.t != nil) # generate an assignment to d:
  55. var list: TLoc
  56. initLoc(list, locCall, d.lode, OnUnknown)
  57. list.r = pl
  58. genAssignment(p, d, list, {}) # no need for deep copying
  59. else:
  60. add(pl, ~");$n")
  61. line(p, cpsStmts, pl)
  62. proc genBoundsCheck(p: BProc; arr, a, b: TLoc)
  63. proc openArrayLoc(p: BProc, n: PNode): Rope =
  64. var a: TLoc
  65. let q = skipConv(n)
  66. if getMagic(q) == mSlice:
  67. # magic: pass slice to openArray:
  68. var b, c: TLoc
  69. initLocExpr(p, q[1], a)
  70. initLocExpr(p, q[2], b)
  71. initLocExpr(p, q[3], c)
  72. # but first produce the required index checks:
  73. if optBoundsCheck in p.options:
  74. genBoundsCheck(p, a, b, c)
  75. let ty = skipTypes(a.t, abstractVar+{tyPtr})
  76. case ty.kind
  77. of tyArray:
  78. let first = firstOrd(p.config, ty)
  79. if first == 0:
  80. result = "($1)+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
  81. else:
  82. result = "($1)+(($2)-($4)), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), intLiteral(first)]
  83. of tyOpenArray, tyVarargs, tyUncheckedArray:
  84. result = "($1)+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
  85. of tyString, tySequence:
  86. if skipTypes(n.typ, abstractInst).kind == tyVar and
  87. not compileToCpp(p.module):
  88. result = "(*$1)$4+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), dataField(p)]
  89. else:
  90. result = "$1$4+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), dataField(p)]
  91. else:
  92. internalError(p.config, "openArrayLoc: " & typeToString(a.t))
  93. else:
  94. initLocExpr(p, n, a)
  95. case skipTypes(a.t, abstractVar).kind
  96. of tyOpenArray, tyVarargs:
  97. result = "$1, $1Len_0" % [rdLoc(a)]
  98. of tyString, tySequence:
  99. if skipTypes(n.typ, abstractInst).kind == tyVar and
  100. not compileToCpp(p.module):
  101. var t: TLoc
  102. t.r = "(*$1)" % [a.rdLoc]
  103. result = "(*$1)$3, $2" % [a.rdLoc, lenExpr(p, t), dataField(p)]
  104. else:
  105. result = "$1$3, $2" % [a.rdLoc, lenExpr(p, a), dataField(p)]
  106. of tyArray:
  107. result = "$1, $2" % [rdLoc(a), rope(lengthOrd(p.config, a.t))]
  108. of tyPtr, tyRef:
  109. case lastSon(a.t).kind
  110. of tyString, tySequence:
  111. var t: TLoc
  112. t.r = "(*$1)" % [a.rdLoc]
  113. result = "(*$1)$3, $2" % [a.rdLoc, lenExpr(p, t), dataField(p)]
  114. of tyArray:
  115. result = "$1, $2" % [rdLoc(a), rope(lengthOrd(p.config, lastSon(a.t)))]
  116. else:
  117. internalError(p.config, "openArrayLoc: " & typeToString(a.t))
  118. else: internalError(p.config, "openArrayLoc: " & typeToString(a.t))
  119. proc genArgStringToCString(p: BProc, n: PNode): Rope {.inline.} =
  120. var a: TLoc
  121. initLocExpr(p, n.sons[0], a)
  122. result = ropecg(p.module, "#nimToCStringConv($1)", [a.rdLoc])
  123. proc genArg(p: BProc, n: PNode, param: PSym; call: PNode): Rope =
  124. var a: TLoc
  125. if n.kind == nkStringToCString:
  126. result = genArgStringToCString(p, n)
  127. elif skipTypes(param.typ, abstractVar).kind in {tyOpenArray, tyVarargs}:
  128. var n = if n.kind != nkHiddenAddr: n else: n.sons[0]
  129. result = openArrayLoc(p, n)
  130. elif ccgIntroducedPtr(p.config, param):
  131. initLocExpr(p, n, a)
  132. result = addrLoc(p.config, a)
  133. elif p.module.compileToCpp and param.typ.kind == tyVar and
  134. n.kind == nkHiddenAddr:
  135. initLocExprSingleUse(p, n.sons[0], a)
  136. # if the proc is 'importc'ed but not 'importcpp'ed then 'var T' still
  137. # means '*T'. See posix.nim for lots of examples that do that in the wild.
  138. let callee = call.sons[0]
  139. if callee.kind == nkSym and
  140. {sfImportC, sfInfixCall, sfCompilerProc} * callee.sym.flags == {sfImportC} and
  141. {lfHeader, lfNoDecl} * callee.sym.loc.flags != {}:
  142. result = addrLoc(p.config, a)
  143. else:
  144. result = rdLoc(a)
  145. else:
  146. initLocExprSingleUse(p, n, a)
  147. result = rdLoc(a)
  148. proc genArgNoParam(p: BProc, n: PNode): Rope =
  149. var a: TLoc
  150. if n.kind == nkStringToCString:
  151. result = genArgStringToCString(p, n)
  152. else:
  153. initLocExprSingleUse(p, n, a)
  154. result = rdLoc(a)
  155. template genParamLoop(params) {.dirty.} =
  156. if i < sonsLen(typ):
  157. assert(typ.n.sons[i].kind == nkSym)
  158. let paramType = typ.n.sons[i]
  159. if not paramType.typ.isCompileTimeOnly:
  160. if params != nil: add(params, ~", ")
  161. add(params, genArg(p, ri.sons[i], paramType.sym, ri))
  162. else:
  163. if params != nil: add(params, ~", ")
  164. add(params, genArgNoParam(p, ri.sons[i]))
  165. proc genPrefixCall(p: BProc, le, ri: PNode, d: var TLoc) =
  166. var op: TLoc
  167. # this is a hotspot in the compiler
  168. initLocExpr(p, ri.sons[0], op)
  169. var params: Rope
  170. # getUniqueType() is too expensive here:
  171. var typ = skipTypes(ri.sons[0].typ, abstractInst)
  172. assert(typ.kind == tyProc)
  173. assert(sonsLen(typ) == sonsLen(typ.n))
  174. var length = sonsLen(ri)
  175. for i in countup(1, length - 1):
  176. genParamLoop(params)
  177. fixupCall(p, le, ri, d, op.r, params)
  178. proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
  179. proc getRawProcType(p: BProc, t: PType): Rope =
  180. result = getClosureType(p.module, t, clHalf)
  181. proc addComma(r: Rope): Rope =
  182. result = if r == nil: r else: r & ~", "
  183. const PatProc = "$1.ClE_0? $1.ClP_0($3$1.ClE_0):(($4)($1.ClP_0))($2)"
  184. const PatIter = "$1.ClP_0($3$1.ClE_0)" # we know the env exists
  185. var op: TLoc
  186. initLocExpr(p, ri.sons[0], op)
  187. var pl: Rope
  188. var typ = skipTypes(ri.sons[0].typ, abstractInst)
  189. assert(typ.kind == tyProc)
  190. var length = sonsLen(ri)
  191. for i in countup(1, length - 1):
  192. assert(sonsLen(typ) == sonsLen(typ.n))
  193. genParamLoop(pl)
  194. template genCallPattern {.dirty.} =
  195. lineF(p, cpsStmts, callPattern & ";$n", [op.r, pl, pl.addComma, rawProc])
  196. let rawProc = getRawProcType(p, typ)
  197. let callPattern = if tfIterator in typ.flags: PatIter else: PatProc
  198. if typ.sons[0] != nil:
  199. if isInvalidReturnType(p.config, typ.sons[0]):
  200. if sonsLen(ri) > 1: add(pl, ~", ")
  201. # beware of 'result = p(result)'. We may need to allocate a temporary:
  202. if d.k in {locTemp, locNone} or not leftAppearsOnRightSide(le, ri):
  203. # Great, we can use 'd':
  204. if d.k == locNone:
  205. getTemp(p, typ.sons[0], d, needsInit=true)
  206. elif d.k notin {locTemp} and not hasNoInit(ri):
  207. # reset before pass as 'result' var:
  208. discard "resetLoc(p, d)"
  209. add(pl, addrLoc(p.config, d))
  210. genCallPattern()
  211. else:
  212. var tmp: TLoc
  213. getTemp(p, typ.sons[0], tmp, needsInit=true)
  214. add(pl, addrLoc(p.config, tmp))
  215. genCallPattern()
  216. genAssignment(p, d, tmp, {}) # no need for deep copying
  217. else:
  218. if d.k == locNone: getTemp(p, typ.sons[0], d)
  219. assert(d.t != nil) # generate an assignment to d:
  220. var list: TLoc
  221. initLoc(list, locCall, d.lode, OnUnknown)
  222. list.r = callPattern % [op.r, pl, pl.addComma, rawProc]
  223. genAssignment(p, d, list, {}) # no need for deep copying
  224. else:
  225. genCallPattern()
  226. proc genOtherArg(p: BProc; ri: PNode; i: int; typ: PType): Rope =
  227. if i < sonsLen(typ):
  228. # 'var T' is 'T&' in C++. This means we ignore the request of
  229. # any nkHiddenAddr when it's a 'var T'.
  230. let paramType = typ.n.sons[i]
  231. assert(paramType.kind == nkSym)
  232. if paramType.typ.isCompileTimeOnly:
  233. result = nil
  234. elif typ.sons[i].kind == tyVar and ri.sons[i].kind == nkHiddenAddr:
  235. result = genArgNoParam(p, ri.sons[i][0])
  236. else:
  237. result = genArgNoParam(p, ri.sons[i]) #, typ.n.sons[i].sym)
  238. else:
  239. if tfVarargs notin typ.flags:
  240. localError(p.config, ri.info, "wrong argument count")
  241. result = nil
  242. else:
  243. result = genArgNoParam(p, ri.sons[i])
  244. discard """
  245. Dot call syntax in C++
  246. ======================
  247. so c2nim translates 'this' sometimes to 'T' and sometimes to 'var T'
  248. both of which are wrong, but often more convenient to use.
  249. For manual wrappers it can also be 'ptr T'
  250. Fortunately we know which parameter is the 'this' parameter and so can fix this
  251. mess in the codegen.
  252. now ... if the *argument* is a 'ptr' the codegen shall emit -> and otherwise .
  253. but this only depends on the argument and not on how the 'this' was declared
  254. however how the 'this' was declared affects whether we end up with
  255. wrong 'addr' and '[]' ops...
  256. Since I'm tired I'll enumerate all the cases here:
  257. var
  258. x: ptr T
  259. y: T
  260. proc t(x: T)
  261. x[].t() --> (*x).t() is correct.
  262. y.t() --> y.t() is correct
  263. proc u(x: ptr T)
  264. x.u() --> needs to become x->u()
  265. (addr y).u() --> needs to become y.u()
  266. proc v(x: var T)
  267. --> first skip the implicit 'nkAddr' node
  268. x[].v() --> (*x).v() is correct, but might have been eliminated due
  269. to the nkAddr node! So for this case we need to generate '->'
  270. y.v() --> y.v() is correct
  271. """
  272. proc skipAddrDeref(node: PNode): PNode =
  273. var n = node
  274. var isAddr = false
  275. case n.kind
  276. of nkAddr, nkHiddenAddr:
  277. n = n.sons[0]
  278. isAddr = true
  279. of nkDerefExpr, nkHiddenDeref:
  280. n = n.sons[0]
  281. else: return n
  282. if n.kind == nkObjDownConv: n = n.sons[0]
  283. if isAddr and n.kind in {nkDerefExpr, nkHiddenDeref}:
  284. result = n.sons[0]
  285. elif n.kind in {nkAddr, nkHiddenAddr}:
  286. result = n.sons[0]
  287. else:
  288. result = node
  289. proc genThisArg(p: BProc; ri: PNode; i: int; typ: PType): Rope =
  290. # for better or worse c2nim translates the 'this' argument to a 'var T'.
  291. # However manual wrappers may also use 'ptr T'. In any case we support both
  292. # for convenience.
  293. internalAssert p.config, i < sonsLen(typ)
  294. assert(typ.n.sons[i].kind == nkSym)
  295. # if the parameter is lying (tyVar) and thus we required an additional deref,
  296. # skip the deref:
  297. var ri = ri[i]
  298. while ri.kind == nkObjDownConv: ri = ri[0]
  299. let t = typ.sons[i].skipTypes({tyGenericInst, tyAlias, tySink})
  300. if t.kind == tyVar:
  301. let x = if ri.kind == nkHiddenAddr: ri[0] else: ri
  302. if x.typ.kind == tyPtr:
  303. result = genArgNoParam(p, x)
  304. result.add("->")
  305. elif x.kind in {nkHiddenDeref, nkDerefExpr} and x[0].typ.kind == tyPtr:
  306. result = genArgNoParam(p, x[0])
  307. result.add("->")
  308. else:
  309. result = genArgNoParam(p, x)
  310. result.add(".")
  311. elif t.kind == tyPtr:
  312. if ri.kind in {nkAddr, nkHiddenAddr}:
  313. result = genArgNoParam(p, ri[0])
  314. result.add(".")
  315. else:
  316. result = genArgNoParam(p, ri)
  317. result.add("->")
  318. else:
  319. ri = skipAddrDeref(ri)
  320. if ri.kind in {nkAddr, nkHiddenAddr}: ri = ri[0]
  321. result = genArgNoParam(p, ri) #, typ.n.sons[i].sym)
  322. result.add(".")
  323. proc genPatternCall(p: BProc; ri: PNode; pat: string; typ: PType): Rope =
  324. var i = 0
  325. var j = 1
  326. while i < pat.len:
  327. case pat[i]
  328. of '@':
  329. if j < ri.len:
  330. result.add genOtherArg(p, ri, j, typ)
  331. for k in j+1 ..< ri.len:
  332. result.add(~", ")
  333. result.add genOtherArg(p, ri, k, typ)
  334. inc i
  335. of '#':
  336. if pat[i+1] in {'+', '@'}:
  337. let ri = ri[j]
  338. if ri.kind in nkCallKinds:
  339. let typ = skipTypes(ri.sons[0].typ, abstractInst)
  340. if pat[i+1] == '+': result.add genArgNoParam(p, ri.sons[0])
  341. result.add(~"(")
  342. if 1 < ri.len:
  343. result.add genOtherArg(p, ri, 1, typ)
  344. for k in j+1 ..< ri.len:
  345. result.add(~", ")
  346. result.add genOtherArg(p, ri, k, typ)
  347. result.add(~")")
  348. else:
  349. localError(p.config, ri.info, "call expression expected for C++ pattern")
  350. inc i
  351. elif pat[i+1] == '.':
  352. result.add genThisArg(p, ri, j, typ)
  353. inc i
  354. elif pat[i+1] == '[':
  355. var arg = ri.sons[j].skipAddrDeref
  356. while arg.kind in {nkAddr, nkHiddenAddr, nkObjDownConv}: arg = arg[0]
  357. result.add genArgNoParam(p, arg)
  358. #result.add debugTree(arg, 0, 10)
  359. else:
  360. result.add genOtherArg(p, ri, j, typ)
  361. inc j
  362. inc i
  363. of '\'':
  364. var idx, stars: int
  365. if scanCppGenericSlot(pat, i, idx, stars):
  366. var t = resolveStarsInCppType(typ, idx, stars)
  367. if t == nil: result.add(~"void")
  368. else: result.add(getTypeDesc(p.module, t))
  369. else:
  370. let start = i
  371. while i < pat.len:
  372. if pat[i] notin {'@', '#', '\''}: inc(i)
  373. else: break
  374. if i - 1 >= start:
  375. add(result, substr(pat, start, i - 1))
  376. proc genInfixCall(p: BProc, le, ri: PNode, d: var TLoc) =
  377. var op: TLoc
  378. initLocExpr(p, ri.sons[0], op)
  379. # getUniqueType() is too expensive here:
  380. var typ = skipTypes(ri.sons[0].typ, abstractInst)
  381. assert(typ.kind == tyProc)
  382. var length = sonsLen(ri)
  383. assert(sonsLen(typ) == sonsLen(typ.n))
  384. # don't call '$' here for efficiency:
  385. let pat = ri.sons[0].sym.loc.r.data
  386. internalAssert p.config, pat.len > 0
  387. if pat.contains({'#', '(', '@', '\''}):
  388. var pl = genPatternCall(p, ri, pat, typ)
  389. # simpler version of 'fixupCall' that works with the pl+params combination:
  390. var typ = skipTypes(ri.sons[0].typ, abstractInst)
  391. if typ.sons[0] != nil:
  392. if p.module.compileToCpp and lfSingleUse in d.flags:
  393. # do not generate spurious temporaries for C++! For C we're better off
  394. # with them to prevent undefined behaviour and because the codegen
  395. # is free to emit expressions multiple times!
  396. d.k = locCall
  397. d.r = pl
  398. excl d.flags, lfSingleUse
  399. else:
  400. if d.k == locNone: getTemp(p, typ.sons[0], d)
  401. assert(d.t != nil) # generate an assignment to d:
  402. var list: TLoc
  403. initLoc(list, locCall, d.lode, OnUnknown)
  404. list.r = pl
  405. genAssignment(p, d, list, {}) # no need for deep copying
  406. else:
  407. add(pl, ~";$n")
  408. line(p, cpsStmts, pl)
  409. else:
  410. var pl: Rope = nil
  411. #var param = typ.n.sons[1].sym
  412. if 1 < ri.len:
  413. add(pl, genThisArg(p, ri, 1, typ))
  414. add(pl, op.r)
  415. var params: Rope
  416. for i in countup(2, length - 1):
  417. if params != nil: params.add(~", ")
  418. assert(sonsLen(typ) == sonsLen(typ.n))
  419. add(params, genOtherArg(p, ri, i, typ))
  420. fixupCall(p, le, ri, d, pl, params)
  421. proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
  422. # generates a crappy ObjC call
  423. var op: TLoc
  424. initLocExpr(p, ri.sons[0], op)
  425. var pl = ~"["
  426. # getUniqueType() is too expensive here:
  427. var typ = skipTypes(ri.sons[0].typ, abstractInst)
  428. assert(typ.kind == tyProc)
  429. var length = sonsLen(ri)
  430. assert(sonsLen(typ) == sonsLen(typ.n))
  431. # don't call '$' here for efficiency:
  432. let pat = ri.sons[0].sym.loc.r.data
  433. internalAssert p.config, pat.len > 0
  434. var start = 3
  435. if ' ' in pat:
  436. start = 1
  437. add(pl, op.r)
  438. if length > 1:
  439. add(pl, ~": ")
  440. add(pl, genArg(p, ri.sons[1], typ.n.sons[1].sym, ri))
  441. start = 2
  442. else:
  443. if length > 1:
  444. add(pl, genArg(p, ri.sons[1], typ.n.sons[1].sym, ri))
  445. add(pl, ~" ")
  446. add(pl, op.r)
  447. if length > 2:
  448. add(pl, ~": ")
  449. add(pl, genArg(p, ri.sons[2], typ.n.sons[2].sym, ri))
  450. for i in countup(start, length-1):
  451. assert(sonsLen(typ) == sonsLen(typ.n))
  452. if i >= sonsLen(typ):
  453. internalError(p.config, ri.info, "varargs for objective C method?")
  454. assert(typ.n.sons[i].kind == nkSym)
  455. var param = typ.n.sons[i].sym
  456. add(pl, ~" ")
  457. add(pl, param.name.s)
  458. add(pl, ~": ")
  459. add(pl, genArg(p, ri.sons[i], param, ri))
  460. if typ.sons[0] != nil:
  461. if isInvalidReturnType(p.config, typ.sons[0]):
  462. if sonsLen(ri) > 1: add(pl, ~" ")
  463. # beware of 'result = p(result)'. We always allocate a temporary:
  464. if d.k in {locTemp, locNone}:
  465. # We already got a temp. Great, special case it:
  466. if d.k == locNone: getTemp(p, typ.sons[0], d, needsInit=true)
  467. add(pl, ~"Result: ")
  468. add(pl, addrLoc(p.config, d))
  469. add(pl, ~"];$n")
  470. line(p, cpsStmts, pl)
  471. else:
  472. var tmp: TLoc
  473. getTemp(p, typ.sons[0], tmp, needsInit=true)
  474. add(pl, addrLoc(p.config, tmp))
  475. add(pl, ~"];$n")
  476. line(p, cpsStmts, pl)
  477. genAssignment(p, d, tmp, {}) # no need for deep copying
  478. else:
  479. add(pl, ~"]")
  480. if d.k == locNone: getTemp(p, typ.sons[0], d)
  481. assert(d.t != nil) # generate an assignment to d:
  482. var list: TLoc
  483. initLoc(list, locCall, ri, OnUnknown)
  484. list.r = pl
  485. genAssignment(p, d, list, {}) # no need for deep copying
  486. else:
  487. add(pl, ~"];$n")
  488. line(p, cpsStmts, pl)
  489. proc genCall(p: BProc, e: PNode, d: var TLoc) =
  490. if e.sons[0].typ.skipTypes({tyGenericInst, tyAlias, tySink}).callConv == ccClosure:
  491. genClosureCall(p, nil, e, d)
  492. elif e.sons[0].kind == nkSym and sfInfixCall in e.sons[0].sym.flags:
  493. genInfixCall(p, nil, e, d)
  494. elif e.sons[0].kind == nkSym and sfNamedParamCall in e.sons[0].sym.flags:
  495. genNamedParamCall(p, e, d)
  496. else:
  497. genPrefixCall(p, nil, e, d)
  498. postStmtActions(p)
  499. proc genAsgnCall(p: BProc, le, ri: PNode, d: var TLoc) =
  500. if ri.sons[0].typ.skipTypes({tyGenericInst, tyAlias, tySink}).callConv == ccClosure:
  501. genClosureCall(p, le, ri, d)
  502. elif ri.sons[0].kind == nkSym and sfInfixCall in ri.sons[0].sym.flags:
  503. genInfixCall(p, le, ri, d)
  504. elif ri.sons[0].kind == nkSym and sfNamedParamCall in ri.sons[0].sym.flags:
  505. genNamedParamCall(p, ri, d)
  506. else:
  507. genPrefixCall(p, le, ri, d)
  508. postStmtActions(p)