macrocache.nim 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #
  2. #
  3. # Nim's Runtime Library
  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 provides an API for macros that need to collect compile
  10. ## time information across module boundaries in global variables.
  11. ## Starting with version 0.19 of Nim this is not directly supported anymore
  12. ## as it breaks incremental compilations.
  13. ## Instead the API here needs to be used.
  14. type
  15. CacheSeq* = distinct string
  16. CacheTable* = distinct string
  17. CacheCounter* = distinct string
  18. proc value*(c: CacheCounter): int {.magic: "NccValue".}
  19. proc inc*(c: CacheCounter; by = 1) {.magic: "NccInc".}
  20. proc add*(s: CacheSeq; value: NimNode) {.magic: "NcsAdd".}
  21. proc incl*(s: CacheSeq; value: NimNode) {.magic: "NcsIncl".}
  22. proc len*(s: CacheSeq): int {.magic: "NcsLen".}
  23. proc `[]`*(s: CacheSeq; i: int): NimNode {.magic: "NcsAt".}
  24. iterator items*(s: CacheSeq): NimNode =
  25. for i in 0 ..< len(s): yield s[i]
  26. proc `[]=`*(t: CacheTable; key: string, value: NimNode) {.magic: "NctPut".}
  27. ## 'key' has to be unique!
  28. proc len*(t: CacheTable): int {.magic: "NctLen".}
  29. proc `[]`*(t: CacheTable; key: string): NimNode {.magic: "NctGet".}
  30. proc hasNext(t: CacheTable; iter: int): bool {.magic: "NctHasNext".}
  31. proc next(t: CacheTable; iter: int): (string, NimNode, int) {.magic: "NctNext".}
  32. iterator pairs*(t: CacheTable): (string, NimNode) =
  33. var h = 0
  34. while hasNext(t, h):
  35. let (a, b, h2) = next(t, h)
  36. yield (a, b)
  37. h = h2