Are there any ? I think I saw some mention of them in the manual, but can't find it anymore.
Is there any API to program the compiler to generate new typedesc ?
I'm not sure exactly what you're asking, but if it's just generating new types based on compile-time parameters, then yes, templates or macros can generate new types.
Example:
template genType(name, varType:expr): stmt =
type name = object
someVar: varType
genType Foo, int
genType Bar, string
var f: Foo
var b: Bar
f.someVar = 123
b.someVar = "text"
What I mean by generating new types, is having a hook into the compiler to create new types on the fly. In your example you just pass to the template preexisting types (int and string).
Here's some pseudo code of things I would like to do:
macro TAosoa(rangeExpr: expr, T: typedesc{object}, rateExpr: expr): typedesc =
let tys = get a seq of typedesc from T fields
# create a new typedesc that would be of type object that have
# tys.length fields, each one would be of type TSoa = array[rateExpr, tys[i]]
# compute the size of the exterior array and create it of TSoa type
const size = ...
TAosoa = array[size, TSoa]
result = TAosoa
# usage
TVec3 = object
x: float32
y: float32
z: float32
var
vertices: TAosoa[8, TVec3, 4]
# the type generates this kind of layout for 8 vertices and a rate of 4
# XXXX YYYY ZZZZ XXXX YYYY ZZZZ
# \ /
# ------------
# TSoa
# \ /
# ----------------------------
# TAosoa
This kind of data structure (with TSoa) is utterly needed for efficient SIMD/SPMD memory accesses. It's painful to write that kind of code by hand.
My interest would be to implement a SPMD model as done by Intel with ispc but right away into Nimrod, as a macro pass.
stringer, such type manipulations are supposed to be done with the compile-time type traits API.
Unfortunately, the API is extremely poor at the moment: https://github.com/Araq/Nimrod/blob/master/lib/pure/typetraits.nim
On the upside, adding type traits is one of the easiest things to do within the compiler. There is only one function that has to be modified: https://github.com/Araq/Nimrod/blob/184684989b9033bdeb07d48067e5393ed53fc268/compiler/evals.nim#L911
I'd love to see more people playing with the compiler and submitting patches and this is the best place to start.
Excellent ! Thanks for the tips !
I will see what I can do :)