macrocache.nim 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. See XXX (wikipedia page) for a
  14. ## theoretical foundation behind this.
  15. type
  16. CacheSeq* = distinct string
  17. CacheTable* = distinct string
  18. CacheCounter* = distinct string
  19. proc value*(c: CacheCounter): int {.magic: "NccValue".}
  20. proc inc*(c: CacheCounter; by = 1) {.magic: "NccInc".}
  21. proc add*(s: CacheSeq; value: NimNode) {.magic: "NcsAdd".}
  22. proc incl*(s: CacheSeq; value: NimNode) {.magic: "NcsIncl".}
  23. proc len*(s: CacheSeq): int {.magic: "NcsLen".}
  24. proc `[]`*(s: CacheSeq; i: int): NimNode {.magic: "NcsAt".}
  25. iterator items*(s: CacheSeq): NimNode =
  26. for i in 0 ..< len(s): yield s[i]
  27. proc `[]=`*(t: CacheTable; key: string, value: NimNode) {.magic: "NctPut".}
  28. ## 'key' has to be unique!
  29. proc len*(t: CacheTable): int {.magic: "NctLen".}
  30. proc `[]`*(t: CacheTable; key: string): NimNode {.magic: "NctGet".}
  31. proc hasNext(t: CacheTable; iter: int): bool {.magic: "NctHasNext".}
  32. proc next(t: CacheTable; iter: int): (string, NimNode, int) {.magic: "NctNext".}
  33. iterator pairs*(t: CacheTable): (string, NimNode) =
  34. var h = 0
  35. while hasNext(t, h):
  36. let (a, b, h2) = next(t, h)
  37. yield (a, b)
  38. h = h2