ccgcalls.nim 19 KB

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