macrocacheimpl.nim 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2018 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements helpers for the macro cache.
  10. import lineinfos, ast, modulegraphs, vmdef, magicsys
  11. proc recordInc*(c: PCtx; info: TLineInfo; key: string; by: BiggestInt) =
  12. var recorded = newNodeI(nkCommentStmt, info)
  13. recorded.add newStrNode("inc", info)
  14. recorded.add newStrNode(key, info)
  15. recorded.add newIntNode(nkIntLit, by)
  16. c.graph.recordStmt(c.graph, c.module, recorded)
  17. proc recordPut*(c: PCtx; info: TLineInfo; key: string; k: string; val: PNode) =
  18. var recorded = newNodeI(nkCommentStmt, info)
  19. recorded.add newStrNode("put", info)
  20. recorded.add newStrNode(key, info)
  21. recorded.add newStrNode(k, info)
  22. recorded.add copyTree(val)
  23. c.graph.recordStmt(c.graph, c.module, recorded)
  24. proc recordAdd*(c: PCtx; info: TLineInfo; key: string; val: PNode) =
  25. var recorded = newNodeI(nkCommentStmt, info)
  26. recorded.add newStrNode("add", info)
  27. recorded.add newStrNode(key, info)
  28. recorded.add copyTree(val)
  29. c.graph.recordStmt(c.graph, c.module, recorded)
  30. proc recordIncl*(c: PCtx; info: TLineInfo; key: string; val: PNode) =
  31. var recorded = newNodeI(nkCommentStmt, info)
  32. recorded.add newStrNode("incl", info)
  33. recorded.add newStrNode(key, info)
  34. recorded.add copyTree(val)
  35. c.graph.recordStmt(c.graph, c.module, recorded)
  36. when false:
  37. proc genCall3(g: ModuleGraph; m: TMagic; s: string; a, b, c: PNode): PNode =
  38. newTree(nkStaticStmt, newTree(nkCall, createMagic(g, s, m).newSymNode, a, b, c))
  39. proc genCall2(g: ModuleGraph; m: TMagic; s: string; a, b: PNode): PNode =
  40. newTree(nkStaticStmt, newTree(nkCall, createMagic(g, s, m).newSymNode, a, b))
  41. template nodeFrom(s: string): PNode =
  42. var res = newStrNode(s, info)
  43. res.typ = getSysType(g, info, tyString)
  44. res
  45. template nodeFrom(i: BiggestInt): PNode =
  46. var res = newIntNode(i, info)
  47. res.typ = getSysType(g, info, tyInt)
  48. res
  49. template nodeFrom(n: PNode): PNode = copyTree(n)
  50. template record(call) =
  51. g.recordStmt(g, c.module, call)
  52. proc recordInc*(c: PCtx; info: TLineInfo; key: string; by: BiggestInt) =
  53. let g = c.graph
  54. record genCall2(mNccInc, "inc", nodeFrom key, nodeFrom by)
  55. proc recordPut*(c: PCtx; info: TLineInfo; key: string; k: string; val: PNode) =
  56. let g = c.graph
  57. record genCall3(mNctPut, "[]=", nodeFrom key, nodeFrom k, nodeFrom val)
  58. proc recordAdd*(c: PCtx; info: TLineInfo; key: string; val: PNode) =
  59. let g = c.graph
  60. record genCall2(mNcsAdd, "add", nodeFrom key, nodeFrom val)
  61. proc recordIncl*(c: PCtx; info: TLineInfo; key: string; val: PNode) =
  62. let g = c.graph
  63. record genCall2(mNcsIncl, "incl", nodeFrom key, nodeFrom val)