core/macrocache

  Source   Edit

This module provides an API for macros to collect compile-time information across module boundaries. It should be used instead of global {.compileTime.} variables as those break incremental compilation.

The main feature of this module is that if you create CacheTables or any other Cache types with the same name in different modules, their content will be shared, meaning that you can fill a CacheTable in one module, and iterate over its contents in another.

Example:

import core/macrocache
import std/macros

const mcTable = CacheTable"myTable"
const mcSeq = CacheSeq"mySeq"
const mcCounter = CacheCounter"myCounter"

static:
  # add new key "val" with the value `myval`
  let myval = newLit("hello ic")
  mcTable["val"] = myval
  assert mcTable["val"].kind == nnkStrLit

# Can access the same cache from different static contexts
# All the information is retained
static:
  # get value from `mcTable` and add it to `mcSeq`
  mcSeq.add(mcTable["val"])
  assert mcSeq.len == 1

static:
  assert mcSeq[0].strVal == "hello ic"

  # increase `mcCounter` by 3
  mcCounter.inc(3)
  assert mcCounter.value == 3

Iterators

iterator items(s: CacheSeq): NimNode {....raises: [], tags: [].}
Iterates over each item in s.

Example:

import std/macros
const myseq = CacheSeq"itemsTest"

static:
  myseq.add(newLit(5))
  myseq.add(newLit(42))

  for val in myseq:
    # check that all values in `myseq` are int literals
    assert val.kind == nnkIntLit
  Source   Edit
iterator pairs(t: CacheTable): (string, NimNode) {....raises: [], tags: [].}
Iterates over all (key, value) pairs in t.

Example:

import std/macros
const mytabl = CacheTable"values"

static:
  mytabl["intVal"] = newLit(5)
  mytabl["otherVal"] = newLit(6)
  for key, val in mytabl:
    # make sure that we actually get the same keys
    assert key in ["intVal", "otherVal"]

    # all vals are int literals
    assert val.kind == nnkIntLit
  Source   Edit